Lesson 22 of 26 · 12 min read
Crash consistency: fsck and write-ahead journaling
Trace six possible crash outcomes when appending a data block, compare full-disk fsck scans against redo write-ahead journaling, and test power cuts live.

Course syllabus · lesson 22 of 26
In this lesson
Even a tiny append of one 4 KiB block (Db) to an existing file must update three separate disk blocks: the inode (I[v2]), the data bitmap (B[v2]), and the new data block (Db) itself. Assume for this first trace that each block write is atomic and the three can become durable independently. The model exposes inconsistent combinations even before we account for writes that tear within a block.
The crux of the problem is how to update multiple interrelated on-disk structures atomically across arbitrary power losses or kernel panics without scanning the entire terabyte disk on every reboot.
Cut power at each point in the journal timeline
Append one block (I[v2], B[v2], Db). Write barriers force earlier stages to reach disk before the next starts. Cut power to predict recovery.
Trace the journal write barrier
Ordered mode · 3 · After TxE, before checkpoint
New write preserved or clean pre-write state
Journal on disk: Complete committed transaction: TxB + I[v2] + B[v2] (Db already written directly to home slot) + TxE.
Home blocks on disk: Db is durable at its home address; home I[v1] and B[v1] are still stale.
Recovery pass: Recovery sees TxE and REPLAYS I[v2] and B[v2] to their home blocks. Because Db wrote before TxB, inode v2 never points to garbage.
Six partial crash states of a three-block append
The old inode describes a one-block file, and the data bitmap shows disk block 4 is free. An append sets the second direct pointer to 4, increases file length from 4 KiB to 8 KiB, marks block 4 allocated, and puts the new bytes there. Of the eight possible durable subsets, two are endpoints: none of the writes gives the old file and all three give the new file. The six partial states differ in what recovery can infer.
- Only
Dbwrote: OldI[v1]andB[v1]are untouched — filesystem stays consistent (the new append simply vanished). - Only
I[v2]wrote:I[v2]now points to unallocated disk address4containing old garbage data AND contradictsB[v1](bitmap says block 4 is free!). - Only
B[v2]wrote: Block 4 is marked allocated in the bitmap while zero inodes reference it — a space leak shrinking usable capacity forever! I[v2]+B[v2]wrote,Dbdid NOT: Metadata is mutually consistent, yet reading the file returns stale garbage from block 4!I[v2]andDbwrote. The file reads the intended new bytes, but block 4 still looks free. A later allocator could hand it to another inode. Repair can mark it allocated if it is otherwise uniquely owned.B[v2]andDbwrote. Block 4 contains the new bytes and appears allocated, but the old inode does not claim it. The checker can recover the space. The bytes alone provide no reliable way to connect them to the old file.
Try to express each case using three independent predicates: the inode claims block 4, the bitmap reserves block 4, and the device holds the desired new bytes at block 4. A metadata scan can test the first two. In the state where the inode and bitmap agree and the data write was lost, the file's size and pointers can look perfectly ordinary to that scan.
Approach #1: fsck (File System Checker)
Early UNIX accepted inconsistent states on crash and ran fsck during boot before mounting. fsck performs a full multi-pass sweep checking superblock consistency, rebuilding free-space bitmaps by walking every inode, checking directory . / .. links and reference counts (links_count), freeing orphaned unreferenced inodes, and fixing duplicate block pointers.
A checker can first validate geometry using the superblock, then walk allocated inodes and their indirect blocks to build an independent map of claimed blocks. A claimed block marked free can be added to the allocation bitmap. An allocated but unclaimed block can be freed after accounting for reserved blocks and filesystem metadata. Two live inodes claiming the same payload block need a different check. The bitmap only has one bit and cannot tell which claimant is right. A repair may copy the bytes to a second block to restore unique ownership, yet the original ownership may still be ambiguous.
A later directory walk verifies that entries reference valid inodes and counts links independently of the counters stored in those inodes. A valid orphaned inode with useful data may be reconnected under lost+found. A checker can restore invariants without recovering every intended update. Its work scales with the structures it must inspect even when the interrupted operation was tiny. For scale, streaming 4 decimal TB at 200 MB/s would take about 20,000 seconds (5.6 hours). A metadata-focused pass need not stream every payload byte, and scattered I/O may deliver far less than sequential streaming speed.
Approach #2: Journaling (Write-Ahead Logging)
Inspired by database write-ahead logs, journaling (ext3/ext4, XFS, NTFS) appends a small note describing intended updates to a sequential journal before touching final home locations:
- Data Journaling (
journal=data): WriteTxB(transaction begin ID),I[v2],B[v2], andDbto the journal and wait for durability. Then write theTxEcommit record and wait for durability. The recovery format must detect incomplete commits, for example by fitting the commit marker into a guaranteed atomic write unit. Only then checkpointI[v2],B[v2],Dbasynchronously to their final home blocks and mark the journal transaction free!- Crash before
TxE: Recovery finds an incomplete transaction withoutTxEand skips it — home blocks remain cleanly atv1! - Crash after
TxE, before checkpoint: Recovery reads the committed journal entry and replaysI[v2],B[v2],Dbidempotently into their home locations (redo logging).
- Crash before
- Ordered / Metadata Journaling (
journal=orderedin ext-family filesystems): Full data journaling writes each payload block twice, once to the log and once to its home location. Ordered mode writesDbstraight to its home block and logs only metadata (TxB + I[v2] + B[v2]). Both the home data and the log body must be durable before the commit record. BecauseDbhit disk beforeI[v2]was ever committed, recovery afterTxEnever points an inode at uninitialized garbage!
Ordered journaling durability dependencies
Write Db at home and [TxB, I[v2], B[v2]] to the journal
Wait for both to become durable
Write [TxE] and wait for its durability
Write I[v2], B[v2] at home and wait for durability
Allow reuse of the journal slot
Consider a circular journal with 4 KiB blocks. A begin record gives the transaction sequence number and identifies the home destinations of the logged block images. The end record identifies the committed sequence. If TxB and TxE each use one block, the append in data mode consumes five journal blocks (begin, inode, bitmap, payload, commit) plus three home writes, or eight block writes. Ordered mode consumes four journal blocks plus three home writes, or seven block writes. These figures omit journal bookkeeping, cache flush commands, and batching across transactions. With a large contiguous append, the extra payload copies in data mode can dominate the cost.
The durability edges matter more than the order in which the kernel issues independent writes. In ordered mode data and the log body can be issued together. Both must survive a power loss before the commit can count. Checkpoint may begin once the commit is durable. The tail of the circular log can advance past the transaction only after the home writes are durable. If recovery itself loses power halfway through checkpointing, it can replay the same full block images again. Transaction sequence numbers distinguish a current commit from an old marker left in the same physical log slot.
Two subtle journaling traps: Write barriers & Block reuse (revoke)
Modern drives carry volatile write-back caches that reorder requests for speed. Without durability ordering implemented through device cache flushes, force-unit-access writes, or equivalent guarantees between writing TxB + I + B and writing TxE, a device could persist TxE first. Recovery needs validation strong enough to reject a commit whose body is missing or torn. In ordered mode it cannot rely on a checksum of the log body to tell whether the unlogged payload reached home.
Second, suppose Tx 10 journals an image of directory foo at block 4. A later transaction frees that block when removing the directory, and you reuse foo's freed disk block as a user file data block (with the new payload unlogged in ordered mode), and crash before checkpointing Tx 10. Naive replay of Tx 10 would overwrite your new file data with old directory bytes! Real kernels (ext3/ext4) write revoke records into the journal that suppress replay of an older image of that address. Recovery applies revocations from committed transactions, and a later committed image for that address can still be replayed.
Worked trace: cut power at four points on the timeline
Place these power cuts on the timeline and state which block version recovery uses:
- After
Dbis durable but before the ordered-mode journal body is written:Dbsits in a free home slot. This is one allowed order. The oldI[v1]andB[v1]are clean. The old file remains intact because its inode has no pointer to the new block. - After
TxB + I[v2] + B[v2], BEFORETxE: Commit block is missing! Recovery scans the circular log, ignores the partial transaction, and preservesv1cleanly (free block stays free). - After
TxE, BEFORE checkpoint completes: Commit blockTxEis present! Recovery replaysI[v2]andB[v2]to home blocks. In data mode, recovery also replaysDb. In ordered mode, the existing home copy ofDbwas made durable beforeTxE. - After checkpoint, before log reuse. The home blocks already contain the new state. If the journal entry is still present, repeating its full-block redo writes leaves the same result. Once recovery can rely on those home blocks, the journal space can be reused.
Repeat the second power cut in data mode. The new payload was written into the journal along with the inode and the bitmap, so the home data slot is unchanged at that point. Compare that to an ordered-mode overwrite of an already allocated block. If the new data reaches its home block before the commit, the old inode may already expose those bytes after the crash. The append trace preserves the old file before commit because its old inode has no reference to the new block.
Copy-on-write publication
A copy-on-write filesystem can place the new data at a fresh address, write a replacement inode pointing to it, and copy each modified ancestor up to a new root. For a simplified tree, write data at 90, an inode at 91 referencing 90, and a root at 92 referencing 91. The current checkpoint still selects the old root at 40. Once the replacement branch is durable, publish 92 through a root record with an atomic or recoverably redundant update. Before that record persists, the old reachable tree remains usable. After it persists, the new root selects the replacement tree. Reclaim the old blocks only once no active recovery root or snapshot needs them. Copying ancestors costs extra writes, and space accounting has to survive a crash along with the file data.
Separate the commit boundary from what the caller sees
Suppose an application appends one record, waits for a successful write(), and immediately loses power. A buffered write can return while the file's new data and metadata are still in memory. The return alone does not select any of the journal crash points. If the application instead waits for a successful fsync() on the file, the filesystem must have provided the durability promised for that operation before returning. A filesystem can satisfy that by durably committing a replayable transaction and checkpointing later. This is why counting home writes does not by itself predict synchronous write latency.
A related distinction matters to a checker. A cleanly replayed transaction can restore the file's pointers, allocation bits, and expected data from the log, or rely on data that was already made durable. A checker working without that ordering record may only know that block 4 is allocated. How the filesystem publishes reachability determines which evidence is available at reboot.
Next, see how Flash SSDs rewrite disk assumptions underneath journals and file systems, and how checksums defend against silent disk corruption, in Flash SSDs and end-to-end data integrity.
Before you move on
Try it in your workspace
Trace a redo journal timeline (TxB -> I[v2], B[v2], Db -> TxE -> checkpoint). Cut power before TxE and after TxE before checkpoint; verify recovery behavior in the interactive journal lab.
Keep a short note of what you tried, what passed, and what you still need to check.
0 of 3 checked.
Saved in this browser when storage is available. Uncheck any item to revisit it. This is your own record, not an assessment.