Linux-EXT2 file system details

Source: Internet
Author: User

Linux-EXT2 file system details

File System features

For example, the Microsoft operating system before Windows 98 mainly uses the file system FAT (or FAT16). Versions later than Windows 2000 have the so-called NTFS file system, for Linux, the Orthodox file system is Ext2 (Linux second extended file system, ext2fs. In addition, by default, Windows does not know Linux Ext2.

How does the file system run? This is related to the file data of the operating system. In addition to the actual file content, file data of newer operating systems usually contains many attributes, such as file permissions of Linux operating systems (rwx) and file attributes (owner, group, time parameter, etc ). 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, there is a super block that records the overall information of the entire file system, including the total amount, usage, and remaining amount of inode and block.

Each inode and block is numbered. The meaning of the three data items can be described as follows:

Superblock: records the overall information of this filesystem, including the total inode/block quantity, usage, remaining amount, and file system format and related information;

Inode: records the attributes of a file. A file occupies one inode and records the block number of the data in the file;

Block: Actually records the content of a file. If the file is too large, multiple blocks are occupied.

The inode and block blocks are illustrated. As shown in, the file system first formats the inode and block blocks, assume that the attributes and permissions of a file are stored in inode 4 (in a small square), and The inode records the actual locations of file data: 2, 7, 13, at this time, the operating system can sort the reading order of the disk based on the four block numbers. You can read the four blocks in one breath! Then the data is read as specified by the arrow in.

This method of data access is called indexed allocation ). Are there other common file systems that can be compared? Some, that is, our usual flash drive (flash). The file system used by flash disks is generally in the FAT format. The file system in FAT format does not have inode, so FAT cannot read all blocks of this file from the beginning. Each block number is recorded in the previous block. Its reading method is a bit like below:

Assume that the data in the file is written to the four block numbers 1-> 7-> 4-> 15 in sequence, but the file system cannot know the number of the four blocks in one breath, he needs to read the block one by one to know where the next block is. If the blocks written to the same file are too scattered, the read header of the disk cannot read all the data in a circle, therefore, the disk will be rotated several more times to completely read the content of this file!

Data block)

Data block is used to store data in the file content. In the Ext2 file system, the supported block sizes are 1 K, 2 K, and 4 K. During formatting, the block size is fixed, and each block is numbered to facilitate inode record. However, due to the difference in block size, the maximum disk capacity supported by the file system is different from the maximum single file capacity. The Ext2 file system is subject to the following restrictions due to the block size:

In addition, what are the limitations on the block of the Ext2 file system? Yes! The basic limits are as follows:

In principle, the size and number of blocks cannot be changed after being formatted (unless re-formatted );

Data of only one file can be placed in each block;

If the file size is greater than the block size, one file will occupy multiple blocks;

If the file size is smaller than the block size, the remaining capacity of the block will no longer be used (disk space will be wasted ).

Inode table (inode table)

Let's discuss inode! As mentioned above, inode records the attributes of the file and the block where the actual data of the file is stored! Basically, inode records at least the following file data:

The access mode of the file (read/write/excute );

Owner and group of the file );

Capacity of the file;

The time when the file is created or the status changes (ctime );

The last read time (atime );

The latest modification time (mtime );

Define the flag of file features, such as SetUID ...;

The true point of the file content (pointer );

The number and size of inode are fixed during formatting. What are the characteristics of inode?

The size of each inode is fixed to 128 bytes;

Each file occupies only one inode;

Therefore, the number of files that the file system can create is related to the number of inode;

When the system reads a file, it needs to find the inode and analyze whether the permissions recorded by the inode match the user. If yes, the system can read the block content.

Let's take a rough look at the relationship between inode/block and file size. Inode only records a large amount of data, but only 128 bytes. inode takes 4 bytes to record a block number, assume that a file contains 400 MB and each block is 4 kb, at least 100,000 block numbers are required! Which of the following information can be recorded in inode? Therefore, our system intelligently defines the inode record block number as 12 direct, indirect, dual indirect, and triindirect record areas. What is this? We have drawn the inode structure.

The leftmost part is inode itself (128 bytes), which contains 12 comparisons pointing directly to the block number. These 12 records can directly obtain the block number! The so-called indirect method is to use another block as the record area for recording the block number. If the file is too large, an indirect block will be used to record the number. For example, in 1.3.2, only a block is used to record additional numbers. Similarly, if the file continues to grow up, it will use the so-called double indirect. the first block only points out 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 use the third-layer block to record numbers!

Superblock)

Superblock records the information related to the entire filesystem. If there is no Superblock, this filesystem will be unavailable. The following information is recorded:

Total number of blocks and inode;

Number of unused and used inode/blocks;

Size of the block and inode (block is 1, 2, 4 K, inode is 128 bytes );

Information about the file system, such as the Mount time of the filesystem, the time when data was last written, and the time when the last disk (fsck) was verified;

A valid bit value. If the file system has been mounted, valid bit is 0. If not, valid bit is 1.

[Root @ www ~] # Dumpe2fs [-bh] Device File Name

Options and parameters:

-B: list the parts that are retained as bad tracks (generally unavailable !?)

-H: only list superblock data. Other sections are not listed!

Example: Find the disk file name in my root directory and observe the file system information.

[Root @ www ~] # Df

Filesystem 1K-blocks Used Available Use % Mounted on

/Dev/hdc2 9920624 3822848 5585708 41%/

/Dev/hdc3 4956316 141376 4559108 4%/home

/Dev/hdc1 101086 11126 84741 12%/boot

Tmpfs 371332 0 371332 0%/dev/shm

[Root @ www ~] # Dumpe2fs/dev/hdc2

Dumpe2fs 1.39 (29-may-2006)

Filesystem volume name:/1

Filesystem features: has_journal ext_attr resize_inode dir_index

Filetype needs_recovery sparse_super large_file

Default mount options: user_xattr acl

Filesystem state: clean

Errors behavior: Continue

Filesystem OS type: Linux

Inode count: 2560864

Block count: 2560359

Free blocks: 1524760

Free inodes: 2411225

First block: 0

Block size: 4096

Filesystem created: Fri Sep 5 01:49:20 2008

Last mount time: Mon Sep 22 12:09:30 2008

Last write time: Mon Sep 22 12:09:30 2008

Last checked: Fri Sep 5 01:49:20 2008

First inode: 11

Inode size: 128

Journal inode: 8

Journal backup: inode blocks

Journal size: 128 M

Group 0: (Blocks 0-32767)

Primary superblock at 0, Group descriptors at 1-1

Reserved GDT blocks at 2-626

Block bitmap at 627 (+ 627), Inode bitmap at 628 (+ 628)

Inode table at 629-1641 (+ 629)

0 free blocks, 32405 free inodes, 2 directories

Free blocks:

Free inodes: 12-32416

Group 1: (Blocks 32768-65535)

... (Omitted below )....

# Some information is omitted because the data volume is very large!

# The first half shows the supberblock content, including the header name (Label) and inode/block information.

# The following is the individual information of each block group! You can see the data number of each region section!

# That is to say, basically all the data is related to the block number! Very important!

Directory

When we create a directory in the ext2 File System in Linux, ext2 allocates an inode and at least one block to the directory. Inode records the 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. That is to say, the block content occupied by the directory is recorded in the following information:

[Root @ www ~] # Ls-li

Total 92

654683-rw ------- 1 root 1474 Sep 4 anaconda-ks.cfg

648322-rw-r -- 1 root 42304 Sep 4 18:26 install. log

648323-rw-r -- 1 root 5661 Sep 4 install. log. syslog

[Root @ www ~] # Ll-d // bin/boot/proc/lost + found/sbin

Drwxr-xr-x 23 root 4096 Sep 22 12:09/

Drwxr-xr-x 2 root 4096 Sep 24 00:07/bin

Drwxr-xr-x 4 root 1024 Sep 4 18:06/boot

Drwx ------ 2 root 16384 Sep 5 0:49/lost + found

Dr-xr-x 96 root 0 Sep 22 :07/proc

Drwxr-xr-x 2 root 12288 Sep 5 12: 33/sb

File:

When we create a general file in ext2 in Linux, ext2 allocates an inode and the number of blocks relative to the file size to this file. For example, if one of my blocks is 4 Kbytes and I want to create a 100 KBytes file, linux will allocate an inode and 25 blocks to store the file! However, note that there are only 12 Direct points in inode, so you need to add a block to record the block number.

Directory tree reading:

Because the directory tree is read from the root directory, therefore, the system can find the inode Number of the mount point through the mounting information (usually the top-level inode number of a filesystem starts from 2 !), In this case, you can get the inode content of the root directory, 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.

[Root @ www ~] # Ll-di/etc/passwd

2 drwxr-xr-x 23 root 4096 Sep 22 12:09/

1912545 drwxr-xr-x 105 root 12288 Oct 14/etc

1914888-rw-r -- 1 root 1945 Sep 29/etc/passwd

/Inode:

Find the inode in the root directory of/dev/hdc2 whose inode number is 2 through the mount point information, and the inode standard permission allows us to read the content of the block (with r and x );

/Block:

Obtain the block number in the previous step and find the inode number (1912545) in the etc/directory );

Etc/inode:

Read inode No. 1912545 to learn that vbird has the r and x permissions, so it can read the block content of etc;

Block of etc:

Obtain the block number in the previous step and find the inode number (1914888) of the passwd file );

Inode of passwd:

Read inode No. 1914888 and learn that vbird has the r permission. Therefore, you can read the block content of passwd;

Passwd block:

Finally, read the data in the block.

EXT2/EXT3 file access and Log File System Functions

The previous section only talks about reading data. How does Ext2 work when creating a new file or directory? At this time, we need to block bitmap and inode bitmap! Suppose we want to add a new file. The file system will act as follows:

1. First, determine whether the user has the permissions w and x for the Directory of the file to be added. If yes, the user can add the file;

2. Locate the unused inode number based on inode bitmap and write the permissions/attributes of the new file;

3. Locate the unused block number based on the block bitmap, write the actual data into the block, and upgrade the inode block to point to the data;

4. Synchronize the newly written inode and block data to upgrade inode bitmap and block bitmap, and upgrade the superblock content.

Generally, inode table and data block are called data storage areas. For other areas such as superblock, block bitmap, and inode bitmap, they are called metadata (intermediary data, inode bitmap and block bitmap data change frequently. Each addition, removal, and editing may affect the data of these three parts. Therefore, they are called intermediary data.

Journaling filesystem)

To avoid inconsistencies in the file system mentioned above, our predecessors thought of a way to plan a block in our filesystem, this section is designed to record the steps for writing or revising files. Can this simplify the consistency check procedure? That is to say:

1. Preparation: when the system wants to write a file, it will first record the information of the file to be written in the log record block;

2. Actual write: start to write permissions and data to the file; start to upgrade metadata data;

3. Conclusion: After the data and metadata are upgraded, the file is recorded in the log record block.

In such a program, if a problem occurs during the data record process, our system only needs to check the log record block to know which file has a problem, check the consistency of the entire filesystem, so that you can quickly fix the filesystem! This is the most basic function of log files.

Linux File System running:

To solve this problem, we use a method called asynchronously in Linux. The so-called asynchronous processing is like this:

After the system loads a file to the memory, if the file has not been changed, the file data in the memory segment will be configured as clean. However, if the file data in the memory has been changed (for example, you used nano to edit the file), the data in the memory will be configured as Dirty (Dirty ). At this time, all the actions are still running in the memory and are not written to the disk! The system periodically writes the data configured as "Dirty" in the memory back to the disk to maintain the consistency between the disk and memory data. You can also use the sync command in Chapter 5 to manually force data into the disk.

We know that the memory speed is much faster than the hard disk. Therefore, if we can place common files in the memory, will this add system performance? That's right! This is the idea! Therefore, the file system on Linux has a very large relationship with memory:

The system places common file data in the buffer zone of the primary storage to accelerate the reading/writing of the file system;

Therefore, the Linux physical memory will eventually be used up! This is normal! System performance can be accelerated;

You can manually use sync to force the files configured as Dirty in the memory to be written back to the disk;

If the system shuts down normally, the shutdown command will actively call sync to write memory data back to the disk;

However, if the instance is shut down abnormally (for example, power-off, crash, or other unknown reasons), since the data has not been written back to the disk, it may take a lot of time to perform disk inspection after the instance is restarted, it may even cause damage to the file system (non-disk damage ).

Other Linux-Supported file systems and VFS

Although the standard file system in Linux is ext2 and the log function ext3 is added, in fact, Linux also supports many File System formats, in particular, several fast-paced log file systems have been launched in recent years, including the sgi xfs file system, which can be applied to Reiserfs file systems with smaller files, windows's FAT file system and so on can all be supported by Linux! Common supported file systems include:

Traditional File System: ext2/minix/MS-DOS/FAT (with vfat module)/iso9660 (CD) and so on;

Log File System: ext3/ReiserFS/Windows 'ntfs/IBM's JFS/SGI's XFS

Network File System: NFS/SMBFS

After learning about the file system we use, we will mention it again. How can we manage these known file systems at the core of Linux? In fact, the entire Linux System reads Filesystem through a core function named Virtual filesystem Switch. That is to say, all the filesystems we know in Linux are managed by VFS. We do not need to know what the filesystem header is for each partition ~ VFS will take the initiative to help us read the data ~

Assume that your/uses/dev/hda1 and ext3, while/home uses/dev/hda2 and reiserfs, you can use/home/dmtsai /. when bashrc is used, do I specify which file system modules to use for reading? No! This is the function of VFS! Using this VFS function to manage all filesystems, We need to configure and read the definition of the file system by ourselves ~ Great convenience! The entire VFS can be described in the following formula:

This article permanently updates the link address:

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.