1 /* -*- auto-fill -*- */
3 Overview of the Virtual File System
5 Richard Gooch <rgooch@atnf.csiro.au>
10 Conventions used in this document <section>
11 =================================
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.
18 NOTE that the master copy of this document is available online at:
19 http://www.atnf.csiro.au/~rgooch/linux/docs/vfs.txt
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 programmes. It also provides an
28 abstraction within the kernel which allows different filesystem
29 implementations to co-exist.
32 A Quick Look At How It Works <section>
33 ============================
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 programmes open and manipulate files, and then look from the
38 other view which is how a filesystem is supported and subsequently
41 Opening a File <subsection>
44 The VFS implements the open(2) system call. The pathname argument is
45 used by the VFS to search through the directory entry cache (dentry
46 cache or "dcache"). This provides a very fast lookup mechanism to
47 translate a pathname (filename) into a specific dentry.
49 An individual dentry usually has a pointer to an inode. Inodes are the
50 things that live on disc drives, and can be regular files (you know:
51 those things that you write data into), directories, FIFOs and other
52 beasts. Dentries live in RAM and are never saved to disc: they exist
53 only for performance. Inodes live on disc and are copied into memory
54 when required. Later any changes are written back to disc. The inode
55 that lives in RAM is a VFS inode, and it is this which the dentry
58 The dcache is meant to be a view into your entire filespace. Unlike
59 Linus, most of us losers can't fit enough dentries into RAM to cover
60 all of our filespace, so the dcache has bits missing. In order to
61 resolve your pathname into a dentry, the VFS may have to resort to
62 creating dentries along the way, and then loading the inode. This is
63 done by looking up the inode.
65 To lookup an inode (usually read from disc) requires that the VFS
66 calls the lookup() method of the parent directory inode. This method
67 is installed by the specific filesystem implementation that the inode
68 lives in. There will be more on this later.
70 Once the VFS has the required dentry (and hence the inode), we can do
71 all those boring things like open(2) the file, or stat(2) it to peek
72 at the inode data. The stat(2) operation is fairly simple: once the
73 VFS has the dentry, it peeks at the inode data and passes some of it
76 Opening a file requires another operation: allocation of a file
77 structure (this is the kernel-side implementation of file
78 descriptors). The freshly allocated file structure is initialised with
79 a pointer to the dentry and a set of file operation member
80 functions. These are taken from the inode data. The open() file method
81 is then called so the specific filesystem implementation can do it's
82 work. You can see that this is another switch performed by the VFS.
84 The file structure is placed into the file descriptor table for the
87 Reading, writing and closing files (and other assorted VFS operations)
88 is done by using the userspace file descriptor to grab the appropriate
89 file structure, and then calling the required file structure method
90 function to do whatever is required.
92 For as long as the file is open, it keeps the dentry "open" (in use),
93 which in turn means that the VFS inode is still in use.
95 Registering and Mounting a Filesystem <subsection>
96 -------------------------------------
98 If you want to support a new kind of filesystem in the kernel, all you
99 need to do is call register_filesystem(). You pass a structure
100 describing the filesystem implementation (struct file_system_type)
101 which is then added to an internal table of supported filesystems. You
104 % cat /proc/filesystems
106 to see what filesystems are currently available on your system.
108 When a request is made to mount a block device onto a directory in
109 your filespace the VFS will call the appropriate method for the
110 specific filesystem. The dentry for the mount point will then be
111 updated to point to the root inode for the new filesystem.
113 It's now time to look at things in more detail.
116 struct file_system_type <section>
117 =======================
119 This describes the filesystem. As of kernel 2.1.99, the following
122 struct file_system_type {
125 struct super_block *(*read_super) (struct super_block *, void *, int);
126 struct file_system_type * next;
129 name: the name of the filesystem type, such as "ext2", "iso9660",
132 fs_flags: various flags (i.e. FS_REQUIRES_DEV, FS_NO_DCACHE, etc.)
134 read_super: the method to call when a new instance of this
135 filesystem should be mounted
137 next: for internal VFS use: you should initialise this to NULL
139 The read_super() method has the following arguments:
141 struct super_block *sb: the superblock structure. This is partially
142 initialised by the VFS and the rest must be initialised by the
145 void *data: arbitrary mount options, usually comes as an ASCII
148 int silent: whether or not to be silent on error
150 The read_super() method must determine if the block device specified
151 in the superblock contains a filesystem of the type the method
152 supports. On success the method returns the superblock pointer, on
153 failure it returns NULL.
155 The most interesting member of the superblock structure that the
156 read_super() method fills in is the "s_op" field. This is a pointer to
157 a "struct super_operations" which describes the next level of the
158 filesystem implementation.
161 struct super_operations <section>
162 =======================
164 This describes how the VFS can manipulate the superblock of your
165 filesystem. As of kernel 2.1.99, the following members are defined:
167 struct super_operations {
168 void (*read_inode) (struct inode *);
169 void (*write_inode) (struct inode *);
170 void (*put_inode) (struct inode *);
171 void (*delete_inode) (struct inode *);
172 int (*notify_change) (struct dentry *, struct iattr *);
173 void (*put_super) (struct super_block *);
174 void (*write_super) (struct super_block *);
175 int (*statfs) (struct super_block *, struct statfs *, int);
176 int (*remount_fs) (struct super_block *, int *, char *);
177 void (*clear_inode) (struct inode *);
180 All methods are called without any locks being held, unless otherwise
181 noted. This means that most methods can block safely. All methods are
182 only called from a process context (i.e. not from an interrupt handler
185 read_inode: this method is called to read a specific inode from the
186 mounted filesystem. The "i_ino" member in the "struct inode"
187 will be initialised by the VFS to indicate which inode to
188 read. Other members are filled in by this method
190 write_inode: this method is called when the VFS needs to write an
193 put_inode: called when the VFS inode is removed from the inode
194 cache. This method is optional
196 delete_inode: called when the VFS wants to delete an inode
198 notify_change: called when VFS inode attributes are changed. If this
199 is NULL the VFS falls back to the write_inode() method. This
200 is called with the kernel lock held
202 put_super: called when the VFS wishes to free the superblock
203 (i.e. unmount). This is called with the superblock lock held
205 write_super: called when the VFS superblock needs to be written to
206 disc. This method is optional
208 statfs: called when the VFS needs to get filesystem statistics. This
209 is called with the kernel lock held
211 remount_fs: called when the filesystem is remounted. This is called
212 with the kernel lock held
214 clear_inode: called then the VFS clears the inode. Optional
216 The read_inode() method is responsible for filling in the "i_op"
217 field. This is a pointer to a "struct inode_operations" which
218 describes the methods that can be performed on individual inodes.
221 struct inode_operations <section>
222 =======================
224 This describes how the VFS can manipulate an inode in your
225 filesystem. As of kernel 2.1.99, the following members are defined:
227 struct inode_operations {
228 struct file_operations * default_file_ops;
229 int (*create) (struct inode *,struct dentry *,int);
230 int (*lookup) (struct inode *,struct dentry *);
231 int (*link) (struct dentry *,struct inode *,struct dentry *);
232 int (*unlink) (struct inode *,struct dentry *);
233 int (*symlink) (struct inode *,struct dentry *,const char *);
234 int (*mkdir) (struct inode *,struct dentry *,int);
235 int (*rmdir) (struct inode *,struct dentry *);
236 int (*mknod) (struct inode *,struct dentry *,int,int);
237 int (*rename) (struct inode *, struct dentry *,
238 struct inode *, struct dentry *);
239 int (*readlink) (struct dentry *, char *,int);
240 struct dentry * (*follow_link) (struct dentry *, struct dentry *);
241 int (*readpage) (struct file *, struct page *);
242 int (*writepage) (struct file *, struct page *);
243 int (*bmap) (struct inode *,int);
244 void (*truncate) (struct inode *);
245 int (*permission) (struct inode *, int);
246 int (*smap) (struct inode *,int);
247 int (*updatepage) (struct file *, struct page *, const char *,
248 unsigned long, unsigned int, int);
249 int (*revalidate) (struct dentry *);
252 default_file_ops: this is a pointer to a "struct file_operations"
253 which describes how to manipulate open files
255 create: called by the open(2) and creat(2) system calls. Only
256 required if you want to support regular files. The dentry you
257 get should not have an inode (i.e. it should be a negative
258 dentry). Here you will probably call d_instantiate() with the
259 dentry and the newly created inode
261 lookup: called when the VFS needs to lookup an inode in a parent
262 directory. The name to look for is found in the dentry. This
263 method must call d_add() to insert the found inode into the
264 dentry. The "i_count" field in the inode structure should be
265 incremented. If the named inode does not exist a NULL inode
266 should be inserted into the dentry (this is called a negative
267 dentry). Returning an error code from this routine must only
268 be done on a real error, otherwise creating inodes with system
269 calls like create(2), mknod(2), mkdir(2) and so on will fail.
270 If you wish to overload the dentry methods then you should
271 initialise the "d_dop" field in the dentry; this is a pointer
272 to a struct "dentry_operations".
273 This method is called with the directory semaphore held
275 link: called by the link(2) system call. Only required if you want
276 to support hard links. You will probably need to call
277 d_instantiate() just as you would in the create() method
279 unlink: called by the unlink(2) system call. Only required if you
280 want to support deleting inodes
282 symlink: called by the symlink(2) system call. Only required if you
283 want to support symlinks. You will probably need to call
284 d_instantiate() just as you would in the create() method
286 mkdir: called by the mkdir(2) system call. Only required if you want
287 to support creating subdirectories. You will probably need to
288 call d_instantiate() just as you would in the create() method
290 rmdir: called by the rmdir(2) system call. Only required if you want
291 to support deleting subdirectories
293 mknod: called by the mknod(2) system call to create a device (char,
294 block) inode or a named pipe (FIFO) or socket. Only required
295 if you want to support creating these types of inodes. You
296 will probably need to call d_instantiate() just as you would
297 in the create() method
299 readlink: called by the readlink(2) system call. Only required if
300 you want to support reading symbolic links
302 follow_link: called by the VFS to follow a symbolic link to the
303 inode it points to. Only required if you want to support
307 struct file_operations <section>
308 ======================
310 This describes how the VFS can manipulate an open file. As of kernel
311 2.1.99, the following members are defined:
313 struct file_operations {
314 loff_t (*llseek) (struct file *, loff_t, int);
315 ssize_t (*read) (struct file *, char *, size_t, loff_t *);
316 ssize_t (*write) (struct file *, const char *, size_t, loff_t *);
317 int (*readdir) (struct file *, void *, filldir_t);
318 unsigned int (*poll) (struct file *, struct poll_table_struct *);
319 int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
320 int (*mmap) (struct file *, struct vm_area_struct *);
321 int (*open) (struct inode *, struct file *);
322 int (*release) (struct inode *, struct file *);
323 int (*fsync) (struct file *, struct dentry *);
324 int (*fasync) (struct file *, int);
325 int (*check_media_change) (kdev_t dev);
326 int (*revalidate) (kdev_t dev);
327 int (*lock) (struct file *, int, struct file_lock *);
330 llseek: called when the VFS needs to move the file position index
332 read: called by the read(2) system call
334 write: called by the write(2) system call
336 readdir: called when the VFS needs to read the directory contents
338 poll: called by the VFS when a process wants to check if there is
339 activity on this file and (optionally) go to sleep until there
342 ioctl: called by the ioctl(2) system call
344 mmap: called by the mmap(2) system call
346 open: called by the VFS when an inode should be opened. When the VFS
347 opens a file, it creates a new "struct file" and initialises
348 the "f_op" file operations member with the "default_file_ops"
349 field in the inode structure. It then calls the open method
350 for the newly allocated file structure. You might think that
351 the open method really belongs in "struct inode_operations",
352 and you may be right. I think it's done the way it is because
353 it makes filesystems simpler to implement. The open() method
354 is a good place to initialise the "private_data" member in the
355 file structure if you want to point to a device structure
357 release: called when the last reference to an open file is closed
359 fsync: called by the fsync(2) system call
361 fasync: called by the fcntl(2) system call when asynchronous
362 (non-blocking) mode is enabled for a file
364 Note that the file operations are implemented by the specific
365 filesystem in which the inode resides. When opening a device node
366 (character or block special) most filesystems will call special
367 support routines in the VFS which will locate the required device
368 driver information. These support routines replace the filesystem file
369 operations with those for the device driver, and then proceed to call
370 the new open() method for the file. This is how opening a device file
371 in the filesystem eventually ends up calling the device driver open()
372 method. Note the devfs (the Device FileSystem) has a more direct path
373 from device node to device driver (this is an unofficial kernel
377 struct dentry_operations <section>
378 ========================
380 This describes how a filesystem can overload the standard dentry
381 operations. Dentries and the dcache are the domain of the VFS and the
382 individual filesystem implementations. Device drivers have no business
383 here. As of kernel 2.1.99, the following members are defined:
385 struct dentry_operations {
386 int (*d_revalidate)(struct dentry *);
387 int (*d_hash) (struct dentry *, struct qstr *);
388 int (*d_compare) (struct dentry *, struct qstr *, struct qstr *);
389 void (*d_delete)(struct dentry *);
390 void (*d_release)(struct dentry *);
391 void (*d_iput)(struct dentry *, struct inode *);
394 d_revalidate: called when the VFS needs to revalidate a dentry
396 d_hash: called when the VFS adds a dentry to the hash table
398 d_compare: called when a dentry should be compared with another
400 d_delete: called when the last reference to a dentry is
401 deleted. This means no-one is using the dentry, however it is
402 still valid and in the dcache
404 d_release: called when a dentry is deallocated
406 d_iput: called when a dentry looses its inode (just prior to its
407 being deallocated). The default when this is NULL is that the
408 VFS calls iput(). If you define this method, you must call
411 Each dentry has a pointer to its parent dentry, as well as a hash list
412 of child dentries. Child dentries are basically like files in a
415 There are a number of functions defined which permit a filesystem to
418 dget: open a new handle for an existing dentry (this just increments
421 dput: close a handle for a dentry (decrements the usage count). If
422 the usage count drops to 0, the "d_delete" method is called
423 and the dentry is placed on the unused list if the dentry is
424 still in its parents hash list. Putting the dentry on the
425 unused list just means that if the system needs some RAM, it
426 goes through the unused list of dentries and deallocates them.
427 If the dentry has already been unhashed and the usage count
428 drops to 0, in this case the dentry is deallocated after the
429 "d_delete" method is called
431 d_drop: this unhashes a dentry from its parents hash list. A
432 subsequent call to dput() will dellocate the dentry if its
433 usage count drops to 0
435 d_delete: delete a dentry. If there are no other open references to
436 the dentry then the dentry is turned into a negative dentry
437 (the d_iput() method is called). If there are other
438 references, then d_drop() is called instead
440 d_add: add a dentry to its parents hash list and then calls
443 d_instantiate: add a dentry to the alias hash list for the inode and
444 updates the "d_inode" member. The "i_count" member in the
445 inode structure should be set/incremented. If the inode
446 pointer is NULL, the dentry is called a "negative
447 dentry". This function is commonly called when an inode is
448 created for an existing negative dentry