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