vfs.txt 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. Overview of the Linux Virtual File System
  2. Original author: Richard Gooch <rgooch@atnf.csiro.au>
  3. Last updated on October 28, 2005
  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.13, the following
  80. members are defined:
  81. struct file_system_type {
  82. const char *name;
  83. int fs_flags;
  84. struct super_block *(*get_sb) (struct file_system_type *, int,
  85. const char *, void *);
  86. void (*kill_sb) (struct super_block *);
  87. struct module *owner;
  88. struct file_system_type * next;
  89. struct list_head fs_supers;
  90. };
  91. name: the name of the filesystem type, such as "ext2", "iso9660",
  92. "msdos" and so on
  93. fs_flags: various flags (i.e. FS_REQUIRES_DEV, FS_NO_DCACHE, etc.)
  94. get_sb: the method to call when a new instance of this
  95. filesystem should be mounted
  96. kill_sb: the method to call when an instance of this filesystem
  97. should be unmounted
  98. owner: for internal VFS use: you should initialize this to THIS_MODULE in
  99. most cases.
  100. next: for internal VFS use: you should initialize this to NULL
  101. The get_sb() method has the following arguments:
  102. struct super_block *sb: the superblock structure. This is partially
  103. initialized by the VFS and the rest must be initialized by the
  104. get_sb() method
  105. int flags: mount flags
  106. const char *dev_name: the device name we are mounting.
  107. void *data: arbitrary mount options, usually comes as an ASCII
  108. string
  109. int silent: whether or not to be silent on error
  110. The get_sb() method must determine if the block device specified
  111. in the superblock contains a filesystem of the type the method
  112. supports. On success the method returns the superblock pointer, on
  113. failure it returns NULL.
  114. The most interesting member of the superblock structure that the
  115. get_sb() method fills in is the "s_op" field. This is a pointer to
  116. a "struct super_operations" which describes the next level of the
  117. filesystem implementation.
  118. Usually, a filesystem uses one of the generic get_sb() implementations
  119. and provides a fill_super() method instead. The generic methods are:
  120. get_sb_bdev: mount a filesystem residing on a block device
  121. get_sb_nodev: mount a filesystem that is not backed by a device
  122. get_sb_single: mount a filesystem which shares the instance between
  123. all mounts
  124. A fill_super() method implementation has the following arguments:
  125. struct super_block *sb: the superblock structure. The method fill_super()
  126. must initialize this properly.
  127. void *data: arbitrary mount options, usually comes as an ASCII
  128. string
  129. int silent: whether or not to be silent on error
  130. The Superblock Object
  131. =====================
  132. A superblock object represents a mounted filesystem.
  133. struct super_operations
  134. -----------------------
  135. This describes how the VFS can manipulate the superblock of your
  136. filesystem. As of kernel 2.6.13, the following members are defined:
  137. struct super_operations {
  138. struct inode *(*alloc_inode)(struct super_block *sb);
  139. void (*destroy_inode)(struct inode *);
  140. void (*read_inode) (struct inode *);
  141. void (*dirty_inode) (struct inode *);
  142. int (*write_inode) (struct inode *, int);
  143. void (*put_inode) (struct inode *);
  144. void (*drop_inode) (struct inode *);
  145. void (*delete_inode) (struct inode *);
  146. void (*put_super) (struct super_block *);
  147. void (*write_super) (struct super_block *);
  148. int (*sync_fs)(struct super_block *sb, int wait);
  149. void (*write_super_lockfs) (struct super_block *);
  150. void (*unlockfs) (struct super_block *);
  151. int (*statfs) (struct super_block *, struct kstatfs *);
  152. int (*remount_fs) (struct super_block *, int *, char *);
  153. void (*clear_inode) (struct inode *);
  154. void (*umount_begin) (struct super_block *);
  155. void (*sync_inodes) (struct super_block *sb,
  156. struct writeback_control *wbc);
  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.
  167. destroy_inode: this method is called by destroy_inode() to release
  168. resources allocated for struct inode.
  169. read_inode: this method is called to read a specific inode from the
  170. mounted filesystem. The i_ino member in the struct inode is
  171. initialized by the VFS to indicate which inode to read. Other
  172. members are filled in by this method.
  173. You can set this to NULL and use iget5_locked() instead of iget()
  174. to read inodes. This is necessary for filesystems for which the
  175. inode number is not sufficient to identify an inode.
  176. dirty_inode: this method is called by the VFS to mark an inode dirty.
  177. write_inode: this method is called when the VFS needs to write an
  178. inode to disc. The second parameter indicates whether the write
  179. should be synchronous or not, not all filesystems check this flag.
  180. put_inode: called when the VFS inode is removed from the inode
  181. cache.
  182. drop_inode: called when the last access to the inode is dropped,
  183. with the inode_lock spinlock held.
  184. This method should be either NULL (normal UNIX filesystem
  185. semantics) or "generic_delete_inode" (for filesystems that do not
  186. want to cache inodes - causing "delete_inode" to always be
  187. called regardless of the value of i_nlink)
  188. The "generic_delete_inode()" behavior is equivalent to the
  189. old practice of using "force_delete" in the put_inode() case,
  190. but does not have the races that the "force_delete()" approach
  191. had.
  192. delete_inode: called when the VFS wants to delete an inode
  193. put_super: called when the VFS wishes to free the superblock
  194. (i.e. unmount). This is called with the superblock lock held
  195. write_super: called when the VFS superblock needs to be written to
  196. disc. This method is optional
  197. sync_fs: called when VFS is writing out all dirty data associated with
  198. a superblock. The second parameter indicates whether the method
  199. should wait until the write out has been completed. Optional.
  200. write_super_lockfs: called when VFS is locking a filesystem and
  201. forcing it into a consistent state. This method is currently
  202. used by the Logical Volume Manager (LVM).
  203. unlockfs: called when VFS is unlocking a filesystem and making it writable
  204. again.
  205. statfs: called when the VFS needs to get filesystem statistics. This
  206. is called with the kernel lock held
  207. remount_fs: called when the filesystem is remounted. This is called
  208. with the kernel lock held
  209. clear_inode: called then the VFS clears the inode. Optional
  210. umount_begin: called when the VFS is unmounting a filesystem.
  211. sync_inodes: called when the VFS is writing out dirty data associated with
  212. a superblock.
  213. show_options: called by the VFS to show mount options for /proc/<pid>/mounts.
  214. quota_read: called by the VFS to read from filesystem quota file.
  215. quota_write: called by the VFS to write to filesystem quota file.
  216. The read_inode() method is responsible for filling in the "i_op"
  217. field. This is a pointer to a "struct inode_operations" which
  218. describes the methods that can be performed on individual inodes.
  219. The Inode Object
  220. ================
  221. An inode object represents an object within the filesystem.
  222. struct inode_operations
  223. -----------------------
  224. This describes how the VFS can manipulate an inode in your
  225. filesystem. As of kernel 2.6.13, the following members are defined:
  226. struct inode_operations {
  227. int (*create) (struct inode *,struct dentry *,int, struct nameidata *);
  228. struct dentry * (*lookup) (struct inode *,struct dentry *, struct nameidata *);
  229. int (*link) (struct dentry *,struct inode *,struct dentry *);
  230. int (*unlink) (struct inode *,struct dentry *);
  231. int (*symlink) (struct inode *,struct dentry *,const char *);
  232. int (*mkdir) (struct inode *,struct dentry *,int);
  233. int (*rmdir) (struct inode *,struct dentry *);
  234. int (*mknod) (struct inode *,struct dentry *,int,dev_t);
  235. int (*rename) (struct inode *, struct dentry *,
  236. struct inode *, struct dentry *);
  237. int (*readlink) (struct dentry *, char __user *,int);
  238. void * (*follow_link) (struct dentry *, struct nameidata *);
  239. void (*put_link) (struct dentry *, struct nameidata *, void *);
  240. void (*truncate) (struct inode *);
  241. int (*permission) (struct inode *, int, struct nameidata *);
  242. int (*setattr) (struct dentry *, struct iattr *);
  243. int (*getattr) (struct vfsmount *mnt, struct dentry *, struct kstat *);
  244. int (*setxattr) (struct dentry *, const char *,const void *,size_t,int);
  245. ssize_t (*getxattr) (struct dentry *, const char *, void *, size_t);
  246. ssize_t (*listxattr) (struct dentry *, char *, size_t);
  247. int (*removexattr) (struct dentry *, const char *);
  248. };
  249. Again, all methods are called without any locks being held, unless
  250. otherwise noted.
  251. create: called by the open(2) and creat(2) system calls. Only
  252. required if you want to support regular files. The dentry you
  253. get should not have an inode (i.e. it should be a negative
  254. dentry). Here you will probably call d_instantiate() with the
  255. dentry and the newly created inode
  256. lookup: called when the VFS needs to look up an inode in a parent
  257. directory. The name to look for is found in the dentry. This
  258. method must call d_add() to insert the found inode into the
  259. dentry. The "i_count" field in the inode structure should be
  260. incremented. If the named inode does not exist a NULL inode
  261. should be inserted into the dentry (this is called a negative
  262. dentry). Returning an error code from this routine must only
  263. be done on a real error, otherwise creating inodes with system
  264. calls like create(2), mknod(2), mkdir(2) and so on will fail.
  265. If you wish to overload the dentry methods then you should
  266. initialise the "d_dop" field in the dentry; this is a pointer
  267. to a struct "dentry_operations".
  268. This method is called with the directory inode semaphore held
  269. link: called by the link(2) system call. Only required if you want
  270. to support hard links. You will probably need to call
  271. d_instantiate() just as you would in the create() method
  272. unlink: called by the unlink(2) system call. Only required if you
  273. want to support deleting inodes
  274. symlink: called by the symlink(2) system call. Only required if you
  275. want to support symlinks. You will probably need to call
  276. d_instantiate() just as you would in the create() method
  277. mkdir: called by the mkdir(2) system call. Only required if you want
  278. to support creating subdirectories. You will probably need to
  279. call d_instantiate() just as you would in the create() method
  280. rmdir: called by the rmdir(2) system call. Only required if you want
  281. to support deleting subdirectories
  282. mknod: called by the mknod(2) system call to create a device (char,
  283. block) inode or a named pipe (FIFO) or socket. Only required
  284. if you want to support creating these types of inodes. You
  285. will probably need to call d_instantiate() just as you would
  286. in the create() method
  287. rename: called by the rename(2) system call to rename the object to
  288. have the parent and name given by the second inode and dentry.
  289. readlink: called by the readlink(2) system call. Only required if
  290. you want to support reading symbolic links
  291. follow_link: called by the VFS to follow a symbolic link to the
  292. inode it points to. Only required if you want to support
  293. symbolic links. This method returns a void pointer cookie
  294. that is passed to put_link().
  295. put_link: called by the VFS to release resources allocated by
  296. follow_link(). The cookie returned by follow_link() is passed
  297. to to this method as the last parameter. It is used by
  298. filesystems such as NFS where page cache is not stable
  299. (i.e. page that was installed when the symbolic link walk
  300. started might not be in the page cache at the end of the
  301. walk).
  302. truncate: called by the VFS to change the size of a file. The
  303. i_size field of the inode is set to the desired size by the
  304. VFS before this method is called. This method is called by
  305. the truncate(2) system call and related functionality.
  306. permission: called by the VFS to check for access rights on a POSIX-like
  307. filesystem.
  308. setattr: called by the VFS to set attributes for a file. This method
  309. is called by chmod(2) and related system calls.
  310. getattr: called by the VFS to get attributes of a file. This method
  311. is called by stat(2) and related system calls.
  312. setxattr: called by the VFS to set an extended attribute for a file.
  313. Extended attribute is a name:value pair associated with an
  314. inode. This method is called by setxattr(2) system call.
  315. getxattr: called by the VFS to retrieve the value of an extended
  316. attribute name. This method is called by getxattr(2) function
  317. call.
  318. listxattr: called by the VFS to list all extended attributes for a
  319. given file. This method is called by listxattr(2) system call.
  320. removexattr: called by the VFS to remove an extended attribute from
  321. a file. This method is called by removexattr(2) system call.
  322. The Address Space Object
  323. ========================
  324. The address space object is used to identify pages in the page cache.
  325. struct address_space_operations
  326. -------------------------------
  327. This describes how the VFS can manipulate mapping of a file to page cache in
  328. your filesystem. As of kernel 2.6.13, the following members are defined:
  329. struct address_space_operations {
  330. int (*writepage)(struct page *page, struct writeback_control *wbc);
  331. int (*readpage)(struct file *, struct page *);
  332. int (*sync_page)(struct page *);
  333. int (*writepages)(struct address_space *, struct writeback_control *);
  334. int (*set_page_dirty)(struct page *page);
  335. int (*readpages)(struct file *filp, struct address_space *mapping,
  336. struct list_head *pages, unsigned nr_pages);
  337. int (*prepare_write)(struct file *, struct page *, unsigned, unsigned);
  338. int (*commit_write)(struct file *, struct page *, unsigned, unsigned);
  339. sector_t (*bmap)(struct address_space *, sector_t);
  340. int (*invalidatepage) (struct page *, unsigned long);
  341. int (*releasepage) (struct page *, int);
  342. ssize_t (*direct_IO)(int, struct kiocb *, const struct iovec *iov,
  343. loff_t offset, unsigned long nr_segs);
  344. struct page* (*get_xip_page)(struct address_space *, sector_t,
  345. int);
  346. };
  347. writepage: called by the VM write a dirty page to backing store.
  348. readpage: called by the VM to read a page from backing store.
  349. sync_page: called by the VM to notify the backing store to perform all
  350. queued I/O operations for a page. I/O operations for other pages
  351. associated with this address_space object may also be performed.
  352. writepages: called by the VM to write out pages associated with the
  353. address_space object.
  354. set_page_dirty: called by the VM to set a page dirty.
  355. readpages: called by the VM to read pages associated with the address_space
  356. object.
  357. prepare_write: called by the generic write path in VM to set up a write
  358. request for a page.
  359. commit_write: called by the generic write path in VM to write page to
  360. its backing store.
  361. bmap: called by the VFS to map a logical block offset within object to
  362. physical block number. This method is use by for the legacy FIBMAP
  363. ioctl. Other uses are discouraged.
  364. invalidatepage: called by the VM on truncate to disassociate a page from its
  365. address_space mapping.
  366. releasepage: called by the VFS to release filesystem specific metadata from
  367. a page.
  368. direct_IO: called by the VM for direct I/O writes and reads.
  369. get_xip_page: called by the VM to translate a block number to a page.
  370. The page is valid until the corresponding filesystem is unmounted.
  371. Filesystems that want to use execute-in-place (XIP) need to implement
  372. it. An example implementation can be found in fs/ext2/xip.c.
  373. The File Object
  374. ===============
  375. A file object represents a file opened by a process.
  376. struct file_operations
  377. ----------------------
  378. This describes how the VFS can manipulate an open file. As of kernel
  379. 2.6.13, the following members are defined:
  380. struct file_operations {
  381. loff_t (*llseek) (struct file *, loff_t, int);
  382. ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
  383. ssize_t (*aio_read) (struct kiocb *, char __user *, size_t, loff_t);
  384. ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
  385. ssize_t (*aio_write) (struct kiocb *, const char __user *, size_t, loff_t);
  386. int (*readdir) (struct file *, void *, filldir_t);
  387. unsigned int (*poll) (struct file *, struct poll_table_struct *);
  388. int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
  389. long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
  390. long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
  391. int (*mmap) (struct file *, struct vm_area_struct *);
  392. int (*open) (struct inode *, struct file *);
  393. int (*flush) (struct file *);
  394. int (*release) (struct inode *, struct file *);
  395. int (*fsync) (struct file *, struct dentry *, int datasync);
  396. int (*aio_fsync) (struct kiocb *, int datasync);
  397. int (*fasync) (int, struct file *, int);
  398. int (*lock) (struct file *, int, struct file_lock *);
  399. ssize_t (*readv) (struct file *, const struct iovec *, unsigned long, loff_t *);
  400. ssize_t (*writev) (struct file *, const struct iovec *, unsigned long, loff_t *);
  401. ssize_t (*sendfile) (struct file *, loff_t *, size_t, read_actor_t, void *);
  402. ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
  403. unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
  404. int (*check_flags)(int);
  405. int (*dir_notify)(struct file *filp, unsigned long arg);
  406. int (*flock) (struct file *, int, struct file_lock *);
  407. };
  408. Again, all methods are called without any locks being held, unless
  409. otherwise noted.
  410. llseek: called when the VFS needs to move the file position index
  411. read: called by read(2) and related system calls
  412. aio_read: called by io_submit(2) and other asynchronous I/O operations
  413. write: called by write(2) and related system calls
  414. aio_write: called by io_submit(2) and other asynchronous I/O operations
  415. readdir: called when the VFS needs to read the directory contents
  416. poll: called by the VFS when a process wants to check if there is
  417. activity on this file and (optionally) go to sleep until there
  418. is activity. Called by the select(2) and poll(2) system calls
  419. ioctl: called by the ioctl(2) system call
  420. unlocked_ioctl: called by the ioctl(2) system call. Filesystems that do not
  421. require the BKL should use this method instead of the ioctl() above.
  422. compat_ioctl: called by the ioctl(2) system call when 32 bit system calls
  423. are used on 64 bit kernels.
  424. mmap: called by the mmap(2) system call
  425. open: called by the VFS when an inode should be opened. When the VFS
  426. opens a file, it creates a new "struct file". It then calls the
  427. open method for the newly allocated file structure. You might
  428. think that the open method really belongs in
  429. "struct inode_operations", and you may be right. I think it's
  430. done the way it is because it makes filesystems simpler to
  431. implement. The open() method is a good place to initialize the
  432. "private_data" member in the file structure if you want to point
  433. to a device structure
  434. flush: called by the close(2) system call to flush a file
  435. release: called when the last reference to an open file is closed
  436. fsync: called by the fsync(2) system call
  437. fasync: called by the fcntl(2) system call when asynchronous
  438. (non-blocking) mode is enabled for a file
  439. lock: called by the fcntl(2) system call for F_GETLK, F_SETLK, and F_SETLKW
  440. commands
  441. readv: called by the readv(2) system call
  442. writev: called by the writev(2) system call
  443. sendfile: called by the sendfile(2) system call
  444. get_unmapped_area: called by the mmap(2) system call
  445. check_flags: called by the fcntl(2) system call for F_SETFL command
  446. dir_notify: called by the fcntl(2) system call for F_NOTIFY command
  447. flock: called by the flock(2) system call
  448. Note that the file operations are implemented by the specific
  449. filesystem in which the inode resides. When opening a device node
  450. (character or block special) most filesystems will call special
  451. support routines in the VFS which will locate the required device
  452. driver information. These support routines replace the filesystem file
  453. operations with those for the device driver, and then proceed to call
  454. the new open() method for the file. This is how opening a device file
  455. in the filesystem eventually ends up calling the device driver open()
  456. method.
  457. Directory Entry Cache (dcache)
  458. ==============================
  459. struct dentry_operations
  460. ------------------------
  461. This describes how a filesystem can overload the standard dentry
  462. operations. Dentries and the dcache are the domain of the VFS and the
  463. individual filesystem implementations. Device drivers have no business
  464. here. These methods may be set to NULL, as they are either optional or
  465. the VFS uses a default. As of kernel 2.6.13, the following members are
  466. defined:
  467. struct dentry_operations {
  468. int (*d_revalidate)(struct dentry *, struct nameidata *);
  469. int (*d_hash) (struct dentry *, struct qstr *);
  470. int (*d_compare) (struct dentry *, struct qstr *, struct qstr *);
  471. int (*d_delete)(struct dentry *);
  472. void (*d_release)(struct dentry *);
  473. void (*d_iput)(struct dentry *, struct inode *);
  474. };
  475. d_revalidate: called when the VFS needs to revalidate a dentry. This
  476. is called whenever a name look-up finds a dentry in the
  477. dcache. Most filesystems leave this as NULL, because all their
  478. dentries in the dcache are valid
  479. d_hash: called when the VFS adds a dentry to the hash table
  480. d_compare: called when a dentry should be compared with another
  481. d_delete: called when the last reference to a dentry is
  482. deleted. This means no-one is using the dentry, however it is
  483. still valid and in the dcache
  484. d_release: called when a dentry is really deallocated
  485. d_iput: called when a dentry loses its inode (just prior to its
  486. being deallocated). The default when this is NULL is that the
  487. VFS calls iput(). If you define this method, you must call
  488. iput() yourself
  489. Each dentry has a pointer to its parent dentry, as well as a hash list
  490. of child dentries. Child dentries are basically like files in a
  491. directory.
  492. Directory Entry Cache API
  493. --------------------------
  494. There are a number of functions defined which permit a filesystem to
  495. manipulate dentries:
  496. dget: open a new handle for an existing dentry (this just increments
  497. the usage count)
  498. dput: close a handle for a dentry (decrements the usage count). If
  499. the usage count drops to 0, the "d_delete" method is called
  500. and the dentry is placed on the unused list if the dentry is
  501. still in its parents hash list. Putting the dentry on the
  502. unused list just means that if the system needs some RAM, it
  503. goes through the unused list of dentries and deallocates them.
  504. If the dentry has already been unhashed and the usage count
  505. drops to 0, in this case the dentry is deallocated after the
  506. "d_delete" method is called
  507. d_drop: this unhashes a dentry from its parents hash list. A
  508. subsequent call to dput() will deallocate the dentry if its
  509. usage count drops to 0
  510. d_delete: delete a dentry. If there are no other open references to
  511. the dentry then the dentry is turned into a negative dentry
  512. (the d_iput() method is called). If there are other
  513. references, then d_drop() is called instead
  514. d_add: add a dentry to its parents hash list and then calls
  515. d_instantiate()
  516. d_instantiate: add a dentry to the alias hash list for the inode and
  517. updates the "d_inode" member. The "i_count" member in the
  518. inode structure should be set/incremented. If the inode
  519. pointer is NULL, the dentry is called a "negative
  520. dentry". This function is commonly called when an inode is
  521. created for an existing negative dentry
  522. d_lookup: look up a dentry given its parent and path name component
  523. It looks up the child of that given name from the dcache
  524. hash table. If it is found, the reference count is incremented
  525. and the dentry is returned. The caller must use d_put()
  526. to free the dentry when it finishes using it.
  527. For further information on dentry locking, please refer to the document
  528. Documentation/filesystems/dentry-locking.txt.
  529. Resources
  530. =========
  531. (Note some of these resources are not up-to-date with the latest kernel
  532. version.)
  533. Creating Linux virtual filesystems. 2002
  534. <http://lwn.net/Articles/13325/>
  535. The Linux Virtual File-system Layer by Neil Brown. 1999
  536. <http://www.cse.unsw.edu.au/~neilb/oss/linux-commentary/vfs.html>
  537. A tour of the Linux VFS by Michael K. Johnson. 1996
  538. <http://www.tldp.org/LDP/khg/HyperNews/get/fs/vfstour.html>
  539. A small trail through the Linux kernel by Andries Brouwer. 2001
  540. <http://www.win.tue.nl/~aeb/linux/vfs/trail.html>