File Systems
Files, directories and inodes; allocation methods; hard and soft links; journaling and crash consistency; copy-on-write file systems; and fsync.
A file system turns a disk — a giant array of numbered blocks — into named files in a hierarchy of directories, with permissions, timestamps, and (hopefully) no corruption when the power fails.
The abstractions
- A file is a sequence of bytes plus metadata (size, owner, permissions, timestamps).
- A directory is a special file mapping names → inode numbers.
- A path like
/home/ada/notes.txtis resolved one component at a time from the root directory.
Inodes
In Unix file systems, each file's metadata lives in an inode (index node). The name is not in the inode; it's in the directory entry that points to it.
Classic Unix inodes hold 12 direct block pointers plus single, double and triple indirect pointers, so small files are fast to access and large files are still supported. ext4 and most modern file systems use extents instead — (start block, length) runs — which are far more compact for large contiguous files.
Allocation methods
| Method | How | Pros | Cons |
|---|---|---|---|
| Contiguous | Each file is one run of blocks | Fast sequential and random access | External fragmentation; files can't grow easily |
| Linked (FAT) | Each block points to the next (in a table) | No fragmentation | Random access is O(n) |
| Indexed (inodes) | An index block lists the data blocks | Random access, growth | Index overhead |
| Extents | Lists of contiguous runs | Compact, fast | More complex |
Free space is tracked with bitmaps (one bit per block) or extent trees.
Hard links vs symbolic links
Another directory entry pointing to the same inode. The file only disappears when the inode's link count drops to 0. It can't cross file systems or point to directories.
ln notes.txt backup.txtA small file containing a path. It can point anywhere, including other file systems and directories, but it breaks ("dangles") if the target moves.
ln -s /opt/app/v2 currentDeleting a file is really unlink: removing a name. If a process still has the file open, the data survives until it's closed — which is why deleting a huge log file sometimes frees no space until the process writing to it restarts.
Crash consistency and journaling
Appending one block to a file needs several separate writes: the data block, the inode (size, pointers) and the free-space bitmap. If power fails halfway, the file system can be left inconsistent: a block marked used but owned by no file, or an inode pointing to garbage.
- fsck (old approach): after a crash, scan the whole disk to repair it. Takes hours on large disks.
- Journaling (write-ahead logging): first write the intended changes to a journal, then a commit record, then apply them in place. After a crash, replay committed journal entries and discard incomplete ones. Recovery takes seconds.
| Journaling mode (ext4) | Journals | Trade-off |
|---|---|---|
journal | Metadata and data | Safest, slowest (data written twice) |
ordered (default) | Metadata; data is written before its metadata commits | Good balance |
writeback | Metadata only, no ordering | Fastest; files may contain stale data after a crash |
Copy-on-write file systems
ZFS, Btrfs and Apple's APFS never overwrite data in place. They write new blocks and then atomically switch a pointer at the top of the tree. That gives:
- Consistency without a journal (the old tree stays valid until the switch).
- Cheap snapshots and clones (keep the old root).
- Checksums on every block to detect silent corruption (bit rot), and self-healing with redundancy.
The trade-off is fragmentation for workloads with many random overwrites, such as databases.
Durability: the fsync story
write() only copies data into the OS page cache. It reaches the disk later. To make it durable you must call fsync (or fdatasync), which waits until the data is on stable storage.
try (FileChannel ch = FileChannel.open(path, CREATE, WRITE)) {
ch.write(ByteBuffer.wrap(bytes));
ch.force(true); // fsync: data + metadata on disk
}import os
with open("data.tmp", "wb") as f:
f.write(payload)
f.flush() # Python buffer → OS
os.fsync(f.fileno()) # OS → disk
os.replace("data.tmp", "data.json") # atomic renameimport { open, rename } from "node:fs/promises";
const fh = await open("data.tmp", "w");
await fh.writeFile(payload);
await fh.sync(); // fsync
await fh.close();
await rename("data.tmp", "data.json");Virtual file system and "everything is a file"
Linux's VFS layer gives every file system (ext4, XFS, NFS, tmpfs, FUSE…) the same interface. Many non-disk things look like files too: /proc (process information), /sys (kernel and device settings), /dev (devices), pipes and sockets. One set of tools (cat, ls, read, write) works on all of them.
- Directories map names to inodes; inodes hold metadata and block pointers (or extents).
- Hard links share an inode; symlinks store a path.
- Journaling writes intent to a log first so crashes can be recovered quickly.
- Copy-on-write file systems give atomic updates, snapshots and checksums.
writeisn't durable;fsyncis. Use write-temp + fsync + rename for atomic updates.
QWhy might deleting a big file not free disk space?medium
Deletion removes the directory entry (unlink). If a process still has the file open, the inode and its data blocks remain until the last file descriptor is closed. lsof +L1 lists such deleted-but-open files.