Introduction
The super block is the most important administrative data structure in a UNIX file system. It contains essential metadata that describes the entire file system. If the super block is corrupted, the file system becomes unusable.
What is a Super Block?
- A data structure stored at a fixed location on disk (typically block 1 of the partition).
- It is the master record of the file system.
- The kernel reads the super block into memory when the file system is mounted and writes it back periodically.
Super Block Fields
| Field | Description |
|---|---|
| File System Size | Total size of the file system in blocks |
| Number of Inodes | Total number of inodes in the file system |
| Number of Data Blocks | Total number of data blocks |
| Free Block Count | Number of currently free data blocks |
| Free Inode Count | Number of currently free inodes |
| Free Block List | A partial list/cache of free data block numbers |
| Free Inode List | A partial list/cache of free inode numbers |
| Block Size | Size of each block (512 bytes, 1 KB, 4 KB, etc.) |
| Lock Fields | Locks for the free block and free inode lists |
| Modified Flag | Indicates if the super block has been modified in memory |
| Read-Only Flag | Indicates if the file system is mounted read-only |
| Last Update Time | Timestamp of last super block update |
Free Block List Management
- The super block maintains a linked list of free data blocks.
- It caches a batch of free block numbers directly in the super block for fast access.
- When the cached list is exhausted, the kernel follows a link to the next batch of free blocks.
- This is more efficient than scanning the entire disk for free blocks.
How it works:
Super Block Free List:
[50, 48, 47, 46, 45, ...] → Next batch pointer → [120, 119, 118, ...]
- When a block is needed, the kernel takes one from the cached list.
- When the cache is empty, it loads the next batch using the pointer.
- When a block is freed, it is added back to the cache.
Free Inode List Management
- The super block also caches a list of free inode numbers.
- When this cache is empty, the kernel scans the inode list on disk to refill it.
- Unlike free blocks, the free inode list uses a linear search to refill because inodes are stored sequentially.
The remembered inode:
- The super block remembers the highest inode number from which it last filled the free list.
- On the next refill, it starts searching from this point to avoid rescanning already-allocated inodes.
In-Core Super Block
When a file system is mounted, the kernel:
- Reads the super block from disk into an in-core (in-memory) super block.
- All file system operations use this in-memory copy.
- The modified flag is set whenever changes are made.
- The super block is synced (written back to disk) periodically by:
- The
synccommand. - The
updatedaemon (runs every 30 seconds typically). - The
umountcommand (when unmounting).
Why Super Block is Critical
- If the super block is corrupted, the file system cannot be mounted.
- UNIX (and Linux) maintain backup super blocks at regular intervals on the disk.
- The
fsck(file system check) utility can use a backup super block to repair the primary one.
Summary
- The super block is the master control structure of the UNIX file system.
- It tracks total and free inodes and data blocks.
- It caches free block and inode lists for fast allocation.
- It is loaded into memory at mount time and synced to disk periodically.
- Corruption of the super block can make the entire file system inaccessible.