ECE344 Fall 2026 (Sec 1) Lec 25 - Advanced Filesystems
Overview
Unix file-system updates can modify several blocks, so a crash may leave data, inodes, and allocation bitmaps inconsistent; fsck can repair metadata but must scan the whole file system, while fsync forces selected changes to storage. Journaling makes metadata updates recoverable through logged transactions, and copy-on-write systems such as Btrfs and ZFS avoid in-place updates, enabling fast snapshots and checksummed storage while introducing tradeoffs such as fragmentation.
Key takeaways
- A single file append can require separate writes to the data block, inode, and allocation bitmap, so a crash can produce leaks, dangling pointers, or inconsistent metadata.
- fsck repairs by scanning the full file system, whereas journaling limits crash recovery to committed transactions recorded in the log.
- For durable creation of a new file, fsync both the file and its containing directory; PostgreSQL's fsyncgate incident shows that treating fsync errors as harmless retries can lose data.
- Atomic file replacement requires writing and syncing a temporary file before renaming it over the original, because a journal does not combine multiple application calls into one transaction.
- Copy-on-write root switching supports crash-safe updates and cheap snapshots, but repeated in-place changes become new blocks and can fragment large files.
- Checksums detect silent storage corruption, while replication is needed to repair bad data; Btrfs combines checksums, duplicate metadata, RAID options, and background scrubbing.
Chapters
- Deleting a Unix file changes its directory entry, inode allocation state, and data-block allocation state, potentially across three disk blocks.
- A crash between deletion steps can leak storage or leave metadata inconsistent.
- Inodes combine direct pointers for small files with indirect pointers for larger files; directories map names to inode numbers.
- Appending a block can update the ext4 allocation bitmap, inode, and data block, and the kernel may write them in any order.
- Writing only the inode can point a file at an uninitialized block that remains marked free; writing only the bitmap can leak a block.
- fsck scans inodes and directories to rebuild bitmaps and check link counts, placing unreferenced inodes in lost+found.
- A full fsck takes time proportional to the file system size and can restore metadata consistency without recovering missing or incorrect file data.
- A 64 MB regular file can serve as a safe ext4 test image, created with truncate and initialized with mkfs.
- debugfs can inspect the image; the root directory is inode 2, lost+found is commonly inode 11, and the example file used inode 13.
- debugfs -w unlink removes a directory entry without deleting the inode or its data, simulating a partial update.
- e2fsck -F -N checks the image without applying repairs and reports the detached inode.
- A successful write usually means bytes reached the kernel page cache, not the storage device; Linux commonly writes dirty pages after about 30 seconds.
- fsync waits for a file's data and inode to reach the device, while creating a durable new filename also requires syncing its directory.
- PostgreSQL's 2018 fsyncgate incident involved failed fsync calls being retried after the Linux kernel had marked dirty pages clean, risking silent data loss.
- PostgreSQL now treats fsync failure as fatal on older kernels rather than assuming a retry will safely persist the data.
- A journal records a transaction's intended block changes before applying them to their home locations on disk.
- The sequence is a begin record, new block contents, a commit record, and then checkpointing the changes to their destinations.
- A crash before commit discards the incomplete transaction; a crash after commit replays the logged blocks, even if some were already checkpointed.
- Write ordering and flushes ensure the commit record cannot reach disk before the transaction data it certifies.
- ext4's journal mode logs data and metadata, while the default ordered mode writes data to its home before committing metadata.
- Writeback mode journals metadata without ordering data, so a file can expose stale data even when metadata remains consistent.
- Journaling does not combine separate application system calls into one logical transaction; a truncate followed by a write can leave an empty file after a crash.
- For atomic replacement, write and fsync a temporary file, rename it over the old file, then fsync the containing directory.
- A copy-on-write file system writes modified data to new blocks, then creates new copies of parent metadata blocks up to the root.
- Once the new tree is durable, switching the superblock to its new root makes the updated file system visible in one operation.
- A crash before the root switch leaves the old tree intact; a crash afterward exposes the complete new tree without journal replay.
- Btrfs and ZFS use copy-on-write structures, and retaining an old root creates a snapshot that shares unchanged blocks.
- Btrfs snapshots retain old tree roots and share unchanged blocks, so snapshot cost grows mainly as the old and new trees diverge.
- Subvolumes let administrators snapshot data separately, such as keeping a frequently changing system tree apart from a home directory.
- Reflink copies create independent inodes that initially share file blocks, making large copies nearly instantaneous and space-efficient.
- Coreutils cp can request reflinks; Btrfs, XFS, and ZFS support them, while ext4 does not.
- Btrfs and ZFS checksum data and metadata to detect silent corruption such as bit rot; ext4 checksums metadata but not ordinary file data.
- Btrfs can keep duplicate metadata copies and use checksums to identify a correct copy for repair; unreplicated data corruption is detectable but not recoverable.
- Btrfs supports separate RAID layouts for data and metadata and can scrub blocks in the background to verify checksums and repair available copies.
- Copy-on-write can fragment frequently updated files and increase write amplification; disabling it with the +C attribute also disables its checksum benefits.
- A log-structured file system appends updates sequentially and uses an inode map to find the latest inode versions.
- A cleaner relocates live blocks from mostly obsolete segments so entire segments can be reclaimed.
- F2FS aligns log-structured writes with flash storage behavior, where pages must be erased in blocks before reuse; Android devices use F2FS.
- TRIM tells an SSD which discarded blocks it can erase, complementing the file system's cleanup process.
Transcript excerpt
1:44 All right. So crashing. Woo. So crash can leave your file system inconsistent. To actually delete a file, now you can imagine there's a lot of steps knowing that, you know, there's a directory has a name to an inode, then the inode points to data blocks. There's a whole lot of stuff going on. So, deleting a file on a Unix file system actually takes three steps. So, I have to remove its directory entry, which means I'm changing the contents of the directory. I'm removing, you know, that name inode pair. Then there's also, whoops, an also a pool of inodes on your disk. So, they're allocated in giant pool. So, I have to say, hey, I'm not using that inode anymore. I have no more references
2:32 to it. So, there's a pool of free inodes. And then there's also something keeping track of the pool of free blocks. So, I'm not using that data anymore. Whatever data it was using, I can actually mark them unused now. So, I have to mark them actually unused somewhere. And then it knows that I can use those blocks for data for other files. So, three-step process that gets changed in, well, up to three different disk blocks, right? And a crash can happen between any of these or you could someone could pull the power out or the hard drive could die, although you probably have a bigger problem at that point. So, we'd get in weird because a crash between any two of them leaks some storage.
3:20 There is something called a journal that records operations in progress, like groups these all together because it's one logical operation. And that's how we can recover from a crash, which you'll see in the next lecture, which is immediately now. So,
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Jon Eyolfson.