vfs.txt 41 KB

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