读取超级块【ext2_fill_super】

这篇博客深入探讨了在ext2文件系统中`ext2_fill_super`函数的角色,该函数负责获取超级块信息。在缺乏硬盘详细信息的情况下,如何有效地读取并解析超级块成为一个挑战。文章进一步介绍了如何利用这些信息来初始化块组,为后续的文件系统操作做准备。

该函数是的作用其实很简单,就是为了取得超级快的信息。但是比较麻烦的地方是,如果没有超级块的信息,我们对硬盘还一无所知,怎么读取到想要的信息?

//从data参数中读取超级块的块号“sb=XXXX”
unsigned long sb_block = get_sb_block(&data);

static unsigned long get_sb_block(void **data)
{
        unsigned long   sb_block;
        char            *options = (char *) *data;

        if (!options || strncmp(options, "sb=", 3) != 0)
               return 1;       /* Default location */
        options += 3;
        sb_block = simple_strtoul(options, &options, 0);
        if (*options && *options != ',') {
                printk("EXT2-fs: Invalid sb specification: %s\n",(char *) *data);
                return 1;
        }
        if (*options == ',')
                options++;
        *data = (void *) options;
        return sb_block;
}
get_sb_block函数其实就是简单的从data中提取信息,simple_strtoul函数也很简单,把各种进制的字符串转换成数字。

//为ext2_sb_info分配空间
sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
这里我们还是不知道blocksize的具体的值,但是block_device中保存的值是可以取到的。在取blocksize值后要去设置super_block中的属性,这时候就要判断大小合不合适。如果太小,比512都小,那block_device搞不定这么小的值啊。或者说这个值不是2的幂级,等等。

blocksize = sb_min_blocksize(sb, BLOCK_SIZE);

int sb_min_blocksize(struct super_block *sb, int size)
{
	/*
	 * super_block->block_device->request_queue->hardsect_size
	 * 默认是512
	 */
        int minsize = bdev_hardsect_size(sb->s_bdev);
        if (size < minsize)
                size = minsize;
        return sb_set_blocksize(sb, size);
}
刚才从data中取到的sb_block是按照块大小为BLOCK_SIZE来的,如果blocksize不是这个值,那现在的sb_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;
}
下面就开始读ext2_super_block的内容了,读取的过程由sb_bread来完成(貌似读取没那么简单,这个过程以后补充)。根据offset设置es指针:

if (!(bh = sb_bread(sb, logic_sb_block))) {
        printk ("EXT2-fs: unable to read superblock\n");
        goto failed_sbi;
}
es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
在设置完一些默认属性之后开始根据mount时带的data参数来设置对应的属性,这个里面的
// SPDX-License-Identifier: GPL-2.0 #include <linux/kernel.h> #include <linux/fs.h> #include <linux/minix_fs.h> #include <linux/ext2_fs.h> #include <linux/romfs_fs.h> #include <uapi/linux/cramfs_fs.h> #include <linux/initrd.h> #include <linux/string.h> #include <linux/slab.h> #include "do_mounts.h" #include "../fs/squashfs/squashfs_fs.h" #include <linux/decompress/generic.h> static struct file *in_file, *out_file; static loff_t in_pos, out_pos; static int __init prompt_ramdisk(char *str) { pr_warn("ignoring the deprecated prompt_ramdisk= option\n"); return 1; } __setup("prompt_ramdisk=", prompt_ramdisk); int __initdata rd_image_start; /* starting block # of image */ static int __init ramdisk_start_setup(char *str) { rd_image_start = simple_strtol(str,NULL,0); return 1; } __setup("ramdisk_start=", ramdisk_start_setup); static int __init crd_load(decompress_fn deco); /* * This routine tries to find a RAM disk image to load, and returns the * number of blocks to read for a non-compressed image, 0 if the image * is a compressed image, and -1 if an image with the right magic * numbers could not be found. * * We currently check for the following magic numbers: * minix * ext2 * romfs * cramfs * squashfs * gzip * bzip2 * lzma * xz * lzo * lz4 */ static int __init identify_ramdisk_image(struct file *file, loff_t pos, decompress_fn *decompressor) { const int size = 512; struct minix_super_block *minixsb; struct romfs_super_block *romfsb; struct cramfs_super *cramfsb; struct squashfs_super_block *squashfsb; int nblocks = -1; unsigned char *buf; const char *compress_name; unsigned long n; int start_block = rd_image_start; buf = kmalloc(size, GFP_KERNEL); if (!buf) return -ENOMEM; minixsb = (struct minix_super_block *) buf; romfsb = (struct romfs_super_block *) buf; cramfsb = (struct cramfs_super *) buf; squashfsb = (struct squashfs_super_block *) buf; memset(buf, 0xe5, size); /* * Read block 0 to test for compressed kernel */ pos = start_block * BLOCK_SIZE; kernel_read(file, buf, size, &pos); *decompressor = decompress_method(buf, size, &compress_name); if (compress_name) { printk(KERN_NOTICE "RAMDISK: %s image found at block %d\n", compress_name, start_block); if (!*decompressor) printk(KERN_EMERG "RAMDISK: %s decompressor not configured!\n", compress_name); nblocks = 0; goto done; } /* romfs is at block zero too */ if (romfsb->word0 == ROMSB_WORD0 && romfsb->word1 == ROMSB_WORD1) { printk(KERN_NOTICE "RAMDISK: romfs filesystem found at block %d\n", start_block); nblocks = (ntohl(romfsb->size)+BLOCK_SIZE-1)>>BLOCK_SIZE_BITS; goto done; } if (cramfsb->magic == CRAMFS_MAGIC) { printk(KERN_NOTICE "RAMDISK: cramfs filesystem found at block %d\n", start_block); nblocks = (cramfsb->size + BLOCK_SIZE - 1) >> BLOCK_SIZE_BITS; goto done; } /* squashfs is at block zero too */ if (le32_to_cpu(squashfsb->s_magic) == SQUASHFS_MAGIC) { printk(KERN_NOTICE "RAMDISK: squashfs filesystem found at block %d\n", start_block); nblocks = (le64_to_cpu(squashfsb->bytes_used) + BLOCK_SIZE - 1) >> BLOCK_SIZE_BITS; goto done; } /* * Read 512 bytes further to check if cramfs is padded */ pos = start_block * BLOCK_SIZE + 0x200; kernel_read(file, buf, size, &pos); if (cramfsb->magic == CRAMFS_MAGIC) { printk(KERN_NOTICE "RAMDISK: cramfs filesystem found at block %d\n", start_block); nblocks = (cramfsb->size + BLOCK_SIZE - 1) >> BLOCK_SIZE_BITS; goto done; } /* * Read block 1 to test for minix and ext2 superblock */ pos = (start_block + 1) * BLOCK_SIZE; kernel_read(file, buf, size, &pos); /* Try minix */ if (minixsb->s_magic == MINIX_SUPER_MAGIC || minixsb->s_magic == MINIX_SUPER_MAGIC2) { printk(KERN_NOTICE "RAMDISK: Minix filesystem found at block %d\n", start_block); nblocks = minixsb->s_nzones << minixsb->s_log_zone_size; goto done; } /* Try ext2 */ n = ext2_image_size(buf); if (n) { printk(KERN_NOTICE "RAMDISK: ext2 filesystem found at block %d\n", start_block); nblocks = n; goto done; } printk(KERN_NOTICE "RAMDISK: Couldn't find valid RAM disk image starting at %d.\n", start_block); done: kfree(buf); return nblocks; } static unsigned long nr_blocks(struct file *file) { struct inode *inode = file->f_mapping->host; if (!S_ISBLK(inode->i_mode)) return 0; return i_size_read(inode) >> 10; } int __init rd_load_image(char *from) { int res = 0; unsigned long rd_blocks, devblocks; int nblocks, i; char *buf = NULL; unsigned short rotate = 0; decompress_fn decompressor = NULL; #if !defined(CONFIG_S390) char rotator[4] = { '|' , '/' , '-' , '\\' }; #endif out_file = filp_open("/dev/ram", O_RDWR, 0); if (IS_ERR(out_file)) goto out; in_file = filp_open(from, O_RDONLY, 0); if (IS_ERR(in_file)) goto noclose_input; in_pos = rd_image_start * BLOCK_SIZE; nblocks = identify_ramdisk_image(in_file, in_pos, &decompressor); if (nblocks < 0) goto done; if (nblocks == 0) { if (crd_load(decompressor) == 0) goto successful_load; goto done; } /* * NOTE NOTE: nblocks is not actually blocks but * the number of kibibytes of data to load into a ramdisk. */ rd_blocks = nr_blocks(out_file); if (nblocks > rd_blocks) { printk("RAMDISK: image too big! (%dKiB/%ldKiB)\n", nblocks, rd_blocks); goto done; } /* * OK, time to copy in the data */ if (strcmp(from, "/initrd.image") == 0) devblocks = nblocks; else devblocks = nr_blocks(in_file); if (devblocks == 0) { printk(KERN_ERR "RAMDISK: could not determine device size\n"); goto done; } buf = kmalloc(BLOCK_SIZE, GFP_KERNEL); if (!buf) { printk(KERN_ERR "RAMDISK: could not allocate buffer\n"); goto done; } printk(KERN_NOTICE "RAMDISK: Loading %dKiB [%ld disk%s] into ram disk... ", nblocks, ((nblocks-1)/devblocks)+1, nblocks>devblocks ? "s" : ""); for (i = 0; i < nblocks; i++) { if (i && (i % devblocks == 0)) { pr_cont("done disk #1.\n"); rotate = 0; fput(in_file); break; } kernel_read(in_file, buf, BLOCK_SIZE, &in_pos); kernel_write(out_file, buf, BLOCK_SIZE, &out_pos); #if !defined(CONFIG_S390) if (!(i % 16)) { pr_cont("%c\b", rotator[rotate & 0x3]); rotate++; } #endif } pr_cont("done.\n"); successful_load: res = 1; done: fput(in_file); noclose_input: fput(out_file); out: kfree(buf); init_unlink("/dev/ram"); return res; } int __init rd_load_disk(int n) { create_dev("/dev/root", ROOT_DEV); create_dev("/dev/ram", MKDEV(RAMDISK_MAJOR, n)); return rd_load_image("/dev/root"); } static int exit_code; static int decompress_error; static long __init compr_fill(void *buf, unsigned long len) { long r = kernel_read(in_file, buf, len, &in_pos); if (r < 0) printk(KERN_ERR "RAMDISK: error while reading compressed data"); else if (r == 0) printk(KERN_ERR "RAMDISK: EOF while reading compressed data"); return r; } static long __init compr_flush(void *window, unsigned long outcnt) { long written = kernel_write(out_file, window, outcnt, &out_pos); if (written != outcnt) { if (decompress_error == 0) printk(KERN_ERR "RAMDISK: incomplete write (%ld != %ld)\n", written, outcnt); decompress_error = 1; return -1; } return outcnt; } static void __init error(char *x) { printk(KERN_ERR "%s\n", x); exit_code = 1; decompress_error = 1; } static int __init crd_load(decompress_fn deco) { int result; if (!deco) { pr_emerg("Invalid ramdisk decompression routine. " "Select appropriate config option.\n"); panic("Could not decompress initial ramdisk image."); } result = deco(NULL, 0, compr_fill, compr_flush, NULL, NULL, error); if (decompress_error) result = 1; return result; }
10-24
这段代码是Linux内核中用于加载RAM磁盘(ramdisk)镜像的实现,主要功能包括: 1. **RAM磁盘的识别与加载**: - `identify_ramdisk_image`函数负责检测RAM磁盘镜像的文件系统类型(如minix、ext2、romfs、cramfs、squashfs)或压缩格式(如gzip、bzip2、xz等),并返回镜像的大小(以为单位)。 - 如果镜像被压缩,则调用相应的解压函数(如`crd_load`)进行解压。 2. **RAM磁盘的配置选项**: - `prompt_ramdisk`和`ramdisk_start`用于处理内核启动参数中的RAM磁盘相关选项,但`prompt_ramdisk`已被弃用。 - `rd_image_start`变量用于存储RAM磁盘镜像的起始。 3. **RAM磁盘的加载过程**: - `rd_load_image`函数负责从指定的文件(如`/initrd.image`)加载RAM磁盘镜像到`/dev/ram`设备中。 - 如果镜像未压缩,则直接按读取并写入RAM磁盘设备。 - 如果镜像是压缩的,则调用`crd_load`函数进行解压,解压过程中通过`compr_fill`和`compr_flush`分别读取压缩数据和写入解压后的数据。 4. **解压支持**: - `compr_fill`函数从输入文件中读取压缩数据。 - `compr_flush`函数将解压后的数据写入RAM磁盘设备。 - `error`函数处理解压过程中的错误信息。 5. **RAM磁盘设备的创建与加载**: - `rd_load_disk`函数用于创建RAM磁盘设备(如`/dev/ram`)并调用`rd_load_image`加载镜像。 ### 示例格式 快速排序的基本思想是选择一个基准元素 ```python def quick_sort(arr): if len(arr) <= 1: return arr else: # 选择基准值 pivot = arr[len(arr) // 2] # 分别存放比基准小和大的元素 left = [x for x in arr if x < pivot] middle = [x for x in arr if x == pivot] right = [x for x in arr if x > pivot] # 递归地对左右两边进行快排,并合并结果 return quick_sort(left) + middle + quick_sort(right)```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值