pass5.c revision 1.15 1 /* $NetBSD: pass5.c,v 1.15 2008/03/16 23:17:55 lukem 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.15 2008/03/16 23:17:55 lukem 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 <string.h>
76 #include <malloc.h>
77 #include <stdio.h>
78
79 #include "fsutil.h"
80 #include "fsck.h"
81 #include "extern.h"
82
83
84 void print_bmap(u_char *,u_int32_t);
85
86 void
87 pass5(void)
88 {
89 int c;
90 struct m_ext2fs *fs = &sblock;
91 daddr_t dbase, dmax;
92 daddr_t d;
93 long i, j;
94 struct inodesc idesc[3];
95 struct bufarea *ino_bitmap = NULL, *blk_bitmap = NULL;
96 char *ibmap, *bbmap;
97 u_int32_t cs_ndir, cs_nbfree, cs_nifree;
98 char msg[255];
99
100 cs_ndir = 0;
101 cs_nbfree = 0;
102 cs_nifree = 0;
103
104 ibmap = malloc(fs->e2fs_bsize);
105 bbmap = malloc(fs->e2fs_bsize);
106 if (ibmap == NULL || bbmap == NULL) {
107 errexit("out of memory");
108 }
109
110 for (c = 0; c < fs->e2fs_ncg; c++) {
111 u_int32_t nbfree = 0;
112 u_int32_t nifree = 0;
113 u_int32_t ndirs = 0;
114
115 nbfree = 0;
116 nifree = fs->e2fs.e2fs_ipg;
117 ndirs = 0;
118
119 if (blk_bitmap == NULL) {
120 blk_bitmap = getdatablk(fs2h32(fs->e2fs_gd[c].ext2bgd_b_bitmap),
121 fs->e2fs_bsize);
122 } else {
123 getblk(blk_bitmap, fs2h32(fs->e2fs_gd[c].ext2bgd_b_bitmap),
124 fs->e2fs_bsize);
125 }
126 if (ino_bitmap == NULL) {
127 ino_bitmap = getdatablk(fs2h32(fs->e2fs_gd[c].ext2bgd_i_bitmap),
128 fs->e2fs_bsize);
129 } else {
130 getblk(ino_bitmap, fs2h32(fs->e2fs_gd[c].ext2bgd_i_bitmap),
131 fs->e2fs_bsize);
132 }
133 memset(bbmap, 0, fs->e2fs_bsize);
134 memset(ibmap, 0, fs->e2fs_bsize);
135 memset(&idesc[0], 0, sizeof idesc);
136 for (i = 0; i < 3; i++) {
137 idesc[i].id_type = ADDR;
138 }
139
140 j = fs->e2fs.e2fs_ipg * c + 1;
141
142 for (i = 0; i < fs->e2fs.e2fs_ipg; j++, i++) {
143 if ((j < EXT2_FIRSTINO) && (j != EXT2_ROOTINO)) {
144 setbit(ibmap, i);
145 nifree--;
146 continue;
147 }
148 if (j > fs->e2fs.e2fs_icount) {
149 setbit(ibmap, i);
150 continue;
151 }
152 switch (statemap[j]) {
153
154 case USTATE:
155 break;
156
157 case DSTATE:
158 case DCLEAR:
159 case DFOUND:
160 ndirs++;
161 /* fall through */
162
163 case FSTATE:
164 case FCLEAR:
165 nifree--;
166 setbit(ibmap, i);
167 break;
168
169 default:
170 errexit("BAD STATE %d FOR INODE I=%ld",
171 statemap[j], j);
172 }
173 }
174
175 /* fill in unused par of the inode map */
176 for (i = fs->e2fs.e2fs_ipg / NBBY; i < fs->e2fs_bsize; i++)
177 ibmap[i] = 0xff;
178
179 dbase = c * sblock.e2fs.e2fs_bpg +
180 sblock.e2fs.e2fs_first_dblock;
181 dmax = (c+1) * sblock.e2fs.e2fs_bpg +
182 sblock.e2fs.e2fs_first_dblock;
183
184 for (i = 0, d = dbase;
185 d < dmax;
186 d ++, i ++) {
187 if (testbmap(d) || d >= sblock.e2fs.e2fs_bcount) {
188 setbit(bbmap, i);
189 continue;
190 } else {
191 nbfree++;
192 }
193
194 }
195 cs_nbfree += nbfree;
196 cs_nifree += nifree;
197 cs_ndir += ndirs;
198
199 if (debug && (fs2h16(fs->e2fs_gd[c].ext2bgd_nbfree) != nbfree ||
200 fs2h16(fs->e2fs_gd[c].ext2bgd_nifree) != nifree ||
201 fs2h16(fs->e2fs_gd[c].ext2bgd_ndirs) != ndirs)) {
202 printf("summary info for cg %d is %d, %d, %d,"
203 "should be %d, %d, %d\n", c,
204 fs2h16(fs->e2fs_gd[c].ext2bgd_nbfree),
205 fs2h16(fs->e2fs_gd[c].ext2bgd_nifree),
206 fs2h16(fs->e2fs_gd[c].ext2bgd_ndirs),
207 nbfree,
208 nifree,
209 ndirs);
210 }
211 (void)snprintf(msg, sizeof(msg),
212 "SUMMARY INFORMATIONS WRONG FOR CG #%d", c);
213 if ((fs2h16(fs->e2fs_gd[c].ext2bgd_nbfree) != nbfree ||
214 fs2h16(fs->e2fs_gd[c].ext2bgd_nifree) != nifree ||
215 fs2h16(fs->e2fs_gd[c].ext2bgd_ndirs) != ndirs) &&
216 dofix(&idesc[0], msg)) {
217 fs->e2fs_gd[c].ext2bgd_nbfree = h2fs16(nbfree);
218 fs->e2fs_gd[c].ext2bgd_nifree = h2fs16(nifree);
219 fs->e2fs_gd[c].ext2bgd_ndirs = h2fs16(ndirs);
220 sbdirty();
221 }
222
223 if (debug && memcmp(blk_bitmap->b_un.b_buf, bbmap, fs->e2fs_bsize)) {
224 printf("blk_bitmap:\n");
225 print_bmap(blk_bitmap->b_un.b_buf, fs->e2fs_bsize);
226 printf("bbmap:\n");
227 print_bmap(bbmap, fs->e2fs_bsize);
228 }
229
230 (void)snprintf(msg, sizeof(msg),
231 "BLK(S) MISSING IN BIT MAPS #%d", c);
232 if (memcmp(blk_bitmap->b_un.b_buf, bbmap, fs->e2fs_bsize) &&
233 dofix(&idesc[1], msg)) {
234 memcpy(blk_bitmap->b_un.b_buf, bbmap, fs->e2fs_bsize);
235 dirty(blk_bitmap);
236 }
237 if (debug && memcmp(ino_bitmap->b_un.b_buf, ibmap, fs->e2fs_bsize)) {
238 printf("ino_bitmap:\n");
239 print_bmap(ino_bitmap->b_un.b_buf, fs->e2fs_bsize);
240 printf("ibmap:\n");
241 print_bmap(ibmap, fs->e2fs_bsize);
242 }
243 (void)snprintf(msg, sizeof(msg),
244 "INODE(S) MISSING IN BIT MAPS #%d", c);
245 if (memcmp(ino_bitmap->b_un.b_buf, ibmap, fs->e2fs_bsize) &&
246 dofix(&idesc[1], msg)) {
247 memcpy(ino_bitmap->b_un.b_buf, ibmap, fs->e2fs_bsize);
248 dirty(ino_bitmap);
249 }
250
251 }
252 if (debug && (fs->e2fs.e2fs_fbcount != cs_nbfree ||
253 fs->e2fs.e2fs_ficount != cs_nifree)) {
254 printf("summary info bad in superblock: %d, %d should be %d, %d\n",
255 fs->e2fs.e2fs_fbcount, fs->e2fs.e2fs_ficount,
256 cs_nbfree, cs_nifree);
257 }
258 if ((fs->e2fs.e2fs_fbcount != cs_nbfree ||
259 fs->e2fs.e2fs_ficount != cs_nifree)
260 && dofix(&idesc[0], "SUPERBLK SUMMARY INFORMATION BAD")) {
261 fs->e2fs.e2fs_fbcount = cs_nbfree;
262 fs->e2fs.e2fs_ficount = cs_nifree;
263 sbdirty();
264 }
265 free(ibmap);
266 free(bbmap);
267 }
268
269 void
270 print_bmap(u_char *map, u_int32_t size)
271 {
272 int i, j;
273
274 i = 0;
275 while (i < size) {
276 printf("%04x: ",i);
277 for (j = 0; j < 16; j++, i++)
278 printf("%02x ", (u_int)map[i] & 0xff);
279 printf("\n");
280 }
281 }
282