How the Linux VFS Works: Inodes, Dentries, and Why Paths Are Not Files
An open("/var/log/app.log", O_RDONLY) does not hand the kernel a file. It hands the kernel a string. The Virtual File System (VFS) is the layer that turns that string into an object the rest of the kernel can use: an inode, a page-cache mapping, and a per-process file description. Ext4, XFS, tmpfs, proc, and NFS all look different on disk or on the wire. After mount they answer the same questions through the same four objects.
This article covers those objects, how a path walk uses the dentry cache, and why a name is not the same thing as a file. See also How the Linux Page Cache Works, How mmap Works, How sendfile and splice Work, and How System Calls Work.
The four objects
VFS is not a filesystem. It is a table of operations plus four in-memory types:
- super_block — one mounted instance. Block size, root dentry, type, and
super_operationssuch asalloc_inode,write_inode,sync_fs,statfs. - inode — one filesystem object: regular file, directory, symlink, device, fifo, socket. Mode, owner, size, link count, timestamps, and
i_mappingfor the page cache. Identified by (super_block, inode number), not by a path. - dentry — one name in one parent. Parent pointer, component name (
qstr), and an inode pointer that may be null (negative dentry). Dentries exist only in RAM. They are never written to disk. - file — one open description in one process: dentry, inode, cursor, flags, and
file_operations(read,write,mmap,poll,release).
Hard links are extra dentries on the same inode. Two processes that open the same path get two file objects and usually two file descriptors; they share one inode and one address space.
Why names and files split
Users speak paths. Storage and network filesystems speak numbers. A rename changes a dentry, not the inode number. An unlink drops a link count; the inode and its pages can survive until the last file is released. That is why a deleted log that is still open keeps growing, and why lsof reports (deleted).
The split also lets one inode have several names across directories on the same filesystem, and lets bind mounts and mount namespaces present different trees over the same objects.
Path walk
open, stat, unlink, and execve all start the same way: split the string on /, start from the process root or cwd (or from the root dentry if the path is absolute), and resolve each component.
For each component the VFS hashes (parent dentry, name) into the dentry hash table. A hit returns a dentry that already points at an inode, or a negative dentry that records "this name does not exist." A miss calls the parent inode's lookup() method. Ext4 then reads a directory block. NFS sends LOOKUP. proc synthesizes a result. The VFS stores whatever comes back as a dentry so the next walk does not repeat that work.
Walks also handle:
.and.., including..at a mount point, which must leave the mounted tree rather than the filesystem root that was mounted there.- Symlinks, with a recursion limit so a loop cannot livelock the kernel.
- Mount crossings: a dentry can be a mount point. The walk switches to the mounted superblock's root dentry.
- Permission checks on each directory that is traversed, not only on the final inode.
- RCU-walk versus ref-walk. The fast path follows dentries under RCU without taking per-dentry locks. A rename, mount change, or sequence-count mismatch drops to the slower ref-walk that holds references.
The file object is the open handle
Once the final dentry and inode are known, open allocates a struct file, stores flags and the current offset, and installs a descriptor in the process fd table. Later read and write syscalls never see the path again. They index the fd table, take the file, and call f_op->read_iter / write_iter.
That is why dup2 and inherited descriptors after fork keep working after a rename, and why passing a directory fd with openat is more precise than passing a string that another thread can replace.
Where the page cache attaches
File data is not stored on the inode. The inode holds i_mapping, an address_space, whose pages are the page cache. VFS read finds or creates those pages; the filesystem fills them from storage if they miss. Writeback walks dirty pages on that mapping, not names. A path that never opened the file never pins those pages. See How the Linux Page Cache Works.
Pseudo filesystems still use VFS
proc, sysfs, and tmpfs register a file_system_type and implement the same operation tables. Their inodes are created in memory. Their dentries still cache names so that /proc/self/status is not rebuilt from scratch on every open. The contract is the operations, not a block device.
A compact mental model of open
path = "/var/log/app.log"
start = process root dentry
for each component in path:
d = dcache_lookup(parent, name)
if miss:
d = parent->inode->i_op->lookup(parent, name)
if d is mount point:
d = mounted_root(d)
if d is symlink:
restart walk with target
parent = d
inode = final_dentry->d_inode
file = alloc_file(final_dentry, flags)
fd = install(file)
Real link_path_walk is more careful about RCU, LOOKUP_FOLLOW, trailing slashes, and O_NOFOLLOW, but the objects do not change.
Where it bites
- Stale paths. A string is a claim about the tree at one instant. Between
statandopenanother process can replace the name.openatwith a directory fd shrinks that window. - Negative dentries. A failed lookup is cached. Creating the file must invalidate that dentry or later opens keep failing until the cache drops it.
- Unlinked but open. Space is not freed until
i_nlinkis zero and nofileremains. Disk-full surprises often start here. - Cross-device rename. Rename cannot move an inode onto another superblock. User space copies and unlinks instead.
- Dcache memory. A workload that touches millions of unique paths fills the dentry and inode caches. Reclaim is possible; the first lookup after reclaim pays for I/O again.
Misconceptions
- The path is the file. The path is a walk. The file is the inode plus an open
fileobject. - Inodes live on every filesystem's disk the same way. On-disk layout is private. VFS inodes are the in-memory translation.
- Dentries are directory entries on disk. On-disk directories are filesystem-specific. Dentries are a RAM cache of resolved names.
- Closing the last fd deletes the name. Close releases a
file. Unlink removes a name. Only both, withi_nlink == 0, lets the inode go. - The page cache is VFS. The page cache sits under the inode's address space. VFS is the name and object layer above it.
Takeaways
VFS gives every filesystem the same four objects: a mounted superblock, inodes that are the files, dentries that are the names, and file objects that are the open handles. A path walk is a dcache search that falls back to lookup(). Data lives in the page cache attached to the inode, not in the path string. Once you treat names, objects, and open handles as separate, delete-while-open, hard links, mount crossings, and openat stop looking like special cases.