8. Filesystem internals
One-liner. VFS gives one interface; the filesystem decides how blocks, metadata and crash safety work.
Memorize
- VFS layer: inode, dentry, file, superblock objects; each filesystem implements operations on them.
- Journaling (ext4): metadata (and optionally data) writes to a journal first, then to final locations — crash recovery replays it.
- COW (btrfs/ZFS): never overwrite live blocks; snapshots are cheap; fragmentation is the cost.
- fsync flushes data + metadata for that file; the directory must also be synced for a new name to be durable.
- Write barriers stop the device reordering flushes; disabling them can corrupt on power loss.
mount -o noatimereduces metadata writes;discard/fstrimmatters for SSDs.
Trap. Assuming fsync on a new file makes it appear after a crash — the directory entry is separate. Do this. fsync(file) then fsync(dir) when creating durable files.
Check
Why must the parent directory be synced after creating a file, if the file itself was fsynced?
Why
A crash can leave the inode allocated but unreachable if the directory update was never flushed.