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