Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * pass5.c --- check block and inode bitmaps against on-disk bitmaps |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 3 | * |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 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% |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 10 | * |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
Theodore Ts'o | d1154eb | 2011-09-18 17:34:37 -0400 | [diff] [blame] | 13 | #include "config.h" |
Lukas Czerner | efa1a35 | 2010-11-18 03:38:38 +0000 | [diff] [blame] | 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 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 21 | #include "e2fsck.h" |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 22 | #include "problem.h" |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 23 | |
Lukas Czerner | efa1a35 | 2010-11-18 03:38:38 +0000 | [diff] [blame] | 24 | #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) |
| 25 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 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); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 30 | |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 31 | void e2fsck_pass5(e2fsck_t ctx) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 32 | { |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 33 | #ifdef RESOURCE_TRACK |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 34 | struct resource_track rtrack; |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 35 | #endif |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 36 | struct problem_context pctx; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 37 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 38 | #ifdef MTRACE |
| 39 | mtrace_print("Pass 5"); |
| 40 | #endif |
| 41 | |
Theodore Ts'o | 6d96b00 | 2007-08-03 20:07:09 -0400 | [diff] [blame] | 42 | init_resource_track(&rtrack, ctx->fs->io); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 43 | clear_problem_context(&pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 44 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 45 | if (!(ctx->options & E2F_OPT_PREEN)) |
| 46 | fix_problem(ctx, PR_5_PASS_HEADER, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 47 | |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 48 | if (ctx->progress) |
Theodore Ts'o | efac9a1 | 1998-05-07 05:02:00 +0000 | [diff] [blame] | 49 | if ((ctx->progress)(ctx, 5, 0, ctx->fs->group_desc_count*2)) |
Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 50 | return; |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 51 | |
| 52 | e2fsck_read_bitmaps(ctx); |
| 53 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 54 | check_block_bitmaps(ctx); |
Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 55 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK) |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 56 | return; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 57 | check_inode_bitmaps(ctx); |
Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 58 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK) |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 59 | return; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 60 | check_inode_end(ctx); |
Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 61 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK) |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 62 | return; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 63 | check_block_end(ctx); |
Theodore Ts'o | a02ce9d | 1998-02-24 20:22:23 +0000 | [diff] [blame] | 64 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK) |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 65 | return; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 66 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 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 | |
Ken Chen | 9facd07 | 2009-05-28 09:55:10 -0400 | [diff] [blame] | 74 | print_resource_track(ctx, _("Pass 5"), &rtrack, ctx->fs->io); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Lukas Czerner | 4679532 | 2012-03-11 13:39:45 -0400 | [diff] [blame] | 77 | static void e2fsck_discard_blocks(e2fsck_t ctx, blk64_t start, |
| 78 | blk64_t count) |
Lukas Czerner | efa1a35 | 2010-11-18 03:38:38 +0000 | [diff] [blame] | 79 | { |
| 80 | ext2_filsys fs = ctx->fs; |
Lukas Czerner | efa1a35 | 2010-11-18 03:38:38 +0000 | [diff] [blame] | 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 | */ |
Lukas Czerner | 4679532 | 2012-03-11 13:39:45 -0400 | [diff] [blame] | 88 | if (ext2fs_test_changed(fs)) |
Lukas Czerner | efa1a35 | 2010-11-18 03:38:38 +0000 | [diff] [blame] | 89 | ctx->options &= ~E2F_OPT_DISCARD; |
| 90 | |
Lukas Czerner | f0fe5da | 2012-03-11 15:35:06 -0400 | [diff] [blame] | 91 | if ((ctx->options & E2F_OPT_DISCARD) && |
Lukas Czerner | efa1a35 | 2010-11-18 03:38:38 +0000 | [diff] [blame] | 92 | (io_channel_discard(fs->io, start, count))) |
| 93 | ctx->options &= ~E2F_OPT_DISCARD; |
| 94 | } |
| 95 | |
Lukas Czerner | 57581c1 | 2012-03-11 12:55:45 -0400 | [diff] [blame] | 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 | */ |
Theodore Ts'o | e64e676 | 2012-04-05 12:13:05 -0700 | [diff] [blame] | 102 | static void e2fsck_discard_inodes(e2fsck_t ctx, dgrp_t group, |
| 103 | ext2_ino_t start, int count) |
Lukas Czerner | 57581c1 | 2012-03-11 12:55:45 -0400 | [diff] [blame] | 104 | { |
| 105 | ext2_filsys fs = ctx->fs; |
| 106 | blk64_t blk, num; |
Lukas Czerner | 57581c1 | 2012-03-11 12:55:45 -0400 | [diff] [blame] | 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 | |
Lukas Czerner | c15386c | 2012-03-11 15:36:45 -0400 | [diff] [blame] | 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)) |
Lukas Czerner | 57581c1 | 2012-03-11 12:55:45 -0400 | [diff] [blame] | 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) |
Lukas Czerner | 4679532 | 2012-03-11 13:39:45 -0400 | [diff] [blame] | 149 | e2fsck_discard_blocks(ctx, blk, num); |
Lukas Czerner | 57581c1 | 2012-03-11 12:55:45 -0400 | [diff] [blame] | 150 | } |
| 151 | |
Valerie Aurora Henson | 6dc6439 | 2010-06-13 17:00:00 -0400 | [diff] [blame] | 152 | #define NO_BLK ((blk64_t) -1) |
Theodore Ts'o | f122632 | 2002-03-07 02:47:07 -0500 | [diff] [blame] | 153 | |
Theodore Ts'o | 3c7c6d7 | 2013-12-02 20:52:43 -0500 | [diff] [blame] | 154 | static void print_bitmap_problem(e2fsck_t ctx, problem_t problem, |
Theodore Ts'o | f122632 | 2002-03-07 02:47:07 -0500 | [diff] [blame] | 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 | } |
Brian Behlendorf | 49e2df2 | 2007-03-28 12:07:41 -0400 | [diff] [blame] | 187 | |
Theodore Ts'o | 3385a25 | 2011-06-15 22:15:34 -0400 | [diff] [blame] | 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 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 194 | static void check_block_bitmaps(e2fsck_t ctx) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 195 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 196 | ext2_filsys fs = ctx->fs; |
Jose R. Santos | 20f2ccb | 2009-07-11 21:29:30 -0400 | [diff] [blame] | 197 | blk64_t i; |
Theodore Ts'o | e64e676 | 2012-04-05 12:13:05 -0700 | [diff] [blame] | 198 | unsigned int *free_array; |
Theodore Ts'o | 3971bfe | 2013-12-02 23:21:31 -0500 | [diff] [blame] | 199 | dgrp_t g, group = 0; |
Theodore Ts'o | e64e676 | 2012-04-05 12:13:05 -0700 | [diff] [blame] | 200 | unsigned int blocks = 0; |
Valerie Aurora Henson | 6dc6439 | 2010-06-13 17:00:00 -0400 | [diff] [blame] | 201 | blk64_t free_blocks = 0; |
Lukas Czerner | efa1a35 | 2010-11-18 03:38:38 +0000 | [diff] [blame] | 202 | blk64_t first_free = ext2fs_blocks_count(fs->super); |
Theodore Ts'o | e64e676 | 2012-04-05 12:13:05 -0700 | [diff] [blame] | 203 | unsigned int group_free = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 204 | int actual, bitmap; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 205 | struct problem_context pctx; |
Theodore Ts'o | 3c7c6d7 | 2013-12-02 20:52:43 -0500 | [diff] [blame] | 206 | problem_t problem, save_problem; |
| 207 | int fixit, had_problem; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 208 | errcode_t retval; |
Kazuya Mio | 479463a | 2009-07-09 13:46:59 -0400 | [diff] [blame] | 209 | int old_desc_blocks = 0; |
| 210 | int count = 0; |
| 211 | int cmp_block = 0; |
| 212 | int redo_flag = 0; |
Jose R. Santos | 20f2ccb | 2009-07-11 21:29:30 -0400 | [diff] [blame] | 213 | blk64_t super_blk, old_desc_blk, new_desc_blk; |
Theodore Ts'o | 53e3120 | 2012-11-24 19:17:44 -0500 | [diff] [blame] | 214 | char *actual_buf, *bitmap_buf; |
| 215 | |
| 216 | actual_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize, |
| 217 | "actual bitmap buffer"); |
| 218 | bitmap_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize, |
| 219 | "bitmap block buffer"); |
Brian Behlendorf | 49e2df2 | 2007-03-28 12:07:41 -0400 | [diff] [blame] | 220 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 221 | clear_problem_context(&pctx); |
Theodore Ts'o | e64e676 | 2012-04-05 12:13:05 -0700 | [diff] [blame] | 222 | free_array = (unsigned int *) e2fsck_allocate_memory(ctx, |
| 223 | fs->group_desc_count * sizeof(unsigned int), "free block count array"); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 224 | |
Theodore Ts'o | 3385a25 | 2011-06-15 22:15:34 -0400 | [diff] [blame] | 225 | if ((B2C(fs->super->s_first_data_block) < |
Valerie Aurora Henson | c5d2f50 | 2009-08-22 22:29:02 -0400 | [diff] [blame] | 226 | ext2fs_get_block_bitmap_start2(ctx->block_found_map)) || |
Theodore Ts'o | 3385a25 | 2011-06-15 22:15:34 -0400 | [diff] [blame] | 227 | (B2C(ext2fs_blocks_count(fs->super)-1) > |
Valerie Aurora Henson | c5d2f50 | 2009-08-22 22:29:02 -0400 | [diff] [blame] | 228 | ext2fs_get_block_bitmap_end2(ctx->block_found_map))) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 229 | pctx.num = 1; |
Theodore Ts'o | 3385a25 | 2011-06-15 22:15:34 -0400 | [diff] [blame] | 230 | pctx.blk = B2C(fs->super->s_first_data_block); |
| 231 | pctx.blk2 = B2C(ext2fs_blocks_count(fs->super) - 1); |
Valerie Aurora Henson | c5d2f50 | 2009-08-22 22:29:02 -0400 | [diff] [blame] | 232 | pctx.ino = ext2fs_get_block_bitmap_start2(ctx->block_found_map); |
| 233 | pctx.ino2 = ext2fs_get_block_bitmap_end2(ctx->block_found_map); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 234 | fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 235 | |
| 236 | ctx->flags |= E2F_FLAG_ABORT; /* fatal */ |
Brian Behlendorf | 49e2df2 | 2007-03-28 12:07:41 -0400 | [diff] [blame] | 237 | goto errout; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 238 | } |
Brian Behlendorf | 49e2df2 | 2007-03-28 12:07:41 -0400 | [diff] [blame] | 239 | |
Theodore Ts'o | 3385a25 | 2011-06-15 22:15:34 -0400 | [diff] [blame] | 240 | if ((B2C(fs->super->s_first_data_block) < |
Valerie Aurora Henson | c5d2f50 | 2009-08-22 22:29:02 -0400 | [diff] [blame] | 241 | ext2fs_get_block_bitmap_start2(fs->block_map)) || |
Theodore Ts'o | 3385a25 | 2011-06-15 22:15:34 -0400 | [diff] [blame] | 242 | (B2C(ext2fs_blocks_count(fs->super)-1) > |
Valerie Aurora Henson | c5d2f50 | 2009-08-22 22:29:02 -0400 | [diff] [blame] | 243 | ext2fs_get_block_bitmap_end2(fs->block_map))) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 244 | pctx.num = 2; |
Theodore Ts'o | 3385a25 | 2011-06-15 22:15:34 -0400 | [diff] [blame] | 245 | pctx.blk = B2C(fs->super->s_first_data_block); |
| 246 | pctx.blk2 = B2C(ext2fs_blocks_count(fs->super) - 1); |
Valerie Aurora Henson | c5d2f50 | 2009-08-22 22:29:02 -0400 | [diff] [blame] | 247 | pctx.ino = ext2fs_get_block_bitmap_start2(fs->block_map); |
| 248 | pctx.ino2 = ext2fs_get_block_bitmap_end2(fs->block_map); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 249 | fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 250 | |
| 251 | ctx->flags |= E2F_FLAG_ABORT; /* fatal */ |
Brian Behlendorf | 49e2df2 | 2007-03-28 12:07:41 -0400 | [diff] [blame] | 252 | goto errout; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 253 | } |
Brian Behlendorf | 49e2df2 | 2007-03-28 12:07:41 -0400 | [diff] [blame] | 254 | |
Theodore Ts'o | 63c4969 | 1998-02-20 05:24:59 +0000 | [diff] [blame] | 255 | redo_counts: |
| 256 | had_problem = 0; |
Theodore Ts'o | f122632 | 2002-03-07 02:47:07 -0500 | [diff] [blame] | 257 | save_problem = 0; |
| 258 | pctx.blk = pctx.blk2 = NO_BLK; |
Theodore Ts'o | 3385a25 | 2011-06-15 22:15:34 -0400 | [diff] [blame] | 259 | for (i = B2C(fs->super->s_first_data_block); |
Valerie Aurora Henson | 4efbac6 | 2009-09-07 20:46:34 -0400 | [diff] [blame] | 260 | i < ext2fs_blocks_count(fs->super); |
Theodore Ts'o | 44fe08f | 2011-06-10 18:58:16 -0400 | [diff] [blame] | 261 | i += EXT2FS_CLUSTER_RATIO(fs)) { |
Theodore Ts'o | 53e3120 | 2012-11-24 19:17:44 -0500 | [diff] [blame] | 262 | int first_block_in_bg = (B2C(i) - |
| 263 | B2C(fs->super->s_first_data_block)) % |
| 264 | fs->super->s_clusters_per_group == 0; |
| 265 | int n, nbytes = fs->super->s_clusters_per_group / 8; |
| 266 | |
Valerie Aurora Henson | c5d2f50 | 2009-08-22 22:29:02 -0400 | [diff] [blame] | 267 | actual = ext2fs_fast_test_block_bitmap2(ctx->block_found_map, i); |
Theodore Ts'o | f5fa200 | 2006-05-08 20:17:26 -0400 | [diff] [blame] | 268 | |
Theodore Ts'o | 53e3120 | 2012-11-24 19:17:44 -0500 | [diff] [blame] | 269 | /* |
| 270 | * Try to optimize pass5 by extracting a bitmap block |
| 271 | * as expected from what we have on disk, and then |
| 272 | * comparing the two. If they are identical, then |
| 273 | * update the free block counts and go on to the next |
| 274 | * block group. This is much faster than doing the |
| 275 | * individual bit-by-bit comparison. The one downside |
| 276 | * is that this doesn't work if we are asking e2fsck |
| 277 | * to do a discard operation. |
| 278 | */ |
| 279 | if (!first_block_in_bg || |
| 280 | (group == (int)fs->group_desc_count - 1) || |
| 281 | (ctx->options & E2F_OPT_DISCARD)) |
| 282 | goto no_optimize; |
| 283 | |
| 284 | retval = ext2fs_get_block_bitmap_range2(ctx->block_found_map, |
| 285 | B2C(i), fs->super->s_clusters_per_group, |
| 286 | actual_buf); |
| 287 | if (retval) |
| 288 | goto no_optimize; |
Darrick J. Wong | a48da27 | 2014-01-11 14:05:02 -0500 | [diff] [blame] | 289 | retval = ext2fs_get_block_bitmap_range2(fs->block_map, |
| 290 | B2C(i), fs->super->s_clusters_per_group, |
| 291 | bitmap_buf); |
| 292 | if (retval) |
| 293 | goto no_optimize; |
Theodore Ts'o | 53e3120 | 2012-11-24 19:17:44 -0500 | [diff] [blame] | 294 | if (memcmp(actual_buf, bitmap_buf, nbytes) != 0) |
| 295 | goto no_optimize; |
| 296 | n = ext2fs_bitcount(actual_buf, nbytes); |
| 297 | group_free = fs->super->s_clusters_per_group - n; |
| 298 | free_blocks += group_free; |
Theodore Ts'o | c7e2932 | 2012-12-15 22:32:23 -0500 | [diff] [blame] | 299 | i += EXT2FS_C2B(fs, fs->super->s_clusters_per_group - 1); |
Theodore Ts'o | 53e3120 | 2012-11-24 19:17:44 -0500 | [diff] [blame] | 300 | goto next_group; |
| 301 | no_optimize: |
| 302 | |
Darrick J. Wong | a48da27 | 2014-01-11 14:05:02 -0500 | [diff] [blame] | 303 | if (redo_flag) |
Kazuya Mio | 479463a | 2009-07-09 13:46:59 -0400 | [diff] [blame] | 304 | bitmap = actual; |
| 305 | else |
Valerie Aurora Henson | c5d2f50 | 2009-08-22 22:29:02 -0400 | [diff] [blame] | 306 | bitmap = ext2fs_fast_test_block_bitmap2(fs->block_map, i); |
Brian Behlendorf | 49e2df2 | 2007-03-28 12:07:41 -0400 | [diff] [blame] | 307 | |
Theodore Ts'o | e35ff9b | 2011-12-17 11:56:35 -0500 | [diff] [blame] | 308 | if (!actual == !bitmap) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 309 | goto do_counts; |
Theodore Ts'o | f5fa200 | 2006-05-08 20:17:26 -0400 | [diff] [blame] | 310 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 311 | if (!actual && bitmap) { |
| 312 | /* |
| 313 | * Block not used, but marked in use in the bitmap. |
| 314 | */ |
Theodore Ts'o | f122632 | 2002-03-07 02:47:07 -0500 | [diff] [blame] | 315 | problem = PR_5_BLOCK_UNUSED; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 316 | } else { |
| 317 | /* |
| 318 | * Block used, but not marked in use in the bitmap. |
| 319 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 320 | problem = PR_5_BLOCK_USED; |
Jose R. Santos | 49a7360 | 2007-10-21 21:04:03 -0500 | [diff] [blame] | 321 | |
Darrick J. Wong | a48da27 | 2014-01-11 14:05:02 -0500 | [diff] [blame] | 322 | if (ext2fs_bg_flags_test(fs, group, |
| 323 | EXT2_BG_BLOCK_UNINIT)) { |
Jose R. Santos | 49a7360 | 2007-10-21 21:04:03 -0500 | [diff] [blame] | 324 | struct problem_context pctx2; |
| 325 | pctx2.blk = i; |
| 326 | pctx2.group = group; |
Darrick J. Wong | a48da27 | 2014-01-11 14:05:02 -0500 | [diff] [blame] | 327 | if (fix_problem(ctx, PR_5_BLOCK_UNINIT, |
| 328 | &pctx2)) |
| 329 | ext2fs_bg_flags_clear(fs, group, |
| 330 | EXT2_BG_BLOCK_UNINIT); |
Jose R. Santos | 49a7360 | 2007-10-21 21:04:03 -0500 | [diff] [blame] | 331 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 332 | } |
Theodore Ts'o | f122632 | 2002-03-07 02:47:07 -0500 | [diff] [blame] | 333 | if (pctx.blk == NO_BLK) { |
| 334 | pctx.blk = pctx.blk2 = i; |
| 335 | save_problem = problem; |
| 336 | } else { |
| 337 | if ((problem == save_problem) && |
Darrick J. Wong | 69beadc | 2013-12-15 23:53:32 -0500 | [diff] [blame] | 338 | (pctx.blk2 == i - EXT2FS_CLUSTER_RATIO(fs))) |
| 339 | pctx.blk2 += EXT2FS_CLUSTER_RATIO(fs); |
Theodore Ts'o | f122632 | 2002-03-07 02:47:07 -0500 | [diff] [blame] | 340 | else { |
| 341 | print_bitmap_problem(ctx, save_problem, &pctx); |
| 342 | pctx.blk = pctx.blk2 = i; |
| 343 | save_problem = problem; |
| 344 | } |
| 345 | } |
Theodore Ts'o | 5596def | 1999-07-19 15:27:37 +0000 | [diff] [blame] | 346 | ctx->flags |= E2F_FLAG_PROG_SUPPRESS; |
Theodore Ts'o | 63c4969 | 1998-02-20 05:24:59 +0000 | [diff] [blame] | 347 | had_problem++; |
Brian Behlendorf | 49e2df2 | 2007-03-28 12:07:41 -0400 | [diff] [blame] | 348 | |
Lukas Czerner | efa1a35 | 2010-11-18 03:38:38 +0000 | [diff] [blame] | 349 | /* |
| 350 | * If there a problem we should turn off the discard so we |
| 351 | * do not compromise the filesystem. |
| 352 | */ |
| 353 | ctx->options &= ~E2F_OPT_DISCARD; |
| 354 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 355 | do_counts: |
Theodore Ts'o | d2c9c42 | 2012-03-11 14:15:19 -0400 | [diff] [blame] | 356 | if (!bitmap) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 357 | group_free++; |
| 358 | free_blocks++; |
Lukas Czerner | efa1a35 | 2010-11-18 03:38:38 +0000 | [diff] [blame] | 359 | if (first_free > i) |
| 360 | first_free = i; |
Theodore Ts'o | d2c9c42 | 2012-03-11 14:15:19 -0400 | [diff] [blame] | 361 | } else if (i > first_free) { |
| 362 | e2fsck_discard_blocks(ctx, first_free, |
| 363 | (i - first_free)); |
Lukas Czerner | efa1a35 | 2010-11-18 03:38:38 +0000 | [diff] [blame] | 364 | first_free = ext2fs_blocks_count(fs->super); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 365 | } |
| 366 | blocks ++; |
Theodore Ts'o | 44fe08f | 2011-06-10 18:58:16 -0400 | [diff] [blame] | 367 | if ((blocks == fs->super->s_clusters_per_group) || |
| 368 | (EXT2FS_B2C(fs, i) == |
| 369 | EXT2FS_B2C(fs, ext2fs_blocks_count(fs->super)-1))) { |
Lukas Czerner | deae60a | 2012-03-05 08:49:34 +0100 | [diff] [blame] | 370 | /* |
| 371 | * If the last block of this group is free, then we can |
| 372 | * discard it as well. |
| 373 | */ |
| 374 | if (!bitmap && i >= first_free) |
| 375 | e2fsck_discard_blocks(ctx, first_free, |
| 376 | (i - first_free) + 1); |
Theodore Ts'o | 53e3120 | 2012-11-24 19:17:44 -0500 | [diff] [blame] | 377 | next_group: |
Lukas Czerner | deae60a | 2012-03-05 08:49:34 +0100 | [diff] [blame] | 378 | first_free = ext2fs_blocks_count(fs->super); |
| 379 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 380 | free_array[group] = group_free; |
| 381 | group ++; |
| 382 | blocks = 0; |
| 383 | group_free = 0; |
Theodore Ts'o | efac9a1 | 1998-05-07 05:02:00 +0000 | [diff] [blame] | 384 | if (ctx->progress) |
| 385 | if ((ctx->progress)(ctx, 5, group, |
| 386 | fs->group_desc_count*2)) |
Brian Behlendorf | 49e2df2 | 2007-03-28 12:07:41 -0400 | [diff] [blame] | 387 | goto errout; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 388 | } |
| 389 | } |
Theodore Ts'o | f122632 | 2002-03-07 02:47:07 -0500 | [diff] [blame] | 390 | if (pctx.blk != NO_BLK) |
| 391 | print_bitmap_problem(ctx, save_problem, &pctx); |
Theodore Ts'o | 63c4969 | 1998-02-20 05:24:59 +0000 | [diff] [blame] | 392 | if (had_problem) |
Theodore Ts'o | f122632 | 2002-03-07 02:47:07 -0500 | [diff] [blame] | 393 | fixit = end_problem_latch(ctx, PR_LATCH_BBITMAP); |
Theodore Ts'o | 63c4969 | 1998-02-20 05:24:59 +0000 | [diff] [blame] | 394 | else |
| 395 | fixit = -1; |
Theodore Ts'o | 5596def | 1999-07-19 15:27:37 +0000 | [diff] [blame] | 396 | ctx->flags &= ~E2F_FLAG_PROG_SUPPRESS; |
Brian Behlendorf | 49e2df2 | 2007-03-28 12:07:41 -0400 | [diff] [blame] | 397 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 398 | if (fixit == 1) { |
| 399 | ext2fs_free_block_bitmap(fs->block_map); |
| 400 | retval = ext2fs_copy_bitmap(ctx->block_found_map, |
| 401 | &fs->block_map); |
Theodore Ts'o | bbd47d7 | 2000-06-10 19:21:33 +0000 | [diff] [blame] | 402 | if (retval) { |
| 403 | clear_problem_context(&pctx); |
| 404 | fix_problem(ctx, PR_5_COPY_BBITMAP_ERROR, &pctx); |
| 405 | ctx->flags |= E2F_FLAG_ABORT; |
Brian Behlendorf | 49e2df2 | 2007-03-28 12:07:41 -0400 | [diff] [blame] | 406 | goto errout; |
Theodore Ts'o | bbd47d7 | 2000-06-10 19:21:33 +0000 | [diff] [blame] | 407 | } |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 408 | ext2fs_set_bitmap_padding(fs->block_map); |
| 409 | ext2fs_mark_bb_dirty(fs); |
Brian Behlendorf | 49e2df2 | 2007-03-28 12:07:41 -0400 | [diff] [blame] | 410 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 411 | /* Redo the counts */ |
| 412 | blocks = 0; free_blocks = 0; group_free = 0; group = 0; |
| 413 | memset(free_array, 0, fs->group_desc_count * sizeof(int)); |
Kazuya Mio | 479463a | 2009-07-09 13:46:59 -0400 | [diff] [blame] | 414 | redo_flag++; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 415 | goto redo_counts; |
| 416 | } else if (fixit == 0) |
| 417 | ext2fs_unmark_valid(fs); |
| 418 | |
Theodore Ts'o | 3971bfe | 2013-12-02 23:21:31 -0500 | [diff] [blame] | 419 | for (g = 0; g < fs->group_desc_count; g++) { |
| 420 | if (free_array[g] != ext2fs_bg_free_blocks_count(fs, g)) { |
| 421 | pctx.group = g; |
| 422 | pctx.blk = ext2fs_bg_free_blocks_count(fs, g); |
| 423 | pctx.blk2 = free_array[g]; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 424 | |
| 425 | if (fix_problem(ctx, PR_5_FREE_BLOCK_COUNT_GROUP, |
| 426 | &pctx)) { |
Theodore Ts'o | 3971bfe | 2013-12-02 23:21:31 -0500 | [diff] [blame] | 427 | ext2fs_bg_free_blocks_count_set(fs, g, free_array[g]); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 428 | ext2fs_mark_super_dirty(fs); |
| 429 | } else |
| 430 | ext2fs_unmark_valid(fs); |
| 431 | } |
| 432 | } |
Theodore Ts'o | fe75afb | 2011-06-16 01:38:43 -0400 | [diff] [blame] | 433 | free_blocks = EXT2FS_C2B(fs, free_blocks); |
Valerie Aurora Henson | 4efbac6 | 2009-09-07 20:46:34 -0400 | [diff] [blame] | 434 | if (free_blocks != ext2fs_free_blocks_count(fs->super)) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 435 | pctx.group = 0; |
Valerie Aurora Henson | 4efbac6 | 2009-09-07 20:46:34 -0400 | [diff] [blame] | 436 | pctx.blk = ext2fs_free_blocks_count(fs->super); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 437 | pctx.blk2 = free_blocks; |
| 438 | |
| 439 | if (fix_problem(ctx, PR_5_FREE_BLOCK_COUNT, &pctx)) { |
Valerie Aurora Henson | 4efbac6 | 2009-09-07 20:46:34 -0400 | [diff] [blame] | 440 | ext2fs_free_blocks_count_set(fs->super, free_blocks); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 441 | ext2fs_mark_super_dirty(fs); |
Andreas Dilger | 2788cc8 | 2012-04-24 16:22:48 -0400 | [diff] [blame] | 442 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 443 | } |
Brian Behlendorf | 49e2df2 | 2007-03-28 12:07:41 -0400 | [diff] [blame] | 444 | errout: |
Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 445 | ext2fs_free_mem(&free_array); |
Theodore Ts'o | 53e3120 | 2012-11-24 19:17:44 -0500 | [diff] [blame] | 446 | ext2fs_free_mem(&actual_buf); |
| 447 | ext2fs_free_mem(&bitmap_buf); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 448 | } |
Brian Behlendorf | 49e2df2 | 2007-03-28 12:07:41 -0400 | [diff] [blame] | 449 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 450 | static void check_inode_bitmaps(e2fsck_t ctx) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 451 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 452 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 86c627e | 2001-01-11 15:12:14 +0000 | [diff] [blame] | 453 | ext2_ino_t i; |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 454 | unsigned int free_inodes = 0; |
| 455 | int group_free = 0; |
| 456 | int dirs_count = 0; |
Theodore Ts'o | 3971bfe | 2013-12-02 23:21:31 -0500 | [diff] [blame] | 457 | dgrp_t group = 0; |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 458 | unsigned int inodes = 0; |
Theodore Ts'o | e64e676 | 2012-04-05 12:13:05 -0700 | [diff] [blame] | 459 | ext2_ino_t *free_array; |
| 460 | ext2_ino_t *dir_array; |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 461 | int actual, bitmap; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 462 | errcode_t retval; |
| 463 | struct problem_context pctx; |
Theodore Ts'o | 3c7c6d7 | 2013-12-02 20:52:43 -0500 | [diff] [blame] | 464 | problem_t problem, save_problem; |
| 465 | int fixit, had_problem; |
Theodore Ts'o | 16b851c | 2008-04-20 23:33:34 -0400 | [diff] [blame] | 466 | int csum_flag; |
Theodore Ts'o | f5fa200 | 2006-05-08 20:17:26 -0400 | [diff] [blame] | 467 | int skip_group = 0; |
Kazuya Mio | 479463a | 2009-07-09 13:46:59 -0400 | [diff] [blame] | 468 | int redo_flag = 0; |
Theodore Ts'o | e64e676 | 2012-04-05 12:13:05 -0700 | [diff] [blame] | 469 | ext2_ino_t first_free = fs->super->s_inodes_per_group + 1; |
Brian Behlendorf | 49e2df2 | 2007-03-28 12:07:41 -0400 | [diff] [blame] | 470 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 471 | clear_problem_context(&pctx); |
Theodore Ts'o | e64e676 | 2012-04-05 12:13:05 -0700 | [diff] [blame] | 472 | free_array = (ext2_ino_t *) e2fsck_allocate_memory(ctx, |
| 473 | fs->group_desc_count * sizeof(ext2_ino_t), "free inode count array"); |
Brian Behlendorf | 49e2df2 | 2007-03-28 12:07:41 -0400 | [diff] [blame] | 474 | |
Theodore Ts'o | e64e676 | 2012-04-05 12:13:05 -0700 | [diff] [blame] | 475 | dir_array = (ext2_ino_t *) e2fsck_allocate_memory(ctx, |
| 476 | fs->group_desc_count * sizeof(ext2_ino_t), "directory count array"); |
Brian Behlendorf | 49e2df2 | 2007-03-28 12:07:41 -0400 | [diff] [blame] | 477 | |
Valerie Aurora Henson | c5d2f50 | 2009-08-22 22:29:02 -0400 | [diff] [blame] | 478 | if ((1 < ext2fs_get_inode_bitmap_start2(ctx->inode_used_map)) || |
Brian Behlendorf | 49e2df2 | 2007-03-28 12:07:41 -0400 | [diff] [blame] | 479 | (fs->super->s_inodes_count > |
Valerie Aurora Henson | c5d2f50 | 2009-08-22 22:29:02 -0400 | [diff] [blame] | 480 | ext2fs_get_inode_bitmap_end2(ctx->inode_used_map))) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 481 | pctx.num = 3; |
| 482 | pctx.blk = 1; |
| 483 | pctx.blk2 = fs->super->s_inodes_count; |
Valerie Aurora Henson | c5d2f50 | 2009-08-22 22:29:02 -0400 | [diff] [blame] | 484 | pctx.ino = ext2fs_get_inode_bitmap_start2(ctx->inode_used_map); |
| 485 | pctx.ino2 = ext2fs_get_inode_bitmap_end2(ctx->inode_used_map); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 486 | fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 487 | |
| 488 | ctx->flags |= E2F_FLAG_ABORT; /* fatal */ |
Brian Behlendorf | 49e2df2 | 2007-03-28 12:07:41 -0400 | [diff] [blame] | 489 | goto errout; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 490 | } |
Valerie Aurora Henson | c5d2f50 | 2009-08-22 22:29:02 -0400 | [diff] [blame] | 491 | if ((1 < ext2fs_get_inode_bitmap_start2(fs->inode_map)) || |
Brian Behlendorf | 49e2df2 | 2007-03-28 12:07:41 -0400 | [diff] [blame] | 492 | (fs->super->s_inodes_count > |
Valerie Aurora Henson | c5d2f50 | 2009-08-22 22:29:02 -0400 | [diff] [blame] | 493 | ext2fs_get_inode_bitmap_end2(fs->inode_map))) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 494 | pctx.num = 4; |
| 495 | pctx.blk = 1; |
| 496 | pctx.blk2 = fs->super->s_inodes_count; |
Valerie Aurora Henson | c5d2f50 | 2009-08-22 22:29:02 -0400 | [diff] [blame] | 497 | pctx.ino = ext2fs_get_inode_bitmap_start2(fs->inode_map); |
| 498 | pctx.ino2 = ext2fs_get_inode_bitmap_end2(fs->inode_map); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 499 | fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 500 | |
| 501 | ctx->flags |= E2F_FLAG_ABORT; /* fatal */ |
Brian Behlendorf | 49e2df2 | 2007-03-28 12:07:41 -0400 | [diff] [blame] | 502 | goto errout; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Jose R. Santos | 49a7360 | 2007-10-21 21:04:03 -0500 | [diff] [blame] | 505 | csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(fs->super, |
| 506 | EXT4_FEATURE_RO_COMPAT_GDT_CSUM); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 507 | redo_counts: |
Theodore Ts'o | 63c4969 | 1998-02-20 05:24:59 +0000 | [diff] [blame] | 508 | had_problem = 0; |
Theodore Ts'o | f122632 | 2002-03-07 02:47:07 -0500 | [diff] [blame] | 509 | save_problem = 0; |
| 510 | pctx.ino = pctx.ino2 = 0; |
Theodore Ts'o | 16b851c | 2008-04-20 23:33:34 -0400 | [diff] [blame] | 511 | if (csum_flag && |
Theodore Ts'o | cd65a24 | 2009-10-25 21:42:12 -0400 | [diff] [blame] | 512 | (ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT))) |
Theodore Ts'o | f5fa200 | 2006-05-08 20:17:26 -0400 | [diff] [blame] | 513 | skip_group++; |
| 514 | |
Eric Sandeen | 5830d6b | 2006-08-30 02:16:55 -0400 | [diff] [blame] | 515 | /* Protect loop from wrap-around if inodes_count is maxed */ |
| 516 | for (i = 1; i <= fs->super->s_inodes_count && i > 0; i++) { |
Kazuya Mio | 479463a | 2009-07-09 13:46:59 -0400 | [diff] [blame] | 517 | bitmap = 0; |
| 518 | if (skip_group && |
| 519 | i % fs->super->s_inodes_per_group == 1) { |
| 520 | /* |
| 521 | * Current inode is the first inode |
| 522 | * in the current block group. |
| 523 | */ |
| 524 | if (ext2fs_test_inode_bitmap_range( |
| 525 | ctx->inode_used_map, i, |
| 526 | fs->super->s_inodes_per_group)) { |
| 527 | /* |
| 528 | * When the compared inodes in inodes bitmap |
| 529 | * are 0, count the free inode, |
| 530 | * skip the current block group. |
| 531 | */ |
Lukas Czerner | 57581c1 | 2012-03-11 12:55:45 -0400 | [diff] [blame] | 532 | first_free = 1; |
Kazuya Mio | 479463a | 2009-07-09 13:46:59 -0400 | [diff] [blame] | 533 | inodes = fs->super->s_inodes_per_group - 1; |
| 534 | group_free = inodes; |
| 535 | free_inodes += inodes; |
| 536 | i += inodes; |
| 537 | skip_group = 0; |
| 538 | goto do_counts; |
| 539 | } |
| 540 | } |
| 541 | |
Valerie Aurora Henson | c5d2f50 | 2009-08-22 22:29:02 -0400 | [diff] [blame] | 542 | actual = ext2fs_fast_test_inode_bitmap2(ctx->inode_used_map, i); |
Kazuya Mio | 479463a | 2009-07-09 13:46:59 -0400 | [diff] [blame] | 543 | if (redo_flag) |
| 544 | bitmap = actual; |
| 545 | else if (!skip_group) |
Valerie Aurora Henson | c5d2f50 | 2009-08-22 22:29:02 -0400 | [diff] [blame] | 546 | bitmap = ext2fs_fast_test_inode_bitmap2(fs->inode_map, i); |
Theodore Ts'o | e35ff9b | 2011-12-17 11:56:35 -0500 | [diff] [blame] | 547 | if (!actual == !bitmap) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 548 | goto do_counts; |
Brian Behlendorf | 49e2df2 | 2007-03-28 12:07:41 -0400 | [diff] [blame] | 549 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 550 | if (!actual && bitmap) { |
| 551 | /* |
| 552 | * Inode wasn't used, but marked in bitmap |
| 553 | */ |
Theodore Ts'o | f122632 | 2002-03-07 02:47:07 -0500 | [diff] [blame] | 554 | problem = PR_5_INODE_UNUSED; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 555 | } else /* if (actual && !bitmap) */ { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 556 | /* |
| 557 | * Inode used, but not in bitmap |
| 558 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 559 | problem = PR_5_INODE_USED; |
Jose R. Santos | 49a7360 | 2007-10-21 21:04:03 -0500 | [diff] [blame] | 560 | |
| 561 | /* We should never hit this, because it means that |
| 562 | * inodes were marked in use that weren't noticed |
| 563 | * in pass1 or pass 2. It is easier to fix the problem |
| 564 | * than to kill e2fsck and leave the user stuck. */ |
| 565 | if (skip_group) { |
| 566 | struct problem_context pctx2; |
| 567 | pctx2.blk = i; |
| 568 | pctx2.group = group; |
| 569 | if (fix_problem(ctx, PR_5_INODE_UNINIT,&pctx2)){ |
Eric Sandeen | e633b58 | 2009-10-25 21:41:32 -0400 | [diff] [blame] | 570 | ext2fs_bg_flags_clear(fs, group, EXT2_BG_INODE_UNINIT); |
Jose R. Santos | 49a7360 | 2007-10-21 21:04:03 -0500 | [diff] [blame] | 571 | skip_group = 0; |
| 572 | } |
| 573 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 574 | } |
Theodore Ts'o | f122632 | 2002-03-07 02:47:07 -0500 | [diff] [blame] | 575 | if (pctx.ino == 0) { |
| 576 | pctx.ino = pctx.ino2 = i; |
| 577 | save_problem = problem; |
| 578 | } else { |
| 579 | if ((problem == save_problem) && |
| 580 | (pctx.ino2 == i-1)) |
| 581 | pctx.ino2++; |
| 582 | else { |
| 583 | print_bitmap_problem(ctx, save_problem, &pctx); |
| 584 | pctx.ino = pctx.ino2 = i; |
| 585 | save_problem = problem; |
| 586 | } |
| 587 | } |
Theodore Ts'o | 5596def | 1999-07-19 15:27:37 +0000 | [diff] [blame] | 588 | ctx->flags |= E2F_FLAG_PROG_SUPPRESS; |
Theodore Ts'o | 63c4969 | 1998-02-20 05:24:59 +0000 | [diff] [blame] | 589 | had_problem++; |
Lukas Czerner | efa1a35 | 2010-11-18 03:38:38 +0000 | [diff] [blame] | 590 | /* |
| 591 | * If there a problem we should turn off the discard so we |
| 592 | * do not compromise the filesystem. |
| 593 | */ |
| 594 | ctx->options &= ~E2F_OPT_DISCARD; |
Brian Behlendorf | 49e2df2 | 2007-03-28 12:07:41 -0400 | [diff] [blame] | 595 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 596 | do_counts: |
Lukas Czerner | 57581c1 | 2012-03-11 12:55:45 -0400 | [diff] [blame] | 597 | inodes++; |
Theodore Ts'o | f5fa200 | 2006-05-08 20:17:26 -0400 | [diff] [blame] | 598 | if (bitmap) { |
Valerie Aurora Henson | c5d2f50 | 2009-08-22 22:29:02 -0400 | [diff] [blame] | 599 | if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, i)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 600 | dirs_count++; |
Lukas Czerner | 57581c1 | 2012-03-11 12:55:45 -0400 | [diff] [blame] | 601 | if (inodes > first_free) { |
| 602 | e2fsck_discard_inodes(ctx, group, first_free, |
| 603 | inodes - first_free); |
| 604 | first_free = fs->super->s_inodes_per_group + 1; |
| 605 | } |
Theodore Ts'o | d2c9c42 | 2012-03-11 14:15:19 -0400 | [diff] [blame] | 606 | } else { |
Theodore Ts'o | f5fa200 | 2006-05-08 20:17:26 -0400 | [diff] [blame] | 607 | group_free++; |
| 608 | free_inodes++; |
Lukas Czerner | 57581c1 | 2012-03-11 12:55:45 -0400 | [diff] [blame] | 609 | if (first_free > inodes) |
| 610 | first_free = inodes; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 611 | } |
Lukas Czerner | efa1a35 | 2010-11-18 03:38:38 +0000 | [diff] [blame] | 612 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 613 | if ((inodes == fs->super->s_inodes_per_group) || |
| 614 | (i == fs->super->s_inodes_count)) { |
Lukas Czerner | 57581c1 | 2012-03-11 12:55:45 -0400 | [diff] [blame] | 615 | /* |
| 616 | * If the last inode is free, we can discard it as well. |
| 617 | */ |
| 618 | if (!bitmap && inodes >= first_free) |
| 619 | e2fsck_discard_inodes(ctx, group, first_free, |
| 620 | inodes - first_free + 1); |
Lukas Czerner | efa1a35 | 2010-11-18 03:38:38 +0000 | [diff] [blame] | 621 | /* |
| 622 | * If discard zeroes data and the group inode table |
| 623 | * was not zeroed yet, set itable as zeroed |
| 624 | */ |
| 625 | if ((ctx->options & E2F_OPT_DISCARD) && |
Lukas Czerner | 57581c1 | 2012-03-11 12:55:45 -0400 | [diff] [blame] | 626 | io_channel_discard_zeroes_data(fs->io) && |
Lukas Czerner | efa1a35 | 2010-11-18 03:38:38 +0000 | [diff] [blame] | 627 | !(ext2fs_bg_flags_test(fs, group, |
Lukas Czerner | 57581c1 | 2012-03-11 12:55:45 -0400 | [diff] [blame] | 628 | EXT2_BG_INODE_ZEROED))) { |
Lukas Czerner | efa1a35 | 2010-11-18 03:38:38 +0000 | [diff] [blame] | 629 | ext2fs_bg_flags_set(fs, group, |
| 630 | EXT2_BG_INODE_ZEROED); |
| 631 | ext2fs_group_desc_csum_set(fs, group); |
| 632 | } |
| 633 | |
Lukas Czerner | 57581c1 | 2012-03-11 12:55:45 -0400 | [diff] [blame] | 634 | first_free = fs->super->s_inodes_per_group + 1; |
| 635 | free_array[group] = group_free; |
| 636 | dir_array[group] = dirs_count; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 637 | group ++; |
| 638 | inodes = 0; |
Theodore Ts'o | f5fa200 | 2006-05-08 20:17:26 -0400 | [diff] [blame] | 639 | skip_group = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 640 | group_free = 0; |
| 641 | dirs_count = 0; |
Theodore Ts'o | efac9a1 | 1998-05-07 05:02:00 +0000 | [diff] [blame] | 642 | if (ctx->progress) |
| 643 | if ((ctx->progress)(ctx, 5, |
| 644 | group + fs->group_desc_count, |
| 645 | fs->group_desc_count*2)) |
Brian Behlendorf | 49e2df2 | 2007-03-28 12:07:41 -0400 | [diff] [blame] | 646 | goto errout; |
Theodore Ts'o | 16b851c | 2008-04-20 23:33:34 -0400 | [diff] [blame] | 647 | if (csum_flag && |
Theodore Ts'o | f5fa200 | 2006-05-08 20:17:26 -0400 | [diff] [blame] | 648 | (i != fs->super->s_inodes_count) && |
Theodore Ts'o | cd65a24 | 2009-10-25 21:42:12 -0400 | [diff] [blame] | 649 | (ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT) |
Theodore Ts'o | 732c8cd | 2009-09-07 21:15:12 -0400 | [diff] [blame] | 650 | )) |
Theodore Ts'o | f5fa200 | 2006-05-08 20:17:26 -0400 | [diff] [blame] | 651 | skip_group++; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 652 | } |
| 653 | } |
Theodore Ts'o | f122632 | 2002-03-07 02:47:07 -0500 | [diff] [blame] | 654 | if (pctx.ino) |
| 655 | print_bitmap_problem(ctx, save_problem, &pctx); |
Brian Behlendorf | 49e2df2 | 2007-03-28 12:07:41 -0400 | [diff] [blame] | 656 | |
Theodore Ts'o | 63c4969 | 1998-02-20 05:24:59 +0000 | [diff] [blame] | 657 | if (had_problem) |
| 658 | fixit = end_problem_latch(ctx, PR_LATCH_IBITMAP); |
| 659 | else |
| 660 | fixit = -1; |
Theodore Ts'o | 5596def | 1999-07-19 15:27:37 +0000 | [diff] [blame] | 661 | ctx->flags &= ~E2F_FLAG_PROG_SUPPRESS; |
Brian Behlendorf | 49e2df2 | 2007-03-28 12:07:41 -0400 | [diff] [blame] | 662 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 663 | if (fixit == 1) { |
| 664 | ext2fs_free_inode_bitmap(fs->inode_map); |
| 665 | retval = ext2fs_copy_bitmap(ctx->inode_used_map, |
| 666 | &fs->inode_map); |
Theodore Ts'o | bbd47d7 | 2000-06-10 19:21:33 +0000 | [diff] [blame] | 667 | if (retval) { |
| 668 | clear_problem_context(&pctx); |
| 669 | fix_problem(ctx, PR_5_COPY_IBITMAP_ERROR, &pctx); |
| 670 | ctx->flags |= E2F_FLAG_ABORT; |
Brian Behlendorf | 49e2df2 | 2007-03-28 12:07:41 -0400 | [diff] [blame] | 671 | goto errout; |
Theodore Ts'o | bbd47d7 | 2000-06-10 19:21:33 +0000 | [diff] [blame] | 672 | } |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 673 | ext2fs_set_bitmap_padding(fs->inode_map); |
| 674 | ext2fs_mark_ib_dirty(fs); |
| 675 | |
| 676 | /* redo counts */ |
| 677 | inodes = 0; free_inodes = 0; group_free = 0; |
| 678 | dirs_count = 0; group = 0; |
| 679 | memset(free_array, 0, fs->group_desc_count * sizeof(int)); |
| 680 | memset(dir_array, 0, fs->group_desc_count * sizeof(int)); |
Kazuya Mio | 479463a | 2009-07-09 13:46:59 -0400 | [diff] [blame] | 681 | redo_flag++; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 682 | goto redo_counts; |
| 683 | } else if (fixit == 0) |
| 684 | ext2fs_unmark_valid(fs); |
Brian Behlendorf | 49e2df2 | 2007-03-28 12:07:41 -0400 | [diff] [blame] | 685 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 686 | for (i = 0; i < fs->group_desc_count; i++) { |
Valerie Aurora Henson | d7cca6b | 2009-10-25 21:43:47 -0400 | [diff] [blame] | 687 | if (free_array[i] != ext2fs_bg_free_inodes_count(fs, i)) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 688 | pctx.group = i; |
Valerie Aurora Henson | d7cca6b | 2009-10-25 21:43:47 -0400 | [diff] [blame] | 689 | pctx.ino = ext2fs_bg_free_inodes_count(fs, i); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 690 | pctx.ino2 = free_array[i]; |
| 691 | if (fix_problem(ctx, PR_5_FREE_INODE_COUNT_GROUP, |
| 692 | &pctx)) { |
Valerie Aurora Henson | d7cca6b | 2009-10-25 21:43:47 -0400 | [diff] [blame] | 693 | ext2fs_bg_free_inodes_count_set(fs, i, free_array[i]); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 694 | ext2fs_mark_super_dirty(fs); |
| 695 | } else |
| 696 | ext2fs_unmark_valid(fs); |
| 697 | } |
Valerie Aurora Henson | d7cca6b | 2009-10-25 21:43:47 -0400 | [diff] [blame] | 698 | if (dir_array[i] != ext2fs_bg_used_dirs_count(fs, i)) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 699 | pctx.group = i; |
Valerie Aurora Henson | d7cca6b | 2009-10-25 21:43:47 -0400 | [diff] [blame] | 700 | pctx.ino = ext2fs_bg_used_dirs_count(fs, i); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 701 | pctx.ino2 = dir_array[i]; |
| 702 | |
| 703 | if (fix_problem(ctx, PR_5_FREE_DIR_COUNT_GROUP, |
| 704 | &pctx)) { |
Valerie Aurora Henson | d7cca6b | 2009-10-25 21:43:47 -0400 | [diff] [blame] | 705 | ext2fs_bg_used_dirs_count_set(fs, i, dir_array[i]); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 706 | ext2fs_mark_super_dirty(fs); |
| 707 | } else |
| 708 | ext2fs_unmark_valid(fs); |
| 709 | } |
| 710 | } |
| 711 | if (free_inodes != fs->super->s_free_inodes_count) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 712 | pctx.group = -1; |
| 713 | pctx.ino = fs->super->s_free_inodes_count; |
| 714 | pctx.ino2 = free_inodes; |
| 715 | |
| 716 | if (fix_problem(ctx, PR_5_FREE_INODE_COUNT, &pctx)) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 717 | fs->super->s_free_inodes_count = free_inodes; |
| 718 | ext2fs_mark_super_dirty(fs); |
Andreas Dilger | 2788cc8 | 2012-04-24 16:22:48 -0400 | [diff] [blame] | 719 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 720 | } |
Brian Behlendorf | 49e2df2 | 2007-03-28 12:07:41 -0400 | [diff] [blame] | 721 | errout: |
Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 722 | ext2fs_free_mem(&free_array); |
| 723 | ext2fs_free_mem(&dir_array); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 726 | static void check_inode_end(e2fsck_t ctx) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 727 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 728 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 86c627e | 2001-01-11 15:12:14 +0000 | [diff] [blame] | 729 | ext2_ino_t end, save_inodes_count, i; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 730 | struct problem_context pctx; |
| 731 | |
| 732 | clear_problem_context(&pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 733 | |
| 734 | end = EXT2_INODES_PER_GROUP(fs->super) * fs->group_desc_count; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 735 | pctx.errcode = ext2fs_fudge_inode_bitmap_end(fs->inode_map, end, |
| 736 | &save_inodes_count); |
| 737 | if (pctx.errcode) { |
| 738 | pctx.num = 1; |
| 739 | fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 740 | ctx->flags |= E2F_FLAG_ABORT; /* fatal */ |
| 741 | return; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 742 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 743 | if (save_inodes_count == end) |
| 744 | return; |
Eric Sandeen | 5830d6b | 2006-08-30 02:16:55 -0400 | [diff] [blame] | 745 | |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 746 | /* protect loop from wrap-around if end is maxed */ |
Eric Sandeen | 5830d6b | 2006-08-30 02:16:55 -0400 | [diff] [blame] | 747 | for (i = save_inodes_count + 1; i <= end && i > save_inodes_count; i++) { |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 748 | if (!ext2fs_test_inode_bitmap(fs->inode_map, i)) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 749 | if (fix_problem(ctx, PR_5_INODE_BMAP_PADDING, &pctx)) { |
Kazuya Mio | 01ec126 | 2009-07-06 17:15:24 +0900 | [diff] [blame] | 750 | for (; i <= end; i++) |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 751 | ext2fs_mark_inode_bitmap(fs->inode_map, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 752 | i); |
| 753 | ext2fs_mark_ib_dirty(fs); |
| 754 | } else |
| 755 | ext2fs_unmark_valid(fs); |
| 756 | break; |
| 757 | } |
| 758 | } |
| 759 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 760 | pctx.errcode = ext2fs_fudge_inode_bitmap_end(fs->inode_map, |
| 761 | save_inodes_count, 0); |
| 762 | if (pctx.errcode) { |
| 763 | pctx.num = 2; |
| 764 | fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 765 | ctx->flags |= E2F_FLAG_ABORT; /* fatal */ |
| 766 | return; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 767 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 768 | } |
| 769 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 770 | static void check_block_end(e2fsck_t ctx) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 771 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 772 | ext2_filsys fs = ctx->fs; |
Valerie Aurora Henson | c5d2f50 | 2009-08-22 22:29:02 -0400 | [diff] [blame] | 773 | blk64_t end, save_blocks_count, i; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 774 | struct problem_context pctx; |
| 775 | |
| 776 | clear_problem_context(&pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 777 | |
Valerie Aurora Henson | c5d2f50 | 2009-08-22 22:29:02 -0400 | [diff] [blame] | 778 | end = ext2fs_get_block_bitmap_start2(fs->block_map) + |
Theodore Ts'o | 1e33a8b | 2014-07-26 07:40:36 -0400 | [diff] [blame] | 779 | EXT2_GROUPS_TO_CLUSTERS(fs->super, fs->group_desc_count) - 1; |
Valerie Aurora Henson | c5d2f50 | 2009-08-22 22:29:02 -0400 | [diff] [blame] | 780 | pctx.errcode = ext2fs_fudge_block_bitmap_end2(fs->block_map, end, |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 781 | &save_blocks_count); |
| 782 | if (pctx.errcode) { |
| 783 | pctx.num = 3; |
| 784 | fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 785 | ctx->flags |= E2F_FLAG_ABORT; /* fatal */ |
| 786 | return; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 787 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 788 | if (save_blocks_count == end) |
| 789 | return; |
Eric Sandeen | 5830d6b | 2006-08-30 02:16:55 -0400 | [diff] [blame] | 790 | |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 791 | /* Protect loop from wrap-around if end is maxed */ |
Eric Sandeen | 5830d6b | 2006-08-30 02:16:55 -0400 | [diff] [blame] | 792 | for (i = save_blocks_count + 1; i <= end && i > save_blocks_count; i++) { |
Theodore Ts'o | 44fe08f | 2011-06-10 18:58:16 -0400 | [diff] [blame] | 793 | if (!ext2fs_test_block_bitmap2(fs->block_map, |
| 794 | EXT2FS_C2B(fs, i))) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 795 | if (fix_problem(ctx, PR_5_BLOCK_BMAP_PADDING, &pctx)) { |
Kazuya Mio | 01ec126 | 2009-07-06 17:15:24 +0900 | [diff] [blame] | 796 | for (; i <= end; i++) |
Valerie Aurora Henson | c5d2f50 | 2009-08-22 22:29:02 -0400 | [diff] [blame] | 797 | ext2fs_mark_block_bitmap2(fs->block_map, |
Theodore Ts'o | 44fe08f | 2011-06-10 18:58:16 -0400 | [diff] [blame] | 798 | EXT2FS_C2B(fs, i)); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 799 | ext2fs_mark_bb_dirty(fs); |
| 800 | } else |
| 801 | ext2fs_unmark_valid(fs); |
| 802 | break; |
| 803 | } |
| 804 | } |
| 805 | |
Valerie Aurora Henson | c5d2f50 | 2009-08-22 22:29:02 -0400 | [diff] [blame] | 806 | pctx.errcode = ext2fs_fudge_block_bitmap_end2(fs->block_map, |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 807 | save_blocks_count, 0); |
| 808 | if (pctx.errcode) { |
| 809 | pctx.num = 4; |
| 810 | fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 811 | ctx->flags |= E2F_FLAG_ABORT; /* fatal */ |
| 812 | return; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 813 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 814 | } |
| 815 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 816 | |
| 817 | |