LFS writes everything sequentially β which creates a new problem: where did the inodes go?
43.4 The Problem: Finding Inodes
In FFS (or the old UNIX FS), inodes live in a fixed on-disk array, so you find
one by simple arithmetic: address = start + inode_number Γ inode_size. In
LFS, thatβs impossible β inodes are scattered all over the disk, and since
LFS never overwrites in place, an inodeβs newest version keeps moving with
every update.
43.5 The Inode Map (imap)
The fix is the classic one: a level of indirection. The inode map (imap) inode map The imap: LFS's level of indirection from inode number β the disk address of the most recent version of that inode. Since LFS scatters inodes across the disk and moves them on every write, the imap is how they're found; it's updated (and written) right alongside each segment. It also solves the recursive update problem. defined in ch. 43 β open in glossary maps an inode number to the disk address of that inodeβs most recent version β a simple array with a 4-byte pointer per entry, updated whenever an inode is written. To avoid extra seeks, LFS doesnβt keep the imap at a fixed spot; it writes each imap chunk right next to the data and inode it describes, in the same segment.
Tip: Use A Level Of Indirection
As Wheeler said, βall problems in computer science can be solved by another level of indirectionβ (except too many indirections). Virtual memory, files, and here the imap β a virtualization of inode numbers β all let us move structures around freely without updating every reference. The cost is a bit of overhead; weigh it.43.6β43.7 The Checkpoint Region, and Reading
But if imap pieces are also scattered, how do we find them? There has to be one fixed place to start. Thatβs the checkpoint region (CR) checkpoint region The CR: a fixed, well-known disk location (LFS keeps two) that points to the latest pieces of the inode map β the starting point for finding everything. It's updated only periodically (~every 30 s) so it doesn't hurt performance; on reboot, LFS reads the CR to bootstrap recovery. defined in ch. 43 β open in glossary : a known location holding pointers to the latest imap pieces. Crucially, the CR is updated only periodically (~every 30 s), so it doesnβt add a seek to every write. Reading a file thus walks a chain of indirection:
1Start at the checkpoint region
With nothing in memory, LFS reads the checkpoint region β the one fixed, well-known location. It holds pointers to the latest pieces of the inode map.
43.8 What About Directories?
Directories work exactly as before β a directory is just a file whose data is a
set of (name β inode number) mappings β and itβs written into the log too.
Looking up foo walks the imap twice (once for the directory, once for the
file):
1The on-disk layout after creating foo
Creating file foo in a directory writes: foo's data (Dk), foo's inode I[k], the directory's data D (holding the entry (foo, k)), the directory inode, and an imap chunk mapping both inode k and the directory's inode.
The imap also quietly solves the recursive update problem recursive update problem A hazard in any never-overwrite-in-place file system: moving an inode to a new address would force updating the directory that points to it, then that directory's parent, and so on up to the root. LFS avoids it with the inode map β the directory keeps the same nameβinode-number mapping, and only the imap entry changes. defined in ch. 43 β open in glossary : in any never-overwrite-in-place FS, moving an inode to a new address would force updating the directory that points to it β then that directoryβs parent, and so on up to the root. LFS avoids the whole cascade because the directory stores only the inode number (unchanged); when the inode moves, only its imap entry is updated, not the directory.
Check yourself: inode map and checkpoint region
1.Why is finding an inode harder in LFS than in FFS?
2.What does the inode map (imap) do?
3.Where does LFS store the imap, and why?
4.If imap pieces are scattered too, how does LFS find them? And what keeps this cheap?
5.What is the chain of indirection to read a file in LFS?
6.How does the imap solve the recursive update problem?