The virtual file system (VFS) in Linux is an interface layer used by the operating system to abstract different file systems. Allow users to access and manipulate file systems in a uniform manner, rather than concern themselves with underlying implementation differences. VFS allows linux to support multiple file systems such as ext4, NTFS, FAT, XFS, NFS, etc. It provides a unified interface for managing files and directories.
The primary purpose of a virtual file system is to provide an abstraction layer so that different file systems can work together within the same operating system. Acts as an intermediary between the kernel and the file system. Provides consensual access to hard disks, network file systems, and other types of storage media.
For example, an abstract file system abstracts different file types into a consistent interface, so that the programmer does not need to care about the underlying details of file storage. Unified interfaces enable users and applications to interact with file systems through unified interfaces, regardless of differences in underlying file systems. The VFS supports linux systems to attach to and access many different types of file systems. For example, an ext4 partition, an NTFS partition, or even a remote file system that is attached via NFS on the same linux server.
The VFS is a set of core data structures and interfaces that provide consensual access to the file system. The key data structures that VFS work on are:
struct file: represents an open file. Each time a file is opened, a struct file instance is created in the kernel, containing the file's descriptor, pointer, read/write location, and so on.
struct inode: Represents a file or directory in a file system (inode is the basic unit of a file system). It stores metadata about the file, such as permissions, size, creation time, etc., but not the content of the file itself.
struct dentry: represents a directory entry. It contains directory information in the file system and is used to locate files or directories.
struct super_block: represents a superblock of a file system. It stores metadata about the file system, such as the file system type, block size, and root directory.
The VFS workflow is roughly the process of opening a file and performing operations on it such as reading, writing, deleting, renaming, and so on. According to the type of the operating system and the type of the file system, the VFS calls the corresponding file system driver functions to mount the file system. When the system starts, the VFS loads the metadata of the file system through super_block and makes it part of the system. Each file system is associated with a mount point through which the VFS manages access to different file systems.
The VFS provides file opening, closing, reading, writing, file permission and attribute management, file path analysis, file system mounting and unmounting, file system caching, etc.
The VFS is an important part of the linux kernel. It uses data structures to provide an abstraction layer and supports multiple file systems. A good understanding of the VFS helps to better understand the linux file system architecture, especially for development and storage, file access related applications.