main.c revision 55275
11558Srgrimes/*
21558Srgrimes * Copyright (c) 1980, 1986, 1993
31558Srgrimes *	The Regents of the University of California.  All rights reserved.
41558Srgrimes *
51558Srgrimes * Redistribution and use in source and binary forms, with or without
61558Srgrimes * modification, are permitted provided that the following conditions
71558Srgrimes * are met:
81558Srgrimes * 1. Redistributions of source code must retain the above copyright
91558Srgrimes *    notice, this list of conditions and the following disclaimer.
101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111558Srgrimes *    notice, this list of conditions and the following disclaimer in the
121558Srgrimes *    documentation and/or other materials provided with the distribution.
131558Srgrimes * 3. All advertising materials mentioning features or use of this software
141558Srgrimes *    must display the following acknowledgement:
151558Srgrimes *	This product includes software developed by the University of
161558Srgrimes *	California, Berkeley and its contributors.
171558Srgrimes * 4. Neither the name of the University nor the names of its contributors
181558Srgrimes *    may be used to endorse or promote products derived from this software
191558Srgrimes *    without specific prior written permission.
201558Srgrimes *
211558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311558Srgrimes * SUCH DAMAGE.
321558Srgrimes */
331558Srgrimes
341558Srgrimes#ifndef lint
357585Sbdestatic const char copyright[] =
361558Srgrimes"@(#) Copyright (c) 1980, 1986, 1993\n\
371558Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381558Srgrimes#endif /* not lint */
391558Srgrimes
401558Srgrimes#ifndef lint
4141477Sjulian#if 0
4241477Sjulianstatic char sccsid[] = "@(#)main.c	8.6 (Berkeley) 5/14/95";
4341477Sjulian#endif
4441477Sjulianstatic const char rcsid[] =
4550476Speter  "$FreeBSD: head/sbin/fsck_ffs/main.c 55275 1999-12-30 16:32:40Z peter $";
461558Srgrimes#endif /* not lint */
471558Srgrimes
481558Srgrimes#include <sys/param.h>
4955275Speter#include <sys/stat.h>
501558Srgrimes#include <sys/time.h>
511558Srgrimes#include <sys/mount.h>
5240918Smjacob#include <sys/resource.h>
5323675Speter
541558Srgrimes#include <ufs/ufs/dinode.h>
5523675Speter#include <ufs/ufs/ufsmount.h>
561558Srgrimes#include <ufs/ffs/fs.h>
5723675Speter
5823675Speter#include <err.h>
591558Srgrimes#include <fstab.h>
6023675Speter
611558Srgrimes#include "fsck.h"
621558Srgrimes
6323675Speterstatic int argtoi __P((int flag, char *req, char *str, int base));
6423675Speterstatic int docheck __P((struct fstab *fsp));
6523675Speterstatic int checkfilesys __P((char *filesys, char *mntpt, long auxdata,
6623675Speter		int child));
6755275Speterstatic struct statfs *getmntpt __P((const char *));
6823675Speterint main __P((int argc, char *argv[]));
6923675Speter
707585Sbdeint
711558Srgrimesmain(argc, argv)
721558Srgrimes	int	argc;
731558Srgrimes	char	*argv[];
741558Srgrimes{
751558Srgrimes	int ch;
761558Srgrimes	int ret, maxrun = 0;
7741474Sjulian	struct rlimit rlimit;
781558Srgrimes
791558Srgrimes	sync();
8023795Sbde	while ((ch = getopt(argc, argv, "dfpnNyYb:c:l:m:")) != -1) {
811558Srgrimes		switch (ch) {
821558Srgrimes		case 'p':
831558Srgrimes			preen++;
841558Srgrimes			break;
851558Srgrimes
861558Srgrimes		case 'b':
871558Srgrimes			bflag = argtoi('b', "number", optarg, 10);
881558Srgrimes			printf("Alternate super block location: %d\n", bflag);
891558Srgrimes			break;
901558Srgrimes
911558Srgrimes		case 'c':
921558Srgrimes			cvtlevel = argtoi('c', "conversion level", optarg, 10);
931558Srgrimes			break;
948871Srgrimes
951558Srgrimes		case 'd':
961558Srgrimes			debug++;
971558Srgrimes			break;
981558Srgrimes
992153Sdg		case 'f':
1002153Sdg			fflag++;
1012153Sdg			break;
1022153Sdg
1031558Srgrimes		case 'l':
1041558Srgrimes			maxrun = argtoi('l', "number", optarg, 10);
1051558Srgrimes			break;
1061558Srgrimes
1071558Srgrimes		case 'm':
1081558Srgrimes			lfmode = argtoi('m', "mode", optarg, 8);
1091558Srgrimes			if (lfmode &~ 07777)
11023675Speter				errx(EEXIT, "bad mode to -m: %o", lfmode);
1111558Srgrimes			printf("** lost+found creation mode %o\n", lfmode);
1121558Srgrimes			break;
1131558Srgrimes
1141558Srgrimes		case 'n':
1151558Srgrimes		case 'N':
1161558Srgrimes			nflag++;
1171558Srgrimes			yflag = 0;
1181558Srgrimes			break;
1191558Srgrimes
1201558Srgrimes		case 'y':
1211558Srgrimes		case 'Y':
1221558Srgrimes			yflag++;
1231558Srgrimes			nflag = 0;
1241558Srgrimes			break;
1251558Srgrimes
1261558Srgrimes		default:
12723675Speter			errx(EEXIT, "%c option?", ch);
1281558Srgrimes		}
1291558Srgrimes	}
1301558Srgrimes	argc -= optind;
1311558Srgrimes	argv += optind;
1321558Srgrimes	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1331558Srgrimes		(void)signal(SIGINT, catch);
1341558Srgrimes	if (preen)
1351558Srgrimes		(void)signal(SIGQUIT, catchquit);
13641474Sjulian	/*
13741474Sjulian	 * Push up our allowed memory limit so we can cope
13841474Sjulian	 * with huge filesystems.
13941474Sjulian	 */
14041474Sjulian	if (getrlimit(RLIMIT_DATA, &rlimit) == 0) {
14141474Sjulian		rlimit.rlim_cur = rlimit.rlim_max;
14241474Sjulian		(void)setrlimit(RLIMIT_DATA, &rlimit);
14341474Sjulian	}
1441558Srgrimes	if (argc) {
14541474Sjulian		while (argc-- > 0) {
14641474Sjulian			char *path = blockcheck(*argv);
14741474Sjulian
14841474Sjulian			if (path == NULL)
14941474Sjulian				pfatal("Can't check %s\n", *argv);
15041474Sjulian			else
15141474Sjulian				(void)checkfilesys(path, 0, 0L, 0);
15241474Sjulian			++argv;
15341474Sjulian		}
1541558Srgrimes		exit(0);
1551558Srgrimes	}
1561558Srgrimes	ret = checkfstab(preen, maxrun, docheck, checkfilesys);
1571558Srgrimes	if (returntosingle)
1581558Srgrimes		exit(2);
1591558Srgrimes	exit(ret);
1601558Srgrimes}
1611558Srgrimes
16223675Speterstatic int
1631558Srgrimesargtoi(flag, req, str, base)
1641558Srgrimes	int flag;
1651558Srgrimes	char *req, *str;
1661558Srgrimes	int base;
1671558Srgrimes{
1681558Srgrimes	char *cp;
1691558Srgrimes	int ret;
1701558Srgrimes
1711558Srgrimes	ret = (int)strtol(str, &cp, base);
1721558Srgrimes	if (cp == str || *cp)
17323675Speter		errx(EEXIT, "-%c flag requires a %s", flag, req);
1741558Srgrimes	return (ret);
1751558Srgrimes}
1761558Srgrimes
1771558Srgrimes/*
1781558Srgrimes * Determine whether a filesystem should be checked.
1791558Srgrimes */
18023675Speterstatic int
1811558Srgrimesdocheck(fsp)
1821558Srgrimes	register struct fstab *fsp;
1831558Srgrimes{
1841558Srgrimes
1851558Srgrimes	if (strcmp(fsp->fs_vfstype, "ufs") ||
1861558Srgrimes	    (strcmp(fsp->fs_type, FSTAB_RW) &&
1871558Srgrimes	     strcmp(fsp->fs_type, FSTAB_RO)) ||
1881558Srgrimes	    fsp->fs_passno == 0)
1891558Srgrimes		return (0);
1901558Srgrimes	return (1);
1911558Srgrimes}
1921558Srgrimes
1931558Srgrimes/*
1941558Srgrimes * Check the specified filesystem.
1951558Srgrimes */
1961558Srgrimes/* ARGSUSED */
19723675Speterstatic int
1981558Srgrimescheckfilesys(filesys, mntpt, auxdata, child)
1991558Srgrimes	char *filesys, *mntpt;
2001558Srgrimes	long auxdata;
2017585Sbde	int child;
2021558Srgrimes{
20323675Speter	ufs_daddr_t n_ffree, n_bfree;
2041558Srgrimes	struct dups *dp;
2051558Srgrimes	struct zlncnt *zlnp;
20655275Speter	struct statfs *mntbuf;
20755275Speter	int cylno;
2081558Srgrimes
2091558Srgrimes	if (preen && child)
2101558Srgrimes		(void)signal(SIGQUIT, voidquit);
2111558Srgrimes	cdevname = filesys;
2121558Srgrimes	if (debug && preen)
2131558Srgrimes		pwarn("starting\n");
21423675Speter	switch (setup(filesys)) {
21523675Speter	case 0:
2161558Srgrimes		if (preen)
2171558Srgrimes			pfatal("CAN'T CHECK FILE SYSTEM.");
21841477Sjulian		return (0);
21923675Speter	case -1:
22041477Sjulian		pwarn("clean, %ld free ", sblock.fs_cstotal.cs_nffree +
22141477Sjulian		    sblock.fs_frag * sblock.fs_cstotal.cs_nbfree);
22241477Sjulian		printf("(%d frags, %d blocks, %.1f%% fragmentation)\n",
22341477Sjulian		    sblock.fs_cstotal.cs_nffree, sblock.fs_cstotal.cs_nbfree,
22441477Sjulian		    sblock.fs_cstotal.cs_nffree * 100.0 / sblock.fs_dsize);
22531904Sbde		return (0);
2262153Sdg	}
22741477Sjulian
2281558Srgrimes	/*
22955275Speter	 * get the mount point information of the filesystem, if
23055275Speter	 * it is available.
23155275Speter	 */
23255275Speter	mntbuf = getmntpt(filesys);
23355275Speter
23455275Speter	/*
23534266Sjulian	 * Cleared if any questions answered no. Used to decide if
23634266Sjulian	 * the superblock should be marked clean.
23734266Sjulian	 */
23834266Sjulian	resolved = 1;
23934266Sjulian	/*
2401558Srgrimes	 * 1: scan inodes tallying blocks used
2411558Srgrimes	 */
2421558Srgrimes	if (preen == 0) {
2431558Srgrimes		printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
24455275Speter		if (mntbuf != NULL && mntbuf->f_flags & MNT_ROOTFS)
2451558Srgrimes			printf("** Root file system\n");
2461558Srgrimes		printf("** Phase 1 - Check Blocks and Sizes\n");
2471558Srgrimes	}
2481558Srgrimes	pass1();
2491558Srgrimes
2501558Srgrimes	/*
2511558Srgrimes	 * 1b: locate first references to duplicates, if any
2521558Srgrimes	 */
2531558Srgrimes	if (duplist) {
25434266Sjulian		if (preen || usedsoftdep)
2551558Srgrimes			pfatal("INTERNAL ERROR: dups with -p");
2561558Srgrimes		printf("** Phase 1b - Rescan For More DUPS\n");
2571558Srgrimes		pass1b();
2581558Srgrimes	}
2591558Srgrimes
2601558Srgrimes	/*
2611558Srgrimes	 * 2: traverse directories from root to mark all connected directories
2621558Srgrimes	 */
2631558Srgrimes	if (preen == 0)
2641558Srgrimes		printf("** Phase 2 - Check Pathnames\n");
2651558Srgrimes	pass2();
2661558Srgrimes
2671558Srgrimes	/*
2681558Srgrimes	 * 3: scan inodes looking for disconnected directories
2691558Srgrimes	 */
2701558Srgrimes	if (preen == 0)
2711558Srgrimes		printf("** Phase 3 - Check Connectivity\n");
2721558Srgrimes	pass3();
2731558Srgrimes
2741558Srgrimes	/*
2751558Srgrimes	 * 4: scan inodes looking for disconnected files; check reference counts
2761558Srgrimes	 */
2771558Srgrimes	if (preen == 0)
2781558Srgrimes		printf("** Phase 4 - Check Reference Counts\n");
2791558Srgrimes	pass4();
2801558Srgrimes
2811558Srgrimes	/*
2821558Srgrimes	 * 5: check and repair resource counts in cylinder groups
2831558Srgrimes	 */
2841558Srgrimes	if (preen == 0)
2851558Srgrimes		printf("** Phase 5 - Check Cyl groups\n");
2861558Srgrimes	pass5();
2871558Srgrimes
2881558Srgrimes	/*
2891558Srgrimes	 * print out summary statistics
2901558Srgrimes	 */
2911558Srgrimes	n_ffree = sblock.fs_cstotal.cs_nffree;
2921558Srgrimes	n_bfree = sblock.fs_cstotal.cs_nbfree;
2931558Srgrimes	pwarn("%ld files, %ld used, %ld free ",
2941558Srgrimes	    n_files, n_blks, n_ffree + sblock.fs_frag * n_bfree);
29531904Sbde	printf("(%d frags, %d blocks, %.1f%% fragmentation)\n",
29631904Sbde	    n_ffree, n_bfree, n_ffree * 100.0 / sblock.fs_dsize);
2971558Srgrimes	if (debug &&
2981558Srgrimes	    (n_files -= maxino - ROOTINO - sblock.fs_cstotal.cs_nifree))
29931904Sbde		printf("%d files missing\n", n_files);
3001558Srgrimes	if (debug) {
3011558Srgrimes		n_blks += sblock.fs_ncg *
3021558Srgrimes			(cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
3031558Srgrimes		n_blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
3041558Srgrimes		n_blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
3051558Srgrimes		if (n_blks -= maxfsblock - (n_ffree + sblock.fs_frag * n_bfree))
30631904Sbde			printf("%d blocks missing\n", n_blks);
3071558Srgrimes		if (duplist != NULL) {
3081558Srgrimes			printf("The following duplicate blocks remain:");
3091558Srgrimes			for (dp = duplist; dp; dp = dp->next)
31031904Sbde				printf(" %d,", dp->dup);
3111558Srgrimes			printf("\n");
3121558Srgrimes		}
3131558Srgrimes		if (zlnhead != NULL) {
3141558Srgrimes			printf("The following zero link count inodes remain:");
3151558Srgrimes			for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
31631904Sbde				printf(" %u,", zlnp->zlncnt);
3171558Srgrimes			printf("\n");
3181558Srgrimes		}
3191558Srgrimes	}
3201558Srgrimes	zlnhead = (struct zlncnt *)0;
3211558Srgrimes	duplist = (struct dups *)0;
3221558Srgrimes	muldup = (struct dups *)0;
3231558Srgrimes	inocleanup();
3242179Sdg	if (fsmodified) {
32541474Sjulian		sblock.fs_time = time(NULL);
3261558Srgrimes		sbdirty();
3271558Srgrimes	}
3281558Srgrimes	if (cvtlevel && sblk.b_dirty) {
3298871Srgrimes		/*
3301558Srgrimes		 * Write out the duplicate super blocks
3311558Srgrimes		 */
3321558Srgrimes		for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
3331558Srgrimes			bwrite(fswritefd, (char *)&sblock,
3341558Srgrimes			    fsbtodb(&sblock, cgsblock(&sblock, cylno)), SBSIZE);
3351558Srgrimes	}
33634266Sjulian	if (rerun)
33734266Sjulian		resolved = 0;
33855275Speter
33955275Speter	/* Check to see if the filesystem if mounted read-write */
34055275Speter	if (mntbuf != NULL && (mntbuf->f_flags & MNT_RDONLY) == 0)
34155275Speter		resolved = 0;
34234266Sjulian	ckfini(resolved);
34341474Sjulian
34441474Sjulian	for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
34541474Sjulian		if (inostathead[cylno].il_stat != NULL)
34641474Sjulian			free((char *)inostathead[cylno].il_stat);
34741474Sjulian	free((char *)inostathead);
34841474Sjulian	inostathead = NULL;
34941474Sjulian	if (fsmodified && !preen)
3501558Srgrimes		printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
35118808Sguido	if (rerun)
35218808Sguido		printf("\n***** PLEASE RERUN FSCK *****\n");
35355275Speter
35455275Speter	/*
35555275Speter	 * Always do a mount update if the current filesystem
35655275Speter	 * is mounted read-only.
35755275Speter	 */
35855275Speter	if (mntbuf != NULL && mntbuf->f_flags & MNT_RDONLY) {
35923675Speter		struct ufs_args args;
36023675Speter		int ret;
36155275Speter
36255275Speter		args.fspec = 0;
36355275Speter		args.export.ex_flags = 0;
36455275Speter		args.export.ex_root = 0;
36555275Speter		mntbuf->f_flags |= MNT_UPDATE | MNT_RELOAD;
36655275Speter		ret = mount("ufs", mntbuf->f_mntonname , mntbuf->f_flags, &args);
36755275Speter		if (ret < 0)
36855275Speter			perror("mount");
36955275Speter		if (ret == 0)
37055275Speter			return 0;
37141474Sjulian		if (!fsmodified)
37241474Sjulian			return (0);
3731558Srgrimes		if (!preen)
3741558Srgrimes			printf("\n***** REBOOT NOW *****\n");
3751558Srgrimes		sync();
3761558Srgrimes		return (4);
3771558Srgrimes	}
3781558Srgrimes	return (0);
3791558Srgrimes}
38055275Speter
38155275Speter/*
38255275Speter * get the directory the device is mounted on.
38355275Speter */
38455275Speterstatic struct statfs *
38555275Spetergetmntpt(name)
38655275Speter	const char *name;
38755275Speter{
38855275Speter	struct statfs *mntbuf;
38955275Speter	struct stat devstat, mntdevstat;
39055275Speter	char device[MAXPATHLEN];
39155275Speter	int i, mntsize;
39255275Speter
39355275Speter	if (realpath(name, device) == NULL)
39455275Speter		return (NULL);
39555275Speter
39655275Speter	if (stat(device, &devstat) != 0 ||
39755275Speter	    !(S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode)))
39855275Speter		return (NULL);
39955275Speter
40055275Speter	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
40155275Speter	for (i = 0; i < mntsize; i++) {
40255275Speter		if (realpath(mntbuf[i].f_mntfromname, device) == NULL ||
40355275Speter		    stat(device, &mntdevstat) != 0 ||
40455275Speter		    !(S_ISCHR(mntdevstat.st_mode) ||
40555275Speter		      S_ISBLK(mntdevstat.st_mode)))
40655275Speter			continue;
40755275Speter		if (mntdevstat.st_rdev == devstat.st_rdev)
40855275Speter			return (&mntbuf[i]);
40955275Speter	}
41055275Speter	return (NULL);
41155275Speter}
412