utilities.c revision 1.25.4.1 1 /* $NetBSD: utilities.c,v 1.25.4.1 1999/10/19 13:01:30 fvdl 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[] = "@(#)utilities.c 8.6 (Berkeley) 5/19/95";
40 #else
41 __RCSID("$NetBSD: utilities.c,v 1.25.4.1 1999/10/19 13:01:30 fvdl 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 #include <ufs/ffs/ffs_extern.h>
52 #include <ufs/ufs/ufs_bswap.h>
53
54 #include <ctype.h>
55 #include <err.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <unistd.h>
60
61 #include "fsutil.h"
62 #include "fsck.h"
63 #include "extern.h"
64
65 long diskreads, totalreads; /* Disk cache statistics */
66
67 static void rwerror __P((char *, ufs_daddr_t));
68
69 int
70 ftypeok(dp)
71 struct dinode *dp;
72 {
73 switch (iswap16(dp->di_mode) & IFMT) {
74
75 case IFDIR:
76 case IFREG:
77 case IFBLK:
78 case IFCHR:
79 case IFLNK:
80 case IFSOCK:
81 case IFIFO:
82 return (1);
83
84 default:
85 if (debug)
86 printf("bad file type 0%o\n", iswap16(dp->di_mode));
87 return (0);
88 }
89 }
90
91 int
92 reply(question)
93 char *question;
94 {
95 int persevere;
96 char c;
97
98 if (preen)
99 pfatal("INTERNAL ERROR: GOT TO reply()");
100 persevere = !strcmp(question, "CONTINUE");
101 printf("\n");
102 if (!persevere && (nflag || fswritefd < 0)) {
103 printf("%s? no\n\n", question);
104 resolved = 0;
105 return (0);
106 }
107 if (yflag || (persevere && nflag)) {
108 printf("%s? yes\n\n", question);
109 return (1);
110 }
111 do {
112 printf("%s? [yn] ", question);
113 (void) fflush(stdout);
114 c = getc(stdin);
115 while (c != '\n' && getc(stdin) != '\n') {
116 if (feof(stdin)) {
117 resolved = 0;
118 return (0);
119 }
120 }
121 } while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
122 printf("\n");
123 if (c == 'y' || c == 'Y')
124 return (1);
125 resolved = 0;
126 return (0);
127 }
128
129 /*
130 * Malloc buffers and set up cache.
131 */
132 void
133 bufinit()
134 {
135 struct bufarea *bp;
136 long bufcnt, i;
137 char *bufp;
138
139 pbp = pdirbp = (struct bufarea *)0;
140 bufp = malloc((unsigned int)sblock->fs_bsize);
141 if (bufp == 0)
142 errx(EEXIT, "cannot allocate buffer pool");
143 cgblk.b_un.b_buf = bufp;
144 initbarea(&cgblk);
145 bufhead.b_next = bufhead.b_prev = &bufhead;
146 bufcnt = MAXBUFSPACE / sblock->fs_bsize;
147 if (bufcnt < MINBUFS)
148 bufcnt = MINBUFS;
149 for (i = 0; i < bufcnt; i++) {
150 bp = (struct bufarea *)malloc(sizeof(struct bufarea));
151 bufp = malloc((unsigned int)sblock->fs_bsize);
152 if (bp == NULL || bufp == NULL) {
153 if (i >= MINBUFS)
154 break;
155 errx(EEXIT, "cannot allocate buffer pool");
156 }
157 bp->b_un.b_buf = bufp;
158 bp->b_prev = &bufhead;
159 bp->b_next = bufhead.b_next;
160 bufhead.b_next->b_prev = bp;
161 bufhead.b_next = bp;
162 initbarea(bp);
163 }
164 bufhead.b_size = i; /* save number of buffers */
165 }
166
167 /*
168 * Manage a cache of directory blocks.
169 */
170 struct bufarea *
171 getdatablk(blkno, size)
172 ufs_daddr_t blkno;
173 long size;
174 {
175 struct bufarea *bp;
176
177 for (bp = bufhead.b_next; bp != &bufhead; bp = bp->b_next)
178 if (bp->b_bno == fsbtodb(sblock, blkno))
179 goto foundit;
180 for (bp = bufhead.b_prev; bp != &bufhead; bp = bp->b_prev)
181 if ((bp->b_flags & B_INUSE) == 0)
182 break;
183 if (bp == &bufhead)
184 errx(EEXIT, "deadlocked buffer pool");
185 getblk(bp, blkno, size);
186 /* fall through */
187 foundit:
188 totalreads++;
189 bp->b_prev->b_next = bp->b_next;
190 bp->b_next->b_prev = bp->b_prev;
191 bp->b_prev = &bufhead;
192 bp->b_next = bufhead.b_next;
193 bufhead.b_next->b_prev = bp;
194 bufhead.b_next = bp;
195 bp->b_flags |= B_INUSE;
196 return (bp);
197 }
198
199 void
200 getblk(bp, blk, size)
201 struct bufarea *bp;
202 ufs_daddr_t blk;
203 long size;
204 {
205 ufs_daddr_t dblk;
206
207 dblk = fsbtodb(sblock, blk);
208 if (bp->b_bno != dblk) {
209 flush(fswritefd, bp);
210 diskreads++;
211 bp->b_errs = bread(fsreadfd, bp->b_un.b_buf, dblk, size);
212 bp->b_bno = dblk;
213 bp->b_size = size;
214 }
215 }
216
217 void
218 flush(fd, bp)
219 int fd;
220 struct bufarea *bp;
221 {
222 int i, j;
223
224 if (!bp->b_dirty)
225 return;
226 if (bp->b_errs != 0)
227 pfatal("WRITING %sZERO'ED BLOCK %d TO DISK\n",
228 (bp->b_errs == bp->b_size / dev_bsize) ? "" : "PARTIALLY ",
229 bp->b_bno);
230 bp->b_dirty = 0;
231 bp->b_errs = 0;
232 bwrite(fd, bp->b_un.b_buf, bp->b_bno, (long)bp->b_size);
233 if (bp != &sblk)
234 return;
235 for (i = 0, j = 0; i < sblock->fs_cssize; i += sblock->fs_bsize, j++) {
236 int size = sblock->fs_cssize - i < sblock->fs_bsize ?
237 sblock->fs_cssize - i : sblock->fs_bsize;
238 /*
239 * The following routines assumes that struct csum is made of
240 * u_int32_t's
241 */
242 if (needswap) {
243 u_int32_t *cd = (u_int32_t *)sblock->fs_csp[j];
244 int k;
245 for (k = 0; k < size / sizeof(u_int32_t); k++)
246 cd[k] = bswap32(cd[k]);
247 }
248 bwrite(fswritefd, (char *)sblock->fs_csp[j],
249 fsbtodb(sblock, sblock->fs_csaddr + j * sblock->fs_frag),
250 sblock->fs_cssize - i < sblock->fs_bsize ?
251 sblock->fs_cssize - i : sblock->fs_bsize);
252 if (needswap) {
253 u_int32_t *cd = (u_int32_t *)sblock->fs_csp[j];
254 int k;
255 for (k = 0; k < size / sizeof(u_int32_t); k++)
256 cd[k] = bswap32(cd[k]);
257 }
258 }
259 }
260
261 static void
262 rwerror(mesg, blk)
263 char *mesg;
264 ufs_daddr_t blk;
265 {
266
267 if (preen == 0)
268 printf("\n");
269 pfatal("CANNOT %s: BLK %d", mesg, blk);
270 if (reply("CONTINUE") == 0)
271 exit(EEXIT);
272 }
273
274 void
275 ckfini()
276 {
277 struct bufarea *bp, *nbp;
278 int ofsmodified, cnt = 0;
279
280 if (fswritefd < 0) {
281 (void)close(fsreadfd);
282 return;
283 }
284 flush(fswritefd, &sblk);
285 if (havesb && sblk.b_bno != SBOFF / dev_bsize &&
286 !preen && reply("UPDATE STANDARD SUPERBLOCK")) {
287 sblk.b_bno = SBOFF / dev_bsize;
288 sbdirty();
289 flush(fswritefd, &sblk);
290 }
291 flush(fswritefd, &cgblk);
292 free(cgblk.b_un.b_buf);
293 for (bp = bufhead.b_prev; bp && bp != &bufhead; bp = nbp) {
294 cnt++;
295 flush(fswritefd, bp);
296 nbp = bp->b_prev;
297 free(bp->b_un.b_buf);
298 free((char *)bp);
299 }
300 if (bufhead.b_size != cnt)
301 errx(EEXIT, "Panic: lost %d buffers", bufhead.b_size - cnt);
302 pbp = pdirbp = (struct bufarea *)0;
303 if (markclean && (sblock->fs_clean & FS_ISCLEAN) == 0) {
304 /*
305 * Mark the file system as clean, and sync the superblock.
306 */
307 if (preen)
308 pwarn("MARKING FILE SYSTEM CLEAN\n");
309 else if (!reply("MARK FILE SYSTEM CLEAN"))
310 markclean = 0;
311 if (markclean) {
312 sblock->fs_clean = FS_ISCLEAN;
313 sbdirty();
314 ofsmodified = fsmodified;
315 flush(fswritefd, &sblk);
316 #if LITE2BORKEN
317 fsmodified = ofsmodified;
318 #endif
319 if (!preen)
320 printf(
321 "\n***** FILE SYSTEM MARKED CLEAN *****\n");
322 }
323 }
324 if (debug)
325 printf("cache missed %ld of %ld (%d%%)\n", diskreads,
326 totalreads, (int)(diskreads * 100 / totalreads));
327 (void)close(fsreadfd);
328 (void)close(fswritefd);
329 }
330
331 int
332 bread(fd, buf, blk, size)
333 int fd;
334 char *buf;
335 ufs_daddr_t blk;
336 long size;
337 {
338 char *cp;
339 int i, errs;
340 off_t offset;
341
342 offset = blk;
343 offset *= dev_bsize;
344 if (lseek(fd, offset, 0) < 0)
345 rwerror("SEEK", blk);
346 else if (read(fd, buf, (int)size) == size)
347 return (0);
348 rwerror("READ", blk);
349 if (lseek(fd, offset, 0) < 0)
350 rwerror("SEEK", blk);
351 errs = 0;
352 memset(buf, 0, (size_t)size);
353 printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:");
354 for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) {
355 if (read(fd, cp, (int)secsize) != secsize) {
356 (void)lseek(fd, offset + i + secsize, 0);
357 if (secsize != dev_bsize && dev_bsize != 1)
358 printf(" %ld (%ld),",
359 (blk * dev_bsize + i) / secsize,
360 blk + i / dev_bsize);
361 else
362 printf(" %ld,", blk + i / dev_bsize);
363 errs++;
364 }
365 }
366 printf("\n");
367 return (errs);
368 }
369
370 void
371 bwrite(fd, buf, blk, size)
372 int fd;
373 char *buf;
374 ufs_daddr_t blk;
375 long size;
376 {
377 int i;
378 char *cp;
379 off_t offset;
380
381 if (fd < 0)
382 return;
383 offset = blk;
384 offset *= dev_bsize;
385 if (lseek(fd, offset, 0) < 0)
386 rwerror("SEEK", blk);
387 else if (write(fd, buf, (int)size) == size) {
388 fsmodified = 1;
389 return;
390 }
391 rwerror("WRITE", blk);
392 if (lseek(fd, offset, 0) < 0)
393 rwerror("SEEK", blk);
394 printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:");
395 for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize)
396 if (write(fd, cp, (int)dev_bsize) != dev_bsize) {
397 (void)lseek(fd, offset + i + dev_bsize, 0);
398 printf(" %ld,", blk + i / dev_bsize);
399 }
400 printf("\n");
401 return;
402 }
403
404 /*
405 * allocate a data block with the specified number of fragments
406 */
407 ufs_daddr_t
408 allocblk(frags)
409 long frags;
410 {
411 int i, j, k, cg, baseblk;
412 struct cg *cgp = cgrp;
413
414 if (frags <= 0 || frags > sblock->fs_frag)
415 return (0);
416 for (i = 0; i < maxfsblock - sblock->fs_frag; i += sblock->fs_frag) {
417 for (j = 0; j <= sblock->fs_frag - frags; j++) {
418 if (testbmap(i + j))
419 continue;
420 for (k = 1; k < frags; k++)
421 if (testbmap(i + j + k))
422 break;
423 if (k < frags) {
424 j += k;
425 continue;
426 }
427 cg = dtog(sblock, i + j);
428 getblk(&cgblk, cgtod(sblock, cg), sblock->fs_cgsize);
429 if (!cg_chkmagic(cgp, 0))
430 pfatal("CG %d: BAD MAGIC NUMBER\n", cg);
431 baseblk = dtogd(sblock, i + j);
432 for (k = 0; k < frags; k++) {
433 setbmap(i + j + k);
434 clrbit(cg_blksfree(cgp, 0), baseblk + k);
435 }
436 n_blks += frags;
437 if (frags == sblock->fs_frag)
438 cgp->cg_cs.cs_nbfree--;
439 else
440 cgp->cg_cs.cs_nffree -= frags;
441 cgdirty();
442 return (i + j);
443 }
444 }
445 return (0);
446 }
447
448 /*
449 * Free a previously allocated block
450 */
451 void
452 freeblk(blkno, frags)
453 ufs_daddr_t blkno;
454 long frags;
455 {
456 struct inodesc idesc;
457
458 idesc.id_blkno = blkno;
459 idesc.id_numfrags = frags;
460 (void)pass4check(&idesc);
461 }
462
463 /*
464 * Find a pathname
465 */
466 void
467 getpathname(namebuf, curdir, ino)
468 char *namebuf;
469 ino_t curdir, ino;
470 {
471 int len;
472 char *cp;
473 struct inodesc idesc;
474 static int busy = 0;
475
476 if (curdir == ino && ino == ROOTINO) {
477 (void)strcpy(namebuf, "/");
478 return;
479 }
480 if (busy ||
481 (statemap[curdir] != DSTATE && statemap[curdir] != DFOUND)) {
482 (void)strcpy(namebuf, "?");
483 return;
484 }
485 busy = 1;
486 memset(&idesc, 0, sizeof(struct inodesc));
487 idesc.id_type = DATA;
488 idesc.id_fix = IGNORE;
489 cp = &namebuf[MAXPATHLEN - 1];
490 *cp = '\0';
491 if (curdir != ino) {
492 idesc.id_parent = curdir;
493 goto namelookup;
494 }
495 while (ino != ROOTINO) {
496 idesc.id_number = ino;
497 idesc.id_func = findino;
498 idesc.id_name = "..";
499 if ((ckinode(ginode(ino), &idesc) & FOUND) == 0)
500 break;
501 namelookup:
502 idesc.id_number = idesc.id_parent;
503 idesc.id_parent = ino;
504 idesc.id_func = findname;
505 idesc.id_name = namebuf;
506 if ((ckinode(ginode(idesc.id_number), &idesc)&FOUND) == 0)
507 break;
508 len = strlen(namebuf);
509 cp -= len;
510 memmove(cp, namebuf, (size_t)len);
511 *--cp = '/';
512 if (cp < &namebuf[MAXNAMLEN])
513 break;
514 ino = idesc.id_number;
515 }
516 busy = 0;
517 if (ino != ROOTINO)
518 *--cp = '?';
519 memmove(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp));
520 }
521
522 void
523 catch(sig)
524 int sig;
525 {
526 if (!doinglevel2) {
527 markclean = 0;
528 ckfini();
529 }
530 exit(12);
531 }
532
533 /*
534 * When preening, allow a single quit to signal
535 * a special exit after filesystem checks complete
536 * so that reboot sequence may be interrupted.
537 */
538 void
539 catchquit(sig)
540 int sig;
541 {
542 extern int returntosingle;
543
544 printf("returning to single-user after filesystem check\n");
545 returntosingle = 1;
546 (void)signal(SIGQUIT, SIG_DFL);
547 }
548
549 /*
550 * Ignore a single quit signal; wait and flush just in case.
551 * Used by child processes in preen.
552 */
553 void
554 voidquit(sig)
555 int sig;
556 {
557
558 sleep(1);
559 (void)signal(SIGQUIT, SIG_IGN);
560 (void)signal(SIGQUIT, SIG_DFL);
561 }
562
563 /*
564 * determine whether an inode should be fixed.
565 */
566 int
567 dofix(idesc, msg)
568 struct inodesc *idesc;
569 char *msg;
570 {
571
572 switch (idesc->id_fix) {
573
574 case DONTKNOW:
575 if (idesc->id_type == DATA)
576 direrror(idesc->id_number, msg);
577 else
578 pwarn(msg);
579 if (preen) {
580 printf(" (SALVAGED)\n");
581 idesc->id_fix = FIX;
582 return (ALTERED);
583 }
584 if (reply("SALVAGE") == 0) {
585 idesc->id_fix = NOFIX;
586 return (0);
587 }
588 idesc->id_fix = FIX;
589 return (ALTERED);
590
591 case FIX:
592 return (ALTERED);
593
594 case NOFIX:
595 case IGNORE:
596 return (0);
597
598 default:
599 errx(EEXIT, "UNKNOWN INODESC FIX MODE %d", idesc->id_fix);
600 }
601 /* NOTREACHED */
602 return (0);
603 }
604
605 void
606 copyback_cg(blk)
607 struct bufarea *blk;
608 {
609
610 memcpy(blk->b_un.b_cg, cgrp, sblock->fs_cgsize);
611 if (needswap)
612 swap_cg(cgrp, blk->b_un.b_cg);
613 }
614
615 void
616 swap_cg(o, n)
617 struct cg *o, *n;
618 {
619 int i;
620 u_int32_t *n32, *o32;
621 u_int16_t *n16, *o16;
622
623 n->cg_firstfield = bswap32(o->cg_firstfield);
624 n->cg_magic = bswap32(o->cg_magic);
625 n->cg_time = bswap32(o->cg_time);
626 n->cg_cgx = bswap32(o->cg_cgx);
627 n->cg_ncyl = bswap16(o->cg_ncyl);
628 n->cg_niblk = bswap16(o->cg_niblk);
629 n->cg_ndblk = bswap32(o->cg_ndblk);
630 n->cg_cs.cs_ndir = bswap32(o->cg_cs.cs_ndir);
631 n->cg_cs.cs_nbfree = bswap32(o->cg_cs.cs_nbfree);
632 n->cg_cs.cs_nifree = bswap32(o->cg_cs.cs_nifree);
633 n->cg_cs.cs_nffree = bswap32(o->cg_cs.cs_nffree);
634 n->cg_rotor = bswap32(o->cg_rotor);
635 n->cg_frotor = bswap32(o->cg_frotor);
636 n->cg_irotor = bswap32(o->cg_irotor);
637 n->cg_btotoff = bswap32(o->cg_btotoff);
638 n->cg_boff = bswap32(o->cg_boff);
639 n->cg_iusedoff = bswap32(o->cg_iusedoff);
640 n->cg_freeoff = bswap32(o->cg_freeoff);
641 n->cg_nextfreeoff = bswap32(o->cg_nextfreeoff);
642 n->cg_clustersumoff = bswap32(o->cg_clustersumoff);
643 n->cg_clusteroff = bswap32(o->cg_clusteroff);
644 n->cg_nclusterblks = bswap32(o->cg_nclusterblks);
645 for (i=0; i < MAXFRAG; i++)
646 n->cg_frsum[i] = bswap32(o->cg_frsum[i]);
647
648 if (sblock->fs_postblformat == FS_42POSTBLFMT) { /* old format */
649 struct ocg *on, *oo;
650 int j;
651 on = (struct ocg *)n;
652 oo = (struct ocg *)o;
653 for(i = 0; i < 8; i++) {
654 on->cg_frsum[i] = bswap32(oo->cg_frsum[i]);
655 }
656 for(i = 0; i < 32; i++) {
657 on->cg_btot[i] = bswap32(oo->cg_btot[i]);
658 for (j = 0; j < 8; j++)
659 on->cg_b[i][j] = bswap16(oo->cg_b[i][j]);
660 }
661 memmove(on->cg_iused, oo->cg_iused, 256);
662 on->cg_magic = bswap32(oo->cg_magic);
663 } else { /* new format */
664 if (n->cg_magic == CG_MAGIC) {
665 n32 = (u_int32_t*)((u_int8_t*)n + n->cg_btotoff);
666 o32 = (u_int32_t*)((u_int8_t*)o + n->cg_btotoff);
667 n16 = (u_int16_t*)((u_int8_t*)n + n->cg_boff);
668 o16 = (u_int16_t*)((u_int8_t*)o + n->cg_boff);
669 } else {
670 n32 = (u_int32_t*)((u_int8_t*)n + o->cg_btotoff);
671 o32 = (u_int32_t*)((u_int8_t*)o + o->cg_btotoff);
672 n16 = (u_int16_t*)((u_int8_t*)n + o->cg_boff);
673 o16 = (u_int16_t*)((u_int8_t*)o + o->cg_boff);
674 }
675 for (i=0; i < sblock->fs_cpg; i++)
676 n32[i] = bswap32(o32[i]);
677
678 for (i=0; i < sblock->fs_cpg * sblock->fs_nrpos; i++)
679 n16[i] = bswap16(o16[i]);
680
681 if (n->cg_magic == CG_MAGIC) {
682 n32 = (u_int32_t*)((u_int8_t*)n + n->cg_clustersumoff);
683 o32 = (u_int32_t*)((u_int8_t*)o + n->cg_clustersumoff);
684 } else {
685 n32 = (u_int32_t*)((u_int8_t*)n + o->cg_clustersumoff);
686 o32 = (u_int32_t*)((u_int8_t*)o + o->cg_clustersumoff);
687 }
688 for (i = 0; i < sblock->fs_contigsumsize + 1; i++)
689 n32[i] = bswap32(o32[i]);
690 }
691 }
692