从ext2文件系统上读出超级块
概述
实现
- static int ext2_fill_super(struct super_block *sb, void *data, int silent)
- {
- struct buffer_head * bh;
- struct ext2_sb_info * sbi;
- struct ext2_super_block * es;
- struct inode *root;
- unsigned long block;
- unsigned long sb_block = get_sb_block(&data);
- unsigned long logic_sb_block;
- unsigned long offset = ;
- unsigned long def_mount_opts;
- long ret = -EINVAL;
- //default block size is 1024B
- int blocksize = BLOCK_SIZE;
- int db_count;
- int i, j;
- __le32 features;
- int err;
- //allocate memory ext2_super_block in memory
- sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
- if (!sbi)
- return -ENOMEM;
- sbi->s_blockgroup_lock =
- kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
- if (!sbi->s_blockgroup_lock) {
- kfree(sbi);
- return -ENOMEM;
- }
- //sb is vfs super_block
- //sb->s_fs_info is specific file system super block
- sb->s_fs_info = sbi;
- sbi->s_sb_block = sb_block;
- spin_lock_init(&sbi->s_lock);
- /*
- * See what the current blocksize for the device is, and
- * use that as the blocksize. Otherwise (or if the blocksize
- * is smaller than the default) use the default.
- * This is important for devices that have a hardware
- * sectorsize that is larger than the default.
- */
- //the block size can't be smaller than BLOCK_SIZE=1024B
- //and block size must be smaller than PAGE_SIZE = 4096B now
- blocksize = sb_min_blocksize(sb, BLOCK_SIZE);
- if (!blocksize) {
- ext2_msg(sb, KERN_ERR, "error: unable to set blocksize");
- goto failed_sbi;
- }
- /*
- * If the superblock doesn't start on a hardware sector boundary,
- * calculate the offset.
- */
- //blocksize may bigger than BLOCK_SIZE=1024B
- //because we read blocksize bytes data from disk
- //Block 0 is 1024B and super_block is also 1024B
- //if blocksize is not 1024B,it must be bigger than 1024B,for example,if blocksize is 2048B
- //we must read block 0(first 2048B on disk),then we read offset 1024~2047 as super block
- if (blocksize != BLOCK_SIZE) {
- logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
- offset = (sb_block*BLOCK_SIZE) % blocksize;
- } else {
- logic_sb_block = sb_block;
- }
- //read block @logic_sb_block containg super block
- if (!(bh = sb_bread(sb, logic_sb_block))) {
- ext2_msg(sb, KERN_ERR, "error: unable to read superblock");
- goto failed_sbi;
- }
- /*
- * Note: s_es must be initialized as soon as possible because
- * some ext2 macro-instructions depend on its value
- */
- es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
- //sbi is ext2_super_block in memory while sbi->s_es is ext2_super_block on disk
- sbi->s_es = es;
- 超级块的起始位置在哪?
- 超级块的大小是多少?
- 在实现中我们自己定义的块大小(默认1024)与磁盘设备的块大小如果不一致怎么办?
- 分配ext2内存超级块结构struct ext2_sb_info,如果分配内存失败,则直接返回-ENOMEM;
- 确定逻辑磁盘块大小,比较默认逻辑块大小和真实逻辑块大小(根据磁盘设备的一些信息确定),将最大者设置为逻辑块大小,但注意:该最大者必须是2的次幂且不可大于4096
- 从磁盘上读出超级块根据2中计算的块大小确定超级块所在逻辑块号和块内偏移,读出超级块,存储在1中分配的内存超级块结构中sbi->s_es = es。
- sb->s_magic = le16_to_cpu(es->s_magic);
- if (sb->s_magic != EXT2_SUPER_MAGIC)
- goto cantfind_ext2;
- /* Set defaults before we parse the mount options */
- /* 接下来这段根据磁盘超级块
- ** 结构来设置内存超级块结构的部分选项
- ** 相比较而言这些选项的重要性没那么高
- */
- def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
- if (def_mount_opts & EXT2_DEFM_DEBUG)
- set_opt(sbi->s_mount_opt, DEBUG);
- if (def_mount_opts & EXT2_DEFM_BSDGROUPS)
- set_opt(sbi->s_mount_opt, GRPID);
- if (def_mount_opts & EXT2_DEFM_UID16)
- set_opt(sbi->s_mount_opt, NO_UID32);
- #ifdef CONFIG_EXT2_FS_XATTR
- if (def_mount_opts & EXT2_DEFM_XATTR_USER)
- set_opt(sbi->s_mount_opt, XATTR_USER);
- #endif
- #ifdef CONFIG_EXT2_FS_POSIX_ACL
- if (def_mount_opts & EXT2_DEFM_ACL)
- set_opt(sbi->s_mount_opt, POSIX_ACL);
- #endif
- /* 这个选项决定了挂载出错时的处理方法
- ** 如PANIC即指示出错就奔溃...
- */
- if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_PANIC)
- set_opt(sbi->s_mount_opt, ERRORS_PANIC);
- else if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_CONTINUE)
- set_opt(sbi->s_mount_opt, ERRORS_CONT);
- else
- set_opt(sbi->s_mount_opt, ERRORS_RO);
- sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
- sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
- set_opt(sbi->s_mount_opt, RESERVATION);
- if (!parse_options((char *) data, sb))
- goto failed_mount;
- sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
- ((EXT2_SB(sb)->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ?
- MS_POSIXACL : );
- ext2_xip_verify_sb(sb); /* see if bdev supports xip, unset
- EXT2_MOUNT_XIP if not */
- if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV &&
- (EXT2_HAS_COMPAT_FEATURE(sb, ~0U) ||
- EXT2_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
- EXT2_HAS_INCOMPAT_FEATURE(sb, ~0U)))
- ext2_msg(sb, KERN_WARNING,
- "warning: feature flags set on rev 0 fs, "
- "running e2fsck is recommended");
- /*
- * Check feature flags regardless of the revision level, since we
- * previously didn't change the revision level when setting the flags,
- * so there is a chance incompat flags are set on a rev 0 filesystem.
- */
- features = EXT2_HAS_INCOMPAT_FEATURE(sb, ~EXT2_FEATURE_INCOMPAT_SUPP);
- if (features) {
- ext2_msg(sb, KERN_ERR, "error: couldn't mount because of "
- "unsupported optional features (%x)",
- le32_to_cpu(features));
- goto failed_mount;
- }
- if (!(sb->s_flags & MS_RDONLY) &&
- (features = EXT2_HAS_RO_COMPAT_FEATURE(sb, ~EXT2_FEATURE_RO_COMPAT_SUPP))){
- ext2_msg(sb, KERN_ERR, "error: couldn't mount RDWR because of "
- "unsupported optional features (%x)",
- le32_to_cpu(features));
- goto failed_mount;
- }
- /*
- ** 超级块中可能记录着逻辑块大小,因此我们必须
- ** 以此为准
- */
- blocksize = BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
- if (ext2_use_xip(sb) && blocksize != PAGE_SIZE) {
- if (!silent)
- ext2_msg(sb, KERN_ERR,
- "error: unsupported blocksize for xip");
- goto failed_mount;
- }
- /* If the blocksize doesn't match, re-read the thing.. */
- /* 如果块大小和我们之前确定的不太一样
- ** 我们有必要重新读一次超级块
- ** 因为之前读的可能并不准确
- */
- if (sb->s_blocksize != blocksize) {
- brelse(bh);
- if (!sb_set_blocksize(sb, blocksize)) {
- ext2_msg(sb, KERN_ERR, "error: blocksize is too small");
- goto failed_sbi;
- }
- logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
- offset = (sb_block*BLOCK_SIZE) % blocksize;
- bh = sb_bread(sb, logic_sb_block);
- if(!bh) {
- ext2_msg(sb, KERN_ERR, "error: couldn't read"
- "superblock on 2nd try");
- goto failed_sbi;
- }
- es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
- sbi->s_es = es;
- if (es->s_magic != cpu_to_le16(EXT2_SUPER_MAGIC)) {
- ext2_msg(sb, KERN_ERR, "error: magic mismatch");
- goto failed_mount;
- }
- }
- /* 计算ext2最大可支持文件的大小*/
- sb->s_maxbytes = ext2_max_size(sb->s_blocksize_bits);
- if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV) {
- sbi->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
- sbi->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
- } else {
- sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
- sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
- if ((sbi->s_inode_size < EXT2_GOOD_OLD_INODE_SIZE) ||
- !is_power_of_2(sbi->s_inode_size) ||
- (sbi->s_inode_size > blocksize)) {
- ext2_msg(sb, KERN_ERR,
- "error: unsupported inode size: %d",
- sbi->s_inode_size);
- goto failed_mount;
- }
- }
- /* 对于逻辑块较大的ext2文件系统,为了
- ** 减少块内碎片问题,设置了fragment,
- ** 即每个磁盘块内可再细分成多个fragment
- ** 这个思想源自FFS,对于1024大小的磁盘块
- ** 也就没有必要再划分fragment了
- ** 因为最小的fragment大小就是1024字节
- */
- sbi->s_frag_size = EXT2_MIN_FRAG_SIZE <<
- le32_to_cpu(es->s_log_frag_size);
- if (sbi->s_frag_size == )
- goto cantfind_ext2;
- /* 初始化一些静态信息*/
- sbi->s_frags_per_block = sb->s_blocksize / sbi->s_frag_size;
- sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
- sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
- sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
- if (EXT2_INODE_SIZE(sb) == )
- goto cantfind_ext2;
- sbi->s_inodes_per_block = sb->s_blocksize / EXT2_INODE_SIZE(sb);
- if (sbi->s_inodes_per_block == || sbi->s_inodes_per_group == )
- goto cantfind_ext2;
- sbi->s_itb_per_group = sbi->s_inodes_per_group /
- sbi->s_inodes_per_block;
- sbi->s_desc_per_block = sb->s_blocksize /
- sizeof (struct ext2_group_desc);
- sbi->s_sbh = bh;
- sbi->s_mount_state = le16_to_cpu(es->s_state);
- sbi->s_addr_per_block_bits =
- ilog2 (EXT2_ADDR_PER_BLOCK(sb));
- sbi->s_desc_per_block_bits =
- ilog2 (EXT2_DESC_PER_BLOCK(sb));
- if (sb->s_magic != EXT2_SUPER_MAGIC)
- goto cantfind_ext2;
- if (sb->s_blocksize != bh->b_size) {
- if (!silent)
- ext2_msg(sb, KERN_ERR, "error: unsupported blocksize");
- goto failed_mount;
- }
- /* 目前仅支持块大小和fragment size大小相同*/
- if (sb->s_blocksize != sbi->s_frag_size) {
- ext2_msg(sb, KERN_ERR,
- "error: fragsize %lu != blocksize %lu"
- "(not supported yet)",
- sbi->s_frag_size, sb->s_blocksize);
- goto failed_mount;
- }
- if (sbi->s_blocks_per_group > sb->s_blocksize * ) {
- ext2_msg(sb, KERN_ERR,
- "error: #blocks per group too big: %lu",
- sbi->s_blocks_per_group);
- goto failed_mount;
- }
- if (sbi->s_frags_per_group > sb->s_blocksize * ) {
- ext2_msg(sb, KERN_ERR,
- "error: #fragments per group too big: %lu",
- sbi->s_frags_per_group);
- goto failed_mount;
- }
- if (sbi->s_inodes_per_group > sb->s_blocksize * ) {
- ext2_msg(sb, KERN_ERR,
- "error: #inodes per group too big: %lu",
- sbi->s_inodes_per_group);
- goto failed_mount;
- }
- if (EXT2_BLOCKS_PER_GROUP(sb) == )
- goto cantfind_ext2;
- sbi->s_groups_count = ((le32_to_cpu(es->s_blocks_count) -
- le32_to_cpu(es->s_first_data_block) - )
- / EXT2_BLOCKS_PER_GROUP(sb)) + ;
- db_count = (sbi->s_groups_count + EXT2_DESC_PER_BLOCK(sb) - ) /
- EXT2_DESC_PER_BLOCK(sb);
- sbi->s_group_desc = kmalloc (db_count * sizeof (struct buffer_head *), GFP_KERNEL);
- if (sbi->s_group_desc == NULL) {
- ext2_msg(sb, KERN_ERR, "error: not enough memory");
- goto failed_mount;
- }
- bgl_lock_init(sbi->s_blockgroup_lock);
- /* 这个数据结构干嘛的现在还不得而知*/
- sbi->s_debts = kcalloc(sbi->s_groups_count, sizeof(*sbi->s_debts), GFP_KERNEL);
- if (!sbi->s_debts) {
- ext2_msg(sb, KERN_ERR, "error: not enough memory");
- goto failed_mount_group_desc;
- }
- /* 读出块组描述符信息 */
- for (i = ; i < db_count; i++) {
- block = descriptor_loc(sb, logic_sb_block, i);
- sbi->s_group_desc[i] = sb_bread(sb, block);
- if (!sbi->s_group_desc[i]) {
- for (j = ; j < i; j++)
- brelse (sbi->s_group_desc[j]);
- ext2_msg(sb, KERN_ERR,
- "error: unable to read group descriptors");
- goto failed_mount_group_desc;
- }
- }
- if (!ext2_check_descriptors (sb)) {
- ext2_msg(sb, KERN_ERR, "group descriptors corrupted");
- goto failed_mount2;
- }
- sbi->s_gdb_count = db_count;
- get_random_bytes(&sbi->s_next_generation, sizeof(u32));
- spin_lock_init(&sbi->s_next_gen_lock);
- /* per fileystem reservation list head & lock */
- //init something for reservation windows of every file
- spin_lock_init(&sbi->s_rsv_window_lock);
- sbi->s_rsv_window_root = RB_ROOT;
- /*
- * Add a single, static dummy reservation to the start of the
- * reservation window list --- it gives us a placeholder for
- * append-at-start-of-list which makes the allocation logic
- * _much_ simpler.
- */
- /* 初始化内存超级块的预留窗口
- ** 所谓的预留窗口是在分配数据块的时候
- ** 每一次多分配一点,以提高文件数据存储
- ** 的连续性
- */
- sbi->s_rsv_window_head.rsv_start = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
- sbi->s_rsv_window_head.rsv_end = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
- sbi->s_rsv_window_head.rsv_alloc_hit = ;
- sbi->s_rsv_window_head.rsv_goal_size = ;
- ext2_rsv_window_add(sb, &sbi->s_rsv_window_head);
- err = percpu_counter_init(&sbi->s_freeblocks_counter,
- ext2_count_free_blocks(sb));
- if (!err) {
- err = percpu_counter_init(&sbi->s_freeinodes_counter,
- ext2_count_free_inodes(sb));
- }
- if (!err) {
- err = percpu_counter_init(&sbi->s_dirs_counter,
- ext2_count_dirs(sb));
- }
- if (err) {
- ext2_msg(sb, KERN_ERR, "error: insufficient memory");
- goto failed_mount3;
- }
- /*
- * set up enough so that it can read an inode
- */
- sb->s_op = &ext2_sops;
- sb->s_export_op = &ext2_export_ops;
- sb->s_xattr = ext2_xattr_handlers;
- #ifdef CONFIG_QUOTA
- sb->dq_op = &dquot_operations;
- sb->s_qcop = &dquot_quotactl_ops;
- #endif
- /* 读根目录的inode,inode号为默认值2
- ** 读出后保存在内存inode结构中
- */
- root = ext2_iget(sb, EXT2_ROOT_INO);
- if (IS_ERR(root)) {
- ret = PTR_ERR(root);
- goto failed_mount3;
- }
- if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
- iput(root);
- ext2_msg(sb, KERN_ERR, "error: corrupt root inode, run e2fsck");
- goto failed_mount3;
- }
- /* 分配根目录的内存目录项
- ** 因为根目录没有父目录这个概念
- ** 因此,没法从其父目录中读出其目录
- ** 只能在内存中构造一个
- */
- sb->s_root = d_alloc_root(root);
- if (!sb->s_root) {
- iput(root);
- ext2_msg(sb, KERN_ERR, "error: get root inode failed");
- ret = -ENOMEM;
- goto failed_mount3;
- }
- if (EXT2_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL))
- ext2_msg(sb, KERN_WARNING,
- "warning: mounting ext3 filesystem as ext2");
- if (ext2_setup_super (sb, es, sb->s_flags & MS_RDONLY))
- sb->s_flags |= MS_RDONLY;
- /* 在填充超级块时有可能会修改磁盘超级块
- ** 因此有必要作一次写回操作
- */
- ext2_write_super(sb);
- return ;
到此,整个从磁盘读出超级块直至填充内存超级块结构的过程就结束了,整个流程虽然繁杂,但还算简单。
从ext2文件系统上读出超级块的更多相关文章
- Linux 虚拟文件系统四大对象:超级块、inode、dentry、file之间关系
更多嵌入式原创文章,请关注公众号:一口Linux 一:文件系统 1. 什么是文件系统? 操作系统中负责管理和存储文件信息的软件机构称为文件管理系统,简称文件系统. 通常文件系统是用于存储和组织文件的一 ...
- 文件系统VFS数据结构(超级块 inode dentry file)(收集整理)
Linux虚拟文件系统四大对象: 1)超级块(super block) 2)索引节点(inode) 3)目录项(dentry) 4)文件对象(file) 一个进程在对一个文件进行操作时各种对象的引用过 ...
- Linux 文件系统错误的修复方法 ddrescue替代dd的恢复软件 备用超级块
Linux 文件系统错误的修复方法 ddrescue替代dd的恢复软件 备用超级块 最近处理的一件 linux 服务器断电导致文件系统启动后文件系统不可读写,数据不可用的案例,现总结下 Linux ...
- linux ext2 文件系统学习
Linux ext2文件系统理解 硬盘组成: 硬盘由多个圆形硬盘片组成.按照硬盘片能够容纳的数据量分为单盘和多盘.硬盘的数据读取主要靠机械手臂上的磁头,在机械手臂上有多个磁头.机械手臂不动硬盘旋转一 ...
- linux超级块和inode 详解 和 df 、du 命令详解与环境变量
一.inode块,Unix文件的核心. 首先需要明白的是,在Unix操作系统中的任何资源都被当作文件来管理.如目录.光驱.终端设备等等,都被当作是一种文件.从这方面来说,Unix操作系统中的所有的目录 ...
- 81 dumpe2fs-打印“ext2/ext3”文件系统的超级块和快组信息
dumpe2fs打印"ext2/ext3"文件系统的超级块和快组信息. 语法 dumpe2fs (选项) (参数) 选项 -b:打印文件系统中预留的块信息: -ob<超级块& ...
- Ext2文件系统布局,文件数据块寻址,VFS虚拟文件系统
注:本分类下文章大多整理自<深入分析linux内核源代码>一书,另有参考其他一些资料如<linux内核完全剖析>.<linux c 编程一站式学习>等,只是为了更好 ...
- linux操作系统故障处理-ext4文件系统超级块损坏修复
linux操作系统故障处理-ext4文件系统超级块损坏修复 背景 前天外面出差大数据测试环境平台有7台服务器挂了,同事重启好了五台服务器,但是还有两台服务器启动不起来,第二天回来后我和同事再次去机 ...
- 一步一步粗谈linux文件系统(三)----超级块(superblock)【转】
本文转载自:https://blog.csdn.net/fenglifeng1987/article/details/8302921 超级块是来描述整个文件系统信息的,可以说是一个全局的数据结构,可以 ...
随机推荐
- R语言简单入门
一.运行R语言可以做哪些事? 1.探索性数据分析(将数据绘制图表) 2.统计推断(根据数据进行预测) 3.回归分析(对数据进行拟合分析) 4.机器学习(对数据集进行训练和预测) 5.数据产品开发 二. ...
- poj 1552 Doubles
#include <stdio.h> #include <stdlib.h> ]; int cmp(const void *a, const void *b) { return ...
- Nginx下Redmine2.6配置
Apache下Redmine2.6配置,请参考:http://blog.linuxeye.com/405.html CentOS6.6下安装Redmine2.6+MySQL5.6+Nginx,教程如下 ...
- jBox使用方法
1.引入jquery文件 2.引入css和jBox文件 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml& ...
- 自己制作 SPx N合1 自动安装盘(x86)
来处"xinso" 一.制作方法: 以技嘉和惠普为例作,其它的可以如法泡制及变通: 1.复制一份最常用的 OEM XP,例如技嘉,到D:\1TO2 2.在 D:\ 创造一个 HP ...
- Socket小项目的一些心得(鸣谢传智的教学视频)
Socket是一种封装了四层通信的整体抽象入口,通常也称作"套接字",这是常用的四层通信这是访问Socket的流程图,这个分为客户端和服务器端,其中服务器端有以下步骤去建立,前面的 ...
- yum添加网易和搜狐源
先进入yum源配置目录 cd /etc/yum.repos.d 备份系统自带的yum源 mv CentOS-Base.repo CentOS-Base.repo.save 163的yum源: wget ...
- ajax提交后台自动反序列化成对象
下面是ajax提交的方法,主要用到了$form.serializeArray()将表单序列化成json对象,然后用each处理一下,代码如下: var $form = $('<form>& ...
- FullCalendar应用——读取JSON数据
开发者可以使用FullCalendar创建强大的日历日程应用,FullCalendar提供了丰富的选项设置和方法调用,使得开发者可以轻松的创建各种功能的日历程序.本文将结合实例使用PHP读取MySQl ...
- AO中的GraphicsLayer---------元素的容器
come from AO中的GraphicsLayer---------元素的容器 图形元素(Graphic Element)是存储于GraphicsLayer中的,本文主要涉及的接口和类主要有以下几 ...