xref: /freebsd/sbin/fsck_ffs/main.c (revision bc7512cc58af2e8bbe5bbf5ca0059b1daa1da897)
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	IN_RTLD			/* So we pickup the P_OSREL defines */
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 static int chkdoreload(struct statfs *mntp);
80 static struct statfs *getmntpt(const char *);
81 
82 int
83 main(int argc, char *argv[])
84 {
85 	int ch;
86 	struct rlimit rlimit;
87 	struct itimerval itimerval;
88 	int fsret;
89 	int ret = 0;
90 
91 	sync();
92 	skipclean = 1;
93 	inoopt = 0;
94 	while ((ch = getopt(argc, argv, "b:Bc:CdEfFm:npRrSyZz")) != -1) {
95 		switch (ch) {
96 		case 'b':
97 			skipclean = 0;
98 			bflag = argtoimax('b', "number", optarg, 10);
99 			printf("Alternate super block location: %jd\n", bflag);
100 			break;
101 
102 		case 'B':
103 			bkgrdflag = 1;
104 			break;
105 
106 		case 'c':
107 			skipclean = 0;
108 			cvtlevel = argtoimax('c', "conversion level", optarg,
109 			    10);
110 			if (cvtlevel < 3)
111 				errx(EEXIT, "cannot do level %d conversion",
112 				    cvtlevel);
113 			break;
114 
115 		case 'd':
116 			debug++;
117 			break;
118 
119 		case 'E':
120 			Eflag++;
121 			break;
122 
123 		case 'f':
124 			skipclean = 0;
125 			break;
126 
127 		case 'F':
128 			bkgrdcheck = 1;
129 			break;
130 
131 		case 'm':
132 			lfmode = argtoimax('m', "mode", optarg, 8);
133 			if (lfmode &~ 07777)
134 				errx(EEXIT, "bad mode to -m: %o", lfmode);
135 			printf("** lost+found creation mode %o\n", lfmode);
136 			break;
137 
138 		case 'n':
139 			nflag++;
140 			yflag = 0;
141 			break;
142 
143 		case 'p':
144 			preen++;
145 			/*FALLTHROUGH*/
146 
147 		case 'C':
148 			ckclean++;
149 			break;
150 
151 		case 'R':
152 			wantrestart = 1;
153 			break;
154 		case 'r':
155 			inoopt++;
156 			break;
157 
158 		case 'S':
159 			surrender = 1;
160 			break;
161 
162 		case 'y':
163 			yflag++;
164 			nflag = 0;
165 			break;
166 
167 		case 'Z':
168 			Zflag++;
169 			break;
170 
171 		case 'z':
172 			zflag++;
173 			break;
174 
175 		default:
176 			usage();
177 		}
178 	}
179 	argc -= optind;
180 	argv += optind;
181 
182 	if (!argc)
183 		usage();
184 
185 	if (bkgrdflag && cvtlevel > 0) {
186 		pfatal("CANNOT CONVERT A SNAPSHOT\n");
187 		exit(EEXIT);
188 	}
189 
190 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
191 		(void)signal(SIGINT, catch);
192 	if (ckclean)
193 		(void)signal(SIGQUIT, catchquit);
194 	signal(SIGINFO, infohandler);
195 	if (bkgrdflag) {
196 		signal(SIGALRM, alarmhandler);
197 		itimerval.it_interval.tv_sec = 5;
198 		itimerval.it_interval.tv_usec = 0;
199 		itimerval.it_value.tv_sec = 5;
200 		itimerval.it_value.tv_usec = 0;
201 		setitimer(ITIMER_REAL, &itimerval, NULL);
202 	}
203 	/*
204 	 * Push up our allowed memory limit so we can cope
205 	 * with huge file systems.
206 	 */
207 	if (getrlimit(RLIMIT_DATA, &rlimit) == 0) {
208 		rlimit.rlim_cur = rlimit.rlim_max;
209 		(void)setrlimit(RLIMIT_DATA, &rlimit);
210 	}
211 	while (argc > 0) {
212 		if ((fsret = checkfilesys(*argv)) == ERESTART)
213 			continue;
214 		ret |= fsret;
215 		argc--;
216 		argv++;
217 	}
218 
219 	if (returntosingle)
220 		ret = 2;
221 	exit(ret);
222 }
223 
224 static intmax_t
225 argtoimax(int flag, const char *req, const char *str, int base)
226 {
227 	char *cp;
228 	intmax_t ret;
229 
230 	ret = strtoimax(str, &cp, base);
231 	if (cp == str || *cp)
232 		errx(EEXIT, "-%c flag requires a %s", flag, req);
233 	return (ret);
234 }
235 
236 /*
237  * Check the specified file system.
238  */
239 /* ARGSUSED */
240 static int
241 checkfilesys(char *filesys)
242 {
243 	ufs2_daddr_t n_ffree, n_bfree;
244 	struct dups *dp;
245 	struct statfs *mntp;
246 	intmax_t blks, files;
247 	size_t size;
248 	int sbreadfailed, ofsmodified;
249 
250 	fsutilinit();
251 	fsckinit();
252 
253 	cdevname = filesys;
254 	if (debug && ckclean)
255 		pwarn("starting\n");
256 	/*
257 	 * Make best effort to get the disk name. Check first to see
258 	 * if it is listed among the mounted file systems. Failing that
259 	 * check to see if it is listed in /etc/fstab.
260 	 */
261 	mntp = getmntpt(filesys);
262 	if (mntp != NULL)
263 		filesys = mntp->f_mntfromname;
264 	else
265 		filesys = blockcheck(filesys);
266 	/*
267 	 * If -F flag specified, check to see whether a background check
268 	 * is possible and needed. If possible and needed, exit with
269 	 * status zero. Otherwise exit with status non-zero. A non-zero
270 	 * exit status will cause a foreground check to be run.
271 	 */
272 	sblock_init();
273 	sbreadfailed = 0;
274 	if (openfilesys(filesys) == 0 || readsb(0) == 0)
275 		sbreadfailed = 1;
276 	if (bkgrdcheck) {
277 		if (sbreadfailed)
278 			exit(3);	/* Cannot read superblock */
279 		/* Earlier background failed or journaled */
280 		if (sblock.fs_flags & (FS_NEEDSFSCK | FS_SUJ))
281 			exit(4);
282 		if ((sblock.fs_flags & FS_DOSOFTDEP) == 0)
283 			exit(5);	/* Not running soft updates */
284 		size = MIBSIZE;
285 		if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0)
286 			exit(6);	/* Lacks kernel support */
287 		if ((mntp == NULL && sblock.fs_clean == 1) ||
288 		    (mntp != NULL && (sblock.fs_flags & FS_UNCLEAN) == 0))
289 			exit(7);	/* Filesystem clean, report it now */
290 		exit(0);
291 	}
292 	if (ckclean && skipclean) {
293 		/*
294 		 * If file system is gjournaled, check it here.
295 		 */
296 		if (sbreadfailed)
297 			exit(3);	/* Cannot read superblock */
298 		if (bkgrdflag == 0 &&
299 		    (nflag || (fswritefd = open(filesys, O_WRONLY)) < 0)) {
300 			fswritefd = -1;
301 			if (preen)
302 				pfatal("NO WRITE ACCESS");
303 			printf(" (NO WRITE)");
304 		}
305 		if ((sblock.fs_flags & FS_GJOURNAL) != 0) {
306 			if (sblock.fs_clean == 1) {
307 				pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
308 				exit(0);
309 			}
310 			if ((sblock.fs_flags &
311 			    (FS_UNCLEAN | FS_NEEDSFSCK)) == 0) {
312 				bufinit();
313 				gjournal_check(filesys);
314 				if (chkdoreload(mntp) == 0)
315 					exit(0);
316 				exit(4);
317 			} else {
318 				pfatal("FULL FSCK NEEDED, CANNOT RUN FAST "
319 				    "FSCK\n");
320 			}
321 		}
322 		close(fswritefd);
323 		fswritefd = -1;
324 	}
325 	if (bkgrdflag) {
326 		switch (setup_bkgrdchk(mntp, sbreadfailed, &filesys)) {
327 		case -1: /* filesystem clean */
328 			goto clean;
329 		case 0: /* cannot do background, give up */
330 			exit(EEXIT);
331 		case 1: /* doing background check, preen rules apply */
332 			preen = 1;
333 			break;
334 		}
335 	}
336 
337 	switch (setup(filesys)) {
338 	case 0:
339 		if (preen)
340 			pfatal("CAN'T CHECK FILE SYSTEM.");
341 		return (EEXIT);
342 	case -1:
343 	clean:
344 		pwarn("clean, %ld free ", (long)(sblock.fs_cstotal.cs_nffree +
345 		    sblock.fs_frag * sblock.fs_cstotal.cs_nbfree));
346 		printf("(%jd frags, %jd blocks, %.1f%% fragmentation)\n",
347 		    (intmax_t)sblock.fs_cstotal.cs_nffree,
348 		    (intmax_t)sblock.fs_cstotal.cs_nbfree,
349 		    sblock.fs_cstotal.cs_nffree * 100.0 / sblock.fs_dsize);
350 		return (0);
351 	}
352 	/*
353 	 * Determine if we can and should do journal recovery.
354 	 */
355 	if ((sblock.fs_flags & FS_SUJ) == FS_SUJ) {
356 		if ((sblock.fs_flags & FS_NEEDSFSCK) != FS_NEEDSFSCK && skipclean) {
357 			sujrecovery = 1;
358 			if (suj_check(filesys) == 0) {
359 				printf("\n***** FILE SYSTEM MARKED CLEAN *****\n");
360 				if (chkdoreload(mntp) == 0)
361 					exit(0);
362 				exit(4);
363 			}
364 			sujrecovery = 0;
365 			printf("** Skipping journal, falling through to full fsck\n\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) {
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 		printf("** Phase 1b - Rescan For More DUPS\n");
461 		pass1b();
462 		IOstats("Pass1b");
463 	}
464 
465 	/*
466 	 * 2: traverse directories from root to mark all connected directories
467 	 */
468 	if (preen == 0)
469 		printf("** Phase 2 - Check Pathnames\n");
470 	pass2();
471 	IOstats("Pass2");
472 
473 	/*
474 	 * 3: scan inodes looking for disconnected directories
475 	 */
476 	if (preen == 0)
477 		printf("** Phase 3 - Check Connectivity\n");
478 	pass3();
479 	IOstats("Pass3");
480 
481 	/*
482 	 * 4: scan inodes looking for disconnected files; check reference counts
483 	 */
484 	if (preen == 0)
485 		printf("** Phase 4 - Check Reference Counts\n");
486 	pass4();
487 	IOstats("Pass4");
488 
489 	/*
490 	 * 5: check and repair resource counts in cylinder groups
491 	 */
492 	if (preen == 0)
493 		printf("** Phase 5 - Check Cyl groups\n");
494 	pass5();
495 	IOstats("Pass5");
496 
497 	/*
498 	 * print out summary statistics
499 	 */
500 	n_ffree = sblock.fs_cstotal.cs_nffree;
501 	n_bfree = sblock.fs_cstotal.cs_nbfree;
502 	files = maxino - UFS_ROOTINO - sblock.fs_cstotal.cs_nifree - n_files;
503 	blks = n_blks +
504 	    sblock.fs_ncg * (cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
505 	blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
506 	blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
507 	blks = maxfsblock - (n_ffree + sblock.fs_frag * n_bfree) - blks;
508 	if (bkgrdflag && (files > 0 || blks > 0)) {
509 		countdirs = sblock.fs_cstotal.cs_ndir - countdirs;
510 		pwarn("Reclaimed: %ld directories, %jd files, %jd fragments\n",
511 		    countdirs, files - countdirs, blks);
512 	}
513 	pwarn("%ld files, %jd used, %ju free ",
514 	    (long)n_files, (intmax_t)n_blks,
515 	    (uintmax_t)n_ffree + sblock.fs_frag * n_bfree);
516 	printf("(%ju frags, %ju blocks, %.1f%% fragmentation)\n",
517 	    (uintmax_t)n_ffree, (uintmax_t)n_bfree,
518 	    n_ffree * 100.0 / sblock.fs_dsize);
519 	if (debug) {
520 		if (files < 0)
521 			printf("%jd inodes missing\n", -files);
522 		if (blks < 0)
523 			printf("%jd blocks missing\n", -blks);
524 		if (duplist != NULL) {
525 			printf("The following duplicate blocks remain:");
526 			for (dp = duplist; dp; dp = dp->next)
527 				printf(" %jd,", (intmax_t)dp->dup);
528 			printf("\n");
529 		}
530 	}
531 	duplist = (struct dups *)0;
532 	muldup = (struct dups *)0;
533 	inocleanup();
534 	if (fsmodified) {
535 		sblock.fs_time = time(NULL);
536 		sbdirty();
537 	}
538 	if (cvtlevel && (sblk.b_flags & B_DIRTY) != 0) {
539 		/*
540 		 * Write out the duplicate super blocks
541 		 */
542 		if (sbput(fswritefd, &sblock, sblock.fs_ncg) == 0)
543 			fsmodified = 1;
544 	}
545 	if (rerun)
546 		resolved = 0;
547 
548 	/*
549 	 * Check to see if the file system is mounted read-write.
550 	 */
551 	if (bkgrdflag == 0 && mntp != NULL && (mntp->f_flags & MNT_RDONLY) == 0)
552 		resolved = 0;
553 	ckfini(resolved);
554 
555 	if (fsmodified && !preen)
556 		printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
557 	if (rerun) {
558 		if (wantrestart && (restarts++ < 10) &&
559 		    (preen || reply("RESTART")))
560 			return (ERESTART);
561 		printf("\n***** PLEASE RERUN FSCK *****\n");
562 	}
563 	if (chkdoreload(mntp) != 0) {
564 		if (!fsmodified)
565 			return (0);
566 		if (!preen)
567 			printf("\n***** REBOOT NOW *****\n");
568 		sync();
569 		return (4);
570 	}
571 	return (rerun ? ERERUN : 0);
572 }
573 
574 /*
575  * If we are to do a background check:
576  *	Get the mount point information of the file system
577  *	If already clean, return -1
578  *	Check that kernel supports background fsck
579  *	Find or create the snapshot directory
580  *	Create the snapshot file
581  *	Open snapshot
582  *	If anything fails print reason and return 0 which exits
583  */
584 static int
585 setup_bkgrdchk(struct statfs *mntp, int sbreadfailed, char **filesys)
586 {
587 	struct stat snapdir;
588 	struct group *grp;
589 	struct iovec *iov;
590 	char errmsg[255];
591 	int iovlen;
592 	size_t size;
593 
594 	/* Get the mount point information of the file system */
595 	if (mntp == NULL) {
596 		pwarn("NOT MOUNTED, CANNOT RUN IN BACKGROUND\n");
597 		return (0);
598 	}
599 	if ((mntp->f_flags & MNT_RDONLY) != 0) {
600 		pwarn("MOUNTED READ-ONLY, CANNOT RUN IN BACKGROUND\n");
601 		return (0);
602 	}
603 	if ((mntp->f_flags & MNT_SOFTDEP) == 0) {
604 		pwarn("NOT USING SOFT UPDATES, CANNOT RUN IN BACKGROUND\n");
605 		return (0);
606 	}
607 	if (sbreadfailed) {
608 		pwarn("SUPERBLOCK READ FAILED, CANNOT RUN IN BACKGROUND\n");
609 		return (0);
610 	}
611 	if ((sblock.fs_flags & FS_NEEDSFSCK) != 0) {
612 		pwarn("FULL FSCK NEEDED, CANNOT RUN IN BACKGROUND\n");
613 		return (0);
614 	}
615 	if ((sblock.fs_flags & FS_SUJ) != 0) {
616 		pwarn("JOURNALED FILESYSTEM, CANNOT RUN IN BACKGROUND\n");
617 		return (0);
618 	}
619 	if (skipclean && ckclean &&
620 	   (sblock.fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK)) == 0) {
621 		/*
622 		 * file system is clean;
623 		 * skip snapshot and report it clean
624 		 */
625 		pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
626 		return (-1);
627 	}
628 	/* Check that kernel supports background fsck */
629 	size = MIBSIZE;
630 	if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0||
631 	    sysctlnametomib("vfs.ffs.adjblkcnt", adjblkcnt, &size) < 0||
632 	    sysctlnametomib("vfs.ffs.setsize", setsize, &size) < 0 ||
633 	    sysctlnametomib("vfs.ffs.freefiles", freefiles, &size) < 0||
634 	    sysctlnametomib("vfs.ffs.freedirs", freedirs, &size) < 0 ||
635 	    sysctlnametomib("vfs.ffs.freeblks", freeblks, &size) < 0) {
636 		pwarn("KERNEL LACKS BACKGROUND FSCK SUPPORT\n");
637 		return (0);
638 	}
639 	/*
640 	 * When kernel lacks runtime bgfsck superblock summary
641 	 * adjustment functionality, it does not mean we can not
642 	 * continue, as old kernels will recompute the summary at
643 	 * mount time. However, it will be an unexpected softupdates
644 	 * inconsistency if it turns out that the summary is still
645 	 * incorrect. Set a flag so subsequent operation can know this.
646 	 */
647 	bkgrdsumadj = 1;
648 	if (sysctlnametomib("vfs.ffs.adjndir", adjndir, &size) < 0 ||
649 	   sysctlnametomib("vfs.ffs.adjnbfree", adjnbfree, &size) < 0 ||
650 	   sysctlnametomib("vfs.ffs.adjnifree", adjnifree, &size) < 0 ||
651 	   sysctlnametomib("vfs.ffs.adjnffree", adjnffree, &size) < 0 ||
652 	   sysctlnametomib("vfs.ffs.adjnumclusters", adjnumclusters,
653 	   &size) < 0) {
654 		bkgrdsumadj = 0;
655 		pwarn("KERNEL LACKS RUNTIME SUPERBLOCK SUMMARY ADJUSTMENT "
656 		    "SUPPORT\n");
657 	}
658 	/* Find or create the snapshot directory */
659 	snprintf(snapname, sizeof snapname, "%s/.snap",
660 	    mntp->f_mntonname);
661 	if (stat(snapname, &snapdir) < 0) {
662 		if (errno != ENOENT) {
663 			pwarn("CANNOT FIND SNAPSHOT DIRECTORY %s: %s, CANNOT "
664 			    "RUN IN BACKGROUND\n", snapname, strerror(errno));
665 			return (0);
666 		}
667 		if ((grp = getgrnam("operator")) == NULL ||
668 			   mkdir(snapname, 0770) < 0 ||
669 			   chown(snapname, -1, grp->gr_gid) < 0 ||
670 			   chmod(snapname, 0770) < 0) {
671 			pwarn("CANNOT CREATE SNAPSHOT DIRECTORY %s: %s, "
672 			    "CANNOT RUN IN BACKGROUND\n", snapname,
673 			    strerror(errno));
674 			return (0);
675 		}
676 	} else if (!S_ISDIR(snapdir.st_mode)) {
677 		pwarn("%s IS NOT A DIRECTORY, CANNOT RUN IN BACKGROUND\n",
678 		    snapname);
679 		return (0);
680 	}
681 	/* Create the snapshot file */
682 	iov = NULL;
683 	iovlen = 0;
684 	errmsg[0] = '\0';
685 	snprintf(snapname, sizeof snapname, "%s/.snap/fsck_snapshot",
686 	    mntp->f_mntonname);
687 	build_iovec(&iov, &iovlen, "fstype", "ffs", 4);
688 	build_iovec(&iov, &iovlen, "from", snapname, (size_t)-1);
689 	build_iovec(&iov, &iovlen, "fspath", mntp->f_mntonname, (size_t)-1);
690 	build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
691 	build_iovec(&iov, &iovlen, "update", NULL, 0);
692 	build_iovec(&iov, &iovlen, "snapshot", NULL, 0);
693 	/* Create snapshot, removing old snapshot if it exists */
694 	while (nmount(iov, iovlen, mntp->f_flags) < 0) {
695 		if (errno == EEXIST && unlink(snapname) == 0)
696 			continue;
697 		pwarn("CANNOT CREATE SNAPSHOT %s: %s %s\n", snapname,
698 		    strerror(errno), errmsg);
699 		return (0);
700 	}
701 	/* Open snapshot */
702 	if (openfilesys(snapname) == 0) {
703 		unlink(snapname);
704 		pwarn("CANNOT OPEN SNAPSHOT %s: %s, CANNOT RUN IN "
705 		    "BACKGROUND\n", snapname, strerror(errno));
706 		return (0);
707 	}
708 	free(sblock.fs_csp);
709 	free(sblock.fs_si);
710 	havesb = 0;
711 	*filesys = snapname;
712 	cmd.version = FFS_CMD_VERSION;
713 	cmd.handle = fsreadfd;
714 	return (1);
715 }
716 
717 static int
718 chkdoreload(struct statfs *mntp)
719 {
720 	struct iovec *iov;
721 	int iovlen;
722 	char errmsg[255];
723 
724 	if (mntp == NULL)
725 		return (0);
726 
727 	iov = NULL;
728 	iovlen = 0;
729 	errmsg[0] = '\0';
730 	/*
731 	 * We modified a mounted file system.  Do a mount update on
732 	 * it unless it is read-write, so we can continue using it
733 	 * as safely as possible.
734 	 */
735 	if (mntp->f_flags & MNT_RDONLY) {
736 		build_iovec(&iov, &iovlen, "fstype", "ffs", 4);
737 		build_iovec(&iov, &iovlen, "from", mntp->f_mntfromname,
738 		    (size_t)-1);
739 		build_iovec(&iov, &iovlen, "fspath", mntp->f_mntonname,
740 		    (size_t)-1);
741 		build_iovec(&iov, &iovlen, "errmsg", errmsg,
742 		    sizeof(errmsg));
743 		build_iovec(&iov, &iovlen, "update", NULL, 0);
744 		build_iovec(&iov, &iovlen, "reload", NULL, 0);
745 		/*
746 		 * XX: We need the following line until we clean up
747 		 * nmount parsing of root mounts and NFS root mounts.
748 		 */
749 		build_iovec(&iov, &iovlen, "ro", NULL, 0);
750 		if (nmount(iov, iovlen, mntp->f_flags) == 0) {
751 			return (0);
752 		}
753 		pwarn("mount reload of '%s' failed: %s %s\n\n",
754 		    mntp->f_mntonname, strerror(errno), errmsg);
755 		return (1);
756 	}
757 	return (0);
758 }
759 
760 /*
761  * Get the mount point information for name.
762  */
763 static struct statfs *
764 getmntpt(const char *name)
765 {
766 	struct stat devstat, mntdevstat;
767 	char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
768 	char *ddevname;
769 	struct statfs *mntbuf, *statfsp;
770 	int i, mntsize, isdev;
771 
772 	if (stat(name, &devstat) != 0)
773 		return (NULL);
774 	if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))
775 		isdev = 1;
776 	else
777 		isdev = 0;
778 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
779 	for (i = 0; i < mntsize; i++) {
780 		statfsp = &mntbuf[i];
781 		ddevname = statfsp->f_mntfromname;
782 		if (*ddevname != '/') {
783 			if (strlen(_PATH_DEV) + strlen(ddevname) + 1 >
784 			    sizeof(statfsp->f_mntfromname))
785 				continue;
786 			strcpy(device, _PATH_DEV);
787 			strcat(device, ddevname);
788 			strcpy(statfsp->f_mntfromname, device);
789 		}
790 		if (isdev == 0) {
791 			if (strcmp(name, statfsp->f_mntonname))
792 				continue;
793 			return (statfsp);
794 		}
795 		if (stat(ddevname, &mntdevstat) == 0 &&
796 		    mntdevstat.st_rdev == devstat.st_rdev)
797 			return (statfsp);
798 	}
799 	statfsp = NULL;
800 	return (statfsp);
801 }
802 
803 static void
804 usage(void)
805 {
806 	(void) fprintf(stderr,
807 "usage: %s [-BCdEFfnpRrSyZ] [-b block] [-c level] [-m mode] filesystem ...\n",
808 	    getprogname());
809 	exit(1);
810 }
811 
812 void
813 infohandler(int sig __unused)
814 {
815 	got_siginfo = 1;
816 }
817 
818 void
819 alarmhandler(int sig __unused)
820 {
821 	got_sigalarm = 1;
822 }
823