]>
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 | ||
1b6bf175 TT |
12 | #ifdef HAVE_ERRNO_H |
13 | #include <errno.h> | |
14 | #endif | |
1b6bf175 | 15 | |
54be2ccc | 16 | #ifndef EXT2_SKIP_UUID |
1b6bf175 | 17 | #include "uuid/uuid.h" |
54be2ccc | 18 | #endif |
1b6bf175 TT |
19 | #include "e2fsck.h" |
20 | #include "problem.h" | |
1b6bf175 TT |
21 | |
22 | #define MIN_CHECK 1 | |
23 | #define MAX_CHECK 2 | |
24 | ||
25 | static void check_super_value(e2fsck_t ctx, const char *descr, | |
26 | unsigned long value, int flags, | |
7f813ba3 | 27 | unsigned long min_val, unsigned long max_val) |
1b6bf175 TT |
28 | { |
29 | struct problem_context pctx; | |
30 | ||
7f813ba3 TT |
31 | if (((flags & MIN_CHECK) && (value < min_val)) || |
32 | ((flags & MAX_CHECK) && (value > max_val))) { | |
1b6bf175 TT |
33 | clear_problem_context(&pctx); |
34 | pctx.num = value; | |
35 | pctx.str = descr; | |
36 | fix_problem(ctx, PR_0_MISC_CORRUPT_SUPER, &pctx); | |
08b21301 | 37 | ctx->flags |= E2F_FLAG_ABORT; /* never get here! */ |
1b6bf175 TT |
38 | } |
39 | } | |
40 | ||
4313932c TT |
41 | /* |
42 | * This routine may get stubbed out in special compilations of the | |
43 | * e2fsck code.. | |
44 | */ | |
45 | #ifndef EXT2_SPECIAL_DEVICE_SIZE | |
46 | errcode_t e2fsck_get_device_size(e2fsck_t ctx) | |
47 | { | |
48 | return (ext2fs_get_device_size(ctx->filesystem_name, | |
49 | EXT2_BLOCK_SIZE(ctx->fs->super), | |
50 | &ctx->num_blocks)); | |
51 | } | |
4313932c TT |
52 | #endif |
53 | ||
80bfaa3e TT |
54 | /* |
55 | * helper function to release an inode | |
56 | */ | |
57 | struct process_block_struct { | |
58 | ino_t ino; | |
59 | e2fsck_t ctx; | |
60 | struct problem_context *pctx; | |
61 | int abort; | |
62 | }; | |
63 | ||
64 | static int release_inode_block(ext2_filsys fs, | |
65 | blk_t *block_nr, | |
66 | int blockcnt, | |
67 | void *priv_data) | |
68 | { | |
69 | struct process_block_struct *pb; | |
70 | e2fsck_t ctx; | |
71 | struct problem_context *pctx; | |
72 | blk_t blk = *block_nr; | |
73 | ||
74 | pb = (struct process_block_struct *) priv_data; | |
75 | ctx = pb->ctx; | |
76 | pctx = pb->pctx; | |
77 | ||
78 | pctx->blk = blk; | |
79 | pctx->blkcount = blockcnt; | |
80 | ||
81 | if (HOLE_BLKADDR(blk)) | |
82 | return 0; | |
83 | ||
84 | if ((blk < fs->super->s_first_data_block) || | |
85 | (blk >= fs->super->s_blocks_count)) { | |
86 | fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_BLOCK_NUM, pctx); | |
87 | pb->abort = 1; | |
88 | return BLOCK_ABORT; | |
89 | } | |
90 | ||
91 | if (!ext2fs_test_block_bitmap(fs->block_map, blk)) { | |
92 | fix_problem(ctx, PR_0_ORPHAN_ALREADY_CLEARED_BLOCK, pctx); | |
93 | pb->abort = 1; | |
94 | return BLOCK_ABORT; | |
95 | } | |
96 | ||
97 | ext2fs_unmark_block_bitmap(fs->block_map, blk); | |
ecf1b776 TT |
98 | fs->group_desc[ext2fs_group_of_blk(fs, blk)].bg_free_blocks_count++; |
99 | fs->super->s_free_blocks_count++; | |
80bfaa3e TT |
100 | |
101 | return 0; | |
102 | } | |
103 | ||
104 | /* | |
105 | * This function releases an inode. Returns 1 if an inconsistency was | |
106 | * found. | |
107 | */ | |
108 | static int release_inode_blocks(e2fsck_t ctx, ino_t ino, char* block_buf, | |
109 | struct problem_context *pctx) | |
110 | { | |
111 | ext2_filsys fs = ctx->fs; | |
112 | errcode_t retval; | |
113 | struct process_block_struct pb; | |
114 | ||
115 | pb.ino = ino; | |
116 | pb.ctx = ctx; | |
117 | pb.abort = 0; | |
118 | pb.pctx = pctx; | |
119 | retval = ext2fs_block_iterate(fs, ino, 0, block_buf, | |
120 | release_inode_block, &pb); | |
121 | if (retval) { | |
122 | com_err("delete_file", retval, | |
123 | _("while calling ext2fs_block_iterate for inode %d"), | |
124 | ino); | |
125 | return 1; | |
126 | } | |
127 | if (pb.abort) | |
128 | return 1; | |
129 | ||
80bfaa3e TT |
130 | ext2fs_mark_bb_dirty(fs); |
131 | return 0; | |
132 | } | |
133 | ||
134 | /* | |
135 | * This function releases all of the orphan inodes. It returns 1 if | |
136 | * it hit some error, and 0 on success. | |
137 | */ | |
138 | static int release_orphan_inodes(e2fsck_t ctx) | |
139 | { | |
140 | ext2_filsys fs = ctx->fs; | |
ecf1b776 | 141 | int group; |
80bfaa3e TT |
142 | ino_t ino, next_ino; |
143 | struct ext2_inode inode; | |
144 | struct problem_context pctx; | |
145 | char *block_buf; | |
146 | ||
147 | if ((ino = fs->super->s_last_orphan) == 0) | |
148 | return 0; | |
149 | ||
25c63ba0 TT |
150 | /* |
151 | * Win or lose, we won't be using the head of the orphan inode | |
152 | * list again. | |
153 | */ | |
154 | fs->super->s_last_orphan = 0; | |
155 | ext2fs_mark_super_dirty(fs); | |
156 | ||
80bfaa3e TT |
157 | if ((ino < EXT2_FIRST_INODE(fs->super)) || |
158 | (ino > fs->super->s_inodes_count)) { | |
159 | clear_problem_context(&pctx); | |
160 | pctx.ino; | |
161 | fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_HEAD_INODE, &pctx); | |
162 | return 1; | |
163 | } | |
164 | ||
165 | block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3, | |
166 | "block interate buffer"); | |
167 | e2fsck_read_bitmaps(ctx); | |
168 | ||
169 | while (ino) { | |
80bfaa3e TT |
170 | e2fsck_read_inode(ctx, ino, &inode, "delete_file"); |
171 | clear_problem_context(&pctx); | |
ecf1b776 | 172 | pctx.ino = ino; |
80bfaa3e TT |
173 | pctx.inode = &inode; |
174 | ||
ecf1b776 TT |
175 | fix_problem(ctx, PR_0_CLEAR_ORPHAN_INODE, &pctx); |
176 | ||
80bfaa3e TT |
177 | if (inode.i_links_count) { |
178 | fix_problem(ctx, PR_0_ORPHAN_INODE_INUSE, &pctx); | |
179 | goto abort; | |
180 | } | |
181 | next_ino = inode.i_dtime; | |
182 | if (next_ino && | |
183 | ((ino < EXT2_FIRST_INODE(fs->super)) || | |
184 | (ino > fs->super->s_inodes_count))) { | |
185 | fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_INODE, &pctx); | |
186 | goto abort; | |
187 | } | |
188 | ||
189 | if (release_inode_blocks(ctx, ino, block_buf, &pctx)) | |
190 | goto abort; | |
191 | ||
192 | inode.i_dtime = time(0); | |
193 | e2fsck_write_inode(ctx, ino, &inode, "delete_file"); | |
ecf1b776 TT |
194 | |
195 | ext2fs_unmark_inode_bitmap(fs->inode_map, ino); | |
196 | ext2fs_mark_ib_dirty(fs); | |
197 | group = ext2fs_group_of_ino(fs, ino); | |
198 | fs->group_desc[group].bg_free_inodes_count++; | |
199 | fs->super->s_free_inodes_count++; | |
200 | if (LINUX_S_ISDIR(inode.i_mode)) | |
201 | fs->group_desc[group].bg_used_dirs_count--; | |
202 | ||
80bfaa3e TT |
203 | ino = next_ino; |
204 | } | |
205 | return 0; | |
206 | abort: | |
207 | ext2fs_free_mem((void **) &block_buf); | |
208 | return 1; | |
209 | } | |
210 | ||
211 | ||
1b6bf175 TT |
212 | void check_super_block(e2fsck_t ctx) |
213 | { | |
214 | ext2_filsys fs = ctx->fs; | |
215 | blk_t first_block, last_block; | |
216 | struct ext2fs_sb *s = (struct ext2fs_sb *) fs->super; | |
217 | blk_t blocks_per_group = fs->super->s_blocks_per_group; | |
78cf0547 | 218 | int inodes_per_block; |
7f813ba3 | 219 | dgrp_t i; |
1b6bf175 TT |
220 | blk_t should_be; |
221 | struct problem_context pctx; | |
78cf0547 TT |
222 | |
223 | inodes_per_block = (EXT2_INODE_SIZE(fs->super) + | |
224 | EXT2_BLOCK_SIZE(fs->super) - 1) / | |
225 | EXT2_BLOCK_SIZE(fs->super); | |
1b6bf175 | 226 | |
54dc7ca2 | 227 | ctx->invalid_inode_bitmap_flag = (int *) e2fsck_allocate_memory(ctx, |
f8188fff | 228 | sizeof(int) * fs->group_desc_count, "invalid_inode_bitmap"); |
54dc7ca2 | 229 | ctx->invalid_block_bitmap_flag = (int *) e2fsck_allocate_memory(ctx, |
f8188fff | 230 | sizeof(int) * fs->group_desc_count, "invalid_block_bitmap"); |
54dc7ca2 | 231 | ctx->invalid_inode_table_flag = (int *) e2fsck_allocate_memory(ctx, |
f8188fff | 232 | sizeof(int) * fs->group_desc_count, "invalid_inode_table"); |
1b6bf175 TT |
233 | |
234 | clear_problem_context(&pctx); | |
235 | ||
236 | /* | |
237 | * Verify the super block constants... | |
238 | */ | |
239 | check_super_value(ctx, "inodes_count", s->s_inodes_count, | |
240 | MIN_CHECK, 1, 0); | |
241 | check_super_value(ctx, "blocks_count", s->s_blocks_count, | |
242 | MIN_CHECK, 1, 0); | |
243 | check_super_value(ctx, "first_data_block", s->s_first_data_block, | |
244 | MAX_CHECK, 0, s->s_blocks_count); | |
245 | check_super_value(ctx, "log_frag_size", s->s_log_frag_size, | |
246 | MAX_CHECK, 0, 2); | |
247 | check_super_value(ctx, "log_block_size", s->s_log_block_size, | |
248 | MIN_CHECK | MAX_CHECK, s->s_log_frag_size, | |
249 | 2); | |
250 | check_super_value(ctx, "frags_per_group", s->s_frags_per_group, | |
251 | MIN_CHECK | MAX_CHECK, 1, 8 * EXT2_BLOCK_SIZE(s)); | |
252 | check_super_value(ctx, "blocks_per_group", s->s_blocks_per_group, | |
253 | MIN_CHECK | MAX_CHECK, 1, 8 * EXT2_BLOCK_SIZE(s)); | |
254 | check_super_value(ctx, "inodes_per_group", s->s_inodes_per_group, | |
78cf0547 TT |
255 | MIN_CHECK | MAX_CHECK, 1, |
256 | inodes_per_block * blocks_per_group); | |
1b6bf175 TT |
257 | check_super_value(ctx, "r_blocks_count", s->s_r_blocks_count, |
258 | MAX_CHECK, 0, s->s_blocks_count); | |
259 | ||
f8188fff | 260 | if (!ctx->num_blocks) { |
4313932c | 261 | pctx.errcode = e2fsck_get_device_size(ctx); |
f8188fff TT |
262 | if (pctx.errcode && pctx.errcode != EXT2_ET_UNIMPLEMENTED) { |
263 | fix_problem(ctx, PR_0_GETSIZE_ERROR, &pctx); | |
08b21301 TT |
264 | ctx->flags |= E2F_FLAG_ABORT; |
265 | return; | |
266 | } | |
f8188fff TT |
267 | if ((pctx.errcode != EXT2_ET_UNIMPLEMENTED) && |
268 | (ctx->num_blocks < s->s_blocks_count)) { | |
269 | pctx.blk = s->s_blocks_count; | |
270 | pctx.blk2 = ctx->num_blocks; | |
271 | if (fix_problem(ctx, PR_0_FS_SIZE_WRONG, &pctx)) { | |
272 | ctx->flags |= E2F_FLAG_ABORT; | |
273 | return; | |
274 | } | |
275 | } | |
1b6bf175 TT |
276 | } |
277 | ||
278 | if (s->s_log_block_size != s->s_log_frag_size) { | |
279 | pctx.blk = EXT2_BLOCK_SIZE(s); | |
280 | pctx.blk2 = EXT2_FRAG_SIZE(s); | |
281 | fix_problem(ctx, PR_0_NO_FRAGMENTS, &pctx); | |
08b21301 TT |
282 | ctx->flags |= E2F_FLAG_ABORT; |
283 | return; | |
1b6bf175 TT |
284 | } |
285 | ||
17dba281 TT |
286 | should_be = s->s_frags_per_group >> |
287 | (s->s_log_block_size - s->s_log_frag_size); | |
1b6bf175 TT |
288 | if (s->s_blocks_per_group != should_be) { |
289 | pctx.blk = s->s_blocks_per_group; | |
290 | pctx.blk2 = should_be; | |
291 | fix_problem(ctx, PR_0_BLOCKS_PER_GROUP, &pctx); | |
08b21301 TT |
292 | ctx->flags |= E2F_FLAG_ABORT; |
293 | return; | |
1b6bf175 TT |
294 | } |
295 | ||
296 | should_be = (s->s_log_block_size == 0) ? 1 : 0; | |
297 | if (s->s_first_data_block != should_be) { | |
298 | pctx.blk = s->s_first_data_block; | |
299 | pctx.blk2 = should_be; | |
300 | fix_problem(ctx, PR_0_FIRST_DATA_BLOCK, &pctx); | |
08b21301 TT |
301 | ctx->flags |= E2F_FLAG_ABORT; |
302 | return; | |
1b6bf175 TT |
303 | } |
304 | ||
d4b0ce03 TT |
305 | should_be = s->s_inodes_per_group * fs->group_desc_count; |
306 | if (s->s_inodes_count != should_be) { | |
307 | pctx.ino = s->s_inodes_count; | |
308 | pctx.ino2 = should_be; | |
309 | if (fix_problem(ctx, PR_0_INODE_COUNT_WRONG, &pctx)) { | |
310 | s->s_inodes_count = should_be; | |
311 | ext2fs_mark_super_dirty(fs); | |
312 | } | |
313 | } | |
314 | ||
1b6bf175 TT |
315 | /* |
316 | * Verify the group descriptors.... | |
317 | */ | |
318 | first_block = fs->super->s_first_data_block; | |
319 | last_block = first_block + blocks_per_group; | |
320 | ||
321 | for (i = 0; i < fs->group_desc_count; i++) { | |
322 | pctx.group = i; | |
323 | ||
324 | if (i == fs->group_desc_count - 1) | |
325 | last_block = fs->super->s_blocks_count; | |
326 | if ((fs->group_desc[i].bg_block_bitmap < first_block) || | |
327 | (fs->group_desc[i].bg_block_bitmap >= last_block)) { | |
328 | pctx.blk = fs->group_desc[i].bg_block_bitmap; | |
329 | if (fix_problem(ctx, PR_0_BB_NOT_GROUP, &pctx)) { | |
330 | fs->group_desc[i].bg_block_bitmap = 0; | |
331 | ctx->invalid_block_bitmap_flag[i]++; | |
332 | ctx->invalid_bitmaps++; | |
333 | } | |
334 | } | |
335 | if ((fs->group_desc[i].bg_inode_bitmap < first_block) || | |
336 | (fs->group_desc[i].bg_inode_bitmap >= last_block)) { | |
337 | pctx.blk = fs->group_desc[i].bg_inode_bitmap; | |
338 | if (fix_problem(ctx, PR_0_IB_NOT_GROUP, &pctx)) { | |
339 | fs->group_desc[i].bg_inode_bitmap = 0; | |
340 | ctx->invalid_inode_bitmap_flag[i]++; | |
341 | ctx->invalid_bitmaps++; | |
342 | } | |
343 | } | |
344 | if ((fs->group_desc[i].bg_inode_table < first_block) || | |
345 | ((fs->group_desc[i].bg_inode_table + | |
346 | fs->inode_blocks_per_group - 1) >= last_block)) { | |
347 | pctx.blk = fs->group_desc[i].bg_inode_table; | |
348 | if (fix_problem(ctx, PR_0_ITABLE_NOT_GROUP, &pctx)) { | |
349 | fs->group_desc[i].bg_inode_table = 0; | |
350 | ctx->invalid_inode_table_flag[i]++; | |
351 | ctx->invalid_bitmaps++; | |
352 | } | |
353 | } | |
354 | first_block += fs->super->s_blocks_per_group; | |
355 | last_block += fs->super->s_blocks_per_group; | |
356 | } | |
357 | /* | |
358 | * If we have invalid bitmaps, set the error state of the | |
359 | * filesystem. | |
360 | */ | |
361 | if (ctx->invalid_bitmaps && !(ctx->options & E2F_OPT_READONLY)) { | |
362 | fs->super->s_state &= ~EXT2_VALID_FS; | |
363 | ext2fs_mark_super_dirty(fs); | |
364 | } | |
365 | ||
4ea0a110 TT |
366 | clear_problem_context(&pctx); |
367 | ||
54be2ccc | 368 | #ifndef EXT2_SKIP_UUID |
1b6bf175 TT |
369 | /* |
370 | * If the UUID field isn't assigned, assign it. | |
371 | */ | |
372 | if (!(ctx->options & E2F_OPT_READONLY) && uuid_is_null(s->s_uuid)) { | |
1b6bf175 TT |
373 | if (fix_problem(ctx, PR_0_ADD_UUID, &pctx)) { |
374 | uuid_generate(s->s_uuid); | |
375 | ext2fs_mark_super_dirty(fs); | |
376 | } | |
377 | } | |
54be2ccc | 378 | #endif |
4ea0a110 TT |
379 | |
380 | /* | |
381 | * For the Hurd, check to see if the filetype option is set, | |
382 | * since it doesn't support it. | |
383 | */ | |
80bfaa3e TT |
384 | if (!(ctx->options & E2F_OPT_READONLY) && |
385 | fs->super->s_creator_os == EXT2_OS_HURD && | |
4ea0a110 TT |
386 | (fs->super->s_feature_incompat & |
387 | EXT2_FEATURE_INCOMPAT_FILETYPE)) { | |
388 | if (fix_problem(ctx, PR_0_HURD_CLEAR_FILETYPE, &pctx)) { | |
389 | fs->super->s_feature_incompat &= | |
390 | ~EXT2_FEATURE_INCOMPAT_FILETYPE; | |
391 | ext2fs_mark_super_dirty(fs); | |
392 | ||
393 | } | |
394 | } | |
80bfaa3e TT |
395 | |
396 | /* | |
397 | * Clean up any orphan inodes, if present. | |
398 | */ | |
399 | if (!(ctx->options & E2F_OPT_READONLY) && release_orphan_inodes(ctx)) { | |
400 | fs->super->s_state &= ~EXT2_VALID_FS; | |
401 | ext2fs_mark_super_dirty(ctx->fs); | |
402 | } | |
403 | ||
1b6bf175 TT |
404 | return; |
405 | } | |
80bfaa3e TT |
406 | |
407 |