| Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * check_desc.c --- Check the group descriptors of an ext2 filesystem |
| 3 | * |
| 4 | * Copyright (C) 1993, 1994 Theodore Ts'o. This file may be redistributed |
| 5 | * under the terms of the GNU Public License. |
| 6 | */ |
| 7 | |
| 8 | #include <stdio.h> |
| 9 | #include <string.h> |
| 10 | #include <unistd.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <fcntl.h> |
| 13 | #include <time.h> |
| 14 | #include <sys/stat.h> |
| 15 | #include <sys/types.h> |
| 16 | |
| 17 | #include <linux/ext2_fs.h> |
| 18 | |
| 19 | #include "ext2fs.h" |
| 20 | |
| 21 | /* |
| 22 | * This routine sanity checks the group descriptors |
| 23 | */ |
| 24 | errcode_t ext2fs_check_desc(ext2_filsys fs) |
| 25 | { |
| 26 | int i; |
| 27 | int block = fs->super->s_first_data_block; |
| 28 | int next, inode_blocks_per_group; |
| 29 | |
| 30 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); |
| 31 | |
| 32 | inode_blocks_per_group = fs->super->s_inodes_per_group / |
| 33 | EXT2_INODES_PER_BLOCK (fs->super); |
| 34 | |
| 35 | for (i = 0; i < fs->group_desc_count; i++) { |
| 36 | next = block + fs->super->s_blocks_per_group; |
| 37 | /* |
| 38 | * Check to make sure block bitmap for group is |
| 39 | * located within the group. |
| 40 | */ |
| 41 | if (fs->group_desc[i].bg_block_bitmap < block || |
| 42 | fs->group_desc[i].bg_block_bitmap >= next) |
| 43 | return EXT2_ET_GDESC_BAD_BLOCK_MAP; |
| 44 | /* |
| 45 | * Check to make sure inode bitmap for group is |
| 46 | * located within the group |
| 47 | */ |
| 48 | if (fs->group_desc[i].bg_inode_bitmap < block || |
| 49 | fs->group_desc[i].bg_inode_bitmap >= next) |
| 50 | return EXT2_ET_GDESC_BAD_INODE_MAP; |
| 51 | /* |
| 52 | * Check to make sure inode table for group is located |
| 53 | * within the group |
| 54 | */ |
| 55 | if (fs->group_desc[i].bg_inode_table < block || |
| 56 | fs->group_desc[i].bg_inode_table+inode_blocks_per_group >= |
| 57 | next) |
| 58 | return EXT2_ET_GDESC_BAD_INODE_TABLE; |
| 59 | |
| 60 | block = next; |
| 61 | } |
| 62 | return 0; |
| 63 | } |