Whamcloud - gitweb
mke2fs: fix for the "-E offset=N" option
[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  */
15
16 #include "config.h"
17 #include "e2fsck.h"
18 #include "problem.h"
19 #include <ext2fs/ext2_ext_attr.h>
20
21 /*
22  * This routine is called when an inode is not connected to the
23  * directory tree.
24  *
25  * This subroutine returns 1 then the caller shouldn't bother with the
26  * rest of the pass 4 tests.
27  */
28 static int disconnect_inode(e2fsck_t ctx, ext2_ino_t i,
29                             struct ext2_inode *inode)
30 {
31         ext2_filsys fs = ctx->fs;
32         struct problem_context  pctx;
33         __u32 eamagic = 0;
34         int extra_size = 0;
35
36         if (EXT2_INODE_SIZE(fs->super) > EXT2_GOOD_OLD_INODE_SIZE) {
37                 e2fsck_read_inode_full(ctx, i, inode,EXT2_INODE_SIZE(fs->super),
38                                        "pass4: disconnect_inode");
39                 extra_size = ((struct ext2_inode_large *)inode)->i_extra_isize;
40         } else {
41                 e2fsck_read_inode(ctx, i, inode, "pass4: disconnect_inode");
42         }
43         clear_problem_context(&pctx);
44         pctx.ino = i;
45         pctx.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, 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
90
91 void e2fsck_pass4(e2fsck_t ctx)
92 {
93         ext2_filsys fs = ctx->fs;
94         ext2_ino_t      i;
95         struct ext2_inode       *inode;
96 #ifdef RESOURCE_TRACK
97         struct resource_track   rtrack;
98 #endif
99         struct problem_context  pctx;
100         __u16   link_count, link_counted;
101         char    *buf = 0;
102         dgrp_t  group, maxgroup;
103
104         init_resource_track(&rtrack, ctx->fs->io);
105
106 #ifdef MTRACE
107         mtrace_print("Pass 4");
108 #endif
109         /*
110          * Since pass4 is mostly CPU bound, start readahead of bitmaps
111          * ahead of pass 5 if we haven't already loaded them.
112          */
113         if (ctx->readahead_kb &&
114             (fs->block_map == NULL || fs->inode_map == NULL))
115                 e2fsck_readahead(fs, E2FSCK_READA_BBITMAP |
116                                      E2FSCK_READA_IBITMAP,
117                                  0, fs->group_desc_count);
118
119         clear_problem_context(&pctx);
120
121         if (!(ctx->options & E2F_OPT_PREEN))
122                 fix_problem(ctx, PR_4_PASS_HEADER, &pctx);
123
124         group = 0;
125         maxgroup = fs->group_desc_count;
126         if (ctx->progress)
127                 if ((ctx->progress)(ctx, 4, 0, maxgroup))
128                         return;
129
130         inode = e2fsck_allocate_memory(ctx, EXT2_INODE_SIZE(fs->super),
131                                        "scratch inode");
132
133         /* Protect loop from wrap-around if s_inodes_count maxed */
134         for (i=1; i <= fs->super->s_inodes_count && i > 0; i++) {
135                 int isdir;
136
137                 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
138                         goto errout;
139                 if ((i % fs->super->s_inodes_per_group) == 0) {
140                         group++;
141                         if (ctx->progress)
142                                 if ((ctx->progress)(ctx, 4, group, maxgroup))
143                                         goto errout;
144                 }
145                 if (i == quota_type2inum(PRJQUOTA, ctx->fs->super) ||
146                     i == EXT2_BAD_INO ||
147                     (i > EXT2_ROOT_INO && i < EXT2_FIRST_INODE(fs->super)))
148                         continue;
149                 if (!(ext2fs_test_inode_bitmap2(ctx->inode_used_map, i)) ||
150                     (ctx->inode_imagic_map &&
151                      ext2fs_test_inode_bitmap2(ctx->inode_imagic_map, i)) ||
152                     (ctx->inode_bb_map &&
153                      ext2fs_test_inode_bitmap2(ctx->inode_bb_map, i)))
154                         continue;
155                 ext2fs_icount_fetch(ctx->inode_link_info, i, &link_count);
156                 ext2fs_icount_fetch(ctx->inode_count, i, &link_counted);
157                 if (link_counted == 0) {
158                         if (!buf)
159                                 buf = e2fsck_allocate_memory(ctx,
160                                      fs->blocksize, "bad_inode buffer");
161                         if (e2fsck_process_bad_inode(ctx, 0, i, buf))
162                                 continue;
163                         if (disconnect_inode(ctx, i, inode))
164                                 continue;
165                         ext2fs_icount_fetch(ctx->inode_link_info, i,
166                                             &link_count);
167                         ext2fs_icount_fetch(ctx->inode_count, i,
168                                             &link_counted);
169                 }
170                 isdir = ext2fs_test_inode_bitmap2(ctx->inode_dir_map, i);
171                 if (isdir && (link_counted > EXT2_LINK_MAX))
172                         link_counted = 1;
173                 if (link_counted != link_count) {
174                         e2fsck_read_inode(ctx, i, inode, "pass4");
175                         pctx.ino = i;
176                         pctx.inode = inode;
177                         if ((link_count != inode->i_links_count) && !isdir &&
178                             (inode->i_links_count <= EXT2_LINK_MAX)) {
179                                 pctx.num = link_count;
180                                 fix_problem(ctx,
181                                             PR_4_INCONSISTENT_COUNT, &pctx);
182                         }
183                         pctx.num = link_counted;
184                         /* i_link_count was previously exceeded, but no longer
185                          * is, fix this but don't consider it an error */
186                         if ((isdir && link_counted > 1 &&
187                              (inode->i_flags & EXT2_INDEX_FL) &&
188                              link_count == 1 && !(ctx->options & E2F_OPT_NO)) ||
189                             fix_problem(ctx, PR_4_BAD_REF_COUNT, &pctx)) {
190                                 inode->i_links_count = link_counted;
191                                 e2fsck_write_inode(ctx, i, inode, "pass4");
192                         }
193                 }
194         }
195         ext2fs_free_icount(ctx->inode_link_info); ctx->inode_link_info = 0;
196         ext2fs_free_icount(ctx->inode_count); ctx->inode_count = 0;
197         ext2fs_free_inode_bitmap(ctx->inode_bb_map);
198         ctx->inode_bb_map = 0;
199         ext2fs_free_inode_bitmap(ctx->inode_imagic_map);
200         ctx->inode_imagic_map = 0;
201 errout:
202         if (buf)
203                 ext2fs_free_mem(&buf);
204
205         ext2fs_free_mem(&inode);
206         print_resource_track(ctx, _("Pass 4"), &rtrack, ctx->fs->io);
207 }
208