Sub topic 14
File System Layout and In-Memory Tables
This chapter assumes you know what a block and an inode are, and how a process makes system calls. It covers what a file system stores on disk, what the kernel keeps in memory, and what open() does with them. Allocation methods come in later chapters.
On-disk layout
A file system divides a partition into fixed-size blocks and reserves the first blocks for metadata. A typical Unix-style layout is:
| Region | Purpose |
|---|---|
| Boot block (boot control block) | Code to load an OS from this partition. It is empty if the partition is not bootable. |
| Superblock (volume control block) | Block size, total blocks, free-block count, inode count, and a magic number identifying the file system type. |
| Inode bitmap and data bitmap | One bit per inode and one bit per data block, marking each as free or used. |
| Inode table | The array of per-file control blocks (FCBs), one inode per file. |
| Data blocks | File contents and directory contents. |
The following points matter for exam questions:
- The inode holds metadata, not the name. It stores owner, permissions, size, timestamps, link count and block pointers. The name lives in the directory entry, which maps a name to an inode number. This is why one file can have several names (hard links).
- A directory is a file whose data blocks hold (name, inode number) pairs.
- The superblock is critical. File systems usually keep backup copies, because losing it makes the volume unmountable.
- The FCB is called an inode in Unix and a directory entry in FAT. In FAT, the file's attributes sit inside the directory entry itself.
A worked sizing example
Take a 64 MB partition with 4 KB blocks, and suppose it has 1024 inodes of 128 bytes each.
- Total blocks: 64 MB / 4 KB = 16384.
- Data bitmap: 16384 bits = 2048 bytes, which fits in 1 block.
- Inode bitmap: 1024 bits = 128 bytes, which fits in 1 block.
- Inode table: 1024 × 128 B = 128 KB = 32 blocks.
- Metadata blocks: boot (1) + superblock (1) + inode bitmap (1) + data bitmap (1) + inode table (32) = 36.
- Data blocks: 16384 − 36 = 16348.
A bitmap or table that fills a fraction of a block still occupies a whole block. Round up to whole blocks in these questions.
In-memory structures
Reading metadata from disk on every access would be far too slow, so the kernel caches it. Mounting a volume loads the superblock into memory and adds an entry to the mount table. The kernel also keeps:
- A directory cache of recently used path lookups.
- A system-wide open-file table. It holds one entry per open file, with a copy of the file's FCB and an open count.
- A per-process open-file table. It holds one entry per file the process has opened. Each entry has a pointer into the system-wide table, the current file position, and the access mode.
- Buffer caches for data blocks and metadata blocks.
The per-process table is indexed by a small integer, the file descriptor (in Windows, a handle). That integer is what read() and write() receive.
What `open()` does
Lookup reads directory blocks from disk, and that is the expensive part. After open(), read() and write() skip the directory search. They use the descriptor to reach the cached FCB directly, which is why the open/close design exists.
close() removes the per-process entry and decrements the open count. When the count reaches zero, the system-wide entry is removed, and any modified metadata is written back to disk.
Simulating the tables
This program models the two tables. The offset lives in the per-process entry, as in the textbook model.
directory = {"/notes.txt": 5, "/data.bin": 9} # name -> inode number
disk_sizes = {5: 1200, 9: 40960} # inode number -> file size
system_table = {} # inode number -> {"size", "count"}
class Process:
def __init__(self):
self.fds = [] # index = file descriptor
def open(self, path):
ino = directory[path] # KeyError if no such file
entry = system_table.get(ino)
if entry is None: # first open: "read inode from disk"
entry = {"size": disk_sizes[ino], "count": 0}
system_table[ino] = entry
entry["count"] += 1
slot = {"ino": ino, "offset": 0}
for i, s in enumerate(self.fds): # reuse the lowest free slot
if s is None:
self.fds[i] = slot
return i
self.fds.append(slot)
return len(self.fds) - 1
def read(self, fd, n):
slot = self.fds[fd]
size = system_table[slot["ino"]]["size"]
got = max(0, min(n, size - slot["offset"]))
slot["offset"] += got
return got
def close(self, fd):
ino = self.fds[fd]["ino"]
self.fds[fd] = None
system_table[ino]["count"] -= 1
if system_table[ino]["count"] == 0:
del system_table[ino]
p, q = Process(), Process()
a = p.open("/notes.txt")
b = q.open("/notes.txt")
print(system_table[5]["count"]) # 2
print(p.read(a, 1000)) # 1000
print(p.read(a, 1000)) # 200
print(q.read(b, 500)) # 500, q has its own offset
p.close(a)
q.close(b)
print(system_table) # {}Both processes share one system-wide entry, so the file's metadata is cached once. Each has its own offset, so their reads do not interfere.
Common errors and gotchas
- Real Unix splits the tables differently. Each descriptor points to an open file description that holds the offset and flags. That description points to the in-memory inode. Two separate
open()calls on one file give two descriptions with separate offsets, which matches the simulation. Afork()ordup()copies the descriptor but shares the description, so the parent and child share one offset. Exam questions that say "textbook model" use the simpler two-table version above. - The open count is not the link count. The open count lives in memory and tracks how many opens are active. The link count lives in the inode on disk and tracks how many directory entries name the file. A file's data is freed only when both are zero. This is why you can delete an open file and keep reading it.
- Descriptors are per process. Descriptor 3 in one process has no relation to descriptor 3 in another.
- Metadata updates are cached. A crash before write-back can leave the disk inconsistent. This is the motivation for journaling, covered later.
Summary of who holds what
| Item | Held in | Scope |
|---|---|---|
| Superblock | Disk, cached in memory while mounted | Whole volume |
| Inode | Disk, cached in the system-wide table while open | One file |
| Open count | System-wide table | Whole system |
| File offset | Per-process entry (textbook model) | One open of one file |
| File descriptor | Per-process table index | One process |
Exercise
A 128 MB partition uses 8 KB blocks. It has 4096 inodes of 256 bytes each, plus one boot block, one superblock, one inode bitmap block, and a data-block bitmap. How many blocks are left for data? (Assume the bitmap uses one bit per block in the partition and is rounded up to whole blocks.)
Show answer
Total blocks = 128 MB / 8 KB = 16384. Data bitmap = 16384 bits = 2048 bytes, which is 1 block. Inode table = 4096 × 256 B = 1 MB = 128 blocks. Metadata = 1 (boot) + 1 (superblock) + 1 (inode bitmap) + 1 (data bitmap) + 128 (inode table) = 132 blocks. Data blocks = 16384 − 132 = 16252.
Exercise
Two processes each open the same file once, and one of them then calls close. How many entries are in each table, and what is the open count?
Show answer
There is 1 entry in the system-wide table with an open count of 1, because the count went from 2 to 1. The closing process's per-process table has no active entry. The other process still has one entry.