Article count:922 Read by:3074353

Account Entry

Many interviews have asked what does an inode in Linux contain? Many people don’t know this!

Latest update time:2024-06-04
    Reads:


Source: https://blog.csdn.net/fuming0210sc/article/details/78699050 

Inode is an important concept

It is the basis for understanding Unix/Linux file systems and hard disk storage.



To understand inode, we must start with file storage.
Files are stored on a hard disk, and the smallest storage unit of a hard disk is called a 'sector'. Each sector stores 512 bytes (equivalent to 0.5KB).
When the operating system reads the hard disk, it does not read sector by sector, which is too inefficient. Instead, it reads multiple sectors at a time, that is, it reads a "block" at a time. This "block" composed of multiple sectors is the smallest unit of file access. The most common size of a "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 file's meta information, such as the creator of the file, the creation date of the file, the size of the file, etc. This area for storing file meta information is called inode, which is translated into "index node" in Chinese.

The inode contains the meta information of the file, specifically the following:
* The number of bytes in the file
* User ID of the file owner
* Group ID of the file
* File read, write, and execute permissions
* There are three timestamps for a file: ctime refers to the last time the inode was changed, mtime refers to the last time the file content was changed, and atime refers to the last time the file was opened.
* Number of links, that is, how many file names point to this inode
* The location of the file data block
You can use the stat command to view the inode information of a file:
stat example.txt
In short, all file information except the file name is stored in the inode. As for why there is no file name, there will be a detailed explanation below.

Inodes 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, and the other is the inode area (inode table), which stores the information contained in the inode.
The size of each inode node is generally 128 bytes or 256 bytes. The total number of inode nodes is given during formatting, and generally one inode is set for every 1KB or every 2KB. 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 disk.
To view the total number of inodes and the number of inodes used by each hard disk partition, you can use the df command.
df -i
To view the size of each inode node, you can use the following command:
sudo dumpe2fs -h /dev/hda | grep 'Inode size'
Since each file must have an inode, it is possible that the inodes have been used up, but the hard disk is not yet full. At this time, it is impossible to create new files on the hard disk.

Each inode has a number, and the operating system uses the inode number to identify different files.
It is worth repeating here that Unix/Linux systems do not use file names internally, but use inode numbers to identify files. For the system, the file name is just an alias or nickname for the inode number to facilitate identification. On the surface, users open files through the file name. In fact, this process is divided into three steps within the system: first, the system finds the inode number corresponding to the file name; second, 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.
Use the ls -i command to see the inode number corresponding to the file name:
ls -i example.txt

In Unix/Linux systems, a directory is also a file. Opening a directory actually means opening the directory file.
The structure of a directory file is very simple, it is a list of directory entries (dirent). Each directory entry consists of two parts: the file name of the file contained, and the inode number corresponding to the file name.
The ls command simply lists all the 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
If you want to view detailed information about a file, you must access the inode node and read the information based on the inode number. The ls -l command lists detailed information about a file.
ls -l /etc

Generally, there is a one-to-one correspondence between file names and inode numbers, with each inode number corresponding to a file name. However, Unix/Linux systems allow multiple file names to point to the same inode number. This means that the same content can be accessed with different file names; modifying the file content will affect all file names; however, deleting a file name will not affect access to another file name. This situation is called a 'hard link'.
The ln command can create hard links:
ln source file target file
After running the above command, the source file and the target file have the same inode number, and both point to the same inode. There is an item in the inode information called "link count", which records the total number of file names pointing to the inode, and it will increase by 1. Conversely, deleting a file name will reduce the "link count" in the inode node by 1. When this value decreases to 0, it means that there is no file name pointing to this inode, and the system will reclaim this inode number and its corresponding block area.
Here, let me talk about the 'number of links' of directory files. When creating a directory, two directory entries are generated by default: '.' and '..'. The inode number of the former is the inode number of the current directory, which is equivalent to the 'hard link' of the current directory; the inode number of the latter is the inode number of the parent directory of the current directory, which is equivalent to the 'hard link' of the parent directory. Therefore, the total number of 'hard links' of any directory is always equal to 2 plus the total number of its subdirectories (including hidden directories). The 2 here is the "hard link" of the parent directory to it and the '. hard link' under the current directory.

In addition to hard links, there is another 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 will automatically direct the visitor to file B. Therefore, no matter which file is opened, the file that is ultimately read is file B. At this time, file A is called a 'soft link' or 'symbolic link' of file B.
This means that file A depends on file B to exist. If file B is deleted, an error message will be displayed when opening file A: '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, not the inode number of file B, and the inode 'link number' of file B will not change.
The ln -s command can create a soft link.
ln -s source file or directory target file or directory

Since the inode number is separated from the file name, this mechanism leads to some phenomena unique to Unix/Linux systems.
1. Sometimes, the file name contains special characters and cannot be deleted normally. In this case, directly deleting the inode node can delete the file.
2. Moving or renaming a file only changes the file name and does not affect the inode number.
3. After opening a file, the system identifies the file by its inode number, not its file name. Therefore, generally speaking, the system cannot know the file name from its inode number.
Point 3 makes software updates easy. You can update without closing the software or restarting it. This is because the system identifies running files by inode number, not by file name. When updating, the new version of the file generates a new inode with the same file name, which will not affect the running file. When you run the software next time, the file name will automatically point to the new version of the file, and the inode of the old version of the file will be recycled.

When creating a file in the /data partition of a low-configuration Linux server (small memory and hard disk), the system prompts that the disk space is insufficient. I checked the disk usage with the df -h command and found that the /data partition was only 66% in use, with 12G of remaining space. This problem should not occur. Later, I checked the index node (inode) of the /data partition with df -i and found that it was full (IUsed=100%), causing the system to be unable to create new directories and files.
Find the cause:

There are a large number of small-byte cache files in the /data/cache directory. They occupy few blocks but a large number of inodes.
solution:

1. Delete some files in the /data/cache directory to release some inodes of the /data partition.
2. Use a soft link to connect the newcache directory in the free partition /opt to /data/cache, and use the inodes of the /opt partition to alleviate the problem of insufficient inodes in the /data partition:
ln -s /opt/newcache /data/cache


The spring recruitment has begun. If you are not adequately prepared, it will be difficult to find a good job during the spring recruitment.


Here is a big employment gift package for everyone. You can prepare for the spring recruitment and find a good job!



Latest articles about

 
EEWorld WeChat Subscription

 
EEWorld WeChat Service Number

 
AutoDevelopers

About Us Customer Service Contact Information Datasheet Sitemap LatestNews

Room 1530, Zhongguancun MOOC Times Building,Block B, 18 Zhongguancun Street, Haidian District,Beijing, China Tel:(010)82350740 Postcode:100190

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号