2 * pass1.c -- pass #1 of e2fsck: sequential scan of the inode table
4 * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
11 * Pass 1 of e2fsck iterates over all the inodes in the filesystems,
12 * and applies the following tests to each inode:
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
18 * Pass 1 also gathers the collects the following information:
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)
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
36 * Note that pass 1B will be invoked if there are any duplicate blocks
46 #include <ext2fs/ext2_ext_attr.h>
50 #ifdef NO_INLINE_FUNCS
53 #define _INLINE_ inline
56 static int process_block(ext2_filsys fs, blk_t *blocknr,
57 e2_blkcnt_t blockcnt, blk_t ref_blk,
58 int ref_offset, void *priv_data);
59 static int process_bad_block(ext2_filsys fs, blk_t *block_nr,
60 e2_blkcnt_t blockcnt, blk_t ref_blk,
61 int ref_offset, void *priv_data);
62 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
64 static void mark_table_blocks(e2fsck_t ctx);
65 static void alloc_bb_map(e2fsck_t ctx);
66 static void alloc_imagic_map(e2fsck_t ctx);
67 static void mark_inode_bad(e2fsck_t ctx, ino_t ino);
68 static void handle_fs_bad_blocks(e2fsck_t ctx);
69 static void process_inodes(e2fsck_t ctx, char *block_buf);
70 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b);
71 static errcode_t scan_callback(ext2_filsys fs, ext2_inode_scan scan,
72 dgrp_t group, void * priv_data);
73 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
74 char *block_buf, int adjust_sign);
75 /* static char *describe_illegal_block(ext2_filsys fs, blk_t block); */
77 struct process_block_struct {
79 int is_dir:1, is_reg:1, clear:1, suppress:1,
80 fragmented:1, compressed:1;
83 e2_blkcnt_t last_block;
84 int num_illegal_blocks;
86 struct ext2_inode *inode;
87 struct problem_context *pctx;
91 struct process_inode_block {
93 struct ext2_inode inode;
96 struct scan_callback_struct {
102 * For the inodes to process list.
104 static struct process_inode_block *inodes_to_process;
105 static int process_inode_count;
107 static __u64 ext2_max_sizes[EXT2_MAX_BLOCK_LOG_SIZE -
108 EXT2_MIN_BLOCK_LOG_SIZE + 1];
111 * Free all memory allocated by pass1 in preparation for restarting
114 static void unwind_pass1(ext2_filsys fs)
116 ext2fs_free_mem((void **) &inodes_to_process);
117 inodes_to_process = 0;
121 * Check to make sure a device inode is real. Returns 1 if the device
122 * checks out, 0 if not.
124 * Note: this routine is now also used to check FIFO's and Sockets,
125 * since they have the same requirement; the i_block fields should be
128 int e2fsck_pass1_check_device_inode(struct ext2_inode *inode)
133 * If i_blocks is non-zero, or the index flag is set, then
134 * this is a bogus device/fifo/socket
136 if (inode->i_blocks || (inode->i_flags & EXT2_INDEX_FL))
140 * We should be able to do the test below all the time, but
141 * because the kernel doesn't forcibly clear the device
142 * inode's additional i_block fields, there are some rare
143 * occasions when a legitimate device inode will have non-zero
144 * additional i_block fields. So for now, we only complain
145 * when the immutable flag is set, which should never happen
146 * for devices. (And that's when the problem is caused, since
147 * you can't set or clear immutable flags for devices.) Once
148 * the kernel has been fixed we can change this...
150 if (inode->i_flags & (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)) {
151 for (i=4; i < EXT2_N_BLOCKS; i++)
152 if (inode->i_block[i])
160 * Incredibly, libc5 doesn't appear to have strnlen. So we have to
163 static int strnlen(const char * s, int count)
167 while (count-- && *cp)
174 * Check to make sure a symlink inode is real. Returns 1 if the symlink
175 * checks out, 0 if not.
177 int e2fsck_pass1_check_symlink(ext2_filsys fs, struct ext2_inode *inode,
183 if ((inode->i_size_high || inode->i_size == 0) ||
184 (inode->i_flags & EXT2_INDEX_FL))
187 if (inode->i_blocks) {
188 if ((inode->i_size >= fs->blocksize) ||
189 (inode->i_blocks != fs->blocksize >> 9) ||
190 (inode->i_block[0] < fs->super->s_first_data_block) ||
191 (inode->i_block[0] >= fs->super->s_blocks_count))
194 for (i = 1; i < EXT2_N_BLOCKS; i++)
195 if (inode->i_block[i])
198 if (io_channel_read_blk(fs->io, inode->i_block[0], 1, buf))
201 len = strnlen(buf, fs->blocksize);
202 if (len == fs->blocksize)
205 if (inode->i_size >= sizeof(inode->i_block))
208 len = strnlen((char *)inode->i_block, sizeof(inode->i_block));
209 if (len == sizeof(inode->i_block))
212 if (len != inode->i_size)
218 * If the immutable (or append-only) flag is set on the inode, offer
221 #define BAD_SPECIAL_FLAGS (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)
222 static void check_immutable(e2fsck_t ctx, struct problem_context *pctx)
224 if (!(pctx->inode->i_flags & BAD_SPECIAL_FLAGS))
227 if (!fix_problem(ctx, PR_1_SET_IMMUTABLE, pctx))
230 pctx->inode->i_flags &= ~BAD_SPECIAL_FLAGS;
231 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
235 * If device, fifo or socket, check size is zero -- if not offer to
238 static void check_size(e2fsck_t ctx, struct problem_context *pctx)
240 struct ext2_inode *inode = pctx->inode;
242 if ((inode->i_size == 0) && (inode->i_size_high == 0))
245 if (!fix_problem(ctx, PR_1_SET_NONZSIZE, pctx))
249 inode->i_size_high = 0;
250 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
254 void e2fsck_pass1(e2fsck_t ctx)
258 ext2_filsys fs = ctx->fs;
260 struct ext2_inode inode;
261 ext2_inode_scan scan;
263 #ifdef RESOURCE_TRACK
264 struct resource_track rtrack;
266 unsigned char frag, fsize;
267 struct problem_context pctx;
268 struct scan_callback_struct scan_struct;
269 struct ext2_super_block *sb = ctx->fs->super;
272 #ifdef RESOURCE_TRACK
273 init_resource_track(&rtrack);
275 clear_problem_context(&pctx);
277 if (!(ctx->options & E2F_OPT_PREEN))
278 fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
281 mtrace_print("Pass 1");
284 #define EXT2_BPP(bits) (1ULL << ((bits) - 2))
286 for (i = EXT2_MIN_BLOCK_LOG_SIZE; i <= EXT2_MAX_BLOCK_LOG_SIZE; i++) {
287 max_sizes = EXT2_NDIR_BLOCKS + EXT2_BPP(i);
288 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i);
289 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i) * EXT2_BPP(i);
290 max_sizes = (max_sizes * (1UL << i)) - 1;
291 ext2_max_sizes[i - EXT2_MIN_BLOCK_LOG_SIZE] = max_sizes;
295 imagic_fs = (sb->s_feature_compat & EXT2_FEATURE_COMPAT_IMAGIC_INODES);
298 * Allocate bitmaps structures
300 pctx.errcode = ext2fs_allocate_inode_bitmap(fs, _("in-use inode map"),
301 &ctx->inode_used_map);
304 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
305 ctx->flags |= E2F_FLAG_ABORT;
308 pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
309 _("directory inode map"), &ctx->inode_dir_map);
312 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
313 ctx->flags |= E2F_FLAG_ABORT;
316 pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
317 _("regular file inode map"), &ctx->inode_reg_map);
320 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
321 ctx->flags |= E2F_FLAG_ABORT;
324 pctx.errcode = ext2fs_allocate_block_bitmap(fs, _("in-use block map"),
325 &ctx->block_found_map);
328 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
329 ctx->flags |= E2F_FLAG_ABORT;
332 pctx.errcode = ext2fs_create_icount2(fs, 0, 0, 0,
333 &ctx->inode_link_info);
335 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
336 ctx->flags |= E2F_FLAG_ABORT;
339 inodes_to_process = (struct process_inode_block *)
340 e2fsck_allocate_memory(ctx,
341 (ctx->process_inode_size *
342 sizeof(struct process_inode_block)),
343 "array of inodes to process");
344 process_inode_count = 0;
346 pctx.errcode = ext2fs_init_dblist(fs, 0);
348 fix_problem(ctx, PR_1_ALLOCATE_DBCOUNT, &pctx);
349 ctx->flags |= E2F_FLAG_ABORT;
354 * If the last orphan field is set, clear it, since the pass1
355 * processing will automatically find and clear the orphans.
356 * In the future, we may want to try using the last_orphan
357 * linked list ourselves, but for now, we clear it so that the
358 * ext3 mount code won't get confused.
360 if (!(ctx->options & E2F_OPT_READONLY)) {
361 if (fs->super->s_last_orphan) {
362 fs->super->s_last_orphan = 0;
363 ext2fs_mark_super_dirty(fs);
367 mark_table_blocks(ctx);
368 block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3,
369 "block interate buffer");
370 e2fsck_use_inode_shortcuts(ctx, 1);
371 ehandler_operation(_("doing inode scan"));
372 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
375 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
376 ctx->flags |= E2F_FLAG_ABORT;
379 ext2fs_inode_scan_flags(scan, EXT2_SF_SKIP_MISSING_ITABLE, 0);
380 pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode);
382 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
383 ctx->flags |= E2F_FLAG_ABORT;
386 ctx->stashed_inode = &inode;
387 scan_struct.ctx = ctx;
388 scan_struct.block_buf = block_buf;
389 ext2fs_set_inode_callback(scan, scan_callback, &scan_struct);
391 if ((ctx->progress)(ctx, 1, 0, ctx->fs->group_desc_count))
396 ctx->stashed_ino = ino;
397 if (inode.i_links_count) {
398 pctx.errcode = ext2fs_icount_store(ctx->inode_link_info,
399 ino, inode.i_links_count);
401 pctx.num = inode.i_links_count;
402 fix_problem(ctx, PR_1_ICOUNT_STORE, &pctx);
403 ctx->flags |= E2F_FLAG_ABORT;
407 if (ino == EXT2_BAD_INO) {
408 struct process_block_struct pb;
410 pb.ino = EXT2_BAD_INO;
411 pb.num_blocks = pb.last_block = 0;
412 pb.num_illegal_blocks = 0;
413 pb.suppress = 0; pb.clear = 0; pb.is_dir = 0;
414 pb.is_reg = 0; pb.fragmented = 0;
418 pctx.errcode = ext2fs_block_iterate2(fs, ino, 0,
419 block_buf, process_bad_block, &pb);
421 fix_problem(ctx, PR_1_BLOCK_ITERATE, &pctx);
422 ctx->flags |= E2F_FLAG_ABORT;
425 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
426 clear_problem_context(&pctx);
428 } else if (ino == EXT2_ROOT_INO) {
430 * Make sure the root inode is a directory; if
431 * not, offer to clear it. It will be
432 * regnerated in pass #3.
434 if (!LINUX_S_ISDIR(inode.i_mode)) {
435 if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx)) {
436 inode.i_dtime = time(0);
437 inode.i_links_count = 0;
438 ext2fs_icount_store(ctx->inode_link_info,
440 e2fsck_write_inode(ctx, ino, &inode,
446 * If dtime is set, offer to clear it. mke2fs
447 * version 0.2b created filesystems with the
448 * dtime field set for the root and lost+found
449 * directories. We won't worry about
450 * /lost+found, since that can be regenerated
451 * easily. But we will fix the root directory
454 if (inode.i_dtime && inode.i_links_count) {
455 if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) {
457 e2fsck_write_inode(ctx, ino, &inode,
461 } else if (ino == EXT2_JOURNAL_INO) {
462 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
463 if (fs->super->s_journal_inum == EXT2_JOURNAL_INO) {
464 if (!LINUX_S_ISREG(inode.i_mode) &&
465 fix_problem(ctx, PR_1_JOURNAL_BAD_MODE,
467 inode.i_mode = LINUX_S_IFREG;
468 e2fsck_write_inode(ctx, ino, &inode,
471 check_blocks(ctx, &pctx, block_buf);
474 if ((inode.i_links_count || inode.i_blocks ||
475 inode.i_blocks || inode.i_block[0]) &&
476 fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR,
478 memset(&inode, 0, sizeof(inode));
479 ext2fs_icount_store(ctx->inode_link_info,
481 e2fsck_write_inode(ctx, ino, &inode, "pass1");
483 } else if (ino < EXT2_FIRST_INODE(fs->super)) {
486 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
487 if (ino == EXT2_BOOT_LOADER_INO) {
488 if (LINUX_S_ISDIR(inode.i_mode))
489 problem = PR_1_RESERVED_BAD_MODE;
490 } else if (ino == EXT2_RESIZE_INO) {
492 !LINUX_S_ISREG(inode.i_mode))
493 problem = PR_1_RESERVED_BAD_MODE;
495 if (inode.i_mode != 0)
496 problem = PR_1_RESERVED_BAD_MODE;
499 if (fix_problem(ctx, problem, &pctx)) {
501 e2fsck_write_inode(ctx, ino, &inode,
505 check_blocks(ctx, &pctx, block_buf);
509 * Check for inodes who might have been part of the
510 * orphaned list linked list. They should have gotten
511 * dealt with by now, unless the list had somehow been
514 * FIXME: In the future, inodes which are still in use
515 * (and which are therefore) pending truncation should
516 * be handled specially. Right now we just clear the
517 * dtime field, and the normal e2fsck handling of
518 * inodes where i_size and the inode blocks are
519 * inconsistent is to fix i_size, instead of releasing
520 * the extra blocks. This won't catch the inodes that
521 * was at the end of the orphan list, but it's better
522 * than nothing. The right answer is that there
523 * shouldn't be any bugs in the orphan list handling. :-)
526 inode.i_dtime < ctx->fs->super->s_inodes_count) {
527 if (fix_problem(ctx, PR_1_LOW_DTIME, &pctx)) {
528 inode.i_dtime = inode.i_links_count ?
530 e2fsck_write_inode(ctx, ino, &inode,
536 * This code assumes that deleted inodes have
537 * i_links_count set to 0.
539 if (!inode.i_links_count) {
540 if (!inode.i_dtime && inode.i_mode) {
542 PR_1_ZERO_DTIME, &pctx)) {
543 inode.i_dtime = time(0);
544 e2fsck_write_inode(ctx, ino, &inode,
551 * n.b. 0.3c ext2fs code didn't clear i_links_count for
552 * deleted files. Oops.
554 * Since all new ext2 implementations get this right,
555 * we now assume that the case of non-zero
556 * i_links_count and non-zero dtime means that we
557 * should keep the file, not delete it.
561 if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) {
563 e2fsck_write_inode(ctx, ino, &inode, "pass1");
567 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
568 switch (fs->super->s_creator_os) {
570 frag = inode.osd2.linux2.l_i_frag;
571 fsize = inode.osd2.linux2.l_i_fsize;
574 frag = inode.osd2.hurd2.h_i_frag;
575 fsize = inode.osd2.hurd2.h_i_fsize;
578 frag = inode.osd2.masix2.m_i_frag;
579 fsize = inode.osd2.masix2.m_i_fsize;
585 if (inode.i_faddr || frag || fsize ||
586 (LINUX_S_ISDIR(inode.i_mode) && inode.i_dir_acl))
587 mark_inode_bad(ctx, ino);
588 if (inode.i_flags & EXT2_IMAGIC_FL) {
590 if (!ctx->inode_imagic_map)
591 alloc_imagic_map(ctx);
592 ext2fs_mark_inode_bitmap(ctx->inode_imagic_map,
595 if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) {
596 inode.i_flags &= ~EXT2_IMAGIC_FL;
597 e2fsck_write_inode(ctx, ino,
603 if (LINUX_S_ISDIR(inode.i_mode)) {
604 ext2fs_mark_inode_bitmap(ctx->inode_dir_map, ino);
605 e2fsck_add_dir_info(ctx, ino, 0);
606 ctx->fs_directory_count++;
607 } else if (LINUX_S_ISREG (inode.i_mode)) {
608 ext2fs_mark_inode_bitmap(ctx->inode_reg_map, ino);
609 ctx->fs_regular_count++;
610 } else if (LINUX_S_ISCHR (inode.i_mode) &&
611 e2fsck_pass1_check_device_inode(&inode)) {
612 check_immutable(ctx, &pctx);
613 check_size(ctx, &pctx);
614 ctx->fs_chardev_count++;
615 } else if (LINUX_S_ISBLK (inode.i_mode) &&
616 e2fsck_pass1_check_device_inode(&inode)) {
617 check_immutable(ctx, &pctx);
618 check_size(ctx, &pctx);
619 ctx->fs_blockdev_count++;
620 } else if (LINUX_S_ISLNK (inode.i_mode) &&
621 e2fsck_pass1_check_symlink(fs, &inode, block_buf)) {
622 check_immutable(ctx, &pctx);
623 ctx->fs_symlinks_count++;
624 if (!inode.i_blocks) {
625 ctx->fs_fast_symlinks_count++;
629 else if (LINUX_S_ISFIFO (inode.i_mode) &&
630 e2fsck_pass1_check_device_inode(&inode)) {
631 check_immutable(ctx, &pctx);
632 check_size(ctx, &pctx);
633 ctx->fs_fifo_count++;
634 } else if ((LINUX_S_ISSOCK (inode.i_mode)) &&
635 e2fsck_pass1_check_device_inode(&inode)) {
636 check_immutable(ctx, &pctx);
637 check_size(ctx, &pctx);
638 ctx->fs_sockets_count++;
640 mark_inode_bad(ctx, ino);
641 if (inode.i_block[EXT2_IND_BLOCK])
643 if (inode.i_block[EXT2_DIND_BLOCK])
644 ctx->fs_dind_count++;
645 if (inode.i_block[EXT2_TIND_BLOCK])
646 ctx->fs_tind_count++;
647 if (inode.i_block[EXT2_IND_BLOCK] ||
648 inode.i_block[EXT2_DIND_BLOCK] ||
649 inode.i_block[EXT2_TIND_BLOCK] ||
651 inodes_to_process[process_inode_count].ino = ino;
652 inodes_to_process[process_inode_count].inode = inode;
653 process_inode_count++;
655 check_blocks(ctx, &pctx, block_buf);
657 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
660 if (process_inode_count >= ctx->process_inode_size) {
661 process_inodes(ctx, block_buf);
663 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
667 pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode);
668 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
670 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) {
671 if (!ctx->inode_bb_map)
673 ext2fs_mark_inode_bitmap(ctx->inode_bb_map, ino);
674 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
678 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
679 ctx->flags |= E2F_FLAG_ABORT;
683 process_inodes(ctx, block_buf);
684 ext2fs_close_inode_scan(scan);
685 ehandler_operation(0);
688 * If any extended attribute blocks' reference counts need to
689 * be adjusted, either up (ctx->refcount_extra), or down
690 * (ctx->refcount), then fix them.
693 adjust_extattr_refcount(ctx, ctx->refcount, block_buf, -1);
694 ea_refcount_free(ctx->refcount);
697 if (ctx->refcount_extra) {
698 adjust_extattr_refcount(ctx, ctx->refcount_extra,
700 ea_refcount_free(ctx->refcount_extra);
701 ctx->refcount_extra = 0;
704 if (ctx->invalid_bitmaps)
705 handle_fs_bad_blocks(ctx);
707 /* We don't need the block_ea_map any more */
708 if (ctx->block_ea_map) {
709 ext2fs_free_block_bitmap(ctx->block_ea_map);
710 ctx->block_ea_map = 0;
713 if (ctx->flags & E2F_FLAG_RESTART) {
715 * Only the master copy of the superblock and block
716 * group descriptors are going to be written during a
717 * restart, so set the superblock to be used to be the
720 ctx->use_superblock = 0;
725 if (ctx->block_dup_map) {
726 if (ctx->options & E2F_OPT_PREEN) {
727 clear_problem_context(&pctx);
728 fix_problem(ctx, PR_1_DUP_BLOCKS_PREENSTOP, &pctx);
730 e2fsck_pass1_dupblocks(ctx, block_buf);
732 ext2fs_free_mem((void **) &inodes_to_process);
734 e2fsck_use_inode_shortcuts(ctx, 0);
736 ext2fs_free_mem((void **) &block_buf);
738 #ifdef RESOURCE_TRACK
739 if (ctx->options & E2F_OPT_TIME2) {
740 e2fsck_clear_progbar(ctx);
741 print_resource_track(_("Pass 1"), &rtrack);
747 * When the inode_scan routines call this callback at the end of the
748 * glock group, call process_inodes.
750 static errcode_t scan_callback(ext2_filsys fs, ext2_inode_scan scan,
751 dgrp_t group, void * priv_data)
753 struct scan_callback_struct *scan_struct;
756 scan_struct = (struct scan_callback_struct *) priv_data;
757 ctx = scan_struct->ctx;
759 process_inodes((e2fsck_t) fs->priv_data, scan_struct->block_buf);
762 if ((ctx->progress)(ctx, 1, group+1,
763 ctx->fs->group_desc_count))
764 return EXT2_ET_CANCEL_REQUESTED;
770 * Process the inodes in the "inodes to process" list.
772 static void process_inodes(e2fsck_t ctx, char *block_buf)
775 struct ext2_inode *old_stashed_inode;
776 ext2_ino_t old_stashed_ino;
777 const char *old_operation;
779 struct problem_context pctx;
782 printf("begin process_inodes: ");
784 if (process_inode_count == 0)
786 old_operation = ehandler_operation(0);
787 old_stashed_inode = ctx->stashed_inode;
788 old_stashed_ino = ctx->stashed_ino;
789 qsort(inodes_to_process, process_inode_count,
790 sizeof(struct process_inode_block), process_inode_cmp);
791 clear_problem_context(&pctx);
792 for (i=0; i < process_inode_count; i++) {
793 pctx.inode = ctx->stashed_inode = &inodes_to_process[i].inode;
794 pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino;
797 printf("%u ", pctx.ino);
799 sprintf(buf, _("reading indirect blocks of inode %u"),
801 ehandler_operation(buf);
802 check_blocks(ctx, &pctx, block_buf);
803 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
806 ctx->stashed_inode = old_stashed_inode;
807 ctx->stashed_ino = old_stashed_ino;
808 process_inode_count = 0;
810 printf("end process inodes\n");
812 ehandler_operation(old_operation);
815 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
817 const struct process_inode_block *ib_a =
818 (const struct process_inode_block *) a;
819 const struct process_inode_block *ib_b =
820 (const struct process_inode_block *) b;
823 ret = (ib_a->inode.i_block[EXT2_IND_BLOCK] -
824 ib_b->inode.i_block[EXT2_IND_BLOCK]);
826 ret = ib_a->inode.i_file_acl - ib_b->inode.i_file_acl;
831 * Mark an inode as being bad in some what
833 static void mark_inode_bad(e2fsck_t ctx, ino_t ino)
835 struct problem_context pctx;
837 if (!ctx->inode_bad_map) {
838 clear_problem_context(&pctx);
840 pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
841 _("bad inode map"), &ctx->inode_bad_map);
844 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
845 /* Should never get here */
846 ctx->flags |= E2F_FLAG_ABORT;
850 ext2fs_mark_inode_bitmap(ctx->inode_bad_map, ino);
855 * This procedure will allocate the inode "bb" (badblock) map table
857 static void alloc_bb_map(e2fsck_t ctx)
859 struct problem_context pctx;
861 clear_problem_context(&pctx);
862 pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
863 _("inode in bad block map"),
867 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
868 /* Should never get here */
869 ctx->flags |= E2F_FLAG_ABORT;
875 * This procedure will allocate the inode imagic table
877 static void alloc_imagic_map(e2fsck_t ctx)
879 struct problem_context pctx;
881 clear_problem_context(&pctx);
882 pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
883 _("imagic inode map"),
884 &ctx->inode_imagic_map);
887 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
888 /* Should never get here */
889 ctx->flags |= E2F_FLAG_ABORT;
895 * Marks a block as in use, setting the dup_map if it's been set
896 * already. Called by process_block and process_bad_block.
898 * WARNING: Assumes checks have already been done to make sure block
899 * is valid. This is true in both process_block and process_bad_block.
901 static _INLINE_ void mark_block_used(e2fsck_t ctx, blk_t block)
903 struct problem_context pctx;
905 clear_problem_context(&pctx);
907 if (ext2fs_fast_test_block_bitmap(ctx->block_found_map, block)) {
908 if (!ctx->block_dup_map) {
909 pctx.errcode = ext2fs_allocate_block_bitmap(ctx->fs,
910 _("multiply claimed block map"),
911 &ctx->block_dup_map);
914 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR,
916 /* Should never get here */
917 ctx->flags |= E2F_FLAG_ABORT;
921 ext2fs_fast_mark_block_bitmap(ctx->block_dup_map, block);
923 ext2fs_fast_mark_block_bitmap(ctx->block_found_map, block);
928 * Adjust the extended attribute block's reference counts at the end
929 * of pass 1, either by subtracting out references for EA blocks that
930 * are still referenced in ctx->refcount, or by adding references for
931 * EA blocks that had extra references as accounted for in
932 * ctx->refcount_extra.
934 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
935 char *block_buf, int adjust_sign)
937 struct ext2_ext_attr_header *header;
938 struct problem_context pctx;
939 ext2_filsys fs = ctx->fs;
944 clear_problem_context(&pctx);
946 ea_refcount_intr_begin(refcount);
948 if ((blk = ea_refcount_intr_next(refcount, &count)) == 0)
951 pctx.errcode = ext2fs_read_ext_attr(fs, blk, block_buf);
953 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
956 header = (struct ext2_ext_attr_header *) block_buf;
957 pctx.blkcount = header->h_refcount;
958 should_be = header->h_refcount + adjust_sign * count;
959 pctx.num = should_be;
960 if (fix_problem(ctx, PR_1_EXTATTR_REFCOUNT, &pctx)) {
961 header->h_refcount = should_be;
962 pctx.errcode = ext2fs_write_ext_attr(fs, blk,
965 fix_problem(ctx, PR_1_EXTATTR_WRITE, &pctx);
973 * Handle processing the extended attribute blocks
975 static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
978 ext2_filsys fs = ctx->fs;
979 ext2_ino_t ino = pctx->ino;
980 struct ext2_inode *inode = pctx->inode;
983 struct ext2_ext_attr_header *header;
984 struct ext2_ext_attr_entry *entry;
989 blk = inode->i_file_acl;
994 * If the Extended attribute flag isn't set, then a non-zero
995 * file acl means that the inode is corrupted.
997 * Or if the extended attribute block is an invalid block,
998 * then the inode is also corrupted.
1000 if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR) ||
1001 (blk < fs->super->s_first_data_block) ||
1002 (blk >= fs->super->s_blocks_count)) {
1003 mark_inode_bad(ctx, ino);
1007 /* If ea bitmap hasn't been allocated, create it */
1008 if (!ctx->block_ea_map) {
1009 pctx->errcode = ext2fs_allocate_block_bitmap(fs,
1010 _("ext attr block map"),
1011 &ctx->block_ea_map);
1012 if (pctx->errcode) {
1014 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, pctx);
1015 ctx->flags |= E2F_FLAG_ABORT;
1020 /* Create the EA refcount structure if necessary */
1021 if (!ctx->refcount) {
1022 pctx->errcode = ea_refcount_create(0, &ctx->refcount);
1023 if (pctx->errcode) {
1025 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
1026 ctx->flags |= E2F_FLAG_ABORT;
1032 /* Debugging text */
1033 printf("Inode %u has EA block %u\n", ino, blk);
1036 /* Have we seen this EA block before? */
1037 if (ext2fs_fast_test_block_bitmap(ctx->block_ea_map, blk)) {
1038 if (ea_refcount_decrement(ctx->refcount, blk, 0) == 0)
1040 /* Ooops, this EA was referenced more than it stated */
1041 if (!ctx->refcount_extra) {
1042 pctx->errcode = ea_refcount_create(0,
1043 &ctx->refcount_extra);
1044 if (pctx->errcode) {
1046 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
1047 ctx->flags |= E2F_FLAG_ABORT;
1051 ea_refcount_increment(ctx->refcount_extra, blk, 0);
1056 * OK, we haven't seen this EA block yet. So we need to
1060 pctx->errcode = ext2fs_read_ext_attr(fs, blk, block_buf);
1061 if (pctx->errcode && fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx))
1063 header = (struct ext2_ext_attr_header *) block_buf;
1064 pctx->blk = inode->i_file_acl;
1065 if (header->h_magic != EXT2_EXT_ATTR_MAGIC_v1)
1067 if (header->h_magic != EXT2_EXT_ATTR_MAGIC)
1069 else if (fix_problem(ctx, PR_1_BAD_EA_BLOCK, pctx))
1072 if (header->h_blocks != 1) {
1073 if (fix_problem(ctx, PR_1_EA_MULTI_BLOCK, pctx))
1077 region = region_create(0, fs->blocksize);
1079 fix_problem(ctx, PR_1_EA_ALLOC_REGION, pctx);
1080 ctx->flags |= E2F_FLAG_ABORT;
1083 if (region_allocate(region, 0, sizeof(struct ext2_ext_attr_header))) {
1084 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1088 entry = (struct ext2_ext_attr_entry *)(header+1);
1089 end = block_buf + fs->blocksize;
1090 while ((char *)entry < end && *(__u32 *)entry) {
1091 if (region_allocate(region, (char *)entry - (char *)header,
1092 EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
1093 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1096 if ((ext_attr_ver == 1 &&
1097 (entry->e_name_len == 0 || entry->e_name_index != 0)) ||
1098 (ext_attr_ver == 2 &&
1099 entry->e_name_index == 0)) {
1100 if (fix_problem(ctx, PR_1_EA_BAD_NAME, pctx))
1103 if (entry->e_value_block != 0) {
1104 if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
1107 if (entry->e_value_size &&
1108 region_allocate(region, entry->e_value_offs,
1109 EXT2_EXT_ATTR_SIZE(entry->e_value_size))) {
1110 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1113 entry = EXT2_EXT_ATTR_NEXT(entry);
1115 if (region_allocate(region, (char *)entry - (char *)header, 4)) {
1116 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1119 region_free(region);
1121 count = header->h_refcount - 1;
1123 ea_refcount_store(ctx->refcount, blk, count);
1124 mark_block_used(ctx, blk);
1125 ext2fs_fast_mark_block_bitmap(ctx->block_ea_map, blk);
1130 inode->i_file_acl = 0;
1131 e2fsck_write_inode(ctx, ino, inode, "check_ext_attr");
1135 /* Returns 1 if bad htree, 0 if OK */
1136 static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
1137 ext2_ino_t ino, struct ext2_inode *inode,
1140 struct ext2_dx_root_info *root;
1141 ext2_filsys fs = ctx->fs;
1145 if ((!LINUX_S_ISDIR(inode->i_mode) &&
1146 fix_problem(ctx, PR_1_HTREE_NODIR, pctx)) ||
1147 (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) &&
1148 fix_problem(ctx, PR_1_HTREE_SET, pctx)))
1151 blk = inode->i_block[0];
1153 (blk < fs->super->s_first_data_block) ||
1154 (blk >= fs->super->s_blocks_count)) &&
1155 fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1158 retval = io_channel_read_blk(fs->io, blk, 1, block_buf);
1159 if (retval && fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1162 /* XXX should check that beginning matches a directory */
1163 root = (struct ext2_dx_root_info *) (block_buf + 24);
1165 if ((root->reserved_zero || root->info_length < 8) &&
1166 fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1169 pctx->num = root->hash_version;
1170 if ((root->hash_version != EXT2_HASH_LEGACY) &&
1171 (root->hash_version != EXT2_HASH_HALF_MD4) &&
1172 fix_problem(ctx, PR_1_HTREE_HASHV, pctx))
1175 if ((root->unused_flags & EXT2_HASH_FLAG_INCOMPAT) &&
1176 fix_problem(ctx, PR_1_HTREE_INCOMPAT, pctx))
1179 pctx->num = root->indirect_levels;
1180 if ((root->indirect_levels > 1) &&
1181 fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
1188 * This subroutine is called on each inode to account for all of the
1189 * blocks used by that inode.
1191 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
1194 ext2_filsys fs = ctx->fs;
1195 struct process_block_struct pb;
1196 ext2_ino_t ino = pctx->ino;
1197 struct ext2_inode *inode = pctx->inode;
1199 int dirty_inode = 0;
1202 if (!ext2fs_inode_has_valid_blocks(inode))
1206 pb.num_blocks = pb.last_block = 0;
1207 pb.num_illegal_blocks = 0;
1208 pb.suppress = 0; pb.clear = 0;
1211 pb.previous_block = 0;
1212 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
1213 pb.is_reg = LINUX_S_ISREG(inode->i_mode);
1214 pb.max_blocks = 1 << (31 - fs->super->s_log_block_size);
1220 if (inode->i_flags & EXT2_COMPRBLK_FL) {
1221 if (fs->super->s_feature_incompat &
1222 EXT2_FEATURE_INCOMPAT_COMPRESSION)
1225 if (fix_problem(ctx, PR_1_COMPR_SET, pctx)) {
1226 inode->i_flags &= ~EXT2_COMPRBLK_FL;
1232 pctx->errcode = ext2fs_block_iterate2(fs, ino,
1233 pb.is_dir ? BLOCK_FLAG_HOLE : 0,
1234 block_buf, process_block, &pb);
1235 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1237 end_problem_latch(ctx, PR_LATCH_BLOCK);
1238 end_problem_latch(ctx, PR_LATCH_TOOBIG);
1240 fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx);
1242 if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group)
1243 ctx->fs_fragmented++;
1246 inode->i_links_count = 0;
1247 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
1248 inode->i_dtime = time(0);
1250 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
1251 ext2fs_unmark_inode_bitmap(ctx->inode_reg_map, ino);
1252 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
1254 * The inode was probably partially accounted for
1255 * before processing was aborted, so we need to
1256 * restart the pass 1 scan.
1258 ctx->flags |= E2F_FLAG_RESTART;
1262 if (inode->i_flags & EXT2_INDEX_FL) {
1263 if (handle_htree(ctx, pctx, ino, inode, block_buf)) {
1264 inode->i_flags &= ~EXT2_INDEX_FL;
1268 e2fsck_add_dx_dir(ctx, ino, pb.last_block+1);
1273 if (inode->i_file_acl && check_ext_attr(ctx, pctx, block_buf))
1276 pb.num_blocks *= (fs->blocksize / 512);
1278 printf("inode %u, i_size = %lu, last_block = %lld, i_blocks=%lu, num_blocks = %lu\n",
1279 ino, inode->i_size, pb.last_block, inode->i_blocks,
1282 if (!pb.num_blocks && pb.is_dir) {
1283 if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) {
1284 inode->i_links_count = 0;
1285 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
1286 inode->i_dtime = time(0);
1288 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
1289 ext2fs_unmark_inode_bitmap(ctx->inode_reg_map, ino);
1290 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
1291 ctx->fs_directory_count--;
1296 int nblock = inode->i_size >> EXT2_BLOCK_SIZE_BITS(fs->super);
1297 if (nblock > (pb.last_block + 1))
1299 else if (nblock < (pb.last_block + 1)) {
1300 if (((pb.last_block + 1) - nblock) >
1301 fs->super->s_prealloc_dir_blocks)
1305 if (!LINUX_S_ISREG(inode->i_mode) && inode->i_size_high)
1307 size = inode->i_size | ((__u64) inode->i_size_high << 32);
1308 if ((size < pb.last_block * fs->blocksize))
1310 else if (size > ext2_max_sizes[fs->super->s_log_block_size])
1314 pctx->num = (pb.last_block+1) * fs->blocksize;
1315 if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
1316 inode->i_size = pctx->num;
1317 if (LINUX_S_ISREG(inode->i_mode))
1318 inode->i_size_high = pctx->num >> 32;
1323 if (LINUX_S_ISREG(inode->i_mode) &&
1324 (inode->i_size_high || inode->i_size & 0x80000000UL))
1326 if (pb.num_blocks != inode->i_blocks) {
1327 pctx->num = pb.num_blocks;
1328 if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) {
1329 inode->i_blocks = pb.num_blocks;
1336 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
1341 * Helper function called by process block when an illegal block is
1342 * found. It returns a description about why the block is illegal
1344 static char *describe_illegal_block(ext2_filsys fs, blk_t block)
1348 static char problem[80];
1350 super = fs->super->s_first_data_block;
1351 strcpy(problem, "PROGRAMMING ERROR: Unknown reason for illegal block");
1352 if (block < super) {
1353 sprintf(problem, "< FIRSTBLOCK (%u)", super);
1355 } else if (block >= fs->super->s_blocks_count) {
1356 sprintf(problem, "> BLOCKS (%u)", fs->super->s_blocks_count);
1359 for (i = 0; i < fs->group_desc_count; i++) {
1360 if (block == super) {
1361 sprintf(problem, "is the superblock in group %d", i);
1364 if (block > super &&
1365 block <= (super + fs->desc_blocks)) {
1366 sprintf(problem, "is in the group descriptors "
1370 if (block == fs->group_desc[i].bg_block_bitmap) {
1371 sprintf(problem, "is the block bitmap of group %d", i);
1374 if (block == fs->group_desc[i].bg_inode_bitmap) {
1375 sprintf(problem, "is the inode bitmap of group %d", i);
1378 if (block >= fs->group_desc[i].bg_inode_table &&
1379 (block < fs->group_desc[i].bg_inode_table
1380 + fs->inode_blocks_per_group)) {
1381 sprintf(problem, "is in the inode table of group %d",
1385 super += fs->super->s_blocks_per_group;
1392 * This is a helper function for check_blocks().
1394 static int process_block(ext2_filsys fs,
1396 e2_blkcnt_t blockcnt,
1401 struct process_block_struct *p;
1402 struct problem_context *pctx;
1403 blk_t blk = *block_nr;
1408 p = (struct process_block_struct *) priv_data;
1412 if (p->compressed && (blk == EXT2FS_COMPRESSED_BLKADDR)) {
1413 /* todo: Check that the comprblk_fl is high, that the
1414 blkaddr pattern looks right (all non-holes up to
1415 first EXT2FS_COMPRESSED_BLKADDR, then all
1416 EXT2FS_COMPRESSED_BLKADDR up to end of cluster),
1417 that the feature_incompat bit is high, and that the
1418 inode is a regular file. If we're doing a "full
1419 check" (a concept introduced to e2fsck by e2compr,
1420 meaning that we look at data blocks as well as
1421 metadata) then call some library routine that
1422 checks the compressed data. I'll have to think
1423 about this, because one particularly important
1424 problem to be able to fix is to recalculate the
1425 cluster size if necessary. I think that perhaps
1426 we'd better do most/all e2compr-specific checks
1427 separately, after the non-e2compr checks. If not
1428 doing a full check, it may be useful to test that
1429 the personality is linux; e.g. if it isn't then
1430 perhaps this really is just an illegal block. */
1435 if (p->is_dir == 0) {
1437 * Should never happen, since only directories
1438 * get called with BLOCK_FLAG_HOLE
1441 printf("process_block() called with blk == 0, "
1442 "blockcnt=%d, inode %lu???\n",
1449 if (blockcnt * fs->blocksize < p->inode->i_size) {
1451 printf("Missing block (#%d) in directory inode %lu!\n",
1460 printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk,
1465 * Simplistic fragmentation check. We merely require that the
1466 * file be contiguous. (Which can never be true for really
1467 * big files that are greater than a block group.)
1469 if (!HOLE_BLKADDR(p->previous_block)) {
1470 if (p->previous_block+1 != blk)
1473 p->previous_block = blk;
1475 if (p->is_dir && blockcnt > (1 << (15 - fs->super->s_log_block_size)))
1476 problem = PR_1_TOOBIG_DIR;
1477 if (p->is_reg && p->num_blocks+1 >= p->max_blocks)
1478 problem = PR_1_TOOBIG_REG;
1479 if (!p->is_dir && !p->is_reg && blockcnt > 0)
1480 problem = PR_1_TOOBIG_SYMLINK;
1482 if (blk < fs->super->s_first_data_block ||
1483 blk >= fs->super->s_blocks_count)
1484 problem = PR_1_ILLEGAL_BLOCK_NUM;
1487 p->num_illegal_blocks++;
1488 if (!p->suppress && (p->num_illegal_blocks % 12) == 0) {
1489 if (fix_problem(ctx, PR_1_TOO_MANY_BAD_BLOCKS, pctx)) {
1493 if (fix_problem(ctx, PR_1_SUPPRESS_MESSAGES, pctx)) {
1495 set_latch_flags(PR_LATCH_BLOCK,
1500 pctx->blkcount = blockcnt;
1501 if (fix_problem(ctx, problem, pctx)) {
1502 blk = *block_nr = 0;
1503 ret_code = BLOCK_CHANGED;
1509 mark_block_used(ctx, blk);
1512 p->last_block = blockcnt;
1514 if (p->is_dir && (blockcnt >= 0)) {
1515 pctx->errcode = ext2fs_add_dir_block(fs->dblist, p->ino,
1517 if (pctx->errcode) {
1519 pctx->num = blockcnt;
1520 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
1521 /* Should never get here */
1522 ctx->flags |= E2F_FLAG_ABORT;
1529 static void bad_block_indirect(e2fsck_t ctx, blk_t blk)
1531 struct problem_context pctx;
1533 clear_problem_context(&pctx);
1535 * Prompt to see if we should continue or not.
1537 if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, &pctx))
1538 ctx->flags |= E2F_FLAG_ABORT;
1541 static int process_bad_block(ext2_filsys fs,
1543 e2_blkcnt_t blockcnt,
1548 struct process_block_struct *p;
1549 blk_t blk = *block_nr;
1552 struct problem_context *pctx;
1556 * Note: This function processes blocks for the bad blocks
1557 * inode, which is never compressed. So we don't use HOLE_BLKADDR().
1563 p = (struct process_block_struct *) priv_data;
1567 pctx->ino = EXT2_BAD_INO;
1569 pctx->blkcount = blockcnt;
1571 if ((blk < fs->super->s_first_data_block) ||
1572 (blk >= fs->super->s_blocks_count)) {
1573 if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) {
1575 return BLOCK_CHANGED;
1581 if (ext2fs_test_block_bitmap(ctx->block_found_map, blk)) {
1582 bad_block_indirect(ctx, blk);
1583 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1586 mark_block_used(ctx, blk);
1590 printf ("DEBUG: Marking %u as bad.\n", blk);
1592 ctx->fs_badblocks_count++;
1594 * If the block is not used, then mark it as used and return.
1595 * If it is already marked as found, this must mean that
1596 * there's an overlap between the filesystem table blocks
1597 * (bitmaps and inode table) and the bad block list.
1599 if (!ext2fs_test_block_bitmap(ctx->block_found_map, blk)) {
1600 ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
1604 * Try to find the where the filesystem block was used...
1606 first_block = fs->super->s_first_data_block;
1608 for (i = 0; i < fs->group_desc_count; i++ ) {
1611 if (!ext2fs_bg_has_super(fs, i))
1613 if (blk == first_block) {
1615 if (fix_problem(ctx,
1616 PR_1_BAD_PRIMARY_SUPERBLOCK,
1619 return BLOCK_CHANGED;
1623 fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx);
1626 if ((blk > first_block) &&
1627 (blk <= first_block + fs->desc_blocks)) {
1629 pctx->blk = *block_nr;
1630 if (fix_problem(ctx,
1631 PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) {
1633 return BLOCK_CHANGED;
1637 fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx);
1641 if (blk == fs->group_desc[i].bg_block_bitmap) {
1642 if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) {
1643 ctx->invalid_block_bitmap_flag[i]++;
1644 ctx->invalid_bitmaps++;
1648 if (blk == fs->group_desc[i].bg_inode_bitmap) {
1649 if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) {
1650 ctx->invalid_inode_bitmap_flag[i]++;
1651 ctx->invalid_bitmaps++;
1655 if ((blk >= fs->group_desc[i].bg_inode_table) &&
1656 (blk < (fs->group_desc[i].bg_inode_table +
1657 fs->inode_blocks_per_group))) {
1659 * If there are bad blocks in the inode table,
1660 * the inode scan code will try to do
1661 * something reasonable automatically.
1665 first_block += fs->super->s_blocks_per_group;
1668 * If we've gotten to this point, then the only
1669 * possibility is that the bad block inode meta data
1670 * is using a bad block.
1672 if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) ||
1673 p->inode->i_block[EXT2_DIND_BLOCK]) {
1674 bad_block_indirect(ctx, blk);
1675 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1682 /* Warn user that the block wasn't claimed */
1683 fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx);
1688 static void new_table_block(e2fsck_t ctx, blk_t first_block, int group,
1689 const char *name, int num, blk_t *new_block)
1691 ext2_filsys fs = ctx->fs;
1692 blk_t old_block = *new_block;
1695 struct problem_context pctx;
1697 clear_problem_context(&pctx);
1700 pctx.blk = old_block;
1703 pctx.errcode = ext2fs_get_free_blocks(fs, first_block,
1704 first_block + fs->super->s_blocks_per_group,
1705 num, ctx->block_found_map, new_block);
1708 fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx);
1709 ext2fs_unmark_valid(fs);
1712 pctx.errcode = ext2fs_get_mem(fs->blocksize, (void **) &buf);
1714 fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
1715 ext2fs_unmark_valid(fs);
1718 ext2fs_mark_super_dirty(fs);
1719 pctx.blk2 = *new_block;
1720 fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO :
1721 PR_1_RELOC_TO), &pctx);
1723 for (i = 0; i < num; i++) {
1725 ext2fs_mark_block_bitmap(ctx->block_found_map, (*new_block)+i);
1727 pctx.errcode = io_channel_read_blk(fs->io,
1728 old_block + i, 1, buf);
1730 fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx);
1732 memset(buf, 0, fs->blocksize);
1734 pctx.blk = (*new_block) + i;
1735 pctx.errcode = io_channel_write_blk(fs->io, pctx.blk,
1738 fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
1740 ext2fs_free_mem((void **) &buf);
1744 * This routine gets called at the end of pass 1 if bad blocks are
1745 * detected in the superblock, group descriptors, inode_bitmaps, or
1746 * block bitmaps. At this point, all of the blocks have been mapped
1747 * out, so we can try to allocate new block(s) to replace the bad
1750 static void handle_fs_bad_blocks(e2fsck_t ctx)
1752 ext2_filsys fs = ctx->fs;
1754 int first_block = fs->super->s_first_data_block;
1756 for (i = 0; i < fs->group_desc_count; i++) {
1757 if (ctx->invalid_block_bitmap_flag[i]) {
1758 new_table_block(ctx, first_block, i, _("block bitmap"),
1759 1, &fs->group_desc[i].bg_block_bitmap);
1761 if (ctx->invalid_inode_bitmap_flag[i]) {
1762 new_table_block(ctx, first_block, i, _("inode bitmap"),
1763 1, &fs->group_desc[i].bg_inode_bitmap);
1765 if (ctx->invalid_inode_table_flag[i]) {
1766 new_table_block(ctx, first_block, i, _("inode table"),
1767 fs->inode_blocks_per_group,
1768 &fs->group_desc[i].bg_inode_table);
1769 ctx->flags |= E2F_FLAG_RESTART;
1771 first_block += fs->super->s_blocks_per_group;
1773 ctx->invalid_bitmaps = 0;
1777 * This routine marks all blocks which are used by the superblock,
1778 * group descriptors, inode bitmaps, and block bitmaps.
1780 static void mark_table_blocks(e2fsck_t ctx)
1782 ext2_filsys fs = ctx->fs;
1785 struct problem_context pctx;
1787 clear_problem_context(&pctx);
1789 block = fs->super->s_first_data_block;
1790 for (i = 0; i < fs->group_desc_count; i++) {
1793 if (ext2fs_bg_has_super(fs, i)) {
1795 * Mark this group's copy of the superblock
1797 ext2fs_mark_block_bitmap(ctx->block_found_map, block);
1800 * Mark this group's copy of the descriptors
1802 for (j = 0; j < fs->desc_blocks; j++) {
1803 ext2fs_mark_block_bitmap(ctx->block_found_map,
1809 * Mark the blocks used for the inode table
1811 if (fs->group_desc[i].bg_inode_table) {
1812 for (j = 0, b = fs->group_desc[i].bg_inode_table;
1813 j < fs->inode_blocks_per_group;
1815 if (ext2fs_test_block_bitmap(ctx->block_found_map,
1818 if (fix_problem(ctx,
1819 PR_1_ITABLE_CONFLICT, &pctx)) {
1820 ctx->invalid_inode_table_flag[i]++;
1821 ctx->invalid_bitmaps++;
1824 ext2fs_mark_block_bitmap(ctx->block_found_map,
1831 * Mark block used for the block bitmap
1833 if (fs->group_desc[i].bg_block_bitmap) {
1834 if (ext2fs_test_block_bitmap(ctx->block_found_map,
1835 fs->group_desc[i].bg_block_bitmap)) {
1836 pctx.blk = fs->group_desc[i].bg_block_bitmap;
1837 if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) {
1838 ctx->invalid_block_bitmap_flag[i]++;
1839 ctx->invalid_bitmaps++;
1842 ext2fs_mark_block_bitmap(ctx->block_found_map,
1843 fs->group_desc[i].bg_block_bitmap);
1848 * Mark block used for the inode bitmap
1850 if (fs->group_desc[i].bg_inode_bitmap) {
1851 if (ext2fs_test_block_bitmap(ctx->block_found_map,
1852 fs->group_desc[i].bg_inode_bitmap)) {
1853 pctx.blk = fs->group_desc[i].bg_inode_bitmap;
1854 if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) {
1855 ctx->invalid_inode_bitmap_flag[i]++;
1856 ctx->invalid_bitmaps++;
1859 ext2fs_mark_block_bitmap(ctx->block_found_map,
1860 fs->group_desc[i].bg_inode_bitmap);
1863 block += fs->super->s_blocks_per_group;
1868 * Thes subroutines short circuits ext2fs_get_blocks and
1869 * ext2fs_check_directory; we use them since we already have the inode
1870 * structure, so there's no point in letting the ext2fs library read
1873 static errcode_t pass1_get_blocks(ext2_filsys fs, ext2_ino_t ino,
1876 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
1879 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
1880 return EXT2_ET_CALLBACK_NOTHANDLED;
1882 for (i=0; i < EXT2_N_BLOCKS; i++)
1883 blocks[i] = ctx->stashed_inode->i_block[i];
1887 static errcode_t pass1_read_inode(ext2_filsys fs, ext2_ino_t ino,
1888 struct ext2_inode *inode)
1890 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
1892 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
1893 return EXT2_ET_CALLBACK_NOTHANDLED;
1894 *inode = *ctx->stashed_inode;
1898 static errcode_t pass1_write_inode(ext2_filsys fs, ext2_ino_t ino,
1899 struct ext2_inode *inode)
1901 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
1903 if ((ino == ctx->stashed_ino) && ctx->stashed_inode)
1904 *ctx->stashed_inode = *inode;
1905 return EXT2_ET_CALLBACK_NOTHANDLED;
1908 static errcode_t pass1_check_directory(ext2_filsys fs, ext2_ino_t ino)
1910 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
1912 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
1913 return EXT2_ET_CALLBACK_NOTHANDLED;
1915 if (!LINUX_S_ISDIR(ctx->stashed_inode->i_mode))
1916 return EXT2_ET_NO_DIRECTORY;
1920 void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int bool)
1922 ext2_filsys fs = ctx->fs;
1925 fs->get_blocks = pass1_get_blocks;
1926 fs->check_directory = pass1_check_directory;
1927 fs->read_inode = pass1_read_inode;
1928 fs->write_inode = pass1_write_inode;
1929 ctx->stashed_ino = 0;
1932 fs->check_directory = 0;
1934 fs->write_inode = 0;