blob: 602753d200a75f46bbbf7837b2eb3f4756a83440 [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>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000014#if HAVE_UNISTD_H
Theodore Ts'of3db3561997-04-26 13:34:30 +000015#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000016#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +000017#include <stdlib.h>
18#include <fcntl.h>
19#include <time.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000020#if HAVE_SYS_STAT_H
Theodore Ts'of3db3561997-04-26 13:34:30 +000021#include <sys/stat.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000022#endif
23#if HAVE_SYS_TYPES_H
Theodore Ts'of3db3561997-04-26 13:34:30 +000024#include <sys/types.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000025#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +000026
27#include <linux/ext2_fs.h>
28
29#include "ext2fs.h"
30
31/*
32 * This routine sanity checks the group descriptors
33 */
34errcode_t ext2fs_check_desc(ext2_filsys fs)
35{
36 int i;
Theodore Ts'o3cb6c501997-08-11 20:29:22 +000037 blk_t block = fs->super->s_first_data_block;
38 blk_t next;
Theodore Ts'of3db3561997-04-26 13:34:30 +000039
40 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
41
Theodore Ts'of3db3561997-04-26 13:34:30 +000042 for (i = 0; i < fs->group_desc_count; i++) {
43 next = block + fs->super->s_blocks_per_group;
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 < block ||
49 fs->group_desc[i].bg_block_bitmap >= next)
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 < block ||
56 fs->group_desc[i].bg_inode_bitmap >= next)
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 < block ||
Theodore Ts'o7f88b041997-04-26 14:48:50 +000063 ((fs->group_desc[i].bg_inode_table +
64 fs->inode_blocks_per_group) >= next))
Theodore Ts'of3db3561997-04-26 13:34:30 +000065 return EXT2_ET_GDESC_BAD_INODE_TABLE;
66
67 block = next;
68 }
69 return 0;
70}