xref: /freebsd/sbin/fsck_ffs/main.c (revision aa5344b7d7ef5e1d1b220594e6a21622b6be7357)
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 
6531f4ab50SBruce Evans static int argtoi __P((int flag, char *req, char *str, int base));
6631f4ab50SBruce Evans static int docheck __P((struct fstab *fsp));
6731f4ab50SBruce Evans static int checkfilesys __P((char *filesys, char *mntpt, long auxdata,
6831f4ab50SBruce Evans 		int child));
69571b29eeSPeter Wemm static struct statfs *getmntpt __P((const char *));
70780a5c1eSPeter Wemm int main __P((int argc, char *argv[]));
718fae3551SRodney W. Grimes 
7231f4ab50SBruce Evans int
738fae3551SRodney W. Grimes main(argc, argv)
748fae3551SRodney W. Grimes 	int	argc;
758fae3551SRodney W. Grimes 	char	*argv[];
768fae3551SRodney W. Grimes {
778fae3551SRodney W. Grimes 	int ch;
788fae3551SRodney W. Grimes 	int ret, maxrun = 0;
79d33e92f9SJulian Elischer 	struct rlimit rlimit;
808fae3551SRodney W. Grimes 
818fae3551SRodney W. Grimes 	sync();
82473e3a9aSBruce Evans 	while ((ch = getopt(argc, argv, "dfpnNyYb:c:l:m:")) != -1) {
838fae3551SRodney W. Grimes 		switch (ch) {
848fae3551SRodney W. Grimes 		case 'p':
858fae3551SRodney W. Grimes 			preen++;
868fae3551SRodney W. Grimes 			break;
878fae3551SRodney W. Grimes 
888fae3551SRodney W. Grimes 		case 'b':
898fae3551SRodney W. Grimes 			bflag = argtoi('b', "number", optarg, 10);
908fae3551SRodney W. Grimes 			printf("Alternate super block location: %d\n", bflag);
918fae3551SRodney W. Grimes 			break;
928fae3551SRodney W. Grimes 
938fae3551SRodney W. Grimes 		case 'c':
948fae3551SRodney W. Grimes 			cvtlevel = argtoi('c', "conversion level", optarg, 10);
958fae3551SRodney W. Grimes 			break;
968fae3551SRodney W. Grimes 
978fae3551SRodney W. Grimes 		case 'd':
988fae3551SRodney W. Grimes 			debug++;
998fae3551SRodney W. Grimes 			break;
1008fae3551SRodney W. Grimes 
10141cee58cSDavid Greenman 		case 'f':
10241cee58cSDavid Greenman 			fflag++;
10341cee58cSDavid Greenman 			break;
10441cee58cSDavid Greenman 
1058fae3551SRodney W. Grimes 		case 'l':
1068fae3551SRodney W. Grimes 			maxrun = argtoi('l', "number", optarg, 10);
1078fae3551SRodney W. Grimes 			break;
1088fae3551SRodney W. Grimes 
1098fae3551SRodney W. Grimes 		case 'm':
1108fae3551SRodney W. Grimes 			lfmode = argtoi('m', "mode", optarg, 8);
1118fae3551SRodney W. Grimes 			if (lfmode &~ 07777)
112780a5c1eSPeter Wemm 				errx(EEXIT, "bad mode to -m: %o", lfmode);
1138fae3551SRodney W. Grimes 			printf("** lost+found creation mode %o\n", lfmode);
1148fae3551SRodney W. Grimes 			break;
1158fae3551SRodney W. Grimes 
1168fae3551SRodney W. Grimes 		case 'n':
1178fae3551SRodney W. Grimes 		case 'N':
1188fae3551SRodney W. Grimes 			nflag++;
1198fae3551SRodney W. Grimes 			yflag = 0;
1208fae3551SRodney W. Grimes 			break;
1218fae3551SRodney W. Grimes 
1228fae3551SRodney W. Grimes 		case 'y':
1238fae3551SRodney W. Grimes 		case 'Y':
1248fae3551SRodney W. Grimes 			yflag++;
1258fae3551SRodney W. Grimes 			nflag = 0;
1268fae3551SRodney W. Grimes 			break;
1278fae3551SRodney W. Grimes 
1288fae3551SRodney W. Grimes 		default:
129780a5c1eSPeter Wemm 			errx(EEXIT, "%c option?", ch);
1308fae3551SRodney W. Grimes 		}
1318fae3551SRodney W. Grimes 	}
1328fae3551SRodney W. Grimes 	argc -= optind;
1338fae3551SRodney W. Grimes 	argv += optind;
1348fae3551SRodney W. Grimes 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1358fae3551SRodney W. Grimes 		(void)signal(SIGINT, catch);
1368fae3551SRodney W. Grimes 	if (preen)
1378fae3551SRodney W. Grimes 		(void)signal(SIGQUIT, catchquit);
138d33e92f9SJulian Elischer 	/*
139d33e92f9SJulian Elischer 	 * Push up our allowed memory limit so we can cope
140d33e92f9SJulian Elischer 	 * with huge filesystems.
141d33e92f9SJulian Elischer 	 */
142d33e92f9SJulian Elischer 	if (getrlimit(RLIMIT_DATA, &rlimit) == 0) {
143d33e92f9SJulian Elischer 		rlimit.rlim_cur = rlimit.rlim_max;
144d33e92f9SJulian Elischer 		(void)setrlimit(RLIMIT_DATA, &rlimit);
145d33e92f9SJulian Elischer 	}
1468fae3551SRodney W. Grimes 	if (argc) {
147d33e92f9SJulian Elischer 		while (argc-- > 0) {
148d33e92f9SJulian Elischer 			char *path = blockcheck(*argv);
149d33e92f9SJulian Elischer 
150d33e92f9SJulian Elischer 			if (path == NULL)
151d33e92f9SJulian Elischer 				pfatal("Can't check %s\n", *argv);
152d33e92f9SJulian Elischer 			else
153d33e92f9SJulian Elischer 				(void)checkfilesys(path, 0, 0L, 0);
154d33e92f9SJulian Elischer 			++argv;
155d33e92f9SJulian Elischer 		}
1568fae3551SRodney W. Grimes 		exit(0);
1578fae3551SRodney W. Grimes 	}
1588fae3551SRodney W. Grimes 	ret = checkfstab(preen, maxrun, docheck, checkfilesys);
1598fae3551SRodney W. Grimes 	if (returntosingle)
1608fae3551SRodney W. Grimes 		exit(2);
1618fae3551SRodney W. Grimes 	exit(ret);
1628fae3551SRodney W. Grimes }
1638fae3551SRodney W. Grimes 
164780a5c1eSPeter Wemm static int
1658fae3551SRodney W. Grimes argtoi(flag, req, str, base)
1668fae3551SRodney W. Grimes 	int flag;
1678fae3551SRodney W. Grimes 	char *req, *str;
1688fae3551SRodney W. Grimes 	int base;
1698fae3551SRodney W. Grimes {
1708fae3551SRodney W. Grimes 	char *cp;
1718fae3551SRodney W. Grimes 	int ret;
1728fae3551SRodney W. Grimes 
1738fae3551SRodney W. Grimes 	ret = (int)strtol(str, &cp, base);
1748fae3551SRodney W. Grimes 	if (cp == str || *cp)
175780a5c1eSPeter Wemm 		errx(EEXIT, "-%c flag requires a %s", flag, req);
1768fae3551SRodney W. Grimes 	return (ret);
1778fae3551SRodney W. Grimes }
1788fae3551SRodney W. Grimes 
1798fae3551SRodney W. Grimes /*
1808fae3551SRodney W. Grimes  * Determine whether a filesystem should be checked.
1818fae3551SRodney W. Grimes  */
182780a5c1eSPeter Wemm static int
1838fae3551SRodney W. Grimes docheck(fsp)
1848fae3551SRodney W. Grimes 	register struct fstab *fsp;
1858fae3551SRodney W. Grimes {
1868fae3551SRodney W. Grimes 
1878fae3551SRodney W. Grimes 	if (strcmp(fsp->fs_vfstype, "ufs") ||
1888fae3551SRodney W. Grimes 	    (strcmp(fsp->fs_type, FSTAB_RW) &&
1898fae3551SRodney W. Grimes 	     strcmp(fsp->fs_type, FSTAB_RO)) ||
1908fae3551SRodney W. Grimes 	    fsp->fs_passno == 0)
1918fae3551SRodney W. Grimes 		return (0);
1928fae3551SRodney W. Grimes 	return (1);
1938fae3551SRodney W. Grimes }
1948fae3551SRodney W. Grimes 
1958fae3551SRodney W. Grimes /*
1968fae3551SRodney W. Grimes  * Check the specified filesystem.
1978fae3551SRodney W. Grimes  */
1988fae3551SRodney W. Grimes /* ARGSUSED */
199780a5c1eSPeter Wemm static int
2008fae3551SRodney W. Grimes checkfilesys(filesys, mntpt, auxdata, child)
2018fae3551SRodney W. Grimes 	char *filesys, *mntpt;
2028fae3551SRodney W. Grimes 	long auxdata;
20331f4ab50SBruce Evans 	int child;
2048fae3551SRodney W. Grimes {
205780a5c1eSPeter Wemm 	ufs_daddr_t n_ffree, n_bfree;
2068fae3551SRodney W. Grimes 	struct dups *dp;
207571b29eeSPeter Wemm 	struct statfs *mntbuf;
208d0e1503bSPeter Wemm 	struct zlncnt *zlnp;
209571b29eeSPeter Wemm 	int cylno;
2108fae3551SRodney W. Grimes 
2118fae3551SRodney W. Grimes 	if (preen && child)
2128fae3551SRodney W. Grimes 		(void)signal(SIGQUIT, voidquit);
2138fae3551SRodney W. Grimes 	cdevname = filesys;
2148fae3551SRodney W. Grimes 	if (debug && preen)
2158fae3551SRodney W. Grimes 		pwarn("starting\n");
216780a5c1eSPeter Wemm 	switch (setup(filesys)) {
217780a5c1eSPeter Wemm 	case 0:
2188fae3551SRodney W. Grimes 		if (preen)
2198fae3551SRodney W. Grimes 			pfatal("CAN'T CHECK FILE SYSTEM.");
2206b100474SJulian Elischer 		return (0);
221780a5c1eSPeter Wemm 	case -1:
2226b100474SJulian Elischer 		pwarn("clean, %ld free ", sblock.fs_cstotal.cs_nffree +
2236b100474SJulian Elischer 		    sblock.fs_frag * sblock.fs_cstotal.cs_nbfree);
2246b100474SJulian Elischer 		printf("(%d frags, %d blocks, %.1f%% fragmentation)\n",
2256b100474SJulian Elischer 		    sblock.fs_cstotal.cs_nffree, sblock.fs_cstotal.cs_nbfree,
2266b100474SJulian Elischer 		    sblock.fs_cstotal.cs_nffree * 100.0 / sblock.fs_dsize);
22741cee58cSDavid Greenman 		return (0);
22841cee58cSDavid Greenman 	}
2296b100474SJulian Elischer 
2308fae3551SRodney W. Grimes 	/*
231d0e1503bSPeter Wemm 	 * Get the mount point information of the filesystem, if
232571b29eeSPeter Wemm 	 * it is available.
233571b29eeSPeter Wemm 	 */
234571b29eeSPeter Wemm 	mntbuf = getmntpt(filesys);
235571b29eeSPeter Wemm 
236571b29eeSPeter Wemm 	/*
237b1897c19SJulian Elischer 	 * Cleared if any questions answered no. Used to decide if
238b1897c19SJulian Elischer 	 * the superblock should be marked clean.
239b1897c19SJulian Elischer 	 */
240b1897c19SJulian Elischer 	resolved = 1;
241b1897c19SJulian Elischer 	/*
2428fae3551SRodney W. Grimes 	 * 1: scan inodes tallying blocks used
2438fae3551SRodney W. Grimes 	 */
2448fae3551SRodney W. Grimes 	if (preen == 0) {
2458fae3551SRodney W. Grimes 		printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
246571b29eeSPeter Wemm 		if (mntbuf != NULL && mntbuf->f_flags & MNT_ROOTFS)
2478fae3551SRodney W. Grimes 			printf("** Root file system\n");
2488fae3551SRodney W. Grimes 		printf("** Phase 1 - Check Blocks and Sizes\n");
2498fae3551SRodney W. Grimes 	}
2508fae3551SRodney W. Grimes 	pass1();
2518fae3551SRodney W. Grimes 
2528fae3551SRodney W. Grimes 	/*
2538fae3551SRodney W. Grimes 	 * 1b: locate first references to duplicates, if any
2548fae3551SRodney W. Grimes 	 */
2558fae3551SRodney W. Grimes 	if (duplist) {
256b1897c19SJulian Elischer 		if (preen || usedsoftdep)
2578fae3551SRodney W. Grimes 			pfatal("INTERNAL ERROR: dups with -p");
2588fae3551SRodney W. Grimes 		printf("** Phase 1b - Rescan For More DUPS\n");
2598fae3551SRodney W. Grimes 		pass1b();
2608fae3551SRodney W. Grimes 	}
2618fae3551SRodney W. Grimes 
2628fae3551SRodney W. Grimes 	/*
2638fae3551SRodney W. Grimes 	 * 2: traverse directories from root to mark all connected directories
2648fae3551SRodney W. Grimes 	 */
2658fae3551SRodney W. Grimes 	if (preen == 0)
2668fae3551SRodney W. Grimes 		printf("** Phase 2 - Check Pathnames\n");
2678fae3551SRodney W. Grimes 	pass2();
2688fae3551SRodney W. Grimes 
2698fae3551SRodney W. Grimes 	/*
2708fae3551SRodney W. Grimes 	 * 3: scan inodes looking for disconnected directories
2718fae3551SRodney W. Grimes 	 */
2728fae3551SRodney W. Grimes 	if (preen == 0)
2738fae3551SRodney W. Grimes 		printf("** Phase 3 - Check Connectivity\n");
2748fae3551SRodney W. Grimes 	pass3();
2758fae3551SRodney W. Grimes 
2768fae3551SRodney W. Grimes 	/*
2778fae3551SRodney W. Grimes 	 * 4: scan inodes looking for disconnected files; check reference counts
2788fae3551SRodney W. Grimes 	 */
2798fae3551SRodney W. Grimes 	if (preen == 0)
2808fae3551SRodney W. Grimes 		printf("** Phase 4 - Check Reference Counts\n");
2818fae3551SRodney W. Grimes 	pass4();
2828fae3551SRodney W. Grimes 
2838fae3551SRodney W. Grimes 	/*
2848fae3551SRodney W. Grimes 	 * 5: check and repair resource counts in cylinder groups
2858fae3551SRodney W. Grimes 	 */
2868fae3551SRodney W. Grimes 	if (preen == 0)
2878fae3551SRodney W. Grimes 		printf("** Phase 5 - Check Cyl groups\n");
2888fae3551SRodney W. Grimes 	pass5();
2898fae3551SRodney W. Grimes 
2908fae3551SRodney W. Grimes 	/*
2918fae3551SRodney W. Grimes 	 * print out summary statistics
2928fae3551SRodney W. Grimes 	 */
2938fae3551SRodney W. Grimes 	n_ffree = sblock.fs_cstotal.cs_nffree;
2948fae3551SRodney W. Grimes 	n_bfree = sblock.fs_cstotal.cs_nbfree;
2958fae3551SRodney W. Grimes 	pwarn("%ld files, %ld used, %ld free ",
2968fae3551SRodney W. Grimes 	    n_files, n_blks, n_ffree + sblock.fs_frag * n_bfree);
297ccc3fadfSBruce Evans 	printf("(%d frags, %d blocks, %.1f%% fragmentation)\n",
298ccc3fadfSBruce Evans 	    n_ffree, n_bfree, n_ffree * 100.0 / sblock.fs_dsize);
2998fae3551SRodney W. Grimes 	if (debug &&
3008fae3551SRodney W. Grimes 	    (n_files -= maxino - ROOTINO - sblock.fs_cstotal.cs_nifree))
301ccc3fadfSBruce Evans 		printf("%d files missing\n", n_files);
3028fae3551SRodney W. Grimes 	if (debug) {
3038fae3551SRodney W. Grimes 		n_blks += sblock.fs_ncg *
3048fae3551SRodney W. Grimes 			(cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
3058fae3551SRodney W. Grimes 		n_blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
3068fae3551SRodney W. Grimes 		n_blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
3078fae3551SRodney W. Grimes 		if (n_blks -= maxfsblock - (n_ffree + sblock.fs_frag * n_bfree))
308ccc3fadfSBruce Evans 			printf("%d blocks missing\n", n_blks);
3098fae3551SRodney W. Grimes 		if (duplist != NULL) {
3108fae3551SRodney W. Grimes 			printf("The following duplicate blocks remain:");
3118fae3551SRodney W. Grimes 			for (dp = duplist; dp; dp = dp->next)
312ccc3fadfSBruce Evans 				printf(" %d,", dp->dup);
3138fae3551SRodney W. Grimes 			printf("\n");
3148fae3551SRodney W. Grimes 		}
3158fae3551SRodney W. Grimes 		if (zlnhead != NULL) {
3168fae3551SRodney W. Grimes 			printf("The following zero link count inodes remain:");
3178fae3551SRodney W. Grimes 			for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
318ccc3fadfSBruce Evans 				printf(" %u,", zlnp->zlncnt);
3198fae3551SRodney W. Grimes 			printf("\n");
3208fae3551SRodney W. Grimes 		}
3218fae3551SRodney W. Grimes 	}
3228fae3551SRodney W. Grimes 	zlnhead = (struct zlncnt *)0;
3238fae3551SRodney W. Grimes 	duplist = (struct dups *)0;
3248fae3551SRodney W. Grimes 	muldup = (struct dups *)0;
3258fae3551SRodney W. Grimes 	inocleanup();
326802cd8e6SDavid Greenman 	if (fsmodified) {
327d33e92f9SJulian Elischer 		sblock.fs_time = time(NULL);
3288fae3551SRodney W. Grimes 		sbdirty();
3298fae3551SRodney W. Grimes 	}
3308fae3551SRodney W. Grimes 	if (cvtlevel && sblk.b_dirty) {
3318fae3551SRodney W. Grimes 		/*
3328fae3551SRodney W. Grimes 		 * Write out the duplicate super blocks
3338fae3551SRodney W. Grimes 		 */
3348fae3551SRodney W. Grimes 		for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
3358fae3551SRodney W. Grimes 			bwrite(fswritefd, (char *)&sblock,
3368fae3551SRodney W. Grimes 			    fsbtodb(&sblock, cgsblock(&sblock, cylno)), SBSIZE);
3378fae3551SRodney W. Grimes 	}
338b1897c19SJulian Elischer 	if (rerun)
339b1897c19SJulian Elischer 		resolved = 0;
340571b29eeSPeter Wemm 
341d0e1503bSPeter Wemm 	/*
342aa5344b7SPeter Wemm 	 * Check to see if the filesystem is mounted read-write.
343d0e1503bSPeter Wemm 	 */
344571b29eeSPeter Wemm 	if (mntbuf != NULL && (mntbuf->f_flags & MNT_RDONLY) == 0)
345b1897c19SJulian Elischer 		resolved = 0;
346b1897c19SJulian Elischer 	ckfini(resolved);
347d33e92f9SJulian Elischer 
348d33e92f9SJulian Elischer 	for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
349d33e92f9SJulian Elischer 		if (inostathead[cylno].il_stat != NULL)
350d33e92f9SJulian Elischer 			free((char *)inostathead[cylno].il_stat);
351d33e92f9SJulian Elischer 	free((char *)inostathead);
352d33e92f9SJulian Elischer 	inostathead = NULL;
353d33e92f9SJulian Elischer 	if (fsmodified && !preen)
3548fae3551SRodney W. Grimes 		printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
35547ceb636SGuido van Rooij 	if (rerun)
35647ceb636SGuido van Rooij 		printf("\n***** PLEASE RERUN FSCK *****\n");
357d0e1503bSPeter Wemm 	if (mntbuf != NULL) {
358780a5c1eSPeter Wemm 		struct ufs_args args;
359780a5c1eSPeter Wemm 		int ret;
360d0e1503bSPeter Wemm 		/*
361d0e1503bSPeter Wemm 		 * We modified a mounted filesystem.  Do a mount update on
362d0e1503bSPeter Wemm 		 * it unless it is read-write, so we can continue using it
363d0e1503bSPeter Wemm 		 * as safely as possible.
364d0e1503bSPeter Wemm 		 */
365d0e1503bSPeter Wemm 		if (mntbuf->f_flags & MNT_RDONLY) {
3668fae3551SRodney W. Grimes 			args.fspec = 0;
3678fae3551SRodney W. Grimes 			args.export.ex_flags = 0;
3688fae3551SRodney W. Grimes 			args.export.ex_root = 0;
369d0e1503bSPeter Wemm 			ret = mount("ufs", mntbuf->f_mntonname,
370d0e1503bSPeter Wemm 			    mntbuf->f_flags | MNT_UPDATE | MNT_RELOAD, &args);
3718fae3551SRodney W. Grimes 			if (ret == 0)
372d0e1503bSPeter Wemm 				return (0);
373d0e1503bSPeter Wemm 			pwarn("mount reload of '%s' failed: %s\n\n",
374d0e1503bSPeter Wemm 			    mntbuf->f_mntonname, strerror(errno));
375d0e1503bSPeter Wemm 		}
376d33e92f9SJulian Elischer 		if (!fsmodified)
377d33e92f9SJulian Elischer 			return (0);
3788fae3551SRodney W. Grimes 		if (!preen)
3798fae3551SRodney W. Grimes 			printf("\n***** REBOOT NOW *****\n");
3808fae3551SRodney W. Grimes 		sync();
3818fae3551SRodney W. Grimes 		return (4);
3828fae3551SRodney W. Grimes 	}
3838fae3551SRodney W. Grimes 	return (0);
3848fae3551SRodney W. Grimes }
385571b29eeSPeter Wemm 
386571b29eeSPeter Wemm /*
387d0e1503bSPeter Wemm  * Get the directory that the device is mounted on.
388571b29eeSPeter Wemm  */
389571b29eeSPeter Wemm static struct statfs *
390571b29eeSPeter Wemm getmntpt(name)
391571b29eeSPeter Wemm 	const char *name;
392571b29eeSPeter Wemm {
393571b29eeSPeter Wemm 	struct stat devstat, mntdevstat;
394d0e1503bSPeter Wemm 	char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
395d0e1503bSPeter Wemm 	char *devname;
396d0e1503bSPeter Wemm 	struct statfs *mntbuf;
397571b29eeSPeter Wemm 	int i, mntsize;
398571b29eeSPeter Wemm 
399d0e1503bSPeter Wemm 	if (stat(name, &devstat) != 0 ||
400571b29eeSPeter Wemm 	    !(S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode)))
401571b29eeSPeter Wemm 		return (NULL);
402571b29eeSPeter Wemm 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
403571b29eeSPeter Wemm 	for (i = 0; i < mntsize; i++) {
404d0e1503bSPeter Wemm 		if (strcmp(mntbuf[i].f_fstypename, "ufs") != 0)
405571b29eeSPeter Wemm 			continue;
406d0e1503bSPeter Wemm 		devname = mntbuf[i].f_mntfromname;
407d0e1503bSPeter Wemm 		if (*devname != '/') {
408d0e1503bSPeter Wemm 			strcpy(device, _PATH_DEV);
409d0e1503bSPeter Wemm 			strcat(device, devname);
410d0e1503bSPeter Wemm 			devname = device;
411d0e1503bSPeter Wemm 		}
412aa5344b7SPeter Wemm 		if (stat(devname, &mntdevstat) == 0 &&
413d0e1503bSPeter Wemm 		    mntdevstat.st_rdev == devstat.st_rdev)
414571b29eeSPeter Wemm 			return (&mntbuf[i]);
415571b29eeSPeter Wemm 	}
416571b29eeSPeter Wemm 	return (NULL);
417571b29eeSPeter Wemm }
418