blob: 515cb84df7fe10bdcf4d92f8de558dc95e442519 [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"
Andreas Dilgerfefaef32008-02-02 01:16:32 -070018#include <ext2fs/ext2_ext_attr.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000019
Theodore Ts'o5c576471997-04-29 15:29:49 +000020/*
21 * This routine is called when an inode is not connected to the
22 * directory tree.
Theodore Ts'oefc6f622008-08-27 23:07:54 -040023 *
Theodore Ts'o5c576471997-04-29 15:29:49 +000024 * This subroutine returns 1 then the caller shouldn't bother with the
25 * rest of the pass 4 tests.
26 */
Andreas Dilgerfefaef32008-02-02 01:16:32 -070027static int disconnect_inode(e2fsck_t ctx, ext2_ino_t i,
28 struct ext2_inode *inode)
Theodore Ts'o5c576471997-04-29 15:29:49 +000029{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000030 ext2_filsys fs = ctx->fs;
Theodore Ts'o21c84b71997-04-29 16:15:03 +000031 struct problem_context pctx;
Andreas Dilgerfefaef32008-02-02 01:16:32 -070032 __u32 eamagic = 0;
33 int extra_size = 0;
Theodore Ts'o5c576471997-04-29 15:29:49 +000034
Andreas Dilgerfefaef32008-02-02 01:16:32 -070035 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 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +000042 clear_problem_context(&pctx);
43 pctx.ino = i;
Andreas Dilgerfefaef32008-02-02 01:16:32 -070044 pctx.inode = inode;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040045
Andreas Dilgerfefaef32008-02-02 01:16:32 -070046 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);
Theodore Ts'o0684a4f2002-08-17 10:19:44 -040049 /*
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 */
Andreas Dilgerfefaef32008-02-02 01:16:32 -070055 if (!inode->i_blocks && eamagic != EXT2_EXT_ATTR_MAGIC &&
56 (LINUX_S_ISREG(inode->i_mode) || LINUX_S_ISDIR(inode->i_mode))) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000057 if (fix_problem(ctx, PR_4_ZERO_LEN_INODE, &pctx)) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -040058 e2fsck_clear_inode(ctx, i, inode, 0,
Theodore Ts'o5c576471997-04-29 15:29:49 +000059 "disconnect_inode");
60 /*
61 * Fix up the bitmaps...
62 */
Theodore Ts'of8188ff1997-11-14 05:23:04 +000063 e2fsck_read_bitmaps(ctx);
Theodore Ts'o0684a4f2002-08-17 10:19:44 -040064 ext2fs_inode_alloc_stats2(fs, i, -1,
Andreas Dilgerfefaef32008-02-02 01:16:32 -070065 LINUX_S_ISDIR(inode->i_mode));
Theodore Ts'o5c576471997-04-29 15:29:49 +000066 return 0;
67 }
68 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -040069
Theodore Ts'o5c576471997-04-29 15:29:49 +000070 /*
71 * Prompt to reconnect.
72 */
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000073 if (fix_problem(ctx, PR_4_UNATTACHED_INODE, &pctx)) {
Theodore Ts'o08b21301997-11-03 19:42:40 +000074 if (e2fsck_reconnect_file(ctx, i))
Theodore Ts'o5c576471997-04-29 15:29:49 +000075 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
Theodore Ts'o08b21301997-11-03 19:42:40 +000089void e2fsck_pass4(e2fsck_t ctx)
Theodore Ts'o3839e651997-04-26 13:21:57 +000090{
Theodore Ts'o1b6bf171997-10-03 17:48:10 +000091 ext2_filsys fs = ctx->fs;
Theodore Ts'o86c627e2001-01-11 15:12:14 +000092 ext2_ino_t i;
Andreas Dilgerfefaef32008-02-02 01:16:32 -070093 struct ext2_inode *inode;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000094#ifdef RESOURCE_TRACK
Theodore Ts'o3839e651997-04-26 13:21:57 +000095 struct resource_track rtrack;
Theodore Ts'o8bf191e1997-10-20 01:38:32 +000096#endif
Theodore Ts'o21c84b71997-04-29 16:15:03 +000097 struct problem_context pctx;
98 __u16 link_count, link_counted;
Theodore Ts'obcf9c5d2002-05-21 09:14:17 -040099 char *buf = 0;
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000100 int group, maxgroup;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400101
Theodore Ts'o6d96b002007-08-03 20:07:09 -0400102 init_resource_track(&rtrack, ctx->fs->io);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000103
104#ifdef MTRACE
105 mtrace_print("Pass 4");
106#endif
107
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000108 clear_problem_context(&pctx);
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000109
110 if (!(ctx->options & E2F_OPT_PREEN))
111 fix_problem(ctx, PR_4_PASS_HEADER, &pctx);
112
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000113 group = 0;
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000114 maxgroup = fs->group_desc_count;
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000115 if (ctx->progress)
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000116 if ((ctx->progress)(ctx, 4, 0, maxgroup))
Theodore Ts'oa02ce9d1998-02-24 20:22:23 +0000117 return;
Eric Sandeen5830d6b2006-08-30 02:16:55 -0400118
Andreas Dilgerfefaef32008-02-02 01:16:32 -0700119 inode = e2fsck_allocate_memory(ctx, EXT2_INODE_SIZE(fs->super),
120 "scratch inode");
121
Eric Sandeen5830d6b2006-08-30 02:16:55 -0400122 /* Protect loop from wrap-around if s_inodes_count maxed */
123 for (i=1; i <= fs->super->s_inodes_count && i > 0; i++) {
Theodore Ts'o992016c2010-11-26 19:09:43 -0500124 int isdir = ext2fs_test_inode_bitmap(ctx->inode_dir_map, i);
125
Theodore Ts'o4cae0452002-07-21 14:14:03 -0400126 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
Brian Behlendorf06c9c6f2007-03-28 12:43:05 -0400127 goto errout;
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000128 if ((i % fs->super->s_inodes_per_group) == 0) {
129 group++;
130 if (ctx->progress)
Theodore Ts'o7f813ba1998-09-03 01:26:03 +0000131 if ((ctx->progress)(ctx, 4, group, maxgroup))
Brian Behlendorf06c9c6f2007-03-28 12:43:05 -0400132 goto errout;
Theodore Ts'of8188ff1997-11-14 05:23:04 +0000133 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000134 if (i == EXT2_BAD_INO ||
Theodore Ts'o7f88b041997-04-26 14:48:50 +0000135 (i > EXT2_ROOT_INO && i < EXT2_FIRST_INODE(fs->super)))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000136 continue;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000137 if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map, i)) ||
Theodore Ts'oaa4115a1999-10-21 19:33:18 +0000138 (ctx->inode_imagic_map &&
139 ext2fs_test_inode_bitmap(ctx->inode_imagic_map, i)) ||
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000140 (ctx->inode_bb_map &&
141 ext2fs_test_inode_bitmap(ctx->inode_bb_map, i)))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000142 continue;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000143 ext2fs_icount_fetch(ctx->inode_link_info, i, &link_count);
144 ext2fs_icount_fetch(ctx->inode_count, i, &link_counted);
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000145 if (link_counted == 0) {
Theodore Ts'obcf9c5d2002-05-21 09:14:17 -0400146 if (!buf)
147 buf = e2fsck_allocate_memory(ctx,
148 fs->blocksize, "bad_inode buffer");
149 if (e2fsck_process_bad_inode(ctx, 0, i, buf))
Theodore Ts'oe72a9ba1999-06-25 15:40:18 +0000150 continue;
Andreas Dilgerfefaef32008-02-02 01:16:32 -0700151 if (disconnect_inode(ctx, i, inode))
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000152 continue;
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000153 ext2fs_icount_fetch(ctx->inode_link_info, i,
154 &link_count);
155 ext2fs_icount_fetch(ctx->inode_count, i,
156 &link_counted);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000157 }
Theodore Ts'o992016c2010-11-26 19:09:43 -0500158 if (isdir && (link_counted > EXT2_LINK_MAX))
Andreas Dilgera7c9cb72008-02-02 01:25:03 -0700159 link_counted = 1;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000160 if (link_counted != link_count) {
Andreas Dilgerfefaef32008-02-02 01:16:32 -0700161 e2fsck_read_inode(ctx, i, inode, "pass4");
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000162 pctx.ino = i;
Andreas Dilgerfefaef32008-02-02 01:16:32 -0700163 pctx.inode = inode;
Theodore Ts'o992016c2010-11-26 19:09:43 -0500164 if ((link_count != inode->i_links_count) && !isdir &&
165 (inode->i_links_count <= EXT2_LINK_MAX)) {
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000166 pctx.num = link_count;
167 fix_problem(ctx,
168 PR_4_INCONSISTENT_COUNT, &pctx);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000169 }
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000170 pctx.num = link_counted;
Andreas Dilgera7c9cb72008-02-02 01:25:03 -0700171 /* i_link_count was previously exceeded, but no longer
172 * is, fix this but don't consider it an error */
Theodore Ts'o992016c2010-11-26 19:09:43 -0500173 if ((isdir && link_counted > 1 &&
Andreas Dilgera7c9cb72008-02-02 01:25:03 -0700174 (inode->i_flags & EXT2_INDEX_FL) &&
175 link_count == 1 && !(ctx->options & E2F_OPT_NO)) ||
Theodore Ts'o992016c2010-11-26 19:09:43 -0500176 fix_problem(ctx, PR_4_BAD_REF_COUNT, &pctx)) {
Andreas Dilgerfefaef32008-02-02 01:16:32 -0700177 inode->i_links_count = link_counted;
178 e2fsck_write_inode(ctx, i, inode, "pass4");
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000179 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000180 }
181 }
Theodore Ts'o1b6bf171997-10-03 17:48:10 +0000182 ext2fs_free_icount(ctx->inode_link_info); ctx->inode_link_info = 0;
183 ext2fs_free_icount(ctx->inode_count); ctx->inode_count = 0;
184 ext2fs_free_inode_bitmap(ctx->inode_bb_map);
185 ctx->inode_bb_map = 0;
Theodore Ts'o7142db01999-11-08 18:46:54 +0000186 ext2fs_free_inode_bitmap(ctx->inode_imagic_map);
187 ctx->inode_imagic_map = 0;
Brian Behlendorf06c9c6f2007-03-28 12:43:05 -0400188errout:
Theodore Ts'obcf9c5d2002-05-21 09:14:17 -0400189 if (buf)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400190 ext2fs_free_mem(&buf);
Andreas Dilgerfefaef32008-02-02 01:16:32 -0700191
192 ext2fs_free_mem(&inode);
Ken Chen9facd072009-05-28 09:55:10 -0400193 print_resource_track(ctx, _("Pass 4"), &rtrack, ctx->fs->io);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000194}
195