A glimpse of the ext2/ext3 file system in Linux

Source: Internet
Author: User
In Linux, the most traditional disk file system (filesystem) uses ext2. therefore, to understand the file system, you must start with ext2. I. file system features the partition must be formatted after the disk partition is completed before the operating system can use the partition. This is because the most traditional disk file system (filesystem) in Linux uses ext2, so to understand the file system, you must start with ext2.

I. file system features
After the disk partition is completed, you still need to format it before the operating system can use this partition. This is because the file attributes/permissions set by each operating system are different. to store the data required for these files, you need to format the partitions, to become a file system format available to the operating system. What is a partition in traditional disk and file system applications? Only one file system can be formatted, so we can say that a file system is a partition.
Www.2cto.com
In addition to file content, file data in the operating system usually has many attributes, such as file permission (rxw) and file attribute (owner, group, and time parameter) in Linux ). The file system usually stores these two parts of data in different blocks, permissions and attributes in inode, and actual data in the data block. In addition, a super block records the overall information of the entire file system, including the total amount, usage, and remaining amount of inode and block, as well as the format and related information of the file system.
Inode: records the attributes of a file. a file occupies one inode and records the block number of the data in this file.
Block: actually records the content of a file. if the file is too large, multiple blocks are occupied.

Because each inode and block has a number, each file occupies one inode, and the inode contains a block number for storing file data. Therefore, if you can find the inode of the file, you will naturally know the block number of the data stored in the file, and you will be able to read the actual data of the file.
Www.2cto.com
Assume that the permission and attribute data of a file are placed under inode 4 (small square), and the inode records the actual locations of the file data: the four block numbers, in this case, the operating system can sort the reading order of the disk accordingly, and read the four blocks at once.


II. Linux ext2 file system
The file system has planned inode and block at the beginning. The inode and block will not change unless it is reformatted (or the file system size is changed using commands such as resize2fs. To facilitate management, the ext2 file system is essentially divided into multiple block groups during formatting. Each block group has an independent inode/block/super block system. As shown in:


There is a boot sector at the beginning of the file system, which can install the boot loader, so that we can install different boot loaders to the front-end of individual file systems, instead of overwriting the unique MBR of the entire hard disk, a multi-boot environment can be created.

* Data block: where the file content is stored. The size of the block supported in the ext2 file system is 1 KB, 2 kB, and 4 kB. it is fixed when the file is formatted, each block can only store the data of one file. if the file is larger than the size of one block, one file will occupy multiple blocks. if the file is smaller than the block, the remaining space of the block cannot be used again. The ext2 file system generated by the block size is limited as follows:


* Inode table (inode table)
Inode records at least the following file data:
1. access mode of the file; (rwx)
2. ower and group of the file );
3. size of the file;
4. time when the file is created or its status changes (ctime );
5. last read time (atime );
6. last modification time (mtime );
7. flag of the file's features );
8. pointer );

The size of inode with such powerful functions is fixed to B. In addition to the file permission attribute record area, inode also has 12 direct, 1 indirect, and 1 Double indirect and 3 indirect record areas. If the 12 records direct to the number, the 12 records can directly obtain the block number. The so-called indirect method is to use another block as the block number record area. if the file is too large, indirect blocks are used to record numbers. Similarly, if the file continues to grow, the so-called double indirect mechanism will be reused. The first method only specifies where the block of the next record number is, and the actual record is in the second block. In this way, the third indirect method is to reuse the third-layer block to record numbers. As shown in:


* Super block:
Super block is very important, because the basic information of the file system is written here, a file system should have only one super block. It records the following information:
1. total number of blocks and inode;
2. number of inactive and used inode and block;
3. size of the block and inode;
4. information about the file system, such as the Mount time of the file system, the last time data is written, and the last time the disk is verified;
5. a validbit value. if the file system has been mounted, validbit is 0. if not, validbit is 1;
Www.2cto.com
* File system Description (File system Description)
This section describes the start and end numbers of each block group and the block numbers between each block group. You can view it using dumpe2fs.

* Block bitmap (block table)
Through block bitmap, we can know which blocks are empty, so our system can quickly find available space to process files. Similarly, if you delete some files, the block numbers occupied by those files must be released, in this case, the logo corresponding to the block number in the block bitmap must be changed to "unused ".

* Inode bitmap (inode table)
Similar to block bitmap.

III. relationship with the directory tree
1. Directory: When a directory is created in the ext2 file system under Linux, ext2 allocates an inode and at least one block to the directory. Inode records the related permissions and attributes of the Directory and records the allocated block number; block records the inode data occupied by the file name and the file name in this directory.

2. File: When a file is created in the ext2 file system in Linux, ext2 allocates an inode and the number of blocks relative to the file size to the file. Note: If the file size is too large, the number of blocks must be added with indirect, double indirect, and three indirect directions pointing to the block.

3. we can see from the above that inode itself does not record the file name, and the file name is recorded in the directory block. When we want to read a file, we must pass through the inode and block of the directory before we can find the inode number of the file to be read, in the end, the data in the block of the correct file will be read. Because the directory tree is read from the root directory, the system can find the inode number of the Mount point through the mounting information. in this case, the inode content of the root directory can be obtained, read the file name data in the block of the root directory based on the inode, and read the correct file name layer by layer.
Www.2cto.com
IV. ext3 file system
To avoid file system inconsistencies, plan a block in the file system, which records the steps for writing or revising the file, that is, adding the log file system. This is the ext3 file system, which is an upgraded version of the ext2 file system.

5. Significance of Mount points
Each file system has independent inode, block, super block, and other information. this file system must be able to link to the directory tree before it can be used. The operations that combine the file system with the directory tree are called mounting. The Mount point must be a directory. to access the file system, you must "mount" the directory to the directory tree before using the file system.
Related Article

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.