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