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