Understanding Linux’s Virtual File System (VFS): Types, Objects, and Caching
Linux’s Virtual File System (VFS) acts as a glue layer between user space and various file systems, providing a unified interface and object model—including superblocks, inodes, file and dentry objects—and employs caching mechanisms like dcache to efficiently manage directory entries.
One of Linux’s success factors is its ability to coexist harmoniously with other operating systems, exemplified by the Virtual File System (VFS).
VFS’s idea is to place common information of different file types into the kernel by introducing an abstract layer between user processes (or the C library) and file systems, called the Virtual File System.
Types of Virtual File System
VFS types are mainly divided into three categories:
Disk‑based file systems: the classic method of storing files on non‑volatile media, preserving file contents across multiple sessions.
Special file systems: generated inside the kernel, they provide a way for user applications to communicate with the kernel and handle all system calls related to the Unix standard file system (the proc file system is a prime example).
Network file systems: a compromise between disk file systems and VFS, allowing easy access to files that reside on other networked computers (common examples include NFS, Coda, AFS, CIFS, NCP).
Generic File Model
The generic file model strictly reflects the traditional Unix file system model; Linux does not hard‑code functions such as read() or ioctl() for a specific file system. Instead, each operation uses a pointer to the appropriate function in the underlying file system. Through this model, users can call open(), read(), write() without needing to consider the concrete file system.
VFS adopts an object‑oriented design, using a set of data structures to represent generic file objects, forming Linux’s generic file model.
Key objects:
1) Superblock object: stores information about a mounted file system.
2) Inode object: holds general information about a specific file. An inode represents a file in the file system and is created in memory only when the file is accessed.
3) File object: represents a file opened by a process. This information exists only while the process accesses the file; the file object (not the physical file) is created by the open() system call and removed by close().
4) Dentry object: not a directory itself; VFS treats directories as files, and a dentry represents a component of a path linked to the corresponding file.
The relationships among VFS objects are shown below:
Interaction between processes and VFS objects is illustrated here:
Directory Entry Cache (dcache)
To maximize efficiency when handling dentry objects, Linux uses a directory entry cache composed of two data structures:
A collection of dentry objects in three states: 正在使用 , 未使用 , and 负状态 . Active and unused dentries correspond to a valid inode, while negative dentries have no valid inode.
A hash table that allows rapid lookup of the dentry object corresponding to a given filename and directory name.
Summary
VFS is a glue layer situated between the kernel’s low level and user space. It provides various abstract data structures to represent files and inodes, and real file systems must populate these structures so that applications can always use the same interface to access and manipulate files regardless of the underlying file system.
Original source: https://lihaoquan.me/2019/2/16/linux-vfs.html
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
