Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Linux VFS 虛擬檔案系統機制深度解析:從註冊到掛載全流程

編輯:Linux 教學

Linux VFS(Virtual Filesystem Switch)是檔案系統的核心抽象層,僅存在於記憶體中。本文基於 2.4.20 核心原始碼,追蹤檔案系統註冊、VFS 目錄樹建立、根檔案系統掛載、目錄建立(sys_mkdir)全流程,配合關鍵資料結構(super_block、dentry、inode、vfsmount)關係圖解。

三大模組架構

  1. 系統呼叫層:open/read/write/mkdir 等使用者介面。
  2. VFS 層:統一抽象,管理 super_block、dentry、inode、vfsmount 等記憶體物件。
  3. 實體檔案系統:ext2、jffs、proc、rootfs 等,掛載至 VFS 目錄樹提供實際操作。

檔案系統註冊:file_systems 鏈表

struct file_system_type {
    const char *name;                    // "ext2", "proc", "rootfs"
    int fs_flags;                        // FS_REQUIRES_DEV 等
    struct super_block *(*read_super)(struct super_block *, void *, int);
    struct module *owner;
    struct file_system_type *next;       // 單向鏈表
    struct list_head fs_supers;          // 該類型所有 super_block
};

extern struct file_system_type *file_systems;  // 全域鏈表頭

register_filesystem() 將結構體實例掛入 file_systems 鏈表。

rootfs:VFS 存在的基石

rootfs 註冊在核心初始化早期(init_rootfs()),不經由 module_init:

struct file_system_type rootfs_fs_type = {
    .name = "rootfs",
    .read_super = ramfs_read_super,      // 實際操作導向 ramfs
    .fs_flags = FS_NOMOUNT | FS_LITTER,
};
register_filesystem(&rootfs_fs_type);
  • FS_NOMOUNT:不可透過 mount(2) 手動掛載。
  • ramfs_read_super:所有操作針對記憶體,完美契合 VFS 純記憶體特性。

VFS 目錄樹建立:init_mount_tree()

  1. do_kern_mount("rootfs") → 分配 vfsmount、super_block、inode、dentry 四大核心物件。
  2. ramfs_read_super() → ramfs_get_inode() 分配根目錄 inode,設定 i_op = &ramfs_dir_inode_operations、i_fop = &dcache_dir_ops。
  3. d_alloc_root() 建立根 dentry(名稱 "/"),關聯 d_sb、d_inode。
  4. vfsmount 關聯:mnt_sb → sb、mnt_root → dentry、mnt_mountpoint → dentry、mnt_parent → self。
  5. 將 mnt、dentry 寫入 init_task 的 namespace、fs->pwd、fs->root,所有子進程繼承。

sys_mkdir("/dev") 實戰追蹤

  1. sys_mkdir("/dev", 0700) → path_lookup("/dev", LOOKUP_PARENT, &nd)。
  2. path_lookup 從 current->fs->root 起始,走訪路徑,回傳 nd:
    nd.dentry = 父目錄 "/" 的 dentry,nd.mnt = 根掛載點,nd.last.name = "dev"。
  3. lookup_create(&nd, 1) 分配新 dentry(掛入父目錄 d_subdirs 鏈表)與新 inode(i_sb 指向同一 super_block)。
  4. 呼叫 rootfs ramfs_mkdir()、ramfs_lookup() 完成實體操作。
  5. 新目錄掛入 VFS 樹,dentry、inode、super_block、vfsmount 關係延伸。

關鍵資料結構關係圖

vfsmount (mnt)
  ├── mnt_sb ──────────→ super_block (sb)
  │                        ├── s_type → file_system_type ("rootfs")
  │                        ├── s_root → dentry ("/")
  │                        └── s_instances 鏈接同類型 sb
  ├── mnt_root ───────→ dentry ("/")
  │                        ├── d_sb → sb
  │                        ├── d_inode → inode (根目錄)
  │                        └── d_subdirs → 子目錄 dentry 鏈表
  ├── mnt_mountpoint → dentry ("/")
  └── mnt_parent ─────→ self (根掛載點)

inode (根目錄)
  ├── i_sb → sb
  ├── i_op → ramfs_dir_inode_operations
  └── i_fop → dcache_dir_ops

核心洞見

  • VFS 目錄樹純記憶體結構,**目錄主要提供掛載點**,實際 I/O 委派給掛載檔案系統的 inode_operations/file_operations。
  • 同一檔案系統下建立目錄**共用同一 super_block**,只新增 dentry + inode。
  • namespace 機制讓每個進程擁有獨立掛載視圖,容器技術基石。

掌握 VFS 核心物件生命週期與關聯,是深入檔案系統開發、除錯掛載失敗、理解容器儲存隔離的前提。

Copyright © Windows教程網 All Rights Reserved