]>
Commit | Line | Data |
---|---|---|
1b6bf175 TT |
1 | /* |
2 | * e2fsck.c - superblock checks | |
3 | * | |
4 | * Copyright (C) 1993, 1994, 1995, 1996, 1997 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% | |
10 | */ | |
11 | ||
12 | #include <stdio.h> | |
13 | #ifdef HAVE_STDLIB_H | |
14 | #include <stdlib.h> | |
15 | #endif | |
16 | #include <string.h> | |
1b6bf175 TT |
17 | #include <time.h> |
18 | #ifdef HAVE_GETOPT_H | |
19 | #include <getopt.h> | |
20 | #endif | |
21 | #include <unistd.h> | |
22 | #ifdef HAVE_ERRNO_H | |
23 | #include <errno.h> | |
24 | #endif | |
25 | #ifdef HAVE_MNTENT_H | |
26 | #include <mntent.h> | |
27 | #endif | |
1b6bf175 TT |
28 | |
29 | #include "uuid/uuid.h" | |
30 | #include "e2fsck.h" | |
31 | #include "problem.h" | |
32 | #include "../version.h" | |
33 | ||
34 | #define MIN_CHECK 1 | |
35 | #define MAX_CHECK 2 | |
36 | ||
37 | static void check_super_value(e2fsck_t ctx, const char *descr, | |
38 | unsigned long value, int flags, | |
39 | unsigned long min, unsigned long max) | |
40 | { | |
41 | struct problem_context pctx; | |
42 | ||
43 | if (((flags & MIN_CHECK) && (value < min)) || | |
44 | ((flags & MAX_CHECK) && (value > max))) { | |
45 | clear_problem_context(&pctx); | |
46 | pctx.num = value; | |
47 | pctx.str = descr; | |
48 | fix_problem(ctx, PR_0_MISC_CORRUPT_SUPER, &pctx); | |
08b21301 | 49 | ctx->flags |= E2F_FLAG_ABORT; /* never get here! */ |
1b6bf175 TT |
50 | } |
51 | } | |
52 | ||
53 | void check_super_block(e2fsck_t ctx) | |
54 | { | |
55 | ext2_filsys fs = ctx->fs; | |
56 | blk_t first_block, last_block; | |
57 | struct ext2fs_sb *s = (struct ext2fs_sb *) fs->super; | |
58 | blk_t blocks_per_group = fs->super->s_blocks_per_group; | |
59 | int i; | |
60 | blk_t should_be; | |
61 | struct problem_context pctx; | |
62 | ||
54dc7ca2 | 63 | ctx->invalid_inode_bitmap_flag = (int *) e2fsck_allocate_memory(ctx, |
f8188fff | 64 | sizeof(int) * fs->group_desc_count, "invalid_inode_bitmap"); |
54dc7ca2 | 65 | ctx->invalid_block_bitmap_flag = (int *) e2fsck_allocate_memory(ctx, |
f8188fff | 66 | sizeof(int) * fs->group_desc_count, "invalid_block_bitmap"); |
54dc7ca2 | 67 | ctx->invalid_inode_table_flag = (int *) e2fsck_allocate_memory(ctx, |
f8188fff | 68 | sizeof(int) * fs->group_desc_count, "invalid_inode_table"); |
1b6bf175 TT |
69 | |
70 | clear_problem_context(&pctx); | |
71 | ||
72 | /* | |
73 | * Verify the super block constants... | |
74 | */ | |
75 | check_super_value(ctx, "inodes_count", s->s_inodes_count, | |
76 | MIN_CHECK, 1, 0); | |
77 | check_super_value(ctx, "blocks_count", s->s_blocks_count, | |
78 | MIN_CHECK, 1, 0); | |
79 | check_super_value(ctx, "first_data_block", s->s_first_data_block, | |
80 | MAX_CHECK, 0, s->s_blocks_count); | |
81 | check_super_value(ctx, "log_frag_size", s->s_log_frag_size, | |
82 | MAX_CHECK, 0, 2); | |
83 | check_super_value(ctx, "log_block_size", s->s_log_block_size, | |
84 | MIN_CHECK | MAX_CHECK, s->s_log_frag_size, | |
85 | 2); | |
86 | check_super_value(ctx, "frags_per_group", s->s_frags_per_group, | |
87 | MIN_CHECK | MAX_CHECK, 1, 8 * EXT2_BLOCK_SIZE(s)); | |
88 | check_super_value(ctx, "blocks_per_group", s->s_blocks_per_group, | |
89 | MIN_CHECK | MAX_CHECK, 1, 8 * EXT2_BLOCK_SIZE(s)); | |
90 | check_super_value(ctx, "inodes_per_group", s->s_inodes_per_group, | |
91 | MIN_CHECK, 1, 0); | |
92 | check_super_value(ctx, "r_blocks_count", s->s_r_blocks_count, | |
93 | MAX_CHECK, 0, s->s_blocks_count); | |
94 | ||
f8188fff TT |
95 | if (!ctx->num_blocks) { |
96 | pctx.errcode = ext2fs_get_device_size(ctx->filesystem_name, | |
97 | EXT2_BLOCK_SIZE(s), &ctx->num_blocks); | |
98 | if (pctx.errcode && pctx.errcode != EXT2_ET_UNIMPLEMENTED) { | |
99 | fix_problem(ctx, PR_0_GETSIZE_ERROR, &pctx); | |
08b21301 TT |
100 | ctx->flags |= E2F_FLAG_ABORT; |
101 | return; | |
102 | } | |
f8188fff TT |
103 | if ((pctx.errcode != EXT2_ET_UNIMPLEMENTED) && |
104 | (ctx->num_blocks < s->s_blocks_count)) { | |
105 | pctx.blk = s->s_blocks_count; | |
106 | pctx.blk2 = ctx->num_blocks; | |
107 | if (fix_problem(ctx, PR_0_FS_SIZE_WRONG, &pctx)) { | |
108 | ctx->flags |= E2F_FLAG_ABORT; | |
109 | return; | |
110 | } | |
111 | } | |
1b6bf175 TT |
112 | } |
113 | ||
114 | if (s->s_log_block_size != s->s_log_frag_size) { | |
115 | pctx.blk = EXT2_BLOCK_SIZE(s); | |
116 | pctx.blk2 = EXT2_FRAG_SIZE(s); | |
117 | fix_problem(ctx, PR_0_NO_FRAGMENTS, &pctx); | |
08b21301 TT |
118 | ctx->flags |= E2F_FLAG_ABORT; |
119 | return; | |
1b6bf175 TT |
120 | } |
121 | ||
122 | should_be = s->s_frags_per_group / | |
123 | (s->s_log_block_size - s->s_log_frag_size + 1); | |
124 | if (s->s_blocks_per_group != should_be) { | |
125 | pctx.blk = s->s_blocks_per_group; | |
126 | pctx.blk2 = should_be; | |
127 | fix_problem(ctx, PR_0_BLOCKS_PER_GROUP, &pctx); | |
08b21301 TT |
128 | ctx->flags |= E2F_FLAG_ABORT; |
129 | return; | |
1b6bf175 TT |
130 | } |
131 | ||
132 | should_be = (s->s_log_block_size == 0) ? 1 : 0; | |
133 | if (s->s_first_data_block != should_be) { | |
134 | pctx.blk = s->s_first_data_block; | |
135 | pctx.blk2 = should_be; | |
136 | fix_problem(ctx, PR_0_FIRST_DATA_BLOCK, &pctx); | |
08b21301 TT |
137 | ctx->flags |= E2F_FLAG_ABORT; |
138 | return; | |
1b6bf175 TT |
139 | } |
140 | ||
141 | /* | |
142 | * Verify the group descriptors.... | |
143 | */ | |
144 | first_block = fs->super->s_first_data_block; | |
145 | last_block = first_block + blocks_per_group; | |
146 | ||
147 | for (i = 0; i < fs->group_desc_count; i++) { | |
148 | pctx.group = i; | |
149 | ||
150 | if (i == fs->group_desc_count - 1) | |
151 | last_block = fs->super->s_blocks_count; | |
152 | if ((fs->group_desc[i].bg_block_bitmap < first_block) || | |
153 | (fs->group_desc[i].bg_block_bitmap >= last_block)) { | |
154 | pctx.blk = fs->group_desc[i].bg_block_bitmap; | |
155 | if (fix_problem(ctx, PR_0_BB_NOT_GROUP, &pctx)) { | |
156 | fs->group_desc[i].bg_block_bitmap = 0; | |
157 | ctx->invalid_block_bitmap_flag[i]++; | |
158 | ctx->invalid_bitmaps++; | |
159 | } | |
160 | } | |
161 | if ((fs->group_desc[i].bg_inode_bitmap < first_block) || | |
162 | (fs->group_desc[i].bg_inode_bitmap >= last_block)) { | |
163 | pctx.blk = fs->group_desc[i].bg_inode_bitmap; | |
164 | if (fix_problem(ctx, PR_0_IB_NOT_GROUP, &pctx)) { | |
165 | fs->group_desc[i].bg_inode_bitmap = 0; | |
166 | ctx->invalid_inode_bitmap_flag[i]++; | |
167 | ctx->invalid_bitmaps++; | |
168 | } | |
169 | } | |
170 | if ((fs->group_desc[i].bg_inode_table < first_block) || | |
171 | ((fs->group_desc[i].bg_inode_table + | |
172 | fs->inode_blocks_per_group - 1) >= last_block)) { | |
173 | pctx.blk = fs->group_desc[i].bg_inode_table; | |
174 | if (fix_problem(ctx, PR_0_ITABLE_NOT_GROUP, &pctx)) { | |
175 | fs->group_desc[i].bg_inode_table = 0; | |
176 | ctx->invalid_inode_table_flag[i]++; | |
177 | ctx->invalid_bitmaps++; | |
178 | } | |
179 | } | |
180 | first_block += fs->super->s_blocks_per_group; | |
181 | last_block += fs->super->s_blocks_per_group; | |
182 | } | |
183 | /* | |
184 | * If we have invalid bitmaps, set the error state of the | |
185 | * filesystem. | |
186 | */ | |
187 | if (ctx->invalid_bitmaps && !(ctx->options & E2F_OPT_READONLY)) { | |
188 | fs->super->s_state &= ~EXT2_VALID_FS; | |
189 | ext2fs_mark_super_dirty(fs); | |
190 | } | |
191 | ||
192 | /* | |
193 | * If the UUID field isn't assigned, assign it. | |
194 | */ | |
195 | if (!(ctx->options & E2F_OPT_READONLY) && uuid_is_null(s->s_uuid)) { | |
196 | clear_problem_context(&pctx); | |
197 | if (fix_problem(ctx, PR_0_ADD_UUID, &pctx)) { | |
198 | uuid_generate(s->s_uuid); | |
199 | ext2fs_mark_super_dirty(fs); | |
200 | } | |
201 | } | |
202 | return; | |
203 | } | |
204 |