pass1.c revision 9c40d14841f04811097a123d6e8555e78ce56811
1/* 2 * pass1.c -- pass #1 of e2fsck: sequential scan of the inode table 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 1 of e2fsck iterates over all the inodes in the filesystems, 12 * and applies the following tests to each inode: 13 * 14 * - The mode field of the inode must be legal. 15 * - The size and block count fields of the inode are correct. 16 * - A data block must not be used by another inode 17 * 18 * Pass 1 also gathers the collects the following information: 19 * 20 * - A bitmap of which inodes are in use. (inode_used_map) 21 * - A bitmap of which inodes are directories. (inode_dir_map) 22 * - A bitmap of which inodes are regular files. (inode_reg_map) 23 * - A bitmap of which inodes have bad fields. (inode_bad_map) 24 * - A bitmap of which inodes are in bad blocks. (inode_bb_map) 25 * - A bitmap of which inodes are imagic inodes. (inode_imagic_map) 26 * - A bitmap of which blocks are in use. (block_found_map) 27 * - A bitmap of which blocks are in use by two inodes (block_dup_map) 28 * - The data blocks of the directory inodes. (dir_map) 29 * 30 * Pass 1 is designed to stash away enough information so that the 31 * other passes should not need to read in the inode information 32 * during the normal course of a filesystem check. (Althogh if an 33 * inconsistency is detected, other passes may need to read in an 34 * inode to fix it.) 35 * 36 * Note that pass 1B will be invoked if there are any duplicate blocks 37 * found. 38 */ 39 40#define _GNU_SOURCE 1 /* get strnlen() */ 41#include "config.h" 42#include <string.h> 43#include <time.h> 44#ifdef HAVE_ERRNO_H 45#include <errno.h> 46#endif 47 48#include "e2fsck.h" 49#include <ext2fs/ext2_ext_attr.h> 50 51#include "problem.h" 52 53#ifdef NO_INLINE_FUNCS 54#define _INLINE_ 55#else 56#define _INLINE_ inline 57#endif 58 59static int process_block(ext2_filsys fs, blk64_t *blocknr, 60 e2_blkcnt_t blockcnt, blk64_t ref_blk, 61 int ref_offset, void *priv_data); 62static int process_bad_block(ext2_filsys fs, blk64_t *block_nr, 63 e2_blkcnt_t blockcnt, blk64_t ref_blk, 64 int ref_offset, void *priv_data); 65static void check_blocks(e2fsck_t ctx, struct problem_context *pctx, 66 char *block_buf); 67static void mark_table_blocks(e2fsck_t ctx); 68static void alloc_bb_map(e2fsck_t ctx); 69static void alloc_imagic_map(e2fsck_t ctx); 70static void mark_inode_bad(e2fsck_t ctx, ino_t ino); 71static void handle_fs_bad_blocks(e2fsck_t ctx); 72static void process_inodes(e2fsck_t ctx, char *block_buf); 73static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b); 74static errcode_t scan_callback(ext2_filsys fs, ext2_inode_scan scan, 75 dgrp_t group, void * priv_data); 76static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount, 77 char *block_buf, int adjust_sign); 78/* static char *describe_illegal_block(ext2_filsys fs, blk64_t block); */ 79 80struct process_block_struct { 81 ext2_ino_t ino; 82 unsigned is_dir:1, is_reg:1, clear:1, suppress:1, 83 fragmented:1, compressed:1, bbcheck:1; 84 blk64_t num_blocks; 85 blk64_t max_blocks; 86 e2_blkcnt_t last_block; 87 e2_blkcnt_t last_init_lblock; 88 e2_blkcnt_t last_db_block; 89 int num_illegal_blocks; 90 blk64_t previous_block; 91 struct ext2_inode *inode; 92 struct problem_context *pctx; 93 ext2fs_block_bitmap fs_meta_blocks; 94 e2fsck_t ctx; 95}; 96 97struct process_inode_block { 98 ext2_ino_t ino; 99 struct ext2_inode inode; 100}; 101 102struct scan_callback_struct { 103 e2fsck_t ctx; 104 char *block_buf; 105}; 106 107/* 108 * For the inodes to process list. 109 */ 110static struct process_inode_block *inodes_to_process; 111static int process_inode_count; 112 113static __u64 ext2_max_sizes[EXT2_MAX_BLOCK_LOG_SIZE - 114 EXT2_MIN_BLOCK_LOG_SIZE + 1]; 115 116/* 117 * Free all memory allocated by pass1 in preparation for restarting 118 * things. 119 */ 120static void unwind_pass1(ext2_filsys fs EXT2FS_ATTR((unused))) 121{ 122 ext2fs_free_mem(&inodes_to_process); 123 inodes_to_process = 0; 124} 125 126/* 127 * Check to make sure a device inode is real. Returns 1 if the device 128 * checks out, 0 if not. 129 * 130 * Note: this routine is now also used to check FIFO's and Sockets, 131 * since they have the same requirement; the i_block fields should be 132 * zero. 133 */ 134int e2fsck_pass1_check_device_inode(ext2_filsys fs EXT2FS_ATTR((unused)), 135 struct ext2_inode *inode) 136{ 137 int i; 138 139 /* 140 * If the index flag is set, then this is a bogus 141 * device/fifo/socket 142 */ 143 if (inode->i_flags & EXT2_INDEX_FL) 144 return 0; 145 146 /* 147 * We should be able to do the test below all the time, but 148 * because the kernel doesn't forcibly clear the device 149 * inode's additional i_block fields, there are some rare 150 * occasions when a legitimate device inode will have non-zero 151 * additional i_block fields. So for now, we only complain 152 * when the immutable flag is set, which should never happen 153 * for devices. (And that's when the problem is caused, since 154 * you can't set or clear immutable flags for devices.) Once 155 * the kernel has been fixed we can change this... 156 */ 157 if (inode->i_flags & (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)) { 158 for (i=4; i < EXT2_N_BLOCKS; i++) 159 if (inode->i_block[i]) 160 return 0; 161 } 162 return 1; 163} 164 165/* 166 * Check to make sure a symlink inode is real. Returns 1 if the symlink 167 * checks out, 0 if not. 168 */ 169int e2fsck_pass1_check_symlink(ext2_filsys fs, ext2_ino_t ino, 170 struct ext2_inode *inode, char *buf) 171{ 172 unsigned int len; 173 int i; 174 blk64_t blocks; 175 ext2_extent_handle_t handle; 176 struct ext2_extent_info info; 177 struct ext2fs_extent extent; 178 179 if ((inode->i_size_high || inode->i_size == 0) || 180 (inode->i_flags & EXT2_INDEX_FL)) 181 return 0; 182 183 if (inode->i_flags & EXT4_EXTENTS_FL) { 184 if (inode->i_size > fs->blocksize) 185 return 0; 186 if (ext2fs_extent_open2(fs, ino, inode, &handle)) 187 return 0; 188 i = 0; 189 if (ext2fs_extent_get_info(handle, &info) || 190 (info.num_entries != 1) || 191 (info.max_depth != 0)) 192 goto exit_extent; 193 if (ext2fs_extent_get(handle, EXT2_EXTENT_ROOT, &extent) || 194 (extent.e_lblk != 0) || 195 (extent.e_len != 1) || 196 (extent.e_pblk < fs->super->s_first_data_block) || 197 (extent.e_pblk >= ext2fs_blocks_count(fs->super))) 198 goto exit_extent; 199 i = 1; 200 exit_extent: 201 ext2fs_extent_free(handle); 202 return i; 203 } 204 205 blocks = ext2fs_inode_data_blocks2(fs, inode); 206 if (blocks) { 207 if ((inode->i_size >= fs->blocksize) || 208 (blocks != fs->blocksize >> 9) || 209 (inode->i_block[0] < fs->super->s_first_data_block) || 210 (inode->i_block[0] >= ext2fs_blocks_count(fs->super))) 211 return 0; 212 213 for (i = 1; i < EXT2_N_BLOCKS; i++) 214 if (inode->i_block[i]) 215 return 0; 216 217 if (io_channel_read_blk64(fs->io, inode->i_block[0], 1, buf)) 218 return 0; 219 220 len = strnlen(buf, fs->blocksize); 221 if (len == fs->blocksize) 222 return 0; 223 } else { 224 if (inode->i_size >= sizeof(inode->i_block)) 225 return 0; 226 227 len = strnlen((char *)inode->i_block, sizeof(inode->i_block)); 228 if (len == sizeof(inode->i_block)) 229 return 0; 230 } 231 if (len != inode->i_size) 232 return 0; 233 return 1; 234} 235 236/* 237 * If the immutable (or append-only) flag is set on the inode, offer 238 * to clear it. 239 */ 240#define BAD_SPECIAL_FLAGS (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL) 241static void check_immutable(e2fsck_t ctx, struct problem_context *pctx) 242{ 243 if (!(pctx->inode->i_flags & BAD_SPECIAL_FLAGS)) 244 return; 245 246 if (!fix_problem(ctx, PR_1_SET_IMMUTABLE, pctx)) 247 return; 248 249 pctx->inode->i_flags &= ~BAD_SPECIAL_FLAGS; 250 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1"); 251} 252 253/* 254 * If device, fifo or socket, check size is zero -- if not offer to 255 * clear it 256 */ 257static void check_size(e2fsck_t ctx, struct problem_context *pctx) 258{ 259 struct ext2_inode *inode = pctx->inode; 260 261 if (EXT2_I_SIZE(inode) == 0) 262 return; 263 264 if (!fix_problem(ctx, PR_1_SET_NONZSIZE, pctx)) 265 return; 266 267 inode->i_size = 0; 268 inode->i_size_high = 0; 269 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1"); 270} 271 272static void check_ea_in_inode(e2fsck_t ctx, struct problem_context *pctx) 273{ 274 struct ext2_super_block *sb = ctx->fs->super; 275 struct ext2_inode_large *inode; 276 struct ext2_ext_attr_entry *entry; 277 char *start, *end; 278 unsigned int storage_size, remain; 279 int problem = 0; 280 281 inode = (struct ext2_inode_large *) pctx->inode; 282 storage_size = EXT2_INODE_SIZE(ctx->fs->super) - EXT2_GOOD_OLD_INODE_SIZE - 283 inode->i_extra_isize; 284 start = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE + 285 inode->i_extra_isize + sizeof(__u32); 286 end = (char *) inode + EXT2_INODE_SIZE(ctx->fs->super); 287 entry = (struct ext2_ext_attr_entry *) start; 288 289 /* scan all entry's headers first */ 290 291 /* take finish entry 0UL into account */ 292 remain = storage_size - sizeof(__u32); 293 294 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) { 295 __u32 hash; 296 297 /* header eats this space */ 298 remain -= sizeof(struct ext2_ext_attr_entry); 299 300 /* is attribute name valid? */ 301 if (EXT2_EXT_ATTR_SIZE(entry->e_name_len) > remain) { 302 pctx->num = entry->e_name_len; 303 problem = PR_1_ATTR_NAME_LEN; 304 goto fix; 305 } 306 307 /* attribute len eats this space */ 308 remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len); 309 310 /* check value size */ 311 if (entry->e_value_size == 0 || entry->e_value_size > remain) { 312 pctx->num = entry->e_value_size; 313 problem = PR_1_ATTR_VALUE_SIZE; 314 goto fix; 315 } 316 317 /* e_value_block must be 0 in inode's ea */ 318 if (entry->e_value_block != 0) { 319 pctx->num = entry->e_value_block; 320 problem = PR_1_ATTR_VALUE_BLOCK; 321 goto fix; 322 } 323 324 hash = ext2fs_ext_attr_hash_entry(entry, 325 start + entry->e_value_offs); 326 327 /* e_hash may be 0 in older inode's ea */ 328 if (entry->e_hash != 0 && entry->e_hash != hash) { 329 pctx->num = entry->e_hash; 330 problem = PR_1_ATTR_HASH; 331 goto fix; 332 } 333 334 remain -= entry->e_value_size; 335 336 entry = EXT2_EXT_ATTR_NEXT(entry); 337 } 338fix: 339 /* 340 * it seems like a corruption. it's very unlikely we could repair 341 * EA(s) in automatic fashion -bzzz 342 */ 343 if (problem == 0 || !fix_problem(ctx, problem, pctx)) 344 return; 345 346 /* simply remove all possible EA(s) */ 347 *((__u32 *)start) = 0UL; 348 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode, 349 EXT2_INODE_SIZE(sb), "pass1"); 350} 351 352static void check_inode_extra_space(e2fsck_t ctx, struct problem_context *pctx) 353{ 354 struct ext2_super_block *sb = ctx->fs->super; 355 struct ext2_inode_large *inode; 356 __u32 *eamagic; 357 int min, max; 358 359 inode = (struct ext2_inode_large *) pctx->inode; 360 if (EXT2_INODE_SIZE(sb) == EXT2_GOOD_OLD_INODE_SIZE) { 361 /* this isn't large inode. so, nothing to check */ 362 return; 363 } 364 365#if 0 366 printf("inode #%u, i_extra_size %d\n", pctx->ino, 367 inode->i_extra_isize); 368#endif 369 /* i_extra_isize must cover i_extra_isize + i_checksum_hi at least */ 370 min = sizeof(inode->i_extra_isize) + sizeof(inode->i_checksum_hi); 371 max = EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE; 372 /* 373 * For now we will allow i_extra_isize to be 0, but really 374 * implementations should never allow i_extra_isize to be 0 375 */ 376 if (inode->i_extra_isize && 377 (inode->i_extra_isize < min || inode->i_extra_isize > max)) { 378 if (!fix_problem(ctx, PR_1_EXTRA_ISIZE, pctx)) 379 return; 380 inode->i_extra_isize = min; 381 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode, 382 EXT2_INODE_SIZE(sb), "pass1"); 383 return; 384 } 385 386 eamagic = (__u32 *) (((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE + 387 inode->i_extra_isize); 388 if (*eamagic == EXT2_EXT_ATTR_MAGIC) { 389 /* it seems inode has an extended attribute(s) in body */ 390 check_ea_in_inode(ctx, pctx); 391 } 392} 393 394/* 395 * Check to see if the inode might really be a directory, despite i_mode 396 * 397 * This is a lot of complexity for something for which I'm not really 398 * convinced happens frequently in the wild. If for any reason this 399 * causes any problems, take this code out. 400 * [tytso:20070331.0827EDT] 401 */ 402static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx, 403 char *buf) 404{ 405 struct ext2_inode *inode = pctx->inode; 406 struct ext2_dir_entry *dirent; 407 errcode_t retval; 408 blk64_t blk; 409 unsigned int i, rec_len, not_device = 0; 410 int extent_fs; 411 412 /* 413 * If the mode looks OK, we believe it. If the first block in 414 * the i_block array is 0, this cannot be a directory. If the 415 * inode is extent-mapped, it is still the case that the latter 416 * cannot be 0 - the magic number in the extent header would make 417 * it nonzero. 418 */ 419 if (LINUX_S_ISDIR(inode->i_mode) || LINUX_S_ISREG(inode->i_mode) || 420 LINUX_S_ISLNK(inode->i_mode) || inode->i_block[0] == 0) 421 return; 422 423 /* 424 * Check the block numbers in the i_block array for validity: 425 * zero blocks are skipped (but the first one cannot be zero - 426 * see above), other blocks are checked against the first and 427 * max data blocks (from the the superblock) and against the 428 * block bitmap. Any invalid block found means this cannot be 429 * a directory. 430 * 431 * If there are non-zero blocks past the fourth entry, then 432 * this cannot be a device file: we remember that for the next 433 * check. 434 * 435 * For extent mapped files, we don't do any sanity checking: 436 * just try to get the phys block of logical block 0 and run 437 * with it. 438 */ 439 440 extent_fs = (ctx->fs->super->s_feature_incompat & 441 EXT3_FEATURE_INCOMPAT_EXTENTS); 442 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) { 443 /* extent mapped */ 444 if (ext2fs_bmap2(ctx->fs, pctx->ino, inode, 0, 0, 0, 0, 445 &blk)) 446 return; 447 /* device files are never extent mapped */ 448 not_device++; 449 } else { 450 for (i=0; i < EXT2_N_BLOCKS; i++) { 451 blk = inode->i_block[i]; 452 if (!blk) 453 continue; 454 if (i >= 4) 455 not_device++; 456 457 if (blk < ctx->fs->super->s_first_data_block || 458 blk >= ext2fs_blocks_count(ctx->fs->super) || 459 ext2fs_fast_test_block_bitmap2(ctx->block_found_map, 460 blk)) 461 return; /* Invalid block, can't be dir */ 462 } 463 blk = inode->i_block[0]; 464 } 465 466 /* 467 * If the mode says this is a device file and the i_links_count field 468 * is sane and we have not ruled it out as a device file previously, 469 * we declare it a device file, not a directory. 470 */ 471 if ((LINUX_S_ISCHR(inode->i_mode) || LINUX_S_ISBLK(inode->i_mode)) && 472 (inode->i_links_count == 1) && !not_device) 473 return; 474 475 /* read the first block */ 476 ehandler_operation(_("reading directory block")); 477 retval = ext2fs_read_dir_block3(ctx->fs, blk, buf, 0); 478 ehandler_operation(0); 479 if (retval) 480 return; 481 482 dirent = (struct ext2_dir_entry *) buf; 483 retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len); 484 if (retval) 485 return; 486 if (((dirent->name_len & 0xFF) != 1) || 487 (dirent->name[0] != '.') || 488 (dirent->inode != pctx->ino) || 489 (rec_len < 12) || 490 (rec_len % 4) || 491 (rec_len >= ctx->fs->blocksize - 12)) 492 return; 493 494 dirent = (struct ext2_dir_entry *) (buf + rec_len); 495 retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len); 496 if (retval) 497 return; 498 if (((dirent->name_len & 0xFF) != 2) || 499 (dirent->name[0] != '.') || 500 (dirent->name[1] != '.') || 501 (rec_len < 12) || 502 (rec_len % 4)) 503 return; 504 505 if (fix_problem(ctx, PR_1_TREAT_AS_DIRECTORY, pctx)) { 506 inode->i_mode = (inode->i_mode & 07777) | LINUX_S_IFDIR; 507 e2fsck_write_inode_full(ctx, pctx->ino, inode, 508 EXT2_INODE_SIZE(ctx->fs->super), 509 "check_is_really_dir"); 510 } 511} 512 513extern void e2fsck_setup_tdb_icount(e2fsck_t ctx, int flags, 514 ext2_icount_t *ret) 515{ 516 unsigned int threshold; 517 ext2_ino_t num_dirs; 518 errcode_t retval; 519 char *tdb_dir; 520 int enable; 521 522 *ret = 0; 523 524 profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0, 525 &tdb_dir); 526 profile_get_uint(ctx->profile, "scratch_files", 527 "numdirs_threshold", 0, 0, &threshold); 528 profile_get_boolean(ctx->profile, "scratch_files", 529 "icount", 0, 1, &enable); 530 531 retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs); 532 if (retval) 533 num_dirs = 1024; /* Guess */ 534 535 if (!enable || !tdb_dir || access(tdb_dir, W_OK) || 536 (threshold && num_dirs <= threshold)) 537 return; 538 539 retval = ext2fs_create_icount_tdb(ctx->fs, tdb_dir, flags, ret); 540 if (retval) 541 *ret = 0; 542} 543 544void e2fsck_pass1(e2fsck_t ctx) 545{ 546 int i; 547 __u64 max_sizes; 548 ext2_filsys fs = ctx->fs; 549 ext2_ino_t ino = 0; 550 struct ext2_inode *inode; 551 ext2_inode_scan scan; 552 char *block_buf; 553#ifdef RESOURCE_TRACK 554 struct resource_track rtrack; 555#endif 556 unsigned char frag, fsize; 557 struct problem_context pctx; 558 struct scan_callback_struct scan_struct; 559 struct ext2_super_block *sb = ctx->fs->super; 560 const char *old_op; 561 unsigned int save_type; 562 int imagic_fs, extent_fs; 563 int busted_fs_time = 0; 564 int inode_size; 565 566 init_resource_track(&rtrack, ctx->fs->io); 567 clear_problem_context(&pctx); 568 569 if (!(ctx->options & E2F_OPT_PREEN)) 570 fix_problem(ctx, PR_1_PASS_HEADER, &pctx); 571 572 if ((fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) && 573 !(ctx->options & E2F_OPT_NO)) { 574 if (ext2fs_u32_list_create(&ctx->dirs_to_hash, 50)) 575 ctx->dirs_to_hash = 0; 576 } 577 578#ifdef MTRACE 579 mtrace_print("Pass 1"); 580#endif 581 582#define EXT2_BPP(bits) (1ULL << ((bits) - 2)) 583 584 for (i = EXT2_MIN_BLOCK_LOG_SIZE; i <= EXT2_MAX_BLOCK_LOG_SIZE; i++) { 585 max_sizes = EXT2_NDIR_BLOCKS + EXT2_BPP(i); 586 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i); 587 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i) * EXT2_BPP(i); 588 max_sizes = (max_sizes * (1UL << i)); 589 ext2_max_sizes[i - EXT2_MIN_BLOCK_LOG_SIZE] = max_sizes; 590 } 591#undef EXT2_BPP 592 593 imagic_fs = (sb->s_feature_compat & EXT2_FEATURE_COMPAT_IMAGIC_INODES); 594 extent_fs = (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS); 595 596 /* 597 * Allocate bitmaps structures 598 */ 599 pctx.errcode = e2fsck_allocate_inode_bitmap(fs, _("in-use inode map"), 600 EXT2FS_BMAP64_RBTREE, 601 "inode_used_map", 602 &ctx->inode_used_map); 603 if (pctx.errcode) { 604 pctx.num = 1; 605 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx); 606 ctx->flags |= E2F_FLAG_ABORT; 607 return; 608 } 609 pctx.errcode = e2fsck_allocate_inode_bitmap(fs, 610 _("directory inode map"), 611 EXT2FS_BMAP64_AUTODIR, 612 "inode_dir_map", &ctx->inode_dir_map); 613 if (pctx.errcode) { 614 pctx.num = 2; 615 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx); 616 ctx->flags |= E2F_FLAG_ABORT; 617 return; 618 } 619 pctx.errcode = e2fsck_allocate_inode_bitmap(fs, 620 _("regular file inode map"), EXT2FS_BMAP64_RBTREE, 621 "inode_reg_map", &ctx->inode_reg_map); 622 if (pctx.errcode) { 623 pctx.num = 6; 624 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx); 625 ctx->flags |= E2F_FLAG_ABORT; 626 return; 627 } 628 pctx.errcode = e2fsck_allocate_subcluster_bitmap(fs, 629 _("in-use block map"), EXT2FS_BMAP64_RBTREE, 630 "block_found_map", &ctx->block_found_map); 631 if (pctx.errcode) { 632 pctx.num = 1; 633 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx); 634 ctx->flags |= E2F_FLAG_ABORT; 635 return; 636 } 637 e2fsck_setup_tdb_icount(ctx, 0, &ctx->inode_link_info); 638 if (!ctx->inode_link_info) { 639 e2fsck_set_bitmap_type(fs, EXT2FS_BMAP64_RBTREE, 640 "inode_link_info", &save_type); 641 pctx.errcode = ext2fs_create_icount2(fs, 0, 0, 0, 642 &ctx->inode_link_info); 643 fs->default_bitmap_type = save_type; 644 } 645 646 if (pctx.errcode) { 647 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx); 648 ctx->flags |= E2F_FLAG_ABORT; 649 return; 650 } 651 inode_size = EXT2_INODE_SIZE(fs->super); 652 inode = (struct ext2_inode *) 653 e2fsck_allocate_memory(ctx, inode_size, "scratch inode"); 654 655 inodes_to_process = (struct process_inode_block *) 656 e2fsck_allocate_memory(ctx, 657 (ctx->process_inode_size * 658 sizeof(struct process_inode_block)), 659 "array of inodes to process"); 660 process_inode_count = 0; 661 662 pctx.errcode = ext2fs_init_dblist(fs, 0); 663 if (pctx.errcode) { 664 fix_problem(ctx, PR_1_ALLOCATE_DBCOUNT, &pctx); 665 ctx->flags |= E2F_FLAG_ABORT; 666 ext2fs_free_mem(&inode); 667 return; 668 } 669 670 /* 671 * If the last orphan field is set, clear it, since the pass1 672 * processing will automatically find and clear the orphans. 673 * In the future, we may want to try using the last_orphan 674 * linked list ourselves, but for now, we clear it so that the 675 * ext3 mount code won't get confused. 676 */ 677 if (!(ctx->options & E2F_OPT_READONLY)) { 678 if (fs->super->s_last_orphan) { 679 fs->super->s_last_orphan = 0; 680 ext2fs_mark_super_dirty(fs); 681 } 682 } 683 684 mark_table_blocks(ctx); 685 pctx.errcode = ext2fs_convert_subcluster_bitmap(fs, 686 &ctx->block_found_map); 687 if (pctx.errcode) { 688 fix_problem(ctx, PR_1_CONVERT_SUBCLUSTER, &pctx); 689 ctx->flags |= E2F_FLAG_ABORT; 690 ext2fs_free_mem(&inode); 691 return; 692 } 693 block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3, 694 "block interate buffer"); 695 e2fsck_use_inode_shortcuts(ctx, 1); 696 old_op = ehandler_operation(_("opening inode scan")); 697 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks, 698 &scan); 699 ehandler_operation(old_op); 700 if (pctx.errcode) { 701 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx); 702 ctx->flags |= E2F_FLAG_ABORT; 703 ext2fs_free_mem(&block_buf); 704 ext2fs_free_mem(&inode); 705 return; 706 } 707 ext2fs_inode_scan_flags(scan, EXT2_SF_SKIP_MISSING_ITABLE, 0); 708 ctx->stashed_inode = inode; 709 scan_struct.ctx = ctx; 710 scan_struct.block_buf = block_buf; 711 ext2fs_set_inode_callback(scan, scan_callback, &scan_struct); 712 if (ctx->progress) 713 if ((ctx->progress)(ctx, 1, 0, ctx->fs->group_desc_count)) 714 return; 715 if ((fs->super->s_wtime < fs->super->s_inodes_count) || 716 (fs->super->s_mtime < fs->super->s_inodes_count)) 717 busted_fs_time = 1; 718 719 if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) && 720 !(fs->super->s_mmp_block <= fs->super->s_first_data_block || 721 fs->super->s_mmp_block >= fs->super->s_blocks_count)) 722 ext2fs_mark_block_bitmap2(ctx->block_found_map, 723 fs->super->s_mmp_block); 724 725 while (1) { 726 if (ino % (fs->super->s_inodes_per_group * 4) == 1) { 727 if (e2fsck_mmp_update(fs)) 728 fatal_error(ctx, 0); 729 } 730 old_op = ehandler_operation(_("getting next inode from scan")); 731 pctx.errcode = ext2fs_get_next_inode_full(scan, &ino, 732 inode, inode_size); 733 ehandler_operation(old_op); 734 if (ctx->flags & E2F_FLAG_SIGNAL_MASK) 735 return; 736 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) { 737 if (!ctx->inode_bb_map) 738 alloc_bb_map(ctx); 739 ext2fs_mark_inode_bitmap2(ctx->inode_bb_map, ino); 740 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino); 741 continue; 742 } 743 if (pctx.errcode) { 744 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx); 745 ctx->flags |= E2F_FLAG_ABORT; 746 return; 747 } 748 if (!ino) 749 break; 750 pctx.ino = ino; 751 pctx.inode = inode; 752 ctx->stashed_ino = ino; 753 if (inode->i_links_count) { 754 pctx.errcode = ext2fs_icount_store(ctx->inode_link_info, 755 ino, inode->i_links_count); 756 if (pctx.errcode) { 757 pctx.num = inode->i_links_count; 758 fix_problem(ctx, PR_1_ICOUNT_STORE, &pctx); 759 ctx->flags |= E2F_FLAG_ABORT; 760 return; 761 } 762 } 763 764 /* 765 * Test for incorrect extent flag settings. 766 * 767 * On big-endian machines we must be careful: 768 * When the inode is read, the i_block array is not swapped 769 * if the extent flag is set. Therefore if we are testing 770 * for or fixing a wrongly-set flag, we must potentially 771 * (un)swap before testing, or after fixing. 772 */ 773 774 /* 775 * In this case the extents flag was set when read, so 776 * extent_header_verify is ok. If the inode is cleared, 777 * no need to swap... so no extra swapping here. 778 */ 779 if ((inode->i_flags & EXT4_EXTENTS_FL) && !extent_fs && 780 (inode->i_links_count || (ino == EXT2_BAD_INO) || 781 (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO))) { 782 if ((ext2fs_extent_header_verify(inode->i_block, 783 sizeof(inode->i_block)) == 0) && 784 fix_problem(ctx, PR_1_EXTENT_FEATURE, &pctx)) { 785 sb->s_feature_incompat |= EXT3_FEATURE_INCOMPAT_EXTENTS; 786 ext2fs_mark_super_dirty(fs); 787 extent_fs = 1; 788 } else if (fix_problem(ctx, PR_1_EXTENTS_SET, &pctx)) { 789 clear_inode: 790 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1"); 791 if (ino == EXT2_BAD_INO) 792 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, 793 ino); 794 continue; 795 } 796 } 797 798 /* 799 * For big-endian machines: 800 * If the inode didn't have the extents flag set when it 801 * was read, then the i_blocks array was swapped. To test 802 * as an extents header, we must swap it back first. 803 * IF we then set the extents flag, the entire i_block 804 * array must be un/re-swapped to make it proper extents data. 805 */ 806 if (extent_fs && !(inode->i_flags & EXT4_EXTENTS_FL) && 807 (inode->i_links_count || (ino == EXT2_BAD_INO) || 808 (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO)) && 809 (LINUX_S_ISREG(inode->i_mode) || 810 LINUX_S_ISDIR(inode->i_mode))) { 811 void *ehp; 812#ifdef WORDS_BIGENDIAN 813 __u32 tmp_block[EXT2_N_BLOCKS]; 814 815 for (i = 0; i < EXT2_N_BLOCKS; i++) 816 tmp_block[i] = ext2fs_swab32(inode->i_block[i]); 817 ehp = tmp_block; 818#else 819 ehp = inode->i_block; 820#endif 821 if ((ext2fs_extent_header_verify(ehp, 822 sizeof(inode->i_block)) == 0) && 823 (fix_problem(ctx, PR_1_UNSET_EXTENT_FL, &pctx))) { 824 inode->i_flags |= EXT4_EXTENTS_FL; 825#ifdef WORDS_BIGENDIAN 826 memcpy(inode->i_block, tmp_block, 827 sizeof(inode->i_block)); 828#endif 829 e2fsck_write_inode(ctx, ino, inode, "pass1"); 830 } 831 } 832 833 if (ino == EXT2_BAD_INO) { 834 struct process_block_struct pb; 835 836 if ((inode->i_mode || inode->i_uid || inode->i_gid || 837 inode->i_links_count || inode->i_file_acl) && 838 fix_problem(ctx, PR_1_INVALID_BAD_INODE, &pctx)) { 839 memset(inode, 0, sizeof(struct ext2_inode)); 840 e2fsck_write_inode(ctx, ino, inode, 841 "clear bad inode"); 842 } 843 844 pctx.errcode = ext2fs_copy_bitmap(ctx->block_found_map, 845 &pb.fs_meta_blocks); 846 if (pctx.errcode) { 847 pctx.num = 4; 848 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx); 849 ctx->flags |= E2F_FLAG_ABORT; 850 return; 851 } 852 pb.ino = EXT2_BAD_INO; 853 pb.num_blocks = pb.last_block = 0; 854 pb.last_db_block = -1; 855 pb.num_illegal_blocks = 0; 856 pb.suppress = 0; pb.clear = 0; pb.is_dir = 0; 857 pb.is_reg = 0; pb.fragmented = 0; pb.bbcheck = 0; 858 pb.inode = inode; 859 pb.pctx = &pctx; 860 pb.ctx = ctx; 861 pctx.errcode = ext2fs_block_iterate3(fs, ino, 0, 862 block_buf, process_bad_block, &pb); 863 ext2fs_free_block_bitmap(pb.fs_meta_blocks); 864 if (pctx.errcode) { 865 fix_problem(ctx, PR_1_BLOCK_ITERATE, &pctx); 866 ctx->flags |= E2F_FLAG_ABORT; 867 return; 868 } 869 if (pb.bbcheck) 870 if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK_PROMPT, &pctx)) { 871 ctx->flags |= E2F_FLAG_ABORT; 872 return; 873 } 874 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino); 875 clear_problem_context(&pctx); 876 continue; 877 } else if (ino == EXT2_ROOT_INO) { 878 /* 879 * Make sure the root inode is a directory; if 880 * not, offer to clear it. It will be 881 * regnerated in pass #3. 882 */ 883 if (!LINUX_S_ISDIR(inode->i_mode)) { 884 if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx)) 885 goto clear_inode; 886 } 887 /* 888 * If dtime is set, offer to clear it. mke2fs 889 * version 0.2b created filesystems with the 890 * dtime field set for the root and lost+found 891 * directories. We won't worry about 892 * /lost+found, since that can be regenerated 893 * easily. But we will fix the root directory 894 * as a special case. 895 */ 896 if (inode->i_dtime && inode->i_links_count) { 897 if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) { 898 inode->i_dtime = 0; 899 e2fsck_write_inode(ctx, ino, inode, 900 "pass1"); 901 } 902 } 903 } else if (ino == EXT2_JOURNAL_INO) { 904 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino); 905 if (fs->super->s_journal_inum == EXT2_JOURNAL_INO) { 906 if (!LINUX_S_ISREG(inode->i_mode) && 907 fix_problem(ctx, PR_1_JOURNAL_BAD_MODE, 908 &pctx)) { 909 inode->i_mode = LINUX_S_IFREG; 910 e2fsck_write_inode(ctx, ino, inode, 911 "pass1"); 912 } 913 check_blocks(ctx, &pctx, block_buf); 914 continue; 915 } 916 if ((inode->i_links_count || 917 inode->i_blocks || inode->i_block[0]) && 918 fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR, 919 &pctx)) { 920 memset(inode, 0, inode_size); 921 ext2fs_icount_store(ctx->inode_link_info, 922 ino, 0); 923 e2fsck_write_inode_full(ctx, ino, inode, 924 inode_size, "pass1"); 925 } 926 } else if ((ino == EXT4_USR_QUOTA_INO) || 927 (ino == EXT4_GRP_QUOTA_INO)) { 928 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino); 929 if ((fs->super->s_feature_ro_compat & 930 EXT4_FEATURE_RO_COMPAT_QUOTA) && 931 ((fs->super->s_usr_quota_inum == ino) || 932 (fs->super->s_grp_quota_inum == ino))) { 933 if (!LINUX_S_ISREG(inode->i_mode) && 934 fix_problem(ctx, PR_1_QUOTA_BAD_MODE, 935 &pctx)) { 936 inode->i_mode = LINUX_S_IFREG; 937 e2fsck_write_inode(ctx, ino, inode, 938 "pass1"); 939 } 940 check_blocks(ctx, &pctx, block_buf); 941 continue; 942 } 943 if ((inode->i_links_count || 944 inode->i_blocks || inode->i_block[0]) && 945 fix_problem(ctx, PR_1_QUOTA_INODE_NOT_CLEAR, 946 &pctx)) { 947 memset(inode, 0, inode_size); 948 ext2fs_icount_store(ctx->inode_link_info, 949 ino, 0); 950 e2fsck_write_inode_full(ctx, ino, inode, 951 inode_size, "pass1"); 952 } 953 } else if (ino < EXT2_FIRST_INODE(fs->super)) { 954 int problem = 0; 955 956 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino); 957 if (ino == EXT2_BOOT_LOADER_INO) { 958 if (LINUX_S_ISDIR(inode->i_mode)) 959 problem = PR_1_RESERVED_BAD_MODE; 960 } else if (ino == EXT2_RESIZE_INO) { 961 if (inode->i_mode && 962 !LINUX_S_ISREG(inode->i_mode)) 963 problem = PR_1_RESERVED_BAD_MODE; 964 } else { 965 if (inode->i_mode != 0) 966 problem = PR_1_RESERVED_BAD_MODE; 967 } 968 if (problem) { 969 if (fix_problem(ctx, problem, &pctx)) { 970 inode->i_mode = 0; 971 e2fsck_write_inode(ctx, ino, inode, 972 "pass1"); 973 } 974 } 975 check_blocks(ctx, &pctx, block_buf); 976 continue; 977 } 978 979 /* 980 * Check for inodes who might have been part of the 981 * orphaned list linked list. They should have gotten 982 * dealt with by now, unless the list had somehow been 983 * corrupted. 984 * 985 * FIXME: In the future, inodes which are still in use 986 * (and which are therefore) pending truncation should 987 * be handled specially. Right now we just clear the 988 * dtime field, and the normal e2fsck handling of 989 * inodes where i_size and the inode blocks are 990 * inconsistent is to fix i_size, instead of releasing 991 * the extra blocks. This won't catch the inodes that 992 * was at the end of the orphan list, but it's better 993 * than nothing. The right answer is that there 994 * shouldn't be any bugs in the orphan list handling. :-) 995 */ 996 if (inode->i_dtime && !busted_fs_time && 997 inode->i_dtime < ctx->fs->super->s_inodes_count) { 998 if (fix_problem(ctx, PR_1_LOW_DTIME, &pctx)) { 999 inode->i_dtime = inode->i_links_count ? 1000 0 : ctx->now; 1001 e2fsck_write_inode(ctx, ino, inode, 1002 "pass1"); 1003 } 1004 } 1005 1006 /* 1007 * This code assumes that deleted inodes have 1008 * i_links_count set to 0. 1009 */ 1010 if (!inode->i_links_count) { 1011 if (!inode->i_dtime && inode->i_mode) { 1012 if (fix_problem(ctx, 1013 PR_1_ZERO_DTIME, &pctx)) { 1014 inode->i_dtime = ctx->now; 1015 e2fsck_write_inode(ctx, ino, inode, 1016 "pass1"); 1017 } 1018 } 1019 continue; 1020 } 1021 /* 1022 * n.b. 0.3c ext2fs code didn't clear i_links_count for 1023 * deleted files. Oops. 1024 * 1025 * Since all new ext2 implementations get this right, 1026 * we now assume that the case of non-zero 1027 * i_links_count and non-zero dtime means that we 1028 * should keep the file, not delete it. 1029 * 1030 */ 1031 if (inode->i_dtime) { 1032 if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) { 1033 inode->i_dtime = 0; 1034 e2fsck_write_inode(ctx, ino, inode, "pass1"); 1035 } 1036 } 1037 1038 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino); 1039 switch (fs->super->s_creator_os) { 1040 case EXT2_OS_HURD: 1041 frag = inode->osd2.hurd2.h_i_frag; 1042 fsize = inode->osd2.hurd2.h_i_fsize; 1043 break; 1044 default: 1045 frag = fsize = 0; 1046 } 1047 1048 if (inode->i_faddr || frag || fsize || 1049 (LINUX_S_ISDIR(inode->i_mode) && inode->i_dir_acl)) 1050 mark_inode_bad(ctx, ino); 1051 if (!(fs->super->s_feature_incompat & 1052 EXT4_FEATURE_INCOMPAT_64BIT) && 1053 inode->osd2.linux2.l_i_file_acl_high != 0) 1054 mark_inode_bad(ctx, ino); 1055 if ((fs->super->s_creator_os == EXT2_OS_LINUX) && 1056 !(fs->super->s_feature_ro_compat & 1057 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) && 1058 (inode->osd2.linux2.l_i_blocks_hi != 0)) 1059 mark_inode_bad(ctx, ino); 1060 if (inode->i_flags & EXT2_IMAGIC_FL) { 1061 if (imagic_fs) { 1062 if (!ctx->inode_imagic_map) 1063 alloc_imagic_map(ctx); 1064 ext2fs_mark_inode_bitmap2(ctx->inode_imagic_map, 1065 ino); 1066 } else { 1067 if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) { 1068 inode->i_flags &= ~EXT2_IMAGIC_FL; 1069 e2fsck_write_inode(ctx, ino, 1070 inode, "pass1"); 1071 } 1072 } 1073 } 1074 1075 check_inode_extra_space(ctx, &pctx); 1076 check_is_really_dir(ctx, &pctx, block_buf); 1077 1078 /* 1079 * ext2fs_inode_has_valid_blocks2 does not actually look 1080 * at i_block[] values, so not endian-sensitive here. 1081 */ 1082 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL) && 1083 LINUX_S_ISLNK(inode->i_mode) && 1084 !ext2fs_inode_has_valid_blocks2(fs, inode) && 1085 fix_problem(ctx, PR_1_FAST_SYMLINK_EXTENT_FL, &pctx)) { 1086 inode->i_flags &= ~EXT4_EXTENTS_FL; 1087 e2fsck_write_inode(ctx, ino, inode, "pass1"); 1088 } 1089 1090 if (LINUX_S_ISDIR(inode->i_mode)) { 1091 ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, ino); 1092 e2fsck_add_dir_info(ctx, ino, 0); 1093 ctx->fs_directory_count++; 1094 } else if (LINUX_S_ISREG (inode->i_mode)) { 1095 ext2fs_mark_inode_bitmap2(ctx->inode_reg_map, ino); 1096 ctx->fs_regular_count++; 1097 } else if (LINUX_S_ISCHR (inode->i_mode) && 1098 e2fsck_pass1_check_device_inode(fs, inode)) { 1099 check_immutable(ctx, &pctx); 1100 check_size(ctx, &pctx); 1101 ctx->fs_chardev_count++; 1102 } else if (LINUX_S_ISBLK (inode->i_mode) && 1103 e2fsck_pass1_check_device_inode(fs, inode)) { 1104 check_immutable(ctx, &pctx); 1105 check_size(ctx, &pctx); 1106 ctx->fs_blockdev_count++; 1107 } else if (LINUX_S_ISLNK (inode->i_mode) && 1108 e2fsck_pass1_check_symlink(fs, ino, inode, 1109 block_buf)) { 1110 check_immutable(ctx, &pctx); 1111 ctx->fs_symlinks_count++; 1112 if (ext2fs_inode_data_blocks(fs, inode) == 0) { 1113 ctx->fs_fast_symlinks_count++; 1114 check_blocks(ctx, &pctx, block_buf); 1115 continue; 1116 } 1117 } 1118 else if (LINUX_S_ISFIFO (inode->i_mode) && 1119 e2fsck_pass1_check_device_inode(fs, inode)) { 1120 check_immutable(ctx, &pctx); 1121 check_size(ctx, &pctx); 1122 ctx->fs_fifo_count++; 1123 } else if ((LINUX_S_ISSOCK (inode->i_mode)) && 1124 e2fsck_pass1_check_device_inode(fs, inode)) { 1125 check_immutable(ctx, &pctx); 1126 check_size(ctx, &pctx); 1127 ctx->fs_sockets_count++; 1128 } else 1129 mark_inode_bad(ctx, ino); 1130 if (!(inode->i_flags & EXT4_EXTENTS_FL)) { 1131 if (inode->i_block[EXT2_IND_BLOCK]) 1132 ctx->fs_ind_count++; 1133 if (inode->i_block[EXT2_DIND_BLOCK]) 1134 ctx->fs_dind_count++; 1135 if (inode->i_block[EXT2_TIND_BLOCK]) 1136 ctx->fs_tind_count++; 1137 } 1138 if (!(inode->i_flags & EXT4_EXTENTS_FL) && 1139 (inode->i_block[EXT2_IND_BLOCK] || 1140 inode->i_block[EXT2_DIND_BLOCK] || 1141 inode->i_block[EXT2_TIND_BLOCK] || 1142 ext2fs_file_acl_block(fs, inode))) { 1143 inodes_to_process[process_inode_count].ino = ino; 1144 inodes_to_process[process_inode_count].inode = *inode; 1145 process_inode_count++; 1146 } else 1147 check_blocks(ctx, &pctx, block_buf); 1148 1149 if (ctx->flags & E2F_FLAG_SIGNAL_MASK) 1150 return; 1151 1152 if (process_inode_count >= ctx->process_inode_size) { 1153 process_inodes(ctx, block_buf); 1154 1155 if (ctx->flags & E2F_FLAG_SIGNAL_MASK) 1156 return; 1157 } 1158 } 1159 process_inodes(ctx, block_buf); 1160 ext2fs_close_inode_scan(scan); 1161 1162 /* 1163 * If any extended attribute blocks' reference counts need to 1164 * be adjusted, either up (ctx->refcount_extra), or down 1165 * (ctx->refcount), then fix them. 1166 */ 1167 if (ctx->refcount) { 1168 adjust_extattr_refcount(ctx, ctx->refcount, block_buf, -1); 1169 ea_refcount_free(ctx->refcount); 1170 ctx->refcount = 0; 1171 } 1172 if (ctx->refcount_extra) { 1173 adjust_extattr_refcount(ctx, ctx->refcount_extra, 1174 block_buf, +1); 1175 ea_refcount_free(ctx->refcount_extra); 1176 ctx->refcount_extra = 0; 1177 } 1178 1179 if (ctx->invalid_bitmaps) 1180 handle_fs_bad_blocks(ctx); 1181 1182 /* We don't need the block_ea_map any more */ 1183 if (ctx->block_ea_map) { 1184 ext2fs_free_block_bitmap(ctx->block_ea_map); 1185 ctx->block_ea_map = 0; 1186 } 1187 1188 if (ctx->flags & E2F_FLAG_RESIZE_INODE) { 1189 ext2fs_block_bitmap save_bmap; 1190 1191 save_bmap = fs->block_map; 1192 fs->block_map = ctx->block_found_map; 1193 clear_problem_context(&pctx); 1194 pctx.errcode = ext2fs_create_resize_inode(fs); 1195 if (pctx.errcode) { 1196 if (!fix_problem(ctx, PR_1_RESIZE_INODE_CREATE, 1197 &pctx)) { 1198 ctx->flags |= E2F_FLAG_ABORT; 1199 return; 1200 } 1201 pctx.errcode = 0; 1202 } 1203 if (!pctx.errcode) { 1204 e2fsck_read_inode(ctx, EXT2_RESIZE_INO, inode, 1205 "recreate inode"); 1206 inode->i_mtime = ctx->now; 1207 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, inode, 1208 "recreate inode"); 1209 } 1210 fs->block_map = save_bmap; 1211 ctx->flags &= ~E2F_FLAG_RESIZE_INODE; 1212 } 1213 1214 if (ctx->flags & E2F_FLAG_RESTART) { 1215 /* 1216 * Only the master copy of the superblock and block 1217 * group descriptors are going to be written during a 1218 * restart, so set the superblock to be used to be the 1219 * master superblock. 1220 */ 1221 ctx->use_superblock = 0; 1222 unwind_pass1(fs); 1223 goto endit; 1224 } 1225 1226 if (ctx->block_dup_map) { 1227 if (ctx->options & E2F_OPT_PREEN) { 1228 clear_problem_context(&pctx); 1229 fix_problem(ctx, PR_1_DUP_BLOCKS_PREENSTOP, &pctx); 1230 } 1231 e2fsck_pass1_dupblocks(ctx, block_buf); 1232 } 1233 ext2fs_free_mem(&inodes_to_process); 1234endit: 1235 e2fsck_use_inode_shortcuts(ctx, 0); 1236 1237 ext2fs_free_mem(&block_buf); 1238 ext2fs_free_mem(&inode); 1239 1240 print_resource_track(ctx, _("Pass 1"), &rtrack, ctx->fs->io); 1241} 1242 1243/* 1244 * When the inode_scan routines call this callback at the end of the 1245 * glock group, call process_inodes. 1246 */ 1247static errcode_t scan_callback(ext2_filsys fs, 1248 ext2_inode_scan scan EXT2FS_ATTR((unused)), 1249 dgrp_t group, void * priv_data) 1250{ 1251 struct scan_callback_struct *scan_struct; 1252 e2fsck_t ctx; 1253 1254 scan_struct = (struct scan_callback_struct *) priv_data; 1255 ctx = scan_struct->ctx; 1256 1257 process_inodes((e2fsck_t) fs->priv_data, scan_struct->block_buf); 1258 1259 if (ctx->progress) 1260 if ((ctx->progress)(ctx, 1, group+1, 1261 ctx->fs->group_desc_count)) 1262 return EXT2_ET_CANCEL_REQUESTED; 1263 1264 return 0; 1265} 1266 1267/* 1268 * Process the inodes in the "inodes to process" list. 1269 */ 1270static void process_inodes(e2fsck_t ctx, char *block_buf) 1271{ 1272 int i; 1273 struct ext2_inode *old_stashed_inode; 1274 ext2_ino_t old_stashed_ino; 1275 const char *old_operation; 1276 char buf[80]; 1277 struct problem_context pctx; 1278 1279#if 0 1280 printf("begin process_inodes: "); 1281#endif 1282 if (process_inode_count == 0) 1283 return; 1284 old_operation = ehandler_operation(0); 1285 old_stashed_inode = ctx->stashed_inode; 1286 old_stashed_ino = ctx->stashed_ino; 1287 qsort(inodes_to_process, process_inode_count, 1288 sizeof(struct process_inode_block), process_inode_cmp); 1289 clear_problem_context(&pctx); 1290 for (i=0; i < process_inode_count; i++) { 1291 pctx.inode = ctx->stashed_inode = &inodes_to_process[i].inode; 1292 pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino; 1293 1294#if 0 1295 printf("%u ", pctx.ino); 1296#endif 1297 sprintf(buf, _("reading indirect blocks of inode %u"), 1298 pctx.ino); 1299 ehandler_operation(buf); 1300 check_blocks(ctx, &pctx, block_buf); 1301 if (ctx->flags & E2F_FLAG_SIGNAL_MASK) 1302 break; 1303 } 1304 ctx->stashed_inode = old_stashed_inode; 1305 ctx->stashed_ino = old_stashed_ino; 1306 process_inode_count = 0; 1307#if 0 1308 printf("end process inodes\n"); 1309#endif 1310 ehandler_operation(old_operation); 1311} 1312 1313static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b) 1314{ 1315 const struct process_inode_block *ib_a = 1316 (const struct process_inode_block *) a; 1317 const struct process_inode_block *ib_b = 1318 (const struct process_inode_block *) b; 1319 int ret; 1320 1321 ret = (ib_a->inode.i_block[EXT2_IND_BLOCK] - 1322 ib_b->inode.i_block[EXT2_IND_BLOCK]); 1323 if (ret == 0) 1324 /* 1325 * We only call process_inodes() for non-extent 1326 * inodes, so it's OK to pass NULL to 1327 * ext2fs_file_acl_block() here. 1328 */ 1329 ret = ext2fs_file_acl_block(0, &(ib_a->inode)) - 1330 ext2fs_file_acl_block(0, &(ib_b->inode)); 1331 if (ret == 0) 1332 ret = ib_a->ino - ib_b->ino; 1333 return ret; 1334} 1335 1336/* 1337 * Mark an inode as being bad in some what 1338 */ 1339static void mark_inode_bad(e2fsck_t ctx, ino_t ino) 1340{ 1341 struct problem_context pctx; 1342 1343 if (!ctx->inode_bad_map) { 1344 clear_problem_context(&pctx); 1345 1346 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs, 1347 _("bad inode map"), EXT2FS_BMAP64_RBTREE, 1348 "inode_bad_map", &ctx->inode_bad_map); 1349 if (pctx.errcode) { 1350 pctx.num = 3; 1351 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx); 1352 /* Should never get here */ 1353 ctx->flags |= E2F_FLAG_ABORT; 1354 return; 1355 } 1356 } 1357 ext2fs_mark_inode_bitmap2(ctx->inode_bad_map, ino); 1358} 1359 1360 1361/* 1362 * This procedure will allocate the inode "bb" (badblock) map table 1363 */ 1364static void alloc_bb_map(e2fsck_t ctx) 1365{ 1366 struct problem_context pctx; 1367 1368 clear_problem_context(&pctx); 1369 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs, 1370 _("inode in bad block map"), EXT2FS_BMAP64_RBTREE, 1371 "inode_bb_map", &ctx->inode_bb_map); 1372 if (pctx.errcode) { 1373 pctx.num = 4; 1374 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx); 1375 /* Should never get here */ 1376 ctx->flags |= E2F_FLAG_ABORT; 1377 return; 1378 } 1379} 1380 1381/* 1382 * This procedure will allocate the inode imagic table 1383 */ 1384static void alloc_imagic_map(e2fsck_t ctx) 1385{ 1386 struct problem_context pctx; 1387 1388 clear_problem_context(&pctx); 1389 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs, 1390 _("imagic inode map"), EXT2FS_BMAP64_RBTREE, 1391 "inode_imagic_map", &ctx->inode_imagic_map); 1392 if (pctx.errcode) { 1393 pctx.num = 5; 1394 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx); 1395 /* Should never get here */ 1396 ctx->flags |= E2F_FLAG_ABORT; 1397 return; 1398 } 1399} 1400 1401/* 1402 * Marks a block as in use, setting the dup_map if it's been set 1403 * already. Called by process_block and process_bad_block. 1404 * 1405 * WARNING: Assumes checks have already been done to make sure block 1406 * is valid. This is true in both process_block and process_bad_block. 1407 */ 1408static _INLINE_ void mark_block_used(e2fsck_t ctx, blk64_t block) 1409{ 1410 struct problem_context pctx; 1411 1412 clear_problem_context(&pctx); 1413 1414 if (ext2fs_fast_test_block_bitmap2(ctx->block_found_map, block)) { 1415 if (!ctx->block_dup_map) { 1416 pctx.errcode = e2fsck_allocate_block_bitmap(ctx->fs, 1417 _("multiply claimed block map"), 1418 EXT2FS_BMAP64_RBTREE, "block_dup_map", 1419 &ctx->block_dup_map); 1420 if (pctx.errcode) { 1421 pctx.num = 3; 1422 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, 1423 &pctx); 1424 /* Should never get here */ 1425 ctx->flags |= E2F_FLAG_ABORT; 1426 return; 1427 } 1428 } 1429 ext2fs_fast_mark_block_bitmap2(ctx->block_dup_map, block); 1430 } else { 1431 ext2fs_fast_mark_block_bitmap2(ctx->block_found_map, block); 1432 } 1433} 1434 1435/* 1436 * Adjust the extended attribute block's reference counts at the end 1437 * of pass 1, either by subtracting out references for EA blocks that 1438 * are still referenced in ctx->refcount, or by adding references for 1439 * EA blocks that had extra references as accounted for in 1440 * ctx->refcount_extra. 1441 */ 1442static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount, 1443 char *block_buf, int adjust_sign) 1444{ 1445 struct ext2_ext_attr_header *header; 1446 struct problem_context pctx; 1447 ext2_filsys fs = ctx->fs; 1448 blk64_t blk; 1449 __u32 should_be; 1450 int count; 1451 1452 clear_problem_context(&pctx); 1453 1454 ea_refcount_intr_begin(refcount); 1455 while (1) { 1456 if ((blk = ea_refcount_intr_next(refcount, &count)) == 0) 1457 break; 1458 pctx.blk = blk; 1459 pctx.errcode = ext2fs_read_ext_attr2(fs, blk, block_buf); 1460 if (pctx.errcode) { 1461 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx); 1462 return; 1463 } 1464 header = (struct ext2_ext_attr_header *) block_buf; 1465 pctx.blkcount = header->h_refcount; 1466 should_be = header->h_refcount + adjust_sign * count; 1467 pctx.num = should_be; 1468 if (fix_problem(ctx, PR_1_EXTATTR_REFCOUNT, &pctx)) { 1469 header->h_refcount = should_be; 1470 pctx.errcode = ext2fs_write_ext_attr2(fs, blk, 1471 block_buf); 1472 if (pctx.errcode) { 1473 fix_problem(ctx, PR_1_EXTATTR_WRITE_ABORT, 1474 &pctx); 1475 continue; 1476 } 1477 } 1478 } 1479} 1480 1481/* 1482 * Handle processing the extended attribute blocks 1483 */ 1484static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx, 1485 char *block_buf) 1486{ 1487 ext2_filsys fs = ctx->fs; 1488 ext2_ino_t ino = pctx->ino; 1489 struct ext2_inode *inode = pctx->inode; 1490 blk64_t blk; 1491 char * end; 1492 struct ext2_ext_attr_header *header; 1493 struct ext2_ext_attr_entry *entry; 1494 int count; 1495 region_t region = 0; 1496 1497 blk = ext2fs_file_acl_block(fs, inode); 1498 if (blk == 0) 1499 return 0; 1500 1501 /* 1502 * If the Extended attribute flag isn't set, then a non-zero 1503 * file acl means that the inode is corrupted. 1504 * 1505 * Or if the extended attribute block is an invalid block, 1506 * then the inode is also corrupted. 1507 */ 1508 if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR) || 1509 (blk < fs->super->s_first_data_block) || 1510 (blk >= ext2fs_blocks_count(fs->super))) { 1511 mark_inode_bad(ctx, ino); 1512 return 0; 1513 } 1514 1515 /* If ea bitmap hasn't been allocated, create it */ 1516 if (!ctx->block_ea_map) { 1517 pctx->errcode = e2fsck_allocate_block_bitmap(fs, 1518 _("ext attr block map"), 1519 EXT2FS_BMAP64_RBTREE, "block_ea_map", 1520 &ctx->block_ea_map); 1521 if (pctx->errcode) { 1522 pctx->num = 2; 1523 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, pctx); 1524 ctx->flags |= E2F_FLAG_ABORT; 1525 return 0; 1526 } 1527 } 1528 1529 /* Create the EA refcount structure if necessary */ 1530 if (!ctx->refcount) { 1531 pctx->errcode = ea_refcount_create(0, &ctx->refcount); 1532 if (pctx->errcode) { 1533 pctx->num = 1; 1534 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx); 1535 ctx->flags |= E2F_FLAG_ABORT; 1536 return 0; 1537 } 1538 } 1539 1540#if 0 1541 /* Debugging text */ 1542 printf("Inode %u has EA block %u\n", ino, blk); 1543#endif 1544 1545 /* Have we seen this EA block before? */ 1546 if (ext2fs_fast_test_block_bitmap2(ctx->block_ea_map, blk)) { 1547 if (ea_refcount_decrement(ctx->refcount, blk, 0) == 0) 1548 return 1; 1549 /* Ooops, this EA was referenced more than it stated */ 1550 if (!ctx->refcount_extra) { 1551 pctx->errcode = ea_refcount_create(0, 1552 &ctx->refcount_extra); 1553 if (pctx->errcode) { 1554 pctx->num = 2; 1555 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx); 1556 ctx->flags |= E2F_FLAG_ABORT; 1557 return 0; 1558 } 1559 } 1560 ea_refcount_increment(ctx->refcount_extra, blk, 0); 1561 return 1; 1562 } 1563 1564 /* 1565 * OK, we haven't seen this EA block yet. So we need to 1566 * validate it 1567 */ 1568 pctx->blk = blk; 1569 pctx->errcode = ext2fs_read_ext_attr2(fs, blk, block_buf); 1570 if (pctx->errcode && fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx)) 1571 goto clear_extattr; 1572 header = (struct ext2_ext_attr_header *) block_buf; 1573 pctx->blk = ext2fs_file_acl_block(fs, inode); 1574 if (((ctx->ext_attr_ver == 1) && 1575 (header->h_magic != EXT2_EXT_ATTR_MAGIC_v1)) || 1576 ((ctx->ext_attr_ver == 2) && 1577 (header->h_magic != EXT2_EXT_ATTR_MAGIC))) { 1578 if (fix_problem(ctx, PR_1_BAD_EA_BLOCK, pctx)) 1579 goto clear_extattr; 1580 } 1581 1582 if (header->h_blocks != 1) { 1583 if (fix_problem(ctx, PR_1_EA_MULTI_BLOCK, pctx)) 1584 goto clear_extattr; 1585 } 1586 1587 region = region_create(0, fs->blocksize); 1588 if (!region) { 1589 fix_problem(ctx, PR_1_EA_ALLOC_REGION_ABORT, pctx); 1590 ctx->flags |= E2F_FLAG_ABORT; 1591 return 0; 1592 } 1593 if (region_allocate(region, 0, sizeof(struct ext2_ext_attr_header))) { 1594 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx)) 1595 goto clear_extattr; 1596 } 1597 1598 entry = (struct ext2_ext_attr_entry *)(header+1); 1599 end = block_buf + fs->blocksize; 1600 while ((char *)entry < end && *(__u32 *)entry) { 1601 __u32 hash; 1602 1603 if (region_allocate(region, (char *)entry - (char *)header, 1604 EXT2_EXT_ATTR_LEN(entry->e_name_len))) { 1605 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx)) 1606 goto clear_extattr; 1607 break; 1608 } 1609 if ((ctx->ext_attr_ver == 1 && 1610 (entry->e_name_len == 0 || entry->e_name_index != 0)) || 1611 (ctx->ext_attr_ver == 2 && 1612 entry->e_name_index == 0)) { 1613 if (fix_problem(ctx, PR_1_EA_BAD_NAME, pctx)) 1614 goto clear_extattr; 1615 break; 1616 } 1617 if (entry->e_value_block != 0) { 1618 if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx)) 1619 goto clear_extattr; 1620 } 1621 if (entry->e_value_offs + entry->e_value_size > fs->blocksize) { 1622 if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx)) 1623 goto clear_extattr; 1624 break; 1625 } 1626 if (entry->e_value_size && 1627 region_allocate(region, entry->e_value_offs, 1628 EXT2_EXT_ATTR_SIZE(entry->e_value_size))) { 1629 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx)) 1630 goto clear_extattr; 1631 } 1632 1633 hash = ext2fs_ext_attr_hash_entry(entry, block_buf + 1634 entry->e_value_offs); 1635 1636 if (entry->e_hash != hash) { 1637 pctx->num = entry->e_hash; 1638 if (fix_problem(ctx, PR_1_ATTR_HASH, pctx)) 1639 goto clear_extattr; 1640 entry->e_hash = hash; 1641 } 1642 1643 entry = EXT2_EXT_ATTR_NEXT(entry); 1644 } 1645 if (region_allocate(region, (char *)entry - (char *)header, 4)) { 1646 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx)) 1647 goto clear_extattr; 1648 } 1649 region_free(region); 1650 1651 count = header->h_refcount - 1; 1652 if (count) 1653 ea_refcount_store(ctx->refcount, blk, count); 1654 mark_block_used(ctx, blk); 1655 ext2fs_fast_mark_block_bitmap2(ctx->block_ea_map, blk); 1656 return 1; 1657 1658clear_extattr: 1659 if (region) 1660 region_free(region); 1661 ext2fs_file_acl_block_set(fs, inode, 0); 1662 e2fsck_write_inode(ctx, ino, inode, "check_ext_attr"); 1663 return 0; 1664} 1665 1666/* Returns 1 if bad htree, 0 if OK */ 1667static int handle_htree(e2fsck_t ctx, struct problem_context *pctx, 1668 ext2_ino_t ino, struct ext2_inode *inode, 1669 char *block_buf) 1670{ 1671 struct ext2_dx_root_info *root; 1672 ext2_filsys fs = ctx->fs; 1673 errcode_t retval; 1674 blk64_t blk; 1675 1676 if ((!LINUX_S_ISDIR(inode->i_mode) && 1677 fix_problem(ctx, PR_1_HTREE_NODIR, pctx)) || 1678 (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) && 1679 fix_problem(ctx, PR_1_HTREE_SET, pctx))) 1680 return 1; 1681 1682 pctx->errcode = ext2fs_bmap2(fs, ino, inode, 0, 0, 0, 0, &blk); 1683 1684 if ((pctx->errcode) || 1685 (blk == 0) || 1686 (blk < fs->super->s_first_data_block) || 1687 (blk >= ext2fs_blocks_count(fs->super))) { 1688 if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx)) 1689 return 1; 1690 else 1691 return 0; 1692 } 1693 1694 retval = io_channel_read_blk64(fs->io, blk, 1, block_buf); 1695 if (retval && fix_problem(ctx, PR_1_HTREE_BADROOT, pctx)) 1696 return 1; 1697 1698 /* XXX should check that beginning matches a directory */ 1699 root = (struct ext2_dx_root_info *) (block_buf + 24); 1700 1701 if ((root->reserved_zero || root->info_length < 8) && 1702 fix_problem(ctx, PR_1_HTREE_BADROOT, pctx)) 1703 return 1; 1704 1705 pctx->num = root->hash_version; 1706 if ((root->hash_version != EXT2_HASH_LEGACY) && 1707 (root->hash_version != EXT2_HASH_HALF_MD4) && 1708 (root->hash_version != EXT2_HASH_TEA) && 1709 fix_problem(ctx, PR_1_HTREE_HASHV, pctx)) 1710 return 1; 1711 1712 if ((root->unused_flags & EXT2_HASH_FLAG_INCOMPAT) && 1713 fix_problem(ctx, PR_1_HTREE_INCOMPAT, pctx)) 1714 return 1; 1715 1716 pctx->num = root->indirect_levels; 1717 if ((root->indirect_levels > 1) && 1718 fix_problem(ctx, PR_1_HTREE_DEPTH, pctx)) 1719 return 1; 1720 1721 return 0; 1722} 1723 1724void e2fsck_clear_inode(e2fsck_t ctx, ext2_ino_t ino, 1725 struct ext2_inode *inode, int restart_flag, 1726 const char *source) 1727{ 1728 inode->i_flags = 0; 1729 inode->i_links_count = 0; 1730 ext2fs_icount_store(ctx->inode_link_info, ino, 0); 1731 inode->i_dtime = ctx->now; 1732 1733 ext2fs_unmark_inode_bitmap2(ctx->inode_dir_map, ino); 1734 ext2fs_unmark_inode_bitmap2(ctx->inode_used_map, ino); 1735 if (ctx->inode_reg_map) 1736 ext2fs_unmark_inode_bitmap2(ctx->inode_reg_map, ino); 1737 if (ctx->inode_bad_map) 1738 ext2fs_unmark_inode_bitmap2(ctx->inode_bad_map, ino); 1739 1740 /* 1741 * If the inode was partially accounted for before processing 1742 * was aborted, we need to restart the pass 1 scan. 1743 */ 1744 ctx->flags |= restart_flag; 1745 1746 if (ino == EXT2_BAD_INO) 1747 memset(inode, 0, sizeof(struct ext2_inode)); 1748 1749 e2fsck_write_inode(ctx, ino, inode, source); 1750} 1751 1752static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx, 1753 struct process_block_struct *pb, 1754 blk64_t start_block, 1755 ext2_extent_handle_t ehandle) 1756{ 1757 struct ext2fs_extent extent; 1758 blk64_t blk; 1759 e2_blkcnt_t blockcnt; 1760 unsigned int i; 1761 int is_dir, is_leaf; 1762 errcode_t problem; 1763 struct ext2_extent_info info; 1764 1765 pctx->errcode = ext2fs_extent_get_info(ehandle, &info); 1766 if (pctx->errcode) 1767 return; 1768 1769 pctx->errcode = ext2fs_extent_get(ehandle, EXT2_EXTENT_FIRST_SIB, 1770 &extent); 1771 while (!pctx->errcode && info.num_entries-- > 0) { 1772 is_leaf = extent.e_flags & EXT2_EXTENT_FLAGS_LEAF; 1773 is_dir = LINUX_S_ISDIR(pctx->inode->i_mode); 1774 1775 problem = 0; 1776 if (extent.e_pblk == 0 || 1777 extent.e_pblk < ctx->fs->super->s_first_data_block || 1778 extent.e_pblk >= ext2fs_blocks_count(ctx->fs->super)) 1779 problem = PR_1_EXTENT_BAD_START_BLK; 1780 else if (extent.e_lblk < start_block) 1781 problem = PR_1_OUT_OF_ORDER_EXTENTS; 1782 else if (is_leaf && extent.e_len == 0) 1783 problem = PR_1_EXTENT_LENGTH_ZERO; 1784 else if (is_leaf && 1785 (extent.e_pblk + extent.e_len) > 1786 ext2fs_blocks_count(ctx->fs->super)) 1787 problem = PR_1_EXTENT_ENDS_BEYOND; 1788 1789 if (problem) { 1790 report_problem: 1791 pctx->blk = extent.e_pblk; 1792 pctx->blk2 = extent.e_lblk; 1793 pctx->num = extent.e_len; 1794 if (fix_problem(ctx, problem, pctx)) { 1795 e2fsck_read_bitmaps(ctx); 1796 pctx->errcode = 1797 ext2fs_extent_delete(ehandle, 0); 1798 if (pctx->errcode) { 1799 pctx->str = "ext2fs_extent_delete"; 1800 return; 1801 } 1802 pctx->errcode = ext2fs_extent_get(ehandle, 1803 EXT2_EXTENT_CURRENT, 1804 &extent); 1805 if (pctx->errcode == EXT2_ET_NO_CURRENT_NODE) { 1806 pctx->errcode = 0; 1807 break; 1808 } 1809 continue; 1810 } 1811 goto next; 1812 } 1813 1814 if (!is_leaf) { 1815 blk = extent.e_pblk; 1816 pctx->errcode = ext2fs_extent_get(ehandle, 1817 EXT2_EXTENT_DOWN, &extent); 1818 if (pctx->errcode) { 1819 pctx->str = "EXT2_EXTENT_DOWN"; 1820 problem = PR_1_EXTENT_HEADER_INVALID; 1821 if (pctx->errcode == EXT2_ET_EXTENT_HEADER_BAD) 1822 goto report_problem; 1823 return; 1824 } 1825 scan_extent_node(ctx, pctx, pb, extent.e_lblk, ehandle); 1826 if (pctx->errcode) 1827 return; 1828 pctx->errcode = ext2fs_extent_get(ehandle, 1829 EXT2_EXTENT_UP, &extent); 1830 if (pctx->errcode) { 1831 pctx->str = "EXT2_EXTENT_UP"; 1832 return; 1833 } 1834 mark_block_used(ctx, blk); 1835 pb->num_blocks++; 1836 goto next; 1837 } 1838 1839 if ((pb->previous_block != 0) && 1840 (pb->previous_block+1 != extent.e_pblk)) { 1841 if (ctx->options & E2F_OPT_FRAGCHECK) { 1842 char type = '?'; 1843 1844 if (pb->is_dir) 1845 type = 'd'; 1846 else if (pb->is_reg) 1847 type = 'f'; 1848 1849 printf(("%6lu(%c): expecting %6lu " 1850 "actual extent " 1851 "phys %6lu log %lu len %lu\n"), 1852 (unsigned long) pctx->ino, type, 1853 (unsigned long) pb->previous_block+1, 1854 (unsigned long) extent.e_pblk, 1855 (unsigned long) extent.e_lblk, 1856 (unsigned long) extent.e_len); 1857 } 1858 pb->fragmented = 1; 1859 } 1860 while (is_dir && ++pb->last_db_block < extent.e_lblk) { 1861 pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, 1862 pb->ino, 0, 1863 pb->last_db_block); 1864 if (pctx->errcode) { 1865 pctx->blk = 0; 1866 pctx->num = pb->last_db_block; 1867 goto failed_add_dir_block; 1868 } 1869 } 1870 for (blk = extent.e_pblk, blockcnt = extent.e_lblk, i = 0; 1871 i < extent.e_len; 1872 blk++, blockcnt++, i++) { 1873 if (!(ctx->fs->cluster_ratio_bits && 1874 pb->previous_block && 1875 (EXT2FS_B2C(ctx->fs, blk) == 1876 EXT2FS_B2C(ctx->fs, pb->previous_block)) && 1877 (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) == 1878 (blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) { 1879 mark_block_used(ctx, blk); 1880 pb->num_blocks++; 1881 } 1882 1883 pb->previous_block = blk; 1884 1885 if (is_dir) { 1886 pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, pctx->ino, blk, blockcnt); 1887 if (pctx->errcode) { 1888 pctx->blk = blk; 1889 pctx->num = blockcnt; 1890 failed_add_dir_block: 1891 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx); 1892 /* Should never get here */ 1893 ctx->flags |= E2F_FLAG_ABORT; 1894 return; 1895 } 1896 } 1897 } 1898 if (is_dir && extent.e_len > 0) 1899 pb->last_db_block = blockcnt - 1; 1900 pb->previous_block = extent.e_pblk + extent.e_len - 1; 1901 start_block = pb->last_block = extent.e_lblk + extent.e_len - 1; 1902 if (is_leaf && !is_dir && 1903 !(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT)) 1904 pb->last_init_lblock = extent.e_lblk + extent.e_len - 1; 1905 next: 1906 pctx->errcode = ext2fs_extent_get(ehandle, 1907 EXT2_EXTENT_NEXT_SIB, 1908 &extent); 1909 } 1910 if (pctx->errcode == EXT2_ET_EXTENT_NO_NEXT) 1911 pctx->errcode = 0; 1912} 1913 1914static void check_blocks_extents(e2fsck_t ctx, struct problem_context *pctx, 1915 struct process_block_struct *pb) 1916{ 1917 struct ext2_extent_info info; 1918 struct ext2_inode *inode = pctx->inode; 1919 ext2_extent_handle_t ehandle; 1920 ext2_filsys fs = ctx->fs; 1921 ext2_ino_t ino = pctx->ino; 1922 errcode_t retval; 1923 1924 pctx->errcode = ext2fs_extent_open2(fs, ino, inode, &ehandle); 1925 if (pctx->errcode) { 1926 if (fix_problem(ctx, PR_1_READ_EXTENT, pctx)) 1927 e2fsck_clear_inode(ctx, ino, inode, 0, 1928 "check_blocks_extents"); 1929 pctx->errcode = 0; 1930 return; 1931 } 1932 1933 retval = ext2fs_extent_get_info(ehandle, &info); 1934 if (retval == 0) { 1935 if (info.max_depth >= MAX_EXTENT_DEPTH_COUNT) 1936 info.max_depth = MAX_EXTENT_DEPTH_COUNT-1; 1937 ctx->extent_depth_count[info.max_depth]++; 1938 } 1939 1940 scan_extent_node(ctx, pctx, pb, 0, ehandle); 1941 if (pctx->errcode && 1942 fix_problem(ctx, PR_1_EXTENT_ITERATE_FAILURE, pctx)) { 1943 pb->num_blocks = 0; 1944 inode->i_blocks = 0; 1945 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART, 1946 "check_blocks_extents"); 1947 pctx->errcode = 0; 1948 } 1949 ext2fs_extent_free(ehandle); 1950} 1951 1952/* 1953 * This subroutine is called on each inode to account for all of the 1954 * blocks used by that inode. 1955 */ 1956static void check_blocks(e2fsck_t ctx, struct problem_context *pctx, 1957 char *block_buf) 1958{ 1959 ext2_filsys fs = ctx->fs; 1960 struct process_block_struct pb; 1961 ext2_ino_t ino = pctx->ino; 1962 struct ext2_inode *inode = pctx->inode; 1963 int bad_size = 0; 1964 int dirty_inode = 0; 1965 int extent_fs; 1966 __u64 size; 1967 1968 pb.ino = ino; 1969 pb.num_blocks = 0; 1970 pb.last_block = -1; 1971 pb.last_init_lblock = -1; 1972 pb.last_db_block = -1; 1973 pb.num_illegal_blocks = 0; 1974 pb.suppress = 0; pb.clear = 0; 1975 pb.fragmented = 0; 1976 pb.compressed = 0; 1977 pb.previous_block = 0; 1978 pb.is_dir = LINUX_S_ISDIR(inode->i_mode); 1979 pb.is_reg = LINUX_S_ISREG(inode->i_mode); 1980 pb.max_blocks = 1 << (31 - fs->super->s_log_block_size); 1981 pb.inode = inode; 1982 pb.pctx = pctx; 1983 pb.ctx = ctx; 1984 pctx->ino = ino; 1985 pctx->errcode = 0; 1986 1987 extent_fs = (ctx->fs->super->s_feature_incompat & 1988 EXT3_FEATURE_INCOMPAT_EXTENTS); 1989 1990 if (inode->i_flags & EXT2_COMPRBLK_FL) { 1991 if (fs->super->s_feature_incompat & 1992 EXT2_FEATURE_INCOMPAT_COMPRESSION) 1993 pb.compressed = 1; 1994 else { 1995 if (fix_problem(ctx, PR_1_COMPR_SET, pctx)) { 1996 inode->i_flags &= ~EXT2_COMPRBLK_FL; 1997 dirty_inode++; 1998 } 1999 } 2000 } 2001 2002 if (ext2fs_file_acl_block(fs, inode) && 2003 check_ext_attr(ctx, pctx, block_buf)) { 2004 if (ctx->flags & E2F_FLAG_SIGNAL_MASK) 2005 goto out; 2006 pb.num_blocks++; 2007 } 2008 2009 if (ext2fs_inode_has_valid_blocks2(fs, inode)) { 2010 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) 2011 check_blocks_extents(ctx, pctx, &pb); 2012 else { 2013 pctx->errcode = ext2fs_block_iterate3(fs, ino, 2014 pb.is_dir ? BLOCK_FLAG_HOLE : 0, 2015 block_buf, process_block, &pb); 2016 /* 2017 * We do not have uninitialized extents in non extent 2018 * files. 2019 */ 2020 pb.last_init_lblock = pb.last_block; 2021 } 2022 } 2023 end_problem_latch(ctx, PR_LATCH_BLOCK); 2024 end_problem_latch(ctx, PR_LATCH_TOOBIG); 2025 if (ctx->flags & E2F_FLAG_SIGNAL_MASK) 2026 goto out; 2027 if (pctx->errcode) 2028 fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx); 2029 2030 if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group) { 2031 if (LINUX_S_ISDIR(inode->i_mode)) 2032 ctx->fs_fragmented_dir++; 2033 else 2034 ctx->fs_fragmented++; 2035 } 2036 2037 if (pb.clear) { 2038 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART, 2039 "check_blocks"); 2040 return; 2041 } 2042 2043 if (inode->i_flags & EXT2_INDEX_FL) { 2044 if (handle_htree(ctx, pctx, ino, inode, block_buf)) { 2045 inode->i_flags &= ~EXT2_INDEX_FL; 2046 dirty_inode++; 2047 } else { 2048#ifdef ENABLE_HTREE 2049 e2fsck_add_dx_dir(ctx, ino, pb.last_block+1); 2050#endif 2051 } 2052 } 2053 2054 if (!pb.num_blocks && pb.is_dir) { 2055 if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) { 2056 e2fsck_clear_inode(ctx, ino, inode, 0, "check_blocks"); 2057 ctx->fs_directory_count--; 2058 return; 2059 } 2060 } 2061 2062 if (ino == EXT2_ROOT_INO || ino >= EXT2_FIRST_INODE(ctx->fs->super)) { 2063 quota_data_add(ctx->qctx, inode, ino, 2064 pb.num_blocks * fs->blocksize); 2065 quota_data_inodes(ctx->qctx, inode, ino, +1); 2066 } 2067 2068 if (!(fs->super->s_feature_ro_compat & 2069 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) || 2070 !(inode->i_flags & EXT4_HUGE_FILE_FL)) 2071 pb.num_blocks *= (fs->blocksize / 512); 2072 pb.num_blocks *= EXT2FS_CLUSTER_RATIO(fs); 2073#if 0 2074 printf("inode %u, i_size = %u, last_block = %lld, i_blocks=%llu, num_blocks = %llu\n", 2075 ino, inode->i_size, pb.last_block, ext2fs_inode_i_blocks(fs, inode), 2076 pb.num_blocks); 2077#endif 2078 if (pb.is_dir) { 2079 int nblock = inode->i_size >> EXT2_BLOCK_SIZE_BITS(fs->super); 2080 if (inode->i_size & (fs->blocksize - 1)) 2081 bad_size = 5; 2082 else if (nblock > (pb.last_block + 1)) 2083 bad_size = 1; 2084 else if (nblock < (pb.last_block + 1)) { 2085 if (((pb.last_block + 1) - nblock) > 2086 fs->super->s_prealloc_dir_blocks) 2087 bad_size = 2; 2088 } 2089 } else { 2090 e2_blkcnt_t blkpg = ctx->blocks_per_page; 2091 2092 size = EXT2_I_SIZE(inode); 2093 if ((pb.last_init_lblock >= 0) && 2094 /* allow allocated blocks to end of PAGE_SIZE */ 2095 (size < (__u64)pb.last_init_lblock * fs->blocksize) && 2096 (pb.last_init_lblock / blkpg * blkpg != pb.last_init_lblock || 2097 size < (__u64)(pb.last_init_lblock & ~(blkpg-1)) * 2098 fs->blocksize)) 2099 bad_size = 3; 2100 else if (!(extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) && 2101 size > ext2_max_sizes[fs->super->s_log_block_size]) 2102 /* too big for a direct/indirect-mapped file */ 2103 bad_size = 4; 2104 else if ((extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) && 2105 size > 2106 ((1ULL << (32 + EXT2_BLOCK_SIZE_BITS(fs->super))) - 1)) 2107 /* too big for an extent-based file - 32bit ee_block */ 2108 bad_size = 6; 2109 } 2110 /* i_size for symlinks is checked elsewhere */ 2111 if (bad_size && !LINUX_S_ISLNK(inode->i_mode)) { 2112 pctx->num = (pb.last_block+1) * fs->blocksize; 2113 pctx->group = bad_size; 2114 if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) { 2115 inode->i_size = pctx->num; 2116 if (!LINUX_S_ISDIR(inode->i_mode)) 2117 inode->i_size_high = pctx->num >> 32; 2118 dirty_inode++; 2119 } 2120 pctx->num = 0; 2121 } 2122 if (LINUX_S_ISREG(inode->i_mode) && EXT2_I_SIZE(inode) >= 0x80000000UL) 2123 ctx->large_files++; 2124 if ((pb.num_blocks != ext2fs_inode_i_blocks(fs, inode)) || 2125 ((fs->super->s_feature_ro_compat & 2126 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) && 2127 (inode->i_flags & EXT4_HUGE_FILE_FL) && 2128 (inode->osd2.linux2.l_i_blocks_hi != 0))) { 2129 pctx->num = pb.num_blocks; 2130 if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) { 2131 inode->i_blocks = pb.num_blocks; 2132 inode->osd2.linux2.l_i_blocks_hi = pb.num_blocks >> 32; 2133 dirty_inode++; 2134 } 2135 pctx->num = 0; 2136 } 2137 2138 if (ctx->dirs_to_hash && pb.is_dir && 2139 !(inode->i_flags & EXT2_INDEX_FL) && 2140 ((inode->i_size / fs->blocksize) >= 3)) 2141 ext2fs_u32_list_add(ctx->dirs_to_hash, ino); 2142 2143out: 2144 if (dirty_inode) 2145 e2fsck_write_inode(ctx, ino, inode, "check_blocks"); 2146} 2147 2148#if 0 2149/* 2150 * Helper function called by process block when an illegal block is 2151 * found. It returns a description about why the block is illegal 2152 */ 2153static char *describe_illegal_block(ext2_filsys fs, blk64_t block) 2154{ 2155 blk64_t super; 2156 int i; 2157 static char problem[80]; 2158 2159 super = fs->super->s_first_data_block; 2160 strcpy(problem, "PROGRAMMING ERROR: Unknown reason for illegal block"); 2161 if (block < super) { 2162 sprintf(problem, "< FIRSTBLOCK (%u)", super); 2163 return(problem); 2164 } else if (block >= ext2fs_blocks_count(fs->super)) { 2165 sprintf(problem, "> BLOCKS (%u)", ext2fs_blocks_count(fs->super)); 2166 return(problem); 2167 } 2168 for (i = 0; i < fs->group_desc_count; i++) { 2169 if (block == super) { 2170 sprintf(problem, "is the superblock in group %d", i); 2171 break; 2172 } 2173 if (block > super && 2174 block <= (super + fs->desc_blocks)) { 2175 sprintf(problem, "is in the group descriptors " 2176 "of group %d", i); 2177 break; 2178 } 2179 if (block == ext2fs_block_bitmap_loc(fs, i)) { 2180 sprintf(problem, "is the block bitmap of group %d", i); 2181 break; 2182 } 2183 if (block == ext2fs_inode_bitmap_loc(fs, i)) { 2184 sprintf(problem, "is the inode bitmap of group %d", i); 2185 break; 2186 } 2187 if (block >= ext2fs_inode_table_loc(fs, i) && 2188 (block < ext2fs_inode_table_loc(fs, i) 2189 + fs->inode_blocks_per_group)) { 2190 sprintf(problem, "is in the inode table of group %d", 2191 i); 2192 break; 2193 } 2194 super += fs->super->s_blocks_per_group; 2195 } 2196 return(problem); 2197} 2198#endif 2199 2200/* 2201 * This is a helper function for check_blocks(). 2202 */ 2203static int process_block(ext2_filsys fs, 2204 blk64_t *block_nr, 2205 e2_blkcnt_t blockcnt, 2206 blk64_t ref_block EXT2FS_ATTR((unused)), 2207 int ref_offset EXT2FS_ATTR((unused)), 2208 void *priv_data) 2209{ 2210 struct process_block_struct *p; 2211 struct problem_context *pctx; 2212 blk64_t blk = *block_nr; 2213 int ret_code = 0; 2214 int problem = 0; 2215 e2fsck_t ctx; 2216 2217 p = (struct process_block_struct *) priv_data; 2218 pctx = p->pctx; 2219 ctx = p->ctx; 2220 2221 if (p->compressed && (blk == EXT2FS_COMPRESSED_BLKADDR)) { 2222 /* todo: Check that the comprblk_fl is high, that the 2223 blkaddr pattern looks right (all non-holes up to 2224 first EXT2FS_COMPRESSED_BLKADDR, then all 2225 EXT2FS_COMPRESSED_BLKADDR up to end of cluster), 2226 that the feature_incompat bit is high, and that the 2227 inode is a regular file. If we're doing a "full 2228 check" (a concept introduced to e2fsck by e2compr, 2229 meaning that we look at data blocks as well as 2230 metadata) then call some library routine that 2231 checks the compressed data. I'll have to think 2232 about this, because one particularly important 2233 problem to be able to fix is to recalculate the 2234 cluster size if necessary. I think that perhaps 2235 we'd better do most/all e2compr-specific checks 2236 separately, after the non-e2compr checks. If not 2237 doing a full check, it may be useful to test that 2238 the personality is linux; e.g. if it isn't then 2239 perhaps this really is just an illegal block. */ 2240 return 0; 2241 } 2242 2243 if (blk == 0) 2244 return 0; 2245 2246#if 0 2247 printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk, 2248 blockcnt); 2249#endif 2250 2251 /* 2252 * Simplistic fragmentation check. We merely require that the 2253 * file be contiguous. (Which can never be true for really 2254 * big files that are greater than a block group.) 2255 */ 2256 if (!HOLE_BLKADDR(p->previous_block) && p->ino != EXT2_RESIZE_INO) { 2257 if (p->previous_block+1 != blk) { 2258 if (ctx->options & E2F_OPT_FRAGCHECK) { 2259 char type = '?'; 2260 2261 if (p->is_dir) 2262 type = 'd'; 2263 else if (p->is_reg) 2264 type = 'f'; 2265 2266 printf(_("%6lu(%c): expecting %6lu " 2267 "got phys %6lu (blkcnt %lld)\n"), 2268 (unsigned long) pctx->ino, type, 2269 (unsigned long) p->previous_block+1, 2270 (unsigned long) blk, 2271 blockcnt); 2272 } 2273 p->fragmented = 1; 2274 } 2275 } 2276 2277 if (p->is_dir && blockcnt > (1 << (21 - fs->super->s_log_block_size))) 2278 problem = PR_1_TOOBIG_DIR; 2279 if (p->is_reg && p->num_blocks+1 >= p->max_blocks) 2280 problem = PR_1_TOOBIG_REG; 2281 if (!p->is_dir && !p->is_reg && blockcnt > 0) 2282 problem = PR_1_TOOBIG_SYMLINK; 2283 2284 if (blk < fs->super->s_first_data_block || 2285 blk >= ext2fs_blocks_count(fs->super)) 2286 problem = PR_1_ILLEGAL_BLOCK_NUM; 2287 2288 if (problem) { 2289 p->num_illegal_blocks++; 2290 if (!p->suppress && (p->num_illegal_blocks % 12) == 0) { 2291 if (fix_problem(ctx, PR_1_TOO_MANY_BAD_BLOCKS, pctx)) { 2292 p->clear = 1; 2293 return BLOCK_ABORT; 2294 } 2295 if (fix_problem(ctx, PR_1_SUPPRESS_MESSAGES, pctx)) { 2296 p->suppress = 1; 2297 set_latch_flags(PR_LATCH_BLOCK, 2298 PRL_SUPPRESS, 0); 2299 } 2300 } 2301 pctx->blk = blk; 2302 pctx->blkcount = blockcnt; 2303 if (fix_problem(ctx, problem, pctx)) { 2304 blk = *block_nr = 0; 2305 ret_code = BLOCK_CHANGED; 2306 goto mark_dir; 2307 } else 2308 return 0; 2309 } 2310 2311 if (p->ino == EXT2_RESIZE_INO) { 2312 /* 2313 * The resize inode has already be sanity checked 2314 * during pass #0 (the superblock checks). All we 2315 * have to do is mark the double indirect block as 2316 * being in use; all of the other blocks are handled 2317 * by mark_table_blocks()). 2318 */ 2319 if (blockcnt == BLOCK_COUNT_DIND) 2320 mark_block_used(ctx, blk); 2321 p->num_blocks++; 2322 } else if (!(ctx->fs->cluster_ratio_bits && 2323 p->previous_block && 2324 (EXT2FS_B2C(ctx->fs, blk) == 2325 EXT2FS_B2C(ctx->fs, p->previous_block)) && 2326 (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) == 2327 (blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) { 2328 mark_block_used(ctx, blk); 2329 p->num_blocks++; 2330 } 2331 if (blockcnt >= 0) 2332 p->last_block = blockcnt; 2333 p->previous_block = blk; 2334mark_dir: 2335 if (p->is_dir && (blockcnt >= 0)) { 2336 while (++p->last_db_block < blockcnt) { 2337 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, 2338 p->ino, 0, 2339 p->last_db_block); 2340 if (pctx->errcode) { 2341 pctx->blk = 0; 2342 pctx->num = p->last_db_block; 2343 goto failed_add_dir_block; 2344 } 2345 } 2346 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino, 2347 blk, blockcnt); 2348 if (pctx->errcode) { 2349 pctx->blk = blk; 2350 pctx->num = blockcnt; 2351 failed_add_dir_block: 2352 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx); 2353 /* Should never get here */ 2354 ctx->flags |= E2F_FLAG_ABORT; 2355 return BLOCK_ABORT; 2356 } 2357 } 2358 return ret_code; 2359} 2360 2361static int process_bad_block(ext2_filsys fs, 2362 blk64_t *block_nr, 2363 e2_blkcnt_t blockcnt, 2364 blk64_t ref_block EXT2FS_ATTR((unused)), 2365 int ref_offset EXT2FS_ATTR((unused)), 2366 void *priv_data) 2367{ 2368 struct process_block_struct *p; 2369 blk64_t blk = *block_nr; 2370 blk64_t first_block; 2371 dgrp_t i; 2372 struct problem_context *pctx; 2373 e2fsck_t ctx; 2374 2375 /* 2376 * Note: This function processes blocks for the bad blocks 2377 * inode, which is never compressed. So we don't use HOLE_BLKADDR(). 2378 */ 2379 2380 if (!blk) 2381 return 0; 2382 2383 p = (struct process_block_struct *) priv_data; 2384 ctx = p->ctx; 2385 pctx = p->pctx; 2386 2387 pctx->ino = EXT2_BAD_INO; 2388 pctx->blk = blk; 2389 pctx->blkcount = blockcnt; 2390 2391 if ((blk < fs->super->s_first_data_block) || 2392 (blk >= ext2fs_blocks_count(fs->super))) { 2393 if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) { 2394 *block_nr = 0; 2395 return BLOCK_CHANGED; 2396 } else 2397 return 0; 2398 } 2399 2400 if (blockcnt < 0) { 2401 if (ext2fs_test_block_bitmap2(p->fs_meta_blocks, blk)) { 2402 p->bbcheck = 1; 2403 if (fix_problem(ctx, PR_1_BB_FS_BLOCK, pctx)) { 2404 *block_nr = 0; 2405 return BLOCK_CHANGED; 2406 } 2407 } else if (ext2fs_test_block_bitmap2(ctx->block_found_map, 2408 blk)) { 2409 p->bbcheck = 1; 2410 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, 2411 pctx)) { 2412 *block_nr = 0; 2413 return BLOCK_CHANGED; 2414 } 2415 if (ctx->flags & E2F_FLAG_SIGNAL_MASK) 2416 return BLOCK_ABORT; 2417 } else 2418 mark_block_used(ctx, blk); 2419 return 0; 2420 } 2421#if 0 2422 printf ("DEBUG: Marking %u as bad.\n", blk); 2423#endif 2424 ctx->fs_badblocks_count++; 2425 /* 2426 * If the block is not used, then mark it as used and return. 2427 * If it is already marked as found, this must mean that 2428 * there's an overlap between the filesystem table blocks 2429 * (bitmaps and inode table) and the bad block list. 2430 */ 2431 if (!ext2fs_test_block_bitmap2(ctx->block_found_map, blk)) { 2432 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk); 2433 return 0; 2434 } 2435 /* 2436 * Try to find the where the filesystem block was used... 2437 */ 2438 first_block = fs->super->s_first_data_block; 2439 2440 for (i = 0; i < fs->group_desc_count; i++ ) { 2441 pctx->group = i; 2442 pctx->blk = blk; 2443 if (!ext2fs_bg_has_super(fs, i)) 2444 goto skip_super; 2445 if (blk == first_block) { 2446 if (i == 0) { 2447 if (fix_problem(ctx, 2448 PR_1_BAD_PRIMARY_SUPERBLOCK, 2449 pctx)) { 2450 *block_nr = 0; 2451 return BLOCK_CHANGED; 2452 } 2453 return 0; 2454 } 2455 fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx); 2456 return 0; 2457 } 2458 if ((blk > first_block) && 2459 (blk <= first_block + fs->desc_blocks)) { 2460 if (i == 0) { 2461 pctx->blk = *block_nr; 2462 if (fix_problem(ctx, 2463 PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) { 2464 *block_nr = 0; 2465 return BLOCK_CHANGED; 2466 } 2467 return 0; 2468 } 2469 fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx); 2470 return 0; 2471 } 2472 skip_super: 2473 if (blk == ext2fs_block_bitmap_loc(fs, i)) { 2474 if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) { 2475 ctx->invalid_block_bitmap_flag[i]++; 2476 ctx->invalid_bitmaps++; 2477 } 2478 return 0; 2479 } 2480 if (blk == ext2fs_inode_bitmap_loc(fs, i)) { 2481 if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) { 2482 ctx->invalid_inode_bitmap_flag[i]++; 2483 ctx->invalid_bitmaps++; 2484 } 2485 return 0; 2486 } 2487 if ((blk >= ext2fs_inode_table_loc(fs, i)) && 2488 (blk < (ext2fs_inode_table_loc(fs, i) + 2489 fs->inode_blocks_per_group))) { 2490 /* 2491 * If there are bad blocks in the inode table, 2492 * the inode scan code will try to do 2493 * something reasonable automatically. 2494 */ 2495 return 0; 2496 } 2497 first_block += fs->super->s_blocks_per_group; 2498 } 2499 /* 2500 * If we've gotten to this point, then the only 2501 * possibility is that the bad block inode meta data 2502 * is using a bad block. 2503 */ 2504 if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) || 2505 (blk == p->inode->i_block[EXT2_DIND_BLOCK]) || 2506 (blk == p->inode->i_block[EXT2_TIND_BLOCK])) { 2507 p->bbcheck = 1; 2508 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, pctx)) { 2509 *block_nr = 0; 2510 return BLOCK_CHANGED; 2511 } 2512 if (ctx->flags & E2F_FLAG_SIGNAL_MASK) 2513 return BLOCK_ABORT; 2514 return 0; 2515 } 2516 2517 pctx->group = -1; 2518 2519 /* Warn user that the block wasn't claimed */ 2520 fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx); 2521 2522 return 0; 2523} 2524 2525static void new_table_block(e2fsck_t ctx, blk_t first_block, int group, 2526 const char *name, int num, blk64_t *new_block) 2527{ 2528 ext2_filsys fs = ctx->fs; 2529 dgrp_t last_grp; 2530 blk64_t old_block = *new_block; 2531 blk64_t last_block; 2532 int i, is_flexbg, flexbg, flexbg_size; 2533 char *buf; 2534 struct problem_context pctx; 2535 2536 clear_problem_context(&pctx); 2537 2538 pctx.group = group; 2539 pctx.blk = old_block; 2540 pctx.str = name; 2541 2542 /* 2543 * For flex_bg filesystems, first try to allocate the metadata 2544 * within the flex_bg, and if that fails then try finding the 2545 * space anywhere in the filesystem. 2546 */ 2547 is_flexbg = EXT2_HAS_INCOMPAT_FEATURE(fs->super, 2548 EXT4_FEATURE_INCOMPAT_FLEX_BG); 2549 if (is_flexbg) { 2550 flexbg_size = 1 << fs->super->s_log_groups_per_flex; 2551 flexbg = group / flexbg_size; 2552 first_block = ext2fs_group_first_block2(fs, 2553 flexbg_size * flexbg); 2554 last_grp = group | (flexbg_size - 1); 2555 if (last_grp > fs->group_desc_count) 2556 last_grp = fs->group_desc_count; 2557 last_block = ext2fs_group_last_block2(fs, last_grp); 2558 } else 2559 last_block = ext2fs_group_last_block2(fs, group); 2560 pctx.errcode = ext2fs_get_free_blocks2(fs, first_block, last_block, 2561 num, ctx->block_found_map, 2562 new_block); 2563 if (is_flexbg && (pctx.errcode == EXT2_ET_BLOCK_ALLOC_FAIL)) 2564 pctx.errcode = ext2fs_get_free_blocks2(fs, 2565 fs->super->s_first_data_block, 2566 ext2fs_blocks_count(fs->super), 2567 num, ctx->block_found_map, new_block); 2568 if (pctx.errcode) { 2569 pctx.num = num; 2570 fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx); 2571 ext2fs_unmark_valid(fs); 2572 ctx->flags |= E2F_FLAG_ABORT; 2573 return; 2574 } 2575 pctx.errcode = ext2fs_get_mem(fs->blocksize, &buf); 2576 if (pctx.errcode) { 2577 fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx); 2578 ext2fs_unmark_valid(fs); 2579 ctx->flags |= E2F_FLAG_ABORT; 2580 return; 2581 } 2582 ext2fs_mark_super_dirty(fs); 2583 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY; 2584 pctx.blk2 = *new_block; 2585 fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO : 2586 PR_1_RELOC_TO), &pctx); 2587 pctx.blk2 = 0; 2588 for (i = 0; i < num; i++) { 2589 pctx.blk = i; 2590 ext2fs_mark_block_bitmap2(ctx->block_found_map, (*new_block)+i); 2591 if (old_block) { 2592 pctx.errcode = io_channel_read_blk64(fs->io, 2593 old_block + i, 1, buf); 2594 if (pctx.errcode) 2595 fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx); 2596 } else 2597 memset(buf, 0, fs->blocksize); 2598 2599 pctx.blk = (*new_block) + i; 2600 pctx.errcode = io_channel_write_blk64(fs->io, pctx.blk, 2601 1, buf); 2602 if (pctx.errcode) 2603 fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx); 2604 } 2605 ext2fs_free_mem(&buf); 2606} 2607 2608/* 2609 * This routine gets called at the end of pass 1 if bad blocks are 2610 * detected in the superblock, group descriptors, inode_bitmaps, or 2611 * block bitmaps. At this point, all of the blocks have been mapped 2612 * out, so we can try to allocate new block(s) to replace the bad 2613 * blocks. 2614 */ 2615static void handle_fs_bad_blocks(e2fsck_t ctx) 2616{ 2617 ext2_filsys fs = ctx->fs; 2618 dgrp_t i; 2619 blk64_t first_block; 2620 blk64_t new_blk; 2621 2622 for (i = 0; i < fs->group_desc_count; i++) { 2623 first_block = ext2fs_group_first_block2(fs, i); 2624 2625 if (ctx->invalid_block_bitmap_flag[i]) { 2626 new_blk = ext2fs_block_bitmap_loc(fs, i); 2627 new_table_block(ctx, first_block, i, _("block bitmap"), 2628 1, &new_blk); 2629 ext2fs_block_bitmap_loc_set(fs, i, new_blk); 2630 } 2631 if (ctx->invalid_inode_bitmap_flag[i]) { 2632 new_blk = ext2fs_inode_bitmap_loc(fs, i); 2633 new_table_block(ctx, first_block, i, _("inode bitmap"), 2634 1, &new_blk); 2635 ext2fs_inode_bitmap_loc_set(fs, i, new_blk); 2636 } 2637 if (ctx->invalid_inode_table_flag[i]) { 2638 new_blk = ext2fs_inode_table_loc(fs, i); 2639 new_table_block(ctx, first_block, i, _("inode table"), 2640 fs->inode_blocks_per_group, 2641 &new_blk); 2642 ext2fs_inode_table_loc_set(fs, i, new_blk); 2643 ctx->flags |= E2F_FLAG_RESTART; 2644 } 2645 } 2646 ctx->invalid_bitmaps = 0; 2647} 2648 2649/* 2650 * This routine marks all blocks which are used by the superblock, 2651 * group descriptors, inode bitmaps, and block bitmaps. 2652 */ 2653static void mark_table_blocks(e2fsck_t ctx) 2654{ 2655 ext2_filsys fs = ctx->fs; 2656 blk64_t b; 2657 dgrp_t i; 2658 int j; 2659 struct problem_context pctx; 2660 2661 clear_problem_context(&pctx); 2662 2663 for (i = 0; i < fs->group_desc_count; i++) { 2664 pctx.group = i; 2665 2666 ext2fs_reserve_super_and_bgd(fs, i, ctx->block_found_map); 2667 2668 /* 2669 * Mark the blocks used for the inode table 2670 */ 2671 if (ext2fs_inode_table_loc(fs, i)) { 2672 for (j = 0, b = ext2fs_inode_table_loc(fs, i); 2673 j < fs->inode_blocks_per_group; 2674 j++, b++) { 2675 if (ext2fs_test_block_bitmap2(ctx->block_found_map, 2676 b)) { 2677 pctx.blk = b; 2678 if (!ctx->invalid_inode_table_flag[i] && 2679 fix_problem(ctx, 2680 PR_1_ITABLE_CONFLICT, &pctx)) { 2681 ctx->invalid_inode_table_flag[i]++; 2682 ctx->invalid_bitmaps++; 2683 } 2684 } else { 2685 ext2fs_mark_block_bitmap2(ctx->block_found_map, 2686 b); 2687 } 2688 } 2689 } 2690 2691 /* 2692 * Mark block used for the block bitmap 2693 */ 2694 if (ext2fs_block_bitmap_loc(fs, i)) { 2695 if (ext2fs_test_block_bitmap2(ctx->block_found_map, 2696 ext2fs_block_bitmap_loc(fs, i))) { 2697 pctx.blk = ext2fs_block_bitmap_loc(fs, i); 2698 if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) { 2699 ctx->invalid_block_bitmap_flag[i]++; 2700 ctx->invalid_bitmaps++; 2701 } 2702 } else { 2703 ext2fs_mark_block_bitmap2(ctx->block_found_map, 2704 ext2fs_block_bitmap_loc(fs, i)); 2705 } 2706 2707 } 2708 /* 2709 * Mark block used for the inode bitmap 2710 */ 2711 if (ext2fs_inode_bitmap_loc(fs, i)) { 2712 if (ext2fs_test_block_bitmap2(ctx->block_found_map, 2713 ext2fs_inode_bitmap_loc(fs, i))) { 2714 pctx.blk = ext2fs_inode_bitmap_loc(fs, i); 2715 if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) { 2716 ctx->invalid_inode_bitmap_flag[i]++; 2717 ctx->invalid_bitmaps++; 2718 } 2719 } else { 2720 ext2fs_mark_block_bitmap2(ctx->block_found_map, 2721 ext2fs_inode_bitmap_loc(fs, i)); 2722 } 2723 } 2724 } 2725} 2726 2727/* 2728 * Thes subroutines short circuits ext2fs_get_blocks and 2729 * ext2fs_check_directory; we use them since we already have the inode 2730 * structure, so there's no point in letting the ext2fs library read 2731 * the inode again. 2732 */ 2733static errcode_t pass1_get_blocks(ext2_filsys fs, ext2_ino_t ino, 2734 blk_t *blocks) 2735{ 2736 e2fsck_t ctx = (e2fsck_t) fs->priv_data; 2737 int i; 2738 2739 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode) 2740 return EXT2_ET_CALLBACK_NOTHANDLED; 2741 2742 for (i=0; i < EXT2_N_BLOCKS; i++) 2743 blocks[i] = ctx->stashed_inode->i_block[i]; 2744 return 0; 2745} 2746 2747static errcode_t pass1_read_inode(ext2_filsys fs, ext2_ino_t ino, 2748 struct ext2_inode *inode) 2749{ 2750 e2fsck_t ctx = (e2fsck_t) fs->priv_data; 2751 2752 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode) 2753 return EXT2_ET_CALLBACK_NOTHANDLED; 2754 *inode = *ctx->stashed_inode; 2755 return 0; 2756} 2757 2758static errcode_t pass1_write_inode(ext2_filsys fs, ext2_ino_t ino, 2759 struct ext2_inode *inode) 2760{ 2761 e2fsck_t ctx = (e2fsck_t) fs->priv_data; 2762 2763 if ((ino == ctx->stashed_ino) && ctx->stashed_inode && 2764 (inode != ctx->stashed_inode)) 2765 *ctx->stashed_inode = *inode; 2766 return EXT2_ET_CALLBACK_NOTHANDLED; 2767} 2768 2769static errcode_t pass1_check_directory(ext2_filsys fs, ext2_ino_t ino) 2770{ 2771 e2fsck_t ctx = (e2fsck_t) fs->priv_data; 2772 2773 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode) 2774 return EXT2_ET_CALLBACK_NOTHANDLED; 2775 2776 if (!LINUX_S_ISDIR(ctx->stashed_inode->i_mode)) 2777 return EXT2_ET_NO_DIRECTORY; 2778 return 0; 2779} 2780 2781static errcode_t e2fsck_get_alloc_block(ext2_filsys fs, blk64_t goal, 2782 blk64_t *ret) 2783{ 2784 e2fsck_t ctx = (e2fsck_t) fs->priv_data; 2785 errcode_t retval; 2786 blk64_t new_block; 2787 2788 if (ctx->block_found_map) { 2789 retval = ext2fs_new_block2(fs, goal, ctx->block_found_map, 2790 &new_block); 2791 if (retval) 2792 return retval; 2793 if (fs->block_map) { 2794 ext2fs_mark_block_bitmap2(fs->block_map, new_block); 2795 ext2fs_mark_bb_dirty(fs); 2796 } 2797 } else { 2798 if (!fs->block_map) { 2799 retval = ext2fs_read_block_bitmap(fs); 2800 if (retval) 2801 return retval; 2802 } 2803 2804 retval = ext2fs_new_block2(fs, goal, 0, &new_block); 2805 if (retval) 2806 return retval; 2807 } 2808 2809 *ret = new_block; 2810 return (0); 2811} 2812 2813static void e2fsck_block_alloc_stats(ext2_filsys fs, blk64_t blk, int inuse) 2814{ 2815 e2fsck_t ctx = (e2fsck_t) fs->priv_data; 2816 2817 if (ctx->block_found_map) { 2818 if (inuse > 0) 2819 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk); 2820 else 2821 ext2fs_unmark_block_bitmap2(ctx->block_found_map, blk); 2822 } 2823} 2824 2825void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int bool) 2826{ 2827 ext2_filsys fs = ctx->fs; 2828 2829 if (bool) { 2830 fs->get_blocks = pass1_get_blocks; 2831 fs->check_directory = pass1_check_directory; 2832 fs->read_inode = pass1_read_inode; 2833 fs->write_inode = pass1_write_inode; 2834 ctx->stashed_ino = 0; 2835 ext2fs_set_alloc_block_callback(fs, e2fsck_get_alloc_block, 2836 0); 2837 ext2fs_set_block_alloc_stats_callback(fs, 2838 e2fsck_block_alloc_stats, 2839 0); 2840 } else { 2841 fs->get_blocks = 0; 2842 fs->check_directory = 0; 2843 fs->read_inode = 0; 2844 fs->write_inode = 0; 2845 } 2846} 2847