Home > Article > Operation and Maintenance > Tutorial on inode usage in Linux
This article mainly introduces you to the relevant information about inode in Linux. The introduction in the article is very detailed and has a certain reference and learning value for everyone. Friends who need it can take a look below.
Background
I was reviewing Linux commands recently, and when I went to df, I discovered 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 inode used for?
Inode is an area used to store file meta information. The Chinese translation is called "index node".
Background knowledge about inode
Let’s first review some contents of file storage. We know that files are stored on the hard disk. The smallest storage unit of the hard disk is also called a sector. 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, approximately 8 sectors in size. It should be noted that the blocks read are contiguous spaces.
At this time we can know that files are stored in "blocks", just like when we write a C language program, we know that when we declare an array, not only will it be stored The value placed in the array will also store the corresponding array information, such as the first address of the array, file type, array length, etc. Similarly, you need to find a place to store the meta-information of the file, similar to the information related to file creation. , the length of the file, etc. And this place is called inode.
The content stored in the inode
The inode contains the meta-information of the stored file, including these contents:
The number of bytes in the file.
The ID of the file creator.
The Group ID of the file.
File read and write permissions.
The relevant 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 position of file data
inode number
After seeing the above storage content for the first time, I think everyone will have the same question. Since inode stores file-related information, why not store files? The name. The reason is that file names are not the standard for Unix/Linux operating systems to identify different files.
The operating system identifies different files through inode numbers.
In Unix/Linux systems, the user level name is used to open the file through the file name. The system level mainly goes through three steps to open the file:
Find the corresponding inode number based on the file name.
Get inode information through inode number.
According to the inode information, find the block where the file data is stored, and store the data alone.
The special function of inode
In the Unix/Linux system, the inode number and the file name are separated. This has led to some special phenomena in the system:
Deleting the inode node means deleting the file. Some files may not be deleted correctly. In this case, 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 and no longer consider the file name.
Because of the existence of the inode number, the system can be updated without closing the software. The system identifies the running file through the inode number. During the update process, the file exists with the same file name and a new inode without affecting the currently running file. The original old version of the inode will be recycled the next time the software is opened, 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 definitely occupy the hard disk space. When formatting the hard disk, 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.
Specially, the size of the area occupied by the inode is already given by the operating system when the disk is formatted. The consequence of this is that the space in the data area has not been used up, but data cannot be accessed anymore. At this time, because the inode table area is full, new files cannot be stored on the disk.
Directory file
We know that in Unix/Linux, any resource exists in the form of a file. So does the catalog. When we open a directory, we actually open the directory file. The structure of a directory file is a list.
Directory entry = file name of the included file + corresponding inode number.
Hard links and soft links
I won’t cover the specifics of what is a hard link and what is a soft link in this blog post I won’t go into details, just consider it from the inode perspective.
Considering from the perspective of inode number, in Unix/Linux system, multiple file names are allowed to point to the same inode number. At this time, if one of the file names is deleted, access to the other file name will not be affected. At the same time, if the file is opened through one file name and modifications are made, the modifications can be shared when other file names are opened. Then call this a "hard link". In Linux, we can create hard links through the ln command.
As summarized above, in the inode, there is a storage item called "number of links", 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, the link number in the inode data field corresponding to the file will be + 1, and vice versa - 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 recycled and the corresponding block area will be recycled.
As for the corresponding soft link, suppose there is file A and file B, and B is the 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, file A will be accessed. At this time, file B is called a "soft link" or "symbolic link" to file A.
In Unix/Linux systems, we can create soft links through the ln -s command.
Summary and small additions
Through the above description, we know that inode is like the pointer field in C language, the pointer field It records a variety of information and directs us to the correct file location to read the required information. (Of course it is not exactly the same.)
When creating a directory in a Unix/Linux system, two directory entries will be automatically generated:
.Directory
.. Directory
You can observe these two directories through 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, while 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 number of directory hard links = 2 + total number of subdirectories (including hidden files).
The above is the detailed content of Tutorial on inode usage in Linux. For more information, please follow other related articles on the PHP Chinese website!