144 / 163 · C11 · 8 min
File System Implementation
This chapter uses a minimal vsfs example to show how core on-disk structures can be designed entirely in software to manage files, focusing on the division of labor among the superblock, bitmaps, inode table and data region, plus how system calls map onto those structures.
In this lesson
Understanding File Systems via Structures and Access Paths
The key to mastering a file system is seeing two things at once: which on-disk structures actually hold data and metadata, and which of those structures a process’s open, read or write calls will touch in sequence. Once both parts are clear you have a complete mental model, without having to memorize any particular code fragment.
Fixed-Block Layout of vsfs
The whole partition is sliced into uniform 4 KB blocks. First come the superblock (recording total inode count, data-block count and a magic number), the inode bitmap and the data bitmap, then a fixed-length inode table; everything left is user data. The simple division lets any inode number be turned directly into a block number plus an intra-block offset.
How the Inode Pins File Metadata
Each file owns a fixed-size inode that stores length, permissions, timestamps and pointers to its data blocks. The inodes themselves sit in a numbered array, so locating any one of them is a single arithmetic step rather than a search. That “number-is-address” design keeps later read and write paths as short as possible.
Pitfalls
- Believing a file system needs special hardware acceleration
- Treating an inode number as a user-visible file name
Run an example
Minimum C11 · complete program · Download .c
#include <stdio.h>
int main(void) {
const int block_size = 4096;
const int inode_size = 256;
const int inode_blocks = 5;
int inodes_per_block = block_size / inode_size;
int max_files = inode_blocks * inodes_per_block;
printf("Very Simple File System (vsfs) layout simulation\n");
printf("Block size: %d bytes\n", block_size);
printf("Inode size: %d bytes\n", inode_size);
printf("Inodes per block: %d\n", inodes_per_block);
printf("Inode table blocks: %d\n", inode_blocks);
printf("Maximum files: %d\n", max_files);
printf("Disk layout (64 blocks):\n");
printf(" Block 0: Superblock\n");
printf(" Block 1: Inode bitmap\n");
printf(" Block 2: Data bitmap\n");
printf(" Blocks 3-7: Inode table\n");
printf(" Blocks 8-63: Data region\n");
int example_inum = 42;
int block = 3 + (example_inum / inodes_per_block);
int offset = (example_inum % inodes_per_block) * inode_size;
printf("Example: inode %d located in block %d at byte offset %d\n", example_inum, block, offset);
return 0;
}
Compile locally
gcc -std=c11 -Wall -Wextra -Wpedantic -Werror ostep-40-file-system-implementation.c -o example && ./exampleExpected result
Very Simple File System (vsfs) layout simulation
Block size: 4096 bytes
Inode size: 256 bytes
Inodes per block: 16
Inode table blocks: 5
Maximum files: 80
Disk layout (64 blocks):
Block 0: Superblock
Block 1: Inode bitmap
Block 2: Data bitmap
Blocks 3-7: Inode table
Blocks 8-63: Data region
Example: inode 42 located in block 5 at byte offset 2560
CHECK YOUR UNDERSTANDING
Close the answer. Explain it.
If an inode is 256 bytes, a block is 4 KB and the inode table occupies 5 blocks, how many files can this vsfs instance hold at most?
Show a reference answer
Each block holds 16 inodes, 5 blocks give 80, so the limit is 80 files.
Check the sources
Drafts and official chapters change. The version mark is only the example’s minimum.