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