Unix inode Structure
inode Concept
Read aloud is unavailable in this browser.
The Hidden Architecture Behind Every Unix File
When you type ls -l in a Unix terminal, you see a beautifully formatted listing of files with their permissions, owners, sizes, and dates. What you don't see is the elegant data structure that makes all of this possible in microseconds—even when your file system contains millions of files.
That invisible structure is the inode, short for "index node." It is perhaps the most consequential design decision in Unix file system history, and understanding it deeply will transform how you think about file systems, storage efficiency, and operating system architecture.
Every file in a Unix system has an inode. Every directory has an inode. Even the filesystem itself maintains special inodes for its metadata. The inode is not just a data structure—it is the foundational abstraction that makes Unix file systems work.
By the end of this page, you will understand: why Unix designers separated file metadata from file data; how inodes uniquely identify files at the kernel level; the relationship between filenames, directory entries, and inodes; why this separation enables powerful features like hard links; and how inode design has influenced every major file system since 1971.
The Problem inodes Solve
To understand inodes, we must first understand the problem they were designed to solve. In the earliest days of computing, file systems were relatively simple. But as systems grew more complex, a fundamental question emerged:
How do you organize file metadata efficiently when you have thousands—or millions—of files?
Consider the naive approach: store all information about a file (its name, size, permissions, owner, creation date, modification date, and location on disk) in a single data structure attached to the file itself. This seems natural, but it creates serious problems:
- •Variable-length names complicate storage — Filenames can range from 1 character to 255+ characters. If you allocate fixed space for names, you waste storage. If you use variable space, you need complex allocation schemes for the metadata itself.
- •Searching becomes expensive — To find a file by name, you must read every file's metadata from disk, comparing names one by one. With millions of files, this is catastrophically slow.
- •Moving or renaming is costly — If a file's metadata is stored with its data, renaming or moving files requires physically relocating data or updating every reference.
- •Links become impossible — What if two different names should point to the same file? If metadata is embedded with data, you cannot have multiple references to the same content.
- •Metadata updates affect data — Changing a file's permissions should not require touching the file's data blocks, but if they are interleaved, simple operations become I/O intensive.
The Unix designers at Bell Labs—Ken Thompson and Dennis Ritchie—recognized these problems in 1971 when creating the original Unix file system. Their solution was elegant: completely separate the concept of a file's identity and metadata from the file's name and data.
This separation created two distinct concepts:
- The inode: A fixed-size structure containing all file metadata and pointers to data
- The directory entry: A simple mapping from a filename string to an inode number
This seemingly simple division had profound consequences.
The key insight is that filenames are not properties of files—they are properties of directories. A file exists independently of what you call it or where you place it in the directory tree. The inode IS the file; the name is just a human-readable label stored elsewhere.
What is an inode?
An inode (index node) is a data structure on a Unix-style file system that stores all information about a file except its name and actual data content. Every file, directory, symbolic link, device file, socket, and named pipe in a Unix system has exactly one inode.
Think of an inode as a file's identity card at the kernel level. Just as a person's identity exists independently of their name (you remain the same person even if you change your name), a file's inode exists independently of what names refer to it.
| Property | Description | Implication |
|---|---|---|
| Fixed Size | Each inode is exactly the same size (typically 128-256 bytes) | Enables direct indexing: inode #N is at byte offset N × inode_size |
| Unique Number | Each inode has a unique number within its filesystem | Provides filesystem-unique file identification |
| Pre-allocated | inodes are created when filesystem is formatted | Total number of files is limited by inode count, not just disk space |
| No Filename | inodes do not store the file's name(s) | Enables hard links—multiple names for one file |
| Contains Pointers | Stores disk block addresses where file data resides | Provides fast random access to file data |
The inode number is the kernel's true identifier for a file. When you reference a file by path (like /home/user/document.txt), the kernel must resolve that path to an inode number. The path is merely a convenience for humans; the kernel operates on inode numbers.
You can view a file's inode number using ls -i:
$ ls -i /etc/passwd
131074 /etc/passwd
Here, 131074 is the inode number. If you create a hard link to this file, both names will share the same inode:
$ ln /etc/passwd /tmp/passwd-link
$ ls -i /etc/passwd /tmp/passwd-link
131074 /etc/passwd
131074 /tmp/passwd-link
Same inode number = same file. The kernel does not distinguish between the original name and the link.
When you format a filesystem with mkfs.ext4, you can specify the number of inodes. The default formula typically creates one inode per 16KB of disk space. This means a 1TB drive might have ~64 million inodes. You can run out of inodes (unable to create new files) even with free disk space if you have many small files—a situation sometimes called 'inode exhaustion.'
The inode Table
All inodes on a filesystem are stored in a contiguous region called the inode table (or inode array). This is a critical design choice that enables extremely fast inode lookup.
Because all inodes are the same size and stored contiguously, finding a specific inode is a simple calculation:
inode_location = inode_table_start + (inode_number × inode_size)
This is O(1) access—finding any inode takes constant time regardless of how many files exist on the filesystem. Compare this to searching through variable-length records, which would require O(n) time.
The inode table is typically located near the beginning of the filesystem, after the superblock and bitmap structures. Key characteristics:
Reserved inodes: The first few inodes are reserved for special purposes:
- inode 0: Reserved (often used as a null marker)
- inode 1: Tracks bad blocks on the filesystem
- inode 2: Always the root directory (
/) - inodes 3-10: Reserved for future use in most implementations
Distributed across block groups: In modern filesystems like ext4, the inode table is actually split across multiple block groups for performance and reliability. Each block group contains a portion of the total inodes, reducing seek times when accessing files within the same directory.
inode 2 is universally the root directory in Unix filesystems. When you boot the system or mount a filesystem, the kernel doesn't search for the root—it simply reads inode 2. This hard-coded convention enables fast mounting and eliminates any ambiguity about where the directory tree begins.
The Directory-inode Relationship
If inodes don't store filenames, where do filenames live? The answer reveals the true elegance of the Unix design: filenames are stored in directories, and directories are just files containing name-to-inode mappings.
A directory in Unix is a special type of file. Like any file, it has an inode. But instead of arbitrary user data, a directory's data blocks contain directory entries (often called "dentries")—records that map human-readable filenames to inode numbers.
| Field | Size | Description |
|---|---|---|
| inode number | 4 bytes | The inode this entry points to |
| Record length | 2 bytes | Total size of this directory entry |
| Name length | 1 byte | Length of the filename |
| File type | 1 byte | Type indicator (file, dir, symlink, etc.) |
| Filename | Variable | The actual filename (not null-terminated) |
When you run ls /home/user/, the kernel:
- Starts at inode 2 (the root directory)
- Reads the root directory's data blocks
- Searches for an entry named "home", finds its inode number
- Reads that inode, which is also a directory
- Reads those data blocks, searches for "user", finds its inode number
- Reads that inode (the final directory)
- Reads its data blocks and lists all entries
Each step involves looking up an inode number and reading its contents. This is why deeply nested paths require more I/O—each component requires another inode lookup.
Every directory contains at least two entries: '.' (dot) pointing to its own inode, and '..' (dot-dot) pointing to its parent's inode. For the root directory, both point to inode 2 (itself). These special entries enable relative path navigation and are why 'cd ..' works.
Why Separation Matters: Hard Links
The separation of filenames from file identity (inodes) enables one of Unix's most powerful features: hard links.
A hard link is simply another directory entry pointing to the same inode. Since an inode contains all file metadata and data pointers, any number of directory entries can reference the same inode. All names are equally valid—there is no "original" and "link"; they are simply different names for the same file.
Creating a Hard Link:
$ echo "Hello, World!" > original.txt
$ ln original.txt hardlink.txt
$ ls -li
12345 -rw-r--r-- 2 user group 14 Jan 15 10:00 hardlink.txt
12345 -rw-r--r-- 2 user group 14 Jan 15 10:00 original.txt
Notice:
- Same inode (12345) for both files
- Link count is 2 (the number after permissions)
- Same size, same timestamps—because it's the same file
What Happens on Deletion:
$ rm original.txt
$ cat hardlink.txt
Hello, World!
Deleting original.txt only removes that directory entry and decrements the link count. The inode and data remain because hardlink.txt still references them.
The file is truly deleted only when:
- Link count reaches 0
- No process has the file open
Practical Implications of Hard Links:
The inode's link count tracks how many directory entries reference it. This design has profound implications:
-
Deletion is reference counting:
rmdoesn't delete files; it unlinks directory entries. The kernel deletes the inode and data only when no references remain. -
Moving is instant within a filesystem:
mvjust creates a new directory entry and removes the old one. The inode and data don't move. -
Renaming doesn't affect the file: Since the inode is the file's identity, renaming only changes the directory entry.
-
Backups and deduplication: Hard links allow multiple "copies" that share storage. Many backup systems use hard links to create space-efficient snapshots.
Hard links have two key limitations: (1) They cannot span filesystem boundaries—since inode numbers are only unique within a filesystem, you cannot hard-link to a file on a different partition. (2) They typically cannot link to directories—to prevent creating cycles in the directory tree, most Unix systems prohibit hard links to directories (the kernel-created '.' and '..' entries are the exceptions).
inodes in Action: File Operations
Understanding how common file operations interact with inodes reveals the elegance of this design. Let's trace through several operations at the inode level:
Opening a file: open("/home/user/doc.txt", O_RDONLY)
- Path resolution begins at inode 2 (root)
- Kernel reads root directory, finds "home" → inode 100
- Reads inode 100, verifies it's a directory, checks
xpermission - Reads that directory, finds "user" → inode 150
- Reads inode 150, verifies directory, checks
xpermission - Reads that directory, finds "doc.txt" → inode 500
- Reads inode 500, verifies it's a regular file, checks
rpermission - Kernel creates a file descriptor pointing to inode 500
- Returns the file descriptor to the process
Key insight: The inode number becomes the kernel's internal reference to this open file. If the file is renamed or moved while open, the process continues accessing the same inode—the original data.
Design Tradeoffs and Limitations
The inode design, while elegant, involves tradeoffs that every systems engineer should understand:
| Advantage | Corresponding Limitation | Mitigation |
|---|---|---|
| O(1) inode lookup by number | Fixed inode count decided at format time | Choose inode ratio carefully; some FS allow dynamic inodes |
| Fast path resolution via inode chain | Deep paths require multiple disk reads | Kernel maintains dentry cache to avoid repeated lookups |
| Hard links share storage efficiently | Cannot hard-link across filesystems | Use symbolic links for cross-filesystem references |
| Fixed inode size enables simple math | Limits metadata that can be stored inline | Extended attributes stored in separate blocks |
| Separation enables atomic renames | Renames across filesystems require copy | Application layer handles cross-FS moves |
The inode Exhaustion Problem:
A particularly insidious limitation occurs when you run out of inodes before running out of disk space. This happens when:
- You create millions of very small files
- Each file requires one inode regardless of size
- The inode table fills up while data blocks remain available
You'll see errors like "No space left on device" even though df shows free space. You must check inode usage with df -i:
$ df -h /home
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 100G 60G 40G 60% /home
$ df -i /home
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sda1 6553600 6553600 0 100% /home # No inodes left!
This is common with mail servers (many small files), build systems (many intermediate files), or applications that create many temporary files.
In production systems, monitor both disk space AND inode usage. Set alerts for inode exhaustion—it causes the same 'no space' errors as disk exhaustion but has different causes and solutions. Format filesystems with appropriate inode ratios for your workload.
KEEP LEARNING
Keep learning for free.
Read the complete lesson, save your notes and pick up where you left off with a free account. No card needed.