TT Lab
Blog Get started
Learn Learning paths Courses

Linux Fundamentals

Names, inodes and Links

Continue in TT Lab

In one sentence

On Linux, a file name is not the thing itself; it is only a reference that points to the thing. Everything we learn today follows from this one sentence.

Why this matters

On a production server, logs have filled the disk. You delete a large log file with rm, yet the free space reported by df does not grow by a single byte. Everyone runs into this once, and everyone panics once.

If you conclude here that "rm didn't work," the night gets long. What actually happened is this: rm is not a command that erases a file; it is a command that detaches one name from a directory (unlink). For the data blocks to be released, two conditions must hold at the same time.

  1. No names remain that point to the thing, and
  2. No process has it open.

If the application that was writing the log still has the file open, the second condition fails. The name is gone but the data is alive, and because du counts by walking paths, it never finds that nameless file. The moment df and du disagree is the signal for this situation.

How it works

You need to tell three things apart.

Concept What it is Does it hold the name?
inode The thing itself: size, permissions, owner, timestamps, link count, data block locations No
Directory entry A (name, inode number) pair The name lives only here
File descriptor A number that refers to a file a process has opened No

A directory is, in the end, just a list of these pairs. That is why all of the following is explained naturally.

A directory is a list of pairs of names and inode numbers. app.log and app.log.1 are hard links to the same inode 12, so its link count is 2, and latest.log is a separate inode 13 that holds only a path string. Process fd 7 holds inode 12 directly without going through a name, so even if every name is detached, the data blocks are not released as long as that fd is alive

What you see in the field

First, recovering a deleted log. If a process still has the file open, a symlink to that file remains alive under /proc/PID/fd. Find it with ls -l /proc/1234/fd | grep deleted, then run cp /proc/1234/fd/7 /backup/recovered.log to salvage the whole content. The most important thing here is the order: the moment you restart the process, the chance of recovery disappears with it. In this situation, the first action should be copying, not restarting.

Second, generational backups that blow up in size. rsync's --link-dest makes generational backups cheap by linking unchanged files as hard links. But if you copy that backup directory with a tool that does not know about hard links, the size jumps several times over. You have to specify an option that preserves links, such as rsync -aH, and -H is not included in -a.

Third, inode exhaustion. ext4 fixes the number of inodes at format time and cannot increase it later. When session files or a mail queue create millions of small files, inodes run out first even though blocks remain, and you get No space left on device. That is why capacity alerts must cover both df -h and df -i.

A structure that keeps logs from eating the disk

After going through the incident above, the work does not end with recovering that file. Making sure the same thing does not happen again is the real point, and it makes direct use of the property that names and things are separate.

There are two ways to rotate logs, and the incident above happens when one of them is used wrongly.

Move and send a signal. Rename the file (this finishes instantly because it is the same filesystem) and send the application a signal saying "reopen the log file." When the application opens a new file, the last reference to the old file disappears and the space is reclaimed. If you do not send the signal, or the application does not handle it, it keeps writing to the old file, and that is the "I deleted it but it didn't shrink" situation we saw earlier.

Copy and truncate. Copy the content to another file, then truncate the original to length 0. Because the file descriptor stays the same, you do not need to touch the application. The trade-off is that logs written between the copy and the truncate may be lost, and for a large file the disk usage briefly doubles at that moment.

Which one is right is decided by the application. If the program handles the signal, the first method is better; if it does not, the second is the only option. Using the rotation settings without making this judgment and leaving the defaults is where the incident starts.

And in today's container environments, it is more common for the application not to write to files at all. It emits only to standard output, and the runtime and the collector take care of rotation and retention. This makes the rotation-signal problem disappear entirely, but if you do not check the runtime's rotation settings, the same incident of a node disk filling up with logs happens again, only one layer down. Whichever structure you use, the key is knowing who is supposed to delete.

Why you still read the old value after a fix: where a name points splits three ways

The deployment owner fixed the canonical configuration, yet a few services keep reading the old value. If you start a report like this by comparing contents, it takes a long time. The first question to ask is where the name that the service reads points to. In the field, the paths usually split three ways.

A symbolic link pointing to the old location. In deployments that create a new directory for every release and re-point a link, this is the case where only one service's link was not moved. The link is intact and the target file exists, so it reads the old value without any error. Reading what follows the arrow in ls -l ends the problem, but skipping that one field costs half a day.

A broken relative-path link. A relative-path link is resolved not from where the person who created it was standing but from the directory that contains the link file. So even if the path after the arrow looks plausible, it may actually point to a place that does not exist. This is the odd situation where the name shows up in ls but opening it gives "No such file or directory".

A copy. If the deployment script copied the canonical file and handed it out, from that moment the two are separate files with different inodes. No matter how many times you edit the canonical file, the copy does not follow.

There is one more trap here. You might think "then just tie them together with a hard link", but editors such as vim often save by writing a new file and swapping the name over to it. The canonical name then points to a new inode while the hard link stays on the old inode. The advantage of a hard link, sharing the same inode, breaks at exactly that moment. That is why, in places that "must follow the canonical file when it changes", you use a symbolic link, which follows the name (path).

When you deleted a file but the space does not come back: find the remaining reference

Turning the two conditions you saw earlier around gives you the diagnostic order. If the space did not come back, then either a name is still left, or someone still has the file open. When a name is left, the link count in stat is greater than 1 and another name with the same inode number exists elsewhere. When it is open, there is no name at all, so no amount of searching the filesystem finds it; instead, (deleted) shows up in that process's fd list. du reporting less than df is the signal for this case.

The order of handling the open case is always the same. First recover the content through the fd, then make the process let go of the file. Whether by a restart or a reopen signal, the moment it lets go, the last reference disappears and the content disappears with it. If something that must not be deleted, such as payment records, was mixed in, this one ordering decides whether you can recover it.

What you will do in the next lab

Create a workspace under /root/work, create hard links and symbolic links yourself, and see with your own eyes how the inode numbers and link counts change. You move a file and confirm that its inode stays the same, and you bundle the whole log directory into a compressed archive. The last two steps raise incidents: you tell apart and fix why each of three services reads an old configuration, and you reclaim space that did not come back after a deletion without losing its content.