CSE Roadmap

Core tier · about 3 hours · 7 parts

Part 3 of 7 · 3 min read

fsck

Think of an accountant who, after a fire, checks every ledger against every receipt to find the entries that do not match. It works and it takes days. The analogy stops in one place: an accountant can skip a ledger that was in the safe, while fsck cannot know which parts were safe.

In plain words: fsck (file system check) reads all the metadata and checks that it agrees with itself. It does not know what was in progress, so it checks everything. The ext tools do it in passes: first the inodes and their blocks, then the directory structure, then connectivity (every directory is reachable from the root), then the link counts (the count in each inode matches the entries that point to it), and finally the group summaries and the bitmaps (each used block belongs to a file, and each free block is free). Where it finds a disagreement, it fixes it by a rule, for example by marking blocks in use or by moving an orphan file to lost+found.

A worked example. On a test image I used debugfs -w to make two errors: three blocks (1000 to 1002) marked free in the bitmap while in use, and a link count of 3 on a file with one name. e2fsck -fn (check only, change nothing) reported:

Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Inode 13 ref count is 3, should be 1.  Fix? no
Pass 5: Checking group summary information
Block bitmap differences:  +(1000--1002)
Free blocks count wrong for group #0 (2548, counted=2551).
img4: ********** WARNING: Filesystem still has errors **********

The tool found both errors by comparison with other structures, and it also noticed that the free block count in the group summary no longer matched. It could not tell why. The time of fsck grows with the amount of metadata. If a 4 TiB disk has 64 GiB of inode tables (page 2) and fsck must read them at 100 MB/s, it needs 64 GiB / 100 MB/s = 687 s, about 11.5 minutes, before the other passes. That is the time that the machine spends offline after each unclean shutdown.

The key lines. fsck can restore consistency, but it cannot restore lost data, and it may delete a damaged file. It fixes the structure and does not return what a crash made impossible to know.

fsck

fsck is a program that checks a file system's on-disk structures for disagreements, for example between bitmaps and inodes or between link counts and directory entries, and repairs them by fixed rules. It must read most of the metadata, so its running time grows with the size of the file system.

Example: e2fsck found a link count of 3 where only one directory entry existed and offered to set it to 1.

Common mistake

Never run fsck with repairs on a file system that is mounted read-write. The tool and the kernel would change the same structures at the same time and make the damage worse. Unmount it first, or use the check-only flag.

Check yourself: 1. Why does fsck check everything and not only what was in progress? 2. Which pass compares link counts with directory entries? 3. Why does the repair time grow with the size of the disk?