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 四大核心對象實現詳解:super_block、inode、dentry、file

編輯:Linux 教學

前言

VFS(Virtual File System Switch)以面向對象設計統一管理 ext4、xfs、btrfs、procfs、sysfs 等數十種文件系統。核心抽象為四大對象:super_block、inode、dentry、file。本文深入內核源碼(include/linux/fs.h),逐一拆解數據結構、關鍵字段、操作函數表與實例化流程。

一、super_block:掛載文件系統的控制塊

1. 數據結構關鍵字段

struct super_block {
    struct list_head s_list;      // 全局超級塊雙向鏈表
    struct list_head s_inodes;    // 本文件系統所有 inode 鏈表
    spinlock_t s_lock;            // 保護上述鏈表
    struct super_operations *s_op; // 操作函數表(核心)
    unsigned long s_flags;        // 掛載標誌(MS_RDONLY 等)
    unsigned long s_magic;        // 魔數(識別文件系統類型)
    struct dentry *s_root;        // 掛載點根目錄 dentry
    void *s_fs_info;              // 具體文件系統私有數據(如 ext4_sb_info)
    int s_dirt;                   // 髒標誌,需回寫磁盤
    // ... 時間戳、配額、設備號等
};

2. 核心操作函數表 super_operations

struct super_operations {
    struct inode *(*alloc_inode)(struct super_block *sb);
    void (*destroy_inode)(struct inode *);
    void (*dirty_inode)(struct inode *);
    int (*write_inode)(struct inode *, struct writeback_control *);
    void (*evict_inode)(struct inode *);
    void (*put_super)(struct super_block *);
    void (*write_super)(struct super_block *);
    int (*sync_fs)(struct super_block *, int wait);
    int (*statfs)(struct dentry *, struct kstatfs *);
    int (*remount_fs)(struct super_block *, int *, char *);
    // ... 配額、凍結/解凍、顯示選項等
};

調用範例:sb->s_op->write_super(sb);(C 語言模擬虛函數,需傳 this 指針 sb)。

3. 生命週期

  • 掛載時:alloc_super() 分配 → 讀取磁盤超級塊填充 → 掛入全局 super_blocks 鏈表。
  • 協寫:sync_fs() 定期回寫髒 inode/超級塊。
  • 卸載時:put_super() 釋放資源 → 從全局鏈表移除 → destroy_super() 釋放結構體。

二、inode:文件元數據與操作入口

1. 關鍵字段

2. 三大狀態鏈表(由 i_list 串聯)

  1. inode_unused:未使用 inode(緩存)
  2. inode_in_use:正在使用
  3. sb->s_dirty:髒 inode(需回寫磁盤)

同時掛載在哈希表(i_hash)加速查找。

3. 核心操作函數表 inode_operations

struct inode_operations {
    int (*create)(struct inode *, struct dentry *, umode_t, bool);
    struct dentry *(*lookup)(struct inode *, struct dentry *, struct nameidata *);
    int (*link)(struct dentry *, struct inode *, struct dentry *);
    int (*unlink)(struct inode *, struct dentry *);
    int (*mkdir)(struct inode *, struct dentry *, umode_t);
    int (*rename)(struct inode *, struct dentry *, struct inode *, struct dentry *);
    int (*setattr)(struct dentry *, struct iattr *);
    int (*getattr)(struct vfsmount *, struct dentry *, struct kstat *);
    // ... symlink, readlink, mknod, xattr, fiemap 等
};

三、dentry:路徑快取與目錄項

1. 數據結構

struct dentry {
    struct qstr d_name;           // 目錄項名稱
    struct inode *d_inode;        // 指向關聯 inode(NULL 為負歸存)
    struct dentry *d_parent;      // 父目錄項
    struct list_head d_lru;       // LRU 回收鏈表
    struct list_head d_child;     // 子目錄項鏈表
    struct list_head d_subdirs;   // 子目錄鏈表頭
    struct hlist_node d_hash;     // 哈希表節點(快速查找)
    struct dentry_operations *d_op; // 操作函數表
    // ... 鎖、引用計數、標誌等
};

2. 狀態

  • 正向:d_inode != NULL,路徑存在。
  • 負向:d_inode == NULL,路徑不存在(加速失敗查找,如 open("notexist"))。

3. 核心操作 dentry_operations

struct dentry_operations {
    int (*d_revalidate)(struct dentry *, unsigned int); // 重驗證(NFS 等網絡 FS 必須)
    int (*d_hash)(struct dentry *, struct qstr *);      // 自定義哈希
    int (*d_compare)(struct dentry *, struct qstr *, struct qstr *);
    int (*d_delete)(struct dentry *);                   // 刪除回調
    void (*d_release)(struct dentry *);                 // 釋放回調
    // ... d_iput, d_dname 等
};

四、file:打開文件實例

1. 數據結構

struct file {
    struct path f_path;           // {dentry, vfsmount}
    struct inode *f_inode;        // 便捷指向 inode
    const struct file_operations *f_op; // 文件操作表
    atomic_long_t f_count;        // 引用計數
    unsigned int f_flags;         // O_RDONLY, O_APPEND, O_NONBLOCK 等
    fmode_t f_mode;               // FMODE_READ/WRITE
    loff_t f_pos;                 // 文件偏移量(核心共享單位)
    struct fown_struct f_owner;   // 異步 I/O 擁有者
    // ... 鎖、私有數據等
};

2. 文件操作表 file_operations(節選)

struct file_operations {
    loff_t (*llseek)(struct file *, loff_t, int);
    ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
    ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
    int (*open)(struct inode *, struct file *);
    int (*release)(struct inode *, struct file *);
    int (*mmap)(struct file *, struct vm_area_struct *);
    long (*unlocked_ioctl)(struct file *, unsigned int, unsigned long);
    // ... poll, fsync, splice, fallocate, io_uring 等
};

五、四大對象關聯圖譜

super_block (s_root)
       │
       ▼
dentry (d_inode) ◄── d_parent/d_child 形成目錄樹
       │
       ▼
inode (i_sb → super_block, i_op, i_fop)
       │
       ▼
file (f_inode, f_path.dentry, f_pos)  ← 進程 fd_table 指向

關鍵點:
- 一個 inode 可被多個 dentry 引用(硬鏈接)。
- 一個 inode 可被多個 file 引用(fork/dup 共享偏移量)。
- dentry 緩存(dcache)是路徑查找性能核心。

總結

VFS 四大對象以「操作函數表」實現多態,super_block 管掛載、inode 管元數據與目錄操作、dentry 管路徑快取、file 管打開實例與偏移量。理解四大對象字段、鏈表、操作表與生命週期,是文件系統開發、內核調試、存儲性能分析的基石。

Copyright © Windows教程網 All Rights Reserved
字段含義用戶可見命令
i_inoinode 號ls -i
i_mode文件類型+權限ls -l
i_uid/i_gid擁有者/組ls -n
i_size文件大小ls -l
i_atime/i_mtime/i_ctime訪問/修改內容/修改屬性時間ls -lu/-l/-lc
i_nlink硬鏈接數ls -l 第2欄
i_blocks佔用塊數ls -s
i_sb指向超級塊內核內部
i_opinode 操作表創建/刪除/重命名/鏈接等
i_fop文件操作表open/read/write/mmap 等