vfs.txt 43 KB

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