pass1.c revision 1.42 1 /* $NetBSD: pass1.c,v 1.42 2006/10/16 03:09:26 christos 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 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)pass1.c 8.6 (Berkeley) 4/28/95";
36 #else
37 __RCSID("$NetBSD: pass1.c,v 1.42 2006/10/16 03:09:26 christos Exp $");
38 #endif
39 #endif /* not lint */
40
41 #include <sys/param.h>
42 #include <sys/stat.h>
43 #include <sys/time.h>
44
45 #include <ufs/ufs/dinode.h>
46 #include <ufs/ufs/dir.h>
47 #include <ufs/ffs/fs.h>
48 #include <ufs/ufs/ufs_bswap.h>
49 #include <ufs/ffs/ffs_extern.h>
50
51 #include <err.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55
56 #include "fsck.h"
57 #include "extern.h"
58 #include "fsutil.h"
59
60 static daddr_t badblk;
61 static daddr_t dupblk;
62 static void checkinode(ino_t, struct inodesc *);
63 static ino_t lastino;
64
65 void
66 pass1(void)
67 {
68 ino_t inumber, inosused;
69 int c;
70 daddr_t i, cgd;
71 struct inodesc idesc;
72 struct cg *cgp = cgrp;
73 struct inostat *info;
74 uint8_t *cp;
75
76 /*
77 * Set file system reserved blocks in used block map.
78 */
79 for (c = 0; c < sblock->fs_ncg; c++) {
80 cgd = cgdmin(sblock, c);
81 if (c == 0)
82 i = cgbase(sblock, c);
83 else
84 i = cgsblock(sblock, c);
85 for (; i < cgd; i++)
86 setbmap(i);
87 }
88 i = sblock->fs_csaddr;
89 cgd = i + howmany(sblock->fs_cssize, sblock->fs_fsize);
90 for (; i < cgd; i++)
91 setbmap(i);
92 /*
93 * Find all allocated blocks.
94 */
95 memset(&idesc, 0, sizeof(struct inodesc));
96 idesc.id_func = pass1check;
97 n_files = n_blks = 0;
98 for (c = 0; c < sblock->fs_ncg; c++) {
99 inumber = c * sblock->fs_ipg;
100 setinodebuf(inumber);
101 getblk(&cgblk, cgtod(sblock, c), sblock->fs_cgsize);
102 memcpy(cgp, cgblk.b_un.b_cg, sblock->fs_cgsize);
103 if((doswap && !needswap) || (!doswap && needswap))
104 ffs_cg_swap(cgblk.b_un.b_cg, cgp, sblock);
105 if (is_ufs2)
106 inosused = cgp->cg_initediblk;
107 else
108 inosused = sblock->fs_ipg;
109 if (got_siginfo) {
110 printf("%s: phase 1: cyl group %d of %d (%d%%)\n",
111 cdevname(), c, sblock->fs_ncg,
112 c * 100 / sblock->fs_ncg);
113 got_siginfo = 0;
114 }
115 #ifdef PROGRESS
116 progress_bar(cdevname(), preen ? NULL : "phase 1",
117 c, sblock->fs_ncg);
118 #endif /* PROGRESS */
119 /*
120 * If we are using soft updates, then we can trust the
121 * cylinder group inode allocation maps to tell us which
122 * inodes are allocated. We will scan the used inode map
123 * to find the inodes that are really in use, and then
124 * read only those inodes in from disk.
125 */
126 if (preen && usedsoftdep) {
127 if (!cg_chkmagic(cgp, 0))
128 pfatal("CG %d: BAD MAGIC NUMBER\n", c);
129 cp = &cg_inosused(cgp, 0)[(inosused - 1) / CHAR_BIT];
130 for ( ; inosused > 0; inosused -= CHAR_BIT, cp--) {
131 if (*cp == 0)
132 continue;
133 for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) {
134 if (*cp & i)
135 break;
136 inosused--;
137 }
138 break;
139 }
140 #ifdef notdef
141 if (inosused < 0)
142 inosused = 0;
143 #endif
144 }
145 /*
146 * Allocate inoinfo structures for the allocated inodes.
147 */
148 inostathead[c].il_numalloced = inosused;
149 if (inosused == 0) {
150 inostathead[c].il_stat = 0;
151 continue;
152 }
153 info = calloc((unsigned)inosused, sizeof(struct inostat));
154 if (info == NULL) {
155 pfatal("cannot alloc %u bytes for inoinfo\n",
156 (unsigned)(sizeof(struct inostat) * inosused));
157 exit(EEXIT);
158 }
159 inostathead[c].il_stat = info;
160 /*
161 * Scan the allocated inodes.
162 */
163 for (i = 0; i < inosused; i++, inumber++) {
164 if (inumber < ROOTINO) {
165 (void)getnextinode(inumber);
166 continue;
167 }
168 checkinode(inumber, &idesc);
169 }
170 lastino += 1;
171 if (inosused < sblock->fs_ipg || inumber == lastino)
172 continue;
173 /*
174 * If we were not able to determine in advance which inodes
175 * were in use, then reduce the size of the inoinfo structure
176 * to the size necessary to describe the inodes that we
177 * really found.
178 */
179 if (lastino < (c * sblock->fs_ipg))
180 inosused = 0;
181 else
182 inosused = lastino - (c * sblock->fs_ipg);
183 inostathead[c].il_numalloced = inosused;
184 if (inosused == 0) {
185 free(inostathead[c].il_stat);
186 inostathead[c].il_stat = 0;
187 continue;
188 }
189 info = calloc((unsigned)inosused, sizeof(struct inostat));
190 if (info == NULL) {
191 pfatal("cannot alloc %u bytes for inoinfo\n",
192 (unsigned)(sizeof(struct inostat) * inosused));
193 exit(EEXIT);
194 }
195 memmove(info, inostathead[c].il_stat, inosused * sizeof(*info));
196 free(inostathead[c].il_stat);
197 inostathead[c].il_stat = info;
198 }
199 #ifdef PROGRESS
200 if (preen)
201 progress_add(sblock->fs_ncg);
202 else
203 progress_done();
204 #endif /* PROGRESS */
205 freeinodebuf();
206 do_blkswap = 0; /* has been done */
207 }
208
209 static void
210 checkinode(ino_t inumber, struct inodesc *idesc)
211 {
212 union dinode *dp;
213 struct zlncnt *zlnp;
214 daddr_t ndb;
215 int j;
216 mode_t mode;
217 u_int64_t size, kernmaxfilesize;
218 int64_t blocks;
219 char symbuf[MAXBSIZE];
220 struct inostat *info;
221
222 dp = getnextinode(inumber);
223 info = inoinfo(inumber);
224 mode = iswap16(DIP(dp, mode)) & IFMT;
225 size = iswap64(DIP(dp, size));
226 if (mode == 0) {
227 if ((is_ufs2 &&
228 (memcmp(dp->dp2.di_db, ufs2_zino.di_db,
229 NDADDR * sizeof(int64_t)) ||
230 memcmp(dp->dp2.di_ib, ufs2_zino.di_ib,
231 NIADDR * sizeof(int64_t))))
232 ||
233 (!is_ufs2 &&
234 (memcmp(dp->dp1.di_db, ufs1_zino.di_db,
235 NDADDR * sizeof(int32_t)) ||
236 memcmp(dp->dp1.di_ib, ufs1_zino.di_ib,
237 NIADDR * sizeof(int32_t)))) ||
238 mode || size) {
239 pfatal("PARTIALLY ALLOCATED INODE I=%llu",
240 (unsigned long long)inumber);
241 if (reply("CLEAR") == 1) {
242 dp = ginode(inumber);
243 clearinode(dp);
244 inodirty();
245 } else
246 markclean = 0;
247 }
248 info->ino_state = USTATE;
249 return;
250 }
251 lastino = inumber;
252 /* This should match the file size limit in ffs_mountfs(). */
253 if (is_ufs2)
254 kernmaxfilesize = sblock->fs_maxfilesize;
255 else
256 kernmaxfilesize = (u_int64_t)0x80000000 * sblock->fs_bsize - 1;
257 if (size > kernmaxfilesize || size + sblock->fs_bsize - 1 < size ||
258 (mode == IFDIR && size > MAXDIRSIZE)) {
259 if (debug)
260 printf("bad size %llu:",(unsigned long long)size);
261 goto unknown;
262 }
263 if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
264 dp = ginode(inumber);
265 DIP_SET(dp, size, iswap64(sblock->fs_fsize));
266 size = sblock->fs_fsize;
267 DIP_SET(dp, mode, iswap16(IFREG|0600));
268 inodirty();
269 }
270 ndb = howmany(size, sblock->fs_bsize);
271 if (ndb < 0) {
272 if (debug)
273 printf("bad size %llu ndb %lld:",
274 (unsigned long long)size, (long long)ndb);
275 goto unknown;
276 }
277 if (mode == IFBLK || mode == IFCHR)
278 ndb++;
279 if (mode == IFLNK) {
280 /*
281 * Note that the old fastlink format always had di_blocks set
282 * to 0. Other than that we no longer use the `spare' field
283 * (which is now the extended uid) for sanity checking, the
284 * new format is the same as the old. We simply ignore the
285 * conversion altogether. - mycroft, 19MAY1994
286 */
287 if (!is_ufs2 && doinglevel2 &&
288 size > 0 && size < MAXSYMLINKLEN_UFS1 &&
289 DIP(dp, blocks) != 0) {
290 if (bread(fsreadfd, symbuf,
291 fsbtodb(sblock, iswap32(DIP(dp, db[0]))),
292 (long)secsize) != 0)
293 errx(EEXIT, "cannot read symlink");
294 if (debug) {
295 symbuf[size] = 0;
296 printf("convert symlink %llu(%s) "
297 "of size %lld\n",
298 (unsigned long long)inumber, symbuf,
299 (unsigned long long)size);
300 }
301 dp = ginode(inumber);
302 memmove(dp->dp1.di_db, symbuf, (long)size);
303 DIP_SET(dp, blocks, 0);
304 inodirty();
305 }
306 /*
307 * Fake ndb value so direct/indirect block checks below
308 * will detect any garbage after symlink string.
309 */
310 if ((sblock->fs_maxsymlinklen < 0) ||
311 (size < sblock->fs_maxsymlinklen) ||
312 (isappleufs && (size < APPLEUFS_MAXSYMLINKLEN)) ||
313 (sblock->fs_maxsymlinklen == 0 && DIP(dp, blocks) == 0)) {
314 if (is_ufs2)
315 ndb = howmany(size, sizeof(int64_t));
316 else
317 ndb = howmany(size, sizeof(int32_t));
318 if (ndb > NDADDR) {
319 j = ndb - NDADDR;
320 for (ndb = 1; j > 1; j--)
321 ndb *= NINDIR(sblock);
322 ndb += NDADDR;
323 }
324 }
325 }
326 if (ndb < NDADDR) {
327 for (j = ndb; j < NDADDR; j++)
328 if (DIP(dp, db[j]) != 0) {
329 if (debug) {
330 if (!is_ufs2)
331 printf("bad direct addr ix %d: %d [ndb %lld]\n",
332 j, iswap32(dp->dp1.di_db[j]),
333 (long long)ndb);
334 else
335 printf("bad direct addr ix %d: %lld [ndb %lld]\n",
336 j, (long long)iswap64(dp->dp2.di_db[j]),
337 (long long)ndb);
338 }
339 goto unknown;
340 }
341 }
342
343 for (j = 0, ndb -= NDADDR; ndb > 0; j++)
344 ndb /= NINDIR(sblock);
345
346 for (; j < NIADDR; j++)
347 if (DIP(dp, ib[j]) != 0) {
348 if (debug) {
349 if (!is_ufs2)
350 printf("bad indirect addr: %d\n",
351 iswap32(dp->dp1.di_ib[j]));
352 else
353 printf("bad indirect addr: %lld\n",
354 (long long)iswap64(dp->dp2.di_ib[j]));
355 }
356 goto unknown;
357 }
358 if (ftypeok(dp) == 0)
359 goto unknown;
360 n_files++;
361 info->ino_linkcnt = iswap16(DIP(dp, nlink));
362 if (info->ino_linkcnt <= 0) {
363 zlnp = (struct zlncnt *)malloc(sizeof *zlnp);
364 if (zlnp == NULL) {
365 markclean = 0;
366 pfatal("LINK COUNT TABLE OVERFLOW");
367 if (reply("CONTINUE") == 0) {
368 ckfini();
369 exit(EEXIT);
370 }
371 } else {
372 zlnp->zlncnt = inumber;
373 zlnp->next = zlnhead;
374 zlnhead = zlnp;
375 }
376 }
377 if (mode == IFDIR) {
378 if (size == 0)
379 info->ino_state = DCLEAR;
380 else
381 info->ino_state = DSTATE;
382 cacheino(dp, inumber);
383 countdirs++;
384 } else
385 info->ino_state = FSTATE;
386 info->ino_type = IFTODT(mode);
387 if (!is_ufs2 && doinglevel2 &&
388 (iswap16(dp->dp1.di_ouid) != (u_short)-1 ||
389 iswap16(dp->dp1.di_ogid) != (u_short)-1)) {
390 dp = ginode(inumber);
391 dp->dp1.di_uid = iswap32(iswap16(dp->dp1.di_ouid));
392 dp->dp1.di_ouid = iswap16(-1);
393 dp->dp1.di_gid = iswap32(iswap16(dp->dp1.di_ogid));
394 dp->dp1.di_ogid = iswap16(-1);
395 inodirty();
396 }
397 badblk = dupblk = 0;
398 idesc->id_number = inumber;
399 if (iswap32(DIP(dp, flags)) & SF_SNAPSHOT)
400 idesc->id_type = SNAP;
401 else
402 idesc->id_type = ADDR;
403 (void)ckinode(dp, idesc);
404 #ifdef notyet
405 if (is_ufs2 && iswap32(dp->dp2.di_extsize) > 0) {
406 int ret, offset;
407 idesc->id_type = ADDR;
408 ndb = howmany(iswap32(dp->dp2.di_extsize), sblock->fs_bsize);
409 for (j = 0; j < NXADDR; j++) {
410 if (--ndb == 0 &&
411 (offset = blkoff(sblock, iswap32(dp->dp2.di_extsize))) != 0)
412 idesc->id_numfrags = numfrags(sblock,
413 fragroundup(sblock, offset));
414 else
415 idesc->id_numfrags = sblock->fs_frag;
416 if (dp->dp2.di_extb[j] == 0)
417 continue;
418 idesc->id_blkno = iswap64(dp->dp2.di_extb[j]);
419 ret = (*idesc->id_func)(idesc);
420 if (ret & STOP)
421 break;
422 }
423 }
424 #endif
425 idesc->id_entryno *= btodb(sblock->fs_fsize);
426 if (is_ufs2)
427 blocks = iswap64(dp->dp2.di_blocks);
428 else
429 blocks = iswap32(dp->dp1.di_blocks);
430 if (blocks != idesc->id_entryno) {
431 pwarn("INCORRECT BLOCK COUNT I=%llu (%lld should be %lld)",
432 (unsigned long long)inumber, (long long)blocks,
433 (long long)idesc->id_entryno);
434 if (preen)
435 printf(" (CORRECTED)\n");
436 else if (reply("CORRECT") == 0) {
437 markclean = 0;
438 return;
439 }
440 dp = ginode(inumber);
441 if (is_ufs2)
442 dp->dp2.di_blocks = iswap64(idesc->id_entryno);
443 else
444 dp->dp1.di_blocks = iswap32((int32_t)idesc->id_entryno);
445 inodirty();
446 }
447 return;
448 unknown:
449 pfatal("UNKNOWN FILE TYPE I=%llu", (unsigned long long)inumber);
450 info->ino_state = FCLEAR;
451 if (reply("CLEAR") == 1) {
452 info->ino_state = USTATE;
453 dp = ginode(inumber);
454 clearinode(dp);
455 inodirty();
456 } else
457 markclean = 0;
458 }
459
460 int
461 pass1check(struct inodesc *idesc)
462 {
463 int res = KEEPON;
464 int anyout, nfrags;
465 daddr_t blkno = idesc->id_blkno;
466 struct dups *dlp;
467 struct dups *new;
468
469 if (idesc->id_type == SNAP) {
470 if (blkno == BLK_NOCOPY || blkno == BLK_SNAP)
471 return (KEEPON);
472 }
473 if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
474 blkerror(idesc->id_number, "BAD", blkno);
475 if (badblk++ >= MAXBAD) {
476 pwarn("EXCESSIVE BAD BLKS I=%llu",
477 (unsigned long long)idesc->id_number);
478 if (preen)
479 printf(" (SKIPPING)\n");
480 else if (reply("CONTINUE") == 0) {
481 markclean = 0;
482 ckfini();
483 exit(EEXIT);
484 }
485 return (STOP);
486 }
487 }
488 for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
489 if (anyout && chkrange(blkno, 1)) {
490 res = SKIP;
491 } else if (!testbmap(blkno)) {
492 n_blks++;
493 setbmap(blkno);
494 } else {
495 blkerror(idesc->id_number, "DUP", blkno);
496 if (dupblk++ >= MAXDUP) {
497 pwarn("EXCESSIVE DUP BLKS I=%llu",
498 (unsigned long long)idesc->id_number);
499 if (preen)
500 printf(" (SKIPPING)\n");
501 else if (reply("CONTINUE") == 0) {
502 markclean = 0;
503 ckfini();
504 exit(EEXIT);
505 }
506 return (STOP);
507 }
508 new = (struct dups *)malloc(sizeof(struct dups));
509 if (new == NULL) {
510 markclean = 0;
511 pfatal("DUP TABLE OVERFLOW.");
512 if (reply("CONTINUE") == 0) {
513 markclean = 0;
514 ckfini();
515 exit(EEXIT);
516 }
517 return (STOP);
518 }
519 new->dup = blkno;
520 if (muldup == 0) {
521 duplist = muldup = new;
522 new->next = 0;
523 } else {
524 new->next = muldup->next;
525 muldup->next = new;
526 }
527 for (dlp = duplist; dlp != muldup; dlp = dlp->next)
528 if (dlp->dup == blkno)
529 break;
530 if (dlp == muldup && dlp->dup != blkno)
531 muldup = new;
532 }
533 /*
534 * count the number of blocks found in id_entryno
535 */
536 idesc->id_entryno++;
537 }
538 return (res);
539 }
540