fsutil.c revision 253155
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 32static 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: stable/9/sbin/fsck_ffs/fsutil.c 253155 2013-07-10 14:13:37Z des $"); 37 38#include <sys/param.h> 39#include <sys/time.h> 40#include <sys/types.h> 41#include <sys/sysctl.h> 42#include <sys/disk.h> 43#include <sys/disklabel.h> 44#include <sys/ioctl.h> 45#include <sys/stat.h> 46 47#include <ufs/ufs/dinode.h> 48#include <ufs/ufs/dir.h> 49#include <ufs/ffs/fs.h> 50 51#include <err.h> 52#include <errno.h> 53#include <string.h> 54#include <ctype.h> 55#include <fstab.h> 56#include <stdint.h> 57#include <stdio.h> 58#include <stdlib.h> 59#include <time.h> 60#include <unistd.h> 61 62#include "fsck.h" 63 64static void slowio_start(void); 65static void slowio_end(void); 66static void printIOstats(void); 67 68static long diskreads, totaldiskreads, totalreads; /* Disk cache statistics */ 69static struct timespec startpass, finishpass; 70struct timeval slowio_starttime; 71int slowio_delay_usec = 10000; /* Initial IO delay for background fsck */ 72int slowio_pollcnt; 73static struct bufarea cgblk; /* backup buffer for cylinder group blocks */ 74static TAILQ_HEAD(buflist, bufarea) bufhead; /* head of buffer cache list */ 75static int numbufs; /* size of buffer cache */ 76static char *buftype[BT_NUMBUFTYPES] = BT_NAMES; 77 78int 79ftypeok(union dinode *dp) 80{ 81 switch (DIP(dp, di_mode) & IFMT) { 82 83 case IFDIR: 84 case IFREG: 85 case IFBLK: 86 case IFCHR: 87 case IFLNK: 88 case IFSOCK: 89 case IFIFO: 90 return (1); 91 92 default: 93 if (debug) 94 printf("bad file type 0%o\n", DIP(dp, di_mode)); 95 return (0); 96 } 97} 98 99int 100reply(const char *question) 101{ 102 int persevere; 103 char c; 104 105 if (preen) 106 pfatal("INTERNAL ERROR: GOT TO reply()"); 107 persevere = !strcmp(question, "CONTINUE"); 108 printf("\n"); 109 if (!persevere && (nflag || (fswritefd < 0 && bkgrdflag == 0))) { 110 printf("%s? no\n\n", question); 111 resolved = 0; 112 return (0); 113 } 114 if (yflag || (persevere && nflag)) { 115 printf("%s? yes\n\n", question); 116 return (1); 117 } 118 do { 119 printf("%s? [yn] ", question); 120 (void) fflush(stdout); 121 c = getc(stdin); 122 while (c != '\n' && getc(stdin) != '\n') { 123 if (feof(stdin)) { 124 resolved = 0; 125 return (0); 126 } 127 } 128 } while (c != 'y' && c != 'Y' && c != 'n' && c != 'N'); 129 printf("\n"); 130 if (c == 'y' || c == 'Y') 131 return (1); 132 resolved = 0; 133 return (0); 134} 135 136/* 137 * Look up state information for an inode. 138 */ 139struct inostat * 140inoinfo(ino_t inum) 141{ 142 static struct inostat unallocated = { USTATE, 0, 0 }; 143 struct inostatlist *ilp; 144 int iloff; 145 146 if (inum > maxino) 147 errx(EEXIT, "inoinfo: inumber %d out of range", inum); 148 ilp = &inostathead[inum / sblock.fs_ipg]; 149 iloff = inum % sblock.fs_ipg; 150 if (iloff >= ilp->il_numalloced) 151 return (&unallocated); 152 return (&ilp->il_stat[iloff]); 153} 154 155/* 156 * Malloc buffers and set up cache. 157 */ 158void 159bufinit(void) 160{ 161 struct bufarea *bp; 162 long bufcnt, i; 163 char *bufp; 164 165 pbp = pdirbp = (struct bufarea *)0; 166 bufp = Malloc((unsigned int)sblock.fs_bsize); 167 if (bufp == 0) 168 errx(EEXIT, "cannot allocate buffer pool"); 169 cgblk.b_un.b_buf = bufp; 170 initbarea(&cgblk, BT_CYLGRP); 171 TAILQ_INIT(&bufhead); 172 bufcnt = MAXBUFS; 173 if (bufcnt < MINBUFS) 174 bufcnt = MINBUFS; 175 for (i = 0; i < bufcnt; i++) { 176 bp = (struct bufarea *)Malloc(sizeof(struct bufarea)); 177 bufp = Malloc((unsigned int)sblock.fs_bsize); 178 if (bp == NULL || bufp == NULL) { 179 if (i >= MINBUFS) 180 break; 181 errx(EEXIT, "cannot allocate buffer pool"); 182 } 183 bp->b_un.b_buf = bufp; 184 TAILQ_INSERT_HEAD(&bufhead, bp, b_list); 185 initbarea(bp, BT_UNKNOWN); 186 } 187 numbufs = i; /* save number of buffers */ 188 for (i = 0; i < BT_NUMBUFTYPES; i++) { 189 readtime[i].tv_sec = totalreadtime[i].tv_sec = 0; 190 readtime[i].tv_nsec = totalreadtime[i].tv_nsec = 0; 191 readcnt[i] = totalreadcnt[i] = 0; 192 } 193} 194 195/* 196 * Manage cylinder group buffers. 197 */ 198static struct bufarea *cgbufs; /* header for cylinder group cache */ 199static int flushtries; /* number of tries to reclaim memory */ 200 201struct bufarea * 202cgget(int cg) 203{ 204 struct bufarea *cgbp; 205 struct cg *cgp; 206 207 if (cgbufs == NULL) { 208 cgbufs = Calloc(sblock.fs_ncg, sizeof(struct bufarea)); 209 if (cgbufs == NULL) 210 errx(EEXIT, "cannot allocate cylinder group buffers"); 211 } 212 cgbp = &cgbufs[cg]; 213 if (cgbp->b_un.b_cg != NULL) 214 return (cgbp); 215 cgp = NULL; 216 if (flushtries == 0) 217 cgp = malloc((unsigned int)sblock.fs_cgsize); 218 if (cgp == NULL) { 219 getblk(&cgblk, cgtod(&sblock, cg), sblock.fs_cgsize); 220 return (&cgblk); 221 } 222 cgbp->b_un.b_cg = cgp; 223 initbarea(cgbp, BT_CYLGRP); 224 getblk(cgbp, cgtod(&sblock, cg), sblock.fs_cgsize); 225 return (cgbp); 226} 227 228/* 229 * Attempt to flush a cylinder group cache entry. 230 * Return whether the flush was successful. 231 */ 232int 233flushentry(void) 234{ 235 struct bufarea *cgbp; 236 237 cgbp = &cgbufs[flushtries++]; 238 if (cgbp->b_un.b_cg == NULL) 239 return (0); 240 flush(fswritefd, cgbp); 241 free(cgbp->b_un.b_buf); 242 cgbp->b_un.b_buf = NULL; 243 return (1); 244} 245 246/* 247 * Manage a cache of directory blocks. 248 */ 249struct bufarea * 250getdatablk(ufs2_daddr_t blkno, long size, int type) 251{ 252 struct bufarea *bp; 253 254 TAILQ_FOREACH(bp, &bufhead, b_list) 255 if (bp->b_bno == fsbtodb(&sblock, blkno)) 256 goto foundit; 257 TAILQ_FOREACH_REVERSE(bp, &bufhead, buflist, b_list) 258 if ((bp->b_flags & B_INUSE) == 0) 259 break; 260 if (bp == NULL) 261 errx(EEXIT, "deadlocked buffer pool"); 262 bp->b_type = type; 263 getblk(bp, blkno, size); 264 /* fall through */ 265foundit: 266 if (debug && bp->b_type != type) 267 printf("Buffer type changed from %s to %s\n", 268 buftype[bp->b_type], buftype[type]); 269 TAILQ_REMOVE(&bufhead, bp, b_list); 270 TAILQ_INSERT_HEAD(&bufhead, bp, b_list); 271 bp->b_flags |= B_INUSE; 272 return (bp); 273} 274 275/* 276 * Timespec operations (from <sys/time.h>). 277 */ 278#define timespecsub(vvp, uvp) \ 279 do { \ 280 (vvp)->tv_sec -= (uvp)->tv_sec; \ 281 (vvp)->tv_nsec -= (uvp)->tv_nsec; \ 282 if ((vvp)->tv_nsec < 0) { \ 283 (vvp)->tv_sec--; \ 284 (vvp)->tv_nsec += 1000000000; \ 285 } \ 286 } while (0) 287#define timespecadd(vvp, uvp) \ 288 do { \ 289 (vvp)->tv_sec += (uvp)->tv_sec; \ 290 (vvp)->tv_nsec += (uvp)->tv_nsec; \ 291 if ((vvp)->tv_nsec >= 1000000000) { \ 292 (vvp)->tv_sec++; \ 293 (vvp)->tv_nsec -= 1000000000; \ 294 } \ 295 } while (0) 296 297void 298getblk(struct bufarea *bp, ufs2_daddr_t blk, long size) 299{ 300 ufs2_daddr_t dblk; 301 struct timespec start, finish; 302 303 dblk = fsbtodb(&sblock, blk); 304 if (bp->b_bno == dblk) { 305 totalreads++; 306 } else { 307 flush(fswritefd, bp); 308 if (debug) { 309 readcnt[bp->b_type]++; 310 clock_gettime(CLOCK_REALTIME_PRECISE, &start); 311 } 312 bp->b_errs = blread(fsreadfd, bp->b_un.b_buf, dblk, size); 313 if (debug) { 314 clock_gettime(CLOCK_REALTIME_PRECISE, &finish); 315 timespecsub(&finish, &start); 316 timespecadd(&readtime[bp->b_type], &finish); 317 } 318 bp->b_bno = dblk; 319 bp->b_size = size; 320 } 321} 322 323void 324flush(int fd, struct bufarea *bp) 325{ 326 int i, j; 327 328 if (!bp->b_dirty) 329 return; 330 bp->b_dirty = 0; 331 if (fswritefd < 0) { 332 pfatal("WRITING IN READ_ONLY MODE.\n"); 333 return; 334 } 335 if (bp->b_errs != 0) 336 pfatal("WRITING %sZERO'ED BLOCK %lld TO DISK\n", 337 (bp->b_errs == bp->b_size / dev_bsize) ? "" : "PARTIALLY ", 338 (long long)bp->b_bno); 339 bp->b_errs = 0; 340 blwrite(fd, bp->b_un.b_buf, bp->b_bno, (long)bp->b_size); 341 if (bp != &sblk) 342 return; 343 for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) { 344 blwrite(fswritefd, (char *)sblock.fs_csp + i, 345 fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag), 346 sblock.fs_cssize - i < sblock.fs_bsize ? 347 sblock.fs_cssize - i : sblock.fs_bsize); 348 } 349} 350 351void 352rwerror(const char *mesg, ufs2_daddr_t blk) 353{ 354 355 if (bkgrdcheck) 356 exit(EEXIT); 357 if (preen == 0) 358 printf("\n"); 359 pfatal("CANNOT %s: %ld", mesg, (long)blk); 360 if (reply("CONTINUE") == 0) 361 exit(EEXIT); 362} 363 364void 365ckfini(int markclean) 366{ 367 struct bufarea *bp, *nbp; 368 int ofsmodified, cnt; 369 370 if (bkgrdflag) { 371 unlink(snapname); 372 if ((!(sblock.fs_flags & FS_UNCLEAN)) != markclean) { 373 cmd.value = FS_UNCLEAN; 374 cmd.size = markclean ? -1 : 1; 375 if (sysctlbyname("vfs.ffs.setflags", 0, 0, 376 &cmd, sizeof cmd) == -1) 377 rwerror("SET FILE SYSTEM FLAGS", FS_UNCLEAN); 378 if (!preen) { 379 printf("\n***** FILE SYSTEM MARKED %s *****\n", 380 markclean ? "CLEAN" : "DIRTY"); 381 if (!markclean) 382 rerun = 1; 383 } 384 } else if (!preen && !markclean) { 385 printf("\n***** FILE SYSTEM STILL DIRTY *****\n"); 386 rerun = 1; 387 } 388 } 389 if (debug && totalreads > 0) 390 printf("cache with %d buffers missed %ld of %ld (%d%%)\n", 391 numbufs, totaldiskreads, totalreads, 392 (int)(totaldiskreads * 100 / totalreads)); 393 if (fswritefd < 0) { 394 (void)close(fsreadfd); 395 return; 396 } 397 flush(fswritefd, &sblk); 398 if (havesb && cursnapshot == 0 && sblock.fs_magic == FS_UFS2_MAGIC && 399 sblk.b_bno != sblock.fs_sblockloc / dev_bsize && 400 !preen && reply("UPDATE STANDARD SUPERBLOCK")) { 401 sblk.b_bno = sblock.fs_sblockloc / dev_bsize; 402 sbdirty(); 403 flush(fswritefd, &sblk); 404 } 405 flush(fswritefd, &cgblk); 406 free(cgblk.b_un.b_buf); 407 cnt = 0; 408 TAILQ_FOREACH_REVERSE_SAFE(bp, &bufhead, buflist, b_list, nbp) { 409 TAILQ_REMOVE(&bufhead, bp, b_list); 410 cnt++; 411 flush(fswritefd, bp); 412 free(bp->b_un.b_buf); 413 free((char *)bp); 414 } 415 if (numbufs != cnt) 416 errx(EEXIT, "panic: lost %d buffers", numbufs - cnt); 417 for (cnt = 0; cnt < sblock.fs_ncg; cnt++) { 418 if (cgbufs[cnt].b_un.b_cg == NULL) 419 continue; 420 flush(fswritefd, &cgbufs[cnt]); 421 free(cgbufs[cnt].b_un.b_cg); 422 } 423 free(cgbufs); 424 pbp = pdirbp = (struct bufarea *)0; 425 if (cursnapshot == 0 && sblock.fs_clean != markclean) { 426 if ((sblock.fs_clean = markclean) != 0) { 427 sblock.fs_flags &= ~(FS_UNCLEAN | FS_NEEDSFSCK); 428 sblock.fs_pendingblocks = 0; 429 sblock.fs_pendinginodes = 0; 430 } 431 sbdirty(); 432 ofsmodified = fsmodified; 433 flush(fswritefd, &sblk); 434 fsmodified = ofsmodified; 435 if (!preen) { 436 printf("\n***** FILE SYSTEM MARKED %s *****\n", 437 markclean ? "CLEAN" : "DIRTY"); 438 if (!markclean) 439 rerun = 1; 440 } 441 } else if (!preen) { 442 if (markclean) { 443 printf("\n***** FILE SYSTEM IS CLEAN *****\n"); 444 } else { 445 printf("\n***** FILE SYSTEM STILL DIRTY *****\n"); 446 rerun = 1; 447 } 448 } 449 (void)close(fsreadfd); 450 (void)close(fswritefd); 451} 452 453/* 454 * Print out I/O statistics. 455 */ 456void 457IOstats(char *what) 458{ 459 int i; 460 461 if (debug == 0) 462 return; 463 if (diskreads == 0) { 464 printf("%s: no I/O\n\n", what); 465 return; 466 } 467 if (startpass.tv_sec == 0) 468 startpass = startprog; 469 printf("%s: I/O statistics\n", what); 470 printIOstats(); 471 totaldiskreads += diskreads; 472 diskreads = 0; 473 for (i = 0; i < BT_NUMBUFTYPES; i++) { 474 timespecadd(&totalreadtime[i], &readtime[i]); 475 totalreadcnt[i] += readcnt[i]; 476 readtime[i].tv_sec = readtime[i].tv_nsec = 0; 477 readcnt[i] = 0; 478 } 479 clock_gettime(CLOCK_REALTIME_PRECISE, &startpass); 480} 481 482void 483finalIOstats(void) 484{ 485 int i; 486 487 if (debug == 0) 488 return; 489 printf("Final I/O statistics\n"); 490 totaldiskreads += diskreads; 491 diskreads = totaldiskreads; 492 startpass = startprog; 493 for (i = 0; i < BT_NUMBUFTYPES; i++) { 494 timespecadd(&totalreadtime[i], &readtime[i]); 495 totalreadcnt[i] += readcnt[i]; 496 readtime[i] = totalreadtime[i]; 497 readcnt[i] = totalreadcnt[i]; 498 } 499 printIOstats(); 500} 501 502static void printIOstats(void) 503{ 504 long long msec, totalmsec; 505 int i; 506 507 clock_gettime(CLOCK_REALTIME_PRECISE, &finishpass); 508 timespecsub(&finishpass, &startpass); 509 printf("Running time: %jd.%03ld sec\n", 510 (intmax_t)finishpass.tv_sec, finishpass.tv_nsec / 1000000); 511 printf("buffer reads by type:\n"); 512 for (totalmsec = 0, i = 0; i < BT_NUMBUFTYPES; i++) 513 totalmsec += readtime[i].tv_sec * 1000 + 514 readtime[i].tv_nsec / 1000000; 515 if (totalmsec == 0) 516 totalmsec = 1; 517 for (i = 0; i < BT_NUMBUFTYPES; i++) { 518 if (readcnt[i] == 0) 519 continue; 520 msec = 521 readtime[i].tv_sec * 1000 + readtime[i].tv_nsec / 1000000; 522 printf("%21s:%8ld %2ld.%ld%% %4jd.%03ld sec %2lld.%lld%%\n", 523 buftype[i], readcnt[i], readcnt[i] * 100 / diskreads, 524 (readcnt[i] * 1000 / diskreads) % 10, 525 (intmax_t)readtime[i].tv_sec, readtime[i].tv_nsec / 1000000, 526 msec * 100 / totalmsec, (msec * 1000 / totalmsec) % 10); 527 } 528 printf("\n"); 529} 530 531int 532blread(int fd, char *buf, ufs2_daddr_t blk, long size) 533{ 534 char *cp; 535 int i, errs; 536 off_t offset; 537 538 offset = blk; 539 offset *= dev_bsize; 540 if (bkgrdflag) 541 slowio_start(); 542 totalreads++; 543 diskreads++; 544 if (lseek(fd, offset, 0) < 0) 545 rwerror("SEEK BLK", blk); 546 else if (read(fd, buf, (int)size) == size) { 547 if (bkgrdflag) 548 slowio_end(); 549 return (0); 550 } 551 rwerror("READ BLK", blk); 552 if (lseek(fd, offset, 0) < 0) 553 rwerror("SEEK BLK", blk); 554 errs = 0; 555 memset(buf, 0, (size_t)size); 556 printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:"); 557 for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) { 558 if (read(fd, cp, (int)secsize) != secsize) { 559 (void)lseek(fd, offset + i + secsize, 0); 560 if (secsize != dev_bsize && dev_bsize != 1) 561 printf(" %jd (%jd),", 562 (intmax_t)(blk * dev_bsize + i) / secsize, 563 (intmax_t)blk + i / dev_bsize); 564 else 565 printf(" %jd,", (intmax_t)blk + i / dev_bsize); 566 errs++; 567 } 568 } 569 printf("\n"); 570 if (errs) 571 resolved = 0; 572 return (errs); 573} 574 575void 576blwrite(int fd, char *buf, ufs2_daddr_t blk, long size) 577{ 578 int i; 579 char *cp; 580 off_t offset; 581 582 if (fd < 0) 583 return; 584 offset = blk; 585 offset *= dev_bsize; 586 if (lseek(fd, offset, 0) < 0) 587 rwerror("SEEK BLK", blk); 588 else if (write(fd, buf, (int)size) == size) { 589 fsmodified = 1; 590 return; 591 } 592 resolved = 0; 593 rwerror("WRITE BLK", blk); 594 if (lseek(fd, offset, 0) < 0) 595 rwerror("SEEK BLK", blk); 596 printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:"); 597 for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize) 598 if (write(fd, cp, (int)dev_bsize) != dev_bsize) { 599 (void)lseek(fd, offset + i + dev_bsize, 0); 600 printf(" %jd,", (intmax_t)blk + i / dev_bsize); 601 } 602 printf("\n"); 603 return; 604} 605 606void 607blerase(int fd, ufs2_daddr_t blk, long size) 608{ 609 off_t ioarg[2]; 610 611 if (fd < 0) 612 return; 613 ioarg[0] = blk * dev_bsize; 614 ioarg[1] = size; 615 ioctl(fd, DIOCGDELETE, ioarg); 616 /* we don't really care if we succeed or not */ 617 return; 618} 619 620void 621blzero(int fd, ufs2_daddr_t blk, long size) 622{ 623 static char *zero; 624 off_t offset, len; 625 626 if (fd < 0) 627 return; 628 len = ZEROBUFSIZE; 629 if (zero == NULL) { 630 zero = calloc(len, 1); 631 if (zero == NULL) 632 errx(EEXIT, "cannot allocate buffer pool"); 633 } 634 offset = blk * dev_bsize; 635 if (lseek(fd, offset, 0) < 0) 636 rwerror("SEEK BLK", blk); 637 while (size > 0) { 638 if (size > len) 639 size = len; 640 else 641 len = size; 642 if (write(fd, zero, len) != len) 643 rwerror("WRITE BLK", blk); 644 blk += len / dev_bsize; 645 size -= len; 646 } 647} 648 649/* 650 * Verify cylinder group's magic number and other parameters. If the 651 * test fails, offer an option to rebuild the whole cylinder group. 652 */ 653int 654check_cgmagic(int cg, struct bufarea *cgbp) 655{ 656 struct cg *cgp = cgbp->b_un.b_cg; 657 658 /* 659 * Extended cylinder group checks. 660 */ 661 if (cg_chkmagic(cgp) && 662 ((sblock.fs_magic == FS_UFS1_MAGIC && 663 cgp->cg_old_niblk == sblock.fs_ipg && 664 cgp->cg_ndblk <= sblock.fs_fpg && 665 cgp->cg_old_ncyl <= sblock.fs_old_cpg) || 666 (sblock.fs_magic == FS_UFS2_MAGIC && 667 cgp->cg_niblk == sblock.fs_ipg && 668 cgp->cg_ndblk <= sblock.fs_fpg && 669 cgp->cg_initediblk <= sblock.fs_ipg))) { 670 return (1); 671 } 672 pfatal("CYLINDER GROUP %d: BAD MAGIC NUMBER", cg); 673 if (!reply("REBUILD CYLINDER GROUP")) { 674 printf("YOU WILL NEED TO RERUN FSCK.\n"); 675 rerun = 1; 676 return (1); 677 } 678 /* 679 * Zero out the cylinder group and then initialize critical fields. 680 * Bit maps and summaries will be recalculated by later passes. 681 */ 682 memset(cgp, 0, (size_t)sblock.fs_cgsize); 683 cgp->cg_magic = CG_MAGIC; 684 cgp->cg_cgx = cg; 685 cgp->cg_niblk = sblock.fs_ipg; 686 cgp->cg_initediblk = sblock.fs_ipg < 2 * INOPB(&sblock) ? 687 sblock.fs_ipg : 2 * INOPB(&sblock); 688 if (cgbase(&sblock, cg) + sblock.fs_fpg < sblock.fs_size) 689 cgp->cg_ndblk = sblock.fs_fpg; 690 else 691 cgp->cg_ndblk = sblock.fs_size - cgbase(&sblock, cg); 692 cgp->cg_iusedoff = &cgp->cg_space[0] - (u_char *)(&cgp->cg_firstfield); 693 if (sblock.fs_magic == FS_UFS1_MAGIC) { 694 cgp->cg_niblk = 0; 695 cgp->cg_initediblk = 0; 696 cgp->cg_old_ncyl = sblock.fs_old_cpg; 697 cgp->cg_old_niblk = sblock.fs_ipg; 698 cgp->cg_old_btotoff = cgp->cg_iusedoff; 699 cgp->cg_old_boff = cgp->cg_old_btotoff + 700 sblock.fs_old_cpg * sizeof(int32_t); 701 cgp->cg_iusedoff = cgp->cg_old_boff + 702 sblock.fs_old_cpg * sizeof(u_int16_t); 703 } 704 cgp->cg_freeoff = cgp->cg_iusedoff + howmany(sblock.fs_ipg, CHAR_BIT); 705 cgp->cg_nextfreeoff = cgp->cg_freeoff + howmany(sblock.fs_fpg,CHAR_BIT); 706 if (sblock.fs_contigsumsize > 0) { 707 cgp->cg_nclusterblks = cgp->cg_ndblk / sblock.fs_frag; 708 cgp->cg_clustersumoff = 709 roundup(cgp->cg_nextfreeoff, sizeof(u_int32_t)); 710 cgp->cg_clustersumoff -= sizeof(u_int32_t); 711 cgp->cg_clusteroff = cgp->cg_clustersumoff + 712 (sblock.fs_contigsumsize + 1) * sizeof(u_int32_t); 713 cgp->cg_nextfreeoff = cgp->cg_clusteroff + 714 howmany(fragstoblks(&sblock, sblock.fs_fpg), CHAR_BIT); 715 } 716 dirty(cgbp); 717 return (0); 718} 719 720/* 721 * allocate a data block with the specified number of fragments 722 */ 723ufs2_daddr_t 724allocblk(long frags) 725{ 726 int i, j, k, cg, baseblk; 727 struct bufarea *cgbp; 728 struct cg *cgp; 729 730 if (frags <= 0 || frags > sblock.fs_frag) 731 return (0); 732 for (i = 0; i < maxfsblock - sblock.fs_frag; i += sblock.fs_frag) { 733 for (j = 0; j <= sblock.fs_frag - frags; j++) { 734 if (testbmap(i + j)) 735 continue; 736 for (k = 1; k < frags; k++) 737 if (testbmap(i + j + k)) 738 break; 739 if (k < frags) { 740 j += k; 741 continue; 742 } 743 cg = dtog(&sblock, i + j); 744 cgbp = cgget(cg); 745 cgp = cgbp->b_un.b_cg; 746 if (!check_cgmagic(cg, cgbp)) 747 return (0); 748 baseblk = dtogd(&sblock, i + j); 749 for (k = 0; k < frags; k++) { 750 setbmap(i + j + k); 751 clrbit(cg_blksfree(cgp), baseblk + k); 752 } 753 n_blks += frags; 754 if (frags == sblock.fs_frag) 755 cgp->cg_cs.cs_nbfree--; 756 else 757 cgp->cg_cs.cs_nffree -= frags; 758 dirty(cgbp); 759 return (i + j); 760 } 761 } 762 return (0); 763} 764 765/* 766 * Free a previously allocated block 767 */ 768void 769freeblk(ufs2_daddr_t blkno, long frags) 770{ 771 struct inodesc idesc; 772 773 idesc.id_blkno = blkno; 774 idesc.id_numfrags = frags; 775 (void)pass4check(&idesc); 776} 777 778/* Slow down IO so as to leave some disk bandwidth for other processes */ 779void 780slowio_start() 781{ 782 783 /* Delay one in every 8 operations */ 784 slowio_pollcnt = (slowio_pollcnt + 1) & 7; 785 if (slowio_pollcnt == 0) { 786 gettimeofday(&slowio_starttime, NULL); 787 } 788} 789 790void 791slowio_end() 792{ 793 struct timeval tv; 794 int delay_usec; 795 796 if (slowio_pollcnt != 0) 797 return; 798 799 /* Update the slowdown interval. */ 800 gettimeofday(&tv, NULL); 801 delay_usec = (tv.tv_sec - slowio_starttime.tv_sec) * 1000000 + 802 (tv.tv_usec - slowio_starttime.tv_usec); 803 if (delay_usec < 64) 804 delay_usec = 64; 805 if (delay_usec > 2500000) 806 delay_usec = 2500000; 807 slowio_delay_usec = (slowio_delay_usec * 63 + delay_usec) >> 6; 808 /* delay by 8 times the average IO delay */ 809 if (slowio_delay_usec > 64) 810 usleep(slowio_delay_usec * 8); 811} 812 813/* 814 * Find a pathname 815 */ 816void 817getpathname(char *namebuf, ino_t curdir, ino_t ino) 818{ 819 int len; 820 char *cp; 821 struct inodesc idesc; 822 static int busy = 0; 823 824 if (curdir == ino && ino == ROOTINO) { 825 (void)strcpy(namebuf, "/"); 826 return; 827 } 828 if (busy || !INO_IS_DVALID(curdir)) { 829 (void)strcpy(namebuf, "?"); 830 return; 831 } 832 busy = 1; 833 memset(&idesc, 0, sizeof(struct inodesc)); 834 idesc.id_type = DATA; 835 idesc.id_fix = IGNORE; 836 cp = &namebuf[MAXPATHLEN - 1]; 837 *cp = '\0'; 838 if (curdir != ino) { 839 idesc.id_parent = curdir; 840 goto namelookup; 841 } 842 while (ino != ROOTINO) { 843 idesc.id_number = ino; 844 idesc.id_func = findino; 845 idesc.id_name = strdup(".."); 846 if ((ckinode(ginode(ino), &idesc) & FOUND) == 0) 847 break; 848 namelookup: 849 idesc.id_number = idesc.id_parent; 850 idesc.id_parent = ino; 851 idesc.id_func = findname; 852 idesc.id_name = namebuf; 853 if ((ckinode(ginode(idesc.id_number), &idesc)&FOUND) == 0) 854 break; 855 len = strlen(namebuf); 856 cp -= len; 857 memmove(cp, namebuf, (size_t)len); 858 *--cp = '/'; 859 if (cp < &namebuf[MAXNAMLEN]) 860 break; 861 ino = idesc.id_number; 862 } 863 busy = 0; 864 if (ino != ROOTINO) 865 *--cp = '?'; 866 memmove(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp)); 867} 868 869void 870catch(int sig __unused) 871{ 872 873 ckfini(0); 874 exit(12); 875} 876 877/* 878 * When preening, allow a single quit to signal 879 * a special exit after file system checks complete 880 * so that reboot sequence may be interrupted. 881 */ 882void 883catchquit(int sig __unused) 884{ 885 printf("returning to single-user after file system check\n"); 886 returntosingle = 1; 887 (void)signal(SIGQUIT, SIG_DFL); 888} 889 890/* 891 * determine whether an inode should be fixed. 892 */ 893int 894dofix(struct inodesc *idesc, const char *msg) 895{ 896 897 switch (idesc->id_fix) { 898 899 case DONTKNOW: 900 if (idesc->id_type == DATA) 901 direrror(idesc->id_number, msg); 902 else 903 pwarn("%s", msg); 904 if (preen) { 905 printf(" (SALVAGED)\n"); 906 idesc->id_fix = FIX; 907 return (ALTERED); 908 } 909 if (reply("SALVAGE") == 0) { 910 idesc->id_fix = NOFIX; 911 return (0); 912 } 913 idesc->id_fix = FIX; 914 return (ALTERED); 915 916 case FIX: 917 return (ALTERED); 918 919 case NOFIX: 920 case IGNORE: 921 return (0); 922 923 default: 924 errx(EEXIT, "UNKNOWN INODESC FIX MODE %d", idesc->id_fix); 925 } 926 /* NOTREACHED */ 927 return (0); 928} 929 930#include <stdarg.h> 931 932/* 933 * An unexpected inconsistency occured. 934 * Die if preening or file system is running with soft dependency protocol, 935 * otherwise just print message and continue. 936 */ 937void 938pfatal(const char *fmt, ...) 939{ 940 va_list ap; 941 va_start(ap, fmt); 942 if (!preen) { 943 (void)vfprintf(stdout, fmt, ap); 944 va_end(ap); 945 if (usedsoftdep) 946 (void)fprintf(stdout, 947 "\nUNEXPECTED SOFT UPDATE INCONSISTENCY\n"); 948 /* 949 * Force foreground fsck to clean up inconsistency. 950 */ 951 if (bkgrdflag) { 952 cmd.value = FS_NEEDSFSCK; 953 cmd.size = 1; 954 if (sysctlbyname("vfs.ffs.setflags", 0, 0, 955 &cmd, sizeof cmd) == -1) 956 pwarn("CANNOT SET FS_NEEDSFSCK FLAG\n"); 957 fprintf(stdout, "CANNOT RUN IN BACKGROUND\n"); 958 ckfini(0); 959 exit(EEXIT); 960 } 961 return; 962 } 963 if (cdevname == NULL) 964 cdevname = strdup("fsck"); 965 (void)fprintf(stdout, "%s: ", cdevname); 966 (void)vfprintf(stdout, fmt, ap); 967 (void)fprintf(stdout, 968 "\n%s: UNEXPECTED%sINCONSISTENCY; RUN fsck MANUALLY.\n", 969 cdevname, usedsoftdep ? " SOFT UPDATE " : " "); 970 /* 971 * Force foreground fsck to clean up inconsistency. 972 */ 973 if (bkgrdflag) { 974 cmd.value = FS_NEEDSFSCK; 975 cmd.size = 1; 976 if (sysctlbyname("vfs.ffs.setflags", 0, 0, 977 &cmd, sizeof cmd) == -1) 978 pwarn("CANNOT SET FS_NEEDSFSCK FLAG\n"); 979 } 980 ckfini(0); 981 exit(EEXIT); 982} 983 984/* 985 * Pwarn just prints a message when not preening or running soft dependency 986 * protocol, or a warning (preceded by filename) when preening. 987 */ 988void 989pwarn(const char *fmt, ...) 990{ 991 va_list ap; 992 va_start(ap, fmt); 993 if (preen) 994 (void)fprintf(stdout, "%s: ", cdevname); 995 (void)vfprintf(stdout, fmt, ap); 996 va_end(ap); 997} 998 999/* 1000 * Stub for routines from kernel. 1001 */ 1002void 1003panic(const char *fmt, ...) 1004{ 1005 va_list ap; 1006 va_start(ap, fmt); 1007 pfatal("INTERNAL INCONSISTENCY:"); 1008 (void)vfprintf(stdout, fmt, ap); 1009 va_end(ap); 1010 exit(EEXIT); 1011} 1012