blob: 8f70f058ff4250596c6c43cf172b1e5a76a966c0 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * pass4.c -- pass #4 of e2fsck: Check reference counts
3 *
Theodore Ts'o21c84b71997-04-29 16:15:03 +00004 * 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)
Theodore Ts'oaa4115a1999-10-21 19:33:18 +000013 * - A bitmap of which inodes are imagic inodes. (inode_imagic_map)
Theodore Ts'o3839e651997-04-26 13:21:57 +000014 */
15
16#include "e2fsck.h"
Theodore Ts'o21c84b71997-04-29 16:15:03 +000017#include "problem.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000018
Theodore Ts'o5c576471997-04-29 15:29:49 +000019/*
20 * This routine is called when an inode is not connected to the
21 * directory tree.
22 *
23 * This subroutine returns 1 then the caller shouldn't bother with the
24 * rest of the pass 4 tests.
25 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000026static int disconnect_inode(e2fsck_t ctx, ino_t i)
Theodore Ts'o5c576471997-04-29 15:29:49 +000027{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000028 ext2_filsys fs = ctx->fs;
Theodore Ts'o5c576471997-04-29 15:29:49 +000029 struct ext2_inode inode;
Theodore Ts'o21c84b71997-04-29 16:15:03 +000030 struct problem_context pctx;
Theodore Ts'o5c576471997-04-29 15:29:49 +000031
Theodore Ts'o08b21301997-11-03 19:42:40 +000032 e2fsck_read_inode(ctx, i, &inode, "pass4: disconnect_inode");
Theodore Ts'o21c84b71997-04-29 16:15:03 +000033 clear_problem_context(&pctx);
34 pctx.ino = i;
35 pctx.inode = &inode;
36
Theodore Ts'o5c576471997-04-29 15:29:49 +000037 if (!inode.i_blocks && (LINUX_S_ISREG(inode.i_mode) ||
38 LINUX_S_ISDIR(inode.i_mode))) {
39 /*
40 * This is a zero-length file; prompt to delete it...
41 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000042 if (fix_problem(ctx, PR_4_ZERO_LEN_INODE, &pctx)) {
43 ext2fs_icount_store(ctx->inode_link_info, i, 0);
Theodore Ts'o5c576471997-04-29 15:29:49 +000044 inode.i_links_count = 0;
45 inode.i_dtime = time(0);
Theodore Ts'o08b21301997-11-03 19:42:40 +000046 e2fsck_write_inode(ctx, i, &inode,
Theodore Ts'o5c576471997-04-29 15:29:49 +000047 "disconnect_inode");
48 /*
49 * Fix up the bitmaps...
50 */
Theodore Ts'of8188ff1997-11-14 05:23:04 +000051 e2fsck_read_bitmaps(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000052 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, i);
53 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, i);
Theodore Ts'o5c576471997-04-29 15:29:49 +000054 ext2fs_unmark_inode_bitmap(fs->inode_map, i);
55 ext2fs_mark_ib_dirty(fs);
56 return 0;
57 }
58 }
59
60 /*
61 * Prompt to reconnect.
62 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000063 if (fix_problem(ctx, PR_4_UNATTACHED_INODE, &pctx)) {
Theodore Ts'o08b21301997-11-03 19:42:40 +000064 if (e2fsck_reconnect_file(ctx, i))
Theodore Ts'o5c576471997-04-29 15:29:49 +000065 ext2fs_unmark_valid(fs);
66 } else {
67 /*
68 * If we don't attach the inode, then skip the
69 * i_links_test since there's no point in trying to
70 * force i_links_count to zero.
71 */
72 ext2fs_unmark_valid(fs);
73 return 1;
74 }
75 return 0;
76}
77
78
Theodore Ts'o08b21301997-11-03 19:42:40 +000079void e2fsck_pass4(e2fsck_t ctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +000080{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000081 ext2_filsys fs = ctx->fs;
Theodore Ts'of3db3561997-04-26 13:34:30 +000082 ino_t i;
Theodore Ts'o3839e651997-04-26 13:21:57 +000083 struct ext2_inode inode;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000084#ifdef RESOURCE_TRACK
Theodore Ts'o3839e651997-04-26 13:21:57 +000085 struct resource_track rtrack;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000086#endif
Theodore Ts'o21c84b71997-04-29 16:15:03 +000087 struct problem_context pctx;
88 __u16 link_count, link_counted;
Theodore Ts'o7f813ba1998-09-03 01:26:03 +000089 int group, maxgroup;
Theodore Ts'o3839e651997-04-26 13:21:57 +000090
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000091#ifdef RESOURCE_TRACK
Theodore Ts'o3839e651997-04-26 13:21:57 +000092 init_resource_track(&rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000093#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000094
95#ifdef MTRACE
96 mtrace_print("Pass 4");
97#endif
98
Theodore Ts'o21c84b71997-04-29 16:15:03 +000099 clear_problem_context(&pctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000100
101 if (!(ctx->options & E2F_OPT_PREEN))
102 fix_problem(ctx, PR_4_PASS_HEADER, &pctx);
103
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000104 group = 0;
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000105 maxgroup = fs->group_desc_count;
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000106 if (ctx->progress)
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000107 if ((ctx->progress)(ctx, 4, 0, maxgroup))
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000108 return;
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000109
Theodore Ts'o3839e651997-04-26 13:21:57 +0000110 for (i=1; i <= fs->super->s_inodes_count; i++) {
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000111 if ((i % fs->super->s_inodes_per_group) == 0) {
112 group++;
113 if (ctx->progress)
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000114 if ((ctx->progress)(ctx, 4, group, maxgroup))
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000115 return;
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000116 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000117 if (i == EXT2_BAD_INO ||
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000118 (i > EXT2_ROOT_INO && i < EXT2_FIRST_INODE(fs->super)))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000119 continue;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000120 if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map, i)) ||
Theodore Ts'oaa4115a1999-10-21 19:33:18 +0000121 (ctx->inode_imagic_map &&
122 ext2fs_test_inode_bitmap(ctx->inode_imagic_map, i)) ||
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000123 (ctx->inode_bb_map &&
124 ext2fs_test_inode_bitmap(ctx->inode_bb_map, i)))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000125 continue;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000126 ext2fs_icount_fetch(ctx->inode_link_info, i, &link_count);
127 ext2fs_icount_fetch(ctx->inode_count, i, &link_counted);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000128 if (link_counted == 0) {
Theodore Ts'oe72a9ba1999-06-25 15:40:18 +0000129 if (e2fsck_process_bad_inode(ctx, 0, i))
130 continue;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000131 if (disconnect_inode(ctx, i))
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000132 continue;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000133 ext2fs_icount_fetch(ctx->inode_link_info, i,
134 &link_count);
135 ext2fs_icount_fetch(ctx->inode_count, i,
136 &link_counted);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000137 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000138 if (link_counted != link_count) {
Theodore Ts'o08b21301997-11-03 19:42:40 +0000139 e2fsck_read_inode(ctx, i, &inode, "pass4");
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000140 pctx.ino = i;
141 pctx.inode = &inode;
142 if (link_count != inode.i_links_count) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000143 pctx.num = link_count;
144 fix_problem(ctx,
145 PR_4_INCONSISTENT_COUNT, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000146 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000147 pctx.num = link_counted;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000148 if (fix_problem(ctx, PR_4_BAD_REF_COUNT, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000149 inode.i_links_count = link_counted;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000150 e2fsck_write_inode(ctx, i, &inode, "pass4");
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000151 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000152 }
153 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000154 ext2fs_free_icount(ctx->inode_link_info); ctx->inode_link_info = 0;
155 ext2fs_free_icount(ctx->inode_count); ctx->inode_count = 0;
156 ext2fs_free_inode_bitmap(ctx->inode_bb_map);
157 ctx->inode_bb_map = 0;
Theodore Ts'o7142db01999-11-08 18:46:54 +0000158 ext2fs_free_inode_bitmap(ctx->inode_imagic_map);
159 ctx->inode_imagic_map = 0;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000160#ifdef RESOURCE_TRACK
Theodore Ts'o5596def1999-07-19 15:27:37 +0000161 if (ctx->options & E2F_OPT_TIME2) {
162 e2fsck_clear_progbar(ctx);
Theodore Ts'o0c4a0722000-02-07 03:11:03 +0000163 print_resource_track(_("Pass 4"), &rtrack);
Theodore Ts'o5596def1999-07-19 15:27:37 +0000164 }
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000165#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000166}
167