Whamcloud - gitweb
misc: fix gcc warnings
[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     redo_flag = 0;
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");
337
338         clear_problem_context(&pctx);
339         free_array = (unsigned int *) e2fsck_allocate_memory(ctx,
340             fs->group_desc_count * sizeof(unsigned int), "free block count array");
341
342         if ((B2C(fs->super->s_first_data_block) <
343              ext2fs_get_block_bitmap_start2(ctx->block_found_map)) ||
344             (B2C(ext2fs_blocks_count(fs->super)-1) >
345              ext2fs_get_block_bitmap_end2(ctx->block_found_map))) {
346                 pctx.num = 1;
347                 pctx.blk = B2C(fs->super->s_first_data_block);
348                 pctx.blk2 = B2C(ext2fs_blocks_count(fs->super) - 1);
349                 pctx.ino = ext2fs_get_block_bitmap_start2(ctx->block_found_map);
350                 pctx.ino2 = ext2fs_get_block_bitmap_end2(ctx->block_found_map);
351                 fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx);
352
353                 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
354                 goto errout;
355         }
356
357         if ((B2C(fs->super->s_first_data_block) <
358              ext2fs_get_block_bitmap_start2(fs->block_map)) ||
359             (B2C(ext2fs_blocks_count(fs->super)-1) >
360              ext2fs_get_block_bitmap_end2(fs->block_map))) {
361                 pctx.num = 2;
362                 pctx.blk = B2C(fs->super->s_first_data_block);
363                 pctx.blk2 = B2C(ext2fs_blocks_count(fs->super) - 1);
364                 pctx.ino = ext2fs_get_block_bitmap_start2(fs->block_map);
365                 pctx.ino2 = ext2fs_get_block_bitmap_end2(fs->block_map);
366                 fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx);
367
368                 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
369                 goto errout;
370         }
371
372 redo_counts:
373         had_problem = 0;
374         save_problem = 0;
375         pctx.blk = pctx.blk2 = NO_BLK;
376         for (i = B2C(fs->super->s_first_data_block);
377              i < ext2fs_blocks_count(fs->super);
378              i += EXT2FS_CLUSTER_RATIO(fs)) {
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
384                 actual = ext2fs_fast_test_block_bitmap2(ctx->block_found_map, i);
385
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;
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;
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;
416                 i += EXT2FS_C2B(fs, fs->super->s_clusters_per_group - 1);
417                 goto next_group;
418         no_optimize:
419
420                 if (redo_flag)
421                         bitmap = actual;
422                 else
423                         bitmap = ext2fs_fast_test_block_bitmap2(fs->block_map, i);
424
425                 if (!actual == !bitmap)
426                         goto do_counts;
427
428                 if (!actual && bitmap) {
429                         /*
430                          * Block not used, but marked in use in the bitmap.
431                          */
432                         problem = PR_5_BLOCK_UNUSED;
433                 } else {
434                         /*
435                          * Block used, but not marked in use in the bitmap.
436                          */
437                         problem = PR_5_BLOCK_USED;
438
439                         if (ext2fs_bg_flags_test(fs, group,
440                                                  EXT2_BG_BLOCK_UNINIT)) {
441                                 struct problem_context pctx2;
442                                 pctx2.blk = i;
443                                 pctx2.group = group;
444                                 if (fix_problem(ctx, PR_5_BLOCK_UNINIT,
445                                                 &pctx2))
446                                         ext2fs_bg_flags_clear(fs, group,
447                                                         EXT2_BG_BLOCK_UNINIT);
448                         }
449                 }
450                 if (pctx.blk == NO_BLK) {
451                         pctx.blk = pctx.blk2 = i;
452                         save_problem = problem;
453                 } else {
454                         if ((problem == save_problem) &&
455                             (pctx.blk2 == i - EXT2FS_CLUSTER_RATIO(fs)))
456                                 pctx.blk2 += EXT2FS_CLUSTER_RATIO(fs);
457                         else {
458                                 print_bitmap_problem(ctx, save_problem, &pctx);
459                                 pctx.blk = pctx.blk2 = i;
460                                 save_problem = problem;
461                         }
462                 }
463                 ctx->flags |= E2F_FLAG_PROG_SUPPRESS;
464                 had_problem++;
465
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
472         do_counts:
473                 if (!bitmap) {
474                         group_free++;
475                         free_blocks++;
476                         if (first_free > i)
477                                 first_free = i;
478                 } else if (i > first_free) {
479                         e2fsck_discard_blocks(ctx, first_free,
480                                               (i - first_free));
481                         first_free = ext2fs_blocks_count(fs->super);
482                 }
483                 blocks ++;
484                 if ((blocks == fs->super->s_clusters_per_group) ||
485                     (EXT2FS_B2C(fs, i) ==
486                      EXT2FS_B2C(fs, ext2fs_blocks_count(fs->super)-1))) {
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);
494                 next_group:
495                         first_free = ext2fs_blocks_count(fs->super);
496
497                         free_array[group] = group_free;
498                         group ++;
499                         blocks = 0;
500                         group_free = 0;
501                         if (ctx->progress)
502                                 if ((ctx->progress)(ctx, 5, group,
503                                                     fs->group_desc_count*2))
504                                         goto errout;
505                 }
506         }
507         if (pctx.blk != NO_BLK)
508                 print_bitmap_problem(ctx, save_problem, &pctx);
509         if (had_problem)
510                 fixit = end_problem_latch(ctx, PR_LATCH_BBITMAP);
511         else
512                 fixit = -1;
513         ctx->flags &= ~E2F_FLAG_PROG_SUPPRESS;
514
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);
519                 if (retval) {
520                         clear_problem_context(&pctx);
521                         fix_problem(ctx, PR_5_COPY_BBITMAP_ERROR, &pctx);
522                         ctx->flags |= E2F_FLAG_ABORT;
523                         goto errout;
524                 }
525                 ext2fs_set_bitmap_padding(fs->block_map);
526                 ext2fs_mark_bb_dirty(fs);
527
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));
531                 redo_flag++;
532                 goto redo_counts;
533         } else if (fixit == 0)
534                 ext2fs_unmark_valid(fs);
535
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];
541
542                         if (fix_problem(ctx, PR_5_FREE_BLOCK_COUNT_GROUP,
543                                         &pctx)) {
544                                 ext2fs_bg_free_blocks_count_set(fs, g, free_array[g]);
545                                 ext2fs_mark_super_dirty(fs);
546                         } else
547                                 ext2fs_unmark_valid(fs);
548                 }
549         }
550         free_blocks = EXT2FS_C2B(fs, free_blocks);
551         if (free_blocks != ext2fs_free_blocks_count(fs->super)) {
552                 pctx.group = 0;
553                 pctx.blk = ext2fs_free_blocks_count(fs->super);
554                 pctx.blk2 = free_blocks;
555
556                 if (fix_problem(ctx, PR_5_FREE_BLOCK_COUNT, &pctx)) {
557                         ext2fs_free_blocks_count_set(fs->super, free_blocks);
558                         ext2fs_mark_super_dirty(fs);
559                 }
560         }
561 errout:
562         ext2fs_free_mem(&free_array);
563         ext2fs_free_mem(&actual_buf);
564         ext2fs_free_mem(&bitmap_buf);
565 }
566
567 static void check_inode_bitmaps(e2fsck_t ctx)
568 {
569         ext2_filsys fs = ctx->fs;
570         ext2_ino_t      i;
571         unsigned int    free_inodes = 0;
572         int             group_free = 0;
573         int             dirs_count = 0;
574         dgrp_t          group = 0;
575         unsigned int    inodes = 0;
576         ext2_ino_t      *free_array;
577         ext2_ino_t      *dir_array;
578         int             actual, bitmap;
579         errcode_t       retval;
580         struct problem_context  pctx;
581         problem_t       problem, save_problem;
582         int             fixit, had_problem;
583         int             csum_flag;
584         int             skip_group = 0;
585         int             redo_flag = 0;
586         ext2_ino_t              first_free = fs->super->s_inodes_per_group + 1;
587
588         clear_problem_context(&pctx);
589         free_array = (ext2_ino_t *) e2fsck_allocate_memory(ctx,
590             fs->group_desc_count * sizeof(ext2_ino_t), "free inode count array");
591
592         dir_array = (ext2_ino_t *) e2fsck_allocate_memory(ctx,
593            fs->group_desc_count * sizeof(ext2_ino_t), "directory count array");
594
595         if ((1 < ext2fs_get_inode_bitmap_start2(ctx->inode_used_map)) ||
596             (fs->super->s_inodes_count >
597              ext2fs_get_inode_bitmap_end2(ctx->inode_used_map))) {
598                 pctx.num = 3;
599                 pctx.blk = 1;
600                 pctx.blk2 = fs->super->s_inodes_count;
601                 pctx.ino = ext2fs_get_inode_bitmap_start2(ctx->inode_used_map);
602                 pctx.ino2 = ext2fs_get_inode_bitmap_end2(ctx->inode_used_map);
603                 fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx);
604
605                 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
606                 goto errout;
607         }
608         if ((1 < ext2fs_get_inode_bitmap_start2(fs->inode_map)) ||
609             (fs->super->s_inodes_count >
610              ext2fs_get_inode_bitmap_end2(fs->inode_map))) {
611                 pctx.num = 4;
612                 pctx.blk = 1;
613                 pctx.blk2 = fs->super->s_inodes_count;
614                 pctx.ino = ext2fs_get_inode_bitmap_start2(fs->inode_map);
615                 pctx.ino2 = ext2fs_get_inode_bitmap_end2(fs->inode_map);
616                 fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx);
617
618                 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
619                 goto errout;
620         }
621
622         csum_flag = ext2fs_has_group_desc_csum(fs);
623 redo_counts:
624         had_problem = 0;
625         save_problem = 0;
626         pctx.ino = pctx.ino2 = 0;
627         if (csum_flag &&
628             (ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)))
629                 skip_group++;
630
631         /* Protect loop from wrap-around if inodes_count is maxed */
632         for (i = 1; i <= fs->super->s_inodes_count && i > 0; i++) {
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                                  */
648                                 first_free = 1;
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
658                 actual = ext2fs_fast_test_inode_bitmap2(ctx->inode_used_map, i);
659                 if (redo_flag)
660                         bitmap = actual;
661                 else if (!skip_group)
662                         bitmap = ext2fs_fast_test_inode_bitmap2(fs->inode_map, i);
663                 if (!actual == !bitmap)
664                         goto do_counts;
665
666                 if (!actual && bitmap) {
667                         /*
668                          * Inode wasn't used, but marked in bitmap
669                          */
670                         problem = PR_5_INODE_UNUSED;
671                 } else /* if (actual && !bitmap) */ {
672                         /*
673                          * Inode used, but not in bitmap
674                          */
675                         problem = PR_5_INODE_USED;
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)){
686                                         ext2fs_bg_flags_clear(fs, group, EXT2_BG_INODE_UNINIT);
687                                         skip_group = 0;
688                                 }
689                         }
690                 }
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                 }
704                 ctx->flags |= E2F_FLAG_PROG_SUPPRESS;
705                 had_problem++;
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;
711
712 do_counts:
713                 inodes++;
714                 if (bitmap) {
715                         if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, i))
716                                 dirs_count++;
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                         }
722                 } else {
723                         group_free++;
724                         free_inodes++;
725                         if (first_free > inodes)
726                                 first_free = inodes;
727                 }
728
729                 if ((inodes == fs->super->s_inodes_per_group) ||
730                     (i == fs->super->s_inodes_count)) {
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);
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) &&
742                             io_channel_discard_zeroes_data(fs->io) &&
743                             !(ext2fs_bg_flags_test(fs, group,
744                                                    EXT2_BG_INODE_ZEROED))) {
745                                 ext2fs_bg_flags_set(fs, group,
746                                                     EXT2_BG_INODE_ZEROED);
747                                 ext2fs_group_desc_csum_set(fs, group);
748                         }
749
750                         first_free = fs->super->s_inodes_per_group + 1;
751                         free_array[group] = group_free;
752                         dir_array[group] = dirs_count;
753                         group ++;
754                         inodes = 0;
755                         skip_group = 0;
756                         group_free = 0;
757                         dirs_count = 0;
758                         if (ctx->progress)
759                                 if ((ctx->progress)(ctx, 5,
760                                             group + fs->group_desc_count,
761                                             fs->group_desc_count*2))
762                                         goto errout;
763                         if (csum_flag &&
764                             (i != fs->super->s_inodes_count) &&
765                             (ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)
766                              ))
767                                 skip_group++;
768                 }
769         }
770         if (pctx.ino)
771                 print_bitmap_problem(ctx, save_problem, &pctx);
772
773         if (had_problem)
774                 fixit = end_problem_latch(ctx, PR_LATCH_IBITMAP);
775         else
776                 fixit = -1;
777         ctx->flags &= ~E2F_FLAG_PROG_SUPPRESS;
778
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);
783                 if (retval) {
784                         clear_problem_context(&pctx);
785                         fix_problem(ctx, PR_5_COPY_IBITMAP_ERROR, &pctx);
786                         ctx->flags |= E2F_FLAG_ABORT;
787                         goto errout;
788                 }
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));
797                 redo_flag++;
798                 goto redo_counts;
799         } else if (fixit == 0)
800                 ext2fs_unmark_valid(fs);
801
802         for (i = 0; i < fs->group_desc_count; i++) {
803                 if (free_array[i] != ext2fs_bg_free_inodes_count(fs, i)) {
804                         pctx.group = i;
805                         pctx.ino = ext2fs_bg_free_inodes_count(fs, i);
806                         pctx.ino2 = free_array[i];
807                         if (fix_problem(ctx, PR_5_FREE_INODE_COUNT_GROUP,
808                                         &pctx)) {
809                                 ext2fs_bg_free_inodes_count_set(fs, i, free_array[i]);
810                                 ext2fs_mark_super_dirty(fs);
811                         } else
812                                 ext2fs_unmark_valid(fs);
813                 }
814                 if (dir_array[i] != ext2fs_bg_used_dirs_count(fs, i)) {
815                         pctx.group = i;
816                         pctx.ino = ext2fs_bg_used_dirs_count(fs, i);
817                         pctx.ino2 = dir_array[i];
818
819                         if (fix_problem(ctx, PR_5_FREE_DIR_COUNT_GROUP,
820                                         &pctx)) {
821                                 ext2fs_bg_used_dirs_count_set(fs, i, dir_array[i]);
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) {
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)) {
833                         fs->super->s_free_inodes_count = free_inodes;
834                         ext2fs_mark_super_dirty(fs);
835                 }
836         }
837 errout:
838         ext2fs_free_mem(&free_array);
839         ext2fs_free_mem(&dir_array);
840 }
841
842 static void check_inode_end(e2fsck_t ctx)
843 {
844         ext2_filsys fs = ctx->fs;
845         ext2_ino_t      end, save_inodes_count, i;
846         struct problem_context  pctx;
847
848         clear_problem_context(&pctx);
849
850         end = EXT2_INODES_PER_GROUP(fs->super) * fs->group_desc_count;
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);
856                 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
857                 return;
858         }
859         if (save_inodes_count == end)
860                 return;
861
862         /* protect loop from wrap-around if end is maxed */
863         for (i = save_inodes_count + 1; i <= end && i > save_inodes_count; i++) {
864                 if (!ext2fs_test_inode_bitmap(fs->inode_map, i)) {
865                         if (fix_problem(ctx, PR_5_INODE_BMAP_PADDING, &pctx)) {
866                                 for (; i <= end; i++)
867                                         ext2fs_mark_inode_bitmap(fs->inode_map,
868                                                                  i);
869                                 ext2fs_mark_ib_dirty(fs);
870                         } else
871                                 ext2fs_unmark_valid(fs);
872                         break;
873                 }
874         }
875
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);
881                 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
882                 return;
883         }
884 }
885
886 static void check_block_end(e2fsck_t ctx)
887 {
888         ext2_filsys fs = ctx->fs;
889         blk64_t end, save_blocks_count, i;
890         struct problem_context  pctx;
891
892         clear_problem_context(&pctx);
893
894         end = ext2fs_get_block_bitmap_start2(fs->block_map) +
895                 EXT2_GROUPS_TO_CLUSTERS(fs->super, fs->group_desc_count) - 1;
896         pctx.errcode = ext2fs_fudge_block_bitmap_end2(fs->block_map, end,
897                                                      &save_blocks_count);
898         if (pctx.errcode) {
899                 pctx.num = 3;
900                 fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx);
901                 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
902                 return;
903         }
904         if (save_blocks_count == end)
905                 return;
906
907         /* Protect loop from wrap-around if end is maxed */
908         for (i = save_blocks_count + 1; i <= end && i > save_blocks_count; i++) {
909                 if (!ext2fs_test_block_bitmap2(fs->block_map,
910                                                EXT2FS_C2B(fs, i))) {
911                         if (fix_problem(ctx, PR_5_BLOCK_BMAP_PADDING, &pctx)) {
912                                 for (; i <= end; i++)
913                                         ext2fs_mark_block_bitmap2(fs->block_map,
914                                                         EXT2FS_C2B(fs, i));
915                                 ext2fs_mark_bb_dirty(fs);
916                         } else
917                                 ext2fs_unmark_valid(fs);
918                         break;
919                 }
920         }
921
922         pctx.errcode = ext2fs_fudge_block_bitmap_end2(fs->block_map,
923                                                      save_blocks_count, 0);
924         if (pctx.errcode) {
925                 pctx.num = 4;
926                 fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx);
927                 ctx->flags |= E2F_FLAG_ABORT; /* fatal */
928                 return;
929         }
930 }
931
932
933