Home > System Tutorial > LINUX > File metadata in Linux systems: inode detailed explanation

File metadata in Linux systems: inode detailed explanation

WBOY
Release: 2024-02-10 09:09:13
forward
660 people have browsed it

Inode is an important data structure in Linux systems. It is used to store file metadata, such as file type, size, permissions, timestamp, number of links, data block location, etc. Inode is the core component of the file system, which can be used to create, delete, modify, search and other operations on files. In this article, we will introduce the principles and characteristics of inodes, including inode numbering, allocation, release, search, indexing, etc., and give examples of their usage and precautions.

File name -> inode -> device block

File metadata in Linux systems: inode detailed explanation

1. What is inode?

To understand inode, we must start with file storage.

Files are stored on the hard disk. 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 it sector by sector, which is too inefficient. Instead, it reads multiple sectors continuously at one time, that is, it reads one "block" at a time. . This "block" consisting 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, etc. This area that stores file metainformation is called inode, and its Chinese translation is "index node".

2. Contents of inode

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

* 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 files: ctime refers to the time when the inode was last changed, mtime refers to the time when the file content was last changed, and atime refers to the time when the file was last 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 certain 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.

3. Inode size

Inode also consumes 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, usually one 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 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 in each hard disk partition and the number that has been used, 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 may happen that the inodes have been used up, but the hard disk is not yet full. At this time, new files cannot be created on the hard drive.

4. 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. For the system, the file name is just an alias or nickname for the inode number for easy identification. On the surface, the user opens the file by the file name. In fact, the process within 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, based on the inode information, it 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. Directory files

In Unix/Linux systems, a directory is also a kind of file. Opening a directory actually means opening the directory file.

The structure of a directory file is very simple, which 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.

ls command only lists all file names in directory files:

ls /etc

ls -i command lists the entire directory files, that is, file names and inode numbers:

ls -i /etc

If you want to view the detailed information of the file, you must access the inode node and read the information according to the inode number. The ls -l command lists detailed information about a file.

ls -l /etc

6. Hard link

Generally, the file name and the inode number are in a "one-to-one correspondence" relationship, and each inode number corresponds 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; modification of the file content will affect all file names; however, deleting one 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 inode numbers of the source file and the target file are the same and 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 will increase by 1 at this time. Conversely, deleting a file name will reduce the "number of links" in the inode node by 1. When this value decreases 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.

By the way, let’s talk about the “number of links” of the directory file. 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 links" of the parent directory and the "hard links" of 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 reading file A, the system will automatically direct the visitor to file B. Therefore, no matter which file is opened, file B is ultimately 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 for existence. If file B is deleted, an error will be reported 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. The inode "link number" of file B will not change as a result.

ln -s command can create soft links.

ln -s source file or directory target file or directory

8. Special functions of inode

Because 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. At this time, directly deleting the inode node can play the role of deleting the file.

2. Moving files or renaming files only changes the file name and does not affect the inode number.

3. After opening a file, the system identifies the file based on the inode number and no longer considers the file name. Therefore, generally speaking, the system cannot learn the file name from the inode number.

Point 3 makes software updates simple, and can be updated without closing the software and without restarting. Because the system identifies running files through the inode number, not the file name. When updating, the new version of the file will generate a new inode with the same file name, which will not affect the running file. The next time you run this software, 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 issues

When creating a file in the /data partition of a Linux server with low configuration (small memory and hard disk), the system prompts that there is insufficient disk space. Use the df -h command to check the disk usage and find that the /data partition Only 66% is used, and there is still 12G of remaining space. It stands to reason that this problem will 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%), causing the system to be unable to create new directories and files.

Find the reason:

There are a very large number of small-byte cache files in the

/data/cache directory, which occupy not many Blocks but a large number of inodes.

solution:
1. Delete some files in the /data/cache directory and release some inodes in the /data partition.
2. Use a soft connection 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 inodes in the /data partition:
ln -s /opt/newcache /data/cache

Through this article, we understand the principles and characteristics of inode, which can be used to manage and operate files. We should choose an appropriate file system based on actual needs and follow some basic principles, such as avoiding inode exhaustion, regularly checking inode status, using hard links or soft links, etc. Inode is one of the most basic concepts in the Linux system. It can abstract and encapsulate files, and can also improve the performance and reliability of the file system. I hope this article can be helpful and inspiring to you.

The above is the detailed content of File metadata in Linux systems: inode detailed explanation. For more information, please follow other related articles on the PHP Chinese website!

source:lxlinux.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template