Sub topic 17
Journaling and Crash Consistency
Assume you know processes, system calls, kernel mode, binary units, interrupts, and simple arithmetic. This chapter explains how a file system remains usable when power fails during an update.
Why crashes are dangerous
A file operation can require several disk writes. Creating a file may update the directory, inode, free-space map, and file data. A crash between these writes can leave structures disagreeing.
For example, a directory entry might point to an inode whose allocation is not recorded. The file system then contains an inconsistent state. A consistency checker may repair it, but repair can lose data or require a long scan.
Crash consistency is the property that the file system reaches a valid state after a crash. It does not necessarily mean that every recent application write survives.
Write-ahead journaling
A journal is a reserved disk area that records changes before the normal file-system locations are updated. A group of related changes is a transaction, which is an all-or-nothing unit of work.
A typical sequence is:
- Write the transaction's intended changes to the journal.
- Flush the journal data to stable storage.
- Write a commit record.
- Flush the commit record.
- Copy the changes to their normal locations.
- Mark the journal space reusable.
During recovery, the file system scans the journal. A transaction with a valid commit record can be replayed. An incomplete transaction is ignored or discarded.
Journaling modes and ordering
| Mode | What the journal records | Main trade-off |
|---|---|---|
| Metadata journaling | File-system metadata | Faster, but recent file data may be uncertain |
| Ordered journaling | Metadata, with data written first | Reduces stale-data exposure |
| Full data journaling | Metadata and file data | Stronger recovery, but more writes |
The commit record must not reach stable storage before the transaction's journal records. Otherwise recovery may try to replay missing information. The operating system uses flush operations, such as fsync, to request that data reach stable storage.
A journal protects file-system structure, not every application-level invariant. If an application updates two separate files, journaling does not automatically make the whole application operation atomic.
def recover(log):
"""Replay only transactions that contain a commit record."""
committed = set()
for record in log:
if record[0] == "COMMIT":
committed.add(record[1])
disk = {}
for record in log:
if record[0] == "WRITE" and record[1] in committed:
_, transaction_id, name, value = record
disk[name] = value
return disk
journal = [
("WRITE", 1, "inode_7", "allocated"),
("COMMIT", 1),
("WRITE", 2, "inode_8", "allocated"), # crash before commit
]
print(recover(journal)) # {'inode_7': 'allocated'}Exercise
Exercise
A journal contains WRITE records for transaction 4 but no COMMIT record. Should recovery replay those writes?
Show answer
No. Recovery treats transaction 4 as incomplete and discards it, unless a separate recovery protocol proves that it committed.