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 "support/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_block2(ext2_filsys fs, 65 struct ext2_db_entry2 *dir_blocks_info, 66 void *priv_data); 67 static int check_dir_block(ext2_filsys fs, 68 struct ext2_db_entry2 *dir_blocks_info, 69 void *priv_data); 70 static int allocate_dir_block(e2fsck_t ctx, 71 struct ext2_db_entry2 *dir_blocks_info, 72 char *buf, struct problem_context *pctx); 73 static void clear_htree(e2fsck_t ctx, ext2_ino_t ino); 74 static int htree_depth(struct dx_dir_info *dx_dir, 75 struct dx_dirblock_info *dx_db); 76 static EXT2_QSORT_TYPE special_dir_block_cmp(const void *a, const void *b); 77 78 struct check_dir_struct { 79 char *buf; 80 struct problem_context pctx; 81 int count, max; 82 e2fsck_t ctx; 83 unsigned long long list_offset; 84 unsigned long long ra_entries; 85 unsigned long long next_ra_off; 86 }; 87 88 void e2fsck_pass2(e2fsck_t ctx) 89 { 90 struct ext2_super_block *sb = ctx->fs->super; 91 struct problem_context pctx; 92 ext2_filsys fs = ctx->fs; 93 char *buf; 94 #ifdef RESOURCE_TRACK 95 struct resource_track rtrack; 96 #endif 97 struct check_dir_struct cd; 98 struct dx_dir_info *dx_dir; 99 struct dx_dirblock_info *dx_db, *dx_parent; 100 int b; 101 int i, depth; 102 problem_t code; 103 int bad_dir; 104 int (*check_dir_func)(ext2_filsys fs, 105 struct ext2_db_entry2 *dir_blocks_info, 106 void *priv_data); 107 108 init_resource_track(&rtrack, ctx->fs->io); 109 clear_problem_context(&cd.pctx); 110 111 #ifdef MTRACE 112 mtrace_print("Pass 2"); 113 #endif 114 115 if (!(ctx->options & E2F_OPT_PREEN)) 116 fix_problem(ctx, PR_2_PASS_HEADER, &cd.pctx); 117 118 cd.pctx.errcode = e2fsck_setup_icount(ctx, "inode_count", 119 EXT2_ICOUNT_OPT_INCREMENT, 120 ctx->inode_link_info, &ctx->inode_count); 121 if (cd.pctx.errcode) { 122 fix_problem(ctx, PR_2_ALLOCATE_ICOUNT, &cd.pctx); 123 ctx->flags |= E2F_FLAG_ABORT; 124 return; 125 } 126 buf = (char *) e2fsck_allocate_memory(ctx, 2*fs->blocksize, 127 "directory scan buffer"); 128 129 /* 130 * Set up the parent pointer for the root directory, if 131 * present. (If the root directory is not present, we will 132 * create it in pass 3.) 133 */ 134 (void) e2fsck_dir_info_set_parent(ctx, EXT2_ROOT_INO, EXT2_ROOT_INO); 135 136 cd.buf = buf; 137 cd.ctx = ctx; 138 cd.count = 1; 139 cd.max = ext2fs_dblist_count2(fs->dblist); 140 cd.list_offset = 0; 141 cd.ra_entries = ctx->readahead_kb * 1024 / ctx->fs->blocksize; 142 cd.next_ra_off = 0; 143 144 if (ctx->progress) 145 (void) (ctx->progress)(ctx, 2, 0, cd.max); 146 147 if (ext2fs_has_feature_dir_index(fs->super)) 148 ext2fs_dblist_sort2(fs->dblist, special_dir_block_cmp); 149 150 check_dir_func = cd.ra_entries ? check_dir_block2 : check_dir_block; 151 cd.pctx.errcode = ext2fs_dblist_iterate2(fs->dblist, check_dir_func, 152 &cd); 153 if (ctx->flags & E2F_FLAG_RESTART_LATER) { 154 ctx->flags |= E2F_FLAG_RESTART; 155 ctx->flags &= ~E2F_FLAG_RESTART_LATER; 156 } 157 158 if (ctx->flags & E2F_FLAG_RUN_RETURN) 159 return; 160 161 if (cd.pctx.errcode) { 162 fix_problem(ctx, PR_2_DBLIST_ITERATE, &cd.pctx); 163 ctx->flags |= E2F_FLAG_ABORT; 164 return; 165 } 166 167 for (i=0; (dx_dir = e2fsck_dx_dir_info_iter(ctx, &i)) != 0;) { 168 if (ctx->flags & E2F_FLAG_SIGNAL_MASK) 169 return; 170 if (e2fsck_dir_will_be_rehashed(ctx, dx_dir->ino) || 171 dx_dir->numblocks == 0) 172 continue; 173 clear_problem_context(&pctx); 174 bad_dir = 0; 175 pctx.dir = dx_dir->ino; 176 dx_db = dx_dir->dx_block; 177 if (dx_db->flags & DX_FLAG_REFERENCED) 178 dx_db->flags |= DX_FLAG_DUP_REF; 179 else 180 dx_db->flags |= DX_FLAG_REFERENCED; 181 /* 182 * Find all of the first and last leaf blocks, and 183 * update their parent's min and max hash values 184 */ 185 for (b=0, dx_db = dx_dir->dx_block; 186 b < dx_dir->numblocks; 187 b++, dx_db++) { 188 if ((dx_db->type != DX_DIRBLOCK_LEAF) || 189 !(dx_db->flags & (DX_FLAG_FIRST | DX_FLAG_LAST))) 190 continue; 191 dx_parent = &dx_dir->dx_block[dx_db->parent]; 192 /* 193 * XXX Make sure dx_parent->min_hash > dx_db->min_hash 194 */ 195 if (dx_db->flags & DX_FLAG_FIRST) 196 dx_parent->min_hash = dx_db->min_hash; 197 /* 198 * XXX Make sure dx_parent->max_hash < dx_db->max_hash 199 */ 200 if (dx_db->flags & DX_FLAG_LAST) 201 dx_parent->max_hash = dx_db->max_hash; 202 } 203 204 for (b=0, dx_db = dx_dir->dx_block; 205 b < dx_dir->numblocks; 206 b++, dx_db++) { 207 pctx.blkcount = b; 208 pctx.group = dx_db->parent; 209 code = 0; 210 if (!(dx_db->flags & DX_FLAG_FIRST) && 211 (dx_db->min_hash < dx_db->node_min_hash)) { 212 pctx.blk = dx_db->min_hash; 213 pctx.blk2 = dx_db->node_min_hash; 214 code = PR_2_HTREE_MIN_HASH; 215 fix_problem(ctx, code, &pctx); 216 bad_dir++; 217 } 218 if (dx_db->type == DX_DIRBLOCK_LEAF) { 219 depth = htree_depth(dx_dir, dx_db); 220 if (depth != dx_dir->depth) { 221 pctx.num = dx_dir->depth; 222 code = PR_2_HTREE_BAD_DEPTH; 223 fix_problem(ctx, code, &pctx); 224 bad_dir++; 225 } 226 } 227 /* 228 * This test doesn't apply for the root block 229 * at block #0 230 */ 231 if (b && 232 (dx_db->max_hash > dx_db->node_max_hash)) { 233 pctx.blk = dx_db->max_hash; 234 pctx.blk2 = dx_db->node_max_hash; 235 code = PR_2_HTREE_MAX_HASH; 236 fix_problem(ctx, code, &pctx); 237 bad_dir++; 238 } 239 if (!(dx_db->flags & DX_FLAG_REFERENCED)) { 240 code = PR_2_HTREE_NOTREF; 241 fix_problem(ctx, code, &pctx); 242 bad_dir++; 243 } else if (dx_db->flags & DX_FLAG_DUP_REF) { 244 code = PR_2_HTREE_DUPREF; 245 fix_problem(ctx, code, &pctx); 246 bad_dir++; 247 } 248 } 249 if (bad_dir && fix_problem(ctx, PR_2_HTREE_CLEAR, &pctx)) { 250 clear_htree(ctx, dx_dir->ino); 251 dx_dir->numblocks = 0; 252 } 253 } 254 e2fsck_free_dx_dir_info(ctx); 255 256 ext2fs_free_mem(&buf); 257 ext2fs_free_dblist(fs->dblist); 258 259 if (ctx->inode_bad_map) { 260 ext2fs_free_inode_bitmap(ctx->inode_bad_map); 261 ctx->inode_bad_map = 0; 262 } 263 if (ctx->inode_reg_map) { 264 ext2fs_free_inode_bitmap(ctx->inode_reg_map); 265 ctx->inode_reg_map = 0; 266 } 267 if (ctx->encrypted_dirs) { 268 ext2fs_u32_list_free(ctx->encrypted_dirs); 269 ctx->encrypted_dirs = 0; 270 } 271 272 clear_problem_context(&pctx); 273 if (ctx->large_files) { 274 if (!ext2fs_has_feature_large_file(sb) && 275 fix_problem(ctx, PR_2_FEATURE_LARGE_FILES, &pctx)) { 276 ext2fs_set_feature_large_file(sb); 277 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY; 278 ext2fs_mark_super_dirty(fs); 279 } 280 if (sb->s_rev_level == EXT2_GOOD_OLD_REV && 281 fix_problem(ctx, PR_1_FS_REV_LEVEL, &pctx)) { 282 ext2fs_update_dynamic_rev(fs); 283 ext2fs_mark_super_dirty(fs); 284 } 285 } 286 287 print_resource_track(ctx, _("Pass 2"), &rtrack, fs->io); 288 } 289 290 #define MAX_DEPTH 32000 291 static int htree_depth(struct dx_dir_info *dx_dir, 292 struct dx_dirblock_info *dx_db) 293 { 294 int depth = 0; 295 296 while (dx_db->type != DX_DIRBLOCK_ROOT && depth < MAX_DEPTH) { 297 dx_db = &dx_dir->dx_block[dx_db->parent]; 298 depth++; 299 } 300 return depth; 301 } 302 303 static int dict_de_cmp(const void *a, const void *b) 304 { 305 const struct ext2_dir_entry *de_a, *de_b; 306 int a_len, b_len; 307 308 de_a = (const struct ext2_dir_entry *) a; 309 a_len = ext2fs_dirent_name_len(de_a); 310 de_b = (const struct ext2_dir_entry *) b; 311 b_len = ext2fs_dirent_name_len(de_b); 312 313 if (a_len != b_len) 314 return (a_len - b_len); 315 316 return memcmp(de_a->name, de_b->name, a_len); 317 } 318 319 /* 320 * This is special sort function that makes sure that directory blocks 321 * with a dirblock of zero are sorted to the beginning of the list. 322 * This guarantees that the root node of the htree directories are 323 * processed first, so we know what hash version to use. 324 */ 325 static EXT2_QSORT_TYPE special_dir_block_cmp(const void *a, const void *b) 326 { 327 const struct ext2_db_entry2 *db_a = 328 (const struct ext2_db_entry2 *) a; 329 const struct ext2_db_entry2 *db_b = 330 (const struct ext2_db_entry2 *) b; 331 332 if (db_a->blockcnt && !db_b->blockcnt) 333 return 1; 334 335 if (!db_a->blockcnt && db_b->blockcnt) 336 return -1; 337 338 if (db_a->blk != db_b->blk) 339 return (int) (db_a->blk - db_b->blk); 340 341 if (db_a->ino != db_b->ino) 342 return (int) (db_a->ino - db_b->ino); 343 344 return (int) (db_a->blockcnt - db_b->blockcnt); 345 } 346 347 348 /* 349 * Make sure the first entry in the directory is '.', and that the 350 * directory entry is sane. 351 */ 352 static int check_dot(e2fsck_t ctx, 353 struct ext2_dir_entry *dirent, 354 ext2_ino_t ino, struct problem_context *pctx) 355 { 356 struct ext2_dir_entry *nextdir; 357 unsigned int rec_len, new_len; 358 int status = 0; 359 int created = 0; 360 problem_t problem = 0; 361 362 if (!dirent->inode) 363 problem = PR_2_MISSING_DOT; 364 else if ((ext2fs_dirent_name_len(dirent) != 1) || 365 (dirent->name[0] != '.')) 366 problem = PR_2_1ST_NOT_DOT; 367 else if (dirent->name[1] != '\0') 368 problem = PR_2_DOT_NULL_TERM; 369 370 (void) ext2fs_get_rec_len(ctx->fs, dirent, &rec_len); 371 if (problem) { 372 if (fix_problem(ctx, problem, pctx)) { 373 if (rec_len < 12) 374 rec_len = dirent->rec_len = 12; 375 dirent->inode = ino; 376 ext2fs_dirent_set_name_len(dirent, 1); 377 ext2fs_dirent_set_file_type(dirent, EXT2_FT_UNKNOWN); 378 dirent->name[0] = '.'; 379 dirent->name[1] = '\0'; 380 status = 1; 381 created = 1; 382 } 383 } 384 if (dirent->inode != ino) { 385 if (fix_problem(ctx, PR_2_BAD_INODE_DOT, pctx)) { 386 dirent->inode = ino; 387 status = 1; 388 } 389 } 390 if (rec_len > 12) { 391 new_len = rec_len - 12; 392 if (new_len > 12) { 393 if (created || 394 fix_problem(ctx, PR_2_SPLIT_DOT, pctx)) { 395 nextdir = (struct ext2_dir_entry *) 396 ((char *) dirent + 12); 397 dirent->rec_len = 12; 398 (void) ext2fs_set_rec_len(ctx->fs, new_len, 399 nextdir); 400 nextdir->inode = 0; 401 ext2fs_dirent_set_name_len(nextdir, 0); 402 ext2fs_dirent_set_file_type(nextdir, 403 EXT2_FT_UNKNOWN); 404 status = 1; 405 } 406 } 407 } 408 return status; 409 } 410 411 /* 412 * Make sure the second entry in the directory is '..', and that the 413 * directory entry is sane. We do not check the inode number of '..' 414 * here; this gets done in pass 3. 415 */ 416 static int check_dotdot(e2fsck_t ctx, 417 struct ext2_dir_entry *dirent, 418 ext2_ino_t ino, struct problem_context *pctx) 419 { 420 problem_t problem = 0; 421 unsigned int rec_len; 422 423 if (!dirent->inode) 424 problem = PR_2_MISSING_DOT_DOT; 425 else if ((ext2fs_dirent_name_len(dirent) != 2) || 426 (dirent->name[0] != '.') || 427 (dirent->name[1] != '.')) 428 problem = PR_2_2ND_NOT_DOT_DOT; 429 else if (dirent->name[2] != '\0') 430 problem = PR_2_DOT_DOT_NULL_TERM; 431 432 (void) ext2fs_get_rec_len(ctx->fs, dirent, &rec_len); 433 if (problem) { 434 if (fix_problem(ctx, problem, pctx)) { 435 if (rec_len < 12) 436 dirent->rec_len = 12; 437 /* 438 * Note: we don't have the parent inode just 439 * yet, so we will fill it in with the root 440 * inode. This will get fixed in pass 3. 441 */ 442 dirent->inode = EXT2_ROOT_INO; 443 ext2fs_dirent_set_name_len(dirent, 2); 444 ext2fs_dirent_set_file_type(dirent, EXT2_FT_UNKNOWN); 445 dirent->name[0] = '.'; 446 dirent->name[1] = '.'; 447 dirent->name[2] = '\0'; 448 return 1; 449 } 450 return 0; 451 } 452 if (e2fsck_dir_info_set_dotdot(ctx, ino, dirent->inode)) { 453 fix_problem(ctx, PR_2_NO_DIRINFO, pctx); 454 return -1; 455 } 456 return 0; 457 } 458 459 /* 460 * Check to make sure a directory entry doesn't contain any illegal 461 * characters. 462 */ 463 static int check_name(e2fsck_t ctx, 464 struct ext2_dir_entry *dirent, 465 struct problem_context *pctx) 466 { 467 int i; 468 int fixup = -1; 469 int ret = 0; 470 471 for ( i = 0; i < ext2fs_dirent_name_len(dirent); i++) { 472 if (dirent->name[i] != '/' && dirent->name[i] != '\0') 473 continue; 474 if (fixup < 0) 475 fixup = fix_problem(ctx, PR_2_BAD_NAME, pctx); 476 if (fixup == 0) 477 return 0; 478 dirent->name[i] = '.'; 479 ret = 1; 480 } 481 return ret; 482 } 483 484 static int encrypted_check_name(e2fsck_t ctx, 485 struct ext2_dir_entry *dirent, 486 struct problem_context *pctx) 487 { 488 if (ext2fs_dirent_name_len(dirent) < EXT4_CRYPTO_BLOCK_SIZE) { 489 if (fix_problem(ctx, PR_2_BAD_ENCRYPTED_NAME, pctx)) { 490 dirent->inode = 0; 491 return 1; 492 } 493 ext2fs_unmark_valid(ctx->fs); 494 } 495 return 0; 496 } 497 498 /* 499 * Check the directory filetype (if present) 500 */ 501 static _INLINE_ int check_filetype(e2fsck_t ctx, 502 struct ext2_dir_entry *dirent, 503 ext2_ino_t dir_ino EXT2FS_ATTR((unused)), 504 struct problem_context *pctx) 505 { 506 int filetype = ext2fs_dirent_file_type(dirent); 507 int should_be = EXT2_FT_UNKNOWN; 508 struct ext2_inode inode; 509 510 if (!ext2fs_has_feature_filetype(ctx->fs->super)) { 511 if (filetype == 0 || 512 !fix_problem(ctx, PR_2_CLEAR_FILETYPE, pctx)) 513 return 0; 514 ext2fs_dirent_set_file_type(dirent, EXT2_FT_UNKNOWN); 515 return 1; 516 } 517 518 if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, dirent->inode)) { 519 should_be = EXT2_FT_DIR; 520 } else if (ext2fs_test_inode_bitmap2(ctx->inode_reg_map, 521 dirent->inode)) { 522 should_be = EXT2_FT_REG_FILE; 523 } else if (ctx->inode_bad_map && 524 ext2fs_test_inode_bitmap2(ctx->inode_bad_map, 525 dirent->inode)) 526 should_be = 0; 527 else { 528 e2fsck_read_inode(ctx, dirent->inode, &inode, 529 "check_filetype"); 530 should_be = ext2_file_type(inode.i_mode); 531 } 532 if (filetype == should_be) 533 return 0; 534 pctx->num = should_be; 535 536 if (fix_problem(ctx, filetype ? PR_2_BAD_FILETYPE : PR_2_SET_FILETYPE, 537 pctx) == 0) 538 return 0; 539 540 ext2fs_dirent_set_file_type(dirent, should_be); 541 return 1; 542 } 543 544 static void parse_int_node(ext2_filsys fs, 545 struct ext2_db_entry2 *db, 546 struct check_dir_struct *cd, 547 struct dx_dir_info *dx_dir, 548 char *block_buf, int failed_csum) 549 { 550 struct ext2_dx_root_info *root; 551 struct ext2_dx_entry *ent; 552 struct ext2_dx_countlimit *limit; 553 struct dx_dirblock_info *dx_db; 554 int i, expect_limit, count; 555 blk_t blk; 556 ext2_dirhash_t min_hash = 0xffffffff; 557 ext2_dirhash_t max_hash = 0; 558 ext2_dirhash_t hash = 0, prev_hash; 559 int csum_size = 0; 560 561 if (db->blockcnt == 0) { 562 root = (struct ext2_dx_root_info *) (block_buf + 24); 563 564 #ifdef DX_DEBUG 565 printf("Root node dump:\n"); 566 printf("\t Reserved zero: %u\n", root->reserved_zero); 567 printf("\t Hash Version: %d\n", root->hash_version); 568 printf("\t Info length: %d\n", root->info_length); 569 printf("\t Indirect levels: %d\n", root->indirect_levels); 570 printf("\t Flags: %d\n", root->unused_flags); 571 #endif 572 573 ent = (struct ext2_dx_entry *) (block_buf + 24 + root->info_length); 574 575 if (failed_csum && 576 (e2fsck_dir_will_be_rehashed(cd->ctx, cd->pctx.ino) || 577 fix_problem(cd->ctx, PR_2_HTREE_ROOT_CSUM_INVALID, 578 &cd->pctx))) 579 goto clear_and_exit; 580 } else { 581 ent = (struct ext2_dx_entry *) (block_buf+8); 582 583 if (failed_csum && 584 (e2fsck_dir_will_be_rehashed(cd->ctx, cd->pctx.ino) || 585 fix_problem(cd->ctx, PR_2_HTREE_NODE_CSUM_INVALID, 586 &cd->pctx))) 587 goto clear_and_exit; 588 } 589 590 limit = (struct ext2_dx_countlimit *) ent; 591 592 #ifdef DX_DEBUG 593 printf("Number of entries (count): %d\n", 594 ext2fs_le16_to_cpu(limit->count)); 595 printf("Number of entries (limit): %d\n", 596 ext2fs_le16_to_cpu(limit->limit)); 597 #endif 598 599 count = ext2fs_le16_to_cpu(limit->count); 600 if (ext2fs_has_feature_metadata_csum(fs->super)) 601 csum_size = sizeof(struct ext2_dx_tail); 602 expect_limit = (fs->blocksize - 603 (csum_size + ((char *) ent - block_buf))) / 604 sizeof(struct ext2_dx_entry); 605 if (ext2fs_le16_to_cpu(limit->limit) != expect_limit) { 606 cd->pctx.num = ext2fs_le16_to_cpu(limit->limit); 607 if (fix_problem(cd->ctx, PR_2_HTREE_BAD_LIMIT, &cd->pctx)) 608 goto clear_and_exit; 609 } 610 if (count > expect_limit) { 611 cd->pctx.num = count; 612 if (fix_problem(cd->ctx, PR_2_HTREE_BAD_COUNT, &cd->pctx)) 613 goto clear_and_exit; 614 count = expect_limit; 615 } 616 617 for (i=0; i < count; i++) { 618 prev_hash = hash; 619 hash = i ? (ext2fs_le32_to_cpu(ent[i].hash) & ~1) : 0; 620 #ifdef DX_DEBUG 621 printf("Entry #%d: Hash 0x%08x, block %u\n", i, 622 hash, ext2fs_le32_to_cpu(ent[i].block)); 623 #endif 624 blk = ext2fs_le32_to_cpu(ent[i].block) & 0x0ffffff; 625 /* Check to make sure the block is valid */ 626 if (blk >= (blk_t) dx_dir->numblocks) { 627 cd->pctx.blk = blk; 628 if (fix_problem(cd->ctx, PR_2_HTREE_BADBLK, 629 &cd->pctx)) 630 goto clear_and_exit; 631 continue; 632 } 633 if (hash < prev_hash && 634 fix_problem(cd->ctx, PR_2_HTREE_HASH_ORDER, &cd->pctx)) 635 goto clear_and_exit; 636 dx_db = &dx_dir->dx_block[blk]; 637 if (dx_db->flags & DX_FLAG_REFERENCED) { 638 dx_db->flags |= DX_FLAG_DUP_REF; 639 } else { 640 dx_db->flags |= DX_FLAG_REFERENCED; 641 dx_db->parent = db->blockcnt; 642 } 643 if (hash < min_hash) 644 min_hash = hash; 645 if (hash > max_hash) 646 max_hash = hash; 647 dx_db->node_min_hash = hash; 648 if ((i+1) < count) 649 dx_db->node_max_hash = 650 ext2fs_le32_to_cpu(ent[i+1].hash) & ~1; 651 else { 652 dx_db->node_max_hash = 0xfffffffe; 653 dx_db->flags |= DX_FLAG_LAST; 654 } 655 if (i == 0) 656 dx_db->flags |= DX_FLAG_FIRST; 657 } 658 #ifdef DX_DEBUG 659 printf("Blockcnt = %d, min hash 0x%08x, max hash 0x%08x\n", 660 db->blockcnt, min_hash, max_hash); 661 #endif 662 dx_db = &dx_dir->dx_block[db->blockcnt]; 663 dx_db->min_hash = min_hash; 664 dx_db->max_hash = max_hash; 665 return; 666 667 clear_and_exit: 668 clear_htree(cd->ctx, cd->pctx.ino); 669 dx_dir->numblocks = 0; 670 e2fsck_rehash_dir_later(cd->ctx, cd->pctx.ino); 671 } 672 673 /* 674 * Given a busted directory, try to salvage it somehow. 675 * 676 */ 677 static void salvage_directory(ext2_filsys fs, 678 struct ext2_dir_entry *dirent, 679 struct ext2_dir_entry *prev, 680 unsigned int *offset, 681 unsigned int block_len) 682 { 683 char *cp = (char *) dirent; 684 int left; 685 unsigned int rec_len, prev_rec_len; 686 unsigned int name_len; 687 688 /* 689 * If the space left for the entry is too small to be an entry, 690 * we can't access dirent's fields, so plumb in the values needed 691 * so that the previous entry absorbs this one. 692 */ 693 if (block_len - *offset < EXT2_DIR_ENTRY_HEADER_LEN) { 694 name_len = 0; 695 rec_len = block_len - *offset; 696 } else { 697 name_len = ext2fs_dirent_name_len(dirent); 698 (void) ext2fs_get_rec_len(fs, dirent, &rec_len); 699 } 700 left = block_len - *offset - rec_len; 701 702 /* 703 * Special case of directory entry of size 8: copy what's left 704 * of the directory block up to cover up the invalid hole. 705 */ 706 if ((left >= 12) && (rec_len == EXT2_DIR_ENTRY_HEADER_LEN)) { 707 memmove(cp, cp+EXT2_DIR_ENTRY_HEADER_LEN, left); 708 memset(cp + left, 0, EXT2_DIR_ENTRY_HEADER_LEN); 709 return; 710 } 711 /* 712 * If the directory entry overruns the end of the directory 713 * block, and the name is small enough to fit, then adjust the 714 * record length. 715 */ 716 if ((left < 0) && 717 ((int) rec_len + left > EXT2_DIR_ENTRY_HEADER_LEN) && 718 ((int) name_len + EXT2_DIR_ENTRY_HEADER_LEN <= (int) rec_len + left) && 719 dirent->inode <= fs->super->s_inodes_count && 720 strnlen(dirent->name, name_len) == name_len) { 721 (void) ext2fs_set_rec_len(fs, (int) rec_len + left, dirent); 722 return; 723 } 724 /* 725 * If the record length of the directory entry is a multiple 726 * of four, and not too big, such that it is valid, let the 727 * previous directory entry absorb the invalid one. 728 */ 729 if (prev && rec_len && (rec_len % 4) == 0 && 730 (*offset + rec_len <= block_len)) { 731 (void) ext2fs_get_rec_len(fs, prev, &prev_rec_len); 732 prev_rec_len += rec_len; 733 (void) ext2fs_set_rec_len(fs, prev_rec_len, prev); 734 *offset += rec_len; 735 return; 736 } 737 /* 738 * Default salvage method --- kill all of the directory 739 * entries for the rest of the block. We will either try to 740 * absorb it into the previous directory entry, or create a 741 * new empty directory entry the rest of the directory block. 742 */ 743 if (prev) { 744 (void) ext2fs_get_rec_len(fs, prev, &prev_rec_len); 745 prev_rec_len += block_len - *offset; 746 (void) ext2fs_set_rec_len(fs, prev_rec_len, prev); 747 *offset = fs->blocksize; 748 } else { 749 rec_len = block_len - *offset; 750 (void) ext2fs_set_rec_len(fs, rec_len, dirent); 751 ext2fs_dirent_set_name_len(dirent, 0); 752 ext2fs_dirent_set_file_type(dirent, EXT2_FT_UNKNOWN); 753 dirent->inode = 0; 754 } 755 } 756 757 #define NEXT_DIRENT(d) ((void *)((char *)(d) + (d)->rec_len)) 758 static errcode_t insert_dirent_tail(ext2_filsys fs, void *dirbuf) 759 { 760 struct ext2_dir_entry *d; 761 void *top; 762 struct ext2_dir_entry_tail *t; 763 764 d = dirbuf; 765 top = EXT2_DIRENT_TAIL(dirbuf, fs->blocksize); 766 767 while (d->rec_len && !(d->rec_len & 0x3) && NEXT_DIRENT(d) <= top) 768 d = NEXT_DIRENT(d); 769 770 if (d != top) { 771 unsigned int min_size = EXT2_DIR_REC_LEN( 772 ext2fs_dirent_name_len(dirbuf)); 773 if (min_size > (char *)top - (char *)d) 774 return EXT2_ET_DIR_NO_SPACE_FOR_CSUM; 775 d->rec_len = (char *)top - (char *)d; 776 } 777 778 t = (struct ext2_dir_entry_tail *)top; 779 if (t->det_reserved_zero1 || 780 t->det_rec_len != sizeof(struct ext2_dir_entry_tail) || 781 t->det_reserved_name_len != EXT2_DIR_NAME_LEN_CSUM) 782 ext2fs_initialize_dirent_tail(fs, t); 783 784 return 0; 785 } 786 #undef NEXT_DIRENT 787 788 static errcode_t fix_inline_dir_size(e2fsck_t ctx, ext2_ino_t ino, 789 size_t *inline_data_size, 790 struct problem_context *pctx, 791 char *buf) 792 { 793 ext2_filsys fs = ctx->fs; 794 struct ext2_inode inode; 795 size_t new_size, old_size; 796 errcode_t retval; 797 798 old_size = *inline_data_size; 799 /* 800 * If there's not enough bytes to start the "second" dir block 801 * (in the EA space) then truncate everything to the first block. 802 */ 803 if (old_size > EXT4_MIN_INLINE_DATA_SIZE && 804 old_size < EXT4_MIN_INLINE_DATA_SIZE + 805 EXT2_DIR_REC_LEN(1)) { 806 old_size = EXT4_MIN_INLINE_DATA_SIZE; 807 new_size = old_size; 808 } else 809 /* Increase to the next four-byte boundary for salvaging */ 810 new_size = old_size + (4 - (old_size & 3)); 811 memset(buf + old_size, 0, new_size - old_size); 812 retval = ext2fs_inline_data_set(fs, ino, 0, buf, new_size); 813 if (retval == EXT2_ET_INLINE_DATA_NO_SPACE) { 814 /* Or we can't, so truncate. */ 815 new_size -= 4; 816 retval = ext2fs_inline_data_set(fs, ino, 0, buf, new_size); 817 if (retval) { 818 if (fix_problem(ctx, PR_2_FIX_INLINE_DIR_FAILED, 819 pctx)) { 820 new_size = 0; 821 goto write_inode; 822 } 823 goto err; 824 } 825 } else if (retval) { 826 if (fix_problem(ctx, PR_2_FIX_INLINE_DIR_FAILED, 827 pctx)) { 828 new_size = 0; 829 goto write_inode; 830 } 831 goto err; 832 } 833 834 write_inode: 835 retval = ext2fs_read_inode(fs, ino, &inode); 836 if (retval) 837 goto err; 838 839 retval = ext2fs_inode_size_set(fs, &inode, new_size); 840 if (retval) 841 goto err; 842 if (new_size == 0) 843 inode.i_flags &= ~EXT4_INLINE_DATA_FL; 844 retval = ext2fs_write_inode(fs, ino, &inode); 845 if (retval) 846 goto err; 847 *inline_data_size = new_size; 848 849 err: 850 return retval; 851 } 852 853 static int check_dir_block2(ext2_filsys fs, 854 struct ext2_db_entry2 *db, 855 void *priv_data) 856 { 857 int err; 858 struct check_dir_struct *cd = priv_data; 859 860 if (cd->ra_entries && cd->list_offset >= cd->next_ra_off) { 861 err = e2fsck_readahead_dblist(fs, 862 E2FSCK_RA_DBLIST_IGNORE_BLOCKCNT, 863 fs->dblist, 864 cd->list_offset + cd->ra_entries / 8, 865 cd->ra_entries); 866 if (err) 867 cd->ra_entries = 0; 868 cd->next_ra_off = cd->list_offset + (cd->ra_entries * 7 / 8); 869 } 870 871 err = check_dir_block(fs, db, priv_data); 872 cd->list_offset++; 873 return err; 874 } 875 876 static int check_dir_block(ext2_filsys fs, 877 struct ext2_db_entry2 *db, 878 void *priv_data) 879 { 880 struct dx_dir_info *dx_dir; 881 struct dx_dirblock_info *dx_db = 0; 882 struct ext2_dir_entry *dirent, *prev, dot, dotdot; 883 ext2_dirhash_t hash; 884 unsigned int offset = 0; 885 int dir_modified = 0; 886 int dot_state; 887 unsigned int rec_len; 888 blk64_t block_nr = db->blk; 889 ext2_ino_t ino = db->ino; 890 ext2_ino_t subdir_parent; 891 __u16 links; 892 struct check_dir_struct *cd; 893 char *buf, *ibuf; 894 e2fsck_t ctx; 895 problem_t problem; 896 struct ext2_dx_root_info *root; 897 struct ext2_dx_countlimit *limit; 898 static dict_t de_dict; 899 struct problem_context pctx; 900 int dups_found = 0; 901 int ret; 902 int dx_csum_size = 0, de_csum_size = 0; 903 int failed_csum = 0; 904 int is_leaf = 1; 905 size_t inline_data_size = 0; 906 int filetype = 0; 907 int encrypted = 0; 908 size_t max_block_size; 909 910 cd = (struct check_dir_struct *) priv_data; 911 ibuf = buf = cd->buf; 912 ctx = cd->ctx; 913 914 if (ctx->flags & E2F_FLAG_RUN_RETURN) 915 return DIRENT_ABORT; 916 917 if (ctx->progress && (ctx->progress)(ctx, 2, cd->count++, cd->max)) 918 return DIRENT_ABORT; 919 920 if (ext2fs_has_feature_metadata_csum(fs->super)) { 921 dx_csum_size = sizeof(struct ext2_dx_tail); 922 de_csum_size = sizeof(struct ext2_dir_entry_tail); 923 } 924 925 if (ext2fs_has_feature_filetype(fs->super)) 926 filetype = EXT2_FT_DIR << 8; 927 928 /* 929 * Make sure the inode is still in use (could have been 930 * deleted in the duplicate/bad blocks pass. 931 */ 932 if (!(ext2fs_test_inode_bitmap2(ctx->inode_used_map, ino))) 933 return 0; 934 935 cd->pctx.ino = ino; 936 cd->pctx.blk = block_nr; 937 cd->pctx.blkcount = db->blockcnt; 938 cd->pctx.ino2 = 0; 939 cd->pctx.dirent = 0; 940 cd->pctx.num = 0; 941 942 if (ext2fs_has_feature_inline_data(fs->super)) { 943 errcode_t ec; 944 945 ec = ext2fs_inline_data_size(fs, ino, &inline_data_size); 946 if (ec && ec != EXT2_ET_NO_INLINE_DATA) 947 return DIRENT_ABORT; 948 } 949 950 if (db->blk == 0 && !inline_data_size) { 951 if (allocate_dir_block(ctx, db, buf, &cd->pctx)) 952 return 0; 953 block_nr = db->blk; 954 } 955 956 if (db->blockcnt) 957 dot_state = 2; 958 else 959 dot_state = 0; 960 961 if (ctx->dirs_to_hash && 962 ext2fs_u32_list_test(ctx->dirs_to_hash, ino)) 963 dups_found++; 964 965 #if 0 966 printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr, 967 db->blockcnt, ino); 968 #endif 969 970 ehandler_operation(_("reading directory block")); 971 if (inline_data_size) { 972 memset(buf, 0, fs->blocksize - inline_data_size); 973 cd->pctx.errcode = ext2fs_inline_data_get(fs, ino, 0, buf, 0); 974 if (cd->pctx.errcode) 975 goto inline_read_fail; 976 #ifdef WORDS_BIGENDIAN 977 if (db->blockcnt) 978 goto skip_first_read_swab; 979 *((__u32 *)buf) = ext2fs_le32_to_cpu(*((__u32 *)buf)); 980 cd->pctx.errcode = ext2fs_dirent_swab_in2(fs, 981 buf + EXT4_INLINE_DATA_DOTDOT_SIZE, 982 EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DATA_DOTDOT_SIZE, 983 0); 984 if (cd->pctx.errcode) 985 goto inline_read_fail; 986 skip_first_read_swab: 987 if (inline_data_size <= EXT4_MIN_INLINE_DATA_SIZE || 988 !db->blockcnt) 989 goto inline_read_fail; 990 cd->pctx.errcode = ext2fs_dirent_swab_in2(fs, 991 buf + EXT4_MIN_INLINE_DATA_SIZE, 992 inline_data_size - EXT4_MIN_INLINE_DATA_SIZE, 993 0); 994 #endif 995 } else 996 cd->pctx.errcode = ext2fs_read_dir_block4(fs, block_nr, 997 buf, 0, ino); 998 inline_read_fail: 999 pctx.ino = ino; 1000 pctx.num = inline_data_size; 1001 if (((inline_data_size & 3) || 1002 (inline_data_size > EXT4_MIN_INLINE_DATA_SIZE && 1003 inline_data_size < EXT4_MIN_INLINE_DATA_SIZE + 1004 EXT2_DIR_REC_LEN(1))) && 1005 fix_problem(ctx, PR_2_BAD_INLINE_DIR_SIZE, &pctx)) { 1006 errcode_t err = fix_inline_dir_size(ctx, ino, 1007 &inline_data_size, &pctx, 1008 buf); 1009 if (err) 1010 return DIRENT_ABORT; 1011 1012 } 1013 ehandler_operation(0); 1014 if (cd->pctx.errcode == EXT2_ET_DIR_CORRUPTED) 1015 cd->pctx.errcode = 0; /* We'll handle this ourselves */ 1016 else if (cd->pctx.errcode == EXT2_ET_DIR_CSUM_INVALID) { 1017 cd->pctx.errcode = 0; /* We'll handle this ourselves */ 1018 failed_csum = 1; 1019 } 1020 if (cd->pctx.errcode) { 1021 char *buf2; 1022 if (!fix_problem(ctx, PR_2_READ_DIRBLOCK, &cd->pctx)) { 1023 ctx->flags |= E2F_FLAG_ABORT; 1024 return DIRENT_ABORT; 1025 } 1026 ext2fs_new_dir_block(fs, db->blockcnt == 0 ? ino : 0, 1027 EXT2_ROOT_INO, &buf2); 1028 memcpy(buf, buf2, fs->blocksize); 1029 ext2fs_free_mem(&buf2); 1030 } 1031 dx_dir = e2fsck_get_dx_dir_info(ctx, ino); 1032 if (dx_dir && dx_dir->numblocks) { 1033 if (db->blockcnt >= dx_dir->numblocks) { 1034 pctx.dir = ino; 1035 if (fix_problem(ctx, PR_2_UNEXPECTED_HTREE_BLOCK, 1036 &pctx)) { 1037 clear_htree(ctx, ino); 1038 dx_dir->numblocks = 0; 1039 dx_db = 0; 1040 goto out_htree; 1041 } 1042 fatal_error(ctx, _("Can not continue.")); 1043 } 1044 dx_db = &dx_dir->dx_block[db->blockcnt]; 1045 dx_db->type = DX_DIRBLOCK_LEAF; 1046 dx_db->phys = block_nr; 1047 dx_db->min_hash = ~0; 1048 dx_db->max_hash = 0; 1049 1050 dirent = (struct ext2_dir_entry *) buf; 1051 (void) ext2fs_get_rec_len(fs, dirent, &rec_len); 1052 limit = (struct ext2_dx_countlimit *) (buf+8); 1053 if (db->blockcnt == 0) { 1054 root = (struct ext2_dx_root_info *) (buf + 24); 1055 dx_db->type = DX_DIRBLOCK_ROOT; 1056 dx_db->flags |= DX_FLAG_FIRST | DX_FLAG_LAST; 1057 if ((root->reserved_zero || 1058 root->info_length < 8 || 1059 root->indirect_levels > 1) && 1060 fix_problem(ctx, PR_2_HTREE_BAD_ROOT, &cd->pctx)) { 1061 clear_htree(ctx, ino); 1062 dx_dir->numblocks = 0; 1063 dx_db = 0; 1064 } 1065 dx_dir->hashversion = root->hash_version; 1066 if ((dx_dir->hashversion <= EXT2_HASH_TEA) && 1067 (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH)) 1068 dx_dir->hashversion += 3; 1069 dx_dir->depth = root->indirect_levels + 1; 1070 } else if ((dirent->inode == 0) && 1071 (rec_len == fs->blocksize) && 1072 (ext2fs_dirent_name_len(dirent) == 0) && 1073 (ext2fs_le16_to_cpu(limit->limit) == 1074 ((fs->blocksize - (8 + dx_csum_size)) / 1075 sizeof(struct ext2_dx_entry)))) 1076 dx_db->type = DX_DIRBLOCK_NODE; 1077 is_leaf = (dx_db->type == DX_DIRBLOCK_LEAF); 1078 } 1079 out_htree: 1080 1081 /* Leaf node with no space for csum? Rebuild dirs in pass 3A. */ 1082 if (is_leaf && !inline_data_size && failed_csum && 1083 !ext2fs_dirent_has_tail(fs, (struct ext2_dir_entry *)buf)) { 1084 de_csum_size = 0; 1085 if (e2fsck_dir_will_be_rehashed(ctx, ino)) { 1086 failed_csum = 0; 1087 goto skip_checksum; 1088 } 1089 if (!fix_problem(cd->ctx, PR_2_LEAF_NODE_MISSING_CSUM, 1090 &cd->pctx)) 1091 goto skip_checksum; 1092 e2fsck_rehash_dir_later(ctx, ino); 1093 failed_csum = 0; 1094 goto skip_checksum; 1095 } 1096 /* htree nodes don't use fake dirents to store checksums */ 1097 if (!is_leaf) 1098 de_csum_size = 0; 1099 1100 skip_checksum: 1101 if (inline_data_size) { 1102 if (db->blockcnt) { 1103 buf += EXT4_MIN_INLINE_DATA_SIZE; 1104 max_block_size = inline_data_size - EXT4_MIN_INLINE_DATA_SIZE; 1105 /* Zero-length second block, just exit */ 1106 if (max_block_size == 0) 1107 return 0; 1108 } else { 1109 max_block_size = EXT4_MIN_INLINE_DATA_SIZE; 1110 } 1111 } else 1112 max_block_size = fs->blocksize - de_csum_size; 1113 1114 if (ctx->encrypted_dirs) 1115 encrypted = ext2fs_u32_list_test(ctx->encrypted_dirs, ino); 1116 1117 dict_init(&de_dict, DICTCOUNT_T_MAX, dict_de_cmp); 1118 prev = 0; 1119 do { 1120 dgrp_t group; 1121 ext2_ino_t first_unused_inode; 1122 unsigned int name_len; 1123 1124 problem = 0; 1125 if (!inline_data_size || dot_state > 1) { 1126 dirent = (struct ext2_dir_entry *) (buf + offset); 1127 /* 1128 * If there's not even space for the entry header, 1129 * force salvaging this dir. 1130 */ 1131 if (max_block_size - offset < EXT2_DIR_ENTRY_HEADER_LEN) 1132 rec_len = EXT2_DIR_REC_LEN(1); 1133 else 1134 (void) ext2fs_get_rec_len(fs, dirent, &rec_len); 1135 cd->pctx.dirent = dirent; 1136 cd->pctx.num = offset; 1137 if ((offset + rec_len > max_block_size) || 1138 (rec_len < 12) || 1139 ((rec_len % 4) != 0) || 1140 (((unsigned) ext2fs_dirent_name_len(dirent) + EXT2_DIR_ENTRY_HEADER_LEN) > rec_len)) { 1141 if (fix_problem(ctx, PR_2_DIR_CORRUPTED, 1142 &cd->pctx)) { 1143 #ifdef WORDS_BIGENDIAN 1144 /* 1145 * On big-endian systems, if the dirent 1146 * swap routine finds a rec_len that it 1147 * doesn't like, it continues 1148 * processing the block as if rec_len 1149 * == EXT2_DIR_ENTRY_HEADER_LEN. This means that the name 1150 * field gets byte swapped, which means 1151 * that salvage will not detect the 1152 * correct name length (unless the name 1153 * has a length that's an exact 1154 * multiple of four bytes), and it'll 1155 * discard the entry (unnecessarily) 1156 * and the rest of the dirent block. 1157 * Therefore, swap the rest of the 1158 * block back to disk order, run 1159 * salvage, and re-swap anything after 1160 * the salvaged dirent. 1161 */ 1162 int need_reswab = 0; 1163 if (rec_len < EXT2_DIR_ENTRY_HEADER_LEN || rec_len % 4) { 1164 need_reswab = 1; 1165 ext2fs_dirent_swab_in2(fs, 1166 ((char *)dirent) + EXT2_DIR_ENTRY_HEADER_LEN, 1167 max_block_size - offset - EXT2_DIR_ENTRY_HEADER_LEN, 1168 0); 1169 } 1170 #endif 1171 salvage_directory(fs, dirent, prev, 1172 &offset, 1173 max_block_size); 1174 #ifdef WORDS_BIGENDIAN 1175 if (need_reswab) { 1176 (void) ext2fs_get_rec_len(fs, 1177 dirent, &rec_len); 1178 ext2fs_dirent_swab_in2(fs, 1179 ((char *)dirent) + offset + rec_len, 1180 max_block_size - offset - rec_len, 1181 0); 1182 } 1183 #endif 1184 dir_modified++; 1185 continue; 1186 } else 1187 goto abort_free_dict; 1188 } 1189 } else { 1190 if (dot_state == 0) { 1191 memset(&dot, 0, sizeof(dot)); 1192 dirent = ˙ 1193 dirent->inode = ino; 1194 dirent->rec_len = EXT2_DIR_REC_LEN(1); 1195 dirent->name_len = 1 | filetype; 1196 dirent->name[0] = '.'; 1197 } else if (dot_state == 1) { 1198 memset(&dotdot, 0, sizeof(dotdot)); 1199 dirent = &dotdot; 1200 dirent->inode = 1201 ((struct ext2_dir_entry *)buf)->inode; 1202 dirent->rec_len = EXT2_DIR_REC_LEN(2); 1203 dirent->name_len = 2 | filetype; 1204 dirent->name[0] = '.'; 1205 dirent->name[1] = '.'; 1206 } else { 1207 fatal_error(ctx, _("Can not continue.")); 1208 } 1209 cd->pctx.dirent = dirent; 1210 cd->pctx.num = offset; 1211 } 1212 1213 if (dot_state == 0) { 1214 if (check_dot(ctx, dirent, ino, &cd->pctx)) 1215 dir_modified++; 1216 } else if (dot_state == 1) { 1217 ret = check_dotdot(ctx, dirent, ino, &cd->pctx); 1218 if (ret < 0) 1219 goto abort_free_dict; 1220 if (ret) 1221 dir_modified++; 1222 } else if (dirent->inode == ino) { 1223 problem = PR_2_LINK_DOT; 1224 if (fix_problem(ctx, PR_2_LINK_DOT, &cd->pctx)) { 1225 dirent->inode = 0; 1226 dir_modified++; 1227 goto next; 1228 } 1229 } 1230 if (!dirent->inode) 1231 goto next; 1232 1233 /* 1234 * Make sure the inode listed is a legal one. 1235 */ 1236 name_len = ext2fs_dirent_name_len(dirent); 1237 if (((dirent->inode != EXT2_ROOT_INO) && 1238 (dirent->inode < EXT2_FIRST_INODE(fs->super))) || 1239 (dirent->inode > fs->super->s_inodes_count)) { 1240 problem = PR_2_BAD_INO; 1241 } else if (ctx->inode_bb_map && 1242 (ext2fs_test_inode_bitmap2(ctx->inode_bb_map, 1243 dirent->inode))) { 1244 /* 1245 * If the inode is in a bad block, offer to 1246 * clear it. 1247 */ 1248 problem = PR_2_BB_INODE; 1249 } else if ((dot_state > 1) && (name_len == 1) && 1250 (dirent->name[0] == '.')) { 1251 /* 1252 * If there's a '.' entry in anything other 1253 * than the first directory entry, it's a 1254 * duplicate entry that should be removed. 1255 */ 1256 problem = PR_2_DUP_DOT; 1257 } else if ((dot_state > 1) && (name_len == 2) && 1258 (dirent->name[0] == '.') && 1259 (dirent->name[1] == '.')) { 1260 /* 1261 * If there's a '..' entry in anything other 1262 * than the second directory entry, it's a 1263 * duplicate entry that should be removed. 1264 */ 1265 problem = PR_2_DUP_DOT_DOT; 1266 } else if ((dot_state > 1) && 1267 (dirent->inode == EXT2_ROOT_INO)) { 1268 /* 1269 * Don't allow links to the root directory. 1270 * We check this specially to make sure we 1271 * catch this error case even if the root 1272 * directory hasn't been created yet. 1273 */ 1274 problem = PR_2_LINK_ROOT; 1275 } else if ((dot_state > 1) && (name_len == 0)) { 1276 /* 1277 * Don't allow zero-length directory names. 1278 */ 1279 problem = PR_2_NULL_NAME; 1280 } 1281 1282 if (problem) { 1283 if (fix_problem(ctx, problem, &cd->pctx)) { 1284 dirent->inode = 0; 1285 dir_modified++; 1286 goto next; 1287 } else { 1288 ext2fs_unmark_valid(fs); 1289 if (problem == PR_2_BAD_INO) 1290 goto next; 1291 } 1292 } 1293 1294 /* 1295 * If the inode was marked as having bad fields in 1296 * pass1, process it and offer to fix/clear it. 1297 * (We wait until now so that we can display the 1298 * pathname to the user.) 1299 */ 1300 if (ctx->inode_bad_map && 1301 ext2fs_test_inode_bitmap2(ctx->inode_bad_map, 1302 dirent->inode)) { 1303 if (e2fsck_process_bad_inode(ctx, ino, 1304 dirent->inode, 1305 buf + fs->blocksize)) { 1306 dirent->inode = 0; 1307 dir_modified++; 1308 goto next; 1309 } 1310 if (ctx->flags & E2F_FLAG_SIGNAL_MASK) 1311 return DIRENT_ABORT; 1312 } 1313 1314 group = ext2fs_group_of_ino(fs, dirent->inode); 1315 first_unused_inode = group * fs->super->s_inodes_per_group + 1316 1 + fs->super->s_inodes_per_group - 1317 ext2fs_bg_itable_unused(fs, group); 1318 cd->pctx.group = group; 1319 1320 /* 1321 * Check if the inode was missed out because 1322 * _INODE_UNINIT flag was set or bg_itable_unused was 1323 * incorrect. If so, clear the _INODE_UNINIT flag and 1324 * restart e2fsck. In the future it would be nice if 1325 * we could call a function in pass1.c that checks the 1326 * newly visible inodes. 1327 */ 1328 if (ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)) { 1329 pctx.num = dirent->inode; 1330 if (fix_problem(ctx, PR_2_INOREF_BG_INO_UNINIT, 1331 &cd->pctx)){ 1332 ext2fs_bg_flags_clear(fs, group, 1333 EXT2_BG_INODE_UNINIT); 1334 ext2fs_mark_super_dirty(fs); 1335 ctx->flags |= E2F_FLAG_RESTART_LATER; 1336 } else { 1337 ext2fs_unmark_valid(fs); 1338 if (problem == PR_2_BAD_INO) 1339 goto next; 1340 } 1341 } else if (dirent->inode >= first_unused_inode) { 1342 pctx.num = dirent->inode; 1343 if (fix_problem(ctx, PR_2_INOREF_IN_UNUSED, &cd->pctx)){ 1344 ext2fs_bg_itable_unused_set(fs, group, 0); 1345 ext2fs_mark_super_dirty(fs); 1346 ctx->flags |= E2F_FLAG_RESTART_LATER; 1347 } else { 1348 ext2fs_unmark_valid(fs); 1349 if (problem == PR_2_BAD_INO) 1350 goto next; 1351 } 1352 } 1353 1354 /* 1355 * Offer to clear unused inodes; if we are going to be 1356 * restarting the scan due to bg_itable_unused being 1357 * wrong, then don't clear any inodes to avoid zapping 1358 * inodes that were skipped during pass1 due to an 1359 * incorrect bg_itable_unused; we'll get any real 1360 * problems after we restart. 1361 */ 1362 if (!(ctx->flags & E2F_FLAG_RESTART_LATER) && 1363 !(ext2fs_test_inode_bitmap2(ctx->inode_used_map, 1364 dirent->inode))) 1365 problem = PR_2_UNUSED_INODE; 1366 1367 if (problem) { 1368 if (fix_problem(ctx, problem, &cd->pctx)) { 1369 dirent->inode = 0; 1370 dir_modified++; 1371 goto next; 1372 } else { 1373 ext2fs_unmark_valid(fs); 1374 if (problem == PR_2_BAD_INO) 1375 goto next; 1376 } 1377 } 1378 1379 if (!encrypted && check_name(ctx, dirent, &cd->pctx)) 1380 dir_modified++; 1381 1382 if (encrypted && (dot_state) > 1 && 1383 encrypted_check_name(ctx, dirent, &cd->pctx)) { 1384 dir_modified++; 1385 goto next; 1386 } 1387 1388 if (check_filetype(ctx, dirent, ino, &cd->pctx)) 1389 dir_modified++; 1390 1391 if (dx_db) { 1392 ext2fs_dirhash(dx_dir->hashversion, dirent->name, 1393 ext2fs_dirent_name_len(dirent), 1394 fs->super->s_hash_seed, &hash, 0); 1395 if (hash < dx_db->min_hash) 1396 dx_db->min_hash = hash; 1397 if (hash > dx_db->max_hash) 1398 dx_db->max_hash = hash; 1399 } 1400 1401 /* 1402 * If this is a directory, then mark its parent in its 1403 * dir_info structure. If the parent field is already 1404 * filled in, then this directory has more than one 1405 * hard link. We assume the first link is correct, 1406 * and ask the user if he/she wants to clear this one. 1407 */ 1408 if ((dot_state > 1) && 1409 (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, 1410 dirent->inode))) { 1411 if (e2fsck_dir_info_get_parent(ctx, dirent->inode, 1412 &subdir_parent)) { 1413 cd->pctx.ino = dirent->inode; 1414 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx); 1415 goto abort_free_dict; 1416 } 1417 if (subdir_parent) { 1418 cd->pctx.ino2 = subdir_parent; 1419 if (fix_problem(ctx, PR_2_LINK_DIR, 1420 &cd->pctx)) { 1421 dirent->inode = 0; 1422 dir_modified++; 1423 goto next; 1424 } 1425 cd->pctx.ino2 = 0; 1426 } else { 1427 (void) e2fsck_dir_info_set_parent(ctx, 1428 dirent->inode, ino); 1429 } 1430 } 1431 1432 if (dups_found) { 1433 ; 1434 } else if (dict_lookup(&de_dict, dirent)) { 1435 clear_problem_context(&pctx); 1436 pctx.ino = ino; 1437 pctx.dirent = dirent; 1438 fix_problem(ctx, PR_2_REPORT_DUP_DIRENT, &pctx); 1439 e2fsck_rehash_dir_later(ctx, ino); 1440 dups_found++; 1441 } else 1442 dict_alloc_insert(&de_dict, dirent, dirent); 1443 1444 ext2fs_icount_increment(ctx->inode_count, dirent->inode, 1445 &links); 1446 if (links > 1) 1447 ctx->fs_links_count++; 1448 ctx->fs_total_count++; 1449 next: 1450 prev = dirent; 1451 if (dir_modified) 1452 (void) ext2fs_get_rec_len(fs, dirent, &rec_len); 1453 if (!inline_data_size || dot_state > 1) { 1454 offset += rec_len; 1455 } else { 1456 if (dot_state == 1) { 1457 offset = 4; 1458 /* 1459 * If we get here, we're checking an inline 1460 * directory and we've just checked a (fake) 1461 * dotdot entry that we created on the stack. 1462 * Therefore set 'prev' to NULL so that if we 1463 * call salvage_directory on the next entry, 1464 * it won't try to absorb the next entry into 1465 * the on-stack dotdot entry. 1466 */ 1467 prev = NULL; 1468 } 1469 } 1470 dot_state++; 1471 } while (offset < max_block_size); 1472 #if 0 1473 printf("\n"); 1474 #endif 1475 if (dx_db) { 1476 #ifdef DX_DEBUG 1477 printf("db_block %d, type %d, min_hash 0x%0x, max_hash 0x%0x\n", 1478 db->blockcnt, dx_db->type, 1479 dx_db->min_hash, dx_db->max_hash); 1480 #endif 1481 cd->pctx.dir = cd->pctx.ino; 1482 if ((dx_db->type == DX_DIRBLOCK_ROOT) || 1483 (dx_db->type == DX_DIRBLOCK_NODE)) 1484 parse_int_node(fs, db, cd, dx_dir, buf, failed_csum); 1485 } 1486 1487 if (offset != max_block_size) { 1488 cd->pctx.num = rec_len + offset - max_block_size; 1489 if (fix_problem(ctx, PR_2_FINAL_RECLEN, &cd->pctx)) { 1490 dirent->rec_len = cd->pctx.num; 1491 dir_modified++; 1492 } 1493 } 1494 if (dir_modified) { 1495 int flags, will_rehash; 1496 /* leaf block with no tail? Rehash dirs later. */ 1497 if (ext2fs_has_feature_metadata_csum(fs->super) && 1498 is_leaf && 1499 !inline_data_size && 1500 !ext2fs_dirent_has_tail(fs, (struct ext2_dir_entry *)buf)) { 1501 if (insert_dirent_tail(fs, buf) == 0) 1502 goto write_and_fix; 1503 e2fsck_rehash_dir_later(ctx, ino); 1504 } 1505 1506 write_and_fix: 1507 will_rehash = e2fsck_dir_will_be_rehashed(ctx, ino); 1508 if (will_rehash) { 1509 flags = ctx->fs->flags; 1510 ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS; 1511 } 1512 if (inline_data_size) { 1513 buf = ibuf; 1514 #ifdef WORDS_BIGENDIAN 1515 if (db->blockcnt) 1516 goto skip_first_write_swab; 1517 *((__u32 *)buf) = ext2fs_le32_to_cpu(*((__u32 *)buf)); 1518 cd->pctx.errcode = ext2fs_dirent_swab_out2(fs, 1519 buf + EXT4_INLINE_DATA_DOTDOT_SIZE, 1520 EXT4_MIN_INLINE_DATA_SIZE - 1521 EXT4_INLINE_DATA_DOTDOT_SIZE, 1522 0); 1523 if (cd->pctx.errcode) 1524 goto skip_second_write_swab; 1525 skip_first_write_swab: 1526 if (inline_data_size <= EXT4_MIN_INLINE_DATA_SIZE || 1527 !db->blockcnt) 1528 goto skip_second_write_swab; 1529 cd->pctx.errcode = ext2fs_dirent_swab_out2(fs, 1530 buf + EXT4_MIN_INLINE_DATA_SIZE, 1531 inline_data_size - 1532 EXT4_MIN_INLINE_DATA_SIZE, 1533 0); 1534 skip_second_write_swab: 1535 if (cd->pctx.errcode && 1536 !fix_problem(ctx, PR_2_WRITE_DIRBLOCK, &cd->pctx)) 1537 goto abort_free_dict; 1538 #endif 1539 cd->pctx.errcode = 1540 ext2fs_inline_data_set(fs, ino, 0, buf, 1541 inline_data_size); 1542 } else 1543 cd->pctx.errcode = ext2fs_write_dir_block4(fs, block_nr, 1544 buf, 0, ino); 1545 if (will_rehash) 1546 ctx->fs->flags = (flags & 1547 EXT2_FLAG_IGNORE_CSUM_ERRORS) | 1548 (ctx->fs->flags & 1549 ~EXT2_FLAG_IGNORE_CSUM_ERRORS); 1550 if (cd->pctx.errcode) { 1551 if (!fix_problem(ctx, PR_2_WRITE_DIRBLOCK, 1552 &cd->pctx)) 1553 goto abort_free_dict; 1554 } 1555 ext2fs_mark_changed(fs); 1556 } else if (is_leaf && failed_csum && !dir_modified) { 1557 /* 1558 * If a leaf node that fails csum makes it this far without 1559 * alteration, ask the user if the checksum should be fixed. 1560 */ 1561 if (fix_problem(ctx, PR_2_LEAF_NODE_ONLY_CSUM_INVALID, 1562 &cd->pctx)) 1563 goto write_and_fix; 1564 } 1565 dict_free_nodes(&de_dict); 1566 return 0; 1567 abort_free_dict: 1568 ctx->flags |= E2F_FLAG_ABORT; 1569 dict_free_nodes(&de_dict); 1570 return DIRENT_ABORT; 1571 } 1572 1573 struct del_block { 1574 e2fsck_t ctx; 1575 e2_blkcnt_t num; 1576 }; 1577 1578 /* 1579 * This function is called to deallocate a block, and is an interator 1580 * functioned called by deallocate inode via ext2fs_iterate_block(). 1581 */ 1582 static int deallocate_inode_block(ext2_filsys fs, 1583 blk64_t *block_nr, 1584 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)), 1585 blk64_t ref_block EXT2FS_ATTR((unused)), 1586 int ref_offset EXT2FS_ATTR((unused)), 1587 void *priv_data) 1588 { 1589 struct del_block *p = priv_data; 1590 1591 if (*block_nr == 0) 1592 return 0; 1593 if ((*block_nr < fs->super->s_first_data_block) || 1594 (*block_nr >= ext2fs_blocks_count(fs->super))) 1595 return 0; 1596 if ((*block_nr % EXT2FS_CLUSTER_RATIO(fs)) == 0) 1597 ext2fs_block_alloc_stats2(fs, *block_nr, -1); 1598 p->num++; 1599 return 0; 1600 } 1601 1602 /* 1603 * This fuction deallocates an inode 1604 */ 1605 static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf) 1606 { 1607 ext2_filsys fs = ctx->fs; 1608 struct ext2_inode inode; 1609 struct problem_context pctx; 1610 __u32 count; 1611 struct del_block del_block; 1612 1613 e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode"); 1614 clear_problem_context(&pctx); 1615 pctx.ino = ino; 1616 1617 /* 1618 * Fix up the bitmaps... 1619 */ 1620 e2fsck_read_bitmaps(ctx); 1621 ext2fs_inode_alloc_stats2(fs, ino, -1, LINUX_S_ISDIR(inode.i_mode)); 1622 1623 if (ext2fs_file_acl_block(fs, &inode) && 1624 ext2fs_has_feature_xattr(fs->super)) { 1625 pctx.errcode = ext2fs_adjust_ea_refcount3(fs, 1626 ext2fs_file_acl_block(fs, &inode), 1627 block_buf, -1, &count, ino); 1628 if (pctx.errcode == EXT2_ET_BAD_EA_BLOCK_NUM) { 1629 pctx.errcode = 0; 1630 count = 1; 1631 } 1632 if (pctx.errcode) { 1633 pctx.blk = ext2fs_file_acl_block(fs, &inode); 1634 fix_problem(ctx, PR_2_ADJ_EA_REFCOUNT, &pctx); 1635 ctx->flags |= E2F_FLAG_ABORT; 1636 return; 1637 } 1638 if (count == 0) { 1639 ext2fs_block_alloc_stats2(fs, 1640 ext2fs_file_acl_block(fs, &inode), -1); 1641 } 1642 ext2fs_file_acl_block_set(fs, &inode, 0); 1643 } 1644 1645 if (!ext2fs_inode_has_valid_blocks2(fs, &inode)) 1646 goto clear_inode; 1647 1648 /* Inline data inodes don't have blocks to iterate */ 1649 if (inode.i_flags & EXT4_INLINE_DATA_FL) 1650 goto clear_inode; 1651 1652 if (LINUX_S_ISREG(inode.i_mode) && 1653 ext2fs_needs_large_file_feature(EXT2_I_SIZE(&inode))) 1654 ctx->large_files--; 1655 1656 del_block.ctx = ctx; 1657 del_block.num = 0; 1658 pctx.errcode = ext2fs_block_iterate3(fs, ino, 0, block_buf, 1659 deallocate_inode_block, 1660 &del_block); 1661 if (pctx.errcode) { 1662 fix_problem(ctx, PR_2_DEALLOC_INODE, &pctx); 1663 ctx->flags |= E2F_FLAG_ABORT; 1664 return; 1665 } 1666 clear_inode: 1667 /* Inode may have changed by block_iterate, so reread it */ 1668 e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode"); 1669 e2fsck_clear_inode(ctx, ino, &inode, 0, "deallocate_inode"); 1670 } 1671 1672 /* 1673 * This fuction clears the htree flag on an inode 1674 */ 1675 static void clear_htree(e2fsck_t ctx, ext2_ino_t ino) 1676 { 1677 struct ext2_inode inode; 1678 1679 e2fsck_read_inode(ctx, ino, &inode, "clear_htree"); 1680 inode.i_flags = inode.i_flags & ~EXT2_INDEX_FL; 1681 e2fsck_write_inode(ctx, ino, &inode, "clear_htree"); 1682 if (ctx->dirs_to_hash) 1683 ext2fs_u32_list_add(ctx->dirs_to_hash, ino); 1684 } 1685 1686 1687 int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir, 1688 ext2_ino_t ino, char *buf) 1689 { 1690 ext2_filsys fs = ctx->fs; 1691 struct ext2_inode inode; 1692 int inode_modified = 0; 1693 int not_fixed = 0; 1694 unsigned char *frag, *fsize; 1695 struct problem_context pctx; 1696 problem_t problem = 0; 1697 1698 e2fsck_read_inode(ctx, ino, &inode, "process_bad_inode"); 1699 1700 clear_problem_context(&pctx); 1701 pctx.ino = ino; 1702 pctx.dir = dir; 1703 pctx.inode = &inode; 1704 1705 if (ext2fs_file_acl_block(fs, &inode) && 1706 !ext2fs_has_feature_xattr(fs->super)) { 1707 if (fix_problem(ctx, PR_2_FILE_ACL_ZERO, &pctx)) { 1708 ext2fs_file_acl_block_set(fs, &inode, 0); 1709 inode_modified++; 1710 } else 1711 not_fixed++; 1712 } 1713 1714 if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) && 1715 !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) && 1716 !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) && 1717 !(LINUX_S_ISSOCK(inode.i_mode))) 1718 problem = PR_2_BAD_MODE; 1719 else if (LINUX_S_ISCHR(inode.i_mode) 1720 && !e2fsck_pass1_check_device_inode(fs, &inode)) 1721 problem = PR_2_BAD_CHAR_DEV; 1722 else if (LINUX_S_ISBLK(inode.i_mode) 1723 && !e2fsck_pass1_check_device_inode(fs, &inode)) 1724 problem = PR_2_BAD_BLOCK_DEV; 1725 else if (LINUX_S_ISFIFO(inode.i_mode) 1726 && !e2fsck_pass1_check_device_inode(fs, &inode)) 1727 problem = PR_2_BAD_FIFO; 1728 else if (LINUX_S_ISSOCK(inode.i_mode) 1729 && !e2fsck_pass1_check_device_inode(fs, &inode)) 1730 problem = PR_2_BAD_SOCKET; 1731 else if (LINUX_S_ISLNK(inode.i_mode) 1732 && !e2fsck_pass1_check_symlink(fs, ino, &inode, buf)) { 1733 problem = PR_2_INVALID_SYMLINK; 1734 } 1735 1736 if (problem) { 1737 if (fix_problem(ctx, problem, &pctx)) { 1738 deallocate_inode(ctx, ino, 0); 1739 if (ctx->flags & E2F_FLAG_SIGNAL_MASK) 1740 return 0; 1741 return 1; 1742 } else 1743 not_fixed++; 1744 problem = 0; 1745 } 1746 1747 if (inode.i_faddr) { 1748 if (fix_problem(ctx, PR_2_FADDR_ZERO, &pctx)) { 1749 inode.i_faddr = 0; 1750 inode_modified++; 1751 } else 1752 not_fixed++; 1753 } 1754 1755 switch (fs->super->s_creator_os) { 1756 case EXT2_OS_HURD: 1757 frag = &inode.osd2.hurd2.h_i_frag; 1758 fsize = &inode.osd2.hurd2.h_i_fsize; 1759 break; 1760 default: 1761 frag = fsize = 0; 1762 } 1763 if (frag && *frag) { 1764 pctx.num = *frag; 1765 if (fix_problem(ctx, PR_2_FRAG_ZERO, &pctx)) { 1766 *frag = 0; 1767 inode_modified++; 1768 } else 1769 not_fixed++; 1770 pctx.num = 0; 1771 } 1772 if (fsize && *fsize) { 1773 pctx.num = *fsize; 1774 if (fix_problem(ctx, PR_2_FSIZE_ZERO, &pctx)) { 1775 *fsize = 0; 1776 inode_modified++; 1777 } else 1778 not_fixed++; 1779 pctx.num = 0; 1780 } 1781 1782 if ((fs->super->s_creator_os == EXT2_OS_LINUX) && 1783 !ext2fs_has_feature_huge_file(fs->super) && 1784 (inode.osd2.linux2.l_i_blocks_hi != 0)) { 1785 pctx.num = inode.osd2.linux2.l_i_blocks_hi; 1786 if (fix_problem(ctx, PR_2_BLOCKS_HI_ZERO, &pctx)) { 1787 inode.osd2.linux2.l_i_blocks_hi = 0; 1788 inode_modified++; 1789 } 1790 } 1791 1792 if ((fs->super->s_creator_os == EXT2_OS_LINUX) && 1793 !ext2fs_has_feature_64bit(fs->super) && 1794 inode.osd2.linux2.l_i_file_acl_high != 0) { 1795 pctx.num = inode.osd2.linux2.l_i_file_acl_high; 1796 if (fix_problem(ctx, PR_2_I_FILE_ACL_HI_ZERO, &pctx)) { 1797 inode.osd2.linux2.l_i_file_acl_high = 0; 1798 inode_modified++; 1799 } else 1800 not_fixed++; 1801 } 1802 1803 if (ext2fs_file_acl_block(fs, &inode) && 1804 ((ext2fs_file_acl_block(fs, &inode) < fs->super->s_first_data_block) || 1805 (ext2fs_file_acl_block(fs, &inode) >= ext2fs_blocks_count(fs->super)))) { 1806 if (fix_problem(ctx, PR_2_FILE_ACL_BAD, &pctx)) { 1807 ext2fs_file_acl_block_set(fs, &inode, 0); 1808 inode_modified++; 1809 } else 1810 not_fixed++; 1811 } 1812 if (inode.i_dir_acl && 1813 LINUX_S_ISDIR(inode.i_mode)) { 1814 if (fix_problem(ctx, PR_2_DIR_ACL_ZERO, &pctx)) { 1815 inode.i_dir_acl = 0; 1816 inode_modified++; 1817 } else 1818 not_fixed++; 1819 } 1820 1821 if (inode_modified) 1822 e2fsck_write_inode(ctx, ino, &inode, "process_bad_inode"); 1823 if (!not_fixed && ctx->inode_bad_map) 1824 ext2fs_unmark_inode_bitmap2(ctx->inode_bad_map, ino); 1825 return 0; 1826 } 1827 1828 /* 1829 * allocate_dir_block --- this function allocates a new directory 1830 * block for a particular inode; this is done if a directory has 1831 * a "hole" in it, or if a directory has a illegal block number 1832 * that was zeroed out and now needs to be replaced. 1833 */ 1834 static int allocate_dir_block(e2fsck_t ctx, 1835 struct ext2_db_entry2 *db, 1836 char *buf EXT2FS_ATTR((unused)), 1837 struct problem_context *pctx) 1838 { 1839 ext2_filsys fs = ctx->fs; 1840 blk64_t blk = 0; 1841 char *block; 1842 struct ext2_inode inode; 1843 1844 if (fix_problem(ctx, PR_2_DIRECTORY_HOLE, pctx) == 0) 1845 return 1; 1846 1847 /* 1848 * Read the inode and block bitmaps in; we'll be messing with 1849 * them. 1850 */ 1851 e2fsck_read_bitmaps(ctx); 1852 1853 /* 1854 * First, find a free block 1855 */ 1856 e2fsck_read_inode(ctx, db->ino, &inode, "allocate_dir_block"); 1857 pctx->errcode = ext2fs_map_cluster_block(fs, db->ino, &inode, 1858 db->blockcnt, &blk); 1859 if (pctx->errcode || blk == 0) { 1860 blk = ext2fs_find_inode_goal(fs, db->ino, &inode, db->blockcnt); 1861 pctx->errcode = ext2fs_new_block2(fs, blk, 1862 ctx->block_found_map, &blk); 1863 if (pctx->errcode) { 1864 pctx->str = "ext2fs_new_block"; 1865 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx); 1866 return 1; 1867 } 1868 } 1869 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk); 1870 ext2fs_mark_block_bitmap2(fs->block_map, blk); 1871 ext2fs_mark_bb_dirty(fs); 1872 1873 /* 1874 * Now let's create the actual data block for the inode 1875 */ 1876 if (db->blockcnt) 1877 pctx->errcode = ext2fs_new_dir_block(fs, 0, 0, &block); 1878 else 1879 pctx->errcode = ext2fs_new_dir_block(fs, db->ino, 1880 EXT2_ROOT_INO, &block); 1881 1882 if (pctx->errcode) { 1883 pctx->str = "ext2fs_new_dir_block"; 1884 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx); 1885 return 1; 1886 } 1887 1888 pctx->errcode = ext2fs_write_dir_block4(fs, blk, block, 0, db->ino); 1889 ext2fs_free_mem(&block); 1890 if (pctx->errcode) { 1891 pctx->str = "ext2fs_write_dir_block"; 1892 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx); 1893 return 1; 1894 } 1895 1896 /* 1897 * Update the inode block count 1898 */ 1899 ext2fs_iblk_add_blocks(fs, &inode, 1); 1900 if (EXT2_I_SIZE(&inode) < ((__u64) db->blockcnt+1) * fs->blocksize) { 1901 pctx->errcode = ext2fs_inode_size_set(fs, &inode, 1902 (db->blockcnt+1) * fs->blocksize); 1903 if (pctx->errcode) { 1904 pctx->str = "ext2fs_inode_size_set"; 1905 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx); 1906 return 1; 1907 } 1908 } 1909 e2fsck_write_inode(ctx, db->ino, &inode, "allocate_dir_block"); 1910 1911 /* 1912 * Finally, update the block pointers for the inode 1913 */ 1914 db->blk = blk; 1915 pctx->errcode = ext2fs_bmap2(fs, db->ino, &inode, 0, BMAP_SET, 1916 db->blockcnt, 0, &blk); 1917 if (pctx->errcode) { 1918 pctx->str = "ext2fs_block_iterate"; 1919 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx); 1920 return 1; 1921 } 1922 1923 return 0; 1924 } 1925