Whamcloud - gitweb
bf3f733da29f2d1cb366edc64f0bcbf80fa149a9
[tools/e2fsprogs.git] / e2fsck / pass5.c
1 /*
2  * pass5.c --- check block and inode bitmaps against on-disk bitmaps
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
13 #include "config.h"
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
21 #include "e2fsck.h"
22 #include "problem.h"
23
24 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
25
26 static void check_block_bitmaps(e2fsck_t ctx);
27 static void check_inode_bitmaps(e2fsck_t ctx);
28 static void check_inode_end(e2fsck_t ctx);
29 static void check_block_end(e2fsck_t ctx);
30 static void check_inode_bitmap_checksum(e2fsck_t ctx);
31 static void check_block_bitmap_checksum(e2fsck_t ctx);
32
33 void e2fsck_pass5(e2fsck_t ctx)
34 {
35 #ifdef RESOURCE_TRACK
36         struct resource_track   rtrack;
37 #endif
38         struct problem_context  pctx;
39
40 #ifdef MTRACE
41         mtrace_print("Pass 5");
42 #endif
43
44         init_resource_track(&rtrack, ctx->fs->io);
45         clear_problem_context(&pctx);
46
47         if (!(ctx->options & E2F_OPT_PREEN))
48                 fix_problem(ctx, PR_5_PASS_HEADER, &pctx);
49
50         if (ctx->progress)
51                 if ((ctx->progress)(ctx, 5, 0, ctx->fs->group_desc_count*2))
52                         return;
53
54         e2fsck_read_bitmaps(ctx);
55
56         check_block_bitmaps(ctx);
57         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
58                 return;
59         check_inode_bitmaps(ctx);
60         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
61                 return;
62         check_inode_end(ctx);
63         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
64                 return;
65         check_block_end(ctx);
66         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
67                 return;
68
69         check_inode_bitmap_checksum(ctx);
70         check_block_bitmap_checksum(ctx);
71
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;
78         ext2fs_free_block_bitmap(ctx->block_metadata_map);
79         ctx->block_metadata_map = 0;
80
81         print_resource_track(ctx, _("Pass 5"), &rtrack, ctx->fs->io);
82 }
83
84 static void check_inode_bitmap_checksum(e2fsck_t ctx)
85 {
86         struct problem_context  pctx;
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) {
105                 com_err(ctx->program_name, 0, "%s",
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));
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
140 static void check_block_bitmap_checksum(e2fsck_t ctx)
141 {
142         struct problem_context  pctx;
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) {
161                 com_err(ctx->program_name, 0, "%s",
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) +
173                           ((blk64_t) i * (nbytes << 3));
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
198 static void e2fsck_discard_blocks(e2fsck_t ctx, blk64_t start,
199                                   blk64_t count)
200 {
201         ext2_filsys fs = ctx->fs;
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          */
209         if (ext2fs_test_changed(fs))
210                 ctx->options &= ~E2F_OPT_DISCARD;
211
212         if ((ctx->options & E2F_OPT_DISCARD) &&
213             (io_channel_discard(fs->io, start, count)))
214                 ctx->options &= ~E2F_OPT_DISCARD;
215 }
216
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  */
223 static void e2fsck_discard_inodes(e2fsck_t ctx, dgrp_t group,
224                                   ext2_ino_t start, int count)
225 {
226         ext2_filsys fs = ctx->fs;
227         blk64_t blk, num;
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
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))
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)
270                 e2fsck_discard_blocks(ctx, blk, num);
271 }
272
273 #define NO_BLK ((blk64_t) -1)
274
275 static void print_bitmap_problem(e2fsck_t ctx, problem_t problem,
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 }
308
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
315 static void check_block_bitmaps(e2fsck_t ctx)
316 {
317         ext2_filsys fs = ctx->fs;
318         blk64_t i;
319         unsigned int    *free_array;
320         dgrp_t          g, group = 0;
321         unsigned int    blocks = 0;
322         blk64_t free_blocks = 0;
323         blk64_t first_free = ext2fs_blocks_count(fs->super);
324         unsigned int    group_free = 0;
325         int     actual, bitmap;
326         struct problem_context  pctx;
327         problem_t       problem, save_problem;
328         int             fixit, had_problem;
329         errcode_t       retval;
330         int     old_desc_blocks = 0;
331         int     count = 0;
332         int     cmp_block = 0;
333         int     redo_flag = 0;
334         blk64_t super_blk, old_desc_blk, new_desc_blk;
335         char *actual_buf, *bitmap_buf;
336
337         actual_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize,
338                                                      "actual bitmap buffer");
339         bitmap_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize,
340                                                      "bitmap block buffer");
341
342         clear_problem_context(&pctx);
343         free_array = (unsigned int *) e2fsck_allocate_memory(ctx,
344             fs->group_desc_count * sizeof(unsigned int), "free block count array");
345
346         if ((B2C(fs->super->s_first_data_block) <
347              ext2fs_get_block_bitmap_start2(ctx->block_found_map)) ||
348             (B2C(ext2fs_blocks_count(fs->super)-1) >
349              ext2fs_get_block_bitmap_end2(ctx->block_found_map))) {
350                 pctx.num = 1;
351                 pctx.blk = B2C(fs->super->s_first_data_block);
352                 pctx.blk2 = B2C(ext2fs_blocks_count(fs->super) - 1);
353                 pctx.ino = ext2fs_get_block_bitmap_start2(ctx->block_found_map);
354                 pctx.ino2 = ext2fs_get_block_bitmap_end2(ctx->block_found_map);
355                 fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx);
356
357                 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
358                 goto errout;
359         }
360
361         if ((B2C(fs->super->s_first_data_block) <
362              ext2fs_get_block_bitmap_start2(fs->block_map)) ||
363             (B2C(ext2fs_blocks_count(fs->super)-1) >
364              ext2fs_get_block_bitmap_end2(fs->block_map))) {
365                 pctx.num = 2;
366                 pctx.blk = B2C(fs->super->s_first_data_block);
367                 pctx.blk2 = B2C(ext2fs_blocks_count(fs->super) - 1);
368                 pctx.ino = ext2fs_get_block_bitmap_start2(fs->block_map);
369                 pctx.ino2 = ext2fs_get_block_bitmap_end2(fs->block_map);
370                 fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx);
371
372                 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
373                 goto errout;
374         }
375
376 redo_counts:
377         had_problem = 0;
378         save_problem = 0;
379         pctx.blk = pctx.blk2 = NO_BLK;
380         for (i = B2C(fs->super->s_first_data_block);
381              i < ext2fs_blocks_count(fs->super);
382              i += EXT2FS_CLUSTER_RATIO(fs)) {
383                 int first_block_in_bg = (B2C(i) -
384                                          B2C(fs->super->s_first_data_block)) %
385                         fs->super->s_clusters_per_group == 0;
386                 int n, nbytes = fs->super->s_clusters_per_group / 8;
387
388                 actual = ext2fs_fast_test_block_bitmap2(ctx->block_found_map, i);
389
390                 /*
391                  * Try to optimize pass5 by extracting a bitmap block
392                  * as expected from what we have on disk, and then
393                  * comparing the two.  If they are identical, then
394                  * update the free block counts and go on to the next
395                  * block group.  This is much faster than doing the
396                  * individual bit-by-bit comparison.  The one downside
397                  * is that this doesn't work if we are asking e2fsck
398                  * to do a discard operation.
399                  */
400                 if (!first_block_in_bg ||
401                     (group == (int)fs->group_desc_count - 1) ||
402                     (ctx->options & E2F_OPT_DISCARD))
403                         goto no_optimize;
404
405                 retval = ext2fs_get_block_bitmap_range2(ctx->block_found_map,
406                                 B2C(i), fs->super->s_clusters_per_group,
407                                 actual_buf);
408                 if (retval)
409                         goto no_optimize;
410                 retval = ext2fs_get_block_bitmap_range2(fs->block_map,
411                                 B2C(i), fs->super->s_clusters_per_group,
412                                 bitmap_buf);
413                 if (retval)
414                         goto no_optimize;
415                 if (memcmp(actual_buf, bitmap_buf, nbytes) != 0)
416                         goto no_optimize;
417                 n = ext2fs_bitcount(actual_buf, nbytes);
418                 group_free = fs->super->s_clusters_per_group - n;
419                 free_blocks += group_free;
420                 i += EXT2FS_C2B(fs, fs->super->s_clusters_per_group - 1);
421                 goto next_group;
422         no_optimize:
423
424                 if (redo_flag)
425                         bitmap = actual;
426                 else
427                         bitmap = ext2fs_fast_test_block_bitmap2(fs->block_map, i);
428
429                 if (!actual == !bitmap)
430                         goto do_counts;
431
432                 if (!actual && bitmap) {
433                         /*
434                          * Block not used, but marked in use in the bitmap.
435                          */
436                         problem = PR_5_BLOCK_UNUSED;
437                 } else {
438                         /*
439                          * Block used, but not marked in use in the bitmap.
440                          */
441                         problem = PR_5_BLOCK_USED;
442
443                         if (ext2fs_bg_flags_test(fs, group,
444                                                  EXT2_BG_BLOCK_UNINIT)) {
445                                 struct problem_context pctx2;
446                                 pctx2.blk = i;
447                                 pctx2.group = group;
448                                 if (fix_problem(ctx, PR_5_BLOCK_UNINIT,
449                                                 &pctx2))
450                                         ext2fs_bg_flags_clear(fs, group,
451                                                         EXT2_BG_BLOCK_UNINIT);
452                         }
453                 }
454                 if (pctx.blk == NO_BLK) {
455                         pctx.blk = pctx.blk2 = i;
456                         save_problem = problem;
457                 } else {
458                         if ((problem == save_problem) &&
459                             (pctx.blk2 == i - EXT2FS_CLUSTER_RATIO(fs)))
460                                 pctx.blk2 += EXT2FS_CLUSTER_RATIO(fs);
461                         else {
462                                 print_bitmap_problem(ctx, save_problem, &pctx);
463                                 pctx.blk = pctx.blk2 = i;
464                                 save_problem = problem;
465                         }
466                 }
467                 ctx->flags |= E2F_FLAG_PROG_SUPPRESS;
468                 had_problem++;
469
470                 /*
471                  * If there a problem we should turn off the discard so we
472                  * do not compromise the filesystem.
473                  */
474                 ctx->options &= ~E2F_OPT_DISCARD;
475
476         do_counts:
477                 if (!bitmap) {
478                         group_free++;
479                         free_blocks++;
480                         if (first_free > i)
481                                 first_free = i;
482                 } else if (i > first_free) {
483                         e2fsck_discard_blocks(ctx, first_free,
484                                               (i - first_free));
485                         first_free = ext2fs_blocks_count(fs->super);
486                 }
487                 blocks ++;
488                 if ((blocks == fs->super->s_clusters_per_group) ||
489                     (EXT2FS_B2C(fs, i) ==
490                      EXT2FS_B2C(fs, ext2fs_blocks_count(fs->super)-1))) {
491                         /*
492                          * If the last block of this group is free, then we can
493                          * discard it as well.
494                          */
495                         if (!bitmap && i >= first_free)
496                                 e2fsck_discard_blocks(ctx, first_free,
497                                                       (i - first_free) + 1);
498                 next_group:
499                         first_free = ext2fs_blocks_count(fs->super);
500
501                         free_array[group] = group_free;
502                         group ++;
503                         blocks = 0;
504                         group_free = 0;
505                         if (ctx->progress)
506                                 if ((ctx->progress)(ctx, 5, group,
507                                                     fs->group_desc_count*2))
508                                         goto errout;
509                 }
510         }
511         if (pctx.blk != NO_BLK)
512                 print_bitmap_problem(ctx, save_problem, &pctx);
513         if (had_problem)
514                 fixit = end_problem_latch(ctx, PR_LATCH_BBITMAP);
515         else
516                 fixit = -1;
517         ctx->flags &= ~E2F_FLAG_PROG_SUPPRESS;
518
519         if (fixit == 1) {
520                 ext2fs_free_block_bitmap(fs->block_map);
521                 retval = ext2fs_copy_bitmap(ctx->block_found_map,
522                                                   &fs->block_map);
523                 if (retval) {
524                         clear_problem_context(&pctx);
525                         fix_problem(ctx, PR_5_COPY_BBITMAP_ERROR, &pctx);
526                         ctx->flags |= E2F_FLAG_ABORT;
527                         goto errout;
528                 }
529                 ext2fs_set_bitmap_padding(fs->block_map);
530                 ext2fs_mark_bb_dirty(fs);
531
532                 /* Redo the counts */
533                 blocks = 0; free_blocks = 0; group_free = 0; group = 0;
534                 memset(free_array, 0, fs->group_desc_count * sizeof(int));
535                 redo_flag++;
536                 goto redo_counts;
537         } else if (fixit == 0)
538                 ext2fs_unmark_valid(fs);
539
540         for (g = 0; g < fs->group_desc_count; g++) {
541                 if (free_array[g] != ext2fs_bg_free_blocks_count(fs, g)) {
542                         pctx.group = g;
543                         pctx.blk = ext2fs_bg_free_blocks_count(fs, g);
544                         pctx.blk2 = free_array[g];
545
546                         if (fix_problem(ctx, PR_5_FREE_BLOCK_COUNT_GROUP,
547                                         &pctx)) {
548                                 ext2fs_bg_free_blocks_count_set(fs, g, free_array[g]);
549                                 ext2fs_mark_super_dirty(fs);
550                         } else
551                                 ext2fs_unmark_valid(fs);
552                 }
553         }
554         free_blocks = EXT2FS_C2B(fs, free_blocks);
555         if (free_blocks != ext2fs_free_blocks_count(fs->super)) {
556                 pctx.group = 0;
557                 pctx.blk = ext2fs_free_blocks_count(fs->super);
558                 pctx.blk2 = free_blocks;
559
560                 if (fix_problem(ctx, PR_5_FREE_BLOCK_COUNT, &pctx)) {
561                         ext2fs_free_blocks_count_set(fs->super, free_blocks);
562                         ext2fs_mark_super_dirty(fs);
563                 }
564         }
565 errout:
566         ext2fs_free_mem(&free_array);
567         ext2fs_free_mem(&actual_buf);
568         ext2fs_free_mem(&bitmap_buf);
569 }
570
571 static void check_inode_bitmaps(e2fsck_t ctx)
572 {
573         ext2_filsys fs = ctx->fs;
574         ext2_ino_t      i;
575         unsigned int    free_inodes = 0;
576         int             group_free = 0;
577         int             dirs_count = 0;
578         dgrp_t          group = 0;
579         unsigned int    inodes = 0;
580         ext2_ino_t      *free_array;
581         ext2_ino_t      *dir_array;
582         int             actual, bitmap;
583         errcode_t       retval;
584         struct problem_context  pctx;
585         problem_t       problem, save_problem;
586         int             fixit, had_problem;
587         int             csum_flag;
588         int             skip_group = 0;
589         int             redo_flag = 0;
590         ext2_ino_t              first_free = fs->super->s_inodes_per_group + 1;
591
592         clear_problem_context(&pctx);
593         free_array = (ext2_ino_t *) e2fsck_allocate_memory(ctx,
594             fs->group_desc_count * sizeof(ext2_ino_t), "free inode count array");
595
596         dir_array = (ext2_ino_t *) e2fsck_allocate_memory(ctx,
597            fs->group_desc_count * sizeof(ext2_ino_t), "directory count array");
598
599         if ((1 < ext2fs_get_inode_bitmap_start2(ctx->inode_used_map)) ||
600             (fs->super->s_inodes_count >
601              ext2fs_get_inode_bitmap_end2(ctx->inode_used_map))) {
602                 pctx.num = 3;
603                 pctx.blk = 1;
604                 pctx.blk2 = fs->super->s_inodes_count;
605                 pctx.ino = ext2fs_get_inode_bitmap_start2(ctx->inode_used_map);
606                 pctx.ino2 = ext2fs_get_inode_bitmap_end2(ctx->inode_used_map);
607                 fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx);
608
609                 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
610                 goto errout;
611         }
612         if ((1 < ext2fs_get_inode_bitmap_start2(fs->inode_map)) ||
613             (fs->super->s_inodes_count >
614              ext2fs_get_inode_bitmap_end2(fs->inode_map))) {
615                 pctx.num = 4;
616                 pctx.blk = 1;
617                 pctx.blk2 = fs->super->s_inodes_count;
618                 pctx.ino = ext2fs_get_inode_bitmap_start2(fs->inode_map);
619                 pctx.ino2 = ext2fs_get_inode_bitmap_end2(fs->inode_map);
620                 fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx);
621
622                 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
623                 goto errout;
624         }
625
626         csum_flag = ext2fs_has_group_desc_csum(fs);
627 redo_counts:
628         had_problem = 0;
629         save_problem = 0;
630         pctx.ino = pctx.ino2 = 0;
631         if (csum_flag &&
632             (ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)))
633                 skip_group++;
634
635         /* Protect loop from wrap-around if inodes_count is maxed */
636         for (i = 1; i <= fs->super->s_inodes_count && i > 0; i++) {
637                 bitmap = 0;
638                 if (skip_group &&
639                     i % fs->super->s_inodes_per_group == 1) {
640                         /*
641                          * Current inode is the first inode
642                          * in the current block group.
643                          */
644                         if (ext2fs_test_inode_bitmap_range(
645                                     ctx->inode_used_map, i,
646                                     fs->super->s_inodes_per_group)) {
647                                 /*
648                                  * When the compared inodes in inodes bitmap
649                                  * are 0, count the free inode,
650                                  * skip the current block group.
651                                  */
652                                 first_free = 1;
653                                 inodes = fs->super->s_inodes_per_group - 1;
654                                 group_free = inodes;
655                                 free_inodes += inodes;
656                                 i += inodes;
657                                 skip_group = 0;
658                                 goto do_counts;
659                         }
660                 }
661
662                 actual = ext2fs_fast_test_inode_bitmap2(ctx->inode_used_map, i);
663                 if (redo_flag)
664                         bitmap = actual;
665                 else if (!skip_group)
666                         bitmap = ext2fs_fast_test_inode_bitmap2(fs->inode_map, i);
667                 if (!actual == !bitmap)
668                         goto do_counts;
669
670                 if (!actual && bitmap) {
671                         /*
672                          * Inode wasn't used, but marked in bitmap
673                          */
674                         problem = PR_5_INODE_UNUSED;
675                 } else /* if (actual && !bitmap) */ {
676                         /*
677                          * Inode used, but not in bitmap
678                          */
679                         problem = PR_5_INODE_USED;
680
681                         /* We should never hit this, because it means that
682                          * inodes were marked in use that weren't noticed
683                          * in pass1 or pass 2. It is easier to fix the problem
684                          * than to kill e2fsck and leave the user stuck. */
685                         if (skip_group) {
686                                 struct problem_context pctx2;
687                                 pctx2.blk = i;
688                                 pctx2.group = group;
689                                 if (fix_problem(ctx, PR_5_INODE_UNINIT,&pctx2)){
690                                         ext2fs_bg_flags_clear(fs, group, EXT2_BG_INODE_UNINIT);
691                                         skip_group = 0;
692                                 }
693                         }
694                 }
695                 if (pctx.ino == 0) {
696                         pctx.ino = pctx.ino2 = i;
697                         save_problem = problem;
698                 } else {
699                         if ((problem == save_problem) &&
700                             (pctx.ino2 == i-1))
701                                 pctx.ino2++;
702                         else {
703                                 print_bitmap_problem(ctx, save_problem, &pctx);
704                                 pctx.ino = pctx.ino2 = i;
705                                 save_problem = problem;
706                         }
707                 }
708                 ctx->flags |= E2F_FLAG_PROG_SUPPRESS;
709                 had_problem++;
710                 /*
711                  * If there a problem we should turn off the discard so we
712                  * do not compromise the filesystem.
713                  */
714                 ctx->options &= ~E2F_OPT_DISCARD;
715
716 do_counts:
717                 inodes++;
718                 if (bitmap) {
719                         if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, i))
720                                 dirs_count++;
721                         if (inodes > first_free) {
722                                 e2fsck_discard_inodes(ctx, group, first_free,
723                                                       inodes - first_free);
724                                 first_free = fs->super->s_inodes_per_group + 1;
725                         }
726                 } else {
727                         group_free++;
728                         free_inodes++;
729                         if (first_free > inodes)
730                                 first_free = inodes;
731                 }
732
733                 if ((inodes == fs->super->s_inodes_per_group) ||
734                     (i == fs->super->s_inodes_count)) {
735                         /*
736                          * If the last inode is free, we can discard it as well.
737                          */
738                         if (!bitmap && inodes >= first_free)
739                                 e2fsck_discard_inodes(ctx, group, first_free,
740                                                       inodes - first_free + 1);
741                         /*
742                          * If discard zeroes data and the group inode table
743                          * was not zeroed yet, set itable as zeroed
744                          */
745                         if ((ctx->options & E2F_OPT_DISCARD) &&
746                             io_channel_discard_zeroes_data(fs->io) &&
747                             !(ext2fs_bg_flags_test(fs, group,
748                                                    EXT2_BG_INODE_ZEROED))) {
749                                 ext2fs_bg_flags_set(fs, group,
750                                                     EXT2_BG_INODE_ZEROED);
751                                 ext2fs_group_desc_csum_set(fs, group);
752                         }
753
754                         first_free = fs->super->s_inodes_per_group + 1;
755                         free_array[group] = group_free;
756                         dir_array[group] = dirs_count;
757                         group ++;
758                         inodes = 0;
759                         skip_group = 0;
760                         group_free = 0;
761                         dirs_count = 0;
762                         if (ctx->progress)
763                                 if ((ctx->progress)(ctx, 5,
764                                             group + fs->group_desc_count,
765                                             fs->group_desc_count*2))
766                                         goto errout;
767                         if (csum_flag &&
768                             (i != fs->super->s_inodes_count) &&
769                             (ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)
770                              ))
771                                 skip_group++;
772                 }
773         }
774         if (pctx.ino)
775                 print_bitmap_problem(ctx, save_problem, &pctx);
776
777         if (had_problem)
778                 fixit = end_problem_latch(ctx, PR_LATCH_IBITMAP);
779         else
780                 fixit = -1;
781         ctx->flags &= ~E2F_FLAG_PROG_SUPPRESS;
782
783         if (fixit == 1) {
784                 ext2fs_free_inode_bitmap(fs->inode_map);
785                 retval = ext2fs_copy_bitmap(ctx->inode_used_map,
786                                                   &fs->inode_map);
787                 if (retval) {
788                         clear_problem_context(&pctx);
789                         fix_problem(ctx, PR_5_COPY_IBITMAP_ERROR, &pctx);
790                         ctx->flags |= E2F_FLAG_ABORT;
791                         goto errout;
792                 }
793                 ext2fs_set_bitmap_padding(fs->inode_map);
794                 ext2fs_mark_ib_dirty(fs);
795
796                 /* redo counts */
797                 inodes = 0; free_inodes = 0; group_free = 0;
798                 dirs_count = 0; group = 0;
799                 memset(free_array, 0, fs->group_desc_count * sizeof(int));
800                 memset(dir_array, 0, fs->group_desc_count * sizeof(int));
801                 redo_flag++;
802                 goto redo_counts;
803         } else if (fixit == 0)
804                 ext2fs_unmark_valid(fs);
805
806         for (i = 0; i < fs->group_desc_count; i++) {
807                 if (free_array[i] != ext2fs_bg_free_inodes_count(fs, i)) {
808                         pctx.group = i;
809                         pctx.ino = ext2fs_bg_free_inodes_count(fs, i);
810                         pctx.ino2 = free_array[i];
811                         if (fix_problem(ctx, PR_5_FREE_INODE_COUNT_GROUP,
812                                         &pctx)) {
813                                 ext2fs_bg_free_inodes_count_set(fs, i, free_array[i]);
814                                 ext2fs_mark_super_dirty(fs);
815                         } else
816                                 ext2fs_unmark_valid(fs);
817                 }
818                 if (dir_array[i] != ext2fs_bg_used_dirs_count(fs, i)) {
819                         pctx.group = i;
820                         pctx.ino = ext2fs_bg_used_dirs_count(fs, i);
821                         pctx.ino2 = dir_array[i];
822
823                         if (fix_problem(ctx, PR_5_FREE_DIR_COUNT_GROUP,
824                                         &pctx)) {
825                                 ext2fs_bg_used_dirs_count_set(fs, i, dir_array[i]);
826                                 ext2fs_mark_super_dirty(fs);
827                         } else
828                                 ext2fs_unmark_valid(fs);
829                 }
830         }
831         if (free_inodes != fs->super->s_free_inodes_count) {
832                 pctx.group = -1;
833                 pctx.ino = fs->super->s_free_inodes_count;
834                 pctx.ino2 = free_inodes;
835
836                 if (fix_problem(ctx, PR_5_FREE_INODE_COUNT, &pctx)) {
837                         fs->super->s_free_inodes_count = free_inodes;
838                         ext2fs_mark_super_dirty(fs);
839                 }
840         }
841 errout:
842         ext2fs_free_mem(&free_array);
843         ext2fs_free_mem(&dir_array);
844 }
845
846 static void check_inode_end(e2fsck_t ctx)
847 {
848         ext2_filsys fs = ctx->fs;
849         ext2_ino_t      end, save_inodes_count, i;
850         struct problem_context  pctx;
851
852         clear_problem_context(&pctx);
853
854         end = EXT2_INODES_PER_GROUP(fs->super) * fs->group_desc_count;
855         pctx.errcode = ext2fs_fudge_inode_bitmap_end(fs->inode_map, end,
856                                                      &save_inodes_count);
857         if (pctx.errcode) {
858                 pctx.num = 1;
859                 fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx);
860                 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
861                 return;
862         }
863         if (save_inodes_count == end)
864                 return;
865
866         /* protect loop from wrap-around if end is maxed */
867         for (i = save_inodes_count + 1; i <= end && i > save_inodes_count; i++) {
868                 if (!ext2fs_test_inode_bitmap(fs->inode_map, i)) {
869                         if (fix_problem(ctx, PR_5_INODE_BMAP_PADDING, &pctx)) {
870                                 for (; i <= end; i++)
871                                         ext2fs_mark_inode_bitmap(fs->inode_map,
872                                                                  i);
873                                 ext2fs_mark_ib_dirty(fs);
874                         } else
875                                 ext2fs_unmark_valid(fs);
876                         break;
877                 }
878         }
879
880         pctx.errcode = ext2fs_fudge_inode_bitmap_end(fs->inode_map,
881                                                      save_inodes_count, 0);
882         if (pctx.errcode) {
883                 pctx.num = 2;
884                 fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx);
885                 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
886                 return;
887         }
888 }
889
890 static void check_block_end(e2fsck_t ctx)
891 {
892         ext2_filsys fs = ctx->fs;
893         blk64_t end, save_blocks_count, i;
894         struct problem_context  pctx;
895
896         clear_problem_context(&pctx);
897
898         end = ext2fs_get_block_bitmap_start2(fs->block_map) +
899                 EXT2_GROUPS_TO_CLUSTERS(fs->super, fs->group_desc_count) - 1;
900         pctx.errcode = ext2fs_fudge_block_bitmap_end2(fs->block_map, end,
901                                                      &save_blocks_count);
902         if (pctx.errcode) {
903                 pctx.num = 3;
904                 fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx);
905                 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
906                 return;
907         }
908         if (save_blocks_count == end)
909                 return;
910
911         /* Protect loop from wrap-around if end is maxed */
912         for (i = save_blocks_count + 1; i <= end && i > save_blocks_count; i++) {
913                 if (!ext2fs_test_block_bitmap2(fs->block_map,
914                                                EXT2FS_C2B(fs, i))) {
915                         if (fix_problem(ctx, PR_5_BLOCK_BMAP_PADDING, &pctx)) {
916                                 for (; i <= end; i++)
917                                         ext2fs_mark_block_bitmap2(fs->block_map,
918                                                         EXT2FS_C2B(fs, i));
919                                 ext2fs_mark_bb_dirty(fs);
920                         } else
921                                 ext2fs_unmark_valid(fs);
922                         break;
923                 }
924         }
925
926         pctx.errcode = ext2fs_fudge_block_bitmap_end2(fs->block_map,
927                                                      save_blocks_count, 0);
928         if (pctx.errcode) {
929                 pctx.num = 4;
930                 fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx);
931                 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
932                 return;
933         }
934 }
935
936
937