UNIX Inodes
What an inode is
An inode (index node) is the on-disk data structure that represents a file. It stores everything about the file except its name and its data:
| Field | Contents |
|---|---|
| File type | Regular file, directory, symlink, device, pipe, socket |
| Permissions | rwx bits for owner, group, other; SetUID, SetGID, sticky bit |
| Owner | UID and GID |
| Size | File size in bytes |
| Timestamps | atime (last access), mtime (last modification), ctime (last inode change) |
| Link count | Number of directory entries pointing to this inode |
| Block pointers | Pointers to the data blocks containing the file’s content |
| Extended attributes | ACLs, security labels, etc. |
An inode is identified by its inode number, which is unique within a file system (but not across file systems). The directory entry maps a name to an inode number — the name is not stored in the inode.
The critical insight
The inode is the file. The name is separate. This enables several UNIX features:
- Hard links: multiple directory entries point to the same inode. The file exists as long as at least one link exists (link count > 0). All links are equal — none is the “real” name.
- Symbolic links: a special file whose content is a string (the target path). The kernel follows the string during path resolution. If the target is deleted, the symlink “dangles.”
- Rename:
mvupdates a directory entry to point to a different name, or updates a different directory entry to point to the same inode. The inode itself is unchanged.
Data block pointers
An inode contains pointers to the file’s data blocks. Since an inode is small (typically 128–256 bytes), it cannot store pointers for a large file directly. UNIX uses an indirect block scheme:
- Direct pointers: the first N blocks (typically 12) are pointed to directly.
- Single indirect: pointer to a block that contains pointers to data blocks.
- Double indirect: pointer to a block of pointers to blocks of pointers to data blocks.
- Triple indirect: three levels of indirection.
For an ext2/ext4 inode with 4 KB blocks and 4-byte pointers:
- Direct: 12 × 4 KB = 48 KB.
- Single indirect: 1024 pointers × 4 KB = 4 MB.
- Double indirect: 1024² × 4 KB = 4 GB.
- Triple indirect: 1024³ × 4 KB = 4 TB.
This balances fast access for small files (most files are small) with support for very large files.
Summary
- The inode stores metadata (permissions, timestamps, block pointers) but not the name.
- Names live in directory entries, which map name → inode number.
- Hard links: multiple names for the same inode; symlinks: a path string.
- Indirect blocks give a tiered pointer structure: direct for small files, increasingly indirect for large files.