Articles

What Is a Journaling File System? Crash Safety Explained

A journaling file system logs intended changes before applying them, so it can recover quickly and consistently after a crash or power loss. How it works.

Chisato Chisato · · 5 min read
Server racks with network cables

A journaling file system records the changes it is about to make in a dedicated log, called the journal, before applying them to their final locations on disk. If the machine crashes or loses power partway through, the file system replays or discards the journal entries on the next boot and returns to a consistent state in seconds, instead of scanning the entire disk for damage. Most mainstream file systems on servers, desktops, and phones use some form of journaling or a closely related technique.

The problem journaling solves

Even a simple file operation touches several places on disk. Appending to a file might require:

  1. Allocating a new data block and marking it used in the free-space bitmap.
  2. Writing the data into that block.
  3. Updating the file’s metadata record (the inode on Unix-like systems) with the new size and block pointer.

These writes go to different locations, and storage devices don’t guarantee they complete together. If power fails after step 1 but before step 3, the disk has a block marked as used that no file points to — leaked space. If step 3 lands but step 2 doesn’t, the file points to a block full of stale data from some previously deleted file. Other orderings can produce two files claiming the same block.

Older file systems handled this by running a full consistency checker (such as fsck) after an unclean shutdown. That tool walks every metadata structure on the volume, which on a large disk can take a very long time while the system stays offline.

How a journal works

Journaling borrows the idea behind a database’s write-ahead log:

  1. Write the intent. The file system writes a description of all the changes in an operation — a transaction — to the journal, a reserved area on disk.
  2. Commit. It writes a commit record marking the transaction complete, and makes sure it has reached stable storage.
  3. Checkpoint. It applies the changes to their real locations in the file system.
  4. Free the journal space. Once the changes are safely in place, that journal entry can be reused.

After a crash, recovery is simple. Any transaction with a commit record is replayed, which is safe to do more than once because replaying writes the same final values. Any transaction without a commit record is ignored, as if it never started. The volume ends up reflecting either all of an operation or none of it — the same atomicity guarantee covered in ACID transactions explained.

Journaling modes

Writing everything twice is expensive, so file systems usually let you choose what goes through the journal:

ModeWhat is journaledCrash consistencyPerformance
Journal (full data)Metadata and file contentsStrongest: metadata and data both consistentSlowest; all data written twice
OrderedMetadata only, but data is written before its metadata commitsMetadata consistent; no stale data exposedGood balance; a common default
WritebackMetadata only, no ordering for dataMetadata consistent; files may contain stale or garbage data after a crashFastest

In ordered mode, the file system forces a file’s data blocks to disk before committing the metadata that points to them. That prevents the worst case — a file pointing at someone else’s old data — without paying to journal the data itself.

What journaling does not guarantee

Journaling protects the file system’s own structures. It does not make your application’s data durable on its own. When a program calls write(), the data usually lands in the operating system’s page cache and reaches disk later. If the machine crashes before that happens, the write is lost, journal or not.

Applications that need durability — databases, message queues, anything holding transactions — must explicitly flush with fsync at the right moments. A common safe pattern for replacing a file is to write a new temporary file, fsync it, rename it over the original, and then fsync the containing directory so the rename itself is durable.

Journaling also cannot fix storage hardware that lies about having persisted data, or silent corruption of the bits themselves. Some file systems add checksums on data and metadata to detect the latter, and redundancy through RAID or replication to repair it.

Alternatives to journaling

Two other designs achieve crash consistency differently:

  • Copy-on-write file systems never overwrite data in place. They write modified blocks to new locations and then atomically switch a top-level pointer to the new tree. Old versions remain intact until freed, which also makes snapshots cheap. It’s the same principle as copy-on-write in memory management.
  • Log-structured file systems treat the entire disk as an append-only log. Every write goes to the end, and a background process reclaims space from obsolete entries. This design fits flash storage well, since flash cannot overwrite in place anyway — see how SSD wear leveling works.

Both trade journaling’s double write for different costs, such as fragmentation or garbage collection.

Practical implications

  • Fast reboots. After a power cut, a journaled volume typically mounts after a brief journal replay instead of a full scan.
  • Write amplification. Journaling adds extra writes, especially in full data mode. On high-throughput systems that is a real cost.
  • Databases double up. A database on a journaling file system logs its own changes too. That is normal: the file system protects its structures, the database protects its records.
  • Mount options matter. Choices about journaling mode and write barriers change both safety and speed, so they should be deliberate rather than copied from a forum post.

The takeaway

A journaling file system writes a record of each change to a log before applying it, so a crash leaves either a complete transaction to replay or an incomplete one to discard. That turns long consistency checks into quick recoveries and keeps metadata coherent. It does not make application writes durable — that still requires fsync — and copy-on-write and log-structured designs reach the same goal by other routes.

Chisato Chisato · · 6 min read

DVFS Explained: Dynamic Voltage and Frequency Scaling

DVFS lets a chip lower its voltage and clock speed when demand drops, cutting power sharply. How it works, why voltage matters most, and its trade-offs.

#Hardware #Chips #Computer Science
Chisato Chisato · · 5 min read

What Is CPU Microcode? The Layer Below Machine Code

CPU microcode is the internal firmware that translates complex instructions into simple hardware operations. How it works, why it gets updated, and the risks.

#Hardware #Chips #Computer Science