Detailed explanation of Linux index node inode

Detailed explanation of Linux index node inode

1. Introduction to inode

To understand inode, we must 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 it sector by sector, which is too inefficient. Instead, it reads multiple sectors continuously at one 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 "block" size 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 metadata, such as the creator of the file, the creation date of the file, the size of the file, and so on. This area that stores file metadata is called inode, which is translated into "index node" in Chinese.

2. Inode contains content

Each item in the data block of a directory in Linux contains the file name and its corresponding inode. The inode records the attributes of the file and the actual storage location of the file, that is, the data block number. Each block (common size 4KB) can be used to locate the file through the inode. Inode is in Linux and vnode is in Unix. Basically, the inode contains at least the following information:

(1) File type (2) File access permissions;
(3) The owner and group of the file;
(4) File size;
(5) The number of links, that is, the total number of file names pointing to the inode;
(6) The file's status change time (ctime), last access time (atime), and last modification time (mtime);
(7) File special attributes: SUID, SGID and SBIT;
(8) The actual pointer to the file content.

You can use the stat command to view the inode information of a file.

3. Inode characteristics

The number and size of inodes are fixed when the disk is formatted. The characteristics of inodes are:

(1) The size of each inode is fixed at 128B. You can use the dumpe2fs command to display ext2/ext3/ext4 file system information.

$ dumpe2fs -h /dev/sda1 | grep "Inode size"
dumpe2fs 1.41.12 (17-May-2010)
Inode size: 128

(2) Each file occupies only one inode. Therefore, the number of files that a file system can create is related to the number of inodes. When the system reads a file, it needs to find the inode first and analyze whether the permissions recorded in the inode match those of the user. Only if they match can it actually start reading the contents of the block.

4. The process of operating system reading disk files

The process of the operating system reading disk files is as follows:

(1) According to the directory where the given file is located, obtain the data entity of the directory, and find the inode of the corresponding file according to the data items in the data entity;
(2) Find the inodeTable based on the file inode;
(3) Find the corresponding block according to the corresponding relationship in inodeTable;
(4) Read the file.

The schematic diagram of the system reading disk files is as follows:

For example, if you want to read the /etc/passwd file, the reading process is as follows:

(1) Get the inode of the root directory /. Through the mount point information, we find that the inode number of the root directory is 2;

ll -di /
2 dr-xr-xr-x 19 root root 4096 Feb 14 09:32 /

(2) According to the inode of the root directory, find the data entity block of the root directory, which can be understood as a mapping table from files to inode numbers, and find the inode number of the directory etc;

ll -di /etc
786433 drwxr-xr-x 98 root root 12288 Feb 13 17:18 /etc

(3) According to the inode number of the directory etc, read the data entity block of the directory etc and find the inode number of the file passwd;

ll -i /etc/passwd
787795 -rw-r--r-- 1 root root 1552 Jan 4 14:56 /etc/passwd

(4) Based on the inode number of the /etc/passwd file, the data entity block of the /etc/passwd file can be obtained to complete the file reading.

5. Many advantages of inode

(1) For some files that cannot be deleted, you can delete them by deleting the inode node;
(2) Moving or renaming a file only changes the mapping from the file name to the inode in the directory, and does not require actual operation on the hard disk;
(3) When deleting a file, you only need to delete the inode. You do not need to actually clear the hard disk. You only need to overwrite it the next time you write to it (this is one of the reasons why deleted data can be recovered);
(4) After opening a file, you only need to identify the file through inode.

The above is a detailed explanation of the Linux index node inode. For more information about the Linux index node inode, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • linux No space left on device 500 error caused by inode fullness
  • Linux Network Setup Details
  • How to use MyCat to implement MySQL master-slave read-write separation in Linux
  • Hidden overhead of Unix/Linux forks
  • Learning about UDP in Linux
  • Linux swap partition (detailed explanation)
  • C++ Network Programming under Linux epoll technology and IOCP model under Windows
  • How many ports can a Linux server open at most?
  • Details of Linux file descriptors, file pointers, and inodes

<<:  MySQL performance optimization tips

>>:  Vue implements tree table

Recommend

How to use MyCat to implement MySQL master-slave read-write separation in Linux

Table of contents Linux-Use MyCat to implement My...

SQL serial number acquisition code example

This article mainly introduces the sql serial num...

CSS3 simple cutting carousel picture implementation code

Implementation ideas First, create a parent conta...

An article to show you how to create and use Vue components

Table of contents 1. What is a component? 2. Crea...

CSS3+HTML5+JS realizes the shrinking and expanding animation effect of a block

When I was working on a project recently, I found...

Install Kafka in Linux

Table of contents 1.1 Java environment as a prere...

Pure CSS implementation of radio and checkbox effect example

radio-and-checkbox Pure CSS to achieve radio and ...

JavaScript to achieve elastic navigation effect

This article shares the specific code for JavaScr...

Steps to deploy hyper-V to achieve desktop virtualization (graphic tutorial)

The hardware requirements for deploying Hyper-V a...

Uniapp realizes sliding scoring effect

This article shares the specific code of uniapp t...

Navicat remote connection to MySQL implementation steps analysis

Preface I believe that everyone has been developi...

Detailed explanation of Vue filters

<body> <div id="root"> <...

Detailed discussion of memory and variable storage in JS

Table of contents Preface JS Magic Number Storing...

Detailed explanation of Vue component reuse and expansion

Table of contents Overview Is the extension neces...