main.c revision 249788
1121572Sume/*
262583Sitojun * Copyright (c) 1980, 1986, 1993
355505Sshin *	The Regents of the University of California.  All rights reserved.
455505Sshin *
555505Sshin * Redistribution and use in source and binary forms, with or without
655505Sshin * modification, are permitted provided that the following conditions
755505Sshin * are met:
855505Sshin * 1. Redistributions of source code must retain the above copyright
955505Sshin *    notice, this list of conditions and the following disclaimer.
1055505Sshin * 2. Redistributions in binary form must reproduce the above copyright
1155505Sshin *    notice, this list of conditions and the following disclaimer in the
1255505Sshin *    documentation and/or other materials provided with the distribution.
1355505Sshin * 4. Neither the name of the University nor the names of its contributors
1455505Sshin *    may be used to endorse or promote products derived from this software
1555505Sshin *    without specific prior written permission.
1655505Sshin *
1755505Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1855505Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1955505Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2055505Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2155505Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2255505Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2355505Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2455505Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2555505Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2655505Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2755505Sshin * SUCH DAMAGE.
2855505Sshin */
2955505Sshin
3055505Sshin#if 0
3155505Sshin#ifndef lint
3284208Sdillonstatic const char copyright[] =
3384208Sdillon"@(#) Copyright (c) 1980, 1986, 1993\n\
3484208Sdillon	The Regents of the University of California.  All rights reserved.\n";
3555505Sshin#endif /* not lint */
3655505Sshin
3755505Sshin#ifndef lint
3855505Sshinstatic char sccsid[] = "@(#)main.c	8.6 (Berkeley) 5/14/95";
39171135Sgnn#endif /* not lint */
4055505Sshin#endif
41171135Sgnn#include <sys/cdefs.h>
4255505Sshin__FBSDID("$FreeBSD: stable/9/sbin/fsck_ffs/main.c 249788 2013-04-23 06:28:49Z mckusick $");
4355505Sshin
4455505Sshin#include <sys/param.h>
4562583Sitojun#include <sys/file.h>
4655505Sshin#include <sys/mount.h>
4755505Sshin#include <sys/resource.h>
4855505Sshin#include <sys/stat.h>
4955505Sshin#include <sys/sysctl.h>
5055505Sshin#include <sys/uio.h>
5155505Sshin#include <sys/disklabel.h>
5255505Sshin
5355505Sshin#include <ufs/ufs/dinode.h>
5455505Sshin#include <ufs/ffs/fs.h>
5555505Sshin
5655505Sshin#include <err.h>
5755505Sshin#include <errno.h>
5855505Sshin#include <fstab.h>
5955505Sshin#include <grp.h>
6092941Sobrien#include <mntopts.h>
6192941Sobrien#include <paths.h>
6292941Sobrien#include <stdint.h>
6392917Sobrien#include <string.h>
6455505Sshin#include <time.h>
6555505Sshin
6655505Sshin#include "fsck.h"
6755505Sshin
6855505Sshinstatic void usage(void) __dead2;
6955505Sshinstatic int argtoi(int flag, const char *req, const char *str, int base);
7055505Sshinstatic int checkfilesys(char *filesys);
7155505Sshinstatic int chkdoreload(struct statfs *mntp);
7255505Sshinstatic struct statfs *getmntpt(const char *);
7355505Sshin
7455505Sshinint
7555505Sshinmain(int argc, char *argv[])
7655505Sshin{
7762583Sitojun	int ch;
7855505Sshin	struct rlimit rlimit;
7962583Sitojun	struct itimerval itimerval;
8062583Sitojun	int ret = 0;
8155505Sshin
8255505Sshin	sync();
8355505Sshin	skipclean = 1;
8455505Sshin	inoopt = 0;
8555505Sshin	while ((ch = getopt(argc, argv, "b:Bc:CdEfFm:npry")) != -1) {
8662583Sitojun		switch (ch) {
8755505Sshin		case 'b':
8855505Sshin			skipclean = 0;
8955505Sshin			bflag = argtoi('b', "number", optarg, 10);
9055505Sshin			printf("Alternate super block location: %d\n", bflag);
9155505Sshin			break;
9255505Sshin
9355505Sshin		case 'B':
9455505Sshin			bkgrdflag = 1;
9555505Sshin			break;
9655505Sshin
9755505Sshin		case 'c':
9855505Sshin			skipclean = 0;
9955505Sshin			cvtlevel = argtoi('c', "conversion level", optarg, 10);
10062583Sitojun			if (cvtlevel < 3)
10155505Sshin				errx(EEXIT, "cannot do level %d conversion",
10255505Sshin				    cvtlevel);
10355505Sshin			break;
10455505Sshin
10555505Sshin		case 'd':
10655505Sshin			debug++;
10755505Sshin			break;
10855505Sshin
10955505Sshin		case 'E':
11055505Sshin			Eflag++;
11155505Sshin			break;
11262583Sitojun
11355505Sshin		case 'f':
11455505Sshin			skipclean = 0;
11555505Sshin			break;
11655505Sshin
11755505Sshin		case 'F':
11855505Sshin			bkgrdcheck = 1;
11955505Sshin			break;
12055505Sshin
12155505Sshin		case 'm':
12262583Sitojun			lfmode = argtoi('m', "mode", optarg, 8);
12355505Sshin			if (lfmode &~ 07777)
12455505Sshin				errx(EEXIT, "bad mode to -m: %o", lfmode);
12562583Sitojun			printf("** lost+found creation mode %o\n", lfmode);
12662583Sitojun			break;
12755505Sshin
12855505Sshin		case 'n':
12962583Sitojun			nflag++;
13055505Sshin			yflag = 0;
13155505Sshin			break;
13255505Sshin
13355505Sshin		case 'p':
13462583Sitojun			preen++;
13562583Sitojun			/*FALLTHROUGH*/
13662583Sitojun
13762583Sitojun		case 'C':
13855505Sshin			ckclean++;
13955505Sshin			break;
14055505Sshin
14162583Sitojun		case 'r':
14262583Sitojun			inoopt++;
14355505Sshin			break;
14455505Sshin
14555505Sshin		case 'y':
14655505Sshin			yflag++;
14762583Sitojun			nflag = 0;
14862583Sitojun			break;
14962583Sitojun
15055505Sshin		default:
15162583Sitojun			usage();
15262583Sitojun		}
15355505Sshin	}
15455505Sshin	argc -= optind;
15555505Sshin	argv += optind;
15655505Sshin
15762583Sitojun	if (!argc)
15862583Sitojun		usage();
15962583Sitojun
16062583Sitojun	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
16155505Sshin		(void)signal(SIGINT, catch);
16255505Sshin	if (ckclean)
16355505Sshin		(void)signal(SIGQUIT, catchquit);
16462583Sitojun	signal(SIGINFO, infohandler);
165216369Skevlo	if (bkgrdflag) {
166216369Skevlo		signal(SIGALRM, alarmhandler);
16755505Sshin		itimerval.it_interval.tv_sec = 5;
16862583Sitojun		itimerval.it_interval.tv_usec = 0;
16962583Sitojun		itimerval.it_value.tv_sec = 5;
17055505Sshin		itimerval.it_value.tv_usec = 0;
17162583Sitojun		setitimer(ITIMER_REAL, &itimerval, NULL);
17262583Sitojun	}
17362583Sitojun	/*
17455505Sshin	 * Push up our allowed memory limit so we can cope
17562583Sitojun	 * with huge file systems.
17662583Sitojun	 */
17762583Sitojun	if (getrlimit(RLIMIT_DATA, &rlimit) == 0) {
17862583Sitojun		rlimit.rlim_cur = rlimit.rlim_max;
17962583Sitojun		(void)setrlimit(RLIMIT_DATA, &rlimit);
18062583Sitojun	}
18162583Sitojun	while (argc-- > 0)
18262583Sitojun		(void)checkfilesys(*argv++);
18362583Sitojun
18462583Sitojun	if (returntosingle)
18562583Sitojun		ret = 2;
18662583Sitojun	exit(ret);
18762583Sitojun}
18862583Sitojun
18962583Sitojunstatic int
19062583Sitojunargtoi(int flag, const char *req, const char *str, int base)
19162583Sitojun{
19262583Sitojun	char *cp;
19362583Sitojun	int ret;
19462583Sitojun
19562583Sitojun	ret = (int)strtol(str, &cp, base);
19662583Sitojun	if (cp == str || *cp)
19762583Sitojun		errx(EEXIT, "-%c flag requires a %s", flag, req);
19862583Sitojun	return (ret);
19962583Sitojun}
200125681Sbms
201125681Sbms/*
202264585Sbrueffer * Check the specified file system.
20362583Sitojun */
20462583Sitojun/* ARGSUSED */
20562583Sitojunstatic int
20662583Sitojuncheckfilesys(char *filesys)
20762583Sitojun{
20862583Sitojun	ufs2_daddr_t n_ffree, n_bfree;
20962583Sitojun	struct dups *dp;
21062583Sitojun	struct statfs *mntp;
21162583Sitojun	struct stat snapdir;
21262583Sitojun	struct group *grp;
21362583Sitojun	ufs2_daddr_t blks;
21462583Sitojun	struct iovec *iov;
21562583Sitojun	char errmsg[255];
21662583Sitojun	int iovlen;
21762583Sitojun	int cylno;
21862583Sitojun	ino_t files;
21962583Sitojun	size_t size;
22062583Sitojun
22162583Sitojun	iov = NULL;
22262583Sitojun	iovlen = 0;
22362583Sitojun	errmsg[0] = '\0';
22462583Sitojun
22562583Sitojun	cdevname = filesys;
22662583Sitojun	if (debug && ckclean)
22762583Sitojun		pwarn("starting\n");
22862583Sitojun	/*
22962583Sitojun	 * Make best effort to get the disk name. Check first to see
23062583Sitojun	 * if it is listed among the mounted file systems. Failing that
23162583Sitojun	 * check to see if it is listed in /etc/fstab.
23262583Sitojun	 */
23362583Sitojun	mntp = getmntpt(filesys);
23455505Sshin	if (mntp != NULL)
23555505Sshin		filesys = mntp->f_mntfromname;
23662583Sitojun	else
23762583Sitojun		filesys = blockcheck(filesys);
23862583Sitojun	/*
23962583Sitojun	 * If -F flag specified, check to see whether a background check
24062583Sitojun	 * is possible and needed. If possible and needed, exit with
24155505Sshin	 * status zero. Otherwise exit with status non-zero. A non-zero
24262583Sitojun	 * exit status will cause a foreground check to be run.
24362583Sitojun	 */
24462583Sitojun	sblock_init();
24562583Sitojun	if (bkgrdcheck) {
24662583Sitojun		if ((fsreadfd = open(filesys, O_RDONLY)) < 0 || readsb(0) == 0)
24762583Sitojun			exit(3);	/* Cannot read superblock */
24862583Sitojun		close(fsreadfd);
24962583Sitojun		/* Earlier background failed or journaled */
25062583Sitojun		if (sblock.fs_flags & (FS_NEEDSFSCK | FS_SUJ))
25162583Sitojun			exit(4);
25262583Sitojun		if ((sblock.fs_flags & FS_DOSOFTDEP) == 0)
25362583Sitojun			exit(5);	/* Not running soft updates */
25462583Sitojun		size = MIBSIZE;
25562583Sitojun		if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0)
25662583Sitojun			exit(6);	/* Lacks kernel support */
25762583Sitojun		if ((mntp == NULL && sblock.fs_clean == 1) ||
25855505Sshin		    (mntp != NULL && (sblock.fs_flags & FS_UNCLEAN) == 0))
25955505Sshin			exit(7);	/* Filesystem clean, report it now */
26062583Sitojun		exit(0);
26162583Sitojun	}
26262583Sitojun	if (ckclean && skipclean) {
26362583Sitojun		/*
26462583Sitojun		 * If file system is gjournaled, check it here.
26562583Sitojun		 */
26662583Sitojun		if ((fsreadfd = open(filesys, O_RDONLY)) < 0 || readsb(0) == 0)
26762583Sitojun			exit(3);	/* Cannot read superblock */
26862583Sitojun		close(fsreadfd);
269121572Sume		if ((sblock.fs_flags & FS_GJOURNAL) != 0) {
27062583Sitojun			//printf("GJournaled file system detected on %s.\n",
27162583Sitojun			//    filesys);
27262583Sitojun			if (sblock.fs_clean == 1) {
27355505Sshin				pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
27455505Sshin				exit(0);
27555505Sshin			}
27655505Sshin			if ((sblock.fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0) {
27762583Sitojun				gjournal_check(filesys);
27855505Sshin				if (chkdoreload(mntp) == 0)
27962583Sitojun					exit(0);
28062583Sitojun				exit(4);
28162583Sitojun			} else {
28255505Sshin				pfatal("UNEXPECTED INCONSISTENCY, %s\n",
28362583Sitojun				    "CANNOT RUN FAST FSCK\n");
28455505Sshin			}
28562583Sitojun		}
28662583Sitojun	}
28762583Sitojun	/*
28862583Sitojun	 * If we are to do a background check:
28962583Sitojun	 *	Get the mount point information of the file system
29062583Sitojun	 *	create snapshot file
29162583Sitojun	 *	return created snapshot file
29262583Sitojun	 *	if not found, clear bkgrdflag and proceed with normal fsck
29355505Sshin	 */
29462583Sitojun	if (bkgrdflag) {
29562583Sitojun		if (mntp == NULL) {
29662583Sitojun			bkgrdflag = 0;
29762583Sitojun			pfatal("NOT MOUNTED, CANNOT RUN IN BACKGROUND\n");
29862583Sitojun		} else if ((mntp->f_flags & MNT_SOFTDEP) == 0) {
29962583Sitojun			bkgrdflag = 0;
30062583Sitojun			pfatal("NOT USING SOFT UPDATES, %s\n",
30155505Sshin			    "CANNOT RUN IN BACKGROUND");
30262583Sitojun		} else if ((mntp->f_flags & MNT_RDONLY) != 0) {
30362583Sitojun			bkgrdflag = 0;
30462583Sitojun			pfatal("MOUNTED READ-ONLY, CANNOT RUN IN BACKGROUND\n");
30562583Sitojun		} else if ((fsreadfd = open(filesys, O_RDONLY)) >= 0) {
30662583Sitojun			if (readsb(0) != 0) {
30762583Sitojun				if (sblock.fs_flags & (FS_NEEDSFSCK | FS_SUJ)) {
30855505Sshin					bkgrdflag = 0;
309					pfatal("UNEXPECTED INCONSISTENCY, %s\n",
310					    "CANNOT RUN IN BACKGROUND\n");
311				}
312				if ((sblock.fs_flags & FS_UNCLEAN) == 0 &&
313				    skipclean && ckclean) {
314					/*
315					 * file system is clean;
316					 * skip snapshot and report it clean
317					 */
318					pwarn("FILE SYSTEM CLEAN; %s\n",
319					    "SKIPPING CHECKS");
320					goto clean;
321				}
322			}
323			close(fsreadfd);
324		}
325		if (bkgrdflag) {
326			snprintf(snapname, sizeof snapname, "%s/.snap",
327			    mntp->f_mntonname);
328			if (stat(snapname, &snapdir) < 0) {
329				if (errno != ENOENT) {
330					bkgrdflag = 0;
331					pfatal("CANNOT FIND %s %s: %s, %s\n",
332					    "SNAPSHOT DIRECTORY",
333					    snapname, strerror(errno),
334					    "CANNOT RUN IN BACKGROUND");
335				} else if ((grp = getgrnam("operator")) == 0 ||
336				    mkdir(snapname, 0770) < 0 ||
337				    chown(snapname, -1, grp->gr_gid) < 0 ||
338				    chmod(snapname, 0770) < 0) {
339					bkgrdflag = 0;
340					pfatal("CANNOT CREATE %s %s: %s, %s\n",
341					    "SNAPSHOT DIRECTORY",
342					    snapname, strerror(errno),
343					    "CANNOT RUN IN BACKGROUND");
344				}
345			} else if (!S_ISDIR(snapdir.st_mode)) {
346				bkgrdflag = 0;
347				pfatal("%s IS NOT A DIRECTORY, %s\n", snapname,
348				    "CANNOT RUN IN BACKGROUND");
349			}
350		}
351		if (bkgrdflag) {
352			snprintf(snapname, sizeof snapname,
353			    "%s/.snap/fsck_snapshot", mntp->f_mntonname);
354			build_iovec(&iov, &iovlen, "fstype", "ffs", 4);
355			build_iovec(&iov, &iovlen, "from", snapname,
356			    (size_t)-1);
357			build_iovec(&iov, &iovlen, "fspath", mntp->f_mntonname,
358			    (size_t)-1);
359			build_iovec(&iov, &iovlen, "errmsg", errmsg,
360			    sizeof(errmsg));
361			build_iovec(&iov, &iovlen, "update", NULL, 0);
362			build_iovec(&iov, &iovlen, "snapshot", NULL, 0);
363
364			while (nmount(iov, iovlen, mntp->f_flags) < 0) {
365				if (errno == EEXIST && unlink(snapname) == 0)
366					continue;
367				bkgrdflag = 0;
368				pfatal("CANNOT CREATE SNAPSHOT %s: %s %s\n",
369				    snapname, strerror(errno), errmsg);
370				break;
371			}
372			if (bkgrdflag != 0)
373				filesys = snapname;
374		}
375	}
376
377	switch (setup(filesys)) {
378	case 0:
379		if (preen)
380			pfatal("CAN'T CHECK FILE SYSTEM.");
381		return (0);
382	case -1:
383	clean:
384		pwarn("clean, %ld free ", (long)(sblock.fs_cstotal.cs_nffree +
385		    sblock.fs_frag * sblock.fs_cstotal.cs_nbfree));
386		printf("(%lld frags, %lld blocks, %.1f%% fragmentation)\n",
387		    (long long)sblock.fs_cstotal.cs_nffree,
388		    (long long)sblock.fs_cstotal.cs_nbfree,
389		    sblock.fs_cstotal.cs_nffree * 100.0 / sblock.fs_dsize);
390		return (0);
391	}
392	/*
393	 * Determine if we can and should do journal recovery.
394	 */
395	if ((sblock.fs_flags & FS_SUJ) == FS_SUJ) {
396		if ((sblock.fs_flags & FS_NEEDSFSCK) != FS_NEEDSFSCK && skipclean) {
397			if (preen || reply("USE JOURNAL")) {
398				if (suj_check(filesys) == 0) {
399					printf("\n***** FILE SYSTEM MARKED CLEAN *****\n");
400					if (chkdoreload(mntp) == 0)
401						exit(0);
402					exit(4);
403				}
404			}
405			printf("** Skipping journal, falling through to full fsck\n\n");
406		}
407		/*
408		 * Write the superblock so we don't try to recover the
409		 * journal on another pass.
410		 */
411		sblock.fs_mtime = time(NULL);
412		sbdirty();
413	}
414
415	/*
416	 * Cleared if any questions answered no. Used to decide if
417	 * the superblock should be marked clean.
418	 */
419	resolved = 1;
420	/*
421	 * 1: scan inodes tallying blocks used
422	 */
423	if (preen == 0) {
424		printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
425		if (mntp != NULL && mntp->f_flags & MNT_ROOTFS)
426			printf("** Root file system\n");
427		printf("** Phase 1 - Check Blocks and Sizes\n");
428	}
429	clock_gettime(CLOCK_REALTIME_PRECISE, &startprog);
430	pass1();
431	IOstats("Pass1");
432
433	/*
434	 * 1b: locate first references to duplicates, if any
435	 */
436	if (duplist) {
437		if (preen || usedsoftdep)
438			pfatal("INTERNAL ERROR: dups with %s%s%s",
439			    preen ? "-p" : "",
440			    (preen && usedsoftdep) ? " and " : "",
441			    usedsoftdep ? "softupdates" : "");
442		printf("** Phase 1b - Rescan For More DUPS\n");
443		pass1b();
444		IOstats("Pass1b");
445	}
446
447	/*
448	 * 2: traverse directories from root to mark all connected directories
449	 */
450	if (preen == 0)
451		printf("** Phase 2 - Check Pathnames\n");
452	pass2();
453	IOstats("Pass2");
454
455	/*
456	 * 3: scan inodes looking for disconnected directories
457	 */
458	if (preen == 0)
459		printf("** Phase 3 - Check Connectivity\n");
460	pass3();
461	IOstats("Pass3");
462
463	/*
464	 * 4: scan inodes looking for disconnected files; check reference counts
465	 */
466	if (preen == 0)
467		printf("** Phase 4 - Check Reference Counts\n");
468	pass4();
469	IOstats("Pass4");
470
471	/*
472	 * 5: check and repair resource counts in cylinder groups
473	 */
474	if (preen == 0)
475		printf("** Phase 5 - Check Cyl groups\n");
476	pass5();
477	IOstats("Pass5");
478
479	/*
480	 * print out summary statistics
481	 */
482	n_ffree = sblock.fs_cstotal.cs_nffree;
483	n_bfree = sblock.fs_cstotal.cs_nbfree;
484	files = maxino - ROOTINO - sblock.fs_cstotal.cs_nifree - n_files;
485	blks = n_blks +
486	    sblock.fs_ncg * (cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
487	blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
488	blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
489	blks = maxfsblock - (n_ffree + sblock.fs_frag * n_bfree) - blks;
490	if (bkgrdflag && (files > 0 || blks > 0)) {
491		countdirs = sblock.fs_cstotal.cs_ndir - countdirs;
492		pwarn("Reclaimed: %ld directories, %ld files, %lld fragments\n",
493		    countdirs, (long)files - countdirs, (long long)blks);
494	}
495	pwarn("%ld files, %jd used, %ju free ",
496	    (long)n_files, (intmax_t)n_blks,
497	    (uintmax_t)n_ffree + sblock.fs_frag * n_bfree);
498	printf("(%ju frags, %ju blocks, %.1f%% fragmentation)\n",
499	    (uintmax_t)n_ffree, (uintmax_t)n_bfree,
500	    n_ffree * 100.0 / sblock.fs_dsize);
501	if (debug) {
502		if (files < 0)
503			printf("%d inodes missing\n", -files);
504		if (blks < 0)
505			printf("%lld blocks missing\n", -(long long)blks);
506		if (duplist != NULL) {
507			printf("The following duplicate blocks remain:");
508			for (dp = duplist; dp; dp = dp->next)
509				printf(" %lld,", (long long)dp->dup);
510			printf("\n");
511		}
512	}
513	duplist = (struct dups *)0;
514	muldup = (struct dups *)0;
515	inocleanup();
516	if (fsmodified) {
517		sblock.fs_time = time(NULL);
518		sbdirty();
519	}
520	if (cvtlevel && sblk.b_dirty) {
521		/*
522		 * Write out the duplicate super blocks
523		 */
524		for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
525			blwrite(fswritefd, (char *)&sblock,
526			    fsbtodb(&sblock, cgsblock(&sblock, cylno)),
527			    SBLOCKSIZE);
528	}
529	if (rerun)
530		resolved = 0;
531	finalIOstats();
532
533	/*
534	 * Check to see if the file system is mounted read-write.
535	 */
536	if (bkgrdflag == 0 && mntp != NULL && (mntp->f_flags & MNT_RDONLY) == 0)
537		resolved = 0;
538	ckfini(resolved);
539
540	for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
541		if (inostathead[cylno].il_stat != NULL)
542			free((char *)inostathead[cylno].il_stat);
543	free((char *)inostathead);
544	inostathead = NULL;
545	if (fsmodified && !preen)
546		printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
547	if (rerun)
548		printf("\n***** PLEASE RERUN FSCK *****\n");
549	if (chkdoreload(mntp) != 0) {
550		if (!fsmodified)
551			return (0);
552		if (!preen)
553			printf("\n***** REBOOT NOW *****\n");
554		sync();
555		return (4);
556	}
557	return (0);
558}
559
560static int
561chkdoreload(struct statfs *mntp)
562{
563	struct iovec *iov;
564	int iovlen;
565	char errmsg[255];
566
567	if (mntp == NULL)
568		return (0);
569
570	iov = NULL;
571	iovlen = 0;
572	errmsg[0] = '\0';
573	/*
574	 * We modified a mounted file system.  Do a mount update on
575	 * it unless it is read-write, so we can continue using it
576	 * as safely as possible.
577	 */
578	if (mntp->f_flags & MNT_RDONLY) {
579		build_iovec(&iov, &iovlen, "fstype", "ffs", 4);
580		build_iovec(&iov, &iovlen, "from", mntp->f_mntfromname,
581		    (size_t)-1);
582		build_iovec(&iov, &iovlen, "fspath", mntp->f_mntonname,
583		    (size_t)-1);
584		build_iovec(&iov, &iovlen, "errmsg", errmsg,
585		    sizeof(errmsg));
586		build_iovec(&iov, &iovlen, "update", NULL, 0);
587		build_iovec(&iov, &iovlen, "reload", NULL, 0);
588		/*
589		 * XX: We need the following line until we clean up
590		 * nmount parsing of root mounts and NFS root mounts.
591		 */
592		build_iovec(&iov, &iovlen, "ro", NULL, 0);
593		if (nmount(iov, iovlen, mntp->f_flags) == 0) {
594			return (0);
595		}
596		pwarn("mount reload of '%s' failed: %s %s\n\n",
597		    mntp->f_mntonname, strerror(errno), errmsg);
598		return (1);
599	}
600	return (0);
601}
602
603/*
604 * Get the mount point information for name.
605 */
606static struct statfs *
607getmntpt(const char *name)
608{
609	struct stat devstat, mntdevstat;
610	char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
611	char *ddevname;
612	struct statfs *mntbuf, *statfsp;
613	int i, mntsize, isdev;
614
615	if (stat(name, &devstat) != 0)
616		return (NULL);
617	if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))
618		isdev = 1;
619	else
620		isdev = 0;
621	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
622	for (i = 0; i < mntsize; i++) {
623		statfsp = &mntbuf[i];
624		ddevname = statfsp->f_mntfromname;
625		if (*ddevname != '/') {
626			strcpy(device, _PATH_DEV);
627			strcat(device, ddevname);
628			strcpy(statfsp->f_mntfromname, device);
629		}
630		if (isdev == 0) {
631			if (strcmp(name, statfsp->f_mntonname))
632				continue;
633			return (statfsp);
634		}
635		if (stat(ddevname, &mntdevstat) == 0 &&
636		    mntdevstat.st_rdev == devstat.st_rdev)
637			return (statfsp);
638	}
639	statfsp = NULL;
640	return (statfsp);
641}
642
643static void
644usage(void)
645{
646	(void) fprintf(stderr,
647	    "usage: %s [-BEFfnpry] [-b block] [-c level] [-m mode] "
648			"filesystem ...\n",
649	    getprogname());
650	exit(1);
651}
652