JOURNALING

Write the plan down before you touch the disk

L214 MINOPERATING SYSTEMS
PREDICT

A journaling file system first logs the new block images to a journal, between a TxBegin and a commit record, and only then writes them in place. The journal holds TxBegin and all three new block images for creating /b.txt, but no commit record. The power fails. What does /b.txt look like after reboot?

Creating a file changes three blocks: the inode bitmap, the inode table and the directory. The disk writes one block at a time. Crash between two writes and the structures disagree: an inode marked used that no entry points at, or an entry that points at a free inode.

A journal makes the three writes atomic. First write the new block images to a log. Then write a commit record. Only then write the blocks to their real home. On reboot, replay every committed transaction and discard the rest.

▶ SIMULATION
FS (MEMORY)
operation: —
blocks written in place: —
—
DISK
inode bitmap: 1 1 0 0 0 0
inode table: 0:dir 1:file
/ entries: a.txt → 1
JOURNAL
none
commit record: no
crashes: 0
consistencynot checked until the operation finishes or recovery runs
✓ after recovery: every dir entry points at an allocated inode✓ after recovery: every allocated inode is referenced
SCHEDULE
01[FS]create /b.txt: pick free inode (in memory)
02[DISK]write inode bitmap block in place
03[DISK]write inode table block in place
04[DISK]write directory block in place
05[FS]create returns; check the disk
Start of no journal, no crash scenario. 5 steps.
Journaling (write-ahead logging for file system metadata)
===================================================

create /b.txt changes 3 blocks:
  inode bitmap  (mark inode 2 allocated)
  inode table   (initialise inode 2)
  directory     (add "b.txt" -> 2)
The disk writes them one at a time. A crash between them leaves
a leaked inode or a dangling entry, and only fsck can find it.

With a journal:
  1. journal write:  TxBegin + the 3 new block images
  2. journal commit: TxEnd  (wait for 1 to be durable first)
  3. checkpoint:     write the 3 blocks to their real home
  4. free:           mark the transaction done in the journal

Recovery on mount:
  scan the journal
  transaction has TxEnd?  replay its blocks (redo; safe to repeat)
  no TxEnd?               discard it; the disk was never touched

Modes (ext4): journal = data + metadata; ordered (default) =
metadata journaled, data written before the commit; writeback =
metadata only, no ordering for data.

Watch the two invariants once recovery runs. NO JOURNAL: CRASH MID-CREATE leaks inode 2. Both journal scenarios crash at the same kind of moment and recover to a consistent disk: before the commit the create vanishes, after the commit it is finished by replay.

BREAK IT NOT YET

This file system has no journal. Creating /b.txt writes three blocks in place, one at a time. Insert a crash (with reboot) so that the disk is inconsistent after recovery.

GOAL: after reboot, an inode is leaked or an entry dangles

FIX IT NOT YET

Write the mount-time recovery. journal is the log in order: {type:'begin', tx}, {type:'write', tx, block, data}, {type:'commit', tx}. disk maps block names to contents. Replay the writes of every committed transaction in log order, ignore transactions with no commit record, and return the disk.

Mental model:

a journal turns several block writes into one atomic step. Write the plan to the log, then the commit record, then do the work. A crash before the commit means the plan never happened. A crash after it means recovery will replay the plan. The commit record write is the only moment that matters.

IN PRODUCTION

ext3/ext4 (jbd2), XFS and NTFS metadata logging, and the same idea one level up as the write-ahead log in every database. Symptoms when it is missing or misconfigured: long fsck after power loss, files that come back zero-length after a crash because the metadata was journaled but the data never reached disk, and disks or controllers that acknowledge a write before it is durable and break the ordering the journal depends on.

IN INTERVIEWS

“What happens if the machine crashes halfway through creating a file?” or “How does a journaling file system recover?” Name the blocks involved, show that no write order avoids every inconsistency (soft updates still leaks inodes), walk journal write, commit, checkpoint, and say what recovery does with committed and uncommitted transactions. Mention ordered mode: data blocks hit disk before the metadata commits.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.
NEXT: UNIX SIGNALS →