🌟 What is This Planet?
The Storage planet explores how data persists on disk, how file systems organize
files and directories, and how the OS reads/writes to storage devices.
You'll understand:
- File systems (ext4, NTFS, etc.)
- Inodes and file metadata
- Blocks and allocation
- Directories as special files
- Hard links vs symbolic links
- File descriptors and open file table
📚 Core Concepts
File System: OS layer that organizes files on disk
Inode: Metadata structure (size, permissions, block pointers)
Block: Fixed-size chunk of disk (e.g., 4KB)
Directory: Special file mapping names to inodes
File Descriptor: Integer handle to open file (per-process)
Path Resolution: Walking directory tree to find inode
🧠 The Mental Model
Think of a file system like a library:
- Files = books
- Inodes = catalog cards (metadata about books)
- Blocks = shelves holding book pages
- Directories = sections of the library
- File path = address to find a book
- Hard link = multiple catalog cards for same book
⚡ Key Flows
Opening a File:
- Process calls open("/path/to/file")
- Kernel resolves path (walks directories)
- Finds inode number
- Creates file descriptor
- Returns FD to process
Reading a File:
- Process calls read(fd, buffer, size)
- Kernel looks up inode via FD
- Finds block numbers for data
- Reads blocks from disk
- Copies to process buffer
- Updates file offset
🔗 Connection to Other Planets
Storage connects to everything:
- Memory: File contents cached in RAM (page cache)
- OS: Kernel manages file system, open file table
- I/O: Disk drivers handle block read/write
- Processes: Each process has file descriptor table
When you write code that opens files, you're using this entire stack!
🎯 File System Hierarchy
Typical Unix-like structure:
- / - root directory
- /bin - essential binaries
- /etc - configuration files
- /home - user directories
- /usr - user programs
- /var - variable data (logs)
- /dev - device files
- /tmp - temporary files
📖 Important Syscalls
open(): Open file, returns file descriptor
read(): Read bytes from file
write(): Write bytes to file
close(): Close file descriptor
lseek(): Change file offset (seek to position)
stat(): Get file metadata (size, permissions)
mkdir(): Create directory
unlink(): Delete file
⚙️ Behind the Scenes
What happens during open("/home/user/file.txt"):
- Kernel reads root inode
- Finds "home" entry → reads its inode
- Finds "user" entry → reads its inode
- Finds "file.txt" entry → reads its inode
- Allocates file descriptor
- Creates open file entry in kernel
- Returns FD to process