Writing an OS Kernel from Scratch 201: Storage System – VFS: Designing a Unified File Abstraction Layer

“In a custom OS, how do you make open/read/write work with both ext2 and procfs? This article designs a VFS abstraction layer from scratch, building four core objects, implementing path resolution and caching, providing a unified interface for subsequent filesystems.”

Why a Custom OS Needs VFS

In a custom OS, you might initially implement just one filesystem (like ext2). But as the system evolves, you’ll need:

  • procfs: Expose kernel info (/proc/cpuinfo)
  • sysfs: Manage devices (/sys/devices)
  • devfs: Device files (/dev/tty0)
  • tmpfs: In-memory filesystem

If each filesystem directly exposes ext2_open, proc_open interfaces, user programs would have to hardcode the filesystem type — terrible extensibility.

VFS solves this by providing a unified abstraction layer: the upper application just calls open(path), and VFS automatically routes to the corresponding filesystem implementation.

Four Core Objects

Object Role Lifetime
vfs_super Describes filesystem instance Created on mount, destroyed on unmount
vfs_inode Describes a single file Created on first access, LRU reclaimed
vfs_dentry Caches path-to-inode mapping Created on path resolution, LRU reclaimed
vfs_file Describes an open file Created on open, destroyed on close
Last modified: 2025年8月8日

Author

Comments

Write a Reply or Comment

Your email address will not be published.