# 索引节点

索引节点（inode，是“index node”的缩写）是 Unix 风格文件系统中位于磁盘上的数据结构，用于保存关于单个文件的所有元数据，包括文件大小、所有者、权限、时间戳以及数据块的位置，而文件名则单独存放在目录项中。<sup>[1](https://web.stanford.edu/class/archive/cs/cs111/cs111.1246/lectures/2/Lecture2.pdf)</sup><sup> • </sup><sup>[2](https://www.kernel.org/doc/html/latest/filesystems/ext4/inodes.html?highlight=inode)</sup> 文件名是索引节点编号的别名；多个文件名可以指向同一个索引节点，这正是硬链接背后的机制。<sup>[3](https://www.gnu.org/software/coreutils/manual/html_node/ln-invocation.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 存储内容 | 文件大小、所有者和组 ID、权限、链接计数、时间戳，以及文件数据的块映射表或区段树；文件名不属于索引节点的一部分<sup>[2](https://www.kernel.org/doc/html/latest/filesystems/ext4/inodes.html?highlight=inode)</sup><sup> • </sup><sup>[4](https://man7.org/linux/man-pages/man7/inode.7.html)</sup> |
| 索引节点编号 | 在单个文件系统内唯一；不同文件系统可能复用相同的编号，因此硬链接不能跨越文件系统边界<sup>[4](https://man7.org/linux/man-pages/man7/inode.7.html)</sup> |
| 硬链接 | 指向同一索引节点的所有文件名地位平等；索引节点记录链接计数，只有当计数归零时数据块才会被释放<sup>[3](https://www.gnu.org/software/coreutils/manual/html_node/ln-invocation.html)</sup><sup> • </sup><sup>[5](https://man7.org/training/download/Linux_System_Programming-man7.org-mkerrisk-NDC-TechTown-2020.pdf)</sup> |
| ext4 索引节点大小 | 最初的 ext2 索引节点固定为 128 字节；ext4 在格式化时分配更大的磁盘记录，默认为 256 字节，实际使用的扩展部分大小记录在 i_extra_isize 中<sup>[2](https://www.kernel.org/doc/html/latest/filesystems/ext4/inodes.html?highlight=inode)</sup> |
| ext4 链接限制 | 通常每个索引节点最多 65,000 个硬链接，每个目录最多 64,998 个子目录；DIR_NLINK 特性可解除子目录数量限制<sup>[2](https://www.kernel.org/doc/html/latest/filesystems/ext4/inodes.html?highlight=inode)</sup> |
| 时间戳 | ctime、atime、mtime 和 dtime 均为自 Unix 纪元起的 32 位秒数，将于 2038 年 1 月溢出；ext4 的扩展字段将前三个扩展为 34 位秒加 30 位纳秒，可安全使用至 2446 年 5 月<sup>[2](https://www.kernel.org/doc/html/latest/filesystems/ext4/inodes.html?highlight=inode)</sup> |
| 供给固定 | 在 ext2 和 ext4 中，索引节点的数量在文件系统创建时即已确定；在 ext2 中，增加索引节点数量的唯一方法是扩展文件系统<sup>[6](https://docs.kernel.org/7.0/filesystems/ext2.html)</sup> |
| 检查工具 | `ls -i` 显示索引节点编号，`stat` 报告完整的元数据，`df -i` 报告索引节点使用情况而非块使用情况<sup>[7](https://man7.org/linux/man-pages/man1/df.1.html)</sup><sup> • </sup><sup>[4](https://man7.org/linux/man-pages/man7/inode.7.html)</sup> |

## 索引节点包含的内容

在常规的 Unix 文件系统中，索引节点存储与文件相关的所有元数据：时间戳、块映射表、扩展属性以及类似信息。目录项只保存文件名和索引节点编号。要了解一个文件，系统会遍历目录文件找到对应的目录项，然后加载索引节点。ext4 出于性能考虑略有不同，它将通常保存在索引节点中的文件类型副本直接存放在目录项中。<sup>[2](https://www.kernel.org/doc/html/latest/filesystems/ext4/inodes.html?highlight=inode)</sup>

应用程序通过 `stat(2)` 或 `statx(2)` 可以读取的字段包括：承载文件系统的设备、索引节点编号、文件类型和模式、硬链接计数、用户和组 ID、文件大小、首选 I/O 块大小、已分配块数，以及访问、修改和状态变更时间戳。<sup>[4](https://man7.org/linux/man-pages/man7/inode.7.html)</sup> POSIX 要求 `st_ino` 和 `st_dev` 两个字段合在一起能够在系统内唯一标识一个文件。<sup>[8](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_stat.h.html)</sup>

块映射表占据索引节点中的 `i_block` 数组。早期设计直接存储块指针：Unix v6 的索引节点保存文件数据块编号的有序列表，可容纳 8 个块号，选择这个固定大小是因为变长索引节点不切实际，但固定数量又限制了最大文件大小。<sup>[1](https://web.stanford.edu/class/archive/cs/cs111/cs111.1246/lectures/2/Lecture2.pdf)</sup> ext2 用间接指针扩展了这一方案，分别指向一个间接块、一个双重间接块和一个三重间接块；而 ext4 则用区段树取代了这一方案，以减少元数据开销。<sup>[6](https://docs.kernel.org/7.0/filesystems/ext2.html)</sup><sup> • </sup><sup>[9](https://cdn.kernel.org/doc/html/latest/admin-guide/ext4.html)</sup>

## 文件名、硬链接与删除

**文件就是它的索引节点。** 严格来说，指向一个文件的所有硬链接共享同一个索引节点，索引节点包含关于该文件的所有信息；说索引节点就是文件并不算错。<sup>[3](https://www.gnu.org/software/coreutils/manual/html_node/ln-invocation.html)</sup> 用 `ln` 创建硬链接会添加另一个指向现有索引节点编号的目录项，并递增链接计数。所有链接地位平等，没有原件与副本之分。<sup>[5](https://man7.org/training/download/Linux_System_Programming-man7.org-mkerrisk-NDC-TechTown-2020.pdf)</sup>

删除操作由计数决定。`rm` 移除一个链接，递减索引节点中的计数器；当计数归零时，文件内容会在所有已打开的引用关闭之后才被回收，在此之前已打开的文件仍然可用。<sup>[5](https://man7.org/training/download/Linux_System_Programming-man7.org-mkerrisk-NDC-TechTown-2020.pdf)</sup><sup> • </sup><sup>[12](https://www.man7.org/linux/man-pages/man3/unlink.3p.html)</sup> 大多数系统禁止指向目录的硬链接，以防止文件系统树中出现环；在允许的系统上，只有超级用户才能创建此类链接。<sup>[3](https://www.gnu.org/software/coreutils/manual/html_node/ln-invocation.html)</sup> 符号链接则不同：它们是独立的文件，其内容是按名称指向另一个文件的路径名，而不是同一索引节点的另一个目录项。<sup>[3](https://www.gnu.org/software/coreutils/manual/html_node/ln-invocation.html)</sup>

## 存储布局

索引节点集中存放在索引节点表中，而不是与数据分散存放。Unix v6 从第 2 块开始预留了一段块区域用于此表，位于引导块和超级块之后，这样索引节点可以被快速读取并在使用时缓存到内存中。<sup>[1](https://web.stanford.edu/class/archive/cs/cs111/cs111.1246/lectures/2/Lecture2.pdf)</sup> ext2 和 ext4 将文件系统划分为块组，每个块组有自己的索引节点位图和索引节点表；块分配器会尽量将数据块放在引用它们的索引节点所在的同一块组中。<sup>[6](https://docs.kernel.org/7.0/filesystems/ext2.html)</sup>

给定索引节点的位置可以通过算术计算得出。每个块组包含 `s_inodes_per_group` 个索引节点，由于索引节点 0 不存在，因此所在块组为 `bg = (inode_num - 1) / s_inodes_per_group`，在组表中的索引为 `(inode_num - 1) % s_inodes_per_group`，再乘以索引节点大小即得到字节偏移量。<sup>[2](https://www.kernel.org/doc/html/latest/filesystems/ext4/inodes.html?highlight=inode)</sup>

## ext4 中的大小与增长

在 ext2 和 ext3 中，索引节点结构固定为 128 字节，即 `EXT2_GOOD_OLD_INODE_SIZE` 的值。从 ext4 开始，可以在格式化时为所有索引节点分配更大的磁盘索引节点，记录大小存储在超级块的 `s_inode_size` 中。字段 `i_extra_isize` 记录原始 128 字节之外实际使用了多少字节，这使得内核侧的结构体可以增长而无需重写每个磁盘上的索引节点。默认情况下 ext4 索引节点记录为 256 字节，截至 2019 年 8 月，结构体本身为 160 字节。结构体末尾与记录末尾之间的空间可用于存放扩展属性，记录最大可达文件系统块大小。<sup>[2](https://www.kernel.org/doc/html/latest/filesystems/ext4/inodes.html?highlight=inode)</sup>

## 时间戳与限制

ext4 索引节点的前 128 字节中包含四个时间戳：索引节点变更时间（ctime）、访问时间（atime）、数据修改时间（mtime）和删除时间（dtime）。每个都是自 Unix 纪元起的 32 位有符号秒数，因此这些字段将在 2038 年 1 月溢出。当索引节点足够大时，配套的 `i_[cma]time_extra` 字段将前三个扩展为 34 位秒和 30 位纳秒，将溢出时间推迟到 2446 年 5 月；dtime 未被扩展。<sup>[2](https://www.kernel.org/doc/html/latest/filesystems/ext4/inodes.html?highlight=inode)</sup> 自 Linux 2.6.23 起，ext4 支持纳秒级时间戳，XFS、JFS 和 Btrfs 也支持，但 ext2、ext3 和 Reiserfs 不支持。<sup>[4](https://man7.org/linux/man-pages/man7/inode.7.html)</sup>

链接计数字段 `i_links_count` 施加了结构性限制。ext4 通常不允许一个索引节点超过 65,000 个硬链接，这也将目录的子目录数量限制在 64,998 个，因为每个子目录的 `..` 目录项和目录自身的 `.` 目录项都计入链接数。启用 DIR_NLINK 特性后，该字段被设为 1 以表示计数未知，从而允许更多子目录。<sup>[2](https://www.kernel.org/doc/html/latest/filesystems/ext4/inodes.html?highlight=inode)</sup>

由于在这些文件系统中索引节点数量在创建时即已固定，文件系统可能在数据块仍有空闲时耗尽索引节点；`df -i` 报告索引节点使用情况，以便监控。<sup>[6](https://docs.kernel.org/7.0/filesystems/ext2.html)</sup><sup> • </sup><sup>[7](https://man7.org/linux/man-pages/man1/df.1.html)</sup>

## 历史

索引节点可追溯到 Unix 文件系统的最初设计。1969 年，在贝尔实验室（Bell Laboratories），肯·汤普森（Ken Thompson）、R. H. 卡纳迪（R. H. Canaday）和[丹尼斯·里奇](https://www.edgepedia.org/zh/dennis-ritchie)（Dennis Ritchie）在黑板上和草稿纸上推敲出了一个文件系统的基本设计，后来成为 Unix 的核心，其中大部分设计归功于汤普森。<sup>[10](https://pdos.csail.mit.edu/6.828/2010/readings/ritchie79evolution.html)</sup> 将文件元数据与文件名分离——文件名保存在目录中，作为对每个文件的索引节点的引用——这一设计从那时起一直延续到 ext2、ext3、ext4 以及它们之上的 Linux 虚拟文件系统层，内核在其中为每个活跃的文件对象维护一个内存中的 `struct inode`。<sup>[2](https://www.kernel.org/doc/html/latest/filesystems/ext4/inodes.html?highlight=inode)</sup><sup> • </sup><sup>[11](https://www.kernel.org/doc/html/latest/filesystems/path-lookup.html)</sup>

## References

1. [CS111 Lecture 2, Stanford University](https://web.stanford.edu/class/archive/cs/cs111/cs111.1246/lectures/2/Lecture2.pdf)
2. [Index Nodes, ext4 Data Structures and Algorithms, The Linux Kernel documentation](https://www.kernel.org/doc/html/latest/filesystems/ext4/inodes.html?highlight=inode)
3. [ln invocation, GNU Coreutils manual](https://www.gnu.org/software/coreutils/manual/html_node/ln-invocation.html)
4. [inode(7), Linux manual page](https://man7.org/linux/man-pages/man7/inode.7.html)
5. [Linux/UNIX System Programming Fundamentals, Michael Kerrisk](https://man7.org/training/download/Linux_System_Programming-man7.org-mkerrisk-NDC-TechTown-2020.pdf)
6. [The Second Extended Filesystem, The Linux Kernel documentation](https://docs.kernel.org/7.0/filesystems/ext2.html)
7. [df(1), Linux manual page](https://man7.org/linux/man-pages/man1/df.1.html)
8. [<sys/stat.h>, POSIX.1-2017, The Open Group](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_stat.h.html)
9. [ext4 General Information, The Linux Kernel documentation](https://cdn.kernel.org/doc/html/latest/admin-guide/ext4.html)
10. [Early Unix history and evolution, D. M. Ritchie](https://pdos.csail.mit.edu/6.828/2010/readings/ritchie79evolution.html)
11. [Pathname lookup, The Linux Kernel documentation](https://www.kernel.org/doc/html/latest/filesystems/path-lookup.html)
12. [unlink(3p) - Linux manual page](https://www.man7.org/linux/man-pages/man3/unlink.3p.html)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Operating systems › File systems

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/inode

English original: https://www.edgepedia.org/inode
