Skip to main content

Lesson 21 of 26 · 12 min read

Disk locality: Fast File System and Log-Structured FS

Compare how BSD FFS organizes cylinder groups for spatial locality against how Sprite LFS turns all disk writes into sequential log segments using an inode map.

Four file system locality designs: Cylinder groups, Sequential log, Inode map, and Segment cleaner.
FFS organizes metadata by cylinder group for spatial locality; LFS buffers writes into sequential log segments.
Course syllabus · lesson 21 of 26
In this lesson

VSFS treated a disk as an abstract array of blocks, ignoring physical seek and rotational delays completely. Early UNIX suffered terribly as a result: after weeks of file edits, free lists scattered blocks everywhere, and even reading a sequential file triggered constant disk seeks and obtaining only a small fraction of the drive's streaming throughput in the historical measurements described by OSTEP.

The crux of the problem is how to organize file system structures and allocation policies to reduce the time spent positioning the disk head — whether by placing related items together in cylinder groups (FFS) or turning every write into sequential segments (LFS).

Side-by-side comparison of FFS cylinder-group locality (grouping directories and files near each other, splitting huge files) and LFS sequential log buffering (CR -> imap -> inode -> data plus garbage-collecting segment cleaner).
Open full-size diagram
FFS makes in-place allocation disk-aware; LFS treats the disk itself as an append-only sequential log.

Compare FFS placement and LFS appends

FFS inode fixed slot, Block group nearby data, LFS imap —, Log tail —FFS inodefixed slotBlock groupnearby dataLFS imap—Log tail—

Step 1 of 3

Write in FFS

FFS tries to allocate file data near its inode in a cylinder group to reduce seeks on disk.

Design 1: BSD Fast File System (FFS) — Disk awareness

Led by Marshall Kirk McKusick at UC Berkeley, FFS partitioned the disk into contiguous cylinder groups (called block groups in Linux ext2/ext3/ext4). Every group carries its own superblock copy, inode bitmap, data bitmap, inode slice, and data slice — keeping a file's inode and data blocks physically adjacent!

Three clean allocation policies turn seek storms into local neighborhood trips:

  1. Directories: Pick the cylinder group with a low number of allocated directories and plenty of free inodes so directory trees spread evenly across the disk.
  2. Files: Place a new file's inode and its first data blocks inside the same cylinder group as its parent directory! Because users frequently access files inside the same folder (src/*.c, /etc/*), temporal and spatial locality line up neatly.
  3. The Large-File Exception: What happens if a 2 GB movie lands in /home/alice and fills the entire cylinder group, forcing future small files elsewhere? Once a file fills its direct blocks, FFS moves subsequent large chunks across different cylinder groups (e.g., 1 MB or indirect-block spans) — amortizing a tiny seek across megabytes while keeping small files clustered near home!

FFS also pioneered parameterization (track skew), 512-byte sub-blocks inside 4 KiB blocks (avoiding huge internal waste on 1 KiB files), long filenames, and atomic symlinks — proving that usability matters alongside raw performance (TIP: MAKE THE SYSTEM USABLE).

Allocate two files and account for the seek cost

Imagine group 2 contains the inode for /src and has 60 free data blocks. Creating /src/main.c uses one of that group's free inode slots and stores its first two blocks there. Opening the file cold still requires reading the directory and inode blocks, but those reads and the first payload read can be physically close. If you create /src/video and write 2 MiB, it needs 512 blocks at 4 KiB each. Put its first 12 direct blocks in group 2. In this model the next indirect span goes to a group with room for all remaining 500 payload blocks and its indirect block. Group 2 retains 60 - 2 - 12 = 46 free data blocks for related files, assuming the directory entry fits in an already allocated block. A policy that fills the parent group first would leave it no free data blocks.

The sequential reader pays for changing groups. Suppose positioning costs 10 ms per chunk and the platter streams at 40 decimal MB/s. A 400,000-byte chunk transfers in 10 ms, so half the service time moves data and throughput is 20 MB/s. To spend 90% of the time transferring, allow 90 ms of transfer for each 10 ms of positioning. That takes 3.6 MB per chunk. An indirect span of 1,024 blocks at 4 KiB each gives a contiguous 4 MiB chunk, enough to exceed that target under these assumptions. Fragmentation within a group can add positioning events, so group membership alone provides only a bound on placement policy, not a prediction of contiguity.

A rotating disk also makes block-size decisions visible. With 512-byte fragments and 4 KiB blocks, a file of 1,300 bytes can occupy three fragments (1,536 bytes) and leave 236 bytes unused at the end. Reserving an entire 4 KiB block would leave 2,796 bytes unused. When a file grows, the allocator may need to move that fragment-sized tail into a larger allocation. The layout trades some allocator work for less wasted space on small files.

Design 2: Log-Structured File Systems (LFS)

By the early 1990s, RAM caches absorbed most reads, making small random writes the dominant bottleneck (RAID-5 small-write penalties made random updates hurt even more). Mendel Rosenblum and John Ousterhout asked a radical question: what if the file system never overwrites blocks in place, and converts EVERY update — data, inodes, directories — into sequential writes?

Instead of hunting down scattered home slots, LFS batches dirty writes in RAM, bundles them into a multi-megabyte segment (Segment Summary + Data + Inodes + Imap pieces), and flushes the entire segment sequentially in one fast disk sweep!

How LFS finds moving inodes: The imap and Checkpoint Region (CR)

Wait — if LFS never overwrites an inode at a fixed disk address, how does open() find the latest version of inode k? With one level of indirection: the Inode Map (imap)!

  • Whenever LFS writes a new segment containing updated inode k at disk address A2, it appends an updated chunk of imap pointing k -> A2 in the very same segment.
  • A tiny fixed Checkpoint Region (CR) at both ends of the disk records the disk addresses of checkpointed imap pieces along with log state. LFS writes the two checkpoint copies alternately. On mount, the OS reads the CR, loads the entire imap into RAM, and translates any inode lookup (inum -> disk address) in memory with zero disk seeks!
Code exampleTrace
LFS Read Path:
  RAM: CR (on mount) -> imap[inum] -> disk_addr(inode)
  Disk read #1: inode block at disk_addr -> read direct[offset]
  Disk read #2: data block at direct[offset]

Follow an overwrite through a segment and checkpoint

Start with imap[17] = 101. The inode at 101 points to disk block 100 for file block 0 and to block 107 for file block 1. A process updates file block 0. The segment buffer packs the new payload at 200, an inode at 201 with pointers 200 and 107, and a new copy of its inode-map piece at 202 with entry 17 -> 201. The segment summary identifies the new data as file 17, file block 0. File block 1 stays live at 107. A directory entry for this file continues to carry inode number 17, so changing the inode's location requires zero changes to that directory entry.

The checkpoint is the stable way to discover map pieces at mount. If the last complete checkpoint still points to the old map piece, recovery can use the log position in that checkpoint and scan forward to discover valid later updates. An interrupted segment can leave incomplete records at the tail. Recovery must validate the records it applies. To survive an interrupted checkpoint write, LFS can retain the older copy and choose the newest complete copy, using matching timestamp records as completion markers in the OSTEP example.

Segment size amortizes positioning in the same way as FFS chunk size. At 40 MB/s with 10 ms positioning, packing 4 decimal MB into a sequential write gives 4 MB / 0.110 s = 36.4 MB/s of gross segment throughput. Summary blocks, inode blocks, and future cleaning consume some of that bandwidth. Batching can also delay writes that arrive just after a flush, and a durability request can force a smaller batch.

The price of logs: Segment cleaning (Garbage Collection)

Because LFS writes updated blocks to new disk locations, older overwritten versions stay behind as dead garbage inside older segments. Eventually the disk runs out of clean contiguous segments!

An idle background LFS cleaner reads M partially filled old segments into memory, checks liveness using each segment's Segment Summary block (comparing (inum, offset) back through the current imap/inode to see if that copy is still live!), copies only live blocks out sequentially into N (< M) compacted segments, and frees the old segments for fresh writes. Choosing when and which segments to clean balances read/write overhead — hot-modifying files belong in their own segments so dead versions drop cleanly while cold static blocks stay grouped untouched!

In the overwrite example the summary of the old segment says block 100 belongs to inode 17 at file offset zero. Resolve 17 via the current map to inode block 201 and compare its first direct pointer with 100. It holds 200, so block 100 is dead. The same check for block 107 finds a match in the second direct slot, so 107 must be copied before reclaiming its segment. An inode version in the map and summary can reject stale entries after deletion or truncation without first reading the inode. A matching version still needs the address comparison. When the cleaner relocates live records it must publish the new references before it makes the old space reusable.

Suppose four 1 MiB victim segments are each 25% live. In a simplified model with no additional bookkeeping bytes, cleaning reads 4 MiB, writes 1 MiB of survivors into a clean segment, and frees four victims. It gains 3 MiB of reusable space net. Fill that space with new segments and the write traffic is (1 + 3) / 3 = 1.33 times the new bytes. Counting reads as well, total media traffic is (4 + 1 + 3) / 3 = 2.67 times the new bytes. These ratios assume enough spare space to hold the survivors while the original segments still exist. If free space runs too low, the cleaner may have to run synchronously and hold up new writes. If the same four segments were 75% live, cleaning would free only 1 MiB net and that traffic ratio would rise to (4 + 3 + 1) / 1 = 8. Grouping frequently updated data gives some victims time to become sparse. Consolidating long-lived data separately can help the cleaner leave it alone for longer.

Worked trace: read path in LFS vs. FFS

Compare reading block 0 of file /dir/a.txt after resolving its name to inum = 17. Start with an empty RAM data cache (imap already cached in memory):

  1. LFS lookup: Look up inum 17 directly in the in-memory imap -> get latest on-disk address disk_addr(inode 17) -> read inode 17 -> follow direct[0] -> read data block 0! (2 reads for this direct block.)
  2. FFS lookup: Compute cylinder group CG = inum / inodes_per_cg -> read inode 17 directly from that group's fixed inode table -> read nearby data block 0 in the same cylinder group (minimal seek!).
  3. Change one input: Suppose a workload repeatedly updates 4 KiB slices at random offsets inside 1,000 small metadata-heavy files on a spinning HDD. Which wins on raw write throughput — FFS or LFS? LFS can win the foreground write phase if it can batch enough updates into large clean segments. For sustained throughput, include relocated live blocks and victim reads. FFS can still benefit from coalescing cached updates and from the locality of files in the same group.

Next, inspect what happens when the power cord gets pulled right in the middle of those disk writes in crash consistency: fsck and write-ahead journaling.

Before you move on

Try it in your workspace

Compare FFS cylinder-group placement for a directory and small files vs. LFS sequential segment buffering with an inode map (imap) and checkpoint region (CR), noting when each wins.

Keep a short note of what you tried, what passed, and what you still need to check.

Your practice record

0 of 3 checked.

Saved in this browser when storage is available. Uncheck any item to revisit it. This is your own record, not an assessment.