Last Updated:

What is inode in Linux?

Has your system ever complained that you didn't have enough space left when there was clearly more than enough space?

 

Inodes and metadata

 

The file system should store files that can reside in directories, which in turn can have subdirectories. At the same time, something somewhere should record information about the location of all files in the file system, file names, their size, what accounts they belong to, what permissions they have, etc. This information is called metadata - data that describes other data. Metadata is stored in the inode of the file.

An inode (or inode) is a data structure that stores metadata about standard files, directories, or other file system objects. Each inode used refers to 1 file. Each file has 1 inode. Directories, character and block devices are all files, which means that each of them has 1 inode.

Information about aliens is stored in a special table, usually located at the beginning of each disk partition. Each section has its own, separate table of inodes.

For each file in the directory, there is an entry containing the file name and its associated alien number. All other information about the file is extracted from the inode table using the alien number of the file.

Inodes are unique only within the partition boundaries of their file systems. If you have two files that are on different partitions on the disk, they may have the same alien numbers.

If you run out of inodes, you will no longer be able to create new files, even if you still have space on the disk partition.

How do I get information about aliens in Linux?


You can easily display a list of inod numbers with the following command:

ls –li /

The following screenshot shows my root directory with the corresponding alien numbers:

root directory

The number of inodes in each file system is set at the stage of its creation, and, as a rule, for most users their number is more than enough.

By default, the file system settings are such that 1 inode is created per 2 KB of disk space. This number of inodes is sufficient for most systems. Rather, the space on your hard drive will run out before all the inodes run out. If necessary, when you define the initial file system settings, you can specify how many aliens you want to create.

If you do run out of inods, neither you nor your system will be able to create new files anymore. This is a fairly rare situation, but it can still arise. For example, in the old days, some mail servers that stored email messages as separate files (which quickly led to the creation of large collections of small files smaller than 2 kilobytes in size) quite often faced this problem. However, when they switched to using databases, the problem was solved.

Some file systems, such as BtrfsJFSXFS, implement dynamic inodes. If necessary, such file systems can increase the number of available aliens.

How does Inode work?

When you create a new file, it is assigned a foreign number and a file name. A foreign number is a unique file number in the file system. Both the name and the number of the alien are stored as a directory entry.

When I ran the command, I displayed the names of the root directory files and the numbers of their inodes. The remaining information about the file owner, group membership, access permissions, size, etc. was retrieved from the alien table using the alien number.ls –li /

You can list the information about the inodes for each file system using the following command:

df -hi

 

References and references


Symbolic links are a well-known feature of Linux. But what happens to inodes when we create a symbolic link? In the following screenshot, I have a file named file1, a directory called dir1, inside of which is a symbolic link called slink1 that points to :../file1

symbolic link

Now let's compare their numbers of foreigners:

ls -liR

numbers of foreigners

As expected, dir1 and file1 have different alien numbers. But the same applies to a symbolic link. When you define a symbolic link, you create a new file. In its metadata, it points to the target. For each symbolic link you create, you use a new inode.

Now let's create a hard link and see what happens to the inodes:

ln ../file1 hlink1

Here is a list of inod numbers:

inod numbers

You can see that file1 and hlink1 have the same alien number. A hard link does not create a new file, it only provides a new name for the same data. This is possible due to the introduction of the mechanism of aliens.

Note: In older versions of Linux, it was possible to create a hard link to the directory. It was even possible to make the directory its own parent. But now there are some restrictions in place so that users don't create a very confusing directory structure.

 

Benefits of inodes

The way inodes work also explains why it is not possible to create a hard link from one file system to another. Solving such a problem would open up the possibility of conflicting numbers of foreigners. At the same time, a symbolic link can be created in different file systems.

Since the hard link has the same alien number as the source file, you can delete the source file and the data will still be available through the hard link. All you did in this case was remove one of the names indicating the specified alien number. The data associated with this alien will remain available until all names associated with it are deleted.

Inodes are also an important reason why Linux systems can be updated without the need for a reboot: one process can use a library file, while another process replaces this file with a new version. An already running process will continue to use the old file, while each new call to it will result in the new version being used.

Another interesting feature that comes with inodes is the ability to store data in the inode itself. This is called "inlining". This storage method has the advantage of saving space because it does not require the use of data blocks, but it also increases the search time, avoiding additional access to the disk to retrieve data.

Some file systems, such as ext4, have an option called inline_data that allows the operating system to store data in the manner described above. Because of the size limitation, embedding only works for very small files.

Conclusion


Inodes are not something you interact with directly, but they play an important role in the operation of the operating system. If a partition needs to contain a lot of small files, then knowing and understanding what inodes are and how they work can save you many problems in the future.