What is Inodes?

In a library, all the books are organized by genre, author name or audience age-group. If you want to locate a thriller by “Bobby Daniels” you would directly walk to the thriller section and begin finding author names that start with “B”. Just like a library, all the files in a Linux system are organized for efficient retrieval and usage. Inode is an entity assisting in arrangement of files within a Linux system.

What is File Metadata?

Let’s say we have a file called “sample.txt” containing the data “hello”. This file has some data and information relevant to it like file size, permissions, user and group ownership, creation/access/modification timestamps, link count, etc. All these information is collectively referred to as file metadata.

What are File Systems?

On a storage device like a hard drive, a partition is formatted as a file system before it is used for file storage. A file system is the librarian for the hard drive. Formally, it is a set of data structures and algorithms which work in tandem to methodically store files on the partition. Partitions on Linux systems are frequently formatted with EXT4. Other file systems like B-Tree File System (BTRFS), Reiser File System, ZFS, etc. are also available for use.

Tying Them All Together

Inode is a data structure on Ext4 which holds all the metadata for a file. Of course there would be numerous files on a file system. As you guessed it, every file would have its own inode. To uniquely identify the inodes, each inode is numbered.

What would Inode Numbering be like?

Inode numbers on a file system begin from 1. The first ten inodes are reserved for system use. User files have their metadata stored from inode 11. All the inodes are neatly stacked together in an Inode Table. One entry in the Inode Table would be 256 bytes in size. For a file, Linux intelligently organizes all the metadata within 256 bytes! Additionally, an inode for a file would also have information about where in the file system the file’s data is located. Remember – only the file’s metadata is stored in the inode. The total number of inodes in a file system depend on the available space and the number of files that can be stored on the partition.

How are Inodes allocated and deallocated?

As the user adds files to a freshly formatted file system, inodes starting from 11 are allocated to hold file metadata. There is another data structure called “Inode Bitmap” to track the allocation status of an inode. It is a collection of bits which acts as a map. Let us consider eight bits in the inode bitmap to denote the allocation status of inodes 11 to 18 as shown in the table below. A value of 1 in the bitmap means that inode is allocated, i.e. used to hold the metadata for a file. A value of 0 in the bitmap means that inode is currently not in use. Here, we can see that inode 17 is in use. In case the file that has its metadata in inode 17 is deleted, then its corresponding bitmap status would become 0, indicating that it is free to be used by another file.

How to view the Inode Number for a file?

There are two ways to do this. The first is using ls command with –i switch, followed by the name of a file. The first field in the output is the inode number that has the metadata of “sample.txt”. The same information can be obtained using the stat command followed by the file name. To view the total number of inodes available for a partition, df command can be used with -i switch. In “/dev/sda4” partition, 404754480 inodes are available for use, out of which only 359044 inodes are used.

Conclusion

By nature, the Inode Table stacks the metadata of all files meticulously in inodes, along with information about the location of file data. All that we have discussed about Linux inodes, is simply the tip of the iceberg. For more information about its inner working, you can take a look at this link.