check_desc.c revision 9f8046fc6dfc13eee2f5c363214e60b533872cac
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 */
32errcode_t ext2fs_check_desc(ext2_filsys fs)
33{
34	int i;
35	blk_t block = fs->super->s_first_data_block;
36	blk_t next;
37
38	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
39
40	for (i = 0; i < fs->group_desc_count; i++) {
41		next = block + fs->super->s_blocks_per_group;
42		/*
43		 * Check to make sure block bitmap for group is
44		 * located within the group.
45		 */
46		if (fs->group_desc[i].bg_block_bitmap < block ||
47		    fs->group_desc[i].bg_block_bitmap >= next)
48			return EXT2_ET_GDESC_BAD_BLOCK_MAP;
49		/*
50		 * Check to make sure inode bitmap for group is
51		 * located within the group
52		 */
53		if (fs->group_desc[i].bg_inode_bitmap < block ||
54		    fs->group_desc[i].bg_inode_bitmap >= next)
55			return EXT2_ET_GDESC_BAD_INODE_MAP;
56		/*
57		 * Check to make sure inode table for group is located
58		 * within the group
59		 */
60		if (fs->group_desc[i].bg_inode_table < block ||
61		    ((fs->group_desc[i].bg_inode_table +
62		      fs->inode_blocks_per_group) >= next))
63			return EXT2_ET_GDESC_BAD_INODE_TABLE;
64
65		block = next;
66	}
67	return 0;
68}
69