check_desc.c revision bb1a46a430a99f73ddaf7cf74e380dd5cf36382f
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		if (i == fs->group_desc_count - 1)
42			last_block = fs->super->s_blocks_count - 1;
43		else
44			last_block = first_block +
45				     fs->super->s_blocks_per_group - 1;
46		/*
47		 * Check to make sure block bitmap for group is
48		 * located within the group.
49		 */
50		if (fs->group_desc[i].bg_block_bitmap < first_block ||
51		    fs->group_desc[i].bg_block_bitmap > last_block)
52			return EXT2_ET_GDESC_BAD_BLOCK_MAP;
53		/*
54		 * Check to make sure inode bitmap for group is
55		 * located within the group
56		 */
57		if (fs->group_desc[i].bg_inode_bitmap < first_block ||
58		    fs->group_desc[i].bg_inode_bitmap > last_block)
59			return EXT2_ET_GDESC_BAD_INODE_MAP;
60		/*
61		 * Check to make sure inode table for group is located
62		 * within the group
63		 */
64		if (fs->group_desc[i].bg_inode_table < first_block ||
65		    ((fs->group_desc[i].bg_inode_table +
66		      fs->inode_blocks_per_group) > last_block))
67			return EXT2_ET_GDESC_BAD_INODE_TABLE;
68
69		first_block += fs->super->s_blocks_per_group;
70	}
71	return 0;
72}
73