Skip to content
systemcoding.dev
Kernels

How the Linux VFS Works: Superblocks, Inodes, Dentries and Files

The Linux VFS gives every filesystem four shared objects, superblock, inode, dentry and file, plus operation tables that make ext4, tmpfs and FUSE look alike.

6 min read
Summarize in AI
Flat illustration of user space above the system call boundary, the kernel and the hardware, with the Linux, C and GNU logos.

A single open("/mnt/usb/photo.jpg", O_RDONLY) can land on ext4, FAT, NFS or a FUSE daemon, and the program never knows which. The layer that makes this possible is the virtual filesystem switch, usually called the VFS. It defines four core objects and a table of function pointers for each, and every filesystem in the kernel fills those tables in. This post walks through the four objects, how a system call travels through them, and the structures you will meet if you read fs/ or write a filesystem of your own. It follows Linux 6.12, and the names come from include/linux/fs.h and the kernel's own VFS document.

The four objects

The kernel's VFS overview, Overview of the Linux Virtual File System, describes the model in terms of four in-memory structures:

ObjectStructWhat it representsLifetime
Superblockstruct super_blockOne mounted filesystem instanceFrom mount to unmount
Inodestruct inodeOne file's metadata and data locationWhile cached or referenced
Dentrystruct dentryOne name in a directory treeCached, can be evicted
Filestruct fileOne open file descriptionFrom open to the last close

The first three describe things that exist on a filesystem. The fourth, struct file, describes a process's relationship to one of them, and that is why two processes can have different offsets into the same inode.

The superblock: one per mount

When a filesystem is mounted, its driver reads the on-disk superblock (or builds a synthetic one) and creates a struct super_block. It records the block size, the filesystem's magic number, the root dentry, and a pointer to struct super_operations. That table holds functions such as alloc_inode, write_inode, sync_fs and statfs. The call statfs("/mnt/usb") ends up in the statfs member for whichever filesystem owns that mount, which is how df can print numbers for any of them.

You can see the registered filesystem types, which are the factories for superblocks, in /proc/filesystems:

Shell
cat /proc/filesystems
Text
nodev	sysfs
nodev	tmpfs
nodev	proc
nodev	devtmpfs
nodev	overlay
	ext4
	vfat
nodev	fuse

The nodev marker means the type does not need a block device behind it. The list varies by kernel configuration, so yours will differ.

The inode: metadata and operations

struct inode carries the fields you saw in stat output: mode, owner, size, timestamps and link count, plus a pointer to struct inode_operations and one to struct file_operations. Directory-shaped operations live in inode_operations:

  • lookup finds a name inside a directory and returns the dentry's inode

  • create, mkdir, unlink, rmdir and rename change the namespace

  • setattr and getattr back chmod, chown and stat

Data-shaped operations live in file_operations: read_iter, write_iter, mmap, fsync, iterate_shared for directory listing, and so on. An ext4 regular file and a FUSE regular file differ only in which tables their inodes point at.

The dentry: names and the path cache

A dentry (directory entry) ties a name to an inode and to a parent dentry. Walking /mnt/usb/photo.jpg produces a chain of dentries for /, mnt, usb and photo.jpg. Dentries exist only in memory, they are built on demand by calling the filesystem's lookup, and they are cached in the dentry cache so the next walk of the same path skips the filesystem entirely. A dentry can also be negative: it records that a name does not exist, so repeated failed lookups are cheap too. Because the same inode can be reached through several names, one inode can have several dentries, which is exactly the hard link case.

The file: an open file description

struct file is created by open(2). It holds the current offset f_pos, the flags the file was opened with, a reference to the dentry and inode, and f_op, the file_operations table copied from the inode. The integer file descriptor in user space is an index into the process's descriptor table, and each entry points to one struct file. Calling dup(2) or fork(2) makes two descriptors share one struct file, so they share an offset; opening the same path twice gives two struct file objects and two offsets over one inode.

Following one system call through the layers

Take a read of a file that is already open:

  1. The read(2) system call looks up the descriptor and gets a struct file.

  2. vfs_read() checks the open mode and permission, then calls file->f_op->read_iter.

  3. For ext4 that function is ext4_file_read_iter, which goes to the page cache and, on a miss, asks the block layer for the data.

  4. The result returns through the same chain, and the VFS advances f_pos.

An open of a path does more. do_sys_open calls the path walker, which resolves each component through the dentry cache and falls back to inode->i_op->lookup on a miss. The last dentry's inode supplies f_op, and the VFS calls the filesystem's open method if one is set. The kernel's path walking document, Pathname lookup, covers the lock-free RCU mode that makes most of this fast.

A filesystem is a set of tables

To see how little a filesystem needs to provide, here is the registration skeleton of a hypothetical examplefs. It is a sketch of the shape, not a complete driver; the next posts in this cluster build real ones.

examplefs_skeleton.cc
static const struct super_operations examplefs_sops = {
    .statfs     = simple_statfs,
    .drop_inode = generic_delete_inode,
};

static const struct inode_operations examplefs_dir_iops = {
    .lookup = simple_lookup,
};

static const struct file_operations examplefs_dir_fops = {
    .iterate_shared = dcache_readdir,
    .read           = generic_read_dir,
    .llseek         = generic_file_llseek,
};

Each assigned member is a pointer the VFS will call. The simple_* and generic_* helpers come from fs/libfs.c and the VFS core and exist so that small filesystems can fill the tables without writing every function. Members left unset mean the operation is unsupported, and the VFS returns an error such as EPERM or ENOTDIR instead of crashing.

Where the layers show up in practice

Knowing the four objects explains several behaviors you meet as an operator:

  • df -i and stat read the superblock and inode.

  • lsof lists struct file objects, which is why one inode can appear many times.

  • echo 2 > /proc/sys/vm/drop_caches drops cached dentries and inodes, while open files keep their own references.

  • The Slab line in /proc/meminfo includes dentry and inode caches, so a directory scan of millions of paths can grow it.

  • mount --bind creates a second mount of an existing dentry tree, which is possible because mounts are separate from dentries.

Boundaries of the model

The VFS is a unifying interface, not a promise that all filesystems behave alike. Semantics differ in places the tables cannot hide: case sensitivity, maximum name length, timestamp resolution, whether hard links exist, and what fsync guarantees. FAT has no permissions and vfat fakes them from mount options. Network filesystems can return ESTALE for an inode the server dropped. Treat the VFS as the common vocabulary and read each filesystem's documentation for its dialect.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.