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