lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * pass2.c --- check directory structure |
| 3 | * |
| 4 | * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o |
| 5 | * |
| 6 | * %Begin-Header% |
| 7 | * This file may be redistributed under the terms of the GNU Public |
| 8 | * License. |
| 9 | * %End-Header% |
| 10 | * |
| 11 | * Pass 2 of e2fsck iterates through all active directory inodes, and |
| 12 | * applies to following tests to each directory entry in the directory |
| 13 | * blocks in the inodes: |
| 14 | * |
| 15 | * - The length of the directory entry (rec_len) should be at |
| 16 | * least 8 bytes, and no more than the remaining space |
| 17 | * left in the directory block. |
| 18 | * - The length of the name in the directory entry (name_len) |
| 19 | * should be less than (rec_len - 8). |
| 20 | * - The inode number in the directory entry should be within |
| 21 | * legal bounds. |
| 22 | * - The inode number should refer to a in-use inode. |
| 23 | * - The first entry should be '.', and its inode should be |
| 24 | * the inode of the directory. |
| 25 | * - The second entry should be '..'. |
| 26 | * |
| 27 | * To minimize disk seek time, the directory blocks are processed in |
| 28 | * sorted order of block numbers. |
| 29 | * |
| 30 | * Pass 2 also collects the following information: |
| 31 | * - The inode numbers of the subdirectories for each directory. |
| 32 | * |
| 33 | * Pass 2 relies on the following information from previous passes: |
| 34 | * - The directory information collected in pass 1. |
| 35 | * - The inode_used_map bitmap |
| 36 | * - The inode_bad_map bitmap |
| 37 | * - The inode_dir_map bitmap |
| 38 | * |
| 39 | * Pass 2 frees the following data structures |
| 40 | * - The inode_bad_map bitmap |
| 41 | * - The inode_reg_map bitmap |
| 42 | */ |
| 43 | |
| 44 | #define _GNU_SOURCE 1 /* get strnlen() */ |
| 45 | #include "config.h" |
| 46 | #include <string.h> |
| 47 | |
| 48 | #include "e2fsck.h" |
| 49 | #include "problem.h" |
| 50 | #include "dict.h" |
| 51 | |
| 52 | #ifdef NO_INLINE_FUNCS |
| 53 | #define _INLINE_ |
| 54 | #else |
| 55 | #define _INLINE_ inline |
| 56 | #endif |
| 57 | |
| 58 | /* #define DX_DEBUG */ |
| 59 | |
| 60 | /* |
| 61 | * Keeps track of how many times an inode is referenced. |
| 62 | */ |
| 63 | static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf); |
| 64 | static int check_dir_block(ext2_filsys fs, |
| 65 | struct ext2_db_entry2 *dir_blocks_info, |
| 66 | void *priv_data); |
| 67 | static int allocate_dir_block(e2fsck_t ctx, |
| 68 | struct ext2_db_entry2 *dir_blocks_info, |
| 69 | char *buf, struct problem_context *pctx); |
| 70 | static void clear_htree(e2fsck_t ctx, ext2_ino_t ino); |
| 71 | static int htree_depth(struct dx_dir_info *dx_dir, |
| 72 | struct dx_dirblock_info *dx_db); |
| 73 | static EXT2_QSORT_TYPE special_dir_block_cmp(const void *a, const void *b); |
| 74 | |
| 75 | struct check_dir_struct { |
| 76 | char *buf; |
| 77 | struct problem_context pctx; |
| 78 | int count, max; |
| 79 | e2fsck_t ctx; |
| 80 | }; |
| 81 | |
| 82 | void e2fsck_pass2(e2fsck_t ctx) |
| 83 | { |
| 84 | struct ext2_super_block *sb = ctx->fs->super; |
| 85 | struct problem_context pctx; |
| 86 | ext2_filsys fs = ctx->fs; |
| 87 | char *buf; |
| 88 | #ifdef RESOURCE_TRACK |
| 89 | struct resource_track rtrack; |
| 90 | #endif |
| 91 | struct check_dir_struct cd; |
| 92 | struct dx_dir_info *dx_dir; |
| 93 | struct dx_dirblock_info *dx_db, *dx_parent; |
| 94 | unsigned int save_type; |
| 95 | int b; |
| 96 | int i, depth; |
| 97 | problem_t code; |
| 98 | int bad_dir; |
| 99 | |
| 100 | init_resource_track(&rtrack, ctx->fs->io); |
| 101 | clear_problem_context(&cd.pctx); |
| 102 | |
| 103 | #ifdef MTRACE |
| 104 | mtrace_print("Pass 2"); |
| 105 | #endif |
| 106 | |
| 107 | if (!(ctx->options & E2F_OPT_PREEN)) |
| 108 | fix_problem(ctx, PR_2_PASS_HEADER, &cd.pctx); |
| 109 | |
| 110 | e2fsck_setup_tdb_icount(ctx, EXT2_ICOUNT_OPT_INCREMENT, |
| 111 | &ctx->inode_count); |
| 112 | if (ctx->inode_count) |
| 113 | cd.pctx.errcode = 0; |
| 114 | else { |
| 115 | e2fsck_set_bitmap_type(fs, EXT2FS_BMAP64_RBTREE, |
| 116 | "inode_count", &save_type); |
| 117 | cd.pctx.errcode = ext2fs_create_icount2(fs, |
| 118 | EXT2_ICOUNT_OPT_INCREMENT, |
| 119 | 0, ctx->inode_link_info, |
| 120 | &ctx->inode_count); |
| 121 | fs->default_bitmap_type = save_type; |
| 122 | } |
| 123 | if (cd.pctx.errcode) { |
| 124 | fix_problem(ctx, PR_2_ALLOCATE_ICOUNT, &cd.pctx); |
| 125 | ctx->flags |= E2F_FLAG_ABORT; |
| 126 | return; |
| 127 | } |
| 128 | buf = (char *) e2fsck_allocate_memory(ctx, 2*fs->blocksize, |
| 129 | "directory scan buffer"); |
| 130 | |
| 131 | /* |
| 132 | * Set up the parent pointer for the root directory, if |
| 133 | * present. (If the root directory is not present, we will |
| 134 | * create it in pass 3.) |
| 135 | */ |
| 136 | (void) e2fsck_dir_info_set_parent(ctx, EXT2_ROOT_INO, EXT2_ROOT_INO); |
| 137 | |
| 138 | cd.buf = buf; |
| 139 | cd.ctx = ctx; |
| 140 | cd.count = 1; |
| 141 | cd.max = ext2fs_dblist_count2(fs->dblist); |
| 142 | |
| 143 | if (ctx->progress) |
| 144 | (void) (ctx->progress)(ctx, 2, 0, cd.max); |
| 145 | |
| 146 | if (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) |
| 147 | ext2fs_dblist_sort2(fs->dblist, special_dir_block_cmp); |
| 148 | |
| 149 | cd.pctx.errcode = ext2fs_dblist_iterate2(fs->dblist, check_dir_block, |
| 150 | &cd); |
| 151 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK || ctx->flags & E2F_FLAG_RESTART) |
| 152 | return; |
| 153 | |
| 154 | if (ctx->flags & E2F_FLAG_RESTART_LATER) { |
| 155 | ctx->flags |= E2F_FLAG_RESTART; |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | if (cd.pctx.errcode) { |
| 160 | fix_problem(ctx, PR_2_DBLIST_ITERATE, &cd.pctx); |
| 161 | ctx->flags |= E2F_FLAG_ABORT; |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | #ifdef ENABLE_HTREE |
| 166 | for (i=0; (dx_dir = e2fsck_dx_dir_info_iter(ctx, &i)) != 0;) { |
| 167 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK) |
| 168 | return; |
| 169 | if (dx_dir->numblocks == 0) |
| 170 | continue; |
| 171 | clear_problem_context(&pctx); |
| 172 | bad_dir = 0; |
| 173 | pctx.dir = dx_dir->ino; |
| 174 | dx_db = dx_dir->dx_block; |
| 175 | if (dx_db->flags & DX_FLAG_REFERENCED) |
| 176 | dx_db->flags |= DX_FLAG_DUP_REF; |
| 177 | else |
| 178 | dx_db->flags |= DX_FLAG_REFERENCED; |
| 179 | /* |
| 180 | * Find all of the first and last leaf blocks, and |
| 181 | * update their parent's min and max hash values |
| 182 | */ |
| 183 | for (b=0, dx_db = dx_dir->dx_block; |
| 184 | b < dx_dir->numblocks; |
| 185 | b++, dx_db++) { |
| 186 | if ((dx_db->type != DX_DIRBLOCK_LEAF) || |
| 187 | !(dx_db->flags & (DX_FLAG_FIRST | DX_FLAG_LAST))) |
| 188 | continue; |
| 189 | dx_parent = &dx_dir->dx_block[dx_db->parent]; |
| 190 | /* |
| 191 | * XXX Make sure dx_parent->min_hash > dx_db->min_hash |
| 192 | */ |
| 193 | if (dx_db->flags & DX_FLAG_FIRST) |
| 194 | dx_parent->min_hash = dx_db->min_hash; |
| 195 | /* |
| 196 | * XXX Make sure dx_parent->max_hash < dx_db->max_hash |
| 197 | */ |
| 198 | if (dx_db->flags & DX_FLAG_LAST) |
| 199 | dx_parent->max_hash = dx_db->max_hash; |
| 200 | } |
| 201 | |
| 202 | for (b=0, dx_db = dx_dir->dx_block; |
| 203 | b < dx_dir->numblocks; |
| 204 | b++, dx_db++) { |
| 205 | pctx.blkcount = b; |
| 206 | pctx.group = dx_db->parent; |
| 207 | code = 0; |
| 208 | if (!(dx_db->flags & DX_FLAG_FIRST) && |
| 209 | (dx_db->min_hash < dx_db->node_min_hash)) { |
| 210 | pctx.blk = dx_db->min_hash; |
| 211 | pctx.blk2 = dx_db->node_min_hash; |
| 212 | code = PR_2_HTREE_MIN_HASH; |
| 213 | fix_problem(ctx, code, &pctx); |
| 214 | bad_dir++; |
| 215 | } |
| 216 | if (dx_db->type == DX_DIRBLOCK_LEAF) { |
| 217 | depth = htree_depth(dx_dir, dx_db); |
| 218 | if (depth != dx_dir->depth) { |
| 219 | pctx.num = dx_dir->depth; |
| 220 | code = PR_2_HTREE_BAD_DEPTH; |
| 221 | fix_problem(ctx, code, &pctx); |
| 222 | bad_dir++; |
| 223 | } |
| 224 | } |
| 225 | /* |
| 226 | * This test doesn't apply for the root block |
| 227 | * at block #0 |
| 228 | */ |
| 229 | if (b && |
| 230 | (dx_db->max_hash > dx_db->node_max_hash)) { |
| 231 | pctx.blk = dx_db->max_hash; |
| 232 | pctx.blk2 = dx_db->node_max_hash; |
| 233 | code = PR_2_HTREE_MAX_HASH; |
| 234 | fix_problem(ctx, code, &pctx); |
| 235 | bad_dir++; |
| 236 | } |
| 237 | if (!(dx_db->flags & DX_FLAG_REFERENCED)) { |
| 238 | code = PR_2_HTREE_NOTREF; |
| 239 | fix_problem(ctx, code, &pctx); |
| 240 | bad_dir++; |
| 241 | } else if (dx_db->flags & DX_FLAG_DUP_REF) { |
| 242 | code = PR_2_HTREE_DUPREF; |
| 243 | fix_problem(ctx, code, &pctx); |
| 244 | bad_dir++; |
| 245 | } |
| 246 | } |
| 247 | if (bad_dir && fix_problem(ctx, PR_2_HTREE_CLEAR, &pctx)) { |
| 248 | clear_htree(ctx, dx_dir->ino); |
| 249 | dx_dir->numblocks = 0; |
| 250 | } |
| 251 | } |
| 252 | e2fsck_free_dx_dir_info(ctx); |
| 253 | #endif |
| 254 | ext2fs_free_mem(&buf); |
| 255 | ext2fs_free_dblist(fs->dblist); |
| 256 | |
| 257 | if (ctx->inode_bad_map) { |
| 258 | ext2fs_free_inode_bitmap(ctx->inode_bad_map); |
| 259 | ctx->inode_bad_map = 0; |
| 260 | } |
| 261 | if (ctx->inode_reg_map) { |
| 262 | ext2fs_free_inode_bitmap(ctx->inode_reg_map); |
| 263 | ctx->inode_reg_map = 0; |
| 264 | } |
| 265 | |
| 266 | clear_problem_context(&pctx); |
| 267 | if (ctx->large_files) { |
| 268 | if (!(sb->s_feature_ro_compat & |
| 269 | EXT2_FEATURE_RO_COMPAT_LARGE_FILE) && |
| 270 | fix_problem(ctx, PR_2_FEATURE_LARGE_FILES, &pctx)) { |
| 271 | sb->s_feature_ro_compat |= |
| 272 | EXT2_FEATURE_RO_COMPAT_LARGE_FILE; |
| 273 | fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY; |
| 274 | ext2fs_mark_super_dirty(fs); |
| 275 | } |
| 276 | if (sb->s_rev_level == EXT2_GOOD_OLD_REV && |
| 277 | fix_problem(ctx, PR_1_FS_REV_LEVEL, &pctx)) { |
| 278 | ext2fs_update_dynamic_rev(fs); |
| 279 | ext2fs_mark_super_dirty(fs); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | print_resource_track(ctx, _("Pass 2"), &rtrack, fs->io); |
| 284 | } |
| 285 | |
| 286 | #define MAX_DEPTH 32000 |
| 287 | static int htree_depth(struct dx_dir_info *dx_dir, |
| 288 | struct dx_dirblock_info *dx_db) |
| 289 | { |
| 290 | int depth = 0; |
| 291 | |
| 292 | while (dx_db->type != DX_DIRBLOCK_ROOT && depth < MAX_DEPTH) { |
| 293 | dx_db = &dx_dir->dx_block[dx_db->parent]; |
| 294 | depth++; |
| 295 | } |
| 296 | return depth; |
| 297 | } |
| 298 | |
| 299 | static int dict_de_cmp(const void *a, const void *b) |
| 300 | { |
| 301 | const struct ext2_dir_entry *de_a, *de_b; |
| 302 | int a_len, b_len; |
| 303 | |
| 304 | de_a = (const struct ext2_dir_entry *) a; |
| 305 | a_len = de_a->name_len & 0xFF; |
| 306 | de_b = (const struct ext2_dir_entry *) b; |
| 307 | b_len = de_b->name_len & 0xFF; |
| 308 | |
| 309 | if (a_len != b_len) |
| 310 | return (a_len - b_len); |
| 311 | |
| 312 | return strncmp(de_a->name, de_b->name, a_len); |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | * This is special sort function that makes sure that directory blocks |
| 317 | * with a dirblock of zero are sorted to the beginning of the list. |
| 318 | * This guarantees that the root node of the htree directories are |
| 319 | * processed first, so we know what hash version to use. |
| 320 | */ |
| 321 | static EXT2_QSORT_TYPE special_dir_block_cmp(const void *a, const void *b) |
| 322 | { |
| 323 | const struct ext2_db_entry2 *db_a = |
| 324 | (const struct ext2_db_entry2 *) a; |
| 325 | const struct ext2_db_entry2 *db_b = |
| 326 | (const struct ext2_db_entry2 *) b; |
| 327 | |
| 328 | if (db_a->blockcnt && !db_b->blockcnt) |
| 329 | return 1; |
| 330 | |
| 331 | if (!db_a->blockcnt && db_b->blockcnt) |
| 332 | return -1; |
| 333 | |
| 334 | if (db_a->blk != db_b->blk) |
| 335 | return (int) (db_a->blk - db_b->blk); |
| 336 | |
| 337 | if (db_a->ino != db_b->ino) |
| 338 | return (int) (db_a->ino - db_b->ino); |
| 339 | |
| 340 | return (int) (db_a->blockcnt - db_b->blockcnt); |
| 341 | } |
| 342 | |
| 343 | |
| 344 | /* |
| 345 | * Make sure the first entry in the directory is '.', and that the |
| 346 | * directory entry is sane. |
| 347 | */ |
| 348 | static int check_dot(e2fsck_t ctx, |
| 349 | struct ext2_dir_entry *dirent, |
| 350 | ext2_ino_t ino, struct problem_context *pctx) |
| 351 | { |
| 352 | struct ext2_dir_entry *nextdir; |
| 353 | unsigned int rec_len, new_len; |
| 354 | int status = 0; |
| 355 | int created = 0; |
| 356 | problem_t problem = 0; |
| 357 | |
| 358 | if (!dirent->inode) |
| 359 | problem = PR_2_MISSING_DOT; |
| 360 | else if (((dirent->name_len & 0xFF) != 1) || |
| 361 | (dirent->name[0] != '.')) |
| 362 | problem = PR_2_1ST_NOT_DOT; |
| 363 | else if (dirent->name[1] != '\0') |
| 364 | problem = PR_2_DOT_NULL_TERM; |
| 365 | |
| 366 | (void) ext2fs_get_rec_len(ctx->fs, dirent, &rec_len); |
| 367 | if (problem) { |
| 368 | if (fix_problem(ctx, problem, pctx)) { |
| 369 | if (rec_len < 12) |
| 370 | rec_len = dirent->rec_len = 12; |
| 371 | dirent->inode = ino; |
| 372 | dirent->name_len = 1; |
| 373 | dirent->name[0] = '.'; |
| 374 | dirent->name[1] = '\0'; |
| 375 | status = 1; |
| 376 | created = 1; |
| 377 | } |
| 378 | } |
| 379 | if (dirent->inode != ino) { |
| 380 | if (fix_problem(ctx, PR_2_BAD_INODE_DOT, pctx)) { |
| 381 | dirent->inode = ino; |
| 382 | status = 1; |
| 383 | } |
| 384 | } |
| 385 | if (rec_len > 12) { |
| 386 | new_len = rec_len - 12; |
| 387 | if (new_len > 12) { |
| 388 | if (created || |
| 389 | fix_problem(ctx, PR_2_SPLIT_DOT, pctx)) { |
| 390 | nextdir = (struct ext2_dir_entry *) |
| 391 | ((char *) dirent + 12); |
| 392 | dirent->rec_len = 12; |
| 393 | (void) ext2fs_set_rec_len(ctx->fs, new_len, |
| 394 | nextdir); |
| 395 | nextdir->inode = 0; |
| 396 | nextdir->name_len = 0; |
| 397 | status = 1; |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | return status; |
| 402 | } |
| 403 | |
| 404 | /* |
| 405 | * Make sure the second entry in the directory is '..', and that the |
| 406 | * directory entry is sane. We do not check the inode number of '..' |
| 407 | * here; this gets done in pass 3. |
| 408 | */ |
| 409 | static int check_dotdot(e2fsck_t ctx, |
| 410 | struct ext2_dir_entry *dirent, |
| 411 | ext2_ino_t ino, struct problem_context *pctx) |
| 412 | { |
| 413 | problem_t problem = 0; |
| 414 | unsigned int rec_len; |
| 415 | |
| 416 | if (!dirent->inode) |
| 417 | problem = PR_2_MISSING_DOT_DOT; |
| 418 | else if (((dirent->name_len & 0xFF) != 2) || |
| 419 | (dirent->name[0] != '.') || |
| 420 | (dirent->name[1] != '.')) |
| 421 | problem = PR_2_2ND_NOT_DOT_DOT; |
| 422 | else if (dirent->name[2] != '\0') |
| 423 | problem = PR_2_DOT_DOT_NULL_TERM; |
| 424 | |
| 425 | (void) ext2fs_get_rec_len(ctx->fs, dirent, &rec_len); |
| 426 | if (problem) { |
| 427 | if (fix_problem(ctx, problem, pctx)) { |
| 428 | if (rec_len < 12) |
| 429 | dirent->rec_len = 12; |
| 430 | /* |
| 431 | * Note: we don't have the parent inode just |
| 432 | * yet, so we will fill it in with the root |
| 433 | * inode. This will get fixed in pass 3. |
| 434 | */ |
| 435 | dirent->inode = EXT2_ROOT_INO; |
| 436 | dirent->name_len = 2; |
| 437 | dirent->name[0] = '.'; |
| 438 | dirent->name[1] = '.'; |
| 439 | dirent->name[2] = '\0'; |
| 440 | return 1; |
| 441 | } |
| 442 | return 0; |
| 443 | } |
| 444 | if (e2fsck_dir_info_set_dotdot(ctx, ino, dirent->inode)) { |
| 445 | fix_problem(ctx, PR_2_NO_DIRINFO, pctx); |
| 446 | return -1; |
| 447 | } |
| 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | /* |
| 452 | * Check to make sure a directory entry doesn't contain any illegal |
| 453 | * characters. |
| 454 | */ |
| 455 | static int check_name(e2fsck_t ctx, |
| 456 | struct ext2_dir_entry *dirent, |
| 457 | ext2_ino_t dir_ino EXT2FS_ATTR((unused)), |
| 458 | struct problem_context *pctx) |
| 459 | { |
| 460 | int i; |
| 461 | int fixup = -1; |
| 462 | int ret = 0; |
| 463 | |
| 464 | for ( i = 0; i < (dirent->name_len & 0xFF); i++) { |
| 465 | if (dirent->name[i] == '/' || dirent->name[i] == '\0') { |
| 466 | if (fixup < 0) { |
| 467 | fixup = fix_problem(ctx, PR_2_BAD_NAME, pctx); |
| 468 | } |
| 469 | if (fixup) { |
| 470 | dirent->name[i] = '.'; |
| 471 | ret = 1; |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | return ret; |
| 476 | } |
| 477 | |
| 478 | /* |
| 479 | * Check the directory filetype (if present) |
| 480 | */ |
| 481 | static _INLINE_ int check_filetype(e2fsck_t ctx, |
| 482 | struct ext2_dir_entry *dirent, |
| 483 | ext2_ino_t dir_ino EXT2FS_ATTR((unused)), |
| 484 | struct problem_context *pctx) |
| 485 | { |
| 486 | int filetype = dirent->name_len >> 8; |
| 487 | int should_be = EXT2_FT_UNKNOWN; |
| 488 | struct ext2_inode inode; |
| 489 | |
| 490 | if (!(ctx->fs->super->s_feature_incompat & |
| 491 | EXT2_FEATURE_INCOMPAT_FILETYPE)) { |
| 492 | if (filetype == 0 || |
| 493 | !fix_problem(ctx, PR_2_CLEAR_FILETYPE, pctx)) |
| 494 | return 0; |
| 495 | dirent->name_len = dirent->name_len & 0xFF; |
| 496 | return 1; |
| 497 | } |
| 498 | |
| 499 | if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, dirent->inode)) { |
| 500 | should_be = EXT2_FT_DIR; |
| 501 | } else if (ext2fs_test_inode_bitmap2(ctx->inode_reg_map, |
| 502 | dirent->inode)) { |
| 503 | should_be = EXT2_FT_REG_FILE; |
| 504 | } else if (ctx->inode_bad_map && |
| 505 | ext2fs_test_inode_bitmap2(ctx->inode_bad_map, |
| 506 | dirent->inode)) |
| 507 | should_be = 0; |
| 508 | else { |
| 509 | e2fsck_read_inode(ctx, dirent->inode, &inode, |
| 510 | "check_filetype"); |
| 511 | should_be = ext2_file_type(inode.i_mode); |
| 512 | } |
| 513 | if (filetype == should_be) |
| 514 | return 0; |
| 515 | pctx->num = should_be; |
| 516 | |
| 517 | if (fix_problem(ctx, filetype ? PR_2_BAD_FILETYPE : PR_2_SET_FILETYPE, |
| 518 | pctx) == 0) |
| 519 | return 0; |
| 520 | |
| 521 | dirent->name_len = (dirent->name_len & 0xFF) | should_be << 8; |
| 522 | return 1; |
| 523 | } |
| 524 | |
| 525 | #ifdef ENABLE_HTREE |
| 526 | static void parse_int_node(ext2_filsys fs, |
| 527 | struct ext2_db_entry2 *db, |
| 528 | struct check_dir_struct *cd, |
| 529 | struct dx_dir_info *dx_dir, |
| 530 | char *block_buf) |
| 531 | { |
| 532 | struct ext2_dx_root_info *root; |
| 533 | struct ext2_dx_entry *ent; |
| 534 | struct ext2_dx_countlimit *limit; |
| 535 | struct dx_dirblock_info *dx_db; |
| 536 | int i, expect_limit, count; |
| 537 | blk_t blk; |
| 538 | ext2_dirhash_t min_hash = 0xffffffff; |
| 539 | ext2_dirhash_t max_hash = 0; |
| 540 | ext2_dirhash_t hash = 0, prev_hash; |
| 541 | |
| 542 | if (db->blockcnt == 0) { |
| 543 | root = (struct ext2_dx_root_info *) (block_buf + 24); |
| 544 | |
| 545 | #ifdef DX_DEBUG |
| 546 | printf("Root node dump:\n"); |
| 547 | printf("\t Reserved zero: %u\n", root->reserved_zero); |
| 548 | printf("\t Hash Version: %d\n", root->hash_version); |
| 549 | printf("\t Info length: %d\n", root->info_length); |
| 550 | printf("\t Indirect levels: %d\n", root->indirect_levels); |
| 551 | printf("\t Flags: %d\n", root->unused_flags); |
| 552 | #endif |
| 553 | |
| 554 | ent = (struct ext2_dx_entry *) (block_buf + 24 + root->info_length); |
| 555 | } else { |
| 556 | ent = (struct ext2_dx_entry *) (block_buf+8); |
| 557 | } |
| 558 | limit = (struct ext2_dx_countlimit *) ent; |
| 559 | |
| 560 | #ifdef DX_DEBUG |
| 561 | printf("Number of entries (count): %d\n", |
| 562 | ext2fs_le16_to_cpu(limit->count)); |
| 563 | printf("Number of entries (limit): %d\n", |
| 564 | ext2fs_le16_to_cpu(limit->limit)); |
| 565 | #endif |
| 566 | |
| 567 | count = ext2fs_le16_to_cpu(limit->count); |
| 568 | expect_limit = (fs->blocksize - ((char *) ent - block_buf)) / |
| 569 | sizeof(struct ext2_dx_entry); |
| 570 | if (ext2fs_le16_to_cpu(limit->limit) != expect_limit) { |
| 571 | cd->pctx.num = ext2fs_le16_to_cpu(limit->limit); |
| 572 | if (fix_problem(cd->ctx, PR_2_HTREE_BAD_LIMIT, &cd->pctx)) |
| 573 | goto clear_and_exit; |
| 574 | } |
| 575 | if (count > expect_limit) { |
| 576 | cd->pctx.num = count; |
| 577 | if (fix_problem(cd->ctx, PR_2_HTREE_BAD_COUNT, &cd->pctx)) |
| 578 | goto clear_and_exit; |
| 579 | count = expect_limit; |
| 580 | } |
| 581 | |
| 582 | for (i=0; i < count; i++) { |
| 583 | prev_hash = hash; |
| 584 | hash = i ? (ext2fs_le32_to_cpu(ent[i].hash) & ~1) : 0; |
| 585 | #ifdef DX_DEBUG |
| 586 | printf("Entry #%d: Hash 0x%08x, block %u\n", i, |
| 587 | hash, ext2fs_le32_to_cpu(ent[i].block)); |
| 588 | #endif |
| 589 | blk = ext2fs_le32_to_cpu(ent[i].block) & 0x0ffffff; |
| 590 | /* Check to make sure the block is valid */ |
| 591 | if (blk >= (blk_t) dx_dir->numblocks) { |
| 592 | cd->pctx.blk = blk; |
| 593 | if (fix_problem(cd->ctx, PR_2_HTREE_BADBLK, |
| 594 | &cd->pctx)) |
| 595 | goto clear_and_exit; |
| 596 | continue; |
| 597 | } |
| 598 | if (hash < prev_hash && |
| 599 | fix_problem(cd->ctx, PR_2_HTREE_HASH_ORDER, &cd->pctx)) |
| 600 | goto clear_and_exit; |
| 601 | dx_db = &dx_dir->dx_block[blk]; |
| 602 | if (dx_db->flags & DX_FLAG_REFERENCED) { |
| 603 | dx_db->flags |= DX_FLAG_DUP_REF; |
| 604 | } else { |
| 605 | dx_db->flags |= DX_FLAG_REFERENCED; |
| 606 | dx_db->parent = db->blockcnt; |
| 607 | } |
| 608 | if (hash < min_hash) |
| 609 | min_hash = hash; |
| 610 | if (hash > max_hash) |
| 611 | max_hash = hash; |
| 612 | dx_db->node_min_hash = hash; |
| 613 | if ((i+1) < count) |
| 614 | dx_db->node_max_hash = |
| 615 | ext2fs_le32_to_cpu(ent[i+1].hash) & ~1; |
| 616 | else { |
| 617 | dx_db->node_max_hash = 0xfffffffe; |
| 618 | dx_db->flags |= DX_FLAG_LAST; |
| 619 | } |
| 620 | if (i == 0) |
| 621 | dx_db->flags |= DX_FLAG_FIRST; |
| 622 | } |
| 623 | #ifdef DX_DEBUG |
| 624 | printf("Blockcnt = %d, min hash 0x%08x, max hash 0x%08x\n", |
| 625 | db->blockcnt, min_hash, max_hash); |
| 626 | #endif |
| 627 | dx_db = &dx_dir->dx_block[db->blockcnt]; |
| 628 | dx_db->min_hash = min_hash; |
| 629 | dx_db->max_hash = max_hash; |
| 630 | return; |
| 631 | |
| 632 | clear_and_exit: |
| 633 | clear_htree(cd->ctx, cd->pctx.ino); |
| 634 | dx_dir->numblocks = 0; |
| 635 | } |
| 636 | #endif /* ENABLE_HTREE */ |
| 637 | |
| 638 | /* |
| 639 | * Given a busted directory, try to salvage it somehow. |
| 640 | * |
| 641 | */ |
| 642 | static void salvage_directory(ext2_filsys fs, |
| 643 | struct ext2_dir_entry *dirent, |
| 644 | struct ext2_dir_entry *prev, |
| 645 | unsigned int *offset) |
| 646 | { |
| 647 | char *cp = (char *) dirent; |
| 648 | int left; |
| 649 | unsigned int rec_len, prev_rec_len; |
| 650 | unsigned int name_len = dirent->name_len & 0xFF; |
| 651 | |
| 652 | (void) ext2fs_get_rec_len(fs, dirent, &rec_len); |
| 653 | left = fs->blocksize - *offset - rec_len; |
| 654 | |
| 655 | /* |
| 656 | * Special case of directory entry of size 8: copy what's left |
| 657 | * of the directory block up to cover up the invalid hole. |
| 658 | */ |
| 659 | if ((left >= 12) && (rec_len == 8)) { |
| 660 | memmove(cp, cp+8, left); |
| 661 | memset(cp + left, 0, 8); |
| 662 | return; |
| 663 | } |
| 664 | /* |
| 665 | * If the directory entry overruns the end of the directory |
| 666 | * block, and the name is small enough to fit, then adjust the |
| 667 | * record length. |
| 668 | */ |
| 669 | if ((left < 0) && |
| 670 | ((int) rec_len + left > 8) && |
| 671 | ((int) name_len + 8 <= (int) rec_len + left) && |
| 672 | dirent->inode <= fs->super->s_inodes_count && |
| 673 | strnlen(dirent->name, name_len) == name_len) { |
| 674 | (void) ext2fs_set_rec_len(fs, (int) rec_len + left, dirent); |
| 675 | return; |
| 676 | } |
| 677 | /* |
| 678 | * If the record length of the directory entry is a multiple |
| 679 | * of four, and not too big, such that it is valid, let the |
| 680 | * previous directory entry absorb the invalid one. |
| 681 | */ |
| 682 | if (prev && rec_len && (rec_len % 4) == 0 && |
| 683 | (*offset + rec_len <= fs->blocksize)) { |
| 684 | (void) ext2fs_get_rec_len(fs, prev, &prev_rec_len); |
| 685 | prev_rec_len += rec_len; |
| 686 | (void) ext2fs_set_rec_len(fs, prev_rec_len, prev); |
| 687 | *offset += rec_len; |
| 688 | return; |
| 689 | } |
| 690 | /* |
| 691 | * Default salvage method --- kill all of the directory |
| 692 | * entries for the rest of the block. We will either try to |
| 693 | * absorb it into the previous directory entry, or create a |
| 694 | * new empty directory entry the rest of the directory block. |
| 695 | */ |
| 696 | if (prev) { |
| 697 | (void) ext2fs_get_rec_len(fs, prev, &prev_rec_len); |
| 698 | prev_rec_len += fs->blocksize - *offset; |
| 699 | (void) ext2fs_set_rec_len(fs, prev_rec_len, prev); |
| 700 | *offset = fs->blocksize; |
| 701 | } else { |
| 702 | rec_len = fs->blocksize - *offset; |
| 703 | (void) ext2fs_set_rec_len(fs, rec_len, dirent); |
| 704 | dirent->name_len = 0; |
| 705 | dirent->inode = 0; |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | static int check_dir_block(ext2_filsys fs, |
| 710 | struct ext2_db_entry2 *db, |
| 711 | void *priv_data) |
| 712 | { |
| 713 | struct dx_dir_info *dx_dir; |
| 714 | #ifdef ENABLE_HTREE |
| 715 | struct dx_dirblock_info *dx_db = 0; |
| 716 | #endif /* ENABLE_HTREE */ |
| 717 | struct ext2_dir_entry *dirent, *prev; |
| 718 | ext2_dirhash_t hash; |
| 719 | unsigned int offset = 0; |
| 720 | int dir_modified = 0; |
| 721 | int dot_state; |
| 722 | unsigned int rec_len; |
| 723 | blk64_t block_nr = db->blk; |
| 724 | ext2_ino_t ino = db->ino; |
| 725 | ext2_ino_t subdir_parent; |
| 726 | __u16 links; |
| 727 | struct check_dir_struct *cd; |
| 728 | char *buf; |
| 729 | e2fsck_t ctx; |
| 730 | problem_t problem; |
| 731 | struct ext2_dx_root_info *root; |
| 732 | struct ext2_dx_countlimit *limit; |
| 733 | static dict_t de_dict; |
| 734 | struct problem_context pctx; |
| 735 | int dups_found = 0; |
| 736 | int ret; |
| 737 | |
| 738 | cd = (struct check_dir_struct *) priv_data; |
| 739 | buf = cd->buf; |
| 740 | ctx = cd->ctx; |
| 741 | |
| 742 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK || ctx->flags & E2F_FLAG_RESTART) |
| 743 | return DIRENT_ABORT; |
| 744 | |
| 745 | if (ctx->progress && (ctx->progress)(ctx, 2, cd->count++, cd->max)) |
| 746 | return DIRENT_ABORT; |
| 747 | |
| 748 | /* |
| 749 | * Make sure the inode is still in use (could have been |
| 750 | * deleted in the duplicate/bad blocks pass. |
| 751 | */ |
| 752 | if (!(ext2fs_test_inode_bitmap2(ctx->inode_used_map, ino))) |
| 753 | return 0; |
| 754 | |
| 755 | cd->pctx.ino = ino; |
| 756 | cd->pctx.blk = block_nr; |
| 757 | cd->pctx.blkcount = db->blockcnt; |
| 758 | cd->pctx.ino2 = 0; |
| 759 | cd->pctx.dirent = 0; |
| 760 | cd->pctx.num = 0; |
| 761 | |
| 762 | if (db->blk == 0) { |
| 763 | if (allocate_dir_block(ctx, db, buf, &cd->pctx)) |
| 764 | return 0; |
| 765 | block_nr = db->blk; |
| 766 | } |
| 767 | |
| 768 | if (db->blockcnt) |
| 769 | dot_state = 2; |
| 770 | else |
| 771 | dot_state = 0; |
| 772 | |
| 773 | if (ctx->dirs_to_hash && |
| 774 | ext2fs_u32_list_test(ctx->dirs_to_hash, ino)) |
| 775 | dups_found++; |
| 776 | |
| 777 | #if 0 |
| 778 | printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr, |
| 779 | db->blockcnt, ino); |
| 780 | #endif |
| 781 | |
| 782 | ehandler_operation(_("reading directory block")); |
| 783 | cd->pctx.errcode = ext2fs_read_dir_block3(fs, block_nr, buf, 0); |
| 784 | ehandler_operation(0); |
| 785 | if (cd->pctx.errcode == EXT2_ET_DIR_CORRUPTED) |
| 786 | cd->pctx.errcode = 0; /* We'll handle this ourselves */ |
| 787 | if (cd->pctx.errcode) { |
| 788 | if (!fix_problem(ctx, PR_2_READ_DIRBLOCK, &cd->pctx)) { |
| 789 | ctx->flags |= E2F_FLAG_ABORT; |
| 790 | return DIRENT_ABORT; |
| 791 | } |
| 792 | memset(buf, 0, fs->blocksize); |
| 793 | } |
| 794 | #ifdef ENABLE_HTREE |
| 795 | dx_dir = e2fsck_get_dx_dir_info(ctx, ino); |
| 796 | if (dx_dir && dx_dir->numblocks) { |
| 797 | if (db->blockcnt >= dx_dir->numblocks) { |
| 798 | if (fix_problem(ctx, PR_2_UNEXPECTED_HTREE_BLOCK, |
| 799 | &pctx)) { |
| 800 | clear_htree(ctx, ino); |
| 801 | dx_dir->numblocks = 0; |
| 802 | dx_db = 0; |
| 803 | goto out_htree; |
| 804 | } |
| 805 | fatal_error(ctx, _("Can not continue.")); |
| 806 | } |
| 807 | dx_db = &dx_dir->dx_block[db->blockcnt]; |
| 808 | dx_db->type = DX_DIRBLOCK_LEAF; |
| 809 | dx_db->phys = block_nr; |
| 810 | dx_db->min_hash = ~0; |
| 811 | dx_db->max_hash = 0; |
| 812 | |
| 813 | dirent = (struct ext2_dir_entry *) buf; |
| 814 | (void) ext2fs_get_rec_len(fs, dirent, &rec_len); |
| 815 | limit = (struct ext2_dx_countlimit *) (buf+8); |
| 816 | if (db->blockcnt == 0) { |
| 817 | root = (struct ext2_dx_root_info *) (buf + 24); |
| 818 | dx_db->type = DX_DIRBLOCK_ROOT; |
| 819 | dx_db->flags |= DX_FLAG_FIRST | DX_FLAG_LAST; |
| 820 | if ((root->reserved_zero || |
| 821 | root->info_length < 8 || |
| 822 | root->indirect_levels > 1) && |
| 823 | fix_problem(ctx, PR_2_HTREE_BAD_ROOT, &cd->pctx)) { |
| 824 | clear_htree(ctx, ino); |
| 825 | dx_dir->numblocks = 0; |
| 826 | dx_db = 0; |
| 827 | } |
| 828 | dx_dir->hashversion = root->hash_version; |
| 829 | if ((dx_dir->hashversion <= EXT2_HASH_TEA) && |
| 830 | (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH)) |
| 831 | dx_dir->hashversion += 3; |
| 832 | dx_dir->depth = root->indirect_levels + 1; |
| 833 | } else if ((dirent->inode == 0) && |
| 834 | (rec_len == fs->blocksize) && |
| 835 | (dirent->name_len == 0) && |
| 836 | (ext2fs_le16_to_cpu(limit->limit) == |
| 837 | ((fs->blocksize-8) / |
| 838 | sizeof(struct ext2_dx_entry)))) |
| 839 | dx_db->type = DX_DIRBLOCK_NODE; |
| 840 | } |
| 841 | out_htree: |
| 842 | #endif /* ENABLE_HTREE */ |
| 843 | |
| 844 | dict_init(&de_dict, DICTCOUNT_T_MAX, dict_de_cmp); |
| 845 | prev = 0; |
| 846 | do { |
| 847 | dgrp_t group; |
| 848 | ext2_ino_t first_unused_inode; |
| 849 | |
| 850 | problem = 0; |
| 851 | dirent = (struct ext2_dir_entry *) (buf + offset); |
| 852 | (void) ext2fs_get_rec_len(fs, dirent, &rec_len); |
| 853 | cd->pctx.dirent = dirent; |
| 854 | cd->pctx.num = offset; |
| 855 | if (((offset + rec_len) > fs->blocksize) || |
| 856 | (rec_len < 12) || |
| 857 | ((rec_len % 4) != 0) || |
| 858 | (((dirent->name_len & (unsigned) 0xFF)+8) > rec_len)) { |
| 859 | if (fix_problem(ctx, PR_2_DIR_CORRUPTED, &cd->pctx)) { |
| 860 | salvage_directory(fs, dirent, prev, &offset); |
| 861 | dir_modified++; |
| 862 | continue; |
| 863 | } else |
| 864 | goto abort_free_dict; |
| 865 | } |
| 866 | |
| 867 | if (dot_state == 0) { |
| 868 | if (check_dot(ctx, dirent, ino, &cd->pctx)) |
| 869 | dir_modified++; |
| 870 | } else if (dot_state == 1) { |
| 871 | ret = check_dotdot(ctx, dirent, ino, &cd->pctx); |
| 872 | if (ret < 0) |
| 873 | goto abort_free_dict; |
| 874 | if (ret) |
| 875 | dir_modified++; |
| 876 | } else if (dirent->inode == ino) { |
| 877 | problem = PR_2_LINK_DOT; |
| 878 | if (fix_problem(ctx, PR_2_LINK_DOT, &cd->pctx)) { |
| 879 | dirent->inode = 0; |
| 880 | dir_modified++; |
| 881 | goto next; |
| 882 | } |
| 883 | } |
| 884 | if (!dirent->inode) |
| 885 | goto next; |
| 886 | |
| 887 | /* |
| 888 | * Make sure the inode listed is a legal one. |
| 889 | */ |
| 890 | if (((dirent->inode != EXT2_ROOT_INO) && |
| 891 | (dirent->inode < EXT2_FIRST_INODE(fs->super))) || |
| 892 | (dirent->inode > fs->super->s_inodes_count)) { |
| 893 | problem = PR_2_BAD_INO; |
| 894 | } else if (ctx->inode_bb_map && |
| 895 | (ext2fs_test_inode_bitmap2(ctx->inode_bb_map, |
| 896 | dirent->inode))) { |
| 897 | /* |
| 898 | * If the inode is in a bad block, offer to |
| 899 | * clear it. |
| 900 | */ |
| 901 | problem = PR_2_BB_INODE; |
| 902 | } else if ((dot_state > 1) && |
| 903 | ((dirent->name_len & 0xFF) == 1) && |
| 904 | (dirent->name[0] == '.')) { |
| 905 | /* |
| 906 | * If there's a '.' entry in anything other |
| 907 | * than the first directory entry, it's a |
| 908 | * duplicate entry that should be removed. |
| 909 | */ |
| 910 | problem = PR_2_DUP_DOT; |
| 911 | } else if ((dot_state > 1) && |
| 912 | ((dirent->name_len & 0xFF) == 2) && |
| 913 | (dirent->name[0] == '.') && |
| 914 | (dirent->name[1] == '.')) { |
| 915 | /* |
| 916 | * If there's a '..' entry in anything other |
| 917 | * than the second directory entry, it's a |
| 918 | * duplicate entry that should be removed. |
| 919 | */ |
| 920 | problem = PR_2_DUP_DOT_DOT; |
| 921 | } else if ((dot_state > 1) && |
| 922 | (dirent->inode == EXT2_ROOT_INO)) { |
| 923 | /* |
| 924 | * Don't allow links to the root directory. |
| 925 | * We check this specially to make sure we |
| 926 | * catch this error case even if the root |
| 927 | * directory hasn't been created yet. |
| 928 | */ |
| 929 | problem = PR_2_LINK_ROOT; |
| 930 | } else if ((dot_state > 1) && |
| 931 | (dirent->name_len & 0xFF) == 0) { |
| 932 | /* |
| 933 | * Don't allow zero-length directory names. |
| 934 | */ |
| 935 | problem = PR_2_NULL_NAME; |
| 936 | } |
| 937 | |
| 938 | if (problem) { |
| 939 | if (fix_problem(ctx, problem, &cd->pctx)) { |
| 940 | dirent->inode = 0; |
| 941 | dir_modified++; |
| 942 | goto next; |
| 943 | } else { |
| 944 | ext2fs_unmark_valid(fs); |
| 945 | if (problem == PR_2_BAD_INO) |
| 946 | goto next; |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | /* |
| 951 | * If the inode was marked as having bad fields in |
| 952 | * pass1, process it and offer to fix/clear it. |
| 953 | * (We wait until now so that we can display the |
| 954 | * pathname to the user.) |
| 955 | */ |
| 956 | if (ctx->inode_bad_map && |
| 957 | ext2fs_test_inode_bitmap2(ctx->inode_bad_map, |
| 958 | dirent->inode)) { |
| 959 | if (e2fsck_process_bad_inode(ctx, ino, |
| 960 | dirent->inode, |
| 961 | buf + fs->blocksize)) { |
| 962 | dirent->inode = 0; |
| 963 | dir_modified++; |
| 964 | goto next; |
| 965 | } |
| 966 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK) |
| 967 | return DIRENT_ABORT; |
| 968 | } |
| 969 | |
| 970 | group = ext2fs_group_of_ino(fs, dirent->inode); |
| 971 | first_unused_inode = group * fs->super->s_inodes_per_group + |
| 972 | 1 + fs->super->s_inodes_per_group - |
| 973 | ext2fs_bg_itable_unused(fs, group); |
| 974 | cd->pctx.group = group; |
| 975 | |
| 976 | /* |
| 977 | * Check if the inode was missed out because |
| 978 | * _INODE_UNINIT flag was set or bg_itable_unused was |
| 979 | * incorrect. If so, clear the _INODE_UNINIT flag and |
| 980 | * restart e2fsck. In the future it would be nice if |
| 981 | * we could call a function in pass1.c that checks the |
| 982 | * newly visible inodes. |
| 983 | */ |
| 984 | if (ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)) { |
| 985 | pctx.num = dirent->inode; |
| 986 | if (fix_problem(ctx, PR_2_INOREF_BG_INO_UNINIT, |
| 987 | &cd->pctx)){ |
| 988 | ext2fs_bg_flags_clear(fs, group, |
| 989 | EXT2_BG_INODE_UNINIT); |
| 990 | ext2fs_mark_super_dirty(fs); |
| 991 | ctx->flags |= E2F_FLAG_RESTART_LATER; |
| 992 | } else { |
| 993 | ext2fs_unmark_valid(fs); |
| 994 | if (problem == PR_2_BAD_INO) |
| 995 | goto next; |
| 996 | } |
| 997 | } else if (dirent->inode >= first_unused_inode) { |
| 998 | pctx.num = dirent->inode; |
| 999 | if (fix_problem(ctx, PR_2_INOREF_IN_UNUSED, &cd->pctx)){ |
| 1000 | ext2fs_bg_itable_unused_set(fs, group, 0); |
| 1001 | ext2fs_mark_super_dirty(fs); |
| 1002 | ctx->flags |= E2F_FLAG_RESTART_LATER; |
| 1003 | } else { |
| 1004 | ext2fs_unmark_valid(fs); |
| 1005 | if (problem == PR_2_BAD_INO) |
| 1006 | goto next; |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | /* |
| 1011 | * Offer to clear unused inodes; if we are going to be |
| 1012 | * restarting the scan due to bg_itable_unused being |
| 1013 | * wrong, then don't clear any inodes to avoid zapping |
| 1014 | * inodes that were skipped during pass1 due to an |
| 1015 | * incorrect bg_itable_unused; we'll get any real |
| 1016 | * problems after we restart. |
| 1017 | */ |
| 1018 | if (!(ctx->flags & E2F_FLAG_RESTART_LATER) && |
| 1019 | !(ext2fs_test_inode_bitmap2(ctx->inode_used_map, |
| 1020 | dirent->inode))) |
| 1021 | problem = PR_2_UNUSED_INODE; |
| 1022 | |
| 1023 | if (problem) { |
| 1024 | if (fix_problem(ctx, problem, &cd->pctx)) { |
| 1025 | dirent->inode = 0; |
| 1026 | dir_modified++; |
| 1027 | goto next; |
| 1028 | } else { |
| 1029 | ext2fs_unmark_valid(fs); |
| 1030 | if (problem == PR_2_BAD_INO) |
| 1031 | goto next; |
| 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | if (check_name(ctx, dirent, ino, &cd->pctx)) |
| 1036 | dir_modified++; |
| 1037 | |
| 1038 | if (check_filetype(ctx, dirent, ino, &cd->pctx)) |
| 1039 | dir_modified++; |
| 1040 | |
| 1041 | #ifdef ENABLE_HTREE |
| 1042 | if (dx_db) { |
| 1043 | ext2fs_dirhash(dx_dir->hashversion, dirent->name, |
| 1044 | (dirent->name_len & 0xFF), |
| 1045 | fs->super->s_hash_seed, &hash, 0); |
| 1046 | if (hash < dx_db->min_hash) |
| 1047 | dx_db->min_hash = hash; |
| 1048 | if (hash > dx_db->max_hash) |
| 1049 | dx_db->max_hash = hash; |
| 1050 | } |
| 1051 | #endif |
| 1052 | |
| 1053 | /* |
| 1054 | * If this is a directory, then mark its parent in its |
| 1055 | * dir_info structure. If the parent field is already |
| 1056 | * filled in, then this directory has more than one |
| 1057 | * hard link. We assume the first link is correct, |
| 1058 | * and ask the user if he/she wants to clear this one. |
| 1059 | */ |
| 1060 | if ((dot_state > 1) && |
| 1061 | (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, |
| 1062 | dirent->inode))) { |
| 1063 | if (e2fsck_dir_info_get_parent(ctx, dirent->inode, |
| 1064 | &subdir_parent)) { |
| 1065 | cd->pctx.ino = dirent->inode; |
| 1066 | fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx); |
| 1067 | goto abort_free_dict; |
| 1068 | } |
| 1069 | if (subdir_parent) { |
| 1070 | cd->pctx.ino2 = subdir_parent; |
| 1071 | if (fix_problem(ctx, PR_2_LINK_DIR, |
| 1072 | &cd->pctx)) { |
| 1073 | dirent->inode = 0; |
| 1074 | dir_modified++; |
| 1075 | goto next; |
| 1076 | } |
| 1077 | cd->pctx.ino2 = 0; |
| 1078 | } else { |
| 1079 | (void) e2fsck_dir_info_set_parent(ctx, |
| 1080 | dirent->inode, ino); |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | if (dups_found) { |
| 1085 | ; |
| 1086 | } else if (dict_lookup(&de_dict, dirent)) { |
| 1087 | clear_problem_context(&pctx); |
| 1088 | pctx.ino = ino; |
| 1089 | pctx.dirent = dirent; |
| 1090 | fix_problem(ctx, PR_2_REPORT_DUP_DIRENT, &pctx); |
| 1091 | if (!ctx->dirs_to_hash) |
| 1092 | ext2fs_u32_list_create(&ctx->dirs_to_hash, 50); |
| 1093 | if (ctx->dirs_to_hash) |
| 1094 | ext2fs_u32_list_add(ctx->dirs_to_hash, ino); |
| 1095 | dups_found++; |
| 1096 | } else |
| 1097 | dict_alloc_insert(&de_dict, dirent, dirent); |
| 1098 | |
| 1099 | ext2fs_icount_increment(ctx->inode_count, dirent->inode, |
| 1100 | &links); |
| 1101 | if (links > 1) |
| 1102 | ctx->fs_links_count++; |
| 1103 | ctx->fs_total_count++; |
| 1104 | next: |
| 1105 | prev = dirent; |
| 1106 | if (dir_modified) |
| 1107 | (void) ext2fs_get_rec_len(fs, dirent, &rec_len); |
| 1108 | offset += rec_len; |
| 1109 | dot_state++; |
| 1110 | } while (offset < fs->blocksize); |
| 1111 | #if 0 |
| 1112 | printf("\n"); |
| 1113 | #endif |
| 1114 | #ifdef ENABLE_HTREE |
| 1115 | if (dx_db) { |
| 1116 | #ifdef DX_DEBUG |
| 1117 | printf("db_block %d, type %d, min_hash 0x%0x, max_hash 0x%0x\n", |
| 1118 | db->blockcnt, dx_db->type, |
| 1119 | dx_db->min_hash, dx_db->max_hash); |
| 1120 | #endif |
| 1121 | cd->pctx.dir = cd->pctx.ino; |
| 1122 | if ((dx_db->type == DX_DIRBLOCK_ROOT) || |
| 1123 | (dx_db->type == DX_DIRBLOCK_NODE)) |
| 1124 | parse_int_node(fs, db, cd, dx_dir, buf); |
| 1125 | } |
| 1126 | #endif /* ENABLE_HTREE */ |
| 1127 | if (offset != fs->blocksize) { |
| 1128 | cd->pctx.num = rec_len - fs->blocksize + offset; |
| 1129 | if (fix_problem(ctx, PR_2_FINAL_RECLEN, &cd->pctx)) { |
| 1130 | dirent->rec_len = cd->pctx.num; |
| 1131 | dir_modified++; |
| 1132 | } |
| 1133 | } |
| 1134 | if (dir_modified) { |
| 1135 | cd->pctx.errcode = ext2fs_write_dir_block3(fs, block_nr, buf, 0); |
| 1136 | if (cd->pctx.errcode) { |
| 1137 | if (!fix_problem(ctx, PR_2_WRITE_DIRBLOCK, |
| 1138 | &cd->pctx)) |
| 1139 | goto abort_free_dict; |
| 1140 | } |
| 1141 | ext2fs_mark_changed(fs); |
| 1142 | } |
| 1143 | dict_free_nodes(&de_dict); |
| 1144 | return 0; |
| 1145 | abort_free_dict: |
| 1146 | ctx->flags |= E2F_FLAG_ABORT; |
| 1147 | dict_free_nodes(&de_dict); |
| 1148 | return DIRENT_ABORT; |
| 1149 | } |
| 1150 | |
| 1151 | struct del_block { |
| 1152 | e2fsck_t ctx; |
| 1153 | e2_blkcnt_t num; |
| 1154 | }; |
| 1155 | |
| 1156 | /* |
| 1157 | * This function is called to deallocate a block, and is an interator |
| 1158 | * functioned called by deallocate inode via ext2fs_iterate_block(). |
| 1159 | */ |
| 1160 | static int deallocate_inode_block(ext2_filsys fs, |
| 1161 | blk64_t *block_nr, |
| 1162 | e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)), |
| 1163 | blk64_t ref_block EXT2FS_ATTR((unused)), |
| 1164 | int ref_offset EXT2FS_ATTR((unused)), |
| 1165 | void *priv_data) |
| 1166 | { |
| 1167 | struct del_block *p = priv_data; |
| 1168 | |
| 1169 | if (HOLE_BLKADDR(*block_nr)) |
| 1170 | return 0; |
| 1171 | if ((*block_nr < fs->super->s_first_data_block) || |
| 1172 | (*block_nr >= ext2fs_blocks_count(fs->super))) |
| 1173 | return 0; |
| 1174 | ext2fs_unmark_block_bitmap2(p->ctx->block_found_map, *block_nr); |
| 1175 | ext2fs_block_alloc_stats2(fs, *block_nr, -1); |
| 1176 | p->num++; |
| 1177 | return 0; |
| 1178 | } |
| 1179 | |
| 1180 | /* |
| 1181 | * This fuction deallocates an inode |
| 1182 | */ |
| 1183 | static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf) |
| 1184 | { |
| 1185 | ext2_filsys fs = ctx->fs; |
| 1186 | struct ext2_inode inode; |
| 1187 | struct problem_context pctx; |
| 1188 | __u32 count; |
| 1189 | struct del_block del_block; |
| 1190 | |
| 1191 | e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode"); |
| 1192 | clear_problem_context(&pctx); |
| 1193 | pctx.ino = ino; |
| 1194 | |
| 1195 | /* |
| 1196 | * Fix up the bitmaps... |
| 1197 | */ |
| 1198 | e2fsck_read_bitmaps(ctx); |
| 1199 | ext2fs_inode_alloc_stats2(fs, ino, -1, LINUX_S_ISDIR(inode.i_mode)); |
| 1200 | |
| 1201 | if (ext2fs_file_acl_block(fs, &inode) && |
| 1202 | (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) { |
| 1203 | pctx.errcode = ext2fs_adjust_ea_refcount2(fs, |
| 1204 | ext2fs_file_acl_block(fs, &inode), |
| 1205 | block_buf, -1, &count); |
| 1206 | if (pctx.errcode == EXT2_ET_BAD_EA_BLOCK_NUM) { |
| 1207 | pctx.errcode = 0; |
| 1208 | count = 1; |
| 1209 | } |
| 1210 | if (pctx.errcode) { |
| 1211 | pctx.blk = ext2fs_file_acl_block(fs, &inode); |
| 1212 | fix_problem(ctx, PR_2_ADJ_EA_REFCOUNT, &pctx); |
| 1213 | ctx->flags |= E2F_FLAG_ABORT; |
| 1214 | return; |
| 1215 | } |
| 1216 | if (count == 0) { |
| 1217 | ext2fs_unmark_block_bitmap2(ctx->block_found_map, |
| 1218 | ext2fs_file_acl_block(fs, &inode)); |
| 1219 | ext2fs_block_alloc_stats2(fs, |
| 1220 | ext2fs_file_acl_block(fs, &inode), -1); |
| 1221 | } |
| 1222 | ext2fs_file_acl_block_set(fs, &inode, 0); |
| 1223 | } |
| 1224 | |
| 1225 | if (!ext2fs_inode_has_valid_blocks2(fs, &inode)) |
| 1226 | goto clear_inode; |
| 1227 | |
| 1228 | if (LINUX_S_ISREG(inode.i_mode) && |
| 1229 | ext2fs_needs_large_file_feature(EXT2_I_SIZE(&inode))) |
| 1230 | ctx->large_files--; |
| 1231 | |
| 1232 | del_block.ctx = ctx; |
| 1233 | del_block.num = 0; |
| 1234 | pctx.errcode = ext2fs_block_iterate3(fs, ino, 0, block_buf, |
| 1235 | deallocate_inode_block, |
| 1236 | &del_block); |
| 1237 | if (pctx.errcode) { |
| 1238 | fix_problem(ctx, PR_2_DEALLOC_INODE, &pctx); |
| 1239 | ctx->flags |= E2F_FLAG_ABORT; |
| 1240 | return; |
| 1241 | } |
| 1242 | clear_inode: |
| 1243 | /* Inode may have changed by block_iterate, so reread it */ |
| 1244 | e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode"); |
| 1245 | e2fsck_clear_inode(ctx, ino, &inode, 0, "deallocate_inode"); |
| 1246 | } |
| 1247 | |
| 1248 | /* |
| 1249 | * This fuction clears the htree flag on an inode |
| 1250 | */ |
| 1251 | static void clear_htree(e2fsck_t ctx, ext2_ino_t ino) |
| 1252 | { |
| 1253 | struct ext2_inode inode; |
| 1254 | |
| 1255 | e2fsck_read_inode(ctx, ino, &inode, "clear_htree"); |
| 1256 | inode.i_flags = inode.i_flags & ~EXT2_INDEX_FL; |
| 1257 | e2fsck_write_inode(ctx, ino, &inode, "clear_htree"); |
| 1258 | if (ctx->dirs_to_hash) |
| 1259 | ext2fs_u32_list_add(ctx->dirs_to_hash, ino); |
| 1260 | } |
| 1261 | |
| 1262 | |
| 1263 | int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir, |
| 1264 | ext2_ino_t ino, char *buf) |
| 1265 | { |
| 1266 | ext2_filsys fs = ctx->fs; |
| 1267 | struct ext2_inode inode; |
| 1268 | int inode_modified = 0; |
| 1269 | int not_fixed = 0; |
| 1270 | unsigned char *frag, *fsize; |
| 1271 | struct problem_context pctx; |
| 1272 | problem_t problem = 0; |
| 1273 | |
| 1274 | e2fsck_read_inode(ctx, ino, &inode, "process_bad_inode"); |
| 1275 | |
| 1276 | clear_problem_context(&pctx); |
| 1277 | pctx.ino = ino; |
| 1278 | pctx.dir = dir; |
| 1279 | pctx.inode = &inode; |
| 1280 | |
| 1281 | if (ext2fs_file_acl_block(fs, &inode) && |
| 1282 | !(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) { |
| 1283 | if (fix_problem(ctx, PR_2_FILE_ACL_ZERO, &pctx)) { |
| 1284 | ext2fs_file_acl_block_set(fs, &inode, 0); |
| 1285 | inode_modified++; |
| 1286 | } else |
| 1287 | not_fixed++; |
| 1288 | } |
| 1289 | |
| 1290 | if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) && |
| 1291 | !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) && |
| 1292 | !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) && |
| 1293 | !(LINUX_S_ISSOCK(inode.i_mode))) |
| 1294 | problem = PR_2_BAD_MODE; |
| 1295 | else if (LINUX_S_ISCHR(inode.i_mode) |
| 1296 | && !e2fsck_pass1_check_device_inode(fs, &inode)) |
| 1297 | problem = PR_2_BAD_CHAR_DEV; |
| 1298 | else if (LINUX_S_ISBLK(inode.i_mode) |
| 1299 | && !e2fsck_pass1_check_device_inode(fs, &inode)) |
| 1300 | problem = PR_2_BAD_BLOCK_DEV; |
| 1301 | else if (LINUX_S_ISFIFO(inode.i_mode) |
| 1302 | && !e2fsck_pass1_check_device_inode(fs, &inode)) |
| 1303 | problem = PR_2_BAD_FIFO; |
| 1304 | else if (LINUX_S_ISSOCK(inode.i_mode) |
| 1305 | && !e2fsck_pass1_check_device_inode(fs, &inode)) |
| 1306 | problem = PR_2_BAD_SOCKET; |
| 1307 | else if (LINUX_S_ISLNK(inode.i_mode) |
| 1308 | && !e2fsck_pass1_check_symlink(fs, ino, &inode, buf)) { |
| 1309 | problem = PR_2_INVALID_SYMLINK; |
| 1310 | } |
| 1311 | |
| 1312 | if (problem) { |
| 1313 | if (fix_problem(ctx, problem, &pctx)) { |
| 1314 | deallocate_inode(ctx, ino, 0); |
| 1315 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK) |
| 1316 | return 0; |
| 1317 | return 1; |
| 1318 | } else |
| 1319 | not_fixed++; |
| 1320 | problem = 0; |
| 1321 | } |
| 1322 | |
| 1323 | if (inode.i_faddr) { |
| 1324 | if (fix_problem(ctx, PR_2_FADDR_ZERO, &pctx)) { |
| 1325 | inode.i_faddr = 0; |
| 1326 | inode_modified++; |
| 1327 | } else |
| 1328 | not_fixed++; |
| 1329 | } |
| 1330 | |
| 1331 | switch (fs->super->s_creator_os) { |
| 1332 | case EXT2_OS_HURD: |
| 1333 | frag = &inode.osd2.hurd2.h_i_frag; |
| 1334 | fsize = &inode.osd2.hurd2.h_i_fsize; |
| 1335 | break; |
| 1336 | default: |
| 1337 | frag = fsize = 0; |
| 1338 | } |
| 1339 | if (frag && *frag) { |
| 1340 | pctx.num = *frag; |
| 1341 | if (fix_problem(ctx, PR_2_FRAG_ZERO, &pctx)) { |
| 1342 | *frag = 0; |
| 1343 | inode_modified++; |
| 1344 | } else |
| 1345 | not_fixed++; |
| 1346 | pctx.num = 0; |
| 1347 | } |
| 1348 | if (fsize && *fsize) { |
| 1349 | pctx.num = *fsize; |
| 1350 | if (fix_problem(ctx, PR_2_FSIZE_ZERO, &pctx)) { |
| 1351 | *fsize = 0; |
| 1352 | inode_modified++; |
| 1353 | } else |
| 1354 | not_fixed++; |
| 1355 | pctx.num = 0; |
| 1356 | } |
| 1357 | |
| 1358 | if ((fs->super->s_creator_os == EXT2_OS_LINUX) && |
| 1359 | !(fs->super->s_feature_ro_compat & |
| 1360 | EXT4_FEATURE_RO_COMPAT_HUGE_FILE) && |
| 1361 | (inode.osd2.linux2.l_i_blocks_hi != 0)) { |
| 1362 | pctx.num = inode.osd2.linux2.l_i_blocks_hi; |
| 1363 | if (fix_problem(ctx, PR_2_BLOCKS_HI_ZERO, &pctx)) { |
| 1364 | inode.osd2.linux2.l_i_blocks_hi = 0; |
| 1365 | inode_modified++; |
| 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | if (!(fs->super->s_feature_incompat & |
| 1370 | EXT4_FEATURE_INCOMPAT_64BIT) && |
| 1371 | inode.osd2.linux2.l_i_file_acl_high != 0) { |
| 1372 | pctx.num = inode.osd2.linux2.l_i_file_acl_high; |
| 1373 | if (fix_problem(ctx, PR_2_I_FILE_ACL_HI_ZERO, &pctx)) { |
| 1374 | inode.osd2.linux2.l_i_file_acl_high = 0; |
| 1375 | inode_modified++; |
| 1376 | } else |
| 1377 | not_fixed++; |
| 1378 | } |
| 1379 | |
| 1380 | if (ext2fs_file_acl_block(fs, &inode) && |
| 1381 | ((ext2fs_file_acl_block(fs, &inode) < fs->super->s_first_data_block) || |
| 1382 | (ext2fs_file_acl_block(fs, &inode) >= ext2fs_blocks_count(fs->super)))) { |
| 1383 | if (fix_problem(ctx, PR_2_FILE_ACL_BAD, &pctx)) { |
| 1384 | ext2fs_file_acl_block_set(fs, &inode, 0); |
| 1385 | inode_modified++; |
| 1386 | } else |
| 1387 | not_fixed++; |
| 1388 | } |
| 1389 | if (inode.i_dir_acl && |
| 1390 | LINUX_S_ISDIR(inode.i_mode)) { |
| 1391 | if (fix_problem(ctx, PR_2_DIR_ACL_ZERO, &pctx)) { |
| 1392 | inode.i_dir_acl = 0; |
| 1393 | inode_modified++; |
| 1394 | } else |
| 1395 | not_fixed++; |
| 1396 | } |
| 1397 | |
| 1398 | if (inode_modified) |
| 1399 | e2fsck_write_inode(ctx, ino, &inode, "process_bad_inode"); |
| 1400 | if (!not_fixed && ctx->inode_bad_map) |
| 1401 | ext2fs_unmark_inode_bitmap2(ctx->inode_bad_map, ino); |
| 1402 | return 0; |
| 1403 | } |
| 1404 | |
| 1405 | |
| 1406 | /* |
| 1407 | * allocate_dir_block --- this function allocates a new directory |
| 1408 | * block for a particular inode; this is done if a directory has |
| 1409 | * a "hole" in it, or if a directory has a illegal block number |
| 1410 | * that was zeroed out and now needs to be replaced. |
| 1411 | */ |
| 1412 | static int allocate_dir_block(e2fsck_t ctx, |
| 1413 | struct ext2_db_entry2 *db, |
| 1414 | char *buf EXT2FS_ATTR((unused)), |
| 1415 | struct problem_context *pctx) |
| 1416 | { |
| 1417 | ext2_filsys fs = ctx->fs; |
| 1418 | blk64_t blk; |
| 1419 | char *block; |
| 1420 | struct ext2_inode inode; |
| 1421 | |
| 1422 | if (fix_problem(ctx, PR_2_DIRECTORY_HOLE, pctx) == 0) |
| 1423 | return 1; |
| 1424 | |
| 1425 | /* |
| 1426 | * Read the inode and block bitmaps in; we'll be messing with |
| 1427 | * them. |
| 1428 | */ |
| 1429 | e2fsck_read_bitmaps(ctx); |
| 1430 | |
| 1431 | /* |
| 1432 | * First, find a free block |
| 1433 | */ |
| 1434 | pctx->errcode = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk); |
| 1435 | if (pctx->errcode) { |
| 1436 | pctx->str = "ext2fs_new_block"; |
| 1437 | fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx); |
| 1438 | return 1; |
| 1439 | } |
| 1440 | ext2fs_mark_block_bitmap2(ctx->block_found_map, blk); |
| 1441 | ext2fs_mark_block_bitmap2(fs->block_map, blk); |
| 1442 | ext2fs_mark_bb_dirty(fs); |
| 1443 | |
| 1444 | /* |
| 1445 | * Now let's create the actual data block for the inode |
| 1446 | */ |
| 1447 | if (db->blockcnt) |
| 1448 | pctx->errcode = ext2fs_new_dir_block(fs, 0, 0, &block); |
| 1449 | else |
| 1450 | pctx->errcode = ext2fs_new_dir_block(fs, db->ino, |
| 1451 | EXT2_ROOT_INO, &block); |
| 1452 | |
| 1453 | if (pctx->errcode) { |
| 1454 | pctx->str = "ext2fs_new_dir_block"; |
| 1455 | fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx); |
| 1456 | return 1; |
| 1457 | } |
| 1458 | |
| 1459 | pctx->errcode = ext2fs_write_dir_block3(fs, blk, block, 0); |
| 1460 | ext2fs_free_mem(&block); |
| 1461 | if (pctx->errcode) { |
| 1462 | pctx->str = "ext2fs_write_dir_block"; |
| 1463 | fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx); |
| 1464 | return 1; |
| 1465 | } |
| 1466 | |
| 1467 | /* |
| 1468 | * Update the inode block count |
| 1469 | */ |
| 1470 | e2fsck_read_inode(ctx, db->ino, &inode, "allocate_dir_block"); |
| 1471 | ext2fs_iblk_add_blocks(fs, &inode, 1); |
| 1472 | if (inode.i_size < (db->blockcnt+1) * fs->blocksize) |
| 1473 | inode.i_size = (db->blockcnt+1) * fs->blocksize; |
| 1474 | e2fsck_write_inode(ctx, db->ino, &inode, "allocate_dir_block"); |
| 1475 | |
| 1476 | /* |
| 1477 | * Finally, update the block pointers for the inode |
| 1478 | */ |
| 1479 | db->blk = blk; |
| 1480 | pctx->errcode = ext2fs_bmap2(fs, db->ino, &inode, 0, BMAP_SET, |
| 1481 | db->blockcnt, 0, &blk); |
| 1482 | if (pctx->errcode) { |
| 1483 | pctx->str = "ext2fs_block_iterate"; |
| 1484 | fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx); |
| 1485 | return 1; |
| 1486 | } |
| 1487 | |
| 1488 | return 0; |
| 1489 | } |