Linux › Section D · Disks and filesystems › Topic D.6
Inside a traditional filesystem: inodes and links
Underneath every file name is a numbered record called an inode. Once you see that names and files are separate things, hard links, symbolic links, link counts and the way rm really works all make sense.
Two parts: data and inodes
A traditional Unix filesystem such as ext4 has two main parts: a data pool of fixed-size blocks holding file contents, and an inode table with one numbered inode per file. An inode records a file's type (regular file, directory, symbolic link and so on), permissions, owner and group, size, timestamps, link count and where its data lives in the pool. One thing an inode does not hold is the file's name. Names live in directories, and that separation explains everything else in this topic.
Directories map names to inodes
A directory is itself a file with an inode, and its data is a list of entries, each pairing a name with an inode number. Every directory also contains two special entries: . points to the directory's own inode and .. points to its parent's. On ext2, ext3 and ext4 the root directory of a filesystem is always inode 2, so the kernel always knows where to start. Because a directory entry is just a name and a number, two entries, in the same or different directories, can point to one inode. That is a hard link, and it is not a copy: there is only one file with two names.
Worked example: following a path
How does the kernel open /home/ana/notes.txt on a filesystem mounted at /? The inode numbers here are illustrative. 1. Split the path into components: home, ana, notes.txt. 2. Start at inode 2, the root directory, and read its entries; home maps to inode 1310721. 3. Look up inode 1310721, confirm it is a directory and that you may search it, then read its entries; ana maps to inode 1311001. 4. Repeat: in ana's entries, notes.txt maps to inode 1311458. 5. Inode 1311458 is a regular file, so after a permission check the kernel opens it and follows its block map to the data. A missing name at any step gives No such file or directory.
Seeing inodes: ls -i and stat
ls -i prints each name's inode number, and ls -li adds the long listing, whose second column is the link count. stat file shows the whole inode: size, blocks, inode number, link count (Links), permissions, owner and timestamps. stat -c '%i %h %n' file prints just the inode number, link count and name. Its times are mtime (content changed), ctime (inode changed) and atime (last read), often with a birth time. Inode numbers are unique only within one filesystem; two files on different filesystems can share a number.
Hard links and the link count
ln target newname creates a hard link: a new directory entry for an existing inode. The inode's link count goes up by one. Deleting a file with rm is really unlinking: the kernel removes one directory entry and lowers the count. Only when the count reaches 0, and no process still has the file open, are the inode and its blocks freed. Hard links have limits. They cannot cross filesystems, because an inode number means nothing on another filesystem (ln reports an invalid cross-device link). You cannot hard-link a directory. Filesystems such as vfat have no inode-style directory entries, so they do not support hard links at all.
Symbolic links
ln -s target linkname creates a symbolic link, also called a symlink or soft link: a small file of its own, with its own inode, whose contents are just a path. When a program opens the link, the kernel reads the path and follows it. ls -l shows the type as l and an arrow to the target, and the link's size is the length of that path. Because only a path is stored, symlinks can point to directories and to other filesystems, and the target need not exist. If the target is removed or renamed, the link dangles. A relative target is resolved from the directory that contains the link, not from where you were when you made it.
Worked example: links in bash
Run these in bash in an empty directory on ext4. 1. echo "draft one" > notes.txt, then ln notes.txt backup.txt. 2. ls -li shows both names with the same inode number and a link count of 2. 3. ln -s notes.txt shortcut.txt; ls -l shortcut.txt shows lrwxrwxrwx, a size of 9 and shortcut.txt -> notes.txt, since notes.txt has 9 characters. 4. echo "draft two" >> backup.txt, and cat notes.txt now prints both lines: one file, two names. 5. rm notes.txt. cat backup.txt still prints both lines and stat -c '%h' backup.txt prints 1. 6. cat shortcut.txt fails with No such file or directory: the link stored the name notes.txt, which is gone. 7. mkdir photos; ln photos photos2 is refused: hard link not allowed for directory.
Link counts of directories
On ext4, directory link counts follow a pattern (Btrfs always shows 1). A new, empty directory has a count of 2: its entry in the parent, plus its own . entry. Each subdirectory adds one more, because the subdirectory's .. entry points back to it. So a directory with three subdirectories has a count of \(2 + 3 = 5\), and making a fourth raises it to 6. Ordinary files inside do not change it. The root directory has no parent entry, but its .. points back to itself, so the same total results. It is a quick check on ls -ld output.
Block allocation and consistency
To find free blocks, a filesystem can keep a block bitmap with one bit per block, 0 for free and 1 for used; ext filesystems keep similar bitmaps for inodes. These must agree with the inodes: every claimed block marked used, every link count equal to the entries pointing at the inode. An unclean shutdown can leave them out of step. fsck (see "Filesystem types, creating, and checking filesystems") walks every directory and inode, rebuilds the counts and bitmaps, fixes differences and puts orphaned inodes in lost+found.
Running out of inodes
Because inodes are fixed when the filesystem is made, a disk can be full of files without being full of data. A 10 GiB ext4 filesystem with the default one inode per 16,384 bytes has \(10 \times 2^{30} \div 16{,}384 = 655{,}360\) inodes. A cache that writes one million 1 KiB files would need one million inodes, so file creation fails with No space left on device after about 655,000 files, even though those files fill only about 2.5 GiB at one 4 KiB block each. df -h would show plenty of space; df -i shows IUse% at 100%. The fix is deleting files or recreating the filesystem with more inodes.
Common mistakes to avoid
First, treating a hard link as a copy: both names are one file. Second, expecting rm to free space while another hard link remains or a process holds the file open. Third, trying hard links across filesystems, on directories or on vfat. Fourth, making a relative symlink whose path is wrong from the link's directory. Fifth, assuming a symlink keeps its target alive. Sixth, reading No space left on device as lack of space when df -i shows the inodes are used up.
Key takeaways
An inode holds a file's metadata and block map, not its name; directories map names to inode numbers, and ext root is inode 2. ls -i and stat show inode numbers and link counts. A hard link (ln) is another name for one inode; rm unlinks, and data is freed at count 0 with no process holding it open. Hard links stay within one filesystem and never point to directories. A symlink (ln -s) stores a path, can cross filesystems and can dangle. On ext4 a directory's link count is 2 plus its subdirectories. Watch inodes with df -i.
Flashcards
Flashcard
Record of a file's type, permissions, owner, size, times, link count and block map
Test your understanding
Question 1 of 5
report.txt has a link count of 2 because of a hard link, copy.txt. What happens after rm report.txt?
Question 2 of 5
Why does ln /data/big.iso /home/ana/big.iso fail when /data and /home are different filesystems?
Question 3 of 5
You run ln -s config.yaml current, then rename config.yaml to config.old. What happens when a program opens current?
Question 4 of 5
A directory contains four subdirectories and ten regular files. What is its link count?
Question 5 of 5
Creating a file fails with No space left on device, yet df -h shows 70% of space free. Which command would most likely explain this?
