【Linux】文件系统(四):目录、路径解析与 dentry 缓存

上一篇已经建立了这条链:

text 复制代码
inode 号 → inode → 文件属性 + 数据块

可是用户打开文件时提供的是路径,例如:

c 复制代码
open("/home/whb/code/test/test.c", O_RDONLY);

路径中没有 inode 号,inode 中也没有保存文件名。那么 Linux 怎样从这串路径找到最终的 inode?

答案是:文件名由目录项保存,目录项负责建立"名字 → inode 号"的映射;路径解析则逐个处理路径组成部分,最后得到目标 inode。

一、目录也是文件

"目录是文件"并不是为了概念好听,而是 Linux 文件系统实际的组织方式。

目录也有:

  • 自己的 inode
  • 文件类型、权限、所有者和时间
  • 自己的数据块

不同之处在于,目录的数据块保存的不是普通文本,而是目录项。每个目录项至少承担一项任务:

text 复制代码
文件名 → inode 号

因此,查找一个文件实际上发生两次映射:

text 复制代码
目录项:文件名 → inode 号
inode:文件属性 + 数据块编号

#mermaid-svg-EHxXEzHrN05WH0xt{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-EHxXEzHrN05WH0xt .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-EHxXEzHrN05WH0xt .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-EHxXEzHrN05WH0xt .error-icon{fill:#552222;}#mermaid-svg-EHxXEzHrN05WH0xt .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-EHxXEzHrN05WH0xt .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-EHxXEzHrN05WH0xt .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-EHxXEzHrN05WH0xt .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-EHxXEzHrN05WH0xt .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-EHxXEzHrN05WH0xt .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-EHxXEzHrN05WH0xt .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-EHxXEzHrN05WH0xt .marker{fill:#333333;stroke:#333333;}#mermaid-svg-EHxXEzHrN05WH0xt .marker.cross{stroke:#333333;}#mermaid-svg-EHxXEzHrN05WH0xt svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-EHxXEzHrN05WH0xt p{margin:0;}#mermaid-svg-EHxXEzHrN05WH0xt .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-EHxXEzHrN05WH0xt .cluster-label text{fill:#333;}#mermaid-svg-EHxXEzHrN05WH0xt .cluster-label span{color:#333;}#mermaid-svg-EHxXEzHrN05WH0xt .cluster-label span p{background-color:transparent;}#mermaid-svg-EHxXEzHrN05WH0xt .label text,#mermaid-svg-EHxXEzHrN05WH0xt span{fill:#333;color:#333;}#mermaid-svg-EHxXEzHrN05WH0xt .node rect,#mermaid-svg-EHxXEzHrN05WH0xt .node circle,#mermaid-svg-EHxXEzHrN05WH0xt .node ellipse,#mermaid-svg-EHxXEzHrN05WH0xt .node polygon,#mermaid-svg-EHxXEzHrN05WH0xt .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-EHxXEzHrN05WH0xt .rough-node .label text,#mermaid-svg-EHxXEzHrN05WH0xt .node .label text,#mermaid-svg-EHxXEzHrN05WH0xt .image-shape .label,#mermaid-svg-EHxXEzHrN05WH0xt .icon-shape .label{text-anchor:middle;}#mermaid-svg-EHxXEzHrN05WH0xt .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-EHxXEzHrN05WH0xt .rough-node .label,#mermaid-svg-EHxXEzHrN05WH0xt .node .label,#mermaid-svg-EHxXEzHrN05WH0xt .image-shape .label,#mermaid-svg-EHxXEzHrN05WH0xt .icon-shape .label{text-align:center;}#mermaid-svg-EHxXEzHrN05WH0xt .node.clickable{cursor:pointer;}#mermaid-svg-EHxXEzHrN05WH0xt .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-EHxXEzHrN05WH0xt .arrowheadPath{fill:#333333;}#mermaid-svg-EHxXEzHrN05WH0xt .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-EHxXEzHrN05WH0xt .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-EHxXEzHrN05WH0xt .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-EHxXEzHrN05WH0xt .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-EHxXEzHrN05WH0xt .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-EHxXEzHrN05WH0xt .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-EHxXEzHrN05WH0xt .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-EHxXEzHrN05WH0xt .cluster text{fill:#333;}#mermaid-svg-EHxXEzHrN05WH0xt .cluster span{color:#333;}#mermaid-svg-EHxXEzHrN05WH0xt div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-EHxXEzHrN05WH0xt .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-EHxXEzHrN05WH0xt rect.text{fill:none;stroke-width:0;}#mermaid-svg-EHxXEzHrN05WH0xt .icon-shape,#mermaid-svg-EHxXEzHrN05WH0xt .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-EHxXEzHrN05WH0xt .icon-shape p,#mermaid-svg-EHxXEzHrN05WH0xt .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-EHxXEzHrN05WH0xt .icon-shape .label rect,#mermaid-svg-EHxXEzHrN05WH0xt .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-EHxXEzHrN05WH0xt .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-EHxXEzHrN05WH0xt .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-EHxXEzHrN05WH0xt :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 文件名 test.c
目录项
inode 号 1596260
inode
权限、大小、时间
数据块编号
文件内容

这也解释了一个常见现象:同一个 inode 可以有多个目录项。此时多个文件名指向同一个文件对象,这些名字互为硬链接。

二、目录项中有什么

课件通过 readdir 实验说明了目录项至少包含文件名和 inode 号。典型的 Ext2 目录项结构还会包含长度和文件类型,例如:

c 复制代码
struct ext2_dir_entry_2 {
    __le32 inode;       /* 目标对象的 inode 号 */
    __le16 rec_len;     /* 当前目录项的记录长度 */
    __u8   name_len;    /* 文件名长度 */
    __u8   file_type;   /* 文件类型 */
    char   name[];      /* 文件名 */
};

目录数据块中还会出现两个特殊名字:

  • . 表示当前目录
  • .. 表示上级目录

它们本身也是目录项。

三、课堂实验:读取目录文件名和 inode 号

给出的 readdir.c 如下:

c 复制代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/types.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
    if (argc != 2) {
        fprintf(stderr, "Usage: %s <directory>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    DIR *dir = opendir(argv[1]);
    if (!dir) {
        perror("opendir");
        exit(EXIT_FAILURE);
    }

    struct dirent *entry;
    while ((entry = readdir(dir)) != NULL) {
        if (strcmp(entry->d_name, ".") == 0 ||
            strcmp(entry->d_name, "..") == 0) {
            continue;
        }

        printf("Filename: %s, Inode: %lu\n",
               entry->d_name,
               (unsigned long)entry->d_ino);
    }

    closedir(dir);
    return 0;
}

编译并读取根目录:

bash 复制代码
gcc readdir.c -o readdir
./readdir /

课件中的输出节选如下:

text 复制代码
Filename: mnt, Inode: 1048577
Filename: tmp, Inode: 1179650
Filename: sys, Inode: 917506
Filename: srv, Inode: 786434
Filename: sbin, Inode: 18
Filename: dev, Inode: 131073
Filename: run, Inode: 1048578
Filename: proc, Inode: 1179649
Filename: lost+found, Inode: 11
Filename: etc, Inode: 262145
Filename: opt, Inode: 917505
Filename: usr, Inode: 1179651
Filename: boot, Inode: 655361
Filename: var, Inode: 655365
Filename: bin, Inode: 13
Filename: media, Inode: 393217
Filename: home, Inode: 786433
Filename: root, Inode: 655362

这段输出证明:程序遍历目录时,同时取得了文件名和 inode 号。

根目录下还可能有 proc、sys、dev 等特殊文件系统或挂载点。它们也出现在统一路径树里,所以不能仅仅利用根目录中某个 inode 号去推断它一定来自同一块普通磁盘分区。

四、从绝对路径到目标 inode

以路径 /home/whb/code/test/test.c 为例,路径解析要按组成部分逐级进行:

text 复制代码
/ → home → whb → code → test → test.c

大致过程是:

  1. 从根目录开始。
  2. 在根目录的目录项中查找 home,得到 home 的 inode。
  3. 读取 home 目录的数据块,在其中查找 whb。
  4. 得到 whb 的 inode,再继续查找 code。
  5. 依次进入 test。
  6. 最后在 test 的目录项中找到 test.c,得到目标 inode。
  7. 根据目标 inode 读取属性及数据块。

#mermaid-svg-YqLS8c2EGJDjI0qY{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-YqLS8c2EGJDjI0qY .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-YqLS8c2EGJDjI0qY .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-YqLS8c2EGJDjI0qY .error-icon{fill:#552222;}#mermaid-svg-YqLS8c2EGJDjI0qY .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-YqLS8c2EGJDjI0qY .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-YqLS8c2EGJDjI0qY .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-YqLS8c2EGJDjI0qY .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-YqLS8c2EGJDjI0qY .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-YqLS8c2EGJDjI0qY .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-YqLS8c2EGJDjI0qY .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-YqLS8c2EGJDjI0qY .marker{fill:#333333;stroke:#333333;}#mermaid-svg-YqLS8c2EGJDjI0qY .marker.cross{stroke:#333333;}#mermaid-svg-YqLS8c2EGJDjI0qY svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-YqLS8c2EGJDjI0qY p{margin:0;}#mermaid-svg-YqLS8c2EGJDjI0qY .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-YqLS8c2EGJDjI0qY .cluster-label text{fill:#333;}#mermaid-svg-YqLS8c2EGJDjI0qY .cluster-label span{color:#333;}#mermaid-svg-YqLS8c2EGJDjI0qY .cluster-label span p{background-color:transparent;}#mermaid-svg-YqLS8c2EGJDjI0qY .label text,#mermaid-svg-YqLS8c2EGJDjI0qY span{fill:#333;color:#333;}#mermaid-svg-YqLS8c2EGJDjI0qY .node rect,#mermaid-svg-YqLS8c2EGJDjI0qY .node circle,#mermaid-svg-YqLS8c2EGJDjI0qY .node ellipse,#mermaid-svg-YqLS8c2EGJDjI0qY .node polygon,#mermaid-svg-YqLS8c2EGJDjI0qY .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-YqLS8c2EGJDjI0qY .rough-node .label text,#mermaid-svg-YqLS8c2EGJDjI0qY .node .label text,#mermaid-svg-YqLS8c2EGJDjI0qY .image-shape .label,#mermaid-svg-YqLS8c2EGJDjI0qY .icon-shape .label{text-anchor:middle;}#mermaid-svg-YqLS8c2EGJDjI0qY .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-YqLS8c2EGJDjI0qY .rough-node .label,#mermaid-svg-YqLS8c2EGJDjI0qY .node .label,#mermaid-svg-YqLS8c2EGJDjI0qY .image-shape .label,#mermaid-svg-YqLS8c2EGJDjI0qY .icon-shape .label{text-align:center;}#mermaid-svg-YqLS8c2EGJDjI0qY .node.clickable{cursor:pointer;}#mermaid-svg-YqLS8c2EGJDjI0qY .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-YqLS8c2EGJDjI0qY .arrowheadPath{fill:#333333;}#mermaid-svg-YqLS8c2EGJDjI0qY .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-YqLS8c2EGJDjI0qY .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-YqLS8c2EGJDjI0qY .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-YqLS8c2EGJDjI0qY .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-YqLS8c2EGJDjI0qY .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-YqLS8c2EGJDjI0qY .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-YqLS8c2EGJDjI0qY .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-YqLS8c2EGJDjI0qY .cluster text{fill:#333;}#mermaid-svg-YqLS8c2EGJDjI0qY .cluster span{color:#333;}#mermaid-svg-YqLS8c2EGJDjI0qY div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-YqLS8c2EGJDjI0qY .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-YqLS8c2EGJDjI0qY rect.text{fill:none;stroke-width:0;}#mermaid-svg-YqLS8c2EGJDjI0qY .icon-shape,#mermaid-svg-YqLS8c2EGJDjI0qY .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-YqLS8c2EGJDjI0qY .icon-shape p,#mermaid-svg-YqLS8c2EGJDjI0qY .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-YqLS8c2EGJDjI0qY .icon-shape .label rect,#mermaid-svg-YqLS8c2EGJDjI0qY .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-YqLS8c2EGJDjI0qY .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-YqLS8c2EGJDjI0qY .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-YqLS8c2EGJDjI0qY :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 根目录
查找 home
查找 whb
查找 code
查找 test
查找 test.c
目标 inode
文件数据块

绝对路径从根目录开始。相对路径则从进程的当前工作目录开始。

例如当前工作目录是:

text 复制代码
/home/zjy/code/test

那么:

text 复制代码
test.c

等价于从 /home/zjy/code/test 开始查找 test.c。

进程的当前工作目录由进程保存 。前一篇基础 I/O 课程曾通过 /proc/<pid>/cwd 观察进程工作目录,这和这里的相对路径解析是同一个问题的两个视角。

五、根目录为什么是解析出口

如果每次访问目录都要先找到"当前目录的上级目录",看起来会产生无限递归:

text 复制代码
当前目录 → 上级目录 → 更上级目录 → ...?

路径解析需要在某个固定位置结束。

根目录 / 的文件名、inode 和起始位置有确定的系统级来源,因此解析到达根目录后,不再继续寻找"根目录的上级目录"。

用户在根目录下创建目录,目录中再创建文件,系统与用户共同形成了一棵路径树:

text 复制代码
/
├── home
│   └── whb
│       └── code
│           └── test
│               └── test.c
├── etc
├── usr
└── var

磁盘上并没有一个固定的"目录对象类型"把整棵树直接保存成树。树是由一组目录文件及其中的目录项共同表现出来的。

六、dentry 为什么不是磁盘目录项

路径解析需要反复查找。如果每一次打开文件都从根目录开始,逐个读取目录文件的磁盘块,速度会很慢。

Linux 会在内存中维护路径缓存,其中的关键对象是 dentry。

课件给出的 struct dentry 中可以看到这些关键字段:

c 复制代码
struct dentry {
    atomic_t d_count;
    unsigned int d_flags;
    spinlock_t d_lock;
    struct inode *d_inode;
    struct hlist_node d_hash;
    struct dentry *d_parent;
    struct qstr d_name;
    struct list_head d_lru;
    union {
        struct list_head d_child;
        struct rcu_head d_rcu;
    } d_u;
    struct list_head d_subdirs;
    struct list_head d_alias;
    unsigned long d_time;
    struct dentry_operations *d_op;
    struct super_block *d_sb;
    void *d_fsdata;
    int d_mounted;
};

这些字段体现了 dentry 的几种关系:

  • d_parent 指向父目录的 dentry。
  • d_name 保存路径组件名称。
  • d_inode 指向对应的内存 inode。
  • d_hash 把 dentry 加入哈希表,便于快速查找。
  • d_lru 把 dentry 加入最近最少使用淘汰链。
  • d_subdirs 连接子目录。
  • d_sb 指向所属文件系统的 super_block。

#mermaid-svg-H1W3iNLTGKFXF2mz{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-H1W3iNLTGKFXF2mz .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-H1W3iNLTGKFXF2mz .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-H1W3iNLTGKFXF2mz .error-icon{fill:#552222;}#mermaid-svg-H1W3iNLTGKFXF2mz .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-H1W3iNLTGKFXF2mz .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-H1W3iNLTGKFXF2mz .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-H1W3iNLTGKFXF2mz .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-H1W3iNLTGKFXF2mz .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-H1W3iNLTGKFXF2mz .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-H1W3iNLTGKFXF2mz .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-H1W3iNLTGKFXF2mz .marker{fill:#333333;stroke:#333333;}#mermaid-svg-H1W3iNLTGKFXF2mz .marker.cross{stroke:#333333;}#mermaid-svg-H1W3iNLTGKFXF2mz svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-H1W3iNLTGKFXF2mz p{margin:0;}#mermaid-svg-H1W3iNLTGKFXF2mz .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-H1W3iNLTGKFXF2mz .cluster-label text{fill:#333;}#mermaid-svg-H1W3iNLTGKFXF2mz .cluster-label span{color:#333;}#mermaid-svg-H1W3iNLTGKFXF2mz .cluster-label span p{background-color:transparent;}#mermaid-svg-H1W3iNLTGKFXF2mz .label text,#mermaid-svg-H1W3iNLTGKFXF2mz span{fill:#333;color:#333;}#mermaid-svg-H1W3iNLTGKFXF2mz .node rect,#mermaid-svg-H1W3iNLTGKFXF2mz .node circle,#mermaid-svg-H1W3iNLTGKFXF2mz .node ellipse,#mermaid-svg-H1W3iNLTGKFXF2mz .node polygon,#mermaid-svg-H1W3iNLTGKFXF2mz .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-H1W3iNLTGKFXF2mz .rough-node .label text,#mermaid-svg-H1W3iNLTGKFXF2mz .node .label text,#mermaid-svg-H1W3iNLTGKFXF2mz .image-shape .label,#mermaid-svg-H1W3iNLTGKFXF2mz .icon-shape .label{text-anchor:middle;}#mermaid-svg-H1W3iNLTGKFXF2mz .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-H1W3iNLTGKFXF2mz .rough-node .label,#mermaid-svg-H1W3iNLTGKFXF2mz .node .label,#mermaid-svg-H1W3iNLTGKFXF2mz .image-shape .label,#mermaid-svg-H1W3iNLTGKFXF2mz .icon-shape .label{text-align:center;}#mermaid-svg-H1W3iNLTGKFXF2mz .node.clickable{cursor:pointer;}#mermaid-svg-H1W3iNLTGKFXF2mz .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-H1W3iNLTGKFXF2mz .arrowheadPath{fill:#333333;}#mermaid-svg-H1W3iNLTGKFXF2mz .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-H1W3iNLTGKFXF2mz .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-H1W3iNLTGKFXF2mz .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-H1W3iNLTGKFXF2mz .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-H1W3iNLTGKFXF2mz .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-H1W3iNLTGKFXF2mz .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-H1W3iNLTGKFXF2mz .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-H1W3iNLTGKFXF2mz .cluster text{fill:#333;}#mermaid-svg-H1W3iNLTGKFXF2mz .cluster span{color:#333;}#mermaid-svg-H1W3iNLTGKFXF2mz div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-H1W3iNLTGKFXF2mz .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-H1W3iNLTGKFXF2mz rect.text{fill:none;stroke-width:0;}#mermaid-svg-H1W3iNLTGKFXF2mz .icon-shape,#mermaid-svg-H1W3iNLTGKFXF2mz .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-H1W3iNLTGKFXF2mz .icon-shape p,#mermaid-svg-H1W3iNLTGKFXF2mz .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-H1W3iNLTGKFXF2mz .icon-shape .label rect,#mermaid-svg-H1W3iNLTGKFXF2mz .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-H1W3iNLTGKFXF2mz .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-H1W3iNLTGKFXF2mz .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-H1W3iNLTGKFXF2mz :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 哈希查找
缓存淘汰
super_block
inode
dentry: test.c
父 dentry: test
父 dentry: code
父 dentry: zjy
dentry Hash
LRU 链表

磁盘上的目录项是持久化数据;dentry 是内存中的路径节点。二者有关联,但不能把它们当成同一个结构。

第一次访问某个路径时,内核可能从磁盘读取目录数据并创建 dentry。之后再次访问时,可以先在 dentry 缓存和 inode 缓存中查找,命中后直接返回相应对象。

七、VFS 中几个对象怎样协作

文件系统类型可能不同,但 Linux 希望应用继续使用统一的 open、read、write 接口。Virtual File System,也就是 VFS,用统一的抽象对象连接上层接口和具体文件系统。

几个核心对象可以这样理解:

对象 主要职责
super_block 表示一个已经挂载的文件系统
inode 表示一个文件系统对象及其元数据
dentry 表示路径树中的一个名字节点
file 表示一次打开产生的文件对象

关系可以简化为:

text 复制代码
进程
  ↓ 持有
file
  ↓ 指向
dentry
  ↓ 指向
inode
  ↓ 属于
super_block

这个图不能反过来解释成"每个 inode 只有一个 dentry"。硬链接会让多个 dentry 指向同一个 inode;而一次打开产生的 file 对象又与文件描述符相关。

八、路径缓存怎样减少磁盘访问

路径解析的查找策略可以概括为:

  1. 先在 dentry 缓存中按父节点和名字查找。
  2. 命中后取得对应 inode。
  3. 未命中时,读取父目录的数据块。
  4. 从目录项中得到子目录或文件的 inode 号。
  5. 把新建的 dentry 加入路径树、哈希表和缓存管理结构。

#mermaid-svg-axHE55dpnSY9aJ3h{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-axHE55dpnSY9aJ3h .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-axHE55dpnSY9aJ3h .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-axHE55dpnSY9aJ3h .error-icon{fill:#552222;}#mermaid-svg-axHE55dpnSY9aJ3h .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-axHE55dpnSY9aJ3h .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-axHE55dpnSY9aJ3h .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-axHE55dpnSY9aJ3h .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-axHE55dpnSY9aJ3h .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-axHE55dpnSY9aJ3h .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-axHE55dpnSY9aJ3h .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-axHE55dpnSY9aJ3h .marker{fill:#333333;stroke:#333333;}#mermaid-svg-axHE55dpnSY9aJ3h .marker.cross{stroke:#333333;}#mermaid-svg-axHE55dpnSY9aJ3h svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-axHE55dpnSY9aJ3h p{margin:0;}#mermaid-svg-axHE55dpnSY9aJ3h .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-axHE55dpnSY9aJ3h .cluster-label text{fill:#333;}#mermaid-svg-axHE55dpnSY9aJ3h .cluster-label span{color:#333;}#mermaid-svg-axHE55dpnSY9aJ3h .cluster-label span p{background-color:transparent;}#mermaid-svg-axHE55dpnSY9aJ3h .label text,#mermaid-svg-axHE55dpnSY9aJ3h span{fill:#333;color:#333;}#mermaid-svg-axHE55dpnSY9aJ3h .node rect,#mermaid-svg-axHE55dpnSY9aJ3h .node circle,#mermaid-svg-axHE55dpnSY9aJ3h .node ellipse,#mermaid-svg-axHE55dpnSY9aJ3h .node polygon,#mermaid-svg-axHE55dpnSY9aJ3h .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-axHE55dpnSY9aJ3h .rough-node .label text,#mermaid-svg-axHE55dpnSY9aJ3h .node .label text,#mermaid-svg-axHE55dpnSY9aJ3h .image-shape .label,#mermaid-svg-axHE55dpnSY9aJ3h .icon-shape .label{text-anchor:middle;}#mermaid-svg-axHE55dpnSY9aJ3h .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-axHE55dpnSY9aJ3h .rough-node .label,#mermaid-svg-axHE55dpnSY9aJ3h .node .label,#mermaid-svg-axHE55dpnSY9aJ3h .image-shape .label,#mermaid-svg-axHE55dpnSY9aJ3h .icon-shape .label{text-align:center;}#mermaid-svg-axHE55dpnSY9aJ3h .node.clickable{cursor:pointer;}#mermaid-svg-axHE55dpnSY9aJ3h .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-axHE55dpnSY9aJ3h .arrowheadPath{fill:#333333;}#mermaid-svg-axHE55dpnSY9aJ3h .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-axHE55dpnSY9aJ3h .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-axHE55dpnSY9aJ3h .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-axHE55dpnSY9aJ3h .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-axHE55dpnSY9aJ3h .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-axHE55dpnSY9aJ3h .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-axHE55dpnSY9aJ3h .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-axHE55dpnSY9aJ3h .cluster text{fill:#333;}#mermaid-svg-axHE55dpnSY9aJ3h .cluster span{color:#333;}#mermaid-svg-axHE55dpnSY9aJ3h div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-axHE55dpnSY9aJ3h .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-axHE55dpnSY9aJ3h rect.text{fill:none;stroke-width:0;}#mermaid-svg-axHE55dpnSY9aJ3h .icon-shape,#mermaid-svg-axHE55dpnSY9aJ3h .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-axHE55dpnSY9aJ3h .icon-shape p,#mermaid-svg-axHE55dpnSY9aJ3h .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-axHE55dpnSY9aJ3h .icon-shape .label rect,#mermaid-svg-axHE55dpnSY9aJ3h .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-axHE55dpnSY9aJ3h .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-axHE55dpnSY9aJ3h .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-axHE55dpnSY9aJ3h :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 是
否
开始解析一个路径组件
dentry 缓存命中?
返回缓存 dentry/inode
读取父目录数据
按文件名查找目录项
取得 inode 号
读取 inode
创建并缓存 dentry

缓存命中时,目录项基本不再需要从磁盘重新读取。缓存未命中时,内核仍然要回到目录数据块中查找。

LRU 表示最近最少使用。缓存空间有限时,系统会优先淘汰较久没有使用的节点,以免路径缓存无限增长。

九、本篇容易混淆的三组概念

概念 A 概念 B 区别
目录 普通文件 两者都有 inode,但目录数据块保存目录项
磁盘目录项 内存 dentry 前者持久化名字与 inode 号,后者建立内存路径树与缓存
文件名 inode 名字由目录项保存,inode 描述目标文件对象

到这里,已经完成从路径到 inode、再到数据块的整条链:

text 复制代码
路径 → 目录项 → inode 号 → inode → 数据块

最后还剩一个整体问题:不同分区各自有 inode 号,它们怎样一起出现在 / 下面的统一目录树中?软链接和硬链接又为什么会表现不同?下一篇通过课堂中的 loop 镜像挂载和 ln 实验完成收束。

相关推荐
爆米花byh20 分钟前
在RockyLinux9环境下安装RustFS Server1.0.6单机版
linux·中间件
BK贝壳20 分钟前
copy() 出来的副本改了会不会牵动本体?bool-hybrid-array 深拷贝本机实测,对照 view() 视图共享
开发语言·数据结构·python·性能优化·numpy
kiracrimson21 分钟前
最短路径:Dijkstra 与 Bellman-Ford、Floyd 各自的边界
数据结构·c++·算法·最短路径·c++学习
光电的一只菜鸡22 分钟前
ubuntu之坑(二十二)——看门狗
linux·单片机·ubuntu
鬼手点金24 分钟前
事件驱动的异步 Agent
人工智能·安全·机器学习·缓存·prompt
Z21-X36 分钟前
云原生运维学习笔记:Kubernetes 动态卷供应与 StatefulSet 实战
运维·笔记·学习·云原生·kubernetes
sharemylove39 分钟前
Java 继承与权限修饰符 学习笔记
java·开发语言·笔记·学习·课程设计
光电的一只菜鸡1 小时前
ubuntu之坑(二十一)——OTA(以x5为例)
java·linux·ubuntu
一座城和一个人1 小时前
2026年复旦大学计算机考研复试机试真题解析
数据结构·考研·算法·考研机试·复试上机