check_desc.c revision f314bb0821473efcecedc450c18211e781d30a72
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 Library
8 * General Public License, version 2.
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 */
32errcode_t ext2fs_check_desc(ext2_filsys fs)
33{
34	ext2fs_block_bitmap bmap;
35	errcode_t retval;
36	dgrp_t i;
37	blk64_t first_block = fs->super->s_first_data_block;
38	blk64_t last_block = ext2fs_blocks_count(fs->super)-1;
39	blk64_t blk, b;
40	unsigned int j;
41
42	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
43
44	retval = ext2fs_allocate_subcluster_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_block2(fs, i);
55			last_block = ext2fs_group_last_block2(fs, i);
56		}
57
58		/*
59		 * Check to make sure the block bitmap for group is sane
60		 */
61		blk = ext2fs_block_bitmap_loc(fs, i);
62		if (blk < first_block || blk > last_block ||
63		    ext2fs_test_block_bitmap2(bmap, blk)) {
64			retval = EXT2_ET_GDESC_BAD_BLOCK_MAP;
65			goto errout;
66		}
67		ext2fs_mark_block_bitmap2(bmap, blk);
68
69		/*
70		 * Check to make sure the inode bitmap for group is sane
71		 */
72		blk = ext2fs_inode_bitmap_loc(fs, i);
73		if (blk < first_block || blk > last_block ||
74		    ext2fs_test_block_bitmap2(bmap, blk)) {
75			retval = EXT2_ET_GDESC_BAD_INODE_MAP;
76			goto errout;
77		}
78		ext2fs_mark_block_bitmap2(bmap, blk);
79
80		/*
81		 * Check to make sure the inode table for group is sane
82		 */
83		blk = ext2fs_inode_table_loc(fs, i);
84		if (blk < first_block ||
85		    ((blk + fs->inode_blocks_per_group - 1) > last_block)) {
86			retval = EXT2_ET_GDESC_BAD_INODE_TABLE;
87			goto errout;
88		}
89		for (j = 0, b = blk; j < fs->inode_blocks_per_group;
90		     j++, b++) {
91			if (ext2fs_test_block_bitmap2(bmap, b)) {
92				retval = EXT2_ET_GDESC_BAD_INODE_TABLE;
93				goto errout;
94			}
95			ext2fs_mark_block_bitmap2(bmap, b);
96		}
97	}
98errout:
99	ext2fs_free_block_bitmap(bmap);
100	return retval;
101}
102