Understand the implementation of Linux kernel file system

Insert picture description here
To implement the Linux kernel file system, first, Linux file management introduces the way Linux manages files from the user level. Linux has a tree structure to organize files. The top of the tree is the root directory (/), the nodes are directories, and the leaves at the end are files containing data. When we give the full path of a file, we start from the root directory, go through various directories along the way, and finally reach the file.
We can perform many operations on files, such as opening and reading and writing. In Linux file management related commands, we see many commands that operate on files. Most of them are based on file opening and reading and writing operations. For example, cat can open files, read data, and finally display in the terminal:
$cat test.txt
For programmers under Linux, understanding the underlying organization of the file system is necessary for in-depth system programming. Even ordinary Linux users can design better system maintenance programs based on related content.
Storage device partition
The ultimate goal of the file system is to organize large amounts of data into persistent storage devices, such as hard drives and disks. These storage devices are different from memory. Their storage capacity is durable and will not disappear due to power failure; the storage capacity is large, but the reading speed is slow.
Observe common storage devices. The first area is MBR, which is used for Linux booting (refer to Linux booting). The remaining space may be divided into several partitions. Each partition has a related partition table (Partition table), which records information about the partition. This partition table is stored outside the partition. The partition table describes the starting position of the corresponding partition and the size of the partition.
We often see C partition and D partition in Windows system. There can also be multiple partitions under the Linux system, but they are all mounted on the same file system tree. The data is stored in a certain partition.
A typical Linux partition (partition) contains the following parts: The first part of the partition is the boot block (Boot block), which is mainly used to boot the computer. After Linux starts up, it will load MBR first, and then MBR loads programs from the boot area of ​​a certain hard disk. This program is responsible for further loading and starting of the operating system. In order to facilitate management, even if no operating system is installed in a certain partition, Linux will reserve a boot area in the partition.
After the startup zone is the super block. It stores information about the file system, including the type of file system, the number of inodes, and the number of data blocks.
Followed by multiple inodes, they are the key to achieve file storage. In the Linux system, a file can be divided into several data blocks for storage, just like Dragon Ball scattered everywhere. In order to successfully collect Qi Dragon Ball, we need a "radar" guide: the inode corresponding to the file. Each file corresponds to an inode. This inode contains multiple pointers to each data block belonging to the file. When the operating system needs to read the file, it only needs the "map" corresponding to the inode to collect scattered data blocks, and then we can harvest our files. The last part is the data blocks that actually store the data.
Introduction to inodes
Above we saw the macro structure of storage devices. We need to go deep into the structure of the partition, especially the way files are stored in the partition.
A file is a division unit of data in a file system. The file system uses directories to organize files and gives files a hierarchical structure. The key to achieving this hierarchical structure on the hard disk is to use inodes to virtualize ordinary files and directory file objects.

In Linux file management, we know that in addition to its own data, a file also has additional information, namely the metadata of the file. This metadata is used to record many information about the file, such as file size, owner, group to which it belongs, modification date, etc. Metadata is not included in the file data, but is maintained by the operating system. In fact, this so-called metadata is contained in the inode. We can use $ls -l filename to view these metadata. As we have seen above, the area occupied by the inode is different from the area of ​​the data block. Each inode has a unique integer number (inode number).

In preserving metadata, inode is the key to "file" from abstract to concrete. As mentioned in the previous section, inode storage consists of some pointers that point to some data blocks in the storage device, and the contents of the file are stored in these data blocks. When Linux wants to open a file, it only needs to find the inode corresponding to the file, and then collect all the data blocks along the pointer to form the data of a file in memory.
Inodes are not the only way to organize files. The easiest way to organize files is to put the files in the storage device in sequence, and DVD takes a similar approach. But if there is a delete operation, the free space caused by the deletion is mixed among the normal files, which is difficult to use and manage.
A linked list can be used in a complex way. Each data block has a pointer to the next data block belonging to the same file. The advantage of this is that scattered free space can be used, but the disadvantage is that the operation of the file must be performed in a linear manner. If you want random access, you must traverse the linked list to the target location. Since this traversal is not performed in memory, the speed is very slow.
The FAT system takes out the pointer of the above linked list and puts it into an array of memory. In this way, FAT can quickly find a file based on the memory index. The main problem with this is that the size of the index array is the same as the total number of data blocks. Therefore, if the storage device is large, the index array will be relatively large.
The inode can make full use of the space, and the space occupied by the memory is not related to the storage device, which solves the above problems. But inode has its own problems. The total number of data block pointers that each inode can store is fixed. If a file requires more data blocks than this total, the inode needs extra space to store the extra pointers.

Inode example
In Linux, we find a file based on the directory file along the way by parsing the path. In addition to the file name contained, the entries in the directory also have the corresponding inode number. When we enter cat / var / test .txt, Linux will find the inode number of the var directory file in the root directory file, and then synthesize the var data according to the inode. Then, according to the record in var, find the inode number of text.txt, collect data blocks along the pointer in the inode, and synthesize the data of text.txt. Throughout the process, we refer to three inodes: root directory file, var directory file, text.txt file inodes. Under Linux, when cat /var/test.txt can be used, Linux will find the inode number of the var directory file in the root directory file, and then synthesize the var data according to the inode. Then, according to the record in var, find the inode number of text.txt, collect data blocks along the pointer in the inode, and synthesize the data of text.txt. Throughout the process, we referenced three inodes: the root directory file, the var directory file, and the inodes of the text.txt file. Under Linux, you can useC A T / V A R & lt / T E S T . T X T when , L I n- U X to the root destination recording paper member in looking to V A R & lt it a mesh catalog text member of I n- O D E eds No. , then the root data I n- O D E togetherTo V A R & lt 's number data . With post , the root according to V A R & lt In the referred record , to find the T E X T . T X T of I n- O D E eds number , along with I n- O D E of the means needle , close set number of data blocks , combined into tE X T . T X T is the number of data . Whole a through process in , I have reference test of three months I n- O D E : root entry record text items , V A R & lt mesh record text items , T E X T . T X T packets member of I n- O D E S. In the L I n- U X under , it may be to make use stat filename, a file corresponding to the query inode number.
When we read a file, we actually find the inode number of the file in the directory, and then combine the data blocks according to the pointer of the inode and put them into memory for further processing. When we write a file, we allocate a blank inode to the file, record its inode number into the directory to which the file belongs, and then select blank data blocks, let the pointer of the inode point to these data blocks, and put them in the memory Data in.
File sharing
In the Linux process, when we open a file, a file descriptor is returned. This file descriptor is a subscript of an array, and the corresponding array element is a pointer. Interestingly, this pointer does not directly point to the inode of the file, but points to a file table, and then through the table, points to the inode of the target file loaded into the memory. As shown in the figure below, one process opens two files.
It can be seen that each file table records the file open status (status flags), such as read-only, write, etc., and also records the current read and write position (offset) of each file. When two processes open the same file, there can be two file tables, and the corresponding open state and current position of each file table are different, thus supporting some file sharing operations, such as simultaneous reading.
It should be noted that after the process fork, the child process will only copy the array of file descriptors, and share the file table and inode maintained by the kernel with the parent process. At this time, be especially careful about programming.
to sum up
Here is a general summary of the Linux file system. Linux uses inodes to form data into files.
Understanding the Linux file system is an important step in understanding the operating system Linux principles.

In addition, you can join groups or private chats who need learning materials such as C/C++ Linux advanced server architects.
Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_52622200/article/details/110238006