What an inode is
When you create a file in Linux, the filesystem allocates an inode for it. This is a fixed structure in a reserved area of the disk, and it stores:
- the object type (file, directory, symlink, device, and so on)
- permissions (file-permissions): the
rwxbits for u/g/o - the owner's UID and GID
- the file size
- the
atime/mtime/ctimetimestamps - the number of names that point to this inode (link count)
- pointers to the data blocks (where the contents physically live)
The filename is just a string in a directory entry, mapped to an inode
number. That is why one set of contents can have several names (hard-link),
and a directory is a special type of inode holding a list of name → inode
pairs.
Why it matters
A few non-obvious things follow from this:
-
A file is not destroyed while at least one name points to it.
rmremoves a name from a directory and decrements the link count. When the counter reaches 0 and no process holds the file open, the data blocks are freed. -
Inodes are a separate pool from blocks. On ext4 their count is fixed when the filesystem is created. You can hit the inode limit while disk space is still free, the classic incident with millions of small session or cache files.
-
mv within one filesystem is a rename (only the directory entry changes, the inode stays the same). Across different filesystems it is a copy plus a delete.
-
A directory also uses an inode. In deeply nested projects, directories can be the third source of inode consumption after caches and logs.