linux-0.11调试教程,mkfs.c源代码分析(3)

如果mkfs命令加-c选项会执行check_blocks()函数当然会很费时。大约2分钟。

check_blocks()函数通过逐个读取块的内容,如果不能读取,算出哪个块不能读取,既是坏块,计算出坏块的块号既current_block,然后把对应的逻辑块位图中的位置位,表示已经损坏,在make_bad_inode()函数运行的时候会根据逻辑块位图中的位是否被置1换算成坏块的块号放到坏块文件中的i_zone[]中。

void check_blocks(void)

{
    unsigned int current_block=0;
    int try,got;
    static char buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS];

    while (current_block < ZONES) {
        if (lseek(DEV,current_block*BLOCK_SIZE,SEEK_SET) !=
        current_block*BLOCK_SIZE)
            die("seek failed in check_blocks");
        try = TEST_BUFFER_BLOCKS;
        if (current_block + try > ZONES)
            try = ZONES-current_block;
        got = read(DEV, buffer, try * BLOCK_SIZE);
        if (got<0)
            got = 0;
        if (got & (BLOCK_SIZE-1))
            printf("Weird values in check_blocks: probably bugs\n");
        got /= BLOCK_SIZE;
        current_block += got;
        if (got == try)
            continue;
        if (current_block < FIRSTZONE)
            die("bad blocks before data-area: cannot make fs");
        mark_zone(current_block);
        badblocks++;
        current_block++;
    }
    if (badblocks)
        printf("%d bad block%s\n",badblocks,(badblocks>1)?"s":"");
}


write_tables()函数把超级块和i节点位图块和逻辑块位图块和i节点缓存写到设备中。

void write_tables(void)
{
    if (BLOCK_SIZE != lseek(DEV, BLOCK_SIZE, SEEK_SET))
        die("seek failed in write_tables");
    if (BLOCK_SIZE != write(DEV, super_block_buffer, BLOCK_SIZE))
        die("unable to write super-block");
    if (IMAPS*BLOCK_SIZE != write(DEV,inode_map,IMAPS*BLOCK_SIZE))
        die("Unable to write inode map");
    if (ZMAPS*BLOCK_SIZE != write(DEV,zone_map,ZMAPS*BLOCK_SIZE))
        die("Unable to write zone map");
    if (INODE_BUFFER_SIZE != write(DEV,inode_buffer,INODE_BUFFER_SIZE))
        die("Unable to write inodes");
}

你可能感兴趣的:(linux-0.11调试教程,mkfs.c源代码分析(3))