源码
以下代码定义了 ext2_super_block
结构体,这是 ext2 文件系统中超级块(superblock)的数据结构。超级块保存了文件系统的重要元数据信息,用于管理磁盘空间、文件、以及其他文件系统特性。
struct ext2_super_block {
__le32 s_inodes_count; /* Inodes count */
__le32 s_blocks_count; /* Blocks count */
__le32 s_r_blocks_count; /* Reserved blocks count */
__le32 s_free_blocks_count; /* Free blocks count */
__le32 s_free_inodes_count; /* Free inodes count */
__le32 s_first_data_block; /* First Data Block */
__le32 s_log_block_size; /* Block size */
__le32 s_log_frag_size; /* Fragment size */
__le32 s_blocks_per_group; /* # Blocks per group */
__le32 s_frags_per_group; /* # Fragments per group */
__le32 s_inodes_per_group; /* # Inodes per group */
__le32 s_mtime; /* Mount time */
__le32 s_wtime; /* Write time */
__le16 s_mnt_count; /* Mount count */
__le16 s_max_mnt_count; /* Maximal mount count */
__le16 s_magic; /* Magic signature */
__le16 s_state; /* File system state */
__le16 s_errors; /* Behaviour when detecting errors */
__le16 s_minor_rev_level; /* minor revision level */
__le32 s_lastcheck; /* time of last check */
__le32 s_checkinterval; /* max. time between checks */
__le32 s_creator_os; /* OS */
__le32 s_rev_level; /* Revision level */
__le16 s_def_resuid; /* Default uid for reserved blocks */
__le16 s_def_resgid; /* Default gid for reserved blocks */
/*
* These fields are for EXT2_DYNAMIC_REV superblocks only.
*
* Note: the difference between the compatible feature set and
* the incompatible feature set is that if there is a bit set
* in the incompatible feature set that the kernel doesn't
* know about, it should refuse to mount the filesystem.
*
* e2fsck's requirements are more strict; if it doesn't know
* about a feature in either the compatible or incompatible
* feature set, it must abort and not try to meddle with
* things it doesn't understand...
*/
__le32 s_first_ino; /* First non-reserved inode */
__le16 s_inode_size; /* size of inode structure */
__le16 s_block_group_nr; /* block group # of this superblock */
__le32 s_feature_compat; /* compatible feature set */
__le32 s_feature_incompat; /* incompatible feature set */
__le32 s_feature_ro_compat; /* readonly-compatible feature set */
__u8 s_uuid[16]; /* 128-bit uuid for volume */
char s_volume_name[16]; /* volume name */
char s_last_mounted[64]; /* directory where last mounted */
__le32 s_algorithm_usage_bitmap; /* For compression */
/*
* Performance hints. Directory preallocation should only
* happen if the EXT2_COMPAT_PREALLOC flag is on.
*/
__u8 s_prealloc_blocks; /* Nr of blocks to try to preallocate*/
__u8 s_prealloc_dir_blocks; /* Nr to preallocate for dirs */
__u16 s_padding1;
/*
* Journaling support valid if EXT3_FEATURE_COMPAT_HAS_JOURNAL set.
*/
__u8 s_journal_uuid[16]; /* uuid of journal superblock */
__u32 s_journal_inum; /* inode number of journal file */
__u32 s_journal_dev; /* device number of journal file */
__u32 s_last_orphan; /* start of list of inodes to delete */
__u32 s_hash_seed[4]; /* HTREE hash seed */
__u8 s_def_hash_version; /* Default hash version to use */
__u8 s_reserved_char_pad;
__u16 s_reserved_word_pad;
__le32 s_default_mount_opts;
__le32 s_first_meta_bg; /* First metablock block group */
__u32 s_reserved[190]; /* Padding to the end of the block */
};
以下是对各个字段的解释:
- s_inodes_count: 文件系统中的 inode 数量(每个 inode 对应一个文件或目录)。
- s_blocks_count: 文件系统中的块总数。
- s_r_blocks_count: 为超级用户保留的块数量。
- s_free_blocks_count: 文件系统中可用的空闲块数量。
- s_free_inodes_count: 文件系统中可用的空闲 inode 数量。
- s_first_data_block: 文件系统中第一个数据块的块号(通常是超级块后的第一个块)。
- s_log_block_size: 块大小,表示为 1024 字节的对数值(例如,0 表示 1024 字节,1 表示 2048 字节,以此类推)。
- s_log_frag_size: 碎片大小,类似于
s_log_block_size
,控制文件碎片的大小。 - s_blocks_per_group: 每个块组中的块数。
- s_frags_per_group: 每个块组中的碎片数。
- s_inodes_per_group: 每个块组中的 inode 数量。
- s_mtime: 文件系统的最后挂载时间。
- s_wtime: 文件系统的最后写入时间。
- s_mnt_count: 文件系统被挂载的次数。
- s_max_mnt_count: 文件系统需要被检查前的最大挂载次数。
- s_magic: 魔术数字,用于识别 ext2 文件系统,通常值为
0xEF53
。 - s_state: 文件系统的当前状态(例如是否已挂载、是否干净卸载等)。
- s_errors: 当检测到文件系统错误时的行为(例如,忽略、只读或挂起)。
- s_minor_rev_level: 次版本号。
- s_lastcheck: 文件系统上次被 fsck 检查的时间。
- s_checkinterval: fsck 检查之间的最大时间间隔。
- s_creator_os: 创建文件系统的操作系统(例如,Linux 或其他)。
- s_rev_level: 文件系统的版本号。
- s_def_resuid: 保留块的默认用户 ID。
- s_def_resgid: 保留块的默认组 ID。
动态超级块字段(仅适用于动态修订版 EXT2_DYNAMIC_REV 的超级块)
- s_first_ino: 第一个非保留 inode 的编号。
- s_inode_size: inode 结构体的大小。
- s_block_group_nr: 当前超级块所在的块组编号。
- s_feature_compat: 向后兼容的特性集,允许内核识别并使用这些特性。
- s_feature_incompat: 向后不兼容的特性集,内核无法识别的特性会阻止文件系统挂载。
- s_feature_ro_compat: 只读模式下兼容的特性集,内核可以以只读方式挂载文件系统。
- s_uuid: 128 位的 UUID,用于唯一标识该文件系统。
- s_volume_name: 卷名,表示文件系统的名字。
- s_last_mounted: 上一次挂载的目录路径。
- s_algorithm_usage_bitmap: 用于压缩的算法使用位图。
- s_prealloc_blocks: 预分配块的数量,用于提高性能。
- s_prealloc_dir_blocks: 目录预分配的块数。
- s_journal_uuid: 日志超级块的 UUID。
- s_journal_inum: 日志文件的 inode 编号。
- s_journal_dev: 日志文件的设备编号。
- s_last_orphan: 要删除的 inode 列表的开始。
- s_hash_seed: HTREE 哈希树的种子,用于目录加速。
- s_def_hash_version: 默认使用的哈希版本。
- s_default_mount_opts: 文件系统的默认挂载选项。
- s_first_meta_bg: 第一个元数据块组的编号。
- s_reserved: 用于填充的保留字段。
这个结构体定义了 ext2 文件系统的关键元数据,特别是文件系统布局、性能优化和特性支持等方面。
示例
查看超级块
[root@node09 disk]# dumpe2fs /root/disk.img
dumpe2fs 1.42.9 (28-Dec-2013)
Filesystem volume name: <none>
Last mounted on: /disk
Filesystem UUID: a26734a4-4870-488b-aae2-2fe65d1b1b6e
Filesystem magic number: 0xEF53
Filesystem revision #: 1 (dynamic)
Filesystem features: ext_attr resize_inode dir_index filetype sparse_super
Filesystem flags: signed_directory_hash
Default mount options: user_xattr acl
Filesystem state: not clean
Errors behavior: Continue
Filesystem OS type: Linux
Inode count: 7680
Block count: 30720
Reserved block count: 1536
Free blocks: 29374
Free inodes: 7669
First block: 1
Block size: 1024
Fragment size: 1024
Reserved GDT blocks: 119
Blocks per group: 8192
Fragments per group: 8192
Inodes per group: 1920
Inode blocks per group: 240
Filesystem created: Mon Sep 23 16:08:15 2024
Last mount time: Mon Sep 23 16:08:25 2024
Last write time: Mon Sep 23 16:08:25 2024
Mount count: 1
Maximum mount count: -1
Last checked: Mon Sep 23 16:08:15 2024
Check interval: 0 (<none>)
Reserved blocks uid: 0 (user root)
Reserved blocks gid: 0 (group root)
First inode: 11
Inode size: 128
Default directory hash: half_md4
Directory Hash Seed: 7e41a23f-3fd0-4bd8-be59-298c6ebdc79a
Group 0: (Blocks 1-8192)
Primary superblock at 1, Group descriptors at 2-2
Reserved GDT blocks at 3-121
Block bitmap at 122 (+121), Inode bitmap at 123 (+122)
Inode table at 124-363 (+123)
7782 free blocks, 0 free inodes, 2 directories
Free blocks: 411-8192
Free inodes:
超级块对应的16进制内容
[root@node09 ~]# hexdump -s 1K -n 1k /root/disk.img -C
00000400 00 1e 00 00 00 78 00 00 00 06 00 00 be 72 00 00 |.....x.......r..|
00000410 f5 1d 00 00 01 00 00 00 00 00 00 00 00 00 00 00 |................|
00000420 00 20 00 00 00 20 00 00 80 07 00 00 f9 21 f1 66 |. ... .......!.f|
00000430 f9 21 f1 66 01 00 ff ff 53 ef 00 00 01 00 00 00 |.!.f....S.......|
00000440 ef 21 f1 66 00 00 00 00 00 00 00 00 01 00 00 00 |.!.f............|
00000450 00 00 00 00 0b 00 00 00 80 00 00 00 38 00 00 00 |............8...|
00000460 02 00 00 00 01 00 00 00 a2 67 34 a4 48 70 48 8b |.........g4.HpH.|
00000470 aa e2 2f e6 5d 1b 1b 6e 00 00 00 00 00 00 00 00 |../.]..n........|
00000480 00 00 00 00 00 00 00 00 2f 64 69 73 6b 00 00 00 |......../disk...|
00000490 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
000004c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 77 00 |..............w.|
000004d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
000004e0 00 00 00 00 00 00 00 00 00 00 00 00 7e 41 a2 3f |............~A.?|
000004f0 3f d0 4b d8 be 59 29 8c 6e bd c7 9a 01 00 00 00 |?.K..Y).n.......|
00000500 0c 00 00 00 00 00 00 00 ef 21 f1 66 00 00 00 00 |.........!.f....|
00000510 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00000560 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000570 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00000800
[root@node09 ~]#