Sub topic 12
File System Layout and In-Memory Tables
This chapter assumes you understand processes, system calls, kernel mode, binary units, and basic file operations such as open, read, write, and close.
A file system has two parts:
- On-disk structures persist when the computer is powered off.
- In-memory structures help the kernel serve requests quickly while the system runs.
On-disk file system layout
A disk or partition is divided into blocks. A typical file system reserves different regions for different purposes.
| Region | Purpose |
|---|---|
| Boot block | Stores code or information used to start the system |
| Superblock | Stores file system size, block size, and metadata locations |
| Metadata region | Stores file records such as inodes or file control blocks |
| Free-space data | Tracks which blocks are available |
| Data blocks | Store file contents and directory contents |
The superblock is a central description of the file system. It can contain the total number of blocks, the number of free blocks, the block size, and the location of important metadata. The kernel reads it when mounting the file system.
A file's metadata is stored separately from its contents. In a Unix-like system, an inode is a record containing information such as the file type, owner, permissions, size, timestamps, and pointers to the file's data blocks. The file name is usually stored in a directory entry, which maps the name to an inode number.
In-memory tables
When a process calls open, the kernel does not copy the entire file into the process. Instead, it creates or finds several kernel data structures.
- The per-process file descriptor table maps a small integer, such as
3, to an open file entry. The integer is the file descriptor returned to the process. - The system-wide open-file table stores the current file offset, access mode, status flags, and a reference to the file's inode.
- The inode table caches frequently used inode information in memory.
- The buffer cache or page cache keeps recently used disk blocks in memory.
The file descriptor belongs to a process. The open-file entry can be shared. For example, after fork, parent and child descriptors may refer to the same open-file entry. They then share the current file offset. Two independent calls to open normally create separate open-file entries and therefore have separate offsets.
def read_position_after_open_reads(initial_offset, read_sizes):
"""Return the offset after sequential reads."""
offset = initial_offset
for size in read_sizes:
offset += size
return offset
print(read_position_after_open_reads(0, [100, 40])) # 140How a read is resolved
A process issues read(fd, buffer, n). The kernel uses fd to locate the process's open-file entry. That entry identifies the inode and current offset. The inode identifies the needed data blocks. If the blocks are already in the page cache, the kernel can copy data directly from memory. Otherwise, it requests the blocks from the storage device and may cache them for later use.
Exercise
Two processes have descriptors that refer to the same open-file entry. Process A reads 20 bytes, and the shared offset starts at 100. What offset does Process B observe?
Show answer
Process B observes offset 120 because both descriptors share the same current file offset.