Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

Linux 虛擬文件系統剖析:四大核心物件與快取機制

閱讀 APUE 的文件系統章節時,最直觀的做法是從行程描述符出發,看內核如何把「一個行程看到的檔案世界」串起來。

從行程到檔案:三個關鍵結構

除了 files_struct,還有兩個常被提到的結構:

struct fs_struct {
    int users;  spinlock_t lock;  seqcount_t seq;
    int umask;  int in_exec;
    struct path root, pwd;      /* 根目錄與當前工作目錄 */
};

struct nsproxy {
    atomic_t count;
    struct uts_namespace *uts_ns;
    struct ipc_namespace *ipc_ns;
    struct mnt_namespace *mnt_ns;
    struct pid_namespace *pid_ns_for_children;
    struct net *net_ns;
};

前者定義於 include/linux/fs_struct.h,保存 pwd 與 root;後者位於 include/linux/nsproxy.h,屬於命名空間主題,決定了行程看到的掛載層次結構。兩者都與 read/write 這類基本操作關係不大,於是回到 files_struct:

struct files_struct {
    atomic_t count;
    struct fdtable __rcu *fdt;
    struct fdtable fdtab;
    spinlock_t file_lock ____cacheline_aligned_in_smp;
    int next_fd;
    unsigned long close_on_exec_init[1];
    unsigned long open_fds_init[1];
    struct file __rcu *fd_array[NR_OPEN_DEFAULT];
};

struct fdtable {
    unsigned int max_fds;
    struct file __rcu **fd;
    unsigned long *close_on_exec;
    unsigned long *open_fds;
    struct rcu_head rcu;
};

fd_array 指向已開啟的 file 物件;當行程開啟的檔案數超過 NR_OPEN_DEFAULT,內核會配置更大的陣列並讓 fdt 指向它。

VFS 的四個物件

虛擬文件系統(VFS)以四種物件描述一切:超級塊物件代表一個已掛載的檔案系統、索引節點物件代表一個檔案、目錄項物件代表路徑中的一個組成部分、檔案物件代表被行程開啟的檔案。

Linux 虛擬文件系統剖析:四大核心物件與快取機制

檔案物件 struct file

struct file {
    struct path f_path;
    struct inode *f_inode;              /* cached value */
    const struct file_operations *f_op;
    loff_t f_pos;
    /* ... */
} __attribute__((aligned(4)));

它是「已開啟檔案」在記憶體中的表示,由 open 建立、close 撤銷。同一個檔案被多個行程開啟會有多個 file 物件,但對應的 inode 與 dentry 唯一。三個值得注意的欄位:

struct path { struct vfsmount *mnt; struct dentry *dentry; };

f_path 把 file 與 dentry 掛上關係;f_inode 是索引節點的快取,讓某些路徑不必繞過 dentry 就能直接存取 inode(這一欄位在 2.6 之後才出現,早期書籍的版本沒有);f_op 則是這個檔案的所有操作集合。

file_operations 裡的 read、write、open、release、poll、mmap、fsync 等函式指標,正是系統呼叫最終落地的位置——注意系統呼叫層的 sys_read 會轉而呼叫這裡的 read。這種「資料 + 操作函式」打包的設計,讓 VFS 成為內核中最具物件導向風味的一塊。

目錄項物件 struct dentry

VFS 把目錄當檔案看待,因此路徑中的每一段都是 dentry。解析路徑需要逐段字串比對,既耗時又繁瑣,dentry 的引入就是為了把這件事變快。

struct dentry {
    unsigned int d_flags;
    struct hlist_bl_node d_hash;
    struct dentry *d_parent;
    struct qstr d_name;
    struct inode *d_inode;      /* NULL 表示負狀態 */
    struct lockref d_lockref;
    const struct dentry_operations *d_op;
    struct super_block *d_sb;
    struct list_head d_subdirs;
    /* ... */
};

dentry 沒有對應的磁碟結構,它是 VFS 依路徑字串現場產生的,因此也沒有「髒」標記。它有三種狀態:

  • 被使用:d_inode 有效且 d_count > 0,內容不可丟棄。
  • 未被使用:仍指向有效 inode,但目前無人使用,留在快取中以便日後快速命中;記憶體吃緊時可回收。
  • 負狀態:d_inode 為 NULL(inode 已刪除或路徑失效),保留它是為了讓後續同名查詢能快速得到「不存在」的結論。

目錄項高速緩存(dcache)由兩部分組成:一個包含各狀態 dentry 的集合,以及一個可用檔名/目錄名快速定位的雜湊表。使用中的 dentry 串在對應 inode 的 i_dentry 鏈表上(一個 inode 可能有多個硬連結,所以需要鏈表),以 d_alias 串接;未使用與負狀態者則進入 LRU 鏈表,頭新尾舊,回收時由尾端開始。

索引節點物件 struct inode

inode 物件包含內核操作檔案或目錄所需的一切資訊。Unix 風格檔案系統可直接從磁碟 inode 讀入;沒有 inode 的檔案系統(把描述資訊當檔案內容存放)則必須在記憶體中「湊」出一個 inode 來。

struct inode {
    umode_t i_mode;  kuid_t i_uid;  kgid_t i_gid;
    const struct inode_operations *i_op;
    struct super_block *i_sb;
    unsigned long i_ino;
    loff_t i_size;
    unsigned long i_state;
    /* ... */
};

i_state 記錄狀態位元,例如 I_DIRTY(髒,需回寫磁碟)、I_NEW、I_SYNC、I_FREEING;i_op 提供 lookup、create、link、unlink、mkdir、rename、setattr 等操作;i_sb 指回所屬超級塊。

超級塊物件 struct super_block

每種檔案系統都必須實作超級塊,用來存放該檔案系統的整體資訊,通常對應磁碟特定扇區上的檔案系統控制塊;而 sysfs 這類非磁碟檔案系統則在現場建立並只存在於記憶體中。s_list、s_dev、s_root、s_inodes、s_op(super_operations)等欄位,分別負責鏈結、識別、根目錄、inode 清單與操作集合。

四物件總結

  • 超級塊物件:已掛載檔案系統的整體資訊,磁碟系統對應檔案系統控制塊。
  • 索引節點物件:單一檔案的一般資訊,inode 號唯一識別檔案,磁碟系統對應檔案控制塊。
  • 檔案物件:已開啟檔案與行程互動的資訊,只存在於內核記憶體,在真實檔案系統中沒有映像。
  • 目錄項物件:名稱與檔案的關聯資訊,同樣沒有磁碟映像。

除了 dcache,還有索引點高速緩存,兩者都屬於「磁碟高速緩存」——把原本在磁碟上的資訊保留在 RAM 中,讓後續存取不必慢速讀盤。與它相對的概念是硬體高速緩存與記憶體高速緩存,可以另外再深入。

Copyright © Linux教程網 All Rights Reserved