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