Abstract
- Virtual File System is an abstraction layer in the Kernel that provides uniform interface to interact with files, regardless of actual file system or storage medium(SSD and network etc)
Uniform interface
VFS exposes a consistent set of file operations like
open()
,read()
,write()
andclose()
to user space, hiding the differences between file system.Mounting support: VFS lets you mount a FAT32 USB stick onto a system running ext4. Internally, VFS routes the system calls to the correct file system driver.
File Access Mechanism
- User Space: Application calls
open()
- System Call Interface: Transitions to kernel mode
- VFS Layer (get inode number):
- Parses and validates the path
- Traverses directories to locate the file (translate the pathname to an inode number)
- Filesystem Specific Operations (get inode structure):
- Relevant filesystem driver handles the request (each filesystem stores data differently)
- Finds inode for the file
- Inode Cache Entry Creation (if inode not found):
- Reads inode from disk and adds to kernel’s inode cache (vnode table)
- Initialises with appropriate function pointers
- File Table Entry Creation:
- Allocates new entry in open file table
- Sets initial file position (
0
orEOF
forO_APPEND
since need to to add from the back of the file) - Sets reference count to
1
- File Descriptor Allocation:
- Finds lowest unused FD in process file descriptor table
- Points it to the new file table entry
- Returns file descriptor to user space
Function pointers
Point to filesystem-specific implementations of various operations.
When a filesystem is mounted, it registers these functions with the VFS. As files are accessed, their inode entries are populated with pointers to the appropriate functions.
Adding kernel-level support for new storage mediums is challenging!
Implementing custom filesystem drivers requires writing low-level C code to define specialised data structures and function pointers within the kernel’s VFS layer.
That is why we have FUSE which shifts implementation to user space, allowing developers to create filesystem interfaces using high-level languages and familiar libraries rather than modifying kernel code.