Inodes, directories and links

Intermediate

Introduction

When you type cat /home/ann/notes.txt, the kernel must turn that text into the disk blocks of one file. The file system does this with two simple structures: directories, which turn names into numbers, and inodes, which turn numbers into blocks. This lesson looks at both, and does the arithmetic that shows why the design works for tiny files and huge ones alike.

How it works

The inode

An inode (index node) is the file system's record of one file. It holds the type (file, directory, link), size, owner, permissions, the times, a link count, and the pointers to the file's data blocks. It does not hold the file's name. Inodes are kept in a table on the disk, and an inode number is an index in it.

Pointers: direct and indirect

The inode holds a few pointers that are used in different ways, as seen in the file-systems lesson:

  • the first D (often 12) are direct: each points to one data block,
  • one single indirect pointer points to a block that is full of pointers to data blocks,
  • one double indirect pointer points to a block of pointers to blocks of pointers,
  • one triple indirect pointer adds one more level.

If a block has B bytes and a pointer has P bytes, one block holds k = B / P pointers. The largest file then has D + k + k² + k³ data blocks. With 4096-byte blocks and 4-byte pointers, k is 1024, and the maximum is about 4 TB.

From a byte offset to a block

To read byte x of a file, compute the block number x / B. Then:

  1. If it is below D, a direct pointer holds it: no extra read.
  2. Otherwise subtract D. If the result is below k, it is in the single indirect block, at that position: one extra read.
  3. Otherwise subtract k. If the result is below k², the outer position is the result divided by k and the inner position is the remainder: two extra reads.
  4. Otherwise subtract k² and do the same with three levels.

Small files, which are by far the most common, never leave the direct pointers.

Directories and path lookup

A directory is a file whose content is a list of entries, each a name and an inode number. Every directory also has the entries . (itself) and .. (its parent). The root directory has a fixed inode number (2 on ext4).

To resolve /home/ann/notes.txt, the kernel starts at the root inode, searches the root's entries for home, takes its inode (which must be a directory), searches that for ann, and so on. If a name is missing the call fails with "no such file", and if a component in the middle is a file, it fails with "not a directory". Kernels keep a cache of recent lookups, because the same paths are used again and again.

Links

A hard link is just another entry, in the same or in another directory, that names the same inode. Both names are equal; the file has one body and a link count of 2. rm removes one entry and lowers the count, and the data is freed only when the count is 0 and no process has the file open. Hard links cannot point to directories (to avoid loops) or across file systems.

A symbolic link is a small file whose data is a path. The kernel follows it when it looks the path up. It can point to a directory, to another file system, or to something that does not exist (a dangling link). A directory's link count is 2 plus its number of subdirectories: one entry in its parent, one . inside, and one .. in each subdirectory.

Example

The program maps four byte offsets to the pointer that reaches them, with 4096-byte blocks, 4-byte pointers and 12 direct pointers, and prints the largest file such an inode allows.

#include <stdio.h>

#define BLOCK 4096
#define POINTER 4
#define DIRECT 12

int main(void) {
    long long per = BLOCK / POINTER;                    /* pointers in one block: 1024 */
    long long offsets[4] = {100, 49152, 4243456, 5000000};

    for (int i = 0; i < 4; i++) {
        long long block = offsets[i] / BLOCK;           /* which block of the file */
        printf("byte %lld is in block %lld: ", offsets[i], block);
        if (block < DIRECT) {
            printf("direct pointer %lld\n", block);
            continue;
        }
        block -= DIRECT;
        if (block < per) {
            printf("single indirect, entry %lld\n", block);
            continue;
        }
        block -= per;
        printf("double indirect, entry %lld then %lld\n", block / per, block % per);
    }
    printf("largest file: %lld bytes\n", (DIRECT + per + per * per + per * per * per) * BLOCK);
    return 0;
}
Open in compiler

Line by line

  • per is the number of pointers in one block: 4096 divided by 4, which is 1024.
  • For each offset, block = offsets[i] / BLOCK is the number of the block that holds the byte.
  • A block number below DIRECT is reached through a direct pointer, and the program prints its number.
  • Otherwise the direct blocks are taken away. If what is left is below per, the block is in the single indirect block, at that entry.
  • Otherwise the single-indirect blocks are taken away too, and the outer entry is block / per and the inner entry block % per, a double indirect block.
  • The last line adds up all four levels and multiplies by the block size to get the largest file.

Expected result

byte 100 is in block 0: direct pointer 0
byte 49152 is in block 12: single indirect, entry 0
byte 4243456 is in block 1036: double indirect, entry 0 then 0
byte 5000000 is in block 1220: double indirect, entry 0 then 184
largest file: 4402345721856 bytes

In the real world

You can see inodes with ls -i and stat file, and the number of links in the second column of ls -l. Running out of inodes is a real failure: a file system holding millions of tiny files may report "no space left on device" with free blocks, because the fixed table of inodes is full (df -i shows it). Modern file systems such as ext4 use extents (a start block and a length) instead of long lists of pointers, which makes large files cheaper, but the directory and the inode work as described here.

Hard links are how backup tools and package managers share identical files, and how git and rsync --link-dest save space. Symbolic links are used for version switching, for instance python pointing to python3.12, and for shortcuts. The link count is also why you can delete a file that a program still has open: the name goes, and the data stays until the program closes it.

Common mistakes

  • Looking for a file's name in its inode. The name is in the directory entry.
  • Forgetting to subtract the earlier levels when finding the position in an indirect block.
  • Calculating the largest file with the block size instead of the number of pointers in a block.
  • Thinking that deleting a hard link deletes the file even when other links remain.
  • Walking through a file as if it were a directory in a path.

Best practices

  • Keep small files within the direct pointers; avoid millions of tiny files when one container will do.
  • Check the inode usage as well as the free blocks when a disk seems full.
  • Use symbolic links for flexible shortcuts, and hard links only within one file system.
  • Use absolute paths in scripts, so that the current directory does not change their meaning.

Summary

A directory maps names to inode numbers, and an inode holds a file's attributes and its block pointers. Direct pointers reach small files at once; single, double and triple indirect blocks multiply the reach by the number of pointers per block, so the largest file has D + k + k² + k³ blocks. A byte offset becomes a block number, and the block number is split by level to find the pointers. Looking up a path means finding each name in a directory in turn, and a hard link is another name for the same inode, counted in the link count.

Quick check

1. A block has 1024 bytes and a pointer has 4 bytes. How many pointers does one indirect block hold?
2. Where is a file's name stored in a UNIX file system?
3. A file has a link count of 2, and one of its names is deleted. What happens to the data?
4. In a path such as /a/b/c, what must be true of a?

Key takeaways

  • The name lives in the directory and everything else about the file lives in the inode
  • Direct pointers serve small files in one step, and each indirect level adds one disk read and a factor of the pointers per block
  • Looking up a path is a loop: find the next name in the current directory, which must really be a directory
  • A file is deleted when its link count reaches zero and nobody has it open

Practice problems for this lesson

Was this lesson clear?

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.