pass1.c revision 1.25 1 /* $NetBSD: pass1.c,v 1.25 2002/09/28 20:11:06 dbj 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)pass1.c 8.6 (Berkeley) 4/28/95";
40 #else
41 __RCSID("$NetBSD: pass1.c,v 1.25 2002/09/28 20:11:06 dbj Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <sys/param.h>
46 #include <sys/time.h>
47
48 #include <ufs/ufs/dinode.h>
49 #include <ufs/ufs/dir.h>
50 #include <ufs/ffs/fs.h>
51
52 #include <err.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56
57 #include "fsck.h"
58 #include "extern.h"
59 #include "fsutil.h"
60
61 static ufs_daddr_t badblk;
62 static ufs_daddr_t dupblk;
63 static void checkinode __P((ino_t, struct inodesc *));
64
65 void
66 pass1()
67 {
68 ino_t inumber;
69 int c, i, cgd;
70 struct inodesc idesc;
71
72 /*
73 * Set file system reserved blocks in used block map.
74 */
75 for (c = 0; c < sblock->fs_ncg; c++) {
76 cgd = cgdmin(sblock, c);
77 if (c == 0)
78 i = cgbase(sblock, c);
79 else
80 i = cgsblock(sblock, c);
81 for (; i < cgd; i++)
82 setbmap(i);
83 }
84 i = sblock->fs_csaddr;
85 cgd = i + howmany(sblock->fs_cssize, sblock->fs_fsize);
86 for (; i < cgd; i++)
87 setbmap(i);
88 /*
89 * Find all allocated blocks.
90 */
91 memset(&idesc, 0, sizeof(struct inodesc));
92 idesc.id_type = ADDR;
93 idesc.id_func = pass1check;
94 inumber = 0;
95 n_files = n_blks = 0;
96 resetinodebuf();
97 for (c = 0; c < sblock->fs_ncg; c++) {
98 if (got_siginfo) {
99 fprintf(stderr,
100 "%s: phase 1: cyl group %d of %d (%d%%)\n",
101 cdevname(), c, sblock->fs_ncg,
102 c * 100 / sblock->fs_ncg);
103 got_siginfo = 0;
104 }
105 for (i = 0; i < sblock->fs_ipg; i++, inumber++) {
106 if (inumber < ROOTINO)
107 continue;
108 checkinode(inumber, &idesc);
109 }
110 }
111 freeinodebuf();
112 do_blkswap = 0; /* has been done */
113 }
114
115 static void
116 checkinode(inumber, idesc)
117 ino_t inumber;
118 struct inodesc *idesc;
119 {
120 struct dinode *dp;
121 struct zlncnt *zlnp;
122 int ndb, j;
123 mode_t mode;
124 u_int64_t size;
125 char symbuf[MAXSYMLINKLEN];
126
127 dp = getnextinode(inumber);
128 mode = iswap16(dp->di_mode) & IFMT;
129 size = iswap64(dp->di_size);
130 if (mode == 0) {
131 if (memcmp(dp->di_db, zino.di_db,
132 NDADDR * sizeof(ufs_daddr_t)) ||
133 memcmp(dp->di_ib, zino.di_ib,
134 NIADDR * sizeof(ufs_daddr_t)) ||
135 dp->di_mode || dp->di_size) {
136 pfatal("PARTIALLY ALLOCATED INODE I=%u", inumber);
137 if (reply("CLEAR") == 1) {
138 dp = ginode(inumber);
139 clearinode(dp);
140 inodirty();
141 } else
142 markclean = 0;
143 }
144 statemap[inumber] = USTATE;
145 return;
146 }
147 lastino = inumber;
148 if (/* dp->di_size < 0 || */
149 size + sblock->fs_bsize - 1 < size ||
150 (mode == IFDIR && size > MAXDIRSIZE)) {
151 if (debug)
152 printf("bad size %llu:",(unsigned long long)size);
153 goto unknown;
154 }
155 if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
156 dp = ginode(inumber);
157 dp->di_size = iswap64(sblock->fs_fsize);
158 size = sblock->fs_fsize;
159 dp->di_mode = iswap16(IFREG|0600);
160 inodirty();
161 }
162 ndb = howmany(size, sblock->fs_bsize);
163 if (ndb < 0) {
164 if (debug)
165 printf("bad size %llu ndb %d:",
166 (unsigned long long)size, ndb);
167 goto unknown;
168 }
169 if (mode == IFBLK || mode == IFCHR)
170 ndb++;
171 if (mode == IFLNK) {
172 /*
173 * Note that the old fastlink format always had di_blocks set
174 * to 0. Other than that we no longer use the `spare' field
175 * (which is now the extended uid) for sanity checking, the
176 * new format is the same as the old. We simply ignore the
177 * conversion altogether. - mycroft, 19MAY1994
178 */
179 if (doinglevel2 &&
180 size > 0 && size < MAXSYMLINKLEN &&
181 dp->di_blocks != 0) {
182 if (bread(fsreadfd, symbuf,
183 fsbtodb(sblock, iswap32(dp->di_db[0])),
184 (long)secsize) != 0)
185 errx(EEXIT, "cannot read symlink");
186 if (debug) {
187 symbuf[size] = 0;
188 printf("convert symlink %u(%s) of size %lld\n",
189 inumber, symbuf,
190 (unsigned long long)size);
191 }
192 dp = ginode(inumber);
193 memmove(dp->di_shortlink, symbuf, (long)size);
194 dp->di_blocks = 0;
195 inodirty();
196 }
197 /*
198 * Fake ndb value so direct/indirect block checks below
199 * will detect any garbage after symlink string.
200 */
201 if (size < sblock->fs_maxsymlinklen ||
202 (isappleufs && (size < APPLEUFS_MAXSYMLINKLEN)) ||
203 (sblock->fs_maxsymlinklen == 0 && dp->di_blocks == 0)) {
204 ndb = howmany(size, sizeof(daddr_t));
205 if (ndb > NDADDR) {
206 j = ndb - NDADDR;
207 for (ndb = 1; j > 1; j--)
208 ndb *= NINDIR(sblock);
209 ndb += NDADDR;
210 }
211 }
212 }
213 for (j = ndb; j < NDADDR; j++)
214 if (dp->di_db[j] != 0) {
215 if (debug)
216 printf("bad direct addr ix %d: %d [ndb %d]\n",
217 j, iswap32(dp->di_db[j]), ndb);
218 goto unknown;
219 }
220 for (j = 0, ndb -= NDADDR; ndb > 0; j++)
221 ndb /= NINDIR(sblock);
222 for (; j < NIADDR; j++)
223 if (dp->di_ib[j] != 0) {
224 if (debug)
225 printf("bad indirect addr: %d\n",
226 iswap32(dp->di_ib[j]));
227 goto unknown;
228 }
229 if (ftypeok(dp) == 0)
230 goto unknown;
231 n_files++;
232 lncntp[inumber] = iswap16(dp->di_nlink);
233 if (lncntp[inumber] <= 0) {
234 zlnp = (struct zlncnt *)malloc(sizeof *zlnp);
235 if (zlnp == NULL) {
236 markclean = 0;
237 pfatal("LINK COUNT TABLE OVERFLOW");
238 if (reply("CONTINUE") == 0) {
239 ckfini();
240 exit(EEXIT);
241 }
242 } else {
243 zlnp->zlncnt = inumber;
244 zlnp->next = zlnhead;
245 zlnhead = zlnp;
246 }
247 }
248 if (mode == IFDIR) {
249 if (size == 0)
250 statemap[inumber] = DCLEAR;
251 else
252 statemap[inumber] = DSTATE;
253 cacheino(dp, inumber);
254 } else
255 statemap[inumber] = FSTATE;
256 typemap[inumber] = IFTODT(mode);
257 if (doinglevel2 &&
258 (iswap16(dp->di_ouid) != (u_short)-1 ||
259 iswap16(dp->di_ogid) != (u_short)-1)) {
260 dp = ginode(inumber);
261 dp->di_uid = iswap32(iswap16(dp->di_ouid));
262 dp->di_ouid = iswap16(-1);
263 dp->di_gid = iswap32(iswap16(dp->di_ogid));
264 dp->di_ogid = iswap16(-1);
265 inodirty();
266 }
267 badblk = dupblk = 0;
268 idesc->id_number = inumber;
269 (void)ckinode(dp, idesc);
270 idesc->id_entryno *= btodb(sblock->fs_fsize);
271 if (iswap32(dp->di_blocks) != idesc->id_entryno) {
272 pwarn("INCORRECT BLOCK COUNT I=%u (%d should be %d)",
273 inumber, iswap32(dp->di_blocks), idesc->id_entryno);
274 if (preen)
275 printf(" (CORRECTED)\n");
276 else if (reply("CORRECT") == 0) {
277 markclean = 0;
278 return;
279 }
280 dp = ginode(inumber);
281 dp->di_blocks = iswap32(idesc->id_entryno);
282 inodirty();
283 }
284 return;
285 unknown:
286 pfatal("UNKNOWN FILE TYPE I=%u", inumber);
287 statemap[inumber] = FCLEAR;
288 if (reply("CLEAR") == 1) {
289 statemap[inumber] = USTATE;
290 dp = ginode(inumber);
291 clearinode(dp);
292 inodirty();
293 } else
294 markclean = 0;
295 }
296
297 int
298 pass1check(idesc)
299 struct inodesc *idesc;
300 {
301 int res = KEEPON;
302 int anyout, nfrags;
303 ufs_daddr_t blkno = idesc->id_blkno;
304 struct dups *dlp;
305 struct dups *new;
306
307 if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
308 blkerror(idesc->id_number, "BAD", blkno);
309 if (badblk++ >= MAXBAD) {
310 pwarn("EXCESSIVE BAD BLKS I=%u",
311 idesc->id_number);
312 if (preen)
313 printf(" (SKIPPING)\n");
314 else if (reply("CONTINUE") == 0) {
315 markclean = 0;
316 ckfini();
317 exit(EEXIT);
318 }
319 return (STOP);
320 }
321 }
322 for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
323 if (anyout && chkrange(blkno, 1)) {
324 res = SKIP;
325 } else if (!testbmap(blkno)) {
326 n_blks++;
327 setbmap(blkno);
328 } else {
329 blkerror(idesc->id_number, "DUP", blkno);
330 if (dupblk++ >= MAXDUP) {
331 pwarn("EXCESSIVE DUP BLKS I=%u",
332 idesc->id_number);
333 if (preen)
334 printf(" (SKIPPING)\n");
335 else if (reply("CONTINUE") == 0) {
336 markclean = 0;
337 ckfini();
338 exit(EEXIT);
339 }
340 return (STOP);
341 }
342 new = (struct dups *)malloc(sizeof(struct dups));
343 if (new == NULL) {
344 markclean = 0;
345 pfatal("DUP TABLE OVERFLOW.");
346 if (reply("CONTINUE") == 0) {
347 markclean = 0;
348 ckfini();
349 exit(EEXIT);
350 }
351 return (STOP);
352 }
353 new->dup = blkno;
354 if (muldup == 0) {
355 duplist = muldup = new;
356 new->next = 0;
357 } else {
358 new->next = muldup->next;
359 muldup->next = new;
360 }
361 for (dlp = duplist; dlp != muldup; dlp = dlp->next)
362 if (dlp->dup == blkno)
363 break;
364 if (dlp == muldup && dlp->dup != blkno)
365 muldup = new;
366 }
367 /*
368 * count the number of blocks found in id_entryno
369 */
370 idesc->id_entryno++;
371 }
372 return (res);
373 }
374