Understanding of Linux inodes

filename -> inode -> device block 

 

Reprinted from:
http://www.ruanyifeng.com/blog/2011/12/inode.html
http://blog.s135.com/post/295/  
http://hi.baidu.com/leejun_2005/blog/item /d9aa13a53b3af6e99152ee7e.html 

 

1. What is an inode?

To understand inode, start with file storage.
Files are stored on the hard disk, and the smallest storage unit of the 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 continuously reads multiple sectors 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 size of the "block", the most common is 4KB, that is, eight consecutive sectors form a block.

 

File data is stored in "chunks", so obviously, we also have to find a place to store the meta information of the file, such as the creator of the file, the date the file was created, the size of the file, and so on. This area of ​​storing file metadata is called inode, which is translated as "index node" in Chinese.

 

 
Second, the content of the inode
The inode contains meta information about the file, specifically the following:
  * number of bytes of the file
  * User ID of the file owner
  * Group ID of the file
  * File read, write and execute permissions
  * There are three timestamps of the file: ctime refers to the time when the inode was last changed, mtime refers to the time when the content of the file was last changed, and 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

 

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.

 

Third, the size of the inode
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; 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, generally an inode is set every 1KB or every 2KB. Assuming that in a 1GB hard disk, the size of each inode node is 128 bytes, and an inode is set 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 used inodes for 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 every file must have an inode, it is possible to run out of inodes but the hard drive is not yet full. In this case, new files cannot be created on the hard disk.

 

Fourth, the inode number
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. To the system, the filename is just a nickname or nickname for the inode number for easy identification. Ostensibly, the user opens the file by the file name. In fact, this process inside the system is divided into three steps: 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

 

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

 

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

 

The ls command just lists all the filenames in the directory file:
ls /etc
 
The ls -i command lists the entire directory file, i.e. file name and inode number:
ls -i /etc
 
If you want to view the detailed information of the file, you must visit the inode node and read the information according to the inode number. The ls -l command lists file details.
ls -l /etc

 

 
6. Hard Links
In general, there is a "one-to-one correspondence" between file names and inode numbers, and each inode number corresponds to a file name. However, Unix/Linux systems allow multiple filenames to point to the same inode number. This means that the same content can be accessed with different filenames; modifications to the file's contents will affect all filenames; however, deleting one filename does not affect access to another filename. This situation is called a "hard link".
 

The ln command can create hard links:

ln source file object file
 
After running the above command, the inode number of the source file and the target file are the same, and both point to the same inode. There is an item in the inode information called "number of links", which records the total number of file names pointing to the inode, and then increases by 1. Conversely, deleting a filename reduces the "link count" in the inode node by 1. When this value is reduced to 0, indicating that there is no file name pointing to this inode, the system will recycle the inode number and its corresponding block area.

 

Here by the way the "link count" of the directory file. When a directory is created, 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), where 2 is the "hard link" of the parent directory and the ".hard link" in the current directory. Link".

 

7. Soft link
In addition to hard links, there is 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 file A is read, the system automatically directs the visitor to file B. Therefore, no matter which file is opened, file B is eventually read. 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, opening file A will report an error: "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 "number of links" of file B will not change because of this.

 

The ln -s command can create soft links.
ln -s source file or directory target file or directory

 

 
Eight, the special role of inode
This mechanism leads to some Unix/Linux-specific phenomena due to the separation of inode numbers from filenames.
  1. Sometimes, the file name contains special characters and cannot be deleted normally. At this time, directly deleting the inode node can play the role of deleting the file.
  2. Move the file or rename the file, just change the file name without affecting the inode number.
  3. After opening a file, the system identifies the file by its inode number, regardless of the file name. Therefore, in general, the system cannot know the filename from the inode number.
      Point 3 makes software updates easy and can be done without shutting down the software, no reboot required. Because the system identifies the running file by the inode number, not the 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 the software is run 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.

 

Nine practical problems

When creating a file in the /data partition of a Linux server with a low configuration (with small memory and hard disk), the system prompts that the disk space is insufficient. Use the df -h command to check the disk usage and find that the /data partition is only used 66%, and there is still 12G of remaining space. It stands to reason that this problem will not occur. Later, I checked the inode (inode) of the /data partition with df -i, and found that it was full (IUsed=100%), so the system could not create new directories and files. 

 

 

Find out why:

  There are a large number of small-byte cache files in the /data/cache directory, which occupy not many blocks, but occupy a large number of inodes. 

 

Solution:
  1. Delete some files in the /data/cache directory and 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 inode of the /opt partition to alleviate the problem of insufficient inode in the /data partition:
  ln -s /opt/newcache /data/cache 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325095443&siteId=291194637