Initial public busybox upstream commit
[busybox4maemo] / e2fsprogs / old_e2fsprogs / ext2fs / check_desc.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * check_desc.c --- Check the group descriptors of an ext2 filesystem
4  *
5  * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
6  *
7  * %Begin-Header%
8  * This file may be redistributed under the terms of the GNU Public
9  * License.
10  * %End-Header%
11  */
12
13 #include <stdio.h>
14 #include <string.h>
15 #if HAVE_UNISTD_H
16 #include <unistd.h>
17 #endif
18 #include <fcntl.h>
19 #include <time.h>
20 #if HAVE_SYS_STAT_H
21 #include <sys/stat.h>
22 #endif
23 #if HAVE_SYS_TYPES_H
24 #include <sys/types.h>
25 #endif
26
27 #include "ext2_fs.h"
28 #include "ext2fs.h"
29
30 /*
31  * This routine sanity checks the group descriptors
32  */
33 errcode_t ext2fs_check_desc(ext2_filsys fs)
34 {
35         dgrp_t i;
36         blk_t block = fs->super->s_first_data_block;
37         blk_t next;
38
39         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
40
41         for (i = 0; i < fs->group_desc_count; i++) {
42                 next = block + fs->super->s_blocks_per_group;
43                 /*
44                  * Check to make sure block bitmap for group is
45                  * located within the group.
46                  */
47                 if (fs->group_desc[i].bg_block_bitmap < block ||
48                     fs->group_desc[i].bg_block_bitmap >= next)
49                         return EXT2_ET_GDESC_BAD_BLOCK_MAP;
50                 /*
51                  * Check to make sure inode bitmap for group is
52                  * located within the group
53                  */
54                 if (fs->group_desc[i].bg_inode_bitmap < block ||
55                     fs->group_desc[i].bg_inode_bitmap >= next)
56                         return EXT2_ET_GDESC_BAD_INODE_MAP;
57                 /*
58                  * Check to make sure inode table for group is located
59                  * within the group
60                  */
61                 if (fs->group_desc[i].bg_inode_table < block ||
62                     ((fs->group_desc[i].bg_inode_table +
63                       fs->inode_blocks_per_group) >= next))
64                         return EXT2_ET_GDESC_BAD_INODE_TABLE;
65
66                 block = next;
67         }
68         return 0;
69 }