In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail what the principle of Linux file system is, the quality of the article content is high, so Xiaobian shares it with you as a reference, I hope you have a certain understanding of relevant knowledge after reading this article.
I. Introduction to Hard Disk
Before we talk about file systems, let's look at hard disks.
It is well known that data will be lost after power failure, so modern computers use hard disks for data storage. In other words, the data in the hard disk can still be preserved after power failure.
The popular hard disks are divided into: mechanical hard disk (HDD) and solid state hard disk (SSD). Since this article focuses on the file system, the principle of the hard disk will not be introduced too much. Below is a comparison of mechanical and solid state drives:
We can think of the hard disk as a huge array, and each element of the array represents a block of data, as shown below:
In the Linux kernel, each data block is defined as the size of 4KB, so a 128GB hard disk can be divided into 33554432 data blocks, and the kernel is to read and write to the hard disk by the number of data blocks.
2 What is a file system?
As mentioned earlier, the kernel reads and writes to the hard disk in blocks, but this is very intuitive for humans because we can't remember what data is stored in each block.
To make it easier and intuitive for users to use, the Linux kernel abstracts two concepts to manage data on a hard disk: File and Directory.
File: Used to save data.
Directory: used to save file lists, of course, directories can also save directories.
Because data is stored in hard disk data blocks, the file only needs to record which data blocks belong to the current file. As shown below:
As you can see from the above figure, you can save both files and directories in the directory. The file stores the data block number belonging to the current file, so when reading and writing a file, you only need to find the data block corresponding to the file to read and write.
III. MINIX file system implementation
Now, let's use the MINIX file system to describe the file system design principles in detail. Because the MINIX file system is very simple, it is suitable for educational use.
1. MINIX Files and Directories
In the MINIX file system, the minix2_inode object describes a file. Let's look at the definition of minix2_inode:
struct minix2_inode { __u16 i_mode; //mode __u16 i_nlinks; //Number of links __u16 i_uid; //User UID __u16 i_gid; //Group ID __u32 i_size; //file size __u32 i_atime; //visit time __u32 i_mtime; //modify time __u32 i_ctime; //Creation time __u32i_zone[10]; //Number of data blocks corresponding to the number of files};
We need to pay special attention to the i_zone field of the minix2_inode object, which is used to record the block numbers belonging to the current file. By definition, i_zone is an array of integers for 10 elements, so does this mean that MINIX files can only hold 40 KB of data?
The answer is no, because the MINIX file system divides the i_zone array into four parts: the first seven elements point directly to the block numbers where the data is stored, that is, the data is stored directly on these blocks, while the eighth element points indirectly at the first level, the ninth element points indirectly at the second level, and the tenth element points indirectly at the third level. We illustrate this relationship with the following diagram:
With this multi-level pointing, a MINIX file can hold more than 40KB of data.
If there are objects describing files, then there should also be objects describing directories, right? In the MINIX file system, directories are also described using minix2_inode objects. So how do you distinguish between files and directories?
There is a field named i_mode in the minix2_inode object, which holds the type corresponding to minix2_inode. Ordinary files are represented by the S_IFREG flag, while directories are represented by S_IFDIR. So essentially, a directory is a special kind of file.
The data blocks of ordinary files store the data of files, so what are the data blocks of directories stored? The answer is the file list, and each entry in the file list is represented by a minix_dir_entry object, defined as follows:
struct minix_dir_entry { __u16 inode; char name[0]; };
inode: The index of the inode array where the minix2_inode object corresponding to the current file is located. We can temporarily ignore the role of this field, which will be described below.
name: used to record the file name of the current file. Since the length of the file name is not fixed, a flexible array (variable size data) is used to represent it.
The following diagram illustrates the difference between the data content pointed to by files and directories:
The image above shows two distinct differences between files and directories:
The i_mode field for files is set to S_IFREG, and the i_mode field for directories is set to S_IFDIR.
The data block pointed to by the i_zone field of a file holds the data of the file, while the data block pointed to by the i_zone field of a directory holds the list of files.
2. MINIX file system formatting
Now that we have a basic understanding of how the MINIX file system stores files and directories, we will describe how the MINIX file system manages files and directories on the hard disk, which is what we often call formatting.
As mentioned earlier, we can think of the hard disk as a huge array of data blocks, so MINIX file system will divide the hard disk into the following parts, as shown in the following figure:
Below we explain these parts:
Boot block: Takes up a block of data for use when the operating system boots, we can ignore it.
Superblock: occupies a data block, used to save file system information, MINIX file system uses minix_super_block object to save file system information, such as inode bitmap occupies several data blocks, data block bitmap occupies several data blocks, etc.
inode bitmap: Takes up several blocks of data describing which members of the inode table have been used, each bit representing the use of an inode.
Block Bitmap: Occupies several blocks of data and describes which members of the block list have been used, each bit representing the use of a block.
Inode table: occupies several data blocks, composed of multiple minix2_inode objects, each minix2_inode object represents a file or directory.
Block list: Occupies several blocks of data used to store the file's data.
The above figure is the format structure of MINIX file system in hard disk. Let's first take a look at what information is recorded in super block. Super block is represented by minix_super_block object, which is defined as follows:
struct minix_super_block { __u16 s_ninodes; //Number of elements in the inode table __u16 s_nzones; //Number of elements in block list (v1 version) __u16 s_imap_blocks; //Number of blocks occupied by inode bitmap __u16 s_zmap_blocks; //Number of blocks occupied by block bitmap __u16 s_firstdatazone; //first data block start number __u16 s_log_zone_size; __u32 s_max_size; //Maximum file size __u16 s_magic; //magic number (used to identify MINIX file system) __u16 s_state; //file system status __u32 s_zones; //Number of elements in the block list (v2 version) };
minix_super_block The role of each field is explained in the comments, through the minix_super_block object we can learn about the MINIX file system information.
3. Read file process
Now that you know the MINIX file system structure, let's look at how the MINIX file system reads files.
For example, if we want to read the contents of the/home/file.txt file, how does the MINIX file system accurately find the file and read its contents? Let's describe this process step by step.
Step 1: Read the root directory
To read the/home/file.txt file, start with the root directory/, which MINIX file system conventions store using the first element of the inode table. As shown below:
As shown above, the root directory is stored using the first element of the inode table, and then the directory home is looked up from the file list in the root directory. As you can see from the figure above, the inode index of the home directory is 5, which means that the home directory is stored in the fifth element of the inode table.
Step 2: Read the home directory
After knowing that the inode index of the home directory is 5, read the fifth element of the inode table, and then look for the file file.txt from the file list of the home directory. The process is as follows:
As shown above, the file.txt file found in the file list of the home directory has an inode index of 9, so you can now get the inode node corresponding to the file.txt file by reading the 9th element of the inode table.
Step 3: Read the contents of file.txt file
Now that we know the inode index corresponding to the file.txt file, reading the 9th element from the inode table can get the inode node of the file.txt file, and then you can read the contents of the file through the data block pointed to by the i_zone field of the inode node, as shown in the following figure:
As shown in the figure above, after obtaining the inode node of file.txt file by reading the 9th element of the inode table, you can read the contents of the file through the data block pointed to by the i_zone field of the inode node.
Also note that inode bitmaps and data block bitmaps are used to quickly find which inode nodes and data blocks are not used when creating files.
Although Linux systems have multiple file systems, the basic idea is how to effectively manage data on a hard disk. Therefore, mastering the MINIX file system design is very helpful for understanding other different file systems.
What is the principle of Linux file system to share here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
# = alarm mode 1 use Wechat official API = to be added # = alarm mode 2 =
© 2024 shulou.com SLNews company. All rights reserved.