blob: 2a754c7f637b9450f4b5fc1bd3af6de2094fa86b [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 <fcntl.h>
18#include <time.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000019#if HAVE_SYS_STAT_H
Theodore Ts'of3db3561997-04-26 13:34:30 +000020#include <sys/stat.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000021#endif
22#if HAVE_SYS_TYPES_H
Theodore Ts'of3db3561997-04-26 13:34:30 +000023#include <sys/types.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000024#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +000025
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000026#include "ext2_fs.h"
Theodore Ts'of3db3561997-04-26 13:34:30 +000027#include "ext2fs.h"
28
29/*
30 * This routine sanity checks the group descriptors
31 */
32errcode_t ext2fs_check_desc(ext2_filsys fs)
33{
Theodore Ts'o54434922003-12-07 01:28:50 -050034 dgrp_t i;
Theodore Ts'o3cb6c501997-08-11 20:29:22 +000035 blk_t block = fs->super->s_first_data_block;
36 blk_t next;
Theodore Ts'of3db3561997-04-26 13:34:30 +000037
38 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
39
Theodore Ts'of3db3561997-04-26 13:34:30 +000040 for (i = 0; i < fs->group_desc_count; i++) {
41 next = block + fs->super->s_blocks_per_group;
42 /*
43 * Check to make sure block bitmap for group is
44 * located within the group.
45 */
46 if (fs->group_desc[i].bg_block_bitmap < block ||
47 fs->group_desc[i].bg_block_bitmap >= next)
48 return EXT2_ET_GDESC_BAD_BLOCK_MAP;
49 /*
50 * Check to make sure inode bitmap for group is
51 * located within the group
52 */
53 if (fs->group_desc[i].bg_inode_bitmap < block ||
54 fs->group_desc[i].bg_inode_bitmap >= next)
55 return EXT2_ET_GDESC_BAD_INODE_MAP;
56 /*
57 * Check to make sure inode table for group is located
58 * within the group
59 */
60 if (fs->group_desc[i].bg_inode_table < block ||
Theodore Ts'o7f88b041997-04-26 14:48:50 +000061 ((fs->group_desc[i].bg_inode_table +
62 fs->inode_blocks_per_group) >= next))
Theodore Ts'of3db3561997-04-26 13:34:30 +000063 return EXT2_ET_GDESC_BAD_INODE_TABLE;
64
65 block = next;
66 }
67 return 0;
68}