pass1.c revision cebe48a1ece8b9f61271c73ead83b59c8b36233e
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 <string.h> 42#include <time.h> 43#ifdef HAVE_ERRNO_H 44#include <errno.h> 45#endif 46 47#include "e2fsck.h" 48#include <ext2fs/ext2_ext_attr.h> 49 50#include "problem.h" 51 52#ifdef NO_INLINE_FUNCS 53#define _INLINE_ 54#else 55#define _INLINE_ inline 56#endif 57 58static int process_block(ext2_filsys fs, blk_t *blocknr, 59 e2_blkcnt_t blockcnt, blk_t ref_blk, 60 int ref_offset, void *priv_data); 61static int process_bad_block(ext2_filsys fs, blk_t *block_nr, 62 e2_blkcnt_t blockcnt, blk_t ref_blk, 63 int ref_offset, void *priv_data); 64static void check_blocks(e2fsck_t ctx, struct problem_context *pctx, 65 char *block_buf); 66static void mark_table_blocks(e2fsck_t ctx); 67static void alloc_bb_map(e2fsck_t ctx); 68static void alloc_imagic_map(e2fsck_t ctx); 69static void mark_inode_bad(e2fsck_t ctx, ino_t ino); 70static void handle_fs_bad_blocks(e2fsck_t ctx); 71static void process_inodes(e2fsck_t ctx, char *block_buf); 72static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b); 73static errcode_t scan_callback(ext2_filsys fs, ext2_inode_scan scan, 74 dgrp_t group, void * priv_data); 75static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount, 76 char *block_buf, int adjust_sign); 77/* static char *describe_illegal_block(ext2_filsys fs, blk_t block); */ 78 79struct process_block_struct { 80 ext2_ino_t ino; 81 unsigned is_dir:1, is_reg:1, clear:1, suppress:1, 82 fragmented:1, compressed:1, bbcheck:1; 83 blk_t num_blocks; 84 blk_t max_blocks; 85 e2_blkcnt_t last_block; 86 int num_illegal_blocks; 87 blk_t previous_block; 88 struct ext2_inode *inode; 89 struct problem_context *pctx; 90 ext2fs_block_bitmap fs_meta_blocks; 91 e2fsck_t ctx; 92}; 93 94struct process_inode_block { 95 ext2_ino_t ino; 96 struct ext2_inode inode; 97}; 98 99struct scan_callback_struct { 100 e2fsck_t ctx; 101 char *block_buf; 102}; 103 104/* 105 * For the inodes to process list. 106 */ 107static struct process_inode_block *inodes_to_process; 108static int process_inode_count; 109 110static __u64 ext2_max_sizes[EXT2_MAX_BLOCK_LOG_SIZE - 111 EXT2_MIN_BLOCK_LOG_SIZE + 1]; 112 113/* 114 * Free all memory allocated by pass1 in preparation for restarting 115 * things. 116 */ 117static void unwind_pass1(ext2_filsys fs EXT2FS_ATTR((unused))) 118{ 119 ext2fs_free_mem(&inodes_to_process); 120 inodes_to_process = 0; 121} 122 123/* 124 * Check to make sure a device inode is real. Returns 1 if the device 125 * checks out, 0 if not. 126 * 127 * Note: this routine is now also used to check FIFO's and Sockets, 128 * since they have the same requirement; the i_block fields should be 129 * zero. 130 */ 131int e2fsck_pass1_check_device_inode(ext2_filsys fs, struct ext2_inode *inode) 132{ 133 int i; 134 135 /* 136 * If i_blocks is non-zero, or the index flag is set, then 137 * this is a bogus device/fifo/socket 138 */ 139 if ((ext2fs_inode_data_blocks(fs, inode) != 0) || 140 (inode->i_flags & EXT2_INDEX_FL)) 141 return 0; 142 143 /* 144 * We should be able to do the test below all the time, but 145 * because the kernel doesn't forcibly clear the device 146 * inode's additional i_block fields, there are some rare 147 * occasions when a legitimate device inode will have non-zero 148 * additional i_block fields. So for now, we only complain 149 * when the immutable flag is set, which should never happen 150 * for devices. (And that's when the problem is caused, since 151 * you can't set or clear immutable flags for devices.) Once 152 * the kernel has been fixed we can change this... 153 */ 154 if (inode->i_flags & (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)) { 155 for (i=4; i < EXT2_N_BLOCKS; i++) 156 if (inode->i_block[i]) 157 return 0; 158 } 159 return 1; 160} 161 162/* 163 * Check to make sure a symlink inode is real. Returns 1 if the symlink 164 * checks out, 0 if not. 165 */ 166int e2fsck_pass1_check_symlink(ext2_filsys fs, struct ext2_inode *inode, 167 char *buf) 168{ 169 unsigned int len; 170 int i; 171 blk_t blocks; 172 173 if ((inode->i_size_high || inode->i_size == 0) || 174 (inode->i_flags & EXT2_INDEX_FL)) 175 return 0; 176 177 blocks = ext2fs_inode_data_blocks(fs, inode); 178 if (blocks) { 179 if ((inode->i_size >= fs->blocksize) || 180 (blocks != fs->blocksize >> 9) || 181 (inode->i_block[0] < fs->super->s_first_data_block) || 182 (inode->i_block[0] >= fs->super->s_blocks_count)) 183 return 0; 184 185 for (i = 1; i < EXT2_N_BLOCKS; i++) 186 if (inode->i_block[i]) 187 return 0; 188 189 if (io_channel_read_blk(fs->io, inode->i_block[0], 1, buf)) 190 return 0; 191 192 len = strnlen(buf, fs->blocksize); 193 if (len == fs->blocksize) 194 return 0; 195 } else { 196 if (inode->i_size >= sizeof(inode->i_block)) 197 return 0; 198 199 len = strnlen((char *)inode->i_block, sizeof(inode->i_block)); 200 if (len == sizeof(inode->i_block)) 201 return 0; 202 } 203 if (len != inode->i_size) 204 return 0; 205 return 1; 206} 207 208/* 209 * If the immutable (or append-only) flag is set on the inode, offer 210 * to clear it. 211 */ 212#define BAD_SPECIAL_FLAGS (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL) 213static void check_immutable(e2fsck_t ctx, struct problem_context *pctx) 214{ 215 if (!(pctx->inode->i_flags & BAD_SPECIAL_FLAGS)) 216 return; 217 218 if (!fix_problem(ctx, PR_1_SET_IMMUTABLE, pctx)) 219 return; 220 221 pctx->inode->i_flags &= ~BAD_SPECIAL_FLAGS; 222 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1"); 223} 224 225/* 226 * If device, fifo or socket, check size is zero -- if not offer to 227 * clear it 228 */ 229static void check_size(e2fsck_t ctx, struct problem_context *pctx) 230{ 231 struct ext2_inode *inode = pctx->inode; 232 233 if ((inode->i_size == 0) && (inode->i_size_high == 0)) 234 return; 235 236 if (!fix_problem(ctx, PR_1_SET_NONZSIZE, pctx)) 237 return; 238 239 inode->i_size = 0; 240 inode->i_size_high = 0; 241 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1"); 242} 243 244static void check_ea_in_inode(e2fsck_t ctx, struct problem_context *pctx) 245{ 246 struct ext2_super_block *sb = ctx->fs->super; 247 struct ext2_inode_large *inode; 248 struct ext2_ext_attr_entry *entry; 249 char *start, *end, *name; 250 int storage_size, remain, offs; 251 int problem = 0; 252 253 inode = (struct ext2_inode_large *) pctx->inode; 254 storage_size = EXT2_INODE_SIZE(ctx->fs->super) - EXT2_GOOD_OLD_INODE_SIZE - 255 inode->i_extra_isize; 256 start = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE + 257 inode->i_extra_isize + sizeof(__u32); 258 end = (char *) inode + EXT2_INODE_SIZE(ctx->fs->super); 259 entry = (struct ext2_ext_attr_entry *) start; 260 261 /* scan all entry's headers first */ 262 263 /* take finish entry 0UL into account */ 264 remain = storage_size - sizeof(__u32); 265 offs = end - start; 266 267 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) { 268 269 /* header eats this space */ 270 remain -= sizeof(struct ext2_ext_attr_entry); 271 272 /* is attribute name valid? */ 273 if (EXT2_EXT_ATTR_SIZE(entry->e_name_len) > remain) { 274 pctx->num = entry->e_name_len; 275 problem = PR_1_ATTR_NAME_LEN; 276 goto fix; 277 } 278 279 /* attribute len eats this space */ 280 remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len); 281 282 /* check value size */ 283 if (entry->e_value_size == 0 || entry->e_value_size > remain) { 284 pctx->num = entry->e_value_size; 285 problem = PR_1_ATTR_VALUE_SIZE; 286 goto fix; 287 } 288 289 /* check value placement */ 290 if (entry->e_value_offs + 291 EXT2_XATTR_SIZE(entry->e_value_size) != offs) { 292 printf("(entry->e_value_offs + entry->e_value_size: %d, offs: %d)\n", entry->e_value_offs + entry->e_value_size, offs); 293 pctx->num = entry->e_value_offs; 294 problem = PR_1_ATTR_VALUE_OFFSET; 295 goto fix; 296 } 297 298 /* e_value_block must be 0 in inode's ea */ 299 if (entry->e_value_block != 0) { 300 pctx->num = entry->e_value_block; 301 problem = PR_1_ATTR_VALUE_BLOCK; 302 goto fix; 303 } 304 305 /* e_hash must be 0 in inode's ea */ 306 if (entry->e_hash != 0) { 307 pctx->num = entry->e_hash; 308 problem = PR_1_ATTR_HASH; 309 goto fix; 310 } 311 312 remain -= entry->e_value_size; 313 offs -= EXT2_XATTR_SIZE(entry->e_value_size); 314 315 entry = EXT2_EXT_ATTR_NEXT(entry); 316 } 317fix: 318 /* 319 * it seems like a corruption. it's very unlikely we could repair 320 * EA(s) in automatic fashion -bzzz 321 */ 322#if 0 323 problem = PR_1_ATTR_HASH; 324#endif 325 if (problem == 0 || !fix_problem(ctx, problem, pctx)) 326 return; 327 328 /* simple remove all possible EA(s) */ 329 *((__u32 *)start) = 0UL; 330 e2fsck_write_inode_full(ctx, pctx->ino, inode, 331 EXT2_INODE_SIZE(sb), "pass1"); 332} 333 334static void check_inode_extra_space(e2fsck_t ctx, struct problem_context *pctx) 335{ 336 struct ext2_super_block *sb = ctx->fs->super; 337 struct ext2_inode_large *inode; 338 __u32 *eamagic; 339 int min, max; 340 341 inode = (struct ext2_inode_large *) pctx->inode; 342 if (EXT2_INODE_SIZE(sb) == EXT2_GOOD_OLD_INODE_SIZE) { 343 /* this isn't large inode. so, nothing to check */ 344 return; 345 } 346 347#if 0 348 printf("inode #%u, i_extra_size %d\n", pctx->ino, 349 inode->i_extra_isize); 350#endif 351 /* i_extra_isize must cover i_extra_isize + i_pad1 at least */ 352 min = sizeof(inode->i_extra_isize) + sizeof(inode->i_pad1); 353 max = EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE; 354 /* 355 * For now we will allow i_extra_isize to be 0, but really 356 * implementations should never allow i_extra_isize to be 0 357 */ 358 if (inode->i_extra_isize && 359 (inode->i_extra_isize < min || inode->i_extra_isize > max)) { 360 if (!fix_problem(ctx, PR_1_EXTRA_ISIZE, pctx)) 361 return; 362 inode->i_extra_isize = min; 363 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode, 364 EXT2_INODE_SIZE(sb), "pass1"); 365 return; 366 } 367 368 eamagic = (__u32 *) (((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE + 369 inode->i_extra_isize); 370 if (*eamagic == EXT2_EXT_ATTR_MAGIC) { 371 /* it seems inode has an extended attribute(s) in body */ 372 check_ea_in_inode(ctx, pctx); 373 } 374} 375 376void e2fsck_pass1(e2fsck_t ctx) 377{ 378 int i; 379 __u64 max_sizes; 380 ext2_filsys fs = ctx->fs; 381 ext2_ino_t ino; 382 struct ext2_inode *inode; 383 ext2_inode_scan scan; 384 char *block_buf; 385#ifdef RESOURCE_TRACK 386 struct resource_track rtrack; 387#endif 388 unsigned char frag, fsize; 389 struct problem_context pctx; 390 struct scan_callback_struct scan_struct; 391 struct ext2_super_block *sb = ctx->fs->super; 392 int imagic_fs; 393 int busted_fs_time = 0; 394 int inode_size; 395 396#ifdef RESOURCE_TRACK 397 init_resource_track(&rtrack); 398#endif 399 clear_problem_context(&pctx); 400 401 if (!(ctx->options & E2F_OPT_PREEN)) 402 fix_problem(ctx, PR_1_PASS_HEADER, &pctx); 403 404 if ((fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) && 405 !(ctx->options & E2F_OPT_NO)) { 406 if (ext2fs_u32_list_create(&ctx->dirs_to_hash, 50)) 407 ctx->dirs_to_hash = 0; 408 } 409 410#ifdef MTRACE 411 mtrace_print("Pass 1"); 412#endif 413 414#define EXT2_BPP(bits) (1ULL << ((bits) - 2)) 415 416 for (i = EXT2_MIN_BLOCK_LOG_SIZE; i <= EXT2_MAX_BLOCK_LOG_SIZE; i++) { 417 max_sizes = EXT2_NDIR_BLOCKS + EXT2_BPP(i); 418 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i); 419 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i) * EXT2_BPP(i); 420 max_sizes = (max_sizes * (1UL << i)) - 1; 421 ext2_max_sizes[i - EXT2_MIN_BLOCK_LOG_SIZE] = max_sizes; 422 } 423#undef EXT2_BPP 424 425 imagic_fs = (sb->s_feature_compat & EXT2_FEATURE_COMPAT_IMAGIC_INODES); 426 427 /* 428 * Allocate bitmaps structures 429 */ 430 pctx.errcode = ext2fs_allocate_inode_bitmap(fs, _("in-use inode map"), 431 &ctx->inode_used_map); 432 if (pctx.errcode) { 433 pctx.num = 1; 434 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx); 435 ctx->flags |= E2F_FLAG_ABORT; 436 return; 437 } 438 pctx.errcode = ext2fs_allocate_inode_bitmap(fs, 439 _("directory inode map"), &ctx->inode_dir_map); 440 if (pctx.errcode) { 441 pctx.num = 2; 442 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx); 443 ctx->flags |= E2F_FLAG_ABORT; 444 return; 445 } 446 pctx.errcode = ext2fs_allocate_inode_bitmap(fs, 447 _("regular file inode map"), &ctx->inode_reg_map); 448 if (pctx.errcode) { 449 pctx.num = 6; 450 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx); 451 ctx->flags |= E2F_FLAG_ABORT; 452 return; 453 } 454 pctx.errcode = ext2fs_allocate_block_bitmap(fs, _("in-use block map"), 455 &ctx->block_found_map); 456 if (pctx.errcode) { 457 pctx.num = 1; 458 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx); 459 ctx->flags |= E2F_FLAG_ABORT; 460 return; 461 } 462 pctx.errcode = ext2fs_create_icount2(fs, 0, 0, 0, 463 &ctx->inode_link_info); 464 if (pctx.errcode) { 465 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx); 466 ctx->flags |= E2F_FLAG_ABORT; 467 return; 468 } 469 inode_size = EXT2_INODE_SIZE(fs->super); 470 inode = (struct ext2_inode *) 471 e2fsck_allocate_memory(ctx, inode_size, "scratch inode"); 472 473 inodes_to_process = (struct process_inode_block *) 474 e2fsck_allocate_memory(ctx, 475 (ctx->process_inode_size * 476 sizeof(struct process_inode_block)), 477 "array of inodes to process"); 478 process_inode_count = 0; 479 480 pctx.errcode = ext2fs_init_dblist(fs, 0); 481 if (pctx.errcode) { 482 fix_problem(ctx, PR_1_ALLOCATE_DBCOUNT, &pctx); 483 ctx->flags |= E2F_FLAG_ABORT; 484 return; 485 } 486 487 /* 488 * If the last orphan field is set, clear it, since the pass1 489 * processing will automatically find and clear the orphans. 490 * In the future, we may want to try using the last_orphan 491 * linked list ourselves, but for now, we clear it so that the 492 * ext3 mount code won't get confused. 493 */ 494 if (!(ctx->options & E2F_OPT_READONLY)) { 495 if (fs->super->s_last_orphan) { 496 fs->super->s_last_orphan = 0; 497 ext2fs_mark_super_dirty(fs); 498 } 499 } 500 501 mark_table_blocks(ctx); 502 block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3, 503 "block interate buffer"); 504 e2fsck_use_inode_shortcuts(ctx, 1); 505 ehandler_operation(_("doing inode scan")); 506 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks, 507 &scan); 508 if (pctx.errcode) { 509 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx); 510 ctx->flags |= E2F_FLAG_ABORT; 511 return; 512 } 513 ext2fs_inode_scan_flags(scan, EXT2_SF_SKIP_MISSING_ITABLE, 0); 514 ctx->stashed_inode = inode; 515 scan_struct.ctx = ctx; 516 scan_struct.block_buf = block_buf; 517 ext2fs_set_inode_callback(scan, scan_callback, &scan_struct); 518 if (ctx->progress) 519 if ((ctx->progress)(ctx, 1, 0, ctx->fs->group_desc_count)) 520 return; 521 if (fs->super->s_wtime < fs->super->s_inodes_count) 522 busted_fs_time = 1; 523 524 while (1) { 525 pctx.errcode = ext2fs_get_next_inode_full(scan, &ino, 526 inode, inode_size); 527 if (ctx->flags & E2F_FLAG_SIGNAL_MASK) 528 return; 529 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) { 530 if (!ctx->inode_bb_map) 531 alloc_bb_map(ctx); 532 ext2fs_mark_inode_bitmap(ctx->inode_bb_map, ino); 533 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino); 534 continue; 535 } 536 if (pctx.errcode) { 537 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx); 538 ctx->flags |= E2F_FLAG_ABORT; 539 return; 540 } 541 if (!ino) 542 break; 543 pctx.ino = ino; 544 pctx.inode = inode; 545 ctx->stashed_ino = ino; 546 if (inode->i_links_count) { 547 pctx.errcode = ext2fs_icount_store(ctx->inode_link_info, 548 ino, inode->i_links_count); 549 if (pctx.errcode) { 550 pctx.num = inode->i_links_count; 551 fix_problem(ctx, PR_1_ICOUNT_STORE, &pctx); 552 ctx->flags |= E2F_FLAG_ABORT; 553 return; 554 } 555 } 556 if (ino == EXT2_BAD_INO) { 557 struct process_block_struct pb; 558 559 pctx.errcode = ext2fs_copy_bitmap(ctx->block_found_map, 560 &pb.fs_meta_blocks); 561 if (pctx.errcode) { 562 pctx.num = 4; 563 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx); 564 ctx->flags |= E2F_FLAG_ABORT; 565 return; 566 } 567 pb.ino = EXT2_BAD_INO; 568 pb.num_blocks = pb.last_block = 0; 569 pb.num_illegal_blocks = 0; 570 pb.suppress = 0; pb.clear = 0; pb.is_dir = 0; 571 pb.is_reg = 0; pb.fragmented = 0; pb.bbcheck = 0; 572 pb.inode = inode; 573 pb.pctx = &pctx; 574 pb.ctx = ctx; 575 pctx.errcode = ext2fs_block_iterate2(fs, ino, 0, 576 block_buf, process_bad_block, &pb); 577 ext2fs_free_block_bitmap(pb.fs_meta_blocks); 578 if (pctx.errcode) { 579 fix_problem(ctx, PR_1_BLOCK_ITERATE, &pctx); 580 ctx->flags |= E2F_FLAG_ABORT; 581 return; 582 } 583 if (pb.bbcheck) 584 if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK_PROMPT, &pctx)) { 585 ctx->flags |= E2F_FLAG_ABORT; 586 return; 587 } 588 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino); 589 clear_problem_context(&pctx); 590 continue; 591 } else if (ino == EXT2_ROOT_INO) { 592 /* 593 * Make sure the root inode is a directory; if 594 * not, offer to clear it. It will be 595 * regnerated in pass #3. 596 */ 597 if (!LINUX_S_ISDIR(inode->i_mode)) { 598 if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx)) { 599 inode->i_dtime = time(0); 600 inode->i_links_count = 0; 601 ext2fs_icount_store(ctx->inode_link_info, 602 ino, 0); 603 e2fsck_write_inode(ctx, ino, inode, 604 "pass1"); 605 } 606 607 } 608 /* 609 * If dtime is set, offer to clear it. mke2fs 610 * version 0.2b created filesystems with the 611 * dtime field set for the root and lost+found 612 * directories. We won't worry about 613 * /lost+found, since that can be regenerated 614 * easily. But we will fix the root directory 615 * as a special case. 616 */ 617 if (inode->i_dtime && inode->i_links_count) { 618 if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) { 619 inode->i_dtime = 0; 620 e2fsck_write_inode(ctx, ino, inode, 621 "pass1"); 622 } 623 } 624 } else if (ino == EXT2_JOURNAL_INO) { 625 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino); 626 if (fs->super->s_journal_inum == EXT2_JOURNAL_INO) { 627 if (!LINUX_S_ISREG(inode->i_mode) && 628 fix_problem(ctx, PR_1_JOURNAL_BAD_MODE, 629 &pctx)) { 630 inode->i_mode = LINUX_S_IFREG; 631 e2fsck_write_inode(ctx, ino, inode, 632 "pass1"); 633 } 634 check_blocks(ctx, &pctx, block_buf); 635 continue; 636 } 637 if ((inode->i_links_count || inode->i_blocks || 638 inode->i_blocks || inode->i_block[0]) && 639 fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR, 640 &pctx)) { 641 memset(inode, 0, inode_size); 642 ext2fs_icount_store(ctx->inode_link_info, 643 ino, 0); 644 e2fsck_write_inode_full(ctx, ino, inode, 645 inode_size, "pass1"); 646 } 647 } else if (ino < EXT2_FIRST_INODE(fs->super)) { 648 int problem = 0; 649 650 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino); 651 if (ino == EXT2_BOOT_LOADER_INO) { 652 if (LINUX_S_ISDIR(inode->i_mode)) 653 problem = PR_1_RESERVED_BAD_MODE; 654 } else if (ino == EXT2_RESIZE_INO) { 655 if (inode->i_mode && 656 !LINUX_S_ISREG(inode->i_mode)) 657 problem = PR_1_RESERVED_BAD_MODE; 658 } else { 659 if (inode->i_mode != 0) 660 problem = PR_1_RESERVED_BAD_MODE; 661 } 662 if (problem) { 663 if (fix_problem(ctx, problem, &pctx)) { 664 inode->i_mode = 0; 665 e2fsck_write_inode(ctx, ino, inode, 666 "pass1"); 667 } 668 } 669 check_blocks(ctx, &pctx, block_buf); 670 continue; 671 } 672 /* 673 * Check for inodes who might have been part of the 674 * orphaned list linked list. They should have gotten 675 * dealt with by now, unless the list had somehow been 676 * corrupted. 677 * 678 * FIXME: In the future, inodes which are still in use 679 * (and which are therefore) pending truncation should 680 * be handled specially. Right now we just clear the 681 * dtime field, and the normal e2fsck handling of 682 * inodes where i_size and the inode blocks are 683 * inconsistent is to fix i_size, instead of releasing 684 * the extra blocks. This won't catch the inodes that 685 * was at the end of the orphan list, but it's better 686 * than nothing. The right answer is that there 687 * shouldn't be any bugs in the orphan list handling. :-) 688 */ 689 if (inode->i_dtime && !busted_fs_time && 690 inode->i_dtime < ctx->fs->super->s_inodes_count) { 691 if (fix_problem(ctx, PR_1_LOW_DTIME, &pctx)) { 692 inode->i_dtime = inode->i_links_count ? 693 0 : time(0); 694 e2fsck_write_inode(ctx, ino, inode, 695 "pass1"); 696 } 697 } 698 699 /* 700 * This code assumes that deleted inodes have 701 * i_links_count set to 0. 702 */ 703 if (!inode->i_links_count) { 704 if (!inode->i_dtime && inode->i_mode) { 705 if (fix_problem(ctx, 706 PR_1_ZERO_DTIME, &pctx)) { 707 inode->i_dtime = time(0); 708 e2fsck_write_inode(ctx, ino, inode, 709 "pass1"); 710 } 711 } 712 continue; 713 } 714 /* 715 * n.b. 0.3c ext2fs code didn't clear i_links_count for 716 * deleted files. Oops. 717 * 718 * Since all new ext2 implementations get this right, 719 * we now assume that the case of non-zero 720 * i_links_count and non-zero dtime means that we 721 * should keep the file, not delete it. 722 * 723 */ 724 if (inode->i_dtime) { 725 if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) { 726 inode->i_dtime = 0; 727 e2fsck_write_inode(ctx, ino, inode, "pass1"); 728 } 729 } 730 731 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino); 732 switch (fs->super->s_creator_os) { 733 case EXT2_OS_LINUX: 734 frag = inode->osd2.linux2.l_i_frag; 735 fsize = inode->osd2.linux2.l_i_fsize; 736 break; 737 case EXT2_OS_HURD: 738 frag = inode->osd2.hurd2.h_i_frag; 739 fsize = inode->osd2.hurd2.h_i_fsize; 740 break; 741 case EXT2_OS_MASIX: 742 frag = inode->osd2.masix2.m_i_frag; 743 fsize = inode->osd2.masix2.m_i_fsize; 744 break; 745 default: 746 frag = fsize = 0; 747 } 748 749 if (inode->i_faddr || frag || fsize || 750 (LINUX_S_ISDIR(inode->i_mode) && inode->i_dir_acl)) 751 mark_inode_bad(ctx, ino); 752 if (inode->i_flags & EXT2_IMAGIC_FL) { 753 if (imagic_fs) { 754 if (!ctx->inode_imagic_map) 755 alloc_imagic_map(ctx); 756 ext2fs_mark_inode_bitmap(ctx->inode_imagic_map, 757 ino); 758 } else { 759 if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) { 760 inode->i_flags &= ~EXT2_IMAGIC_FL; 761 e2fsck_write_inode(ctx, ino, 762 inode, "pass1"); 763 } 764 } 765 } 766 767 check_inode_extra_space(ctx, &pctx); 768 769 if (LINUX_S_ISDIR(inode->i_mode)) { 770 ext2fs_mark_inode_bitmap(ctx->inode_dir_map, ino); 771 e2fsck_add_dir_info(ctx, ino, 0); 772 ctx->fs_directory_count++; 773 } else if (LINUX_S_ISREG (inode->i_mode)) { 774 ext2fs_mark_inode_bitmap(ctx->inode_reg_map, ino); 775 ctx->fs_regular_count++; 776 } else if (LINUX_S_ISCHR (inode->i_mode) && 777 e2fsck_pass1_check_device_inode(fs, inode)) { 778 check_immutable(ctx, &pctx); 779 check_size(ctx, &pctx); 780 ctx->fs_chardev_count++; 781 } else if (LINUX_S_ISBLK (inode->i_mode) && 782 e2fsck_pass1_check_device_inode(fs, inode)) { 783 check_immutable(ctx, &pctx); 784 check_size(ctx, &pctx); 785 ctx->fs_blockdev_count++; 786 } else if (LINUX_S_ISLNK (inode->i_mode) && 787 e2fsck_pass1_check_symlink(fs, inode, block_buf)) { 788 check_immutable(ctx, &pctx); 789 ctx->fs_symlinks_count++; 790 if (ext2fs_inode_data_blocks(fs, inode) == 0) { 791 ctx->fs_fast_symlinks_count++; 792 check_blocks(ctx, &pctx, block_buf); 793 continue; 794 } 795 } 796 else if (LINUX_S_ISFIFO (inode->i_mode) && 797 e2fsck_pass1_check_device_inode(fs, inode)) { 798 check_immutable(ctx, &pctx); 799 check_size(ctx, &pctx); 800 ctx->fs_fifo_count++; 801 } else if ((LINUX_S_ISSOCK (inode->i_mode)) && 802 e2fsck_pass1_check_device_inode(fs, inode)) { 803 check_immutable(ctx, &pctx); 804 check_size(ctx, &pctx); 805 ctx->fs_sockets_count++; 806 } else 807 mark_inode_bad(ctx, ino); 808 if (inode->i_block[EXT2_IND_BLOCK]) 809 ctx->fs_ind_count++; 810 if (inode->i_block[EXT2_DIND_BLOCK]) 811 ctx->fs_dind_count++; 812 if (inode->i_block[EXT2_TIND_BLOCK]) 813 ctx->fs_tind_count++; 814 if (inode->i_block[EXT2_IND_BLOCK] || 815 inode->i_block[EXT2_DIND_BLOCK] || 816 inode->i_block[EXT2_TIND_BLOCK] || 817 inode->i_file_acl) { 818 inodes_to_process[process_inode_count].ino = ino; 819 inodes_to_process[process_inode_count].inode = *inode; 820 process_inode_count++; 821 } else 822 check_blocks(ctx, &pctx, block_buf); 823 824 if (ctx->flags & E2F_FLAG_SIGNAL_MASK) 825 return; 826 827 if (process_inode_count >= ctx->process_inode_size) { 828 process_inodes(ctx, block_buf); 829 830 if (ctx->flags & E2F_FLAG_SIGNAL_MASK) 831 return; 832 } 833 } 834 process_inodes(ctx, block_buf); 835 ext2fs_close_inode_scan(scan); 836 ehandler_operation(0); 837 838 /* 839 * If any extended attribute blocks' reference counts need to 840 * be adjusted, either up (ctx->refcount_extra), or down 841 * (ctx->refcount), then fix them. 842 */ 843 if (ctx->refcount) { 844 adjust_extattr_refcount(ctx, ctx->refcount, block_buf, -1); 845 ea_refcount_free(ctx->refcount); 846 ctx->refcount = 0; 847 } 848 if (ctx->refcount_extra) { 849 adjust_extattr_refcount(ctx, ctx->refcount_extra, 850 block_buf, +1); 851 ea_refcount_free(ctx->refcount_extra); 852 ctx->refcount_extra = 0; 853 } 854 855 if (ctx->invalid_bitmaps) 856 handle_fs_bad_blocks(ctx); 857 858 /* We don't need the block_ea_map any more */ 859 if (ctx->block_ea_map) { 860 ext2fs_free_block_bitmap(ctx->block_ea_map); 861 ctx->block_ea_map = 0; 862 } 863 864 if (ctx->flags & E2F_FLAG_RESIZE_INODE) { 865 ext2fs_block_bitmap save_bmap; 866 errcode_t retval; 867 868 save_bmap = fs->block_map; 869 fs->block_map = ctx->block_found_map; 870 clear_problem_context(&pctx); 871 pctx.errcode = ext2fs_create_resize_inode(fs); 872 if (pctx.errcode) { 873 fix_problem(ctx, PR_1_RESIZE_INODE_CREATE, &pctx); 874 /* Should never get here */ 875 ctx->flags |= E2F_FLAG_ABORT; 876 return; 877 } 878 fs->block_map = save_bmap; 879 ctx->flags &= ~E2F_FLAG_RESIZE_INODE; 880 } 881 882 if (ctx->flags & E2F_FLAG_RESTART) { 883 /* 884 * Only the master copy of the superblock and block 885 * group descriptors are going to be written during a 886 * restart, so set the superblock to be used to be the 887 * master superblock. 888 */ 889 ctx->use_superblock = 0; 890 unwind_pass1(fs); 891 goto endit; 892 } 893 894 if (ctx->block_dup_map) { 895 if (ctx->options & E2F_OPT_PREEN) { 896 clear_problem_context(&pctx); 897 fix_problem(ctx, PR_1_DUP_BLOCKS_PREENSTOP, &pctx); 898 } 899 e2fsck_pass1_dupblocks(ctx, block_buf); 900 } 901 ext2fs_free_mem(&inodes_to_process); 902endit: 903 e2fsck_use_inode_shortcuts(ctx, 0); 904 905 ext2fs_free_mem(&block_buf); 906 ext2fs_free_mem(&inode); 907 908#ifdef RESOURCE_TRACK 909 if (ctx->options & E2F_OPT_TIME2) { 910 e2fsck_clear_progbar(ctx); 911 print_resource_track(_("Pass 1"), &rtrack); 912 } 913#endif 914} 915 916/* 917 * When the inode_scan routines call this callback at the end of the 918 * glock group, call process_inodes. 919 */ 920static errcode_t scan_callback(ext2_filsys fs, 921 ext2_inode_scan scan EXT2FS_ATTR((unused)), 922 dgrp_t group, void * priv_data) 923{ 924 struct scan_callback_struct *scan_struct; 925 e2fsck_t ctx; 926 927 scan_struct = (struct scan_callback_struct *) priv_data; 928 ctx = scan_struct->ctx; 929 930 process_inodes((e2fsck_t) fs->priv_data, scan_struct->block_buf); 931 932 if (ctx->progress) 933 if ((ctx->progress)(ctx, 1, group+1, 934 ctx->fs->group_desc_count)) 935 return EXT2_ET_CANCEL_REQUESTED; 936 937 return 0; 938} 939 940/* 941 * Process the inodes in the "inodes to process" list. 942 */ 943static void process_inodes(e2fsck_t ctx, char *block_buf) 944{ 945 int i; 946 struct ext2_inode *old_stashed_inode; 947 ext2_ino_t old_stashed_ino; 948 const char *old_operation; 949 char buf[80]; 950 struct problem_context pctx; 951 952#if 0 953 printf("begin process_inodes: "); 954#endif 955 if (process_inode_count == 0) 956 return; 957 old_operation = ehandler_operation(0); 958 old_stashed_inode = ctx->stashed_inode; 959 old_stashed_ino = ctx->stashed_ino; 960 qsort(inodes_to_process, process_inode_count, 961 sizeof(struct process_inode_block), process_inode_cmp); 962 clear_problem_context(&pctx); 963 for (i=0; i < process_inode_count; i++) { 964 pctx.inode = ctx->stashed_inode = &inodes_to_process[i].inode; 965 pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino; 966 967#if 0 968 printf("%u ", pctx.ino); 969#endif 970 sprintf(buf, _("reading indirect blocks of inode %u"), 971 pctx.ino); 972 ehandler_operation(buf); 973 check_blocks(ctx, &pctx, block_buf); 974 if (ctx->flags & E2F_FLAG_SIGNAL_MASK) 975 break; 976 } 977 ctx->stashed_inode = old_stashed_inode; 978 ctx->stashed_ino = old_stashed_ino; 979 process_inode_count = 0; 980#if 0 981 printf("end process inodes\n"); 982#endif 983 ehandler_operation(old_operation); 984} 985 986static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b) 987{ 988 const struct process_inode_block *ib_a = 989 (const struct process_inode_block *) a; 990 const struct process_inode_block *ib_b = 991 (const struct process_inode_block *) b; 992 int ret; 993 994 ret = (ib_a->inode.i_block[EXT2_IND_BLOCK] - 995 ib_b->inode.i_block[EXT2_IND_BLOCK]); 996 if (ret == 0) 997 ret = ib_a->inode.i_file_acl - ib_b->inode.i_file_acl; 998 return ret; 999} 1000 1001/* 1002 * Mark an inode as being bad in some what 1003 */ 1004static void mark_inode_bad(e2fsck_t ctx, ino_t ino) 1005{ 1006 struct problem_context pctx; 1007 1008 if (!ctx->inode_bad_map) { 1009 clear_problem_context(&pctx); 1010 1011 pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs, 1012 _("bad inode map"), &ctx->inode_bad_map); 1013 if (pctx.errcode) { 1014 pctx.num = 3; 1015 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx); 1016 /* Should never get here */ 1017 ctx->flags |= E2F_FLAG_ABORT; 1018 return; 1019 } 1020 } 1021 ext2fs_mark_inode_bitmap(ctx->inode_bad_map, ino); 1022} 1023 1024 1025/* 1026 * This procedure will allocate the inode "bb" (badblock) map table 1027 */ 1028static void alloc_bb_map(e2fsck_t ctx) 1029{ 1030 struct problem_context pctx; 1031 1032 clear_problem_context(&pctx); 1033 pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs, 1034 _("inode in bad block map"), 1035 &ctx->inode_bb_map); 1036 if (pctx.errcode) { 1037 pctx.num = 4; 1038 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx); 1039 /* Should never get here */ 1040 ctx->flags |= E2F_FLAG_ABORT; 1041 return; 1042 } 1043} 1044 1045/* 1046 * This procedure will allocate the inode imagic table 1047 */ 1048static void alloc_imagic_map(e2fsck_t ctx) 1049{ 1050 struct problem_context pctx; 1051 1052 clear_problem_context(&pctx); 1053 pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs, 1054 _("imagic inode map"), 1055 &ctx->inode_imagic_map); 1056 if (pctx.errcode) { 1057 pctx.num = 5; 1058 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx); 1059 /* Should never get here */ 1060 ctx->flags |= E2F_FLAG_ABORT; 1061 return; 1062 } 1063} 1064 1065/* 1066 * Marks a block as in use, setting the dup_map if it's been set 1067 * already. Called by process_block and process_bad_block. 1068 * 1069 * WARNING: Assumes checks have already been done to make sure block 1070 * is valid. This is true in both process_block and process_bad_block. 1071 */ 1072static _INLINE_ void mark_block_used(e2fsck_t ctx, blk_t block) 1073{ 1074 struct problem_context pctx; 1075 1076 clear_problem_context(&pctx); 1077 1078 if (ext2fs_fast_test_block_bitmap(ctx->block_found_map, block)) { 1079 if (!ctx->block_dup_map) { 1080 pctx.errcode = ext2fs_allocate_block_bitmap(ctx->fs, 1081 _("multiply claimed block map"), 1082 &ctx->block_dup_map); 1083 if (pctx.errcode) { 1084 pctx.num = 3; 1085 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, 1086 &pctx); 1087 /* Should never get here */ 1088 ctx->flags |= E2F_FLAG_ABORT; 1089 return; 1090 } 1091 } 1092 ext2fs_fast_mark_block_bitmap(ctx->block_dup_map, block); 1093 } else { 1094 ext2fs_fast_mark_block_bitmap(ctx->block_found_map, block); 1095 } 1096} 1097 1098/* 1099 * Adjust the extended attribute block's reference counts at the end 1100 * of pass 1, either by subtracting out references for EA blocks that 1101 * are still referenced in ctx->refcount, or by adding references for 1102 * EA blocks that had extra references as accounted for in 1103 * ctx->refcount_extra. 1104 */ 1105static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount, 1106 char *block_buf, int adjust_sign) 1107{ 1108 struct ext2_ext_attr_header *header; 1109 struct problem_context pctx; 1110 ext2_filsys fs = ctx->fs; 1111 blk_t blk; 1112 __u32 should_be; 1113 int count; 1114 1115 clear_problem_context(&pctx); 1116 1117 ea_refcount_intr_begin(refcount); 1118 while (1) { 1119 if ((blk = ea_refcount_intr_next(refcount, &count)) == 0) 1120 break; 1121 pctx.blk = blk; 1122 pctx.errcode = ext2fs_read_ext_attr(fs, blk, block_buf); 1123 if (pctx.errcode) { 1124 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx); 1125 return; 1126 } 1127 header = (struct ext2_ext_attr_header *) block_buf; 1128 pctx.blkcount = header->h_refcount; 1129 should_be = header->h_refcount + adjust_sign * count; 1130 pctx.num = should_be; 1131 if (fix_problem(ctx, PR_1_EXTATTR_REFCOUNT, &pctx)) { 1132 header->h_refcount = should_be; 1133 pctx.errcode = ext2fs_write_ext_attr(fs, blk, 1134 block_buf); 1135 if (pctx.errcode) { 1136 fix_problem(ctx, PR_1_EXTATTR_WRITE, &pctx); 1137 continue; 1138 } 1139 } 1140 } 1141} 1142 1143/* 1144 * Handle processing the extended attribute blocks 1145 */ 1146static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx, 1147 char *block_buf) 1148{ 1149 ext2_filsys fs = ctx->fs; 1150 ext2_ino_t ino = pctx->ino; 1151 struct ext2_inode *inode = pctx->inode; 1152 blk_t blk; 1153 char * end; 1154 struct ext2_ext_attr_header *header; 1155 struct ext2_ext_attr_entry *entry; 1156 int count; 1157 region_t region; 1158 1159 blk = inode->i_file_acl; 1160 if (blk == 0) 1161 return 0; 1162 1163 /* 1164 * If the Extended attribute flag isn't set, then a non-zero 1165 * file acl means that the inode is corrupted. 1166 * 1167 * Or if the extended attribute block is an invalid block, 1168 * then the inode is also corrupted. 1169 */ 1170 if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR) || 1171 (blk < fs->super->s_first_data_block) || 1172 (blk >= fs->super->s_blocks_count)) { 1173 mark_inode_bad(ctx, ino); 1174 return 0; 1175 } 1176 1177 /* If ea bitmap hasn't been allocated, create it */ 1178 if (!ctx->block_ea_map) { 1179 pctx->errcode = ext2fs_allocate_block_bitmap(fs, 1180 _("ext attr block map"), 1181 &ctx->block_ea_map); 1182 if (pctx->errcode) { 1183 pctx->num = 2; 1184 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, pctx); 1185 ctx->flags |= E2F_FLAG_ABORT; 1186 return 0; 1187 } 1188 } 1189 1190 /* Create the EA refcount structure if necessary */ 1191 if (!ctx->refcount) { 1192 pctx->errcode = ea_refcount_create(0, &ctx->refcount); 1193 if (pctx->errcode) { 1194 pctx->num = 1; 1195 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx); 1196 ctx->flags |= E2F_FLAG_ABORT; 1197 return 0; 1198 } 1199 } 1200 1201#if 0 1202 /* Debugging text */ 1203 printf("Inode %u has EA block %u\n", ino, blk); 1204#endif 1205 1206 /* Have we seen this EA block before? */ 1207 if (ext2fs_fast_test_block_bitmap(ctx->block_ea_map, blk)) { 1208 if (ea_refcount_decrement(ctx->refcount, blk, 0) == 0) 1209 return 1; 1210 /* Ooops, this EA was referenced more than it stated */ 1211 if (!ctx->refcount_extra) { 1212 pctx->errcode = ea_refcount_create(0, 1213 &ctx->refcount_extra); 1214 if (pctx->errcode) { 1215 pctx->num = 2; 1216 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx); 1217 ctx->flags |= E2F_FLAG_ABORT; 1218 return 0; 1219 } 1220 } 1221 ea_refcount_increment(ctx->refcount_extra, blk, 0); 1222 return 1; 1223 } 1224 1225 /* 1226 * OK, we haven't seen this EA block yet. So we need to 1227 * validate it 1228 */ 1229 pctx->blk = blk; 1230 pctx->errcode = ext2fs_read_ext_attr(fs, blk, block_buf); 1231 if (pctx->errcode && fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx)) 1232 goto clear_extattr; 1233 header = (struct ext2_ext_attr_header *) block_buf; 1234 pctx->blk = inode->i_file_acl; 1235 if (((ctx->ext_attr_ver == 1) && 1236 (header->h_magic != EXT2_EXT_ATTR_MAGIC_v1)) || 1237 ((ctx->ext_attr_ver == 2) && 1238 (header->h_magic != EXT2_EXT_ATTR_MAGIC))) { 1239 if (fix_problem(ctx, PR_1_BAD_EA_BLOCK, pctx)) 1240 goto clear_extattr; 1241 } 1242 1243 if (header->h_blocks != 1) { 1244 if (fix_problem(ctx, PR_1_EA_MULTI_BLOCK, pctx)) 1245 goto clear_extattr; 1246 } 1247 1248 region = region_create(0, fs->blocksize); 1249 if (!region) { 1250 fix_problem(ctx, PR_1_EA_ALLOC_REGION, pctx); 1251 ctx->flags |= E2F_FLAG_ABORT; 1252 return 0; 1253 } 1254 if (region_allocate(region, 0, sizeof(struct ext2_ext_attr_header))) { 1255 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx)) 1256 goto clear_extattr; 1257 } 1258 1259 entry = (struct ext2_ext_attr_entry *)(header+1); 1260 end = block_buf + fs->blocksize; 1261 while ((char *)entry < end && *(__u32 *)entry) { 1262 if (region_allocate(region, (char *)entry - (char *)header, 1263 EXT2_EXT_ATTR_LEN(entry->e_name_len))) { 1264 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx)) 1265 goto clear_extattr; 1266 } 1267 if ((ctx->ext_attr_ver == 1 && 1268 (entry->e_name_len == 0 || entry->e_name_index != 0)) || 1269 (ctx->ext_attr_ver == 2 && 1270 entry->e_name_index == 0)) { 1271 if (fix_problem(ctx, PR_1_EA_BAD_NAME, pctx)) 1272 goto clear_extattr; 1273 } 1274 if (entry->e_value_block != 0) { 1275 if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx)) 1276 goto clear_extattr; 1277 } 1278 if (entry->e_value_size && 1279 region_allocate(region, entry->e_value_offs, 1280 EXT2_EXT_ATTR_SIZE(entry->e_value_size))) { 1281 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx)) 1282 goto clear_extattr; 1283 } 1284 entry = EXT2_EXT_ATTR_NEXT(entry); 1285 } 1286 if (region_allocate(region, (char *)entry - (char *)header, 4)) { 1287 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx)) 1288 goto clear_extattr; 1289 } 1290 region_free(region); 1291 1292 count = header->h_refcount - 1; 1293 if (count) 1294 ea_refcount_store(ctx->refcount, blk, count); 1295 mark_block_used(ctx, blk); 1296 ext2fs_fast_mark_block_bitmap(ctx->block_ea_map, blk); 1297 1298 return 1; 1299 1300clear_extattr: 1301 inode->i_file_acl = 0; 1302 e2fsck_write_inode(ctx, ino, inode, "check_ext_attr"); 1303 return 0; 1304} 1305 1306/* Returns 1 if bad htree, 0 if OK */ 1307static int handle_htree(e2fsck_t ctx, struct problem_context *pctx, 1308 ext2_ino_t ino EXT2FS_ATTR((unused)), 1309 struct ext2_inode *inode, 1310 char *block_buf) 1311{ 1312 struct ext2_dx_root_info *root; 1313 ext2_filsys fs = ctx->fs; 1314 errcode_t retval; 1315 blk_t blk; 1316 1317 if ((!LINUX_S_ISDIR(inode->i_mode) && 1318 fix_problem(ctx, PR_1_HTREE_NODIR, pctx)) || 1319 (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) && 1320 fix_problem(ctx, PR_1_HTREE_SET, pctx))) 1321 return 1; 1322 1323 blk = inode->i_block[0]; 1324 if (((blk == 0) || 1325 (blk < fs->super->s_first_data_block) || 1326 (blk >= fs->super->s_blocks_count)) && 1327 fix_problem(ctx, PR_1_HTREE_BADROOT, pctx)) 1328 return 1; 1329 1330 retval = io_channel_read_blk(fs->io, blk, 1, block_buf); 1331 if (retval && fix_problem(ctx, PR_1_HTREE_BADROOT, pctx)) 1332 return 1; 1333 1334 /* XXX should check that beginning matches a directory */ 1335 root = (struct ext2_dx_root_info *) (block_buf + 24); 1336 1337 if ((root->reserved_zero || root->info_length < 8) && 1338 fix_problem(ctx, PR_1_HTREE_BADROOT, pctx)) 1339 return 1; 1340 1341 pctx->num = root->hash_version; 1342 if ((root->hash_version != EXT2_HASH_LEGACY) && 1343 (root->hash_version != EXT2_HASH_HALF_MD4) && 1344 (root->hash_version != EXT2_HASH_TEA) && 1345 fix_problem(ctx, PR_1_HTREE_HASHV, pctx)) 1346 return 1; 1347 1348 if ((root->unused_flags & EXT2_HASH_FLAG_INCOMPAT) && 1349 fix_problem(ctx, PR_1_HTREE_INCOMPAT, pctx)) 1350 return 1; 1351 1352 pctx->num = root->indirect_levels; 1353 if ((root->indirect_levels > 1) && 1354 fix_problem(ctx, PR_1_HTREE_DEPTH, pctx)) 1355 return 1; 1356 1357 return 0; 1358} 1359 1360/* 1361 * This subroutine is called on each inode to account for all of the 1362 * blocks used by that inode. 1363 */ 1364static void check_blocks(e2fsck_t ctx, struct problem_context *pctx, 1365 char *block_buf) 1366{ 1367 ext2_filsys fs = ctx->fs; 1368 struct process_block_struct pb; 1369 ext2_ino_t ino = pctx->ino; 1370 struct ext2_inode *inode = pctx->inode; 1371 int bad_size = 0; 1372 int dirty_inode = 0; 1373 __u64 size; 1374 1375 pb.ino = ino; 1376 pb.num_blocks = 0; 1377 pb.last_block = -1; 1378 pb.num_illegal_blocks = 0; 1379 pb.suppress = 0; pb.clear = 0; 1380 pb.fragmented = 0; 1381 pb.compressed = 0; 1382 pb.previous_block = 0; 1383 pb.is_dir = LINUX_S_ISDIR(inode->i_mode); 1384 pb.is_reg = LINUX_S_ISREG(inode->i_mode); 1385 pb.max_blocks = 1 << (31 - fs->super->s_log_block_size); 1386 pb.inode = inode; 1387 pb.pctx = pctx; 1388 pb.ctx = ctx; 1389 pctx->ino = ino; 1390 pctx->errcode = 0; 1391 1392 if (inode->i_flags & EXT2_COMPRBLK_FL) { 1393 if (fs->super->s_feature_incompat & 1394 EXT2_FEATURE_INCOMPAT_COMPRESSION) 1395 pb.compressed = 1; 1396 else { 1397 if (fix_problem(ctx, PR_1_COMPR_SET, pctx)) { 1398 inode->i_flags &= ~EXT2_COMPRBLK_FL; 1399 dirty_inode++; 1400 } 1401 } 1402 } 1403 1404 if (inode->i_file_acl && check_ext_attr(ctx, pctx, block_buf)) 1405 pb.num_blocks++; 1406 1407 if (ext2fs_inode_has_valid_blocks(inode)) 1408 pctx->errcode = ext2fs_block_iterate2(fs, ino, 1409 pb.is_dir ? BLOCK_FLAG_HOLE : 0, 1410 block_buf, process_block, &pb); 1411 end_problem_latch(ctx, PR_LATCH_BLOCK); 1412 end_problem_latch(ctx, PR_LATCH_TOOBIG); 1413 if (ctx->flags & E2F_FLAG_SIGNAL_MASK) 1414 goto out; 1415 if (pctx->errcode) 1416 fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx); 1417 1418 if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group) 1419 ctx->fs_fragmented++; 1420 1421 if (pb.clear) { 1422 inode->i_links_count = 0; 1423 ext2fs_icount_store(ctx->inode_link_info, ino, 0); 1424 inode->i_dtime = time(0); 1425 dirty_inode++; 1426 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino); 1427 ext2fs_unmark_inode_bitmap(ctx->inode_reg_map, ino); 1428 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino); 1429 /* 1430 * The inode was probably partially accounted for 1431 * before processing was aborted, so we need to 1432 * restart the pass 1 scan. 1433 */ 1434 ctx->flags |= E2F_FLAG_RESTART; 1435 goto out; 1436 } 1437 1438 if (inode->i_flags & EXT2_INDEX_FL) { 1439 if (handle_htree(ctx, pctx, ino, inode, block_buf)) { 1440 inode->i_flags &= ~EXT2_INDEX_FL; 1441 dirty_inode++; 1442 } else { 1443#ifdef ENABLE_HTREE 1444 e2fsck_add_dx_dir(ctx, ino, pb.last_block+1); 1445#endif 1446 } 1447 } 1448 if (ctx->dirs_to_hash && pb.is_dir && 1449 !(inode->i_flags & EXT2_INDEX_FL) && 1450 ((inode->i_size / fs->blocksize) >= 3)) 1451 ext2fs_u32_list_add(ctx->dirs_to_hash, ino); 1452 1453 if (!pb.num_blocks && pb.is_dir) { 1454 if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) { 1455 inode->i_links_count = 0; 1456 ext2fs_icount_store(ctx->inode_link_info, ino, 0); 1457 inode->i_dtime = time(0); 1458 dirty_inode++; 1459 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino); 1460 ext2fs_unmark_inode_bitmap(ctx->inode_reg_map, ino); 1461 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino); 1462 ctx->fs_directory_count--; 1463 goto out; 1464 } 1465 } 1466 1467 pb.num_blocks *= (fs->blocksize / 512); 1468#if 0 1469 printf("inode %u, i_size = %lu, last_block = %lld, i_blocks=%lu, num_blocks = %lu\n", 1470 ino, inode->i_size, pb.last_block, inode->i_blocks, 1471 pb.num_blocks); 1472#endif 1473 if (pb.is_dir) { 1474 int nblock = inode->i_size >> EXT2_BLOCK_SIZE_BITS(fs->super); 1475 if (nblock > (pb.last_block + 1)) 1476 bad_size = 1; 1477 else if (nblock < (pb.last_block + 1)) { 1478 if (((pb.last_block + 1) - nblock) > 1479 fs->super->s_prealloc_dir_blocks) 1480 bad_size = 2; 1481 } 1482 } else { 1483 size = EXT2_I_SIZE(inode); 1484 if ((pb.last_block >= 0) && 1485 (size < (__u64) pb.last_block * fs->blocksize)) 1486 bad_size = 3; 1487 else if (size > ext2_max_sizes[fs->super->s_log_block_size]) 1488 bad_size = 4; 1489 } 1490 /* i_size for symlinks is checked elsewhere */ 1491 if (bad_size && !LINUX_S_ISLNK(inode->i_mode)) { 1492 pctx->num = (pb.last_block+1) * fs->blocksize; 1493 if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) { 1494 inode->i_size = pctx->num; 1495 if (!LINUX_S_ISDIR(inode->i_mode)) 1496 inode->i_size_high = pctx->num >> 32; 1497 dirty_inode++; 1498 } 1499 pctx->num = 0; 1500 } 1501 if (LINUX_S_ISREG(inode->i_mode) && 1502 (inode->i_size_high || inode->i_size & 0x80000000UL)) 1503 ctx->large_files++; 1504 if (pb.num_blocks != inode->i_blocks) { 1505 pctx->num = pb.num_blocks; 1506 if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) { 1507 inode->i_blocks = pb.num_blocks; 1508 dirty_inode++; 1509 } 1510 pctx->num = 0; 1511 } 1512out: 1513 if (dirty_inode) 1514 e2fsck_write_inode(ctx, ino, inode, "check_blocks"); 1515} 1516 1517#if 0 1518/* 1519 * Helper function called by process block when an illegal block is 1520 * found. It returns a description about why the block is illegal 1521 */ 1522static char *describe_illegal_block(ext2_filsys fs, blk_t block) 1523{ 1524 blk_t super; 1525 int i; 1526 static char problem[80]; 1527 1528 super = fs->super->s_first_data_block; 1529 strcpy(problem, "PROGRAMMING ERROR: Unknown reason for illegal block"); 1530 if (block < super) { 1531 sprintf(problem, "< FIRSTBLOCK (%u)", super); 1532 return(problem); 1533 } else if (block >= fs->super->s_blocks_count) { 1534 sprintf(problem, "> BLOCKS (%u)", fs->super->s_blocks_count); 1535 return(problem); 1536 } 1537 for (i = 0; i < fs->group_desc_count; i++) { 1538 if (block == super) { 1539 sprintf(problem, "is the superblock in group %d", i); 1540 break; 1541 } 1542 if (block > super && 1543 block <= (super + fs->desc_blocks)) { 1544 sprintf(problem, "is in the group descriptors " 1545 "of group %d", i); 1546 break; 1547 } 1548 if (block == fs->group_desc[i].bg_block_bitmap) { 1549 sprintf(problem, "is the block bitmap of group %d", i); 1550 break; 1551 } 1552 if (block == fs->group_desc[i].bg_inode_bitmap) { 1553 sprintf(problem, "is the inode bitmap of group %d", i); 1554 break; 1555 } 1556 if (block >= fs->group_desc[i].bg_inode_table && 1557 (block < fs->group_desc[i].bg_inode_table 1558 + fs->inode_blocks_per_group)) { 1559 sprintf(problem, "is in the inode table of group %d", 1560 i); 1561 break; 1562 } 1563 super += fs->super->s_blocks_per_group; 1564 } 1565 return(problem); 1566} 1567#endif 1568 1569/* 1570 * This is a helper function for check_blocks(). 1571 */ 1572static int process_block(ext2_filsys fs, 1573 blk_t *block_nr, 1574 e2_blkcnt_t blockcnt, 1575 blk_t ref_block EXT2FS_ATTR((unused)), 1576 int ref_offset EXT2FS_ATTR((unused)), 1577 void *priv_data) 1578{ 1579 struct process_block_struct *p; 1580 struct problem_context *pctx; 1581 blk_t blk = *block_nr; 1582 int ret_code = 0; 1583 int problem = 0; 1584 e2fsck_t ctx; 1585 1586 p = (struct process_block_struct *) priv_data; 1587 pctx = p->pctx; 1588 ctx = p->ctx; 1589 1590 if (p->compressed && (blk == EXT2FS_COMPRESSED_BLKADDR)) { 1591 /* todo: Check that the comprblk_fl is high, that the 1592 blkaddr pattern looks right (all non-holes up to 1593 first EXT2FS_COMPRESSED_BLKADDR, then all 1594 EXT2FS_COMPRESSED_BLKADDR up to end of cluster), 1595 that the feature_incompat bit is high, and that the 1596 inode is a regular file. If we're doing a "full 1597 check" (a concept introduced to e2fsck by e2compr, 1598 meaning that we look at data blocks as well as 1599 metadata) then call some library routine that 1600 checks the compressed data. I'll have to think 1601 about this, because one particularly important 1602 problem to be able to fix is to recalculate the 1603 cluster size if necessary. I think that perhaps 1604 we'd better do most/all e2compr-specific checks 1605 separately, after the non-e2compr checks. If not 1606 doing a full check, it may be useful to test that 1607 the personality is linux; e.g. if it isn't then 1608 perhaps this really is just an illegal block. */ 1609 return 0; 1610 } 1611 1612 if (blk == 0) { 1613 if (p->is_dir == 0) { 1614 /* 1615 * Should never happen, since only directories 1616 * get called with BLOCK_FLAG_HOLE 1617 */ 1618#if DEBUG_E2FSCK 1619 printf("process_block() called with blk == 0, " 1620 "blockcnt=%d, inode %lu???\n", 1621 blockcnt, p->ino); 1622#endif 1623 return 0; 1624 } 1625 if (blockcnt < 0) 1626 return 0; 1627 if (blockcnt * fs->blocksize < p->inode->i_size) { 1628#if 0 1629 printf("Missing block (#%d) in directory inode %lu!\n", 1630 blockcnt, p->ino); 1631#endif 1632 goto mark_dir; 1633 } 1634 return 0; 1635 } 1636 1637#if 0 1638 printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk, 1639 blockcnt); 1640#endif 1641 1642 /* 1643 * Simplistic fragmentation check. We merely require that the 1644 * file be contiguous. (Which can never be true for really 1645 * big files that are greater than a block group.) 1646 */ 1647 if (!HOLE_BLKADDR(p->previous_block)) { 1648 if (p->previous_block+1 != blk) 1649 p->fragmented = 1; 1650 } 1651 p->previous_block = blk; 1652 1653 if (p->is_dir && blockcnt > (1 << (21 - fs->super->s_log_block_size))) 1654 problem = PR_1_TOOBIG_DIR; 1655 if (p->is_reg && p->num_blocks+1 >= p->max_blocks) 1656 problem = PR_1_TOOBIG_REG; 1657 if (!p->is_dir && !p->is_reg && blockcnt > 0) 1658 problem = PR_1_TOOBIG_SYMLINK; 1659 1660 if (blk < fs->super->s_first_data_block || 1661 blk >= fs->super->s_blocks_count) 1662 problem = PR_1_ILLEGAL_BLOCK_NUM; 1663 1664 if (problem) { 1665 p->num_illegal_blocks++; 1666 if (!p->suppress && (p->num_illegal_blocks % 12) == 0) { 1667 if (fix_problem(ctx, PR_1_TOO_MANY_BAD_BLOCKS, pctx)) { 1668 p->clear = 1; 1669 return BLOCK_ABORT; 1670 } 1671 if (fix_problem(ctx, PR_1_SUPPRESS_MESSAGES, pctx)) { 1672 p->suppress = 1; 1673 set_latch_flags(PR_LATCH_BLOCK, 1674 PRL_SUPPRESS, 0); 1675 } 1676 } 1677 pctx->blk = blk; 1678 pctx->blkcount = blockcnt; 1679 if (fix_problem(ctx, problem, pctx)) { 1680 blk = *block_nr = 0; 1681 ret_code = BLOCK_CHANGED; 1682 goto mark_dir; 1683 } else 1684 return 0; 1685 } 1686 1687 if (p->ino == EXT2_RESIZE_INO) { 1688 /* 1689 * The resize inode has already be sanity checked 1690 * during pass #0 (the superblock checks). All we 1691 * have to do is mark the double indirect block as 1692 * being in use; all of the other blocks are handled 1693 * by mark_table_blocks()). 1694 */ 1695 if (blockcnt == BLOCK_COUNT_DIND) 1696 mark_block_used(ctx, blk); 1697 } else 1698 mark_block_used(ctx, blk); 1699 p->num_blocks++; 1700 if (blockcnt >= 0) 1701 p->last_block = blockcnt; 1702mark_dir: 1703 if (p->is_dir && (blockcnt >= 0)) { 1704 pctx->errcode = ext2fs_add_dir_block(fs->dblist, p->ino, 1705 blk, blockcnt); 1706 if (pctx->errcode) { 1707 pctx->blk = blk; 1708 pctx->num = blockcnt; 1709 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx); 1710 /* Should never get here */ 1711 ctx->flags |= E2F_FLAG_ABORT; 1712 return BLOCK_ABORT; 1713 } 1714 } 1715 return ret_code; 1716} 1717 1718static int process_bad_block(ext2_filsys fs, 1719 blk_t *block_nr, 1720 e2_blkcnt_t blockcnt, 1721 blk_t ref_block EXT2FS_ATTR((unused)), 1722 int ref_offset EXT2FS_ATTR((unused)), 1723 void *priv_data) 1724{ 1725 struct process_block_struct *p; 1726 blk_t blk = *block_nr; 1727 blk_t first_block; 1728 dgrp_t i; 1729 struct problem_context *pctx; 1730 e2fsck_t ctx; 1731 1732 /* 1733 * Note: This function processes blocks for the bad blocks 1734 * inode, which is never compressed. So we don't use HOLE_BLKADDR(). 1735 */ 1736 1737 if (!blk) 1738 return 0; 1739 1740 p = (struct process_block_struct *) priv_data; 1741 ctx = p->ctx; 1742 pctx = p->pctx; 1743 1744 pctx->ino = EXT2_BAD_INO; 1745 pctx->blk = blk; 1746 pctx->blkcount = blockcnt; 1747 1748 if ((blk < fs->super->s_first_data_block) || 1749 (blk >= fs->super->s_blocks_count)) { 1750 if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) { 1751 *block_nr = 0; 1752 return BLOCK_CHANGED; 1753 } else 1754 return 0; 1755 } 1756 1757 if (blockcnt < 0) { 1758 if (ext2fs_test_block_bitmap(p->fs_meta_blocks, blk)) { 1759 p->bbcheck = 1; 1760 if (fix_problem(ctx, PR_1_BB_FS_BLOCK, pctx)) { 1761 *block_nr = 0; 1762 return BLOCK_CHANGED; 1763 } 1764 } else if (ext2fs_test_block_bitmap(ctx->block_found_map, 1765 blk)) { 1766 p->bbcheck = 1; 1767 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, 1768 pctx)) { 1769 *block_nr = 0; 1770 return BLOCK_CHANGED; 1771 } 1772 if (ctx->flags & E2F_FLAG_SIGNAL_MASK) 1773 return BLOCK_ABORT; 1774 } else 1775 mark_block_used(ctx, blk); 1776 return 0; 1777 } 1778#if 0 1779 printf ("DEBUG: Marking %u as bad.\n", blk); 1780#endif 1781 ctx->fs_badblocks_count++; 1782 /* 1783 * If the block is not used, then mark it as used and return. 1784 * If it is already marked as found, this must mean that 1785 * there's an overlap between the filesystem table blocks 1786 * (bitmaps and inode table) and the bad block list. 1787 */ 1788 if (!ext2fs_test_block_bitmap(ctx->block_found_map, blk)) { 1789 ext2fs_mark_block_bitmap(ctx->block_found_map, blk); 1790 return 0; 1791 } 1792 /* 1793 * Try to find the where the filesystem block was used... 1794 */ 1795 first_block = fs->super->s_first_data_block; 1796 1797 for (i = 0; i < fs->group_desc_count; i++ ) { 1798 pctx->group = i; 1799 pctx->blk = blk; 1800 if (!ext2fs_bg_has_super(fs, i)) 1801 goto skip_super; 1802 if (blk == first_block) { 1803 if (i == 0) { 1804 if (fix_problem(ctx, 1805 PR_1_BAD_PRIMARY_SUPERBLOCK, 1806 pctx)) { 1807 *block_nr = 0; 1808 return BLOCK_CHANGED; 1809 } 1810 return 0; 1811 } 1812 fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx); 1813 return 0; 1814 } 1815 if ((blk > first_block) && 1816 (blk <= first_block + fs->desc_blocks)) { 1817 if (i == 0) { 1818 pctx->blk = *block_nr; 1819 if (fix_problem(ctx, 1820 PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) { 1821 *block_nr = 0; 1822 return BLOCK_CHANGED; 1823 } 1824 return 0; 1825 } 1826 fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx); 1827 return 0; 1828 } 1829 skip_super: 1830 if (blk == fs->group_desc[i].bg_block_bitmap) { 1831 if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) { 1832 ctx->invalid_block_bitmap_flag[i]++; 1833 ctx->invalid_bitmaps++; 1834 } 1835 return 0; 1836 } 1837 if (blk == fs->group_desc[i].bg_inode_bitmap) { 1838 if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) { 1839 ctx->invalid_inode_bitmap_flag[i]++; 1840 ctx->invalid_bitmaps++; 1841 } 1842 return 0; 1843 } 1844 if ((blk >= fs->group_desc[i].bg_inode_table) && 1845 (blk < (fs->group_desc[i].bg_inode_table + 1846 fs->inode_blocks_per_group))) { 1847 /* 1848 * If there are bad blocks in the inode table, 1849 * the inode scan code will try to do 1850 * something reasonable automatically. 1851 */ 1852 return 0; 1853 } 1854 first_block += fs->super->s_blocks_per_group; 1855 } 1856 /* 1857 * If we've gotten to this point, then the only 1858 * possibility is that the bad block inode meta data 1859 * is using a bad block. 1860 */ 1861 if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) || 1862 (blk == p->inode->i_block[EXT2_DIND_BLOCK]) || 1863 (blk == p->inode->i_block[EXT2_TIND_BLOCK])) { 1864 p->bbcheck = 1; 1865 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, pctx)) { 1866 *block_nr = 0; 1867 return BLOCK_CHANGED; 1868 } 1869 if (ctx->flags & E2F_FLAG_SIGNAL_MASK) 1870 return BLOCK_ABORT; 1871 return 0; 1872 } 1873 1874 pctx->group = -1; 1875 1876 /* Warn user that the block wasn't claimed */ 1877 fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx); 1878 1879 return 0; 1880} 1881 1882static void new_table_block(e2fsck_t ctx, blk_t first_block, int group, 1883 const char *name, int num, blk_t *new_block) 1884{ 1885 ext2_filsys fs = ctx->fs; 1886 blk_t old_block = *new_block; 1887 int i; 1888 char *buf; 1889 struct problem_context pctx; 1890 1891 clear_problem_context(&pctx); 1892 1893 pctx.group = group; 1894 pctx.blk = old_block; 1895 pctx.str = name; 1896 1897 pctx.errcode = ext2fs_get_free_blocks(fs, first_block, 1898 first_block + fs->super->s_blocks_per_group, 1899 num, ctx->block_found_map, new_block); 1900 if (pctx.errcode) { 1901 pctx.num = num; 1902 fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx); 1903 ext2fs_unmark_valid(fs); 1904 return; 1905 } 1906 pctx.errcode = ext2fs_get_mem(fs->blocksize, &buf); 1907 if (pctx.errcode) { 1908 fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx); 1909 ext2fs_unmark_valid(fs); 1910 return; 1911 } 1912 ext2fs_mark_super_dirty(fs); 1913 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY; 1914 pctx.blk2 = *new_block; 1915 fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO : 1916 PR_1_RELOC_TO), &pctx); 1917 pctx.blk2 = 0; 1918 for (i = 0; i < num; i++) { 1919 pctx.blk = i; 1920 ext2fs_mark_block_bitmap(ctx->block_found_map, (*new_block)+i); 1921 if (old_block) { 1922 pctx.errcode = io_channel_read_blk(fs->io, 1923 old_block + i, 1, buf); 1924 if (pctx.errcode) 1925 fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx); 1926 } else 1927 memset(buf, 0, fs->blocksize); 1928 1929 pctx.blk = (*new_block) + i; 1930 pctx.errcode = io_channel_write_blk(fs->io, pctx.blk, 1931 1, buf); 1932 if (pctx.errcode) 1933 fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx); 1934 } 1935 ext2fs_free_mem(&buf); 1936} 1937 1938/* 1939 * This routine gets called at the end of pass 1 if bad blocks are 1940 * detected in the superblock, group descriptors, inode_bitmaps, or 1941 * block bitmaps. At this point, all of the blocks have been mapped 1942 * out, so we can try to allocate new block(s) to replace the bad 1943 * blocks. 1944 */ 1945static void handle_fs_bad_blocks(e2fsck_t ctx) 1946{ 1947 ext2_filsys fs = ctx->fs; 1948 dgrp_t i; 1949 int first_block = fs->super->s_first_data_block; 1950 1951 for (i = 0; i < fs->group_desc_count; i++) { 1952 if (ctx->invalid_block_bitmap_flag[i]) { 1953 new_table_block(ctx, first_block, i, _("block bitmap"), 1954 1, &fs->group_desc[i].bg_block_bitmap); 1955 } 1956 if (ctx->invalid_inode_bitmap_flag[i]) { 1957 new_table_block(ctx, first_block, i, _("inode bitmap"), 1958 1, &fs->group_desc[i].bg_inode_bitmap); 1959 } 1960 if (ctx->invalid_inode_table_flag[i]) { 1961 new_table_block(ctx, first_block, i, _("inode table"), 1962 fs->inode_blocks_per_group, 1963 &fs->group_desc[i].bg_inode_table); 1964 ctx->flags |= E2F_FLAG_RESTART; 1965 } 1966 first_block += fs->super->s_blocks_per_group; 1967 } 1968 ctx->invalid_bitmaps = 0; 1969} 1970 1971/* 1972 * This routine marks all blocks which are used by the superblock, 1973 * group descriptors, inode bitmaps, and block bitmaps. 1974 */ 1975static void mark_table_blocks(e2fsck_t ctx) 1976{ 1977 ext2_filsys fs = ctx->fs; 1978 blk_t block, b; 1979 dgrp_t i; 1980 int j; 1981 struct problem_context pctx; 1982 1983 clear_problem_context(&pctx); 1984 1985 block = fs->super->s_first_data_block; 1986 for (i = 0; i < fs->group_desc_count; i++) { 1987 pctx.group = i; 1988 1989 ext2fs_reserve_super_and_bgd(fs, i, ctx->block_found_map); 1990 1991 /* 1992 * Mark the blocks used for the inode table 1993 */ 1994 if (fs->group_desc[i].bg_inode_table) { 1995 for (j = 0, b = fs->group_desc[i].bg_inode_table; 1996 j < fs->inode_blocks_per_group; 1997 j++, b++) { 1998 if (ext2fs_test_block_bitmap(ctx->block_found_map, 1999 b)) { 2000 pctx.blk = b; 2001 if (fix_problem(ctx, 2002 PR_1_ITABLE_CONFLICT, &pctx)) { 2003 ctx->invalid_inode_table_flag[i]++; 2004 ctx->invalid_bitmaps++; 2005 } 2006 } else { 2007 ext2fs_mark_block_bitmap(ctx->block_found_map, 2008 b); 2009 } 2010 } 2011 } 2012 2013 /* 2014 * Mark block used for the block bitmap 2015 */ 2016 if (fs->group_desc[i].bg_block_bitmap) { 2017 if (ext2fs_test_block_bitmap(ctx->block_found_map, 2018 fs->group_desc[i].bg_block_bitmap)) { 2019 pctx.blk = fs->group_desc[i].bg_block_bitmap; 2020 if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) { 2021 ctx->invalid_block_bitmap_flag[i]++; 2022 ctx->invalid_bitmaps++; 2023 } 2024 } else { 2025 ext2fs_mark_block_bitmap(ctx->block_found_map, 2026 fs->group_desc[i].bg_block_bitmap); 2027 } 2028 2029 } 2030 /* 2031 * Mark block used for the inode bitmap 2032 */ 2033 if (fs->group_desc[i].bg_inode_bitmap) { 2034 if (ext2fs_test_block_bitmap(ctx->block_found_map, 2035 fs->group_desc[i].bg_inode_bitmap)) { 2036 pctx.blk = fs->group_desc[i].bg_inode_bitmap; 2037 if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) { 2038 ctx->invalid_inode_bitmap_flag[i]++; 2039 ctx->invalid_bitmaps++; 2040 } 2041 } else { 2042 ext2fs_mark_block_bitmap(ctx->block_found_map, 2043 fs->group_desc[i].bg_inode_bitmap); 2044 } 2045 } 2046 block += fs->super->s_blocks_per_group; 2047 } 2048} 2049 2050/* 2051 * Thes subroutines short circuits ext2fs_get_blocks and 2052 * ext2fs_check_directory; we use them since we already have the inode 2053 * structure, so there's no point in letting the ext2fs library read 2054 * the inode again. 2055 */ 2056static errcode_t pass1_get_blocks(ext2_filsys fs, ext2_ino_t ino, 2057 blk_t *blocks) 2058{ 2059 e2fsck_t ctx = (e2fsck_t) fs->priv_data; 2060 int i; 2061 2062 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode) 2063 return EXT2_ET_CALLBACK_NOTHANDLED; 2064 2065 for (i=0; i < EXT2_N_BLOCKS; i++) 2066 blocks[i] = ctx->stashed_inode->i_block[i]; 2067 return 0; 2068} 2069 2070static errcode_t pass1_read_inode(ext2_filsys fs, ext2_ino_t ino, 2071 struct ext2_inode *inode) 2072{ 2073 e2fsck_t ctx = (e2fsck_t) fs->priv_data; 2074 2075 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode) 2076 return EXT2_ET_CALLBACK_NOTHANDLED; 2077 *inode = *ctx->stashed_inode; 2078 return 0; 2079} 2080 2081static errcode_t pass1_write_inode(ext2_filsys fs, ext2_ino_t ino, 2082 struct ext2_inode *inode) 2083{ 2084 e2fsck_t ctx = (e2fsck_t) fs->priv_data; 2085 2086 if ((ino == ctx->stashed_ino) && ctx->stashed_inode) 2087 *ctx->stashed_inode = *inode; 2088 return EXT2_ET_CALLBACK_NOTHANDLED; 2089} 2090 2091static errcode_t pass1_check_directory(ext2_filsys fs, ext2_ino_t ino) 2092{ 2093 e2fsck_t ctx = (e2fsck_t) fs->priv_data; 2094 2095 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode) 2096 return EXT2_ET_CALLBACK_NOTHANDLED; 2097 2098 if (!LINUX_S_ISDIR(ctx->stashed_inode->i_mode)) 2099 return EXT2_ET_NO_DIRECTORY; 2100 return 0; 2101} 2102 2103void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int bool) 2104{ 2105 ext2_filsys fs = ctx->fs; 2106 2107 if (bool) { 2108 fs->get_blocks = pass1_get_blocks; 2109 fs->check_directory = pass1_check_directory; 2110 fs->read_inode = pass1_read_inode; 2111 fs->write_inode = pass1_write_inode; 2112 ctx->stashed_ino = 0; 2113 } else { 2114 fs->get_blocks = 0; 2115 fs->check_directory = 0; 2116 fs->read_inode = 0; 2117 fs->write_inode = 0; 2118 } 2119} 2120