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 * - Ref counts for ea_inodes. (ea_inode_refs)
15 */
16
17 #include "config.h"
18 #include "e2fsck.h"
19 #include "problem.h"
20 #include <ext2fs/ext2_ext_attr.h>
21
22 /*
23 * This routine is called when an inode is not connected to the
24 * directory tree.
25 *
26 * This subroutine returns 1 then the caller shouldn't bother with the
27 * rest of the pass 4 tests.
28 */
disconnect_inode(e2fsck_t ctx,ext2_ino_t i,struct ext2_inode_large * inode)29 static int disconnect_inode(e2fsck_t ctx, ext2_ino_t i,
30 struct ext2_inode_large *inode)
31 {
32 ext2_filsys fs = ctx->fs;
33 struct problem_context pctx;
34 __u32 eamagic = 0;
35 int extra_size = 0;
36
37 e2fsck_read_inode_full(ctx, i, EXT2_INODE(inode),
38 EXT2_INODE_SIZE(fs->super),
39 "pass4: disconnect_inode");
40 if (EXT2_INODE_SIZE(fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
41 extra_size = inode->i_extra_isize;
42
43 clear_problem_context(&pctx);
44 pctx.ino = i;
45 pctx.inode = EXT2_INODE(inode);
46
47 if (EXT2_INODE_SIZE(fs->super) -EXT2_GOOD_OLD_INODE_SIZE -extra_size >0)
48 eamagic = *(__u32 *)(((char *)inode) +EXT2_GOOD_OLD_INODE_SIZE +
49 extra_size);
50 /*
51 * Offer to delete any zero-length files that does not have
52 * blocks. If there is an EA block, it might have useful
53 * information, so we won't prompt to delete it, but let it be
54 * reconnected to lost+found.
55 */
56 if (!inode->i_blocks && eamagic != EXT2_EXT_ATTR_MAGIC &&
57 (LINUX_S_ISREG(inode->i_mode) || LINUX_S_ISDIR(inode->i_mode))) {
58 if (fix_problem(ctx, PR_4_ZERO_LEN_INODE, &pctx)) {
59 e2fsck_clear_inode(ctx, i, EXT2_INODE(inode), 0,
60 "disconnect_inode");
61 /*
62 * Fix up the bitmaps...
63 */
64 e2fsck_read_bitmaps(ctx);
65 ext2fs_inode_alloc_stats2(fs, i, -1,
66 LINUX_S_ISDIR(inode->i_mode));
67 quota_data_inodes(ctx->qctx, inode, i, -1);
68 return 0;
69 }
70 }
71
72 /*
73 * Prompt to reconnect.
74 */
75 if (fix_problem(ctx, PR_4_UNATTACHED_INODE, &pctx)) {
76 if (e2fsck_reconnect_file(ctx, i))
77 ext2fs_unmark_valid(fs);
78 } else {
79 /*
80 * If we don't attach the inode, then skip the
81 * i_links_test since there's no point in trying to
82 * force i_links_count to zero.
83 */
84 ext2fs_unmark_valid(fs);
85 return 1;
86 }
87 return 0;
88 }
89
check_ea_inode(e2fsck_t ctx,ext2_ino_t i,struct ext2_inode_large * inode,__u16 * link_counted)90 static void check_ea_inode(e2fsck_t ctx, ext2_ino_t i,
91 struct ext2_inode_large *inode, __u16 *link_counted)
92 {
93 __u64 actual_refs = 0;
94 __u64 ref_count;
95
96 /*
97 * This function is called when link_counted is zero. So this may not
98 * be an xattr inode at all. Return immediately if EA_INODE flag is not
99 * set.
100 */
101 e2fsck_read_inode_full(ctx, i, EXT2_INODE(inode),
102 EXT2_INODE_SIZE(ctx->fs->super),
103 "pass4: check_ea_inode");
104 if (!(inode->i_flags & EXT4_EA_INODE_FL))
105 return;
106
107 if (ctx->ea_inode_refs)
108 ea_refcount_fetch(ctx->ea_inode_refs, i, &actual_refs);
109 if (!actual_refs)
110 return;
111
112 /*
113 * There are some attribute references, link_counted is now considered
114 * to be 1.
115 */
116 *link_counted = 1;
117
118 ref_count = ext2fs_get_ea_inode_ref(EXT2_INODE(inode));
119
120 /* Old Lustre-style xattr inodes do not have a stored refcount.
121 * However, their i_ctime and i_atime should be the same.
122 */
123 if (ref_count != actual_refs && inode->i_ctime != inode->i_atime) {
124 struct problem_context pctx;
125
126 clear_problem_context(&pctx);
127 pctx.ino = i;
128 pctx.num = ref_count;
129 pctx.num2 = actual_refs;
130 if (fix_problem(ctx, PR_4_EA_INODE_REF_COUNT, &pctx)) {
131 ext2fs_set_ea_inode_ref(EXT2_INODE(inode), actual_refs);
132 e2fsck_write_inode(ctx, i, EXT2_INODE(inode), "pass4");
133 }
134 }
135 }
136
e2fsck_pass4(e2fsck_t ctx)137 void e2fsck_pass4(e2fsck_t ctx)
138 {
139 ext2_filsys fs = ctx->fs;
140 ext2_ino_t i;
141 struct ext2_inode_large *inode;
142 int inode_size = EXT2_INODE_SIZE(fs->super);
143 #ifdef RESOURCE_TRACK
144 struct resource_track rtrack;
145 #endif
146 struct problem_context pctx;
147 __u16 link_count, link_counted;
148 int dir_nlink_fs;
149 char *buf = 0;
150 dgrp_t group, maxgroup;
151
152 init_resource_track(&rtrack, ctx->fs->io);
153
154 #ifdef MTRACE
155 mtrace_print("Pass 4");
156 #endif
157 /*
158 * Since pass4 is mostly CPU bound, start readahead of bitmaps
159 * ahead of pass 5 if we haven't already loaded them.
160 */
161 if (ctx->readahead_kb &&
162 (fs->block_map == NULL || fs->inode_map == NULL))
163 e2fsck_readahead(fs, E2FSCK_READA_BBITMAP |
164 E2FSCK_READA_IBITMAP,
165 0, fs->group_desc_count);
166
167 clear_problem_context(&pctx);
168
169 if (!(ctx->options & E2F_OPT_PREEN))
170 fix_problem(ctx, PR_4_PASS_HEADER, &pctx);
171
172 dir_nlink_fs = ext2fs_has_feature_dir_nlink(fs->super);
173
174 group = 0;
175 maxgroup = fs->group_desc_count;
176 if (ctx->progress)
177 if ((ctx->progress)(ctx, 4, 0, maxgroup))
178 return;
179
180 inode = e2fsck_allocate_memory(ctx, inode_size, "scratch inode");
181
182 /* Protect loop from wrap-around if s_inodes_count maxed */
183 for (i=1; i <= fs->super->s_inodes_count && i > 0; i++) {
184 int isdir;
185
186 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
187 goto errout;
188 if ((i % fs->super->s_inodes_per_group) == 0) {
189 group++;
190 if (ctx->progress)
191 if ((ctx->progress)(ctx, 4, group, maxgroup))
192 goto errout;
193 }
194 if (i == quota_type2inum(PRJQUOTA, ctx->fs->super) ||
195 i == EXT2_BAD_INO ||
196 (i > EXT2_ROOT_INO && i < EXT2_FIRST_INODE(fs->super)))
197 continue;
198 if (!(ext2fs_test_inode_bitmap2(ctx->inode_used_map, i)) ||
199 (ctx->inode_imagic_map &&
200 ext2fs_test_inode_bitmap2(ctx->inode_imagic_map, i)) ||
201 (ctx->inode_bb_map &&
202 ext2fs_test_inode_bitmap2(ctx->inode_bb_map, i)))
203 continue;
204 ext2fs_icount_fetch(ctx->inode_link_info, i, &link_count);
205 ext2fs_icount_fetch(ctx->inode_count, i, &link_counted);
206
207 if (link_counted == 0) {
208 /*
209 * link_counted is expected to be 0 for an ea_inode.
210 * check_ea_inode() will update link_counted if
211 * necessary.
212 */
213 check_ea_inode(ctx, i, inode, &link_counted);
214 }
215
216 if (link_counted == 0) {
217 if (!buf)
218 buf = e2fsck_allocate_memory(ctx,
219 fs->blocksize, "bad_inode buffer");
220 if (e2fsck_process_bad_inode(ctx, 0, i, buf))
221 continue;
222 if (disconnect_inode(ctx, i, inode))
223 continue;
224 ext2fs_icount_fetch(ctx->inode_link_info, i,
225 &link_count);
226 ext2fs_icount_fetch(ctx->inode_count, i,
227 &link_counted);
228 }
229 isdir = ext2fs_test_inode_bitmap2(ctx->inode_dir_map, i);
230 if (isdir && (link_counted > EXT2_LINK_MAX)) {
231 if (!dir_nlink_fs &&
232 fix_problem(ctx, PR_4_DIR_NLINK_FEATURE, &pctx)) {
233 ext2fs_set_feature_dir_nlink(fs->super);
234 ext2fs_mark_super_dirty(fs);
235 dir_nlink_fs = 1;
236 }
237 link_counted = 1;
238 }
239 if (link_counted != link_count) {
240 e2fsck_read_inode_full(ctx, i, EXT2_INODE(inode),
241 inode_size, "pass4");
242 pctx.ino = i;
243 pctx.inode = EXT2_INODE(inode);
244 if ((link_count != inode->i_links_count) && !isdir &&
245 (inode->i_links_count <= EXT2_LINK_MAX)) {
246 pctx.num = link_count;
247 fix_problem(ctx,
248 PR_4_INCONSISTENT_COUNT, &pctx);
249 }
250 pctx.num = link_counted;
251 /* i_link_count was previously exceeded, but no longer
252 * is, fix this but don't consider it an error */
253 if ((isdir && link_counted > 1 &&
254 (inode->i_flags & EXT2_INDEX_FL) &&
255 link_count == 1 && !(ctx->options & E2F_OPT_NO)) ||
256 fix_problem(ctx, PR_4_BAD_REF_COUNT, &pctx)) {
257 inode->i_links_count = link_counted;
258 e2fsck_write_inode_full(ctx, i,
259 EXT2_INODE(inode),
260 inode_size, "pass4");
261 }
262 }
263 }
264 ext2fs_free_icount(ctx->inode_link_info); ctx->inode_link_info = 0;
265 ext2fs_free_icount(ctx->inode_count); ctx->inode_count = 0;
266 ext2fs_free_inode_bitmap(ctx->inode_bb_map);
267 ctx->inode_bb_map = 0;
268 ea_refcount_free(ctx->ea_inode_refs);
269 ctx->ea_inode_refs = 0;
270 ext2fs_free_inode_bitmap(ctx->inode_imagic_map);
271 ctx->inode_imagic_map = 0;
272 errout:
273 if (buf)
274 ext2fs_free_mem(&buf);
275
276 ext2fs_free_mem(&inode);
277 print_resource_track(ctx, _("Pass 4"), &rtrack, ctx->fs->io);
278 }
279
280