1 /* 2 * Copyright (c) 1980, 1986, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #if 0 31 #ifndef lint 32 static const char sccsid[] = "@(#)utilities.c 8.6 (Berkeley) 5/19/95"; 33 #endif /* not lint */ 34 #endif 35 #include <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 #include <sys/param.h> 39 #include <sys/time.h> 40 #include <sys/types.h> 41 #include <sys/sysctl.h> 42 #include <sys/disklabel.h> 43 #include <sys/stat.h> 44 #include <sys/disklabel.h> 45 46 #include <ufs/ufs/dinode.h> 47 #include <ufs/ufs/dir.h> 48 #include <ufs/ffs/fs.h> 49 50 #include <err.h> 51 #include <errno.h> 52 #include <string.h> 53 #include <ctype.h> 54 #include <fstab.h> 55 #include <stdint.h> 56 #include <stdio.h> 57 #include <stdlib.h> 58 #include <unistd.h> 59 60 #include "fsck.h" 61 62 static void slowio_start(void); 63 static void slowio_end(void); 64 65 long diskreads, totalreads; /* Disk cache statistics */ 66 struct timeval slowio_starttime; 67 int slowio_delay_usec = 10000; /* Initial IO delay for background fsck */ 68 int slowio_pollcnt; 69 70 int 71 ftypeok(union dinode *dp) 72 { 73 switch (DIP(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", DIP(dp, di_mode)); 87 return (0); 88 } 89 } 90 91 int 92 reply(const 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 && bkgrdflag == 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 * Look up state information for an inode. 130 */ 131 struct inostat * 132 inoinfo(ino_t inum) 133 { 134 static struct inostat unallocated = { USTATE, 0, 0 }; 135 struct inostatlist *ilp; 136 int iloff; 137 138 if (inum > maxino) 139 errx(EEXIT, "inoinfo: inumber %d out of range", inum); 140 ilp = &inostathead[inum / sblock.fs_ipg]; 141 iloff = inum % sblock.fs_ipg; 142 if (iloff >= ilp->il_numalloced) 143 return (&unallocated); 144 return (&ilp->il_stat[iloff]); 145 } 146 147 /* 148 * Malloc buffers and set up cache. 149 */ 150 void 151 bufinit(void) 152 { 153 struct bufarea *bp; 154 long bufcnt, i; 155 char *bufp; 156 157 pbp = pdirbp = (struct bufarea *)0; 158 bufp = malloc((unsigned int)sblock.fs_bsize); 159 if (bufp == 0) 160 errx(EEXIT, "cannot allocate buffer pool"); 161 cgblk.b_un.b_buf = bufp; 162 initbarea(&cgblk); 163 bufhead.b_next = bufhead.b_prev = &bufhead; 164 bufcnt = MAXBUFSPACE / sblock.fs_bsize; 165 if (bufcnt < MINBUFS) 166 bufcnt = MINBUFS; 167 for (i = 0; i < bufcnt; i++) { 168 bp = (struct bufarea *)malloc(sizeof(struct bufarea)); 169 bufp = malloc((unsigned int)sblock.fs_bsize); 170 if (bp == NULL || bufp == NULL) { 171 if (i >= MINBUFS) 172 break; 173 errx(EEXIT, "cannot allocate buffer pool"); 174 } 175 bp->b_un.b_buf = bufp; 176 bp->b_prev = &bufhead; 177 bp->b_next = bufhead.b_next; 178 bufhead.b_next->b_prev = bp; 179 bufhead.b_next = bp; 180 initbarea(bp); 181 } 182 bufhead.b_size = i; /* save number of buffers */ 183 } 184 185 /* 186 * Manage a cache of directory blocks. 187 */ 188 struct bufarea * 189 getdatablk(ufs2_daddr_t blkno, long size) 190 { 191 struct bufarea *bp; 192 193 for (bp = bufhead.b_next; bp != &bufhead; bp = bp->b_next) 194 if (bp->b_bno == fsbtodb(&sblock, blkno)) 195 goto foundit; 196 for (bp = bufhead.b_prev; bp != &bufhead; bp = bp->b_prev) 197 if ((bp->b_flags & B_INUSE) == 0) 198 break; 199 if (bp == &bufhead) 200 errx(EEXIT, "deadlocked buffer pool"); 201 getblk(bp, blkno, size); 202 /* fall through */ 203 foundit: 204 bp->b_prev->b_next = bp->b_next; 205 bp->b_next->b_prev = bp->b_prev; 206 bp->b_prev = &bufhead; 207 bp->b_next = bufhead.b_next; 208 bufhead.b_next->b_prev = bp; 209 bufhead.b_next = bp; 210 bp->b_flags |= B_INUSE; 211 return (bp); 212 } 213 214 void 215 getblk(struct bufarea *bp, ufs2_daddr_t blk, long size) 216 { 217 ufs2_daddr_t dblk; 218 219 totalreads++; 220 dblk = fsbtodb(&sblock, blk); 221 if (bp->b_bno != dblk) { 222 flush(fswritefd, bp); 223 diskreads++; 224 bp->b_errs = blread(fsreadfd, bp->b_un.b_buf, dblk, size); 225 bp->b_bno = dblk; 226 bp->b_size = size; 227 } 228 } 229 230 void 231 flush(int fd, struct bufarea *bp) 232 { 233 int i, j; 234 235 if (!bp->b_dirty) 236 return; 237 bp->b_dirty = 0; 238 if (fswritefd < 0) { 239 pfatal("WRITING IN READ_ONLY MODE.\n"); 240 return; 241 } 242 if (bp->b_errs != 0) 243 pfatal("WRITING %sZERO'ED BLOCK %lld TO DISK\n", 244 (bp->b_errs == bp->b_size / dev_bsize) ? "" : "PARTIALLY ", 245 (long long)bp->b_bno); 246 bp->b_errs = 0; 247 blwrite(fd, bp->b_un.b_buf, bp->b_bno, (long)bp->b_size); 248 if (bp != &sblk) 249 return; 250 for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) { 251 blwrite(fswritefd, (char *)sblock.fs_csp + i, 252 fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag), 253 sblock.fs_cssize - i < sblock.fs_bsize ? 254 sblock.fs_cssize - i : sblock.fs_bsize); 255 } 256 } 257 258 void 259 rwerror(const char *mesg, ufs2_daddr_t blk) 260 { 261 262 if (bkgrdcheck) 263 exit(EEXIT); 264 if (preen == 0) 265 printf("\n"); 266 pfatal("CANNOT %s: %ld", mesg, (long)blk); 267 if (reply("CONTINUE") == 0) 268 exit(EEXIT); 269 } 270 271 void 272 ckfini(int markclean) 273 { 274 struct bufarea *bp, *nbp; 275 int ofsmodified, cnt = 0; 276 277 if (bkgrdflag) { 278 unlink(snapname); 279 if ((!(sblock.fs_flags & FS_UNCLEAN)) != markclean) { 280 cmd.value = FS_UNCLEAN; 281 cmd.size = markclean ? -1 : 1; 282 if (sysctlbyname("vfs.ffs.setflags", 0, 0, 283 &cmd, sizeof cmd) == -1) 284 rwerror("SET FILE SYSTEM FLAGS", FS_UNCLEAN); 285 if (!preen) { 286 printf("\n***** FILE SYSTEM MARKED %s *****\n", 287 markclean ? "CLEAN" : "DIRTY"); 288 if (!markclean) 289 rerun = 1; 290 } 291 } else if (!preen && !markclean) { 292 printf("\n***** FILE SYSTEM STILL DIRTY *****\n"); 293 rerun = 1; 294 } 295 } 296 if (fswritefd < 0) { 297 (void)close(fsreadfd); 298 return; 299 } 300 flush(fswritefd, &sblk); 301 if (havesb && cursnapshot == 0 && sblock.fs_magic == FS_UFS2_MAGIC && 302 sblk.b_bno != sblock.fs_sblockloc / dev_bsize && 303 !preen && reply("UPDATE STANDARD SUPERBLOCK")) { 304 sblk.b_bno = sblock.fs_sblockloc / dev_bsize; 305 sbdirty(); 306 flush(fswritefd, &sblk); 307 } 308 flush(fswritefd, &cgblk); 309 free(cgblk.b_un.b_buf); 310 for (bp = bufhead.b_prev; bp && bp != &bufhead; bp = nbp) { 311 cnt++; 312 flush(fswritefd, bp); 313 nbp = bp->b_prev; 314 free(bp->b_un.b_buf); 315 free((char *)bp); 316 } 317 if (bufhead.b_size != cnt) 318 errx(EEXIT, "panic: lost %d buffers", bufhead.b_size - cnt); 319 pbp = pdirbp = (struct bufarea *)0; 320 if (cursnapshot == 0 && sblock.fs_clean != markclean) { 321 if ((sblock.fs_clean = markclean) != 0) { 322 sblock.fs_flags &= ~(FS_UNCLEAN | FS_NEEDSFSCK); 323 sblock.fs_pendingblocks = 0; 324 sblock.fs_pendinginodes = 0; 325 } 326 sbdirty(); 327 ofsmodified = fsmodified; 328 flush(fswritefd, &sblk); 329 fsmodified = ofsmodified; 330 if (!preen) { 331 printf("\n***** FILE SYSTEM MARKED %s *****\n", 332 markclean ? "CLEAN" : "DIRTY"); 333 if (!markclean) 334 rerun = 1; 335 } 336 } else if (!preen) { 337 if (markclean) { 338 printf("\n***** FILE SYSTEM IS CLEAN *****\n"); 339 } else { 340 printf("\n***** FILE SYSTEM STILL DIRTY *****\n"); 341 rerun = 1; 342 } 343 } 344 if (debug && totalreads > 0) 345 printf("cache missed %ld of %ld (%d%%)\n", diskreads, 346 totalreads, (int)(diskreads * 100 / totalreads)); 347 (void)close(fsreadfd); 348 (void)close(fswritefd); 349 } 350 351 int 352 blread(int fd, char *buf, ufs2_daddr_t blk, long size) 353 { 354 char *cp; 355 int i, errs; 356 off_t offset; 357 358 offset = blk; 359 offset *= dev_bsize; 360 if (bkgrdflag) 361 slowio_start(); 362 if (lseek(fd, offset, 0) < 0) 363 rwerror("SEEK BLK", blk); 364 else if (read(fd, buf, (int)size) == size) { 365 if (bkgrdflag) 366 slowio_end(); 367 return (0); 368 } 369 rwerror("READ BLK", blk); 370 if (lseek(fd, offset, 0) < 0) 371 rwerror("SEEK BLK", blk); 372 errs = 0; 373 memset(buf, 0, (size_t)size); 374 printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:"); 375 for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) { 376 if (read(fd, cp, (int)secsize) != secsize) { 377 (void)lseek(fd, offset + i + secsize, 0); 378 if (secsize != dev_bsize && dev_bsize != 1) 379 printf(" %jd (%jd),", 380 (intmax_t)(blk * dev_bsize + i) / secsize, 381 (intmax_t)blk + i / dev_bsize); 382 else 383 printf(" %jd,", (intmax_t)blk + i / dev_bsize); 384 errs++; 385 } 386 } 387 printf("\n"); 388 if (errs) 389 resolved = 0; 390 return (errs); 391 } 392 393 void 394 blwrite(int fd, char *buf, ufs2_daddr_t blk, long size) 395 { 396 int i; 397 char *cp; 398 off_t offset; 399 400 if (fd < 0) 401 return; 402 offset = blk; 403 offset *= dev_bsize; 404 if (lseek(fd, offset, 0) < 0) 405 rwerror("SEEK BLK", blk); 406 else if (write(fd, buf, (int)size) == size) { 407 fsmodified = 1; 408 return; 409 } 410 resolved = 0; 411 rwerror("WRITE BLK", blk); 412 if (lseek(fd, offset, 0) < 0) 413 rwerror("SEEK BLK", blk); 414 printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:"); 415 for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize) 416 if (write(fd, cp, (int)dev_bsize) != dev_bsize) { 417 (void)lseek(fd, offset + i + dev_bsize, 0); 418 printf(" %jd,", (intmax_t)blk + i / dev_bsize); 419 } 420 printf("\n"); 421 return; 422 } 423 424 /* 425 * Verify cylinder group's magic number and other parameters. If the 426 * test fails, offer an option to rebuild the whole cylinder group. 427 */ 428 int 429 check_cgmagic(int cg, struct cg *cgp) 430 { 431 432 /* 433 * Extended cylinder group checks. 434 */ 435 if (cg_chkmagic(cgp) && 436 ((sblock.fs_magic == FS_UFS1_MAGIC && 437 cgp->cg_old_niblk == sblock.fs_ipg && 438 cgp->cg_ndblk <= sblock.fs_fpg && 439 cgp->cg_old_ncyl <= sblock.fs_old_cpg) || 440 (sblock.fs_magic == FS_UFS2_MAGIC && 441 cgp->cg_niblk == sblock.fs_ipg && 442 cgp->cg_ndblk <= sblock.fs_fpg && 443 cgp->cg_initediblk <= sblock.fs_ipg))) { 444 return (1); 445 } 446 pfatal("CYLINDER GROUP %d: BAD MAGIC NUMBER", cg); 447 if (!reply("REBUILD CYLINDER GROUP")) { 448 printf("YOU WILL NEED TO RERUN FSCK.\n"); 449 rerun = 1; 450 return (1); 451 } 452 /* 453 * Zero out the cylinder group and then initialize critical fields. 454 * Bit maps and summaries will be recalculated by later passes. 455 */ 456 memset(cgp, 0, (size_t)sblock.fs_cgsize); 457 cgp->cg_magic = CG_MAGIC; 458 cgp->cg_cgx = cg; 459 cgp->cg_niblk = sblock.fs_ipg; 460 cgp->cg_initediblk = sblock.fs_ipg < 2 * INOPB(&sblock) ? 461 sblock.fs_ipg : 2 * INOPB(&sblock); 462 if (cgbase(&sblock, cg) + sblock.fs_fpg < sblock.fs_size) 463 cgp->cg_ndblk = sblock.fs_fpg; 464 else 465 cgp->cg_ndblk = sblock.fs_size - cgbase(&sblock, cg); 466 cgp->cg_iusedoff = &cgp->cg_space[0] - (u_char *)(&cgp->cg_firstfield); 467 if (sblock.fs_magic == FS_UFS1_MAGIC) { 468 cgp->cg_niblk = 0; 469 cgp->cg_initediblk = 0; 470 cgp->cg_old_ncyl = sblock.fs_old_cpg; 471 cgp->cg_old_niblk = sblock.fs_ipg; 472 cgp->cg_old_btotoff = cgp->cg_iusedoff; 473 cgp->cg_old_boff = cgp->cg_old_btotoff + 474 sblock.fs_old_cpg * sizeof(int32_t); 475 cgp->cg_iusedoff = cgp->cg_old_boff + 476 sblock.fs_old_cpg * sizeof(u_int16_t); 477 } 478 cgp->cg_freeoff = cgp->cg_iusedoff + howmany(sblock.fs_ipg, CHAR_BIT); 479 cgp->cg_nextfreeoff = cgp->cg_freeoff + howmany(sblock.fs_fpg,CHAR_BIT); 480 if (sblock.fs_contigsumsize > 0) { 481 cgp->cg_nclusterblks = cgp->cg_ndblk / sblock.fs_frag; 482 cgp->cg_clustersumoff = 483 roundup(cgp->cg_nextfreeoff, sizeof(u_int32_t)); 484 cgp->cg_clustersumoff -= sizeof(u_int32_t); 485 cgp->cg_clusteroff = cgp->cg_clustersumoff + 486 (sblock.fs_contigsumsize + 1) * sizeof(u_int32_t); 487 cgp->cg_nextfreeoff = cgp->cg_clusteroff + 488 howmany(fragstoblks(&sblock, sblock.fs_fpg), CHAR_BIT); 489 } 490 cgdirty(); 491 return (0); 492 } 493 494 /* 495 * allocate a data block with the specified number of fragments 496 */ 497 ufs2_daddr_t 498 allocblk(long frags) 499 { 500 int i, j, k, cg, baseblk; 501 struct cg *cgp = &cgrp; 502 503 if (frags <= 0 || frags > sblock.fs_frag) 504 return (0); 505 for (i = 0; i < maxfsblock - sblock.fs_frag; i += sblock.fs_frag) { 506 for (j = 0; j <= sblock.fs_frag - frags; j++) { 507 if (testbmap(i + j)) 508 continue; 509 for (k = 1; k < frags; k++) 510 if (testbmap(i + j + k)) 511 break; 512 if (k < frags) { 513 j += k; 514 continue; 515 } 516 cg = dtog(&sblock, i + j); 517 getblk(&cgblk, cgtod(&sblock, cg), sblock.fs_cgsize); 518 if (!check_cgmagic(cg, cgp)) 519 return (0); 520 baseblk = dtogd(&sblock, i + j); 521 for (k = 0; k < frags; k++) { 522 setbmap(i + j + k); 523 clrbit(cg_blksfree(cgp), baseblk + k); 524 } 525 n_blks += frags; 526 if (frags == sblock.fs_frag) 527 cgp->cg_cs.cs_nbfree--; 528 else 529 cgp->cg_cs.cs_nffree -= frags; 530 cgdirty(); 531 return (i + j); 532 } 533 } 534 return (0); 535 } 536 537 /* 538 * Free a previously allocated block 539 */ 540 void 541 freeblk(ufs2_daddr_t blkno, long frags) 542 { 543 struct inodesc idesc; 544 545 idesc.id_blkno = blkno; 546 idesc.id_numfrags = frags; 547 (void)pass4check(&idesc); 548 } 549 550 /* Slow down IO so as to leave some disk bandwidth for other processes */ 551 void 552 slowio_start() 553 { 554 555 /* Delay one in every 8 operations */ 556 slowio_pollcnt = (slowio_pollcnt + 1) & 7; 557 if (slowio_pollcnt == 0) { 558 gettimeofday(&slowio_starttime, NULL); 559 } 560 } 561 562 void 563 slowio_end() 564 { 565 struct timeval tv; 566 int delay_usec; 567 568 if (slowio_pollcnt != 0) 569 return; 570 571 /* Update the slowdown interval. */ 572 gettimeofday(&tv, NULL); 573 delay_usec = (tv.tv_sec - slowio_starttime.tv_sec) * 1000000 + 574 (tv.tv_usec - slowio_starttime.tv_usec); 575 if (delay_usec < 64) 576 delay_usec = 64; 577 if (delay_usec > 2500000) 578 delay_usec = 2500000; 579 slowio_delay_usec = (slowio_delay_usec * 63 + delay_usec) >> 6; 580 /* delay by 8 times the average IO delay */ 581 if (slowio_delay_usec > 64) 582 usleep(slowio_delay_usec * 8); 583 } 584 585 /* 586 * Find a pathname 587 */ 588 void 589 getpathname(char *namebuf, ino_t curdir, ino_t ino) 590 { 591 int len; 592 char *cp; 593 struct inodesc idesc; 594 static int busy = 0; 595 596 if (curdir == ino && ino == ROOTINO) { 597 (void)strcpy(namebuf, "/"); 598 return; 599 } 600 if (busy || !INO_IS_DVALID(curdir)) { 601 (void)strcpy(namebuf, "?"); 602 return; 603 } 604 busy = 1; 605 memset(&idesc, 0, sizeof(struct inodesc)); 606 idesc.id_type = DATA; 607 idesc.id_fix = IGNORE; 608 cp = &namebuf[MAXPATHLEN - 1]; 609 *cp = '\0'; 610 if (curdir != ino) { 611 idesc.id_parent = curdir; 612 goto namelookup; 613 } 614 while (ino != ROOTINO) { 615 idesc.id_number = ino; 616 idesc.id_func = findino; 617 idesc.id_name = strdup(".."); 618 if ((ckinode(ginode(ino), &idesc) & FOUND) == 0) 619 break; 620 namelookup: 621 idesc.id_number = idesc.id_parent; 622 idesc.id_parent = ino; 623 idesc.id_func = findname; 624 idesc.id_name = namebuf; 625 if ((ckinode(ginode(idesc.id_number), &idesc)&FOUND) == 0) 626 break; 627 len = strlen(namebuf); 628 cp -= len; 629 memmove(cp, namebuf, (size_t)len); 630 *--cp = '/'; 631 if (cp < &namebuf[MAXNAMLEN]) 632 break; 633 ino = idesc.id_number; 634 } 635 busy = 0; 636 if (ino != ROOTINO) 637 *--cp = '?'; 638 memmove(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp)); 639 } 640 641 void 642 catch(int sig __unused) 643 { 644 645 ckfini(0); 646 exit(12); 647 } 648 649 /* 650 * When preening, allow a single quit to signal 651 * a special exit after file system checks complete 652 * so that reboot sequence may be interrupted. 653 */ 654 void 655 catchquit(int sig __unused) 656 { 657 printf("returning to single-user after file system check\n"); 658 returntosingle = 1; 659 (void)signal(SIGQUIT, SIG_DFL); 660 } 661 662 /* 663 * determine whether an inode should be fixed. 664 */ 665 int 666 dofix(struct inodesc *idesc, const char *msg) 667 { 668 669 switch (idesc->id_fix) { 670 671 case DONTKNOW: 672 if (idesc->id_type == DATA) 673 direrror(idesc->id_number, msg); 674 else 675 pwarn("%s", msg); 676 if (preen) { 677 printf(" (SALVAGED)\n"); 678 idesc->id_fix = FIX; 679 return (ALTERED); 680 } 681 if (reply("SALVAGE") == 0) { 682 idesc->id_fix = NOFIX; 683 return (0); 684 } 685 idesc->id_fix = FIX; 686 return (ALTERED); 687 688 case FIX: 689 return (ALTERED); 690 691 case NOFIX: 692 case IGNORE: 693 return (0); 694 695 default: 696 errx(EEXIT, "UNKNOWN INODESC FIX MODE %d", idesc->id_fix); 697 } 698 /* NOTREACHED */ 699 return (0); 700 } 701 702 #include <stdarg.h> 703 704 /* 705 * An unexpected inconsistency occured. 706 * Die if preening or file system is running with soft dependency protocol, 707 * otherwise just print message and continue. 708 */ 709 void 710 pfatal(const char *fmt, ...) 711 { 712 va_list ap; 713 va_start(ap, fmt); 714 if (!preen) { 715 (void)vfprintf(stdout, fmt, ap); 716 va_end(ap); 717 if (usedsoftdep) 718 (void)fprintf(stdout, 719 "\nUNEXPECTED SOFT UPDATE INCONSISTENCY\n"); 720 /* 721 * Force foreground fsck to clean up inconsistency. 722 */ 723 if (bkgrdflag) { 724 cmd.value = FS_NEEDSFSCK; 725 cmd.size = 1; 726 if (sysctlbyname("vfs.ffs.setflags", 0, 0, 727 &cmd, sizeof cmd) == -1) 728 pwarn("CANNOT SET FS_NEEDSFSCK FLAG\n"); 729 fprintf(stdout, "CANNOT RUN IN BACKGROUND\n"); 730 ckfini(0); 731 exit(EEXIT); 732 } 733 return; 734 } 735 if (cdevname == NULL) 736 cdevname = strdup("fsck"); 737 (void)fprintf(stdout, "%s: ", cdevname); 738 (void)vfprintf(stdout, fmt, ap); 739 (void)fprintf(stdout, 740 "\n%s: UNEXPECTED%sINCONSISTENCY; RUN fsck MANUALLY.\n", 741 cdevname, usedsoftdep ? " SOFT UPDATE " : " "); 742 /* 743 * Force foreground fsck to clean up inconsistency. 744 */ 745 if (bkgrdflag) { 746 cmd.value = FS_NEEDSFSCK; 747 cmd.size = 1; 748 if (sysctlbyname("vfs.ffs.setflags", 0, 0, 749 &cmd, sizeof cmd) == -1) 750 pwarn("CANNOT SET FS_NEEDSFSCK FLAG\n"); 751 } 752 ckfini(0); 753 exit(EEXIT); 754 } 755 756 /* 757 * Pwarn just prints a message when not preening or running soft dependency 758 * protocol, or a warning (preceded by filename) when preening. 759 */ 760 void 761 pwarn(const char *fmt, ...) 762 { 763 va_list ap; 764 va_start(ap, fmt); 765 if (preen) 766 (void)fprintf(stdout, "%s: ", cdevname); 767 (void)vfprintf(stdout, fmt, ap); 768 va_end(ap); 769 } 770 771 /* 772 * Stub for routines from kernel. 773 */ 774 void 775 panic(const char *fmt, ...) 776 { 777 va_list ap; 778 va_start(ap, fmt); 779 pfatal("INTERNAL INCONSISTENCY:"); 780 (void)vfprintf(stdout, fmt, ap); 781 va_end(ap); 782 exit(EEXIT); 783 } 784