简千叶 发表于 2025-6-8 12:27:48

文件系统(六):一文看懂linux ext4文件系统工作原理

liwen01 2024.06.09
前言

Linux系统中的ext2、ext3、ext4 文件系统,它们都有很强的向后和向前兼容性,可以在数据不丢失的情况下进行文件系统的升级。目前ext4是一个相对较成熟、稳定且高效的文件系统,适用于绝大部分规模和需求的Linux环境。
ext4它突出的特点有:数据分段管理、多块分配、延迟分配、持久预分配、日志校验、支持更大的文件系统和文件大小。
ext4文件系统的具体实现比较复杂,本文尝试用比较简单的方式用一篇文章的篇幅来简单地介绍一下它的工作原理。
(一)创建ext文件系统

为了分析ext4 文件系统的内部结构和原理,这里我们在Linux中创建一个ext4文件系统镜像,然后通过loop虚拟设备将ext4镜像文件挂载到某个目录上。具体实现步骤如下:

[*]创建一个1GB的文件
dd if=/dev/zero of=./ext4_image.img bs=1M count=1024
[*]将这个文件格式化成 ext4 文件系统格式
mkfs.ext4 ext4_image.img
[*]通过Linux的loop虚拟设备将文件挂载到目录上
sudo mount -o loop ext4_image.img /home/biao/test/ext4/ext4_simulator
[*]dumpe2fs 查看文件系统基本信息
dumpe2fs ext4_image.img输出内容信息(中间省略了部分内容):
dumpe2fs 1.44.1 (24-Mar-2018)
Filesystem volume name:   <none>
Last mounted on:          /home/biao/test/ext4/ext4_simulator
Filesystem UUID:          0169498e-f5f7-4fb8-9e9e-532088e41333
Filesystem magic number:0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype needs_recovery extent 64bit flex_bg sparse_super large_file huge_file dir_nlink extra_isize metadata_csum
Filesystem flags:         signed_directory_hash
Default mount options:    user_xattr acl
Filesystem state:         clean
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:            65536
Block count:            262144
Reserved block count:   13107
Free blocks:            247703
Free inodes:            65517
First block:            0
Block size:               4096
Fragment size:            4096
Group descriptor size:    64
Reserved GDT blocks:      127
Blocks per group:         32768
Fragments per group:      32768
Inodes per group:         8192
Inode blocks per group:   512
Flex block group size:    16
Filesystem created:       Fri May 24 17:18:57 2024
Last mount time:          Wed Jun5 19:15:36 2024
Last write time:          Wed Jun5 19:15:36 2024
Mount count:            3
Maximum mount count:      -1
Last checked:             Fri May 24 17:18:57 2024
Check interval:         0 (<none>)
Lifetime writes:          6997 kB
Reserved blocks uid:      0 (user root)
Reserved blocks gid:      0 (group root)
First inode:            11
Inode size:               256
Required extra isize:   32
Desired extra isize:      32
Journal inode:            8
Default directory hash:   half_md4
Directory Hash Seed:      0faf0e8c-f385-4ecd-b3a4-db2a3329e121
Journal backup:         inode blocks
Checksum type:            crc32c
Checksum:               0x32dc1b70
Journal features:         journal_64bit journal_checksum_v3
Journal size:             32M
Journal length:         8192
Journal sequence:         0x00000017
Journal start:            1
Journal checksum type:    crc32c
Journal checksum:         0xa3c1b983

Group 0: (Blocks 0-32767) csum 0xf19b
Primary superblock at 0, Group descriptors at 1-1
Reserved GDT blocks at 2-128
Block bitmap at 129 (+129), csum 0x8efc34cf
Inode bitmap at 137 (+137), csum 0x49f91ed6
Inode table at 145-656 (+145)
28517 free blocks, 8176 free inodes, 3 directories, 8176 unused inodes
Free blocks: 4251-32767
Free inodes: 17-8192
..........
..........
..........
Group 7: (Blocks 229376-262143) csum 0x7daa
Backup superblock at 229376, Group descriptors at 229377-229377
Reserved GDT blocks at 229378-229504
Block bitmap at 136 (bg #0 + 136), csum 0x5bd8cca0
Inode bitmap at 144 (bg #0 + 144), csum 0x00000000
Inode table at 3729-4240 (bg #0 + 3729)
32639 free blocks, 8192 free inodes, 0 directories, 8192 unused inodes
Free blocks: 229505-262143
Free inodes: 57345-655361.1 ext4文件系统信息表(二)ext4 磁盘布局

从上面dumpe2fs的数据上我们可以看出,一个1GB大小的空间,ext4 文件系统将它分隔成了0~7的8个Group。
ext4 的总体磁盘布局如下:

图2.1 ext4总体布局其中,每个Group中又有superblock、Group descriptors、bitmap、Inode table、usrer data、还有一些保留空间,细分之后的空间布局如下:
图2.2 ext4 group 布局从上图可以看出:

[*]Backup superblock、Group descriptors、Reserved GDT 是分布在1、3、5、7 这几个Group中,2、4、6Group并没有这些信息。
[*]Block bitmap、Inode bitmap、Inode table 这些元文件在每个Group中的位置并不一样,而是相差1个Block。
为什么需要这样设计?这个下面稍晚点再介绍
(三) superblock超级块

从上面《1.1 ext4文件系统信息表》中可以知道Primary superblock在第0号block,每个block的大小为4096Byte。
用hexdump 命令查看超级块的数据
biao@ubuntu:~/test/ext4$ hexdump -s 0 -n 4096 -C ext4_image.img    0000000000 00 00 00 00 00 00 0000 00 00 00 00 00 00 00|................|*0000040000 00 01 00 00 00 04 0033 33 00 00 97 c7 03 00|........33......|00000410ed ff 00 00 00 00 00 0002 00 00 00 02 00 00 00|................|0000042000 80 00 00 00 80 00 0000 20 00 00 9c c1 5d 66|......... ....]f|0000043000 d0 5f 66 02 00 ff ff53 ef 01 00 01 00 00 00|.._f....S.......|0000044081 5b 50 66 00 00 00 0000 00 00 00 01 00 00 00|.[Pf............|0000045000 00 00 00 0b 00 00 0000 01 00 00 3c 00 00 00|............
页: [1]
查看完整版本: 文件系统(六):一文看懂linux ext4文件系统工作原理