Warm-up: On-disk File System
Easy
Block Device
Traditional storage media can be divided into blocks of fixed size. Each block can then be assigned an address. Thus, we can abstract various devices as devices where you can read and write memory blocks. These abstract devices are called block devices.
xv6 assumes that the file system is stored on such a block device. The size of a block is set to BSIZE bytes in xv6. The block device on which xv6 stores its file system, which is thus divided into blocks of size BSIZE, is called the disk.
What is the block size in xv6? Make a note of this; you will need it later in the lab session.
Note that this disk could be a hard drive, solid-state drive, USB drive, etc. In the QEMU emulator, a hard drive is emulated based on an image file (for xv6:
fs.img). This image contains the content of the virtual disk.
Disk Layout
We know that our disk is divided into blocks of size BSIZE. The type of data you can find in the different blocks is determined by the disk layout. The image below shows this layout.

The very first block of the disk, block 0, is used to store the boot code. This is called the boot sector. It is a convention to store the boot sector in the very first block of a mass storage device. This convention allows us to install other boot loaders or modify the boot loader to boot different types of operating systems on the same machine from different types of mass storage devices.
The boot sector is not used in xv6 and it is not yet part of the file system.
Mass storage devices can be divided into different partitions, each with its own boot section. In that case, sector 0 contains the Master Boot Record, with information about the different partitions on that specific disk. If a partition contains an OS, it will have a Volume Boot Record with the OS boot code on that partition. When the xv6 book or this lab session talks about a disk, think of it as a partition, not the entire hard drive. The boot sector of xv6 would therefore be in the VBR of a partitioned disk.
Superblock
Block 1 is the first block of the filesystem itself. In xv6, this block is called the superblock. The superblock contains meta-information about the filesystem. Based on the superblock, you can deduce the entire disk layout of the file system.
- Check struct superblock in kernel/fs.h. Compare it with the figure above that describes the layout of the disk.
The magic field contains a magic number used to identify the xv6 file system. The magic number of the xv6 file system consists of the bytes 0x40 0x30 0x20 0x10, or the number 0x10203040 represented in little-endian byte order. Magic numbers in files and file systems are used to distinguish different types. For example, every ELF file starts with the bytes 0x7F 0x45 0x4c 0x46, the magic number of ELF files.
Run
makein the xv6 repo. Then run the commandhd -s 1024 -n 1024 fs.img. This command will print the superblock infs.img. Can you find the magic number of the xv6 filesystem? What is the byte address of the first byte of this magic number? Where does this value come from?
The hd (hex dump) command displays the contents of a file byte by byte. Each line of the output (by default) has the following format:
00000410 1e 40 00 00 02 00 00 00 20 00 00 00 2d 00 00 00 |.@...... ...-...| \______/ \______________________________________________/ \________________/ offset 2x 8 bytes from the offset in the file same bytes in ASCIIAll values are displayed in hexadecimal notation. The column with ASCII values will show a
.if the corresponding byte has no ASCII interpretation.If there are multiple lines with only 0-bytes, only the first is shown, followed by a line with a
*. The next shown line is the first with non-0 bytes.Using
-s, you can indicate the offset from which hd displays the file content.-nspecifies how many bytes should be displayed. As with all commands, you can find more information in the manual page. Run the following command in the terminal:man hd.
The second field of struct superblock, size, contains the total size of the file system image expressed in blocks.
Calculate the file system size in bytes based on the
sizefield that you can find viahd. Then runls -land compare it with the size of fs.img.
The uint type is always 4 bytes long, and the values are stored in little-endian byte order.
Inodes
Take a look at how xv6 implements on-disk inodes (dinode).
What is the maximum number of blocks per file in xv6? What is the maximum file size?
Suppose you have a directory with exactly 1 allocated data block in the inode. What is the maximum number of directory entries this directory can contain?
What is the effective maximum number of entries a directory in xv6 can contain?
Hint: Take look at the structure implementing directory entries
struct direntand check Section 10.11 of the book.