EXT2 File System

Source: Internet
Author: User

For file systems, files are only a series of data blocks that can be read and written. The file system does not need to know where the data blocks should be placed on the physical media. These are device-driven tasks. Whenever a file system needs to read information or data from a block device that contains it, it requests the underlying device to drive a block of data that reads an integer multiple of the base block size. The EXT2 file system divides the logical partitions it uses into block groups. Each block group copies the information that is most important to the integrity of the file system and considers the actual file-box directory as information and data blocks.

The file system manages a logical space, which is like a large array, and each element of the array is the basic unit of the file system operation-the logical block. The logical block is numbered starting at 0, and the logical block is contiguous, and the logical block is relative to the physical block. Typically, the physical block of a EXT2 occupies one or several contiguous sectors.

In general, only the block group 0 Super block is read into memory, the other block group of Super Block only as a backup. During system operation, the super block is copied to the memory system buffer.

block bitmap and node bitmap

In the EXT2 file system, using bitmaps to describe the use of blocks and index nodes, each block group occupies two blocks, that is, one is used to describe the usage of the block, and the other describes the use of the group's index nodes. These two blocks are called data bitmap blocks and index node bitmap blocks, respectively. Each of the data bitmap blocks represents the usage of each block in the block group, and if 0, indicates that the corresponding block is idle and, if 1, is allocated.

Index Node Table

The index nodes in each block group are stored in their respective index node tables, and are stored sequentially by index node number. The Index node table usually takes up several blocks of data, and the blocks used by the index nodes table are called into the block cache like normal chunks.

EXT2 the logical structure of the entire disk:

Several data structures of the EXT2 file System 1 EXT2 Super Block

The Super block contains information describing the basic size and morphology of the file system. File System Manager uses them to use and maintain file systems. The EXT2 super block is the data structure used to describe the overall information of the EXT2 file system, which is the core of EXT2. After the Super block is read into the function ext2_fill_supter, an image super_block.u.ext2_sb_info structure is built in memory.

The structure Ext2_super_block is listed as follows:

Logical blocks are numbered starting at 0, for file systems with block sizes of 1KB, s_first_data_block to 1, and 0 for other file systems.

2 EXT2 Super block information structure

EXT2 Super block is read into memory, mainly used to fill the VFS super block. In addition, it has to fill in another structure ext2_super_info. The structure Ext2_super_info describes the EXT2 file system-specific information. The reason for this structure is that the VFS Super block must be compatible with the different super block structures of various file systems, so that each filesystem Super Block specific feature must be stored in memory with another structure to speed up the operation of the file. The relationship between the EXT2 super block and the VFS Super Block:

The structure Ext2_super_info is listed as follows:

3 operation function Structure of Super block

The operation function structure of the Super block is as follows:

4 index node inode for EXT2

Each file and directory in the EXT2 file system is described by the unique inode. The EXT2 inode for each block group is saved in the Inode table, and a bitmap is used by the system to track the allocated and unassigned Inode.

The EXT2 file system uses an index node to log file information. Each normal file box directory has a unique index node corresponding to it, and the index node contains important information about the file or directory. When you want to access a file or directory, the corresponding index node is first found by the file or directory name, and then the index node gets the information of the file or directory and the specific storage location on the disk.

EXT2 the data structure of the index node on the hard disk is as follows:

EXT2 the mapping of logical blocks to physical blocks through an array of data block pointers in the index node. In the EXT2 index node, there are 15 items in the data block, the first 12 are direct pointers, the last three are "one indirect pointer", "two indirect pointers", "three indirect pointers" respectively, EXT2 the default physical block size is 1KB, the block address is 4 bytes, so each physical block can store 256 addresses. This way, the file size can be up to 12KB+256KB+64MB+16GB. But in fact, Linux is a 32-bit system, so file size can only be 4GB, and the entire file system is occupied by a file. An array of physical block pointers to the index nodes:

The system looks for the physical block as the logical block number for the index. For example, to find the physical block corresponding to the 100th logical block, because of the 256+12>100>12, we use an indirect block to find the 88th item in an indirect block, which is the address of the corresponding physical block.

The nodes are numbered on the disk. Some of these nodes have a special purpose and cannot be used by users. These special nodes are defined as:

5 node information structure of EXT2 file system

Similar to the EXT2 Super block, when the index node on the disk into memory, in addition to fill out the index node of the VFS, the system will also fill in another data structure ext2_inode_info, its role is to store the specific file system's own characteristics.

The structural Ext2_inode_info analysis is as follows:

There is no field in the VFS index node that has an array of physical block pointers, and this EXT2-specific information must remain in the EXT2_INODE_INFO structure after it is transferred into memory.

6 node Operation function Structure

7 File Operation function structure

8 Group Descriptor for the EXT2 file system

The block that follows the super block in the Block group is the group descriptor, at the start_sect+block_size*2 position. The reason for multiplying by 2 is that block_size is the KByte unit that we use to access the sector. Group Descriptor list Each item is a group descriptor, which is a data structure called Ext2_group_desc, for 32 bytes. It is used to describe the overall information of a block group.

When a group descriptor is found, you get a lot of information, such as the location of the block group bitmap, the location of the block Inode bitmap, the location of the Group Inode table, and so on.

The group descriptor is defined as follows:

EXT2 File System Setup process

In the function Init_ext2_fs registered the EXT2 file system, the function analysis is as follows:

The file System Instance Ext2_fs_type is listed below:

The function EXT2_GET_SB opens the block device, gets the super block, adds the super block to the global super_blocks linked list, and hooks up the file system. The function EXT2_GET_SB is listed as follows:

The Ext2_fill_super function reads the super block data from the hard disk and populates the super block structure.

The example Ext2_sops of the Super block operand set is listed below:

Read and write files

The read and write operation of the file calls to the structure of the read-write operation function of the specific file system, which calls to the ext2_file_operations structure of the EXT2 file system and obtains the concrete operation function.

For the Read system call operation function hierarchy graph, visible EXT2 file system read operation final call ext2_readpage function completion, for the file read operation process of file-to-memory mapping process.

The structure ext2_file_operations is the EXT2 file system for the file operation function set, listed as follows:

Ext2_aops structure is a set of functions implemented in EXT2 file system, which is called by the function of ext2_file_operations structure. Listed below:

These functions in the EXT2_AOPS structure are listed as follows:

These functions, in addition to calling the Ext2_get_block function, are unique to the EXT2 file system, and are all functions in the fs/buffer.c.

Ext2_get_block () converts the logical block number of the file system to the logical block number of the block device . This conversion relationship is the first 12 entries of the i_block[] array described in the EXT2_INODE structure as the Direct Index table, the 13th item is an indirect index block pointer, the 14th item is a binary index block pointer, and the 15th item is a triple index block pointer. When the file length is not more than 12 blocks, the target block can be directly located by the Direct Block Index table, when the file length exceeds 12, and the remaining parts do not exceed the number of indirect block index, the target block is positioned in the indirect block index block, and so on.

the function Ext2_get_block function is to get the corresponding physical block from the logical block ordinal . If the corresponding physical block is deleted, it is reassigned and gets its indirect block path.

The function ext2_get_block is specifically analyzed as follows:

The system looks for the physical block as the logical block number for the index. For example, to find the physical block corresponding to the 100th logical block, because of the 256+12>100>12, we use an indirect block to find the 88th item in an indirect block, which is the address of the corresponding physical block.

The function Ext2_get_branch reads data from the physical block into the chain buffer, and the parameters of the function are described as follows:

    • node for inode operation
    • Depth the depth of the indirect block
    • An array of pointers to offset indirect physical blocks
    • Chain storage reads data from a physical block
    • Store Error Flags

function Ext2_get_branch is a function that fills an array of indirect structures and returns NULL if it is healthy. The function analysis is as follows:

The function Ext2_alloc_branch allocates and establishes a block list, where the parameter inode represents the node where the block is to be allocated, the parameter num represents the depth of the indirect block, the parameter offsets is an array, or offsets[num], and the parameter branch is where the linked list is stored. The function analysis is as follows:

Data block allocation policy when file extension

A common problem with file systems is fragmentation, where a file contains chunks of data throughout the file system, which makes sequential access to file data blocks more and more slow. The EXT2 file system attempts to resolve this problem by allocating a new block that is adjacent to the current file data block at the physical location, or at least in the same block group.

EXT2 directory entry and document positioning

In EXT2, a directory is a special file that is a list of ext2_dir_entry structures. In order to reduce the waste of disk space, this structure is variable-length. However, it also has a certain length limitation: first, the file name can only be 255 characters long, second, although the file name length may be unlimited, but the system automatically turns it into 4 integer times, insufficient place with 0 padding.

Link file

EXT2 stores the file name and information separately, where the file information is described by an index node, and the directory entry is used to contact the file name and index node. In a directory entry, each pair of file names and an index node number one by one corresponds to a link, which means that the same Index node table can correspond to several different file names. This link is called a hard link. You can use the LN command to create a new hard link for an existing file:

Ln/home/cyf/file1/home/cyf/file2

Set up a file file2, linked to File1. File2 and File1 have the same index node number, that is, share the same index node with file1. After a new hard link has been established, the I_links_count value in this index node will add 1,i_links_count value to reflect the number of files linked to this index node.

The advantage of using hard links is that:

1) Because the deletion of the file, actually first to I_links_count minus 1, if I_links_count is not 0, then the end, that is, just delete a hard link, the actual file data is not deleted. The file is actually deleted from disk only if I_links_count is 0 o'clock. This way, you can make multiple links to important files to prevent files from being deleted.

2) allows the user to process files under the directory without entering a directory.

The biggest difference between a symbolic link and a hard link is that it does not establish a link to the index node, that is, when a symbolic link is established for a file, the link count of the index node does not change. When you delete a file, its symbolic link file also loses its function, and when you delete a file's symbolic link file, it has no effect on the file itself.

EXT2 File System

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.