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