vfs.rst 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513
  1. .. SPDX-License-Identifier: GPL-2.0
  2. =========================================
  3. Overview of the Linux Virtual File System
  4. =========================================
  5. Original author: Richard Gooch <rgooch@atnf.csiro.au>
  6. - Copyright (C) 1999 Richard Gooch
  7. - Copyright (C) 2005 Pekka Enberg
  8. Introduction
  9. ============
  10. The Virtual File System (also known as the Virtual Filesystem Switch) is
  11. the software layer in the kernel that provides the filesystem interface
  12. to userspace programs. It also provides an abstraction within the
  13. kernel which allows different filesystem implementations to coexist.
  14. VFS system calls open(2), stat(2), read(2), write(2), chmod(2) and so on
  15. are called from a process context. Filesystem locking is described in
  16. the document Documentation/filesystems/locking.rst.
  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, some
  27. bits of the cache are missing. In order to resolve your pathname into a
  28. dentry, the VFS may have to resort to creating dentries along the way,
  29. and then loading the inode. This is done by looking up the inode.
  30. The Inode Object
  31. ----------------
  32. An individual dentry usually has a pointer to an inode. Inodes are
  33. filesystem objects such as regular files, directories, FIFOs and other
  34. beasts. They live either on the disc (for block device filesystems) or
  35. in the memory (for pseudo filesystems). Inodes that live on the disc
  36. are copied into the memory when required and changes to the inode are
  37. written back to disc. A single inode can be pointed to by multiple
  38. dentries (hard links, for example, do this).
  39. To look up an inode requires that the VFS calls the lookup() method of
  40. the parent directory inode. This method is installed by the specific
  41. filesystem implementation that the inode lives in. Once the VFS has the
  42. required dentry (and hence the inode), we can do all those boring things
  43. like open(2) the file, or stat(2) it to peek at the inode data. The
  44. stat(2) operation is fairly simple: once the VFS has the dentry, it
  45. peeks at the inode data and passes some of it back to userspace.
  46. The File Object
  47. ---------------
  48. Opening a file requires another operation: allocation of a file
  49. structure (this is the kernel-side implementation of file descriptors).
  50. The freshly allocated file structure is initialized with a pointer to
  51. the dentry and a set of file operation member functions. These are
  52. taken from the inode data. The open() file method is then called so the
  53. specific filesystem implementation can do its work. You can see that
  54. this is another switch performed by the VFS. The file structure is
  55. placed into the file descriptor table for the process.
  56. Reading, writing and closing files (and other assorted VFS operations)
  57. is done by using the userspace file descriptor to grab the appropriate
  58. file structure, and then calling the required file structure method to
  59. do whatever is required. For as long as the file is open, it keeps the
  60. dentry in use, which in turn means that the VFS inode is still in use.
  61. Registering and Mounting a Filesystem
  62. =====================================
  63. To register and unregister a filesystem, use the following API
  64. functions:
  65. .. code-block:: c
  66. #include <linux/fs.h>
  67. extern int register_filesystem(struct file_system_type *);
  68. extern int unregister_filesystem(struct file_system_type *);
  69. The passed struct file_system_type describes your filesystem. When a
  70. request is made to mount a filesystem onto a directory in your
  71. namespace, the VFS will call the appropriate get_tree() method for the
  72. specific filesystem. See Documentation/filesystems/mount_api.rst
  73. for more details.
  74. You can see all filesystems that are registered to the kernel in the
  75. file /proc/filesystems.
  76. struct file_system_type
  77. -----------------------
  78. This describes the filesystem. The following
  79. members are defined:
  80. .. code-block:: c
  81. struct file_system_type {
  82. const char *name;
  83. int fs_flags;
  84. int (*init_fs_context)(struct fs_context *);
  85. const struct fs_parameter_spec *parameters;
  86. void (*kill_sb) (struct super_block *);
  87. struct module *owner;
  88. struct file_system_type * next;
  89. struct hlist_head fs_supers;
  90. struct lock_class_key s_lock_key;
  91. struct lock_class_key s_umount_key;
  92. struct lock_class_key s_vfs_rename_key;
  93. struct lock_class_key s_writers_key[SB_FREEZE_LEVELS];
  94. struct lock_class_key i_lock_key;
  95. struct lock_class_key i_mutex_key;
  96. struct lock_class_key invalidate_lock_key;
  97. struct lock_class_key i_mutex_dir_key;
  98. };
  99. ``name``
  100. the name of the filesystem type, such as "ext2", "iso9660",
  101. "msdos" and so on
  102. ``fs_flags``
  103. various flags (i.e. FS_REQUIRES_DEV, FS_NO_DCACHE, etc.)
  104. ``init_fs_context``
  105. Initializes 'struct fs_context' ->ops and ->fs_private fields with
  106. filesystem-specific data.
  107. ``parameters``
  108. Pointer to the array of filesystem parameters descriptors
  109. 'struct fs_parameter_spec'.
  110. More info in Documentation/filesystems/mount_api.rst.
  111. ``kill_sb``
  112. the method to call when an instance of this filesystem should be
  113. shut down
  114. ``owner``
  115. for internal VFS use: you should initialize this to THIS_MODULE
  116. in most cases.
  117. ``next``
  118. for internal VFS use: you should initialize this to NULL
  119. ``fs_supers``
  120. for internal VFS use: hlist of filesystem instances (superblocks)
  121. s_lock_key, s_umount_key, s_vfs_rename_key, s_writers_key,
  122. i_lock_key, i_mutex_key, invalidate_lock_key, i_mutex_dir_key: lockdep-specific
  123. The Superblock Object
  124. =====================
  125. A superblock object represents a mounted filesystem.
  126. struct super_operations
  127. -----------------------
  128. This describes how the VFS can manipulate the superblock of your
  129. filesystem. The following members are defined:
  130. .. code-block:: c
  131. struct super_operations {
  132. struct inode *(*alloc_inode)(struct super_block *sb);
  133. void (*destroy_inode)(struct inode *);
  134. void (*free_inode)(struct inode *);
  135. void (*dirty_inode) (struct inode *, int flags);
  136. int (*write_inode) (struct inode *, struct writeback_control *wbc);
  137. int (*drop_inode) (struct inode *);
  138. void (*evict_inode) (struct inode *);
  139. void (*put_super) (struct super_block *);
  140. int (*sync_fs)(struct super_block *sb, int wait);
  141. int (*freeze_super) (struct super_block *sb,
  142. enum freeze_holder who);
  143. int (*freeze_fs) (struct super_block *);
  144. int (*thaw_super) (struct super_block *sb,
  145. enum freeze_wholder who);
  146. int (*unfreeze_fs) (struct super_block *);
  147. int (*statfs) (struct dentry *, struct kstatfs *);
  148. void (*umount_begin) (struct super_block *);
  149. int (*show_options)(struct seq_file *, struct dentry *);
  150. int (*show_devname)(struct seq_file *, struct dentry *);
  151. int (*show_path)(struct seq_file *, struct dentry *);
  152. int (*show_stats)(struct seq_file *, struct dentry *);
  153. ssize_t (*quota_read)(struct super_block *, int, char *, size_t, loff_t);
  154. ssize_t (*quota_write)(struct super_block *, int, const char *, size_t, loff_t);
  155. struct dquot **(*get_dquots)(struct inode *);
  156. long (*nr_cached_objects)(struct super_block *,
  157. struct shrink_control *);
  158. long (*free_cached_objects)(struct super_block *,
  159. struct shrink_control *);
  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``
  166. this method is called by alloc_inode() to allocate memory for
  167. struct inode and initialize it. If this function is not
  168. defined, a simple 'struct inode' is allocated. Normally
  169. alloc_inode will be used to allocate a larger structure which
  170. contains a 'struct inode' embedded within it.
  171. ``destroy_inode``
  172. this method is called by destroy_inode() to release resources
  173. allocated for struct inode. It is only required if
  174. ->alloc_inode was defined and simply undoes anything done by
  175. ->alloc_inode.
  176. ``free_inode``
  177. this method is called from RCU callback. If you use call_rcu()
  178. in ->destroy_inode to free 'struct inode' memory, then it's
  179. better to release memory in this method.
  180. ``dirty_inode``
  181. this method is called by the VFS when an inode is marked dirty.
  182. This is specifically for the inode itself being marked dirty,
  183. not its data. If the update needs to be persisted by fdatasync(),
  184. then I_DIRTY_DATASYNC will be set in the flags argument.
  185. I_DIRTY_TIME will be set in the flags in case lazytime is enabled
  186. and struct inode has times updated since the last ->dirty_inode
  187. call.
  188. ``write_inode``
  189. this method is called when the VFS needs to write an inode to
  190. disc. The second parameter indicates whether the write should
  191. be synchronous or not, not all filesystems check this flag.
  192. ``drop_inode``
  193. called when the last access to the inode is dropped, with the
  194. inode->i_lock spinlock held.
  195. This method should be either NULL (normal UNIX filesystem
  196. semantics) or "inode_just_drop" (for filesystems that do
  197. not want to cache inodes - causing "delete_inode" to always be
  198. called regardless of the value of i_nlink)
  199. The "inode_just_drop()" behavior is equivalent to the old
  200. practice of using "force_delete" in the put_inode() case, but
  201. does not have the races that the "force_delete()" approach had.
  202. ``evict_inode``
  203. called when the VFS wants to evict an inode. Caller does
  204. *not* evict the pagecache or inode-associated metadata buffers;
  205. the method has to use truncate_inode_pages_final() to get rid
  206. of those. Caller makes sure async writeback cannot be running for
  207. the inode while (or after) ->evict_inode() is called. Optional.
  208. ``put_super``
  209. called when the VFS wishes to free the superblock
  210. (i.e. unmount). This is called with the superblock lock held
  211. ``sync_fs``
  212. called when VFS is writing out all dirty data associated with a
  213. superblock. The second parameter indicates whether the method
  214. should wait until the write out has been completed. Optional.
  215. ``freeze_super``
  216. Called instead of ->freeze_fs callback if provided.
  217. Main difference is that ->freeze_super is called without taking
  218. down_write(&sb->s_umount). If filesystem implements it and wants
  219. ->freeze_fs to be called too, then it has to call ->freeze_fs
  220. explicitly from this callback. Optional.
  221. ``freeze_fs``
  222. called when VFS is locking a filesystem and forcing it into a
  223. consistent state. This method is currently used by the Logical
  224. Volume Manager (LVM) and ioctl(FIFREEZE). Optional.
  225. ``thaw_super``
  226. called when VFS is unlocking a filesystem and making it writable
  227. again after ->freeze_super. Optional.
  228. ``unfreeze_fs``
  229. called when VFS is unlocking a filesystem and making it writable
  230. again after ->freeze_fs. Optional.
  231. ``statfs``
  232. called when the VFS needs to get filesystem statistics.
  233. ``umount_begin``
  234. called when the VFS is unmounting a filesystem.
  235. ``show_options``
  236. called by the VFS to show mount options for /proc/<pid>/mounts
  237. and /proc/<pid>/mountinfo.
  238. (see "Mount Options" section)
  239. ``show_devname``
  240. Optional. Called by the VFS to show device name for
  241. /proc/<pid>/{mounts,mountinfo,mountstats}. If not provided then
  242. '(struct mount).mnt_devname' will be used.
  243. ``show_path``
  244. Optional. Called by the VFS (for /proc/<pid>/mountinfo) to show
  245. the mount root dentry path relative to the filesystem root.
  246. ``show_stats``
  247. Optional. Called by the VFS (for /proc/<pid>/mountstats) to show
  248. filesystem-specific mount statistics.
  249. ``quota_read``
  250. called by the VFS to read from filesystem quota file.
  251. ``quota_write``
  252. called by the VFS to write to filesystem quota file.
  253. ``get_dquots``
  254. called by quota to get 'struct dquot' array for a particular inode.
  255. Optional.
  256. ``nr_cached_objects``
  257. called by the sb cache shrinking function for the filesystem to
  258. return the number of freeable cached objects it contains.
  259. Optional.
  260. ``free_cache_objects``
  261. called by the sb cache shrinking function for the filesystem to
  262. scan the number of objects indicated to try to free them.
  263. Optional, but any filesystem implementing this method needs to
  264. also implement ->nr_cached_objects for it to be called
  265. correctly.
  266. We can't do anything with any errors that the filesystem might
  267. encountered, hence the void return type. This will never be
  268. called if the VM is trying to reclaim under GFP_NOFS conditions,
  269. hence this method does not need to handle that situation itself.
  270. Implementations must include conditional reschedule calls inside
  271. any scanning loop that is done. This allows the VFS to
  272. determine appropriate scan batch sizes without having to worry
  273. about whether implementations will cause holdoff problems due to
  274. large scan batch sizes.
  275. Whoever sets up the inode is responsible for filling in the "i_op"
  276. field. This is a pointer to a "struct inode_operations" which describes
  277. the methods that can be performed on individual inodes.
  278. struct xattr_handler
  279. ---------------------
  280. On filesystems that support extended attributes (xattrs), the s_xattr
  281. superblock field points to a NULL-terminated array of xattr handlers.
  282. Extended attributes are name:value pairs.
  283. ``name``
  284. Indicates that the handler matches attributes with the specified
  285. name (such as "system.posix_acl_access"); the prefix field must
  286. be NULL.
  287. ``prefix``
  288. Indicates that the handler matches all attributes with the
  289. specified name prefix (such as "user."); the name field must be
  290. NULL.
  291. ``list``
  292. Determine if attributes matching this xattr handler should be
  293. listed for a particular dentry. Used by some listxattr
  294. implementations like generic_listxattr.
  295. ``get``
  296. Called by the VFS to get the value of a particular extended
  297. attribute. This method is called by the getxattr(2) system
  298. call.
  299. ``set``
  300. Called by the VFS to set the value of a particular extended
  301. attribute. When the new value is NULL, called to remove a
  302. particular extended attribute. This method is called by the
  303. setxattr(2) and removexattr(2) system calls.
  304. When none of the xattr handlers of a filesystem match the specified
  305. attribute name or when a filesystem doesn't support extended attributes,
  306. the various ``*xattr(2)`` system calls return -EOPNOTSUPP.
  307. The Inode Object
  308. ================
  309. An inode object represents an object within the filesystem.
  310. struct inode_operations
  311. -----------------------
  312. This describes how the VFS can manipulate an inode in your filesystem.
  313. As of kernel 2.6.22, the following members are defined:
  314. .. code-block:: c
  315. struct inode_operations {
  316. int (*create) (struct mnt_idmap *, struct inode *,struct dentry *, umode_t, bool);
  317. struct dentry * (*lookup) (struct inode *,struct dentry *, unsigned int);
  318. int (*link) (struct dentry *,struct inode *,struct dentry *);
  319. int (*unlink) (struct inode *,struct dentry *);
  320. int (*symlink) (struct mnt_idmap *, struct inode *,struct dentry *,const char *);
  321. struct dentry *(*mkdir) (struct mnt_idmap *, struct inode *,struct dentry *,umode_t);
  322. int (*rmdir) (struct inode *,struct dentry *);
  323. int (*mknod) (struct mnt_idmap *, struct inode *,struct dentry *,umode_t,dev_t);
  324. int (*rename) (struct mnt_idmap *, struct inode *, struct dentry *,
  325. struct inode *, struct dentry *, unsigned int);
  326. int (*readlink) (struct dentry *, char __user *,int);
  327. const char *(*get_link) (struct dentry *, struct inode *,
  328. struct delayed_call *);
  329. int (*permission) (struct mnt_idmap *, struct inode *, int);
  330. struct posix_acl * (*get_inode_acl)(struct inode *, int, bool);
  331. int (*setattr) (struct mnt_idmap *, struct dentry *, struct iattr *);
  332. int (*getattr) (struct mnt_idmap *, const struct path *, struct kstat *, u32, unsigned int);
  333. ssize_t (*listxattr) (struct dentry *, char *, size_t);
  334. void (*update_time)(struct inode *inode, enum fs_update_time type,
  335. int flags);
  336. void (*sync_lazytime)(struct inode *inode);
  337. int (*atomic_open)(struct inode *, struct dentry *, struct file *,
  338. unsigned open_flag, umode_t create_mode);
  339. int (*tmpfile) (struct mnt_idmap *, struct inode *, struct file *, umode_t);
  340. struct posix_acl * (*get_acl)(struct mnt_idmap *, struct dentry *, int);
  341. int (*set_acl)(struct mnt_idmap *, struct dentry *, struct posix_acl *, int);
  342. int (*fileattr_set)(struct mnt_idmap *idmap,
  343. struct dentry *dentry, struct file_kattr *fa);
  344. int (*fileattr_get)(struct dentry *dentry, struct file_kattr *fa);
  345. struct offset_ctx *(*get_offset_ctx)(struct inode *inode);
  346. };
  347. Again, all methods are called without any locks being held, unless
  348. otherwise noted.
  349. ``create``
  350. called by the open(2) and creat(2) system calls. Only required
  351. if you want to support regular files. The dentry you get should
  352. not have an inode (i.e. it should be a negative dentry). Here
  353. you will probably call d_instantiate() with the dentry and the
  354. newly created inode
  355. ``lookup``
  356. called when the VFS needs to look up an inode in a parent
  357. directory. The name to look for is found in the dentry. This
  358. method must call d_add() to insert the found inode into the
  359. dentry. The "i_count" field in the inode structure should be
  360. incremented. If the named inode does not exist a NULL inode
  361. should be inserted into the dentry (this is called a negative
  362. dentry). Returning an error code from this routine must only be
  363. done on a real error, otherwise creating inodes with system
  364. calls like create(2), mknod(2), mkdir(2) and so on will fail.
  365. If you wish to overload the dentry methods then you should
  366. initialise the "d_dop" field in the dentry; this is a pointer to
  367. a struct "dentry_operations". This method is called with the
  368. directory inode semaphore held
  369. ``link``
  370. called by the link(2) system call. Only required if you want to
  371. support hard links. You will probably need to call
  372. d_instantiate() just as you would in the create() method
  373. ``unlink``
  374. called by the unlink(2) system call. Only required if you want
  375. to support deleting inodes
  376. ``symlink``
  377. called by the symlink(2) system call. Only required if you want
  378. to support symlinks. You will probably need to call
  379. d_instantiate() just as you would in the create() method
  380. ``mkdir``
  381. called by the mkdir(2) system call. Only required if you want
  382. to support creating subdirectories. You will probably need to
  383. call d_instantiate_new() just as you would in the create() method.
  384. If d_instantiate_new() is not used and if the fh_to_dentry()
  385. export operation is provided, or if the storage might be
  386. accessible by another path (e.g. with a network filesystem)
  387. then more care may be needed. Importantly d_instantate()
  388. should not be used with an inode that is no longer I_NEW if there
  389. any chance that the inode could already be attached to a dentry.
  390. This is because of a hard rule in the VFS that a directory must
  391. only ever have one dentry.
  392. For example, if an NFS filesystem is mounted twice the new directory
  393. could be visible on the other mount before it is on the original
  394. mount, and a pair of name_to_handle_at(), open_by_handle_at()
  395. calls could instantiate the directory inode with an IS_ROOT()
  396. dentry before the first mkdir returns.
  397. If there is any chance this could happen, then the new inode
  398. should be d_drop()ed and attached with d_splice_alias(). The
  399. returned dentry (if any) should be returned by ->mkdir().
  400. ``rmdir``
  401. called by the rmdir(2) system call. Only required if you want
  402. to support deleting subdirectories
  403. ``mknod``
  404. called by the mknod(2) system call to create a device (char,
  405. block) inode or a named pipe (FIFO) or socket. Only required if
  406. you want to support creating these types of inodes. You will
  407. probably need to call d_instantiate() just as you would in the
  408. create() method
  409. ``rename``
  410. called by the rename(2) system call to rename the object to have
  411. the parent and name given by the second inode and dentry.
  412. The filesystem must return -EINVAL for any unsupported or
  413. unknown flags. Currently the following flags are implemented:
  414. (1) RENAME_NOREPLACE: this flag indicates that if the target of
  415. the rename exists the rename should fail with -EEXIST instead of
  416. replacing the target. The VFS already checks for existence, so
  417. for local filesystems the RENAME_NOREPLACE implementation is
  418. equivalent to plain rename.
  419. (2) RENAME_EXCHANGE: exchange source and target. Both must
  420. exist; this is checked by the VFS. Unlike plain rename, source
  421. and target may be of different type.
  422. ``get_link``
  423. called by the VFS to follow a symbolic link to the inode it
  424. points to. Only required if you want to support symbolic links.
  425. This method returns the symlink body to traverse (and possibly
  426. resets the current position with nd_jump_link()). If the body
  427. won't go away until the inode is gone, nothing else is needed;
  428. if it needs to be otherwise pinned, arrange for its release by
  429. having get_link(..., ..., done) do set_delayed_call(done,
  430. destructor, argument). In that case destructor(argument) will
  431. be called once VFS is done with the body you've returned. May
  432. be called in RCU mode; that is indicated by NULL dentry
  433. argument. If request can't be handled without leaving RCU mode,
  434. have it return ERR_PTR(-ECHILD).
  435. If the filesystem stores the symlink target in ->i_link, the
  436. VFS may use it directly without calling ->get_link(); however,
  437. ->get_link() must still be provided. ->i_link must not be
  438. freed until after an RCU grace period. Writing to ->i_link
  439. post-iget() time requires a 'release' memory barrier.
  440. ``readlink``
  441. this is now just an override for use by readlink(2) for the
  442. cases when ->get_link uses nd_jump_link() or object is not in
  443. fact a symlink. Normally filesystems should only implement
  444. ->get_link for symlinks and readlink(2) will automatically use
  445. that.
  446. ``permission``
  447. called by the VFS to check for access rights on a POSIX-like
  448. filesystem.
  449. May be called in rcu-walk mode (mask & MAY_NOT_BLOCK). If in
  450. rcu-walk mode, the filesystem must check the permission without
  451. blocking or storing to the inode.
  452. If a situation is encountered that rcu-walk cannot handle,
  453. return
  454. -ECHILD and it will be called again in ref-walk mode.
  455. ``setattr``
  456. called by the VFS to set attributes for a file. This method is
  457. called by chmod(2) and related system calls.
  458. ``getattr``
  459. called by the VFS to get attributes of a file. This method is
  460. called by stat(2) and related system calls.
  461. ``listxattr``
  462. called by the VFS to list all extended attributes for a given
  463. file. This method is called by the listxattr(2) system call.
  464. ``update_time``
  465. called by the VFS to update a specific time or the i_version of
  466. an inode. If this is not defined the VFS will update the inode
  467. itself and call mark_inode_dirty_sync.
  468. ``sync_lazytime``:
  469. called by the writeback code to update the lazy time stamps to
  470. regular time stamp updates that get syncing into the on-disk
  471. inode.
  472. ``atomic_open``
  473. called on the last component of an open. Using this optional
  474. method the filesystem can look up, possibly create and open the
  475. file in one atomic operation. If it wants to leave actual
  476. opening to the caller (e.g. if the file turned out to be a
  477. symlink, device, or just something filesystem won't do atomic
  478. open for), it may signal this by returning finish_no_open(file,
  479. dentry). This method is only called if the last component is
  480. negative or needs lookup. Cached positive dentries are still
  481. handled by f_op->open(). If the file was created, FMODE_CREATED
  482. flag should be set in file->f_mode. In case of O_EXCL the
  483. method must only succeed if the file didn't exist and hence
  484. FMODE_CREATED shall always be set on success.
  485. ``tmpfile``
  486. called in the end of O_TMPFILE open(). Optional, equivalent to
  487. atomically creating, opening and unlinking a file in given
  488. directory. On success needs to return with the file already
  489. open; this can be done by calling finish_open_simple() right at
  490. the end.
  491. ``fileattr_get``
  492. called on ioctl(FS_IOC_GETFLAGS) and ioctl(FS_IOC_FSGETXATTR) to
  493. retrieve miscellaneous file flags and attributes. Also called
  494. before the relevant SET operation to check what is being changed
  495. (in this case with i_rwsem locked exclusive). If unset, then
  496. fall back to f_op->ioctl().
  497. ``fileattr_set``
  498. called on ioctl(FS_IOC_SETFLAGS) and ioctl(FS_IOC_FSSETXATTR) to
  499. change miscellaneous file flags and attributes. Callers hold
  500. i_rwsem exclusive. If unset, then fall back to f_op->ioctl().
  501. ``get_offset_ctx``
  502. called to get the offset context for a directory inode. A
  503. filesystem must define this operation to use
  504. simple_offset_dir_operations.
  505. The Address Space Object
  506. ========================
  507. The address space object is used to group and manage pages in the page
  508. cache. It can be used to keep track of the pages in a file (or anything
  509. else) and also track the mapping of sections of the file into process
  510. address spaces.
  511. There are a number of distinct yet related services that an
  512. address-space can provide. These include communicating memory pressure,
  513. page lookup by address, and keeping track of pages tagged as Dirty or
  514. Writeback.
  515. The first can be used independently to the others. The VM can try to
  516. release clean pages in order to reuse them. To do this it can call
  517. ->release_folio on clean folios with the private
  518. flag set. Clean pages without PagePrivate and with no external references
  519. will be released without notice being given to the address_space.
  520. To achieve this functionality, pages need to be placed on an LRU with
  521. lru_cache_add and mark_page_active needs to be called whenever the page
  522. is used.
  523. Pages are normally kept in a radix tree index by ->index. This tree
  524. maintains information about the PG_Dirty and PG_Writeback status of each
  525. page, so that pages with either of these flags can be found quickly.
  526. The Dirty tag is primarily used by mpage_writepages - the default
  527. ->writepages method. It uses the tag to find dirty pages to
  528. write back. If mpage_writepages is not used (i.e. the address
  529. provides its own ->writepages) , the PAGECACHE_TAG_DIRTY tag is almost
  530. unused. write_inode_now and sync_inode do use it (through
  531. __sync_single_inode) to check if ->writepages has been successful in
  532. writing out the whole address_space.
  533. The Writeback tag is used by filemap*wait* and sync_page* functions, via
  534. filemap_fdatawait_range, to wait for all writeback to complete.
  535. An address_space handler may attach extra information to a page,
  536. typically using the 'private' field in the 'struct page'. If such
  537. information is attached, the PG_Private flag should be set. This will
  538. cause various VM routines to make extra calls into the address_space
  539. handler to deal with that data.
  540. An address space acts as an intermediate between storage and
  541. application. Data is read into the address space a whole page at a
  542. time, and provided to the application either by copying of the page, or
  543. by memory-mapping the page. Data is written into the address space by
  544. the application, and then written-back to storage typically in whole
  545. pages, however the address_space has finer control of write sizes.
  546. The read process essentially only requires 'read_folio'. The write
  547. process is more complicated and uses write_begin/write_end or
  548. dirty_folio to write data into the address_space, and
  549. writepages to writeback data to storage.
  550. Removing pages from an address_space requires holding the inode's i_rwsem
  551. exclusively, while adding pages to the address_space requires holding the
  552. inode's i_mapping->invalidate_lock exclusively.
  553. When data is written to a page, the PG_Dirty flag should be set. It
  554. typically remains set until writepages asks for it to be written. This
  555. should clear PG_Dirty and set PG_Writeback. It can be actually written
  556. at any point after PG_Dirty is clear. Once it is known to be safe,
  557. PG_Writeback is cleared.
  558. Writeback makes use of a writeback_control structure to direct the
  559. operations. This gives the writepages operation some
  560. information about the nature of and reason for the writeback request,
  561. and the constraints under which it is being done. It is also used to
  562. return information back to the caller about the result of a
  563. writepages request.
  564. Handling errors during writeback
  565. --------------------------------
  566. Most applications that do buffered I/O will periodically call a file
  567. synchronization call (fsync, fdatasync, msync or sync_file_range) to
  568. ensure that data written has made it to the backing store. When there
  569. is an error during writeback, they expect that error to be reported when
  570. a file sync request is made. After an error has been reported on one
  571. request, subsequent requests on the same file descriptor should return
  572. 0, unless further writeback errors have occurred since the previous file
  573. synchronization.
  574. Ideally, the kernel would report errors only on file descriptions on
  575. which writes were done that subsequently failed to be written back. The
  576. generic pagecache infrastructure does not track the file descriptions
  577. that have dirtied each individual page however, so determining which
  578. file descriptors should get back an error is not possible.
  579. Instead, the generic writeback error tracking infrastructure in the
  580. kernel settles for reporting errors to fsync on all file descriptions
  581. that were open at the time that the error occurred. In a situation with
  582. multiple writers, all of them will get back an error on a subsequent
  583. fsync, even if all of the writes done through that particular file
  584. descriptor succeeded (or even if there were no writes on that file
  585. descriptor at all).
  586. Filesystems that wish to use this infrastructure should call
  587. mapping_set_error to record the error in the address_space when it
  588. occurs. Then, after writing back data from the pagecache in their
  589. file->fsync operation, they should call file_check_and_advance_wb_err to
  590. ensure that the struct file's error cursor has advanced to the correct
  591. point in the stream of errors emitted by the backing device(s).
  592. struct address_space_operations
  593. -------------------------------
  594. This describes how the VFS can manipulate mapping of a file to page
  595. cache in your filesystem. The following members are defined:
  596. .. code-block:: c
  597. struct address_space_operations {
  598. int (*read_folio)(struct file *, struct folio *);
  599. int (*writepages)(struct address_space *, struct writeback_control *);
  600. bool (*dirty_folio)(struct address_space *, struct folio *);
  601. void (*readahead)(struct readahead_control *);
  602. int (*write_begin)(const struct kiocb *, struct address_space *mapping,
  603. loff_t pos, unsigned len,
  604. struct page **pagep, void **fsdata);
  605. int (*write_end)(const struct kiocb *, struct address_space *mapping,
  606. loff_t pos, unsigned len, unsigned copied,
  607. struct folio *folio, void *fsdata);
  608. sector_t (*bmap)(struct address_space *, sector_t);
  609. void (*invalidate_folio) (struct folio *, size_t start, size_t len);
  610. bool (*release_folio)(struct folio *, gfp_t);
  611. void (*free_folio)(struct folio *);
  612. ssize_t (*direct_IO)(struct kiocb *, struct iov_iter *iter);
  613. int (*migrate_folio)(struct mapping *, struct folio *dst,
  614. struct folio *src, enum migrate_mode);
  615. int (*launder_folio) (struct folio *);
  616. bool (*is_partially_uptodate) (struct folio *, size_t from,
  617. size_t count);
  618. void (*is_dirty_writeback)(struct folio *, bool *, bool *);
  619. int (*error_remove_folio)(struct mapping *mapping, struct folio *);
  620. int (*swap_activate)(struct swap_info_struct *sis, struct file *f, sector_t *span)
  621. int (*swap_deactivate)(struct file *);
  622. int (*swap_rw)(struct kiocb *iocb, struct iov_iter *iter);
  623. };
  624. ``read_folio``
  625. Called by the page cache to read a folio from the backing store.
  626. The 'file' argument supplies authentication information to network
  627. filesystems, and is generally not used by block based filesystems.
  628. It may be NULL if the caller does not have an open file (eg if
  629. the kernel is performing a read for itself rather than on behalf
  630. of a userspace process with an open file).
  631. If the mapping does not support large folios, the folio will
  632. contain a single page. The folio will be locked when read_folio
  633. is called. If the read completes successfully, the folio should
  634. be marked uptodate. The filesystem should unlock the folio
  635. once the read has completed, whether it was successful or not.
  636. The filesystem does not need to modify the refcount on the folio;
  637. the page cache holds a reference count and that will not be
  638. released until the folio is unlocked.
  639. Filesystems may implement ->read_folio() synchronously.
  640. In normal operation, folios are read through the ->readahead()
  641. method. Only if this fails, or if the caller needs to wait for
  642. the read to complete will the page cache call ->read_folio().
  643. Filesystems should not attempt to perform their own readahead
  644. in the ->read_folio() operation.
  645. If the filesystem cannot perform the read at this time, it can
  646. unlock the folio, do whatever action it needs to ensure that the
  647. read will succeed in the future and return AOP_TRUNCATED_PAGE.
  648. In this case, the caller should look up the folio, lock it,
  649. and call ->read_folio again.
  650. Callers may invoke the ->read_folio() method directly, but using
  651. read_mapping_folio() will take care of locking, waiting for the
  652. read to complete and handle cases such as AOP_TRUNCATED_PAGE.
  653. ``writepages``
  654. called by the VM to write out pages associated with the
  655. address_space object. If wbc->sync_mode is WB_SYNC_ALL, then
  656. the writeback_control will specify a range of pages that must be
  657. written out. If it is WB_SYNC_NONE, then a nr_to_write is
  658. given and that many pages should be written if possible. If no
  659. ->writepages is given, then mpage_writepages is used instead.
  660. This will choose pages from the address space that are tagged as
  661. DIRTY and will write them back.
  662. ``dirty_folio``
  663. called by the VM to mark a folio as dirty. This is particularly
  664. needed if an address space attaches private data to a folio, and
  665. that data needs to be updated when a folio is dirtied. This is
  666. called, for example, when a memory mapped page gets modified.
  667. If defined, it should set the folio dirty flag, and the
  668. PAGECACHE_TAG_DIRTY search mark in i_pages.
  669. ``readahead``
  670. Called by the VM to read pages associated with the address_space
  671. object. The pages are consecutive in the page cache and are
  672. locked. The implementation should decrement the page refcount
  673. after starting I/O on each page. Usually the page will be
  674. unlocked by the I/O completion handler. The set of pages are
  675. divided into some sync pages followed by some async pages,
  676. rac->ra->async_size gives the number of async pages. The
  677. filesystem should attempt to read all sync pages but may decide
  678. to stop once it reaches the async pages. If it does decide to
  679. stop attempting I/O, it can simply return. The caller will
  680. remove the remaining pages from the address space, unlock them
  681. and decrement the page refcount. Set PageUptodate if the I/O
  682. completes successfully.
  683. ``write_begin``
  684. Called by the generic buffered write code to ask the filesystem
  685. to prepare to write len bytes at the given offset in the file.
  686. The address_space should check that the write will be able to
  687. complete, by allocating space if necessary and doing any other
  688. internal housekeeping. If the write will update parts of any
  689. basic-blocks on storage, then those blocks should be pre-read
  690. (if they haven't been read already) so that the updated blocks
  691. can be written out properly.
  692. The filesystem must return the locked pagecache folio for the
  693. specified offset, in ``*foliop``, for the caller to write into.
  694. It must be able to cope with short writes (where the length
  695. passed to write_begin is greater than the number of bytes copied
  696. into the folio).
  697. A void * may be returned in fsdata, which then gets passed into
  698. write_end.
  699. Returns 0 on success; < 0 on failure (which is the error code),
  700. in which case write_end is not called.
  701. ``write_end``
  702. After a successful write_begin, and data copy, write_end must be
  703. called. len is the original len passed to write_begin, and
  704. copied is the amount that was able to be copied.
  705. The filesystem must take care of unlocking the folio,
  706. decrementing its refcount, and updating i_size.
  707. Returns < 0 on failure, otherwise the number of bytes (<=
  708. 'copied') that were able to be copied into pagecache.
  709. ``bmap``
  710. called by the VFS to map a logical block offset within object to
  711. physical block number. This method is used by the FIBMAP ioctl
  712. and for working with swap-files. To be able to swap to a file,
  713. the file must have a stable mapping to a block device. The swap
  714. system does not go through the filesystem but instead uses bmap
  715. to find out where the blocks in the file are and uses those
  716. addresses directly.
  717. ``invalidate_folio``
  718. If a folio has private data, then invalidate_folio will be
  719. called when part or all of the folio is to be removed from the
  720. address space. This generally corresponds to either a
  721. truncation, punch hole or a complete invalidation of the address
  722. space (in the latter case 'offset' will always be 0 and 'length'
  723. will be folio_size()). Any private data associated with the folio
  724. should be updated to reflect this truncation. If offset is 0
  725. and length is folio_size(), then the private data should be
  726. released, because the folio must be able to be completely
  727. discarded. This may be done by calling the ->release_folio
  728. function, but in this case the release MUST succeed.
  729. ``release_folio``
  730. release_folio is called on folios with private data to tell the
  731. filesystem that the folio is about to be freed. ->release_folio
  732. should remove any private data from the folio and clear the
  733. private flag. If release_folio() fails, it should return false.
  734. release_folio() is used in two distinct though related cases.
  735. The first is when the VM wants to free a clean folio with no
  736. active users. If ->release_folio succeeds, the folio will be
  737. removed from the address_space and be freed.
  738. The second case is when a request has been made to invalidate
  739. some or all folios in an address_space. This can happen
  740. through the fadvise(POSIX_FADV_DONTNEED) system call or by the
  741. filesystem explicitly requesting it as nfs and 9p do (when they
  742. believe the cache may be out of date with storage) by calling
  743. invalidate_inode_pages2(). If the filesystem makes such a call,
  744. and needs to be certain that all folios are invalidated, then
  745. its release_folio will need to ensure this. Possibly it can
  746. clear the uptodate flag if it cannot free private data yet.
  747. ``free_folio``
  748. free_folio is called once the folio is no longer visible in the
  749. page cache in order to allow the cleanup of any private data.
  750. Since it may be called by the memory reclaimer, it should not
  751. assume that the original address_space mapping still exists, and
  752. it should not block.
  753. ``direct_IO``
  754. called by the generic read/write routines to perform direct_IO -
  755. that is IO requests which bypass the page cache and transfer
  756. data directly between the storage and the application's address
  757. space.
  758. ``migrate_folio``
  759. This is used to compact the physical memory usage. If the VM
  760. wants to relocate a folio (maybe from a memory device that is
  761. signalling imminent failure) it will pass a new folio and an old
  762. folio to this function. migrate_folio should transfer any private
  763. data across and update any references that it has to the folio.
  764. ``launder_folio``
  765. Called before freeing a folio - it writes back the dirty folio.
  766. To prevent redirtying the folio, it is kept locked during the
  767. whole operation.
  768. ``is_partially_uptodate``
  769. Called by the VM when reading a file through the pagecache when
  770. the underlying blocksize is smaller than the size of the folio.
  771. If the required block is up to date then the read can complete
  772. without needing I/O to bring the whole page up to date.
  773. ``is_dirty_writeback``
  774. Called by the VM when attempting to reclaim a folio. The VM uses
  775. dirty and writeback information to determine if it needs to
  776. stall to allow flushers a chance to complete some IO.
  777. Ordinarily it can use folio_test_dirty and folio_test_writeback but
  778. some filesystems have more complex state (unstable folios in NFS
  779. prevent reclaim) or do not set those flags due to locking
  780. problems. This callback allows a filesystem to indicate to the
  781. VM if a folio should be treated as dirty or writeback for the
  782. purposes of stalling.
  783. ``error_remove_folio``
  784. normally set to generic_error_remove_folio if truncation is ok
  785. for this address space. Used for memory failure handling.
  786. Setting this implies you deal with pages going away under you,
  787. unless you have them locked or reference counts increased.
  788. ``swap_activate``
  789. Called to prepare the given file for swap. It should perform
  790. any validation and preparation necessary to ensure that writes
  791. can be performed with minimal memory allocation. It should call
  792. add_swap_extent(), or the helper iomap_swapfile_activate(), and
  793. return the number of extents added. If IO should be submitted
  794. through ->swap_rw(), it should set SWP_FS_OPS, otherwise IO will
  795. be submitted directly to the block device ``sis->bdev``.
  796. ``swap_deactivate``
  797. Called during swapoff on files where swap_activate was
  798. successful.
  799. ``swap_rw``
  800. Called to read or write swap pages when SWP_FS_OPS is set.
  801. The File Object
  802. ===============
  803. A file object represents a file opened by a process. This is also known
  804. as an "open file description" in POSIX parlance.
  805. struct file_operations
  806. ----------------------
  807. This describes how the VFS can manipulate an open file. As of kernel
  808. 4.18, the following members are defined:
  809. .. code-block:: c
  810. struct file_operations {
  811. struct module *owner;
  812. fop_flags_t fop_flags;
  813. loff_t (*llseek) (struct file *, loff_t, int);
  814. ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
  815. ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
  816. ssize_t (*read_iter) (struct kiocb *, struct iov_iter *);
  817. ssize_t (*write_iter) (struct kiocb *, struct iov_iter *);
  818. int (*iopoll)(struct kiocb *kiocb, struct io_comp_batch *,
  819. unsigned int flags);
  820. int (*iterate_shared) (struct file *, struct dir_context *);
  821. __poll_t (*poll) (struct file *, struct poll_table_struct *);
  822. long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
  823. long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
  824. int (*mmap) (struct file *, struct vm_area_struct *);
  825. int (*open) (struct inode *, struct file *);
  826. int (*flush) (struct file *, fl_owner_t id);
  827. int (*release) (struct inode *, struct file *);
  828. int (*fsync) (struct file *, loff_t, loff_t, int datasync);
  829. int (*fasync) (int, struct file *, int);
  830. int (*lock) (struct file *, int, struct file_lock *);
  831. unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
  832. int (*check_flags)(int);
  833. int (*flock) (struct file *, int, struct file_lock *);
  834. ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
  835. ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
  836. void (*splice_eof)(struct file *file);
  837. int (*setlease)(struct file *, int, struct file_lease **, void **);
  838. long (*fallocate)(struct file *file, int mode, loff_t offset,
  839. loff_t len);
  840. void (*show_fdinfo)(struct seq_file *m, struct file *f);
  841. #ifndef CONFIG_MMU
  842. unsigned (*mmap_capabilities)(struct file *);
  843. #endif
  844. ssize_t (*copy_file_range)(struct file *, loff_t, struct file *,
  845. loff_t, size_t, unsigned int);
  846. loff_t (*remap_file_range)(struct file *file_in, loff_t pos_in,
  847. struct file *file_out, loff_t pos_out,
  848. loff_t len, unsigned int remap_flags);
  849. int (*fadvise)(struct file *, loff_t, loff_t, int);
  850. int (*uring_cmd)(struct io_uring_cmd *ioucmd, unsigned int issue_flags);
  851. int (*uring_cmd_iopoll)(struct io_uring_cmd *, struct io_comp_batch *,
  852. unsigned int poll_flags);
  853. int (*mmap_prepare)(struct vm_area_desc *);
  854. };
  855. Again, all methods are called without any locks being held, unless
  856. otherwise noted.
  857. ``llseek``
  858. called when the VFS needs to move the file position index
  859. ``read``
  860. called by read(2) and related system calls
  861. ``read_iter``
  862. possibly asynchronous read with iov_iter as destination
  863. ``write``
  864. called by write(2) and related system calls
  865. ``write_iter``
  866. possibly asynchronous write with iov_iter as source
  867. ``iopoll``
  868. called when aio wants to poll for completions on HIPRI iocbs
  869. ``iterate_shared``
  870. called when the VFS needs to read the directory contents
  871. ``poll``
  872. called by the VFS when a process wants to check if there is
  873. activity on this file and (optionally) go to sleep until there
  874. is activity. Called by the select(2) and poll(2) system calls
  875. ``unlocked_ioctl``
  876. called by the ioctl(2) system call.
  877. ``compat_ioctl``
  878. called by the ioctl(2) system call when 32 bit system calls are
  879. used on 64 bit kernels.
  880. ``mmap``
  881. called by the mmap(2) system call. Deprecated in favour of
  882. ``mmap_prepare``.
  883. ``open``
  884. called by the VFS when an inode should be opened. When the VFS
  885. opens a file, it creates a new "struct file". It then calls the
  886. open method for the newly allocated file structure. You might
  887. think that the open method really belongs in "struct
  888. inode_operations", and you may be right. I think it's done the
  889. way it is because it makes filesystems simpler to implement.
  890. The open() method is a good place to initialize the
  891. "private_data" member in the file structure if you want to point
  892. to a device structure
  893. ``flush``
  894. called by the close(2) system call to flush a file
  895. ``release``
  896. called when the last reference to an open file is closed
  897. ``fsync``
  898. called by the fsync(2) system call. Also see the section above
  899. entitled "Handling errors during writeback".
  900. ``fasync``
  901. called by the fcntl(2) system call when asynchronous
  902. (non-blocking) mode is enabled for a file
  903. ``lock``
  904. called by the fcntl(2) system call for F_GETLK, F_SETLK, and
  905. F_SETLKW commands
  906. ``get_unmapped_area``
  907. called by the mmap(2) system call
  908. ``check_flags``
  909. called by the fcntl(2) system call for F_SETFL command
  910. ``flock``
  911. called by the flock(2) system call
  912. ``splice_write``
  913. called by the VFS to splice data from a pipe to a file. This
  914. method is used by the splice(2) system call
  915. ``splice_read``
  916. called by the VFS to splice data from file to a pipe. This
  917. method is used by the splice(2) system call
  918. ``setlease``
  919. called by the VFS to set or release a file lock lease. Local
  920. filesystems that wish to use the kernel-internal lease implementation
  921. should set this to generic_setlease(). Other setlease implementations
  922. should call generic_setlease() to record or remove the lease in the inode
  923. after setting it. When set to NULL, attempts to set or remove a lease will
  924. return -EINVAL.
  925. ``fallocate``
  926. called by the VFS to preallocate blocks or punch a hole.
  927. ``copy_file_range``
  928. called by the copy_file_range(2) system call.
  929. ``remap_file_range``
  930. called by the ioctl(2) system call for FICLONERANGE and FICLONE
  931. and FIDEDUPERANGE commands to remap file ranges. An
  932. implementation should remap len bytes at pos_in of the source
  933. file into the dest file at pos_out. Implementations must handle
  934. callers passing in len == 0; this means "remap to the end of the
  935. source file". The return value should the number of bytes
  936. remapped, or the usual negative error code if errors occurred
  937. before any bytes were remapped. The remap_flags parameter
  938. accepts REMAP_FILE_* flags. If REMAP_FILE_DEDUP is set then the
  939. implementation must only remap if the requested file ranges have
  940. identical contents. If REMAP_FILE_CAN_SHORTEN is set, the caller is
  941. ok with the implementation shortening the request length to
  942. satisfy alignment or EOF requirements (or any other reason).
  943. ``fadvise``
  944. possibly called by the fadvise64() system call.
  945. ``mmap_prepare``
  946. Called by the mmap(2) system call. Allows a VFS to set up a
  947. file-backed memory mapping, most notably establishing relevant
  948. private state and VMA callbacks.
  949. If further action such as pre-population of page tables is required,
  950. this can be specified by the vm_area_desc->action field and related
  951. parameters.
  952. Note that the file operations are implemented by the specific
  953. filesystem in which the inode resides. When opening a device node
  954. (character or block special) most filesystems will call special
  955. support routines in the VFS which will locate the required device
  956. driver information. These support routines replace the filesystem file
  957. operations with those for the device driver, and then proceed to call
  958. the new open() method for the file. This is how opening a device file
  959. in the filesystem eventually ends up calling the device driver open()
  960. method.
  961. Directory Entry Cache (dcache)
  962. ==============================
  963. struct dentry_operations
  964. ------------------------
  965. This describes how a filesystem can overload the standard dentry
  966. operations. Dentries and the dcache are the domain of the VFS and the
  967. individual filesystem implementations. Device drivers have no business
  968. here. These methods may be set to NULL, as they are either optional or
  969. the VFS uses a default. As of kernel 2.6.22, the following members are
  970. defined:
  971. .. code-block:: c
  972. struct dentry_operations {
  973. int (*d_revalidate)(struct inode *, const struct qstr *,
  974. struct dentry *, unsigned int);
  975. int (*d_weak_revalidate)(struct dentry *, unsigned int);
  976. int (*d_hash)(const struct dentry *, struct qstr *);
  977. int (*d_compare)(const struct dentry *,
  978. unsigned int, const char *, const struct qstr *);
  979. int (*d_delete)(const struct dentry *);
  980. int (*d_init)(struct dentry *);
  981. void (*d_release)(struct dentry *);
  982. void (*d_iput)(struct dentry *, struct inode *);
  983. char *(*d_dname)(struct dentry *, char *, int);
  984. struct vfsmount *(*d_automount)(struct path *);
  985. int (*d_manage)(const struct path *, bool);
  986. struct dentry *(*d_real)(struct dentry *, enum d_real_type type);
  987. bool (*d_unalias_trylock)(const struct dentry *);
  988. void (*d_unalias_unlock)(const struct dentry *);
  989. };
  990. ``d_revalidate``
  991. called when the VFS needs to revalidate a dentry. This is
  992. called whenever a name look-up finds a dentry in the dcache.
  993. Most local filesystems leave this as NULL, because all their
  994. dentries in the dcache are valid. Network filesystems are
  995. different since things can change on the server without the
  996. client necessarily being aware of it.
  997. This function should return a positive value if the dentry is
  998. still valid, and zero or a negative error code if it isn't.
  999. d_revalidate may be called in rcu-walk mode (flags &
  1000. LOOKUP_RCU). If in rcu-walk mode, the filesystem must
  1001. revalidate the dentry without blocking or storing to the dentry,
  1002. d_parent and d_inode should not be used without care (because
  1003. they can change and, in d_inode case, even become NULL under
  1004. us).
  1005. If a situation is encountered that rcu-walk cannot handle,
  1006. return
  1007. -ECHILD and it will be called again in ref-walk mode.
  1008. ``d_weak_revalidate``
  1009. called when the VFS needs to revalidate a "jumped" dentry. This
  1010. is called when a path-walk ends at dentry that was not acquired
  1011. by doing a lookup in the parent directory. This includes "/",
  1012. "." and "..", as well as procfs-style symlinks and mountpoint
  1013. traversal.
  1014. In this case, we are less concerned with whether the dentry is
  1015. still fully correct, but rather that the inode is still valid.
  1016. As with d_revalidate, most local filesystems will set this to
  1017. NULL since their dcache entries are always valid.
  1018. This function has the same return code semantics as
  1019. d_revalidate.
  1020. d_weak_revalidate is only called after leaving rcu-walk mode.
  1021. ``d_hash``
  1022. called when the VFS adds a dentry to the hash table. The first
  1023. dentry passed to d_hash is the parent directory that the name is
  1024. to be hashed into.
  1025. Same locking and synchronisation rules as d_compare regarding
  1026. what is safe to dereference etc.
  1027. ``d_compare``
  1028. called to compare a dentry name with a given name. The first
  1029. dentry is the parent of the dentry to be compared, the second is
  1030. the child dentry. len and name string are properties of the
  1031. dentry to be compared. qstr is the name to compare it with.
  1032. Must be constant and idempotent, and should not take locks if
  1033. possible, and should not or store into the dentry. Should not
  1034. dereference pointers outside the dentry without lots of care
  1035. (eg. d_parent, d_inode, d_name should not be used).
  1036. However, our vfsmount is pinned, and RCU held, so the dentries
  1037. and inodes won't disappear, neither will our sb or filesystem
  1038. module. ->d_sb may be used.
  1039. It is a tricky calling convention because it needs to be called
  1040. under "rcu-walk", ie. without any locks or references on things.
  1041. ``d_delete``
  1042. called when the last reference to a dentry is dropped and the
  1043. dcache is deciding whether or not to cache it. Return 1 to
  1044. delete immediately, or 0 to cache the dentry. Default is NULL
  1045. which means to always cache a reachable dentry. d_delete must
  1046. be constant and idempotent.
  1047. ``d_init``
  1048. called when a dentry is allocated
  1049. ``d_release``
  1050. called when a dentry is really deallocated
  1051. ``d_iput``
  1052. called when a dentry loses its inode (just prior to its being
  1053. deallocated). The default when this is NULL is that the VFS
  1054. calls iput(). If you define this method, you must call iput()
  1055. yourself
  1056. ``d_dname``
  1057. called when the pathname of a dentry should be generated.
  1058. Useful for some pseudo filesystems (sockfs, pipefs, ...) to
  1059. delay pathname generation. (Instead of doing it when dentry is
  1060. created, it's done only when the path is needed.). Real
  1061. filesystems probably dont want to use it, because their dentries
  1062. are present in global dcache hash, so their hash should be an
  1063. invariant. As no lock is held, d_dname() should not try to
  1064. modify the dentry itself, unless appropriate SMP safety is used.
  1065. CAUTION : d_path() logic is quite tricky. The correct way to
  1066. return for example "Hello" is to put it at the end of the
  1067. buffer, and returns a pointer to the first char.
  1068. dynamic_dname() helper function is provided to take care of
  1069. this.
  1070. Example :
  1071. .. code-block:: c
  1072. static char *pipefs_dname(struct dentry *dent, char *buffer, int buflen)
  1073. {
  1074. return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
  1075. dentry->d_inode->i_ino);
  1076. }
  1077. ``d_automount``
  1078. called when an automount dentry is to be traversed (optional).
  1079. This should create a new VFS mount record and return the record
  1080. to the caller. The caller is supplied with a path parameter
  1081. giving the automount directory to describe the automount target
  1082. and the parent VFS mount record to provide inheritable mount
  1083. parameters. NULL should be returned if someone else managed to
  1084. make the automount first. If the vfsmount creation failed, then
  1085. an error code should be returned. If -EISDIR is returned, then
  1086. the directory will be treated as an ordinary directory and
  1087. returned to pathwalk to continue walking.
  1088. If a vfsmount is returned, the caller will attempt to mount it
  1089. on the mountpoint and will remove the vfsmount from its
  1090. expiration list in the case of failure.
  1091. This function is only used if DCACHE_NEED_AUTOMOUNT is set on
  1092. the dentry. This is set by __d_instantiate() if S_AUTOMOUNT is
  1093. set on the inode being added.
  1094. ``d_manage``
  1095. called to allow the filesystem to manage the transition from a
  1096. dentry (optional). This allows autofs, for example, to hold up
  1097. clients waiting to explore behind a 'mountpoint' while letting
  1098. the daemon go past and construct the subtree there. 0 should be
  1099. returned to let the calling process continue. -EISDIR can be
  1100. returned to tell pathwalk to use this directory as an ordinary
  1101. directory and to ignore anything mounted on it and not to check
  1102. the automount flag. Any other error code will abort pathwalk
  1103. completely.
  1104. If the 'rcu_walk' parameter is true, then the caller is doing a
  1105. pathwalk in RCU-walk mode. Sleeping is not permitted in this
  1106. mode, and the caller can be asked to leave it and call again by
  1107. returning -ECHILD. -EISDIR may also be returned to tell
  1108. pathwalk to ignore d_automount or any mounts.
  1109. This function is only used if DCACHE_MANAGE_TRANSIT is set on
  1110. the dentry being transited from.
  1111. ``d_real``
  1112. overlay/union type filesystems implement this method to return one
  1113. of the underlying dentries of a regular file hidden by the overlay.
  1114. The 'type' argument takes the values D_REAL_DATA or D_REAL_METADATA
  1115. for returning the real underlying dentry that refers to the inode
  1116. hosting the file's data or metadata respectively.
  1117. For non-regular files, the 'dentry' argument is returned.
  1118. ``d_unalias_trylock``
  1119. if present, will be called by d_splice_alias() before moving a
  1120. preexisting attached alias. Returning false prevents __d_move(),
  1121. making d_splice_alias() fail with -ESTALE.
  1122. Rationale: setting FS_RENAME_DOES_D_MOVE will prevent d_move()
  1123. and d_exchange() calls from the outside of filesystem methods;
  1124. however, it does not guarantee that attached dentries won't
  1125. be renamed or moved by d_splice_alias() finding a preexisting
  1126. alias for a directory inode. Normally we would not care;
  1127. however, something that wants to stabilize the entire path to
  1128. root over a blocking operation might need that. See 9p for one
  1129. (and hopefully only) example.
  1130. ``d_unalias_unlock``
  1131. should be paired with ``d_unalias_trylock``; that one is called after
  1132. __d_move() call in __d_unalias().
  1133. Each dentry has a pointer to its parent dentry, as well as a hash list
  1134. of child dentries. Child dentries are basically like files in a
  1135. directory.
  1136. Directory Entry Cache API
  1137. --------------------------
  1138. There are a number of functions defined which permit a filesystem to
  1139. manipulate dentries:
  1140. ``dget``
  1141. open a new handle for an existing dentry (this just increments
  1142. the usage count)
  1143. ``dput``
  1144. close a handle for a dentry (decrements the usage count). If
  1145. the usage count drops to 0, and the dentry is still in its
  1146. parent's hash, the "d_delete" method is called to check whether
  1147. it should be cached. If it should not be cached, or if the
  1148. dentry is not hashed, it is deleted. Otherwise cached dentries
  1149. are put into an LRU list to be reclaimed on memory shortage.
  1150. ``d_drop``
  1151. this unhashes a dentry from its parents hash list. A subsequent
  1152. call to dput() will deallocate the dentry if its usage count
  1153. drops to 0
  1154. ``d_delete``
  1155. delete a dentry. If there are no other open references to the
  1156. dentry then the dentry is turned into a negative dentry (the
  1157. d_iput() method is called). If there are other references, then
  1158. d_drop() is called instead
  1159. ``d_add``
  1160. add a dentry to its parents hash list and then calls
  1161. d_instantiate()
  1162. ``d_instantiate``
  1163. add a dentry to the alias hash list for the inode and updates
  1164. the "d_inode" member. The "i_count" member in the inode
  1165. structure should be set/incremented. If the inode pointer is
  1166. NULL, the dentry is called a "negative dentry". This function
  1167. is commonly called when an inode is created for an existing
  1168. negative dentry
  1169. ``d_lookup``
  1170. look up a dentry given its parent and path name component It
  1171. looks up the child of that given name from the dcache hash
  1172. table. If it is found, the reference count is incremented and
  1173. the dentry is returned. The caller must use dput() to free the
  1174. dentry when it finishes using it.
  1175. Mount Options
  1176. =============
  1177. Parsing options
  1178. ---------------
  1179. On mount and remount the filesystem is passed a string containing a
  1180. comma separated list of mount options. The options can have either of
  1181. these forms:
  1182. option
  1183. option=value
  1184. The <linux/parser.h> header defines an API that helps parse these
  1185. options. There are plenty of examples on how to use it in existing
  1186. filesystems.
  1187. Showing options
  1188. ---------------
  1189. If a filesystem accepts mount options, it must define show_options() to
  1190. show all the currently active options. The rules are:
  1191. - options MUST be shown which are not default or their values differ
  1192. from the default
  1193. - options MAY be shown which are enabled by default or have their
  1194. default value
  1195. Options used only internally between a mount helper and the kernel (such
  1196. as file descriptors), or which only have an effect during the mounting
  1197. (such as ones controlling the creation of a journal) are exempt from the
  1198. above rules.
  1199. The underlying reason for the above rules is to make sure, that a mount
  1200. can be accurately replicated (e.g. umounting and mounting again) based
  1201. on the information found in /proc/mounts.
  1202. Resources
  1203. =========
  1204. (Note some of these resources are not up-to-date with the latest kernel
  1205. version.)
  1206. Creating Linux virtual filesystems. 2002
  1207. <https://lwn.net/Articles/13325/>
  1208. The Linux Virtual File-system Layer by Neil Brown. 1999
  1209. <http://www.cse.unsw.edu.au/~neilb/oss/linux-commentary/vfs.html>
  1210. A tour of the Linux VFS by Michael K. Johnson. 1996
  1211. <https://www.tldp.org/LDP/khg/HyperNews/get/fs/vfstour.html>
  1212. A small trail through the Linux kernel by Andries Brouwer. 2001
  1213. <https://www.win.tue.nl/~aeb/linux/vfs/trail.html>