blob: 1cb7449f648ceec852280d81407c9f6dd9105173 [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'o3839e651997-04-26 13:21:57 +000013 */
14
15#include "e2fsck.h"
Theodore Ts'o21c84b71997-04-29 16:15:03 +000016#include "problem.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000017
Theodore Ts'o5c576471997-04-29 15:29:49 +000018/*
19 * This routine is called when an inode is not connected to the
20 * directory tree.
21 *
22 * This subroutine returns 1 then the caller shouldn't bother with the
23 * rest of the pass 4 tests.
24 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000025static int disconnect_inode(e2fsck_t ctx, ino_t i)
Theodore Ts'o5c576471997-04-29 15:29:49 +000026{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000027 ext2_filsys fs = ctx->fs;
Theodore Ts'o5c576471997-04-29 15:29:49 +000028 struct ext2_inode inode;
Theodore Ts'o21c84b71997-04-29 16:15:03 +000029 struct problem_context pctx;
Theodore Ts'o5c576471997-04-29 15:29:49 +000030
Theodore Ts'o08b21301997-11-03 19:42:40 +000031 e2fsck_read_inode(ctx, i, &inode, "pass4: disconnect_inode");
Theodore Ts'o21c84b71997-04-29 16:15:03 +000032 clear_problem_context(&pctx);
33 pctx.ino = i;
34 pctx.inode = &inode;
35
Theodore Ts'o5c576471997-04-29 15:29:49 +000036 if (!inode.i_blocks && (LINUX_S_ISREG(inode.i_mode) ||
37 LINUX_S_ISDIR(inode.i_mode))) {
38 /*
39 * This is a zero-length file; prompt to delete it...
40 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000041 if (fix_problem(ctx, PR_4_ZERO_LEN_INODE, &pctx)) {
42 ext2fs_icount_store(ctx->inode_link_info, i, 0);
Theodore Ts'o5c576471997-04-29 15:29:49 +000043 inode.i_links_count = 0;
44 inode.i_dtime = time(0);
Theodore Ts'o08b21301997-11-03 19:42:40 +000045 e2fsck_write_inode(ctx, i, &inode,
Theodore Ts'o5c576471997-04-29 15:29:49 +000046 "disconnect_inode");
47 /*
48 * Fix up the bitmaps...
49 */
Theodore Ts'of8188ff1997-11-14 05:23:04 +000050 e2fsck_read_bitmaps(ctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000051 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, i);
52 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, i);
Theodore Ts'o5c576471997-04-29 15:29:49 +000053 ext2fs_unmark_inode_bitmap(fs->inode_map, i);
54 ext2fs_mark_ib_dirty(fs);
55 return 0;
56 }
57 }
58
59 /*
60 * Prompt to reconnect.
61 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000062 if (fix_problem(ctx, PR_4_UNATTACHED_INODE, &pctx)) {
Theodore Ts'o08b21301997-11-03 19:42:40 +000063 if (e2fsck_reconnect_file(ctx, i))
Theodore Ts'o5c576471997-04-29 15:29:49 +000064 ext2fs_unmark_valid(fs);
65 } else {
66 /*
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.
70 */
71 ext2fs_unmark_valid(fs);
72 return 1;
73 }
74 return 0;
75}
76
77
Theodore Ts'o08b21301997-11-03 19:42:40 +000078void e2fsck_pass4(e2fsck_t ctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +000079{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000080 ext2_filsys fs = ctx->fs;
Theodore Ts'of3db3561997-04-26 13:34:30 +000081 ino_t i;
Theodore Ts'o3839e651997-04-26 13:21:57 +000082 struct ext2_inode inode;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000083#ifdef RESOURCE_TRACK
Theodore Ts'o3839e651997-04-26 13:21:57 +000084 struct resource_track rtrack;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000085#endif
Theodore Ts'o21c84b71997-04-29 16:15:03 +000086 struct problem_context pctx;
87 __u16 link_count, link_counted;
Theodore Ts'of8188ff1997-11-14 05:23:04 +000088 int group, max, j;
Theodore Ts'o3839e651997-04-26 13:21:57 +000089
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000090#ifdef RESOURCE_TRACK
Theodore Ts'o3839e651997-04-26 13:21:57 +000091 init_resource_track(&rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000092#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000093
94#ifdef MTRACE
95 mtrace_print("Pass 4");
96#endif
97
Theodore Ts'o21c84b71997-04-29 16:15:03 +000098 clear_problem_context(&pctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000099
100 if (!(ctx->options & E2F_OPT_PREEN))
101 fix_problem(ctx, PR_4_PASS_HEADER, &pctx);
102
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000103 group = 0;
104 max = fs->group_desc_count;
105 if (ctx->progress)
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000106 if ((ctx->progress)(ctx, 4, 0, max))
107 return;
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000108
Theodore Ts'o3839e651997-04-26 13:21:57 +0000109 for (i=1; i <= fs->super->s_inodes_count; i++) {
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000110 if ((i % fs->super->s_inodes_per_group) == 0) {
111 group++;
112 if (ctx->progress)
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000113 if ((ctx->progress)(ctx, 4, group, max))
114 return;
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000115 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000116 if (i == EXT2_BAD_INO ||
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000117 (i > EXT2_ROOT_INO && i < EXT2_FIRST_INODE(fs->super)))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000118 continue;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000119 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)))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000122 continue;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000123 ext2fs_icount_fetch(ctx->inode_link_info, i, &link_count);
124 ext2fs_icount_fetch(ctx->inode_count, i, &link_counted);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000125 if (link_counted == 0) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000126 if (disconnect_inode(ctx, i))
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000127 continue;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000128 ext2fs_icount_fetch(ctx->inode_link_info, i,
129 &link_count);
130 ext2fs_icount_fetch(ctx->inode_count, i,
131 &link_counted);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000132 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000133 if (link_counted != link_count) {
Theodore Ts'o08b21301997-11-03 19:42:40 +0000134 e2fsck_read_inode(ctx, i, &inode, "pass4");
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000135 pctx.ino = i;
136 pctx.inode = &inode;
137 if (link_count != inode.i_links_count) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000138 pctx.num = link_count;
139 fix_problem(ctx,
140 PR_4_INCONSISTENT_COUNT, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000141 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000142 pctx.num = link_counted;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000143 if (fix_problem(ctx, PR_4_BAD_REF_COUNT, &pctx)) {
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000144 inode.i_links_count = link_counted;
Theodore Ts'o08b21301997-11-03 19:42:40 +0000145 e2fsck_write_inode(ctx, i, &inode, "pass4");
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000146 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000147 }
148 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000149 ext2fs_free_icount(ctx->inode_link_info); ctx->inode_link_info = 0;
150 ext2fs_free_icount(ctx->inode_count); ctx->inode_count = 0;
151 ext2fs_free_inode_bitmap(ctx->inode_bb_map);
152 ctx->inode_bb_map = 0;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000153#ifdef RESOURCE_TRACK
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000154 if (ctx->options & E2F_OPT_TIME2)
155 print_resource_track("Pass 4", &rtrack);
Theodore Ts'o8bf191e1997-10-20 01:38:32 +0000156#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000157}
158