Summary of Linux's knowledge of inode
- 2020-06-07 05:58:36
Recently, while reviewing the Linux command, I came across one that I had previously ignored when I went to df. The -ES5en option lists inode information for the file system partition. What is this inode?
What is inode used for
inode is the area used to store file meta-information. The Chinese translation is called "index node".
Background on inode
Let's review a little bit about file storage. As we know, files are stored on a hard disk, and the smallest unit of storage on a hard disk, also known as a sector, is 512 bytes in size.
When the operating system reads information on the hard disk, it reads multiple sectors once, also known as blocks. Typically, the block size is 4KB, which is about the size of eight sectors. It is important to note that the blocks read are contiguous Spaces.
This time we can know that the file is stored in the "block", just like when we write C language program, we know that when we declare an array, it will not only stored in the array of values, can store information corresponding to the array, such as the first address of the array, the file type and length of the array, etc., also, need to find a place to store file meta information, similar to the length of the file to create relevant information, documents and so on. And this place, we call inode.
Content stored in inode
inode contains the meta information for the stored file, including these contents:The number of bytes in a file. ID for file creators. Group ID for the file. Permissions such as reading and writing of files. The relevant timestamp of the file. There are three specific ones: ctime > Time of one change on inode; mtime - > The time of one change in the content of the document; atime - > The last time the file was opened. Number of links Block location of file data
For the first time, I think you might have the same question, since inode stores information about files, why not store file names. The reason is that file names are not the standard by which the Unix/Linux operating system recognizes different files.
The operating system identifies different files by the inode number.
In Unix/Linux system, the user layer name opens the file through the file name, and the system layer opens the file through three steps:Find the inode number based on the file name. Get inode information from THE inode number. According to inode information, find the block of file data and leave the data alone.
The special role of inode
The separation of the inode number and file name in the Unix/Linux system has led to a number of special phenomena in the system:Deleting the inode node deletes the file. Some files may not be deleted correctly. In this case, we can delete the corresponding inode node directly to delete the file. Moving a file or renaming a file does not change the inode number, just the file name. Generally speaking, the system cannot get the file name by inode number. When a file is opened, the system will recognize the file name by inode from now on, without considering the file name.
Because of the inode number, the system can be updated without the software being turned off. The system USES the inode number to identify the running file. During the update process, the file has the same file name and the new inode exists without affecting the current running file. The old VERSION of 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 occupancy issues
Since the same data is stored on the hard disk, inode, of course, takes up space. When formatting the hard disk, the operating system automatically divides it into two areas:Data area inode table
The data area mainly holds file data, while the inode table area holds inode information.
In particular, the size of the area occupied by inode is given by the operating system at disk formatting time. As a result, the data area has not been used up yet, but the data cannot be accessed again, and since the inode table area is full, new files cannot be stored on disk.
We know that in Unix/Linux, any resource exists as a file. So are the directories. If we open a directory, we actually open a directory file. The structure of a directory file is a list.
Directory entry = the filename of the contained file + corresponds to the inode number.
Hard and soft links
I won't go into the details of what is a hard link and what is a soft link in this post, but just think about it from an inode perspective.
From the point of view of inode number, Unix/Linux system allows multiple filenames to point to the same inode number. At this time, if one of the file names is deleted, the access of the other file name will not be affected. At the same time, if the file is opened with one file name and modified, other file names can be Shared to the modification when opened. Call it a "hard link". In Linux, hard links can be created using the ln command.
As summarized above, in inode, there is a storage item called "link count" that records the total number of file names only for that inode. If a file name is created to point to a file by hard linking, the number of links in the inode data field corresponding to that file will be + 1, and vice versa. When this value is 0, the system defaults to having no file name pointing to the inode, at which point the inode number is reclaimed and the corresponding block area is reclaimed.
For the corresponding soft link, it is assumed that there are file A and file B, and B is the soft link of A. At this point, the A and B's inode Numbers are different because they are different files, but! The content of B is the path of A. When B is read, the system will automatically access A, so no matter which file is opened, the file A is accessed. At this point, file B is referred to as a "soft link" or "symbolic link" to file A.
In THE Unix/Linux system, we can create soft links through the ln-ES168en command.
Summary and minor additions
From the above description, we know that inode is like the pointer field in C language. The pointer field records many kinds of information and directs us to the correct file location to read the information we need. (Not exactly.)
When creating a directory in the Unix/Linux system, two directory entries will be generated automatically:Directory. . directory
You can observe both directories using the ES184en-ES185en command. The inode number for ". Directory "is the inode number for the current directory, equivalent to the hard link for 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 a hard link to the parent directory. Total directory hard links = 2 + total subdirectories (including hidden files).