File Systems: Inodes, Directories and Journaling
Learn how files map to disk blocks, what an inode stores, how hard and symbolic links differ, and how journaling keeps a file system consistent after crashes.
Welcome back!
Step 1 of 13Learn
From blocks to files
A storage device offers only a long row of numbered blocks, typically 4 KB each. Users want something entirely different: named files that grow and shrink, organized in folders, with owners, permissions and timestamps. The file system bridges that gap. It's the most visible abstraction an OS provides, and one where bugs are least forgivable, because they lose data.
To the programmer, a file is a named sequence of bytes. The OS offers system calls to open it (getting back a small integer called a file descriptor), read and write bytes at a current position, seek to another position, and close it. The kernel keeps a per-process table of open files and a system-wide table tracking each open file's position and mode.
Under the hood, the file system must answer several questions efficiently:
- Which blocks hold the contents of this file, in what order?
- Which blocks are free?
- How do names in folders map to files?
- How do we stay consistent if power fails halfway through an update?
Different file systems answer differently. ext4 is the default on many Linux systems, NTFS on Windows, APFS on Apple devices, exFAT and FAT32 on memory cards and USB sticks, and ZFS and Btrfs on servers that need features such as snapshots and checksums. The core ideas, however, are shared.
Step 2 of 13Compare
Three ways to allocate blocks
| Aspect | Contiguous | Linked | Indexed |
|---|---|---|---|
| Idea | Each file occupies one continuous run of blocks | Each block stores a pointer to the next block | A separate index block lists all of the file's blocks |
| Sequential reads | Excellent | Good | Good |
| Jumping to the middle of a file | Instant: compute the block number | Slow: follow the chain from the start | Fast: look up the index |
| Main problem | Files can't grow easily; free space fragments | One damaged pointer loses the rest of the file | Index blocks take space; very large files need several levels |
| Seen in | CD-ROMs, some read-only media | FAT keeps the chain in a separate table | Unix inodes, ext4 extents, NTFS |
Step 3 of 13Learn
Inodes
Unix-style file systems store each file's metadata in a structure called an inode (index node). Every file and directory has exactly one inode, identified by an inode number. The inode holds:
- File type (regular file, directory, symbolic link, device)
- Size in bytes
- Owner user ID and group ID
- Permission bits
- Timestamps: last access, last modification of contents, last change of metadata
- Link count: how many directory entries point to this inode
- Pointers to the data blocks
Notice what's missing: the file's name. Names live in directories, not in inodes.
The classic inode layout uses a clever pointer scheme. It has 12 direct pointers to data blocks, which is enough for small files, plus a single indirect pointer to a block full of further block pointers, a double indirect pointer to a block of pointers to pointer blocks, and a triple indirect pointer one level deeper. Small files, which are the vast majority, are reached in one step, while huge files are still possible.
With 4 KB blocks and 4-byte pointers, one pointer block holds 1,024 pointers. The direct pointers cover 12 × 4 KB = 48 KB. The single indirect pointer adds 1,024 × 4 KB = 4 MB, the double indirect 1,024 × 1,024 × 4 KB = 4 GB, and the triple indirect about 4 TB. Modern ext4 uses extents instead, records like start at block 5,000 and continue for 300 blocks, which describe large contiguous files far more compactly.
Step 4 of 13Diagram
How an inode finds a file's blocks
Tap a shape with to see more.
- Inode
Metadata plus block pointers. No file name.
- 10 direct pointers
Point straight at data blocks: fast for small files.
- Single indirect
Points to a block full of pointers.
- Double indirect
Points to a block of pointers to pointer blocks.
- Data blocks 0-9
- Pointer block
- More data blocks
- Pointer blocks
- Even more data blocks
Step 5 of 13Practice · Work it out
An inode has 10 direct pointers and one single indirect pointer. Blocks are 1 KB (1,024 bytes) and each block pointer is 4 bytes. What is the maximum file size in KB using only these pointers?
Step 6 of 13Learn
Directories, hard links and symbolic links
A directory is just a special file whose contents are a list of entries, each pairing a name with an inode number. To open /home/ana/notes.txt, the kernel starts at the root directory's inode, looks up home to find its inode, reads that directory, looks up ana, and finally notes.txt. This is path resolution, and kernels cache recent lookups because it happens constantly.
Because names and inodes are separate, one file can have several names. A hard link is simply another directory entry pointing to the same inode. Both names are equally real; neither is the original. The inode's link count records how many names exist. Deleting a file (the unlink system call) removes one name and decreases the count. The data is freed only when the count reaches zero and no process still has the file open. That's why a program can keep writing to a log file that someone has already deleted.
A symbolic link (symlink, similar to a Windows shortcut) is different: it's a small file of its own that contains a path. Opening it makes the kernel resolve that path instead. Symlinks can point to directories and across different file systems, which hard links can't. But if the target is moved or deleted, the symlink becomes a dangling link that points nowhere.
Renaming a file within the same file system doesn't copy any data at all: it just adds a new directory entry and removes the old one, which is why moving a 10 GB file to another folder on the same drive is instant.
Step 7 of 13Practice · Diagram
Resolving /home/ana/notes.txt
A hard link named /tmp/n.txt points to the same file. At which step would the two paths finally meet?
Tap the shape that answers the question, then press Check.
- Root directory inode
Its location is fixed, so lookups can start here.
- Look up "home"
The root directory's entries give home's inode number.
- Look up "ana"
- Look up "notes.txt"
The final entry gives the file's inode number.
- Read the file's inode and blocks
Step 8 of 13Practice · Sort into groups
Sort each statement by the kind of link it describes.
Tap an item, then tap its group (or drag it there).
Step 9 of 13Learn
Crashes and journaling
Appending one block to a file requires several separate writes to disk: mark a block as used in the free-space bitmap, write the data into the block, update the inode's block pointers and size. If power fails after some of these writes but not others, the file system becomes inconsistent: perhaps a block is marked used but belongs to no file (leaked space), or worse, an inode points to a block that is still marked free and may be handed to another file.
Older systems repaired this after a crash with a checker such as fsck or chkdsk, which scanned the entire disk. On a large drive that could take hours.
Modern file systems use journaling, borrowed from databases. Before changing the real structures, the file system writes a description of the whole update into a reserved area called the journal (or log), then a commit record. Only after the commit is safely on disk does it apply the changes to their real locations.
After a crash, recovery is quick:
- If a transaction in the journal has its commit record, replay it: apply its changes again (doing so twice is harmless).
- If it has no commit record, discard it: the real structures were never touched.
Either way the file system ends up consistent within seconds. Many file systems journal only metadata by default for speed, which keeps the structure consistent but may leave the newest file contents stale after a crash. Copy-on-write file systems such as ZFS, Btrfs and APFS take another route: they never overwrite live data, writing new versions elsewhere and switching a pointer atomically, which also makes cheap snapshots possible.
Step 10 of 13Compare
Recovering after a crash
| Aspect | Full check (fsck, chkdsk) | Journaling |
|---|---|---|
| How | Scan every inode and block | Replay or discard recent journal entries |
| Time on a big drive | Minutes to hours | Seconds |
| Extra cost in normal use | None | Some extra writes |
| Protects your data from deletion? | No | No: it is not a backup |
Step 11 of 13Practice · Put in order
Order the steps a journaling file system follows to safely apply an update.
Drag the items, or use the arrow buttons, to put them in order.
- Apply the changes to their real locations on disk
- Write a description of all the changes into the journal
- Write a commit record to the journal
- Mark the journal transaction as complete so its space can be reused
Step 12 of 13Note
Step 13 of 13Practice · Choose one
A process has a 2 GB log file open and is still writing to it. An administrator runs rm on the file to free space, but the disk's free space doesn't change. Why?
Lesson complete!