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