In-depth understanding of the inode details of the Linux disk file system

2019/11/0220:05:05 technology 467

What is inode?

inode Chinese translation is "index node"

The file is stored on the hard disk, and the smallest storage unit of the hard disk is called "Sector". Each sector stores 512 bytes (equivalent to 0.5KB).

When the operating system reads the hard disk, it does not read sectors one by one, which is too inefficient. Instead, it reads multiple sectors continuously at one time, that is, reads one block at a time. ). This "block" composed of multiple sectors is the smallest unit of file access. The most common size of "block" is 4KB, that is, eight consecutive sectors form a block.

File data is stored in "blocks", so obviously, we must also find a place to store the meta-information of the file, such as the creator of the file, the creation date of the file, the size of the file, and so on. This area for storing file meta-information is called inode, which is translated as "index node" in Chinese. The content of

inode

inode contains the meta information of the file, specifically the following content:

* The number of bytes of the file

* The file owner’s User ID

* The group ID of the file

* The read, write, and execute permissions of the file

* The timestamp of the file, there are three: ctime refers to the time when the inode last changed, and mtime refers to the file content The time of a change, atime refers to the time when the file was last opened.

* The number of links, that is, how many file names point to this inode

* The location of the file data block

Example 1:

stat command to view the status of a file inode information

[root@localhost mytest] stat a.txt 

In-depth understanding of the inode details of the Linux disk file system - DayDayNews

mtime: modify time, the time to modify the file content

refers to the file content The time of the last change. Such as: echo aa >> a.sh or vim a.sh modify the content

atime: access time access time of the file content

refers to the last time the file was viewed, such as: cat a.sh

ctime refers to the time of the last file attribute change of the inode, change time. For example: chmod +x a.sh

inode size

inode will also consume hard disk space, so when the hard disk is formatted, the operating system automatically divides the hard disk into two areas. One is the data area, which stores file data; the other is the inode area (inode table), which stores the information contained in the inode.

The size of each inode node, generally 128 bytes or 256 bytes. The total number of inode nodes is given when formatting. Assuming that in a 1GB hard disk, the size of each inode node is 128 bytes, and one inode is set for every 1KB, then the size of the inode table will reach 128MB, accounting for 12.8% of the entire hard drive.

inode number

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

Unix/Linux systems do not use file names internally, but use inode numbers to identify files. For the system, the file name is just another name or nickname for easy identification of the inode numbernumber. On the surface, the user opens the file by the file name. In fact, the internal process of the system is divided into three steps: first, the system finds the inode number corresponding to the file name; secondly, obtains the inode information through the inode number; finally, according to the inode information, finds the block where the file data is located, and reads the data.

Example 2

Using the ls -i command, you can see the inode number corresponding to the file name

[root@localhost mytest] ls -i

or
[root@ localhost mytest] ll -i

In-depth understanding of the inode details of the Linux disk file system - DayDayNews

Example 3

To view the total number of inodes and the number of inodes used for each hard disk partition, you can use the df command


[root@localhost mytest] df -i

In-depth understanding of the inode details of the Linux disk file system - DayDayNews

directory file

Unix/Linux system, the directory (directory) is also one Kind of file. Opening a directory is actually opening the directory file.

The structure of the directory file is very simple, it is a list of a series of directory entries. Each directory entry consists of two parts: the file name of the included file, and the inode number corresponding to the file name.


[root@localhost mytest] ls -id /etc/

In-depth understanding of the inode details of the Linux disk file system - DayDayNews

Example 4Columna7d#

ls -icommand Output the entire directory file, namely the file name and inode number:

[root@localhost mytest] ls -id /etc/

In-depth understanding of the inode details of the Linux disk file system - DayDayNews

实战

Solve the problem that the disk has space but the file cannot be created

Scene

When creating a file in the /data partition of a low-configuration Linux server (with small memory and hard disk), the system prompts disk space Insufficient, I checked the disk usage with the df -h command and found that the /data partition was only used by 80%, and there was still 1.9G of remaining space, but new files could not be created. The root user was used at the time. The server was not hacked.

[root@localhost mytest] df -h

File system capacity has been used, available, and used% Mount point

/dev/sda3 10G 8.0G 1.9G 80% /

Common sense: As long as the permissions are sufficient, files can be created if there is space on the disk. This is wrong.

Troubleshooting process

First step:

Use df -i

to check the index node (inode) of the partition where /data is located. ), found that it has been used up (IUsed=100%), causing the system to fail to create new directories and files.

Cause of the problem:

/dThere are a large number of small-byte cache files in the ata/cache directory, which occupies not many blocks, but occupies a lot of inodes.

[root@localhost mytest] df -i 

File system Inode Used (I) Available (I) Used (I)% Mount point

/dev /sda3 5242880 5242880 0 100% /

Solution

The first solution:

Delete some files in the /data/cache directory, and release the /data partition Part of the inode.

The second solution:

Back up some files in /data, then delete these files, release some inodes, and create a folder /data/cache2. Mount a new partition under cache2: sda4, the next time you write data, you need to write to the new partition cache2 directory.

After the inode is partitioned, it cannot be increased. The total number of inodes is determined during formatting.

[root@localhost mytest] mkfs.ext4 -I 500000000000 /dev/sda1 The size can be specified 

Parameter:

[-i bytes-per-inode] [-I inode -size]

In-depth understanding of the inode details of the Linux disk file system - DayDayNews

Extending the special function of

inode

Since the inode number is separated from the file name, this mechanism has caused some A phenomenon peculiar to Unix/Linux systems.

1. Sometimes, the file name contains special characters and cannot be deleted normally. At this time, directly delete the inode node to delete the file.

2. To move or rename a file, just change the file name without affecting the inode number.

3. After opening a file, the system will use the inode number to identify the file without considering the file name.

Therefore, generally speaking, the system cannot know the file name from the inode number.

technology Category Latest News