Things you need to know about Inode in File System !!!

Tram Ho

Foreword

Perhaps all of us are too familiar with the concept of “File”,

But have you ever wondered, how UNIX/Linux operating systems store and manage files in the drive? Are the “files” intrinsically attached to its respective data? And if we delete a “file”, is its data really lost?

To answer the above question, we will need to know the concept of “Inode”.

What is inode?

First, we need to talk a bit about hard drives. On the operating system, the hard drive is considered an array of “logical blocks” to store data. And each data file corresponds to many logical blocks distributed throughout the partitions of the hard drive.

So how does the operating system know which blocks a file contains? In other words, how does it store and manage data blocks in its system?

The answer is: Inode

An inode is a data structure used by UNIX and Linux operating systems to store important file information in the file system. That information includes: Owner, group, Access permission, Size, Number of block, and most importantly, pointer to data blocks (pointers to data blocks of the file).

To view the information of a file stored by the inode, we can use the command

The result will appear similar to the image:

stat_inode.png

Limited number of inodes?

Each file/folder corresponds to an inode, so is the number of inodes finite?

The answer is yes. When the file system is initialized, an algorithm is applied to calculate the number of inodes based on the capacity of the respective drive. For example, some systems use 1:16 KB scaling, which means that there is an inode for every 16KB of storage.

This is also the maximum number of files/folders you can create and store on your file system.

You can check the current inode usage status of the file system with the command:

df_inode.png

Displayed results will include:

  • Total inodes
  • Number of inodes used, % inodes used
  • Number of unused inodes

If you have used up the system Inode, even if there is still disk space, you will not be able to create new files/folders and receive the message “No space left on device”.

The most common case causing this phenomenon is to initialize too many folders and small files. Therefore, we should keep the number of inodes low by measures such as deleting unused files/directories or deleting cache files, old mail files, …

Back to the original question?

Will the corresponding data be lost when deleting a file?

When we delete a file, we actually delete the link from the file to the inode. An inode can be associated with multiple files, this association is called a hard link.

When there is a need to share a data file for many users, many services, instead of copying data, we can create hard links that point to the same inode to save space.

inode_hard_link.png

And the data as well as the corresponding inode will only be lost when we delete all hard links to the inode, or delete all “hard linked” files.

That’s it!!

Conclude

In this article, I have shared some of my knowledge about Inode.

If everyone finds this knowledge useful, don’t hesitate to upvote so that the author has more motivation for the next articles!

Share the news now

Source : Viblo