自学内容网 自学内容网

ext2文件系统中超级块源码解读

源码

以下代码定义了 ext2_super_block 结构体,这是 ext2 文件系统中超级块(superblock)的数据结构。超级块保存了文件系统的重要元数据信息,用于管理磁盘空间、文件、以及其他文件系统特性。

struct ext2_super_block {
__le32s_inodes_count;/* Inodes count */
__le32s_blocks_count;/* Blocks count */
__le32s_r_blocks_count;/* Reserved blocks count */
__le32s_free_blocks_count;/* Free blocks count */
__le32s_free_inodes_count;/* Free inodes count */
__le32s_first_data_block;/* First Data Block */
__le32s_log_block_size;/* Block size */
__le32s_log_frag_size;/* Fragment size */
__le32s_blocks_per_group;/* # Blocks per group */
__le32s_frags_per_group;/* # Fragments per group */
__le32s_inodes_per_group;/* # Inodes per group */
__le32s_mtime;/* Mount time */
__le32s_wtime;/* Write time */
__le16s_mnt_count;/* Mount count */
__le16s_max_mnt_count;/* Maximal mount count */
__le16s_magic;/* Magic signature */
__le16s_state;/* File system state */
__le16s_errors;/* Behaviour when detecting errors */
__le16s_minor_rev_level; /* minor revision level */
__le32s_lastcheck;/* time of last check */
__le32s_checkinterval;/* max. time between checks */
__le32s_creator_os;/* OS */
__le32s_rev_level;/* Revision level */
__le16s_def_resuid;/* Default uid for reserved blocks */
__le16s_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...
 */
__le32s_first_ino; /* First non-reserved inode */
__le16   s_inode_size; /* size of inode structure */
__le16s_block_group_nr; /* block group # of this superblock */
__le32s_feature_compat; /* compatible feature set */
__le32s_feature_incompat; /* incompatible feature set */
__le32s_feature_ro_compat; /* readonly-compatible feature set */
__u8s_uuid[16];/* 128-bit uuid for volume */
chars_volume_name[16]; /* volume name */
chars_last_mounted[64]; /* directory where last mounted */
__le32s_algorithm_usage_bitmap; /* For compression */
/*
 * Performance hints.  Directory preallocation should only
 * happen if the EXT2_COMPAT_PREALLOC flag is on.
 */
__u8s_prealloc_blocks;/* Nr of blocks to try to preallocate*/
__u8s_prealloc_dir_blocks;/* Nr to preallocate for dirs */
__u16s_padding1;
/*
 * Journaling support valid if EXT3_FEATURE_COMPAT_HAS_JOURNAL set.
 */
__u8s_journal_uuid[16];/* uuid of journal superblock */
__u32s_journal_inum;/* inode number of journal file */
__u32s_journal_dev;/* device number of journal file */
__u32s_last_orphan;/* start of list of inodes to delete */
__u32s_hash_seed[4];/* HTREE hash seed */
__u8s_def_hash_version;/* Default hash version to use */
__u8s_reserved_char_pad;
__u16s_reserved_word_pad;
__le32s_default_mount_opts;
 __le32s_first_meta_bg; /* First metablock block group */
__u32s_reserved[190];/* Padding to the end of the block */
};

以下是对各个字段的解释:

  1. s_inodes_count: 文件系统中的 inode 数量(每个 inode 对应一个文件或目录)。
  2. s_blocks_count: 文件系统中的块总数。
  3. s_r_blocks_count: 为超级用户保留的块数量。
  4. s_free_blocks_count: 文件系统中可用的空闲块数量。
  5. s_free_inodes_count: 文件系统中可用的空闲 inode 数量。
  6. s_first_data_block: 文件系统中第一个数据块的块号(通常是超级块后的第一个块)。
  7. s_log_block_size: 块大小,表示为 1024 字节的对数值(例如,0 表示 1024 字节,1 表示 2048 字节,以此类推)。
  8. s_log_frag_size: 碎片大小,类似于 s_log_block_size,控制文件碎片的大小。
  9. s_blocks_per_group: 每个块组中的块数。
  10. s_frags_per_group: 每个块组中的碎片数。
  11. s_inodes_per_group: 每个块组中的 inode 数量。
  12. s_mtime: 文件系统的最后挂载时间。
  13. s_wtime: 文件系统的最后写入时间。
  14. s_mnt_count: 文件系统被挂载的次数。
  15. s_max_mnt_count: 文件系统需要被检查前的最大挂载次数。
  16. s_magic: 魔术数字,用于识别 ext2 文件系统,通常值为 0xEF53
  17. s_state: 文件系统的当前状态(例如是否已挂载、是否干净卸载等)。
  18. s_errors: 当检测到文件系统错误时的行为(例如,忽略、只读或挂起)。
  19. s_minor_rev_level: 次版本号。
  20. s_lastcheck: 文件系统上次被 fsck 检查的时间。
  21. s_checkinterval: fsck 检查之间的最大时间间隔。
  22. s_creator_os: 创建文件系统的操作系统(例如,Linux 或其他)。
  23. s_rev_level: 文件系统的版本号。
  24. s_def_resuid: 保留块的默认用户 ID。
  25. s_def_resgid: 保留块的默认组 ID。

动态超级块字段(仅适用于动态修订版 EXT2_DYNAMIC_REV 的超级块)

  1. s_first_ino: 第一个非保留 inode 的编号。
  2. s_inode_size: inode 结构体的大小。
  3. s_block_group_nr: 当前超级块所在的块组编号。
  4. s_feature_compat: 向后兼容的特性集,允许内核识别并使用这些特性。
  5. s_feature_incompat: 向后不兼容的特性集,内核无法识别的特性会阻止文件系统挂载。
  6. s_feature_ro_compat: 只读模式下兼容的特性集,内核可以以只读方式挂载文件系统。
  7. s_uuid: 128 位的 UUID,用于唯一标识该文件系统。
  8. s_volume_name: 卷名,表示文件系统的名字。
  9. s_last_mounted: 上一次挂载的目录路径。
  10. s_algorithm_usage_bitmap: 用于压缩的算法使用位图。
  11. s_prealloc_blocks: 预分配块的数量,用于提高性能。
  12. s_prealloc_dir_blocks: 目录预分配的块数。
  13. s_journal_uuid: 日志超级块的 UUID。
  14. s_journal_inum: 日志文件的 inode 编号。
  15. s_journal_dev: 日志文件的设备编号。
  16. s_last_orphan: 要删除的 inode 列表的开始。
  17. s_hash_seed: HTREE 哈希树的种子,用于目录加速。
  18. s_def_hash_version: 默认使用的哈希版本。
  19. s_default_mount_opts: 文件系统的默认挂载选项。
  20. s_first_meta_bg: 第一个元数据块组的编号。
  21. 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 ~]# 


原文地址:https://blog.csdn.net/zyqash/article/details/142462029

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!