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 copyright[] = 33 "@(#) Copyright (c) 1980, 1986, 1993\n\ 34 The Regents of the University of California. All rights reserved.\n"; 35 #endif /* not lint */ 36 37 #ifndef lint 38 static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/14/95"; 39 #endif /* not lint */ 40 #endif 41 #include <sys/cdefs.h> 42 __FBSDID("$FreeBSD$"); 43 44 #include <sys/param.h> 45 #include <sys/file.h> 46 #include <sys/mount.h> 47 #include <sys/resource.h> 48 #include <sys/stat.h> 49 #include <sys/sysctl.h> 50 #include <sys/uio.h> 51 #include <sys/disklabel.h> 52 53 #include <ufs/ufs/dinode.h> 54 #include <ufs/ffs/fs.h> 55 56 #include <err.h> 57 #include <errno.h> 58 #include <fstab.h> 59 #include <grp.h> 60 #include <mntopts.h> 61 #include <paths.h> 62 #include <stdint.h> 63 #include <string.h> 64 #include <time.h> 65 66 #include "fsck.h" 67 68 static void usage(void) __dead2; 69 static int argtoi(int flag, const char *req, const char *str, int base); 70 static int checkfilesys(char *filesys); 71 static int chkdoreload(struct statfs *mntp); 72 static struct statfs *getmntpt(const char *); 73 74 int 75 main(int argc, char *argv[]) 76 { 77 int ch; 78 struct rlimit rlimit; 79 struct itimerval itimerval; 80 int ret = 0; 81 82 sync(); 83 skipclean = 1; 84 inoopt = 0; 85 while ((ch = getopt(argc, argv, "b:Bc:CdEfFm:nprSyZ")) != -1) { 86 switch (ch) { 87 case 'b': 88 skipclean = 0; 89 bflag = argtoi('b', "number", optarg, 10); 90 printf("Alternate super block location: %d\n", bflag); 91 break; 92 93 case 'B': 94 bkgrdflag = 1; 95 break; 96 97 case 'c': 98 skipclean = 0; 99 cvtlevel = argtoi('c', "conversion level", optarg, 10); 100 if (cvtlevel < 3) 101 errx(EEXIT, "cannot do level %d conversion", 102 cvtlevel); 103 break; 104 105 case 'd': 106 debug++; 107 break; 108 109 case 'E': 110 Eflag++; 111 break; 112 113 case 'f': 114 skipclean = 0; 115 break; 116 117 case 'F': 118 bkgrdcheck = 1; 119 break; 120 121 case 'm': 122 lfmode = argtoi('m', "mode", optarg, 8); 123 if (lfmode &~ 07777) 124 errx(EEXIT, "bad mode to -m: %o", lfmode); 125 printf("** lost+found creation mode %o\n", lfmode); 126 break; 127 128 case 'n': 129 nflag++; 130 yflag = 0; 131 break; 132 133 case 'p': 134 preen++; 135 /*FALLTHROUGH*/ 136 137 case 'C': 138 ckclean++; 139 break; 140 141 case 'r': 142 inoopt++; 143 break; 144 145 case 'S': 146 surrender = 1; 147 break; 148 149 case 'y': 150 yflag++; 151 nflag = 0; 152 break; 153 154 case 'Z': 155 Zflag++; 156 break; 157 158 default: 159 usage(); 160 } 161 } 162 argc -= optind; 163 argv += optind; 164 165 if (!argc) 166 usage(); 167 168 if (signal(SIGINT, SIG_IGN) != SIG_IGN) 169 (void)signal(SIGINT, catch); 170 if (ckclean) 171 (void)signal(SIGQUIT, catchquit); 172 signal(SIGINFO, infohandler); 173 if (bkgrdflag) { 174 signal(SIGALRM, alarmhandler); 175 itimerval.it_interval.tv_sec = 5; 176 itimerval.it_interval.tv_usec = 0; 177 itimerval.it_value.tv_sec = 5; 178 itimerval.it_value.tv_usec = 0; 179 setitimer(ITIMER_REAL, &itimerval, NULL); 180 } 181 /* 182 * Push up our allowed memory limit so we can cope 183 * with huge file systems. 184 */ 185 if (getrlimit(RLIMIT_DATA, &rlimit) == 0) { 186 rlimit.rlim_cur = rlimit.rlim_max; 187 (void)setrlimit(RLIMIT_DATA, &rlimit); 188 } 189 while (argc-- > 0) 190 (void)checkfilesys(*argv++); 191 192 if (returntosingle) 193 ret = 2; 194 exit(ret); 195 } 196 197 static int 198 argtoi(int flag, const char *req, const char *str, int base) 199 { 200 char *cp; 201 int ret; 202 203 ret = (int)strtol(str, &cp, base); 204 if (cp == str || *cp) 205 errx(EEXIT, "-%c flag requires a %s", flag, req); 206 return (ret); 207 } 208 209 /* 210 * Check the specified file system. 211 */ 212 /* ARGSUSED */ 213 static int 214 checkfilesys(char *filesys) 215 { 216 ufs2_daddr_t n_ffree, n_bfree; 217 struct dups *dp; 218 struct statfs *mntp; 219 struct stat snapdir; 220 struct group *grp; 221 struct iovec *iov; 222 char errmsg[255]; 223 int iovlen; 224 int cylno; 225 intmax_t blks, files; 226 size_t size; 227 228 iov = NULL; 229 iovlen = 0; 230 errmsg[0] = '\0'; 231 232 cdevname = filesys; 233 if (debug && ckclean) 234 pwarn("starting\n"); 235 /* 236 * Make best effort to get the disk name. Check first to see 237 * if it is listed among the mounted file systems. Failing that 238 * check to see if it is listed in /etc/fstab. 239 */ 240 mntp = getmntpt(filesys); 241 if (mntp != NULL) 242 filesys = mntp->f_mntfromname; 243 else 244 filesys = blockcheck(filesys); 245 /* 246 * If -F flag specified, check to see whether a background check 247 * is possible and needed. If possible and needed, exit with 248 * status zero. Otherwise exit with status non-zero. A non-zero 249 * exit status will cause a foreground check to be run. 250 */ 251 sblock_init(); 252 if (bkgrdcheck) { 253 if ((fsreadfd = open(filesys, O_RDONLY)) < 0 || readsb(0) == 0) 254 exit(3); /* Cannot read superblock */ 255 close(fsreadfd); 256 /* Earlier background failed or journaled */ 257 if (sblock.fs_flags & (FS_NEEDSFSCK | FS_SUJ)) 258 exit(4); 259 if ((sblock.fs_flags & FS_DOSOFTDEP) == 0) 260 exit(5); /* Not running soft updates */ 261 size = MIBSIZE; 262 if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0) 263 exit(6); /* Lacks kernel support */ 264 if ((mntp == NULL && sblock.fs_clean == 1) || 265 (mntp != NULL && (sblock.fs_flags & FS_UNCLEAN) == 0)) 266 exit(7); /* Filesystem clean, report it now */ 267 exit(0); 268 } 269 if (ckclean && skipclean) { 270 /* 271 * If file system is gjournaled, check it here. 272 */ 273 if ((fsreadfd = open(filesys, O_RDONLY)) < 0 || readsb(0) == 0) 274 exit(3); /* Cannot read superblock */ 275 close(fsreadfd); 276 if ((sblock.fs_flags & FS_GJOURNAL) != 0) { 277 //printf("GJournaled file system detected on %s.\n", 278 // filesys); 279 if (sblock.fs_clean == 1) { 280 pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n"); 281 exit(0); 282 } 283 if ((sblock.fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0) { 284 gjournal_check(filesys); 285 if (chkdoreload(mntp) == 0) 286 exit(0); 287 exit(4); 288 } else { 289 pfatal( 290 "UNEXPECTED INCONSISTENCY, CANNOT RUN FAST FSCK\n"); 291 } 292 } 293 } 294 /* 295 * If we are to do a background check: 296 * Get the mount point information of the file system 297 * create snapshot file 298 * return created snapshot file 299 * if not found, clear bkgrdflag and proceed with normal fsck 300 */ 301 if (bkgrdflag) { 302 if (mntp == NULL) { 303 bkgrdflag = 0; 304 pfatal("NOT MOUNTED, CANNOT RUN IN BACKGROUND\n"); 305 } else if ((mntp->f_flags & MNT_SOFTDEP) == 0) { 306 bkgrdflag = 0; 307 pfatal( 308 "NOT USING SOFT UPDATES, CANNOT RUN IN BACKGROUND\n"); 309 } else if ((mntp->f_flags & MNT_RDONLY) != 0) { 310 bkgrdflag = 0; 311 pfatal("MOUNTED READ-ONLY, CANNOT RUN IN BACKGROUND\n"); 312 } else if ((fsreadfd = open(filesys, O_RDONLY)) >= 0) { 313 if (readsb(0) != 0) { 314 if (sblock.fs_flags & (FS_NEEDSFSCK | FS_SUJ)) { 315 bkgrdflag = 0; 316 pfatal( 317 "UNEXPECTED INCONSISTENCY, CANNOT RUN IN BACKGROUND\n"); 318 } 319 if ((sblock.fs_flags & FS_UNCLEAN) == 0 && 320 skipclean && ckclean) { 321 /* 322 * file system is clean; 323 * skip snapshot and report it clean 324 */ 325 pwarn( 326 "FILE SYSTEM CLEAN; SKIPPING CHECKS\n"); 327 goto clean; 328 } 329 } 330 close(fsreadfd); 331 } 332 if (bkgrdflag) { 333 snprintf(snapname, sizeof snapname, "%s/.snap", 334 mntp->f_mntonname); 335 if (stat(snapname, &snapdir) < 0) { 336 if (errno != ENOENT) { 337 bkgrdflag = 0; 338 pfatal( 339 "CANNOT FIND SNAPSHOT DIRECTORY %s: %s, CANNOT RUN IN BACKGROUND\n", 340 snapname, strerror(errno)); 341 } else if ((grp = getgrnam("operator")) == 0 || 342 mkdir(snapname, 0770) < 0 || 343 chown(snapname, -1, grp->gr_gid) < 0 || 344 chmod(snapname, 0770) < 0) { 345 bkgrdflag = 0; 346 pfatal( 347 "CANNOT CREATE SNAPSHOT DIRECTORY %s: %s, CANNOT RUN IN BACKGROUND\n", 348 snapname, strerror(errno)); 349 } 350 } else if (!S_ISDIR(snapdir.st_mode)) { 351 bkgrdflag = 0; 352 pfatal( 353 "%s IS NOT A DIRECTORY, CANNOT RUN IN BACKGROUND\n", 354 snapname); 355 } 356 } 357 if (bkgrdflag) { 358 snprintf(snapname, sizeof snapname, 359 "%s/.snap/fsck_snapshot", mntp->f_mntonname); 360 build_iovec(&iov, &iovlen, "fstype", "ffs", 4); 361 build_iovec(&iov, &iovlen, "from", snapname, 362 (size_t)-1); 363 build_iovec(&iov, &iovlen, "fspath", mntp->f_mntonname, 364 (size_t)-1); 365 build_iovec(&iov, &iovlen, "errmsg", errmsg, 366 sizeof(errmsg)); 367 build_iovec(&iov, &iovlen, "update", NULL, 0); 368 build_iovec(&iov, &iovlen, "snapshot", NULL, 0); 369 370 while (nmount(iov, iovlen, mntp->f_flags) < 0) { 371 if (errno == EEXIST && unlink(snapname) == 0) 372 continue; 373 bkgrdflag = 0; 374 pfatal("CANNOT CREATE SNAPSHOT %s: %s %s\n", 375 snapname, strerror(errno), errmsg); 376 break; 377 } 378 if (bkgrdflag != 0) 379 filesys = snapname; 380 } 381 } 382 383 switch (setup(filesys)) { 384 case 0: 385 if (preen) 386 pfatal("CAN'T CHECK FILE SYSTEM."); 387 return (0); 388 case -1: 389 clean: 390 pwarn("clean, %ld free ", (long)(sblock.fs_cstotal.cs_nffree + 391 sblock.fs_frag * sblock.fs_cstotal.cs_nbfree)); 392 printf("(%jd frags, %jd blocks, %.1f%% fragmentation)\n", 393 (intmax_t)sblock.fs_cstotal.cs_nffree, 394 (intmax_t)sblock.fs_cstotal.cs_nbfree, 395 sblock.fs_cstotal.cs_nffree * 100.0 / sblock.fs_dsize); 396 return (0); 397 } 398 /* 399 * Determine if we can and should do journal recovery. 400 */ 401 if ((sblock.fs_flags & FS_SUJ) == FS_SUJ) { 402 if ((sblock.fs_flags & FS_NEEDSFSCK) != FS_NEEDSFSCK && skipclean) { 403 if (preen || reply("USE JOURNAL")) { 404 if (suj_check(filesys) == 0) { 405 printf("\n***** FILE SYSTEM MARKED CLEAN *****\n"); 406 if (chkdoreload(mntp) == 0) 407 exit(0); 408 exit(4); 409 } 410 } 411 printf("** Skipping journal, falling through to full fsck\n\n"); 412 } 413 /* 414 * Write the superblock so we don't try to recover the 415 * journal on another pass. 416 */ 417 sblock.fs_mtime = time(NULL); 418 sbdirty(); 419 } 420 421 /* 422 * Cleared if any questions answered no. Used to decide if 423 * the superblock should be marked clean. 424 */ 425 resolved = 1; 426 /* 427 * 1: scan inodes tallying blocks used 428 */ 429 if (preen == 0) { 430 printf("** Last Mounted on %s\n", sblock.fs_fsmnt); 431 if (mntp != NULL && mntp->f_flags & MNT_ROOTFS) 432 printf("** Root file system\n"); 433 printf("** Phase 1 - Check Blocks and Sizes\n"); 434 } 435 clock_gettime(CLOCK_REALTIME_PRECISE, &startprog); 436 pass1(); 437 IOstats("Pass1"); 438 439 /* 440 * 1b: locate first references to duplicates, if any 441 */ 442 if (duplist) { 443 if (preen || usedsoftdep) 444 pfatal("INTERNAL ERROR: dups with %s%s%s", 445 preen ? "-p" : "", 446 (preen && usedsoftdep) ? " and " : "", 447 usedsoftdep ? "softupdates" : ""); 448 printf("** Phase 1b - Rescan For More DUPS\n"); 449 pass1b(); 450 IOstats("Pass1b"); 451 } 452 453 /* 454 * 2: traverse directories from root to mark all connected directories 455 */ 456 if (preen == 0) 457 printf("** Phase 2 - Check Pathnames\n"); 458 pass2(); 459 IOstats("Pass2"); 460 461 /* 462 * 3: scan inodes looking for disconnected directories 463 */ 464 if (preen == 0) 465 printf("** Phase 3 - Check Connectivity\n"); 466 pass3(); 467 IOstats("Pass3"); 468 469 /* 470 * 4: scan inodes looking for disconnected files; check reference counts 471 */ 472 if (preen == 0) 473 printf("** Phase 4 - Check Reference Counts\n"); 474 pass4(); 475 IOstats("Pass4"); 476 477 /* 478 * 5: check and repair resource counts in cylinder groups 479 */ 480 if (preen == 0) 481 printf("** Phase 5 - Check Cyl groups\n"); 482 pass5(); 483 IOstats("Pass5"); 484 485 /* 486 * print out summary statistics 487 */ 488 n_ffree = sblock.fs_cstotal.cs_nffree; 489 n_bfree = sblock.fs_cstotal.cs_nbfree; 490 files = maxino - ROOTINO - sblock.fs_cstotal.cs_nifree - n_files; 491 blks = n_blks + 492 sblock.fs_ncg * (cgdmin(&sblock, 0) - cgsblock(&sblock, 0)); 493 blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0); 494 blks += howmany(sblock.fs_cssize, sblock.fs_fsize); 495 blks = maxfsblock - (n_ffree + sblock.fs_frag * n_bfree) - blks; 496 if (bkgrdflag && (files > 0 || blks > 0)) { 497 countdirs = sblock.fs_cstotal.cs_ndir - countdirs; 498 pwarn("Reclaimed: %ld directories, %jd files, %jd fragments\n", 499 countdirs, files - countdirs, blks); 500 } 501 pwarn("%ld files, %jd used, %ju free ", 502 (long)n_files, (intmax_t)n_blks, 503 (uintmax_t)n_ffree + sblock.fs_frag * n_bfree); 504 printf("(%ju frags, %ju blocks, %.1f%% fragmentation)\n", 505 (uintmax_t)n_ffree, (uintmax_t)n_bfree, 506 n_ffree * 100.0 / sblock.fs_dsize); 507 if (debug) { 508 if (files < 0) 509 printf("%jd inodes missing\n", -files); 510 if (blks < 0) 511 printf("%jd blocks missing\n", -blks); 512 if (duplist != NULL) { 513 printf("The following duplicate blocks remain:"); 514 for (dp = duplist; dp; dp = dp->next) 515 printf(" %jd,", (intmax_t)dp->dup); 516 printf("\n"); 517 } 518 } 519 duplist = (struct dups *)0; 520 muldup = (struct dups *)0; 521 inocleanup(); 522 if (fsmodified) { 523 sblock.fs_time = time(NULL); 524 sbdirty(); 525 } 526 if (cvtlevel && sblk.b_dirty) { 527 /* 528 * Write out the duplicate super blocks 529 */ 530 for (cylno = 0; cylno < sblock.fs_ncg; cylno++) 531 blwrite(fswritefd, (char *)&sblock, 532 fsbtodb(&sblock, cgsblock(&sblock, cylno)), 533 SBLOCKSIZE); 534 } 535 if (rerun) 536 resolved = 0; 537 finalIOstats(); 538 539 /* 540 * Check to see if the file system is mounted read-write. 541 */ 542 if (bkgrdflag == 0 && mntp != NULL && (mntp->f_flags & MNT_RDONLY) == 0) 543 resolved = 0; 544 ckfini(resolved); 545 546 for (cylno = 0; cylno < sblock.fs_ncg; cylno++) 547 if (inostathead[cylno].il_stat != NULL) 548 free((char *)inostathead[cylno].il_stat); 549 free((char *)inostathead); 550 inostathead = NULL; 551 if (fsmodified && !preen) 552 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n"); 553 if (rerun) 554 printf("\n***** PLEASE RERUN FSCK *****\n"); 555 if (chkdoreload(mntp) != 0) { 556 if (!fsmodified) 557 return (0); 558 if (!preen) 559 printf("\n***** REBOOT NOW *****\n"); 560 sync(); 561 return (4); 562 } 563 return (0); 564 } 565 566 static int 567 chkdoreload(struct statfs *mntp) 568 { 569 struct iovec *iov; 570 int iovlen; 571 char errmsg[255]; 572 573 if (mntp == NULL) 574 return (0); 575 576 iov = NULL; 577 iovlen = 0; 578 errmsg[0] = '\0'; 579 /* 580 * We modified a mounted file system. Do a mount update on 581 * it unless it is read-write, so we can continue using it 582 * as safely as possible. 583 */ 584 if (mntp->f_flags & MNT_RDONLY) { 585 build_iovec(&iov, &iovlen, "fstype", "ffs", 4); 586 build_iovec(&iov, &iovlen, "from", mntp->f_mntfromname, 587 (size_t)-1); 588 build_iovec(&iov, &iovlen, "fspath", mntp->f_mntonname, 589 (size_t)-1); 590 build_iovec(&iov, &iovlen, "errmsg", errmsg, 591 sizeof(errmsg)); 592 build_iovec(&iov, &iovlen, "update", NULL, 0); 593 build_iovec(&iov, &iovlen, "reload", NULL, 0); 594 /* 595 * XX: We need the following line until we clean up 596 * nmount parsing of root mounts and NFS root mounts. 597 */ 598 build_iovec(&iov, &iovlen, "ro", NULL, 0); 599 if (nmount(iov, iovlen, mntp->f_flags) == 0) { 600 return (0); 601 } 602 pwarn("mount reload of '%s' failed: %s %s\n\n", 603 mntp->f_mntonname, strerror(errno), errmsg); 604 return (1); 605 } 606 return (0); 607 } 608 609 /* 610 * Get the mount point information for name. 611 */ 612 static struct statfs * 613 getmntpt(const char *name) 614 { 615 struct stat devstat, mntdevstat; 616 char device[sizeof(_PATH_DEV) - 1 + MNAMELEN]; 617 char *ddevname; 618 struct statfs *mntbuf, *statfsp; 619 int i, mntsize, isdev; 620 621 if (stat(name, &devstat) != 0) 622 return (NULL); 623 if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode)) 624 isdev = 1; 625 else 626 isdev = 0; 627 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 628 for (i = 0; i < mntsize; i++) { 629 statfsp = &mntbuf[i]; 630 ddevname = statfsp->f_mntfromname; 631 if (*ddevname != '/') { 632 strcpy(device, _PATH_DEV); 633 strcat(device, ddevname); 634 strcpy(statfsp->f_mntfromname, device); 635 } 636 if (isdev == 0) { 637 if (strcmp(name, statfsp->f_mntonname)) 638 continue; 639 return (statfsp); 640 } 641 if (stat(ddevname, &mntdevstat) == 0 && 642 mntdevstat.st_rdev == devstat.st_rdev) 643 return (statfsp); 644 } 645 statfsp = NULL; 646 return (statfsp); 647 } 648 649 static void 650 usage(void) 651 { 652 (void) fprintf(stderr, 653 "usage: %s [-BEFfnpry] [-b block] [-c level] [-m mode] filesystem ...\n", 654 getprogname()); 655 exit(1); 656 } 657