1af96c1e3STobin C. Harding.. SPDX-License-Identifier: GPL-2.0
2af96c1e3STobin C. Harding
3af96c1e3STobin C. Harding=========================================
4af96c1e3STobin C. HardingOverview of the Linux Virtual File System
5af96c1e3STobin C. Harding=========================================
6af96c1e3STobin C. Harding
7af96c1e3STobin C. HardingOriginal author: Richard Gooch <rgooch@atnf.csiro.au>
8af96c1e3STobin C. Harding
9af96c1e3STobin C. Harding- Copyright (C) 1999 Richard Gooch
10af96c1e3STobin C. Harding- Copyright (C) 2005 Pekka Enberg
11af96c1e3STobin C. Harding
12af96c1e3STobin C. Harding
13af96c1e3STobin C. HardingIntroduction
14af96c1e3STobin C. Harding============
15af96c1e3STobin C. Harding
16af96c1e3STobin C. HardingThe Virtual File System (also known as the Virtual Filesystem Switch) is
17af96c1e3STobin C. Hardingthe software layer in the kernel that provides the filesystem interface
18af96c1e3STobin C. Hardingto userspace programs.  It also provides an abstraction within the
19af96c1e3STobin C. Hardingkernel which allows different filesystem implementations to coexist.
20af96c1e3STobin C. Harding
21af96c1e3STobin C. HardingVFS system calls open(2), stat(2), read(2), write(2), chmod(2) and so on
22af96c1e3STobin C. Hardingare called from a process context.  Filesystem locking is described in
23ec23eb54SMauro Carvalho Chehabthe document Documentation/filesystems/locking.rst.
24af96c1e3STobin C. Harding
25af96c1e3STobin C. Harding
26af96c1e3STobin C. HardingDirectory Entry Cache (dcache)
27af96c1e3STobin C. Harding------------------------------
28af96c1e3STobin C. Harding
29af96c1e3STobin C. HardingThe VFS implements the open(2), stat(2), chmod(2), and similar system
30af96c1e3STobin C. Hardingcalls.  The pathname argument that is passed to them is used by the VFS
31af96c1e3STobin C. Hardingto search through the directory entry cache (also known as the dentry
32af96c1e3STobin C. Hardingcache or dcache).  This provides a very fast look-up mechanism to
33af96c1e3STobin C. Hardingtranslate a pathname (filename) into a specific dentry.  Dentries live
34af96c1e3STobin C. Hardingin RAM and are never saved to disc: they exist only for performance.
35af96c1e3STobin C. Harding
36af96c1e3STobin C. HardingThe dentry cache is meant to be a view into your entire filespace.  As
37af96c1e3STobin C. Hardingmost computers cannot fit all dentries in the RAM at the same time, some
38af96c1e3STobin C. Hardingbits of the cache are missing.  In order to resolve your pathname into a
39af96c1e3STobin C. Hardingdentry, the VFS may have to resort to creating dentries along the way,
40af96c1e3STobin C. Hardingand then loading the inode.  This is done by looking up the inode.
41af96c1e3STobin C. Harding
42af96c1e3STobin C. Harding
43af96c1e3STobin C. HardingThe Inode Object
44af96c1e3STobin C. Harding----------------
45af96c1e3STobin C. Harding
46af96c1e3STobin C. HardingAn individual dentry usually has a pointer to an inode.  Inodes are
47af96c1e3STobin C. Hardingfilesystem objects such as regular files, directories, FIFOs and other
48af96c1e3STobin C. Hardingbeasts.  They live either on the disc (for block device filesystems) or
49af96c1e3STobin C. Hardingin the memory (for pseudo filesystems).  Inodes that live on the disc
50af96c1e3STobin C. Hardingare copied into the memory when required and changes to the inode are
51af96c1e3STobin C. Hardingwritten back to disc.  A single inode can be pointed to by multiple
52af96c1e3STobin C. Hardingdentries (hard links, for example, do this).
53af96c1e3STobin C. Harding
54af96c1e3STobin C. HardingTo look up an inode requires that the VFS calls the lookup() method of
55af96c1e3STobin C. Hardingthe parent directory inode.  This method is installed by the specific
56af96c1e3STobin C. Hardingfilesystem implementation that the inode lives in.  Once the VFS has the
57af96c1e3STobin C. Hardingrequired dentry (and hence the inode), we can do all those boring things
58af96c1e3STobin C. Hardinglike open(2) the file, or stat(2) it to peek at the inode data.  The
59af96c1e3STobin C. Hardingstat(2) operation is fairly simple: once the VFS has the dentry, it
60af96c1e3STobin C. Hardingpeeks at the inode data and passes some of it back to userspace.
61af96c1e3STobin C. Harding
62af96c1e3STobin C. Harding
63af96c1e3STobin C. HardingThe File Object
64af96c1e3STobin C. Harding---------------
65af96c1e3STobin C. Harding
66af96c1e3STobin C. HardingOpening a file requires another operation: allocation of a file
67af96c1e3STobin C. Hardingstructure (this is the kernel-side implementation of file descriptors).
68af96c1e3STobin C. HardingThe freshly allocated file structure is initialized with a pointer to
69af96c1e3STobin C. Hardingthe dentry and a set of file operation member functions.  These are
70af96c1e3STobin C. Hardingtaken from the inode data.  The open() file method is then called so the
71af96c1e3STobin C. Hardingspecific filesystem implementation can do its work.  You can see that
72af96c1e3STobin C. Hardingthis is another switch performed by the VFS.  The file structure is
73af96c1e3STobin C. Hardingplaced into the file descriptor table for the process.
74af96c1e3STobin C. Harding
75af96c1e3STobin C. HardingReading, writing and closing files (and other assorted VFS operations)
76af96c1e3STobin C. Hardingis done by using the userspace file descriptor to grab the appropriate
77af96c1e3STobin C. Hardingfile structure, and then calling the required file structure method to
78af96c1e3STobin C. Hardingdo whatever is required.  For as long as the file is open, it keeps the
79af96c1e3STobin C. Hardingdentry in use, which in turn means that the VFS inode is still in use.
80af96c1e3STobin C. Harding
81af96c1e3STobin C. Harding
82af96c1e3STobin C. HardingRegistering and Mounting a Filesystem
83af96c1e3STobin C. Harding=====================================
84af96c1e3STobin C. Harding
85af96c1e3STobin C. HardingTo register and unregister a filesystem, use the following API
86af96c1e3STobin C. Hardingfunctions:
87af96c1e3STobin C. Harding
88af96c1e3STobin C. Harding.. code-block:: c
89af96c1e3STobin C. Harding
90af96c1e3STobin C. Harding	#include <linux/fs.h>
91af96c1e3STobin C. Harding
92af96c1e3STobin C. Harding	extern int register_filesystem(struct file_system_type *);
93af96c1e3STobin C. Harding	extern int unregister_filesystem(struct file_system_type *);
94af96c1e3STobin C. Harding
95af96c1e3STobin C. HardingThe passed struct file_system_type describes your filesystem.  When a
96af96c1e3STobin C. Hardingrequest is made to mount a filesystem onto a directory in your
97af96c1e3STobin C. Hardingnamespace, the VFS will call the appropriate mount() method for the
98af96c1e3STobin C. Hardingspecific filesystem.  New vfsmount referring to the tree returned by
99af96c1e3STobin C. Harding->mount() will be attached to the mountpoint, so that when pathname
100af96c1e3STobin C. Hardingresolution reaches the mountpoint it will jump into the root of that
101af96c1e3STobin C. Hardingvfsmount.
102af96c1e3STobin C. Harding
103af96c1e3STobin C. HardingYou can see all filesystems that are registered to the kernel in the
104af96c1e3STobin C. Hardingfile /proc/filesystems.
105af96c1e3STobin C. Harding
106af96c1e3STobin C. Harding
107af96c1e3STobin C. Hardingstruct file_system_type
108af96c1e3STobin C. Harding-----------------------
109af96c1e3STobin C. Harding
11085bf9a0eSAlexander MikhalitsynThis describes the filesystem.  The following
111af96c1e3STobin C. Hardingmembers are defined:
112af96c1e3STobin C. Harding
113af96c1e3STobin C. Harding.. code-block:: c
114af96c1e3STobin C. Harding
1156a2195a1SLiao Pingfang	struct file_system_type {
116af96c1e3STobin C. Harding		const char *name;
117af96c1e3STobin C. Harding		int fs_flags;
11885bf9a0eSAlexander Mikhalitsyn		int (*init_fs_context)(struct fs_context *);
11985bf9a0eSAlexander Mikhalitsyn		const struct fs_parameter_spec *parameters;
120af96c1e3STobin C. Harding		struct dentry *(*mount) (struct file_system_type *, int,
121af96c1e3STobin C. Harding			const char *, void *);
122af96c1e3STobin C. Harding		void (*kill_sb) (struct super_block *);
123af96c1e3STobin C. Harding		struct module *owner;
124af96c1e3STobin C. Harding		struct file_system_type * next;
12585bf9a0eSAlexander Mikhalitsyn		struct hlist_head fs_supers;
12685bf9a0eSAlexander Mikhalitsyn
127af96c1e3STobin C. Harding		struct lock_class_key s_lock_key;
128af96c1e3STobin C. Harding		struct lock_class_key s_umount_key;
12985bf9a0eSAlexander Mikhalitsyn		struct lock_class_key s_vfs_rename_key;
13085bf9a0eSAlexander Mikhalitsyn		struct lock_class_key s_writers_key[SB_FREEZE_LEVELS];
13185bf9a0eSAlexander Mikhalitsyn
13285bf9a0eSAlexander Mikhalitsyn		struct lock_class_key i_lock_key;
13385bf9a0eSAlexander Mikhalitsyn		struct lock_class_key i_mutex_key;
13485bf9a0eSAlexander Mikhalitsyn		struct lock_class_key invalidate_lock_key;
13585bf9a0eSAlexander Mikhalitsyn		struct lock_class_key i_mutex_dir_key;
136af96c1e3STobin C. Harding	};
137af96c1e3STobin C. Harding
138ee5dc049STobin C. Harding``name``
139ee5dc049STobin C. Harding	the name of the filesystem type, such as "ext2", "iso9660",
140af96c1e3STobin C. Harding	"msdos" and so on
141af96c1e3STobin C. Harding
142ee5dc049STobin C. Harding``fs_flags``
143ee5dc049STobin C. Harding	various flags (i.e. FS_REQUIRES_DEV, FS_NO_DCACHE, etc.)
144af96c1e3STobin C. Harding
14585bf9a0eSAlexander Mikhalitsyn``init_fs_context``
14685bf9a0eSAlexander Mikhalitsyn	Initializes 'struct fs_context' ->ops and ->fs_private fields with
14785bf9a0eSAlexander Mikhalitsyn	filesystem-specific data.
14885bf9a0eSAlexander Mikhalitsyn
14985bf9a0eSAlexander Mikhalitsyn``parameters``
15085bf9a0eSAlexander Mikhalitsyn	Pointer to the array of filesystem parameters descriptors
15185bf9a0eSAlexander Mikhalitsyn	'struct fs_parameter_spec'.
15285bf9a0eSAlexander Mikhalitsyn	More info in Documentation/filesystems/mount_api.rst.
15385bf9a0eSAlexander Mikhalitsyn
154ee5dc049STobin C. Harding``mount``
155ee5dc049STobin C. Harding	the method to call when a new instance of this filesystem should
156af96c1e3STobin C. Harding	be mounted
157af96c1e3STobin C. Harding
158ee5dc049STobin C. Harding``kill_sb``
159ee5dc049STobin C. Harding	the method to call when an instance of this filesystem should be
160ee5dc049STobin C. Harding	shut down
161af96c1e3STobin C. Harding
162af96c1e3STobin C. Harding
163ee5dc049STobin C. Harding``owner``
164ee5dc049STobin C. Harding	for internal VFS use: you should initialize this to THIS_MODULE
165ee5dc049STobin C. Harding	in most cases.
166ee5dc049STobin C. Harding
167ee5dc049STobin C. Harding``next``
168ee5dc049STobin C. Harding	for internal VFS use: you should initialize this to NULL
169af96c1e3STobin C. Harding
17085bf9a0eSAlexander Mikhalitsyn``fs_supers``
17185bf9a0eSAlexander Mikhalitsyn	for internal VFS use: hlist of filesystem instances (superblocks)
17285bf9a0eSAlexander Mikhalitsyn
17385bf9a0eSAlexander Mikhalitsyn  s_lock_key, s_umount_key, s_vfs_rename_key, s_writers_key,
17485bf9a0eSAlexander Mikhalitsyn  i_lock_key, i_mutex_key, invalidate_lock_key, i_mutex_dir_key: lockdep-specific
175af96c1e3STobin C. Harding
176af96c1e3STobin C. HardingThe mount() method has the following arguments:
177af96c1e3STobin C. Harding
178ee5dc049STobin C. Harding``struct file_system_type *fs_type``
179ee5dc049STobin C. Harding	describes the filesystem, partly initialized by the specific
180ee5dc049STobin C. Harding	filesystem code
181af96c1e3STobin C. Harding
182ee5dc049STobin C. Harding``int flags``
183ee5dc049STobin C. Harding	mount flags
184af96c1e3STobin C. Harding
185ee5dc049STobin C. Harding``const char *dev_name``
186ee5dc049STobin C. Harding	the device name we are mounting.
187af96c1e3STobin C. Harding
188ee5dc049STobin C. Harding``void *data``
189ee5dc049STobin C. Harding	arbitrary mount options, usually comes as an ASCII string (see
190ee5dc049STobin C. Harding	"Mount Options" section)
191af96c1e3STobin C. Harding
192af96c1e3STobin C. HardingThe mount() method must return the root dentry of the tree requested by
193af96c1e3STobin C. Hardingcaller.  An active reference to its superblock must be grabbed and the
194af96c1e3STobin C. Hardingsuperblock must be locked.  On failure it should return ERR_PTR(error).
195af96c1e3STobin C. Harding
196af96c1e3STobin C. HardingThe arguments match those of mount(2) and their interpretation depends
197af96c1e3STobin C. Hardingon filesystem type.  E.g. for block filesystems, dev_name is interpreted
198af96c1e3STobin C. Hardingas block device name, that device is opened and if it contains a
199af96c1e3STobin C. Hardingsuitable filesystem image the method creates and initializes struct
200af96c1e3STobin C. Hardingsuper_block accordingly, returning its root dentry to caller.
201af96c1e3STobin C. Harding
202af96c1e3STobin C. Harding->mount() may choose to return a subtree of existing filesystem - it
203af96c1e3STobin C. Hardingdoesn't have to create a new one.  The main result from the caller's
204af96c1e3STobin C. Hardingpoint of view is a reference to dentry at the root of (sub)tree to be
205af96c1e3STobin C. Hardingattached; creation of new superblock is a common side effect.
206af96c1e3STobin C. Harding
207af96c1e3STobin C. HardingThe most interesting member of the superblock structure that the mount()
208af96c1e3STobin C. Hardingmethod fills in is the "s_op" field.  This is a pointer to a "struct
209af96c1e3STobin C. Hardingsuper_operations" which describes the next level of the filesystem
210af96c1e3STobin C. Hardingimplementation.
211af96c1e3STobin C. Harding
212af96c1e3STobin C. HardingUsually, a filesystem uses one of the generic mount() implementations
213af96c1e3STobin C. Hardingand provides a fill_super() callback instead.  The generic variants are:
214af96c1e3STobin C. Harding
215ee5dc049STobin C. Harding``mount_bdev``
216ee5dc049STobin C. Harding	mount a filesystem residing on a block device
217af96c1e3STobin C. Harding
218ee5dc049STobin C. Harding``mount_nodev``
219ee5dc049STobin C. Harding	mount a filesystem that is not backed by a device
220af96c1e3STobin C. Harding
221ee5dc049STobin C. Harding``mount_single``
222ee5dc049STobin C. Harding	mount a filesystem which shares the instance between all mounts
223af96c1e3STobin C. Harding
224af96c1e3STobin C. HardingA fill_super() callback implementation has the following arguments:
225af96c1e3STobin C. Harding
226ee5dc049STobin C. Harding``struct super_block *sb``
227ee5dc049STobin C. Harding	the superblock structure.  The callback must initialize this
228ee5dc049STobin C. Harding	properly.
229af96c1e3STobin C. Harding
230ee5dc049STobin C. Harding``void *data``
231ee5dc049STobin C. Harding	arbitrary mount options, usually comes as an ASCII string (see
232ee5dc049STobin C. Harding	"Mount Options" section)
233af96c1e3STobin C. Harding
234ee5dc049STobin C. Harding``int silent``
235ee5dc049STobin C. Harding	whether or not to be silent on error
236af96c1e3STobin C. Harding
237af96c1e3STobin C. Harding
238af96c1e3STobin C. HardingThe Superblock Object
239af96c1e3STobin C. Harding=====================
240af96c1e3STobin C. Harding
241af96c1e3STobin C. HardingA superblock object represents a mounted filesystem.
242af96c1e3STobin C. Harding
243af96c1e3STobin C. Harding
244af96c1e3STobin C. Hardingstruct super_operations
245af96c1e3STobin C. Harding-----------------------
246af96c1e3STobin C. Harding
247af96c1e3STobin C. HardingThis describes how the VFS can manipulate the superblock of your
248592d8072SAlexander Mikhalitsynfilesystem.  The following members are defined:
249af96c1e3STobin C. Harding
250af96c1e3STobin C. Harding.. code-block:: c
251af96c1e3STobin C. Harding
252af96c1e3STobin C. Harding	struct super_operations {
253af96c1e3STobin C. Harding		struct inode *(*alloc_inode)(struct super_block *sb);
254af96c1e3STobin C. Harding		void (*destroy_inode)(struct inode *);
255592d8072SAlexander Mikhalitsyn		void (*free_inode)(struct inode *);
256af96c1e3STobin C. Harding
257af96c1e3STobin C. Harding		void (*dirty_inode) (struct inode *, int flags);
258592d8072SAlexander Mikhalitsyn		int (*write_inode) (struct inode *, struct writeback_control *wbc);
259592d8072SAlexander Mikhalitsyn		int (*drop_inode) (struct inode *);
260592d8072SAlexander Mikhalitsyn		void (*evict_inode) (struct inode *);
261af96c1e3STobin C. Harding		void (*put_super) (struct super_block *);
262af96c1e3STobin C. Harding		int (*sync_fs)(struct super_block *sb, int wait);
263592d8072SAlexander Mikhalitsyn		int (*freeze_super) (struct super_block *sb,
264af96c1e3STobin C. Harding					enum freeze_holder who);
265592d8072SAlexander Mikhalitsyn		int (*freeze_fs) (struct super_block *);
266af96c1e3STobin C. Harding		int (*thaw_super) (struct super_block *sb,
267af96c1e3STobin C. Harding					enum freeze_wholder who);
268af96c1e3STobin C. Harding		int (*unfreeze_fs) (struct super_block *);
269af96c1e3STobin C. Harding		int (*statfs) (struct dentry *, struct kstatfs *);
270af96c1e3STobin C. Harding		int (*remount_fs) (struct super_block *, int *, char *);
271af96c1e3STobin C. Harding		void (*umount_begin) (struct super_block *);
272592d8072SAlexander Mikhalitsyn
273592d8072SAlexander Mikhalitsyn		int (*show_options)(struct seq_file *, struct dentry *);
274592d8072SAlexander Mikhalitsyn		int (*show_devname)(struct seq_file *, struct dentry *);
275af96c1e3STobin C. Harding		int (*show_path)(struct seq_file *, struct dentry *);
276af96c1e3STobin C. Harding		int (*show_stats)(struct seq_file *, struct dentry *);
277af96c1e3STobin C. Harding
278592d8072SAlexander Mikhalitsyn		ssize_t (*quota_read)(struct super_block *, int, char *, size_t, loff_t);
279592d8072SAlexander Mikhalitsyn		ssize_t (*quota_write)(struct super_block *, int, const char *, size_t, loff_t);
280592d8072SAlexander Mikhalitsyn		struct dquot **(*get_dquots)(struct inode *);
281592d8072SAlexander Mikhalitsyn
282592d8072SAlexander Mikhalitsyn		long (*nr_cached_objects)(struct super_block *,
283592d8072SAlexander Mikhalitsyn					struct shrink_control *);
284af96c1e3STobin C. Harding		long (*free_cached_objects)(struct super_block *,
285af96c1e3STobin C. Harding					struct shrink_control *);
286af96c1e3STobin C. Harding	};
287af96c1e3STobin C. Harding
288af96c1e3STobin C. HardingAll methods are called without any locks being held, unless otherwise
289af96c1e3STobin C. Hardingnoted.  This means that most methods can block safely.  All methods are
290af96c1e3STobin C. Hardingonly called from a process context (i.e. not from an interrupt handler
291ee5dc049STobin C. Hardingor bottom half).
292ee5dc049STobin C. Harding
293ee5dc049STobin C. Harding``alloc_inode``
294af96c1e3STobin C. Harding	this method is called by alloc_inode() to allocate memory for
295af96c1e3STobin C. Harding	struct inode and initialize it.  If this function is not
296af96c1e3STobin C. Harding	defined, a simple 'struct inode' is allocated.  Normally
297af96c1e3STobin C. Harding	alloc_inode will be used to allocate a larger structure which
298ee5dc049STobin C. Harding	contains a 'struct inode' embedded within it.
299ee5dc049STobin C. Harding
300ee5dc049STobin C. Harding``destroy_inode``
301af96c1e3STobin C. Harding	this method is called by destroy_inode() to release resources
302af96c1e3STobin C. Harding	allocated for struct inode.  It is only required if
303af96c1e3STobin C. Harding	->alloc_inode was defined and simply undoes anything done by
304592d8072SAlexander Mikhalitsyn	->alloc_inode.
305592d8072SAlexander Mikhalitsyn
306592d8072SAlexander Mikhalitsyn``free_inode``
307592d8072SAlexander Mikhalitsyn	this method is called from RCU callback. If you use call_rcu()
308592d8072SAlexander Mikhalitsyn	in ->destroy_inode to free 'struct inode' memory, then it's
309ee5dc049STobin C. Harding	better to release memory in this method.
310a38ed483SEric Biggers
311a38ed483SEric Biggers``dirty_inode``
312a38ed483SEric Biggers	this method is called by the VFS when an inode is marked dirty.
313a38ed483SEric Biggers	This is specifically for the inode itself being marked dirty,
314cbfecb92SLukas Czerner	not its data.  If the update needs to be persisted by fdatasync(),
315cbfecb92SLukas Czerner	then I_DIRTY_DATASYNC will be set in the flags argument.
316cbfecb92SLukas Czerner	I_DIRTY_TIME will be set in the flags in case lazytime is enabled
317af96c1e3STobin C. Harding	and struct inode has times updated since the last ->dirty_inode
318ee5dc049STobin C. Harding	call.
319ee5dc049STobin C. Harding
320ee5dc049STobin C. Harding``write_inode``
321ee5dc049STobin C. Harding	this method is called when the VFS needs to write an inode to
322af96c1e3STobin C. Harding	disc.  The second parameter indicates whether the write should
323ee5dc049STobin C. Harding	be synchronous or not, not all filesystems check this flag.
324ee5dc049STobin C. Harding
325ee5dc049STobin C. Harding``drop_inode``
326af96c1e3STobin C. Harding	called when the last access to the inode is dropped, with the
327af96c1e3STobin C. Harding	inode->i_lock spinlock held.
328ee5dc049STobin C. Harding
329ee5dc049STobin C. Harding	This method should be either NULL (normal UNIX filesystem
330af96c1e3STobin C. Harding	semantics) or "generic_delete_inode" (for filesystems that do
331af96c1e3STobin C. Harding	not want to cache inodes - causing "delete_inode" to always be
332ee5dc049STobin C. Harding	called regardless of the value of i_nlink)
333ee5dc049STobin C. Harding
334ee5dc049STobin C. Harding	The "generic_delete_inode()" behavior is equivalent to the old
335af96c1e3STobin C. Harding	practice of using "force_delete" in the put_inode() case, but
336592d8072SAlexander Mikhalitsyn	does not have the races that the "force_delete()" approach had.
337592d8072SAlexander Mikhalitsyn
338592d8072SAlexander Mikhalitsyn``evict_inode``
339592d8072SAlexander Mikhalitsyn	called when the VFS wants to evict an inode. Caller does
340592d8072SAlexander Mikhalitsyn	*not* evict the pagecache or inode-associated metadata buffers;
341592d8072SAlexander Mikhalitsyn	the method has to use truncate_inode_pages_final() to get rid
342af96c1e3STobin C. Harding	of those. Caller makes sure async writeback cannot be running for
343ee5dc049STobin C. Harding	the inode while (or after) ->evict_inode() is called. Optional.
344ee5dc049STobin C. Harding
345af96c1e3STobin C. Harding``put_super``
346af96c1e3STobin C. Harding	called when the VFS wishes to free the superblock
347ee5dc049STobin C. Harding	(i.e. unmount).  This is called with the superblock lock held
348ee5dc049STobin C. Harding
349ee5dc049STobin C. Harding``sync_fs``
350af96c1e3STobin C. Harding	called when VFS is writing out all dirty data associated with a
351af96c1e3STobin C. Harding	superblock.  The second parameter indicates whether the method
352592d8072SAlexander Mikhalitsyn	should wait until the write out has been completed.  Optional.
353592d8072SAlexander Mikhalitsyn
354592d8072SAlexander Mikhalitsyn``freeze_super``
355592d8072SAlexander Mikhalitsyn	Called instead of ->freeze_fs callback if provided.
356592d8072SAlexander Mikhalitsyn	Main difference is that ->freeze_super is called without taking
357592d8072SAlexander Mikhalitsyn	down_write(&sb->s_umount). If filesystem implements it and wants
358592d8072SAlexander Mikhalitsyn	->freeze_fs to be called too, then it has to call ->freeze_fs
359ee5dc049STobin C. Harding	explicitly from this callback. Optional.
360ee5dc049STobin C. Harding
361ee5dc049STobin C. Harding``freeze_fs``
362592d8072SAlexander Mikhalitsyn	called when VFS is locking a filesystem and forcing it into a
363592d8072SAlexander Mikhalitsyn	consistent state.  This method is currently used by the Logical
364592d8072SAlexander Mikhalitsyn	Volume Manager (LVM) and ioctl(FIFREEZE). Optional.
365592d8072SAlexander Mikhalitsyn
366592d8072SAlexander Mikhalitsyn``thaw_super``
367af96c1e3STobin C. Harding	called when VFS is unlocking a filesystem and making it writable
368ee5dc049STobin C. Harding	again after ->freeze_super. Optional.
369ee5dc049STobin C. Harding
370592d8072SAlexander Mikhalitsyn``unfreeze_fs``
371af96c1e3STobin C. Harding	called when VFS is unlocking a filesystem and making it writable
372ee5dc049STobin C. Harding	again after ->freeze_fs. Optional.
373ee5dc049STobin C. Harding
374af96c1e3STobin C. Harding``statfs``
375ee5dc049STobin C. Harding	called when the VFS needs to get filesystem statistics.
376ee5dc049STobin C. Harding
377ee5dc049STobin C. Harding``remount_fs``
378af96c1e3STobin C. Harding	called when the filesystem is remounted.  This is called with
379ee5dc049STobin C. Harding	the kernel lock held
380ee5dc049STobin C. Harding
381af96c1e3STobin C. Harding``umount_begin``
382ee5dc049STobin C. Harding	called when the VFS is unmounting a filesystem.
383592d8072SAlexander Mikhalitsyn
384592d8072SAlexander Mikhalitsyn``show_options``
385ee5dc049STobin C. Harding	called by the VFS to show mount options for /proc/<pid>/mounts
386af96c1e3STobin C. Harding	and /proc/<pid>/mountinfo.
387592d8072SAlexander Mikhalitsyn	(see "Mount Options" section)
388592d8072SAlexander Mikhalitsyn
389592d8072SAlexander Mikhalitsyn``show_devname``
390592d8072SAlexander Mikhalitsyn	Optional. Called by the VFS to show device name for
391592d8072SAlexander Mikhalitsyn	/proc/<pid>/{mounts,mountinfo,mountstats}. If not provided then
392592d8072SAlexander Mikhalitsyn	'(struct mount).mnt_devname' will be used.
393592d8072SAlexander Mikhalitsyn
394592d8072SAlexander Mikhalitsyn``show_path``
395592d8072SAlexander Mikhalitsyn	Optional. Called by the VFS (for /proc/<pid>/mountinfo) to show
396592d8072SAlexander Mikhalitsyn	the mount root dentry path relative to the filesystem root.
397592d8072SAlexander Mikhalitsyn
398592d8072SAlexander Mikhalitsyn``show_stats``
399592d8072SAlexander Mikhalitsyn	Optional. Called by the VFS (for /proc/<pid>/mountstats) to show
400ee5dc049STobin C. Harding	filesystem-specific mount statistics.
401ee5dc049STobin C. Harding
402af96c1e3STobin C. Harding``quota_read``
403ee5dc049STobin C. Harding	called by the VFS to read from filesystem quota file.
404ee5dc049STobin C. Harding
405af96c1e3STobin C. Harding``quota_write``
406592d8072SAlexander Mikhalitsyn	called by the VFS to write to filesystem quota file.
407592d8072SAlexander Mikhalitsyn
408592d8072SAlexander Mikhalitsyn``get_dquots``
409592d8072SAlexander Mikhalitsyn	called by quota to get 'struct dquot' array for a particular inode.
410ee5dc049STobin C. Harding	Optional.
411ee5dc049STobin C. Harding
412ee5dc049STobin C. Harding``nr_cached_objects``
413af96c1e3STobin C. Harding	called by the sb cache shrinking function for the filesystem to
414af96c1e3STobin C. Harding	return the number of freeable cached objects it contains.
415ee5dc049STobin C. Harding	Optional.
416ee5dc049STobin C. Harding
417ee5dc049STobin C. Harding``free_cache_objects``
418ee5dc049STobin C. Harding	called by the sb cache shrinking function for the filesystem to
419ee5dc049STobin C. Harding	scan the number of objects indicated to try to free them.
420ee5dc049STobin C. Harding	Optional, but any filesystem implementing this method needs to
421af96c1e3STobin C. Harding	also implement ->nr_cached_objects for it to be called
422af96c1e3STobin C. Harding	correctly.
423ee5dc049STobin C. Harding
424ee5dc049STobin C. Harding	We can't do anything with any errors that the filesystem might
425ee5dc049STobin C. Harding	encountered, hence the void return type.  This will never be
426af96c1e3STobin C. Harding	called if the VM is trying to reclaim under GFP_NOFS conditions,
427ee5dc049STobin C. Harding	hence this method does not need to handle that situation itself.
428ee5dc049STobin C. Harding
429ee5dc049STobin C. Harding	Implementations must include conditional reschedule calls inside
430ee5dc049STobin C. Harding	any scanning loop that is done.  This allows the VFS to
431ee5dc049STobin C. Harding	determine appropriate scan batch sizes without having to worry
432af96c1e3STobin C. Harding	about whether implementations will cause holdoff problems due to
433af96c1e3STobin C. Harding	large scan batch sizes.
434af96c1e3STobin C. Harding
435af96c1e3STobin C. HardingWhoever sets up the inode is responsible for filling in the "i_op"
436af96c1e3STobin C. Hardingfield.  This is a pointer to a "struct inode_operations" which describes
437af96c1e3STobin C. Hardingthe methods that can be performed on individual inodes.
438af96c1e3STobin C. Harding
439af96c1e3STobin C. Harding
440af96c1e3STobin C. Hardingstruct xattr_handlers
441af96c1e3STobin C. Harding---------------------
442af96c1e3STobin C. Harding
443af96c1e3STobin C. HardingOn filesystems that support extended attributes (xattrs), the s_xattr
444af96c1e3STobin C. Hardingsuperblock field points to a NULL-terminated array of xattr handlers.
445ee5dc049STobin C. HardingExtended attributes are name:value pairs.
446ee5dc049STobin C. Harding
447ee5dc049STobin C. Harding``name``
448ee5dc049STobin C. Harding	Indicates that the handler matches attributes with the specified
449af96c1e3STobin C. Harding	name (such as "system.posix_acl_access"); the prefix field must
450ee5dc049STobin C. Harding	be NULL.
451ee5dc049STobin C. Harding
452ee5dc049STobin C. Harding``prefix``
453ee5dc049STobin C. Harding	Indicates that the handler matches all attributes with the
454af96c1e3STobin C. Harding	specified name prefix (such as "user."); the name field must be
455ee5dc049STobin C. Harding	NULL.
456ee5dc049STobin C. Harding
457ee5dc049STobin C. Harding``list``
458ee5dc049STobin C. Harding	Determine if attributes matching this xattr handler should be
459af96c1e3STobin C. Harding	listed for a particular dentry.  Used by some listxattr
460ee5dc049STobin C. Harding	implementations like generic_listxattr.
461ee5dc049STobin C. Harding
462ee5dc049STobin C. Harding``get``
463ee5dc049STobin C. Harding	Called by the VFS to get the value of a particular extended
464af96c1e3STobin C. Harding	attribute.  This method is called by the getxattr(2) system
465ee5dc049STobin C. Harding	call.
466ee5dc049STobin C. Harding
467ee5dc049STobin C. Harding``set``
4688286de7cSRandy Dunlap	Called by the VFS to set the value of a particular extended
469ee5dc049STobin C. Harding	attribute.  When the new value is NULL, called to remove a
470af96c1e3STobin C. Harding	particular extended attribute.  This method is called by the
471af96c1e3STobin C. Harding	setxattr(2) and removexattr(2) system calls.
472af96c1e3STobin C. Harding
473af96c1e3STobin C. HardingWhen none of the xattr handlers of a filesystem match the specified
474af96c1e3STobin C. Hardingattribute name or when a filesystem doesn't support extended attributes,
475af96c1e3STobin C. Hardingthe various ``*xattr(2)`` system calls return -EOPNOTSUPP.
476af96c1e3STobin C. Harding
477af96c1e3STobin C. Harding
478af96c1e3STobin C. HardingThe Inode Object
479af96c1e3STobin C. Harding================
480af96c1e3STobin C. Harding
481af96c1e3STobin C. HardingAn inode object represents an object within the filesystem.
482af96c1e3STobin C. Harding
483af96c1e3STobin C. Harding
484af96c1e3STobin C. Hardingstruct inode_operations
485af96c1e3STobin C. Harding-----------------------
486af96c1e3STobin C. Harding
487af96c1e3STobin C. HardingThis describes how the VFS can manipulate an inode in your filesystem.
488af96c1e3STobin C. HardingAs of kernel 2.6.22, the following members are defined:
489af96c1e3STobin C. Harding
490af96c1e3STobin C. Harding.. code-block:: c
4916c960e68SChristian Brauner
492af96c1e3STobin C. Harding	struct inode_operations {
493af96c1e3STobin C. Harding		int (*create) (struct mnt_idmap *, struct inode *,struct dentry *, umode_t, bool);
494af96c1e3STobin C. Harding		struct dentry * (*lookup) (struct inode *,struct dentry *, unsigned int);
4957a77db95SChristian Brauner		int (*link) (struct dentry *,struct inode *,struct dentry *);
496c54bd91eSChristian Brauner		int (*unlink) (struct inode *,struct dentry *);
497af96c1e3STobin C. Harding		int (*symlink) (struct mnt_idmap *, struct inode *,struct dentry *,const char *);
4985ebb29beSChristian Brauner		int (*mkdir) (struct mnt_idmap *, struct inode *,struct dentry *,umode_t);
499e18275aeSChristian Brauner		int (*rmdir) (struct inode *,struct dentry *);
500af96c1e3STobin C. Harding		int (*mknod) (struct mnt_idmap *, struct inode *,struct dentry *,umode_t,dev_t);
501af96c1e3STobin C. Harding		int (*rename) (struct mnt_idmap *, struct inode *, struct dentry *,
502af96c1e3STobin C. Harding			       struct inode *, struct dentry *, unsigned int);
503af96c1e3STobin C. Harding		int (*readlink) (struct dentry *, char __user *,int);
5044609e1f1SChristian Brauner		const char *(*get_link) (struct dentry *, struct inode *,
505cac2f8b8SChristian Brauner					 struct delayed_call *);
506c1632a0fSChristian Brauner		int (*permission) (struct mnt_idmap *, struct inode *, int);
507b74d24f7SChristian Brauner		struct posix_acl * (*get_inode_acl)(struct inode *, int, bool);
508af96c1e3STobin C. Harding		int (*setattr) (struct mnt_idmap *, struct dentry *, struct iattr *);
509af96c1e3STobin C. Harding		int (*getattr) (struct mnt_idmap *, const struct path *, struct kstat *, u32, unsigned int);
510af96c1e3STobin C. Harding		ssize_t (*listxattr) (struct dentry *, char *, size_t);
511af96c1e3STobin C. Harding		void (*update_time)(struct inode *, struct timespec *, int);
512011e2b71SChristian Brauner		int (*atomic_open)(struct inode *, struct dentry *, struct file *,
51377435322SChristian Brauner				   unsigned open_flag, umode_t create_mode);
51413e83a49SChristian Brauner		int (*tmpfile) (struct mnt_idmap *, struct inode *, struct file *, umode_t);
5158782a9aeSChristian Brauner		struct posix_acl * (*get_acl)(struct mnt_idmap *, struct dentry *, int);
5164c5b4799SMiklos Szeredi	        int (*set_acl)(struct mnt_idmap *, struct dentry *, struct posix_acl *, int);
5174c5b4799SMiklos Szeredi		int (*fileattr_set)(struct mnt_idmap *idmap,
518af96c1e3STobin C. Harding				    struct dentry *dentry, struct fileattr *fa);
519af96c1e3STobin C. Harding		int (*fileattr_get)(struct dentry *dentry, struct fileattr *fa);
520af96c1e3STobin C. Harding	        struct offset_ctx *(*get_offset_ctx)(struct inode *inode);
521af96c1e3STobin C. Harding	};
522af96c1e3STobin C. Harding
523ee5dc049STobin C. HardingAgain, all methods are called without any locks being held, unless
524ee5dc049STobin C. Hardingotherwise noted.
525ee5dc049STobin C. Harding
526ee5dc049STobin C. Harding``create``
527ee5dc049STobin C. Harding	called by the open(2) and creat(2) system calls.  Only required
528ee5dc049STobin C. Harding	if you want to support regular files.  The dentry you get should
529af96c1e3STobin C. Harding	not have an inode (i.e. it should be a negative dentry).  Here
530ee5dc049STobin C. Harding	you will probably call d_instantiate() with the dentry and the
531ee5dc049STobin C. Harding	newly created inode
532af96c1e3STobin C. Harding
533af96c1e3STobin C. Harding``lookup``
534af96c1e3STobin C. Harding	called when the VFS needs to look up an inode in a parent
535af96c1e3STobin C. Harding	directory.  The name to look for is found in the dentry.  This
536af96c1e3STobin C. Harding	method must call d_add() to insert the found inode into the
537ee5dc049STobin C. Harding	dentry.  The "i_count" field in the inode structure should be
538ee5dc049STobin C. Harding	incremented.  If the named inode does not exist a NULL inode
539af96c1e3STobin C. Harding	should be inserted into the dentry (this is called a negative
540af96c1e3STobin C. Harding	dentry).  Returning an error code from this routine must only be
541ee5dc049STobin C. Harding	done on a real error, otherwise creating inodes with system
542ee5dc049STobin C. Harding	calls like create(2), mknod(2), mkdir(2) and so on will fail.
543ee5dc049STobin C. Harding	If you wish to overload the dentry methods then you should
544af96c1e3STobin C. Harding	initialise the "d_dop" field in the dentry; this is a pointer to
545ee5dc049STobin C. Harding	a struct "dentry_operations".  This method is called with the
546ee5dc049STobin C. Harding	directory inode semaphore held
547ee5dc049STobin C. Harding
548af96c1e3STobin C. Harding``link``
549af96c1e3STobin C. Harding	called by the link(2) system call.  Only required if you want to
550ee5dc049STobin C. Harding	support hard links.  You will probably need to call
551ee5dc049STobin C. Harding	d_instantiate() just as you would in the create() method
552ee5dc049STobin C. Harding
553af96c1e3STobin C. Harding``unlink``
554ee5dc049STobin C. Harding	called by the unlink(2) system call.  Only required if you want
555ee5dc049STobin C. Harding	to support deleting inodes
556ee5dc049STobin C. Harding
557af96c1e3STobin C. Harding``symlink``
558af96c1e3STobin C. Harding	called by the symlink(2) system call.  Only required if you want
559ee5dc049STobin C. Harding	to support symlinks.  You will probably need to call
560ee5dc049STobin C. Harding	d_instantiate() just as you would in the create() method
561af96c1e3STobin C. Harding
562af96c1e3STobin C. Harding``mkdir``
563af96c1e3STobin C. Harding	called by the mkdir(2) system call.  Only required if you want
564ee5dc049STobin C. Harding	to support creating subdirectories.  You will probably need to
565ee5dc049STobin C. Harding	call d_instantiate() just as you would in the create() method
566af96c1e3STobin C. Harding
567af96c1e3STobin C. Harding``rmdir``
568ee5dc049STobin C. Harding	called by the rmdir(2) system call.  Only required if you want
569ee5dc049STobin C. Harding	to support deleting subdirectories
570ee5dc049STobin C. Harding
571ee5dc049STobin C. Harding``mknod``
572ee5dc049STobin C. Harding	called by the mknod(2) system call to create a device (char,
573ee5dc049STobin C. Harding	block) inode or a named pipe (FIFO) or socket.  Only required if
574af96c1e3STobin C. Harding	you want to support creating these types of inodes.  You will
575ee5dc049STobin C. Harding	probably need to call d_instantiate() just as you would in the
576ee5dc049STobin C. Harding	create() method
577ee5dc049STobin C. Harding
578af96c1e3STobin C. Harding``rename``
579af96c1e3STobin C. Harding	called by the rename(2) system call to rename the object to have
580af96c1e3STobin C. Harding	the parent and name given by the second inode and dentry.
581ee5dc049STobin C. Harding
582ee5dc049STobin C. Harding	The filesystem must return -EINVAL for any unsupported or
583ee5dc049STobin C. Harding	unknown flags.  Currently the following flags are implemented:
584ee5dc049STobin C. Harding	(1) RENAME_NOREPLACE: this flag indicates that if the target of
585ee5dc049STobin C. Harding	the rename exists the rename should fail with -EEXIST instead of
586af96c1e3STobin C. Harding	replacing the target.  The VFS already checks for existence, so
587ee5dc049STobin C. Harding	for local filesystems the RENAME_NOREPLACE implementation is
588ee5dc049STobin C. Harding	equivalent to plain rename.
589af96c1e3STobin C. Harding	(2) RENAME_EXCHANGE: exchange source and target.  Both must
590ee5dc049STobin C. Harding	exist; this is checked by the VFS.  Unlike plain rename, source
591ee5dc049STobin C. Harding	and target may be of different type.
592ee5dc049STobin C. Harding
593ee5dc049STobin C. Harding``get_link``
594ee5dc049STobin C. Harding	called by the VFS to follow a symbolic link to the inode it
595ee5dc049STobin C. Harding	points to.  Only required if you want to support symbolic links.
596ee5dc049STobin C. Harding	This method returns the symlink body to traverse (and possibly
597ee5dc049STobin C. Harding	resets the current position with nd_jump_link()).  If the body
598ee5dc049STobin C. Harding	won't go away until the inode is gone, nothing else is needed;
599ee5dc049STobin C. Harding	if it needs to be otherwise pinned, arrange for its release by
600ee5dc049STobin C. Harding	having get_link(..., ..., done) do set_delayed_call(done,
601af96c1e3STobin C. Harding	destructor, argument).  In that case destructor(argument) will
602af96c1e3STobin C. Harding	be called once VFS is done with the body you've returned.  May
603af96c1e3STobin C. Harding	be called in RCU mode; that is indicated by NULL dentry
604af96c1e3STobin C. Harding	argument.  If request can't be handled without leaving RCU mode,
605af96c1e3STobin C. Harding	have it return ERR_PTR(-ECHILD).
606af96c1e3STobin C. Harding
607af96c1e3STobin C. Harding	If the filesystem stores the symlink target in ->i_link, the
608af96c1e3STobin C. Harding	VFS may use it directly without calling ->get_link(); however,
609af96c1e3STobin C. Harding	->get_link() must still be provided.  ->i_link must not be
610ee5dc049STobin C. Harding	freed until after an RCU grace period.  Writing to ->i_link
611ee5dc049STobin C. Harding	post-iget() time requires a 'release' memory barrier.
612af96c1e3STobin C. Harding
613af96c1e3STobin C. Harding``readlink``
614af96c1e3STobin C. Harding	this is now just an override for use by readlink(2) for the
615af96c1e3STobin C. Harding	cases when ->get_link uses nd_jump_link() or object is not in
616af96c1e3STobin C. Harding	fact a symlink.  Normally filesystems should only implement
617ee5dc049STobin C. Harding	->get_link for symlinks and readlink(2) will automatically use
618ee5dc049STobin C. Harding	that.
619af96c1e3STobin C. Harding
620af96c1e3STobin C. Harding``permission``
621ee5dc049STobin C. Harding	called by the VFS to check for access rights on a POSIX-like
622ee5dc049STobin C. Harding	filesystem.
623ee5dc049STobin C. Harding
624af96c1e3STobin C. Harding	May be called in rcu-walk mode (mask & MAY_NOT_BLOCK).  If in
625ee5dc049STobin C. Harding	rcu-walk mode, the filesystem must check the permission without
626ee5dc049STobin C. Harding	blocking or storing to the inode.
627af96c1e3STobin C. Harding
628af96c1e3STobin C. Harding	If a situation is encountered that rcu-walk cannot handle,
629ee5dc049STobin C. Harding	return
630ee5dc049STobin C. Harding	-ECHILD and it will be called again in ref-walk mode.
631ee5dc049STobin C. Harding
632af96c1e3STobin C. Harding``setattr``
633ee5dc049STobin C. Harding	called by the VFS to set attributes for a file.  This method is
634ee5dc049STobin C. Harding	called by chmod(2) and related system calls.
635ee5dc049STobin C. Harding
636af96c1e3STobin C. Harding``getattr``
637ee5dc049STobin C. Harding	called by the VFS to get attributes of a file.  This method is
638ee5dc049STobin C. Harding	called by stat(2) and related system calls.
639ee5dc049STobin C. Harding
640af96c1e3STobin C. Harding``listxattr``
641ee5dc049STobin C. Harding	called by the VFS to list all extended attributes for a given
642ee5dc049STobin C. Harding	file.  This method is called by the listxattr(2) system call.
643ee5dc049STobin C. Harding
644ee5dc049STobin C. Harding``update_time``
645af96c1e3STobin C. Harding	called by the VFS to update a specific time or the i_version of
646ee5dc049STobin C. Harding	an inode.  If this is not defined the VFS will update the inode
647ee5dc049STobin C. Harding	itself and call mark_inode_dirty_sync.
648ee5dc049STobin C. Harding
649ee5dc049STobin C. Harding``atomic_open``
650ee5dc049STobin C. Harding	called on the last component of an open.  Using this optional
651ee5dc049STobin C. Harding	method the filesystem can look up, possibly create and open the
652ee5dc049STobin C. Harding	file in one atomic operation.  If it wants to leave actual
653ee5dc049STobin C. Harding	opening to the caller (e.g. if the file turned out to be a
654ee5dc049STobin C. Harding	symlink, device, or just something filesystem won't do atomic
655ee5dc049STobin C. Harding	open for), it may signal this by returning finish_no_open(file,
656ee5dc049STobin C. Harding	dentry).  This method is only called if the last component is
657ee5dc049STobin C. Harding	negative or needs lookup.  Cached positive dentries are still
658ee5dc049STobin C. Harding	handled by f_op->open().  If the file was created, FMODE_CREATED
659af96c1e3STobin C. Harding	flag should be set in file->f_mode.  In case of O_EXCL the
660ee5dc049STobin C. Harding	method must only succeed if the file didn't exist and hence
661ee5dc049STobin C. Harding	FMODE_CREATED shall always be set on success.
662ee5dc049STobin C. Harding
663863f144fSMiklos Szeredi``tmpfile``
664863f144fSMiklos Szeredi	called in the end of O_TMPFILE open().  Optional, equivalent to
665863f144fSMiklos Szeredi	atomically creating, opening and unlinking a file in given
666af96c1e3STobin C. Harding	directory.  On success needs to return with the file already
6674c5b4799SMiklos Szeredi	open; this can be done by calling finish_open_simple() right at
6684c5b4799SMiklos Szeredi	the end.
6694c5b4799SMiklos Szeredi
6704c5b4799SMiklos Szeredi``fileattr_get``
6714c5b4799SMiklos Szeredi	called on ioctl(FS_IOC_GETFLAGS) and ioctl(FS_IOC_FSGETXATTR) to
6724c5b4799SMiklos Szeredi	retrieve miscellaneous file flags and attributes.  Also called
6734c5b4799SMiklos Szeredi	before the relevant SET operation to check what is being changed
6744c5b4799SMiklos Szeredi	(in this case with i_rwsem locked exclusive).  If unset, then
6754c5b4799SMiklos Szeredi	fall back to f_op->ioctl().
6764c5b4799SMiklos Szeredi
6774c5b4799SMiklos Szeredi``fileattr_set``
6784c5b4799SMiklos Szeredi	called on ioctl(FS_IOC_SETFLAGS) and ioctl(FS_IOC_FSSETXATTR) to
679af96c1e3STobin C. Harding	change miscellaneous file flags and attributes.  Callers hold
680af96c1e3STobin C. Harding	i_rwsem exclusive.  If unset, then fall back to f_op->ioctl().
681af96c1e3STobin C. Harding``get_offset_ctx``
682af96c1e3STobin C. Harding	called to get the offset context for a directory inode. A
683af96c1e3STobin C. Harding        filesystem must define this operation to use
684af96c1e3STobin C. Harding        simple_offset_dir_operations.
685af96c1e3STobin C. Harding
686af96c1e3STobin C. HardingThe Address Space Object
687af96c1e3STobin C. Harding========================
688af96c1e3STobin C. Harding
689af96c1e3STobin C. HardingThe address space object is used to group and manage pages in the page
690af96c1e3STobin C. Hardingcache.  It can be used to keep track of the pages in a file (or anything
691af96c1e3STobin C. Hardingelse) and also track the mapping of sections of the file into process
692af96c1e3STobin C. Hardingaddress spaces.
693af96c1e3STobin C. Harding
694af96c1e3STobin C. HardingThere are a number of distinct yet related services that an
695af96c1e3STobin C. Hardingaddress-space can provide.  These include communicating memory pressure,
696fa29000bSMatthew Wilcox (Oracle)page lookup by address, and keeping track of pages tagged as Dirty or
697fa29000bSMatthew Wilcox (Oracle)Writeback.
698fa29000bSMatthew Wilcox (Oracle)
699af96c1e3STobin C. HardingThe first can be used independently to the others.  The VM can try to
700af96c1e3STobin C. Hardingeither write dirty pages in order to clean them, or release clean pages
701af96c1e3STobin C. Hardingin order to reuse them.  To do this it can call the ->writepage method
702af96c1e3STobin C. Hardingon dirty pages, and ->release_folio on clean folios with the private
703af96c1e3STobin C. Hardingflag set.  Clean pages without PagePrivate and with no external references
704af96c1e3STobin C. Hardingwill be released without notice being given to the address_space.
705af96c1e3STobin C. Harding
706af96c1e3STobin C. HardingTo achieve this functionality, pages need to be placed on an LRU with
707af96c1e3STobin C. Hardinglru_cache_add and mark_page_active needs to be called whenever the page
708af96c1e3STobin C. Hardingis used.
709af96c1e3STobin C. Harding
710af96c1e3STobin C. HardingPages are normally kept in a radix tree index by ->index.  This tree
711af96c1e3STobin C. Hardingmaintains information about the PG_Dirty and PG_Writeback status of each
712af96c1e3STobin C. Hardingpage, so that pages with either of these flags can be found quickly.
713af96c1e3STobin C. Harding
714af96c1e3STobin C. HardingThe Dirty tag is primarily used by mpage_writepages - the default
715af96c1e3STobin C. Harding->writepages method.  It uses the tag to find dirty pages to call
716af96c1e3STobin C. Harding->writepage on.  If mpage_writepages is not used (i.e. the address
717af96c1e3STobin C. Hardingprovides its own ->writepages) , the PAGECACHE_TAG_DIRTY tag is almost
718af96c1e3STobin C. Hardingunused.  write_inode_now and sync_inode do use it (through
719af96c1e3STobin C. Harding__sync_single_inode) to check if ->writepages has been successful in
720af96c1e3STobin C. Hardingwriting out the whole address_space.
721af96c1e3STobin C. Harding
722af96c1e3STobin C. HardingThe Writeback tag is used by filemap*wait* and sync_page* functions, via
723af96c1e3STobin C. Hardingfilemap_fdatawait_range, to wait for all writeback to complete.
724af96c1e3STobin C. Harding
725af96c1e3STobin C. HardingAn address_space handler may attach extra information to a page,
726af96c1e3STobin C. Hardingtypically using the 'private' field in the 'struct page'.  If such
727af96c1e3STobin C. Hardinginformation is attached, the PG_Private flag should be set.  This will
728af96c1e3STobin C. Hardingcause various VM routines to make extra calls into the address_space
729af96c1e3STobin C. Hardinghandler to deal with that data.
730af96c1e3STobin C. Harding
731af96c1e3STobin C. HardingAn address space acts as an intermediate between storage and
73208830c8bSMatthew Wilcox (Oracle)application.  Data is read into the address space a whole page at a
733af96c1e3STobin C. Hardingtime, and provided to the application either by copying of the page, or
7346f31a5a2SMatthew Wilcox (Oracle)by memory-mapping the page.  Data is written into the address space by
735af96c1e3STobin C. Hardingthe application, and then written-back to storage typically in whole
736af96c1e3STobin C. Hardingpages, however the address_space has finer control of write sizes.
737af96c1e3STobin C. Harding
738af96c1e3STobin C. HardingThe read process essentially only requires 'read_folio'.  The write
739af96c1e3STobin C. Hardingprocess is more complicated and uses write_begin/write_end or
740af96c1e3STobin C. Hardingdirty_folio to write data into the address_space, and writepage and
741af96c1e3STobin C. Hardingwritepages to writeback data to storage.
742af96c1e3STobin C. Harding
743af96c1e3STobin C. HardingAdding and removing pages to/from an address_space is protected by the
744af96c1e3STobin C. Hardinginode's i_mutex.
745af96c1e3STobin C. Harding
746af96c1e3STobin C. HardingWhen data is written to a page, the PG_Dirty flag should be set.  It
7478286de7cSRandy Dunlaptypically remains set until writepage asks for it to be written.  This
748af96c1e3STobin C. Hardingshould clear PG_Dirty and set PG_Writeback.  It can be actually written
749af96c1e3STobin C. Hardingat any point after PG_Dirty is clear.  Once it is known to be safe,
750af96c1e3STobin C. HardingPG_Writeback is cleared.
751af96c1e3STobin C. Harding
752af96c1e3STobin C. HardingWriteback makes use of a writeback_control structure to direct the
753af96c1e3STobin C. Hardingoperations.  This gives the writepage and writepages operations some
754af96c1e3STobin C. Hardinginformation about the nature of and reason for the writeback request,
755af96c1e3STobin C. Hardingand the constraints under which it is being done.  It is also used to
756af96c1e3STobin C. Hardingreturn information back to the caller about the result of a writepage or
757af96c1e3STobin C. Hardingwritepages request.
758af96c1e3STobin C. Harding
759af96c1e3STobin C. Harding
760af96c1e3STobin C. HardingHandling errors during writeback
761af96c1e3STobin C. Harding--------------------------------
762af96c1e3STobin C. Harding
763af96c1e3STobin C. HardingMost applications that do buffered I/O will periodically call a file
764*d56b699dSBjorn Helgaassynchronization call (fsync, fdatasync, msync or sync_file_range) to
765af96c1e3STobin C. Hardingensure that data written has made it to the backing store.  When there
766af96c1e3STobin C. Hardingis an error during writeback, they expect that error to be reported when
767af96c1e3STobin C. Hardinga file sync request is made.  After an error has been reported on one
768af96c1e3STobin C. Hardingrequest, subsequent requests on the same file descriptor should return
769af96c1e3STobin C. Harding0, unless further writeback errors have occurred since the previous file
770af96c1e3STobin C. Hardingsynchronization.
771af96c1e3STobin C. Harding
772af96c1e3STobin C. HardingIdeally, the kernel would report errors only on file descriptions on
773af96c1e3STobin C. Hardingwhich writes were done that subsequently failed to be written back.  The
774af96c1e3STobin C. Hardinggeneric pagecache infrastructure does not track the file descriptions
775af96c1e3STobin C. Hardingthat have dirtied each individual page however, so determining which
776af96c1e3STobin C. Hardingfile descriptors should get back an error is not possible.
777af96c1e3STobin C. Harding
778af96c1e3STobin C. HardingInstead, the generic writeback error tracking infrastructure in the
779af96c1e3STobin C. Hardingkernel settles for reporting errors to fsync on all file descriptions
780af96c1e3STobin C. Hardingthat were open at the time that the error occurred.  In a situation with
781af96c1e3STobin C. Hardingmultiple writers, all of them will get back an error on a subsequent
782af96c1e3STobin C. Hardingfsync, even if all of the writes done through that particular file
783af96c1e3STobin C. Hardingdescriptor succeeded (or even if there were no writes on that file
784af96c1e3STobin C. Hardingdescriptor at all).
785af96c1e3STobin C. Harding
786af96c1e3STobin C. HardingFilesystems that wish to use this infrastructure should call
787af96c1e3STobin C. Hardingmapping_set_error to record the error in the address_space when it
788af96c1e3STobin C. Hardingoccurs.  Then, after writing back data from the pagecache in their
789af96c1e3STobin C. Hardingfile->fsync operation, they should call file_check_and_advance_wb_err to
790af96c1e3STobin C. Hardingensure that the struct file's error cursor has advanced to the correct
791af96c1e3STobin C. Hardingpoint in the stream of errors emitted by the backing device(s).
792af96c1e3STobin C. Harding
793af96c1e3STobin C. Harding
794af96c1e3STobin C. Hardingstruct address_space_operations
795af96c1e3STobin C. Harding-------------------------------
796af96c1e3STobin C. Harding
797af96c1e3STobin C. HardingThis describes how the VFS can manipulate mapping of a file to page
79808830c8bSMatthew Wilcox (Oracle)cache in your filesystem.  The following members are defined:
799af96c1e3STobin C. Harding
8006f31a5a2SMatthew Wilcox (Oracle).. code-block:: c
8018151b4c8SMatthew Wilcox (Oracle)
802af96c1e3STobin C. Harding	struct address_space_operations {
8039d6b0cd7SMatthew Wilcox (Oracle)		int (*writepage)(struct page *page, struct writeback_control *wbc);
804af96c1e3STobin C. Harding		int (*read_folio)(struct file *, struct folio *);
805af96c1e3STobin C. Harding		int (*writepages)(struct address_space *, struct writeback_control *);
806af96c1e3STobin C. Harding		bool (*dirty_folio)(struct address_space *, struct folio *);
807af96c1e3STobin C. Harding		void (*readahead)(struct readahead_control *);
808af96c1e3STobin C. Harding		int (*write_begin)(struct file *, struct address_space *mapping,
809128d1f82SMatthew Wilcox (Oracle)				   loff_t pos, unsigned len,
810fa29000bSMatthew Wilcox (Oracle)				struct page **pagep, void **fsdata);
811d2329aa0SMatthew Wilcox (Oracle)		int (*write_end)(struct file *, struct address_space *mapping,
812af96c1e3STobin C. Harding				 loff_t pos, unsigned len, unsigned copied,
8135490da4fSMatthew Wilcox (Oracle)				 struct page *page, void *fsdata);
8145490da4fSMatthew Wilcox (Oracle)		sector_t (*bmap)(struct address_space *, sector_t);
815affa80e8SMatthew Wilcox (Oracle)		void (*invalidate_folio) (struct folio *, size_t start, size_t len);
816af96c1e3STobin C. Harding		bool (*release_folio)(struct folio *, gfp_t);
8172e7e80f7SMatthew Wilcox (Oracle)		void (*free_folio)(struct folio *);
8182e7e80f7SMatthew Wilcox (Oracle)		ssize_t (*direct_IO)(struct kiocb *, struct iov_iter *iter);
819520f301cSMatthew Wilcox (Oracle)		int (*migrate_folio)(struct mapping *, struct folio *dst,
820af96c1e3STobin C. Harding				struct folio *src, enum migrate_mode);
821cba738f6SNeilBrown		int (*launder_folio) (struct folio *);
822af96c1e3STobin C. Harding
823cba738f6SNeilBrown		bool (*is_partially_uptodate) (struct folio *, size_t from,
824af96c1e3STobin C. Harding					       size_t count);
825af96c1e3STobin C. Harding		void (*is_dirty_writeback)(struct folio *, bool *, bool *);
826ee5dc049STobin C. Harding		int (*error_remove_page) (struct mapping *mapping, struct page *page);
827ee5dc049STobin C. Harding		int (*swap_activate)(struct swap_info_struct *sis, struct file *f, sector_t *span)
828ee5dc049STobin C. Harding		int (*swap_deactivate)(struct file *);
829ee5dc049STobin C. Harding		int (*swap_rw)(struct kiocb *iocb, struct iov_iter *iter);
830ee5dc049STobin C. Harding	};
831ee5dc049STobin C. Harding
832ee5dc049STobin C. Harding``writepage``
833ee5dc049STobin C. Harding	called by the VM to write a dirty page to backing store.  This
834ee5dc049STobin C. Harding	may happen for data integrity reasons (i.e. 'sync'), or to free
835af96c1e3STobin C. Harding	up memory (flush).  The difference can be seen in
836af96c1e3STobin C. Harding	wbc->sync_mode.  The PG_Dirty flag has been cleared and
837af96c1e3STobin C. Harding	PageLocked is true.  writepage should start writeout, should set
838af96c1e3STobin C. Harding	PG_Writeback, and should make sure the page is unlocked, either
839af96c1e3STobin C. Harding	synchronously or asynchronously when the write operation
840ee5dc049STobin C. Harding	completes.
841ee5dc049STobin C. Harding
842af96c1e3STobin C. Harding	If wbc->sync_mode is WB_SYNC_NONE, ->writepage doesn't have to
843af96c1e3STobin C. Harding	try too hard if there are problems, and may choose to write out
844af96c1e3STobin C. Harding	other pages from the mapping if that is easier (e.g. due to
84508830c8bSMatthew Wilcox (Oracle)	internal dependencies).  If it chooses not to start writeout, it
84690c02eb9SMatthew Wilcox (Oracle)	should return AOP_WRITEPAGE_ACTIVATE so that the VM will not
84790c02eb9SMatthew Wilcox (Oracle)	keep calling ->writepage on that page.
84890c02eb9SMatthew Wilcox (Oracle)
84990c02eb9SMatthew Wilcox (Oracle)	See the file "Locking" for more details.
85090c02eb9SMatthew Wilcox (Oracle)
85190c02eb9SMatthew Wilcox (Oracle)``read_folio``
85290c02eb9SMatthew Wilcox (Oracle)	Called by the page cache to read a folio from the backing store.
85390c02eb9SMatthew Wilcox (Oracle)	The 'file' argument supplies authentication information to network
85490c02eb9SMatthew Wilcox (Oracle)	filesystems, and is generally not used by block based filesystems.
85590c02eb9SMatthew Wilcox (Oracle)	It may be NULL if the caller does not have an open file (eg if
85690c02eb9SMatthew Wilcox (Oracle)	the kernel is performing a read for itself rather than on behalf
85790c02eb9SMatthew Wilcox (Oracle)	of a userspace process with an open file).
85890c02eb9SMatthew Wilcox (Oracle)
85990c02eb9SMatthew Wilcox (Oracle)	If the mapping does not support large folios, the folio will
86090c02eb9SMatthew Wilcox (Oracle)	contain a single page.	The folio will be locked when read_folio
86190c02eb9SMatthew Wilcox (Oracle)	is called.  If the read completes successfully, the folio should
86290c02eb9SMatthew Wilcox (Oracle)	be marked uptodate.  The filesystem should unlock the folio
86390c02eb9SMatthew Wilcox (Oracle)	once the read has completed, whether it was successful or not.
86490c02eb9SMatthew Wilcox (Oracle)	The filesystem does not need to modify the refcount on the folio;
86590c02eb9SMatthew Wilcox (Oracle)	the page cache holds a reference count and that will not be
86690c02eb9SMatthew Wilcox (Oracle)	released until the folio is unlocked.
86790c02eb9SMatthew Wilcox (Oracle)
86890c02eb9SMatthew Wilcox (Oracle)	Filesystems may implement ->read_folio() synchronously.
86990c02eb9SMatthew Wilcox (Oracle)	In normal operation, folios are read through the ->readahead()
87090c02eb9SMatthew Wilcox (Oracle)	method.  Only if this fails, or if the caller needs to wait for
87190c02eb9SMatthew Wilcox (Oracle)	the read to complete will the page cache call ->read_folio().
87290c02eb9SMatthew Wilcox (Oracle)	Filesystems should not attempt to perform their own readahead
87390c02eb9SMatthew Wilcox (Oracle)	in the ->read_folio() operation.
87490c02eb9SMatthew Wilcox (Oracle)
87590c02eb9SMatthew Wilcox (Oracle)	If the filesystem cannot perform the read at this time, it can
87690c02eb9SMatthew Wilcox (Oracle)	unlock the folio, do whatever action it needs to ensure that the
87790c02eb9SMatthew Wilcox (Oracle)	read will succeed in the future and return AOP_TRUNCATED_PAGE.
878af96c1e3STobin C. Harding	In this case, the caller should look up the folio, lock it,
879ee5dc049STobin C. Harding	and call ->read_folio again.
880ee5dc049STobin C. Harding
881e9b2f15bSJulia Lawall	Callers may invoke the ->read_folio() method directly, but using
882af96c1e3STobin C. Harding	read_mapping_folio() will take care of locking, waiting for the
883e9b2f15bSJulia Lawall	read to complete and handle cases such as AOP_TRUNCATED_PAGE.
884ee5dc049STobin C. Harding
885ee5dc049STobin C. Harding``writepages``
886ee5dc049STobin C. Harding	called by the VM to write out pages associated with the
887ee5dc049STobin C. Harding	address_space object.  If wbc->sync_mode is WB_SYNC_ALL, then
888af96c1e3STobin C. Harding	the writeback_control will specify a range of pages that must be
8896f31a5a2SMatthew Wilcox (Oracle)	written out.  If it is WB_SYNC_NONE, then a nr_to_write is
8906f31a5a2SMatthew Wilcox (Oracle)	given and that many pages should be written if possible.  If no
8916f31a5a2SMatthew Wilcox (Oracle)	->writepages is given, then mpage_writepages is used instead.
8926f31a5a2SMatthew Wilcox (Oracle)	This will choose pages from the address space that are tagged as
893ee5dc049STobin C. Harding	DIRTY and will pass them to ->writepage.
8946f31a5a2SMatthew Wilcox (Oracle)
8956f31a5a2SMatthew Wilcox (Oracle)``dirty_folio``
896af96c1e3STobin C. Harding	called by the VM to mark a folio as dirty.  This is particularly
8978151b4c8SMatthew Wilcox (Oracle)	needed if an address space attaches private data to a folio, and
8988151b4c8SMatthew Wilcox (Oracle)	that data needs to be updated when a folio is dirtied.  This is
8998151b4c8SMatthew Wilcox (Oracle)	called, for example, when a memory mapped page gets modified.
9008151b4c8SMatthew Wilcox (Oracle)	If defined, it should set the folio dirty flag, and the
9018151b4c8SMatthew Wilcox (Oracle)	PAGECACHE_TAG_DIRTY search mark in i_pages.
90284dacdbdSNeilBrown
90384dacdbdSNeilBrown``readahead``
90484dacdbdSNeilBrown	Called by the VM to read pages associated with the address_space
90584dacdbdSNeilBrown	object.  The pages are consecutive in the page cache and are
90684dacdbdSNeilBrown	locked.  The implementation should decrement the page refcount
90784dacdbdSNeilBrown	after starting I/O on each page.  Usually the page will be
90884dacdbdSNeilBrown	unlocked by the I/O completion handler.  The set of pages are
90984dacdbdSNeilBrown	divided into some sync pages followed by some async pages,
91084dacdbdSNeilBrown	rac->ra->async_size gives the number of async pages.  The
91184dacdbdSNeilBrown	filesystem should attempt to read all sync pages but may decide
9128151b4c8SMatthew Wilcox (Oracle)	to stop once it reaches the async pages.  If it does decide to
913ee5dc049STobin C. Harding	stop attempting I/O, it can simply return.  The caller will
914ee5dc049STobin C. Harding	remove the remaining pages from the address space, unlock them
915ee5dc049STobin C. Harding	and decrement the page refcount.  Set PageUptodate if the I/O
916ee5dc049STobin C. Harding	completes successfully.  Setting PageError on any page will be
917ee5dc049STobin C. Harding	ignored; simply unlock the page if an I/O error occurs.
918ee5dc049STobin C. Harding
919ee5dc049STobin C. Harding``write_begin``
920ee5dc049STobin C. Harding	Called by the generic buffered write code to ask the filesystem
921ee5dc049STobin C. Harding	to prepare to write len bytes at the given offset in the file.
922af96c1e3STobin C. Harding	The address_space should check that the write will be able to
923ee5dc049STobin C. Harding	complete, by allocating space if necessary and doing any other
924ee5dc049STobin C. Harding	internal housekeeping.  If the write will update parts of any
925af96c1e3STobin C. Harding	basic-blocks on storage, then those blocks should be pre-read
926ee5dc049STobin C. Harding	(if they haven't been read already) so that the updated blocks
927ee5dc049STobin C. Harding	can be written out properly.
928ee5dc049STobin C. Harding
929af96c1e3STobin C. Harding	The filesystem must return the locked pagecache page for the
930af96c1e3STobin C. Harding	specified offset, in ``*pagep``, for the caller to write into.
931af96c1e3STobin C. Harding
932af96c1e3STobin C. Harding	It must be able to cope with short writes (where the length
933ee5dc049STobin C. Harding	passed to write_begin is greater than the number of bytes copied
934ee5dc049STobin C. Harding	into the page).
935af96c1e3STobin C. Harding
936ee5dc049STobin C. Harding	A void * may be returned in fsdata, which then gets passed into
937ee5dc049STobin C. Harding	write_end.
938ee5dc049STobin C. Harding
939ee5dc049STobin C. Harding	Returns 0 on success; < 0 on failure (which is the error code),
940af96c1e3STobin C. Harding	in which case write_end is not called.
941ee5dc049STobin C. Harding
942ee5dc049STobin C. Harding``write_end``
943af96c1e3STobin C. Harding	After a successful write_begin, and data copy, write_end must be
944ee5dc049STobin C. Harding	called.  len is the original len passed to write_begin, and
945ee5dc049STobin C. Harding	copied is the amount that was able to be copied.
946af96c1e3STobin C. Harding
947ee5dc049STobin C. Harding	The filesystem must take care of unlocking the page and
948ee5dc049STobin C. Harding	releasing it refcount, and updating i_size.
949ee5dc049STobin C. Harding
950ee5dc049STobin C. Harding	Returns < 0 on failure, otherwise the number of bytes (<=
951ee5dc049STobin C. Harding	'copied') that were able to be copied into pagecache.
952ee5dc049STobin C. Harding
953ee5dc049STobin C. Harding``bmap``
954ee5dc049STobin C. Harding	called by the VFS to map a logical block offset within object to
955af96c1e3STobin C. Harding	physical block number.  This method is used by the FIBMAP ioctl
956128d1f82SMatthew Wilcox (Oracle)	and for working with swap-files.  To be able to swap to a file,
957128d1f82SMatthew Wilcox (Oracle)	the file must have a stable mapping to a block device.  The swap
958128d1f82SMatthew Wilcox (Oracle)	system does not go through the filesystem but instead uses bmap
959ee5dc049STobin C. Harding	to find out where the blocks in the file are and uses those
960af96c1e3STobin C. Harding	addresses directly.
961af96c1e3STobin C. Harding
962fa29000bSMatthew Wilcox (Oracle)``invalidate_folio``
963ee5dc049STobin C. Harding	If a folio has private data, then invalidate_folio will be
964128d1f82SMatthew Wilcox (Oracle)	called when part or all of the folio is to be removed from the
965fa29000bSMatthew Wilcox (Oracle)	address space.  This generally corresponds to either a
966fa29000bSMatthew Wilcox (Oracle)	truncation, punch hole or a complete invalidation of the address
967ee5dc049STobin C. Harding	space (in the latter case 'offset' will always be 0 and 'length'
968af96c1e3STobin C. Harding	will be folio_size()).  Any private data associated with the folio
969fa29000bSMatthew Wilcox (Oracle)	should be updated to reflect this truncation.  If offset is 0
970fa29000bSMatthew Wilcox (Oracle)	and length is folio_size(), then the private data should be
971fa29000bSMatthew Wilcox (Oracle)	released, because the folio must be able to be completely
972fa29000bSMatthew Wilcox (Oracle)	discarded.  This may be done by calling the ->release_folio
973fa29000bSMatthew Wilcox (Oracle)	function, but in this case the release MUST succeed.
974fa29000bSMatthew Wilcox (Oracle)
975fa29000bSMatthew Wilcox (Oracle)``release_folio``
976fa29000bSMatthew Wilcox (Oracle)	release_folio is called on folios with private data to tell the
977fa29000bSMatthew Wilcox (Oracle)	filesystem that the folio is about to be freed.  ->release_folio
978af96c1e3STobin C. Harding	should remove any private data from the folio and clear the
979af96c1e3STobin C. Harding	private flag.  If release_folio() fails, it should return false.
980fa29000bSMatthew Wilcox (Oracle)	release_folio() is used in two distinct though related cases.
981fa29000bSMatthew Wilcox (Oracle)	The first is when the VM wants to free a clean folio with no
982fa29000bSMatthew Wilcox (Oracle)	active users.  If ->release_folio succeeds, the folio will be
983ee5dc049STobin C. Harding	removed from the address_space and be freed.
984ee5dc049STobin C. Harding
985fa29000bSMatthew Wilcox (Oracle)	The second case is when a request has been made to invalidate
986fa29000bSMatthew Wilcox (Oracle)	some or all folios in an address_space.  This can happen
987fa29000bSMatthew Wilcox (Oracle)	through the fadvise(POSIX_FADV_DONTNEED) system call or by the
988af96c1e3STobin C. Harding	filesystem explicitly requesting it as nfs and 9p do (when they
989d2329aa0SMatthew Wilcox (Oracle)	believe the cache may be out of date with storage) by calling
990d2329aa0SMatthew Wilcox (Oracle)	invalidate_inode_pages2().  If the filesystem makes such a call,
991ee5dc049STobin C. Harding	and needs to be certain that all folios are invalidated, then
992ee5dc049STobin C. Harding	its release_folio will need to ensure this.  Possibly it can
993ee5dc049STobin C. Harding	clear the uptodate flag if it cannot free private data yet.
994ee5dc049STobin C. Harding
995af96c1e3STobin C. Harding``free_folio``
996ee5dc049STobin C. Harding	free_folio is called once the folio is no longer visible in the
997ee5dc049STobin C. Harding	page cache in order to allow the cleanup of any private data.
998ee5dc049STobin C. Harding	Since it may be called by the memory reclaimer, it should not
999ee5dc049STobin C. Harding	assume that the original address_space mapping still exists, and
1000ee5dc049STobin C. Harding	it should not block.
1001af96c1e3STobin C. Harding
10025490da4fSMatthew Wilcox (Oracle)``direct_IO``
1003ee5dc049STobin C. Harding	called by the generic read/write routines to perform direct_IO -
10045490da4fSMatthew Wilcox (Oracle)	that is IO requests which bypass the page cache and transfer
10055490da4fSMatthew Wilcox (Oracle)	data directly between the storage and the application's address
10065490da4fSMatthew Wilcox (Oracle)	space.
10075490da4fSMatthew Wilcox (Oracle)
1008af96c1e3STobin C. Harding``migrate_folio``
1009affa80e8SMatthew Wilcox (Oracle)	This is used to compact the physical memory usage.  If the VM
1010affa80e8SMatthew Wilcox (Oracle)	wants to relocate a folio (maybe from a memory device that is
1011affa80e8SMatthew Wilcox (Oracle)	signalling imminent failure) it will pass a new folio and an old
1012ee5dc049STobin C. Harding	folio to this function.  migrate_folio should transfer any private
1013af96c1e3STobin C. Harding	data across and update any references that it has to the folio.
1014ee5dc049STobin C. Harding
1015ee5dc049STobin C. Harding``launder_folio``
10162e7e80f7SMatthew Wilcox (Oracle)	Called before freeing a folio - it writes back the dirty folio.
10172e7e80f7SMatthew Wilcox (Oracle)	To prevent redirtying the folio, it is kept locked during the
10182e7e80f7SMatthew Wilcox (Oracle)	whole operation.
1019af96c1e3STobin C. Harding
1020ee5dc049STobin C. Harding``is_partially_uptodate``
1021520f301cSMatthew Wilcox (Oracle)	Called by the VM when reading a file through the pagecache when
1022ee5dc049STobin C. Harding	the underlying blocksize is smaller than the size of the folio.
1023ee5dc049STobin C. Harding	If the required block is up to date then the read can complete
1024520f301cSMatthew Wilcox (Oracle)	without needing I/O to bring the whole page up to date.
1025520f301cSMatthew Wilcox (Oracle)
1026ee5dc049STobin C. Harding``is_dirty_writeback``
1027ee5dc049STobin C. Harding	Called by the VM when attempting to reclaim a folio.  The VM uses
1028520f301cSMatthew Wilcox (Oracle)	dirty and writeback information to determine if it needs to
1029ee5dc049STobin C. Harding	stall to allow flushers a chance to complete some IO.
1030af96c1e3STobin C. Harding	Ordinarily it can use folio_test_dirty and folio_test_writeback but
1031ee5dc049STobin C. Harding	some filesystems have more complex state (unstable folios in NFS
1032ee5dc049STobin C. Harding	prevent reclaim) or do not set those flags due to locking
1033ee5dc049STobin C. Harding	problems.  This callback allows a filesystem to indicate to the
1034af96c1e3STobin C. Harding	VM if a folio should be treated as dirty or writeback for the
1035af96c1e3STobin C. Harding	purposes of stalling.
1036af96c1e3STobin C. Harding
1037ee5dc049STobin C. Harding``error_remove_page``
1038cba738f6SNeilBrown	normally set to generic_error_remove_page if truncation is ok
1039cba738f6SNeilBrown	for this address space.  Used for memory failure handling.
1040cba738f6SNeilBrown	Setting this implies you deal with pages going away under you,
1041cba738f6SNeilBrown	unless you have them locked or reference counts increased.
1042cba738f6SNeilBrown
1043cba738f6SNeilBrown``swap_activate``
1044cba738f6SNeilBrown
1045cba738f6SNeilBrown	Called to prepare the given file for swap.  It should perform
1046af96c1e3STobin C. Harding	any validation and preparation necessary to ensure that writes
1047ee5dc049STobin C. Harding	can be performed with minimal memory allocation.  It should call
1048ee5dc049STobin C. Harding	add_swap_extent(), or the helper iomap_swapfile_activate(), and
1049ee5dc049STobin C. Harding	return the number of extents added.  If IO should be submitted
1050af96c1e3STobin C. Harding	through ->swap_rw(), it should set SWP_FS_OPS, otherwise IO will
1051cba738f6SNeilBrown	be submitted directly to the block device ``sis->bdev``.
1052cba738f6SNeilBrown
1053af96c1e3STobin C. Harding``swap_deactivate``
1054af96c1e3STobin C. Harding	Called during swapoff on files where swap_activate was
1055af96c1e3STobin C. Harding	successful.
1056af96c1e3STobin C. Harding
1057af96c1e3STobin C. Harding``swap_rw``
1058af96c1e3STobin C. Harding	Called to read or write swap pages when SWP_FS_OPS is set.
1059af96c1e3STobin C. Harding
1060af96c1e3STobin C. HardingThe File Object
1061af96c1e3STobin C. Harding===============
1062af96c1e3STobin C. Harding
1063af96c1e3STobin C. HardingA file object represents a file opened by a process.  This is also known
1064af96c1e3STobin C. Hardingas an "open file description" in POSIX parlance.
1065af96c1e3STobin C. Harding
1066af96c1e3STobin C. Harding
1067af96c1e3STobin C. Hardingstruct file_operations
1068af96c1e3STobin C. Harding----------------------
1069af96c1e3STobin C. Harding
1070af96c1e3STobin C. HardingThis describes how the VFS can manipulate an open file.  As of kernel
1071af96c1e3STobin C. Harding4.18, the following members are defined:
1072af96c1e3STobin C. Harding
1073af96c1e3STobin C. Harding.. code-block:: c
1074af96c1e3STobin C. Harding
1075af96c1e3STobin C. Harding	struct file_operations {
1076af96c1e3STobin C. Harding		struct module *owner;
1077af96c1e3STobin C. Harding		loff_t (*llseek) (struct file *, loff_t, int);
1078af96c1e3STobin C. Harding		ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
1079af96c1e3STobin C. Harding		ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
1080af96c1e3STobin C. Harding		ssize_t (*read_iter) (struct kiocb *, struct iov_iter *);
1081af96c1e3STobin C. Harding		ssize_t (*write_iter) (struct kiocb *, struct iov_iter *);
1082af96c1e3STobin C. Harding		int (*iopoll)(struct kiocb *kiocb, bool spin);
1083af96c1e3STobin C. Harding		int (*iterate_shared) (struct file *, struct dir_context *);
1084af96c1e3STobin C. Harding		__poll_t (*poll) (struct file *, struct poll_table_struct *);
1085af96c1e3STobin C. Harding		long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
1086af96c1e3STobin C. Harding		long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
1087af96c1e3STobin C. Harding		int (*mmap) (struct file *, struct vm_area_struct *);
1088af96c1e3STobin C. Harding		int (*open) (struct inode *, struct file *);
1089af96c1e3STobin C. Harding		int (*flush) (struct file *, fl_owner_t id);
1090af96c1e3STobin C. Harding		int (*release) (struct inode *, struct file *);
1091af96c1e3STobin C. Harding		int (*fsync) (struct file *, loff_t, loff_t, int datasync);
1092af96c1e3STobin C. Harding		int (*fasync) (int, struct file *, int);
1093af96c1e3STobin C. Harding		int (*lock) (struct file *, int, struct file_lock *);
1094af96c1e3STobin C. Harding		unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
1095af96c1e3STobin C. Harding		int (*check_flags)(int);
1096af96c1e3STobin C. Harding		int (*flock) (struct file *, int, struct file_lock *);
1097af96c1e3STobin C. Harding		ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
1098af96c1e3STobin C. Harding		ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
1099af96c1e3STobin C. Harding		int (*setlease)(struct file *, long, struct file_lock **, void **);
1100af96c1e3STobin C. Harding		long (*fallocate)(struct file *file, int mode, loff_t offset,
1101af96c1e3STobin C. Harding				  loff_t len);
1102af96c1e3STobin C. Harding		void (*show_fdinfo)(struct seq_file *m, struct file *f);
1103af96c1e3STobin C. Harding	#ifndef CONFIG_MMU
1104af96c1e3STobin C. Harding		unsigned (*mmap_capabilities)(struct file *);
1105af96c1e3STobin C. Harding	#endif
1106af96c1e3STobin C. Harding		ssize_t (*copy_file_range)(struct file *, loff_t, struct file *, loff_t, size_t, unsigned int);
1107af96c1e3STobin C. Harding		loff_t (*remap_file_range)(struct file *file_in, loff_t pos_in,
1108af96c1e3STobin C. Harding					   struct file *file_out, loff_t pos_out,
1109af96c1e3STobin C. Harding					   loff_t len, unsigned int remap_flags);
1110ee5dc049STobin C. Harding		int (*fadvise)(struct file *, loff_t, loff_t, int);
1111ee5dc049STobin C. Harding	};
1112af96c1e3STobin C. Harding
1113ee5dc049STobin C. HardingAgain, all methods are called without any locks being held, unless
1114ee5dc049STobin C. Hardingotherwise noted.
1115af96c1e3STobin C. Harding
1116ee5dc049STobin C. Harding``llseek``
1117ee5dc049STobin C. Harding	called when the VFS needs to move the file position index
1118af96c1e3STobin C. Harding
1119ee5dc049STobin C. Harding``read``
1120ee5dc049STobin C. Harding	called by read(2) and related system calls
1121af96c1e3STobin C. Harding
1122ee5dc049STobin C. Harding``read_iter``
1123ee5dc049STobin C. Harding	possibly asynchronous read with iov_iter as destination
1124af96c1e3STobin C. Harding
1125ee5dc049STobin C. Harding``write``
1126ee5dc049STobin C. Harding	called by write(2) and related system calls
1127af96c1e3STobin C. Harding
1128ee5dc049STobin C. Harding``write_iter``
112999b319d3SJonathan Corbet	possibly asynchronous write with iov_iter as source
1130af96c1e3STobin C. Harding
1131ee5dc049STobin C. Harding``iopoll``
1132ee5dc049STobin C. Harding	called when aio wants to poll for completions on HIPRI iocbs
1133af96c1e3STobin C. Harding
1134af96c1e3STobin C. Harding``iterate_shared``
1135af96c1e3STobin C. Harding	called when the VFS needs to read the directory contents
1136ee5dc049STobin C. Harding
1137ee5dc049STobin C. Harding``poll``
1138af96c1e3STobin C. Harding	called by the VFS when a process wants to check if there is
1139ee5dc049STobin C. Harding	activity on this file and (optionally) go to sleep until there
1140ee5dc049STobin C. Harding	is activity.  Called by the select(2) and poll(2) system calls
1141ee5dc049STobin C. Harding
1142af96c1e3STobin C. Harding``unlocked_ioctl``
1143ee5dc049STobin C. Harding	called by the ioctl(2) system call.
1144ee5dc049STobin C. Harding
1145af96c1e3STobin C. Harding``compat_ioctl``
1146ee5dc049STobin C. Harding	called by the ioctl(2) system call when 32 bit system calls are
1147ee5dc049STobin C. Harding	 used on 64 bit kernels.
1148af96c1e3STobin C. Harding
1149af96c1e3STobin C. Harding``mmap``
1150ee5dc049STobin C. Harding	called by the mmap(2) system call
1151ee5dc049STobin C. Harding
1152ee5dc049STobin C. Harding``open``
1153ee5dc049STobin C. Harding	called by the VFS when an inode should be opened.  When the VFS
1154af96c1e3STobin C. Harding	opens a file, it creates a new "struct file".  It then calls the
1155af96c1e3STobin C. Harding	open method for the newly allocated file structure.  You might
1156af96c1e3STobin C. Harding	think that the open method really belongs in "struct
1157ee5dc049STobin C. Harding	inode_operations", and you may be right.  I think it's done the
1158ee5dc049STobin C. Harding	way it is because it makes filesystems simpler to implement.
1159af96c1e3STobin C. Harding	The open() method is a good place to initialize the
1160ee5dc049STobin C. Harding	"private_data" member in the file structure if you want to point
1161ee5dc049STobin C. Harding	to a device structure
1162af96c1e3STobin C. Harding
1163ee5dc049STobin C. Harding``flush``
1164ee5dc049STobin C. Harding	called by the close(2) system call to flush a file
1165af96c1e3STobin C. Harding
1166af96c1e3STobin C. Harding``release``
1167ee5dc049STobin C. Harding	called when the last reference to an open file is closed
1168ee5dc049STobin C. Harding
1169af96c1e3STobin C. Harding``fsync``
1170af96c1e3STobin C. Harding	called by the fsync(2) system call.  Also see the section above
1171ee5dc049STobin C. Harding	entitled "Handling errors during writeback".
1172ee5dc049STobin C. Harding
1173ee5dc049STobin C. Harding``fasync``
1174af96c1e3STobin C. Harding	called by the fcntl(2) system call when asynchronous
1175ee5dc049STobin C. Harding	(non-blocking) mode is enabled for a file
1176ee5dc049STobin C. Harding
1177af96c1e3STobin C. Harding``lock``
1178ee5dc049STobin C. Harding	called by the fcntl(2) system call for F_GETLK, F_SETLK, and
1179ee5dc049STobin C. Harding	F_SETLKW commands
1180af96c1e3STobin C. Harding
1181ee5dc049STobin C. Harding``get_unmapped_area``
1182ee5dc049STobin C. Harding	called by the mmap(2) system call
1183af96c1e3STobin C. Harding
1184ee5dc049STobin C. Harding``check_flags``
1185ee5dc049STobin C. Harding	called by the fcntl(2) system call for F_SETFL command
1186af96c1e3STobin C. Harding
1187af96c1e3STobin C. Harding``flock``
1188ee5dc049STobin C. Harding	called by the flock(2) system call
1189ee5dc049STobin C. Harding
1190af96c1e3STobin C. Harding``splice_write``
1191af96c1e3STobin C. Harding	called by the VFS to splice data from a pipe to a file.  This
1192ee5dc049STobin C. Harding	method is used by the splice(2) system call
1193ee5dc049STobin C. Harding
1194af96c1e3STobin C. Harding``splice_read``
1195af96c1e3STobin C. Harding	called by the VFS to splice data from file to a pipe.  This
1196af96c1e3STobin C. Harding	method is used by the splice(2) system call
1197ee5dc049STobin C. Harding
1198ee5dc049STobin C. Harding``setlease``
1199af96c1e3STobin C. Harding	called by the VFS to set or release a file lock lease.  setlease
1200ee5dc049STobin C. Harding	implementations should call generic_setlease to record or remove
1201ee5dc049STobin C. Harding	the lease in the inode after setting it.
1202af96c1e3STobin C. Harding
1203ee5dc049STobin C. Harding``fallocate``
1204ee5dc049STobin C. Harding	called by the VFS to preallocate blocks or punch a hole.
1205ee5dc049STobin C. Harding
1206ee5dc049STobin C. Harding``copy_file_range``
1207ee5dc049STobin C. Harding	called by the copy_file_range(2) system call.
1208ee5dc049STobin C. Harding
1209ee5dc049STobin C. Harding``remap_file_range``
1210ee5dc049STobin C. Harding	called by the ioctl(2) system call for FICLONERANGE and FICLONE
1211ee5dc049STobin C. Harding	and FIDEDUPERANGE commands to remap file ranges.  An
1212ee5dc049STobin C. Harding	implementation should remap len bytes at pos_in of the source
1213ee5dc049STobin C. Harding	file into the dest file at pos_out.  Implementations must handle
1214cb56ecaeSJulia Lawall	callers passing in len == 0; this means "remap to the end of the
1215ee5dc049STobin C. Harding	source file".  The return value should the number of bytes
1216ee5dc049STobin C. Harding	remapped, or the usual negative error code if errors occurred
1217af96c1e3STobin C. Harding	before any bytes were remapped.  The remap_flags parameter
1218ee5dc049STobin C. Harding	accepts REMAP_FILE_* flags.  If REMAP_FILE_DEDUP is set then the
1219ee5dc049STobin C. Harding	implementation must only remap if the requested file ranges have
1220af96c1e3STobin C. Harding	identical contents.  If REMAP_FILE_CAN_SHORTEN is set, the caller is
1221af96c1e3STobin C. Harding	ok with the implementation shortening the request length to
1222af96c1e3STobin C. Harding	satisfy alignment or EOF requirements (or any other reason).
1223af96c1e3STobin C. Harding
1224af96c1e3STobin C. Harding``fadvise``
1225af96c1e3STobin C. Harding	possibly called by the fadvise64() system call.
1226af96c1e3STobin C. Harding
1227af96c1e3STobin C. HardingNote that the file operations are implemented by the specific
1228af96c1e3STobin C. Hardingfilesystem in which the inode resides.  When opening a device node
1229af96c1e3STobin C. Harding(character or block special) most filesystems will call special
1230af96c1e3STobin C. Hardingsupport routines in the VFS which will locate the required device
1231af96c1e3STobin C. Hardingdriver information.  These support routines replace the filesystem file
1232af96c1e3STobin C. Hardingoperations with those for the device driver, and then proceed to call
1233af96c1e3STobin C. Hardingthe new open() method for the file.  This is how opening a device file
1234af96c1e3STobin C. Hardingin the filesystem eventually ends up calling the device driver open()
1235af96c1e3STobin C. Hardingmethod.
1236af96c1e3STobin C. Harding
1237af96c1e3STobin C. Harding
1238af96c1e3STobin C. HardingDirectory Entry Cache (dcache)
1239af96c1e3STobin C. Harding==============================
1240af96c1e3STobin C. Harding
1241af96c1e3STobin C. Harding
1242af96c1e3STobin C. Hardingstruct dentry_operations
1243af96c1e3STobin C. Harding------------------------
1244af96c1e3STobin C. Harding
1245af96c1e3STobin C. HardingThis describes how a filesystem can overload the standard dentry
1246af96c1e3STobin C. Hardingoperations.  Dentries and the dcache are the domain of the VFS and the
1247af96c1e3STobin C. Hardingindividual filesystem implementations.  Device drivers have no business
1248af96c1e3STobin C. Hardinghere.  These methods may be set to NULL, as they are either optional or
1249af96c1e3STobin C. Hardingthe VFS uses a default.  As of kernel 2.6.22, the following members are
1250af96c1e3STobin C. Hardingdefined:
1251af96c1e3STobin C. Harding
1252af96c1e3STobin C. Harding.. code-block:: c
1253af96c1e3STobin C. Harding
1254af96c1e3STobin C. Harding	struct dentry_operations {
1255af96c1e3STobin C. Harding		int (*d_revalidate)(struct dentry *, unsigned int);
1256af96c1e3STobin C. Harding		int (*d_weak_revalidate)(struct dentry *, unsigned int);
1257af96c1e3STobin C. Harding		int (*d_hash)(const struct dentry *, struct qstr *);
1258af96c1e3STobin C. Harding		int (*d_compare)(const struct dentry *,
1259af96c1e3STobin C. Harding				 unsigned int, const char *, const struct qstr *);
1260af96c1e3STobin C. Harding		int (*d_delete)(const struct dentry *);
1261af96c1e3STobin C. Harding		int (*d_init)(struct dentry *);
1262af96c1e3STobin C. Harding		void (*d_release)(struct dentry *);
1263af96c1e3STobin C. Harding		void (*d_iput)(struct dentry *, struct inode *);
1264ee5dc049STobin C. Harding		char *(*d_dname)(struct dentry *, char *, int);
1265ee5dc049STobin C. Harding		struct vfsmount *(*d_automount)(struct path *);
1266ee5dc049STobin C. Harding		int (*d_manage)(const struct path *, bool);
1267ee5dc049STobin C. Harding		struct dentry *(*d_real)(struct dentry *, const struct inode *);
1268ee5dc049STobin C. Harding	};
1269ee5dc049STobin C. Harding
1270ee5dc049STobin C. Harding``d_revalidate``
1271af96c1e3STobin C. Harding	called when the VFS needs to revalidate a dentry.  This is
1272ee5dc049STobin C. Harding	called whenever a name look-up finds a dentry in the dcache.
1273ee5dc049STobin C. Harding	Most local filesystems leave this as NULL, because all their
1274af96c1e3STobin C. Harding	dentries in the dcache are valid.  Network filesystems are
1275ee5dc049STobin C. Harding	different since things can change on the server without the
1276ee5dc049STobin C. Harding	client necessarily being aware of it.
1277ee5dc049STobin C. Harding
1278ee5dc049STobin C. Harding	This function should return a positive value if the dentry is
1279ee5dc049STobin C. Harding	still valid, and zero or a negative error code if it isn't.
1280ee5dc049STobin C. Harding
1281af96c1e3STobin C. Harding	d_revalidate may be called in rcu-walk mode (flags &
1282ee5dc049STobin C. Harding	LOOKUP_RCU).  If in rcu-walk mode, the filesystem must
1283ee5dc049STobin C. Harding	revalidate the dentry without blocking or storing to the dentry,
1284af96c1e3STobin C. Harding	d_parent and d_inode should not be used without care (because
1285af96c1e3STobin C. Harding	they can change and, in d_inode case, even become NULL under
128674596085SGlenn Washburn	us).
1287ee5dc049STobin C. Harding
1288ee5dc049STobin C. Harding	If a situation is encountered that rcu-walk cannot handle,
1289ee5dc049STobin C. Harding	return
1290ee5dc049STobin C. Harding	-ECHILD and it will be called again in ref-walk mode.
1291ee5dc049STobin C. Harding
1292af96c1e3STobin C. Harding``d_weak_revalidate``
1293ee5dc049STobin C. Harding	called when the VFS needs to revalidate a "jumped" dentry.  This
1294ee5dc049STobin C. Harding	is called when a path-walk ends at dentry that was not acquired
1295ee5dc049STobin C. Harding	by doing a lookup in the parent directory.  This includes "/",
1296ee5dc049STobin C. Harding	"." and "..", as well as procfs-style symlinks and mountpoint
1297af96c1e3STobin C. Harding	traversal.
1298ee5dc049STobin C. Harding
1299ee5dc049STobin C. Harding	In this case, we are less concerned with whether the dentry is
1300af96c1e3STobin C. Harding	still fully correct, but rather that the inode is still valid.
1301af96c1e3STobin C. Harding	As with d_revalidate, most local filesystems will set this to
1302af96c1e3STobin C. Harding	NULL since their dcache entries are always valid.
1303ee5dc049STobin C. Harding
1304ee5dc049STobin C. Harding	This function has the same return code semantics as
1305af96c1e3STobin C. Harding	d_revalidate.
1306af96c1e3STobin C. Harding
1307af96c1e3STobin C. Harding	d_weak_revalidate is only called after leaving rcu-walk mode.
1308af96c1e3STobin C. Harding
1309af96c1e3STobin C. Harding``d_hash``
1310af96c1e3STobin C. Harding	called when the VFS adds a dentry to the hash table.  The first
1311ee5dc049STobin C. Harding	dentry passed to d_hash is the parent directory that the name is
1312ee5dc049STobin C. Harding	to be hashed into.
1313af96c1e3STobin C. Harding
1314ee5dc049STobin C. Harding	Same locking and synchronisation rules as d_compare regarding
1315ee5dc049STobin C. Harding	what is safe to dereference etc.
1316af96c1e3STobin C. Harding
1317af96c1e3STobin C. Harding``d_compare``
1318ee5dc049STobin C. Harding	called to compare a dentry name with a given name.  The first
1319ee5dc049STobin C. Harding	dentry is the parent of the dentry to be compared, the second is
1320ee5dc049STobin C. Harding	the child dentry.  len and name string are properties of the
1321af96c1e3STobin C. Harding	dentry to be compared.  qstr is the name to compare it with.
1322ee5dc049STobin C. Harding
1323ee5dc049STobin C. Harding	Must be constant and idempotent, and should not take locks if
1324ee5dc049STobin C. Harding	possible, and should not or store into the dentry.  Should not
1325af96c1e3STobin C. Harding	dereference pointers outside the dentry without lots of care
1326ee5dc049STobin C. Harding	(eg.  d_parent, d_inode, d_name should not be used).
1327ee5dc049STobin C. Harding
1328af96c1e3STobin C. Harding	However, our vfsmount is pinned, and RCU held, so the dentries
1329ee5dc049STobin C. Harding	and inodes won't disappear, neither will our sb or filesystem
1330ee5dc049STobin C. Harding	module.  ->d_sb may be used.
1331ee5dc049STobin C. Harding
1332ee5dc049STobin C. Harding	It is a tricky calling convention because it needs to be called
1333ee5dc049STobin C. Harding	under "rcu-walk", ie. without any locks or references on things.
1334ee5dc049STobin C. Harding
1335af96c1e3STobin C. Harding``d_delete``
1336ee5dc049STobin C. Harding	called when the last reference to a dentry is dropped and the
1337ee5dc049STobin C. Harding	dcache is deciding whether or not to cache it.  Return 1 to
1338af96c1e3STobin C. Harding	delete immediately, or 0 to cache the dentry.  Default is NULL
1339ee5dc049STobin C. Harding	which means to always cache a reachable dentry.  d_delete must
1340ee5dc049STobin C. Harding	be constant and idempotent.
1341af96c1e3STobin C. Harding
1342ee5dc049STobin C. Harding``d_init``
1343ee5dc049STobin C. Harding	called when a dentry is allocated
1344ee5dc049STobin C. Harding
1345ee5dc049STobin C. Harding``d_release``
1346ee5dc049STobin C. Harding	called when a dentry is really deallocated
1347af96c1e3STobin C. Harding
1348ee5dc049STobin C. Harding``d_iput``
1349ee5dc049STobin C. Harding	called when a dentry loses its inode (just prior to its being
1350ee5dc049STobin C. Harding	deallocated).  The default when this is NULL is that the VFS
1351ee5dc049STobin C. Harding	calls iput().  If you define this method, you must call iput()
1352ee5dc049STobin C. Harding	yourself
1353ee5dc049STobin C. Harding
1354ee5dc049STobin C. Harding``d_dname``
1355ee5dc049STobin C. Harding	called when the pathname of a dentry should be generated.
1356ee5dc049STobin C. Harding	Useful for some pseudo filesystems (sockfs, pipefs, ...) to
1357ee5dc049STobin C. Harding	delay pathname generation.  (Instead of doing it when dentry is
1358ee5dc049STobin C. Harding	created, it's done only when the path is needed.).  Real
1359ee5dc049STobin C. Harding	filesystems probably dont want to use it, because their dentries
1360ee5dc049STobin C. Harding	are present in global dcache hash, so their hash should be an
1361ee5dc049STobin C. Harding	invariant.  As no lock is held, d_dname() should not try to
1362af96c1e3STobin C. Harding	modify the dentry itself, unless appropriate SMP safety is used.
1363af96c1e3STobin C. Harding	CAUTION : d_path() logic is quite tricky.  The correct way to
1364af96c1e3STobin C. Harding	return for example "Hello" is to put it at the end of the
1365af96c1e3STobin C. Harding	buffer, and returns a pointer to the first char.
1366af96c1e3STobin C. Harding	dynamic_dname() helper function is provided to take care of
1367af96c1e3STobin C. Harding	this.
1368af96c1e3STobin C. Harding
1369af96c1e3STobin C. Harding	Example :
1370af96c1e3STobin C. Harding
1371af96c1e3STobin C. Harding.. code-block:: c
1372af96c1e3STobin C. Harding
1373ee5dc049STobin C. Harding	static char *pipefs_dname(struct dentry *dent, char *buffer, int buflen)
1374ee5dc049STobin C. Harding	{
1375ee5dc049STobin C. Harding		return dynamic_dname(dentry, buffer, buflen, "pipe:[%lu]",
1376ee5dc049STobin C. Harding				dentry->d_inode->i_ino);
1377ee5dc049STobin C. Harding	}
1378ee5dc049STobin C. Harding
1379ee5dc049STobin C. Harding``d_automount``
1380ee5dc049STobin C. Harding	called when an automount dentry is to be traversed (optional).
1381ee5dc049STobin C. Harding	This should create a new VFS mount record and return the record
1382ee5dc049STobin C. Harding	to the caller.  The caller is supplied with a path parameter
1383ee5dc049STobin C. Harding	giving the automount directory to describe the automount target
1384af96c1e3STobin C. Harding	and the parent VFS mount record to provide inheritable mount
1385ee5dc049STobin C. Harding	parameters.  NULL should be returned if someone else managed to
1386ee5dc049STobin C. Harding	make the automount first.  If the vfsmount creation failed, then
1387ee5dc049STobin C. Harding	an error code should be returned.  If -EISDIR is returned, then
1388ee5dc049STobin C. Harding	the directory will be treated as an ordinary directory and
1389ee5dc049STobin C. Harding	returned to pathwalk to continue walking.
1390af96c1e3STobin C. Harding
1391ee5dc049STobin C. Harding	If a vfsmount is returned, the caller will attempt to mount it
1392ee5dc049STobin C. Harding	on the mountpoint and will remove the vfsmount from its
1393ee5dc049STobin C. Harding	expiration list in the case of failure.  The vfsmount should be
1394af96c1e3STobin C. Harding	returned with 2 refs on it to prevent automatic expiration - the
1395ee5dc049STobin C. Harding	caller will clean up the additional ref.
1396ee5dc049STobin C. Harding
1397ee5dc049STobin C. Harding	This function is only used if DCACHE_NEED_AUTOMOUNT is set on
1398ee5dc049STobin C. Harding	the dentry.  This is set by __d_instantiate() if S_AUTOMOUNT is
1399ee5dc049STobin C. Harding	set on the inode being added.
1400ee5dc049STobin C. Harding
1401ee5dc049STobin C. Harding``d_manage``
1402ee5dc049STobin C. Harding	called to allow the filesystem to manage the transition from a
1403ee5dc049STobin C. Harding	dentry (optional).  This allows autofs, for example, to hold up
1404ee5dc049STobin C. Harding	clients waiting to explore behind a 'mountpoint' while letting
1405af96c1e3STobin C. Harding	the daemon go past and construct the subtree there.  0 should be
1406af96c1e3STobin C. Harding	returned to let the calling process continue.  -EISDIR can be
1407ee5dc049STobin C. Harding	returned to tell pathwalk to use this directory as an ordinary
1408ee5dc049STobin C. Harding	directory and to ignore anything mounted on it and not to check
1409ee5dc049STobin C. Harding	the automount flag.  Any other error code will abort pathwalk
1410ee5dc049STobin C. Harding	completely.
1411af96c1e3STobin C. Harding
1412ee5dc049STobin C. Harding	If the 'rcu_walk' parameter is true, then the caller is doing a
1413ee5dc049STobin C. Harding	pathwalk in RCU-walk mode.  Sleeping is not permitted in this
1414af96c1e3STobin C. Harding	mode, and the caller can be asked to leave it and call again by
1415ee5dc049STobin C. Harding	returning -ECHILD.  -EISDIR may also be returned to tell
1416ee5dc049STobin C. Harding	pathwalk to ignore d_automount or any mounts.
1417ee5dc049STobin C. Harding
1418ee5dc049STobin C. Harding	This function is only used if DCACHE_MANAGE_TRANSIT is set on
1419af96c1e3STobin C. Harding	the dentry being transited from.
1420ee5dc049STobin C. Harding
1421ee5dc049STobin C. Harding``d_real``
1422ee5dc049STobin C. Harding	overlay/union type filesystems implement this method to return
1423ee5dc049STobin C. Harding	one of the underlying dentries hidden by the overlay.  It is
1424af96c1e3STobin C. Harding	used in two different modes:
1425af96c1e3STobin C. Harding
1426af96c1e3STobin C. Harding	Called from file_dentry() it returns the real dentry matching
1427af96c1e3STobin C. Harding	the inode argument.  The real dentry may be from a lower layer
1428af96c1e3STobin C. Harding	already copied up, but still referenced from the file.  This
1429af96c1e3STobin C. Harding	mode is selected with a non-NULL inode argument.
1430af96c1e3STobin C. Harding
1431af96c1e3STobin C. Harding	With NULL inode the topmost real underlying dentry is returned.
1432af96c1e3STobin C. Harding
1433af96c1e3STobin C. HardingEach dentry has a pointer to its parent dentry, as well as a hash list
1434af96c1e3STobin C. Hardingof child dentries.  Child dentries are basically like files in a
1435af96c1e3STobin C. Hardingdirectory.
1436af96c1e3STobin C. Harding
1437af96c1e3STobin C. Harding
1438ee5dc049STobin C. HardingDirectory Entry Cache API
1439ee5dc049STobin C. Harding--------------------------
1440af96c1e3STobin C. Harding
1441af96c1e3STobin C. HardingThere are a number of functions defined which permit a filesystem to
1442ee5dc049STobin C. Hardingmanipulate dentries:
1443ee5dc049STobin C. Harding
1444af96c1e3STobin C. Harding``dget``
1445af96c1e3STobin C. Harding	open a new handle for an existing dentry (this just increments
1446ee5dc049STobin C. Harding	the usage count)
1447ee5dc049STobin C. Harding
1448ee5dc049STobin C. Harding``dput``
1449af96c1e3STobin C. Harding	close a handle for a dentry (decrements the usage count).  If
1450ee5dc049STobin C. Harding	the usage count drops to 0, and the dentry is still in its
1451ee5dc049STobin C. Harding	parent's hash, the "d_delete" method is called to check whether
1452ee5dc049STobin C. Harding	it should be cached.  If it should not be cached, or if the
1453ee5dc049STobin C. Harding	dentry is not hashed, it is deleted.  Otherwise cached dentries
1454af96c1e3STobin C. Harding	are put into an LRU list to be reclaimed on memory shortage.
1455ee5dc049STobin C. Harding
1456ee5dc049STobin C. Harding``d_drop``
1457ee5dc049STobin C. Harding	this unhashes a dentry from its parents hash list.  A subsequent
1458ee5dc049STobin C. Harding	call to dput() will deallocate the dentry if its usage count
1459ee5dc049STobin C. Harding	drops to 0
1460af96c1e3STobin C. Harding
1461ee5dc049STobin C. Harding``d_delete``
1462ee5dc049STobin C. Harding	delete a dentry.  If there are no other open references to the
1463af96c1e3STobin C. Harding	dentry then the dentry is turned into a negative dentry (the
1464af96c1e3STobin C. Harding	d_iput() method is called).  If there are other references, then
1465ee5dc049STobin C. Harding	d_drop() is called instead
1466ee5dc049STobin C. Harding
1467ee5dc049STobin C. Harding``d_add``
1468ee5dc049STobin C. Harding	add a dentry to its parents hash list and then calls
1469ee5dc049STobin C. Harding	d_instantiate()
1470ee5dc049STobin C. Harding
1471ee5dc049STobin C. Harding``d_instantiate``
1472af96c1e3STobin C. Harding	add a dentry to the alias hash list for the inode and updates
1473ee5dc049STobin C. Harding	the "d_inode" member.  The "i_count" member in the inode
1474ee5dc049STobin C. Harding	structure should be set/incremented.  If the inode pointer is
1475ee5dc049STobin C. Harding	NULL, the dentry is called a "negative dentry".  This function
1476ee5dc049STobin C. Harding	is commonly called when an inode is created for an existing
1477ee5dc049STobin C. Harding	negative dentry
1478ee5dc049STobin C. Harding
1479af96c1e3STobin C. Harding``d_lookup``
1480af96c1e3STobin C. Harding	look up a dentry given its parent and path name component It
1481af96c1e3STobin C. Harding	looks up the child of that given name from the dcache hash
1482af96c1e3STobin C. Harding	table.  If it is found, the reference count is incremented and
1483af96c1e3STobin C. Harding	the dentry is returned.  The caller must use dput() to free the
1484af96c1e3STobin C. Harding	dentry when it finishes using it.
1485af96c1e3STobin C. Harding
1486af96c1e3STobin C. Harding
1487af96c1e3STobin C. HardingMount Options
1488af96c1e3STobin C. Harding=============
1489af96c1e3STobin C. Harding
1490af96c1e3STobin C. Harding
1491af96c1e3STobin C. HardingParsing options
1492af96c1e3STobin C. Harding---------------
1493af96c1e3STobin C. Harding
1494af96c1e3STobin C. HardingOn mount and remount the filesystem is passed a string containing a
1495af96c1e3STobin C. Hardingcomma separated list of mount options.  The options can have either of
1496af96c1e3STobin C. Hardingthese forms:
1497af96c1e3STobin C. Harding
1498af96c1e3STobin C. Harding  option
1499af96c1e3STobin C. Harding  option=value
1500af96c1e3STobin C. Harding
1501af96c1e3STobin C. HardingThe <linux/parser.h> header defines an API that helps parse these
1502af96c1e3STobin C. Hardingoptions.  There are plenty of examples on how to use it in existing
1503af96c1e3STobin C. Hardingfilesystems.
1504af96c1e3STobin C. Harding
1505af96c1e3STobin C. Harding
1506af96c1e3STobin C. HardingShowing options
1507af96c1e3STobin C. Harding---------------
1508af96c1e3STobin C. Harding
1509af96c1e3STobin C. HardingIf a filesystem accepts mount options, it must define show_options() to
1510af96c1e3STobin C. Hardingshow all the currently active options.  The rules are:
1511af96c1e3STobin C. Harding
1512af96c1e3STobin C. Harding  - options MUST be shown which are not default or their values differ
1513af96c1e3STobin C. Harding    from the default
1514af96c1e3STobin C. Harding
1515af96c1e3STobin C. Harding  - options MAY be shown which are enabled by default or have their
1516af96c1e3STobin C. Harding    default value
1517af96c1e3STobin C. Harding
1518af96c1e3STobin C. HardingOptions used only internally between a mount helper and the kernel (such
1519af96c1e3STobin C. Hardingas file descriptors), or which only have an effect during the mounting
1520af96c1e3STobin C. Harding(such as ones controlling the creation of a journal) are exempt from the
1521af96c1e3STobin C. Hardingabove rules.
1522af96c1e3STobin C. Harding
1523af96c1e3STobin C. HardingThe underlying reason for the above rules is to make sure, that a mount
1524af96c1e3STobin C. Hardingcan be accurately replicated (e.g. umounting and mounting again) based
1525af96c1e3STobin C. Hardingon the information found in /proc/mounts.
1526af96c1e3STobin C. Harding
1527af96c1e3STobin C. Harding
1528af96c1e3STobin C. HardingResources
1529c69f22f2SAlexander A. Klimov=========
1530af96c1e3STobin C. Harding
1531af96c1e3STobin C. Harding(Note some of these resources are not up-to-date with the latest kernel
1532af96c1e3STobin C. Harding version.)
1533af96c1e3STobin C. Harding
1534af96c1e3STobin C. HardingCreating Linux virtual filesystems. 2002
1535c69f22f2SAlexander A. Klimov    <https://lwn.net/Articles/13325/>
1536af96c1e3STobin C. Harding
1537af96c1e3STobin C. HardingThe Linux Virtual File-system Layer by Neil Brown. 1999
1538c69f22f2SAlexander A. Klimov    <http://www.cse.unsw.edu.au/~neilb/oss/linux-commentary/vfs.html>
1539
1540A tour of the Linux VFS by Michael K. Johnson. 1996
1541    <https://www.tldp.org/LDP/khg/HyperNews/get/fs/vfstour.html>
1542
1543A small trail through the Linux kernel by Andries Brouwer. 2001
1544    <https://www.win.tue.nl/~aeb/linux/vfs/trail.html>
1545