Home | History | Annotate | Download | only in e2fsck
      1 /*
      2  * pass4.c -- pass #4 of e2fsck: Check reference counts
      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 4 frees the following data structures:
     12  * 	- A bitmap of which inodes are in bad blocks.	(inode_bb_map)
     13  * 	- A bitmap of which inodes are imagic inodes.	(inode_imagic_map)
     14  */
     15 
     16 #include "e2fsck.h"
     17 #include "problem.h"
     18 #include <ext2fs/ext2_ext_attr.h>
     19 
     20 /*
     21  * This routine is called when an inode is not connected to the
     22  * directory tree.
     23  *
     24  * This subroutine returns 1 then the caller shouldn't bother with the
     25  * rest of the pass 4 tests.
     26  */
     27 static int disconnect_inode(e2fsck_t ctx, ext2_ino_t i,
     28 			    struct ext2_inode *inode)
     29 {
     30 	ext2_filsys fs = ctx->fs;
     31 	struct problem_context	pctx;
     32 	__u32 eamagic = 0;
     33 	int extra_size = 0;
     34 
     35 	if (EXT2_INODE_SIZE(fs->super) > EXT2_GOOD_OLD_INODE_SIZE) {
     36 		e2fsck_read_inode_full(ctx, i, inode,EXT2_INODE_SIZE(fs->super),
     37 				       "pass4: disconnect_inode");
     38 		extra_size = ((struct ext2_inode_large *)inode)->i_extra_isize;
     39 	} else {
     40 		e2fsck_read_inode(ctx, i, inode, "pass4: disconnect_inode");
     41 	}
     42 	clear_problem_context(&pctx);
     43 	pctx.ino = i;
     44 	pctx.inode = inode;
     45 
     46 	if (EXT2_INODE_SIZE(fs->super) -EXT2_GOOD_OLD_INODE_SIZE -extra_size >0)
     47 		eamagic = *(__u32 *)(((char *)inode) +EXT2_GOOD_OLD_INODE_SIZE +
     48 				     extra_size);
     49 	/*
     50 	 * Offer to delete any zero-length files that does not have
     51 	 * blocks.  If there is an EA block, it might have useful
     52 	 * information, so we won't prompt to delete it, but let it be
     53 	 * reconnected to lost+found.
     54 	 */
     55 	if (!inode->i_blocks && eamagic != EXT2_EXT_ATTR_MAGIC &&
     56 	    (LINUX_S_ISREG(inode->i_mode) || LINUX_S_ISDIR(inode->i_mode))) {
     57 		if (fix_problem(ctx, PR_4_ZERO_LEN_INODE, &pctx)) {
     58 			e2fsck_clear_inode(ctx, i, inode, 0,
     59 					   "disconnect_inode");
     60 			/*
     61 			 * Fix up the bitmaps...
     62 			 */
     63 			e2fsck_read_bitmaps(ctx);
     64 			ext2fs_inode_alloc_stats2(fs, i, -1,
     65 						  LINUX_S_ISDIR(inode->i_mode));
     66 			return 0;
     67 		}
     68 	}
     69 
     70 	/*
     71 	 * Prompt to reconnect.
     72 	 */
     73 	if (fix_problem(ctx, PR_4_UNATTACHED_INODE, &pctx)) {
     74 		if (e2fsck_reconnect_file(ctx, i))
     75 			ext2fs_unmark_valid(fs);
     76 	} else {
     77 		/*
     78 		 * If we don't attach the inode, then skip the
     79 		 * i_links_test since there's no point in trying to
     80 		 * force i_links_count to zero.
     81 		 */
     82 		ext2fs_unmark_valid(fs);
     83 		return 1;
     84 	}
     85 	return 0;
     86 }
     87 
     88 
     89 void e2fsck_pass4(e2fsck_t ctx)
     90 {
     91 	ext2_filsys fs = ctx->fs;
     92 	ext2_ino_t	i;
     93 	struct ext2_inode	*inode;
     94 #ifdef RESOURCE_TRACK
     95 	struct resource_track	rtrack;
     96 #endif
     97 	struct problem_context	pctx;
     98 	__u16	link_count, link_counted;
     99 	char	*buf = 0;
    100 	int	group, maxgroup;
    101 
    102 	init_resource_track(&rtrack, ctx->fs->io);
    103 
    104 #ifdef MTRACE
    105 	mtrace_print("Pass 4");
    106 #endif
    107 
    108 	clear_problem_context(&pctx);
    109 
    110 	if (!(ctx->options & E2F_OPT_PREEN))
    111 		fix_problem(ctx, PR_4_PASS_HEADER, &pctx);
    112 
    113 	group = 0;
    114 	maxgroup = fs->group_desc_count;
    115 	if (ctx->progress)
    116 		if ((ctx->progress)(ctx, 4, 0, maxgroup))
    117 			return;
    118 
    119 	inode = e2fsck_allocate_memory(ctx, EXT2_INODE_SIZE(fs->super),
    120 				       "scratch inode");
    121 
    122 	/* Protect loop from wrap-around if s_inodes_count maxed */
    123 	for (i=1; i <= fs->super->s_inodes_count && i > 0; i++) {
    124 		if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
    125 			goto errout;
    126 		if ((i % fs->super->s_inodes_per_group) == 0) {
    127 			group++;
    128 			if (ctx->progress)
    129 				if ((ctx->progress)(ctx, 4, group, maxgroup))
    130 					goto errout;
    131 		}
    132 		if (i == EXT2_BAD_INO ||
    133 		    (i > EXT2_ROOT_INO && i < EXT2_FIRST_INODE(fs->super)))
    134 			continue;
    135 		if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map, i)) ||
    136 		    (ctx->inode_imagic_map &&
    137 		     ext2fs_test_inode_bitmap(ctx->inode_imagic_map, i)) ||
    138 		    (ctx->inode_bb_map &&
    139 		     ext2fs_test_inode_bitmap(ctx->inode_bb_map, i)))
    140 			continue;
    141 		ext2fs_icount_fetch(ctx->inode_link_info, i, &link_count);
    142 		ext2fs_icount_fetch(ctx->inode_count, i, &link_counted);
    143 		if (link_counted == 0) {
    144 			if (!buf)
    145 				buf = e2fsck_allocate_memory(ctx,
    146 				     fs->blocksize, "bad_inode buffer");
    147 			if (e2fsck_process_bad_inode(ctx, 0, i, buf))
    148 				continue;
    149 			if (disconnect_inode(ctx, i, inode))
    150 				continue;
    151 			ext2fs_icount_fetch(ctx->inode_link_info, i,
    152 					    &link_count);
    153 			ext2fs_icount_fetch(ctx->inode_count, i,
    154 					    &link_counted);
    155 		}
    156 		if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, i) &&
    157 		    (link_counted > EXT2_LINK_MAX))
    158 			link_counted = 1;
    159 		if (link_counted != link_count) {
    160 			e2fsck_read_inode(ctx, i, inode, "pass4");
    161 			pctx.ino = i;
    162 			pctx.inode = inode;
    163 			if (link_count != inode->i_links_count) {
    164 				pctx.num = link_count;
    165 				fix_problem(ctx,
    166 					    PR_4_INCONSISTENT_COUNT, &pctx);
    167 			}
    168 			pctx.num = link_counted;
    169 			/* i_link_count was previously exceeded, but no longer
    170 			 * is, fix this but don't consider it an error */
    171 			if ((LINUX_S_ISDIR(inode->i_mode) && link_counted > 1 &&
    172 			     (inode->i_flags & EXT2_INDEX_FL) &&
    173 			     link_count == 1 && !(ctx->options & E2F_OPT_NO)) ||
    174 			     (fix_problem(ctx, PR_4_BAD_REF_COUNT, &pctx))) {
    175 				inode->i_links_count = link_counted;
    176 				e2fsck_write_inode(ctx, i, inode, "pass4");
    177 			}
    178 		}
    179 	}
    180 	ext2fs_free_icount(ctx->inode_link_info); ctx->inode_link_info = 0;
    181 	ext2fs_free_icount(ctx->inode_count); ctx->inode_count = 0;
    182 	ext2fs_free_inode_bitmap(ctx->inode_bb_map);
    183 	ctx->inode_bb_map = 0;
    184 	ext2fs_free_inode_bitmap(ctx->inode_imagic_map);
    185 	ctx->inode_imagic_map = 0;
    186 errout:
    187 	if (buf)
    188 		ext2fs_free_mem(&buf);
    189 
    190 	ext2fs_free_mem(&inode);
    191 	print_resource_track(ctx, _("Pass 4"), &rtrack, ctx->fs->io);
    192 }
    193 
    194