Whamcloud - gitweb
Many files:
[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, 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         if (!inode.i_blocks && (LINUX_S_ISREG(inode.i_mode) ||
38                                 LINUX_S_ISDIR(inode.i_mode))) {
39                 /*
40                  * This is a zero-length file; prompt to delete it...
41                  */
42                 if (fix_problem(ctx, PR_4_ZERO_LEN_INODE, &pctx)) {
43                         ext2fs_icount_store(ctx->inode_link_info, i, 0);
44                         inode.i_links_count = 0;
45                         inode.i_dtime = time(0);
46                         e2fsck_write_inode(ctx, i, &inode,
47                                            "disconnect_inode");
48                         /*
49                          * Fix up the bitmaps...
50                          */
51                         e2fsck_read_bitmaps(ctx);
52                         ext2fs_unmark_inode_bitmap(ctx->inode_used_map, i);
53                         ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, i);
54                         ext2fs_unmark_inode_bitmap(fs->inode_map, i);
55                         ext2fs_mark_ib_dirty(fs);
56                         return 0;
57                 }
58         }
59         
60         /*
61          * Prompt to reconnect.
62          */
63         if (fix_problem(ctx, PR_4_UNATTACHED_INODE, &pctx)) {
64                 if (e2fsck_reconnect_file(ctx, i))
65                         ext2fs_unmark_valid(fs);
66         } else {
67                 /*
68                  * If we don't attach the inode, then skip the
69                  * i_links_test since there's no point in trying to
70                  * force i_links_count to zero.
71                  */
72                 ext2fs_unmark_valid(fs);
73                 return 1;
74         }
75         return 0;
76 }
77
78
79 void e2fsck_pass4(e2fsck_t ctx)
80 {
81         ext2_filsys fs = ctx->fs;
82         ino_t   i;
83         struct ext2_inode       inode;
84 #ifdef RESOURCE_TRACK
85         struct resource_track   rtrack;
86 #endif
87         struct problem_context  pctx;
88         __u16   link_count, link_counted;
89         int     group, maxgroup;
90         
91 #ifdef RESOURCE_TRACK
92         init_resource_track(&rtrack);
93 #endif
94
95 #ifdef MTRACE
96         mtrace_print("Pass 4");
97 #endif
98
99         clear_problem_context(&pctx);
100
101         if (!(ctx->options & E2F_OPT_PREEN))
102                 fix_problem(ctx, PR_4_PASS_HEADER, &pctx);
103
104         group = 0;
105         maxgroup = fs->group_desc_count;
106         if (ctx->progress)
107                 if ((ctx->progress)(ctx, 4, 0, maxgroup))
108                         return;
109         
110         for (i=1; i <= fs->super->s_inodes_count; i++) {
111                 if ((i % fs->super->s_inodes_per_group) == 0) {
112                         group++;
113                         if (ctx->progress)
114                                 if ((ctx->progress)(ctx, 4, group, maxgroup))
115                                         return;
116                 }
117                 if (i == EXT2_BAD_INO ||
118                     (i > EXT2_ROOT_INO && i < EXT2_FIRST_INODE(fs->super)))
119                         continue;
120                 if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map, i)) ||
121                     (ctx->inode_imagic_map &&
122                      ext2fs_test_inode_bitmap(ctx->inode_imagic_map, i)) ||
123                     (ctx->inode_bb_map &&
124                      ext2fs_test_inode_bitmap(ctx->inode_bb_map, i)))
125                         continue;
126                 ext2fs_icount_fetch(ctx->inode_link_info, i, &link_count);
127                 ext2fs_icount_fetch(ctx->inode_count, i, &link_counted);
128                 if (link_counted == 0) {
129                         if (e2fsck_process_bad_inode(ctx, 0, i))
130                                 continue;
131                         if (disconnect_inode(ctx, i))
132                                 continue;
133                         ext2fs_icount_fetch(ctx->inode_link_info, i,
134                                             &link_count);
135                         ext2fs_icount_fetch(ctx->inode_count, i,
136                                             &link_counted);
137                 }
138                 if (link_counted != link_count) {
139                         e2fsck_read_inode(ctx, i, &inode, "pass4");
140                         pctx.ino = i;
141                         pctx.inode = &inode;
142                         if (link_count != inode.i_links_count) {
143                                 pctx.num = link_count;
144                                 fix_problem(ctx,
145                                             PR_4_INCONSISTENT_COUNT, &pctx);
146                         }
147                         pctx.num = link_counted;
148                         if (fix_problem(ctx, PR_4_BAD_REF_COUNT, &pctx)) {
149                                 inode.i_links_count = link_counted;
150                                 e2fsck_write_inode(ctx, i, &inode, "pass4");
151                         }
152                 }
153         }
154         ext2fs_free_icount(ctx->inode_link_info); ctx->inode_link_info = 0;
155         ext2fs_free_icount(ctx->inode_count); ctx->inode_count = 0;
156         ext2fs_free_inode_bitmap(ctx->inode_bb_map);
157         ext2fs_free_inode_bitmap(ctx->inode_imagic_map);
158         ctx->inode_bb_map = 0;
159 #ifdef RESOURCE_TRACK
160         if (ctx->options & E2F_OPT_TIME2) {
161                 e2fsck_clear_progbar(ctx);
162                 print_resource_track("Pass 4", &rtrack);
163         }
164 #endif
165 }
166