Home > System Tutorial > LINUX > inode records under Linux

inode records under Linux

PHPz
Release: 2024-02-11 09:51:29
forward
1059 people have browsed it

Files are stored on the hard drive.

The smallest storage unit on 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 it sector by sector, which is inefficient.

Instead, it reads multiple sectors consecutively, i.e. one block at a time.

This "block" consists of multiple sectors and is the smallest unit of file access.

Block size, the most common is 4kb, which is a row of eight sectors forming a block.

inode records under Linux

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

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
Copy after login

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.
inode records under Linux

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. Assume that in a 1GB hard disk, the size of each inode node is 128 bytes, and one 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 in each hard disk partition and the number that has been used, you can use the df command.

df -i
Copy after login

To view the size of each inode node, you can use the following command:

sudo dumpe2fs -h /dev/hda | grep "Inode size"
Copy after login

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.

inode number

inode records under Linux

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, this 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
Copy after login

Directory file

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
Copy after login

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

ls -i /etc
Copy after login

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
Copy after login

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; modifying 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 源文件 目标文件
Copy after login

运 行上面这条命令以后,源文件与目标文件的inode号码相同,都指向同一个inode。inode信息中有一项叫做”链接数”,记录指向该inode的文 件名总数,这时就会增加1。反过来,删除一个文件名,就会使得inode节点中的”链接数”减1。当这个值减到0,表明没有文件名指向这个inode,系 统就会回收这个inode号码,以及其所对应block区域。

这里顺便说一下目录文件的”链接数”。创建目录时, 默认会生成两个目录项:”.”和”..”。前者的inode号码就是当前目录的inode号码,等同于当前目录的”硬链接”;后者的inode号码就是当 前目录的父目录的inode号码,等同于父目录的”硬链接”。所以,任何一个目录的”硬链接”总数,总是等于2加上它的子目录总数(含隐藏目录),这里的 2是父目录对其的“硬链接”和当前目录下的”.硬链接“。

inode records under Linux

软链接

除了硬链接以外,还有 一种特殊情况。文件A和文件B的inode号码虽然不一样,但是文件A的内容是文件B的路径。读取文件A时,系统会自动将访问者导向文件B。因此,无论打 开哪一个文件,最终读取的都是文件B。这时,文件A就称为文件B的”软链接”(soft link)或者”符号链接(symbolic link)。

这 意味着,文件A依赖于文件B而存在,如果删除了文件B,打开文件A就会报错:”No such file or directory”。这是软链接与硬链接最大的不同:文件A指向文件B的文件名,而不是文件B的inode号码,文件B的inode”链接数”不会因此 发生变化。

ln -s命令可以创建软链接。

ln -s 源文文件或目录 目标文件或目录

inode的特殊作用

由于inode号码与文件名分离,这种机制导致了一些Unix/Linux系统特有的现象。

\1. 有时,文件名包含特殊字符,无法正常删除。这时,直接删除inode节点,就能起到删除文件的作用。

\2. 移动文件或重命名文件,只是改变文件名,不影响inode号码。

\3. 打开一个文件以后,系统就以inode号码来识别这个文件,不再考虑文件名。因此,通常来说,系统无法从inode号码得知文件名。

第3点使得软件更新变得简单,可以在不关闭软件的情况下进行更新,不需要重启。因为系统通过inode号码,识别运行中的文件,不通过文件名。更新的时 候,新版文件以同样的文件名,生成一个新的inode,不会影响到运行中的文件。等到下一次运行这个软件的时候,文件名就自动指向新版文件,旧版文件的 inode则被回收。

实际问题

在一台配置较低的Linux服务器(内存、硬盘比较小)的/data分区内创建文件时,系统提示磁盘空间不足,用df -h命令查看了一下磁盘使用情况,发现/data分区只使用了66%,还有12G的剩余空间,按理说不会出现这种问题。 后来用df -i查看了一下/data分区的索引节点(inode),发现已经用满(IUsed=100%),导致系统无法创建新目录和文件。

查找原因:

/data/cache目录中存在数量非常多的小字节缓存文件,占用的Block不多,但是占用了大量的inode。

解决方案:

1、删除/data/cache目录中的部分文件,释放出/data分区的一部分inode。

2、用软连接将空闲分区/opt中的newcache目录连接到/data/cache,使用/opt分区的inode来缓解/data分区inode不足的问题:

ln -s /opt/newcache /data/cache
Copy after login

The above is the detailed content of inode records under Linux. 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