JOURNALING
Write the plan down before you touch the disk
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.
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.
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
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.
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.
“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.