1write(fd, buf, 4096);2fsync(fd);bitmapinode 2block 5DISKtxbslot Islot Bslot DtxebitmapB1inode 2I1block 5junkB2I2DbTxBTxE
Back: your program and memory (the page cache)Front: the disk, with the journal and the home blocksTokens: blocks being written, TxB/TxE, a power failure, the fsck scanMemory and your program at the back; the disk in front, with a journal row and the home blocks; fsck and the power supply on the right. Without a journal, a crash after one of three writes leaves the inode and the bitmap disagreeing. With a journal, the blocks are logged first, TxE commits them, and only then are they copied home; recovery skips an uncommitted transaction and replays a committed one. ext4 has three modes: data=ordered (the default, shown last), data=journal (everything through the journal, as in the middle of the lesson; deprecated, with removal planned for January 2028) and data=writeback (no ordering for data). All three keep the metadata consistent.

The problem

  1. Your program adds 4 KB to a file. On disk that changes three blocks: the new data block Db, inode 2 that now points to it, and the bitmap B that marks it used. They wait in memory first.

The journal

Recovery

What happens

;; step 1/10 · three-blocks
line 1   write(fd, buf, 4096);

blocks to write          old            new
data bitmap  B           00001000       00001100
inode 2      I           size 1 block   size 2 blocks, -> block 5
data block 5 D           junk           Db (your 4096 bytes)

until then they sit in the page cache, in memory

The example is the one in OSTEP chapter 42: a tiny file system with an 8-bit data bitmap, where appending one 4096-byte block to the file in inode 2 allocates data block 5. Real file systems have far more blocks, but the same three kinds change. Hard disks promise that a single 512-byte sector write happens completely or not at all; that is why the commit block is kept to one sector. Real journals such as Linux jbd2 also put a checksum in the commit block, so a damaged commit is discarded during replay.