A preliminary understanding of the file system through inode

What is inode

Inode is translated into Chinese as index node. After each storage device or partition of a storage device (storage device is a hard disk, floppy disk, U disk…) is formatted as a file system, there should be two parts, one part is the inode and the other part It is Block. Block is used to store data. The inode is the information used to store this data. This information includes file size, owner, user group to which it belongs, read and write permissions, etc. The inode indexes information for each file, so the inode value is available. According to the instructions, the operating system can find the corresponding file fastest through the inode value.
For example, in a book, Block is each page of it, and inode is equivalent to its directory. We can quickly find the content of a certain page with directories in various countries.

Contents of inode

We can view the inode information of a certain file through the stat command

Information in inode

  • The number of bytes of the file, the number of blocks
  • User ID of the file owner
  • Group ID of the file
  • File read, write, and execute permissions
  • There are three timestamps for a file: 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.
  • The number of links, that is, how many file names point to this inode
  • The location of the file data block
  • inode number (corresponding to the subscript of the array)

inode size

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.
You can use the df command to view the total number of inodes in each hard disk partition and the number that has been used.
Please add image description
To view the size of each inode node, you can use the following command:

sudo dumpe2fs -h /dev/sda5 | grep "Inode size"

Please add a picture description
Since each file must have an inode, it may happen that the inodes are used up but the hard disk is not full. At this time, new files cannot be created on the hard drive.

inode number

Each inode has a number, and the operating system uses the inode number to identify different files. 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.
You can use the ls -i command to view the inode number of the file
Please add a picture description

Directory file

Opening a directory actually means opening the directory file.
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 simply lists all file names in a directory file:
Please add image description
The ls -i command can view the file name and inode number of the file
Please add an image description
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.
Please add a picture description
The read permission (r) and write permission (w) of the directory file are both for the directory file itself. Since there are only file names and inode numbers in the directory file, if you only have read permission, you can only get the file name and not other information, because other information is stored in the inode node, and reading the information in the inode node requires the execution of the directory file. permission(x).

Hard links and soft links

What is a hard link

In the Linux file system, files with the same inode value are hard link files. That is to say, different file names may have the same inode, and one inode value can correspond to multiple files. Understanding linked files is not difficult, just look at the examples. In Linux, link files are created through the ln tool.
(1) Hard link, which exists in the form of a copy of a file. But it doesn’t take up actual space.

(2) It is not allowed to create hard links to directories.

(3) Hard links can only be created in the same file system.

(4) Deleting one of the hard link files does not affect other files with the same inode number.

What is a soft link

Similar to the shortcut of Windows, it creates a quick access path for files. It depends on the original file and is no different from ordinary files. The inodes point to the same file block on the hard disk. When there is a problem with the original file, the link becomes unavailable. The ln -s command can create soft links.

(1) Can be applied to directories

(2)Can cross file systems

(3) It will not increase the number of links to the linked file

(4) The size is the total number of characters contained in the specified absolute path

(5)Have its own inode number

(6)Permissions are irrelevant

Borrowed a code

#include <stdio.h>
#include <unistd.h>

int main(int argc, char *argv[])
{<!-- -->
    int ret;
    if(argc < 5)
    {<!-- -->
        printf("please check the input file!");
        return 1;
    }
    //hard link
    ret = link(argv[1], argv[2]);
    
    if(ret)
    {<!-- -->
        printf("link failed!");
        return 0;
    }
    printf("link %s to %s success", argv[1], argv[2]);
    
    //soft connection
    ret = symlink(argv[3], argv[4]);
    
    if(ret)
    {<!-- -->
        printf("symlink failed!");
        return 0;
    }
    printf("symlink %s to %s success", argv[3], argv[4]);
    return 0;
}

Compile the program in Linux: gcc link.c -o link, generate a file named link, and then run
./link good good1 bad bad1 command uses two connections to generate new files, as shown below:

The file generated by hard link is good1, and the file generated by soft link is bad1. Obviously, the file ID of the file generated by hard link is the same as the source file, but the ID of soft link is different. The file generated by the hard link is exactly the same as the source file. If any one file is changed, the other one will also change; the file generated by the soft link is equivalent to the shortcut of the source file.

#include <stdio.h>
#include <unistd.h>

int main(int argc, char *argv[])
{<!-- -->
    int ret;
    
    if(argc < 3)
    {<!-- -->
        printf("please check the file!\\
");
        return 1;
    }
    
    //delete hard link
    ret = unlink(argv[1]);
    if(ret)
    {<!-- -->
        printf("unlink %s failed!\\
", argv[1]);
        return 1;
    }
    printf("unlink %s success!\\
", argv[1]);
    
    //Delete soft link
    ret = unlink(argv[2]);
    if(ret)
    {<!-- -->
        printf("unlink %s failed!\\
", argv[2]);
        return 1;
    }
    printf("unlink %s success!\\
", argv[2]);
    
    return 0;

}

Based on the above, call the function to delete the connection and delete the connection program.

Initial understanding of the file system through inode

Inode is equivalent to a directory of files. We can know the size, location and other information of the file by looking at the inode of the file, which is convenient for us to find. Sometimes we can also delete the file by deleting the inode number. 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.