xref: /freebsd/sbin/fsck_ffs/main.c (revision 535af610a4fdace6d50960c0ad9be0597eea7a1b)
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 		if ((sblock.fs_flags & FS_NEEDSFSCK) == FS_NEEDSFSCK)
278 			exit(4);	/* Earlier background failed */
279 		if ((sblock.fs_flags & FS_SUJ) == FS_SUJ) {
280 			maxino = sblock.fs_ncg * sblock.fs_ipg;
281 			maxfsblock = sblock.fs_size;
282 			bufinit();
283 			preen = 1;
284 			if (suj_check(filesys) == 0)
285 				exit(4); /* Journal good, run it now */
286 		}
287 		if ((sblock.fs_flags & FS_DOSOFTDEP) == 0)
288 			exit(5);	/* Not running soft updates */
289 		size = MIBSIZE;
290 		if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0)
291 			exit(6);	/* Lacks kernel support */
292 		if ((mntp == NULL && sblock.fs_clean == 1) ||
293 		    (mntp != NULL && (sblock.fs_flags & FS_UNCLEAN) == 0))
294 			exit(7);	/* Filesystem clean, report it now */
295 		exit(0);
296 	}
297 	if (ckclean && skipclean) {
298 		/*
299 		 * If file system is gjournaled, check it here.
300 		 */
301 		if (sbreadfailed)
302 			exit(3);	/* Cannot read superblock */
303 		if (bkgrdflag == 0 &&
304 		    (nflag || (fswritefd = open(filesys, O_WRONLY)) < 0)) {
305 			fswritefd = -1;
306 			if (preen)
307 				pfatal("NO WRITE ACCESS");
308 			printf(" (NO WRITE)");
309 		}
310 		if ((sblock.fs_flags & FS_GJOURNAL) != 0) {
311 			if (sblock.fs_clean == 1) {
312 				pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
313 				exit(0);
314 			}
315 			if ((sblock.fs_flags &
316 			    (FS_UNCLEAN | FS_NEEDSFSCK)) == 0) {
317 				bufinit();
318 				gjournal_check(filesys);
319 				if (chkdoreload(mntp, pwarn) == 0)
320 					exit(0);
321 				exit(4);
322 			} else {
323 				pfatal("FULL FSCK NEEDED, CANNOT RUN FAST "
324 				    "FSCK\n");
325 			}
326 		}
327 		close(fswritefd);
328 		fswritefd = -1;
329 	}
330 	if (bkgrdflag) {
331 		switch (setup_bkgrdchk(mntp, sbreadfailed, &filesys)) {
332 		case -1: /* filesystem clean */
333 			goto clean;
334 		case 0: /* cannot do background, give up */
335 			exit(EEXIT);
336 		case 1: /* doing background check, preen rules apply */
337 			preen = 1;
338 			break;
339 		}
340 	}
341 
342 	switch (setup(filesys)) {
343 	case 0:
344 		if (preen)
345 			pfatal("CAN'T CHECK FILE SYSTEM.");
346 		return (EEXIT);
347 	case -1:
348 	clean:
349 		pwarn("clean, %ld free ", (long)(sblock.fs_cstotal.cs_nffree +
350 		    sblock.fs_frag * sblock.fs_cstotal.cs_nbfree));
351 		printf("(%jd frags, %jd blocks, %.1f%% fragmentation)\n",
352 		    (intmax_t)sblock.fs_cstotal.cs_nffree,
353 		    (intmax_t)sblock.fs_cstotal.cs_nbfree,
354 		    sblock.fs_cstotal.cs_nffree * 100.0 / sblock.fs_dsize);
355 		return (0);
356 	}
357 	/*
358 	 * Determine if we can and should do journal recovery.
359 	 */
360 	if (bkgrdflag == 0 && (sblock.fs_flags & FS_SUJ) == FS_SUJ) {
361 		if ((sblock.fs_flags & FS_NEEDSFSCK) != FS_NEEDSFSCK &&
362 		    skipclean) {
363 			sujrecovery = 1;
364 			if (suj_check(filesys) == 0) {
365 				pwarn("\n**** FILE SYSTEM MARKED CLEAN ****\n");
366 				if (chkdoreload(mntp, pwarn) == 0)
367 					exit(0);
368 				exit(4);
369 			}
370 			sujrecovery = 0;
371 			pwarn("Skipping journal, "
372 			    "falling through to full fsck\n");
373 		}
374 		if (fswritefd != -1) {
375 			/*
376 			 * Write the superblock so we don't try to recover the
377 			 * journal on another pass. If this is the only change
378 			 * to the filesystem, we do not want it to be called
379 			 * out as modified.
380 			 */
381 			sblock.fs_mtime = time(NULL);
382 			sbdirty();
383 			ofsmodified = fsmodified;
384 			flush(fswritefd, &sblk);
385 			fsmodified = ofsmodified;
386 		}
387 	}
388 	/*
389 	 * If the filesystem was run on an old kernel that did not
390 	 * support check hashes, clear the check-hash flags so that
391 	 * we do not try to verify them.
392 	 */
393 	if ((sblock.fs_flags & FS_METACKHASH) == 0)
394 		sblock.fs_metackhash = 0;
395 	/*
396 	 * If we are running on a kernel that can provide check hashes
397 	 * that are not yet enabled for the filesystem and we are
398 	 * running manually without the -y flag, offer to add any
399 	 * supported check hashes that are not already enabled.
400 	 */
401 	ckhashadd = 0;
402 	if (preen == 0 && yflag == 0 && sblock.fs_magic != FS_UFS1_MAGIC &&
403 	    fswritefd != -1 && getosreldate() >= P_OSREL_CK_CYLGRP) {
404 		if ((sblock.fs_metackhash & CK_CYLGRP) == 0 &&
405 		    reply("ADD CYLINDER GROUP CHECK-HASH PROTECTION") != 0) {
406 			ckhashadd |= CK_CYLGRP;
407 			sblock.fs_metackhash |= CK_CYLGRP;
408 		}
409 		if ((sblock.fs_metackhash & CK_SUPERBLOCK) == 0 &&
410 		    getosreldate() >= P_OSREL_CK_SUPERBLOCK &&
411 		    reply("ADD SUPERBLOCK CHECK-HASH PROTECTION") != 0) {
412 			ckhashadd |= CK_SUPERBLOCK;
413 			sblock.fs_metackhash |= CK_SUPERBLOCK;
414 		}
415 		if ((sblock.fs_metackhash & CK_INODE) == 0 &&
416 		    getosreldate() >= P_OSREL_CK_INODE &&
417 		    reply("ADD INODE CHECK-HASH PROTECTION") != 0) {
418 			ckhashadd |= CK_INODE;
419 			sblock.fs_metackhash |= CK_INODE;
420 		}
421 #ifdef notyet
422 		if ((sblock.fs_metackhash & CK_INDIR) == 0 &&
423 		    getosreldate() >= P_OSREL_CK_INDIR &&
424 		    reply("ADD INDIRECT BLOCK CHECK-HASH PROTECTION") != 0) {
425 			ckhashadd |= CK_INDIR;
426 			sblock.fs_metackhash |= CK_INDIR;
427 		}
428 		if ((sblock.fs_metackhash & CK_DIR) == 0 &&
429 		    getosreldate() >= P_OSREL_CK_DIR &&
430 		    reply("ADD DIRECTORY CHECK-HASH PROTECTION") != 0) {
431 			ckhashadd |= CK_DIR;
432 			sblock.fs_metackhash |= CK_DIR;
433 		}
434 #endif /* notyet */
435 		if (ckhashadd != 0) {
436 			sblock.fs_flags |= FS_METACKHASH;
437 			sbdirty();
438 		}
439 	}
440 	/*
441 	 * Cleared if any questions answered no. Used to decide if
442 	 * the superblock should be marked clean.
443 	 */
444 	resolved = 1;
445 	/*
446 	 * 1: scan inodes tallying blocks used
447 	 */
448 	if (preen == 0 || debug) {
449 		printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
450 		if (mntp != NULL && mntp->f_flags & MNT_ROOTFS)
451 			printf("** Root file system\n");
452 		printf("** Phase 1 - Check Blocks and Sizes\n");
453 	}
454 	clock_gettime(CLOCK_REALTIME_PRECISE, &startprog);
455 	pass1();
456 	IOstats("Pass1");
457 
458 	/*
459 	 * 1b: locate first references to duplicates, if any
460 	 */
461 	if (duplist) {
462 		if (preen || usedsoftdep)
463 			pfatal("INTERNAL ERROR: DUPS WITH %s%s%s",
464 			    preen ? "-p" : "",
465 			    (preen && usedsoftdep) ? " AND " : "",
466 			    usedsoftdep ? "SOFTUPDATES" : "");
467 		if (preen == 0 || debug)
468 			printf("** Phase 1b - Rescan For More DUPS\n");
469 		pass1b();
470 		IOstats("Pass1b");
471 	}
472 
473 	/*
474 	 * 2: traverse directories from root to mark all connected directories
475 	 */
476 	if (preen == 0 || debug)
477 		printf("** Phase 2 - Check Pathnames\n");
478 	pass2();
479 	IOstats("Pass2");
480 
481 	/*
482 	 * 3: scan inodes looking for disconnected directories
483 	 */
484 	if (preen == 0 || debug)
485 		printf("** Phase 3 - Check Connectivity\n");
486 	pass3();
487 	IOstats("Pass3");
488 
489 	/*
490 	 * 4: scan inodes looking for disconnected files; check reference counts
491 	 */
492 	if (preen == 0 || debug)
493 		printf("** Phase 4 - Check Reference Counts\n");
494 	pass4();
495 	IOstats("Pass4");
496 
497 	/*
498 	 * 5: check and repair resource counts in cylinder groups
499 	 */
500 	if (preen == 0 || debug)
501 		printf("** Phase 5 - Check Cyl groups\n");
502 	snapflush(std_checkblkavail);
503 	if (cgheader_corrupt) {
504 		printf("PHASE 5 SKIPPED DUE TO CORRUPT CYLINDER GROUP "
505 		    "HEADER(S)\n\n");
506 	} else {
507 		pass5();
508 		IOstats("Pass5");
509 	}
510 
511 	/*
512 	 * print out summary statistics
513 	 */
514 	n_ffree = sblock.fs_cstotal.cs_nffree;
515 	n_bfree = sblock.fs_cstotal.cs_nbfree;
516 	files = maxino - UFS_ROOTINO - sblock.fs_cstotal.cs_nifree - n_files;
517 	blks = n_blks +
518 	    sblock.fs_ncg * (cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
519 	blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
520 	blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
521 	blks = maxfsblock - (n_ffree + sblock.fs_frag * n_bfree) - blks;
522 	if (bkgrdflag && (files > 0 || blks > 0)) {
523 		countdirs = sblock.fs_cstotal.cs_ndir - countdirs;
524 		pwarn("Reclaimed: %ld directories, %jd files, %jd fragments\n",
525 		    countdirs, files - countdirs, blks);
526 	}
527 	pwarn("%ld files, %jd used, %ju free ",
528 	    (long)n_files, (intmax_t)n_blks,
529 	    (uintmax_t)n_ffree + sblock.fs_frag * n_bfree);
530 	printf("(%ju frags, %ju blocks, %.1f%% fragmentation)\n",
531 	    (uintmax_t)n_ffree, (uintmax_t)n_bfree,
532 	    n_ffree * 100.0 / sblock.fs_dsize);
533 	if (debug) {
534 		if (files < 0)
535 			printf("%jd inodes missing\n", -files);
536 		if (blks < 0)
537 			printf("%jd blocks missing\n", -blks);
538 		if (duplist != NULL) {
539 			printf("The following duplicate blocks remain:");
540 			for (dp = duplist; dp; dp = dp->next)
541 				printf(" %jd,", (intmax_t)dp->dup);
542 			printf("\n");
543 		}
544 	}
545 	duplist = (struct dups *)0;
546 	muldup = (struct dups *)0;
547 	inocleanup();
548 	if (fsmodified) {
549 		sblock.fs_time = time(NULL);
550 		sbdirty();
551 	}
552 	if (cvtlevel && (sblk.b_flags & B_DIRTY) != 0) {
553 		/*
554 		 * Write out the duplicate super blocks
555 		 */
556 		if (sbput(fswritefd, &sblock, sblock.fs_ncg) == 0)
557 			fsmodified = 1;
558 	}
559 	if (rerun)
560 		resolved = 0;
561 
562 	/*
563 	 * Check to see if the file system is mounted read-write.
564 	 */
565 	if (bkgrdflag == 0 && mntp != NULL && (mntp->f_flags & MNT_RDONLY) == 0)
566 		resolved = 0;
567 	ckfini(resolved);
568 
569 	if (fsmodified && !preen)
570 		printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
571 	if (rerun) {
572 		if (wantrestart && (restarts++ < 10) &&
573 		    (preen || reply("RESTART")))
574 			return (ERESTART);
575 		printf("\n***** PLEASE RERUN FSCK *****\n");
576 	}
577 	if (chkdoreload(mntp, pwarn) != 0) {
578 		if (!fsmodified)
579 			return (0);
580 		if (!preen)
581 			printf("\n***** REBOOT NOW *****\n");
582 		sync();
583 		return (4);
584 	}
585 	return (rerun ? ERERUN : 0);
586 }
587 
588 /*
589  * If we are to do a background check:
590  *	Get the mount point information of the file system
591  *	If already clean, return -1
592  *	Check that kernel supports background fsck
593  *	Find or create the snapshot directory
594  *	Create the snapshot file
595  *	Open snapshot
596  *	If anything fails print reason and return 0 which exits
597  */
598 static int
599 setup_bkgrdchk(struct statfs *mntp, int sbreadfailed, char **filesys)
600 {
601 	struct stat snapdir;
602 	struct group *grp;
603 	struct iovec *iov;
604 	char errmsg[255];
605 	int iovlen;
606 	size_t size;
607 
608 	/* Get the mount point information of the file system */
609 	if (mntp == NULL) {
610 		pwarn("NOT MOUNTED, CANNOT RUN IN BACKGROUND\n");
611 		return (0);
612 	}
613 	if ((mntp->f_flags & MNT_RDONLY) != 0) {
614 		pwarn("MOUNTED READ-ONLY, CANNOT RUN IN BACKGROUND\n");
615 		return (0);
616 	}
617 	if ((mntp->f_flags & MNT_SOFTDEP) == 0) {
618 		pwarn("NOT USING SOFT UPDATES, CANNOT RUN IN BACKGROUND\n");
619 		return (0);
620 	}
621 	if (sbreadfailed) {
622 		pwarn("SUPERBLOCK READ FAILED, CANNOT RUN IN BACKGROUND\n");
623 		return (0);
624 	}
625 	if ((sblock.fs_flags & FS_NEEDSFSCK) != 0) {
626 		pwarn("FULL FSCK NEEDED, CANNOT RUN IN BACKGROUND\n");
627 		return (0);
628 	}
629 	if (skipclean && ckclean &&
630 	   (sblock.fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK)) == 0) {
631 		/*
632 		 * file system is clean;
633 		 * skip snapshot and report it clean
634 		 */
635 		pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
636 		return (-1);
637 	}
638 	/* Check that kernel supports background fsck */
639 	size = MIBSIZE;
640 	if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0||
641 	    sysctlnametomib("vfs.ffs.adjblkcnt", adjblkcnt, &size) < 0||
642 	    sysctlnametomib("vfs.ffs.setsize", setsize, &size) < 0 ||
643 	    sysctlnametomib("vfs.ffs.freefiles", freefiles, &size) < 0||
644 	    sysctlnametomib("vfs.ffs.freedirs", freedirs, &size) < 0 ||
645 	    sysctlnametomib("vfs.ffs.freeblks", freeblks, &size) < 0) {
646 		pwarn("KERNEL LACKS BACKGROUND FSCK SUPPORT\n");
647 		return (0);
648 	}
649 	/*
650 	 * When kernel lacks runtime bgfsck superblock summary
651 	 * adjustment functionality, it does not mean we can not
652 	 * continue, as old kernels will recompute the summary at
653 	 * mount time. However, it will be an unexpected softupdates
654 	 * inconsistency if it turns out that the summary is still
655 	 * incorrect. Set a flag so subsequent operation can know this.
656 	 */
657 	bkgrdsumadj = 1;
658 	if (sysctlnametomib("vfs.ffs.adjndir", adjndir, &size) < 0 ||
659 	   sysctlnametomib("vfs.ffs.adjnbfree", adjnbfree, &size) < 0 ||
660 	   sysctlnametomib("vfs.ffs.adjnifree", adjnifree, &size) < 0 ||
661 	   sysctlnametomib("vfs.ffs.adjnffree", adjnffree, &size) < 0 ||
662 	   sysctlnametomib("vfs.ffs.adjnumclusters", adjnumclusters,
663 	   &size) < 0) {
664 		bkgrdsumadj = 0;
665 		pwarn("KERNEL LACKS RUNTIME SUPERBLOCK SUMMARY ADJUSTMENT "
666 		    "SUPPORT\n");
667 	}
668 	/* Find or create the snapshot directory */
669 	snprintf(snapname, sizeof snapname, "%s/.snap",
670 	    mntp->f_mntonname);
671 	if (stat(snapname, &snapdir) < 0) {
672 		if (errno != ENOENT) {
673 			pwarn("CANNOT FIND SNAPSHOT DIRECTORY %s: %s, CANNOT "
674 			    "RUN IN BACKGROUND\n", snapname, strerror(errno));
675 			return (0);
676 		}
677 		if ((grp = getgrnam("operator")) == NULL ||
678 			   mkdir(snapname, 0770) < 0 ||
679 			   chown(snapname, -1, grp->gr_gid) < 0 ||
680 			   chmod(snapname, 0770) < 0) {
681 			pwarn("CANNOT CREATE SNAPSHOT DIRECTORY %s: %s, "
682 			    "CANNOT RUN IN BACKGROUND\n", snapname,
683 			    strerror(errno));
684 			return (0);
685 		}
686 	} else if (!S_ISDIR(snapdir.st_mode)) {
687 		pwarn("%s IS NOT A DIRECTORY, CANNOT RUN IN BACKGROUND\n",
688 		    snapname);
689 		return (0);
690 	}
691 	/* Create the snapshot file */
692 	iov = NULL;
693 	iovlen = 0;
694 	errmsg[0] = '\0';
695 	snprintf(snapname, sizeof snapname, "%s/.snap/fsck_snapshot",
696 	    mntp->f_mntonname);
697 	build_iovec(&iov, &iovlen, "fstype", "ffs", 4);
698 	build_iovec(&iov, &iovlen, "from", snapname, (size_t)-1);
699 	build_iovec(&iov, &iovlen, "fspath", mntp->f_mntonname, (size_t)-1);
700 	build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
701 	build_iovec(&iov, &iovlen, "update", NULL, 0);
702 	build_iovec(&iov, &iovlen, "snapshot", NULL, 0);
703 	/* Create snapshot, removing old snapshot if it exists */
704 	while (nmount(iov, iovlen, mntp->f_flags) < 0) {
705 		if (errno == EEXIST && unlink(snapname) == 0)
706 			continue;
707 		pwarn("CANNOT CREATE SNAPSHOT %s: %s %s\n", snapname,
708 		    strerror(errno), errmsg);
709 		return (0);
710 	}
711 	/* Open snapshot */
712 	if (openfilesys(snapname) == 0) {
713 		unlink(snapname);
714 		pwarn("CANNOT OPEN SNAPSHOT %s: %s, CANNOT RUN IN "
715 		    "BACKGROUND\n", snapname, strerror(errno));
716 		return (0);
717 	}
718 	free(sblock.fs_csp);
719 	free(sblock.fs_si);
720 	if (readsb() == 0) {
721 		pwarn("CANNOT READ SNAPSHOT SUPERBLOCK\n");
722 		return (0);
723 	}
724 	*filesys = snapname;
725 	cmd.version = FFS_CMD_VERSION;
726 	cmd.handle = fsreadfd;
727 	return (1);
728 }
729 
730 static void
731 usage(void)
732 {
733 	(void) fprintf(stderr,
734 "usage: %s [-BCdEFfnpRrSyZ] [-b block] [-c level] [-m mode] filesystem ...\n",
735 	    getprogname());
736 	exit(1);
737 }
738 
739 void
740 infohandler(int sig __unused)
741 {
742 	got_siginfo = 1;
743 }
744 
745 void
746 alarmhandler(int sig __unused)
747 {
748 	got_sigalarm = 1;
749 }
750