pass5.c revision 1.10 1 /* $NetBSD: pass5.c,v 1.10 2004/03/22 19:46:53 bouyer Exp $ */
2
3 /*
4 * Copyright (c) 1980, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1997 Manuel Bouyer.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by Manuel Bouyer.
46 * 4. The name of the author may not be used to endorse or promote products
47 * derived from this software without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
50 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
53 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
54 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
55 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
56 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
58 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 */
60
61 #include <sys/cdefs.h>
62 #ifndef lint
63 #if 0
64 static char sccsid[] = "@(#)pass5.c 8.6 (Berkeley) 11/30/94";
65 #else
66 __RCSID("$NetBSD: pass5.c,v 1.10 2004/03/22 19:46:53 bouyer Exp $");
67 #endif
68 #endif /* not lint */
69
70 #include <sys/param.h>
71 #include <sys/time.h>
72 #include <ufs/ufs/dinode.h>
73 #include <ufs/ext2fs/ext2fs_dinode.h>
74 #include <ufs/ext2fs/ext2fs.h>
75 #include <ufs/ext2fs/ext2fs_extern.h>
76 #include <string.h>
77 #include <malloc.h>
78 #include <stdio.h>
79
80 #include "fsutil.h"
81 #include "fsck.h"
82 #include "extern.h"
83
84
85 void print_bmap __P((u_char *,u_int32_t));
86
87 void
88 pass5()
89 {
90 int c;
91 struct m_ext2fs *fs = &sblock;
92 daddr_t dbase, dmax;
93 daddr_t d;
94 long i, j;
95 struct inodesc idesc[3];
96 struct bufarea *ino_bitmap = NULL, *blk_bitmap = NULL;
97 char *ibmap, *bbmap;
98 u_int32_t cs_ndir, cs_nbfree, cs_nifree;
99 char msg[255];
100
101 cs_ndir = 0;
102 cs_nbfree = 0;
103 cs_nifree = 0;
104
105 ibmap = malloc(fs->e2fs_bsize);
106 bbmap = malloc(fs->e2fs_bsize);
107 if (ibmap == NULL || bbmap == NULL) {
108 errexit("out of memory\n");
109 }
110
111 for (c = 0; c < fs->e2fs_ncg; c++) {
112 u_int32_t nbfree = 0;
113 u_int32_t nifree = 0;
114 u_int32_t ndirs = 0;
115
116 nbfree = 0;
117 nifree = fs->e2fs.e2fs_ipg;
118 ndirs = 0;
119
120 if (blk_bitmap == NULL) {
121 blk_bitmap = getdatablk(fs2h32(fs->e2fs_gd[c].ext2bgd_b_bitmap),
122 fs->e2fs_bsize);
123 } else {
124 getblk(blk_bitmap, fs2h32(fs->e2fs_gd[c].ext2bgd_b_bitmap),
125 fs->e2fs_bsize);
126 }
127 if (ino_bitmap == NULL) {
128 ino_bitmap = getdatablk(fs2h32(fs->e2fs_gd[c].ext2bgd_i_bitmap),
129 fs->e2fs_bsize);
130 } else {
131 getblk(ino_bitmap, fs2h32(fs->e2fs_gd[c].ext2bgd_i_bitmap),
132 fs->e2fs_bsize);
133 }
134 memset(bbmap, 0, fs->e2fs_bsize);
135 memset(ibmap, 0, fs->e2fs_bsize);
136 memset(&idesc[0], 0, sizeof idesc);
137 for (i = 0; i < 3; i++) {
138 idesc[i].id_type = ADDR;
139 }
140
141 j = fs->e2fs.e2fs_ipg * c + 1;
142
143 for (i = 0; i < fs->e2fs.e2fs_ipg; j++, i++) {
144 if ((j < EXT2_FIRSTINO) && (j != EXT2_ROOTINO)) {
145 setbit(ibmap, i);
146 nifree--;
147 continue;
148 }
149 if (j > fs->e2fs.e2fs_icount) {
150 setbit(ibmap, i);
151 continue;
152 }
153 switch (statemap[j]) {
154
155 case USTATE:
156 break;
157
158 case DSTATE:
159 case DCLEAR:
160 case DFOUND:
161 ndirs++;
162 /* fall through */
163
164 case FSTATE:
165 case FCLEAR:
166 nifree--;
167 setbit(ibmap, i);
168 break;
169
170 default:
171 errexit("BAD STATE %d FOR INODE I=%ld\n",
172 statemap[j], j);
173 }
174 }
175
176 /* fill in unused par of the inode map */
177 for (i = fs->e2fs.e2fs_ipg / NBBY; i < fs->e2fs_bsize; i++)
178 ibmap[i] = 0xff;
179
180 dbase = c * sblock.e2fs.e2fs_bpg +
181 sblock.e2fs.e2fs_first_dblock;
182 dmax = (c+1) * sblock.e2fs.e2fs_bpg +
183 sblock.e2fs.e2fs_first_dblock;
184
185 for (i = 0, d = dbase;
186 d < dmax;
187 d ++, i ++) {
188 if (testbmap(d) || d >= sblock.e2fs.e2fs_bcount) {
189 setbit(bbmap, i);
190 continue;
191 } else {
192 nbfree++;
193 }
194
195 }
196 cs_nbfree += nbfree;
197 cs_nifree += nifree;
198 cs_ndir += ndirs;
199
200 if (debug && (fs2h16(fs->e2fs_gd[c].ext2bgd_nbfree) != nbfree ||
201 fs2h16(fs->e2fs_gd[c].ext2bgd_nifree) != nifree ||
202 fs2h16(fs->e2fs_gd[c].ext2bgd_ndirs) != ndirs)) {
203 printf("summary info for cg %d is %d, %d, %d,"
204 "should be %d, %d, %d\n", c,
205 fs2h16(fs->e2fs_gd[c].ext2bgd_nbfree),
206 fs2h16(fs->e2fs_gd[c].ext2bgd_nifree),
207 fs2h16(fs->e2fs_gd[c].ext2bgd_ndirs),
208 nbfree,
209 nifree,
210 ndirs);
211 }
212 (void)snprintf(msg, sizeof(msg),
213 "SUMMARY INFORMATIONS WRONG FOR CG #%d", c);
214 if ((fs2h16(fs->e2fs_gd[c].ext2bgd_nbfree) != nbfree ||
215 fs2h16(fs->e2fs_gd[c].ext2bgd_nifree) != nifree ||
216 fs2h16(fs->e2fs_gd[c].ext2bgd_ndirs) != ndirs) &&
217 dofix(&idesc[0], msg)) {
218 fs->e2fs_gd[c].ext2bgd_nbfree = h2fs16(nbfree);
219 fs->e2fs_gd[c].ext2bgd_nifree = h2fs16(nifree);
220 fs->e2fs_gd[c].ext2bgd_ndirs = h2fs16(ndirs);
221 sbdirty();
222 }
223
224 if (debug && memcmp(blk_bitmap->b_un.b_buf, bbmap, fs->e2fs_bsize)) {
225 printf("blk_bitmap:\n");
226 print_bmap(blk_bitmap->b_un.b_buf, fs->e2fs_bsize);
227 printf("bbmap:\n");
228 print_bmap(bbmap, fs->e2fs_bsize);
229 }
230
231 (void)snprintf(msg, sizeof(msg),
232 "BLK(S) MISSING IN BIT MAPS #%d", c);
233 if (memcmp(blk_bitmap->b_un.b_buf, bbmap, fs->e2fs_bsize) &&
234 dofix(&idesc[1], msg)) {
235 memcpy(blk_bitmap->b_un.b_buf, bbmap, fs->e2fs_bsize);
236 dirty(blk_bitmap);
237 }
238 if (debug && memcmp(ino_bitmap->b_un.b_buf, ibmap, fs->e2fs_bsize)) {
239 printf("ino_bitmap:\n");
240 print_bmap(ino_bitmap->b_un.b_buf, fs->e2fs_bsize);
241 printf("ibmap:\n");
242 print_bmap(ibmap, fs->e2fs_bsize);
243 }
244 (void)snprintf(msg, sizeof(msg),
245 "INODE(S) MISSING IN BIT MAPS #%d", c);
246 if (memcmp(ino_bitmap->b_un.b_buf, ibmap, fs->e2fs_bsize) &&
247 dofix(&idesc[1], msg)) {
248 memcpy(ino_bitmap->b_un.b_buf, ibmap, fs->e2fs_bsize);
249 dirty(ino_bitmap);
250 }
251
252 }
253 if (debug && (fs->e2fs.e2fs_fbcount != cs_nbfree ||
254 fs->e2fs.e2fs_ficount != cs_nifree)) {
255 printf("summary info bad in superblock: %d, %d should be %d, %d\n",
256 fs->e2fs.e2fs_fbcount, fs->e2fs.e2fs_ficount,
257 cs_nbfree, cs_nifree);
258 }
259 if ((fs->e2fs.e2fs_fbcount != cs_nbfree ||
260 fs->e2fs.e2fs_ficount != cs_nifree)
261 && dofix(&idesc[0], "SUPERBLK SUMMARY INFORMATION BAD")) {
262 fs->e2fs.e2fs_fbcount = cs_nbfree;
263 fs->e2fs.e2fs_ficount = cs_nifree;
264 sbdirty();
265 }
266 }
267
268 void
269 print_bmap(map, size)
270 u_char *map;
271 u_int32_t size;
272 {
273 int i, j;
274
275 i = 0;
276 while (i < size) {
277 printf("%u: ",i);
278 for (j = 0; j < 16; j++, i++)
279 printf("%2x ", (u_int)map[i] & 0xff);
280 printf("\n");
281 }
282 }
283