Home | History | Annotate | Download | only in ext2fs
      1 /*
      2  * check_desc.c --- Check the group descriptors of an ext2 filesystem
      3  *
      4  * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
      5  *
      6  * %Begin-Header%
      7  * This file may be redistributed under the terms of the GNU Public
      8  * License.
      9  * %End-Header%
     10  */
     11 
     12 #include <stdio.h>
     13 #include <string.h>
     14 #if HAVE_UNISTD_H
     15 #include <unistd.h>
     16 #endif
     17 #include <fcntl.h>
     18 #include <time.h>
     19 #if HAVE_SYS_STAT_H
     20 #include <sys/stat.h>
     21 #endif
     22 #if HAVE_SYS_TYPES_H
     23 #include <sys/types.h>
     24 #endif
     25 
     26 #include "ext2_fs.h"
     27 #include "ext2fs.h"
     28 
     29 /*
     30  * This routine sanity checks the group descriptors
     31  */
     32 errcode_t ext2fs_check_desc(ext2_filsys fs)
     33 {
     34 	ext2fs_block_bitmap bmap;
     35 	errcode_t retval;
     36 	dgrp_t i;
     37 	blk_t first_block = fs->super->s_first_data_block;
     38 	blk_t last_block = fs->super->s_blocks_count-1;
     39 	blk_t blk, b;
     40 	int j;
     41 
     42 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
     43 
     44 	retval = ext2fs_allocate_block_bitmap(fs, "check_desc map", &bmap);
     45 	if (retval)
     46 		return retval;
     47 
     48 	for (i = 0; i < fs->group_desc_count; i++)
     49 		ext2fs_reserve_super_and_bgd(fs, i, bmap);
     50 
     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 = fs->super->s_blocks_count-1;
     58 		}
     59 
     60 		/*
     61 		 * Check to make sure the block bitmap for group is sane
     62 		 */
     63 		blk = fs->group_desc[i].bg_block_bitmap;
     64 		if (blk < first_block || blk > last_block ||
     65 		    ext2fs_test_block_bitmap(bmap, blk)) {
     66 			retval = EXT2_ET_GDESC_BAD_BLOCK_MAP;
     67 			goto errout;
     68 		}
     69 		ext2fs_mark_block_bitmap(bmap, blk);
     70 
     71 		/*
     72 		 * Check to make sure the inode bitmap for group is sane
     73 		 */
     74 		blk = fs->group_desc[i].bg_inode_bitmap;
     75 		if (blk < first_block || blk > last_block ||
     76 		    ext2fs_test_block_bitmap(bmap, blk)) {
     77 			retval = EXT2_ET_GDESC_BAD_INODE_MAP;
     78 			goto errout;
     79 		}
     80 		ext2fs_mark_block_bitmap(bmap, blk);
     81 
     82 		/*
     83 		 * Check to make sure the inode table for group is sane
     84 		 */
     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;
     89 			goto errout;
     90 		}
     91 		for (j = 0, b = blk; j < fs->inode_blocks_per_group;
     92 		     j++, b++) {
     93 			if (ext2fs_test_block_bitmap(bmap, b)) {
     94 				retval = EXT2_ET_GDESC_BAD_INODE_TABLE;
     95 				goto errout;
     96 			}
     97 			ext2fs_mark_block_bitmap(bmap, b);
     98 		}
     99 	}
    100 errout:
    101 	ext2fs_free_block_bitmap(bmap);
    102 	return retval;
    103 }
    104