ระบบไฟล์
แนะนำใหม่ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| inode/ɪˈnəʊd/ | โหนด inode |
| directory/daɪˈrektəri/ | A file-system structure used to organise entries for files and other directories. |
| Journaling/ˈdʒɜːnəlɪŋ/ | writing planned file-system changes to a log first, so the system can recover after a crash |
| bitmap/ˈbɪtmæp/ | An image representation storing information about a grid of individual pixels. |
| fragmentation/ˌfræɡmənˈteɪʃn/ | free space broken into pieces too small or scattered to use well |
| hard link/hɑːd lɪŋk/ | An extra directory entry pointing to the same inode, so the file has several equal names. |
| symbolic link/sɪmˈbɒlɪk lɪŋk/ | A small file that contains the path of another file; it breaks if that path is removed. |
| Shortest seek first/ˈʃɔːtɪst siːk fɜːst/ | disk scheduling that serves the request nearest the current head position next |
Names, inodes, blocks
- The inode 索引节点 holds a file's metadata, 4 direct pointers and one indirect block (256 pointers, with 1 KiB blocks). A directory 目录 is itself a file of name-to-inode entries.
- The largest file is $(4 + 256) \times 1\,\mathrm{KiB} = 260$ KiB. With 4 KiB blocks it is $(4 + 1024) \times 4\,\mathrm{KiB} = 4112$ KiB, about 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.
The price of block 6
- Reading block 6 of /notes/x: the directory lookup finds the inode (already cached). Block 6 is past the four direct pointers, so the system reads the indirect block (pointer 2) and then the data block: two disk reads for one block of the file.
- Reading a 100-block file from start to end costs $100 + 1$ reads, because the indirect block is read once. Deleting a file clears pointers, not data: the bytes stay until the block is reused.
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.
The crash question
- An append writes the data, then the free-list bit, then the inode size. If the machine crashes between writes: data first, then inode, loses only some space (an orphan block). Inode first, then data, gives a wrong size, and reads return garbage.
- Journaling 日志记录 writes what it is about to do first, and replays it after a crash. Modern file systems pay this small extra write to stay consistent after crashes.
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.
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้