2 * pass4.c -- pass #4 of e2fsck: Check reference counts
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 4 frees the following data structures:
12 * - A bitmap of which inodes are in bad blocks. (inode_bb_map)
19 * This routine is called when an inode is not connected to the
22 * This subroutine returns 1 then the caller shouldn't bother with the
23 * rest of the pass 4 tests.
25 static int disconnect_inode(e2fsck_t ctx, ino_t i)
27 ext2_filsys fs = ctx->fs;
28 struct ext2_inode inode;
29 struct problem_context pctx;
31 e2fsck_read_inode(ctx, i, &inode, "pass4: disconnect_inode");
32 clear_problem_context(&pctx);
36 if (!inode.i_blocks && (LINUX_S_ISREG(inode.i_mode) ||
37 LINUX_S_ISDIR(inode.i_mode))) {
39 * This is a zero-length file; prompt to delete it...
41 if (fix_problem(ctx, PR_4_ZERO_LEN_INODE, &pctx)) {
42 ext2fs_icount_store(ctx->inode_link_info, i, 0);
43 inode.i_links_count = 0;
44 inode.i_dtime = time(0);
45 e2fsck_write_inode(ctx, i, &inode,
48 * Fix up the bitmaps...
50 e2fsck_read_bitmaps(ctx);
51 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, i);
52 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, i);
53 ext2fs_unmark_inode_bitmap(fs->inode_map, i);
54 ext2fs_mark_ib_dirty(fs);
60 * Prompt to reconnect.
62 if (fix_problem(ctx, PR_4_UNATTACHED_INODE, &pctx)) {
63 if (e2fsck_reconnect_file(ctx, i))
64 ext2fs_unmark_valid(fs);
67 * If we don't attach the inode, then skip the
68 * i_links_test since there's no point in trying to
69 * force i_links_count to zero.
71 ext2fs_unmark_valid(fs);
78 void e2fsck_pass4(e2fsck_t ctx)
80 ext2_filsys fs = ctx->fs;
82 struct ext2_inode inode;
84 struct resource_track rtrack;
86 struct problem_context pctx;
87 __u16 link_count, link_counted;
91 init_resource_track(&rtrack);
95 mtrace_print("Pass 4");
98 clear_problem_context(&pctx);
100 if (!(ctx->options & E2F_OPT_PREEN))
101 fix_problem(ctx, PR_4_PASS_HEADER, &pctx);
104 maxgroup = fs->group_desc_count;
106 if ((ctx->progress)(ctx, 4, 0, maxgroup))
109 for (i=1; i <= fs->super->s_inodes_count; i++) {
110 if ((i % fs->super->s_inodes_per_group) == 0) {
113 if ((ctx->progress)(ctx, 4, group, maxgroup))
116 if (i == EXT2_BAD_INO ||
117 (i > EXT2_ROOT_INO && i < EXT2_FIRST_INODE(fs->super)))
119 if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map, i)) ||
120 (ctx->inode_bb_map &&
121 ext2fs_test_inode_bitmap(ctx->inode_bb_map, i)))
123 ext2fs_icount_fetch(ctx->inode_link_info, i, &link_count);
124 ext2fs_icount_fetch(ctx->inode_count, i, &link_counted);
125 if (link_counted == 0) {
126 if (e2fsck_process_bad_inode(ctx, 0, i))
128 if (disconnect_inode(ctx, i))
130 ext2fs_icount_fetch(ctx->inode_link_info, i,
132 ext2fs_icount_fetch(ctx->inode_count, i,
135 if (link_counted != link_count) {
136 e2fsck_read_inode(ctx, i, &inode, "pass4");
139 if (link_count != inode.i_links_count) {
140 pctx.num = link_count;
142 PR_4_INCONSISTENT_COUNT, &pctx);
144 pctx.num = link_counted;
145 if (fix_problem(ctx, PR_4_BAD_REF_COUNT, &pctx)) {
146 inode.i_links_count = link_counted;
147 e2fsck_write_inode(ctx, i, &inode, "pass4");
151 ext2fs_free_icount(ctx->inode_link_info); ctx->inode_link_info = 0;
152 ext2fs_free_icount(ctx->inode_count); ctx->inode_count = 0;
153 ext2fs_free_inode_bitmap(ctx->inode_bb_map);
154 ctx->inode_bb_map = 0;
155 #ifdef RESOURCE_TRACK
156 if (ctx->options & E2F_OPT_TIME2) {
157 e2fsck_clear_progbar(ctx);
158 print_resource_track("Pass 4", &rtrack);