]>
Commit | Line | Data |
---|---|---|
f3db3566 TT |
1 | /* |
2 | * check_desc.c --- Check the group descriptors of an ext2 filesystem | |
efc6f628 | 3 | * |
21c84b71 TT |
4 | * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o. |
5 | * | |
6 | * %Begin-Header% | |
543547a5 TT |
7 | * This file may be redistributed under the terms of the GNU Library |
8 | * General Public License, version 2. | |
21c84b71 | 9 | * %End-Header% |
f3db3566 TT |
10 | */ |
11 | ||
d1154eb4 | 12 | #include "config.h" |
f3db3566 TT |
13 | #include <stdio.h> |
14 | #include <string.h> | |
4cbe8af4 | 15 | #if HAVE_UNISTD_H |
f3db3566 | 16 | #include <unistd.h> |
4cbe8af4 | 17 | #endif |
f3db3566 TT |
18 | #include <fcntl.h> |
19 | #include <time.h> | |
1d2ff46a | 20 | #if HAVE_SYS_STAT_H |
f3db3566 | 21 | #include <sys/stat.h> |
1d2ff46a TT |
22 | #endif |
23 | #if HAVE_SYS_TYPES_H | |
f3db3566 | 24 | #include <sys/types.h> |
1d2ff46a | 25 | #endif |
f3db3566 | 26 | |
b5abe6fa | 27 | #include "ext2_fs.h" |
f3db3566 TT |
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 | { | |
009c02ba TT |
35 | ext2fs_block_bitmap bmap; |
36 | errcode_t retval; | |
54434927 | 37 | dgrp_t i; |
6d8b37fa VAH |
38 | blk64_t first_block = fs->super->s_first_data_block; |
39 | blk64_t last_block = ext2fs_blocks_count(fs->super)-1; | |
d7cca6b0 | 40 | blk64_t blk, b; |
d32c915a | 41 | unsigned int j; |
f3db3566 TT |
42 | |
43 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); | |
44 | ||
44fe08f1 | 45 | retval = ext2fs_allocate_subcluster_bitmap(fs, "check_desc map", &bmap); |
009c02ba TT |
46 | if (retval) |
47 | return retval; | |
48 | ||
49 | for (i = 0; i < fs->group_desc_count; i++) | |
50 | ext2fs_reserve_super_and_bgd(fs, i, bmap); | |
51 | ||
f3db3566 | 52 | for (i = 0; i < fs->group_desc_count; i++) { |
88b34b1f JS |
53 | if (!EXT2_HAS_INCOMPAT_FEATURE(fs->super, |
54 | EXT4_FEATURE_INCOMPAT_FLEX_BG)) { | |
b49f78fe TT |
55 | first_block = ext2fs_group_first_block2(fs, i); |
56 | last_block = ext2fs_group_last_block2(fs, i); | |
88b34b1f | 57 | } |
abf23439 | 58 | |
f3db3566 | 59 | /* |
009c02ba | 60 | * Check to make sure the block bitmap for group is sane |
f3db3566 | 61 | */ |
d7cca6b0 | 62 | blk = ext2fs_block_bitmap_loc(fs, i); |
009c02ba | 63 | if (blk < first_block || blk > last_block || |
8f82ef98 | 64 | ext2fs_test_block_bitmap2(bmap, blk)) { |
009c02ba TT |
65 | retval = EXT2_ET_GDESC_BAD_BLOCK_MAP; |
66 | goto errout; | |
67 | } | |
8f82ef98 | 68 | ext2fs_mark_block_bitmap2(bmap, blk); |
009c02ba | 69 | |
f3db3566 | 70 | /* |
009c02ba | 71 | * Check to make sure the inode bitmap for group is sane |
f3db3566 | 72 | */ |
d7cca6b0 | 73 | blk = ext2fs_inode_bitmap_loc(fs, i); |
009c02ba | 74 | if (blk < first_block || blk > last_block || |
8f82ef98 | 75 | ext2fs_test_block_bitmap2(bmap, blk)) { |
009c02ba TT |
76 | retval = EXT2_ET_GDESC_BAD_INODE_MAP; |
77 | goto errout; | |
78 | } | |
8f82ef98 | 79 | ext2fs_mark_block_bitmap2(bmap, blk); |
009c02ba | 80 | |
f3db3566 | 81 | /* |
009c02ba | 82 | * Check to make sure the inode table for group is sane |
f3db3566 | 83 | */ |
d7cca6b0 | 84 | blk = ext2fs_inode_table_loc(fs, i); |
009c02ba TT |
85 | if (blk < first_block || |
86 | ((blk + fs->inode_blocks_per_group - 1) > last_block)) { | |
87 | retval = EXT2_ET_GDESC_BAD_INODE_TABLE; | |
88 | goto errout; | |
89 | } | |
efc6f628 | 90 | for (j = 0, b = blk; j < fs->inode_blocks_per_group; |
009c02ba | 91 | j++, b++) { |
8f82ef98 | 92 | if (ext2fs_test_block_bitmap2(bmap, b)) { |
009c02ba TT |
93 | retval = EXT2_ET_GDESC_BAD_INODE_TABLE; |
94 | goto errout; | |
95 | } | |
8f82ef98 | 96 | ext2fs_mark_block_bitmap2(bmap, b); |
009c02ba | 97 | } |
f3db3566 | 98 | } |
009c02ba TT |
99 | errout: |
100 | ext2fs_free_block_bitmap(bmap); | |
101 | return retval; | |
f3db3566 | 102 | } |