vfs.txt 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. Overview of the Linux Virtual File System
  2. Original author: Richard Gooch <rgooch@atnf.csiro.au>
  3. Last updated on August 25, 2005
  4. Copyright (C) 1999 Richard Gooch
  5. Copyright (C) 2005 Pekka Enberg
  6. This file is released under the GPLv2.
  7. What is it?
  8. ===========
  9. The Virtual File System (otherwise known as the Virtual Filesystem
  10. Switch) is the software layer in the kernel that provides the
  11. filesystem interface to userspace programs. It also provides an
  12. abstraction within the kernel which allows different filesystem
  13. implementations to coexist.
  14. A Quick Look At How It Works
  15. ============================
  16. In this section I'll briefly describe how things work, before
  17. launching into the details. I'll start with describing what happens
  18. when user programs open and manipulate files, and then look from the
  19. other view which is how a filesystem is supported and subsequently
  20. mounted.
  21. Opening a File
  22. --------------
  23. The VFS implements the open(2), stat(2), chmod(2) and similar system
  24. calls. The pathname argument is used by the VFS to search through the
  25. directory entry cache (dentry cache or "dcache"). This provides a very
  26. fast look-up mechanism to translate a pathname (filename) into a
  27. specific dentry.
  28. An individual dentry usually has a pointer to an inode. Inodes are the
  29. things that live on disc drives, and can be regular files (you know:
  30. those things that you write data into), directories, FIFOs and other
  31. beasts. Dentries live in RAM and are never saved to disc: they exist
  32. only for performance. Inodes live on disc and are copied into memory
  33. when required. Later any changes are written back to disc. The inode
  34. that lives in RAM is a VFS inode, and it is this which the dentry
  35. points to. A single inode can be pointed to by multiple dentries
  36. (think about hardlinks).
  37. The dcache is meant to be a view into your entire filespace. Unlike
  38. Linus, most of us losers can't fit enough dentries into RAM to cover
  39. all of our filespace, so the dcache has bits missing. In order to
  40. resolve your pathname into a dentry, the VFS may have to resort to
  41. creating dentries along the way, and then loading the inode. This is
  42. done by looking up the inode.
  43. To look up an inode (usually read from disc) requires that the VFS
  44. calls the lookup() method of the parent directory inode. This method
  45. is installed by the specific filesystem implementation that the inode
  46. lives in. There will be more on this later.
  47. Once the VFS has the required dentry (and hence the inode), we can do
  48. all those boring things like open(2) the file, or stat(2) it to peek
  49. at the inode data. The stat(2) operation is fairly simple: once the
  50. VFS has the dentry, it peeks at the inode data and passes some of it
  51. back to userspace.
  52. Opening a file requires another operation: allocation of a file
  53. structure (this is the kernel-side implementation of file
  54. descriptors). The freshly allocated file structure is initialized with
  55. a pointer to the dentry and a set of file operation member functions.
  56. These are taken from the inode data. The open() file method is then
  57. called so the specific filesystem implementation can do it's work. You
  58. can see that this is another switch performed by the VFS.
  59. The file structure is placed into the file descriptor table for the
  60. process.
  61. Reading, writing and closing files (and other assorted VFS operations)
  62. is done by using the userspace file descriptor to grab the appropriate
  63. file structure, and then calling the required file structure method
  64. function to do whatever is required.
  65. For as long as the file is open, it keeps the dentry "open" (in use),
  66. which in turn means that the VFS inode is still in use.
  67. All VFS system calls (i.e. open(2), stat(2), read(2), write(2),
  68. chmod(2) and so on) are called from a process context. You should
  69. assume that these calls are made without any kernel locks being
  70. held. This means that the processes may be executing the same piece of
  71. filesystem or driver code at the same time, on different
  72. processors. You should ensure that access to shared resources is
  73. protected by appropriate locks.
  74. Registering and Mounting a Filesystem
  75. -------------------------------------
  76. If you want to support a new kind of filesystem in the kernel, all you
  77. need to do is call register_filesystem(). You pass a structure
  78. describing the filesystem implementation (struct file_system_type)
  79. which is then added to an internal table of supported filesystems. You
  80. can do:
  81. % cat /proc/filesystems
  82. to see what filesystems are currently available on your system.
  83. When a request is made to mount a block device onto a directory in
  84. your filespace the VFS will call the appropriate method for the
  85. specific filesystem. The dentry for the mount point will then be
  86. updated to point to the root inode for the new filesystem.
  87. It's now time to look at things in more detail.
  88. struct file_system_type
  89. =======================
  90. This describes the filesystem. As of kernel 2.6.13, the following
  91. members are defined:
  92. struct file_system_type {
  93. const char *name;
  94. int fs_flags;
  95. struct super_block *(*get_sb) (struct file_system_type *, int,
  96. const char *, void *);
  97. void (*kill_sb) (struct super_block *);
  98. struct module *owner;
  99. struct file_system_type * next;
  100. struct list_head fs_supers;
  101. };
  102. name: the name of the filesystem type, such as "ext2", "iso9660",
  103. "msdos" and so on
  104. fs_flags: various flags (i.e. FS_REQUIRES_DEV, FS_NO_DCACHE, etc.)
  105. get_sb: the method to call when a new instance of this
  106. filesystem should be mounted
  107. kill_sb: the method to call when an instance of this filesystem
  108. should be unmounted
  109. owner: for internal VFS use: you should initialize this to THIS_MODULE in
  110. most cases.
  111. next: for internal VFS use: you should initialize this to NULL
  112. The get_sb() method has the following arguments:
  113. struct super_block *sb: the superblock structure. This is partially
  114. initialized by the VFS and the rest must be initialized by the
  115. get_sb() method
  116. int flags: mount flags
  117. const char *dev_name: the device name we are mounting.
  118. void *data: arbitrary mount options, usually comes as an ASCII
  119. string
  120. int silent: whether or not to be silent on error
  121. The get_sb() method must determine if the block device specified
  122. in the superblock contains a filesystem of the type the method
  123. supports. On success the method returns the superblock pointer, on
  124. failure it returns NULL.
  125. The most interesting member of the superblock structure that the
  126. get_sb() method fills in is the "s_op" field. This is a pointer to
  127. a "struct super_operations" which describes the next level of the
  128. filesystem implementation.
  129. Usually, a filesystem uses generic one of the generic get_sb()
  130. implementations and provides a fill_super() method instead. The
  131. generic methods are:
  132. get_sb_bdev: mount a filesystem residing on a block device
  133. get_sb_nodev: mount a filesystem that is not backed by a device
  134. get_sb_single: mount a filesystem which shares the instance between
  135. all mounts
  136. A fill_super() method implementation has the following arguments:
  137. struct super_block *sb: the superblock structure. The method fill_super()
  138. must initialize this properly.
  139. void *data: arbitrary mount options, usually comes as an ASCII
  140. string
  141. int silent: whether or not to be silent on error
  142. struct super_operations
  143. =======================
  144. This describes how the VFS can manipulate the superblock of your
  145. filesystem. As of kernel 2.6.13, the following members are defined:
  146. struct super_operations {
  147. struct inode *(*alloc_inode)(struct super_block *sb);
  148. void (*destroy_inode)(struct inode *);
  149. void (*read_inode) (struct inode *);
  150. void (*dirty_inode) (struct inode *);
  151. int (*write_inode) (struct inode *, int);
  152. void (*put_inode) (struct inode *);
  153. void (*drop_inode) (struct inode *);
  154. void (*delete_inode) (struct inode *);
  155. void (*put_super) (struct super_block *);
  156. void (*write_super) (struct super_block *);
  157. int (*sync_fs)(struct super_block *sb, int wait);
  158. void (*write_super_lockfs) (struct super_block *);
  159. void (*unlockfs) (struct super_block *);
  160. int (*statfs) (struct super_block *, struct kstatfs *);
  161. int (*remount_fs) (struct super_block *, int *, char *);
  162. void (*clear_inode) (struct inode *);
  163. void (*umount_begin) (struct super_block *);
  164. void (*sync_inodes) (struct super_block *sb,
  165. struct writeback_control *wbc);
  166. int (*show_options)(struct seq_file *, struct vfsmount *);
  167. ssize_t (*quota_read)(struct super_block *, int, char *, size_t, loff_t);
  168. ssize_t (*quota_write)(struct super_block *, int, const char *, size_t, loff_t);
  169. };
  170. All methods are called without any locks being held, unless otherwise
  171. noted. This means that most methods can block safely. All methods are
  172. only called from a process context (i.e. not from an interrupt handler
  173. or bottom half).
  174. alloc_inode: this method is called by inode_alloc() to allocate memory
  175. for struct inode and initialize it.
  176. destroy_inode: this method is called by destroy_inode() to release
  177. resources allocated for struct inode.
  178. read_inode: this method is called to read a specific inode from the
  179. mounted filesystem. The i_ino member in the struct inode is
  180. initialized by the VFS to indicate which inode to read. Other
  181. members are filled in by this method.
  182. You can set this to NULL and use iget5_locked() instead of iget()
  183. to read inodes. This is necessary for filesystems for which the
  184. inode number is not sufficient to identify an inode.
  185. dirty_inode: this method is called by the VFS to mark an inode dirty.
  186. write_inode: this method is called when the VFS needs to write an
  187. inode to disc. The second parameter indicates whether the write
  188. should be synchronous or not, not all filesystems check this flag.
  189. put_inode: called when the VFS inode is removed from the inode
  190. cache.
  191. drop_inode: called when the last access to the inode is dropped,
  192. with the inode_lock spinlock held.
  193. This method should be either NULL (normal UNIX filesystem
  194. semantics) or "generic_delete_inode" (for filesystems that do not
  195. want to cache inodes - causing "delete_inode" to always be
  196. called regardless of the value of i_nlink)
  197. The "generic_delete_inode()" behavior is equivalent to the
  198. old practice of using "force_delete" in the put_inode() case,
  199. but does not have the races that the "force_delete()" approach
  200. had.
  201. delete_inode: called when the VFS wants to delete an inode
  202. put_super: called when the VFS wishes to free the superblock
  203. (i.e. unmount). This is called with the superblock lock held
  204. write_super: called when the VFS superblock needs to be written to
  205. disc. This method is optional
  206. sync_fs: called when VFS is writing out all dirty data associated with
  207. a superblock. The second parameter indicates whether the method
  208. should wait until the write out has been completed. Optional.
  209. write_super_lockfs: called when VFS is locking a filesystem and forcing
  210. it into a consistent state. This function is currently used by the
  211. Logical Volume Manager (LVM).
  212. unlockfs: called when VFS is unlocking a filesystem and making it writable
  213. again.
  214. statfs: called when the VFS needs to get filesystem statistics. This
  215. is called with the kernel lock held
  216. remount_fs: called when the filesystem is remounted. This is called
  217. with the kernel lock held
  218. clear_inode: called then the VFS clears the inode. Optional
  219. umount_begin: called when the VFS is unmounting a filesystem.
  220. sync_inodes: called when the VFS is writing out dirty data associated with
  221. a superblock.
  222. show_options: called by the VFS to show mount options for /proc/<pid>/mounts.
  223. quota_read: called by the VFS to read from filesystem quota file.
  224. quota_write: called by the VFS to write to filesystem quota file.
  225. The read_inode() method is responsible for filling in the "i_op"
  226. field. This is a pointer to a "struct inode_operations" which
  227. describes the methods that can be performed on individual inodes.
  228. struct inode_operations
  229. =======================
  230. This describes how the VFS can manipulate an inode in your
  231. filesystem. As of kernel 2.6.13, the following members are defined:
  232. struct inode_operations {
  233. int (*create) (struct inode *,struct dentry *,int, struct nameidata *);
  234. struct dentry * (*lookup) (struct inode *,struct dentry *, struct nameidata *);
  235. int (*link) (struct dentry *,struct inode *,struct dentry *);
  236. int (*unlink) (struct inode *,struct dentry *);
  237. int (*symlink) (struct inode *,struct dentry *,const char *);
  238. int (*mkdir) (struct inode *,struct dentry *,int);
  239. int (*rmdir) (struct inode *,struct dentry *);
  240. int (*mknod) (struct inode *,struct dentry *,int,dev_t);
  241. int (*rename) (struct inode *, struct dentry *,
  242. struct inode *, struct dentry *);
  243. int (*readlink) (struct dentry *, char __user *,int);
  244. void * (*follow_link) (struct dentry *, struct nameidata *);
  245. void (*put_link) (struct dentry *, struct nameidata *, void *);
  246. void (*truncate) (struct inode *);
  247. int (*permission) (struct inode *, int, struct nameidata *);
  248. int (*setattr) (struct dentry *, struct iattr *);
  249. int (*getattr) (struct vfsmount *mnt, struct dentry *, struct kstat *);
  250. int (*setxattr) (struct dentry *, const char *,const void *,size_t,int);
  251. ssize_t (*getxattr) (struct dentry *, const char *, void *, size_t);
  252. ssize_t (*listxattr) (struct dentry *, char *, size_t);
  253. int (*removexattr) (struct dentry *, const char *);
  254. };
  255. Again, all methods are called without any locks being held, unless
  256. otherwise noted.
  257. create: called by the open(2) and creat(2) system calls. Only
  258. required if you want to support regular files. The dentry you
  259. get should not have an inode (i.e. it should be a negative
  260. dentry). Here you will probably call d_instantiate() with the
  261. dentry and the newly created inode
  262. lookup: called when the VFS needs to look up an inode in a parent
  263. directory. The name to look for is found in the dentry. This
  264. method must call d_add() to insert the found inode into the
  265. dentry. The "i_count" field in the inode structure should be
  266. incremented. If the named inode does not exist a NULL inode
  267. should be inserted into the dentry (this is called a negative
  268. dentry). Returning an error code from this routine must only
  269. be done on a real error, otherwise creating inodes with system
  270. calls like create(2), mknod(2), mkdir(2) and so on will fail.
  271. If you wish to overload the dentry methods then you should
  272. initialise the "d_dop" field in the dentry; this is a pointer
  273. to a struct "dentry_operations".
  274. This method is called with the directory inode semaphore held
  275. link: called by the link(2) system call. Only required if you want
  276. to support hard links. You will probably need to call
  277. d_instantiate() just as you would in the create() method
  278. unlink: called by the unlink(2) system call. Only required if you
  279. want to support deleting inodes
  280. symlink: called by the symlink(2) system call. Only required if you
  281. want to support symlinks. You will probably need to call
  282. d_instantiate() just as you would in the create() method
  283. mkdir: called by the mkdir(2) system call. Only required if you want
  284. to support creating subdirectories. You will probably need to
  285. call d_instantiate() just as you would in the create() method
  286. rmdir: called by the rmdir(2) system call. Only required if you want
  287. to support deleting subdirectories
  288. mknod: called by the mknod(2) system call to create a device (char,
  289. block) inode or a named pipe (FIFO) or socket. Only required
  290. if you want to support creating these types of inodes. You
  291. will probably need to call d_instantiate() just as you would
  292. in the create() method
  293. readlink: called by the readlink(2) system call. Only required if
  294. you want to support reading symbolic links
  295. follow_link: called by the VFS to follow a symbolic link to the
  296. inode it points to. Only required if you want to support
  297. symbolic links. This function returns a void pointer cookie
  298. that is passed to put_link().
  299. put_link: called by the VFS to release resources allocated by
  300. follow_link(). The cookie returned by follow_link() is passed to
  301. to this function as the last parameter. It is used by filesystems
  302. such as NFS where page cache is not stable (i.e. page that was
  303. installed when the symbolic link walk started might not be in the
  304. page cache at the end of the walk).
  305. truncate: called by the VFS to change the size of a file. The i_size
  306. field of the inode is set to the desired size by the VFS before
  307. this function is called. This function is called by the truncate(2)
  308. system call and related functionality.
  309. permission: called by the VFS to check for access rights on a POSIX-like
  310. filesystem.
  311. setattr: called by the VFS to set attributes for a file. This function is
  312. called by chmod(2) and related system calls.
  313. getattr: called by the VFS to get attributes of a file. This function is
  314. called by stat(2) and related system calls.
  315. setxattr: called by the VFS to set an extended attribute for a file.
  316. Extended attribute is a name:value pair associated with an inode. This
  317. function is called by setxattr(2) system call.
  318. getxattr: called by the VFS to retrieve the value of an extended attribute
  319. name. This function is called by getxattr(2) function call.
  320. listxattr: called by the VFS to list all extended attributes for a given
  321. file. This function is called by listxattr(2) system call.
  322. removexattr: called by the VFS to remove an extended attribute from a file.
  323. This function is called by removexattr(2) system call.
  324. struct address_space_operations
  325. ===============================
  326. This describes how the VFS can manipulate mapping of a file to page cache in
  327. your filesystem. As of kernel 2.6.13, the following members are defined:
  328. struct address_space_operations {
  329. int (*writepage)(struct page *page, struct writeback_control *wbc);
  330. int (*readpage)(struct file *, struct page *);
  331. int (*sync_page)(struct page *);
  332. int (*writepages)(struct address_space *, struct writeback_control *);
  333. int (*set_page_dirty)(struct page *page);
  334. int (*readpages)(struct file *filp, struct address_space *mapping,
  335. struct list_head *pages, unsigned nr_pages);
  336. int (*prepare_write)(struct file *, struct page *, unsigned, unsigned);
  337. int (*commit_write)(struct file *, struct page *, unsigned, unsigned);
  338. sector_t (*bmap)(struct address_space *, sector_t);
  339. int (*invalidatepage) (struct page *, unsigned long);
  340. int (*releasepage) (struct page *, int);
  341. ssize_t (*direct_IO)(int, struct kiocb *, const struct iovec *iov,
  342. loff_t offset, unsigned long nr_segs);
  343. struct page* (*get_xip_page)(struct address_space *, sector_t,
  344. int);
  345. };
  346. writepage: called by the VM write a dirty page to backing store.
  347. readpage: called by the VM to read a page from backing store.
  348. sync_page: called by the VM to notify the backing store to perform all
  349. queued I/O operations for a page. I/O operations for other pages
  350. associated with this address_space object may also be performed.
  351. writepages: called by the VM to write out pages associated with the
  352. address_space object.
  353. set_page_dirty: called by the VM to set a page dirty.
  354. readpages: called by the VM to read pages associated with the address_space
  355. object.
  356. prepare_write: called by the generic write path in VM to set up a write
  357. request for a page.
  358. commit_write: called by the generic write path in VM to write page to
  359. its backing store.
  360. bmap: called by the VFS to map a logical block offset within object to
  361. physical block number. This method is use by for the legacy FIBMAP
  362. ioctl. Other uses are discouraged.
  363. invalidatepage: called by the VM on truncate to disassociate a page from its
  364. address_space mapping.
  365. releasepage: called by the VFS to release filesystem specific metadata from
  366. a page.
  367. direct_IO: called by the VM for direct I/O writes and reads.
  368. get_xip_page: called by the VM to translate a block number to a page.
  369. The page is valid until the corresponding filesystem is unmounted.
  370. Filesystems that want to use execute-in-place (XIP) need to implement
  371. it. An example implementation can be found in fs/ext2/xip.c.
  372. struct file_operations
  373. ======================
  374. This describes how the VFS can manipulate an open file. As of kernel
  375. 2.6.13, the following members are defined:
  376. struct file_operations {
  377. loff_t (*llseek) (struct file *, loff_t, int);
  378. ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
  379. ssize_t (*aio_read) (struct kiocb *, char __user *, size_t, loff_t);
  380. ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
  381. ssize_t (*aio_write) (struct kiocb *, const char __user *, size_t, loff_t);
  382. int (*readdir) (struct file *, void *, filldir_t);
  383. unsigned int (*poll) (struct file *, struct poll_table_struct *);
  384. int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
  385. long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
  386. long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
  387. int (*mmap) (struct file *, struct vm_area_struct *);
  388. int (*open) (struct inode *, struct file *);
  389. int (*flush) (struct file *);
  390. int (*release) (struct inode *, struct file *);
  391. int (*fsync) (struct file *, struct dentry *, int datasync);
  392. int (*aio_fsync) (struct kiocb *, int datasync);
  393. int (*fasync) (int, struct file *, int);
  394. int (*lock) (struct file *, int, struct file_lock *);
  395. ssize_t (*readv) (struct file *, const struct iovec *, unsigned long, loff_t *);
  396. ssize_t (*writev) (struct file *, const struct iovec *, unsigned long, loff_t *);
  397. ssize_t (*sendfile) (struct file *, loff_t *, size_t, read_actor_t, void *);
  398. ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
  399. unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
  400. int (*check_flags)(int);
  401. int (*dir_notify)(struct file *filp, unsigned long arg);
  402. int (*flock) (struct file *, int, struct file_lock *);
  403. };
  404. Again, all methods are called without any locks being held, unless
  405. otherwise noted.
  406. llseek: called when the VFS needs to move the file position index
  407. read: called by read(2) and related system calls
  408. aio_read: called by io_submit(2) and other asynchronous I/O operations
  409. write: called by write(2) and related system calls
  410. aio_write: called by io_submit(2) and other asynchronous I/O operations
  411. readdir: called when the VFS needs to read the directory contents
  412. poll: called by the VFS when a process wants to check if there is
  413. activity on this file and (optionally) go to sleep until there
  414. is activity. Called by the select(2) and poll(2) system calls
  415. ioctl: called by the ioctl(2) system call
  416. unlocked_ioctl: called by the ioctl(2) system call. Filesystems that do not
  417. require the BKL should use this method instead of the ioctl() above.
  418. compat_ioctl: called by the ioctl(2) system call when 32 bit system calls
  419. are used on 64 bit kernels.
  420. mmap: called by the mmap(2) system call
  421. open: called by the VFS when an inode should be opened. When the VFS
  422. opens a file, it creates a new "struct file". It then calls the
  423. open method for the newly allocated file structure. You might
  424. think that the open method really belongs in
  425. "struct inode_operations", and you may be right. I think it's
  426. done the way it is because it makes filesystems simpler to
  427. implement. The open() method is a good place to initialize the
  428. "private_data" member in the file structure if you want to point
  429. to a device structure
  430. flush: called by the close(2) system call to flush a file
  431. release: called when the last reference to an open file is closed
  432. fsync: called by the fsync(2) system call
  433. fasync: called by the fcntl(2) system call when asynchronous
  434. (non-blocking) mode is enabled for a file
  435. lock: called by the fcntl(2) system call for F_GETLK, F_SETLK, and F_SETLKW
  436. commands
  437. readv: called by the readv(2) system call
  438. writev: called by the writev(2) system call
  439. sendfile: called by the sendfile(2) system call
  440. get_unmapped_area: called by the mmap(2) system call
  441. check_flags: called by the fcntl(2) system call for F_SETFL command
  442. dir_notify: called by the fcntl(2) system call for F_NOTIFY command
  443. flock: called by the flock(2) system call
  444. Note that the file operations are implemented by the specific
  445. filesystem in which the inode resides. When opening a device node
  446. (character or block special) most filesystems will call special
  447. support routines in the VFS which will locate the required device
  448. driver information. These support routines replace the filesystem file
  449. operations with those for the device driver, and then proceed to call
  450. the new open() method for the file. This is how opening a device file
  451. in the filesystem eventually ends up calling the device driver open()
  452. method.
  453. Directory Entry Cache (dcache)
  454. ==============================
  455. struct dentry_operations
  456. ------------------------
  457. This describes how a filesystem can overload the standard dentry
  458. operations. Dentries and the dcache are the domain of the VFS and the
  459. individual filesystem implementations. Device drivers have no business
  460. here. These methods may be set to NULL, as they are either optional or
  461. the VFS uses a default. As of kernel 2.6.13, the following members are
  462. defined:
  463. struct dentry_operations {
  464. int (*d_revalidate)(struct dentry *, struct nameidata *);
  465. int (*d_hash) (struct dentry *, struct qstr *);
  466. int (*d_compare) (struct dentry *, struct qstr *, struct qstr *);
  467. int (*d_delete)(struct dentry *);
  468. void (*d_release)(struct dentry *);
  469. void (*d_iput)(struct dentry *, struct inode *);
  470. };
  471. d_revalidate: called when the VFS needs to revalidate a dentry. This
  472. is called whenever a name look-up finds a dentry in the
  473. dcache. Most filesystems leave this as NULL, because all their
  474. dentries in the dcache are valid
  475. d_hash: called when the VFS adds a dentry to the hash table
  476. d_compare: called when a dentry should be compared with another
  477. d_delete: called when the last reference to a dentry is
  478. deleted. This means no-one is using the dentry, however it is
  479. still valid and in the dcache
  480. d_release: called when a dentry is really deallocated
  481. d_iput: called when a dentry loses its inode (just prior to its
  482. being deallocated). The default when this is NULL is that the
  483. VFS calls iput(). If you define this method, you must call
  484. iput() yourself
  485. Each dentry has a pointer to its parent dentry, as well as a hash list
  486. of child dentries. Child dentries are basically like files in a
  487. directory.
  488. Directory Entry Cache APIs
  489. --------------------------
  490. There are a number of functions defined which permit a filesystem to
  491. manipulate dentries:
  492. dget: open a new handle for an existing dentry (this just increments
  493. the usage count)
  494. dput: close a handle for a dentry (decrements the usage count). If
  495. the usage count drops to 0, the "d_delete" method is called
  496. and the dentry is placed on the unused list if the dentry is
  497. still in its parents hash list. Putting the dentry on the
  498. unused list just means that if the system needs some RAM, it
  499. goes through the unused list of dentries and deallocates them.
  500. If the dentry has already been unhashed and the usage count
  501. drops to 0, in this case the dentry is deallocated after the
  502. "d_delete" method is called
  503. d_drop: this unhashes a dentry from its parents hash list. A
  504. subsequent call to dput() will deallocate the dentry if its
  505. usage count drops to 0
  506. d_delete: delete a dentry. If there are no other open references to
  507. the dentry then the dentry is turned into a negative dentry
  508. (the d_iput() method is called). If there are other
  509. references, then d_drop() is called instead
  510. d_add: add a dentry to its parents hash list and then calls
  511. d_instantiate()
  512. d_instantiate: add a dentry to the alias hash list for the inode and
  513. updates the "d_inode" member. The "i_count" member in the
  514. inode structure should be set/incremented. If the inode
  515. pointer is NULL, the dentry is called a "negative
  516. dentry". This function is commonly called when an inode is
  517. created for an existing negative dentry
  518. d_lookup: look up a dentry given its parent and path name component
  519. It looks up the child of that given name from the dcache
  520. hash table. If it is found, the reference count is incremented
  521. and the dentry is returned. The caller must use d_put()
  522. to free the dentry when it finishes using it.
  523. RCU-based dcache locking model
  524. ------------------------------
  525. On many workloads, the most common operation on dcache is
  526. to look up a dentry, given a parent dentry and the name
  527. of the child. Typically, for every open(), stat() etc.,
  528. the dentry corresponding to the pathname will be looked
  529. up by walking the tree starting with the first component
  530. of the pathname and using that dentry along with the next
  531. component to look up the next level and so on. Since it
  532. is a frequent operation for workloads like multiuser
  533. environments and web servers, it is important to optimize
  534. this path.
  535. Prior to 2.5.10, dcache_lock was acquired in d_lookup and thus
  536. in every component during path look-up. Since 2.5.10 onwards,
  537. fast-walk algorithm changed this by holding the dcache_lock
  538. at the beginning and walking as many cached path component
  539. dentries as possible. This significantly decreases the number
  540. of acquisition of dcache_lock. However it also increases the
  541. lock hold time significantly and affects performance in large
  542. SMP machines. Since 2.5.62 kernel, dcache has been using
  543. a new locking model that uses RCU to make dcache look-up
  544. lock-free.
  545. The current dcache locking model is not very different from the existing
  546. dcache locking model. Prior to 2.5.62 kernel, dcache_lock
  547. protected the hash chain, d_child, d_alias, d_lru lists as well
  548. as d_inode and several other things like mount look-up. RCU-based
  549. changes affect only the way the hash chain is protected. For everything
  550. else the dcache_lock must be taken for both traversing as well as
  551. updating. The hash chain updates too take the dcache_lock.
  552. The significant change is the way d_lookup traverses the hash chain,
  553. it doesn't acquire the dcache_lock for this and rely on RCU to
  554. ensure that the dentry has not been *freed*.
  555. Dcache locking details
  556. ----------------------
  557. For many multi-user workloads, open() and stat() on files are
  558. very frequently occurring operations. Both involve walking
  559. of path names to find the dentry corresponding to the
  560. concerned file. In 2.4 kernel, dcache_lock was held
  561. during look-up of each path component. Contention and
  562. cache-line bouncing of this global lock caused significant
  563. scalability problems. With the introduction of RCU
  564. in Linux kernel, this was worked around by making
  565. the look-up of path components during path walking lock-free.
  566. Safe lock-free look-up of dcache hash table
  567. ===========================================
  568. Dcache is a complex data structure with the hash table entries
  569. also linked together in other lists. In 2.4 kernel, dcache_lock
  570. protected all the lists. We applied RCU only on hash chain
  571. walking. The rest of the lists are still protected by dcache_lock.
  572. Some of the important changes are :
  573. 1. The deletion from hash chain is done using hlist_del_rcu() macro which
  574. doesn't initialize next pointer of the deleted dentry and this
  575. allows us to walk safely lock-free while a deletion is happening.
  576. 2. Insertion of a dentry into the hash table is done using
  577. hlist_add_head_rcu() which take care of ordering the writes -
  578. the writes to the dentry must be visible before the dentry
  579. is inserted. This works in conjunction with hlist_for_each_rcu()
  580. while walking the hash chain. The only requirement is that
  581. all initialization to the dentry must be done before hlist_add_head_rcu()
  582. since we don't have dcache_lock protection while traversing
  583. the hash chain. This isn't different from the existing code.
  584. 3. The dentry looked up without holding dcache_lock by cannot be
  585. returned for walking if it is unhashed. It then may have a NULL
  586. d_inode or other bogosity since RCU doesn't protect the other
  587. fields in the dentry. We therefore use a flag DCACHE_UNHASHED to
  588. indicate unhashed dentries and use this in conjunction with a
  589. per-dentry lock (d_lock). Once looked up without the dcache_lock,
  590. we acquire the per-dentry lock (d_lock) and check if the
  591. dentry is unhashed. If so, the look-up is failed. If not, the
  592. reference count of the dentry is increased and the dentry is returned.
  593. 4. Once a dentry is looked up, it must be ensured during the path
  594. walk for that component it doesn't go away. In pre-2.5.10 code,
  595. this was done holding a reference to the dentry. dcache_rcu does
  596. the same. In some sense, dcache_rcu path walking looks like
  597. the pre-2.5.10 version.
  598. 5. All dentry hash chain updates must take the dcache_lock as well as
  599. the per-dentry lock in that order. dput() does this to ensure
  600. that a dentry that has just been looked up in another CPU
  601. doesn't get deleted before dget() can be done on it.
  602. 6. There are several ways to do reference counting of RCU protected
  603. objects. One such example is in ipv4 route cache where
  604. deferred freeing (using call_rcu()) is done as soon as
  605. the reference count goes to zero. This cannot be done in
  606. the case of dentries because tearing down of dentries
  607. require blocking (dentry_iput()) which isn't supported from
  608. RCU callbacks. Instead, tearing down of dentries happen
  609. synchronously in dput(), but actual freeing happens later
  610. when RCU grace period is over. This allows safe lock-free
  611. walking of the hash chains, but a matched dentry may have
  612. been partially torn down. The checking of DCACHE_UNHASHED
  613. flag with d_lock held detects such dentries and prevents
  614. them from being returned from look-up.
  615. Maintaining POSIX rename semantics
  616. ==================================
  617. Since look-up of dentries is lock-free, it can race against
  618. a concurrent rename operation. For example, during rename
  619. of file A to B, look-up of either A or B must succeed.
  620. So, if look-up of B happens after A has been removed from the
  621. hash chain but not added to the new hash chain, it may fail.
  622. Also, a comparison while the name is being written concurrently
  623. by a rename may result in false positive matches violating
  624. rename semantics. Issues related to race with rename are
  625. handled as described below :
  626. 1. Look-up can be done in two ways - d_lookup() which is safe
  627. from simultaneous renames and __d_lookup() which is not.
  628. If __d_lookup() fails, it must be followed up by a d_lookup()
  629. to correctly determine whether a dentry is in the hash table
  630. or not. d_lookup() protects look-ups using a sequence
  631. lock (rename_lock).
  632. 2. The name associated with a dentry (d_name) may be changed if
  633. a rename is allowed to happen simultaneously. To avoid memcmp()
  634. in __d_lookup() go out of bounds due to a rename and false
  635. positive comparison, the name comparison is done while holding the
  636. per-dentry lock. This prevents concurrent renames during this
  637. operation.
  638. 3. Hash table walking during look-up may move to a different bucket as
  639. the current dentry is moved to a different bucket due to rename.
  640. But we use hlists in dcache hash table and they are null-terminated.
  641. So, even if a dentry moves to a different bucket, hash chain
  642. walk will terminate. [with a list_head list, it may not since
  643. termination is when the list_head in the original bucket is reached].
  644. Since we redo the d_parent check and compare name while holding
  645. d_lock, lock-free look-up will not race against d_move().
  646. 4. There can be a theoretical race when a dentry keeps coming back
  647. to original bucket due to double moves. Due to this look-up may
  648. consider that it has never moved and can end up in a infinite loop.
  649. But this is not any worse that theoretical livelocks we already
  650. have in the kernel.
  651. Important guidelines for filesystem developers related to dcache_rcu
  652. ====================================================================
  653. 1. Existing dcache interfaces (pre-2.5.62) exported to filesystem
  654. don't change. Only dcache internal implementation changes. However
  655. filesystems *must not* delete from the dentry hash chains directly
  656. using the list macros like allowed earlier. They must use dcache
  657. APIs like d_drop() or __d_drop() depending on the situation.
  658. 2. d_flags is now protected by a per-dentry lock (d_lock). All
  659. access to d_flags must be protected by it.
  660. 3. For a hashed dentry, checking of d_count needs to be protected
  661. by d_lock.
  662. Papers and other documentation on dcache locking
  663. ================================================
  664. 1. Scaling dcache with RCU (http://linuxjournal.com/article.php?sid=7124).
  665. 2. http://lse.sourceforge.net/locking/dcache/dcache.html