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