Inode nodes in Linux file system

Source: Internet
Author: User
This article mainly introduces inode nodes in the Linux file system, and explains in detail what inode is, information contained in inode, and information related to inode numbers. For more information, see

I. What is inode?

To understand inode, start with file storage.
The file is stored on the hard disk. the minimum storage unit of the hard disk is "Sector" (Sector ). Each slice stores 512 bytes (equivalent to kB ).

When the operating system reads a hard disk, it does not read multiple sectors. this is too inefficient. Instead, it reads multiple sectors consecutively at a time, that is, one block at a time ). This "block" composed of multiple sectors is the smallest unit for file access. The size of the "block", the most common is 4 kB, that is, eight consecutive cuts constitute a block.
 
File data is stored in "blocks". Obviously, we must find a place to store the object metadata, for example, the file creator, the file creation date, and the file size. The region where the object metadata is stored is called inode, and the Chinese name is "index node ".

II. inode content

Inode contains the Object Metadata. Specifically, it includes the following content:

Copy codeThe code is as follows: * Number of bytes of the file
* User ID of the file owner
* File Group ID
* File read, write, and execution permissions
* File timestamp. There are three timestamps: ctime refers to the last inode change time, mtime refers to the last change time of the file content, and atime refers to the last time the file was opened.
* Number of links, that is, how many file names point to this inode
* File data block location

You can run the stat command to view inode information of a file:

Copy codeThe code is as follows: stat example.txt

All file information except the file name exists in inode. As to why there is no file name, we will explain it in detail below.
 

3. inode size

Inode also consumes disk space. Therefore, during hard disk formatting, the operating system automatically divides the hard disk into two areas. One is the data zone that stores file data, and the other is the inode partition (inode table) that stores information contained by inode.
The size of each inode node, usually 128 bytes or 256 bytes. The total number of inode nodes, which is given during formatting. generally, an inode is set every 1KB or 2KB. Assume that the size of each inode node in a 1 GB hard disk is 128 bytes, and an inode is set every 1 kB, the size of the inode table will reach 128 MB, it accounts for 12.8% of the total disk.
 

Run the df command to view the total number of inodes and the number of inodes used in each hard disk partition.

Copy codeThe code is as follows: df-I
To view the size of each inode node, run the following command:

Copy codeThe code is as follows: sudo dumpe2fs-h/dev/hda | grep "Inode size"
Because each file must have an inode, inode may be used up, but the hard disk is not full. In this case, you cannot create a new file on the hard disk.
 
IV. inode number

Each inode has a number. the operating system uses the inode number to identify different files.
 
It is worth repeating here. in Unix/Linux systems, inode numbers are used to identify files instead of file names. For the system, the file name is only a nickname or nickname that inode numbers are easy to recognize. On the surface, the user opens the file through the file name. In fact, the process inside the system is divided into three steps: first, the system finds the inode number corresponding to the file name; second, it obtains the inode information through the inode number; finally, according to the inode information, find the block where the file data is located and read the data.
 
Use the ls-I command to view the inode number corresponding to the file name:

Copy codeThe code is as follows:
Ls-I example.txt
 

V. Directory files

In Unix/Linux systems, directory is also a file. Opening a directory is actually opening a directory file.

The structure of directory files is very simple, that is, a list of directory items (dirent. Each directory item consists of two parts: the file name of the included file and the inode number corresponding to the file name.
 

The ls command only lists all file names in the directory file:

Copy codeThe code is as follows: ls/etc
The ls-I command lists the entire directory file, that is, the file name and inode number:

Copy codeThe code is as follows: ls-I/etc
To view the detailed information of a file, you must access the inode node and read the information according to the inode number. The ls-l command is used to list detailed information about a file.

Copy codeThe code is as follows: ls-l/etc
 

6. hard link

In general, the file name and inode number are "one-to-one correspondence", and each inode number corresponds to a file name. However, in Unix/Linux, multiple file names can point to the same inode number. This means that you can access the same content with different file names. modifying the file content affects all file names. However, deleting one file name does not affect access to another file name. This is called hard link ).
 
The ln command can create a hard link:

Ln source file target file
After running the preceding command, the source file and the target file have the same inode number and point to the same inode. In the inode information, there is a name called "number of links", which records the total number of file names pointing to the inode and increases by 1. In turn, deleting a file name will reduce the number of links in the inode node by 1. When this value is reduced to 0, it indicates that no file name points to this inode, and the system will reclaim this inode number and its corresponding block area.
 

Here, by the way, the "number of links" of the directory file ". When creating a directory, two Directory items are generated by default: "." and "..". The former inode number is the inode number of the current directory, which is equivalent to the "hard link" of the current directory. The latter inode number is the inode number of the parent directory of the current directory, it is equivalent to the "hard link" of the parent directory ". Therefore, the total number of "hard links" in any directory is always equal to 2 plus the total number of its subdirectories (including hidden directories ), here 2 is the "hard link" of the parent directory and "under the current directory ". hard link ".
 

7. soft links

In addition to hard links, there is also a special case. Although the inode numbers of file A and file B are different, the content of file A is the path of file B. When reading File A, the system automatically directs the visitor to file B. Therefore, no matter which file is opened, file B is eventually read. In this case, file A is called the soft link or symbolic link of file B ).

This means that file A depends on file B. If file B is deleted, an error is returned when file A is opened: "No such file or directory ". This is the biggest difference between soft links and hard links: file A points to the file name of file B, rather than the inode number of file B. The inode "number of links" of file B will not change.
 

Copy codeThe code is as follows: the ln-s command can create soft links.
Ln-s source file or directory target file or directory
 

VIII. Special functions of inode

Because inode numbers and file names are separated, this mechanism leads to some Unix/Linux system-specific phenomena.
1. sometimes, the file name contains special characters and cannot be deleted normally. In this case, deleting inode nodes can delete files.
2. move or rename a file, but change the file name without affecting the inode number.
3. after opening a file, the system uses the inode number to identify the file, regardless of the file name. Therefore, in general, the system cannot learn the file name from the inode number.
Makes software updates easy. you can update the software without shutting down the software and do not need to restart the software. Because the system uses inode numbers to identify running files, rather than file names. During update, the new version generates a new inode with the same file name, without affecting the running file. When the software runs next time, the file name automatically points to the new version, and the inode of the old version is recycled.
 
IX. actual problems

When a file is created in a/data partition of a Linux server with a low configuration (small memory and hard disk), the system prompts that the disk space is insufficient, run the df-h command to check the disk usage and find that the/data partition only uses 66%, and there is still 12 GB of space. this problem does not occur. Later, I used df-I to check the index node (inode) of the/data partition and found that it was full (IUsed = 100%). as a result, the system could not create new directories and files.
 
Find the cause:
The/data/cache directory contains a large number of small-byte cached files, occupying a small number of blocks, but occupying a large number of inode.

Solution:
1. delete some files in the/data/cache directory and release some inode of the/data partition.
2. use a soft connection to connect the newcache directory in the idle partition/opt to/data/cache, and use the inode of the/opt partition to alleviate the problem of inode deficiency in the/data partition:
Ln-s/opt/newcache/data/cache

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.