Understanding Linux: Week 3: Inodes

So finally I got some time to continue my linux series. This one is not going to be an interesting one but we’ll have to get some basics clear before we get into the complex stuff.

This article deals with inodes in linux filesystem. What they are and why they are important? 

What is an iNode? (Wikipedia)

“The inode is a data structure in a Unix-style file system that describes a filesystem object such as a file or a directory. Each inode stores the attributes and disk block location(s) of the object’s data.[1]Filesystem object attributes may include metadata (times of last change,[2] access, modification), as well as owner and permission data.” – Wikipedia

You can think of an inode as a unique identifier for each file that is present in your system. Inode is used in ext3/ext4 filesystems. They are important because they store metadata related to the files like:

  • Size of file
  • Device ID
  • User ID of the file
  • Group ID of the file
  • The file mode information and access privileges for owner, group and others
  • File protection flags
  • The timestamps for file creation, modification etc
  • link counter to determine the number of hard links
  • Pointers to the blocks storing file’s contents

As you can see different flags are present in the inode datastructure for each file. This helps the OS in limiting access to files and providing file level permissions.

From the inode number, the kernel’s file system driver can access the inode contents, including the location of the file – thus allowing access to the file.

A file’s inode number stays the same when it is moved to another directory on the same device, or when the disk is defragmented which may change its physical location.

Why should I care about inodes?

As pointed out in the sections above, inodes have various uses. Also linux uses filesystem for a large number of things, to understand the future articles in this series understanding inodes is important.

Why no file-name in Inode information?

As pointed out earlier, there is no entry for file name in the Inode, rather the file name is kept as a separate entry parallel to Inode number. The reason for separating out file name from the other information related to same file is for maintaining hard-links to files. I will cover the difference between hard links and soft links in a different article.

When are Inodes created?

As we all now know that Inode is a data structure that contains information of a file. Since data structures occupy storage then an obvious question arises about when the Inodes are created in a system? Well, space for Inodes is allocated when the operating system or a new file system is installed and when it does its initial structuring. So this way we can see that in a file system, maximum number of Inodes and hence maximum number of files are set.

Now, the above concept brings up another interesting fact. A file system can run out of space in two ways :

  • No space for adding new data is left
  • All the Inodes are consumed.
➜ ls
vikas vikas1
# vikas and vikas1 are two files present in the current directory

➜ ls -i
7642822 vikas 7643096 vikas1

# You can see the inode number of the respective files. All the metadata
# Related to a particular file will be stored in respective inode.

# I am going to copy vikas to vikas2 and we can see that a new inode number
# gets assigned to vikas2
➜ cp vikas vikas2
➜ ls -i
7642822 vikas 7643096 vikas1 7643098 vikas2

➜ ln -s vikas vikas-soft
➜ linux git:(master) ✗ ln vikas vikas-h

➜ ls -i
7642822 vikas 7643096 vikas1 7643098 vikas2 7642822 vikas-h 7643108 vikas-soft

# Notice that the inode number of vikas and vikas-h is same. 
# This means if we do some change to file vikas then that change will be
# done to vikas-h too

This link explains hard link vs soft link in detail. Do take a look.

Next article in this series would be about processes and threads.

References:

 

Share

Comments

comments