OS Notes
How file systems are implemented — on-disk structures, in-memory structures, VFS layer, inode-based implementation, and the journey of a file operation from user call to disk access.
Introduction
When you type open("myfile.txt") in your program, a complex chain of operations unfolds — from your user-space program through the kernel's Virtual File System layer, to a specific file system implementation (ext4, NTFS), down to the disk driver that reads physical sectors. Understanding file system implementation reveals how abstract file operations translate into physical disk reads and writes.
On-Disk Structures
A file system stores several data structures on disk:
Superblock
Contains file system metadata: total size, block size, number of free blocks, number of free inodes, location of inode table, and file system type. Read at mount time. Critical — corruption means the entire file system may be unreadable.
Inode Table
Array of inodes (index nodes). Each inode stores all metadata about one file EXCEPT its name: permissions, owner, timestamps, size, and pointers to data blocks.
Data Blocks
The actual file content. Blocks are typically 4 KB in size.
Block Bitmap
A bitmap where each bit represents one disk block: 1 = in use, 0 = free. Used for fast free block allocation.
Inode Bitmap
Similar bitmap for tracking which inodes are in use.
Directory Entries
Map filenames to inode numbers. A directory is simply a file whose content is a list of (name, inode_number) pairs.
| Superblock | Block Group | Inode | Inode Table | Data Blocks |
|---|---|---|---|---|
| Descriptor | & Block | |||
| Table | Bitmaps |
In-Memory Structures
When the file system is mounted and files are opened, the kernel maintains:
- Mount table: Which file systems are mounted and where
- In-memory superblock: Cached copy for fast access
- In-memory inode cache: Recently accessed inodes kept in RAM
- Buffer cache / page cache: Recently accessed disk blocks cached in memory
- Open file table: Per-process and system-wide tables tracking open files
How open("myfile.txt") Works Internally
| 1. User calls | open("/home/user/myfile.txt", O_RDONLY) |
| 2. VFS parses the path: / | home → user → myfile.txt |
| 4. Read myfile.txt's inode | get file metadata |
Reading File Data
| User calls | read(fd, buffer, 4096) |
| 2. Calculate which logical block to read | offset / block_size |
| 3. Look up inode | find physical block number for that logical block |
| - If YES (cache hit) | copy from cache to user buffer |
| - If NO (cache miss) | schedule disk I/O, copy when complete |
| 5. Update file offset | offset += bytes_read |
Virtual File System (VFS)
VFS is an abstraction layer that allows the same system calls (open, read, write) to work with any file system. It defines standard interfaces that each file system must implement.
| User | open("/mnt/usb/photo.jpg") |
| VFS | Which filesystem is mounted at /mnt/usb? |
| Answer | FAT32 |
| FAT32 driver | Read FAT table, find file's clusters, read data |
| Block I/O | Submit read request to USB storage driver |
VFS operations each file system must implement: lookup, create, unlink, read, write, mkdir, rmdir, rename, etc.
Journaling
Modern file systems use journaling to protect against data corruption during crashes. Before modifying file system structures, the intended changes are written to a log (journal). If the system crashes mid-operation, the journal is replayed on next boot to complete or roll back the operation.
Real-World Analogy
File system implementation is like a library's internal management system. The superblock is the library's master catalog card (how many books, how many shelves). Inodes are the detailed catalog entries for each book (location, author, checked-out status — but NOT the title). Directory entries are the card catalog drawers (mapping titles to catalog numbers). Data blocks are the actual bookshelves. The VFS is the universal checkout system that works regardless of whether the book is from the main collection, special collections, or an interlibrary loan.
Key Takeaways
- File systems store structured data on disk: superblock, inodes, bitmaps, directories, and data blocks
- In-memory caches (inode cache, page cache) provide fast access to frequently used data
- The VFS layer enables multiple file system types to coexist with a uniform API
- Path resolution traverses directories from root to target, reading each directory inode and data
- Journaling prevents file system corruption by logging changes before applying them
- Understanding implementation helps diagnose performance issues and recovery from failures
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for File System Implementation.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Operating Systems topic.
Search Terms
operating-systems, operating systems, operating, systems, file, system, implementation, file system implementation
Related Operating Systems Topics