]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - e2fsck/pass5.c
Fix build failures due to missing $(SYSLIBS)
[thirdparty/e2fsprogs.git] / e2fsck / pass5.c
CommitLineData
3839e657
TT
1/*
2 * pass5.c --- check block and inode bitmaps against on-disk bitmaps
efc6f628 3 *
21c84b71
TT
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%
efc6f628 10 *
3839e657
TT
11 */
12
d1154eb4 13#include "config.h"
efa1a355
LC
14#include <stdint.h>
15#include <sys/types.h>
16#include <sys/stat.h>
17#include <sys/ioctl.h>
18#include <fcntl.h>
19#include <errno.h>
20
3839e657 21#include "e2fsck.h"
1b6bf175 22#include "problem.h"
3839e657 23
efa1a355
LC
24#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
25
1b6bf175
TT
26static void check_block_bitmaps(e2fsck_t ctx);
27static void check_inode_bitmaps(e2fsck_t ctx);
28static void check_inode_end(e2fsck_t ctx);
29static void check_block_end(e2fsck_t ctx);
11ac780e 30static void check_inode_bitmap_checksum(e2fsck_t ctx);
da670fe0 31static void check_block_bitmap_checksum(e2fsck_t ctx);
3839e657 32
08b21301 33void e2fsck_pass5(e2fsck_t ctx)
3839e657 34{
8bf191e8 35#ifdef RESOURCE_TRACK
3839e657 36 struct resource_track rtrack;
8bf191e8 37#endif
1b6bf175 38 struct problem_context pctx;
efc6f628 39
3839e657
TT
40#ifdef MTRACE
41 mtrace_print("Pass 5");
42#endif
43
6d96b00d 44 init_resource_track(&rtrack, ctx->fs->io);
1b6bf175 45 clear_problem_context(&pctx);
3839e657 46
1b6bf175
TT
47 if (!(ctx->options & E2F_OPT_PREEN))
48 fix_problem(ctx, PR_5_PASS_HEADER, &pctx);
3839e657 49
f8188fff 50 if (ctx->progress)
efac9a1b 51 if ((ctx->progress)(ctx, 5, 0, ctx->fs->group_desc_count*2))
a02ce9df 52 return;
f8188fff
TT
53
54 e2fsck_read_bitmaps(ctx);
55
1b6bf175 56 check_block_bitmaps(ctx);
a02ce9df 57 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
08b21301 58 return;
1b6bf175 59 check_inode_bitmaps(ctx);
a02ce9df 60 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
08b21301 61 return;
1b6bf175 62 check_inode_end(ctx);
a02ce9df 63 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
08b21301 64 return;
1b6bf175 65 check_block_end(ctx);
a02ce9df 66 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
08b21301 67 return;
3839e657 68
11ac780e 69 check_inode_bitmap_checksum(ctx);
da670fe0 70 check_block_bitmap_checksum(ctx);
11ac780e 71
1b6bf175
TT
72 ext2fs_free_inode_bitmap(ctx->inode_used_map);
73 ctx->inode_used_map = 0;
74 ext2fs_free_inode_bitmap(ctx->inode_dir_map);
75 ctx->inode_dir_map = 0;
76 ext2fs_free_block_bitmap(ctx->block_found_map);
77 ctx->block_found_map = 0;
35c8faaf
DW
78 ext2fs_free_block_bitmap(ctx->block_metadata_map);
79 ctx->block_metadata_map = 0;
1b6bf175 80
9facd076 81 print_resource_track(ctx, _("Pass 5"), &rtrack, ctx->fs->io);
3839e657
TT
82}
83
11ac780e
DW
84static void check_inode_bitmap_checksum(e2fsck_t ctx)
85{
86 struct problem_context pctx;
11ac780e
DW
87 char *buf;
88 dgrp_t i;
89 int nbytes;
90 ext2_ino_t ino_itr;
91 errcode_t retval;
92
93 if (!EXT2_HAS_RO_COMPAT_FEATURE(ctx->fs->super,
94 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
95 return;
96
97 /* If bitmap is dirty from being fixed, checksum will be corrected */
98 if (ext2fs_test_ib_dirty(ctx->fs))
99 return;
100
101 nbytes = (size_t)(EXT2_INODES_PER_GROUP(ctx->fs->super) / 8);
102 retval = ext2fs_get_memalign(ctx->fs->blocksize, ctx->fs->blocksize,
103 &buf);
104 if (retval) {
bbccc6f3 105 com_err(ctx->program_name, 0, "%s",
11ac780e
DW
106 _("check_inode_bitmap_checksum: Memory allocation error"));
107 fatal_error(ctx, 0);
108 }
109
110 clear_problem_context(&pctx);
111 for (i = 0; i < ctx->fs->group_desc_count; i++) {
112 if (ext2fs_bg_flags_test(ctx->fs, i, EXT2_BG_INODE_UNINIT))
113 continue;
114
115 ino_itr = 1 + (i * (nbytes << 3));
11ac780e
DW
116 retval = ext2fs_get_inode_bitmap_range2(ctx->fs->inode_map,
117 ino_itr, nbytes << 3,
118 buf);
119 if (retval)
120 break;
121
122 if (ext2fs_inode_bitmap_csum_verify(ctx->fs, i, buf, nbytes))
123 continue;
124 pctx.group = i;
125 if (!fix_problem(ctx, PR_5_INODE_BITMAP_CSUM_INVALID, &pctx))
126 continue;
127
128 /*
129 * Fixing one checksum will rewrite all of them. The bitmap
130 * will be checked against the one we made during pass1 for
131 * discrepancies, and fixed if need be.
132 */
133 ext2fs_mark_ib_dirty(ctx->fs);
134 break;
135 }
136
137 ext2fs_free_mem(&buf);
138}
139
da670fe0
DW
140static void check_block_bitmap_checksum(e2fsck_t ctx)
141{
142 struct problem_context pctx;
da670fe0
DW
143 char *buf;
144 dgrp_t i;
145 int nbytes;
146 blk64_t blk_itr;
147 errcode_t retval;
148
149 if (!EXT2_HAS_RO_COMPAT_FEATURE(ctx->fs->super,
150 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
151 return;
152
153 /* If bitmap is dirty from being fixed, checksum will be corrected */
154 if (ext2fs_test_bb_dirty(ctx->fs))
155 return;
156
157 nbytes = (size_t)(EXT2_CLUSTERS_PER_GROUP(ctx->fs->super) / 8);
158 retval = ext2fs_get_memalign(ctx->fs->blocksize, ctx->fs->blocksize,
159 &buf);
160 if (retval) {
bbccc6f3 161 com_err(ctx->program_name, 0, "%s",
da670fe0
DW
162 _("check_block_bitmap_checksum: Memory allocation error"));
163 fatal_error(ctx, 0);
164 }
165
166 clear_problem_context(&pctx);
167 for (i = 0; i < ctx->fs->group_desc_count; i++) {
168 if (ext2fs_bg_flags_test(ctx->fs, i, EXT2_BG_BLOCK_UNINIT))
169 continue;
170
171 blk_itr = EXT2FS_B2C(ctx->fs,
172 ctx->fs->super->s_first_data_block) +
2db19bce 173 ((blk64_t) i * (nbytes << 3));
da670fe0
DW
174 retval = ext2fs_get_block_bitmap_range2(ctx->fs->block_map,
175 blk_itr, nbytes << 3,
176 buf);
177 if (retval)
178 break;
179
180 if (ext2fs_block_bitmap_csum_verify(ctx->fs, i, buf, nbytes))
181 continue;
182 pctx.group = i;
183 if (!fix_problem(ctx, PR_5_BLOCK_BITMAP_CSUM_INVALID, &pctx))
184 continue;
185
186 /*
187 * Fixing one checksum will rewrite all of them. The bitmap
188 * will be checked against the one we made during pass1 for
189 * discrepancies, and fixed if need be.
190 */
191 ext2fs_mark_bb_dirty(ctx->fs);
192 break;
193 }
194
195 ext2fs_free_mem(&buf);
196}
197
46795326
LC
198static void e2fsck_discard_blocks(e2fsck_t ctx, blk64_t start,
199 blk64_t count)
efa1a355
LC
200{
201 ext2_filsys fs = ctx->fs;
efa1a355
LC
202
203 /*
204 * If the filesystem has changed it means that there was an corruption
205 * which should be repaired, but in some cases just one e2fsck run is
206 * not enough to fix the problem, hence it is not safe to run discard
207 * in this case.
208 */
46795326 209 if (ext2fs_test_changed(fs))
efa1a355
LC
210 ctx->options &= ~E2F_OPT_DISCARD;
211
f0fe5dae 212 if ((ctx->options & E2F_OPT_DISCARD) &&
efa1a355
LC
213 (io_channel_discard(fs->io, start, count)))
214 ctx->options &= ~E2F_OPT_DISCARD;
215}
216
57581c10
LC
217/*
218 * This will try to discard number 'count' inodes starting at
219 * inode number 'start' within the 'group'. Note that 'start'
220 * is 1-based, it means that we need to adjust it by -1 in this
221 * function to compute right offset in the particular inode table.
222 */
e64e6761
TT
223static void e2fsck_discard_inodes(e2fsck_t ctx, dgrp_t group,
224 ext2_ino_t start, int count)
57581c10
LC
225{
226 ext2_filsys fs = ctx->fs;
227 blk64_t blk, num;
57581c10
LC
228
229 /*
230 * Sanity check for 'start'
231 */
232 if ((start < 1) || (start > EXT2_INODES_PER_GROUP(fs->super))) {
233 printf("PROGRAMMING ERROR: Got start %d outside of group %d!"
234 " Disabling discard\n",
235 start, group);
236 ctx->options &= ~E2F_OPT_DISCARD;
237 }
238
c15386cd
LC
239 /*
240 * Do not attempt to discard if E2F_OPT_DISCARD is not set. And also
241 * skip the discard on this group if discard does not zero data.
242 * The reason is that if the inode table is not zeroed discard would
243 * no help us since we need to zero it anyway, or if the inode table
244 * is zeroed then the read after discard would not be deterministic
245 * anyway and we would not be able to assume that this inode table
246 * was zeroed anymore so we would have to zero it again, which does
247 * not really make sense.
248 */
249 if (!(ctx->options & E2F_OPT_DISCARD) ||
250 !io_channel_discard_zeroes_data(fs->io))
57581c10
LC
251 return;
252
253 /*
254 * Start is inode number within the group which starts
255 * counting from 1, so we need to adjust it.
256 */
257 start -= 1;
258
259 /*
260 * We can discard only blocks containing only unused
261 * inodes in the table.
262 */
263 blk = DIV_ROUND_UP(start,
264 EXT2_INODES_PER_BLOCK(fs->super));
265 count -= (blk * EXT2_INODES_PER_BLOCK(fs->super) - start);
266 blk += ext2fs_inode_table_loc(fs, group);
267 num = count / EXT2_INODES_PER_BLOCK(fs->super);
268
269 if (num > 0)
46795326 270 e2fsck_discard_blocks(ctx, blk, num);
57581c10
LC
271}
272
6dc64392 273#define NO_BLK ((blk64_t) -1)
f122632e 274
3c7c6d73 275static void print_bitmap_problem(e2fsck_t ctx, problem_t problem,
f122632e
TT
276 struct problem_context *pctx)
277{
278 switch (problem) {
279 case PR_5_BLOCK_UNUSED:
280 if (pctx->blk == pctx->blk2)
281 pctx->blk2 = 0;
282 else
283 problem = PR_5_BLOCK_RANGE_UNUSED;
284 break;
285 case PR_5_BLOCK_USED:
286 if (pctx->blk == pctx->blk2)
287 pctx->blk2 = 0;
288 else
289 problem = PR_5_BLOCK_RANGE_USED;
290 break;
291 case PR_5_INODE_UNUSED:
292 if (pctx->ino == pctx->ino2)
293 pctx->ino2 = 0;
294 else
295 problem = PR_5_INODE_RANGE_UNUSED;
296 break;
297 case PR_5_INODE_USED:
298 if (pctx->ino == pctx->ino2)
299 pctx->ino2 = 0;
300 else
301 problem = PR_5_INODE_RANGE_USED;
302 break;
303 }
304 fix_problem(ctx, problem, pctx);
305 pctx->blk = pctx->blk2 = NO_BLK;
306 pctx->ino = pctx->ino2 = 0;
307}
49e2df29 308
3385a254
TT
309/* Just to be more succint */
310#define B2C(x) EXT2FS_B2C(fs, (x))
311#define EQ_CLSTR(x, y) (B2C(x) == B2C(y))
312#define LE_CLSTR(x, y) (B2C(x) <= B2C(y))
313#define GE_CLSTR(x, y) (B2C(x) >= B2C(y))
314
1b6bf175 315static void check_block_bitmaps(e2fsck_t ctx)
3839e657 316{
1b6bf175 317 ext2_filsys fs = ctx->fs;
20f2ccb3 318 blk64_t i;
e64e6761 319 unsigned int *free_array;
3971bfe8 320 dgrp_t g, group = 0;
e64e6761 321 unsigned int blocks = 0;
6dc64392 322 blk64_t free_blocks = 0;
efa1a355 323 blk64_t first_free = ext2fs_blocks_count(fs->super);
e64e6761 324 unsigned int group_free = 0;
3839e657 325 int actual, bitmap;
1b6bf175 326 struct problem_context pctx;
3c7c6d73
TT
327 problem_t problem, save_problem;
328 int fixit, had_problem;
1b6bf175 329 errcode_t retval;
479463aa 330 int redo_flag = 0;
53e3120c
TT
331 char *actual_buf, *bitmap_buf;
332
333 actual_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize,
334 "actual bitmap buffer");
335 bitmap_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize,
336 "bitmap block buffer");
49e2df29 337
1b6bf175 338 clear_problem_context(&pctx);
e64e6761
TT
339 free_array = (unsigned int *) e2fsck_allocate_memory(ctx,
340 fs->group_desc_count * sizeof(unsigned int), "free block count array");
50e1e10f 341
3385a254 342 if ((B2C(fs->super->s_first_data_block) <
c5d2f50d 343 ext2fs_get_block_bitmap_start2(ctx->block_found_map)) ||
3385a254 344 (B2C(ext2fs_blocks_count(fs->super)-1) >
c5d2f50d 345 ext2fs_get_block_bitmap_end2(ctx->block_found_map))) {
1b6bf175 346 pctx.num = 1;
3385a254
TT
347 pctx.blk = B2C(fs->super->s_first_data_block);
348 pctx.blk2 = B2C(ext2fs_blocks_count(fs->super) - 1);
c5d2f50d
VAH
349 pctx.ino = ext2fs_get_block_bitmap_start2(ctx->block_found_map);
350 pctx.ino2 = ext2fs_get_block_bitmap_end2(ctx->block_found_map);
1b6bf175 351 fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx);
08b21301
TT
352
353 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
49e2df29 354 goto errout;
50e1e10f 355 }
49e2df29 356
3385a254 357 if ((B2C(fs->super->s_first_data_block) <
c5d2f50d 358 ext2fs_get_block_bitmap_start2(fs->block_map)) ||
3385a254 359 (B2C(ext2fs_blocks_count(fs->super)-1) >
c5d2f50d 360 ext2fs_get_block_bitmap_end2(fs->block_map))) {
1b6bf175 361 pctx.num = 2;
3385a254
TT
362 pctx.blk = B2C(fs->super->s_first_data_block);
363 pctx.blk2 = B2C(ext2fs_blocks_count(fs->super) - 1);
c5d2f50d
VAH
364 pctx.ino = ext2fs_get_block_bitmap_start2(fs->block_map);
365 pctx.ino2 = ext2fs_get_block_bitmap_end2(fs->block_map);
1b6bf175 366 fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx);
08b21301
TT
367
368 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
49e2df29 369 goto errout;
50e1e10f 370 }
49e2df29 371
63c4969c
TT
372redo_counts:
373 had_problem = 0;
f122632e
TT
374 save_problem = 0;
375 pctx.blk = pctx.blk2 = NO_BLK;
3385a254 376 for (i = B2C(fs->super->s_first_data_block);
4efbac6f 377 i < ext2fs_blocks_count(fs->super);
44fe08f1 378 i += EXT2FS_CLUSTER_RATIO(fs)) {
53e3120c
TT
379 int first_block_in_bg = (B2C(i) -
380 B2C(fs->super->s_first_data_block)) %
381 fs->super->s_clusters_per_group == 0;
382 int n, nbytes = fs->super->s_clusters_per_group / 8;
383
c5d2f50d 384 actual = ext2fs_fast_test_block_bitmap2(ctx->block_found_map, i);
f5fa2007 385
53e3120c
TT
386 /*
387 * Try to optimize pass5 by extracting a bitmap block
388 * as expected from what we have on disk, and then
389 * comparing the two. If they are identical, then
390 * update the free block counts and go on to the next
391 * block group. This is much faster than doing the
392 * individual bit-by-bit comparison. The one downside
393 * is that this doesn't work if we are asking e2fsck
394 * to do a discard operation.
395 */
396 if (!first_block_in_bg ||
397 (group == (int)fs->group_desc_count - 1) ||
398 (ctx->options & E2F_OPT_DISCARD))
399 goto no_optimize;
400
401 retval = ext2fs_get_block_bitmap_range2(ctx->block_found_map,
402 B2C(i), fs->super->s_clusters_per_group,
403 actual_buf);
404 if (retval)
405 goto no_optimize;
a48da274
DW
406 retval = ext2fs_get_block_bitmap_range2(fs->block_map,
407 B2C(i), fs->super->s_clusters_per_group,
408 bitmap_buf);
409 if (retval)
410 goto no_optimize;
53e3120c
TT
411 if (memcmp(actual_buf, bitmap_buf, nbytes) != 0)
412 goto no_optimize;
413 n = ext2fs_bitcount(actual_buf, nbytes);
414 group_free = fs->super->s_clusters_per_group - n;
415 free_blocks += group_free;
c7e29325 416 i += EXT2FS_C2B(fs, fs->super->s_clusters_per_group - 1);
53e3120c
TT
417 goto next_group;
418 no_optimize:
419
a48da274 420 if (redo_flag)
479463aa
KM
421 bitmap = actual;
422 else
c5d2f50d 423 bitmap = ext2fs_fast_test_block_bitmap2(fs->block_map, i);
49e2df29 424
e35ff9b9 425 if (!actual == !bitmap)
3839e657 426 goto do_counts;
f5fa2007 427
3839e657
TT
428 if (!actual && bitmap) {
429 /*
430 * Block not used, but marked in use in the bitmap.
431 */
f122632e 432 problem = PR_5_BLOCK_UNUSED;
3839e657
TT
433 } else {
434 /*
435 * Block used, but not marked in use in the bitmap.
436 */
1b6bf175 437 problem = PR_5_BLOCK_USED;
49a7360b 438
a48da274
DW
439 if (ext2fs_bg_flags_test(fs, group,
440 EXT2_BG_BLOCK_UNINIT)) {
49a7360b
JS
441 struct problem_context pctx2;
442 pctx2.blk = i;
443 pctx2.group = group;
a48da274
DW
444 if (fix_problem(ctx, PR_5_BLOCK_UNINIT,
445 &pctx2))
446 ext2fs_bg_flags_clear(fs, group,
447 EXT2_BG_BLOCK_UNINIT);
49a7360b 448 }
3839e657 449 }
f122632e
TT
450 if (pctx.blk == NO_BLK) {
451 pctx.blk = pctx.blk2 = i;
452 save_problem = problem;
453 } else {
454 if ((problem == save_problem) &&
69beadcf
DW
455 (pctx.blk2 == i - EXT2FS_CLUSTER_RATIO(fs)))
456 pctx.blk2 += EXT2FS_CLUSTER_RATIO(fs);
f122632e
TT
457 else {
458 print_bitmap_problem(ctx, save_problem, &pctx);
459 pctx.blk = pctx.blk2 = i;
460 save_problem = problem;
461 }
462 }
5596defa 463 ctx->flags |= E2F_FLAG_PROG_SUPPRESS;
63c4969c 464 had_problem++;
49e2df29 465
efa1a355
LC
466 /*
467 * If there a problem we should turn off the discard so we
468 * do not compromise the filesystem.
469 */
470 ctx->options &= ~E2F_OPT_DISCARD;
471
3839e657 472 do_counts:
d2c9c42a 473 if (!bitmap) {
3839e657
TT
474 group_free++;
475 free_blocks++;
efa1a355
LC
476 if (first_free > i)
477 first_free = i;
d2c9c42a
TT
478 } else if (i > first_free) {
479 e2fsck_discard_blocks(ctx, first_free,
480 (i - first_free));
efa1a355 481 first_free = ext2fs_blocks_count(fs->super);
3839e657
TT
482 }
483 blocks ++;
44fe08f1
TT
484 if ((blocks == fs->super->s_clusters_per_group) ||
485 (EXT2FS_B2C(fs, i) ==
486 EXT2FS_B2C(fs, ext2fs_blocks_count(fs->super)-1))) {
deae60a0
LC
487 /*
488 * If the last block of this group is free, then we can
489 * discard it as well.
490 */
491 if (!bitmap && i >= first_free)
492 e2fsck_discard_blocks(ctx, first_free,
493 (i - first_free) + 1);
53e3120c 494 next_group:
deae60a0
LC
495 first_free = ext2fs_blocks_count(fs->super);
496
3839e657
TT
497 free_array[group] = group_free;
498 group ++;
499 blocks = 0;
500 group_free = 0;
efac9a1b
TT
501 if (ctx->progress)
502 if ((ctx->progress)(ctx, 5, group,
503 fs->group_desc_count*2))
49e2df29 504 goto errout;
3839e657
TT
505 }
506 }
f122632e
TT
507 if (pctx.blk != NO_BLK)
508 print_bitmap_problem(ctx, save_problem, &pctx);
63c4969c 509 if (had_problem)
f122632e 510 fixit = end_problem_latch(ctx, PR_LATCH_BBITMAP);
63c4969c
TT
511 else
512 fixit = -1;
5596defa 513 ctx->flags &= ~E2F_FLAG_PROG_SUPPRESS;
49e2df29 514
1b6bf175
TT
515 if (fixit == 1) {
516 ext2fs_free_block_bitmap(fs->block_map);
517 retval = ext2fs_copy_bitmap(ctx->block_found_map,
518 &fs->block_map);
bbd47d76
TT
519 if (retval) {
520 clear_problem_context(&pctx);
521 fix_problem(ctx, PR_5_COPY_BBITMAP_ERROR, &pctx);
522 ctx->flags |= E2F_FLAG_ABORT;
49e2df29 523 goto errout;
bbd47d76 524 }
1b6bf175
TT
525 ext2fs_set_bitmap_padding(fs->block_map);
526 ext2fs_mark_bb_dirty(fs);
49e2df29 527
1b6bf175
TT
528 /* Redo the counts */
529 blocks = 0; free_blocks = 0; group_free = 0; group = 0;
530 memset(free_array, 0, fs->group_desc_count * sizeof(int));
479463aa 531 redo_flag++;
1b6bf175
TT
532 goto redo_counts;
533 } else if (fixit == 0)
534 ext2fs_unmark_valid(fs);
535
3971bfe8
TT
536 for (g = 0; g < fs->group_desc_count; g++) {
537 if (free_array[g] != ext2fs_bg_free_blocks_count(fs, g)) {
538 pctx.group = g;
539 pctx.blk = ext2fs_bg_free_blocks_count(fs, g);
540 pctx.blk2 = free_array[g];
1b6bf175
TT
541
542 if (fix_problem(ctx, PR_5_FREE_BLOCK_COUNT_GROUP,
543 &pctx)) {
3971bfe8 544 ext2fs_bg_free_blocks_count_set(fs, g, free_array[g]);
3839e657
TT
545 ext2fs_mark_super_dirty(fs);
546 } else
547 ext2fs_unmark_valid(fs);
548 }
549 }
fe75afbf 550 free_blocks = EXT2FS_C2B(fs, free_blocks);
4efbac6f 551 if (free_blocks != ext2fs_free_blocks_count(fs->super)) {
1b6bf175 552 pctx.group = 0;
4efbac6f 553 pctx.blk = ext2fs_free_blocks_count(fs->super);
1b6bf175
TT
554 pctx.blk2 = free_blocks;
555
556 if (fix_problem(ctx, PR_5_FREE_BLOCK_COUNT, &pctx)) {
4efbac6f 557 ext2fs_free_blocks_count_set(fs->super, free_blocks);
3839e657 558 ext2fs_mark_super_dirty(fs);
2788cc87 559 }
3839e657 560 }
49e2df29 561errout:
c4e3d3f3 562 ext2fs_free_mem(&free_array);
53e3120c
TT
563 ext2fs_free_mem(&actual_buf);
564 ext2fs_free_mem(&bitmap_buf);
3839e657 565}
49e2df29 566
1b6bf175 567static void check_inode_bitmaps(e2fsck_t ctx)
3839e657 568{
1b6bf175 569 ext2_filsys fs = ctx->fs;
86c627ec 570 ext2_ino_t i;
54434927
TT
571 unsigned int free_inodes = 0;
572 int group_free = 0;
573 int dirs_count = 0;
3971bfe8 574 dgrp_t group = 0;
54434927 575 unsigned int inodes = 0;
e64e6761
TT
576 ext2_ino_t *free_array;
577 ext2_ino_t *dir_array;
54434927 578 int actual, bitmap;
1b6bf175
TT
579 errcode_t retval;
580 struct problem_context pctx;
3c7c6d73
TT
581 problem_t problem, save_problem;
582 int fixit, had_problem;
16b851cd 583 int csum_flag;
f5fa2007 584 int skip_group = 0;
479463aa 585 int redo_flag = 0;
e64e6761 586 ext2_ino_t first_free = fs->super->s_inodes_per_group + 1;
49e2df29 587
1b6bf175 588 clear_problem_context(&pctx);
e64e6761
TT
589 free_array = (ext2_ino_t *) e2fsck_allocate_memory(ctx,
590 fs->group_desc_count * sizeof(ext2_ino_t), "free inode count array");
49e2df29 591
e64e6761
TT
592 dir_array = (ext2_ino_t *) e2fsck_allocate_memory(ctx,
593 fs->group_desc_count * sizeof(ext2_ino_t), "directory count array");
49e2df29 594
c5d2f50d 595 if ((1 < ext2fs_get_inode_bitmap_start2(ctx->inode_used_map)) ||
49e2df29 596 (fs->super->s_inodes_count >
c5d2f50d 597 ext2fs_get_inode_bitmap_end2(ctx->inode_used_map))) {
1b6bf175
TT
598 pctx.num = 3;
599 pctx.blk = 1;
600 pctx.blk2 = fs->super->s_inodes_count;
c5d2f50d
VAH
601 pctx.ino = ext2fs_get_inode_bitmap_start2(ctx->inode_used_map);
602 pctx.ino2 = ext2fs_get_inode_bitmap_end2(ctx->inode_used_map);
1b6bf175 603 fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx);
08b21301
TT
604
605 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
49e2df29 606 goto errout;
50e1e10f 607 }
c5d2f50d 608 if ((1 < ext2fs_get_inode_bitmap_start2(fs->inode_map)) ||
49e2df29 609 (fs->super->s_inodes_count >
c5d2f50d 610 ext2fs_get_inode_bitmap_end2(fs->inode_map))) {
1b6bf175
TT
611 pctx.num = 4;
612 pctx.blk = 1;
613 pctx.blk2 = fs->super->s_inodes_count;
c5d2f50d
VAH
614 pctx.ino = ext2fs_get_inode_bitmap_start2(fs->inode_map);
615 pctx.ino2 = ext2fs_get_inode_bitmap_end2(fs->inode_map);
1b6bf175 616 fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx);
08b21301
TT
617
618 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
49e2df29 619 goto errout;
50e1e10f
TT
620 }
621
387e6384 622 csum_flag = ext2fs_has_group_desc_csum(fs);
1b6bf175 623redo_counts:
63c4969c 624 had_problem = 0;
f122632e
TT
625 save_problem = 0;
626 pctx.ino = pctx.ino2 = 0;
16b851cd 627 if (csum_flag &&
cd65a24e 628 (ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)))
f5fa2007
TT
629 skip_group++;
630
5830d6be
ES
631 /* Protect loop from wrap-around if inodes_count is maxed */
632 for (i = 1; i <= fs->super->s_inodes_count && i > 0; i++) {
479463aa
KM
633 bitmap = 0;
634 if (skip_group &&
635 i % fs->super->s_inodes_per_group == 1) {
636 /*
637 * Current inode is the first inode
638 * in the current block group.
639 */
640 if (ext2fs_test_inode_bitmap_range(
641 ctx->inode_used_map, i,
642 fs->super->s_inodes_per_group)) {
643 /*
644 * When the compared inodes in inodes bitmap
645 * are 0, count the free inode,
646 * skip the current block group.
647 */
57581c10 648 first_free = 1;
479463aa
KM
649 inodes = fs->super->s_inodes_per_group - 1;
650 group_free = inodes;
651 free_inodes += inodes;
652 i += inodes;
653 skip_group = 0;
654 goto do_counts;
655 }
656 }
657
c5d2f50d 658 actual = ext2fs_fast_test_inode_bitmap2(ctx->inode_used_map, i);
479463aa
KM
659 if (redo_flag)
660 bitmap = actual;
661 else if (!skip_group)
c5d2f50d 662 bitmap = ext2fs_fast_test_inode_bitmap2(fs->inode_map, i);
e35ff9b9 663 if (!actual == !bitmap)
3839e657 664 goto do_counts;
49e2df29 665
3839e657
TT
666 if (!actual && bitmap) {
667 /*
668 * Inode wasn't used, but marked in bitmap
669 */
f122632e 670 problem = PR_5_INODE_UNUSED;
1b6bf175 671 } else /* if (actual && !bitmap) */ {
3839e657
TT
672 /*
673 * Inode used, but not in bitmap
674 */
1b6bf175 675 problem = PR_5_INODE_USED;
49a7360b
JS
676
677 /* We should never hit this, because it means that
678 * inodes were marked in use that weren't noticed
679 * in pass1 or pass 2. It is easier to fix the problem
680 * than to kill e2fsck and leave the user stuck. */
681 if (skip_group) {
682 struct problem_context pctx2;
683 pctx2.blk = i;
684 pctx2.group = group;
685 if (fix_problem(ctx, PR_5_INODE_UNINIT,&pctx2)){
e633b58a 686 ext2fs_bg_flags_clear(fs, group, EXT2_BG_INODE_UNINIT);
49a7360b
JS
687 skip_group = 0;
688 }
689 }
3839e657 690 }
f122632e
TT
691 if (pctx.ino == 0) {
692 pctx.ino = pctx.ino2 = i;
693 save_problem = problem;
694 } else {
695 if ((problem == save_problem) &&
696 (pctx.ino2 == i-1))
697 pctx.ino2++;
698 else {
699 print_bitmap_problem(ctx, save_problem, &pctx);
700 pctx.ino = pctx.ino2 = i;
701 save_problem = problem;
702 }
703 }
5596defa 704 ctx->flags |= E2F_FLAG_PROG_SUPPRESS;
63c4969c 705 had_problem++;
efa1a355
LC
706 /*
707 * If there a problem we should turn off the discard so we
708 * do not compromise the filesystem.
709 */
710 ctx->options &= ~E2F_OPT_DISCARD;
49e2df29 711
3839e657 712do_counts:
57581c10 713 inodes++;
f5fa2007 714 if (bitmap) {
c5d2f50d 715 if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, i))
3839e657 716 dirs_count++;
57581c10
LC
717 if (inodes > first_free) {
718 e2fsck_discard_inodes(ctx, group, first_free,
719 inodes - first_free);
720 first_free = fs->super->s_inodes_per_group + 1;
721 }
d2c9c42a 722 } else {
f5fa2007
TT
723 group_free++;
724 free_inodes++;
57581c10
LC
725 if (first_free > inodes)
726 first_free = inodes;
3839e657 727 }
efa1a355 728
3839e657
TT
729 if ((inodes == fs->super->s_inodes_per_group) ||
730 (i == fs->super->s_inodes_count)) {
57581c10
LC
731 /*
732 * If the last inode is free, we can discard it as well.
733 */
734 if (!bitmap && inodes >= first_free)
735 e2fsck_discard_inodes(ctx, group, first_free,
736 inodes - first_free + 1);
efa1a355
LC
737 /*
738 * If discard zeroes data and the group inode table
739 * was not zeroed yet, set itable as zeroed
740 */
741 if ((ctx->options & E2F_OPT_DISCARD) &&
57581c10 742 io_channel_discard_zeroes_data(fs->io) &&
efa1a355 743 !(ext2fs_bg_flags_test(fs, group,
57581c10 744 EXT2_BG_INODE_ZEROED))) {
efa1a355
LC
745 ext2fs_bg_flags_set(fs, group,
746 EXT2_BG_INODE_ZEROED);
747 ext2fs_group_desc_csum_set(fs, group);
748 }
749
57581c10
LC
750 first_free = fs->super->s_inodes_per_group + 1;
751 free_array[group] = group_free;
752 dir_array[group] = dirs_count;
3839e657
TT
753 group ++;
754 inodes = 0;
f5fa2007 755 skip_group = 0;
3839e657
TT
756 group_free = 0;
757 dirs_count = 0;
efac9a1b
TT
758 if (ctx->progress)
759 if ((ctx->progress)(ctx, 5,
760 group + fs->group_desc_count,
761 fs->group_desc_count*2))
49e2df29 762 goto errout;
16b851cd 763 if (csum_flag &&
f5fa2007 764 (i != fs->super->s_inodes_count) &&
cd65a24e 765 (ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)
732c8cd5 766 ))
f5fa2007 767 skip_group++;
3839e657
TT
768 }
769 }
f122632e
TT
770 if (pctx.ino)
771 print_bitmap_problem(ctx, save_problem, &pctx);
49e2df29 772
63c4969c
TT
773 if (had_problem)
774 fixit = end_problem_latch(ctx, PR_LATCH_IBITMAP);
775 else
776 fixit = -1;
5596defa 777 ctx->flags &= ~E2F_FLAG_PROG_SUPPRESS;
49e2df29 778
1b6bf175
TT
779 if (fixit == 1) {
780 ext2fs_free_inode_bitmap(fs->inode_map);
781 retval = ext2fs_copy_bitmap(ctx->inode_used_map,
782 &fs->inode_map);
bbd47d76
TT
783 if (retval) {
784 clear_problem_context(&pctx);
785 fix_problem(ctx, PR_5_COPY_IBITMAP_ERROR, &pctx);
786 ctx->flags |= E2F_FLAG_ABORT;
49e2df29 787 goto errout;
bbd47d76 788 }
1b6bf175
TT
789 ext2fs_set_bitmap_padding(fs->inode_map);
790 ext2fs_mark_ib_dirty(fs);
791
792 /* redo counts */
793 inodes = 0; free_inodes = 0; group_free = 0;
794 dirs_count = 0; group = 0;
795 memset(free_array, 0, fs->group_desc_count * sizeof(int));
796 memset(dir_array, 0, fs->group_desc_count * sizeof(int));
479463aa 797 redo_flag++;
1b6bf175
TT
798 goto redo_counts;
799 } else if (fixit == 0)
800 ext2fs_unmark_valid(fs);
49e2df29 801
3839e657 802 for (i = 0; i < fs->group_desc_count; i++) {
d7cca6b0 803 if (free_array[i] != ext2fs_bg_free_inodes_count(fs, i)) {
1b6bf175 804 pctx.group = i;
d7cca6b0 805 pctx.ino = ext2fs_bg_free_inodes_count(fs, i);
1b6bf175
TT
806 pctx.ino2 = free_array[i];
807 if (fix_problem(ctx, PR_5_FREE_INODE_COUNT_GROUP,
808 &pctx)) {
d7cca6b0 809 ext2fs_bg_free_inodes_count_set(fs, i, free_array[i]);
3839e657
TT
810 ext2fs_mark_super_dirty(fs);
811 } else
812 ext2fs_unmark_valid(fs);
813 }
d7cca6b0 814 if (dir_array[i] != ext2fs_bg_used_dirs_count(fs, i)) {
1b6bf175 815 pctx.group = i;
d7cca6b0 816 pctx.ino = ext2fs_bg_used_dirs_count(fs, i);
1b6bf175
TT
817 pctx.ino2 = dir_array[i];
818
819 if (fix_problem(ctx, PR_5_FREE_DIR_COUNT_GROUP,
820 &pctx)) {
d7cca6b0 821 ext2fs_bg_used_dirs_count_set(fs, i, dir_array[i]);
3839e657
TT
822 ext2fs_mark_super_dirty(fs);
823 } else
824 ext2fs_unmark_valid(fs);
825 }
826 }
827 if (free_inodes != fs->super->s_free_inodes_count) {
1b6bf175
TT
828 pctx.group = -1;
829 pctx.ino = fs->super->s_free_inodes_count;
830 pctx.ino2 = free_inodes;
831
832 if (fix_problem(ctx, PR_5_FREE_INODE_COUNT, &pctx)) {
3839e657
TT
833 fs->super->s_free_inodes_count = free_inodes;
834 ext2fs_mark_super_dirty(fs);
2788cc87 835 }
3839e657 836 }
49e2df29 837errout:
c4e3d3f3
TT
838 ext2fs_free_mem(&free_array);
839 ext2fs_free_mem(&dir_array);
3839e657
TT
840}
841
1b6bf175 842static void check_inode_end(e2fsck_t ctx)
3839e657 843{
1b6bf175 844 ext2_filsys fs = ctx->fs;
86c627ec 845 ext2_ino_t end, save_inodes_count, i;
1b6bf175
TT
846 struct problem_context pctx;
847
848 clear_problem_context(&pctx);
3839e657
TT
849
850 end = EXT2_INODES_PER_GROUP(fs->super) * fs->group_desc_count;
1b6bf175
TT
851 pctx.errcode = ext2fs_fudge_inode_bitmap_end(fs->inode_map, end,
852 &save_inodes_count);
853 if (pctx.errcode) {
854 pctx.num = 1;
855 fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx);
08b21301
TT
856 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
857 return;
f3db3566 858 }
3839e657
TT
859 if (save_inodes_count == end)
860 return;
5830d6be 861
efc6f628 862 /* protect loop from wrap-around if end is maxed */
5830d6be 863 for (i = save_inodes_count + 1; i <= end && i > save_inodes_count; i++) {
f3db3566 864 if (!ext2fs_test_inode_bitmap(fs->inode_map, i)) {
1b6bf175 865 if (fix_problem(ctx, PR_5_INODE_BMAP_PADDING, &pctx)) {
01ec1268 866 for (; i <= end; i++)
f3db3566 867 ext2fs_mark_inode_bitmap(fs->inode_map,
3839e657
TT
868 i);
869 ext2fs_mark_ib_dirty(fs);
870 } else
871 ext2fs_unmark_valid(fs);
872 break;
873 }
874 }
875
1b6bf175
TT
876 pctx.errcode = ext2fs_fudge_inode_bitmap_end(fs->inode_map,
877 save_inodes_count, 0);
878 if (pctx.errcode) {
879 pctx.num = 2;
880 fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx);
08b21301
TT
881 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
882 return;
f3db3566 883 }
3839e657
TT
884}
885
1b6bf175 886static void check_block_end(e2fsck_t ctx)
3839e657 887{
1b6bf175 888 ext2_filsys fs = ctx->fs;
c5d2f50d 889 blk64_t end, save_blocks_count, i;
1b6bf175
TT
890 struct problem_context pctx;
891
892 clear_problem_context(&pctx);
3839e657 893
c5d2f50d 894 end = ext2fs_get_block_bitmap_start2(fs->block_map) +
1e33a8b4 895 EXT2_GROUPS_TO_CLUSTERS(fs->super, fs->group_desc_count) - 1;
c5d2f50d 896 pctx.errcode = ext2fs_fudge_block_bitmap_end2(fs->block_map, end,
1b6bf175
TT
897 &save_blocks_count);
898 if (pctx.errcode) {
899 pctx.num = 3;
900 fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx);
08b21301
TT
901 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
902 return;
f3db3566 903 }
3839e657
TT
904 if (save_blocks_count == end)
905 return;
5830d6be 906
efc6f628 907 /* Protect loop from wrap-around if end is maxed */
5830d6be 908 for (i = save_blocks_count + 1; i <= end && i > save_blocks_count; i++) {
44fe08f1
TT
909 if (!ext2fs_test_block_bitmap2(fs->block_map,
910 EXT2FS_C2B(fs, i))) {
1b6bf175 911 if (fix_problem(ctx, PR_5_BLOCK_BMAP_PADDING, &pctx)) {
01ec1268 912 for (; i <= end; i++)
c5d2f50d 913 ext2fs_mark_block_bitmap2(fs->block_map,
44fe08f1 914 EXT2FS_C2B(fs, i));
3839e657
TT
915 ext2fs_mark_bb_dirty(fs);
916 } else
917 ext2fs_unmark_valid(fs);
918 break;
919 }
920 }
921
c5d2f50d 922 pctx.errcode = ext2fs_fudge_block_bitmap_end2(fs->block_map,
1b6bf175
TT
923 save_blocks_count, 0);
924 if (pctx.errcode) {
925 pctx.num = 4;
926 fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx);
08b21301
TT
927 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
928 return;
f3db3566 929 }
3839e657
TT
930}
931
1b6bf175
TT
932
933