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