开源ext2read代码走读之-ext2文件系统中的超级块及对应代码
在进行分区时,每个分区就是一个文件系统,而每个文件系统开始位置的那个块就称为超级块。超级块的作用是存储文件系统的大小、空的和填满的块,以及它们各自的总数和其他诸如此类的信息。这也就是说,要使用这一个分区来进行数据访问时,第一个要经过的就是超级块,所以超级块坏了,这个磁盘也就回天乏术了。
super block的中文名称是超级块,它是硬盘分区开头——开头的第一个byte是byte 0,从 byte 1024开始往后的1024 bytes。 超级块中的数据其实就是文件卷的控制信息部分,也可以说它是卷资源表,有关文件卷的大部分信息都保存在这里。例如:硬盘分区中每个block的大小、硬盘分区上一共有多少个block group、以及每个block group中有多少个inode。
超级块的结构为:
/* The Super Block comes first in the block group */
typedef struct tagEXT2_SUPER_BLOCK
{
uint32_t s_inodes_count; /* total no of inodes */
uint32_t s_blocks_count; /* total no of blocks */
uint32_t s_r_blocks_count; /* total no of blocks reserved for exclusive use of superuser */
uint32_t s_free_blocks_count; /* total no of free blocks */
uint32_t s_free_inodes_count; /* total no of free inodes */
uint32_t s_first_data_block; /* position of the first data block */
uint32_t s_log_block_size; /* used to compute logical block size in bytes */
uint32_t s_log_frag_size; /* used to compute logical fragment size */
uint32_t s_blocks_per_group; /* total number of blocks contained in the group */
uint32_t s_frags_per_group; /* total number of fragments in a group */
uint32_t s_inodes_per_group; /* number of inodes in a group */
uint32_t s_mtime; /* time at which the last mount was performed */
uint32_t s_wtime; /* time at which the last write was performed */
uint16_t s_mnt_count; /* number of time the fs system has been mounted in r/w mode without having checked */
uint16_t s_max_mnt_count; /* the max no of times the fs can be mounted in r/w mode before a check must be done */
uint16_t s_magic; /* a number that identifies the fs (eg. 0xef53 for ext2) */
uint16_t s_state; /* gives the state of fs (eg. 0x001 is Unmounted cleanly) */
uint16_t s_pad; /* unused */
uint16_t s_minor_rev_level; /* */
uint32_t s_lastcheck; /* the time of last check performed */
uint32_t s_checkinterval; /* the max possible time between checks on the fs */
uint32_t s_creator_os; /* os */
uint32_t s_rev_level; /* Revision level */
uint16_t s_def_resuid; /* default uid for reserved blocks */
uint16_t s_def_regid; /* default gid for reserved blocks */
/* for EXT2_DYNAMIC_REV superblocks only */
uint32_t s_first_ino; /* First non-reserved inode */
uint16_t s_inode_size; /* size of inode structure */
uint16_t s_block_group_nr; /* block group # of this superblock */
uint32_t s_feature_compat; /* compatible feature set */
uint32_t s_feature_incompat; /* incompatible feature set */
uint32_t s_feature_ro_compat; /* readonly-compatible feature set */
uint8_t s_uuid[16]; /* 128-bit uuid for volume */
char s_volume_name[16]; /* volume name */
char s_last_mounted[64]; /* directory where last mounted */
uint32_t s_algorithm_usage_bitmap; /* For compression */
uint8_t s_prealloc_blocks; /* Nr of blocks to try to preallocate*/
uint8_t s_prealloc_dir_blocks; /* Nr to preallocate for dirs */
uint16_t s_padding1;
uint32_t s_reserved[204]; /* unused */
} EXT2_SUPER_BLOCK;
而ext2read中进行超级块的读取函数为mount,此函数:
int Ext2Partition::mount()
{
EXT2_SUPER_BLOCK sblock;
int gSizes, gSizeb; /* Size of total group desc in sectors */
char *tmpbuf;
//读取超级块数据,读取位置为分区的1024字节后的1024字节
read_disk(handle, &sblock, relative_sect + 2, 2, sect_size); /* read superBlock of root */
if(sblock.s_magic != EXT2_SUPER_MAGIC)
{
printf("Bad Super Block. The drive %s is not ext2 formatted.\n", linux_name.c_str());
return -1;
}
//判断此分区是否支持ext2的特性
if(sblock.s_feature_incompat & EXT2_FEATURE_INCOMPAT_COMPRESSION)
{
printf("File system compression is used which is not supported.\n");
}
//得到块的大小
blocksize = EXT2_BLOCK_SIZE(&sblock);
//得到每个group中有多少个inode
inodes_per_group = EXT2_INODES_PER_GROUP(&sblock);
//得到每个inode结构体的长度
inode_size = EXT2_INODE_SIZE(&sblock);
printf("Block size %d, inp %d, inodesize %d\n", blocksize, inodes_per_group, inode_size);
//得到总的group的个数,用整个分区的块数/每个group的块的个数
totalGroups = (sblock.s_blocks_count)/EXT2_BLOCKS_PER_GROUP(&sblock);
//得到分区所有的group描述信息的大小
gSizeb = (sizeof(EXT2_GROUP_DESC) * totalGroups);
//得到所有group描述信息所占的扇区数
gSizes = (gSizeb / sect_size)+1;
//为所有group分配描述信息的空间
desc = (EXT2_GROUP_DESC *) calloc(totalGroups, sizeof(EXT2_GROUP_DESC));
if(desc == NULL)
{
printf("Not enough Memory: mount: desc: Exiting\n");
exit(1);
}
//分配所有group描述信息所占的字节长度的存储空间
if((tmpbuf = (char *) malloc(gSizes * sect_size)) == NULL)
{
printf("Not enough Memory: mount: tmpbuf: Exiting\n");
exit(1);
}
/* Read all Group descriptors and store in buffer */
/* I really dont know the official start location of Group Descriptor array */
if((blocksize/sect_size) <= 2)
{
//relative_sect是本分区相对于整个硬盘的开始扇区号
read_disk(handle, tmpbuf, relative_sect + ((blocksize/sect_size) + 2), gSizes, sect_size);
}
else
{
//relative_sect是本分区相对于整个硬盘的开始扇区号,偏移一个块的大小是为什么?偏移一个块是因为第一个块存储的是Boot Block等相关的信息
read_disk(handle, tmpbuf, relative_sect + (blocksize/sect_size), gSizes, sect_size);
}
//得到所有的group信息存储到缓存desc中
memcpy(desc, tmpbuf, gSizeb);
free(tmpbuf);
return 0;
}
开源ext2read代码走读之-ext2文件系统中的超级块及对应代码相关推荐
- python代码大全表解释-Python中顺序表的实现简单代码分享
顺序表python版的实现(部分功能未实现) 结果展示: 代码示例: #!/usr/bin/env python # -*- coding:utf-8 -*- class SeqList(object ...
- python代码大全表解释-python中的字典用法大全的代码
如下代码是关于python中的字典用法大全的代码. #!/usr/bin/env python # # [SNIPPET_NAME: Dictionaries 101] # [SNIPPET_CATE ...
- iOS小技能:-fobjc-arc和 -fno-objc-arc 的使用(在非ARC工程中集成ARC代码、在ARC工程中集成非ARC的第三方代码)
文章目录 前言 I ARC 简介 1.1 ARC的规则 1.2 OC中有强参照strong和弱参照weak. 1.3 ARC只能工作于OC. 前言 在非ARC工程中集成ARC代码: 使用-fobjc- ...
- php代码输出笑脸,利用HTML5中的Canvas绘制笑脸的代码
这篇文章主要介绍了利用HTML5中的Canvas绘制一张笑脸的教程,使用Canvas进行绘图是HTML5中的基本功能,需要的朋友可以参考下 今天,你将学习一项称为Canvas(画布)的web技术,以及 ...
- 元胞自动机matlab代码 交通流,交通流中的NaSch模型及MATLAB代码元胞自动机
元胞自动机NaSch模型及其MATLAB代码 作业要求 根据前面的介绍,对NaSch模型编程并进行数值模拟: ●模型参数取值:Lroad=1000,p=0.3,Vmax=5. ●边界条件:周期性边界. ...
- java邮件模板代码_Java的Spring框架中实现发送邮件功能的核心代码示例
Spring中已经封装了邮件操作类,通过spring配置文件可以便捷地注入到controller.action等地方. 下面是配置: p:host="${mail.host}" p ...
- c语言长空格的代码是什么,c语言中表示空格的是什么代码?
分析如下: 不是所有字符都需要转义的,空格直接就敲空格,或者使用ASCII码值赋值为32. 空格没有转义字符.合法转义字符如下: \a 响铃(BEL) .\b 退格(BS).\f 换页(FF).\n ...
- linux之EXT2文件系统--理解block/block group/索引结点inode/索引位图
0. 文件系统和图书馆 在linux上操作文件,和在图书馆借书是非常相似的. 硬盘上的文件系统,好比图书馆的书架:而vfs则是图书馆的管理系统. 内核的工作: 1. 维护一个文件的目录树(dentry ...
- Linux 文件系统及 ext2 文件系统
linux 支持的文件系统类型 Ext2: 有点像 UNIX 文件系统.有 blocks,inodes,directories 的概念. Ext3: Ext2 的加强版,添加了日志的功 ...
- Linux Ext2文件系统
介绍 Ext2文件系统全称为Second Extended FileSystem(第二扩展文件系统),既然是第二个那么就存在第一个,第一个就是扩展文件系统 Extended FileSystem.当然 ...
最新文章
- R语言使用upper.tri函数、lower.tri函数、diag函数改变matrix矩阵上三角形、下三角形、对角线的数值
- js实现语音播报功能
- convirt2.5在虚拟机上安装笔记
- hadoop3.2.1和java1.7版本不兼容解决方案(virtualBox下ubuntu环境linux系统)
- 打开端口_打印机ip及端口设置
- mysql连接方式左联_数据库中的左连接(left join)和右连接(right join)区别 | 改变自己...
- 看这篇就够了!一文读懂拜占庭将军问题
- .Net 数据类型转化
- Linux下部署WordPress
- Oracle12C-针对log4j漏洞补丁修复
- 轻松看懂P(Y=y|x;θ)表示的含义
- BUUCTF——密码学——old-fashion
- MSP430F149小系统开发板实现RS232串口通信
- GwcNet:逐组相关的立体匹配网络(CVPR 2019)
- SQL Server 2014如何修改数据库名
- Android 仿淘宝属性标签页
- 在PGConf.Asia-中文技术论坛,纵览16个方向42场演讲
- 我的世界天空之城服务器位置,我的世界1.7.2服务器天空之城
- 使用 OpenCV 识别图片中的猫咪
- docker alpine包管理工具 apk
热门文章
- GMM R语言程序 gmm包的使用
- NS3 Tracing System详解
- 用html制作带阴影小球弹跳,AE怎么制作一个有影子的弹跳小球动画?
- 华硕h410csm怎么开启_华硕主板怎么开启uefi模式?华硕主板BIOS开启uefi模式详细方法...
- 数理统计常用统计函数
- JavaScript基础入门
- 102个快递编码的对照表
- matlab chrom(i pos),Matlab遗传算法问题 MATLAB遗传算法
- 良田摄像头 linux,良田万能摄像头高拍仪驱动下载|良田万能摄像头驱动 官方版 - 软件下载 - 绿茶软件园|33LC.com...
- c语言中根号绝对值,根号的绝对值怎么算?