xref: /freebsd/sbin/fsck_ffs/main.c (revision a0409676120c1e558d0ade943019934e0f15118d)
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 	int cylno;
247 	intmax_t blks, files;
248 	size_t size;
249 
250 	iov = NULL;
251 	iovlen = 0;
252 	errmsg[0] = '\0';
253 	fsutilinit();
254 	fsckinit();
255 
256 	cdevname = filesys;
257 	if (debug && ckclean)
258 		pwarn("starting\n");
259 	/*
260 	 * Make best effort to get the disk name. Check first to see
261 	 * if it is listed among the mounted file systems. Failing that
262 	 * check to see if it is listed in /etc/fstab.
263 	 */
264 	mntp = getmntpt(filesys);
265 	if (mntp != NULL)
266 		filesys = mntp->f_mntfromname;
267 	else
268 		filesys = blockcheck(filesys);
269 	/*
270 	 * If -F flag specified, check to see whether a background check
271 	 * is possible and needed. If possible and needed, exit with
272 	 * status zero. Otherwise exit with status non-zero. A non-zero
273 	 * exit status will cause a foreground check to be run.
274 	 */
275 	sblock_init();
276 	if (bkgrdcheck) {
277 		if ((fsreadfd = open(filesys, O_RDONLY)) < 0 || readsb(0) == 0)
278 			exit(3);	/* Cannot read superblock */
279 		close(fsreadfd);
280 		/* Earlier background failed or journaled */
281 		if (sblock.fs_flags & (FS_NEEDSFSCK | FS_SUJ))
282 			exit(4);
283 		if ((sblock.fs_flags & FS_DOSOFTDEP) == 0)
284 			exit(5);	/* Not running soft updates */
285 		size = MIBSIZE;
286 		if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0)
287 			exit(6);	/* Lacks kernel support */
288 		if ((mntp == NULL && sblock.fs_clean == 1) ||
289 		    (mntp != NULL && (sblock.fs_flags & FS_UNCLEAN) == 0))
290 			exit(7);	/* Filesystem clean, report it now */
291 		exit(0);
292 	}
293 	if (ckclean && skipclean) {
294 		/*
295 		 * If file system is gjournaled, check it here.
296 		 */
297 		if ((fsreadfd = open(filesys, O_RDONLY)) < 0 || readsb(0) == 0)
298 			exit(3);	/* Cannot read superblock */
299 		if (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 			//printf("GJournaled file system detected on %s.\n",
307 			//    filesys);
308 			if (sblock.fs_clean == 1) {
309 				pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
310 				exit(0);
311 			}
312 			if ((sblock.fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0) {
313 				gjournal_check(filesys);
314 				if (chkdoreload(mntp) == 0)
315 					exit(0);
316 				exit(4);
317 			} else {
318 				pfatal(
319 			    "UNEXPECTED INCONSISTENCY, CANNOT RUN FAST FSCK\n");
320 				close(fsreadfd);
321 				close(fswritefd);
322 			}
323 		}
324 	}
325 	/*
326 	 * If we are to do a background check:
327 	 *	Get the mount point information of the file system
328 	 *	create snapshot file
329 	 *	return created snapshot file
330 	 *	if not found, clear bkgrdflag and proceed with normal fsck
331 	 */
332 	if (bkgrdflag) {
333 		if (mntp == NULL) {
334 			bkgrdflag = 0;
335 			pfatal("NOT MOUNTED, CANNOT RUN IN BACKGROUND\n");
336 		} else if ((mntp->f_flags & MNT_SOFTDEP) == 0) {
337 			bkgrdflag = 0;
338 			pfatal(
339 			  "NOT USING SOFT UPDATES, CANNOT RUN IN BACKGROUND\n");
340 		} else if ((mntp->f_flags & MNT_RDONLY) != 0) {
341 			bkgrdflag = 0;
342 			pfatal("MOUNTED READ-ONLY, CANNOT RUN IN BACKGROUND\n");
343 		} else if ((fsreadfd = open(filesys, O_RDONLY)) >= 0) {
344 			if (readsb(0) != 0) {
345 				if (sblock.fs_flags & (FS_NEEDSFSCK | FS_SUJ)) {
346 					bkgrdflag = 0;
347 					pfatal(
348 			"UNEXPECTED INCONSISTENCY, CANNOT RUN IN BACKGROUND\n");
349 				}
350 				if ((sblock.fs_flags & FS_UNCLEAN) == 0 &&
351 				    skipclean && ckclean) {
352 					/*
353 					 * file system is clean;
354 					 * skip snapshot and report it clean
355 					 */
356 					pwarn(
357 					"FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
358 					goto clean;
359 				}
360 			}
361 			close(fsreadfd);
362 		}
363 		if (bkgrdflag) {
364 			snprintf(snapname, sizeof snapname, "%s/.snap",
365 			    mntp->f_mntonname);
366 			if (stat(snapname, &snapdir) < 0) {
367 				if (errno != ENOENT) {
368 					bkgrdflag = 0;
369 					pfatal(
370 	"CANNOT FIND SNAPSHOT DIRECTORY %s: %s, CANNOT RUN IN BACKGROUND\n",
371 					    snapname, strerror(errno));
372 				} else if ((grp = getgrnam("operator")) == NULL ||
373 					   mkdir(snapname, 0770) < 0 ||
374 					   chown(snapname, -1, grp->gr_gid) < 0 ||
375 					   chmod(snapname, 0770) < 0) {
376 					bkgrdflag = 0;
377 					pfatal(
378 	"CANNOT CREATE SNAPSHOT DIRECTORY %s: %s, CANNOT RUN IN BACKGROUND\n",
379 					    snapname, strerror(errno));
380 				}
381 			} else if (!S_ISDIR(snapdir.st_mode)) {
382 				bkgrdflag = 0;
383 				pfatal(
384 			"%s IS NOT A DIRECTORY, CANNOT RUN IN BACKGROUND\n",
385 				    snapname);
386 			}
387 		}
388 		if (bkgrdflag) {
389 			snprintf(snapname, sizeof snapname,
390 			    "%s/.snap/fsck_snapshot", mntp->f_mntonname);
391 			build_iovec(&iov, &iovlen, "fstype", "ffs", 4);
392 			build_iovec(&iov, &iovlen, "from", snapname,
393 			    (size_t)-1);
394 			build_iovec(&iov, &iovlen, "fspath", mntp->f_mntonname,
395 			    (size_t)-1);
396 			build_iovec(&iov, &iovlen, "errmsg", errmsg,
397 			    sizeof(errmsg));
398 			build_iovec(&iov, &iovlen, "update", NULL, 0);
399 			build_iovec(&iov, &iovlen, "snapshot", NULL, 0);
400 
401 			while (nmount(iov, iovlen, mntp->f_flags) < 0) {
402 				if (errno == EEXIST && unlink(snapname) == 0)
403 					continue;
404 				bkgrdflag = 0;
405 				pfatal("CANNOT CREATE SNAPSHOT %s: %s %s\n",
406 				    snapname, strerror(errno), errmsg);
407 				break;
408 			}
409 			if (bkgrdflag != 0)
410 				filesys = snapname;
411 		}
412 	}
413 
414 	switch (setup(filesys)) {
415 	case 0:
416 		if (preen)
417 			pfatal("CAN'T CHECK FILE SYSTEM.");
418 		return (EEXIT);
419 	case -1:
420 	clean:
421 		pwarn("clean, %ld free ", (long)(sblock.fs_cstotal.cs_nffree +
422 		    sblock.fs_frag * sblock.fs_cstotal.cs_nbfree));
423 		printf("(%jd frags, %jd blocks, %.1f%% fragmentation)\n",
424 		    (intmax_t)sblock.fs_cstotal.cs_nffree,
425 		    (intmax_t)sblock.fs_cstotal.cs_nbfree,
426 		    sblock.fs_cstotal.cs_nffree * 100.0 / sblock.fs_dsize);
427 		return (0);
428 	}
429 	/*
430 	 * Determine if we can and should do journal recovery.
431 	 */
432 	if ((sblock.fs_flags & FS_SUJ) == FS_SUJ) {
433 		if ((sblock.fs_flags & FS_NEEDSFSCK) != FS_NEEDSFSCK && skipclean) {
434 			sujrecovery = 1;
435 			if (suj_check(filesys) == 0) {
436 				printf("\n***** FILE SYSTEM MARKED CLEAN *****\n");
437 				if (chkdoreload(mntp) == 0)
438 					exit(0);
439 				exit(4);
440 			}
441 			sujrecovery = 0;
442 			printf("** Skipping journal, falling through to full fsck\n\n");
443 		}
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 	 * If the filesystem was run on an old kernel that did not
458 	 * support check hashes, clear the check-hash flags so that
459 	 * we do not try to verify them.
460 	 */
461 	if ((sblock.fs_flags & FS_METACKHASH) == 0)
462 		sblock.fs_metackhash = 0;
463 	/*
464 	 * If we are running on a kernel that can provide check hashes
465 	 * that are not yet enabled for the filesystem and we are
466 	 * running manually without the -y flag, offer to add any
467 	 * supported check hashes that are not already enabled.
468 	 */
469 	ckhashadd = 0;
470 	if (preen == 0 && yflag == 0 && sblock.fs_magic != FS_UFS1_MAGIC &&
471 	    fswritefd != -1 && getosreldate() >= P_OSREL_CK_CYLGRP) {
472 		if ((sblock.fs_metackhash & CK_CYLGRP) == 0 &&
473 		    reply("ADD CYLINDER GROUP CHECK-HASH PROTECTION") != 0) {
474 			ckhashadd |= CK_CYLGRP;
475 			sblock.fs_metackhash |= CK_CYLGRP;
476 		}
477 		if ((sblock.fs_metackhash & CK_SUPERBLOCK) == 0 &&
478 		    getosreldate() >= P_OSREL_CK_SUPERBLOCK &&
479 		    reply("ADD SUPERBLOCK CHECK-HASH PROTECTION") != 0) {
480 			ckhashadd |= CK_SUPERBLOCK;
481 			sblock.fs_metackhash |= CK_SUPERBLOCK;
482 		}
483 		if ((sblock.fs_metackhash & CK_INODE) == 0 &&
484 		    getosreldate() >= P_OSREL_CK_INODE &&
485 		    reply("ADD INODE CHECK-HASH PROTECTION") != 0) {
486 			ckhashadd |= CK_INODE;
487 			sblock.fs_metackhash |= CK_INODE;
488 		}
489 #ifdef notyet
490 		if ((sblock.fs_metackhash & CK_INDIR) == 0 &&
491 		    getosreldate() >= P_OSREL_CK_INDIR &&
492 		    reply("ADD INDIRECT BLOCK CHECK-HASH PROTECTION") != 0) {
493 			ckhashadd |= CK_INDIR;
494 			sblock.fs_metackhash |= CK_INDIR;
495 		}
496 		if ((sblock.fs_metackhash & CK_DIR) == 0 &&
497 		    getosreldate() >= P_OSREL_CK_DIR &&
498 		    reply("ADD DIRECTORY CHECK-HASH PROTECTION") != 0) {
499 			ckhashadd |= CK_DIR;
500 			sblock.fs_metackhash |= CK_DIR;
501 		}
502 #endif /* notyet */
503 		if (ckhashadd != 0) {
504 			sblock.fs_flags |= FS_METACKHASH;
505 			sbdirty();
506 		}
507 	}
508 	/*
509 	 * Cleared if any questions answered no. Used to decide if
510 	 * the superblock should be marked clean.
511 	 */
512 	resolved = 1;
513 	/*
514 	 * 1: scan inodes tallying blocks used
515 	 */
516 	if (preen == 0) {
517 		printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
518 		if (mntp != NULL && mntp->f_flags & MNT_ROOTFS)
519 			printf("** Root file system\n");
520 		printf("** Phase 1 - Check Blocks and Sizes\n");
521 	}
522 	clock_gettime(CLOCK_REALTIME_PRECISE, &startprog);
523 	pass1();
524 	IOstats("Pass1");
525 
526 	/*
527 	 * 1b: locate first references to duplicates, if any
528 	 */
529 	if (duplist) {
530 		if (preen || usedsoftdep)
531 			pfatal("INTERNAL ERROR: dups with %s%s%s",
532 			    preen ? "-p" : "",
533 			    (preen && usedsoftdep) ? " and " : "",
534 			    usedsoftdep ? "softupdates" : "");
535 		printf("** Phase 1b - Rescan For More DUPS\n");
536 		pass1b();
537 		IOstats("Pass1b");
538 	}
539 
540 	/*
541 	 * 2: traverse directories from root to mark all connected directories
542 	 */
543 	if (preen == 0)
544 		printf("** Phase 2 - Check Pathnames\n");
545 	pass2();
546 	IOstats("Pass2");
547 
548 	/*
549 	 * 3: scan inodes looking for disconnected directories
550 	 */
551 	if (preen == 0)
552 		printf("** Phase 3 - Check Connectivity\n");
553 	pass3();
554 	IOstats("Pass3");
555 
556 	/*
557 	 * 4: scan inodes looking for disconnected files; check reference counts
558 	 */
559 	if (preen == 0)
560 		printf("** Phase 4 - Check Reference Counts\n");
561 	pass4();
562 	IOstats("Pass4");
563 
564 	/*
565 	 * 5: check and repair resource counts in cylinder groups
566 	 */
567 	if (preen == 0)
568 		printf("** Phase 5 - Check Cyl groups\n");
569 	pass5();
570 	IOstats("Pass5");
571 
572 	/*
573 	 * print out summary statistics
574 	 */
575 	n_ffree = sblock.fs_cstotal.cs_nffree;
576 	n_bfree = sblock.fs_cstotal.cs_nbfree;
577 	files = maxino - UFS_ROOTINO - sblock.fs_cstotal.cs_nifree - n_files;
578 	blks = n_blks +
579 	    sblock.fs_ncg * (cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
580 	blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
581 	blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
582 	blks = maxfsblock - (n_ffree + sblock.fs_frag * n_bfree) - blks;
583 	if (bkgrdflag && (files > 0 || blks > 0)) {
584 		countdirs = sblock.fs_cstotal.cs_ndir - countdirs;
585 		pwarn("Reclaimed: %ld directories, %jd files, %jd fragments\n",
586 		    countdirs, files - countdirs, blks);
587 	}
588 	pwarn("%ld files, %jd used, %ju free ",
589 	    (long)n_files, (intmax_t)n_blks,
590 	    (uintmax_t)n_ffree + sblock.fs_frag * n_bfree);
591 	printf("(%ju frags, %ju blocks, %.1f%% fragmentation)\n",
592 	    (uintmax_t)n_ffree, (uintmax_t)n_bfree,
593 	    n_ffree * 100.0 / sblock.fs_dsize);
594 	if (debug) {
595 		if (files < 0)
596 			printf("%jd inodes missing\n", -files);
597 		if (blks < 0)
598 			printf("%jd blocks missing\n", -blks);
599 		if (duplist != NULL) {
600 			printf("The following duplicate blocks remain:");
601 			for (dp = duplist; dp; dp = dp->next)
602 				printf(" %jd,", (intmax_t)dp->dup);
603 			printf("\n");
604 		}
605 	}
606 	duplist = (struct dups *)0;
607 	muldup = (struct dups *)0;
608 	inocleanup();
609 	if (fsmodified) {
610 		sblock.fs_time = time(NULL);
611 		sbdirty();
612 	}
613 	if (cvtlevel && (sblk.b_flags & B_DIRTY) != 0) {
614 		/*
615 		 * Write out the duplicate super blocks
616 		 */
617 		if (sbput(fswritefd, &sblock, sblock.fs_ncg) == 0)
618 			fsmodified = 1;
619 	}
620 	if (rerun)
621 		resolved = 0;
622 
623 	/*
624 	 * Check to see if the file system is mounted read-write.
625 	 */
626 	if (bkgrdflag == 0 && mntp != NULL && (mntp->f_flags & MNT_RDONLY) == 0)
627 		resolved = 0;
628 	ckfini(resolved);
629 
630 	for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
631 		if (inostathead[cylno].il_stat != NULL)
632 			free((char *)inostathead[cylno].il_stat);
633 	free((char *)inostathead);
634 	inostathead = NULL;
635 	if (fsmodified && !preen)
636 		printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
637 	if (rerun) {
638 		if (wantrestart && (restarts++ < 10) &&
639 		    (preen || reply("RESTART")))
640 			return (ERESTART);
641 		printf("\n***** PLEASE RERUN FSCK *****\n");
642 	}
643 	if (chkdoreload(mntp) != 0) {
644 		if (!fsmodified)
645 			return (0);
646 		if (!preen)
647 			printf("\n***** REBOOT NOW *****\n");
648 		sync();
649 		return (4);
650 	}
651 	return (rerun ? ERERUN : 0);
652 }
653 
654 static int
655 chkdoreload(struct statfs *mntp)
656 {
657 	struct iovec *iov;
658 	int iovlen;
659 	char errmsg[255];
660 
661 	if (mntp == NULL)
662 		return (0);
663 
664 	iov = NULL;
665 	iovlen = 0;
666 	errmsg[0] = '\0';
667 	/*
668 	 * We modified a mounted file system.  Do a mount update on
669 	 * it unless it is read-write, so we can continue using it
670 	 * as safely as possible.
671 	 */
672 	if (mntp->f_flags & MNT_RDONLY) {
673 		build_iovec(&iov, &iovlen, "fstype", "ffs", 4);
674 		build_iovec(&iov, &iovlen, "from", mntp->f_mntfromname,
675 		    (size_t)-1);
676 		build_iovec(&iov, &iovlen, "fspath", mntp->f_mntonname,
677 		    (size_t)-1);
678 		build_iovec(&iov, &iovlen, "errmsg", errmsg,
679 		    sizeof(errmsg));
680 		build_iovec(&iov, &iovlen, "update", NULL, 0);
681 		build_iovec(&iov, &iovlen, "reload", NULL, 0);
682 		/*
683 		 * XX: We need the following line until we clean up
684 		 * nmount parsing of root mounts and NFS root mounts.
685 		 */
686 		build_iovec(&iov, &iovlen, "ro", NULL, 0);
687 		if (nmount(iov, iovlen, mntp->f_flags) == 0) {
688 			return (0);
689 		}
690 		pwarn("mount reload of '%s' failed: %s %s\n\n",
691 		    mntp->f_mntonname, strerror(errno), errmsg);
692 		return (1);
693 	}
694 	return (0);
695 }
696 
697 /*
698  * Get the mount point information for name.
699  */
700 static struct statfs *
701 getmntpt(const char *name)
702 {
703 	struct stat devstat, mntdevstat;
704 	char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
705 	char *ddevname;
706 	struct statfs *mntbuf, *statfsp;
707 	int i, mntsize, isdev;
708 
709 	if (stat(name, &devstat) != 0)
710 		return (NULL);
711 	if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))
712 		isdev = 1;
713 	else
714 		isdev = 0;
715 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
716 	for (i = 0; i < mntsize; i++) {
717 		statfsp = &mntbuf[i];
718 		ddevname = statfsp->f_mntfromname;
719 		if (*ddevname != '/') {
720 			if (strlen(_PATH_DEV) + strlen(ddevname) + 1 >
721 			    sizeof(statfsp->f_mntfromname))
722 				continue;
723 			strcpy(device, _PATH_DEV);
724 			strcat(device, ddevname);
725 			strcpy(statfsp->f_mntfromname, device);
726 		}
727 		if (isdev == 0) {
728 			if (strcmp(name, statfsp->f_mntonname))
729 				continue;
730 			return (statfsp);
731 		}
732 		if (stat(ddevname, &mntdevstat) == 0 &&
733 		    mntdevstat.st_rdev == devstat.st_rdev)
734 			return (statfsp);
735 	}
736 	statfsp = NULL;
737 	return (statfsp);
738 }
739 
740 static void
741 usage(void)
742 {
743 	(void) fprintf(stderr,
744 "usage: %s [-BCdEFfnpRrSyZ] [-b block] [-c level] [-m mode] filesystem ...\n",
745 	    getprogname());
746 	exit(1);
747 }
748 
749 void
750 infohandler(int sig __unused)
751 {
752 	got_siginfo = 1;
753 }
754 
755 void
756 alarmhandler(int sig __unused)
757 {
758 	got_sigalarm = 1;
759 }
760