The problem
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 disk writes one block at a time. Say the new inode lands, then the power fails and memory is lost. Inode 2 now points to block 5, but the bitmap says block 5 is free, and block 5 holds junk.
The old fix is fsck, a checker run after the crash. It reads every inode and bitmap and trusts the inode, so it marks block 5 used. Now it is consistent, but the file ends in junk, and the scan can take hours.
The journal
Rewind to before the crash. Journaling adds one rule: before changing anything in its real place, write the whole update into the journal, a reserved area of the disk. Databases call the same idea write-ahead logging.
fsync() asks for the file to be safe on disk, and waits. The file system writes a begin block, TxB, then copies of I2, B2 and Db, into the journal. The home blocks are not touched yet.
Only when those four blocks are on disk does it write the end block, TxE. TxE fits in one 512-byte sector, which the disk writes whole or not at all. With TxE on disk, the update is committed.
Now the checkpoint: the file system writes I2, B2 and Db to their real places. Later it marks the transaction free, so the journal space can be used again. The journal is a loop, reused over and over.
Recovery
Now crash at the worst moments. If power fails before TxE is written, recovery at boot finds TxB but no TxE, so it skips the transaction. The disk still has the old blocks: the append is lost, nothing is broken.
If power fails after the commit, say mid-checkpoint, recovery finds a full transaction, TxB to TxE, and writes all its blocks home again. Writing a block twice is harmless, so even a crash during recovery is fine.
Journaling data writes it twice. So ext4 by default journals only metadata: it writes Db straight to block 5 first, then logs I2 and B2 and commits. A crash can lose new data, but never leave a pointer to junk.
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.