EXT4文件系统学习(10)VFS之磁盘结构Group和superblock

2024-04-24 19:18

本文主要是介绍EXT4文件系统学习(10)VFS之磁盘结构Group和superblock,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

direntry建立了各级目录和文件之间的联系,inode建立了文件与数据之间联系,但这还不够,还需要记录inode和数据块从哪里开始从哪里结束,以及哪些是空闲的,这样才能正确的分配文件。

Group

块组是一个逻辑概念,把一个分区分成若干个组,用来限制文件的数据块不要过于散落,尽量保证一个文件的数据块在一个组,这样可以提高读写效率,

格式化一个ext4文件系统后在磁盘上的结构:

Group 0: (Blocks 1-8192)Primary superblock at 1, Group descriptors at 2-2Reserved GDT blocks at 3-246Block bitmap at 247 (+246)Inode bitmap at 251 (+250)Inode table at 255-499 (+254)5481 free blocks, 1916 free inodes, 7 directoriesFree blocks: 1268-2657, 2866-4096, 5327-6112, 6119-8192Free inodes: 39, 46-1960

超级块

超级块记录着一个分区的整体信息,有多少inode,多少个组,多少个block数据块,多少个空闲块。

Group descriptors组描述符记录了这个组中的inode数量,空闲块数量等信息。

Block bitmap是数据块的位图,记录着数据块的状态。

Inode bitmap是inode的位图,记录着inode的状态。

磁盘超级块结构如下:

/** Structure of the super block*/
struct ext4_super_block {
/*00*/	__le32	s_inodes_count;		/* Inodes count */inode总数量__le32	s_blocks_count_lo;	/* Blocks count */block总数量__le32	s_r_blocks_count_lo;	/* Reserved blocks count */预留block总数量__le32	s_free_blocks_count_lo;	/* Free blocks count */空闲block总数量
/*10*/	__le32	s_free_inodes_count;	/* Free inodes count */空闲inode总数量__le32	s_first_data_block;	/* First Data Block */第一个数据块的编号__le32	s_log_block_size;	/* Block size */块大小,一般是4KB,可配置__le32	s_log_cluster_size;	/* Allocation cluster size */分片功能没有启用
/*20*/	__le32	s_blocks_per_group;	/* # Blocks per group */每个组中多个个块__le32	s_clusters_per_group;	/* # Clusters per group */__le32	s_inodes_per_group;	/* # Inodes per group */__le32	s_mtime;		/* Mount time */
/*30*/	__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 */文件系统状态,为0表示被mount状态,挂载之前会检测此状态防止重复挂载__le16	s_errors;		/* Behaviour when detecting errors */__le16	s_minor_rev_level;	/* minor revision level */
/*40*/	__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 */
/*50*/	__le16	s_def_resuid;		/* Default uid for reserved blocks */__le16	s_def_resgid;		/* Default gid for reserved blocks *//** These fields are for EXT4_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 */inode占用空间大小,一般是256字节__le16	s_block_group_nr;	/* block group # of this superblock */__le32	s_feature_compat;	/* compatible feature set */
/*60*/	__le32	s_feature_incompat;	/* incompatible feature set */__le32	s_feature_ro_compat;	/* readonly-compatible feature set */
/*68*/	__u8	s_uuid[16];		/* 128-bit uuid for volume */UID
/*78*/	char	s_volume_name[16];	/* volume name */分区名字
/*88*/	char	s_last_mounted[64];	/* directory where last mounted */
/*C8*/	__le32	s_algorithm_usage_bitmap; /* For compression *//** Performance hints.  Directory preallocation should only* happen if the EXT4_FEATURE_COMPAT_DIR_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 */__le16	s_reserved_gdt_blocks;	/* Per group desc for online growth *//** Journaling support valid if EXT4_FEATURE_COMPAT_HAS_JOURNAL set.*/
/*D0*/	__u8	s_journal_uuid[16];	/* uuid of journal superblock */
/*E0*/	__le32	s_journal_inum;		/* inode number of journal file */__le32	s_journal_dev;		/* device number of journal file */__le32	s_last_orphan;		/* start of list of inodes to delete */__le32	s_hash_seed[4];		/* HTREE hash seed */__u8	s_def_hash_version;	/* Default hash version to use */__u8	s_jnl_backup_type;__le16  s_desc_size;		/* size of group descriptor */
/*100*/	__le32	s_default_mount_opts;__le32	s_first_meta_bg;	/* First metablock block group */__le32	s_mkfs_time;		/* When the filesystem was created */__le32	s_jnl_blocks[17];	/* Backup of the journal inode *//* 64bit support valid if EXT4_FEATURE_COMPAT_64BIT */
/*150*/	__le32	s_blocks_count_hi;	/* Blocks count */__le32	s_r_blocks_count_hi;	/* Reserved blocks count */__le32	s_free_blocks_count_hi;	/* Free blocks count */__le16	s_min_extra_isize;	/* All inodes have at least # bytes */__le16	s_want_extra_isize; 	/* New inodes should reserve # bytes */__le32	s_flags;		/* Miscellaneous flags */__le16  s_raid_stride;		/* RAID stride */__le16  s_mmp_update_interval;  /* # seconds to wait in MMP checking */__le64  s_mmp_block;            /* Block for multi-mount protection */__le32  s_raid_stripe_width;    /* blocks on all data disks (N*stride)*/__u8	s_log_groups_per_flex;  /* FLEX_BG group size */__u8	s_checksum_type;	/* metadata checksum algorithm used */__u8	s_encryption_level;	/* versioning level for encryption */__u8	s_reserved_pad;		/* Padding to next 32bits */__le64	s_kbytes_written;	/* nr of lifetime kilobytes written */__le32	s_snapshot_inum;	/* Inode number of active snapshot */__le32	s_snapshot_id;		/* sequential ID of active snapshot */__le64	s_snapshot_r_blocks_count; /* reserved blocks for activesnapshot's future use */__le32	s_snapshot_list;	/* inode number of the head of theon-disk snapshot list */
#define EXT4_S_ERR_START offsetof(struct ext4_super_block, s_error_count)__le32	s_error_count;		/* number of fs errors */__le32	s_first_error_time;	/* first time an error happened */__le32	s_first_error_ino;	/* inode involved in first error */__le64	s_first_error_block;	/* block involved of first error */__u8	s_first_error_func[32];	/* function where the error happened */__le32	s_first_error_line;	/* line number where error happened */__le32	s_last_error_time;	/* most recent time of an error */__le32	s_last_error_ino;	/* inode involved in last error */__le32	s_last_error_line;	/* line number where error happened */__le64	s_last_error_block;	/* block involved of last error */__u8	s_last_error_func[32];	/* function where the error happened */
#define EXT4_S_ERR_END offsetof(struct ext4_super_block, s_mount_opts)__u8	s_mount_opts[64];__le32	s_usr_quota_inum;	/* inode for tracking user quota */__le32	s_grp_quota_inum;	/* inode for tracking group quota */__le32	s_overhead_clusters;	/* overhead blocks/clusters in fs */__le32	s_backup_bgs[2];	/* groups with sparse_super2 SBs */__u8	s_encrypt_algos[4];	/* Encryption algorithms in use  */__u8	s_encrypt_pw_salt[16];	/* Salt used for string2key algorithm */__le32	s_lpf_ino;		/* Location of the lost+found inode */__le32	s_reserved[100];	/* Padding to the end of the block */__le32	s_checksum;		/* crc32c(superblock) */
};

ext4_super_block与磁盘上面的超级块数据基本是对应的,可直接读出到结构体内存。

组描述符在磁盘上的格式如下:

/** Structure of a blocks group descriptor*/
struct ext4_group_desc
{__le32	bg_block_bitmap_lo;	/* Blocks bitmap block */表示bmap在这个组的那一块__le32	bg_inode_bitmap_lo;	/* Inodes bitmap block */表示imap在这个组的那一块__le32	bg_inode_table_lo;	/* Inodes table block */这个组的inode所在起始块号__le16	bg_free_blocks_count_lo;/* Free blocks count */空闲块数量__le16	bg_free_inodes_count_lo;/* Free inodes count */空闲inode数量__le16	bg_used_dirs_count_lo;	/* Directories count */__le16	bg_flags;		/* EXT4_BG_flags (INODE_UNINIT, etc) */__le32  bg_exclude_bitmap_lo;   /* Exclude bitmap for snapshots */__le16  bg_block_bitmap_csum_lo;/* crc32c(s_uuid+grp_num+bbitmap) LE */__le16  bg_inode_bitmap_csum_lo;/* crc32c(s_uuid+grp_num+ibitmap) LE */__le16  bg_itable_unused_lo;	/* Unused inodes count */__le16  bg_checksum;		/* crc16(sb_uuid+group+desc) */__le32	bg_block_bitmap_hi;	/* Blocks bitmap block MSB */__le32	bg_inode_bitmap_hi;	/* Inodes bitmap block MSB */__le32	bg_inode_table_hi;	/* Inodes table block MSB */__le16	bg_free_blocks_count_hi;/* Free blocks count MSB */__le16	bg_free_inodes_count_hi;/* Free inodes count MSB */__le16	bg_used_dirs_count_hi;	/* Directories count MSB */__le16  bg_itable_unused_hi;    /* Unused inodes count MSB */__le32  bg_exclude_bitmap_hi;   /* Exclude bitmap block MSB */__le16  bg_block_bitmap_csum_hi;/* crc32c(s_uuid+grp_num+bbitmap) BE */__le16  bg_inode_bitmap_csum_hi;/* crc32c(s_uuid+grp_num+ibitmap) BE */__u32   bg_reserved;
};

组描述符占用64字节,一个块占用4KB,所以每个组里面的组描述符块保存了所有组描述符。应该是为了防止数据损坏进行修复使用。

超级块的备份会在2n-1的组里面,防止数据损坏进行修复。

 

这篇关于EXT4文件系统学习(10)VFS之磁盘结构Group和superblock的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/932604

相关文章

Python中顺序结构和循环结构示例代码

《Python中顺序结构和循环结构示例代码》:本文主要介绍Python中的条件语句和循环语句,条件语句用于根据条件执行不同的代码块,循环语句用于重复执行一段代码,文章还详细说明了range函数的使... 目录一、条件语句(1)条件语句的定义(2)条件语句的语法(a)单分支 if(b)双分支 if-else(

Java深度学习库DJL实现Python的NumPy方式

《Java深度学习库DJL实现Python的NumPy方式》本文介绍了DJL库的背景和基本功能,包括NDArray的创建、数学运算、数据获取和设置等,同时,还展示了如何使用NDArray进行数据预处理... 目录1 NDArray 的背景介绍1.1 架构2 JavaDJL使用2.1 安装DJL2.2 基本操

使用Navicat工具比对两个数据库所有表结构的差异案例详解

《使用Navicat工具比对两个数据库所有表结构的差异案例详解》:本文主要介绍如何使用Navicat工具对比两个数据库test_old和test_new,并生成相应的DDLSQL语句,以便将te... 目录概要案例一、如图两个数据库test_old和test_new进行比较:二、开始比较总结概要公司存在多

MySQL报错sql_mode=only_full_group_by的问题解决

《MySQL报错sql_mode=only_full_group_by的问题解决》本文主要介绍了MySQL报错sql_mode=only_full_group_by的问题解决,文中通过示例代码介绍的非... 目录报错信息DataGrip 报错还原Navicat 报错还原报错原因解决方案查看当前 sql mo

Linux使用fdisk进行磁盘的相关操作

《Linux使用fdisk进行磁盘的相关操作》fdisk命令是Linux中用于管理磁盘分区的强大文本实用程序,这篇文章主要为大家详细介绍了如何使用fdisk进行磁盘的相关操作,需要的可以了解下... 目录简介基本语法示例用法列出所有分区查看指定磁盘的区分管理指定的磁盘进入交互式模式创建一个新的分区删除一个存

VMWare报错“指定的文件不是虚拟磁盘“或“The file specified is not a virtual disk”问题

《VMWare报错“指定的文件不是虚拟磁盘“或“Thefilespecifiedisnotavirtualdisk”问题》文章描述了如何修复VMware虚拟机中出现的“指定的文件不是虚拟... 目录VMWare报错“指定的文件不是虚拟磁盘“或“The file specified is not a virt

Java中switch-case结构的使用方法举例详解

《Java中switch-case结构的使用方法举例详解》:本文主要介绍Java中switch-case结构使用的相关资料,switch-case结构是Java中处理多个分支条件的一种有效方式,它... 目录前言一、switch-case结构的基本语法二、使用示例三、注意事项四、总结前言对于Java初学者

Golang使用minio替代文件系统的实战教程

《Golang使用minio替代文件系统的实战教程》本文讨论项目开发中直接文件系统的限制或不足,接着介绍Minio对象存储的优势,同时给出Golang的实际示例代码,包括初始化客户端、读取minio对... 目录文件系统 vs Minio文件系统不足:对象存储:miniogolang连接Minio配置Min

不删数据还能合并磁盘? 让电脑C盘D盘合并并保留数据的技巧

《不删数据还能合并磁盘?让电脑C盘D盘合并并保留数据的技巧》在Windows操作系统中,合并C盘和D盘是一个相对复杂的任务,尤其是当你不希望删除其中的数据时,幸运的是,有几种方法可以实现这一目标且在... 在电脑生产时,制造商常为C盘分配较小的磁盘空间,以确保软件在运行过程中不会出现磁盘空间不足的问题。但在

结构体和联合体的区别及说明

《结构体和联合体的区别及说明》文章主要介绍了C语言中的结构体和联合体,结构体是一种自定义的复合数据类型,可以包含多个成员,每个成员可以是不同的数据类型,联合体是一种特殊的数据结构,可以在内存中共享同一... 目录结构体和联合体的区别1. 结构体(Struct)2. 联合体(Union)3. 联合体与结构体的