vfs.txt 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. Overview of the Linux Virtual File System
  2. Original author: Richard Gooch <rgooch@atnf.csiro.au>
  3. Last updated on June 24, 2007.
  4. Copyright (C) 1999 Richard Gooch
  5. Copyright (C) 2005 Pekka Enberg
  6. This file is released under the GPLv2.
  7. Introduction
  8. ============
  9. The Virtual File System (also known as the Virtual Filesystem Switch)
  10. is the software layer in the kernel that provides the filesystem
  11. interface to userspace programs. It also provides an abstraction
  12. within the kernel which allows different filesystem implementations to
  13. coexist.
  14. VFS system calls open(2), stat(2), read(2), write(2), chmod(2) and so
  15. on are called from a process context. Filesystem locking is described
  16. in the document Documentation/filesystems/Locking.
  17. Directory Entry Cache (dcache)
  18. ------------------------------
  19. The VFS implements the open(2), stat(2), chmod(2), and similar system
  20. calls. The pathname argument that is passed to them is used by the VFS
  21. to search through the directory entry cache (also known as the dentry
  22. cache or dcache). This provides a very fast look-up mechanism to
  23. translate a pathname (filename) into a specific dentry. Dentries live
  24. in RAM and are never saved to disc: they exist only for performance.
  25. The dentry cache is meant to be a view into your entire filespace. As
  26. most computers cannot fit all dentries in the RAM at the same time,
  27. some bits of the cache are missing. In order to resolve your pathname
  28. into a dentry, the VFS may have to resort to creating dentries along
  29. the way, and then loading the inode. This is done by looking up the
  30. inode.
  31. The Inode Object
  32. ----------------
  33. An individual dentry usually has a pointer to an inode. Inodes are
  34. filesystem objects such as regular files, directories, FIFOs and other
  35. beasts. They live either on the disc (for block device filesystems)
  36. or in the memory (for pseudo filesystems). Inodes that live on the
  37. disc are copied into the memory when required and changes to the inode
  38. are written back to disc. A single inode can be pointed to by multiple
  39. dentries (hard links, for example, do this).
  40. To look up an inode requires that the VFS calls the lookup() method of
  41. the parent directory inode. This method is installed by the specific
  42. filesystem implementation that the inode lives in. Once the VFS has
  43. the required dentry (and hence the inode), we can do all those boring
  44. things like open(2) the file, or stat(2) it to peek at the inode
  45. data. The stat(2) operation is fairly simple: once the VFS has the
  46. dentry, it peeks at the inode data and passes some of it back to
  47. userspace.
  48. The File Object
  49. ---------------
  50. Opening a file requires another operation: allocation of a file
  51. structure (this is the kernel-side implementation of file
  52. descriptors). The freshly allocated file structure is initialized with
  53. a pointer to the dentry and a set of file operation member functions.
  54. These are taken from the inode data. The open() file method is then
  55. called so the specific filesystem implementation can do it's work. You
  56. can see that this is another switch performed by the VFS. The file
  57. structure is placed into the file descriptor table for the process.
  58. Reading, writing and closing files (and other assorted VFS operations)
  59. is done by using the userspace file descriptor to grab the appropriate
  60. file structure, and then calling the required file structure method to
  61. do whatever is required. For as long as the file is open, it keeps the
  62. dentry in use, which in turn means that the VFS inode is still in use.
  63. Registering and Mounting a Filesystem
  64. =====================================
  65. To register and unregister a filesystem, use the following API
  66. functions:
  67. #include <linux/fs.h>
  68. extern int register_filesystem(struct file_system_type *);
  69. extern int unregister_filesystem(struct file_system_type *);
  70. The passed struct file_system_type describes your filesystem. When a
  71. request is made to mount a device onto a directory in your filespace,
  72. the VFS will call the appropriate get_sb() method for the specific
  73. filesystem. The dentry for the mount point will then be updated to
  74. point to the root inode for the new filesystem.
  75. You can see all filesystems that are registered to the kernel in the
  76. file /proc/filesystems.
  77. struct file_system_type
  78. -----------------------
  79. This describes the filesystem. As of kernel 2.6.22, the following
  80. members are defined:
  81. struct file_system_type {
  82. const char *name;
  83. int fs_flags;
  84. int (*get_sb) (struct file_system_type *, int,
  85. const char *, void *, struct vfsmount *);
  86. void (*kill_sb) (struct super_block *);
  87. struct module *owner;
  88. struct file_system_type * next;
  89. struct list_head fs_supers;
  90. struct lock_class_key s_lock_key;
  91. struct lock_class_key s_umount_key;
  92. };
  93. name: the name of the filesystem type, such as "ext2", "iso9660",
  94. "msdos" and so on
  95. fs_flags: various flags (i.e. FS_REQUIRES_DEV, FS_NO_DCACHE, etc.)
  96. get_sb: the method to call when a new instance of this
  97. filesystem should be mounted
  98. kill_sb: the method to call when an instance of this filesystem
  99. should be unmounted
  100. owner: for internal VFS use: you should initialize this to THIS_MODULE in
  101. most cases.
  102. next: for internal VFS use: you should initialize this to NULL
  103. s_lock_key, s_umount_key: lockdep-specific
  104. The get_sb() method has the following arguments:
  105. struct file_system_type *fs_type: decribes the filesystem, partly initialized
  106. by the specific filesystem code
  107. int flags: mount flags
  108. const char *dev_name: the device name we are mounting.
  109. void *data: arbitrary mount options, usually comes as an ASCII
  110. string
  111. struct vfsmount *mnt: a vfs-internal representation of a mount point
  112. The get_sb() method must determine if the block device specified
  113. in the dev_name and fs_type contains a filesystem of the type the method
  114. supports. If it succeeds in opening the named block device, it initializes a
  115. struct super_block descriptor for the filesystem contained by the block device.
  116. On failure it returns an error.
  117. The most interesting member of the superblock structure that the
  118. get_sb() method fills in is the "s_op" field. This is a pointer to
  119. a "struct super_operations" which describes the next level of the
  120. filesystem implementation.
  121. Usually, a filesystem uses one of the generic get_sb() implementations
  122. and provides a fill_super() method instead. The generic methods are:
  123. get_sb_bdev: mount a filesystem residing on a block device
  124. get_sb_nodev: mount a filesystem that is not backed by a device
  125. get_sb_single: mount a filesystem which shares the instance between
  126. all mounts
  127. A fill_super() method implementation has the following arguments:
  128. struct super_block *sb: the superblock structure. The method fill_super()
  129. must initialize this properly.
  130. void *data: arbitrary mount options, usually comes as an ASCII
  131. string
  132. int silent: whether or not to be silent on error
  133. The Superblock Object
  134. =====================
  135. A superblock object represents a mounted filesystem.
  136. struct super_operations
  137. -----------------------
  138. This describes how the VFS can manipulate the superblock of your
  139. filesystem. As of kernel 2.6.22, the following members are defined:
  140. struct super_operations {
  141. struct inode *(*alloc_inode)(struct super_block *sb);
  142. void (*destroy_inode)(struct inode *);
  143. void (*read_inode) (struct inode *);
  144. void (*dirty_inode) (struct inode *);
  145. int (*write_inode) (struct inode *, int);
  146. void (*put_inode) (struct inode *);
  147. void (*drop_inode) (struct inode *);
  148. void (*delete_inode) (struct inode *);
  149. void (*put_super) (struct super_block *);
  150. void (*write_super) (struct super_block *);
  151. int (*sync_fs)(struct super_block *sb, int wait);
  152. void (*write_super_lockfs) (struct super_block *);
  153. void (*unlockfs) (struct super_block *);
  154. int (*statfs) (struct dentry *, struct kstatfs *);
  155. int (*remount_fs) (struct super_block *, int *, char *);
  156. void (*clear_inode) (struct inode *);
  157. void (*umount_begin) (struct super_block *);
  158. int (*show_options)(struct seq_file *, struct vfsmount *);
  159. ssize_t (*quota_read)(struct super_block *, int, char *, size_t, loff_t);
  160. ssize_t (*quota_write)(struct super_block *, int, const char *, size_t, loff_t);
  161. };
  162. All methods are called without any locks being held, unless otherwise
  163. noted. This means that most methods can block safely. All methods are
  164. only called from a process context (i.e. not from an interrupt handler
  165. or bottom half).
  166. alloc_inode: this method is called by inode_alloc() to allocate memory
  167. for struct inode and initialize it. If this function is not
  168. defined, a simple 'struct inode' is allocated. Normally
  169. alloc_inode will be used to allocate a larger structure which
  170. contains a 'struct inode' embedded within it.
  171. destroy_inode: this method is called by destroy_inode() to release
  172. resources allocated for struct inode. It is only required if
  173. ->alloc_inode was defined and simply undoes anything done by
  174. ->alloc_inode.
  175. read_inode: this method is called to read a specific inode from the
  176. mounted filesystem. The i_ino member in the struct inode is
  177. initialized by the VFS to indicate which inode to read. Other
  178. members are filled in by this method.
  179. You can set this to NULL and use iget5_locked() instead of iget()
  180. to read inodes. This is necessary for filesystems for which the
  181. inode number is not sufficient to identify an inode.
  182. dirty_inode: this method is called by the VFS to mark an inode dirty.
  183. write_inode: this method is called when the VFS needs to write an
  184. inode to disc. The second parameter indicates whether the write
  185. should be synchronous or not, not all filesystems check this flag.
  186. put_inode: called when the VFS inode is removed from the inode
  187. cache.
  188. drop_inode: called when the last access to the inode is dropped,
  189. with the inode_lock spinlock held.
  190. This method should be either NULL (normal UNIX filesystem
  191. semantics) or "generic_delete_inode" (for filesystems that do not
  192. want to cache inodes - causing "delete_inode" to always be
  193. called regardless of the value of i_nlink)
  194. The "generic_delete_inode()" behavior is equivalent to the
  195. old practice of using "force_delete" in the put_inode() case,
  196. but does not have the races that the "force_delete()" approach
  197. had.
  198. delete_inode: called when the VFS wants to delete an inode
  199. put_super: called when the VFS wishes to free the superblock
  200. (i.e. unmount). This is called with the superblock lock held
  201. write_super: called when the VFS superblock needs to be written to
  202. disc. This method is optional
  203. sync_fs: called when VFS is writing out all dirty data associated with
  204. a superblock. The second parameter indicates whether the method
  205. should wait until the write out has been completed. Optional.
  206. write_super_lockfs: called when VFS is locking a filesystem and
  207. forcing it into a consistent state. This method is currently
  208. used by the Logical Volume Manager (LVM).
  209. unlockfs: called when VFS is unlocking a filesystem and making it writable
  210. again.
  211. statfs: called when the VFS needs to get filesystem statistics. This
  212. is called with the kernel lock held
  213. remount_fs: called when the filesystem is remounted. This is called
  214. with the kernel lock held
  215. clear_inode: called then the VFS clears the inode. Optional
  216. umount_begin: called when the VFS is unmounting a filesystem.
  217. show_options: called by the VFS to show mount options for /proc/<pid>/mounts.
  218. quota_read: called by the VFS to read from filesystem quota file.
  219. quota_write: called by the VFS to write to filesystem quota file.
  220. The read_inode() method is responsible for filling in the "i_op"
  221. field. This is a pointer to a "struct inode_operations" which
  222. describes the methods that can be performed on individual inodes.
  223. The Inode Object
  224. ================
  225. An inode object represents an object within the filesystem.
  226. struct inode_operations
  227. -----------------------
  228. This describes how the VFS can manipulate an inode in your
  229. filesystem. As of kernel 2.6.22, the following members are defined:
  230. struct inode_operations {
  231. int (*create) (struct inode *,struct dentry *,int, struct nameidata *);
  232. struct dentry * (*lookup) (struct inode *,struct dentry *, struct nameidata *);
  233. int (*link) (struct dentry *,struct inode *,struct dentry *);
  234. int (*unlink) (struct inode *,struct dentry *);
  235. int (*symlink) (struct inode *,struct dentry *,const char *);
  236. int (*mkdir) (struct inode *,struct dentry *,int);
  237. int (*rmdir) (struct inode *,struct dentry *);
  238. int (*mknod) (struct inode *,struct dentry *,int,dev_t);
  239. int (*rename) (struct inode *, struct dentry *,
  240. struct inode *, struct dentry *);
  241. int (*readlink) (struct dentry *, char __user *,int);
  242. void * (*follow_link) (struct dentry *, struct nameidata *);
  243. void (*put_link) (struct dentry *, struct nameidata *, void *);
  244. void (*truncate) (struct inode *);
  245. int (*permission) (struct inode *, int, struct nameidata *);
  246. int (*setattr) (struct dentry *, struct iattr *);
  247. int (*getattr) (struct vfsmount *mnt, struct dentry *, struct kstat *);
  248. int (*setxattr) (struct dentry *, const char *,const void *,size_t,int);
  249. ssize_t (*getxattr) (struct dentry *, const char *, void *, size_t);
  250. ssize_t (*listxattr) (struct dentry *, char *, size_t);
  251. int (*removexattr) (struct dentry *, const char *);
  252. void (*truncate_range)(struct inode *, loff_t, loff_t);
  253. };
  254. Again, all methods are called without any locks being held, unless
  255. otherwise noted.
  256. create: called by the open(2) and creat(2) system calls. Only
  257. required if you want to support regular files. The dentry you
  258. get should not have an inode (i.e. it should be a negative
  259. dentry). Here you will probably call d_instantiate() with the
  260. dentry and the newly created inode
  261. lookup: called when the VFS needs to look up an inode in a parent
  262. directory. The name to look for is found in the dentry. This
  263. method must call d_add() to insert the found inode into the
  264. dentry. The "i_count" field in the inode structure should be
  265. incremented. If the named inode does not exist a NULL inode
  266. should be inserted into the dentry (this is called a negative
  267. dentry). Returning an error code from this routine must only
  268. be done on a real error, otherwise creating inodes with system
  269. calls like create(2), mknod(2), mkdir(2) and so on will fail.
  270. If you wish to overload the dentry methods then you should
  271. initialise the "d_dop" field in the dentry; this is a pointer
  272. to a struct "dentry_operations".
  273. This method is called with the directory inode semaphore held
  274. link: called by the link(2) system call. Only required if you want
  275. to support hard links. You will probably need to call
  276. d_instantiate() just as you would in the create() method
  277. unlink: called by the unlink(2) system call. Only required if you
  278. want to support deleting inodes
  279. symlink: called by the symlink(2) system call. Only required if you
  280. want to support symlinks. You will probably need to call
  281. d_instantiate() just as you would in the create() method
  282. mkdir: called by the mkdir(2) system call. Only required if you want
  283. to support creating subdirectories. You will probably need to
  284. call d_instantiate() just as you would in the create() method
  285. rmdir: called by the rmdir(2) system call. Only required if you want
  286. to support deleting subdirectories
  287. mknod: called by the mknod(2) system call to create a device (char,
  288. block) inode or a named pipe (FIFO) or socket. Only required
  289. if you want to support creating these types of inodes. You
  290. will probably need to call d_instantiate() just as you would
  291. in the create() method
  292. rename: called by the rename(2) system call to rename the object to
  293. have the parent and name given by the second inode and dentry.
  294. readlink: called by the readlink(2) system call. Only required if
  295. you want to support reading symbolic links
  296. follow_link: called by the VFS to follow a symbolic link to the
  297. inode it points to. Only required if you want to support
  298. symbolic links. This method returns a void pointer cookie
  299. that is passed to put_link().
  300. put_link: called by the VFS to release resources allocated by
  301. follow_link(). The cookie returned by follow_link() is passed
  302. to this method as the last parameter. It is used by
  303. filesystems such as NFS where page cache is not stable
  304. (i.e. page that was installed when the symbolic link walk
  305. started might not be in the page cache at the end of the
  306. walk).
  307. truncate: called by the VFS to change the size of a file. The
  308. i_size field of the inode is set to the desired size by the
  309. VFS before this method is called. This method is called by
  310. the truncate(2) system call and related functionality.
  311. permission: called by the VFS to check for access rights on a POSIX-like
  312. filesystem.
  313. setattr: called by the VFS to set attributes for a file. This method
  314. is called by chmod(2) and related system calls.
  315. getattr: called by the VFS to get attributes of a file. This method
  316. is called by stat(2) and related system calls.
  317. setxattr: called by the VFS to set an extended attribute for a file.
  318. Extended attribute is a name:value pair associated with an
  319. inode. This method is called by setxattr(2) system call.
  320. getxattr: called by the VFS to retrieve the value of an extended
  321. attribute name. This method is called by getxattr(2) function
  322. call.
  323. listxattr: called by the VFS to list all extended attributes for a
  324. given file. This method is called by listxattr(2) system call.
  325. removexattr: called by the VFS to remove an extended attribute from
  326. a file. This method is called by removexattr(2) system call.
  327. truncate_range: a method provided by the underlying filesystem to truncate a
  328. range of blocks , i.e. punch a hole somewhere in a file.
  329. The Address Space Object
  330. ========================
  331. The address space object is used to group and manage pages in the page
  332. cache. It can be used to keep track of the pages in a file (or
  333. anything else) and also track the mapping of sections of the file into
  334. process address spaces.
  335. There are a number of distinct yet related services that an
  336. address-space can provide. These include communicating memory
  337. pressure, page lookup by address, and keeping track of pages tagged as
  338. Dirty or Writeback.
  339. The first can be used independently to the others. The VM can try to
  340. either write dirty pages in order to clean them, or release clean
  341. pages in order to reuse them. To do this it can call the ->writepage
  342. method on dirty pages, and ->releasepage on clean pages with
  343. PagePrivate set. Clean pages without PagePrivate and with no external
  344. references will be released without notice being given to the
  345. address_space.
  346. To achieve this functionality, pages need to be placed on an LRU with
  347. lru_cache_add and mark_page_active needs to be called whenever the
  348. page is used.
  349. Pages are normally kept in a radix tree index by ->index. This tree
  350. maintains information about the PG_Dirty and PG_Writeback status of
  351. each page, so that pages with either of these flags can be found
  352. quickly.
  353. The Dirty tag is primarily used by mpage_writepages - the default
  354. ->writepages method. It uses the tag to find dirty pages to call
  355. ->writepage on. If mpage_writepages is not used (i.e. the address
  356. provides its own ->writepages) , the PAGECACHE_TAG_DIRTY tag is
  357. almost unused. write_inode_now and sync_inode do use it (through
  358. __sync_single_inode) to check if ->writepages has been successful in
  359. writing out the whole address_space.
  360. The Writeback tag is used by filemap*wait* and sync_page* functions,
  361. via wait_on_page_writeback_range, to wait for all writeback to
  362. complete. While waiting ->sync_page (if defined) will be called on
  363. each page that is found to require writeback.
  364. An address_space handler may attach extra information to a page,
  365. typically using the 'private' field in the 'struct page'. If such
  366. information is attached, the PG_Private flag should be set. This will
  367. cause various VM routines to make extra calls into the address_space
  368. handler to deal with that data.
  369. An address space acts as an intermediate between storage and
  370. application. Data is read into the address space a whole page at a
  371. time, and provided to the application either by copying of the page,
  372. or by memory-mapping the page.
  373. Data is written into the address space by the application, and then
  374. written-back to storage typically in whole pages, however the
  375. address_space has finer control of write sizes.
  376. The read process essentially only requires 'readpage'. The write
  377. process is more complicated and uses prepare_write/commit_write or
  378. set_page_dirty to write data into the address_space, and writepage,
  379. sync_page, and writepages to writeback data to storage.
  380. Adding and removing pages to/from an address_space is protected by the
  381. inode's i_mutex.
  382. When data is written to a page, the PG_Dirty flag should be set. It
  383. typically remains set until writepage asks for it to be written. This
  384. should clear PG_Dirty and set PG_Writeback. It can be actually
  385. written at any point after PG_Dirty is clear. Once it is known to be
  386. safe, PG_Writeback is cleared.
  387. Writeback makes use of a writeback_control structure...
  388. struct address_space_operations
  389. -------------------------------
  390. This describes how the VFS can manipulate mapping of a file to page cache in
  391. your filesystem. As of kernel 2.6.22, the following members are defined:
  392. struct address_space_operations {
  393. int (*writepage)(struct page *page, struct writeback_control *wbc);
  394. int (*readpage)(struct file *, struct page *);
  395. int (*sync_page)(struct page *);
  396. int (*writepages)(struct address_space *, struct writeback_control *);
  397. int (*set_page_dirty)(struct page *page);
  398. int (*readpages)(struct file *filp, struct address_space *mapping,
  399. struct list_head *pages, unsigned nr_pages);
  400. int (*prepare_write)(struct file *, struct page *, unsigned, unsigned);
  401. int (*commit_write)(struct file *, struct page *, unsigned, unsigned);
  402. int (*write_begin)(struct file *, struct address_space *mapping,
  403. loff_t pos, unsigned len, unsigned flags,
  404. struct page **pagep, void **fsdata);
  405. int (*write_end)(struct file *, struct address_space *mapping,
  406. loff_t pos, unsigned len, unsigned copied,
  407. struct page *page, void *fsdata);
  408. sector_t (*bmap)(struct address_space *, sector_t);
  409. int (*invalidatepage) (struct page *, unsigned long);
  410. int (*releasepage) (struct page *, int);
  411. ssize_t (*direct_IO)(int, struct kiocb *, const struct iovec *iov,
  412. loff_t offset, unsigned long nr_segs);
  413. struct page* (*get_xip_page)(struct address_space *, sector_t,
  414. int);
  415. /* migrate the contents of a page to the specified target */
  416. int (*migratepage) (struct page *, struct page *);
  417. int (*launder_page) (struct page *);
  418. };
  419. writepage: called by the VM to write a dirty page to backing store.
  420. This may happen for data integrity reasons (i.e. 'sync'), or
  421. to free up memory (flush). The difference can be seen in
  422. wbc->sync_mode.
  423. The PG_Dirty flag has been cleared and PageLocked is true.
  424. writepage should start writeout, should set PG_Writeback,
  425. and should make sure the page is unlocked, either synchronously
  426. or asynchronously when the write operation completes.
  427. If wbc->sync_mode is WB_SYNC_NONE, ->writepage doesn't have to
  428. try too hard if there are problems, and may choose to write out
  429. other pages from the mapping if that is easier (e.g. due to
  430. internal dependencies). If it chooses not to start writeout, it
  431. should return AOP_WRITEPAGE_ACTIVATE so that the VM will not keep
  432. calling ->writepage on that page.
  433. See the file "Locking" for more details.
  434. readpage: called by the VM to read a page from backing store.
  435. The page will be Locked when readpage is called, and should be
  436. unlocked and marked uptodate once the read completes.
  437. If ->readpage discovers that it needs to unlock the page for
  438. some reason, it can do so, and then return AOP_TRUNCATED_PAGE.
  439. In this case, the page will be relocated, relocked and if
  440. that all succeeds, ->readpage will be called again.
  441. sync_page: called by the VM to notify the backing store to perform all
  442. queued I/O operations for a page. I/O operations for other pages
  443. associated with this address_space object may also be performed.
  444. This function is optional and is called only for pages with
  445. PG_Writeback set while waiting for the writeback to complete.
  446. writepages: called by the VM to write out pages associated with the
  447. address_space object. If wbc->sync_mode is WBC_SYNC_ALL, then
  448. the writeback_control will specify a range of pages that must be
  449. written out. If it is WBC_SYNC_NONE, then a nr_to_write is given
  450. and that many pages should be written if possible.
  451. If no ->writepages is given, then mpage_writepages is used
  452. instead. This will choose pages from the address space that are
  453. tagged as DIRTY and will pass them to ->writepage.
  454. set_page_dirty: called by the VM to set a page dirty.
  455. This is particularly needed if an address space attaches
  456. private data to a page, and that data needs to be updated when
  457. a page is dirtied. This is called, for example, when a memory
  458. mapped page gets modified.
  459. If defined, it should set the PageDirty flag, and the
  460. PAGECACHE_TAG_DIRTY tag in the radix tree.
  461. readpages: called by the VM to read pages associated with the address_space
  462. object. This is essentially just a vector version of
  463. readpage. Instead of just one page, several pages are
  464. requested.
  465. readpages is only used for read-ahead, so read errors are
  466. ignored. If anything goes wrong, feel free to give up.
  467. prepare_write: called by the generic write path in VM to set up a write
  468. request for a page. This indicates to the address space that
  469. the given range of bytes is about to be written. The
  470. address_space should check that the write will be able to
  471. complete, by allocating space if necessary and doing any other
  472. internal housekeeping. If the write will update parts of
  473. any basic-blocks on storage, then those blocks should be
  474. pre-read (if they haven't been read already) so that the
  475. updated blocks can be written out properly.
  476. The page will be locked.
  477. Note: the page _must not_ be marked uptodate in this function
  478. (or anywhere else) unless it actually is uptodate right now. As
  479. soon as a page is marked uptodate, it is possible for a concurrent
  480. read(2) to copy it to userspace.
  481. commit_write: If prepare_write succeeds, new data will be copied
  482. into the page and then commit_write will be called. It will
  483. typically update the size of the file (if appropriate) and
  484. mark the inode as dirty, and do any other related housekeeping
  485. operations. It should avoid returning an error if possible -
  486. errors should have been handled by prepare_write.
  487. write_begin: This is intended as a replacement for prepare_write. The
  488. key differences being that:
  489. - it returns a locked page (in *pagep) rather than being
  490. given a pre locked page;
  491. - it must be able to cope with short writes (where the
  492. length passed to write_begin is greater than the number
  493. of bytes copied into the page).
  494. Called by the generic buffered write code to ask the filesystem to
  495. prepare to write len bytes at the given offset in the file. The
  496. address_space should check that the write will be able to complete,
  497. by allocating space if necessary and doing any other internal
  498. housekeeping. If the write will update parts of any basic-blocks on
  499. storage, then those blocks should be pre-read (if they haven't been
  500. read already) so that the updated blocks can be written out properly.
  501. The filesystem must return the locked pagecache page for the specified
  502. offset, in *pagep, for the caller to write into.
  503. flags is a field for AOP_FLAG_xxx flags, described in
  504. include/linux/fs.h.
  505. A void * may be returned in fsdata, which then gets passed into
  506. write_end.
  507. Returns 0 on success; < 0 on failure (which is the error code), in
  508. which case write_end is not called.
  509. write_end: After a successful write_begin, and data copy, write_end must
  510. be called. len is the original len passed to write_begin, and copied
  511. is the amount that was able to be copied (copied == len is always true
  512. if write_begin was called with the AOP_FLAG_UNINTERRUPTIBLE flag).
  513. The filesystem must take care of unlocking the page and releasing it
  514. refcount, and updating i_size.
  515. Returns < 0 on failure, otherwise the number of bytes (<= 'copied')
  516. that were able to be copied into pagecache.
  517. bmap: called by the VFS to map a logical block offset within object to
  518. physical block number. This method is used by the FIBMAP
  519. ioctl and for working with swap-files. To be able to swap to
  520. a file, the file must have a stable mapping to a block
  521. device. The swap system does not go through the filesystem
  522. but instead uses bmap to find out where the blocks in the file
  523. are and uses those addresses directly.
  524. invalidatepage: If a page has PagePrivate set, then invalidatepage
  525. will be called when part or all of the page is to be removed
  526. from the address space. This generally corresponds to either a
  527. truncation or a complete invalidation of the address space
  528. (in the latter case 'offset' will always be 0).
  529. Any private data associated with the page should be updated
  530. to reflect this truncation. If offset is 0, then
  531. the private data should be released, because the page
  532. must be able to be completely discarded. This may be done by
  533. calling the ->releasepage function, but in this case the
  534. release MUST succeed.
  535. releasepage: releasepage is called on PagePrivate pages to indicate
  536. that the page should be freed if possible. ->releasepage
  537. should remove any private data from the page and clear the
  538. PagePrivate flag. It may also remove the page from the
  539. address_space. If this fails for some reason, it may indicate
  540. failure with a 0 return value.
  541. This is used in two distinct though related cases. The first
  542. is when the VM finds a clean page with no active users and
  543. wants to make it a free page. If ->releasepage succeeds, the
  544. page will be removed from the address_space and become free.
  545. The second case is when a request has been made to invalidate
  546. some or all pages in an address_space. This can happen
  547. through the fadvice(POSIX_FADV_DONTNEED) system call or by the
  548. filesystem explicitly requesting it as nfs and 9fs do (when
  549. they believe the cache may be out of date with storage) by
  550. calling invalidate_inode_pages2().
  551. If the filesystem makes such a call, and needs to be certain
  552. that all pages are invalidated, then its releasepage will
  553. need to ensure this. Possibly it can clear the PageUptodate
  554. bit if it cannot free private data yet.
  555. direct_IO: called by the generic read/write routines to perform
  556. direct_IO - that is IO requests which bypass the page cache
  557. and transfer data directly between the storage and the
  558. application's address space.
  559. get_xip_page: called by the VM to translate a block number to a page.
  560. The page is valid until the corresponding filesystem is unmounted.
  561. Filesystems that want to use execute-in-place (XIP) need to implement
  562. it. An example implementation can be found in fs/ext2/xip.c.
  563. migrate_page: This is used to compact the physical memory usage.
  564. If the VM wants to relocate a page (maybe off a memory card
  565. that is signalling imminent failure) it will pass a new page
  566. and an old page to this function. migrate_page should
  567. transfer any private data across and update any references
  568. that it has to the page.
  569. launder_page: Called before freeing a page - it writes back the dirty page. To
  570. prevent redirtying the page, it is kept locked during the whole
  571. operation.
  572. The File Object
  573. ===============
  574. A file object represents a file opened by a process.
  575. struct file_operations
  576. ----------------------
  577. This describes how the VFS can manipulate an open file. As of kernel
  578. 2.6.22, the following members are defined:
  579. struct file_operations {
  580. struct module *owner;
  581. loff_t (*llseek) (struct file *, loff_t, int);
  582. ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
  583. ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
  584. ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
  585. ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
  586. int (*readdir) (struct file *, void *, filldir_t);
  587. unsigned int (*poll) (struct file *, struct poll_table_struct *);
  588. int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
  589. long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
  590. long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
  591. int (*mmap) (struct file *, struct vm_area_struct *);
  592. int (*open) (struct inode *, struct file *);
  593. int (*flush) (struct file *);
  594. int (*release) (struct inode *, struct file *);
  595. int (*fsync) (struct file *, struct dentry *, int datasync);
  596. int (*aio_fsync) (struct kiocb *, int datasync);
  597. int (*fasync) (int, struct file *, int);
  598. int (*lock) (struct file *, int, struct file_lock *);
  599. ssize_t (*readv) (struct file *, const struct iovec *, unsigned long, loff_t *);
  600. ssize_t (*writev) (struct file *, const struct iovec *, unsigned long, loff_t *);
  601. ssize_t (*sendfile) (struct file *, loff_t *, size_t, read_actor_t, void *);
  602. ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
  603. unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
  604. int (*check_flags)(int);
  605. int (*dir_notify)(struct file *filp, unsigned long arg);
  606. int (*flock) (struct file *, int, struct file_lock *);
  607. ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, size_t, unsigned int);
  608. ssize_t (*splice_read)(struct file *, struct pipe_inode_info *, size_t, unsigned int);
  609. };
  610. Again, all methods are called without any locks being held, unless
  611. otherwise noted.
  612. llseek: called when the VFS needs to move the file position index
  613. read: called by read(2) and related system calls
  614. aio_read: called by io_submit(2) and other asynchronous I/O operations
  615. write: called by write(2) and related system calls
  616. aio_write: called by io_submit(2) and other asynchronous I/O operations
  617. readdir: called when the VFS needs to read the directory contents
  618. poll: called by the VFS when a process wants to check if there is
  619. activity on this file and (optionally) go to sleep until there
  620. is activity. Called by the select(2) and poll(2) system calls
  621. ioctl: called by the ioctl(2) system call
  622. unlocked_ioctl: called by the ioctl(2) system call. Filesystems that do not
  623. require the BKL should use this method instead of the ioctl() above.
  624. compat_ioctl: called by the ioctl(2) system call when 32 bit system calls
  625. are used on 64 bit kernels.
  626. mmap: called by the mmap(2) system call
  627. open: called by the VFS when an inode should be opened. When the VFS
  628. opens a file, it creates a new "struct file". It then calls the
  629. open method for the newly allocated file structure. You might
  630. think that the open method really belongs in
  631. "struct inode_operations", and you may be right. I think it's
  632. done the way it is because it makes filesystems simpler to
  633. implement. The open() method is a good place to initialize the
  634. "private_data" member in the file structure if you want to point
  635. to a device structure
  636. flush: called by the close(2) system call to flush a file
  637. release: called when the last reference to an open file is closed
  638. fsync: called by the fsync(2) system call
  639. fasync: called by the fcntl(2) system call when asynchronous
  640. (non-blocking) mode is enabled for a file
  641. lock: called by the fcntl(2) system call for F_GETLK, F_SETLK, and F_SETLKW
  642. commands
  643. readv: called by the readv(2) system call
  644. writev: called by the writev(2) system call
  645. sendfile: called by the sendfile(2) system call
  646. get_unmapped_area: called by the mmap(2) system call
  647. check_flags: called by the fcntl(2) system call for F_SETFL command
  648. dir_notify: called by the fcntl(2) system call for F_NOTIFY command
  649. flock: called by the flock(2) system call
  650. splice_write: called by the VFS to splice data from a pipe to a file. This
  651. method is used by the splice(2) system call
  652. splice_read: called by the VFS to splice data from file to a pipe. This
  653. method is used by the splice(2) system call
  654. Note that the file operations are implemented by the specific
  655. filesystem in which the inode resides. When opening a device node
  656. (character or block special) most filesystems will call special
  657. support routines in the VFS which will locate the required device
  658. driver information. These support routines replace the filesystem file
  659. operations with those for the device driver, and then proceed to call
  660. the new open() method for the file. This is how opening a device file
  661. in the filesystem eventually ends up calling the device driver open()
  662. method.
  663. Directory Entry Cache (dcache)
  664. ==============================
  665. struct dentry_operations
  666. ------------------------
  667. This describes how a filesystem can overload the standard dentry
  668. operations. Dentries and the dcache are the domain of the VFS and the
  669. individual filesystem implementations. Device drivers have no business
  670. here. These methods may be set to NULL, as they are either optional or
  671. the VFS uses a default. As of kernel 2.6.22, the following members are
  672. defined:
  673. struct dentry_operations {
  674. int (*d_revalidate)(struct dentry *, struct nameidata *);
  675. int (*d_hash) (struct dentry *, struct qstr *);
  676. int (*d_compare) (struct dentry *, struct qstr *, struct qstr *);
  677. int (*d_delete)(struct dentry *);
  678. void (*d_release)(struct dentry *);
  679. void (*d_iput)(struct dentry *, struct inode *);
  680. char *(*d_dname)(struct dentry *, char *, int);
  681. };
  682. d_revalidate: called when the VFS needs to revalidate a dentry. This
  683. is called whenever a name look-up finds a dentry in the
  684. dcache. Most filesystems leave this as NULL, because all their
  685. dentries in the dcache are valid
  686. d_hash: called when the VFS adds a dentry to the hash table
  687. d_compare: called when a dentry should be compared with another
  688. d_delete: called when the last reference to a dentry is
  689. deleted. This means no-one is using the dentry, however it is
  690. still valid and in the dcache
  691. d_release: called when a dentry is really deallocated
  692. d_iput: called when a dentry loses its inode (just prior to its
  693. being deallocated). The default when this is NULL is that the
  694. VFS calls iput(). If you define this method, you must call
  695. iput() yourself
  696. d_dname: called when the pathname of a dentry should be generated.
  697. Usefull for some pseudo filesystems (sockfs, pipefs, ...) to delay
  698. pathname generation. (Instead of doing it when dentry is created,
  699. its done only when the path is needed.). Real filesystems probably
  700. dont want to use it, because their dentries are present in global
  701. dcache hash, so their hash should be an invariant. As no lock is
  702. held, d_dname() should not try to modify the dentry itself, unless
  703. appropriate SMP safety is used. CAUTION : d_path() logic is quite
  704. tricky. The correct way to return for example "Hello" is to put it
  705. at the end of the buffer, and returns a pointer to the first char.
  706. dynamic_dname() helper function is provided to take care of this.
  707. Example :
  708. static char *pipefs_dname(struct dentry *dent, char *buffer, int buflen)
  709. {
  710. return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
  711. dentry->d_inode->i_ino);
  712. }
  713. Each dentry has a pointer to its parent dentry, as well as a hash list
  714. of child dentries. Child dentries are basically like files in a
  715. directory.
  716. Directory Entry Cache API
  717. --------------------------
  718. There are a number of functions defined which permit a filesystem to
  719. manipulate dentries:
  720. dget: open a new handle for an existing dentry (this just increments
  721. the usage count)
  722. dput: close a handle for a dentry (decrements the usage count). If
  723. the usage count drops to 0, the "d_delete" method is called
  724. and the dentry is placed on the unused list if the dentry is
  725. still in its parents hash list. Putting the dentry on the
  726. unused list just means that if the system needs some RAM, it
  727. goes through the unused list of dentries and deallocates them.
  728. If the dentry has already been unhashed and the usage count
  729. drops to 0, in this case the dentry is deallocated after the
  730. "d_delete" method is called
  731. d_drop: this unhashes a dentry from its parents hash list. A
  732. subsequent call to dput() will deallocate the dentry if its
  733. usage count drops to 0
  734. d_delete: delete a dentry. If there are no other open references to
  735. the dentry then the dentry is turned into a negative dentry
  736. (the d_iput() method is called). If there are other
  737. references, then d_drop() is called instead
  738. d_add: add a dentry to its parents hash list and then calls
  739. d_instantiate()
  740. d_instantiate: add a dentry to the alias hash list for the inode and
  741. updates the "d_inode" member. The "i_count" member in the
  742. inode structure should be set/incremented. If the inode
  743. pointer is NULL, the dentry is called a "negative
  744. dentry". This function is commonly called when an inode is
  745. created for an existing negative dentry
  746. d_lookup: look up a dentry given its parent and path name component
  747. It looks up the child of that given name from the dcache
  748. hash table. If it is found, the reference count is incremented
  749. and the dentry is returned. The caller must use d_put()
  750. to free the dentry when it finishes using it.
  751. For further information on dentry locking, please refer to the document
  752. Documentation/filesystems/dentry-locking.txt.
  753. Resources
  754. =========
  755. (Note some of these resources are not up-to-date with the latest kernel
  756. version.)
  757. Creating Linux virtual filesystems. 2002
  758. <http://lwn.net/Articles/13325/>
  759. The Linux Virtual File-system Layer by Neil Brown. 1999
  760. <http://www.cse.unsw.edu.au/~neilb/oss/linux-commentary/vfs.html>
  761. A tour of the Linux VFS by Michael K. Johnson. 1996
  762. <http://www.tldp.org/LDP/khg/HyperNews/get/fs/vfstour.html>
  763. A small trail through the Linux kernel by Andries Brouwer. 2001
  764. <http://www.win.tue.nl/~aeb/linux/vfs/trail.html>