Operating Systems Module 7: Storage and files Lesson 7.5 14 min

Crashes and journaling

One file operation is several block writes, and a power cut between them corrupts the disk. See what fsck does about it, and how a journal makes the whole operation all or nothing.

You save a file, and at that instant the power goes. When the machine comes back, what do you find? The old file, the new one, half of each, or a disk that refuses to mount?

The honest answer is that it depends on exactly when the power went. This lesson shows why, with a simulator that can cut the power at any moment, and then shows the trick that file systems use to make the answer always the same.

One operation, many writes

In Lesson 7.4 you saw that creating a file takes several block writes: the data, the inode, the bitmap and the directory entry. The disk can only write one block at a time and it can lose power between any two writes, so a crash can leave the disk with some of the writes done and the rest not. A disk in that state is inconsistent: its structures disagree with each other.

First, a create of a one-block file with nothing going wrong. Count the writes.

A create with no crash: four writes, in order

  1. Find where the name goes The kernel reads the directory /home/ana (6 block reads) to find the inode and check the name. Then it works out every block it must write.
    Operation
    create /home/ana/todo.txt 4K
    Block reads
    6
    Disk consistent
    yes
  2. Plan: 4 block writes One create is not one write. It is 4 separate block writes, done in this order: data block 1 of the new file; the new inode 15: size 4K and its block pointers; the free-space bitmap: mark block 20 as used; the directory block: add the entry "todo.txt" pointing to inode 15. The disk can lose power between any two of them. With no journal, the disk holds a half-done change at every point in between.
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    0 of 4
    Journal
    off
    Disk consistent
    yes
  3. Write 1 of 4: data block Write data block 1 of the new file.
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    1 of 4
    Journal
    off
    Disk consistent
    yes
  4. Write 2 of 4: inode Write the new inode 15: size 4K and its block pointers. Right now the disk is inconsistent: inode 15 is in use, but no directory entry names it.
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    2 of 4
    Journal
    off
    Disk consistent
    no (2 problems)
  5. Write 3 of 4: bitmap Write the free-space bitmap: mark block 20 as used. Right now the disk is inconsistent: inode 15 is in use, but no directory entry names it.
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    3 of 4
    Journal
    off
    Disk consistent
    no (1 problem)
  6. Write 4 of 4: directory block Write the directory block: add the entry "todo.txt" pointing to inode 15.
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    4 of 4
    Journal
    off
    Disk consistent
    yes
  7. Done: create /home/ana/todo.txt 4K All 4 block writes finished.
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    4 of 4
    Disk consistent
    yes

Four writes, and after the last one everything agrees again. But between the writes, the disk is briefly inconsistent: the fields panel shows it at write 2. Now cut the power inside that gap.

Run this create with the power failing after write 2. The writes go in this order: the data block, the inode, the bitmap, the directory. So write 2 is the inode, and the bitmap and the directory have not been written yet. Switch to Blocks to see the damage. A block with a red frame is one an inode uses but that the bitmap says is free.

Power fails after the inode write, with no journal

  1. Find where the name goes The kernel reads the directory /home/ana (6 block reads) to find the inode and check the name. Then it works out every block it must write.
    Operation
    create /home/ana/todo.txt 4K
    Block reads
    6
    Disk consistent
    yes
  2. Plan: 4 block writes One create is not one write. It is 4 separate block writes, done in this order: data block 1 of the new file; the new inode 15: size 4K and its block pointers; the free-space bitmap: mark block 20 as used; the directory block: add the entry "todo.txt" pointing to inode 15. The disk can lose power between any two of them. With no journal, the disk holds a half-done change at every point in between.
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    0 of 4
    Journal
    off
    Disk consistent
    yes
  3. Write 1 of 4: data block Write data block 1 of the new file.
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    1 of 4
    Journal
    off
    Disk consistent
    yes
  4. Write 2 of 4: inode Write the new inode 15: size 4K and its block pointers. Right now the disk is inconsistent: inode 15 is in use, but no directory entry names it.
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    2 of 4
    Journal
    off
    Disk consistent
    no (2 problems)
  5. Power fails after write 2 The power fails after write 2 of 4. Whatever was in memory is gone. The disk holds what was written so far, and only that. What is on the disk is inconsistent: inode 15 is in use, but no directory entry names it; inode 15 uses block 20, but the bitmap says block 20 is free.
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    2 of 4
    Journal
    off
    Disk consistent
    no (2 problems)
  6. Reboot: the disk is mounted as it is Without a journal and without fsck, nothing checks the disk. The machine mounts it and carries on, with the damage in place: it will cause trouble later.
    Operation
    reboot
    Disk consistent
    no (2 problems)

The disk is inconsistent: two problems, both found by comparing the structures with each other. Nothing in the file system is going to repair them. The machine reboots and mounts the disk as it is, and the damage waits. The most dangerous one is the bitmap: the block is in use but marked free, so a later file can be given the same block, and two files would then overwrite each other’s data.

Other crash points do different damage. A crash after write 1 (the data block only) is harmless: nothing points to the block. After write 3, the inode and the bitmap are written but the directory is not, which leaves an inode that no name points to, so its blocks are wasted. The order of the writes decides which damage you get, but whichever order you choose, some crash point leaves a mess. Press Try it and change the crash point to see each case.

fsck: check everything

The classic fix is a repair program, run at boot after a crash: fsck (file system check). It knows what a consistent file system looks like, so it reads the structures, compares them, and fixes whatever disagrees. It cannot know what the program meant to write, only make the structures agree. Here is the same crash, with fsck running at the next boot.

The same crash, and fsck at the next boot

  1. Find where the name goes The kernel reads the directory /home/ana (6 block reads) to find the inode and check the name. Then it works out every block it must write.
    Operation
    create /home/ana/todo.txt 4K
    Block reads
    6
    Disk consistent
    yes
  2. Plan: 4 block writes One create is not one write. It is 4 separate block writes, done in this order: data block 1 of the new file; the new inode 15: size 4K and its block pointers; the free-space bitmap: mark block 20 as used; the directory block: add the entry "todo.txt" pointing to inode 15. The disk can lose power between any two of them. With no journal, the disk holds a half-done change at every point in between.
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    0 of 4
    Journal
    off
    Disk consistent
    yes
  3. Write 1 of 4: data block Write data block 1 of the new file.
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    1 of 4
    Journal
    off
    Disk consistent
    yes
  4. Write 2 of 4: inode Write the new inode 15: size 4K and its block pointers. Right now the disk is inconsistent: inode 15 is in use, but no directory entry names it.
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    2 of 4
    Journal
    off
    Disk consistent
    no (2 problems)
  5. Power fails after write 2 The power fails after write 2 of 4. Whatever was in memory is gone. The disk holds what was written so far, and only that. What is on the disk is inconsistent: inode 15 is in use, but no directory entry names it; inode 15 uses block 20, but the bitmap says block 20 is free.
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    2 of 4
    Journal
    off
    Disk consistent
    no (2 problems)
  6. Reboot: fsck scans the whole disk With no journal, the kernel does not know which operation was interrupted. fsck has to read every inode, every directory and every indirect block to find out, and compare them with the bitmap. That is 10 block reads on this tiny disk, and it grows with the size of the disk. It finds: inode 15 is in use, but no directory entry names it; inode 15 uses block 20, but the bitmap says block 20 is free.
    Operation
    fsck
    Block reads
    10
    Disk consistent
    no (2 problems)
  7. fsck repairs the disk fsck makes the disk consistent again: give inode 15 a name, /lost+found/#15; mark block 20 as used. It cannot know what the program meant to write, only make the structures agree. The new file may be gone, or empty, or sitting in /lost+found.
    Operation
    fsck
    Block reads
    10
    Disk consistent
    yes

fsck found both problems and repaired them: it marked the block as used, and gave the nameless inode a name in /lost+found. The disk is consistent again, and the half-written file is not lost, but it is not where the program expected it either.

The cost is the problem. fsck read 10 blocks on this tiny disk. It has to read every inode, every directory and every indirect block, so its time grows with the size of the disk and the number of files, not with the size of the damage. On a large disk this takes long enough that, before journaling, a machine could spend a long time after every power cut just checking its disks.

The journal

The fix is an old idea from databases (a write-ahead log). Before changing anything in the real structures, write down what you are about to do, in a separate area called the journal. Then there is a clear point at which the operation is either promised or not.

With the journal on, one create goes like this (in the common “ordered” mode, which writes the file’s data first):

  1. Write the data blocks in their real place.
  2. Write a journal header, saying which blocks are about to change, then a copy of each new metadata block (inode, bitmap, directory). Nothing in the real file system has changed yet.
  3. Write the commit record. From this write on, the operation is promised: it will happen, even after a crash.
  4. Write the metadata blocks in their real place.
  5. Mark the transaction finished in the journal.

Step through it, and watch the purple journal blocks fill up and clear.

One create with a journal: ten writes

  1. Find where the name goes The kernel reads the directory /home/ana (6 block reads) to find the inode and check the name. Then it works out every block it must write.
    Operation
    create /home/ana/todo.txt 4K
    Block reads
    6
    Disk consistent
    yes
  2. Plan: 10 block writes One create is not one write. It is 10 separate block writes, done in this order: data block 1 of the new file; the journal: a header listing the 3 blocks that are about to change; the journal: a copy of the new inode block (block 2); the journal: a copy of the new bitmap (block 1); the journal: a copy of the new directory block (block 17); the journal: the commit record. From now on the change is promised: after a crash it will be finished; the new inode 15: size 4K and its block pointers (now in its real place); the free-space bitmap: mark block 20 as used (now in its real place); the directory block: add the entry "todo.txt" pointing to inode 15 (now in its real place); the journal: mark the transaction finished, so it is never replayed. The disk can lose power between any two of them.
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    0 of 10
    Journal
    on
    Disk consistent
    yes
  3. Write 1 of 10: data block Write data block 1 of the new file.
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    1 of 10
    Journal
    on
    Disk consistent
    yes
  4. Write 2 of 10: journal header Write the journal: a header listing the 3 blocks that are about to change. Nothing in the real file system has changed yet.
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    2 of 10
    Journal
    on
    Disk consistent
    yes
  5. Write 3 of 10: journal copy of the inode block Write the journal: a copy of the new inode block (block 2). Nothing in the real file system has changed yet.
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    3 of 10
    Journal
    on
    Disk consistent
    yes
  6. Write 4 of 10: journal copy of the bitmap Write the journal: a copy of the new bitmap (block 1). Nothing in the real file system has changed yet.
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    4 of 10
    Journal
    on
    Disk consistent
    yes
  7. Write 5 of 10: journal copy of the directory block Write the journal: a copy of the new directory block (block 17). Nothing in the real file system has changed yet.
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    5 of 10
    Journal
    on
    Disk consistent
    yes
  8. Write 6 of 10: journal commit Write the journal: the commit record. From now on the change is promised: after a crash it will be finished.
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    6 of 10
    Journal
    on
    Disk consistent
    yes
  9. Write 7 of 10: inode (in place) Write the new inode 15: size 4K and its block pointers (now in its real place).
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    7 of 10
    Journal
    on
    Disk consistent
    no (2 problems)
  10. Write 8 of 10: bitmap (in place) Write the free-space bitmap: mark block 20 as used (now in its real place).
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    8 of 10
    Journal
    on
    Disk consistent
    no (1 problem)
  11. Write 9 of 10: directory block (in place) Write the directory block: add the entry "todo.txt" pointing to inode 15 (now in its real place).
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    9 of 10
    Journal
    on
    Disk consistent
    yes
  12. Write 10 of 10: journal cleared Write the journal: mark the transaction finished, so it is never replayed.
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    10 of 10
    Journal
    on
    Disk consistent
    yes
  13. Done: create /home/ana/todo.txt 4K All 10 block writes finished. The journal cost extra writes: every metadata block was written twice.
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    10 of 10
    Disk consistent
    yes

A journalled create took 10 block writes instead of 4, because every metadata block is written twice, once to the journal and once in place. That is the price: a little extra I/O in return for never needing a full scan.

Crash before the commit

Now cut the power after write 4: the data, the header and one copy of a block are written, but not the commit.

Crash before the commit: the half-written transaction is thrown away

  1. Find where the name goes The kernel reads the directory /home/ana (6 block reads) to find the inode and check the name. Then it works out every block it must write.
    Operation
    create /home/ana/todo.txt 4K
    Block reads
    6
    Disk consistent
    yes
  2. Plan: 10 block writes One create is not one write. It is 10 separate block writes, done in this order: data block 1 of the new file; the journal: a header listing the 3 blocks that are about to change; the journal: a copy of the new inode block (block 2); the journal: a copy of the new bitmap (block 1); the journal: a copy of the new directory block (block 17); the journal: the commit record. From now on the change is promised: after a crash it will be finished; the new inode 15: size 4K and its block pointers (now in its real place); the free-space bitmap: mark block 20 as used (now in its real place); the directory block: add the entry "todo.txt" pointing to inode 15 (now in its real place); the journal: mark the transaction finished, so it is never replayed. The disk can lose power between any two of them.
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    0 of 10
    Journal
    on
    Disk consistent
    yes
  3. Write 1 of 10: data block Write data block 1 of the new file.
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    1 of 10
    Journal
    on
    Disk consistent
    yes
  4. Write 2 of 10: journal header Write the journal: a header listing the 3 blocks that are about to change. Nothing in the real file system has changed yet.
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    2 of 10
    Journal
    on
    Disk consistent
    yes
  5. Write 3 of 10: journal copy of the inode block Write the journal: a copy of the new inode block (block 2). Nothing in the real file system has changed yet.
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    3 of 10
    Journal
    on
    Disk consistent
    yes
  6. Write 4 of 10: journal copy of the bitmap Write the journal: a copy of the new bitmap (block 1). Nothing in the real file system has changed yet.
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    4 of 10
    Journal
    on
    Disk consistent
    yes
  7. Power fails after write 4 The power fails after write 4 of 10. Whatever was in memory is gone. The disk holds what was written so far, and only that. This particular crash point left the disk consistent or still recoverable.
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    4 of 10
    Journal
    on
    Disk consistent
    yes
  8. Reboot: read the journal The journal holds a header and 2 block copies, but no commit record. The change was never promised, so the kernel throws the half-written transaction away.
    Operation
    recovery
    Block reads
    3
    Disk consistent
    yes
  9. The file system is consistent The disk is consistent again, and no scan of the disk was needed. The journal let the kernel decide in a moment: the operation did not happen at all. There is no state in between.
    Operation
    recovery
    Disk consistent
    yes

The file system never saw the operation. Its structures were not touched, so the disk is consistent and the new file does not exist, as if you had never pressed save. The kernel took one look at the journal and decided, without scanning anything.

Crash after the commit

Now cut the power after write 7: the commit record is written, the inode is in its real place, but the bitmap and directory are not. This is the very crash that corrupted the disk without a journal.

Crash after the commit: the journal is replayed

  1. Find where the name goes The kernel reads the directory /home/ana (6 block reads) to find the inode and check the name. Then it works out every block it must write.
    Operation
    create /home/ana/todo.txt 4K
    Block reads
    6
    Disk consistent
    yes
  2. Plan: 10 block writes One create is not one write. It is 10 separate block writes, done in this order: data block 1 of the new file; the journal: a header listing the 3 blocks that are about to change; the journal: a copy of the new inode block (block 2); the journal: a copy of the new bitmap (block 1); the journal: a copy of the new directory block (block 17); the journal: the commit record. From now on the change is promised: after a crash it will be finished; the new inode 15: size 4K and its block pointers (now in its real place); the free-space bitmap: mark block 20 as used (now in its real place); the directory block: add the entry "todo.txt" pointing to inode 15 (now in its real place); the journal: mark the transaction finished, so it is never replayed. The disk can lose power between any two of them.
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    0 of 10
    Journal
    on
    Disk consistent
    yes
  3. Write 1 of 10: data block Write data block 1 of the new file.
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    1 of 10
    Journal
    on
    Disk consistent
    yes
  4. Write 2 of 10: journal header Write the journal: a header listing the 3 blocks that are about to change. Nothing in the real file system has changed yet.
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    2 of 10
    Journal
    on
    Disk consistent
    yes
  5. Write 3 of 10: journal copy of the inode block Write the journal: a copy of the new inode block (block 2). Nothing in the real file system has changed yet.
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    3 of 10
    Journal
    on
    Disk consistent
    yes
  6. Write 4 of 10: journal copy of the bitmap Write the journal: a copy of the new bitmap (block 1). Nothing in the real file system has changed yet.
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    4 of 10
    Journal
    on
    Disk consistent
    yes
  7. Write 5 of 10: journal copy of the directory block Write the journal: a copy of the new directory block (block 17). Nothing in the real file system has changed yet.
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    5 of 10
    Journal
    on
    Disk consistent
    yes
  8. Write 6 of 10: journal commit Write the journal: the commit record. From now on the change is promised: after a crash it will be finished.
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    6 of 10
    Journal
    on
    Disk consistent
    yes
  9. Write 7 of 10: inode (in place) Write the new inode 15: size 4K and its block pointers (now in its real place).
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    7 of 10
    Journal
    on
    Disk consistent
    no (2 problems)
  10. Power fails after write 7 The power fails after write 7 of 10. Whatever was in memory is gone. The disk holds what was written so far, and only that. What is on the disk is inconsistent: inode 15 is in use, but no directory entry names it; inode 15 uses block 20, but the bitmap says block 20 is free.
    Operation
    create /home/ana/todo.txt 4K
    Block writes
    7 of 10
    Journal
    on
    Disk consistent
    no (2 problems)
  11. Reboot: read the journal The journal holds a header, 3 block copies and a commit record. The commit means the change was promised, so the kernel will finish it.
    Operation
    recovery
    Block reads
    5
    Disk consistent
    no (2 problems)
  12. Replay the committed transaction The kernel copies every logged block to its real place, and clears the journal. Writing a block that was already written is harmless, so it does not matter how far the original writes got. The change is now complete.
    Operation
    recovery
    Block writes
    3
    Disk consistent
    yes
  13. The file system is consistent The disk is consistent again, and no scan of the disk was needed. The journal let the kernel decide in a moment: the operation happened completely. There is no state in between.
    Operation
    recovery
    Disk consistent
    yes

The journal replay repaired the damage in one short step, and the file exists, completely. The same crash point, the same torn disk, and a different outcome from the journal-less case: there, fsck had to scan everything and the new file ended up in /lost+found. Together, the two journal runs show the rule: after a crash with a journal, the operation either happened completely or not at all. There is no state in between.

Press Try it, turn the journal on, and try every crash point from 0 to 10. After recovery the disk is consistent every time, and the file exists only if the crash came after the commit record.

What a journal does and does not promise

A journal protects the file system’s structures: the inodes, bitmaps and directories. It makes sure they are never left disagreeing. It does not, by default, protect the contents of your file. In ext4’s default (“ordered”) mode, the data blocks are written before the metadata is committed, so a committed file never points at blocks that hold garbage. In “writeback” mode there is no such ordering, which is faster and weaker. In “journal” mode, the data goes through the journal too, which is the safest and slowest, since every byte is written twice.

There is a second gap, and it is the one that catches application writers. When a program calls write, the kernel normally keeps the data in memory and writes it to disk a little later. A crash in the meantime loses it, journal or no journal. The call that closes this gap is fsync, which blocks until the data (and the metadata it needs) are really on disk. A database does exactly what a file system does: it writes its change to its own log, calls fsync on the log so the change is promised, and only then updates its data files. That log is the same idea as the journal, and it is called a write-ahead log. A program that writes a file, calls rename to move it into place and never calls fsync can lose the file in a crash, even on a journalling file system.

In the real world

Most Linux file systems have a journal: ext3 and ext4, XFS and others. Windows’ NTFS has one too. You can ask a mounted ext4 file system about its journal and its mode:

Terminal
$ mount | grep ext4
$ sudo dumpe2fs -h /dev/sda1 | grep -i journal
$ sudo tune2fs -l /dev/sda1 | grep -i features

The mount options show data=ordered unless somebody changed it, and the journal lines show its size and features. Some file systems, such as ZFS and Btrfs, use a different trick called copy-on-write: they never overwrite a block in place, so the old version stays valid until a single pointer is switched to the new one. It is another way of making an update all or nothing. :::

Quick check

Quick check

  1. Why can a crash corrupt a file system that has no journal?
  2. Why does fsck take so long on a big disk?
  3. What is the job of the commit record in a journal?
  4. A program writes a file and the power fails a moment later, before the kernel has written it out. Does the journal bring the data back?

Review: what you learned earlier

Coming back to an idea after a break is what makes it stick. These questions come from earlier modules, starting with any you missed. Answer from memory before you look anything up.

Spaced review

This review picks questions for you, so it needs JavaScript.

Module 7 recap

Devices have controllers with registers, and the kernel reaches them through drivers. The CPU can find out that data is ready by polling, by an interrupt, or, for bulk data, by handing the transfer to a DMA controller that interrupts once at the end. On a spinning disk the cost is head movement, so the kernel orders requests: SSTF is greedy and can starve, SCAN and LOOK sweep like an elevator, and the circular versions are fairer. SSDs have no head, so most of that matters less. A file is a named stream of bytes behind a descriptor, and the descriptor, open file and inode tables explain why a forked child shares its parent’s position. On disk, a file system is a superblock, a bitmap, an inode table and data blocks, where directories map names to inodes and big files use indirect blocks. And because one operation is many writes, a crash can leave the structures disagreeing: fsck repairs them by reading everything, and a journal prevents the damage by making each operation all or nothing.

Next, Module 8: you have followed a program from a system call down to a disk. Now you will see how whole operating systems and containers are run on one machine, and then use everything you know to find out why a real server is slow.

Your progress is saved in this browser. Sign in to keep it on every device. Free, no password.

Join the discussion