Whamcloud - gitweb
264b1c7833c31e9825d01cbfa3c061a52672568d
[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  */
14
15 #include "e2fsck.h"
16 #include "problem.h"
17
18 /*
19  * This routine is called when an inode is not connected to the
20  * directory tree.
21  * 
22  * This subroutine returns 1 then the caller shouldn't bother with the
23  * rest of the pass 4 tests.
24  */
25 static int disconnect_inode(e2fsck_t ctx, ino_t i)
26 {
27         ext2_filsys fs = ctx->fs;
28         struct ext2_inode       inode;
29         struct problem_context  pctx;
30
31         e2fsck_read_inode(ctx, i, &inode, "pass4: disconnect_inode");
32         clear_problem_context(&pctx);
33         pctx.ino = i;
34         pctx.inode = &inode;
35         
36         if (!inode.i_blocks && (LINUX_S_ISREG(inode.i_mode) ||
37                                 LINUX_S_ISDIR(inode.i_mode))) {
38                 /*
39                  * This is a zero-length file; prompt to delete it...
40                  */
41                 if (fix_problem(ctx, PR_4_ZERO_LEN_INODE, &pctx)) {
42                         ext2fs_icount_store(ctx->inode_link_info, i, 0);
43                         inode.i_links_count = 0;
44                         inode.i_dtime = time(0);
45                         e2fsck_write_inode(ctx, i, &inode,
46                                            "disconnect_inode");
47                         /*
48                          * Fix up the bitmaps...
49                          */
50                         e2fsck_read_bitmaps(ctx);
51                         ext2fs_unmark_inode_bitmap(ctx->inode_used_map, i);
52                         ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, i);
53                         ext2fs_unmark_inode_bitmap(fs->inode_map, i);
54                         ext2fs_mark_ib_dirty(fs);
55                         return 0;
56                 }
57         }
58         
59         /*
60          * Prompt to reconnect.
61          */
62         if (fix_problem(ctx, PR_4_UNATTACHED_INODE, &pctx)) {
63                 if (e2fsck_reconnect_file(ctx, i))
64                         ext2fs_unmark_valid(fs);
65         } else {
66                 /*
67                  * If we don't attach the inode, then skip the
68                  * i_links_test since there's no point in trying to
69                  * force i_links_count to zero.
70                  */
71                 ext2fs_unmark_valid(fs);
72                 return 1;
73         }
74         return 0;
75 }
76
77
78 void e2fsck_pass4(e2fsck_t ctx)
79 {
80         ext2_filsys fs = ctx->fs;
81         ino_t   i;
82         struct ext2_inode       inode;
83 #ifdef RESOURCE_TRACK
84         struct resource_track   rtrack;
85 #endif
86         struct problem_context  pctx;
87         __u16   link_count, link_counted;
88         int     group, maxgroup;
89         
90 #ifdef RESOURCE_TRACK
91         init_resource_track(&rtrack);
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         for (i=1; i <= fs->super->s_inodes_count; i++) {
110                 if ((i % fs->super->s_inodes_per_group) == 0) {
111                         group++;
112                         if (ctx->progress)
113                                 if ((ctx->progress)(ctx, 4, group, maxgroup))
114                                         return;
115                 }
116                 if (i == EXT2_BAD_INO ||
117                     (i > EXT2_ROOT_INO && i < EXT2_FIRST_INODE(fs->super)))
118                         continue;
119                 if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map, i)) ||
120                     (ctx->inode_bb_map &&
121                      ext2fs_test_inode_bitmap(ctx->inode_bb_map, i)))
122                         continue;
123                 ext2fs_icount_fetch(ctx->inode_link_info, i, &link_count);
124                 ext2fs_icount_fetch(ctx->inode_count, i, &link_counted);
125                 if (link_counted == 0) {
126                         if (e2fsck_process_bad_inode(ctx, 0, i))
127                                 continue;
128                         if (disconnect_inode(ctx, i))
129                                 continue;
130                         ext2fs_icount_fetch(ctx->inode_link_info, i,
131                                             &link_count);
132                         ext2fs_icount_fetch(ctx->inode_count, i,
133                                             &link_counted);
134                 }
135                 if (link_counted != link_count) {
136                         e2fsck_read_inode(ctx, i, &inode, "pass4");
137                         pctx.ino = i;
138                         pctx.inode = &inode;
139                         if (link_count != inode.i_links_count) {
140                                 pctx.num = link_count;
141                                 fix_problem(ctx,
142                                             PR_4_INCONSISTENT_COUNT, &pctx);
143                         }
144                         pctx.num = link_counted;
145                         if (fix_problem(ctx, PR_4_BAD_REF_COUNT, &pctx)) {
146                                 inode.i_links_count = link_counted;
147                                 e2fsck_write_inode(ctx, i, &inode, "pass4");
148                         }
149                 }
150         }
151         ext2fs_free_icount(ctx->inode_link_info); ctx->inode_link_info = 0;
152         ext2fs_free_icount(ctx->inode_count); ctx->inode_count = 0;
153         ext2fs_free_inode_bitmap(ctx->inode_bb_map);
154         ctx->inode_bb_map = 0;
155 #ifdef RESOURCE_TRACK
156         if (ctx->options & E2F_OPT_TIME2) {
157                 e2fsck_clear_progbar(ctx);
158                 print_resource_track("Pass 4", &rtrack);
159         }
160 #endif
161 }
162