xref: /freebsd/sbin/fsck_ffs/main.c (revision 6db798cae4e1313dd0ef9b2bcaceca5ee73c75fd)
18fae3551SRodney W. Grimes /*
28fae3551SRodney W. Grimes  * Copyright (c) 1980, 1986, 1993
38fae3551SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
48fae3551SRodney W. Grimes  *
58fae3551SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
68fae3551SRodney W. Grimes  * modification, are permitted provided that the following conditions
78fae3551SRodney W. Grimes  * are met:
88fae3551SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
98fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
108fae3551SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
118fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
128fae3551SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
138fae3551SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
148fae3551SRodney W. Grimes  *    must display the following acknowledgement:
158fae3551SRodney W. Grimes  *	This product includes software developed by the University of
168fae3551SRodney W. Grimes  *	California, Berkeley and its contributors.
178fae3551SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
188fae3551SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
198fae3551SRodney W. Grimes  *    without specific prior written permission.
208fae3551SRodney W. Grimes  *
218fae3551SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
228fae3551SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
238fae3551SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
248fae3551SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
258fae3551SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
268fae3551SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
278fae3551SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
288fae3551SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
298fae3551SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
308fae3551SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
318fae3551SRodney W. Grimes  * SUCH DAMAGE.
328fae3551SRodney W. Grimes  */
338fae3551SRodney W. Grimes 
348fae3551SRodney W. Grimes #ifndef lint
3531f4ab50SBruce Evans static const char copyright[] =
368fae3551SRodney W. Grimes "@(#) Copyright (c) 1980, 1986, 1993\n\
378fae3551SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
388fae3551SRodney W. Grimes #endif /* not lint */
398fae3551SRodney W. Grimes 
408fae3551SRodney W. Grimes #ifndef lint
416b100474SJulian Elischer #if 0
426b100474SJulian Elischer static char sccsid[] = "@(#)main.c	8.6 (Berkeley) 5/14/95";
436b100474SJulian Elischer #endif
446b100474SJulian Elischer static const char rcsid[] =
457f3dea24SPeter Wemm   "$FreeBSD$";
468fae3551SRodney W. Grimes #endif /* not lint */
478fae3551SRodney W. Grimes 
488fae3551SRodney W. Grimes #include <sys/param.h>
49571b29eeSPeter Wemm #include <sys/stat.h>
508fae3551SRodney W. Grimes #include <sys/time.h>
518fae3551SRodney W. Grimes #include <sys/mount.h>
5233966081SMatt Jacob #include <sys/resource.h>
53780a5c1eSPeter Wemm 
548fae3551SRodney W. Grimes #include <ufs/ufs/dinode.h>
55780a5c1eSPeter Wemm #include <ufs/ufs/ufsmount.h>
568fae3551SRodney W. Grimes #include <ufs/ffs/fs.h>
57780a5c1eSPeter Wemm 
58780a5c1eSPeter Wemm #include <err.h>
59d0e1503bSPeter Wemm #include <errno.h>
608fae3551SRodney W. Grimes #include <fstab.h>
61d0e1503bSPeter Wemm #include <paths.h>
62780a5c1eSPeter Wemm 
638fae3551SRodney W. Grimes #include "fsck.h"
64780a5c1eSPeter Wemm 
659ea6f4f0SAdrian Chadd int returntosingle;
669ea6f4f0SAdrian Chadd 
679ea6f4f0SAdrian Chadd static void usage __P((void));
6831f4ab50SBruce Evans static int argtoi __P((int flag, char *req, char *str, int base));
6931f4ab50SBruce Evans static int docheck __P((struct fstab *fsp));
7031f4ab50SBruce Evans static int checkfilesys __P((char *filesys, char *mntpt, long auxdata,
7131f4ab50SBruce Evans 		int child));
72571b29eeSPeter Wemm static struct statfs *getmntpt __P((const char *));
73780a5c1eSPeter Wemm int main __P((int argc, char *argv[]));
748fae3551SRodney W. Grimes 
7531f4ab50SBruce Evans int
768fae3551SRodney W. Grimes main(argc, argv)
778fae3551SRodney W. Grimes 	int	argc;
788fae3551SRodney W. Grimes 	char	*argv[];
798fae3551SRodney W. Grimes {
808fae3551SRodney W. Grimes 	int ch;
81d33e92f9SJulian Elischer 	struct rlimit rlimit;
829ea6f4f0SAdrian Chadd 	int ret = 0;
838fae3551SRodney W. Grimes 
848fae3551SRodney W. Grimes 	sync();
859ea6f4f0SAdrian Chadd 	skipclean = 1;
869ea6f4f0SAdrian Chadd 	markclean = 1;
879ea6f4f0SAdrian Chadd 	while ((ch = getopt(argc, argv, "b:c:dfm:npy")) != -1) {
888fae3551SRodney W. Grimes 		switch (ch) {
898fae3551SRodney W. Grimes 		case 'b':
909ea6f4f0SAdrian Chadd 			skipclean = 0;
918fae3551SRodney W. Grimes 			bflag = argtoi('b', "number", optarg, 10);
928fae3551SRodney W. Grimes 			printf("Alternate super block location: %d\n", bflag);
938fae3551SRodney W. Grimes 			break;
948fae3551SRodney W. Grimes 
958fae3551SRodney W. Grimes 		case 'c':
969ea6f4f0SAdrian Chadd 			skipclean = 0;
978fae3551SRodney W. Grimes 			cvtlevel = argtoi('c', "conversion level", optarg, 10);
988fae3551SRodney W. Grimes 			break;
998fae3551SRodney W. Grimes 
1008fae3551SRodney W. Grimes 		case 'd':
1018fae3551SRodney W. Grimes 			debug++;
1028fae3551SRodney W. Grimes 			break;
1038fae3551SRodney W. Grimes 
10441cee58cSDavid Greenman 		case 'f':
1059ea6f4f0SAdrian Chadd 			skipclean = 0;
1068fae3551SRodney W. Grimes 			break;
1078fae3551SRodney W. Grimes 
1088fae3551SRodney W. Grimes 		case 'm':
1098fae3551SRodney W. Grimes 			lfmode = argtoi('m', "mode", optarg, 8);
1108fae3551SRodney W. Grimes 			if (lfmode &~ 07777)
111780a5c1eSPeter Wemm 				errx(EEXIT, "bad mode to -m: %o", lfmode);
1128fae3551SRodney W. Grimes 			printf("** lost+found creation mode %o\n", lfmode);
1138fae3551SRodney W. Grimes 			break;
1148fae3551SRodney W. Grimes 
1158fae3551SRodney W. Grimes 		case 'n':
1168fae3551SRodney W. Grimes 			nflag++;
1178fae3551SRodney W. Grimes 			yflag = 0;
1188fae3551SRodney W. Grimes 			break;
1198fae3551SRodney W. Grimes 
1209ea6f4f0SAdrian Chadd 		case 'p':
1219ea6f4f0SAdrian Chadd 			preen++;
1229ea6f4f0SAdrian Chadd 			break;
1239ea6f4f0SAdrian Chadd 
1248fae3551SRodney W. Grimes 		case 'y':
1258fae3551SRodney W. Grimes 			yflag++;
1268fae3551SRodney W. Grimes 			nflag = 0;
1278fae3551SRodney W. Grimes 			break;
1288fae3551SRodney W. Grimes 
1298fae3551SRodney W. Grimes 		default:
1309ea6f4f0SAdrian Chadd 			usage();
1318fae3551SRodney W. Grimes 		}
1328fae3551SRodney W. Grimes 	}
1338fae3551SRodney W. Grimes 	argc -= optind;
1348fae3551SRodney W. Grimes 	argv += optind;
1359ea6f4f0SAdrian Chadd 
1369ea6f4f0SAdrian Chadd 	if (!argc)
1379ea6f4f0SAdrian Chadd 		usage();
1389ea6f4f0SAdrian Chadd 
1398fae3551SRodney W. Grimes 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1408fae3551SRodney W. Grimes 		(void)signal(SIGINT, catch);
1418fae3551SRodney W. Grimes 	if (preen)
1428fae3551SRodney W. Grimes 		(void)signal(SIGQUIT, catchquit);
1436db798caSIan Dowse 	signal(SIGINFO, infohandler);
144d33e92f9SJulian Elischer 	/*
145d33e92f9SJulian Elischer 	 * Push up our allowed memory limit so we can cope
146d33e92f9SJulian Elischer 	 * with huge filesystems.
147d33e92f9SJulian Elischer 	 */
148d33e92f9SJulian Elischer 	if (getrlimit(RLIMIT_DATA, &rlimit) == 0) {
149d33e92f9SJulian Elischer 		rlimit.rlim_cur = rlimit.rlim_max;
150d33e92f9SJulian Elischer 		(void)setrlimit(RLIMIT_DATA, &rlimit);
151d33e92f9SJulian Elischer 	}
1529ea6f4f0SAdrian Chadd 	while (argc-- > 0)
1539ea6f4f0SAdrian Chadd 		(void)checkfilesys(blockcheck(*argv++), 0, 0L, 0);
154d33e92f9SJulian Elischer 
1558fae3551SRodney W. Grimes 	if (returntosingle)
1569ea6f4f0SAdrian Chadd 		ret = 2;
1578fae3551SRodney W. Grimes 	exit(ret);
1588fae3551SRodney W. Grimes }
1598fae3551SRodney W. Grimes 
160780a5c1eSPeter Wemm static int
1618fae3551SRodney W. Grimes argtoi(flag, req, str, base)
1628fae3551SRodney W. Grimes 	int flag;
1638fae3551SRodney W. Grimes 	char *req, *str;
1648fae3551SRodney W. Grimes 	int base;
1658fae3551SRodney W. Grimes {
1668fae3551SRodney W. Grimes 	char *cp;
1678fae3551SRodney W. Grimes 	int ret;
1688fae3551SRodney W. Grimes 
1698fae3551SRodney W. Grimes 	ret = (int)strtol(str, &cp, base);
1708fae3551SRodney W. Grimes 	if (cp == str || *cp)
171780a5c1eSPeter Wemm 		errx(EEXIT, "-%c flag requires a %s", flag, req);
1728fae3551SRodney W. Grimes 	return (ret);
1738fae3551SRodney W. Grimes }
1748fae3551SRodney W. Grimes 
1758fae3551SRodney W. Grimes /*
1768fae3551SRodney W. Grimes  * Check the specified filesystem.
1778fae3551SRodney W. Grimes  */
1788fae3551SRodney W. Grimes /* ARGSUSED */
179780a5c1eSPeter Wemm static int
1808fae3551SRodney W. Grimes checkfilesys(filesys, mntpt, auxdata, child)
1818fae3551SRodney W. Grimes 	char *filesys, *mntpt;
1828fae3551SRodney W. Grimes 	long auxdata;
18331f4ab50SBruce Evans 	int child;
1848fae3551SRodney W. Grimes {
185780a5c1eSPeter Wemm 	ufs_daddr_t n_ffree, n_bfree;
1868fae3551SRodney W. Grimes 	struct dups *dp;
187571b29eeSPeter Wemm 	struct statfs *mntbuf;
188d0e1503bSPeter Wemm 	struct zlncnt *zlnp;
189571b29eeSPeter Wemm 	int cylno;
1908fae3551SRodney W. Grimes 
1918fae3551SRodney W. Grimes 	if (preen && child)
1928fae3551SRodney W. Grimes 		(void)signal(SIGQUIT, voidquit);
1938fae3551SRodney W. Grimes 	cdevname = filesys;
1948fae3551SRodney W. Grimes 	if (debug && preen)
1958fae3551SRodney W. Grimes 		pwarn("starting\n");
196780a5c1eSPeter Wemm 	switch (setup(filesys)) {
197780a5c1eSPeter Wemm 	case 0:
1988fae3551SRodney W. Grimes 		if (preen)
1998fae3551SRodney W. Grimes 			pfatal("CAN'T CHECK FILE SYSTEM.");
2006b100474SJulian Elischer 		return (0);
201780a5c1eSPeter Wemm 	case -1:
2026b100474SJulian Elischer 		pwarn("clean, %ld free ", sblock.fs_cstotal.cs_nffree +
2036b100474SJulian Elischer 		    sblock.fs_frag * sblock.fs_cstotal.cs_nbfree);
2046b100474SJulian Elischer 		printf("(%d frags, %d blocks, %.1f%% fragmentation)\n",
2056b100474SJulian Elischer 		    sblock.fs_cstotal.cs_nffree, sblock.fs_cstotal.cs_nbfree,
2066b100474SJulian Elischer 		    sblock.fs_cstotal.cs_nffree * 100.0 / sblock.fs_dsize);
20741cee58cSDavid Greenman 		return (0);
20841cee58cSDavid Greenman 	}
2096b100474SJulian Elischer 
2108fae3551SRodney W. Grimes 	/*
211d0e1503bSPeter Wemm 	 * Get the mount point information of the filesystem, if
212571b29eeSPeter Wemm 	 * it is available.
213571b29eeSPeter Wemm 	 */
214571b29eeSPeter Wemm 	mntbuf = getmntpt(filesys);
215571b29eeSPeter Wemm 
216571b29eeSPeter Wemm 	/*
217b1897c19SJulian Elischer 	 * Cleared if any questions answered no. Used to decide if
218b1897c19SJulian Elischer 	 * the superblock should be marked clean.
219b1897c19SJulian Elischer 	 */
220b1897c19SJulian Elischer 	resolved = 1;
221b1897c19SJulian Elischer 	/*
2228fae3551SRodney W. Grimes 	 * 1: scan inodes tallying blocks used
2238fae3551SRodney W. Grimes 	 */
2248fae3551SRodney W. Grimes 	if (preen == 0) {
2258fae3551SRodney W. Grimes 		printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
226571b29eeSPeter Wemm 		if (mntbuf != NULL && mntbuf->f_flags & MNT_ROOTFS)
2278fae3551SRodney W. Grimes 			printf("** Root file system\n");
2288fae3551SRodney W. Grimes 		printf("** Phase 1 - Check Blocks and Sizes\n");
2298fae3551SRodney W. Grimes 	}
2308fae3551SRodney W. Grimes 	pass1();
2318fae3551SRodney W. Grimes 
2328fae3551SRodney W. Grimes 	/*
2338fae3551SRodney W. Grimes 	 * 1b: locate first references to duplicates, if any
2348fae3551SRodney W. Grimes 	 */
2358fae3551SRodney W. Grimes 	if (duplist) {
236b1897c19SJulian Elischer 		if (preen || usedsoftdep)
2378fae3551SRodney W. Grimes 			pfatal("INTERNAL ERROR: dups with -p");
2388fae3551SRodney W. Grimes 		printf("** Phase 1b - Rescan For More DUPS\n");
2398fae3551SRodney W. Grimes 		pass1b();
2408fae3551SRodney W. Grimes 	}
2418fae3551SRodney W. Grimes 
2428fae3551SRodney W. Grimes 	/*
2438fae3551SRodney W. Grimes 	 * 2: traverse directories from root to mark all connected directories
2448fae3551SRodney W. Grimes 	 */
2458fae3551SRodney W. Grimes 	if (preen == 0)
2468fae3551SRodney W. Grimes 		printf("** Phase 2 - Check Pathnames\n");
2478fae3551SRodney W. Grimes 	pass2();
2488fae3551SRodney W. Grimes 
2498fae3551SRodney W. Grimes 	/*
2508fae3551SRodney W. Grimes 	 * 3: scan inodes looking for disconnected directories
2518fae3551SRodney W. Grimes 	 */
2528fae3551SRodney W. Grimes 	if (preen == 0)
2538fae3551SRodney W. Grimes 		printf("** Phase 3 - Check Connectivity\n");
2548fae3551SRodney W. Grimes 	pass3();
2558fae3551SRodney W. Grimes 
2568fae3551SRodney W. Grimes 	/*
2578fae3551SRodney W. Grimes 	 * 4: scan inodes looking for disconnected files; check reference counts
2588fae3551SRodney W. Grimes 	 */
2598fae3551SRodney W. Grimes 	if (preen == 0)
2608fae3551SRodney W. Grimes 		printf("** Phase 4 - Check Reference Counts\n");
2618fae3551SRodney W. Grimes 	pass4();
2628fae3551SRodney W. Grimes 
2638fae3551SRodney W. Grimes 	/*
2648fae3551SRodney W. Grimes 	 * 5: check and repair resource counts in cylinder groups
2658fae3551SRodney W. Grimes 	 */
2668fae3551SRodney W. Grimes 	if (preen == 0)
2678fae3551SRodney W. Grimes 		printf("** Phase 5 - Check Cyl groups\n");
2688fae3551SRodney W. Grimes 	pass5();
2698fae3551SRodney W. Grimes 
2708fae3551SRodney W. Grimes 	/*
2718fae3551SRodney W. Grimes 	 * print out summary statistics
2728fae3551SRodney W. Grimes 	 */
2738fae3551SRodney W. Grimes 	n_ffree = sblock.fs_cstotal.cs_nffree;
2748fae3551SRodney W. Grimes 	n_bfree = sblock.fs_cstotal.cs_nbfree;
2758fae3551SRodney W. Grimes 	pwarn("%ld files, %ld used, %ld free ",
2768fae3551SRodney W. Grimes 	    n_files, n_blks, n_ffree + sblock.fs_frag * n_bfree);
277ccc3fadfSBruce Evans 	printf("(%d frags, %d blocks, %.1f%% fragmentation)\n",
278ccc3fadfSBruce Evans 	    n_ffree, n_bfree, n_ffree * 100.0 / sblock.fs_dsize);
2798fae3551SRodney W. Grimes 	if (debug &&
2808fae3551SRodney W. Grimes 	    (n_files -= maxino - ROOTINO - sblock.fs_cstotal.cs_nifree))
281ccc3fadfSBruce Evans 		printf("%d files missing\n", n_files);
2828fae3551SRodney W. Grimes 	if (debug) {
2838fae3551SRodney W. Grimes 		n_blks += sblock.fs_ncg *
2848fae3551SRodney W. Grimes 			(cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
2858fae3551SRodney W. Grimes 		n_blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
2868fae3551SRodney W. Grimes 		n_blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
2878fae3551SRodney W. Grimes 		if (n_blks -= maxfsblock - (n_ffree + sblock.fs_frag * n_bfree))
288ccc3fadfSBruce Evans 			printf("%d blocks missing\n", n_blks);
2898fae3551SRodney W. Grimes 		if (duplist != NULL) {
2908fae3551SRodney W. Grimes 			printf("The following duplicate blocks remain:");
2918fae3551SRodney W. Grimes 			for (dp = duplist; dp; dp = dp->next)
292ccc3fadfSBruce Evans 				printf(" %d,", dp->dup);
2938fae3551SRodney W. Grimes 			printf("\n");
2948fae3551SRodney W. Grimes 		}
2958fae3551SRodney W. Grimes 		if (zlnhead != NULL) {
2968fae3551SRodney W. Grimes 			printf("The following zero link count inodes remain:");
2978fae3551SRodney W. Grimes 			for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
298ccc3fadfSBruce Evans 				printf(" %u,", zlnp->zlncnt);
2998fae3551SRodney W. Grimes 			printf("\n");
3008fae3551SRodney W. Grimes 		}
3018fae3551SRodney W. Grimes 	}
3028fae3551SRodney W. Grimes 	zlnhead = (struct zlncnt *)0;
3038fae3551SRodney W. Grimes 	duplist = (struct dups *)0;
3048fae3551SRodney W. Grimes 	muldup = (struct dups *)0;
3058fae3551SRodney W. Grimes 	inocleanup();
306802cd8e6SDavid Greenman 	if (fsmodified) {
307d33e92f9SJulian Elischer 		sblock.fs_time = time(NULL);
3088fae3551SRodney W. Grimes 		sbdirty();
3098fae3551SRodney W. Grimes 	}
3108fae3551SRodney W. Grimes 	if (cvtlevel && sblk.b_dirty) {
3118fae3551SRodney W. Grimes 		/*
3128fae3551SRodney W. Grimes 		 * Write out the duplicate super blocks
3138fae3551SRodney W. Grimes 		 */
3148fae3551SRodney W. Grimes 		for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
3158fae3551SRodney W. Grimes 			bwrite(fswritefd, (char *)&sblock,
3168fae3551SRodney W. Grimes 			    fsbtodb(&sblock, cgsblock(&sblock, cylno)), SBSIZE);
3178fae3551SRodney W. Grimes 	}
318b1897c19SJulian Elischer 	if (rerun)
319b1897c19SJulian Elischer 		resolved = 0;
320571b29eeSPeter Wemm 
321d0e1503bSPeter Wemm 	/*
322aa5344b7SPeter Wemm 	 * Check to see if the filesystem is mounted read-write.
323d0e1503bSPeter Wemm 	 */
324571b29eeSPeter Wemm 	if (mntbuf != NULL && (mntbuf->f_flags & MNT_RDONLY) == 0)
325b1897c19SJulian Elischer 		resolved = 0;
326b1897c19SJulian Elischer 	ckfini(resolved);
327d33e92f9SJulian Elischer 
328d33e92f9SJulian Elischer 	for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
329d33e92f9SJulian Elischer 		if (inostathead[cylno].il_stat != NULL)
330d33e92f9SJulian Elischer 			free((char *)inostathead[cylno].il_stat);
331d33e92f9SJulian Elischer 	free((char *)inostathead);
332d33e92f9SJulian Elischer 	inostathead = NULL;
333d33e92f9SJulian Elischer 	if (fsmodified && !preen)
3348fae3551SRodney W. Grimes 		printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
33547ceb636SGuido van Rooij 	if (rerun)
33647ceb636SGuido van Rooij 		printf("\n***** PLEASE RERUN FSCK *****\n");
337d0e1503bSPeter Wemm 	if (mntbuf != NULL) {
338780a5c1eSPeter Wemm 		struct ufs_args args;
339780a5c1eSPeter Wemm 		int ret;
340d0e1503bSPeter Wemm 		/*
341d0e1503bSPeter Wemm 		 * We modified a mounted filesystem.  Do a mount update on
342d0e1503bSPeter Wemm 		 * it unless it is read-write, so we can continue using it
343d0e1503bSPeter Wemm 		 * as safely as possible.
344d0e1503bSPeter Wemm 		 */
345d0e1503bSPeter Wemm 		if (mntbuf->f_flags & MNT_RDONLY) {
3468fae3551SRodney W. Grimes 			args.fspec = 0;
3478fae3551SRodney W. Grimes 			args.export.ex_flags = 0;
3488fae3551SRodney W. Grimes 			args.export.ex_root = 0;
349d0e1503bSPeter Wemm 			ret = mount("ufs", mntbuf->f_mntonname,
350d0e1503bSPeter Wemm 			    mntbuf->f_flags | MNT_UPDATE | MNT_RELOAD, &args);
3518fae3551SRodney W. Grimes 			if (ret == 0)
352d0e1503bSPeter Wemm 				return (0);
353d0e1503bSPeter Wemm 			pwarn("mount reload of '%s' failed: %s\n\n",
354d0e1503bSPeter Wemm 			    mntbuf->f_mntonname, strerror(errno));
355d0e1503bSPeter Wemm 		}
356d33e92f9SJulian Elischer 		if (!fsmodified)
357d33e92f9SJulian Elischer 			return (0);
3588fae3551SRodney W. Grimes 		if (!preen)
3598fae3551SRodney W. Grimes 			printf("\n***** REBOOT NOW *****\n");
3608fae3551SRodney W. Grimes 		sync();
3618fae3551SRodney W. Grimes 		return (4);
3628fae3551SRodney W. Grimes 	}
3638fae3551SRodney W. Grimes 	return (0);
3648fae3551SRodney W. Grimes }
365571b29eeSPeter Wemm 
366571b29eeSPeter Wemm /*
367d0e1503bSPeter Wemm  * Get the directory that the device is mounted on.
368571b29eeSPeter Wemm  */
369571b29eeSPeter Wemm static struct statfs *
370571b29eeSPeter Wemm getmntpt(name)
371571b29eeSPeter Wemm 	const char *name;
372571b29eeSPeter Wemm {
373571b29eeSPeter Wemm 	struct stat devstat, mntdevstat;
374d0e1503bSPeter Wemm 	char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
375d0e1503bSPeter Wemm 	char *devname;
376d0e1503bSPeter Wemm 	struct statfs *mntbuf;
377571b29eeSPeter Wemm 	int i, mntsize;
378571b29eeSPeter Wemm 
379d0e1503bSPeter Wemm 	if (stat(name, &devstat) != 0 ||
380571b29eeSPeter Wemm 	    !(S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode)))
381571b29eeSPeter Wemm 		return (NULL);
382571b29eeSPeter Wemm 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
383571b29eeSPeter Wemm 	for (i = 0; i < mntsize; i++) {
384d0e1503bSPeter Wemm 		if (strcmp(mntbuf[i].f_fstypename, "ufs") != 0)
385571b29eeSPeter Wemm 			continue;
386d0e1503bSPeter Wemm 		devname = mntbuf[i].f_mntfromname;
387d0e1503bSPeter Wemm 		if (*devname != '/') {
388d0e1503bSPeter Wemm 			strcpy(device, _PATH_DEV);
389d0e1503bSPeter Wemm 			strcat(device, devname);
390d0e1503bSPeter Wemm 			devname = device;
391d0e1503bSPeter Wemm 		}
392aa5344b7SPeter Wemm 		if (stat(devname, &mntdevstat) == 0 &&
393d0e1503bSPeter Wemm 		    mntdevstat.st_rdev == devstat.st_rdev)
394571b29eeSPeter Wemm 			return (&mntbuf[i]);
395571b29eeSPeter Wemm 	}
396571b29eeSPeter Wemm 	return (NULL);
397571b29eeSPeter Wemm }
3989ea6f4f0SAdrian Chadd 
3999ea6f4f0SAdrian Chadd static void
4009ea6f4f0SAdrian Chadd usage()
4019ea6f4f0SAdrian Chadd {
4029ea6f4f0SAdrian Chadd         extern char *__progname;
4039ea6f4f0SAdrian Chadd 
4049ea6f4f0SAdrian Chadd         (void) fprintf(stderr,
4059ea6f4f0SAdrian Chadd             "Usage: %s [-dfnpy] [-B be|le] [-b block] [-c level] [-m mode] "
4069ea6f4f0SAdrian Chadd                         "filesystem ...\n",
4079ea6f4f0SAdrian Chadd             __progname);
4089ea6f4f0SAdrian Chadd         exit(1);
4099ea6f4f0SAdrian Chadd }
410