inode (on Linux)

Share

What is an inode on Linux?

An inode is an intermediary step between a filepath and a file's data on Linux: when you access a file by its filepath, the filepath tells us a numeric identifier for an inode (the inode number, or inumber), and then the inode tells us where the file's data is in the disk. The term inode is an abbreviation for index node.

More specifically, the inode is a data structure that holds information about a single entry in the filesystem that could be a file, folder (directory), or symlink.

Inodes don't know what their filenames are. Each "file" in a folder is just associating a filepath to an inode number. When an application wants to open a "file," it tells the operating system to open it by its filepath, the system then checks what is the inode number the filepath refers to, and then checks the inode data to see where the file data actually is on disk.

Inodes do keep track, however, of how many different filepaths currently refer to them. These different filepaths are also called hard links. When you create a new file, you create a new inode and a single hard link to it, which becomes its initial filepath. You can create additional filepaths to the same inode by creating additional hard links. Since it's the inode that tells us where the file's data is on disk, all these hard links share the exact same file data. They also share additional properties that are contained in the inode.

For example, the RWX file permissions are stored in the inode, so making a file read-only by removing the write permission will make it so for all hard links since that information is stored in the inode and not in the hard link. Similarly, the inode holds the information about which user and group owns the file, so changing it through one hard link changes it for all hard links.

When you delete a file by its filepath, what is actually happening is that you're removing the hard link and decrementing a hard link reference counter in the inode by 1. When this reference counter reaches zero, that means there are no filepaths left in the filesystem that refer to that inode, so it's become inaccessible (without its inumber, at least). The system will delete the inode and its associated file data when this happen. In other words, if there is at least one hard link to a file, the file isn't deleted, but deleting all hard links causes it to be deleted.

How does an Inode Point to Data on Disk?

An inode uses a set of pointers (file descriptors) to tell the system where its associated data is. This includes pointer that point directly to data and pointers that point to other pointers. There is conflicting information about how many pointers an inode has in total, which might mean that different filesystem formats, e.g. ext2 vs. ext4, may have different inode structures with a different number of pointers.

The Classic Unix Inode

File descriptors: 13 block pointers. The first 10 point to data blocks, the next three to indirect, doubly-indirect, and triply-indirect blocks (256 pointers in each indirect block). Maximum file length is fixed, but large. Descriptor space is not allocated until needed

https://pages.cs.wisc.edu/~bart/537/lecturenotes/s24.html, by professor Barton P. Miller (accessed 2025-03-12)

1.2 Files and inodes

[...]

Through pointers in the inode we can access the file’s data blocks. For reasons of disk space efficiency,
there are :

  • 12 direct pointers
  • 1 single indirect pointer
  • 1 double indirect pointer
  • 1 triple indirect pointer

[...]

If the file consists of 12 or fewer data blocks we can access them directly from the 12 direct pointers
in the inode. For block sizes of 4 Kbytes or 8 Kbytes, this means that files up to 48 Kbytes or 96 Kbytes,
respectively, can be accessed entirely from the information in the inode.

The single indirect pointer is necessary in order to create files of more than 12 data blocks. The
single indirect pointer points to a single data block, whose contents are treated as direct pointers to data
blocks. We can now use a couple of hundred data blocks (files of a few MB).

The double indirect pointer is necessary in order to create file of more than a few MB. The double
indirect pointer points to a data block whose contents are treated as single indirect pointers. Each of
these pointers points to a data block, whose contents are treated as direct pointers to data blocks. This
is enough to reach the file size limit on most systems.

https://user.it.uu.se/~leomu203/osvt08/lab3-documentation.pdf, by professor Magnus Johansson (accessed 2025-03-12)

Quotes

The inode (index node) is a fundamental concept in the ext2 filesystem. Each object in the filesystem is represented by an inode. The inode structure contains pointers to the filesystem blocks which contain the data held in the object and all of the metadata about an object except its name. The metadata about an object includes the permissions, owner, group, flags, size, number of blocks used, access time, change time, modification time, deletion time, number of links, fragments, version (for NFS) and extended attributes (EAs) and/or Access Control Lists (ACLs).

https://docs.kernel.org/filesystems/ext2.html (accessed 2025-03-12)
Written by Noel Santos.

About the Author

I'm a self-taught Brazilian programmer graduated in IT from a FATEC. In a world of increasingly complex and essential computers, I decided to use my technical expertise in hardware, desktop applications, and web technologies to create an informative resource to make PC's easier to understand.

View Comments