Writing an OS Kernel from Scratch | VFS — The Virtual File System: How Linux’s “Everything is a File” Works

You use cat /proc/cpuinfo, ls /dev/null, echo "hello" > /tmp/a.txt — three different operations, all using the same open/read/write system calls. Why?

The answer is VFS (Virtual File System Switch) — an abstraction layer that makes everything look like a file. This implements Unix’s classic philosophy: Everything is a File.


1. Why Do We Need VFS

Without VFS, applications need different APIs for different backends (ext4, sockets, devices). With VFS, they just call open/read/write.

2. Core Data Structures

  • inode: represents the file itself (metadata, operations, content)
  • dentry: connects file names to inodes, caches lookups

3. File Operations

Each filesystem fills in file_operations function pointers. VFS calls them transparently.

4. Path Resolution

Bash
open("/home/user/file.txt") → VFS resolves dentry chain → calls filesystem's open

Summary

VFS enables the same API for disks, /proc, /dev, pipes, sockets. This is “Everything is a file.”

Last modified: 2024年10月18日

Author

Comments

Write a Reply or Comment

Your email address will not be published.