Skip to content

30. Journaling

Problem: writing a file isn't atomic — creating one might need 3 separate writes (inode bitmap, data bitmap, actual data). A crash mid-sequence leaves the filesystem inconsistent.

Core idea (write-ahead logging): write your intentions to a log (the journal) first, then apply them.

  1. Write to journal: TxB | inode bitmap | data bitmap | data block | TxE
  2. Commit journal (mark transaction committed)
  3. Checkpoint: apply the actual changes
  4. Free the journal space

Crash scenarios: crash before TxE → transaction never committed, ignored on reboot, filesystem untouched. Crash after TxE but before checkpoint → replay the journal on reboot, reapplying changes. Crash after checkpoint → nothing to do.

Mode What's logged Safety Performance
Full (data journaling)Metadata + dataHighestSlowest
Ordered (ext4 default)Metadata, data written firstGoodGood
WritebackMetadata onlyLowestFastest

Analogy: like writing a to-do list before surgery. If power cuts mid-surgery, the doctor reads the list and knows exactly what was and wasn't done.