Whamcloud - gitweb
LU-11446 e2fsck: check trusted.link when fixing nlink
[tools/e2fsprogs.git] / e2fsck / pass4.c
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/lfsck.h"
21 #include <ext2fs/ext2_ext_attr.h>
22
23 /*
24  * This routine is called when an inode is not connected to the
25  * directory tree.
26  *
27  * This subroutine returns 1 then the caller shouldn't bother with the
28  * rest of the pass 4 tests.
29  */
30 static int disconnect_inode(e2fsck_t ctx, ext2_ino_t i, ext2_ino_t *last_ino,
31                             struct ext2_inode_large *inode)
32 {
33         ext2_filsys fs = ctx->fs;
34         struct problem_context  pctx;
35         __u32 eamagic = 0;
36         int extra_size = 0;
37
38         if (*last_ino != i) {
39                 e2fsck_read_inode_full(ctx, i, EXT2_INODE(inode),
40                                        EXT2_INODE_SIZE(fs->super),
41                                        "pass4: disconnect_inode");
42                 *last_ino = i;
43         }
44         if (EXT2_INODE_SIZE(fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
45                 extra_size = inode->i_extra_isize;
46
47         clear_problem_context(&pctx);
48         pctx.ino = i;
49         pctx.inode = EXT2_INODE(inode);
50
51         if (EXT2_INODE_SIZE(fs->super) -EXT2_GOOD_OLD_INODE_SIZE -extra_size >0)
52                 eamagic = *(__u32 *)(((char *)inode) +EXT2_GOOD_OLD_INODE_SIZE +
53                                      extra_size);
54         /*
55          * Offer to delete any zero-length files that does not have
56          * blocks.  If there is an EA block, it might have useful
57          * information, so we won't prompt to delete it, but let it be
58          * reconnected to lost+found.
59          */
60         if (!inode->i_blocks && eamagic != EXT2_EXT_ATTR_MAGIC &&
61             (LINUX_S_ISREG(inode->i_mode) || LINUX_S_ISDIR(inode->i_mode))) {
62                 if (fix_problem(ctx, PR_4_ZERO_LEN_INODE, &pctx)) {
63                         e2fsck_clear_inode(ctx, i, EXT2_INODE(inode), 0,
64                                            "disconnect_inode");
65                         /*
66                          * Fix up the bitmaps...
67                          */
68                         e2fsck_read_bitmaps(ctx);
69                         ext2fs_inode_alloc_stats2(fs, i, -1,
70                                                   LINUX_S_ISDIR(inode->i_mode));
71                         quota_data_inodes(ctx->qctx, inode, i, -1);
72                         return 0;
73                 }
74         }
75
76         /*
77          * Prompt to reconnect.
78          */
79         if (fix_problem(ctx, PR_4_UNATTACHED_INODE, &pctx)) {
80                 if (e2fsck_reconnect_file(ctx, i))
81                         ext2fs_unmark_valid(fs);
82                 *last_ino = 0;
83         } else {
84                 /*
85                  * If we don't attach the inode, then skip the
86                  * i_links_test since there's no point in trying to
87                  * force i_links_count to zero.
88                  */
89                 ext2fs_unmark_valid(fs);
90                 return 1;
91         }
92         return 0;
93 }
94
95 /*
96  * This function is called when link_counted is zero. So this may not
97  * be an xattr inode at all. Return immediately if EA_INODE flag is not
98  * set.
99  */
100 static void check_ea_inode(e2fsck_t ctx, ext2_ino_t i, ext2_ino_t *last_ino,
101                            struct ext2_inode_large *inode, __u16 *link_counted)
102 {
103         __u64 actual_refs = 0;
104         __u64 ref_count;
105
106         if (*last_ino != i) {
107                 e2fsck_read_inode_full(ctx, i, EXT2_INODE(inode),
108                                        EXT2_INODE_SIZE(ctx->fs->super),
109                                        "pass4: check_ea_inode");
110                 *last_ino = i;
111         }
112         if (!(inode->i_flags & EXT4_EA_INODE_FL))
113                 return;
114
115         if (ctx->ea_inode_refs)
116                 ea_refcount_fetch(ctx->ea_inode_refs, i, &actual_refs);
117         if (!actual_refs)
118                 return;
119
120         /*
121          * There are some attribute references, link_counted is now considered
122          * to be 1.
123          */
124         *link_counted = 1;
125
126         ref_count = ext2fs_get_ea_inode_ref(EXT2_INODE(inode));
127
128         /* Old Lustre-style xattr inodes do not have a stored refcount.
129          * However, their i_ctime and i_atime should be the same.
130          */
131         if (ref_count != actual_refs && inode->i_ctime != inode->i_atime) {
132                 struct problem_context pctx;
133
134                 clear_problem_context(&pctx);
135                 pctx.ino = i;
136                 pctx.num = ref_count;
137                 pctx.num2 = actual_refs;
138                 if (fix_problem(ctx, PR_4_EA_INODE_REF_COUNT, &pctx)) {
139                         ext2fs_set_ea_inode_ref(EXT2_INODE(inode), actual_refs);
140                         e2fsck_write_inode(ctx, i, EXT2_INODE(inode), "pass4");
141                 }
142         }
143 }
144
145 static errcode_t check_link_ea(e2fsck_t ctx, ext2_ino_t ino,
146                                ext2_ino_t *last_ino,
147                                struct ext2_inode_large *inode,
148                                __u16 *link_counted)
149 {
150         struct ext2_xattr_handle *handle;
151         struct link_ea_header *leh;
152         void *buf;
153         size_t ea_len;
154         errcode_t retval;
155
156         if (*last_ino != ino) {
157                 e2fsck_read_inode_full(ctx, ino, EXT2_INODE(inode),
158                                        EXT2_INODE_SIZE(ctx->fs->super),
159                                        "pass4: get link ea count");
160                 *last_ino = ino;
161         }
162
163         retval = ext2fs_xattrs_open(ctx->fs, ino, &handle);
164         if (retval)
165                 return retval;
166
167         retval = ext2fs_xattrs_read_inode(handle, inode);
168         if (retval)
169                 goto err;
170
171         retval = ext2fs_xattr_get(handle, EXT2_ATTR_INDEX_TRUSTED_PREFIX
172                                   LUSTRE_XATTR_MDT_LINK, &buf, &ea_len);
173         if (retval)
174                 goto err;
175
176         leh = (struct link_ea_header *)buf;
177         if (leh->leh_magic == ext2fs_swab32(LINK_EA_MAGIC)) {
178                 leh->leh_magic = LINK_EA_MAGIC;
179                 leh->leh_reccount = ext2fs_swab32(leh->leh_reccount);
180                 leh->leh_len = ext2fs_swab64(leh->leh_len);
181         }
182         if (leh->leh_magic != LINK_EA_MAGIC) {
183                 retval = EINVAL;
184                 goto err_free;
185         }
186         if (leh->leh_reccount == 0 && !leh->leh_overflow_time) {
187                 retval = ENODATA;
188                 goto err_free;
189         }
190         if (leh->leh_len > ea_len) {
191                 retval = EINVAL;
192                 goto err_free;
193         }
194
195         /* if linkEA overflowed and does not hold all links, assume *some*
196          * links exist until LFSCK is next run and resets leh_overflow_time */
197         if (leh->leh_overflow_time) {
198                 if (inode->i_links_count > *link_counted)
199                         *link_counted = inode->i_links_count;
200                 else if (*link_counted == 0)
201                         *link_counted = 1111;
202         }
203         if (leh->leh_reccount > *link_counted)
204                 *link_counted = leh->leh_reccount;
205 err_free:
206         ext2fs_free_mem(&buf);
207 err:
208         ext2fs_xattrs_close(&handle);
209         return retval;
210 }
211
212 void e2fsck_pass4(e2fsck_t ctx)
213 {
214         ext2_filsys fs = ctx->fs;
215         ext2_ino_t      i;
216         struct ext2_inode_large *inode;
217         int inode_size = EXT2_INODE_SIZE(fs->super);
218 #ifdef RESOURCE_TRACK
219         struct resource_track   rtrack;
220 #endif
221         struct problem_context  pctx;
222         __u16   link_count, link_counted;
223         int dir_nlink_fs;
224         char    *buf = 0;
225         dgrp_t  group, maxgroup;
226
227         init_resource_track(&rtrack, ctx->fs->io);
228
229 #ifdef MTRACE
230         mtrace_print("Pass 4");
231 #endif
232         /*
233          * Since pass4 is mostly CPU bound, start readahead of bitmaps
234          * ahead of pass 5 if we haven't already loaded them.
235          */
236         if (ctx->readahead_kb &&
237             (fs->block_map == NULL || fs->inode_map == NULL))
238                 e2fsck_readahead(fs, E2FSCK_READA_BBITMAP |
239                                      E2FSCK_READA_IBITMAP,
240                                  0, fs->group_desc_count);
241
242         clear_problem_context(&pctx);
243
244         if (!(ctx->options & E2F_OPT_PREEN))
245                 fix_problem(ctx, PR_4_PASS_HEADER, &pctx);
246
247         dir_nlink_fs = ext2fs_has_feature_dir_nlink(fs->super);
248
249         group = 0;
250         maxgroup = fs->group_desc_count;
251         if (ctx->progress)
252                 if ((ctx->progress)(ctx, 4, 0, maxgroup))
253                         return;
254
255         inode = e2fsck_allocate_memory(ctx, inode_size, "scratch inode");
256
257         /* Protect loop from wrap-around if s_inodes_count maxed */
258         for (i = 1; i <= fs->super->s_inodes_count && i > 0; i++) {
259                 ext2_ino_t last_ino = 0;
260                 int isdir;
261
262                 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
263                         goto errout;
264                 if ((i % fs->super->s_inodes_per_group) == 0) {
265                         group++;
266                         if (ctx->progress)
267                                 if ((ctx->progress)(ctx, 4, group, maxgroup))
268                                         goto errout;
269                 }
270                 if (i == quota_type2inum(PRJQUOTA, ctx->fs->super) ||
271                     i == EXT2_BAD_INO ||
272                     (i > EXT2_ROOT_INO && i < EXT2_FIRST_INODE(fs->super)))
273                         continue;
274                 if (!(ext2fs_test_inode_bitmap2(ctx->inode_used_map, i)) ||
275                     (ctx->inode_imagic_map &&
276                      ext2fs_test_inode_bitmap2(ctx->inode_imagic_map, i)) ||
277                     (ctx->inode_bb_map &&
278                      ext2fs_test_inode_bitmap2(ctx->inode_bb_map, i)))
279                         continue;
280                 ext2fs_icount_fetch(ctx->inode_link_info, i, &link_count);
281                 ext2fs_icount_fetch(ctx->inode_count, i, &link_counted);
282
283                 if (link_counted == 0) {
284                         /*
285                          * link_counted is expected to be 0 for an ea_inode.
286                          * check_ea_inode() will update link_counted if
287                          * necessary.
288                          */
289                         check_ea_inode(ctx, i, &last_ino, inode, &link_counted);
290                         check_link_ea(ctx, i, &last_ino, inode, &link_counted);
291                 }
292
293                 if (link_counted == 0) {
294                         if (!buf)
295                                 buf = e2fsck_allocate_memory(ctx,
296                                      fs->blocksize, "bad_inode buffer");
297                         if (e2fsck_process_bad_inode(ctx, 0, i, buf))
298                                 continue;
299                         if (disconnect_inode(ctx, i, &last_ino, inode))
300                                 continue;
301                         ext2fs_icount_fetch(ctx->inode_link_info, i,
302                                             &link_count);
303                         ext2fs_icount_fetch(ctx->inode_count, i,
304                                             &link_counted);
305                 }
306                 isdir = ext2fs_test_inode_bitmap2(ctx->inode_dir_map, i);
307                 if (isdir && (link_counted > EXT2_LINK_MAX)) {
308                         if (!dir_nlink_fs &&
309                             fix_problem(ctx, PR_4_DIR_NLINK_FEATURE, &pctx)) {
310                                 ext2fs_set_feature_dir_nlink(fs->super);
311                                 ext2fs_mark_super_dirty(fs);
312                                 dir_nlink_fs = 1;
313                         }
314                         link_counted = 1;
315                 }
316                 if (link_counted != link_count)
317                         check_link_ea(ctx, i, &last_ino, inode, &link_counted);
318
319                 if (link_counted != link_count) {
320                         int fix_nlink = 0;
321
322                         if (last_ino != i) {
323                                 e2fsck_read_inode_full(ctx, i,
324                                                        EXT2_INODE(inode),
325                                                        inode_size, "pass4");
326                                 last_ino = i;
327                         }
328                         pctx.ino = i;
329                         pctx.inode = EXT2_INODE(inode);
330                         if ((link_count != inode->i_links_count) && !isdir &&
331                             (inode->i_links_count <= EXT2_LINK_MAX)) {
332                                 pctx.num = link_count;
333                                 fix_problem(ctx,
334                                             PR_4_INCONSISTENT_COUNT, &pctx);
335                         }
336                         pctx.num = link_counted;
337                         /* i_link_count was previously exceeded, but no longer
338                          * is, fix this but don't consider it an error */
339                         if (isdir && link_counted > 1 &&
340                             (inode->i_flags & EXT2_INDEX_FL) &&
341                             link_count == 1) {
342                                 if ((ctx->options & E2F_OPT_READONLY) == 0) {
343                                         fix_nlink =
344                                                 fix_problem(ctx,
345                                                         PR_4_DIR_OVERFLOW_REF_COUNT,
346                                                         &pctx);
347                                 }
348                         } else {
349                                 fix_nlink = fix_problem(ctx, PR_4_BAD_REF_COUNT,
350                                                 &pctx);
351                         }
352                         if (fix_nlink) {
353                                 inode->i_links_count = link_counted;
354                                 e2fsck_write_inode_full(ctx, i,
355                                                         EXT2_INODE(inode),
356                                                         inode_size, "pass4");
357                         }
358                 }
359         }
360         ext2fs_free_icount(ctx->inode_link_info); ctx->inode_link_info = 0;
361         ext2fs_free_icount(ctx->inode_count); ctx->inode_count = 0;
362         ext2fs_free_inode_bitmap(ctx->inode_bb_map);
363         ctx->inode_bb_map = 0;
364         ea_refcount_free(ctx->ea_inode_refs);
365         ctx->ea_inode_refs = 0;
366         ext2fs_free_inode_bitmap(ctx->inode_imagic_map);
367         ctx->inode_imagic_map = 0;
368 errout:
369         if (buf)
370                 ext2fs_free_mem(&buf);
371
372         ext2fs_free_mem(&inode);
373         print_resource_track(ctx, _("Pass 4"), &rtrack, ctx->fs->io);
374 }
375