Whamcloud - gitweb
e2fsck: factor out code to clear an inode into e2fsck_clear_inode()
[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 "e2fsck.h"
17 #include "problem.h"
18
19 /*
20  * This routine is called when an inode is not connected to the
21  * directory tree.
22  * 
23  * This subroutine returns 1 then the caller shouldn't bother with the
24  * rest of the pass 4 tests.
25  */
26 static int disconnect_inode(e2fsck_t ctx, ext2_ino_t i)
27 {
28         ext2_filsys fs = ctx->fs;
29         struct ext2_inode       inode;
30         struct problem_context  pctx;
31
32         e2fsck_read_inode(ctx, i, &inode, "pass4: disconnect_inode");
33         clear_problem_context(&pctx);
34         pctx.ino = i;
35         pctx.inode = &inode;
36         
37         /*
38          * Offer to delete any zero-length files that does not have
39          * blocks.  If there is an EA block, it might have useful
40          * information, so we won't prompt to delete it, but let it be
41          * reconnected to lost+found.
42          */
43         if (!inode.i_blocks && (LINUX_S_ISREG(inode.i_mode) ||
44                                 LINUX_S_ISDIR(inode.i_mode))) {
45                 if (fix_problem(ctx, PR_4_ZERO_LEN_INODE, &pctx)) {
46                         e2fsck_clear_inode(ctx, i, &inode, 0,
47                                            "disconnect_inode");
48                         /*
49                          * Fix up the bitmaps...
50                          */
51                         e2fsck_read_bitmaps(ctx);
52                         ext2fs_inode_alloc_stats2(fs, i, -1,
53                                                   LINUX_S_ISDIR(inode.i_mode));
54                         return 0;
55                 }
56         }
57         
58         /*
59          * Prompt to reconnect.
60          */
61         if (fix_problem(ctx, PR_4_UNATTACHED_INODE, &pctx)) {
62                 if (e2fsck_reconnect_file(ctx, i))
63                         ext2fs_unmark_valid(fs);
64         } else {
65                 /*
66                  * If we don't attach the inode, then skip the
67                  * i_links_test since there's no point in trying to
68                  * force i_links_count to zero.
69                  */
70                 ext2fs_unmark_valid(fs);
71                 return 1;
72         }
73         return 0;
74 }
75
76
77 void e2fsck_pass4(e2fsck_t ctx)
78 {
79         ext2_filsys fs = ctx->fs;
80         ext2_ino_t      i;
81         struct ext2_inode       inode;
82 #ifdef RESOURCE_TRACK
83         struct resource_track   rtrack;
84 #endif
85         struct problem_context  pctx;
86         __u16   link_count, link_counted;
87         char    *buf = 0;
88         int     group, maxgroup;
89         
90 #ifdef RESOURCE_TRACK
91         init_resource_track(&rtrack, ctx->fs->io);
92 #endif
93
94 #ifdef MTRACE
95         mtrace_print("Pass 4");
96 #endif
97
98         clear_problem_context(&pctx);
99
100         if (!(ctx->options & E2F_OPT_PREEN))
101                 fix_problem(ctx, PR_4_PASS_HEADER, &pctx);
102
103         group = 0;
104         maxgroup = fs->group_desc_count;
105         if (ctx->progress)
106                 if ((ctx->progress)(ctx, 4, 0, maxgroup))
107                         return;
108
109         /* Protect loop from wrap-around if s_inodes_count maxed */
110         for (i=1; i <= fs->super->s_inodes_count && i > 0; i++) {
111                 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
112                         goto errout;
113                 if ((i % fs->super->s_inodes_per_group) == 0) {
114                         group++;
115                         if (ctx->progress)
116                                 if ((ctx->progress)(ctx, 4, group, maxgroup))
117                                         goto errout;
118                 }
119                 if (i == EXT2_BAD_INO ||
120                     (i > EXT2_ROOT_INO && i < EXT2_FIRST_INODE(fs->super)))
121                         continue;
122                 if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map, i)) ||
123                     (ctx->inode_imagic_map &&
124                      ext2fs_test_inode_bitmap(ctx->inode_imagic_map, i)) ||
125                     (ctx->inode_bb_map &&
126                      ext2fs_test_inode_bitmap(ctx->inode_bb_map, i)))
127                         continue;
128                 ext2fs_icount_fetch(ctx->inode_link_info, i, &link_count);
129                 ext2fs_icount_fetch(ctx->inode_count, i, &link_counted);
130                 if (link_counted == 0) {
131                         if (!buf)
132                                 buf = e2fsck_allocate_memory(ctx,
133                                      fs->blocksize, "bad_inode buffer");
134                         if (e2fsck_process_bad_inode(ctx, 0, i, buf))
135                                 continue;
136                         if (disconnect_inode(ctx, i))
137                                 continue;
138                         ext2fs_icount_fetch(ctx->inode_link_info, i,
139                                             &link_count);
140                         ext2fs_icount_fetch(ctx->inode_count, i,
141                                             &link_counted);
142                 }
143                 if (link_counted != link_count) {
144                         e2fsck_read_inode(ctx, i, &inode, "pass4");
145                         pctx.ino = i;
146                         pctx.inode = &inode;
147                         if (link_count != inode.i_links_count) {
148                                 pctx.num = link_count;
149                                 fix_problem(ctx,
150                                             PR_4_INCONSISTENT_COUNT, &pctx);
151                         }
152                         pctx.num = link_counted;
153                         if (fix_problem(ctx, PR_4_BAD_REF_COUNT, &pctx)) {
154                                 inode.i_links_count = link_counted;
155                                 e2fsck_write_inode(ctx, i, &inode, "pass4");
156                         }
157                 }
158         }
159         ext2fs_free_icount(ctx->inode_link_info); ctx->inode_link_info = 0;
160         ext2fs_free_icount(ctx->inode_count); ctx->inode_count = 0;
161         ext2fs_free_inode_bitmap(ctx->inode_bb_map);
162         ctx->inode_bb_map = 0;
163         ext2fs_free_inode_bitmap(ctx->inode_imagic_map);
164         ctx->inode_imagic_map = 0;
165 errout:
166         if (buf)
167                 ext2fs_free_mem(&buf);
168 #ifdef RESOURCE_TRACK
169         if (ctx->options & E2F_OPT_TIME2) {
170                 e2fsck_clear_progbar(ctx);
171                 print_resource_track(_("Pass 4"), &rtrack, ctx->fs->io);
172         }
173 #endif
174 }
175