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