Section 15.24

Inode reuse

Full name: Reuse of index node (inode) numbers.

Definition

Inode reuse is the assignment of a deleted file’s identifying number to a file created later. On Unix-like file systems each file is represented by an inode (index node): a record of the file’s metadata and of where its content lies, but not of its name. The inode number identifies an inode within one file system; with the device number, which identifies the file system, it identifies a file at one moment. Equal pairs seen at two times therefore do not prove that one file existed throughout.

The idea

A monitoring program must notice whether the file /srv/app/config is replaced. At 10:00 it records the file’s device number and inode number. At 10:05 it finds the same pair and concludes that the same file was there all along. The conclusion does not follow. In between, someone could have deleted the file and created a new one under the same name, and the file system could have given the new file the number that the deleted file had just released. The pair matches, yet the file at 10:05 is a different file, whose content the 10:00 check never saw.

Hotel room numbers behave the same way. Room 204 occupied on Monday and on Friday does not show that one guest stayed all week; to know that, you need something tied to the guest, or you must have watched the room throughout. The picture stops at one point. A hotel reuses its room numbers by design, whereas whether and how soon a file system reuses an inode number depends on that file system, so reuse is neither guaranteed nor ruled out.

How it works

An inode, and with it its number, belongs to one file for as long as either of two counts is above zero. The link count is the number of directory entries, that is names, that refer to the inode; the stat system call, which reads a file’s metadata, such as its size, owner and timestamps, reports it as st_nlink. The open references are the file descriptors §15.21 and similar kernel references that processes hold to the inode. A file descriptor is a small integer through which a process uses a file it has opened, and it refers to the inode itself, not to the name.

The life of an inode number runs in four steps.

  1. Creating a file allocates a free inode, with its number, and adds one directory entry, so the link count is 1.

  2. Deleting the file, with the unlink system call that the rm command uses, removes one directory entry and lowers the link count by 1.

  3. When the link count is 0 and no open reference remains, the file system frees the inode and marks its number as free. While an open reference remains, the inode survives without a name.

  4. A later file creation, by any process, allocates an inode. Many file systems keep a table of numbered inode slots and choose a free one, so a number freed a moment ago can be handed out again at once.

POSIX (Portable Operating System Interface), the standard for Unix-like systems, names the two fields st_ino, the file serial number, and st_dev, the identifier of the device that holds the file, and states that together they uniquely identify a file within the system. That statement is about one moment. It does not promise that a pair is never used again.

Two kinds of evidence go further than equal pairs.

Timestamps, such as a creation time, only narrow the question: not every file system records a creation time, every timestamp has a limited resolution, and the system clock can be set.

Worked example

A file system with device number 2049 holds /srv/app/config, which no process has open.

  1. At 10:00:00 the monitor’s stat call reads device 2049, inode 131074, link count 1, size 412 bytes.

  2. At 10:01:00 an administrator runs rm /srv/app/config. The link count falls to 1−1=01 - 1 = 0, and no process has the file open, so inode 131074 is freed.

  3. At 10:02:00 a program creates a new /srv/app/config, also 412 bytes long, and the allocator happens to choose number 131074 again.

  4. At 10:05:00 stat reads device 2049, inode 131074, link count 1, size 412 bytes.

Every compared field is equal, yet two files were involved, and for the 60 seconds from 10:01:00 to 10:02:00 no file had the number 131074.

Repeat the history with one change: at 10:00:00 the monitor opens the file and keeps the descriptor, number 3, open. At 10:01:00 rm lowers the link count to 0, but descriptor 3 keeps inode 131074 allocated, now without a name. At 10:02:00 the new file must receive another number, say 131075. At 10:05:00 fstat on descriptor 3 reports inode 131074 with link count 0, and stat on the path reports inode 131075. The differing numbers show that the name now leads to a different file. The link count of 0 shows that the original file lost its last name while the monitor held it.

Stated precisely

Call a file live at a moment if at that moment it has at least one name or at least one open reference, and write id(f)\mathrm{id}(f) for the pair (device number, inode number) of a live file ff. On a local Unix-like file system three statements hold.

  1. Uniqueness at one moment. If two different files ff and gg are live at the same moment, then id(f)≠id(g)\mathrm{id}(f) \ne \mathrm{id}(g) at that moment.

  2. No uniqueness across time. If ff is live at a time t1t_1, gg is live at a later time t2t_2, and the two pairs are equal, it does not follow that f=gf = g. The worked example is a counterexample.

  3. Continuity from a held reference. If a process holds an open reference to ff from t1t_1 to t2t_2, then ff is live throughout, id(f)\mathrm{id}(f) keeps its value, and by statement 1 no other file has that pair at any moment in between.

Statement 3 rests on three assumptions. First, the file system follows the POSIX deletion rule: a file whose last name is removed while it is open keeps existing until the last reference closes. On a network file system such as NFS this can fail: depending on the protocol version and the server, a deletion made on another machine can remove a file that this machine holds open, and reads through the descriptor then fail with a stale file handle error. Second, the file system keeps a live file’s inode number fixed. Ext4 does; a format with no inodes on disk, such as FAT (File Allocation Table), gets inode numbers made up by the operating system, which can change after the file system is mounted again. Third, the file system stays mounted throughout, because mounting it again can assign a new device number. Statement 3 says that ff is one file throughout. It says nothing about whether the content of ff changed or whether the name still leads to ff.

Limits and common mistakes

Equal content does not show continuity. The replacement in the worked example had the same size and could have had the same bytes, so a content digest that is equal at 10:00 and at 10:05 is equally consistent with a deletion followed by a re-creation.

Continuity does not show unchanged content. The content of a held file can still change through any name or descriptor of that same file.

Inode numbers repeat across file systems, so always compare the full pair. Every ext4 file system, for example, gives its root directory inode number 2.

Checking a name and then using it is a race. A program that calls stat on a path, compares the pair and then opens the path can open a file that replaced the first one in between, a time-of-check to time-of-use §9.26 (TOCTOU) error. Open the file first, call fstat on the descriptor, and from then on use only the descriptor.

Every recycled finite label has the same weakness. A PID (process identifier) is reused after its process ends and the exit status §13.24 has been collected; on Linux a process file descriptor, called a pidfd, keeps referring to one process, as a held file descriptor does for a file. File descriptor numbers are reused as soon as a descriptor is closed, and sequence numbers recur after counter wraparound §6.21.

Questions answered

Can matching device/inode numbers at different times prove one continuous object?

No, matching device and inode numbers at different times cannot prove that one file existed continuously. Equal pairs show only that, at each moment, the pair belonged to some live file, one that still had a name or was held open. They do not show that it was the same file in between. When a file’s last name is removed and no process has it open, the file system frees its inode, the record that describes the file, and may give the same inode number to a file created later; a deleted file and its replacement can then show identical pairs.

Continuity is established by keeping a file descriptor open on the file for the whole interval, which on a local Unix-like file system stops the inode from being freed and its number from passing to another file. Where the file system keeps one, a generation number that changes when an inode is reused can also be compared. Equal names, sizes or content do not establish continuity, because a replacement can copy them.

Related entries: File descriptor versus pathname (§15.21), Cryptographic digest versus path identity (§13.3), File metadata versus content (§13.11), Hard link versus symbolic link (§13.10), Time-of-check to time-of-use (TOCTOU) (§9.26). Further reading: [50, 139].