xref: /freebsd/sbin/fsck_ffs/main.c (revision aef8d244)
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  * 4. Neither the name of the University nor the names of its contributors
148fae3551SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
158fae3551SRodney W. Grimes  *    without specific prior written permission.
168fae3551SRodney W. Grimes  *
178fae3551SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
188fae3551SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
198fae3551SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
208fae3551SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
218fae3551SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
228fae3551SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
238fae3551SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
248fae3551SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
258fae3551SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
268fae3551SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
278fae3551SRodney W. Grimes  * SUCH DAMAGE.
288fae3551SRodney W. Grimes  */
298fae3551SRodney W. Grimes 
30c69284caSDavid E. O'Brien #if 0
318fae3551SRodney W. Grimes #ifndef lint
3231f4ab50SBruce Evans static const char copyright[] =
338fae3551SRodney W. Grimes "@(#) Copyright (c) 1980, 1986, 1993\n\
348fae3551SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
358fae3551SRodney W. Grimes #endif /* not lint */
368fae3551SRodney W. Grimes 
378fae3551SRodney W. Grimes #ifndef lint
386b100474SJulian Elischer static char sccsid[] = "@(#)main.c	8.6 (Berkeley) 5/14/95";
398fae3551SRodney W. Grimes #endif /* not lint */
40c69284caSDavid E. O'Brien #endif
418e2e167cSMark Murray #include <sys/cdefs.h>
428e2e167cSMark Murray __FBSDID("$FreeBSD$");
438e2e167cSMark Murray 
448fae3551SRodney W. Grimes #include <sys/param.h>
45571b29eeSPeter Wemm #include <sys/stat.h>
4638375c40SKirk McKusick #include <sys/file.h>
478fae3551SRodney W. Grimes #include <sys/time.h>
488fae3551SRodney W. Grimes #include <sys/mount.h>
4933966081SMatt Jacob #include <sys/resource.h>
50bf58d635SIan Dowse #include <sys/sysctl.h>
511c85e6a3SKirk McKusick #include <sys/disklabel.h>
52780a5c1eSPeter Wemm 
538fae3551SRodney W. Grimes #include <ufs/ufs/dinode.h>
54780a5c1eSPeter Wemm #include <ufs/ufs/ufsmount.h>
558fae3551SRodney W. Grimes #include <ufs/ffs/fs.h>
56780a5c1eSPeter Wemm 
57780a5c1eSPeter Wemm #include <err.h>
58d0e1503bSPeter Wemm #include <errno.h>
598fae3551SRodney W. Grimes #include <fstab.h>
60ff76fc7fSKirk McKusick #include <grp.h>
61d0e1503bSPeter Wemm #include <paths.h>
6284fc0d7eSMaxime Henrion #include <stdint.h>
63bf58d635SIan Dowse #include <string.h>
64780a5c1eSPeter Wemm 
658fae3551SRodney W. Grimes #include "fsck.h"
66780a5c1eSPeter Wemm 
67b70cd7eeSWarner Losh static void usage(void) __dead2;
68599304a4SPoul-Henning Kamp static int argtoi(int flag, const char *req, const char *str, int base);
69b70cd7eeSWarner Losh static int checkfilesys(char *filesys);
70b70cd7eeSWarner Losh static struct statfs *getmntpt(const char *);
718fae3551SRodney W. Grimes 
7231f4ab50SBruce Evans int
73b70cd7eeSWarner Losh main(int argc, char *argv[])
748fae3551SRodney W. Grimes {
758fae3551SRodney W. Grimes 	int ch;
76d33e92f9SJulian Elischer 	struct rlimit rlimit;
771660ae87SScott Long 	struct itimerval itimerval;
789ea6f4f0SAdrian Chadd 	int ret = 0;
798fae3551SRodney W. Grimes 
808fae3551SRodney W. Grimes 	sync();
819ea6f4f0SAdrian Chadd 	skipclean = 1;
8215fca934SKirk McKusick 	while ((ch = getopt(argc, argv, "b:Bc:dfFm:npy")) != -1) {
838fae3551SRodney W. Grimes 		switch (ch) {
848fae3551SRodney W. Grimes 		case 'b':
859ea6f4f0SAdrian Chadd 			skipclean = 0;
868fae3551SRodney W. Grimes 			bflag = argtoi('b', "number", optarg, 10);
878fae3551SRodney W. Grimes 			printf("Alternate super block location: %d\n", bflag);
888fae3551SRodney W. Grimes 			break;
898fae3551SRodney W. Grimes 
907578c6abSKirk McKusick 		case 'B':
917578c6abSKirk McKusick 			bkgrdflag = 1;
927578c6abSKirk McKusick 			break;
937578c6abSKirk McKusick 
948fae3551SRodney W. Grimes 		case 'c':
959ea6f4f0SAdrian Chadd 			skipclean = 0;
968fae3551SRodney W. Grimes 			cvtlevel = argtoi('c', "conversion level", optarg, 10);
971c85e6a3SKirk McKusick 			if (cvtlevel < 3)
981c85e6a3SKirk McKusick 				errx(EEXIT, "cannot do level %d conversion",
991c85e6a3SKirk McKusick 				    cvtlevel);
1008fae3551SRodney W. Grimes 			break;
1018fae3551SRodney W. Grimes 
1028fae3551SRodney W. Grimes 		case 'd':
1038fae3551SRodney W. Grimes 			debug++;
1048fae3551SRodney W. Grimes 			break;
1058fae3551SRodney W. Grimes 
10641cee58cSDavid Greenman 		case 'f':
1079ea6f4f0SAdrian Chadd 			skipclean = 0;
1088fae3551SRodney W. Grimes 			break;
1098fae3551SRodney W. Grimes 
11015fca934SKirk McKusick 		case 'F':
11115fca934SKirk McKusick 			bkgrdcheck = 1;
11215fca934SKirk McKusick 			break;
11315fca934SKirk McKusick 
1148fae3551SRodney W. Grimes 		case 'm':
1158fae3551SRodney W. Grimes 			lfmode = argtoi('m', "mode", optarg, 8);
1168fae3551SRodney W. Grimes 			if (lfmode &~ 07777)
117780a5c1eSPeter Wemm 				errx(EEXIT, "bad mode to -m: %o", lfmode);
1188fae3551SRodney W. Grimes 			printf("** lost+found creation mode %o\n", lfmode);
1198fae3551SRodney W. Grimes 			break;
1208fae3551SRodney W. Grimes 
1218fae3551SRodney W. Grimes 		case 'n':
1228fae3551SRodney W. Grimes 			nflag++;
1238fae3551SRodney W. Grimes 			yflag = 0;
1248fae3551SRodney W. Grimes 			break;
1258fae3551SRodney W. Grimes 
1269ea6f4f0SAdrian Chadd 		case 'p':
1279ea6f4f0SAdrian Chadd 			preen++;
1289ea6f4f0SAdrian Chadd 			break;
1299ea6f4f0SAdrian Chadd 
1308fae3551SRodney W. Grimes 		case 'y':
1318fae3551SRodney W. Grimes 			yflag++;
1328fae3551SRodney W. Grimes 			nflag = 0;
1338fae3551SRodney W. Grimes 			break;
1348fae3551SRodney W. Grimes 
1358fae3551SRodney W. Grimes 		default:
1369ea6f4f0SAdrian Chadd 			usage();
1378fae3551SRodney W. Grimes 		}
1388fae3551SRodney W. Grimes 	}
1398fae3551SRodney W. Grimes 	argc -= optind;
1408fae3551SRodney W. Grimes 	argv += optind;
1419ea6f4f0SAdrian Chadd 
1429ea6f4f0SAdrian Chadd 	if (!argc)
1439ea6f4f0SAdrian Chadd 		usage();
1449ea6f4f0SAdrian Chadd 
1458fae3551SRodney W. Grimes 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1468fae3551SRodney W. Grimes 		(void)signal(SIGINT, catch);
1478fae3551SRodney W. Grimes 	if (preen)
1488fae3551SRodney W. Grimes 		(void)signal(SIGQUIT, catchquit);
1496db798caSIan Dowse 	signal(SIGINFO, infohandler);
1501660ae87SScott Long 	if (bkgrdflag) {
1511660ae87SScott Long 		signal(SIGALRM, alarmhandler);
1521660ae87SScott Long 		itimerval.it_interval.tv_sec = 5;
1531660ae87SScott Long 		itimerval.it_interval.tv_usec = 0;
1541660ae87SScott Long 		itimerval.it_value.tv_sec = 5;
1551660ae87SScott Long 		itimerval.it_value.tv_usec = 0;
1561660ae87SScott Long 		setitimer(ITIMER_REAL, &itimerval, NULL);
1571660ae87SScott Long 	}
158d33e92f9SJulian Elischer 	/*
159d33e92f9SJulian Elischer 	 * Push up our allowed memory limit so we can cope
160d33e92f9SJulian Elischer 	 * with huge file systems.
161d33e92f9SJulian Elischer 	 */
162d33e92f9SJulian Elischer 	if (getrlimit(RLIMIT_DATA, &rlimit) == 0) {
163d33e92f9SJulian Elischer 		rlimit.rlim_cur = rlimit.rlim_max;
164d33e92f9SJulian Elischer 		(void)setrlimit(RLIMIT_DATA, &rlimit);
165d33e92f9SJulian Elischer 	}
1669ea6f4f0SAdrian Chadd 	while (argc-- > 0)
167bf58d635SIan Dowse 		(void)checkfilesys(*argv++);
168d33e92f9SJulian Elischer 
1698fae3551SRodney W. Grimes 	if (returntosingle)
1709ea6f4f0SAdrian Chadd 		ret = 2;
1718fae3551SRodney W. Grimes 	exit(ret);
1728fae3551SRodney W. Grimes }
1738fae3551SRodney W. Grimes 
174780a5c1eSPeter Wemm static int
175599304a4SPoul-Henning Kamp argtoi(int flag, const char *req, const char *str, int base)
1768fae3551SRodney W. Grimes {
1778fae3551SRodney W. Grimes 	char *cp;
1788fae3551SRodney W. Grimes 	int ret;
1798fae3551SRodney W. Grimes 
1808fae3551SRodney W. Grimes 	ret = (int)strtol(str, &cp, base);
1818fae3551SRodney W. Grimes 	if (cp == str || *cp)
182780a5c1eSPeter Wemm 		errx(EEXIT, "-%c flag requires a %s", flag, req);
1838fae3551SRodney W. Grimes 	return (ret);
1848fae3551SRodney W. Grimes }
1858fae3551SRodney W. Grimes 
1868fae3551SRodney W. Grimes /*
1878fae3551SRodney W. Grimes  * Check the specified file system.
1888fae3551SRodney W. Grimes  */
1898fae3551SRodney W. Grimes /* ARGSUSED */
190780a5c1eSPeter Wemm static int
191b70cd7eeSWarner Losh checkfilesys(char *filesys)
1928fae3551SRodney W. Grimes {
1931c85e6a3SKirk McKusick 	ufs2_daddr_t n_ffree, n_bfree;
1947578c6abSKirk McKusick 	struct ufs_args args;
1958fae3551SRodney W. Grimes 	struct dups *dp;
1967578c6abSKirk McKusick 	struct statfs *mntp;
197ff76fc7fSKirk McKusick 	struct stat snapdir;
198ff76fc7fSKirk McKusick 	struct group *grp;
1991c85e6a3SKirk McKusick 	ufs2_daddr_t blks;
20084fc0d7eSMaxime Henrion 	int cylno, ret;
2011c85e6a3SKirk McKusick 	ino_t files;
20284fc0d7eSMaxime Henrion 	size_t size;
2038fae3551SRodney W. Grimes 
2048fae3551SRodney W. Grimes 	cdevname = filesys;
2058fae3551SRodney W. Grimes 	if (debug && preen)
2068fae3551SRodney W. Grimes 		pwarn("starting\n");
20715fca934SKirk McKusick 	/*
20815fca934SKirk McKusick 	 * Make best effort to get the disk name. Check first to see
20915fca934SKirk McKusick 	 * if it is listed among the mounted file systems. Failing that
21015fca934SKirk McKusick 	 * check to see if it is listed in /etc/fstab.
21115fca934SKirk McKusick 	 */
21215fca934SKirk McKusick 	mntp = getmntpt(filesys);
21315fca934SKirk McKusick 	if (mntp != NULL)
21415fca934SKirk McKusick 		filesys = mntp->f_mntfromname;
21515fca934SKirk McKusick 	else
21615fca934SKirk McKusick 		filesys = blockcheck(filesys);
21715fca934SKirk McKusick 	/*
21815fca934SKirk McKusick 	 * If -F flag specified, check to see whether a background check
21915fca934SKirk McKusick 	 * is possible and needed. If possible and needed, exit with
22015fca934SKirk McKusick 	 * status zero. Otherwise exit with status non-zero. A non-zero
22115fca934SKirk McKusick 	 * exit status will cause a foreground check to be run.
22215fca934SKirk McKusick 	 */
22338375c40SKirk McKusick 	sblock_init();
22415fca934SKirk McKusick 	if (bkgrdcheck) {
22515fca934SKirk McKusick 		if ((fsreadfd = open(filesys, O_RDONLY)) < 0 || readsb(0) == 0)
22615fca934SKirk McKusick 			exit(3);	/* Cannot read superblock */
22715fca934SKirk McKusick 		close(fsreadfd);
22815fca934SKirk McKusick 		if (sblock.fs_flags & FS_NEEDSFSCK)
22915fca934SKirk McKusick 			exit(4);	/* Earlier background failed */
23015fca934SKirk McKusick 		if ((sblock.fs_flags & FS_DOSOFTDEP) == 0)
23115fca934SKirk McKusick 			exit(5);	/* Not running soft updates */
23215fca934SKirk McKusick 		size = MIBSIZE;
23315fca934SKirk McKusick 		if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0)
23415fca934SKirk McKusick 			exit(6);	/* Lacks kernel support */
23515fca934SKirk McKusick 		if ((mntp == NULL && sblock.fs_clean == 1) ||
23615fca934SKirk McKusick 		    (mntp != NULL && (sblock.fs_flags & FS_UNCLEAN) == 0))
23715fca934SKirk McKusick 			exit(7);	/* Filesystem clean, report it now */
23815fca934SKirk McKusick 		exit(0);
23915fca934SKirk McKusick 	}
240aef8d244SPawel Jakub Dawidek 	if (preen && skipclean) {
241aef8d244SPawel Jakub Dawidek 		/*
242aef8d244SPawel Jakub Dawidek 		 * If file system is gjournaled, check it here.
243aef8d244SPawel Jakub Dawidek 		 */
244aef8d244SPawel Jakub Dawidek 		if ((fsreadfd = open(filesys, O_RDONLY)) < 0 || readsb(0) == 0)
245aef8d244SPawel Jakub Dawidek 			exit(3);	/* Cannot read superblock */
246aef8d244SPawel Jakub Dawidek 		close(fsreadfd);
247aef8d244SPawel Jakub Dawidek 		if ((sblock.fs_flags & FS_GJOURNAL) != 0) {
248aef8d244SPawel Jakub Dawidek 			//printf("GJournaled file system detected on %s.\n",
249aef8d244SPawel Jakub Dawidek 			//    filesys);
250aef8d244SPawel Jakub Dawidek 			if (sblock.fs_clean == 1) {
251aef8d244SPawel Jakub Dawidek 				pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n");
252aef8d244SPawel Jakub Dawidek 				exit(0);
253aef8d244SPawel Jakub Dawidek 			}
254aef8d244SPawel Jakub Dawidek 			if ((sblock.fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0) {
255aef8d244SPawel Jakub Dawidek 				gjournal_check(filesys);
256aef8d244SPawel Jakub Dawidek 				exit(0);
257aef8d244SPawel Jakub Dawidek 			} else {
258aef8d244SPawel Jakub Dawidek 				pfatal("UNEXPECTED INCONSISTENCY, %s\n",
259aef8d244SPawel Jakub Dawidek 				    "CANNOT RUN FAST FSCK\n");
260aef8d244SPawel Jakub Dawidek 			}
261aef8d244SPawel Jakub Dawidek 		}
262aef8d244SPawel Jakub Dawidek 	}
2637578c6abSKirk McKusick 	/*
2647578c6abSKirk McKusick 	 * If we are to do a background check:
2657578c6abSKirk McKusick 	 *	Get the mount point information of the file system
2667578c6abSKirk McKusick 	 *	create snapshot file
2677578c6abSKirk McKusick 	 *	return created snapshot file
2687578c6abSKirk McKusick 	 *	if not found, clear bkgrdflag and proceed with normal fsck
2697578c6abSKirk McKusick 	 */
2707578c6abSKirk McKusick 	if (bkgrdflag) {
2717578c6abSKirk McKusick 		if (mntp == NULL) {
2727578c6abSKirk McKusick 			bkgrdflag = 0;
27338375c40SKirk McKusick 			pfatal("NOT MOUNTED, CANNOT RUN IN BACKGROUND\n");
2747578c6abSKirk McKusick 		} else if ((mntp->f_flags & MNT_SOFTDEP) == 0) {
2757578c6abSKirk McKusick 			bkgrdflag = 0;
27638375c40SKirk McKusick 			pfatal("NOT USING SOFT UPDATES, %s\n",
27738375c40SKirk McKusick 			    "CANNOT RUN IN BACKGROUND");
2787578c6abSKirk McKusick 		} else if ((mntp->f_flags & MNT_RDONLY) != 0) {
2797578c6abSKirk McKusick 			bkgrdflag = 0;
28038375c40SKirk McKusick 			pfatal("MOUNTED READ-ONLY, CANNOT RUN IN BACKGROUND\n");
28138375c40SKirk McKusick 		} else if ((fsreadfd = open(filesys, O_RDONLY)) >= 0) {
28238375c40SKirk McKusick 			if (readsb(0) != 0) {
28338375c40SKirk McKusick 				if (sblock.fs_flags & FS_NEEDSFSCK) {
28438375c40SKirk McKusick 					bkgrdflag = 0;
28538375c40SKirk McKusick 					pfatal("UNEXPECTED INCONSISTENCY, %s\n",
28638375c40SKirk McKusick 					    "CANNOT RUN IN BACKGROUND\n");
28738375c40SKirk McKusick 				}
28838375c40SKirk McKusick 				if ((sblock.fs_flags & FS_UNCLEAN) == 0 &&
28938375c40SKirk McKusick 				    skipclean && preen) {
29038375c40SKirk McKusick 					/*
29138375c40SKirk McKusick 					 * file system is clean;
29238375c40SKirk McKusick 					 * skip snapshot and report it clean
29338375c40SKirk McKusick 					 */
29438375c40SKirk McKusick 					pwarn("FILE SYSTEM CLEAN; %s\n",
29538375c40SKirk McKusick 					    "SKIPPING CHECKS");
29638375c40SKirk McKusick 					goto clean;
29738375c40SKirk McKusick 				}
29838375c40SKirk McKusick 			}
29938375c40SKirk McKusick 			close(fsreadfd);
30038375c40SKirk McKusick 		}
30138375c40SKirk McKusick 		if (bkgrdflag) {
302ff76fc7fSKirk McKusick 			snprintf(snapname, sizeof snapname, "%s/.snap",
3037578c6abSKirk McKusick 			    mntp->f_mntonname);
304ff76fc7fSKirk McKusick 			if (stat(snapname, &snapdir) < 0) {
305ff76fc7fSKirk McKusick 				if (errno != ENOENT) {
306ff76fc7fSKirk McKusick 					bkgrdflag = 0;
307ff76fc7fSKirk McKusick 					pfatal("CANNOT FIND %s %s: %s, %s\n",
308ff76fc7fSKirk McKusick 					    "SNAPSHOT DIRECTORY",
309ff76fc7fSKirk McKusick 					    snapname, strerror(errno),
310ff76fc7fSKirk McKusick 					    "CANNOT RUN IN BACKGROUND");
311ff76fc7fSKirk McKusick 				} else if ((grp = getgrnam("operator")) == 0 ||
312ff76fc7fSKirk McKusick 				    mkdir(snapname, 0770) < 0 ||
313ff76fc7fSKirk McKusick 				    chown(snapname, -1, grp->gr_gid) < 0 ||
314ff76fc7fSKirk McKusick 				    chmod(snapname, 0770) < 0) {
315ff76fc7fSKirk McKusick 					bkgrdflag = 0;
316ff76fc7fSKirk McKusick 					pfatal("CANNOT CREATE %s %s: %s, %s\n",
317ff76fc7fSKirk McKusick 					    "SNAPSHOT DIRECTORY",
318ff76fc7fSKirk McKusick 					    snapname, strerror(errno),
319ff76fc7fSKirk McKusick 					    "CANNOT RUN IN BACKGROUND");
320ff76fc7fSKirk McKusick 				}
321ff76fc7fSKirk McKusick 			} else if (!S_ISDIR(snapdir.st_mode)) {
322ff76fc7fSKirk McKusick 				bkgrdflag = 0;
323ff76fc7fSKirk McKusick 				pfatal("%s IS NOT A DIRECTORY, %s\n", snapname,
324ff76fc7fSKirk McKusick 				    "CANNOT RUN IN BACKGROUND");
325ff76fc7fSKirk McKusick 			}
326ff76fc7fSKirk McKusick 		}
327ff76fc7fSKirk McKusick 		if (bkgrdflag) {
328ff76fc7fSKirk McKusick 			snprintf(snapname, sizeof snapname,
329ff76fc7fSKirk McKusick 			    "%s/.snap/fsck_snapshot", mntp->f_mntonname);
330d90b3ef3SPoul-Henning Kamp 			memset(&args, 0, sizeof args);
3317578c6abSKirk McKusick 			args.fspec = snapname;
3327578c6abSKirk McKusick 			while (mount("ffs", mntp->f_mntonname,
3337578c6abSKirk McKusick 			    mntp->f_flags | MNT_UPDATE | MNT_SNAPSHOT,
3347578c6abSKirk McKusick 			    &args) < 0) {
3357578c6abSKirk McKusick 				if (errno == EEXIST && unlink(snapname) == 0)
3367578c6abSKirk McKusick 					continue;
3377578c6abSKirk McKusick 				bkgrdflag = 0;
33838375c40SKirk McKusick 				pfatal("CANNOT CREATE SNAPSHOT %s: %s\n",
3397578c6abSKirk McKusick 				    snapname, strerror(errno));
3407578c6abSKirk McKusick 				break;
3417578c6abSKirk McKusick 			}
3427578c6abSKirk McKusick 			if (bkgrdflag != 0)
3437578c6abSKirk McKusick 				filesys = snapname;
3447578c6abSKirk McKusick 		}
3457578c6abSKirk McKusick 	}
3467578c6abSKirk McKusick 
347780a5c1eSPeter Wemm 	switch (setup(filesys)) {
348780a5c1eSPeter Wemm 	case 0:
3498fae3551SRodney W. Grimes 		if (preen)
3508fae3551SRodney W. Grimes 			pfatal("CAN'T CHECK FILE SYSTEM.");
3516b100474SJulian Elischer 		return (0);
352780a5c1eSPeter Wemm 	case -1:
35338375c40SKirk McKusick 	clean:
354bf58d635SIan Dowse 		pwarn("clean, %ld free ", (long)(sblock.fs_cstotal.cs_nffree +
355bf58d635SIan Dowse 		    sblock.fs_frag * sblock.fs_cstotal.cs_nbfree));
356599304a4SPoul-Henning Kamp 		printf("(%lld frags, %lld blocks, %.1f%% fragmentation)\n",
357599304a4SPoul-Henning Kamp 		    (long long)sblock.fs_cstotal.cs_nffree,
358599304a4SPoul-Henning Kamp 		    (long long)sblock.fs_cstotal.cs_nbfree,
3596b100474SJulian Elischer 		    sblock.fs_cstotal.cs_nffree * 100.0 / sblock.fs_dsize);
36041cee58cSDavid Greenman 		return (0);
36141cee58cSDavid Greenman 	}
3626b100474SJulian Elischer 
3638fae3551SRodney W. Grimes 	/*
364b1897c19SJulian Elischer 	 * Cleared if any questions answered no. Used to decide if
365b1897c19SJulian Elischer 	 * the superblock should be marked clean.
366b1897c19SJulian Elischer 	 */
367b1897c19SJulian Elischer 	resolved = 1;
368b1897c19SJulian Elischer 	/*
3698fae3551SRodney W. Grimes 	 * 1: scan inodes tallying blocks used
3708fae3551SRodney W. Grimes 	 */
3718fae3551SRodney W. Grimes 	if (preen == 0) {
3728fae3551SRodney W. Grimes 		printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
3737578c6abSKirk McKusick 		if (mntp != NULL && mntp->f_flags & MNT_ROOTFS)
3748fae3551SRodney W. Grimes 			printf("** Root file system\n");
3758fae3551SRodney W. Grimes 		printf("** Phase 1 - Check Blocks and Sizes\n");
3768fae3551SRodney W. Grimes 	}
3778fae3551SRodney W. Grimes 	pass1();
3788fae3551SRodney W. Grimes 
3798fae3551SRodney W. Grimes 	/*
3808fae3551SRodney W. Grimes 	 * 1b: locate first references to duplicates, if any
3818fae3551SRodney W. Grimes 	 */
3828fae3551SRodney W. Grimes 	if (duplist) {
383b1897c19SJulian Elischer 		if (preen || usedsoftdep)
3848fae3551SRodney W. Grimes 			pfatal("INTERNAL ERROR: dups with -p");
3858fae3551SRodney W. Grimes 		printf("** Phase 1b - Rescan For More DUPS\n");
3868fae3551SRodney W. Grimes 		pass1b();
3878fae3551SRodney W. Grimes 	}
3888fae3551SRodney W. Grimes 
3898fae3551SRodney W. Grimes 	/*
3908fae3551SRodney W. Grimes 	 * 2: traverse directories from root to mark all connected directories
3918fae3551SRodney W. Grimes 	 */
3928fae3551SRodney W. Grimes 	if (preen == 0)
3938fae3551SRodney W. Grimes 		printf("** Phase 2 - Check Pathnames\n");
3948fae3551SRodney W. Grimes 	pass2();
3958fae3551SRodney W. Grimes 
3968fae3551SRodney W. Grimes 	/*
3978fae3551SRodney W. Grimes 	 * 3: scan inodes looking for disconnected directories
3988fae3551SRodney W. Grimes 	 */
3998fae3551SRodney W. Grimes 	if (preen == 0)
4008fae3551SRodney W. Grimes 		printf("** Phase 3 - Check Connectivity\n");
4018fae3551SRodney W. Grimes 	pass3();
4028fae3551SRodney W. Grimes 
4038fae3551SRodney W. Grimes 	/*
4048fae3551SRodney W. Grimes 	 * 4: scan inodes looking for disconnected files; check reference counts
4058fae3551SRodney W. Grimes 	 */
4068fae3551SRodney W. Grimes 	if (preen == 0)
4078fae3551SRodney W. Grimes 		printf("** Phase 4 - Check Reference Counts\n");
4088fae3551SRodney W. Grimes 	pass4();
4098fae3551SRodney W. Grimes 
4108fae3551SRodney W. Grimes 	/*
4118fae3551SRodney W. Grimes 	 * 5: check and repair resource counts in cylinder groups
4128fae3551SRodney W. Grimes 	 */
4138fae3551SRodney W. Grimes 	if (preen == 0)
4148fae3551SRodney W. Grimes 		printf("** Phase 5 - Check Cyl groups\n");
4158fae3551SRodney W. Grimes 	pass5();
4168fae3551SRodney W. Grimes 
4178fae3551SRodney W. Grimes 	/*
4188fae3551SRodney W. Grimes 	 * print out summary statistics
4198fae3551SRodney W. Grimes 	 */
4208fae3551SRodney W. Grimes 	n_ffree = sblock.fs_cstotal.cs_nffree;
4218fae3551SRodney W. Grimes 	n_bfree = sblock.fs_cstotal.cs_nbfree;
4227578c6abSKirk McKusick 	files = maxino - ROOTINO - sblock.fs_cstotal.cs_nifree - n_files;
4237578c6abSKirk McKusick 	blks = n_blks +
4247578c6abSKirk McKusick 	    sblock.fs_ncg * (cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
4257578c6abSKirk McKusick 	blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
4267578c6abSKirk McKusick 	blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
4277578c6abSKirk McKusick 	blks = maxfsblock - (n_ffree + sblock.fs_frag * n_bfree) - blks;
4287578c6abSKirk McKusick 	if (bkgrdflag && (files > 0 || blks > 0)) {
4297578c6abSKirk McKusick 		countdirs = sblock.fs_cstotal.cs_ndir - countdirs;
430599304a4SPoul-Henning Kamp 		pwarn("Reclaimed: %ld directories, %ld files, %lld fragments\n",
431599304a4SPoul-Henning Kamp 		    countdirs, (long)files - countdirs, (long long)blks);
4327578c6abSKirk McKusick 	}
43384fc0d7eSMaxime Henrion 	pwarn("%ld files, %jd used, %ju free ",
43484fc0d7eSMaxime Henrion 	    (long)n_files, (intmax_t)n_blks,
43584fc0d7eSMaxime Henrion 	    (uintmax_t)n_ffree + sblock.fs_frag * n_bfree);
43684fc0d7eSMaxime Henrion 	printf("(%ju frags, %ju blocks, %.1f%% fragmentation)\n",
43784fc0d7eSMaxime Henrion 	    (uintmax_t)n_ffree, (uintmax_t)n_bfree,
43884fc0d7eSMaxime Henrion 	    n_ffree * 100.0 / sblock.fs_dsize);
4398fae3551SRodney W. Grimes 	if (debug) {
4407578c6abSKirk McKusick 		if (files < 0)
4417578c6abSKirk McKusick 			printf("%d inodes missing\n", -files);
4427578c6abSKirk McKusick 		if (blks < 0)
443599304a4SPoul-Henning Kamp 			printf("%lld blocks missing\n", -(long long)blks);
4448fae3551SRodney W. Grimes 		if (duplist != NULL) {
4458fae3551SRodney W. Grimes 			printf("The following duplicate blocks remain:");
4468fae3551SRodney W. Grimes 			for (dp = duplist; dp; dp = dp->next)
447599304a4SPoul-Henning Kamp 				printf(" %lld,", (long long)dp->dup);
4488fae3551SRodney W. Grimes 			printf("\n");
4498fae3551SRodney W. Grimes 		}
4508fae3551SRodney W. Grimes 	}
4518fae3551SRodney W. Grimes 	duplist = (struct dups *)0;
4528fae3551SRodney W. Grimes 	muldup = (struct dups *)0;
4538fae3551SRodney W. Grimes 	inocleanup();
454802cd8e6SDavid Greenman 	if (fsmodified) {
455d33e92f9SJulian Elischer 		sblock.fs_time = time(NULL);
4568fae3551SRodney W. Grimes 		sbdirty();
4578fae3551SRodney W. Grimes 	}
4588fae3551SRodney W. Grimes 	if (cvtlevel && sblk.b_dirty) {
4598fae3551SRodney W. Grimes 		/*
4608fae3551SRodney W. Grimes 		 * Write out the duplicate super blocks
4618fae3551SRodney W. Grimes 		 */
4628fae3551SRodney W. Grimes 		for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
463aef8d244SPawel Jakub Dawidek 			blwrite(fswritefd, (char *)&sblock,
4641c85e6a3SKirk McKusick 			    fsbtodb(&sblock, cgsblock(&sblock, cylno)),
4651c85e6a3SKirk McKusick 			    SBLOCKSIZE);
4668fae3551SRodney W. Grimes 	}
467b1897c19SJulian Elischer 	if (rerun)
468b1897c19SJulian Elischer 		resolved = 0;
469571b29eeSPeter Wemm 
470d0e1503bSPeter Wemm 	/*
471aa5344b7SPeter Wemm 	 * Check to see if the file system is mounted read-write.
472d0e1503bSPeter Wemm 	 */
4737578c6abSKirk McKusick 	if (bkgrdflag == 0 && mntp != NULL && (mntp->f_flags & MNT_RDONLY) == 0)
474b1897c19SJulian Elischer 		resolved = 0;
475b1897c19SJulian Elischer 	ckfini(resolved);
476d33e92f9SJulian Elischer 
477d33e92f9SJulian Elischer 	for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
478d33e92f9SJulian Elischer 		if (inostathead[cylno].il_stat != NULL)
479d33e92f9SJulian Elischer 			free((char *)inostathead[cylno].il_stat);
480d33e92f9SJulian Elischer 	free((char *)inostathead);
481d33e92f9SJulian Elischer 	inostathead = NULL;
482d33e92f9SJulian Elischer 	if (fsmodified && !preen)
4838fae3551SRodney W. Grimes 		printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
48447ceb636SGuido van Rooij 	if (rerun)
48547ceb636SGuido van Rooij 		printf("\n***** PLEASE RERUN FSCK *****\n");
4867578c6abSKirk McKusick 	if (mntp != NULL) {
487d0e1503bSPeter Wemm 		/*
488d0e1503bSPeter Wemm 		 * We modified a mounted file system.  Do a mount update on
489d0e1503bSPeter Wemm 		 * it unless it is read-write, so we can continue using it
490d0e1503bSPeter Wemm 		 * as safely as possible.
491d0e1503bSPeter Wemm 		 */
4927578c6abSKirk McKusick 		if (mntp->f_flags & MNT_RDONLY) {
4938fae3551SRodney W. Grimes 			args.fspec = 0;
4948fae3551SRodney W. Grimes 			args.export.ex_flags = 0;
4958fae3551SRodney W. Grimes 			args.export.ex_root = 0;
4967578c6abSKirk McKusick 			ret = mount("ufs", mntp->f_mntonname,
4977578c6abSKirk McKusick 			    mntp->f_flags | MNT_UPDATE | MNT_RELOAD, &args);
4988fae3551SRodney W. Grimes 			if (ret == 0)
499d0e1503bSPeter Wemm 				return (0);
500d0e1503bSPeter Wemm 			pwarn("mount reload of '%s' failed: %s\n\n",
5017578c6abSKirk McKusick 			    mntp->f_mntonname, strerror(errno));
502d0e1503bSPeter Wemm 		}
503d33e92f9SJulian Elischer 		if (!fsmodified)
504d33e92f9SJulian Elischer 			return (0);
5058fae3551SRodney W. Grimes 		if (!preen)
5068fae3551SRodney W. Grimes 			printf("\n***** REBOOT NOW *****\n");
5078fae3551SRodney W. Grimes 		sync();
5088fae3551SRodney W. Grimes 		return (4);
5098fae3551SRodney W. Grimes 	}
5108fae3551SRodney W. Grimes 	return (0);
5118fae3551SRodney W. Grimes }
512571b29eeSPeter Wemm 
513571b29eeSPeter Wemm /*
51415fca934SKirk McKusick  * Get the mount point information for name.
515571b29eeSPeter Wemm  */
516571b29eeSPeter Wemm static struct statfs *
517b70cd7eeSWarner Losh getmntpt(const char *name)
518571b29eeSPeter Wemm {
519571b29eeSPeter Wemm 	struct stat devstat, mntdevstat;
520d0e1503bSPeter Wemm 	char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
521599304a4SPoul-Henning Kamp 	char *ddevname;
52215fca934SKirk McKusick 	struct statfs *mntbuf, *statfsp;
52315fca934SKirk McKusick 	int i, mntsize, isdev;
524571b29eeSPeter Wemm 
52515fca934SKirk McKusick 	if (stat(name, &devstat) != 0)
526571b29eeSPeter Wemm 		return (NULL);
52715fca934SKirk McKusick 	if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))
52815fca934SKirk McKusick 		isdev = 1;
52915fca934SKirk McKusick 	else
53015fca934SKirk McKusick 		isdev = 0;
531571b29eeSPeter Wemm 	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
532571b29eeSPeter Wemm 	for (i = 0; i < mntsize; i++) {
53315fca934SKirk McKusick 		statfsp = &mntbuf[i];
534599304a4SPoul-Henning Kamp 		ddevname = statfsp->f_mntfromname;
535599304a4SPoul-Henning Kamp 		if (*ddevname != '/') {
536d0e1503bSPeter Wemm 			strcpy(device, _PATH_DEV);
537599304a4SPoul-Henning Kamp 			strcat(device, ddevname);
53815fca934SKirk McKusick 			strcpy(statfsp->f_mntfromname, device);
53915fca934SKirk McKusick 		}
54015fca934SKirk McKusick 		if (isdev == 0) {
54115fca934SKirk McKusick 			if (strcmp(name, statfsp->f_mntonname))
54215fca934SKirk McKusick 				continue;
54315fca934SKirk McKusick 			return (statfsp);
544d0e1503bSPeter Wemm 		}
545599304a4SPoul-Henning Kamp 		if (stat(ddevname, &mntdevstat) == 0 &&
546d0e1503bSPeter Wemm 		    mntdevstat.st_rdev == devstat.st_rdev)
54715fca934SKirk McKusick 			return (statfsp);
548571b29eeSPeter Wemm 	}
54915fca934SKirk McKusick 	statfsp = NULL;
55015fca934SKirk McKusick 	return (statfsp);
551571b29eeSPeter Wemm }
5529ea6f4f0SAdrian Chadd 
5539ea6f4f0SAdrian Chadd static void
554b70cd7eeSWarner Losh usage(void)
5559ea6f4f0SAdrian Chadd {
5569ea6f4f0SAdrian Chadd         (void) fprintf(stderr,
557d3974088SDag-Erling Smørgrav             "usage: %s [-BFpfny] [-b block] [-c level] [-m mode] "
5589ea6f4f0SAdrian Chadd                         "filesystem ...\n",
5598e2e167cSMark Murray             getprogname());
5609ea6f4f0SAdrian Chadd         exit(1);
5619ea6f4f0SAdrian Chadd }
562