Knowledge sharing about inodes in Linux

 

This article mainly introduces the relevant information about inodes in Linux. The introduction in the article is very detailed and has a certain reference and learning value for everyone. Friends who need it, let’s take a look together.

background

Recently, I was reviewing Linux commands, and when I arrived at df, I found something that I had ignored before. That is, the -i option lists the inode information of the file system partition. What is this inode?

What is an inode used for

An inode is an area used to store metadata about a file. The Chinese translation is called "index node".

Background knowledge about inodes

Let's review some of the contents of file storage first. We know that files are stored on the hard disk, and the smallest storage unit of the hard disk is also called a sector, and the size of a sector is 512 bytes.

When the operating system reads information on the hard disk, it reads multiple sectors at once, and these multiple sectors are also called blocks. Typically, the block size is 4KB, which is roughly the size of 8 sectors. It should be noted that the blocks read are continuous spaces.

At this time, we can know that files are stored in "blocks", just like when we write C language programs, we know that when we declare an array, we will not only store the values ​​​​in the array, but also Store the corresponding array information, such as the first address of the array, the file type, and the length of the array, etc. Similarly, you need to find a place to store the meta information of the file, similar to the information related to the creation of the file, the length of the file, and so on. And this place, we call it inode.

what is stored in the inode

The inode contains meta information about the stored file, including the following:

  • The number of bytes in the file.
  • The ID of the creator of the file.
  • The Group ID of the file.
  • File read and write permissions.
  • The relative timestamp of the file. There are three specific ones: ctime-->the time when the inode was last changed; mtime-->the time when the file content was last changed; atime-->the time when the file was last opened.
  • number of links
  • block location of file data

inode number

After seeing the above storage content for the first time, I think everyone will have the same question. Since the inode stores file-related information, why not store the file name. The reason is that the file name is not a standard for the Unix/Linux operating system to identify different files.

The operating system identifies different files by inode number.

In the Unix/Linux system, the user layer name is to open the file through the file name, and the system level mainly passes three steps to open the file:

  • Find the corresponding inode number according to the file name.
  • Get inode information by inode number.
  • According to the inode information, find the block where the file data is stored, and save the data alone.

The special role of inode

In the Unix/Linux system, the inode number and the file name are separated, which leads to some special phenomena in the system:

  • Deleting an inode node means deleting a file. Some files may not be deleted correctly. At this time, we can directly delete the corresponding inode node to delete the file.
  • Move files or rename files without changing the inode number, just the file name.
  • Generally speaking, the system cannot obtain the file name through the inode number. When a file is opened, the system will identify the file through the inode in the future, regardless of the file name.

Because of the inode number, the system can be updated without shutting down the software. The system identifies the running file through the inode number. During the update process, the file has the same file name and a new inode exists, without affecting the currently running file. The original inode of the old version will be recycled when the software is opened next time, and the file name will automatically point to the new inode number.

Inode space occupation problem

Since the data is also stored in the hard disk, the inode will inevitably occupy the hard disk space. When the hard disk is formatted, the operating system will automatically divide the hard disk into two areas:

  • data area
  • inode table

The data area mainly stores file data, and the inode table area stores inode information.

In particular, the size of the area occupied by the inode is given by the operating system when the disk is formatted. The consequence of doing this is that the space in the data area is obviously not used up, but the data cannot be accessed anymore. At this time, because the inode table area is full, it is impossible to store new files in the disk.

directory file

We know that in Unix/Linux, any resource exists in the form of a file. So is the directory. When we open the directory, we actually open the directory file. The structure of the directory file is a list.

Directory entry = contained file name + corresponding inode number.

hard link and soft link

As for what is a hard link and what is a soft link, I will not go into details in this blog post, but only consider it from the perspective of inodes.

From the perspective of inode numbers, in Unix/Linux systems, multiple file names are allowed to point to the same inode number. At this time, if one of the file names is deleted, the access to the other file name will not be affected. At the same time, if the file is opened by a file name and modified, other file names can be shared to the modification. Then call this a "hard link". In Linux, we can create hard links through the ln command.

It is concluded above that in the inode, there is a storage item called "link number", which records the total number of file names that only want the inode. If a file name is created to point to a file through a hard link, then the link number in the inode data field corresponding to the file will be + 1, otherwise - 1. When this value is 0, the system will default to no file name pointing to the inode. At this time, the inode number will be reclaimed, and the corresponding block area will be reclaimed.

As for the corresponding soft link, suppose there are file A and file B, and B is a soft link of A. At this time, the inode numbers of A and B are different, because they are different files, but! The content of B is the path of A. When reading B, the system will automatically access A, so no matter which file is opened, the file A is accessed. At this time, file B is called a "soft link" or "symbolic link" of file A.

In Unix/Linux systems, we can create soft links through the ln -s command.

Summary and Small Supplements

Through the above description, we know that inode is like a pointer field in C language. The pointer field records a variety of information and guides us to the correct file location to read the required information. (Not exactly like that, of course.)

When creating a directory in a Unix/Linux system, two directory entries are automatically generated:

  • .Table of contents
  • ..Table of contents

These two directories can be observed with the ls -al command. The inode number of ". directory" is the inode number of the current directory, which is equivalent to the hard link of the current directory, and the inode number of the ".." directory is the inode number of the parent directory of the current directory, which is equivalent to the hard link of the parent directory. Total directory hard links = 2 + total subdirectories (including hidden files).

Well, the above is the whole content of this article. I hope that the content of this article can bring some help to your study or work.

Reposted from: Micro reading    https://www.weidianyuedu.com

Guess you like

Origin blog.csdn.net/weixin_45707610/article/details/131837694