xref: /freebsd/sbin/fsck_ffs/main.c (revision 058ac3e8063366dafa634d9107642e12b038bf09)
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)
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 	snapflush(std_checkblkavail);
495 	pass5();
496 	IOstats("Pass5");
497 
498 	/*
499 	 * print out summary statistics
500 	 */
501 	n_ffree = sblock.fs_cstotal.cs_nffree;
502 	n_bfree = sblock.fs_cstotal.cs_nbfree;
503 	files = maxino - UFS_ROOTINO - sblock.fs_cstotal.cs_nifree - n_files;
504 	blks = n_blks +
505 	    sblock.fs_ncg * (cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
506 	blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
507 	blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
508 	blks = maxfsblock - (n_ffree + sblock.fs_frag * n_bfree) - blks;
509 	if (bkgrdflag && (files > 0 || blks > 0)) {
510 		countdirs = sblock.fs_cstotal.cs_ndir - countdirs;
511 		pwarn("Reclaimed: %ld directories, %jd files, %jd fragments\n",
512 		    countdirs, files - countdirs, blks);
513 	}
514 	pwarn("%ld files, %jd used, %ju free ",
515 	    (long)n_files, (intmax_t)n_blks,
516 	    (uintmax_t)n_ffree + sblock.fs_frag * n_bfree);
517 	printf("(%ju frags, %ju blocks, %.1f%% fragmentation)\n",
518 	    (uintmax_t)n_ffree, (uintmax_t)n_bfree,
519 	    n_ffree * 100.0 / sblock.fs_dsize);
520 	if (debug) {
521 		if (files < 0)
522 			printf("%jd inodes missing\n", -files);
523 		if (blks < 0)
524 			printf("%jd blocks missing\n", -blks);
525 		if (duplist != NULL) {
526 			printf("The following duplicate blocks remain:");
527 			for (dp = duplist; dp; dp = dp->next)
528 				printf(" %jd,", (intmax_t)dp->dup);
529 			printf("\n");
530 		}
531 	}
532 	duplist = (struct dups *)0;
533 	muldup = (struct dups *)0;
534 	inocleanup();
535 	if (fsmodified) {
536 		sblock.fs_time = time(NULL);
537 		sbdirty();
538 	}
539 	if (cvtlevel && (sblk.b_flags & B_DIRTY) != 0) {
540 		/*
541 		 * Write out the duplicate super blocks
542 		 */
543 		if (sbput(fswritefd, &sblock, sblock.fs_ncg) == 0)
544 			fsmodified = 1;
545 	}
546 	if (rerun)
547 		resolved = 0;
548 
549 	/*
550 	 * Check to see if the file system is mounted read-write.
551 	 */
552 	if (bkgrdflag == 0 && mntp != NULL && (mntp->f_flags & MNT_RDONLY) == 0)
553 		resolved = 0;
554 	ckfini(resolved);
555 
556 	if (fsmodified && !preen)
557 		printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
558 	if (rerun) {
559 		if (wantrestart && (restarts++ < 10) &&
560 		    (preen || reply("RESTART")))
561 			return (ERESTART);
562 		printf("\n***** PLEASE RERUN FSCK *****\n");
563 	}
564 	if (chkdoreload(mntp) != 0) {
565 		if (!fsmodified)
566 			return (0);
567 		if (!preen)
568 			printf("\n***** REBOOT NOW *****\n");
569 		sync();
570 		return (4);
571 	}
572 	return (rerun ? ERERUN : 0);
573 }
574 
575 /*
576  * If we are to do a background check:
577  *	Get the mount point information of the file system
578  *	If already clean, return -1
579  *	Check that kernel supports background fsck
580  *	Find or create the snapshot directory
581  *	Create the snapshot file
582  *	Open snapshot
583  *	If anything fails print reason and return 0 which exits
584  */
585 static int
586 setup_bkgrdchk(struct statfs *mntp, int sbreadfailed, char **filesys)
587 {
588 	struct stat snapdir;
589 	struct group *grp;
590 	struct iovec *iov;
591 	char errmsg[255];
592 	int iovlen;
593 	size_t size;
594 
595 	/* Get the mount point information of the file system */
596 	if (mntp == NULL) {
597 		pwarn("NOT MOUNTED, CANNOT RUN IN BACKGROUND\n");
598 		return (0);
599 	}
600 	if ((mntp->f_flags & MNT_RDONLY) != 0) {
601 		pwarn("MOUNTED READ-ONLY, CANNOT RUN IN BACKGROUND\n");
602 		return (0);
603 	}
604 	if ((mntp->f_flags & MNT_SOFTDEP) == 0) {
605 		pwarn("NOT USING SOFT UPDATES, CANNOT RUN IN BACKGROUND\n");
606 		return (0);
607 	}
608 	if (sbreadfailed) {
609 		pwarn("SUPERBLOCK READ FAILED, CANNOT RUN IN BACKGROUND\n");
610 		return (0);
611 	}
612 	if ((sblock.fs_flags & FS_NEEDSFSCK) != 0) {
613 		pwarn("FULL FSCK NEEDED, CANNOT RUN IN BACKGROUND\n");
614 		return (0);
615 	}
616 	if ((sblock.fs_flags & FS_SUJ) != 0) {
617 		pwarn("JOURNALED FILESYSTEM, CANNOT RUN IN BACKGROUND\n");
618 		return (0);
619 	}
620 	if (skipclean && ckclean &&
621 	   (sblock.fs_flags & (FS_UNCLEAN|FS_NEEDSFSCK)) == 0) {
622 		/*
623 		 * file system is clean;
624 		 * skip snapshot and report it clean
625 		 */
626 		pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
627 		return (-1);
628 	}
629 	/* Check that kernel supports background fsck */
630 	size = MIBSIZE;
631 	if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0||
632 	    sysctlnametomib("vfs.ffs.adjblkcnt", adjblkcnt, &size) < 0||
633 	    sysctlnametomib("vfs.ffs.setsize", setsize, &size) < 0 ||
634 	    sysctlnametomib("vfs.ffs.freefiles", freefiles, &size) < 0||
635 	    sysctlnametomib("vfs.ffs.freedirs", freedirs, &size) < 0 ||
636 	    sysctlnametomib("vfs.ffs.freeblks", freeblks, &size) < 0) {
637 		pwarn("KERNEL LACKS BACKGROUND FSCK SUPPORT\n");
638 		return (0);
639 	}
640 	/*
641 	 * When kernel lacks runtime bgfsck superblock summary
642 	 * adjustment functionality, it does not mean we can not
643 	 * continue, as old kernels will recompute the summary at
644 	 * mount time. However, it will be an unexpected softupdates
645 	 * inconsistency if it turns out that the summary is still
646 	 * incorrect. Set a flag so subsequent operation can know this.
647 	 */
648 	bkgrdsumadj = 1;
649 	if (sysctlnametomib("vfs.ffs.adjndir", adjndir, &size) < 0 ||
650 	   sysctlnametomib("vfs.ffs.adjnbfree", adjnbfree, &size) < 0 ||
651 	   sysctlnametomib("vfs.ffs.adjnifree", adjnifree, &size) < 0 ||
652 	   sysctlnametomib("vfs.ffs.adjnffree", adjnffree, &size) < 0 ||
653 	   sysctlnametomib("vfs.ffs.adjnumclusters", adjnumclusters,
654 	   &size) < 0) {
655 		bkgrdsumadj = 0;
656 		pwarn("KERNEL LACKS RUNTIME SUPERBLOCK SUMMARY ADJUSTMENT "
657 		    "SUPPORT\n");
658 	}
659 	/* Find or create the snapshot directory */
660 	snprintf(snapname, sizeof snapname, "%s/.snap",
661 	    mntp->f_mntonname);
662 	if (stat(snapname, &snapdir) < 0) {
663 		if (errno != ENOENT) {
664 			pwarn("CANNOT FIND SNAPSHOT DIRECTORY %s: %s, CANNOT "
665 			    "RUN IN BACKGROUND\n", snapname, strerror(errno));
666 			return (0);
667 		}
668 		if ((grp = getgrnam("operator")) == NULL ||
669 			   mkdir(snapname, 0770) < 0 ||
670 			   chown(snapname, -1, grp->gr_gid) < 0 ||
671 			   chmod(snapname, 0770) < 0) {
672 			pwarn("CANNOT CREATE SNAPSHOT DIRECTORY %s: %s, "
673 			    "CANNOT RUN IN BACKGROUND\n", snapname,
674 			    strerror(errno));
675 			return (0);
676 		}
677 	} else if (!S_ISDIR(snapdir.st_mode)) {
678 		pwarn("%s IS NOT A DIRECTORY, CANNOT RUN IN BACKGROUND\n",
679 		    snapname);
680 		return (0);
681 	}
682 	/* Create the snapshot file */
683 	iov = NULL;
684 	iovlen = 0;
685 	errmsg[0] = '\0';
686 	snprintf(snapname, sizeof snapname, "%s/.snap/fsck_snapshot",
687 	    mntp->f_mntonname);
688 	build_iovec(&iov, &iovlen, "fstype", "ffs", 4);
689 	build_iovec(&iov, &iovlen, "from", snapname, (size_t)-1);
690 	build_iovec(&iov, &iovlen, "fspath", mntp->f_mntonname, (size_t)-1);
691 	build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
692 	build_iovec(&iov, &iovlen, "update", NULL, 0);
693 	build_iovec(&iov, &iovlen, "snapshot", NULL, 0);
694 	/* Create snapshot, removing old snapshot if it exists */
695 	while (nmount(iov, iovlen, mntp->f_flags) < 0) {
696 		if (errno == EEXIST && unlink(snapname) == 0)
697 			continue;
698 		pwarn("CANNOT CREATE SNAPSHOT %s: %s %s\n", snapname,
699 		    strerror(errno), errmsg);
700 		return (0);
701 	}
702 	/* Open snapshot */
703 	if (openfilesys(snapname) == 0) {
704 		unlink(snapname);
705 		pwarn("CANNOT OPEN SNAPSHOT %s: %s, CANNOT RUN IN "
706 		    "BACKGROUND\n", snapname, strerror(errno));
707 		return (0);
708 	}
709 	free(sblock.fs_csp);
710 	free(sblock.fs_si);
711 	havesb = 0;
712 	*filesys = snapname;
713 	cmd.version = FFS_CMD_VERSION;
714 	cmd.handle = fsreadfd;
715 	return (1);
716 }
717 
718 static int
719 chkdoreload(struct statfs *mntp)
720 {
721 	struct iovec *iov;
722 	int iovlen;
723 	char errmsg[255];
724 
725 	if (mntp == NULL)
726 		return (0);
727 
728 	iov = NULL;
729 	iovlen = 0;
730 	errmsg[0] = '\0';
731 	/*
732 	 * We modified a mounted file system.  Do a mount update on
733 	 * it unless it is read-write, so we can continue using it
734 	 * as safely as possible.
735 	 */
736 	if (mntp->f_flags & MNT_RDONLY) {
737 		build_iovec(&iov, &iovlen, "fstype", "ffs", 4);
738 		build_iovec(&iov, &iovlen, "from", mntp->f_mntfromname,
739 		    (size_t)-1);
740 		build_iovec(&iov, &iovlen, "fspath", mntp->f_mntonname,
741 		    (size_t)-1);
742 		build_iovec(&iov, &iovlen, "errmsg", errmsg,
743 		    sizeof(errmsg));
744 		build_iovec(&iov, &iovlen, "update", NULL, 0);
745 		build_iovec(&iov, &iovlen, "reload", NULL, 0);
746 		/*
747 		 * XX: We need the following line until we clean up
748 		 * nmount parsing of root mounts and NFS root mounts.
749 		 */
750 		build_iovec(&iov, &iovlen, "ro", NULL, 0);
751 		if (nmount(iov, iovlen, mntp->f_flags) == 0) {
752 			return (0);
753 		}
754 		pwarn("mount reload of '%s' failed: %s %s\n\n",
755 		    mntp->f_mntonname, strerror(errno), errmsg);
756 		return (1);
757 	}
758 	return (0);
759 }
760 
761 /*
762  * Get the mount point information for name.
763  */
764 static struct statfs *
765 getmntpt(const char *name)
766 {
767 	struct stat devstat, mntdevstat;
768 	char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
769 	char *ddevname;
770 	struct statfs *mntbuf, *statfsp;
771 	int i, mntsize, isdev;
772 
773 	if (stat(name, &devstat) != 0)
774 		return (NULL);
775 	if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))
776 		isdev = 1;
777 	else
778 		isdev = 0;
779 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
780 	for (i = 0; i < mntsize; i++) {
781 		statfsp = &mntbuf[i];
782 		ddevname = statfsp->f_mntfromname;
783 		if (*ddevname != '/') {
784 			if (strlen(_PATH_DEV) + strlen(ddevname) + 1 >
785 			    sizeof(statfsp->f_mntfromname))
786 				continue;
787 			strcpy(device, _PATH_DEV);
788 			strcat(device, ddevname);
789 			strcpy(statfsp->f_mntfromname, device);
790 		}
791 		if (isdev == 0) {
792 			if (strcmp(name, statfsp->f_mntonname))
793 				continue;
794 			return (statfsp);
795 		}
796 		if (stat(ddevname, &mntdevstat) == 0 &&
797 		    mntdevstat.st_rdev == devstat.st_rdev)
798 			return (statfsp);
799 	}
800 	statfsp = NULL;
801 	return (statfsp);
802 }
803 
804 static void
805 usage(void)
806 {
807 	(void) fprintf(stderr,
808 "usage: %s [-BCdEFfnpRrSyZ] [-b block] [-c level] [-m mode] filesystem ...\n",
809 	    getprogname());
810 	exit(1);
811 }
812 
813 void
814 infohandler(int sig __unused)
815 {
816 	got_siginfo = 1;
817 }
818 
819 void
820 alarmhandler(int sig __unused)
821 {
822 	got_sigalarm = 1;
823 }
824