Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The basic concept of Linux inode

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "the basic concept of Linux inode". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn the basic concepts of Linux inode.

Before we understand inode, let's take a look at the disk sector, the smallest unit of the disk. We all know that a disk sector is 512 bytes. Disk read and write are operated according to blocks, a block is generally 4K, that is, 8 consecutive sectors, read and write in blocks is to improve performance.

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 date the file was created, the size of the file, and so on. The area where the meta-information of the file is stored is called inode, which is translated into Chinese as "index node".

Inode node storage and data "block" storage on disk are in two different areas. Inode stores file metadata, but does not include variable-length file name information, so the inode size is fixed and contains the following metadata:

* number of bytes in the file

* User ID of the file owner

* Group ID of the file

* permissions to read, write and execute files

* there are three timestamps for a file: ctime refers to the time when the inode was last changed, mtime refers to the last time the file content 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

* location of file data block

You can use stat file_name to view the inode data of a file, and 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 inode.

The size of each inode node is typically 128byte or 256byte. The total number of inode nodes, which is given when formatted, usually sets one inode per 1KB or per 2KB. Suppose that in a 1GB hard disk, the size of each inode node is 128bytes, and one inode is set for each 1KB, then the size of the inode table will reach 128MB, accounting for 12.8% of the whole hard disk.

Therefore, for storing a large number of small files, the inode of the file system may not be enough, and if the inode resources are exhausted, you will not be able to create new files. For example, Ali's TFS (distributed storage designed to store small files) is to merge small files into "large" files to store, saving inode resources.

You can use df-I to view the Inode usage of each partition:

[luoxn28@localhost] $df-I

Filesystem Inodes IUsed IFree IUse% Mounted on

/ dev/sda3 9283072 297711 8985361 4%

Devtmpfs 180632 384 180248 / dev

Tmpfs 184648 1 184647 1 per dev/shm

Tmpfs 184648 914 183734 / run

Tmpfs 184648 16 184632 1% / sys/fs/cgroup

/ dev/sda1 153600 334 153266 / boot

Tmpfs 184648 20 184628 1% / run/user/1000

The process opens the innode state chart as follows:

Because the File object in the process has an independent file offset (current file offset), multiple processes can read and write data from different locations of the file, but this is generally not recommended, because the system does not guarantee the atomicity of writing in this case, and multi-processes can protect the contents of the file through file locks.

Innode number

Each inode has a number, and the operating system uses inode numbers to identify different files. Instead of using file names within the Unix/Linux system, inode numbers are used to identify files. For the system, the file name is just an alias or nickname for the inode number to identify.

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, it obtains the inode information through the inode number; finally, according to the inode information, it finds the block where the file data is located and reads the data. Use the * ls-I file_name* command to view the inode number.

Catalog file

A directory is also a kind of file, and it also has its own inode. Its inode itself stores the meta-information of the directory, and the corresponding block content stores a series of files (directory + file), each file including the file name and its corresponding inode number.

Hard link and soft link

Both hard links and soft links correspond to the same file block, except that:

Hard links: two file names correspond to the same inode node, except that the number of connections in the inode node is 2. Deleting one of the hard links will not actually delete the file, but will delete the file only when the number of inode connections is 0.

Soft connection: two file names correspond to their respective inode nodes, and the two inode nodes point to the same file block. After the file is deleted by the original file name, the soft connection established cannot be used, and the soft connection is similar to the shortcut of windows.

At this point, I believe you have a deeper understanding of "the basic concept of Linux inode". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report