Whamcloud - gitweb
LU-10641 e2fsck: avoid write inode attemp in readonly mode
[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  *      - A bitmap of EA inodes.                        (inode_ea_map)
15  */
16
17 #include "config.h"
18 #include "e2fsck.h"
19 #include "problem.h"
20 #include <ext2fs/ext2_ext_attr.h>
21
22 /*
23  * This routine is called when an inode is not connected to the
24  * directory tree.
25  *
26  * This subroutine returns 1 then the caller shouldn't bother with the
27  * rest of the pass 4 tests.
28  */
29 static int disconnect_inode(e2fsck_t ctx, ext2_ino_t i,
30                             struct ext2_inode *inode)
31 {
32         ext2_filsys fs = ctx->fs;
33         struct problem_context  pctx;
34         __u32 eamagic = 0;
35         int extra_size = 0;
36
37         if (EXT2_INODE_SIZE(fs->super) > EXT2_GOOD_OLD_INODE_SIZE) {
38                 e2fsck_read_inode_full(ctx, i, inode,EXT2_INODE_SIZE(fs->super),
39                                        "pass4: disconnect_inode");
40                 extra_size = ((struct ext2_inode_large *)inode)->i_extra_isize;
41         } else {
42                 e2fsck_read_inode(ctx, i, inode, "pass4: disconnect_inode");
43         }
44
45         if (inode->i_flags & EXT4_EA_INODE_FL) {
46                 if (ext2fs_test_inode_bitmap2(ctx->inode_ea_map, i)) {
47                         ext2fs_icount_store(ctx->inode_count, i, 1);
48                         return 0;
49                 }
50         }
51
52         clear_problem_context(&pctx);
53         pctx.ino = i;
54         pctx.inode = inode;
55
56         if (EXT2_INODE_SIZE(fs->super) -EXT2_GOOD_OLD_INODE_SIZE -extra_size >0)
57                 eamagic = *(__u32 *)(((char *)inode) +EXT2_GOOD_OLD_INODE_SIZE +
58                                      extra_size);
59         /*
60          * Offer to delete any zero-length files that does not have
61          * blocks.  If there is an EA block, it might have useful
62          * information, so we won't prompt to delete it, but let it be
63          * reconnected to lost+found.
64          */
65         if (!inode->i_blocks && eamagic != EXT2_EXT_ATTR_MAGIC &&
66             (LINUX_S_ISREG(inode->i_mode) || LINUX_S_ISDIR(inode->i_mode))) {
67                 if (fix_problem(ctx, PR_4_ZERO_LEN_INODE, &pctx)) {
68                         e2fsck_clear_inode(ctx, i, inode, 0,
69                                            "disconnect_inode");
70                         /*
71                          * Fix up the bitmaps...
72                          */
73                         e2fsck_read_bitmaps(ctx);
74                         ext2fs_inode_alloc_stats2(fs, i, -1,
75                                                   LINUX_S_ISDIR(inode->i_mode));
76                         quota_data_inodes(ctx->qctx, inode, i, -1);
77                         return 0;
78                 }
79         }
80
81         /*
82          * Prompt to reconnect.
83          */
84         if (fix_problem(ctx, PR_4_UNATTACHED_INODE, &pctx)) {
85                 if (inode->i_flags & EXT4_EA_INODE_FL) {
86                         /* Zero the link count so that when inode is linked to
87                          * lost+found it has correct link count */
88                         inode->i_links_count = 0;
89                         e2fsck_write_inode(ctx, i, inode, "disconnect_inode");
90                         ext2fs_icount_store(ctx->inode_link_info, i, 0);
91                 }
92                 if (e2fsck_reconnect_file(ctx, i))
93                         ext2fs_unmark_valid(fs);
94         } else {
95                 /*
96                  * If we don't attach the inode, then skip the
97                  * i_links_test since there's no point in trying to
98                  * force i_links_count to zero.
99                  */
100                 ext2fs_unmark_valid(fs);
101                 return 1;
102         }
103         return 0;
104 }
105
106
107 void e2fsck_pass4(e2fsck_t ctx)
108 {
109         ext2_filsys fs = ctx->fs;
110         ext2_ino_t      i;
111         struct ext2_inode       *inode;
112 #ifdef RESOURCE_TRACK
113         struct resource_track   rtrack;
114 #endif
115         struct problem_context  pctx;
116         __u16   link_count, link_counted;
117         char    *buf = 0;
118         dgrp_t  group, maxgroup;
119
120         init_resource_track(&rtrack, ctx->fs->io);
121
122 #ifdef MTRACE
123         mtrace_print("Pass 4");
124 #endif
125
126         clear_problem_context(&pctx);
127
128         if (!(ctx->options & E2F_OPT_PREEN))
129                 fix_problem(ctx, PR_4_PASS_HEADER, &pctx);
130
131         group = 0;
132         maxgroup = fs->group_desc_count;
133         if (ctx->progress)
134                 if ((ctx->progress)(ctx, 4, 0, maxgroup))
135                         return;
136
137         inode = e2fsck_allocate_memory(ctx, EXT2_INODE_SIZE(fs->super),
138                                        "scratch inode");
139
140         /* Protect loop from wrap-around if s_inodes_count maxed */
141         for (i=1; i <= fs->super->s_inodes_count && i > 0; i++) {
142                 int isdir;
143
144                 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
145                         goto errout;
146                 if ((i % fs->super->s_inodes_per_group) == 0) {
147                         group++;
148                         if (ctx->progress)
149                                 if ((ctx->progress)(ctx, 4, group, maxgroup))
150                                         goto errout;
151                 }
152                 if (i == quota_type2inum(PRJQUOTA, ctx->fs->super) ||
153                     i == EXT2_BAD_INO ||
154                     (i > EXT2_ROOT_INO && i < EXT2_FIRST_INODE(fs->super)))
155                         continue;
156                 if (!(ext2fs_test_inode_bitmap2(ctx->inode_used_map, i)) ||
157                     (ctx->inode_imagic_map &&
158                      ext2fs_test_inode_bitmap2(ctx->inode_imagic_map, i)) ||
159                     (ctx->inode_bb_map &&
160                      ext2fs_test_inode_bitmap2(ctx->inode_bb_map, i)))
161                         continue;
162                 ext2fs_icount_fetch(ctx->inode_link_info, i, &link_count);
163                 ext2fs_icount_fetch(ctx->inode_count, i, &link_counted);
164                 if (link_counted == 0) {
165                         if (!buf)
166                                 buf = e2fsck_allocate_memory(ctx,
167                                      fs->blocksize, "bad_inode buffer");
168                         if (e2fsck_process_bad_inode(ctx, 0, i, buf))
169                                 continue;
170                         if (disconnect_inode(ctx, i, inode))
171                                 continue;
172                         ext2fs_icount_fetch(ctx->inode_link_info, i,
173                                             &link_count);
174                         ext2fs_icount_fetch(ctx->inode_count, i,
175                                             &link_counted);
176                 }
177                 isdir = ext2fs_test_inode_bitmap2(ctx->inode_dir_map, i);
178                 if (isdir && (link_counted > EXT2_LINK_MAX))
179                         link_counted = 1;
180                 if (link_counted != link_count) {
181                         e2fsck_read_inode(ctx, i, inode, "pass4");
182                         pctx.ino = i;
183                         pctx.inode = inode;
184                         if ((link_count != inode->i_links_count) && !isdir &&
185                             (inode->i_links_count <= EXT2_LINK_MAX)) {
186                                 pctx.num = link_count;
187                                 fix_problem(ctx,
188                                             PR_4_INCONSISTENT_COUNT, &pctx);
189                         }
190                         pctx.num = link_counted;
191                         /* i_link_count was previously exceeded, but no longer
192                          * is, fix this but don't consider it an error */
193                         if ((isdir && link_counted > 1 &&
194                              (inode->i_flags & EXT2_INDEX_FL) &&
195                              link_count == 1 && !(ctx->options & E2F_OPT_NO)) ||
196                             fix_problem(ctx, PR_4_BAD_REF_COUNT, &pctx)) {
197                                 inode->i_links_count = link_counted;
198                                 e2fsck_write_inode(ctx, i, inode, "pass4");
199                         }
200                 }
201         }
202         ext2fs_free_icount(ctx->inode_link_info); ctx->inode_link_info = 0;
203         ext2fs_free_icount(ctx->inode_count); ctx->inode_count = 0;
204         ext2fs_free_icount(ctx->inode_badness); ctx->inode_badness = 0;
205         ext2fs_free_inode_bitmap(ctx->inode_ea_map);
206         ctx->inode_ea_map = 0;
207         ext2fs_free_inode_bitmap(ctx->inode_bb_map);
208         ctx->inode_bb_map = 0;
209         ext2fs_free_inode_bitmap(ctx->inode_imagic_map);
210         ctx->inode_imagic_map = 0;
211 errout:
212         if (buf)
213                 ext2fs_free_mem(&buf);
214
215         ext2fs_free_mem(&inode);
216         print_resource_track(ctx, _("Pass 4"), &rtrack, ctx->fs->io);
217 }
218