跳转至

ext4

1 特点

  1. 64bit 文件系统,block 大小 4kiB 下,单个文件最大 16TiB,文件系统最大 64ZiB。
  2. 32bit 文件系统,block 大小 4kiB 下,单个文件最大 4TiB,文件系统最大 16TiB。
  3. inode 大小 256Byte

2 组成与布局

ext4 文件系统被拆分成一系列 block groups,每一个 block groups 包含一系列的 blocks,每一个 block 的大小是相同的,可以在 mkfs 时设置。默认配置下 block 大小是 4kiB,block groups 包含 32, 768 个 block,总大小是 128MiB。 - 总体布局图

2.1 block groups 布局

ext4 块组分成 3 类布局: 1. 0 号块组:前 1024 个字节未使用,以允许安装 x86 引导扇区和其他用途。 2. 冗余备份块组:0 号块组含有超级块和组描述符,但系统为了安全,做了冗余备份,这类块组会备份超级块和组描述符。 3. 普通块组:这类块组以数据块位图开始,不含有冗余的超级块和组描述符。 - 0 号块组的组成 | Group 0 Padding | ext4 Super Block | Group Descriptors | Reserved GDT Blocks | Data Block Bitmap | inode Bitmap | inode Table | Data Blocks | | --------------- | ---------------- | ----------------- | ------------------- | ----------------- | ------------ | ----------- | ---------------- | | 1024 bytes | 1 block | many blocks | many blocks | 1 block | 1 block | many blocks | many more blocks | - 冗余备份块组的组成 | ext4 Super Block | Group Descriptors | Reserved GDT Blocks | Data Block Bitmap | inode Bitmap | inode Table | Data Blocks | | ---------------- | ----------------- | ------------------- | ----------------- | ------------ | ----------- | ---------------- | | 1 block | many blocks | many blocks | 1 block | 1 block|many blocks|many more blocks| - 普通块组的组成 | Data Block Bitmap | inode Bitmap | inode Table | Data Blocks | | ----------------- | ------------ | ----------- | ---------------- | | 1 block | 1 block| many blocks | many blocks |

冗余块组的备份策略在 mkfs 时,有一个默认的选项 sparse_super,满足以下 2 个条件的需要备份超级块和组描述符参考 - 块组0 - 块组id为3 或5 或7的幂(注意,块组#1是3的0次幂,因此也有backup superblock)。 从下面输出中不难看出,

#1  32768  =  (32876)*3^0
#3  98304  =   (32768) *3^1
#5  163840 =   (32768) *5^1
#7  229376 =   (32768) *7^1
#9  294912 =   (32768) *3^2
#25  
...
root@node-1:~## dumpe2fs /dev/sdb2|grep super
dumpe2fs 1.42 (29-Nov-2011)
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype needs_recovery extent flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize
  Primary superblock at 0, Group descriptors at 1-464
  Backup superblock at 32768, Group descriptors at 32769-33232
  Backup superblock at 98304, Group descriptors at 98305-98768
  Backup superblock at 163840, Group descriptors at 163841-164304
  Backup superblock at 229376, Group descriptors at 229377-229840
  Backup superblock at 294912, Group descriptors at 294913-295376
  Backup superblock at 819200, Group descriptors at 819201-819664
  Backup superblock at 884736, Group descriptors at 884737-885200
  Backup superblock at 1605632, Group descriptors at 1605633-1606096
  Backup superblock at 2654208, Group descriptors at 2654209-2654672
  Backup superblock at 4096000, Group descriptors at 4096001-4096464
  Backup superblock at 7962624, Group descriptors at 7962625-7963088
  Backup superblock at 11239424, Group descriptors at 11239425-11239888
  Backup superblock at 20480000, Group descriptors at 20480001-20480464
  Backup superblock at 23887872, Group descriptors at 23887873-23888336
  Backup superblock at 71663616, Group descriptors at 71663617-71664080
  Backup superblock at 78675968, Group descriptors at 78675969-78676432
  Backup superblock at 102400000, Group descriptors at 102400001-102400464
  Backup superblock at 214990848, Group descriptors at 214990849-214991312
  Backup superblock at 512000000, Group descriptors at 512000001-512000464
  Backup superblock at 550731776, Group descriptors at 550731777-550732240
  Backup superblock at 644972544, Group descriptors at 644972545-644973008
  Backup superblock at 1934917632, Group descriptors at 1934917633-1934918096

3 容量的计算

ext4 allocates storage space in units of “blocks”. A block is a group of sectors between 1KiB and 64KiB, and the number of sectors must be an integral power of 2. Blocks are in turn grouped into larger units called block groups. Block size is specified at mkfs time and typically is 4KiB. You may experience mounting problems if block size is greater than page size (i.e. 64KiB blocks on a i386 which only has 4KiB memory pages). By default a filesystem can contain 2^32 blocks; if the ‘64bit’ feature is enabled, then a filesystem can have 2^64 blocks. The location of structures is stored in terms of the block number the structure lives in and not the absolute offset on disk.

For 32-bit filesystems, limits are as follows:

Item 1KiB 2KiB 4KiB 64KiB
Blocks 2^32 2^32 2^32 2^32
Inodes 2^32 2^32 2^32 2^32
File System Size 4TiB 8TiB 16TiB 256TiB
Blocks Per Block Group 8,192 16,384 32,768 524,288
Inodes Per Block Group 8,192 16,384 32,768 524,288
Block Group Size 8MiB 32MiB 128MiB 32GiB
Blocks Per File, Extents 2^32 2^32 2^32 2^32
Blocks Per File, Block Maps 16,843,020 134,480,396 1,074,791,436 4,398,314,962,956 (really 2^32 due to field size limitations)
File Size, Extents 4TiB 8TiB 16TiB 256TiB
File Size, Block Maps 16GiB 256GiB 4TiB 256TiB

For 64-bit filesystems, limits are as follows:

Item 1KiB 2KiB 4KiB 64KiB
Blocks 2^64 2^64 2^64 2^64
Inodes 2^32 2^32 2^32 2^32
File System Size 16ZiB 32ZiB 64ZiB 1YiB
Blocks Per Block Group 8,192 16,384 32,768 524,288
Inodes Per Block Group 8,192 16,384 32,768 524,288
Block Group Size 8MiB 32MiB 128MiB 32GiB
Blocks Per File, Extents 2^32 2^32 2^32 2^32
Blocks Per File, Block Maps 16,843,020 134,480,396 1,074,791,436 4,398,314,962,956 (really 2^32 due to field size limitations)
File Size, Extents 4TiB 8TiB 16TiB 256TiB
File Size, Block Maps 16GiB 256GiB 4TiB 256TiB

Note: Files not using extents (i.e. files using block maps) must be placed within the first 2^32 blocks of a filesystem. Files with extents must be placed within the first 2^48 blocks of a filesystem. It’s not clear what happens with larger filesystems.

3.1 计算公式

  • block 大小:mkfs 时指定,默认 4KiB
  • 文件系统 block 最大数量:固定值 32 位系统 $2^{32}$,64 位系统 $2^{64}$
  • 文件系统最大容量:文件系统block最大数量 * block大小。如 $2^{32}*4KiB=16TiB$
  • Data Block Bitmap Per Block Group:固定占1 个 block
  • inode Bitmap Per Block Group:固定占 1 个 block
  • Blocks Per Block Group8 * 块的大小 * 数据位图块数。如 $84^{10}1=32768$
  • Block Group Size块组中block数量 * block大小。如 $30768*4KiB=128MiB$
  • 单个文件最大大小(bitmap 模式):通过多级寻址计算得到参考

表 1. 各种数据块对应的文件寻址范围

块大小 直接寻址 间接寻址 二次间接寻址 三次间接寻址
1024 12KB 268KB 64.26MB 16.06GB
2048 24KB 1.02MB 513.02MB 265.5GB
4096 48KB 4.04MB 4GB ~ 4TB
  • ext3 文件系统 inode 多级寻址结构

4 超级块(Super Block)

超级块记录有关封闭文件系统的各种信息,例如块计数、索引节点计数、支持的功能、维护信息等。 如果设置了稀疏_超级特征标志,则仅在组号为 0 或 3、5 或 7 的幂的组中保留超级块和组描述符的冗余副本。如果未设置该标志,则在所有组中保留冗余副本。