pass1.c revision 1b6bf1759af884957234b7dce768b785f792abd0
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 have bad fields. (inode_bad_map) 23 * - A bitmap of which inodes are in bad blocks. (inode_bb_map) 24 * - A bitmap of which blocks are in use. (block_found_map) 25 * - A bitmap of which blocks are in use by two inodes (block_dup_map) 26 * - The data blocks of the directory inodes. (dir_map) 27 * 28 * Pass 1 is designed to stash away enough information so that the 29 * other passes should not need to read in the inode information 30 * during the normal course of a filesystem check. (Althogh if an 31 * inconsistency is detected, other passes may need to read in an 32 * inode to fix it.) 33 * 34 * Note that pass 1B will be invoked if there are any duplicate blocks 35 * found. 36 */ 37 38#include <time.h> 39#ifdef HAVE_ERRNO_H 40#include <errno.h> 41#endif 42 43#include "e2fsck.h" 44#include "problem.h" 45 46#ifdef NO_INLINE_FUNCS 47#define _INLINE_ 48#else 49#define _INLINE_ inline 50#endif 51 52static int process_block(ext2_filsys fs, blk_t *blocknr, 53 int blockcnt, blk_t ref_blk, 54 int ref_offset, void *private); 55static int process_bad_block(ext2_filsys fs, blk_t *block_nr, 56 int blockcnt, blk_t ref_blk, 57 int ref_offset, void *private); 58static void check_blocks(e2fsck_t ctx, struct problem_context *pctx, 59 char *block_buf); 60static void mark_table_blocks(e2fsck_t ctx); 61static void alloc_bad_map(e2fsck_t ctx); 62static void alloc_bb_map(e2fsck_t ctx); 63static void handle_fs_bad_blocks(e2fsck_t ctx); 64static void process_inodes(e2fsck_t ctx, char *block_buf); 65static int process_inode_cmp(const void *a, const void *b); 66static errcode_t scan_callback(ext2_filsys fs, ext2_inode_scan scan, 67 dgrp_t group, void * private); 68/* static char *describe_illegal_block(ext2_filsys fs, blk_t block); */ 69 70struct process_block_struct { 71 ino_t ino; 72 int is_dir:1, clear:1, suppress:1, fragmented:1; 73 int num_blocks; 74 int last_block; 75 int num_illegal_blocks; 76 blk_t previous_block; 77 struct ext2_inode *inode; 78 struct problem_context *pctx; 79 e2fsck_t ctx; 80}; 81 82struct process_inode_block { 83 ino_t ino; 84 struct ext2_inode inode; 85}; 86 87/* 88 * For the inodes to process list. 89 */ 90static struct process_inode_block *inodes_to_process; 91static int process_inode_count; 92 93/* 94 * Free all memory allocated by pass1 in preparation for restarting 95 * things. 96 */ 97static void unwind_pass1(ext2_filsys fs) 98{ 99 free(inodes_to_process);inodes_to_process = 0; 100} 101 102/* 103 * Check to make sure a device inode is real. Returns 1 if the device 104 * checks out, 0 if not. 105 */ 106int e2fsck_pass1_check_device_inode(struct ext2_inode *inode) 107{ 108 int i; 109 110 for (i=4; i < EXT2_N_BLOCKS; i++) 111 if (inode->i_block[i]) 112 return 0; 113 return 1; 114} 115 116void pass1(e2fsck_t ctx) 117{ 118 ext2_filsys fs = ctx->fs; 119 ino_t ino; 120 struct ext2_inode inode; 121 ext2_inode_scan scan; 122 char *block_buf; 123 struct resource_track rtrack; 124 unsigned char frag, fsize; 125 struct problem_context pctx; 126 127 init_resource_track(&rtrack); 128 clear_problem_context(&pctx); 129 130 if (!(ctx->options & E2F_OPT_PREEN)) 131 fix_problem(ctx, PR_1_PASS_HEADER, &pctx); 132 133#ifdef MTRACE 134 mtrace_print("Pass 1"); 135#endif 136 137 /* 138 * Allocate bitmaps structures 139 */ 140 pctx.errcode = ext2fs_allocate_inode_bitmap(fs, "in-use inode map", 141 &ctx->inode_used_map); 142 if (pctx.errcode) { 143 pctx.num = 1; 144 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx); 145 fatal_error(0); 146 } 147 pctx.errcode = ext2fs_allocate_inode_bitmap(fs, "directory inode map", 148 &ctx->inode_dir_map); 149 if (pctx.errcode) { 150 pctx.num = 2; 151 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx); 152 fatal_error(0); 153 } 154 pctx.errcode = ext2fs_allocate_block_bitmap(fs, "in-use block map", 155 &ctx->block_found_map); 156 if (pctx.errcode) { 157 pctx.num = 1; 158 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx); 159 fatal_error(0); 160 } 161 pctx.errcode = ext2fs_allocate_block_bitmap(fs, "illegal block map", 162 &ctx->block_illegal_map); 163 if (pctx.errcode) { 164 pctx.num = 2; 165 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx); 166 fatal_error(0); 167 } 168 pctx.errcode = ext2fs_create_icount2(fs, 0, 0, 0, 169 &ctx->inode_link_info); 170 if (pctx.errcode) { 171 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx); 172 fatal_error(0); 173 } 174 inodes_to_process = allocate_memory(ctx->process_inode_size * 175 sizeof(struct process_inode_block), 176 "array of inodes to process"); 177 process_inode_count = 0; 178 179 pctx.errcode = ext2fs_init_dblist(fs, 0); 180 if (pctx.errcode) { 181 fix_problem(ctx, PR_1_ALLOCATE_DBCOUNT, &pctx); 182 fatal_error(0); 183 } 184 185 mark_table_blocks(ctx); 186 block_buf = allocate_memory(fs->blocksize * 3, "block interate buffer"); 187 fs->get_blocks = pass1_get_blocks; 188 fs->check_directory = pass1_check_directory; 189 fs->read_inode = pass1_read_inode; 190 fs->write_inode = pass1_write_inode; 191 ehandler_operation("doing inode scan"); 192 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks, 193 &scan); 194 if (pctx.errcode) { 195 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx); 196 fatal_error(0); 197 } 198 ext2fs_inode_scan_flags(scan, EXT2_SF_SKIP_MISSING_ITABLE, 0); 199 pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode); 200 if (pctx.errcode) { 201 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx); 202 fatal_error(0); 203 } 204 ctx->stashed_inode = &inode; 205 ext2fs_set_inode_callback(scan, scan_callback, block_buf); 206 while (ino) { 207 pctx.ino = ino; 208 pctx.inode = &inode; 209 ctx->stashed_ino = ino; 210 if (inode.i_links_count) { 211 pctx.errcode = ext2fs_icount_store(ctx->inode_link_info, 212 ino, inode.i_links_count); 213 if (pctx.errcode) { 214 pctx.num = inode.i_links_count; 215 fix_problem(ctx, PR_1_ICOUNT_STORE, &pctx); 216 fatal_error(0); 217 } 218 } 219 if (ino == EXT2_BAD_INO) { 220 struct process_block_struct pb; 221 222 pb.ino = EXT2_BAD_INO; 223 pb.num_blocks = pb.last_block = 0; 224 pb.num_illegal_blocks = 0; 225 pb.suppress = 0; pb.clear = 0; pb.is_dir = 0; 226 pb.fragmented = 0; 227 pb.inode = &inode; 228 pb.pctx = &pctx; 229 pb.ctx = ctx; 230 pctx.errcode = ext2fs_block_iterate2(fs, ino, 0, 231 block_buf, process_bad_block, &pb); 232 if (pctx.errcode) { 233 fix_problem(ctx, PR_1_BLOCK_ITERATE, &pctx); 234 fatal_error(0); 235 } 236 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino); 237 clear_problem_context(&pctx); 238 goto next; 239 } 240 if (ino == EXT2_ROOT_INO) { 241 /* 242 * Make sure the root inode is a directory; if 243 * not, offer to clear it. It will be 244 * regnerated in pass #3. 245 */ 246 if (!LINUX_S_ISDIR(inode.i_mode)) { 247 if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx)) { 248 inode.i_dtime = time(0); 249 inode.i_links_count = 0; 250 ext2fs_icount_store(ctx->inode_link_info, 251 ino, 0); 252 e2fsck_write_inode(fs, ino, &inode, 253 "pass1"); 254 } 255 } 256 /* 257 * If dtime is set, offer to clear it. mke2fs 258 * version 0.2b created filesystems with the 259 * dtime field set for the root and lost+found 260 * directories. We won't worry about 261 * /lost+found, since that can be regenerated 262 * easily. But we will fix the root directory 263 * as a special case. 264 */ 265 if (inode.i_dtime && inode.i_links_count) { 266 if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) { 267 inode.i_dtime = 0; 268 e2fsck_write_inode(fs, ino, &inode, 269 "pass1"); 270 } 271 } 272 } 273 if (ino == EXT2_BOOT_LOADER_INO) { 274 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino); 275 check_blocks(ctx, &pctx, block_buf); 276 goto next; 277 } 278 if ((ino != EXT2_ROOT_INO) && 279 (ino < EXT2_FIRST_INODE(fs->super))) { 280 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino); 281 if (inode.i_mode != 0) { 282 if (fix_problem(ctx, 283 PR_1_RESERVED_BAD_MODE, &pctx)) { 284 inode.i_mode = 0; 285 e2fsck_write_inode(fs, ino, &inode, 286 "pass1"); 287 } 288 } 289 check_blocks(ctx, &pctx, block_buf); 290 goto next; 291 } 292 /* 293 * This code assumes that deleted inodes have 294 * i_links_count set to 0. 295 */ 296 if (!inode.i_links_count) { 297 if (!inode.i_dtime && inode.i_mode) { 298 if (fix_problem(ctx, 299 PR_1_ZERO_DTIME, &pctx)) { 300 inode.i_dtime = time(0); 301 e2fsck_write_inode(fs, ino, &inode, 302 "pass1"); 303 } 304 } 305 goto next; 306 } 307 /* 308 * n.b. 0.3c ext2fs code didn't clear i_links_count for 309 * deleted files. Oops. 310 * 311 * Since all new ext2 implementations get this right, 312 * we now assume that the case of non-zero 313 * i_links_count and non-zero dtime means that we 314 * should keep the file, not delete it. 315 * 316 */ 317 if (inode.i_dtime) { 318 if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) { 319 inode.i_dtime = 0; 320 e2fsck_write_inode(fs, ino, &inode, "pass1"); 321 } 322 } 323 324 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino); 325 switch (fs->super->s_creator_os) { 326 case EXT2_OS_LINUX: 327 frag = inode.osd2.linux2.l_i_frag; 328 fsize = inode.osd2.linux2.l_i_fsize; 329 break; 330 case EXT2_OS_HURD: 331 frag = inode.osd2.hurd2.h_i_frag; 332 fsize = inode.osd2.hurd2.h_i_fsize; 333 break; 334 case EXT2_OS_MASIX: 335 frag = inode.osd2.masix2.m_i_frag; 336 fsize = inode.osd2.masix2.m_i_fsize; 337 break; 338 default: 339 frag = fsize = 0; 340 } 341 342 if (inode.i_faddr || frag || fsize 343 || inode.i_file_acl || inode.i_dir_acl) { 344 if (!ctx->inode_bad_map) 345 alloc_bad_map(ctx); 346 ext2fs_mark_inode_bitmap(ctx->inode_bad_map, ino); 347 } 348 349 if (LINUX_S_ISDIR(inode.i_mode)) { 350 ext2fs_mark_inode_bitmap(ctx->inode_dir_map, ino); 351 add_dir_info(fs, ino, 0); 352 ctx->fs_directory_count++; 353 } else if (LINUX_S_ISREG (inode.i_mode)) 354 ctx->fs_regular_count++; 355 else if (LINUX_S_ISCHR (inode.i_mode) && 356 e2fsck_pass1_check_device_inode(&inode)) 357 ctx->fs_chardev_count++; 358 else if (LINUX_S_ISBLK (inode.i_mode) && 359 e2fsck_pass1_check_device_inode(&inode)) 360 ctx->fs_blockdev_count++; 361 else if (LINUX_S_ISLNK (inode.i_mode)) { 362 ctx->fs_symlinks_count++; 363 if (!inode.i_blocks) { 364 ctx->fs_fast_symlinks_count++; 365 goto next; 366 } 367 } 368 else if (LINUX_S_ISFIFO (inode.i_mode)) 369 ctx->fs_fifo_count++; 370 else if (LINUX_S_ISSOCK (inode.i_mode)) 371 ctx->fs_sockets_count++; 372 else { 373 if (!ctx->inode_bad_map) 374 alloc_bad_map(ctx); 375 ext2fs_mark_inode_bitmap(ctx->inode_bad_map, ino); 376 } 377 if (inode.i_block[EXT2_IND_BLOCK]) 378 ctx->fs_ind_count++; 379 if (inode.i_block[EXT2_DIND_BLOCK]) 380 ctx->fs_dind_count++; 381 if (inode.i_block[EXT2_TIND_BLOCK]) 382 ctx->fs_tind_count++; 383 if (inode.i_block[EXT2_IND_BLOCK] || 384 inode.i_block[EXT2_DIND_BLOCK] || 385 inode.i_block[EXT2_TIND_BLOCK]) { 386 inodes_to_process[process_inode_count].ino = ino; 387 inodes_to_process[process_inode_count].inode = inode; 388 process_inode_count++; 389 } else 390 check_blocks(ctx, &pctx, block_buf); 391 392 if (process_inode_count >= ctx->process_inode_size) 393 process_inodes(ctx, block_buf); 394 next: 395 pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode); 396 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) { 397 if (!ctx->inode_bb_map) 398 alloc_bb_map(ctx); 399 ext2fs_mark_inode_bitmap(ctx->inode_bb_map, ino); 400 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino); 401 goto next; 402 } 403 if (pctx.errcode) { 404 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx); 405 fatal_error(0); 406 } 407 } 408 process_inodes(ctx, block_buf); 409 ext2fs_close_inode_scan(scan); 410 ehandler_operation(0); 411 412 if (ctx->invalid_bitmaps) 413 handle_fs_bad_blocks(ctx); 414 415 if (restart_e2fsck) { 416 unwind_pass1(fs); 417 goto endit; 418 } 419 420 if (ctx->block_dup_map) { 421 if (ctx->options & E2F_OPT_PREEN) { 422 clear_problem_context(&pctx); 423 fix_problem(ctx, PR_1_DUP_BLOCKS_PREENSTOP, &pctx); 424 } 425 pass1_dupblocks(ctx, block_buf); 426 } 427 free(inodes_to_process); 428endit: 429 fs->get_blocks = 0; 430 fs->check_directory = 0; 431 fs->read_inode = 0; 432 fs->write_inode = 0; 433 434 free(block_buf); 435 ext2fs_free_block_bitmap(ctx->block_illegal_map); 436 ctx->block_illegal_map = 0; 437 438 if (ctx->options & E2F_OPT_TIME2) 439 print_resource_track("Pass 1", &rtrack); 440} 441 442/* 443 * When the inode_scan routines call this callback at the end of the 444 * glock group, call process_inodes. 445 */ 446static errcode_t scan_callback(ext2_filsys fs, ext2_inode_scan scan, 447 dgrp_t group, void * private) 448{ 449 process_inodes((e2fsck_t) fs->private, (char *) private); 450 return 0; 451} 452 453/* 454 * Process the inodes in the "inodes to process" list. 455 */ 456static void process_inodes(e2fsck_t ctx, char *block_buf) 457{ 458 int i; 459 struct ext2_inode *old_stashed_inode; 460 ino_t old_stashed_ino; 461 const char *old_operation; 462 char buf[80]; 463 struct problem_context pctx; 464 465#if 0 466 printf("begin process_inodes: "); 467#endif 468 old_operation = ehandler_operation(0); 469 old_stashed_inode = ctx->stashed_inode; 470 old_stashed_ino = ctx->stashed_ino; 471 qsort(inodes_to_process, process_inode_count, 472 sizeof(struct process_inode_block), process_inode_cmp); 473 clear_problem_context(&pctx); 474 for (i=0; i < process_inode_count; i++) { 475 pctx.inode = ctx->stashed_inode = &inodes_to_process[i].inode; 476 pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino; 477 478#if 0 479 printf("%u ", pctx.ino); 480#endif 481 sprintf(buf, "reading indirect blocks of inode %lu", pctx.ino); 482 ehandler_operation(buf); 483 check_blocks(ctx, &pctx, block_buf); 484 } 485 ctx->stashed_inode = old_stashed_inode; 486 ctx->stashed_ino = old_stashed_ino; 487 process_inode_count = 0; 488#if 0 489 printf("end process inodes\n"); 490#endif 491 ehandler_operation(old_operation); 492} 493 494static int process_inode_cmp(const void *a, const void *b) 495{ 496 const struct process_inode_block *ib_a = 497 (const struct process_inode_block *) a; 498 const struct process_inode_block *ib_b = 499 (const struct process_inode_block *) b; 500 501 return (ib_a->inode.i_block[EXT2_IND_BLOCK] - 502 ib_b->inode.i_block[EXT2_IND_BLOCK]); 503} 504 505/* 506 * This procedure will allocate the inode bad map table 507 */ 508static void alloc_bad_map(e2fsck_t ctx) 509{ 510 struct problem_context pctx; 511 512 clear_problem_context(&pctx); 513 514 pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs, "bad inode map", 515 &ctx->inode_bad_map); 516 if (pctx.errcode) { 517 pctx.num = 3; 518 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx); 519 fatal_error(0); 520 } 521} 522 523/* 524 * This procedure will allocate the inode "bb" (badblock) map table 525 */ 526static void alloc_bb_map(e2fsck_t ctx) 527{ 528 struct problem_context pctx; 529 530 clear_problem_context(&pctx); 531 pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs, 532 "inode in bad block map", 533 &ctx->inode_bb_map); 534 if (pctx.errcode) { 535 pctx.num = 4; 536 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx); 537 fatal_error(0); 538 } 539} 540 541/* 542 * Marks a block as in use, setting the dup_map if it's been set 543 * already. Called by process_block and process_bad_block. 544 * 545 * WARNING: Assumes checks have already been done to make sure block 546 * is valid. This is true in both process_block and process_bad_block. 547 */ 548static _INLINE_ void mark_block_used(e2fsck_t ctx, blk_t block) 549{ 550 struct problem_context pctx; 551 552 clear_problem_context(&pctx); 553 554 if (ext2fs_fast_test_block_bitmap(ctx->block_found_map, block)) { 555 if (!ctx->block_dup_map) { 556 pctx.errcode = ext2fs_allocate_block_bitmap(ctx->fs, 557 "multiply claimed block map", 558 &ctx->block_dup_map); 559 if (pctx.errcode) { 560 pctx.num = 3; 561 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, 562 &pctx); 563 fatal_error(0); 564 } 565 } 566 ext2fs_fast_mark_block_bitmap(ctx->block_dup_map, block); 567 } else { 568 ext2fs_fast_mark_block_bitmap(ctx->block_found_map, block); 569 } 570} 571 572/* 573 * This subroutine is called on each inode to account for all of the 574 * blocks used by that inode. 575 */ 576static void check_blocks(e2fsck_t ctx, struct problem_context *pctx, 577 char *block_buf) 578{ 579 ext2_filsys fs = ctx->fs; 580 struct process_block_struct pb; 581 ino_t ino = pctx->ino; 582 struct ext2_inode *inode = pctx->inode; 583 584 if (!ext2fs_inode_has_valid_blocks(pctx->inode)) 585 return; 586 587 pb.ino = ino; 588 pb.num_blocks = pb.last_block = 0; 589 pb.num_illegal_blocks = 0; 590 pb.suppress = 0; pb.clear = 0; 591 pb.fragmented = 0; 592 pb.previous_block = 0; 593 pb.is_dir = LINUX_S_ISDIR(pctx->inode->i_mode); 594 pb.inode = inode; 595 pb.pctx = pctx; 596 pb.ctx = ctx; 597 pctx->ino = ino; 598 pctx->errcode = ext2fs_block_iterate2(fs, ino, 599 pb.is_dir ? BLOCK_FLAG_HOLE : 0, 600 block_buf, process_block, &pb); 601 end_problem_latch(ctx, PR_LATCH_BLOCK); 602 if (pctx->errcode) 603 fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx); 604 605 if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group) 606 ctx->fs_fragmented++; 607 608 if (pb.clear) { 609 e2fsck_read_inode(fs, ino, inode, "check_blocks"); 610 inode->i_links_count = 0; 611 ext2fs_icount_store(ctx->inode_link_info, ino, 0); 612 inode->i_dtime = time(0); 613 e2fsck_write_inode(fs, ino, inode, "check_blocks"); 614 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino); 615 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino); 616 /* 617 * The inode was probably partially accounted for 618 * before processing was aborted, so we need to 619 * restart the pass 1 scan. 620 */ 621 restart_e2fsck++; 622 return; 623 } 624 625 pb.num_blocks *= (fs->blocksize / 512); 626#if 0 627 printf("inode %u, i_size = %lu, last_block = %lu, i_blocks=%lu, num_blocks = %lu\n", 628 ino, inode->i_size, pb.last_block, inode->i_blocks, 629 pb.num_blocks); 630#endif 631 if (!pb.num_blocks && pb.is_dir) { 632 if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) { 633 inode->i_links_count = 0; 634 ext2fs_icount_store(ctx->inode_link_info, ino, 0); 635 inode->i_dtime = time(0); 636 e2fsck_write_inode(fs, ino, inode, "check_blocks"); 637 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino); 638 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino); 639 ctx->fs_directory_count--; 640 pb.is_dir = 0; 641 } 642 } 643 if ((pb.is_dir && (inode->i_size != 644 (pb.last_block + 1) * fs->blocksize)) || 645 (inode->i_size < pb.last_block * fs->blocksize)) { 646 pctx->num = (pb.last_block+1) * fs->blocksize; 647 if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) { 648 inode->i_size = pctx->num; 649 e2fsck_write_inode(fs, ino, inode, "check_blocks"); 650 } 651 pctx->num = 0; 652 } 653 if (pb.num_blocks != inode->i_blocks) { 654 pctx->num = pb.num_blocks; 655 if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) { 656 inode->i_blocks = pb.num_blocks; 657 e2fsck_write_inode(fs, ino, inode, "check_blocks"); 658 } 659 pctx->num = 0; 660 } 661} 662 663#if 0 664/* 665 * Helper function called by process block when an illegal block is 666 * found. It returns a description about why the block is illegal 667 */ 668static char *describe_illegal_block(ext2_filsys fs, blk_t block) 669{ 670 blk_t super; 671 int i; 672 static char problem[80]; 673 674 super = fs->super->s_first_data_block; 675 strcpy(problem, "PROGRAMMING ERROR: Unknown reason for illegal block"); 676 if (block < super) { 677 sprintf(problem, "< FIRSTBLOCK (%u)", super); 678 return(problem); 679 } else if (block >= fs->super->s_blocks_count) { 680 sprintf(problem, "> BLOCKS (%u)", fs->super->s_blocks_count); 681 return(problem); 682 } 683 for (i = 0; i < fs->group_desc_count; i++) { 684 if (block == super) { 685 sprintf(problem, "is the superblock in group %d", i); 686 break; 687 } 688 if (block > super && 689 block <= (super + fs->desc_blocks)) { 690 sprintf(problem, "is in the group descriptors " 691 "of group %d", i); 692 break; 693 } 694 if (block == fs->group_desc[i].bg_block_bitmap) { 695 sprintf(problem, "is the block bitmap of group %d", i); 696 break; 697 } 698 if (block == fs->group_desc[i].bg_inode_bitmap) { 699 sprintf(problem, "is the inode bitmap of group %d", i); 700 break; 701 } 702 if (block >= fs->group_desc[i].bg_inode_table && 703 (block < fs->group_desc[i].bg_inode_table 704 + fs->inode_blocks_per_group)) { 705 sprintf(problem, "is in the inode table of group %d", 706 i); 707 break; 708 } 709 super += fs->super->s_blocks_per_group; 710 } 711 return(problem); 712} 713#endif 714 715/* 716 * This is a helper function for check_blocks(). 717 */ 718int process_block(ext2_filsys fs, 719 blk_t *block_nr, 720 int blockcnt, 721 blk_t ref_block, 722 int ref_offset, 723 void *private) 724{ 725 struct process_block_struct *p; 726 struct problem_context *pctx; 727 blk_t blk = *block_nr; 728 int ret_code = 0; 729 int problem = 0; 730 e2fsck_t ctx; 731 732 p = (struct process_block_struct *) private; 733 pctx = p->pctx; 734 ctx = p->ctx; 735 736 if (blk == 0) { 737 if (p->is_dir == 0) { 738 /* 739 * Should never happen, since only directories 740 * get called with BLOCK_FLAG_HOLE 741 */ 742#if DEBUG_E2FSCK 743 printf("process_block() called with blk == 0, " 744 "blockcnt=%d, inode %lu???\n", 745 blockcnt, p->ino); 746#endif 747 return 0; 748 } 749 if (blockcnt < 0) 750 return 0; 751 if (blockcnt * fs->blocksize < p->inode->i_size) { 752#if 0 753 printf("Missing block (#%d) in directory inode %lu!\n", 754 blockcnt, p->ino); 755#endif 756 goto mark_dir; 757 } 758 return 0; 759 } 760 761#if 0 762 printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk, 763 blockcnt); 764#endif 765 766 /* 767 * Simplistic fragmentation check. We merely require that the 768 * file be contiguous. (Which can never be true for really 769 * big files that are greater than a block group.) 770 */ 771 if (p->previous_block) { 772 if (p->previous_block+1 != blk) 773 p->fragmented = 1; 774 } 775 p->previous_block = blk; 776 777 if (blk < fs->super->s_first_data_block || 778 blk >= fs->super->s_blocks_count) 779 problem = PR_1_ILLEGAL_BLOCK_NUM; 780#if 0 781 else 782 if (ext2fs_test_block_bitmap(block_illegal_map, blk)) 783 problem = PR_1_BLOCK_OVERLAPS_METADATA; 784#endif 785 786 if (problem) { 787 p->num_illegal_blocks++; 788 if (!p->suppress && (p->num_illegal_blocks % 12) == 0) { 789 if (fix_problem(ctx, PR_1_TOO_MANY_BAD_BLOCKS, pctx)) { 790 p->clear = 1; 791 return BLOCK_ABORT; 792 } 793 if (ask(ctx, "Suppress messages", 0)) { 794 p->suppress = 1; 795 set_latch_flags(PR_LATCH_BLOCK, 796 PRL_SUPPRESS, 0); 797 } 798 } 799 pctx->blk = blk; 800 pctx->blkcount = blockcnt; 801 if (fix_problem(ctx, problem, pctx)) { 802 blk = *block_nr = 0; 803 ret_code = BLOCK_CHANGED; 804 goto mark_dir; 805 } else 806 return 0; 807 pctx->blk = 0; 808 pctx->blkcount = -1; 809 } 810 811 mark_block_used(ctx, blk); 812 p->num_blocks++; 813 if (blockcnt >= 0) 814 p->last_block = blockcnt; 815mark_dir: 816 if (p->is_dir && (blockcnt >= 0)) { 817 pctx->errcode = ext2fs_add_dir_block(fs->dblist, p->ino, 818 blk, blockcnt); 819 if (pctx->errcode) { 820 pctx->blk = blk; 821 pctx->num = blockcnt; 822 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx); 823 fatal_error(0); 824 } 825 } 826 return ret_code; 827} 828 829static void bad_block_indirect(e2fsck_t ctx, blk_t blk) 830{ 831 struct problem_context pctx; 832 833 clear_problem_context(&pctx); 834 /* 835 * Prompt to see if we should continue or not. 836 */ 837 if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, &pctx)) 838 fatal_error(0); 839} 840 841int process_bad_block(ext2_filsys fs, 842 blk_t *block_nr, 843 int blockcnt, 844 blk_t ref_block, 845 int ref_offset, 846 void *private) 847{ 848 struct process_block_struct *p; 849 blk_t blk = *block_nr; 850 int first_block; 851 int i; 852 struct problem_context *pctx; 853 e2fsck_t ctx; 854 855 if (!blk) 856 return 0; 857 858 p = (struct process_block_struct *) private; 859 ctx = p->ctx; 860 pctx = p->pctx; 861 862 pctx->blk = blk; 863 pctx->blkcount = blockcnt; 864 865 if ((blk < fs->super->s_first_data_block) || 866 (blk >= fs->super->s_blocks_count)) { 867 if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) { 868 *block_nr = 0; 869 return BLOCK_CHANGED; 870 } else 871 return 0; 872 } 873 874 if (blockcnt < 0) { 875 if (ext2fs_test_block_bitmap(ctx->block_found_map, blk)) 876 bad_block_indirect(ctx, blk); 877 else 878 mark_block_used(ctx, blk); 879 return 0; 880 } 881#if 0 882 printf ("DEBUG: Marking %u as bad.\n", blk); 883#endif 884 ctx->fs_badblocks_count++; 885 /* 886 * If the block is not used, then mark it as used and return. 887 * If it is already marked as found, this must mean that 888 * there's an overlap between the filesystem table blocks 889 * (bitmaps and inode table) and the bad block list. 890 */ 891 if (!ext2fs_test_block_bitmap(ctx->block_found_map, blk)) { 892 ext2fs_mark_block_bitmap(ctx->block_found_map, blk); 893 return 0; 894 } 895 /* 896 * Try to find the where the filesystem block was used... 897 */ 898 first_block = fs->super->s_first_data_block; 899 900 for (i = 0; i < fs->group_desc_count; i++ ) { 901 pctx->group = i; 902 pctx->blk = blk; 903 if (blk == first_block) { 904 if (i == 0) { 905 if (fix_problem(ctx, 906 PR_1_BAD_PRIMARY_SUPERBLOCK, 907 pctx)) { 908 *block_nr = 0; 909 return BLOCK_CHANGED; 910 } 911 return 0; 912 } 913 fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx); 914 return 0; 915 } 916 if ((blk > first_block) && 917 (blk <= first_block + fs->desc_blocks)) { 918 if (i == 0) { 919 pctx->blk = *block_nr; 920 if (fix_problem(ctx, 921 PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) { 922 *block_nr = 0; 923 return BLOCK_CHANGED; 924 } 925 return 0; 926 } 927 fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx); 928 return 0; 929 } 930 if (blk == fs->group_desc[i].bg_block_bitmap) { 931 if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) { 932 ctx->invalid_block_bitmap_flag[i]++; 933 ctx->invalid_bitmaps++; 934 } 935 return 0; 936 } 937 if (blk == fs->group_desc[i].bg_inode_bitmap) { 938 if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) { 939 ctx->invalid_inode_bitmap_flag[i]++; 940 ctx->invalid_bitmaps++; 941 } 942 return 0; 943 } 944 if ((blk >= fs->group_desc[i].bg_inode_table) && 945 (blk < (fs->group_desc[i].bg_inode_table + 946 fs->inode_blocks_per_group))) { 947 /* 948 * If there are bad blocks in the inode table, 949 * the inode scan code will try to do 950 * something reasonable automatically. 951 */ 952 return 0; 953 } 954 } 955 /* 956 * If we've gotten to this point, then the only 957 * possibility is that the bad block inode meta data 958 * is using a bad block. 959 */ 960 if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) || 961 p->inode->i_block[EXT2_DIND_BLOCK]) { 962 bad_block_indirect(ctx, blk); 963 return 0; 964 } 965 966 pctx->group = -1; 967 968 /* Warn user that the block wasn't claimed */ 969 fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx); 970 971 return 0; 972} 973 974static void new_table_block(e2fsck_t ctx, blk_t first_block, int group, 975 const char *name, int num, blk_t *new_block) 976{ 977 ext2_filsys fs = ctx->fs; 978 blk_t old_block = *new_block; 979 int i; 980 char *buf; 981 struct problem_context pctx; 982 983 clear_problem_context(&pctx); 984 985 pctx.group = group; 986 pctx.blk = old_block; 987 pctx.str = name; 988 989 pctx.errcode = ext2fs_get_free_blocks(fs, first_block, 990 first_block + fs->super->s_blocks_per_group, 991 num, ctx->block_found_map, new_block); 992 if (pctx.errcode) { 993 pctx.num = num; 994 fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx); 995 ext2fs_unmark_valid(fs); 996 return; 997 } 998 buf = malloc(fs->blocksize); 999 if (!buf) { 1000 fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx); 1001 ext2fs_unmark_valid(fs); 1002 return; 1003 } 1004 ext2fs_mark_super_dirty(fs); 1005 pctx.blk2 = *new_block; 1006 fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO : 1007 PR_1_RELOC_TO), &pctx); 1008 pctx.blk2 = 0; 1009 for (i = 0; i < num; i++) { 1010 pctx.blk = i; 1011 ext2fs_mark_block_bitmap(ctx->block_found_map, (*new_block)+i); 1012 if (old_block) { 1013 pctx.errcode = io_channel_read_blk(fs->io, 1014 old_block + i, 1, buf); 1015 if (pctx.errcode) 1016 fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx); 1017 } else 1018 memset(buf, 0, fs->blocksize); 1019 1020 pctx.blk = (*new_block) + i; 1021 pctx.errcode = io_channel_write_blk(fs->io, pctx.blk, 1022 1, buf); 1023 if (pctx.errcode) 1024 fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx); 1025 } 1026 free(buf); 1027} 1028 1029/* 1030 * This routine gets called at the end of pass 1 if bad blocks are 1031 * detected in the superblock, group descriptors, inode_bitmaps, or 1032 * block bitmaps. At this point, all of the blocks have been mapped 1033 * out, so we can try to allocate new block(s) to replace the bad 1034 * blocks. 1035 */ 1036static void handle_fs_bad_blocks(e2fsck_t ctx) 1037{ 1038 ext2_filsys fs = ctx->fs; 1039 int i; 1040 int first_block = fs->super->s_first_data_block; 1041 1042 for (i = 0; i < fs->group_desc_count; i++) { 1043 if (ctx->invalid_block_bitmap_flag[i]) { 1044 new_table_block(ctx, first_block, i, "block bitmap", 1045 1, &fs->group_desc[i].bg_block_bitmap); 1046 } 1047 if (ctx->invalid_inode_bitmap_flag[i]) { 1048 new_table_block(ctx, first_block, i, "inode bitmap", 1049 1, &fs->group_desc[i].bg_inode_bitmap); 1050 } 1051 if (ctx->invalid_inode_table_flag[i]) { 1052 new_table_block(ctx, first_block, i, "inode table", 1053 fs->inode_blocks_per_group, 1054 &fs->group_desc[i].bg_inode_table); 1055 restart_e2fsck++; 1056 } 1057 first_block += fs->super->s_blocks_per_group; 1058 } 1059 ctx->invalid_bitmaps = 0; 1060} 1061 1062/* 1063 * This routine marks all blocks which are used by the superblock, 1064 * group descriptors, inode bitmaps, and block bitmaps. 1065 */ 1066static void mark_table_blocks(e2fsck_t ctx) 1067{ 1068 ext2_filsys fs = ctx->fs; 1069 blk_t block, b; 1070 int i,j; 1071 struct problem_context pctx; 1072 1073 clear_problem_context(&pctx); 1074 1075 block = fs->super->s_first_data_block; 1076 for (i = 0; i < fs->group_desc_count; i++) { 1077 pctx.group = i; 1078 1079 if (ext2fs_bg_has_super(fs, i)) { 1080 /* 1081 * Mark this group's copy of the superblock 1082 */ 1083 ext2fs_mark_block_bitmap(ctx->block_found_map, block); 1084 ext2fs_mark_block_bitmap(ctx->block_illegal_map, 1085 block); 1086 1087 /* 1088 * Mark this group's copy of the descriptors 1089 */ 1090 for (j = 0; j < fs->desc_blocks; j++) { 1091 ext2fs_mark_block_bitmap(ctx->block_found_map, 1092 block + j + 1); 1093 ext2fs_mark_block_bitmap(ctx->block_illegal_map, 1094 block + j + 1); 1095 } 1096 } 1097 1098 /* 1099 * Mark the blocks used for the inode table 1100 */ 1101 if (fs->group_desc[i].bg_inode_table) { 1102 for (j = 0, b = fs->group_desc[i].bg_inode_table; 1103 j < fs->inode_blocks_per_group; 1104 j++, b++) { 1105 if (ext2fs_test_block_bitmap(ctx->block_found_map, 1106 b)) { 1107 pctx.blk = b; 1108 if (fix_problem(ctx, 1109 PR_1_ITABLE_CONFLICT, &pctx)) { 1110 ctx->invalid_inode_table_flag[i]++; 1111 ctx->invalid_bitmaps++; 1112 } 1113 } else { 1114 ext2fs_mark_block_bitmap(ctx->block_found_map, 1115 b); 1116 ext2fs_mark_block_bitmap(ctx->block_illegal_map, 1117 b); 1118 } 1119 } 1120 } 1121 1122 /* 1123 * Mark block used for the block bitmap 1124 */ 1125 if (fs->group_desc[i].bg_block_bitmap) { 1126 if (ext2fs_test_block_bitmap(ctx->block_found_map, 1127 fs->group_desc[i].bg_block_bitmap)) { 1128 pctx.blk = fs->group_desc[i].bg_block_bitmap; 1129 if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) { 1130 ctx->invalid_block_bitmap_flag[i]++; 1131 ctx->invalid_bitmaps++; 1132 } 1133 } else { 1134 ext2fs_mark_block_bitmap(ctx->block_found_map, 1135 fs->group_desc[i].bg_block_bitmap); 1136 ext2fs_mark_block_bitmap(ctx->block_illegal_map, 1137 fs->group_desc[i].bg_block_bitmap); 1138 } 1139 1140 } 1141 /* 1142 * Mark block used for the inode bitmap 1143 */ 1144 if (fs->group_desc[i].bg_inode_bitmap) { 1145 if (ext2fs_test_block_bitmap(ctx->block_found_map, 1146 fs->group_desc[i].bg_inode_bitmap)) { 1147 pctx.blk = fs->group_desc[i].bg_inode_bitmap; 1148 if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) { 1149 ctx->invalid_inode_bitmap_flag[i]++; 1150 ctx->invalid_bitmaps++; 1151 } 1152 } else { 1153 ext2fs_mark_block_bitmap(ctx->block_found_map, 1154 fs->group_desc[i].bg_inode_bitmap); 1155 ext2fs_mark_block_bitmap(ctx->block_illegal_map, 1156 fs->group_desc[i].bg_inode_bitmap); 1157 } 1158 } 1159 block += fs->super->s_blocks_per_group; 1160 } 1161} 1162 1163/* 1164 * This subroutines short circuits ext2fs_get_blocks and 1165 * ext2fs_check_directory; we use them since we already have the inode 1166 * structure, so there's no point in letting the ext2fs library read 1167 * the inode again. 1168 */ 1169errcode_t pass1_get_blocks(ext2_filsys fs, ino_t ino, blk_t *blocks) 1170{ 1171 e2fsck_t ctx = fs->private; 1172 int i; 1173 1174 if (ino != ctx->stashed_ino) 1175 return EXT2_ET_CALLBACK_NOTHANDLED; 1176 1177 for (i=0; i < EXT2_N_BLOCKS; i++) 1178 blocks[i] = ctx->stashed_inode->i_block[i]; 1179 return 0; 1180} 1181 1182errcode_t pass1_read_inode(ext2_filsys fs, ino_t ino, struct ext2_inode *inode) 1183{ 1184 e2fsck_t ctx = fs->private; 1185 1186 if (ino != ctx->stashed_ino) 1187 return EXT2_ET_CALLBACK_NOTHANDLED; 1188 *inode = *ctx->stashed_inode; 1189 return 0; 1190} 1191 1192errcode_t pass1_write_inode(ext2_filsys fs, ino_t ino, 1193 struct ext2_inode *inode) 1194{ 1195 e2fsck_t ctx = fs->private; 1196 1197 if (ino == ctx->stashed_ino) 1198 *ctx->stashed_inode = *inode; 1199 return EXT2_ET_CALLBACK_NOTHANDLED; 1200} 1201 1202errcode_t pass1_check_directory(ext2_filsys fs, ino_t ino) 1203{ 1204 e2fsck_t ctx = fs->private; 1205 1206 if (ino != ctx->stashed_ino) 1207 return EXT2_ET_CALLBACK_NOTHANDLED; 1208 1209 if (!LINUX_S_ISDIR(ctx->stashed_inode->i_mode)) 1210 return ENOTDIR; 1211 return 0; 1212} 1213