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 Library
8 * General Public License, version 2.
24 #include <sys/types.h>
31 * This routine sanity checks the group descriptors
33 errcode_t ext2fs_check_desc(ext2_filsys fs)
35 ext2fs_block_bitmap bmap;
38 blk64_t first_block = fs->super->s_first_data_block;
39 blk64_t last_block = ext2fs_blocks_count(fs->super)-1;
43 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
45 retval = ext2fs_allocate_subcluster_bitmap(fs, "check_desc map", &bmap);
49 for (i = 0; i < fs->group_desc_count; i++)
50 ext2fs_reserve_super_and_bgd(fs, i, bmap);
52 for (i = 0; i < fs->group_desc_count; i++) {
53 if (!EXT2_HAS_INCOMPAT_FEATURE(fs->super,
54 EXT4_FEATURE_INCOMPAT_FLEX_BG)) {
55 first_block = ext2fs_group_first_block2(fs, i);
56 last_block = ext2fs_group_last_block2(fs, i);
60 * Check to make sure the block bitmap for group is sane
62 blk = ext2fs_block_bitmap_loc(fs, i);
63 if (blk < first_block || blk > last_block ||
64 ext2fs_test_block_bitmap2(bmap, blk)) {
65 retval = EXT2_ET_GDESC_BAD_BLOCK_MAP;
68 ext2fs_mark_block_bitmap2(bmap, blk);
71 * Check to make sure the inode bitmap for group is sane
73 blk = ext2fs_inode_bitmap_loc(fs, i);
74 if (blk < first_block || blk > last_block ||
75 ext2fs_test_block_bitmap2(bmap, blk)) {
76 retval = EXT2_ET_GDESC_BAD_INODE_MAP;
79 ext2fs_mark_block_bitmap2(bmap, blk);
82 * Check to make sure the inode table for group is sane
84 blk = ext2fs_inode_table_loc(fs, i);
85 if (blk < first_block ||
86 ((blk + fs->inode_blocks_per_group - 1) > last_block)) {
87 retval = EXT2_ET_GDESC_BAD_INODE_TABLE;
90 for (j = 0, b = blk; j < fs->inode_blocks_per_group;
92 if (ext2fs_test_block_bitmap2(bmap, b)) {
93 retval = EXT2_ET_GDESC_BAD_INODE_TABLE;
96 ext2fs_mark_block_bitmap2(bmap, b);
100 ext2fs_free_block_bitmap(bmap);