What About Directories?
Let's look at how directory reads and writes take place in LFS.
We'll cover the following
Thus far, we’ve simplified our discussion a bit by only considering inodes and data blocks. However, to access a file in a file system (such as /home/remzi/foo
, one of our favorite fake file names), some directories must be accessed too.
How does LFS store directory data
Fortunately, the directory structure is basically identical to classic UNIX file systems, in that a directory is just a collection of (name, inode number) mappings. For example, when creating a file on disk, LFS must both write a new inode, some data, as well as the directory data and its inode that refer to this file. Remember that LFS will do so sequentially on the disk (after buffering the updates for some time). Thus, creating a file foo
in a directory would lead to the following new structures on disk:
The piece of the inode map contains the information for the location of both the directory file as well as the newly-created file . Thus, when accessing file foo
(with inode number ), you would first look in the inode map (usually cached in memory) to find the location of the inode of directory (). You then read the directory inode, which gives you the location of the directory data (); reading this data block gives you the name-to-inode-number mapping of (foo
, ). You then consult the inode map again to find the location of inode number (), and finally read the desired data block at address .
Recursive update problem
There is one other serious problem in LFS that the inode map solves, known as the
Specifically, whenever an inode is updated, its location on disk changes. If we hadn’t been careful, this would have also entailed an update to the directory that points to this file, which then would have mandated a change to the parent of that directory, and so on, all the way up the file system tree.
LFS cleverly avoids this problem with the inode map. Even though the location of an inode may change, the change is never reflected in the directory itself. Rather, the imap structure is updated while the directory holds the same name-to-inode-number mapping. Thus, through indirection, LFS avoids the recursive update problem.
Get hands-on with 1400+ tech skills courses.