Whamcloud - gitweb
878d1f693f27e9e0746cacd7c08f9f4bcfbc1241
[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 <stdint.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <sys/ioctl.h>
17 #include <fcntl.h>
18 #include <errno.h>
19
20 #include "e2fsck.h"
21 #include "problem.h"
22
23 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
24
25 static void check_block_bitmaps(e2fsck_t ctx);
26 static void check_inode_bitmaps(e2fsck_t ctx);
27 static void check_inode_end(e2fsck_t ctx);
28 static void check_block_end(e2fsck_t ctx);
29
30 void e2fsck_pass5(e2fsck_t ctx)
31 {
32 #ifdef RESOURCE_TRACK
33         struct resource_track   rtrack;
34 #endif
35         struct problem_context  pctx;
36
37 #ifdef MTRACE
38         mtrace_print("Pass 5");
39 #endif
40
41         init_resource_track(&rtrack, ctx->fs->io);
42         clear_problem_context(&pctx);
43
44         if (!(ctx->options & E2F_OPT_PREEN))
45                 fix_problem(ctx, PR_5_PASS_HEADER, &pctx);
46
47         if (ctx->progress)
48                 if ((ctx->progress)(ctx, 5, 0, ctx->fs->group_desc_count*2))
49                         return;
50
51         e2fsck_read_bitmaps(ctx);
52
53         check_block_bitmaps(ctx);
54         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
55                 return;
56         check_inode_bitmaps(ctx);
57         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
58                 return;
59         check_inode_end(ctx);
60         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
61                 return;
62         check_block_end(ctx);
63         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
64                 return;
65
66         ext2fs_free_inode_bitmap(ctx->inode_used_map);
67         ctx->inode_used_map = 0;
68         ext2fs_free_inode_bitmap(ctx->inode_dir_map);
69         ctx->inode_dir_map = 0;
70         ext2fs_free_block_bitmap(ctx->block_found_map);
71         ctx->block_found_map = 0;
72
73         print_resource_track(ctx, _("Pass 5"), &rtrack, ctx->fs->io);
74 }
75
76 static void e2fsck_discard_blocks(e2fsck_t ctx, io_manager manager,
77                                   blk64_t start, blk64_t count)
78 {
79         ext2_filsys fs = ctx->fs;
80         int ret = 0;
81
82         /*
83          * If the filesystem has changed it means that there was an corruption
84          * which should be repaired, but in some cases just one e2fsck run is
85          * not enough to fix the problem, hence it is not safe to run discard
86          * in this case.
87          */
88         if (ext2fs_test_changed(ctx->fs))
89                 ctx->options &= ~E2F_OPT_DISCARD;
90
91         if ((ctx->options & E2F_OPT_DISCARD) &&
92             (io_channel_discard(fs->io, start, count)))
93                 ctx->options &= ~E2F_OPT_DISCARD;
94 }
95
96 #define NO_BLK ((blk64_t) -1)
97
98 static void print_bitmap_problem(e2fsck_t ctx, int problem,
99                             struct problem_context *pctx)
100 {
101         switch (problem) {
102         case PR_5_BLOCK_UNUSED:
103                 if (pctx->blk == pctx->blk2)
104                         pctx->blk2 = 0;
105                 else
106                         problem = PR_5_BLOCK_RANGE_UNUSED;
107                 break;
108         case PR_5_BLOCK_USED:
109                 if (pctx->blk == pctx->blk2)
110                         pctx->blk2 = 0;
111                 else
112                         problem = PR_5_BLOCK_RANGE_USED;
113                 break;
114         case PR_5_INODE_UNUSED:
115                 if (pctx->ino == pctx->ino2)
116                         pctx->ino2 = 0;
117                 else
118                         problem = PR_5_INODE_RANGE_UNUSED;
119                 break;
120         case PR_5_INODE_USED:
121                 if (pctx->ino == pctx->ino2)
122                         pctx->ino2 = 0;
123                 else
124                         problem = PR_5_INODE_RANGE_USED;
125                 break;
126         }
127         fix_problem(ctx, problem, pctx);
128         pctx->blk = pctx->blk2 = NO_BLK;
129         pctx->ino = pctx->ino2 = 0;
130 }
131
132 static void check_block_bitmaps(e2fsck_t ctx)
133 {
134         ext2_filsys fs = ctx->fs;
135         blk64_t i;
136         int     *free_array;
137         int     group = 0;
138         int     blocks = 0;
139         blk64_t free_blocks = 0;
140         blk64_t first_free = ext2fs_blocks_count(fs->super);
141         int     group_free = 0;
142         int     actual, bitmap;
143         struct problem_context  pctx;
144         int     problem, save_problem, fixit, had_problem;
145         errcode_t       retval;
146         int             csum_flag;
147         int             skip_group = 0;
148         int     old_desc_blocks = 0;
149         int     count = 0;
150         int     cmp_block = 0;
151         int     redo_flag = 0;
152         blk64_t super_blk, old_desc_blk, new_desc_blk;
153         io_manager      manager = ctx->fs->io->manager;
154
155         clear_problem_context(&pctx);
156         free_array = (int *) e2fsck_allocate_memory(ctx,
157             fs->group_desc_count * sizeof(int), "free block count array");
158
159         if ((EXT2FS_B2C(fs, fs->super->s_first_data_block) <
160              ext2fs_get_block_bitmap_start2(ctx->block_found_map)) ||
161             (EXT2FS_B2C(fs, ext2fs_blocks_count(fs->super)-1) >
162              ext2fs_get_block_bitmap_end2(ctx->block_found_map))) {
163                 pctx.num = 1;
164                 pctx.blk = EXT2FS_B2C(fs, fs->super->s_first_data_block);
165                 pctx.blk2 = EXT2FS_B2C(fs, ext2fs_blocks_count(fs->super) - 1);
166                 pctx.ino = ext2fs_get_block_bitmap_start2(ctx->block_found_map);
167                 pctx.ino2 = ext2fs_get_block_bitmap_end2(ctx->block_found_map);
168                 fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx);
169
170                 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
171                 goto errout;
172         }
173
174         if ((EXT2FS_B2C(fs, fs->super->s_first_data_block) <
175              ext2fs_get_block_bitmap_start2(fs->block_map)) ||
176             (EXT2FS_B2C(fs, ext2fs_blocks_count(fs->super)-1) >
177              ext2fs_get_block_bitmap_end2(fs->block_map))) {
178                 pctx.num = 2;
179                 pctx.blk = EXT2FS_B2C(fs, fs->super->s_first_data_block);
180                 pctx.blk2 = EXT2FS_B2C(fs, ext2fs_blocks_count(fs->super) - 1);
181                 pctx.ino = ext2fs_get_block_bitmap_start2(fs->block_map);
182                 pctx.ino2 = ext2fs_get_block_bitmap_end2(fs->block_map);
183                 fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx);
184
185                 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
186                 goto errout;
187         }
188
189         csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
190                                                EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
191 redo_counts:
192         had_problem = 0;
193         save_problem = 0;
194         pctx.blk = pctx.blk2 = NO_BLK;
195         if (csum_flag &&
196             (ext2fs_bg_flags_test(fs, group, EXT2_BG_BLOCK_UNINIT)))
197                 skip_group++;
198         for (i = EXT2FS_B2C(fs, fs->super->s_first_data_block);
199              i < ext2fs_blocks_count(fs->super);
200              i += EXT2FS_CLUSTER_RATIO(fs)) {
201                 actual = ext2fs_fast_test_block_bitmap2(ctx->block_found_map, i);
202
203                 if (skip_group) {
204                         if ((i - fs->super->s_first_data_block) %
205                             fs->super->s_blocks_per_group == 0) {
206                                 super_blk = 0;
207                                 old_desc_blk = 0;
208                                 new_desc_blk = 0;
209                                 ext2fs_super_and_bgd_loc2(fs, group, &super_blk,
210                                          &old_desc_blk, &new_desc_blk, 0);
211
212                                 if (fs->super->s_feature_incompat &
213                                                 EXT2_FEATURE_INCOMPAT_META_BG)
214                                         old_desc_blocks =
215                                                 fs->super->s_first_meta_bg;
216                                 else
217                                         old_desc_blocks = fs->desc_blocks +
218                                         fs->super->s_reserved_gdt_blocks;
219
220                                 count = 0;
221                                 cmp_block = fs->super->s_blocks_per_group;
222                                 if (group == (int)fs->group_desc_count - 1)
223                                         cmp_block =
224                                                 ext2fs_blocks_count(fs->super) %
225                                                 fs->super->s_blocks_per_group;
226                         }
227
228                         bitmap = 0;
229                         if ((i == super_blk) ||
230                             (old_desc_blk && old_desc_blocks &&
231                              (i >= old_desc_blk) &&
232                              (i < old_desc_blk + old_desc_blocks)) ||
233                             (new_desc_blk && (i == new_desc_blk)) ||
234                             (i == ext2fs_block_bitmap_loc(fs, group)) ||
235                             (i == ext2fs_inode_bitmap_loc(fs, group)) ||
236                             (i >= ext2fs_inode_table_loc(fs, group) &&
237                              (i < ext2fs_inode_table_loc(fs, group) +
238                               fs->inode_blocks_per_group))) {
239                                 bitmap = 1;
240                                 actual = (actual != 0);
241                                 count++;
242                                 cmp_block--;
243                         } else if ((i - count - fs->super->s_first_data_block) %
244                                   fs->super->s_blocks_per_group == 0) {
245                                 /*
246                                  * When the compare data blocks in block bitmap
247                                  * are 0, count the free block,
248                                  * skip the current block group.
249                                  */
250                                 if (ext2fs_test_block_bitmap_range2(
251                                             ctx->block_found_map, i,
252                                             cmp_block)) {
253                                         /*
254                                          * -1 means to skip the current block
255                                          * group.
256                                          */
257                                         blocks = fs->super->s_blocks_per_group
258                                                                         - 1;
259                                         group_free = cmp_block;
260                                         free_blocks += cmp_block;
261                                         /*
262                                          * The current block group's last block
263                                          * is set to i.
264                                          */
265                                         i += cmp_block - 1;
266                                         bitmap = 1;
267                                         goto do_counts;
268                                 }
269                         }
270                 } else if (redo_flag)
271                         bitmap = actual;
272                 else
273                         bitmap = ext2fs_fast_test_block_bitmap2(fs->block_map, i);
274
275                 if (actual == bitmap)
276                         goto do_counts;
277
278                 if (!actual && bitmap) {
279                         /*
280                          * Block not used, but marked in use in the bitmap.
281                          */
282                         problem = PR_5_BLOCK_UNUSED;
283                 } else {
284                         /*
285                          * Block used, but not marked in use in the bitmap.
286                          */
287                         problem = PR_5_BLOCK_USED;
288
289                         if (skip_group) {
290                                 struct problem_context pctx2;
291                                 pctx2.blk = i;
292                                 pctx2.group = group;
293                                 if (fix_problem(ctx, PR_5_BLOCK_UNINIT,&pctx2)){
294                                         ext2fs_bg_flags_clear(fs, group, EXT2_BG_BLOCK_UNINIT);
295                                         skip_group = 0;
296                                 }
297                         }
298                 }
299                 if (pctx.blk == NO_BLK) {
300                         pctx.blk = pctx.blk2 = i;
301                         save_problem = problem;
302                 } else {
303                         if ((problem == save_problem) &&
304                             (pctx.blk2 == i-1))
305                                 pctx.blk2++;
306                         else {
307                                 print_bitmap_problem(ctx, save_problem, &pctx);
308                                 pctx.blk = pctx.blk2 = i;
309                                 save_problem = problem;
310                         }
311                 }
312                 ctx->flags |= E2F_FLAG_PROG_SUPPRESS;
313                 had_problem++;
314
315                 /*
316                  * If there a problem we should turn off the discard so we
317                  * do not compromise the filesystem.
318                  */
319                 ctx->options &= ~E2F_OPT_DISCARD;
320
321         do_counts:
322                 if (!bitmap && (!skip_group || csum_flag)) {
323                         group_free++;
324                         free_blocks++;
325                         if (first_free > i)
326                                 first_free = i;
327                 } else {
328                         if ((i > first_free) &&
329                            (ctx->options & E2F_OPT_DISCARD)) {
330                                 e2fsck_discard_blocks(ctx, manager, first_free,
331                                                       (i - first_free));
332                         }
333                         first_free = ext2fs_blocks_count(fs->super);
334                 }
335                 blocks ++;
336                 if ((blocks == fs->super->s_clusters_per_group) ||
337                     (EXT2FS_B2C(fs, i) ==
338                      EXT2FS_B2C(fs, ext2fs_blocks_count(fs->super)-1))) {
339                         free_array[group] = group_free;
340                         group ++;
341                         blocks = 0;
342                         group_free = 0;
343                         skip_group = 0;
344                         if (ctx->progress)
345                                 if ((ctx->progress)(ctx, 5, group,
346                                                     fs->group_desc_count*2))
347                                         goto errout;
348                         if (csum_flag &&
349                             (i != ext2fs_blocks_count(fs->super)-1) &&
350                             ext2fs_bg_flags_test(fs, group, 
351                                                 EXT2_BG_BLOCK_UNINIT))
352                                 skip_group++;
353                 }
354         }
355         if (pctx.blk != NO_BLK)
356                 print_bitmap_problem(ctx, save_problem, &pctx);
357         if (had_problem)
358                 fixit = end_problem_latch(ctx, PR_LATCH_BBITMAP);
359         else
360                 fixit = -1;
361         ctx->flags &= ~E2F_FLAG_PROG_SUPPRESS;
362
363         if (fixit == 1) {
364                 ext2fs_free_block_bitmap(fs->block_map);
365                 retval = ext2fs_copy_bitmap(ctx->block_found_map,
366                                                   &fs->block_map);
367                 if (retval) {
368                         clear_problem_context(&pctx);
369                         fix_problem(ctx, PR_5_COPY_BBITMAP_ERROR, &pctx);
370                         ctx->flags |= E2F_FLAG_ABORT;
371                         goto errout;
372                 }
373                 ext2fs_set_bitmap_padding(fs->block_map);
374                 ext2fs_mark_bb_dirty(fs);
375
376                 /* Redo the counts */
377                 blocks = 0; free_blocks = 0; group_free = 0; group = 0;
378                 memset(free_array, 0, fs->group_desc_count * sizeof(int));
379                 redo_flag++;
380                 goto redo_counts;
381         } else if (fixit == 0)
382                 ext2fs_unmark_valid(fs);
383
384         for (i = 0; i < fs->group_desc_count; i++) {
385                 if (free_array[i] != ext2fs_bg_free_blocks_count(fs, i)) {
386                         pctx.group = i;
387                         pctx.blk = ext2fs_bg_free_blocks_count(fs, i);
388                         pctx.blk2 = free_array[i];
389
390                         if (fix_problem(ctx, PR_5_FREE_BLOCK_COUNT_GROUP,
391                                         &pctx)) {
392                                 ext2fs_bg_free_blocks_count_set(fs, i, free_array[i]);
393                                 ext2fs_mark_super_dirty(fs);
394                         } else
395                                 ext2fs_unmark_valid(fs);
396                 }
397         }
398         if (free_blocks != ext2fs_free_blocks_count(fs->super)) {
399                 pctx.group = 0;
400                 pctx.blk = ext2fs_free_blocks_count(fs->super);
401                 pctx.blk2 = free_blocks;
402
403                 if (fix_problem(ctx, PR_5_FREE_BLOCK_COUNT, &pctx)) {
404                         ext2fs_free_blocks_count_set(fs->super, free_blocks);
405                         ext2fs_mark_super_dirty(fs);
406                 } else
407                         ext2fs_unmark_valid(fs);
408         }
409 errout:
410         ext2fs_free_mem(&free_array);
411 }
412
413 static void check_inode_bitmaps(e2fsck_t ctx)
414 {
415         ext2_filsys fs = ctx->fs;
416         ext2_ino_t      i;
417         unsigned int    free_inodes = 0;
418         int             group_free = 0;
419         int             dirs_count = 0;
420         int             group = 0;
421         unsigned int    inodes = 0;
422         int             *free_array;
423         int             *dir_array;
424         int             actual, bitmap;
425         errcode_t       retval;
426         struct problem_context  pctx;
427         int             problem, save_problem, fixit, had_problem;
428         int             csum_flag;
429         int             skip_group = 0;
430         int             redo_flag = 0;
431         io_manager      manager = ctx->fs->io->manager;
432
433         clear_problem_context(&pctx);
434         free_array = (int *) e2fsck_allocate_memory(ctx,
435             fs->group_desc_count * sizeof(int), "free inode count array");
436
437         dir_array = (int *) e2fsck_allocate_memory(ctx,
438            fs->group_desc_count * sizeof(int), "directory count array");
439
440         if ((1 < ext2fs_get_inode_bitmap_start2(ctx->inode_used_map)) ||
441             (fs->super->s_inodes_count >
442              ext2fs_get_inode_bitmap_end2(ctx->inode_used_map))) {
443                 pctx.num = 3;
444                 pctx.blk = 1;
445                 pctx.blk2 = fs->super->s_inodes_count;
446                 pctx.ino = ext2fs_get_inode_bitmap_start2(ctx->inode_used_map);
447                 pctx.ino2 = ext2fs_get_inode_bitmap_end2(ctx->inode_used_map);
448                 fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx);
449
450                 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
451                 goto errout;
452         }
453         if ((1 < ext2fs_get_inode_bitmap_start2(fs->inode_map)) ||
454             (fs->super->s_inodes_count >
455              ext2fs_get_inode_bitmap_end2(fs->inode_map))) {
456                 pctx.num = 4;
457                 pctx.blk = 1;
458                 pctx.blk2 = fs->super->s_inodes_count;
459                 pctx.ino = ext2fs_get_inode_bitmap_start2(fs->inode_map);
460                 pctx.ino2 = ext2fs_get_inode_bitmap_end2(fs->inode_map);
461                 fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx);
462
463                 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
464                 goto errout;
465         }
466
467         csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
468                                                EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
469 redo_counts:
470         had_problem = 0;
471         save_problem = 0;
472         pctx.ino = pctx.ino2 = 0;
473         if (csum_flag &&
474             (ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)))
475                 skip_group++;
476
477         /* Protect loop from wrap-around if inodes_count is maxed */
478         for (i = 1; i <= fs->super->s_inodes_count && i > 0; i++) {
479                 bitmap = 0;
480                 if (skip_group &&
481                     i % fs->super->s_inodes_per_group == 1) {
482                         /*
483                          * Current inode is the first inode
484                          * in the current block group.
485                          */
486                         if (ext2fs_test_inode_bitmap_range(
487                                     ctx->inode_used_map, i,
488                                     fs->super->s_inodes_per_group)) {
489                                 /*
490                                  * When the compared inodes in inodes bitmap
491                                  * are 0, count the free inode,
492                                  * skip the current block group.
493                                  */
494                                 inodes = fs->super->s_inodes_per_group - 1;
495                                 group_free = inodes;
496                                 free_inodes += inodes;
497                                 i += inodes;
498                                 skip_group = 0;
499                                 goto do_counts;
500                         }
501                 }
502
503                 actual = ext2fs_fast_test_inode_bitmap2(ctx->inode_used_map, i);
504                 if (redo_flag)
505                         bitmap = actual;
506                 else if (!skip_group)
507                         bitmap = ext2fs_fast_test_inode_bitmap2(fs->inode_map, i);
508                 if (actual == bitmap)
509                         goto do_counts;
510
511                 if (!actual && bitmap) {
512                         /*
513                          * Inode wasn't used, but marked in bitmap
514                          */
515                         problem = PR_5_INODE_UNUSED;
516                 } else /* if (actual && !bitmap) */ {
517                         /*
518                          * Inode used, but not in bitmap
519                          */
520                         problem = PR_5_INODE_USED;
521
522                         /* We should never hit this, because it means that
523                          * inodes were marked in use that weren't noticed
524                          * in pass1 or pass 2. It is easier to fix the problem
525                          * than to kill e2fsck and leave the user stuck. */
526                         if (skip_group) {
527                                 struct problem_context pctx2;
528                                 pctx2.blk = i;
529                                 pctx2.group = group;
530                                 if (fix_problem(ctx, PR_5_INODE_UNINIT,&pctx2)){
531                                         ext2fs_bg_flags_clear(fs, group, EXT2_BG_INODE_UNINIT);
532                                         skip_group = 0;
533                                 }
534                         }
535                 }
536                 if (pctx.ino == 0) {
537                         pctx.ino = pctx.ino2 = i;
538                         save_problem = problem;
539                 } else {
540                         if ((problem == save_problem) &&
541                             (pctx.ino2 == i-1))
542                                 pctx.ino2++;
543                         else {
544                                 print_bitmap_problem(ctx, save_problem, &pctx);
545                                 pctx.ino = pctx.ino2 = i;
546                                 save_problem = problem;
547                         }
548                 }
549                 ctx->flags |= E2F_FLAG_PROG_SUPPRESS;
550                 had_problem++;
551                 /*
552                  * If there a problem we should turn off the discard so we
553                  * do not compromise the filesystem.
554                  */
555                 ctx->options &= ~E2F_OPT_DISCARD;
556
557 do_counts:
558                 if (bitmap) {
559                         if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, i))
560                                 dirs_count++;
561                 } else if (!skip_group || csum_flag) {
562                         group_free++;
563                         free_inodes++;
564                 }
565
566                 inodes++;
567                 if ((inodes == fs->super->s_inodes_per_group) ||
568                     (i == fs->super->s_inodes_count)) {
569
570                         free_array[group] = group_free;
571                         dir_array[group] = dirs_count;
572
573                         /* Discard inode table */
574                         if (ctx->options & E2F_OPT_DISCARD) {
575                                 blk64_t used_blks, blk, num;
576
577                                 used_blks = DIV_ROUND_UP(
578                                         (EXT2_INODES_PER_GROUP(fs->super) -
579                                         group_free),
580                                         EXT2_INODES_PER_BLOCK(fs->super));
581
582                                 blk = ext2fs_inode_table_loc(fs, group) +
583                                       used_blks;
584                                 num = fs->inode_blocks_per_group -
585                                       used_blks;
586                                 e2fsck_discard_blocks(ctx, manager, blk, num);
587                         }
588
589                         /*
590                          * If discard zeroes data and the group inode table
591                          * was not zeroed yet, set itable as zeroed
592                          */
593                         if ((ctx->options & E2F_OPT_DISCARD) &&
594                             (io_channel_discard_zeroes_data(fs->io)) &&
595                             !(ext2fs_bg_flags_test(fs, group,
596                                                   EXT2_BG_INODE_ZEROED))) {
597                                 ext2fs_bg_flags_set(fs, group,
598                                                     EXT2_BG_INODE_ZEROED);
599                                 ext2fs_group_desc_csum_set(fs, group);
600                         }
601
602                         group ++;
603                         inodes = 0;
604                         skip_group = 0;
605                         group_free = 0;
606                         dirs_count = 0;
607                         if (ctx->progress)
608                                 if ((ctx->progress)(ctx, 5,
609                                             group + fs->group_desc_count,
610                                             fs->group_desc_count*2))
611                                         goto errout;
612                         if (csum_flag &&
613                             (i != fs->super->s_inodes_count) &&
614                             (ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)
615                              ))
616                                 skip_group++;
617                 }
618         }
619         if (pctx.ino)
620                 print_bitmap_problem(ctx, save_problem, &pctx);
621
622         if (had_problem)
623                 fixit = end_problem_latch(ctx, PR_LATCH_IBITMAP);
624         else
625                 fixit = -1;
626         ctx->flags &= ~E2F_FLAG_PROG_SUPPRESS;
627
628         if (fixit == 1) {
629                 ext2fs_free_inode_bitmap(fs->inode_map);
630                 retval = ext2fs_copy_bitmap(ctx->inode_used_map,
631                                                   &fs->inode_map);
632                 if (retval) {
633                         clear_problem_context(&pctx);
634                         fix_problem(ctx, PR_5_COPY_IBITMAP_ERROR, &pctx);
635                         ctx->flags |= E2F_FLAG_ABORT;
636                         goto errout;
637                 }
638                 ext2fs_set_bitmap_padding(fs->inode_map);
639                 ext2fs_mark_ib_dirty(fs);
640
641                 /* redo counts */
642                 inodes = 0; free_inodes = 0; group_free = 0;
643                 dirs_count = 0; group = 0;
644                 memset(free_array, 0, fs->group_desc_count * sizeof(int));
645                 memset(dir_array, 0, fs->group_desc_count * sizeof(int));
646                 redo_flag++;
647                 goto redo_counts;
648         } else if (fixit == 0)
649                 ext2fs_unmark_valid(fs);
650
651         for (i = 0; i < fs->group_desc_count; i++) {
652                 if (free_array[i] != ext2fs_bg_free_inodes_count(fs, i)) {
653                         pctx.group = i;
654                         pctx.ino = ext2fs_bg_free_inodes_count(fs, i);
655                         pctx.ino2 = free_array[i];
656                         if (fix_problem(ctx, PR_5_FREE_INODE_COUNT_GROUP,
657                                         &pctx)) {
658                                 ext2fs_bg_free_inodes_count_set(fs, i, free_array[i]);
659                                 ext2fs_mark_super_dirty(fs);
660                         } else
661                                 ext2fs_unmark_valid(fs);
662                 }
663                 if (dir_array[i] != ext2fs_bg_used_dirs_count(fs, i)) {
664                         pctx.group = i;
665                         pctx.ino = ext2fs_bg_used_dirs_count(fs, i);
666                         pctx.ino2 = dir_array[i];
667
668                         if (fix_problem(ctx, PR_5_FREE_DIR_COUNT_GROUP,
669                                         &pctx)) {
670                                 ext2fs_bg_used_dirs_count_set(fs, i, dir_array[i]);
671                                 ext2fs_mark_super_dirty(fs);
672                         } else
673                                 ext2fs_unmark_valid(fs);
674                 }
675         }
676         if (free_inodes != fs->super->s_free_inodes_count) {
677                 pctx.group = -1;
678                 pctx.ino = fs->super->s_free_inodes_count;
679                 pctx.ino2 = free_inodes;
680
681                 if (fix_problem(ctx, PR_5_FREE_INODE_COUNT, &pctx)) {
682                         fs->super->s_free_inodes_count = free_inodes;
683                         ext2fs_mark_super_dirty(fs);
684                 } else
685                         ext2fs_unmark_valid(fs);
686         }
687 errout:
688         ext2fs_free_mem(&free_array);
689         ext2fs_free_mem(&dir_array);
690 }
691
692 static void check_inode_end(e2fsck_t ctx)
693 {
694         ext2_filsys fs = ctx->fs;
695         ext2_ino_t      end, save_inodes_count, i;
696         struct problem_context  pctx;
697
698         clear_problem_context(&pctx);
699
700         end = EXT2_INODES_PER_GROUP(fs->super) * fs->group_desc_count;
701         pctx.errcode = ext2fs_fudge_inode_bitmap_end(fs->inode_map, end,
702                                                      &save_inodes_count);
703         if (pctx.errcode) {
704                 pctx.num = 1;
705                 fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx);
706                 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
707                 return;
708         }
709         if (save_inodes_count == end)
710                 return;
711
712         /* protect loop from wrap-around if end is maxed */
713         for (i = save_inodes_count + 1; i <= end && i > save_inodes_count; i++) {
714                 if (!ext2fs_test_inode_bitmap(fs->inode_map, i)) {
715                         if (fix_problem(ctx, PR_5_INODE_BMAP_PADDING, &pctx)) {
716                                 for (; i <= end; i++)
717                                         ext2fs_mark_inode_bitmap(fs->inode_map,
718                                                                  i);
719                                 ext2fs_mark_ib_dirty(fs);
720                         } else
721                                 ext2fs_unmark_valid(fs);
722                         break;
723                 }
724         }
725
726         pctx.errcode = ext2fs_fudge_inode_bitmap_end(fs->inode_map,
727                                                      save_inodes_count, 0);
728         if (pctx.errcode) {
729                 pctx.num = 2;
730                 fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx);
731                 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
732                 return;
733         }
734 }
735
736 static void check_block_end(e2fsck_t ctx)
737 {
738         ext2_filsys fs = ctx->fs;
739         blk64_t end, save_blocks_count, i;
740         struct problem_context  pctx;
741
742         clear_problem_context(&pctx);
743
744         end = ext2fs_get_block_bitmap_start2(fs->block_map) +
745                 ((blk64_t)EXT2_CLUSTERS_PER_GROUP(fs->super) * fs->group_desc_count) - 1;
746         pctx.errcode = ext2fs_fudge_block_bitmap_end2(fs->block_map, end,
747                                                      &save_blocks_count);
748         if (pctx.errcode) {
749                 pctx.num = 3;
750                 fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx);
751                 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
752                 return;
753         }
754         if (save_blocks_count == end)
755                 return;
756
757         /* Protect loop from wrap-around if end is maxed */
758         for (i = save_blocks_count + 1; i <= end && i > save_blocks_count; i++) {
759                 if (!ext2fs_test_block_bitmap2(fs->block_map,
760                                                EXT2FS_C2B(fs, i))) {
761                         if (fix_problem(ctx, PR_5_BLOCK_BMAP_PADDING, &pctx)) {
762                                 for (; i <= end; i++)
763                                         ext2fs_mark_block_bitmap2(fs->block_map,
764                                                         EXT2FS_C2B(fs, i));
765                                 ext2fs_mark_bb_dirty(fs);
766                         } else
767                                 ext2fs_unmark_valid(fs);
768                         break;
769                 }
770         }
771
772         pctx.errcode = ext2fs_fudge_block_bitmap_end2(fs->block_map,
773                                                      save_blocks_count, 0);
774         if (pctx.errcode) {
775                 pctx.num = 4;
776                 fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx);
777                 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
778                 return;
779         }
780 }
781
782
783