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