Inode record and Linuxinode record in Linux

Source: Internet
Author: User

Inode record and Linuxinode record in Linux
Inode record in Linux

Guide 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 ".

Inode content

Inode contains the object metadata. Specifically, it includes the following content:

* Number of file bytes

* 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:

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.

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.

df -i

To view the size of each inode node, run the following command:

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.

Inode number

Each inode has a number. The operating system uses the inode number to identify different files.

This is worth repeating. 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 code; 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:

ls -i example.txt
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:

ls /etc

The ls-I command lists the entire directory file, that is, the file name and inode Number:

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.

ls -l /etc
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 the access to the other file name. This is called hard link ).

The ln command can create a hard link:

Ln source file target file

After the preceding command is run, the inode numbers of the source file and the target file are the same and point to the same inode. In the inode information, there is a "number of links" that records the total number of file names pointing to the inode. This 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, 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 former 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 ".

Soft link

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.

The ln-s command can create soft links.

Ln-s source file or directory target file or directory

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 files. When the software runs next time, the file name automatically points to the new version, and the inode of the old version is recycled.

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

Free to provide the latest Linux technology tutorial books, for open source technology enthusiasts to do more and better: http://www.linuxprobe.com/

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.