blob: 74b3e2d4068d03213eb02308548c5473d893c6b0 [file] [log] [blame]
Theodore Ts'of3db3561997-04-26 13:34:30 +00001/*
2 * check_desc.c --- Check the group descriptors of an ext2 filesystem
3 *
Theodore Ts'o21c84b71997-04-29 16:15:03 +00004 * 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%
Theodore Ts'of3db3561997-04-26 13:34:30 +000010 */
11
12#include <stdio.h>
13#include <string.h>
14#include <unistd.h>
15#include <stdlib.h>
16#include <fcntl.h>
17#include <time.h>
18#include <sys/stat.h>
19#include <sys/types.h>
20
21#include <linux/ext2_fs.h>
22
23#include "ext2fs.h"
24
25/*
26 * This routine sanity checks the group descriptors
27 */
28errcode_t ext2fs_check_desc(ext2_filsys fs)
29{
30 int i;
31 int block = fs->super->s_first_data_block;
Theodore Ts'o7f88b041997-04-26 14:48:50 +000032 int next;
Theodore Ts'of3db3561997-04-26 13:34:30 +000033
34 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
35
Theodore Ts'of3db3561997-04-26 13:34:30 +000036 for (i = 0; i < fs->group_desc_count; i++) {
37 next = block + fs->super->s_blocks_per_group;
38 /*
39 * Check to make sure block bitmap for group is
40 * located within the group.
41 */
42 if (fs->group_desc[i].bg_block_bitmap < block ||
43 fs->group_desc[i].bg_block_bitmap >= next)
44 return EXT2_ET_GDESC_BAD_BLOCK_MAP;
45 /*
46 * Check to make sure inode bitmap for group is
47 * located within the group
48 */
49 if (fs->group_desc[i].bg_inode_bitmap < block ||
50 fs->group_desc[i].bg_inode_bitmap >= next)
51 return EXT2_ET_GDESC_BAD_INODE_MAP;
52 /*
53 * Check to make sure inode table for group is located
54 * within the group
55 */
56 if (fs->group_desc[i].bg_inode_table < block ||
Theodore Ts'o7f88b041997-04-26 14:48:50 +000057 ((fs->group_desc[i].bg_inode_table +
58 fs->inode_blocks_per_group) >= next))
Theodore Ts'of3db3561997-04-26 13:34:30 +000059 return EXT2_ET_GDESC_BAD_INODE_TABLE;
60
61 block = next;
62 }
63 return 0;
64}