1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 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. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #if 0 33 #ifndef lint 34 static const char copyright[] = 35 "@(#) Copyright (c) 1980, 1986, 1993\n\ 36 The Regents of the University of California. All rights reserved.\n"; 37 #endif /* not lint */ 38 39 #ifndef lint 40 static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/14/95"; 41 #endif /* not lint */ 42 #endif 43 #include <sys/cdefs.h> 44 __FBSDID("$FreeBSD$"); 45 46 #define _WANT_P_OSREL 47 #include <sys/param.h> 48 #include <sys/file.h> 49 #include <sys/mount.h> 50 #include <sys/resource.h> 51 #include <sys/stat.h> 52 #include <sys/sysctl.h> 53 #include <sys/uio.h> 54 #include <sys/disklabel.h> 55 56 #include <ufs/ufs/dinode.h> 57 #include <ufs/ffs/fs.h> 58 59 #include <err.h> 60 #include <errno.h> 61 #include <fstab.h> 62 #include <grp.h> 63 #include <inttypes.h> 64 #include <libufs.h> 65 #include <mntopts.h> 66 #include <paths.h> 67 #include <stdint.h> 68 #include <string.h> 69 #include <time.h> 70 71 #include "fsck.h" 72 73 static int restarts; 74 75 static void usage(void) __dead2; 76 static intmax_t argtoimax(int flag, const char *req, const char *str, int base); 77 static int checkfilesys(char *filesys); 78 static int setup_bkgrdchk(struct statfs *mntp, int sbrdfailed, char **filesys); 79 80 int 81 main(int argc, char *argv[]) 82 { 83 int ch; 84 struct rlimit rlimit; 85 struct itimerval itimerval; 86 int fsret; 87 int ret = 0; 88 89 sync(); 90 skipclean = 1; 91 inoopt = 0; 92 while ((ch = getopt(argc, argv, "b:Bc:CdEfFm:npRrSyZz")) != -1) { 93 switch (ch) { 94 case 'b': 95 skipclean = 0; 96 bflag = argtoimax('b', "number", optarg, 10); 97 printf("Alternate super block location: %jd\n", bflag); 98 break; 99 100 case 'B': 101 bkgrdflag = 1; 102 break; 103 104 case 'c': 105 skipclean = 0; 106 cvtlevel = argtoimax('c', "conversion level", optarg, 107 10); 108 if (cvtlevel < 3) 109 errx(EEXIT, "cannot do level %d conversion", 110 cvtlevel); 111 break; 112 113 case 'd': 114 debug++; 115 break; 116 117 case 'E': 118 Eflag++; 119 break; 120 121 case 'f': 122 skipclean = 0; 123 break; 124 125 case 'F': 126 bkgrdcheck = 1; 127 break; 128 129 case 'm': 130 lfmode = argtoimax('m', "mode", optarg, 8); 131 if (lfmode &~ 07777) 132 errx(EEXIT, "bad mode to -m: %o", lfmode); 133 printf("** lost+found creation mode %o\n", lfmode); 134 break; 135 136 case 'n': 137 nflag++; 138 yflag = 0; 139 break; 140 141 case 'p': 142 preen++; 143 /*FALLTHROUGH*/ 144 145 case 'C': 146 ckclean++; 147 break; 148 149 case 'R': 150 wantrestart = 1; 151 break; 152 case 'r': 153 inoopt++; 154 break; 155 156 case 'S': 157 surrender = 1; 158 break; 159 160 case 'y': 161 yflag++; 162 nflag = 0; 163 break; 164 165 case 'Z': 166 Zflag++; 167 break; 168 169 case 'z': 170 zflag++; 171 break; 172 173 default: 174 usage(); 175 } 176 } 177 argc -= optind; 178 argv += optind; 179 180 if (!argc) 181 usage(); 182 183 if (bkgrdflag && cvtlevel > 0) { 184 pfatal("CANNOT CONVERT A SNAPSHOT\n"); 185 exit(EEXIT); 186 } 187 188 if (signal(SIGINT, SIG_IGN) != SIG_IGN) 189 (void)signal(SIGINT, catch); 190 if (ckclean) 191 (void)signal(SIGQUIT, catchquit); 192 signal(SIGINFO, infohandler); 193 if (bkgrdflag) { 194 signal(SIGALRM, alarmhandler); 195 itimerval.it_interval.tv_sec = 5; 196 itimerval.it_interval.tv_usec = 0; 197 itimerval.it_value.tv_sec = 5; 198 itimerval.it_value.tv_usec = 0; 199 setitimer(ITIMER_REAL, &itimerval, NULL); 200 } 201 /* 202 * Push up our allowed memory limit so we can cope 203 * with huge file systems. 204 */ 205 if (getrlimit(RLIMIT_DATA, &rlimit) == 0) { 206 rlimit.rlim_cur = rlimit.rlim_max; 207 (void)setrlimit(RLIMIT_DATA, &rlimit); 208 } 209 while (argc > 0) { 210 if ((fsret = checkfilesys(*argv)) == ERESTART) 211 continue; 212 ret |= fsret; 213 argc--; 214 argv++; 215 } 216 217 if (returntosingle) 218 ret = 2; 219 exit(ret); 220 } 221 222 static intmax_t 223 argtoimax(int flag, const char *req, const char *str, int base) 224 { 225 char *cp; 226 intmax_t ret; 227 228 ret = strtoimax(str, &cp, base); 229 if (cp == str || *cp) 230 errx(EEXIT, "-%c flag requires a %s", flag, req); 231 return (ret); 232 } 233 234 /* 235 * Check the specified file system. 236 */ 237 /* ARGSUSED */ 238 static int 239 checkfilesys(char *filesys) 240 { 241 ufs2_daddr_t n_ffree, n_bfree; 242 struct dups *dp; 243 struct statfs *mntp; 244 intmax_t blks, files; 245 size_t size; 246 int sbreadfailed, ofsmodified; 247 248 fsutilinit(); 249 fsckinit(); 250 251 cdevname = filesys; 252 if (debug && ckclean) 253 pwarn("starting\n"); 254 /* 255 * Make best effort to get the disk name. Check first to see 256 * if it is listed among the mounted file systems. Failing that 257 * check to see if it is listed in /etc/fstab. 258 */ 259 mntp = getmntpoint(filesys); 260 if (mntp != NULL) 261 filesys = mntp->f_mntfromname; 262 else 263 filesys = blockcheck(filesys); 264 /* 265 * If -F flag specified, check to see whether a background check 266 * is possible and needed. If possible and needed, exit with 267 * status zero. Otherwise exit with status non-zero. A non-zero 268 * exit status will cause a foreground check to be run. 269 */ 270 sblock_init(); 271 sbreadfailed = 0; 272 if (openfilesys(filesys) == 0 || readsb() == 0) 273 sbreadfailed = 1; 274 if (bkgrdcheck) { 275 if (sbreadfailed) 276 exit(3); /* Cannot read superblock */ 277 /* Earlier background failed or journaled */ 278 if (sblock.fs_flags & (FS_NEEDSFSCK | FS_SUJ)) 279 exit(4); 280 if ((sblock.fs_flags & FS_DOSOFTDEP) == 0) 281 exit(5); /* Not running soft updates */ 282 size = MIBSIZE; 283 if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0) 284 exit(6); /* Lacks kernel support */ 285 if ((mntp == NULL && sblock.fs_clean == 1) || 286 (mntp != NULL && (sblock.fs_flags & FS_UNCLEAN) == 0)) 287 exit(7); /* Filesystem clean, report it now */ 288 exit(0); 289 } 290 if (ckclean && skipclean) { 291 /* 292 * If file system is gjournaled, check it here. 293 */ 294 if (sbreadfailed) 295 exit(3); /* Cannot read superblock */ 296 if (bkgrdflag == 0 && 297 (nflag || (fswritefd = open(filesys, O_WRONLY)) < 0)) { 298 fswritefd = -1; 299 if (preen) 300 pfatal("NO WRITE ACCESS"); 301 printf(" (NO WRITE)"); 302 } 303 if ((sblock.fs_flags & FS_GJOURNAL) != 0) { 304 if (sblock.fs_clean == 1) { 305 pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n"); 306 exit(0); 307 } 308 if ((sblock.fs_flags & 309 (FS_UNCLEAN | FS_NEEDSFSCK)) == 0) { 310 bufinit(); 311 gjournal_check(filesys); 312 if (chkdoreload(mntp, pwarn) == 0) 313 exit(0); 314 exit(4); 315 } else { 316 pfatal("FULL FSCK NEEDED, CANNOT RUN FAST " 317 "FSCK\n"); 318 } 319 } 320 close(fswritefd); 321 fswritefd = -1; 322 } 323 if (bkgrdflag) { 324 switch (setup_bkgrdchk(mntp, sbreadfailed, &filesys)) { 325 case -1: /* filesystem clean */ 326 goto clean; 327 case 0: /* cannot do background, give up */ 328 exit(EEXIT); 329 case 1: /* doing background check, preen rules apply */ 330 preen = 1; 331 break; 332 } 333 } 334 335 switch (setup(filesys)) { 336 case 0: 337 if (preen) 338 pfatal("CAN'T CHECK FILE SYSTEM."); 339 return (EEXIT); 340 case -1: 341 clean: 342 pwarn("clean, %ld free ", (long)(sblock.fs_cstotal.cs_nffree + 343 sblock.fs_frag * sblock.fs_cstotal.cs_nbfree)); 344 printf("(%jd frags, %jd blocks, %.1f%% fragmentation)\n", 345 (intmax_t)sblock.fs_cstotal.cs_nffree, 346 (intmax_t)sblock.fs_cstotal.cs_nbfree, 347 sblock.fs_cstotal.cs_nffree * 100.0 / sblock.fs_dsize); 348 return (0); 349 } 350 /* 351 * Determine if we can and should do journal recovery. 352 */ 353 if ((sblock.fs_flags & FS_SUJ) == FS_SUJ) { 354 if ((sblock.fs_flags & FS_NEEDSFSCK) != FS_NEEDSFSCK && 355 skipclean) { 356 sujrecovery = 1; 357 if (suj_check(filesys) == 0) { 358 pwarn("\n**** FILE SYSTEM MARKED CLEAN ****\n"); 359 if (chkdoreload(mntp, pwarn) == 0) 360 exit(0); 361 exit(4); 362 } 363 sujrecovery = 0; 364 pwarn("Skipping journal, " 365 "falling through to full fsck\n"); 366 } 367 if (fswritefd != -1) { 368 /* 369 * Write the superblock so we don't try to recover the 370 * journal on another pass. If this is the only change 371 * to the filesystem, we do not want it to be called 372 * out as modified. 373 */ 374 sblock.fs_mtime = time(NULL); 375 sbdirty(); 376 ofsmodified = fsmodified; 377 flush(fswritefd, &sblk); 378 fsmodified = ofsmodified; 379 } 380 } 381 /* 382 * If the filesystem was run on an old kernel that did not 383 * support check hashes, clear the check-hash flags so that 384 * we do not try to verify them. 385 */ 386 if ((sblock.fs_flags & FS_METACKHASH) == 0) 387 sblock.fs_metackhash = 0; 388 /* 389 * If we are running on a kernel that can provide check hashes 390 * that are not yet enabled for the filesystem and we are 391 * running manually without the -y flag, offer to add any 392 * supported check hashes that are not already enabled. 393 */ 394 ckhashadd = 0; 395 if (preen == 0 && yflag == 0 && sblock.fs_magic != FS_UFS1_MAGIC && 396 fswritefd != -1 && getosreldate() >= P_OSREL_CK_CYLGRP) { 397 if ((sblock.fs_metackhash & CK_CYLGRP) == 0 && 398 reply("ADD CYLINDER GROUP CHECK-HASH PROTECTION") != 0) { 399 ckhashadd |= CK_CYLGRP; 400 sblock.fs_metackhash |= CK_CYLGRP; 401 } 402 if ((sblock.fs_metackhash & CK_SUPERBLOCK) == 0 && 403 getosreldate() >= P_OSREL_CK_SUPERBLOCK && 404 reply("ADD SUPERBLOCK CHECK-HASH PROTECTION") != 0) { 405 ckhashadd |= CK_SUPERBLOCK; 406 sblock.fs_metackhash |= CK_SUPERBLOCK; 407 } 408 if ((sblock.fs_metackhash & CK_INODE) == 0 && 409 getosreldate() >= P_OSREL_CK_INODE && 410 reply("ADD INODE CHECK-HASH PROTECTION") != 0) { 411 ckhashadd |= CK_INODE; 412 sblock.fs_metackhash |= CK_INODE; 413 } 414 #ifdef notyet 415 if ((sblock.fs_metackhash & CK_INDIR) == 0 && 416 getosreldate() >= P_OSREL_CK_INDIR && 417 reply("ADD INDIRECT BLOCK CHECK-HASH PROTECTION") != 0) { 418 ckhashadd |= CK_INDIR; 419 sblock.fs_metackhash |= CK_INDIR; 420 } 421 if ((sblock.fs_metackhash & CK_DIR) == 0 && 422 getosreldate() >= P_OSREL_CK_DIR && 423 reply("ADD DIRECTORY CHECK-HASH PROTECTION") != 0) { 424 ckhashadd |= CK_DIR; 425 sblock.fs_metackhash |= CK_DIR; 426 } 427 #endif /* notyet */ 428 if (ckhashadd != 0) { 429 sblock.fs_flags |= FS_METACKHASH; 430 sbdirty(); 431 } 432 } 433 /* 434 * Cleared if any questions answered no. Used to decide if 435 * the superblock should be marked clean. 436 */ 437 resolved = 1; 438 /* 439 * 1: scan inodes tallying blocks used 440 */ 441 if (preen == 0 || debug) { 442 printf("** Last Mounted on %s\n", sblock.fs_fsmnt); 443 if (mntp != NULL && mntp->f_flags & MNT_ROOTFS) 444 printf("** Root file system\n"); 445 printf("** Phase 1 - Check Blocks and Sizes\n"); 446 } 447 clock_gettime(CLOCK_REALTIME_PRECISE, &startprog); 448 pass1(); 449 IOstats("Pass1"); 450 451 /* 452 * 1b: locate first references to duplicates, if any 453 */ 454 if (duplist) { 455 if (preen || usedsoftdep) 456 pfatal("INTERNAL ERROR: DUPS WITH %s%s%s", 457 preen ? "-p" : "", 458 (preen && usedsoftdep) ? " AND " : "", 459 usedsoftdep ? "SOFTUPDATES" : ""); 460 if (preen == 0 || debug) 461 printf("** Phase 1b - Rescan For More DUPS\n"); 462 pass1b(); 463 IOstats("Pass1b"); 464 } 465 466 /* 467 * 2: traverse directories from root to mark all connected directories 468 */ 469 if (preen == 0 || debug) 470 printf("** Phase 2 - Check Pathnames\n"); 471 pass2(); 472 IOstats("Pass2"); 473 474 /* 475 * 3: scan inodes looking for disconnected directories 476 */ 477 if (preen == 0 || debug) 478 printf("** Phase 3 - Check Connectivity\n"); 479 pass3(); 480 IOstats("Pass3"); 481 482 /* 483 * 4: scan inodes looking for disconnected files; check reference counts 484 */ 485 if (preen == 0 || debug) 486 printf("** Phase 4 - Check Reference Counts\n"); 487 pass4(); 488 IOstats("Pass4"); 489 490 /* 491 * 5: check and repair resource counts in cylinder groups 492 */ 493 if (preen == 0 || debug) 494 printf("** Phase 5 - Check Cyl groups\n"); 495 snapflush(std_checkblkavail); 496 if (cgheader_corrupt) { 497 printf("PHASE 5 SKIPPED DUE TO CORRUPT CYLINDER GROUP " 498 "HEADER(S)\n\n"); 499 } else { 500 pass5(); 501 IOstats("Pass5"); 502 } 503 504 /* 505 * print out summary statistics 506 */ 507 n_ffree = sblock.fs_cstotal.cs_nffree; 508 n_bfree = sblock.fs_cstotal.cs_nbfree; 509 files = maxino - UFS_ROOTINO - sblock.fs_cstotal.cs_nifree - n_files; 510 blks = n_blks + 511 sblock.fs_ncg * (cgdmin(&sblock, 0) - cgsblock(&sblock, 0)); 512 blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0); 513 blks += howmany(sblock.fs_cssize, sblock.fs_fsize); 514 blks = maxfsblock - (n_ffree + sblock.fs_frag * n_bfree) - blks; 515 if (bkgrdflag && (files > 0 || blks > 0)) { 516 countdirs = sblock.fs_cstotal.cs_ndir - countdirs; 517 pwarn("Reclaimed: %ld directories, %jd files, %jd fragments\n", 518 countdirs, files - countdirs, blks); 519 } 520 pwarn("%ld files, %jd used, %ju free ", 521 (long)n_files, (intmax_t)n_blks, 522 (uintmax_t)n_ffree + sblock.fs_frag * n_bfree); 523 printf("(%ju frags, %ju blocks, %.1f%% fragmentation)\n", 524 (uintmax_t)n_ffree, (uintmax_t)n_bfree, 525 n_ffree * 100.0 / sblock.fs_dsize); 526 if (debug) { 527 if (files < 0) 528 printf("%jd inodes missing\n", -files); 529 if (blks < 0) 530 printf("%jd blocks missing\n", -blks); 531 if (duplist != NULL) { 532 printf("The following duplicate blocks remain:"); 533 for (dp = duplist; dp; dp = dp->next) 534 printf(" %jd,", (intmax_t)dp->dup); 535 printf("\n"); 536 } 537 } 538 duplist = (struct dups *)0; 539 muldup = (struct dups *)0; 540 inocleanup(); 541 if (fsmodified) { 542 sblock.fs_time = time(NULL); 543 sbdirty(); 544 } 545 if (cvtlevel && (sblk.b_flags & B_DIRTY) != 0) { 546 /* 547 * Write out the duplicate super blocks 548 */ 549 if (sbput(fswritefd, &sblock, sblock.fs_ncg) == 0) 550 fsmodified = 1; 551 } 552 if (rerun) 553 resolved = 0; 554 555 /* 556 * Check to see if the file system is mounted read-write. 557 */ 558 if (bkgrdflag == 0 && mntp != NULL && (mntp->f_flags & MNT_RDONLY) == 0) 559 resolved = 0; 560 ckfini(resolved); 561 562 if (fsmodified && !preen) 563 printf("\n***** FILE SYSTEM WAS MODIFIED *****\n"); 564 if (rerun) { 565 if (wantrestart && (restarts++ < 10) && 566 (preen || reply("RESTART"))) 567 return (ERESTART); 568 printf("\n***** PLEASE RERUN FSCK *****\n"); 569 } 570 if (chkdoreload(mntp, pwarn) != 0) { 571 if (!fsmodified) 572 return (0); 573 if (!preen) 574 printf("\n***** REBOOT NOW *****\n"); 575 sync(); 576 return (4); 577 } 578 return (rerun ? ERERUN : 0); 579 } 580 581 /* 582 * If we are to do a background check: 583 * Get the mount point information of the file system 584 * If already clean, return -1 585 * Check that kernel supports background fsck 586 * Find or create the snapshot directory 587 * Create the snapshot file 588 * Open snapshot 589 * If anything fails print reason and return 0 which exits 590 */ 591 static int 592 setup_bkgrdchk(struct statfs *mntp, int sbreadfailed, char **filesys) 593 { 594 struct stat snapdir; 595 struct group *grp; 596 struct iovec *iov; 597 char errmsg[255]; 598 int iovlen; 599 size_t size; 600 601 /* Get the mount point information of the file system */ 602 if (mntp == NULL) { 603 pwarn("NOT MOUNTED, CANNOT RUN IN BACKGROUND\n"); 604 return (0); 605 } 606 if ((mntp->f_flags & MNT_RDONLY) != 0) { 607 pwarn("MOUNTED READ-ONLY, CANNOT RUN IN BACKGROUND\n"); 608 return (0); 609 } 610 if ((mntp->f_flags & MNT_SOFTDEP) == 0) { 611 pwarn("NOT USING SOFT UPDATES, CANNOT RUN IN BACKGROUND\n"); 612 return (0); 613 } 614 if (sbreadfailed) { 615 pwarn("SUPERBLOCK READ FAILED, CANNOT RUN IN BACKGROUND\n"); 616 return (0); 617 } 618 if ((sblock.fs_flags & FS_NEEDSFSCK) != 0) { 619 pwarn("FULL FSCK NEEDED, CANNOT RUN IN BACKGROUND\n"); 620 return (0); 621 } 622 if ((sblock.fs_flags & FS_SUJ) != 0) { 623 pwarn("JOURNALED FILESYSTEM, CANNOT RUN IN BACKGROUND\n"); 624 return (0); 625 } 626 if (skipclean && ckclean && 627 (sblock.fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK)) == 0) { 628 /* 629 * file system is clean; 630 * skip snapshot and report it clean 631 */ 632 pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n"); 633 return (-1); 634 } 635 /* Check that kernel supports background fsck */ 636 size = MIBSIZE; 637 if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0|| 638 sysctlnametomib("vfs.ffs.adjblkcnt", adjblkcnt, &size) < 0|| 639 sysctlnametomib("vfs.ffs.setsize", setsize, &size) < 0 || 640 sysctlnametomib("vfs.ffs.freefiles", freefiles, &size) < 0|| 641 sysctlnametomib("vfs.ffs.freedirs", freedirs, &size) < 0 || 642 sysctlnametomib("vfs.ffs.freeblks", freeblks, &size) < 0) { 643 pwarn("KERNEL LACKS BACKGROUND FSCK SUPPORT\n"); 644 return (0); 645 } 646 /* 647 * When kernel lacks runtime bgfsck superblock summary 648 * adjustment functionality, it does not mean we can not 649 * continue, as old kernels will recompute the summary at 650 * mount time. However, it will be an unexpected softupdates 651 * inconsistency if it turns out that the summary is still 652 * incorrect. Set a flag so subsequent operation can know this. 653 */ 654 bkgrdsumadj = 1; 655 if (sysctlnametomib("vfs.ffs.adjndir", adjndir, &size) < 0 || 656 sysctlnametomib("vfs.ffs.adjnbfree", adjnbfree, &size) < 0 || 657 sysctlnametomib("vfs.ffs.adjnifree", adjnifree, &size) < 0 || 658 sysctlnametomib("vfs.ffs.adjnffree", adjnffree, &size) < 0 || 659 sysctlnametomib("vfs.ffs.adjnumclusters", adjnumclusters, 660 &size) < 0) { 661 bkgrdsumadj = 0; 662 pwarn("KERNEL LACKS RUNTIME SUPERBLOCK SUMMARY ADJUSTMENT " 663 "SUPPORT\n"); 664 } 665 /* Find or create the snapshot directory */ 666 snprintf(snapname, sizeof snapname, "%s/.snap", 667 mntp->f_mntonname); 668 if (stat(snapname, &snapdir) < 0) { 669 if (errno != ENOENT) { 670 pwarn("CANNOT FIND SNAPSHOT DIRECTORY %s: %s, CANNOT " 671 "RUN IN BACKGROUND\n", snapname, strerror(errno)); 672 return (0); 673 } 674 if ((grp = getgrnam("operator")) == NULL || 675 mkdir(snapname, 0770) < 0 || 676 chown(snapname, -1, grp->gr_gid) < 0 || 677 chmod(snapname, 0770) < 0) { 678 pwarn("CANNOT CREATE SNAPSHOT DIRECTORY %s: %s, " 679 "CANNOT RUN IN BACKGROUND\n", snapname, 680 strerror(errno)); 681 return (0); 682 } 683 } else if (!S_ISDIR(snapdir.st_mode)) { 684 pwarn("%s IS NOT A DIRECTORY, CANNOT RUN IN BACKGROUND\n", 685 snapname); 686 return (0); 687 } 688 /* Create the snapshot file */ 689 iov = NULL; 690 iovlen = 0; 691 errmsg[0] = '\0'; 692 snprintf(snapname, sizeof snapname, "%s/.snap/fsck_snapshot", 693 mntp->f_mntonname); 694 build_iovec(&iov, &iovlen, "fstype", "ffs", 4); 695 build_iovec(&iov, &iovlen, "from", snapname, (size_t)-1); 696 build_iovec(&iov, &iovlen, "fspath", mntp->f_mntonname, (size_t)-1); 697 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg)); 698 build_iovec(&iov, &iovlen, "update", NULL, 0); 699 build_iovec(&iov, &iovlen, "snapshot", NULL, 0); 700 /* Create snapshot, removing old snapshot if it exists */ 701 while (nmount(iov, iovlen, mntp->f_flags) < 0) { 702 if (errno == EEXIST && unlink(snapname) == 0) 703 continue; 704 pwarn("CANNOT CREATE SNAPSHOT %s: %s %s\n", snapname, 705 strerror(errno), errmsg); 706 return (0); 707 } 708 /* Open snapshot */ 709 if (openfilesys(snapname) == 0) { 710 unlink(snapname); 711 pwarn("CANNOT OPEN SNAPSHOT %s: %s, CANNOT RUN IN " 712 "BACKGROUND\n", snapname, strerror(errno)); 713 return (0); 714 } 715 free(sblock.fs_csp); 716 free(sblock.fs_si); 717 if (readsb() == 0) { 718 pwarn("CANNOT READ SNAPSHOT SUPERBLOCK\n"); 719 return (0); 720 } 721 *filesys = snapname; 722 cmd.version = FFS_CMD_VERSION; 723 cmd.handle = fsreadfd; 724 return (1); 725 } 726 727 static void 728 usage(void) 729 { 730 (void) fprintf(stderr, 731 "usage: %s [-BCdEFfnpRrSyZ] [-b block] [-c level] [-m mode] filesystem ...\n", 732 getprogname()); 733 exit(1); 734 } 735 736 void 737 infohandler(int sig __unused) 738 { 739 got_siginfo = 1; 740 } 741 742 void 743 alarmhandler(int sig __unused) 744 { 745 got_sigalarm = 1; 746 } 747