Course contentsShow
← Computer Science
Lesson 1406 of 2,87232. File SystemsFree lesson

File System Consistency and Journaling

Understand crash consistency problems and how journaling logs operations before committing changes.

File System Consistency and Journaling

What you'll learn: How file systems protect against corruption when crashes occur mid-operation, and how journaling prevents inconsistency.


The Crash Consistency Problem

When you save a file, the file system must update multiple data structures: the data blocks themselves, the inode metadata, and the free space bitmaps. These updates aren't atomic—they happen as separate writes.

The danger: If your system crashes after writing only some of these updates, your file system becomes inconsistent. Imagine updating an inode to point to a new data block but crashing before marking that block as allocated in the bitmap. Now two files might claim the same block, or "allocated" blocks appear free!

Without protection, you'd need to run a full file system checker (fsck) after every crash—scanning every inode and bitmap to rebuild consistency. This takes hours on large disks.

Journaling to the Rescue

Journaling (or write-ahead logging) solves this by adding a special log area on disk. Think of it like a notebook where you write down your plans before doing the actual work.

How it works:

  1. Write intent to journal: Before modifying the file system, write a description of the planned changes to the journal (e.g., "allocate block 5047, update inode 892, modify bitmap")
  2. Mark journal entry complete: Once all changes are logged, write a commit marker
  3. Checkpoint: Actually perform the file system updates
  4. Clear journal: Mark the journal entry as processed

If a crash happens before the commit marker, you discard the incomplete log entry—nothing changed yet. If it happens after, you replay the journal on reboot to complete the interrupted operation. Recovery takes seconds, not hours.

Key Takeaway: Journaling logs intended file system operations to a special area before committing them, ensuring that crashes leave the file system in a consistent state that can be quickly recovered by replaying or discarding incomplete transactions.