Crash consistency and journaling
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
Appending one block to a file means three separate disk writes: the data, the inode and the bitmap. If the power fails between them, the disk can end up contradicting itself. Follow one 4 KB append through a crash, the slow fsck repair, and then a journal: write the update to a log first, seal it with a commit block, copy it home, and replay it after a crash. The Linux lessons The file system and Disks, partitions and mounts show the user's view of the same disk, and the Databases lesson Write-ahead logging and recovery uses the same write-ahead idea.
One append, three blocks
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.
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
اقرأ الدرس خطوة بخطوة
One append, three blocks
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.
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 memoryA crash in the middle
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.
on disk after the crash: bitmap 00001000 block 5 free (old) inode 2 -> block 5 size 2 (new) block 5 junk (old) the bitmap and the inode disagree: inconsistent read the file and you get junk at the end two files could later be given block 5The slow fix: fsck
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.
fsck reads the whole disk: superblock, every inode, every bitmap, every directory mismatch inode 2 uses block 5, bitmap says free fix trust the inode: bitmap -> 00001100 consistent again, but: block 5 still holds junk (fsck cannot know) time grows with the disk: minutes or hours (OSTEP)Write a note first
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.
disk layout with a journal: journal TxB | I | B | D | TxE a reserved log area home bitmap, inode 2, block 5 the real places the rule (write-ahead): install none of the update in place until all of it is safely in the journal ext3 and ext4 on Linux keep their journal this way (jbd2)1. Journal write
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.
line 2 fsync(fd); blocks until the disk says done journal TxB I2 B2 Db . TxB begin: transaction id and where each block goes I2 B2 Db exact copies of the three new blocks (these four may be written together, in any order) home untouched: B1 I1 junk2. Journal commit
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.
wait TxB, I2, B2, Db are on disk then write TxE (same transaction id) TxE fits one 512-byte sector: the disk writes it all or nothing, so the commit cannot be half done journal TxB I2 B2 Db TxE committed fsync() can now return 03. Checkpoint, 4. free
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.
checkpoint I2 -> inode 2 B2 -> bitmap (00001100) Db -> block 5 free journal superblock: transaction done the protocol (OSTEP ch. 42): 1 journal write 2 journal commit 3 checkpoint 4 freeCrash before the commit
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.
crash after TxB I2 B2 Db (no TxE) reboot recovery scans the journal no commit block -> discard home B1 I1 junk all old, consistent the program never got fsync() == 0, so it was never told the data was safeCrash after: replay
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.
crash after TxE, and inode 2 checkpointed home then B1 I2 junk inconsistent! reboot TxB ... TxE found: committed replay I2, B2, Db to their homes home B2 I2 Db all new redo logging: replaying twice gives the same resultOrdered mode: data first
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.
ext4 data=ordered (the default): 1 Db -> block 5 data first, written once 2 journal TxB I2 B2 . TxE 3 checkpoint I2, B2 4 free every crash point, then recovery (Python, below): no journal: 4 crash points, 2 broken data journal: 10 crash points, 0 broken ordered: 9 crash points, 0 broken PLANS = { "no journal": ["home I", "home B", "home D"], "data journal": ["log TxB", "log I", "log B", "log D", "log TxE", "home I", "home B", "home D", "free"], "ordered": ["home D", "log TxB", "log I", "log B", "log TxE", "home I", "home B", "free"], } NEW = {"B": "B2", "I": "I2", "D": "Db"} def crash_after(plan, n): disk, log = {"B": "B1", "I": "I1", "D": "junk"}, [] for op in plan[:n]: # power fails after n writes where, x = op.split()[0], op.split()[-1] if where == "home": disk[x] = NEW[x] elif where == "log": log.append(x) else: log = [] # free the journal if "TxE" in log: # recovery: replay if committed for x in log[1:-1]: disk[x] = NEW[x] return disk def broken(d): return ((d["B"] == "B2") != (d["I"] == "I2") or (d["I"] == "I2" and d["D"] == "junk")) for name, plan in PLANS.items(): bad = sum(broken(crash_after(plan, n)) for n in range(len(plan) + 1)) print(f"{name}: {len(plan) + 1} crash points, {bad} broken")
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01Appending one 4 KB block to a file changes which blocks on disk?
The data goes in a new block, the inode must point to it and record the larger size, and the bitmap must mark the block as used. Three separate writes, and the disk does them one at a time.
02Power fails after only the new inode reaches the disk. What is wrong afterwards?
The inode and the bitmap now disagree, which is a file-system inconsistency, and reading the file returns whatever junk was in block 5 before. Later, block 5 could even be given to a second file.
03In the journaling protocol, when is a transaction committed?
The file system first writes TxB and the blocks to the journal, waits, and only then writes TxE. TxE fits in one 512-byte sector, which the disk writes completely or not at all, so the commit is atomic.
04After a crash, recovery finds TxB, I2, B2 and Db in the journal, but no TxE. What does it do?
Without a commit block the transaction might be incomplete, so it is discarded. Nothing was written home yet (the write-ahead rule), so the old blocks are still consistent.
05Why does ext4 default to ordered mode instead of journaling data too?
Most disk traffic is data, so writing it twice is expensive. Writing the data block before committing the metadata that points to it keeps the metadata consistent and means a pointer never points at junk.
المسرد
- Crash consistency
- Keeping a file system's on-disk structures in agreement with each other even if power fails in the middle of an update.
- Inode
- The on-disk record for one file: its size, owner and permissions, and pointers to its data blocks.
- Data bitmap
- One bit per data block on the disk: 1 means the block is in use, 0 means it is free.
- fsck
- The file system checker. After a crash it scans the whole disk and repairs mismatches; it is slow on large disks and cannot restore lost data.
- Journal
- A reserved area of the disk where a file system writes a description of an update before changing anything in its real place. Also called a write-ahead log.
- Commit block (TxE)
- The block that ends a transaction in the journal. Once it is on disk, the transaction counts as complete and will be replayed after a crash.
- Checkpoint
- Writing the blocks of a committed transaction from the journal's copy to their real (home) locations on disk.
- Ordered mode
- ext4's default (data=ordered): only metadata goes through the journal, and data blocks are written to their homes before the metadata that points to them is committed.
الملاحظات والمصادر
- 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.
- The card shows the two calls that matter. The full C program also opens the file and checks every return value; compiled and run, it appends 4096 bytes and returns 0.
- 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.
- Without fsync() the blocks may wait in memory for a while: ext4 commits its running transaction at least every 5 seconds by default (the commit= mount option).
- ext4 has three modes: data=ordered (the default, shown last), data=journal (everything through the journal, as in the middle of the lesson; deprecated, with removal planned for January 2028) and data=writeback (no ordering for data). All three keep the metadata consistent.
- fsync() on a file does not promise that the directory entry naming it is on disk; a newly created file also needs an fsync() on its directory.
- R. and A. Arpaci-Dusseau, Operating Systems: Three Easy Pieces, ch. 42: Crash Consistency: FSCK and Journaling
- Linux kernel documentation: ext4 (mount options and data modes)
- Linux kernel documentation: ext4 journal (jbd2)
- Linux man-pages: fsync(2)
- MIT 6.1810 (2024), lecture notes: Crash recovery, logging
اختبار يتاح بعد الخطوة الأخيرة.