<Linux>(Minimalism is the key, saving time and effort) "Analysis of the principles of Linux operating system: Linux file management (1)" (25)

8 Linux file management

8.1 Linux file system overview

This chapter mainly introduces the structural characteristics of the EXT2 file system and the characteristics of the Linux virtual file system VFS and its implementation technology.

  1. The tree structure of the Linux file system
    The top level of Linux is the root directory, represented by /.
    Insert image description here
    Linux uses directory decomposition to manage file directories. Directory entries in the tree directory are symbol directories for files. As shown below:
    Insert image description here

  2. Linux file types
    Ordinary files
    Directory files
    Device files
    Pipeline file
    Link file (based on index node sharing)

  3. File access permissions
    Each file in Linux is owned by a specific user, and a user is generally associated with a certain user group. Therefore, Linux sets three permissions for visitor identities: file owner, users in the same group as the file owner, and other users.
    Access restrictions on files are mainly reflected in three operations of files, namely file reading, writing and execution. The 3 operation restrictions for 3 types of visitors form 9 situations, represented by 9-bit binary codes.

-rwxr-xr-x 1 user wheel 3212 Dec 4 12:36 a.out

8.2 EXT2 file system

8.2.1 Structure of EXT2 file system

A file system generally uses an independent logical partition on a block device, and establishes a tree hierarchy of the file system in this partition. An EXT2 file system consists of a sequence of logically sequenced blocks. The EXT2 file system divides the disk logical partitions it uses into several block groups, which are numbered sequentially starting from 0. Each block group contains several data blocks, which store the file contents. Each group includes 5 types of information blocks for management and control in addition to data blocks: super block, group descriptor, block bitmap, inode bitmap and inode table. These information bits
are located at the front of each block group, followed by the file's data blocks.

Insert image description here

8.2.2 EXT2 super block (super block)

EXT2 super block: A data structure used to describe the overall information of the EXT2 file system. It mainly describes the static distribution of files in logical partitions, and describes the size and quantity of various structures of the file system. The contents of the superblock contained in all block groups are the same. During system operation, the contents of the super block need to be copied to the memory buffer.

In Linux, the EXT2 file system super block is defined as the ext2_super_block structure, which is defined in /include/linux/ext2_fs.h. The EXT2 superblock consists of two parts:

Basic superblock: The overall static information of the EXT2 file system.
Expansion block: reflects some dynamic characteristics of the block group in which it is located.

The super block itself occupies one physical block (1024B), the basic block occupies 84B, the extension block occupies 20B, and the remaining 920B is defined as an array reserved[230] with an element length of 4B as a spare.

Insert image description here

8.2.3 Group descriptor

Group descriptor: records the description information of each group block (this information is needed when allocating disk space for in-place files). These group descriptors are gathered together to form a group descriptor table. A group descriptor may occupy multiple physical blocks. The same as the super block: the contents of the group descriptor in each block are exactly the same, and its contents must also be read into memory.

Insert image description here
In Linux, the group descriptor (32B) is an ext2_group_desc structure, defined in /include/linux/ext2_fs.h:

8.2.4 Block Bitmap

Block bitmap: records the usage of data blocks for each block group. It occupies one physical block. So the maximum number of data blocks in a block group is 8 times the length of a physical block. For example: For a 1024 physical block, the block bitmap has 1024*8 bits, which can represent 8K data blocks. That is to say, the maximum capacity of the data area of ​​a block group is 8M. If the logical partition used by the EXT2 file system is 100M, it can be divided into 12 block groups.

After the system is running, the block bitmap is loaded into a cache, but due to limited cache space, only the currently commonly used 8-block (default) bitmap is loaded.

8.3 EXT2 inode and file structure

8.3.1 EXT2 file system inode structure

The EXT2 file system inode in Linux is defined as struct ext2_inode. It is defined in /include/linux/ext2_fs.h.

8.3.2 Inode table and inode bitmap

inode table: A collection of inodes for all files in a block group. It may occupy multiple physical blocks. The number of inodes that each block group can contain is given by the member entry s_inoders_per_group in the superblock.

Inode bitmap: reflects the usage of each item in the inode table. Each bit of it represents an entry in the inode table. 1 used; 0 idle. Like block bitmaps, inode bitmaps are also loaded into a cache.
Insert image description here

8.3.3 Physical structure of EXT2 files

uses a hybrid index structure. There are 15 addresses in the i_block[] array in Inode. As shown in the figure below:
The default physical block size of EXT2 file is 1K, and the block address length of EXT2 is 4B, so the index table in each indirect block can include 1024/4=256 physical addresses . So

1.直接地址:允许文件不大于 12K 
2.一次间接地址:当文件大于 12K 时采用,允许文件长达 256K+12K
3.二次间接地址: 当文件大于 256K+12K 时采用,允许文件长达 256*256*K+256K+12K
4.三 次 间 接 地 址 : 当 文 件 大 于 256*256K+256K+12K 时 采 用 , 允 许 文 件 长 达256*256*256K+256*256K+256K+12K=16G+64M+256K+12K

But in fact Linux is a 32-bit file system, and the maximum file size is 4G.
The EXT2 file system searches for data blocks according to the logical block number of the file as the index value, and the logical blocks are numbered sequentially from 0.
Example: How to find the physical block corresponding to logical block number 100.

8.3.4 Directory structure of EXT2

In the Linux tree directory structure, each file directory is a directory file, and each directory entry is an ext2_dir_entry structure, which
is the symbol directory of a file. Defined in /include/linux/ext2_fs.h.

Struct ext2_dir_entry
{
    
    
_u32 inode; /*inode 号*/
_u16 rec_len; /*目录项长度*/
_u16 name_len; /*文件名长度*/
Char name[EXT2_NAME_LEN]/*文件名*/
}

EXT2_NAME_LEN defaults to 255, which means the file name can use up to 255 characters. In addition, the length of the directory entry is variable according to the length of the file name. But it must be a multiple of 4, and the unused parts are filled with \0.
When deleting a file, set the corresponding inode number field to 0, and merge them if there are adjacent blank directory entries.
When adding a file, find a blank directory entry of appropriate length and write the corresponding information; if the remaining space after using the blank entry is greater than 12B, the remaining part will still be used as a blank directory entry. If a suitable empty directory entry cannot be found, a directory entry for this file is created at the end of the file.

Guess you like

Origin blog.csdn.net/tangcoolcole/article/details/134808679