Β§42.2Solution #1: The File System Checker (fsck)

Part III OSTEP pp. 529–530 Β· ~4 min read

  • fsck

The earliest file systems took a lazy stance on crash consistency: let inconsistencies happen, then fix them at reboot. The classic tool for this is fsck (β€œfile system check”), which scans a disk partition for inconsistencies and repairs them.

42.2 The File System Checker

fsck runs before the file system is mounted, assuming no other file-system activity is happening. Its goal is modest but important: make the file system’s metadata internally consistent. (It cannot recover data β€” recall the scenario where the inode and bitmap agree but the data block holds garbage; fsck can’t even tell.) It works in phases:

What fsck checks and repairs, phase by phase. Its only goal is internal METADATA consistency β€” it cannot recover lost or garbage user data.
phasewhat it checkshow it repairs
SuperblockSuperblocksanity, e.g. the file-system size is β‰₯ the number of allocated blocksif it looks corrupt, switch to an alternate superblock copy
Free blocksFree blocksscan all inodes, indirect blocks, etc. to learn which blocks are truly in userebuild the allocation bitmaps; on an inode-vs-bitmap disagreement, TRUST the inodes
Inode stateInode stateeach allocated inode has a valid type field (regular / directory / symlink) and no obvious corruptionclear suspect inodes and update the inode bitmap accordingly
Inode linksInode linksrecompute each inode's link count by walking the ENTIRE directory tree from the rootfix mismatched counts; an allocated inode no directory references β†’ move to lost+found
DuplicatesDuplicatestwo different inodes pointing at the same block (duplicate pointers)clear the obviously-bad inode, or give each inode its own copy of the block
Bad blocksBad blocksa pointer to an address outside the valid range (e.g. beyond the partition size)just remove (clear) that pointer from the inode or indirect block
DirectoriesDirectories"." and ".." are the first entries, every referenced inode is allocated, no directory is linked more than oncerepair the directory's integrity (fsck knows the directory format, unlike file contents)
Dotted-underlined cells have explanations β€” click one.

Building a correct fsck demands intricate knowledge of the file system, and getting every case right is genuinely hard. But it has a deeper, fatal flaw: it’s too slow.

One update changed 3 blocks β€” but fsck must scan every block + the whole directory tree:green = actually updated Β· red = scanned anyway β†’ recovery is O(disk size), not O(update size)

The premise is irrational at scale. Our append changed three blocks β€” yet fsck scans the entire disk and walks the whole directory tree to repair it. Recovery is O(disk size), not O(update size), so on a large volume it can take minutes or hours.

Aside: Searching The Whole House For Keys

It’s like dropping your keys on the floor of your bedroom, and then running a search-the-entire-house recovery algorithm β€” starting in the basement and working through every room. It works, but it’s absurdly wasteful. As disks (and RAIDs) grew, this cost became prohibitive, and people looked for a smarter approach β€” one whose work is proportional to the update, not the whole disk.

Check yourself: the file system checker (fsck)

1.What is fsck's basic strategy for crash consistency?

2.When must fsck run, and what is its goal?

3.When fsck rebuilds the allocation bitmaps and finds a disagreement with the inodes, which does it trust?

4.During its link-count check, fsck finds an allocated inode that no directory references. What does it do?

5.What is the fundamental problem with fsck on modern systems?

6.The 'searching the whole house for keys' analogy captures which idea?

6 questions