import of upstream 2.4.34.4 from kernel.org
[linux-2.4.git] / Documentation / filesystems / vfs.txt
1 /* -*- auto-fill -*-                                                         */
2
3                 Overview of the Virtual File System
4
5                 Richard Gooch <rgooch@atnf.csiro.au>
6
7                               5-JUL-1999
8
9
10 Conventions used in this document                                     <section>
11 =================================
12
13 Each section in this document will have the string "<section>" at the
14 right-hand side of the section title. Each subsection will have
15 "<subsection>" at the right-hand side. These strings are meant to make
16 it easier to search through the document.
17
18 NOTE that the master copy of this document is available online at:
19 http://www.atnf.csiro.au/~rgooch/linux/docs/vfs.txt
20
21
22 What is it?                                                           <section>
23 ===========
24
25 The Virtual File System (otherwise known as the Virtual Filesystem
26 Switch) is the software layer in the kernel that provides the
27 filesystem interface to userspace programs. It also provides an
28 abstraction within the kernel which allows different filesystem
29 implementations to co-exist.
30
31
32 A Quick Look At How It Works                                          <section>
33 ============================
34
35 In this section I'll briefly describe how things work, before
36 launching into the details. I'll start with describing what happens
37 when user programs open and manipulate files, and then look from the
38 other view which is how a filesystem is supported and subsequently
39 mounted.
40
41 Opening a File                                                     <subsection>
42 --------------
43
44 The VFS implements the open(2), stat(2), chmod(2) and similar system
45 calls. The pathname argument is used by the VFS to search through the
46 directory entry cache (dentry cache or "dcache"). This provides a very
47 fast lookup mechanism to translate a pathname (filename) into a
48 specific dentry.
49
50 An individual dentry usually has a pointer to an inode. Inodes are the
51 things that live on disc drives, and can be regular files (you know:
52 those things that you write data into), directories, FIFOs and other
53 beasts. Dentries live in RAM and are never saved to disc: they exist
54 only for performance. Inodes live on disc and are copied into memory
55 when required. Later any changes are written back to disc. The inode
56 that lives in RAM is a VFS inode, and it is this which the dentry
57 points to. A single inode can be pointed to by multiple dentries
58 (think about hardlinks).
59
60 The dcache is meant to be a view into your entire filespace. Unlike
61 Linus, most of us losers can't fit enough dentries into RAM to cover
62 all of our filespace, so the dcache has bits missing. In order to
63 resolve your pathname into a dentry, the VFS may have to resort to
64 creating dentries along the way, and then loading the inode. This is
65 done by looking up the inode.
66
67 To lookup an inode (usually read from disc) requires that the VFS
68 calls the lookup() method of the parent directory inode. This method
69 is installed by the specific filesystem implementation that the inode
70 lives in. There will be more on this later.
71
72 Once the VFS has the required dentry (and hence the inode), we can do
73 all those boring things like open(2) the file, or stat(2) it to peek
74 at the inode data. The stat(2) operation is fairly simple: once the
75 VFS has the dentry, it peeks at the inode data and passes some of it
76 back to userspace.
77
78 Opening a file requires another operation: allocation of a file
79 structure (this is the kernel-side implementation of file
80 descriptors). The freshly allocated file structure is initialised with
81 a pointer to the dentry and a set of file operation member functions.
82 These are taken from the inode data. The open() file method is then
83 called so the specific filesystem implementation can do it's work. You
84 can see that this is another switch performed by the VFS.
85
86 The file structure is placed into the file descriptor table for the
87 process.
88
89 Reading, writing and closing files (and other assorted VFS operations)
90 is done by using the userspace file descriptor to grab the appropriate
91 file structure, and then calling the required file structure method
92 function to do whatever is required.
93
94 For as long as the file is open, it keeps the dentry "open" (in use),
95 which in turn means that the VFS inode is still in use.
96
97 All VFS system calls (i.e. open(2), stat(2), read(2), write(2),
98 chmod(2) and so on) are called from a process context. You should
99 assume that these calls are made without any kernel locks being
100 held. This means that the processes may be executing the same piece of
101 filesystem or driver code at the same time, on different
102 processors. You should ensure that access to shared resources is
103 protected by appropriate locks.
104
105 Registering and Mounting a Filesystem                              <subsection>
106 -------------------------------------
107
108 If you want to support a new kind of filesystem in the kernel, all you
109 need to do is call register_filesystem(). You pass a structure
110 describing the filesystem implementation (struct file_system_type)
111 which is then added to an internal table of supported filesystems. You
112 can do:
113
114 % cat /proc/filesystems
115
116 to see what filesystems are currently available on your system.
117
118 When a request is made to mount a block device onto a directory in
119 your filespace the VFS will call the appropriate method for the
120 specific filesystem. The dentry for the mount point will then be
121 updated to point to the root inode for the new filesystem.
122
123 It's now time to look at things in more detail.
124
125
126 struct file_system_type                                               <section>
127 =======================
128
129 This describes the filesystem. As of kernel 2.1.99, the following
130 members are defined:
131
132 struct file_system_type {
133         const char *name;
134         int fs_flags;
135         struct super_block *(*read_super) (struct super_block *, void *, int);
136         struct file_system_type * next;
137 };
138
139   name: the name of the filesystem type, such as "ext2", "iso9660",
140         "msdos" and so on
141
142   fs_flags: various flags (i.e. FS_REQUIRES_DEV, FS_NO_DCACHE, etc.)
143
144   read_super: the method to call when a new instance of this
145         filesystem should be mounted
146
147   next: for internal VFS use: you should initialise this to NULL
148
149 The read_super() method has the following arguments:
150
151   struct super_block *sb: the superblock structure. This is partially
152         initialised by the VFS and the rest must be initialised by the
153         read_super() method
154
155   void *data: arbitrary mount options, usually comes as an ASCII
156         string
157
158   int silent: whether or not to be silent on error
159
160 The read_super() method must determine if the block device specified
161 in the superblock contains a filesystem of the type the method
162 supports. On success the method returns the superblock pointer, on
163 failure it returns NULL.
164
165 The most interesting member of the superblock structure that the
166 read_super() method fills in is the "s_op" field. This is a pointer to
167 a "struct super_operations" which describes the next level of the
168 filesystem implementation.
169
170
171 struct super_operations                                               <section>
172 =======================
173
174 This describes how the VFS can manipulate the superblock of your
175 filesystem. As of kernel 2.1.99, the following members are defined:
176
177 struct super_operations {
178         void (*read_inode) (struct inode *);
179         void (*write_inode) (struct inode *, int);
180         void (*put_inode) (struct inode *);
181         void (*delete_inode) (struct inode *);
182         int (*notify_change) (struct dentry *, struct iattr *);
183         void (*put_super) (struct super_block *);
184         void (*write_super) (struct super_block *);
185         int (*statfs) (struct super_block *, struct statfs *, int);
186         int (*remount_fs) (struct super_block *, int *, char *);
187         void (*clear_inode) (struct inode *);
188 };
189
190 All methods are called without any locks being held, unless otherwise
191 noted. This means that most methods can block safely. All methods are
192 only called from a process context (i.e. not from an interrupt handler
193 or bottom half).
194
195   read_inode: this method is called to read a specific inode from the
196         mounted filesystem. The "i_ino" member in the "struct inode"
197         will be initialised by the VFS to indicate which inode to
198         read. Other members are filled in by this method
199
200   write_inode: this method is called when the VFS needs to write an
201         inode to disc.  The second parameter indicates whether the write
202         should be synchronous or not, not all filesystems check this flag.
203
204   put_inode: called when the VFS inode is removed from the inode
205         cache. This method is optional
206
207   delete_inode: called when the VFS wants to delete an inode
208
209   notify_change: called when VFS inode attributes are changed. If this
210         is NULL the VFS falls back to the write_inode() method. This
211         is called with the kernel lock held
212
213   put_super: called when the VFS wishes to free the superblock
214         (i.e. unmount). This is called with the superblock lock held
215
216   write_super: called when the VFS superblock needs to be written to
217         disc. This method is optional
218
219   statfs: called when the VFS needs to get filesystem statistics. This
220         is called with the kernel lock held
221
222   remount_fs: called when the filesystem is remounted. This is called
223         with the kernel lock held
224
225   clear_inode: called then the VFS clears the inode. Optional
226
227 The read_inode() method is responsible for filling in the "i_op"
228 field. This is a pointer to a "struct inode_operations" which
229 describes the methods that can be performed on individual inodes.
230
231
232 struct inode_operations                                               <section>
233 =======================
234
235 This describes how the VFS can manipulate an inode in your
236 filesystem. As of kernel 2.1.99, the following members are defined:
237
238 struct inode_operations {
239         struct file_operations * default_file_ops;
240         int (*create) (struct inode *,struct dentry *,int);
241         int (*lookup) (struct inode *,struct dentry *);
242         int (*link) (struct dentry *,struct inode *,struct dentry *);
243         int (*unlink) (struct inode *,struct dentry *);
244         int (*symlink) (struct inode *,struct dentry *,const char *);
245         int (*mkdir) (struct inode *,struct dentry *,int);
246         int (*rmdir) (struct inode *,struct dentry *);
247         int (*mknod) (struct inode *,struct dentry *,int,int);
248         int (*rename) (struct inode *, struct dentry *,
249                         struct inode *, struct dentry *);
250         int (*readlink) (struct dentry *, char *,int);
251         struct dentry * (*follow_link) (struct dentry *, struct dentry *);
252         int (*readpage) (struct file *, struct page *);
253         int (*writepage) (struct file *, struct page *);
254         int (*bmap) (struct inode *,int);
255         void (*truncate) (struct inode *);
256         int (*permission) (struct inode *, int);
257         int (*smap) (struct inode *,int);
258         int (*updatepage) (struct file *, struct page *, const char *,
259                                 unsigned long, unsigned int, int);
260         int (*revalidate) (struct dentry *);
261 };
262
263 Again, all methods are called without any locks being held, unless
264 otherwise noted.
265
266   default_file_ops: this is a pointer to a "struct file_operations"
267         which describes how to open and then manipulate open files
268
269   create: called by the open(2) and creat(2) system calls. Only
270         required if you want to support regular files. The dentry you
271         get should not have an inode (i.e. it should be a negative
272         dentry). Here you will probably call d_instantiate() with the
273         dentry and the newly created inode
274
275   lookup: called when the VFS needs to lookup an inode in a parent
276         directory. The name to look for is found in the dentry. This
277         method must call d_add() to insert the found inode into the
278         dentry. The "i_count" field in the inode structure should be
279         incremented. If the named inode does not exist a NULL inode
280         should be inserted into the dentry (this is called a negative
281         dentry). Returning an error code from this routine must only
282         be done on a real error, otherwise creating inodes with system
283         calls like create(2), mknod(2), mkdir(2) and so on will fail.
284         If you wish to overload the dentry methods then you should
285         initialise the "d_dop" field in the dentry; this is a pointer
286         to a struct "dentry_operations".
287         This method is called with the directory inode semaphore held
288
289   link: called by the link(2) system call. Only required if you want
290         to support hard links. You will probably need to call
291         d_instantiate() just as you would in the create() method
292
293   unlink: called by the unlink(2) system call. Only required if you
294         want to support deleting inodes
295
296   symlink: called by the symlink(2) system call. Only required if you
297         want to support symlinks. You will probably need to call
298         d_instantiate() just as you would in the create() method
299
300   mkdir: called by the mkdir(2) system call. Only required if you want
301         to support creating subdirectories. You will probably need to
302         call d_instantiate() just as you would in the create() method
303
304   rmdir: called by the rmdir(2) system call. Only required if you want
305         to support deleting subdirectories
306
307   mknod: called by the mknod(2) system call to create a device (char,
308         block) inode or a named pipe (FIFO) or socket. Only required
309         if you want to support creating these types of inodes. You
310         will probably need to call d_instantiate() just as you would
311         in the create() method
312
313   readlink: called by the readlink(2) system call. Only required if
314         you want to support reading symbolic links
315
316   follow_link: called by the VFS to follow a symbolic link to the
317         inode it points to. Only required if you want to support
318         symbolic links
319
320
321 struct file_operations                                                <section>
322 ======================
323
324 This describes how the VFS can manipulate an open file. As of kernel
325 2.1.99, the following members are defined:
326
327 struct file_operations {
328         loff_t (*llseek) (struct file *, loff_t, int);
329         ssize_t (*read) (struct file *, char *, size_t, loff_t *);
330         ssize_t (*write) (struct file *, const char *, size_t, loff_t *);
331         int (*readdir) (struct file *, void *, filldir_t);
332         unsigned int (*poll) (struct file *, struct poll_table_struct *);
333         int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
334         int (*mmap) (struct file *, struct vm_area_struct *);
335         int (*open) (struct inode *, struct file *);
336         int (*release) (struct inode *, struct file *);
337         int (*fsync) (struct file *, struct dentry *);
338         int (*fasync) (struct file *, int);
339         int (*check_media_change) (kdev_t dev);
340         int (*revalidate) (kdev_t dev);
341         int (*lock) (struct file *, int, struct file_lock *);
342 };
343
344 Again, all methods are called without any locks being held, unless
345 otherwise noted.
346
347   llseek: called when the VFS needs to move the file position index
348
349   read: called by read(2) and related system calls
350
351   write: called by write(2) and related system calls
352
353   readdir: called when the VFS needs to read the directory contents
354
355   poll: called by the VFS when a process wants to check if there is
356         activity on this file and (optionally) go to sleep until there
357         is activity. Called by the select(2) and poll(2) system calls
358
359   ioctl: called by the ioctl(2) system call
360
361   mmap: called by the mmap(2) system call
362
363   open: called by the VFS when an inode should be opened. When the VFS
364         opens a file, it creates a new "struct file" and initialises
365         the "f_op" file operations member with the "default_file_ops"
366         field in the inode structure. It then calls the open method
367         for the newly allocated file structure. You might think that
368         the open method really belongs in "struct inode_operations",
369         and you may be right. I think it's done the way it is because
370         it makes filesystems simpler to implement. The open() method
371         is a good place to initialise the "private_data" member in the
372         file structure if you want to point to a device structure
373
374   release: called when the last reference to an open file is closed
375
376   fsync: called by the fsync(2) system call
377
378   fasync: called by the fcntl(2) system call when asynchronous
379         (non-blocking) mode is enabled for a file
380
381 Note that the file operations are implemented by the specific
382 filesystem in which the inode resides. When opening a device node
383 (character or block special) most filesystems will call special
384 support routines in the VFS which will locate the required device
385 driver information. These support routines replace the filesystem file
386 operations with those for the device driver, and then proceed to call
387 the new open() method for the file. This is how opening a device file
388 in the filesystem eventually ends up calling the device driver open()
389 method. Note the devfs (the Device FileSystem) has a more direct path
390 from device node to device driver (this is an unofficial kernel
391 patch).
392
393
394 struct dentry_operations                                              <section>
395 ========================
396
397 This describes how a filesystem can overload the standard dentry
398 operations. Dentries and the dcache are the domain of the VFS and the
399 individual filesystem implementations. Device drivers have no business
400 here. These methods may be set to NULL, as they are either optional or
401 the VFS uses a default. As of kernel 2.1.99, the following members are
402 defined:
403
404 struct dentry_operations {
405         int (*d_revalidate)(struct dentry *);
406         int (*d_hash) (struct dentry *, struct qstr *);
407         int (*d_compare) (struct dentry *, struct qstr *, struct qstr *);
408         void (*d_delete)(struct dentry *);
409         void (*d_release)(struct dentry *);
410         void (*d_iput)(struct dentry *, struct inode *);
411 };
412
413   d_revalidate: called when the VFS needs to revalidate a dentry. This
414         is called whenever a name lookup finds a dentry in the
415         dcache. Most filesystems leave this as NULL, because all their
416         dentries in the dcache are valid
417
418   d_hash: called when the VFS adds a dentry to the hash table
419
420   d_compare: called when a dentry should be compared with another
421
422   d_delete: called when the last reference to a dentry is
423         deleted. This means no-one is using the dentry, however it is
424         still valid and in the dcache
425
426   d_release: called when a dentry is really deallocated
427
428   d_iput: called when a dentry looses its inode (just prior to its
429         being deallocated). The default when this is NULL is that the
430         VFS calls iput(). If you define this method, you must call
431         iput() yourself
432
433 Each dentry has a pointer to its parent dentry, as well as a hash list
434 of child dentries. Child dentries are basically like files in a
435 directory.
436
437 There are a number of functions defined which permit a filesystem to
438 manipulate dentries:
439
440   dget: open a new handle for an existing dentry (this just increments
441         the usage count)
442
443   dput: close a handle for a dentry (decrements the usage count). If
444         the usage count drops to 0, the "d_delete" method is called
445         and the dentry is placed on the unused list if the dentry is
446         still in its parents hash list. Putting the dentry on the
447         unused list just means that if the system needs some RAM, it
448         goes through the unused list of dentries and deallocates them.
449         If the dentry has already been unhashed and the usage count
450         drops to 0, in this case the dentry is deallocated after the
451         "d_delete" method is called
452
453   d_drop: this unhashes a dentry from its parents hash list. A
454         subsequent call to dput() will dellocate the dentry if its
455         usage count drops to 0
456
457   d_delete: delete a dentry. If there are no other open references to
458         the dentry then the dentry is turned into a negative dentry
459         (the d_iput() method is called). If there are other
460         references, then d_drop() is called instead
461
462   d_add: add a dentry to its parents hash list and then calls
463         d_instantiate()
464
465   d_instantiate: add a dentry to the alias hash list for the inode and
466         updates the "d_inode" member. The "i_count" member in the
467         inode structure should be set/incremented. If the inode
468         pointer is NULL, the dentry is called a "negative
469         dentry". This function is commonly called when an inode is
470         created for an existing negative dentry