Chapter 7 File system data structures
The disk driver and buffer cache (Chapter 6) provide safe, synchronized access to disk blocks. Individual blocks are still a very low-level interface, too raw for most programs. Xv6, following Unix, provides a hierarchical file system that allows programs to treat storage as a tree of named files, each containing a variable length sequence of bytes. The file system is implemented in four layers:
------------pathnames ------------directories ------------inodes ------------blocks -------------
The first layer is the block allocator. It manages disk blocks, keeping track of which blocks are in use, just as the memory allocator in Chapter 2 tracks which memory pages are in use. The second layer is unnamed files called inodes (pronounced inode). Inodes are a collection of allocated blocks holding a variable length sequence of bytes. The third layer is directories. A directory is a special kind of inode whose content is a sequence of directory entries, each of which lists a name and a pointer to another inode. The last layer is hierarchical path names like /usr/rtm/xv6/fs.c, a convenient syntax for identifying particular files or directories.
File system layout
Xv6 lays out its file system as follows. Block 0 is unused, left available for use by the operating system boot sequence. Block 1 is called the superblock; it contains metadata about the file system. After block 1 comes a sequence of inodes blocks, each containing inode headers. After those come bitmap blocks tracking which data blocks are in use, and then the data blocks themselves. The header fs.h (3550) contains constants and data structures describing the layout of the file system. The superblock contains three numbers: the file system size in blocks, the number of data blocks, and the number of inodes.
Code: Block allocator
1
The block allocator is made up of the two functions: balloc allocates