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 |
| 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 | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 10 | * |
| 11 | */ |
| 12 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 13 | #include "e2fsck.h" |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 14 | #include "problem.h" |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 15 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 16 | static void check_block_bitmaps(e2fsck_t ctx); |
| 17 | static void check_inode_bitmaps(e2fsck_t ctx); |
| 18 | static void check_inode_end(e2fsck_t ctx); |
| 19 | static void check_block_end(e2fsck_t ctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 20 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 21 | void pass5(e2fsck_t ctx) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 22 | { |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame^] | 23 | #ifdef RESOURCE_TRACK |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 24 | struct resource_track rtrack; |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame^] | 25 | #endif |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 26 | struct problem_context pctx; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 27 | |
| 28 | #ifdef MTRACE |
| 29 | mtrace_print("Pass 5"); |
| 30 | #endif |
| 31 | |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame^] | 32 | #ifdef RESOURCE_TRACK |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 33 | init_resource_track(&rtrack); |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame^] | 34 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 35 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 36 | clear_problem_context(&pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 37 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 38 | if (!(ctx->options & E2F_OPT_PREEN)) |
| 39 | fix_problem(ctx, PR_5_PASS_HEADER, &pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 40 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 41 | read_bitmaps(ctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 42 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 43 | check_block_bitmaps(ctx); |
| 44 | check_inode_bitmaps(ctx); |
| 45 | check_inode_end(ctx); |
| 46 | check_block_end(ctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 47 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 48 | ext2fs_free_inode_bitmap(ctx->inode_used_map); |
| 49 | ctx->inode_used_map = 0; |
| 50 | ext2fs_free_inode_bitmap(ctx->inode_dir_map); |
| 51 | ctx->inode_dir_map = 0; |
| 52 | ext2fs_free_block_bitmap(ctx->block_found_map); |
| 53 | ctx->block_found_map = 0; |
| 54 | |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame^] | 55 | #ifdef RESOURCE_TRACK |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 56 | if (ctx->options & E2F_OPT_TIME2) |
| 57 | print_resource_track("Pass 5", &rtrack); |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame^] | 58 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 61 | static void check_block_bitmaps(e2fsck_t ctx) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 62 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 63 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 64 | blk_t i; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 65 | int *free_array; |
| 66 | int group = 0; |
| 67 | int blocks = 0; |
| 68 | int free_blocks = 0; |
| 69 | int group_free = 0; |
| 70 | int actual, bitmap; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 71 | struct problem_context pctx; |
| 72 | int problem, fixit; |
| 73 | errcode_t retval; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 74 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 75 | clear_problem_context(&pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 76 | free_array = allocate_memory(fs->group_desc_count * sizeof(int), |
| 77 | "free block count array"); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 78 | |
| 79 | if ((fs->super->s_first_data_block < |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 80 | ext2fs_get_block_bitmap_start(ctx->block_found_map)) || |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 81 | (fs->super->s_blocks_count-1 > |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 82 | ext2fs_get_block_bitmap_end(ctx->block_found_map))) { |
| 83 | pctx.num = 1; |
| 84 | pctx.blk = fs->super->s_first_data_block; |
| 85 | pctx.blk2 = fs->super->s_blocks_count -1; |
| 86 | pctx.ino = ext2fs_get_block_bitmap_start(ctx->block_found_map); |
| 87 | pctx.ino2 = ext2fs_get_block_bitmap_end(ctx->block_found_map); |
| 88 | fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx); |
| 89 | /* fatal */ |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 90 | fatal_error(0); |
| 91 | } |
| 92 | |
| 93 | if ((fs->super->s_first_data_block < |
| 94 | ext2fs_get_block_bitmap_start(fs->block_map)) || |
| 95 | (fs->super->s_blocks_count-1 > |
| 96 | ext2fs_get_block_bitmap_end(fs->block_map))) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 97 | pctx.num = 2; |
| 98 | pctx.blk = fs->super->s_first_data_block; |
| 99 | pctx.blk2 = fs->super->s_blocks_count -1; |
| 100 | pctx.ino = ext2fs_get_block_bitmap_start(fs->block_map); |
| 101 | pctx.ino2 = ext2fs_get_block_bitmap_end(fs->block_map); |
| 102 | fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx); |
| 103 | /* fatal */ |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 104 | fatal_error(0); |
| 105 | } |
| 106 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 107 | redo_counts: |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 108 | for (i = fs->super->s_first_data_block; |
| 109 | i < fs->super->s_blocks_count; |
| 110 | i++) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 111 | actual = ext2fs_fast_test_block_bitmap(ctx->block_found_map, i); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 112 | bitmap = ext2fs_fast_test_block_bitmap(fs->block_map, i); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 113 | |
| 114 | if (actual == bitmap) |
| 115 | goto do_counts; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 116 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 117 | if (!actual && bitmap) { |
| 118 | /* |
| 119 | * Block not used, but marked in use in the bitmap. |
| 120 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 121 | problem = PR_5_UNUSED_BLOCK; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 122 | } else { |
| 123 | /* |
| 124 | * Block used, but not marked in use in the bitmap. |
| 125 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 126 | problem = PR_5_BLOCK_USED; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 127 | } |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 128 | pctx.blk = i; |
| 129 | fix_problem(ctx, problem, &pctx); |
| 130 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 131 | do_counts: |
| 132 | if (!bitmap) { |
| 133 | group_free++; |
| 134 | free_blocks++; |
| 135 | } |
| 136 | blocks ++; |
| 137 | if ((blocks == fs->super->s_blocks_per_group) || |
| 138 | (i == fs->super->s_blocks_count-1)) { |
| 139 | free_array[group] = group_free; |
| 140 | group ++; |
| 141 | blocks = 0; |
| 142 | group_free = 0; |
| 143 | } |
| 144 | } |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 145 | fixit = end_problem_latch(ctx, PR_LATCH_BBITMAP); |
| 146 | if (fixit == 1) { |
| 147 | ext2fs_free_block_bitmap(fs->block_map); |
| 148 | retval = ext2fs_copy_bitmap(ctx->block_found_map, |
| 149 | &fs->block_map); |
| 150 | /* XXX check retval --- should never fail! */ |
| 151 | ext2fs_set_bitmap_padding(fs->block_map); |
| 152 | ext2fs_mark_bb_dirty(fs); |
| 153 | |
| 154 | /* Redo the counts */ |
| 155 | blocks = 0; free_blocks = 0; group_free = 0; group = 0; |
| 156 | memset(free_array, 0, fs->group_desc_count * sizeof(int)); |
| 157 | goto redo_counts; |
| 158 | } else if (fixit == 0) |
| 159 | ext2fs_unmark_valid(fs); |
| 160 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 161 | for (i = 0; i < fs->group_desc_count; i++) { |
| 162 | if (free_array[i] != fs->group_desc[i].bg_free_blocks_count) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 163 | pctx.group = i; |
| 164 | pctx.blk = fs->group_desc[i].bg_free_blocks_count; |
| 165 | pctx.blk2 = free_array[i]; |
| 166 | |
| 167 | if (fix_problem(ctx, PR_5_FREE_BLOCK_COUNT_GROUP, |
| 168 | &pctx)) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 169 | fs->group_desc[i].bg_free_blocks_count = |
| 170 | free_array[i]; |
| 171 | ext2fs_mark_super_dirty(fs); |
| 172 | } else |
| 173 | ext2fs_unmark_valid(fs); |
| 174 | } |
| 175 | } |
| 176 | if (free_blocks != fs->super->s_free_blocks_count) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 177 | pctx.group = 0; |
| 178 | pctx.blk = fs->super->s_free_blocks_count; |
| 179 | pctx.blk2 = free_blocks; |
| 180 | |
| 181 | if (fix_problem(ctx, PR_5_FREE_BLOCK_COUNT, &pctx)) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 182 | fs->super->s_free_blocks_count = free_blocks; |
| 183 | ext2fs_mark_super_dirty(fs); |
| 184 | } else |
| 185 | ext2fs_unmark_valid(fs); |
| 186 | } |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 187 | free(free_array); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 190 | static void check_inode_bitmaps(e2fsck_t ctx) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 191 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 192 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 193 | ino_t i; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 194 | int free_inodes = 0; |
| 195 | int group_free = 0; |
| 196 | int dirs_count = 0; |
| 197 | int group = 0; |
| 198 | int inodes = 0; |
| 199 | int *free_array; |
| 200 | int *dir_array; |
| 201 | int actual, bitmap; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 202 | errcode_t retval; |
| 203 | struct problem_context pctx; |
| 204 | int problem, fixit; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 205 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 206 | clear_problem_context(&pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 207 | free_array = allocate_memory(fs->group_desc_count * sizeof(int), |
| 208 | "free inode count array"); |
| 209 | |
| 210 | dir_array = allocate_memory(fs->group_desc_count * sizeof(int), |
| 211 | "directory count array"); |
| 212 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 213 | if ((1 < ext2fs_get_inode_bitmap_start(ctx->inode_used_map)) || |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 214 | (fs->super->s_inodes_count > |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 215 | ext2fs_get_inode_bitmap_end(ctx->inode_used_map))) { |
| 216 | pctx.num = 3; |
| 217 | pctx.blk = 1; |
| 218 | pctx.blk2 = fs->super->s_inodes_count; |
| 219 | pctx.ino = ext2fs_get_inode_bitmap_start(ctx->inode_used_map); |
| 220 | pctx.ino2 = ext2fs_get_inode_bitmap_end(ctx->inode_used_map); |
| 221 | fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx); |
| 222 | /* fatal */ |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 223 | fatal_error(0); |
| 224 | } |
| 225 | if ((1 < ext2fs_get_inode_bitmap_start(fs->inode_map)) || |
| 226 | (fs->super->s_inodes_count > |
| 227 | ext2fs_get_inode_bitmap_end(fs->inode_map))) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 228 | pctx.num = 4; |
| 229 | pctx.blk = 1; |
| 230 | pctx.blk2 = fs->super->s_inodes_count; |
| 231 | pctx.ino = ext2fs_get_inode_bitmap_start(fs->inode_map); |
| 232 | pctx.ino2 = ext2fs_get_inode_bitmap_end(fs->inode_map); |
| 233 | fix_problem(ctx, PR_5_BMAP_ENDPOINTS, &pctx); |
| 234 | /* fatal */ |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 235 | fatal_error(0); |
| 236 | } |
| 237 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 238 | redo_counts: |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 239 | for (i = 1; i <= fs->super->s_inodes_count; i++) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 240 | actual = ext2fs_fast_test_inode_bitmap(ctx->inode_used_map, i); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 241 | bitmap = ext2fs_fast_test_inode_bitmap(fs->inode_map, i); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 242 | |
| 243 | if (actual == bitmap) |
| 244 | goto do_counts; |
| 245 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 246 | if (!actual && bitmap) { |
| 247 | /* |
| 248 | * Inode wasn't used, but marked in bitmap |
| 249 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 250 | problem = PR_5_UNUSED_INODE; |
| 251 | } else /* if (actual && !bitmap) */ { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 252 | /* |
| 253 | * Inode used, but not in bitmap |
| 254 | */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 255 | problem = PR_5_INODE_USED; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 256 | } |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 257 | pctx.ino = i; |
| 258 | fix_problem(ctx, problem, &pctx); |
| 259 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 260 | do_counts: |
| 261 | if (!bitmap) { |
| 262 | group_free++; |
| 263 | free_inodes++; |
| 264 | } else { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 265 | if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, i)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 266 | dirs_count++; |
| 267 | } |
| 268 | inodes++; |
| 269 | if ((inodes == fs->super->s_inodes_per_group) || |
| 270 | (i == fs->super->s_inodes_count)) { |
| 271 | free_array[group] = group_free; |
| 272 | dir_array[group] = dirs_count; |
| 273 | group ++; |
| 274 | inodes = 0; |
| 275 | group_free = 0; |
| 276 | dirs_count = 0; |
| 277 | } |
| 278 | } |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 279 | fixit = end_problem_latch(ctx, PR_LATCH_IBITMAP); |
| 280 | if (fixit == 1) { |
| 281 | ext2fs_free_inode_bitmap(fs->inode_map); |
| 282 | retval = ext2fs_copy_bitmap(ctx->inode_used_map, |
| 283 | &fs->inode_map); |
| 284 | /* XXX check retval --- should never fail! */ |
| 285 | ext2fs_set_bitmap_padding(fs->inode_map); |
| 286 | ext2fs_mark_ib_dirty(fs); |
| 287 | |
| 288 | /* redo counts */ |
| 289 | inodes = 0; free_inodes = 0; group_free = 0; |
| 290 | dirs_count = 0; group = 0; |
| 291 | memset(free_array, 0, fs->group_desc_count * sizeof(int)); |
| 292 | memset(dir_array, 0, fs->group_desc_count * sizeof(int)); |
| 293 | goto redo_counts; |
| 294 | } else if (fixit == 0) |
| 295 | ext2fs_unmark_valid(fs); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 296 | |
| 297 | for (i = 0; i < fs->group_desc_count; i++) { |
| 298 | if (free_array[i] != fs->group_desc[i].bg_free_inodes_count) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 299 | pctx.group = i; |
| 300 | pctx.ino = fs->group_desc[i].bg_free_inodes_count; |
| 301 | pctx.ino2 = free_array[i]; |
| 302 | if (fix_problem(ctx, PR_5_FREE_INODE_COUNT_GROUP, |
| 303 | &pctx)) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 304 | fs->group_desc[i].bg_free_inodes_count = |
| 305 | free_array[i]; |
| 306 | ext2fs_mark_super_dirty(fs); |
| 307 | } else |
| 308 | ext2fs_unmark_valid(fs); |
| 309 | } |
| 310 | if (dir_array[i] != fs->group_desc[i].bg_used_dirs_count) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 311 | pctx.group = i; |
| 312 | pctx.ino = fs->group_desc[i].bg_used_dirs_count; |
| 313 | pctx.ino2 = dir_array[i]; |
| 314 | |
| 315 | if (fix_problem(ctx, PR_5_FREE_DIR_COUNT_GROUP, |
| 316 | &pctx)) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 317 | fs->group_desc[i].bg_used_dirs_count = |
| 318 | dir_array[i]; |
| 319 | ext2fs_mark_super_dirty(fs); |
| 320 | } else |
| 321 | ext2fs_unmark_valid(fs); |
| 322 | } |
| 323 | } |
| 324 | if (free_inodes != fs->super->s_free_inodes_count) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 325 | pctx.group = -1; |
| 326 | pctx.ino = fs->super->s_free_inodes_count; |
| 327 | pctx.ino2 = free_inodes; |
| 328 | |
| 329 | if (fix_problem(ctx, PR_5_FREE_INODE_COUNT, &pctx)) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 330 | fs->super->s_free_inodes_count = free_inodes; |
| 331 | ext2fs_mark_super_dirty(fs); |
| 332 | } else |
| 333 | ext2fs_unmark_valid(fs); |
| 334 | } |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 335 | free(free_array); |
| 336 | free(dir_array); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 339 | static void check_inode_end(e2fsck_t ctx) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 340 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 341 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 342 | ino_t end, save_inodes_count, i; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 343 | struct problem_context pctx; |
| 344 | |
| 345 | clear_problem_context(&pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 346 | |
| 347 | end = EXT2_INODES_PER_GROUP(fs->super) * fs->group_desc_count; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 348 | pctx.errcode = ext2fs_fudge_inode_bitmap_end(fs->inode_map, end, |
| 349 | &save_inodes_count); |
| 350 | if (pctx.errcode) { |
| 351 | pctx.num = 1; |
| 352 | fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 353 | fatal_error(0); |
| 354 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 355 | if (save_inodes_count == end) |
| 356 | return; |
| 357 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 358 | for (i = save_inodes_count + 1; i <= end; i++) { |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 359 | if (!ext2fs_test_inode_bitmap(fs->inode_map, i)) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 360 | if (fix_problem(ctx, PR_5_INODE_BMAP_PADDING, &pctx)) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 361 | for (i = save_inodes_count + 1; i <= end; i++) |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 362 | ext2fs_mark_inode_bitmap(fs->inode_map, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 363 | i); |
| 364 | ext2fs_mark_ib_dirty(fs); |
| 365 | } else |
| 366 | ext2fs_unmark_valid(fs); |
| 367 | break; |
| 368 | } |
| 369 | } |
| 370 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 371 | pctx.errcode = ext2fs_fudge_inode_bitmap_end(fs->inode_map, |
| 372 | save_inodes_count, 0); |
| 373 | if (pctx.errcode) { |
| 374 | pctx.num = 2; |
| 375 | fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 376 | fatal_error(0); |
| 377 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 380 | static void check_block_end(e2fsck_t ctx) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 381 | { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 382 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 383 | blk_t end, save_blocks_count, i; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 384 | struct problem_context pctx; |
| 385 | |
| 386 | clear_problem_context(&pctx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 387 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 388 | end = fs->block_map->start + |
| 389 | (EXT2_BLOCKS_PER_GROUP(fs->super) * fs->group_desc_count) - 1; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 390 | pctx.errcode = ext2fs_fudge_block_bitmap_end(fs->block_map, end, |
| 391 | &save_blocks_count); |
| 392 | if (pctx.errcode) { |
| 393 | pctx.num = 3; |
| 394 | fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 395 | fatal_error(0); |
| 396 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 397 | if (save_blocks_count == end) |
| 398 | return; |
| 399 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 400 | for (i = save_blocks_count + 1; i <= end; i++) { |
| 401 | if (!ext2fs_test_block_bitmap(fs->block_map, i)) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 402 | if (fix_problem(ctx, PR_5_BLOCK_BMAP_PADDING, &pctx)) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 403 | for (i = save_blocks_count + 1; i < end; i++) |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 404 | ext2fs_mark_block_bitmap(fs->block_map, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 405 | i); |
| 406 | ext2fs_mark_bb_dirty(fs); |
| 407 | } else |
| 408 | ext2fs_unmark_valid(fs); |
| 409 | break; |
| 410 | } |
| 411 | } |
| 412 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 413 | pctx.errcode = ext2fs_fudge_block_bitmap_end(fs->block_map, |
| 414 | save_blocks_count, 0); |
| 415 | if (pctx.errcode) { |
| 416 | pctx.num = 4; |
| 417 | fix_problem(ctx, PR_5_FUDGE_BITMAP_ERROR, &pctx); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 418 | fatal_error(0); |
| 419 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 422 | |
| 423 | |