文件系统
引入
6:44
操作系统
操作系统让一个处理器看起来像多台机器。前三个单元讲解处理器所呈现的假象:进程、轮流运行和安全共享。接下来的两个单元讲内存和存储。最后一个单元构建程序与内核之间的屏障。本课程中的每张表、每种交错执行和每次页表遍历都是确定的,所以你可以自己重新计算。
英文讲解 · 可切换中英文字幕
本课内容
| English | 释义 | 拼音 |
|---|---|---|
| inode/ɪˈnəʊd/ | 索引节点 | suǒ yǐn jié diǎn |
| directory/daɪˈrektəri/ | 目录 | mù lù |
| Journaling/ˈdʒɜːnəlɪŋ/ | 日志记录 | rì zhì jì lù |
| bitmap/ˈbɪtmæp/ | 位图 | wèi tú |
| fragmentation/ˌfræɡmənˈteɪʃn/ | 碎片 | suì piàn |
| hard link/hɑːd lɪŋk/ | 硬链接 | yìng liàn jiē |
| symbolic link/sɪmˈbɒlɪk lɪŋk/ | 符号链接 | fú hào liàn jiē |
| Shortest seek first/ˈʃɔːtɪst siːk fɜːst/ | 最短寻道优先 | zuì duǎn xún dào yōu xiān |
名称、索引节点与块
- 索引节点存储文件元数据及4个直接指针和一个间接块(256个指针,每个指向1 KiB空间);目录本身是一个包含名称到索引节点条目的文件。
- 大小上限:$(4 + 256) \times 1\,\mathrm{KiB} = 260$ KiB。使用4 KiB块时:$(4 + 1024) \times 4\,\mathrm{KiB} = 4112$ KiB,约4 MiB。


With 1 KiB blocks, 4 direct pointers and one 256-pointer indirect block, the max file is —
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
(4 + 256) x 1 KiB = 260 KiB.
块6的代价
- 读取/notes/x中的块6:目录查找定位索引节点(已缓存);块6超出四个直接指针范围——需读取间接块(指针2),再读取数据块:一次逻辑块对应两次磁盘读取。
- 顺序读取一个100块的文件大小为$100 + 1$次读取(间接块仅需一次)。删除操作清除指针而非数据——字节保留直至被重用。
Reading block 6 (beyond the direct pointers) costs — disk reads
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
The indirect block first (pointer 2), then the data block.
Put the reads for block 6 of a file in order (inode $4$ direct pointers, one indirect block).
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
Blocks $0$ 到 $3$ use the direct pointers, so block 6 is entry $6 - 4 = 2$ of the indirect block: one extra read before the data.
崩溃问题
- 追加写入先写数据,再写空闲列表位,最后写索引节点大小。写入间发生崩溃:若先写数据后写索引节点,仅损失空间(产生孤儿块);若先写索引节点后写数据,则虚报大小(读取返回垃圾数据)。
- 日志记录先写入意图并在崩溃后重放——这是现代文件系统为换取崩溃一致性所支付的小额写入开销。
Journaling survives crashes by —
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
The journal records what is about to happen; replay finishes it after the crash.
这是一张新卡片,翻译完成前会显示英文。
Tracking free space
- A bitmap 位图 keeps one bit per block. A 1 TiB disk with 4 KiB blocks has $2^{40} / 2^{12} = 2^{28}$ blocks, so the bitmap is $2^{28}$ bits $= 32$ MiB, small enough to cache.
- Three ways to give a file its blocks, each with a different weak spot. External fragmentation 碎片 means free space broken into gaps too small to use.
| Allocation | Random access | External fragmentation |
|---|---|---|
| contiguous | fast | yes |
| linked | walk the chain | none |
| indexed (inode) | fast | none |
- Indexed allocation pays with the pointer blocks themselves, which is why large files need an indirect block.
A 256 GiB disk uses 4 KiB blocks. How large is its free-space bitmap, in MiB?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
2^38 / 2^12 = 2^26 blocks, so 2^26 bits = 2^23 bytes = 8 MiB.
Which allocation gives fast random access and no external fragmentation?
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Each pointer jumps straight to a block, and blocks can sit anywhere on the disk.
这是一张新卡片,翻译完成前会显示英文。
Hard links and symbolic links
- A hard link 硬链接 is a second directory entry for the same inode. The inode counts its links and frees the blocks only when the count reaches 0. A symbolic link 符号链接 is a small file that holds a path.
- Delete the original name: the hard link still reads the file (the count goes from 2 to 1), but the symbolic link now points at nothing. Hard links cannot cross file systems; symbolic links can.
Deleting a file's original name breaks its hard links.
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
The inode survives while its link count is above zero; symbolic links break.
这是一张新卡片,翻译完成前会显示英文。
Worked example: ordering disk requests
- The head is at cylinder 50, with requests 82, 170, 43, 140, 24, 16, 190. In arrival order (FCFS) it moves 642 cylinders.
- Shortest seek first 最短寻道优先 goes 43, 24, 16, then out to 82, 140, 170, 190: only $\mathbf{208}$, although requests at the far end can wait for a long time. SSDs have no head, so this ordering does not help them.
Head at 50; requests 82, 170, 43, 140, 24, 16, 190. Total seek distance with shortest seek first?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
$7 + 19 + 8 + 66 + 58 + 30 + 20 = 208$.
Same requests in arrival order: total seek distance?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
$32 + 88 + 127 + 97 + 116 + 8 + 174 = 642$.
这是一张新卡片,翻译完成前会显示英文。
Marks that slip away
- Thinking delete erases. Deletion clears pointers and the free bit; the bytes stay until the block is reused.
- Reading the indirect block every time. A sequential read loads it once: $101$ reads for a 100-block file.
- Writing the inode before the data. A crash between the two leaves an inode 索引节点 that points at garbage. Write the data first, or use a journal.
这是一张新卡片,翻译完成前会显示英文。
Recap
- Inode: metadata, 4 direct pointers, one indirect block; a directory maps names to inodes.
- Largest file: $260$ KiB with 1 KiB blocks, about 4 MiB with 4 KiB blocks.
- Journaling 日志记录 writes the intent first, so a crash replays the change instead of damaging the file system.
出了什么问题?只有网站团队能看到这些信息。