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