2 * check_desc.c --- Check the group descriptors of an ext2 filesystem
4 * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
23 #include <sys/types.h>
30 * This routine sanity checks the group descriptors
32 errcode_t ext2fs_check_desc(ext2_filsys fs)
34 ext2fs_block_bitmap bmap;
37 blk_t first_block = fs->super->s_first_data_block;
38 blk_t last_block = ext2fs_blocks_count(fs->super)-1;
42 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
44 retval = ext2fs_allocate_block_bitmap(fs, "check_desc map", &bmap);
48 for (i = 0; i < fs->group_desc_count; i++)
49 ext2fs_reserve_super_and_bgd(fs, i, bmap);
51 for (i = 0; i < fs->group_desc_count; i++) {
52 if (!EXT2_HAS_INCOMPAT_FEATURE(fs->super,
53 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
54 first_block = ext2fs_group_first_block(fs, i);
55 last_block = ext2fs_group_last_block(fs, i);
56 if (i == (fs->group_desc_count - 1))
57 last_block = ext2fs_blocks_count(fs->super)-1;
61 * Check to make sure the block bitmap for group is sane
63 blk = fs->group_desc[i].bg_block_bitmap;
64 if (blk < first_block || blk > last_block ||
65 ext2fs_test_block_bitmap2(bmap, blk)) {
66 retval = EXT2_ET_GDESC_BAD_BLOCK_MAP;
69 ext2fs_mark_block_bitmap2(bmap, blk);
72 * Check to make sure the inode bitmap for group is sane
74 blk = fs->group_desc[i].bg_inode_bitmap;
75 if (blk < first_block || blk > last_block ||
76 ext2fs_test_block_bitmap2(bmap, blk)) {
77 retval = EXT2_ET_GDESC_BAD_INODE_MAP;
80 ext2fs_mark_block_bitmap2(bmap, blk);
83 * Check to make sure the inode table for group is sane
85 blk = fs->group_desc[i].bg_inode_table;
86 if (blk < first_block ||
87 ((blk + fs->inode_blocks_per_group - 1) > last_block)) {
88 retval = EXT2_ET_GDESC_BAD_INODE_TABLE;
91 for (j = 0, b = blk; j < fs->inode_blocks_per_group;
93 if (ext2fs_test_block_bitmap2(bmap, b)) {
94 retval = EXT2_ET_GDESC_BAD_INODE_TABLE;
97 ext2fs_mark_block_bitmap2(bmap, b);
101 ext2fs_free_block_bitmap(bmap);