xref: /minix3/sbin/fsck_ext2fs/main.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
194715d8eSBen Gras /*	$NetBSD: main.c,v 1.37 2011/06/09 19:57:51 christos Exp $	*/
294715d8eSBen Gras 
394715d8eSBen Gras /*
494715d8eSBen Gras  * Copyright (c) 1980, 1986, 1993
594715d8eSBen Gras  *	The Regents of the University of California.  All rights reserved.
694715d8eSBen Gras  *
794715d8eSBen Gras  * Redistribution and use in source and binary forms, with or without
894715d8eSBen Gras  * modification, are permitted provided that the following conditions
994715d8eSBen Gras  * are met:
1094715d8eSBen Gras  * 1. Redistributions of source code must retain the above copyright
1194715d8eSBen Gras  *    notice, this list of conditions and the following disclaimer.
1294715d8eSBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
1394715d8eSBen Gras  *    notice, this list of conditions and the following disclaimer in the
1494715d8eSBen Gras  *    documentation and/or other materials provided with the distribution.
1594715d8eSBen Gras  * 3. Neither the name of the University nor the names of its contributors
1694715d8eSBen Gras  *    may be used to endorse or promote products derived from this software
1794715d8eSBen Gras  *    without specific prior written permission.
1894715d8eSBen Gras  *
1994715d8eSBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2094715d8eSBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2194715d8eSBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2294715d8eSBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2394715d8eSBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2494715d8eSBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2594715d8eSBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2694715d8eSBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2794715d8eSBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2894715d8eSBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2994715d8eSBen Gras  * SUCH DAMAGE.
3094715d8eSBen Gras  */
3194715d8eSBen Gras 
3294715d8eSBen Gras /*
3394715d8eSBen Gras  * Copyright (c) 1997 Manuel Bouyer.
3494715d8eSBen Gras  *
3594715d8eSBen Gras  * Redistribution and use in source and binary forms, with or without
3694715d8eSBen Gras  * modification, are permitted provided that the following conditions
3794715d8eSBen Gras  * are met:
3894715d8eSBen Gras  * 1. Redistributions of source code must retain the above copyright
3994715d8eSBen Gras  *    notice, this list of conditions and the following disclaimer.
4094715d8eSBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
4194715d8eSBen Gras  *    notice, this list of conditions and the following disclaimer in the
4294715d8eSBen Gras  *    documentation and/or other materials provided with the distribution.
4394715d8eSBen Gras  *
4494715d8eSBen Gras  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
4594715d8eSBen Gras  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
4694715d8eSBen Gras  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
4794715d8eSBen Gras  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
4894715d8eSBen Gras  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
4994715d8eSBen Gras  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
5094715d8eSBen Gras  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
5194715d8eSBen Gras  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
5294715d8eSBen Gras  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
5394715d8eSBen Gras  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5494715d8eSBen Gras  */
5594715d8eSBen Gras 
5694715d8eSBen Gras #include <sys/cdefs.h>
5794715d8eSBen Gras #ifndef lint
5894715d8eSBen Gras __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1993\
5994715d8eSBen Gras  The Regents of the University of California.  All rights reserved.");
6094715d8eSBen Gras #endif /* not lint */
6194715d8eSBen Gras 
6294715d8eSBen Gras #ifndef lint
6394715d8eSBen Gras #if 0
6494715d8eSBen Gras static char sccsid[] = "@(#)main.c	8.2 (Berkeley) 1/23/94";
6594715d8eSBen Gras #else
6694715d8eSBen Gras __RCSID("$NetBSD: main.c,v 1.37 2011/06/09 19:57:51 christos Exp $");
6794715d8eSBen Gras #endif
6894715d8eSBen Gras #endif /* not lint */
6994715d8eSBen Gras 
7094715d8eSBen Gras #include <sys/param.h>
7194715d8eSBen Gras #include <sys/time.h>
7294715d8eSBen Gras #include <sys/mount.h>
7394715d8eSBen Gras #include <ufs/ufs/ufsmount.h>
7494715d8eSBen Gras #include <ufs/ext2fs/ext2fs_dinode.h>
7594715d8eSBen Gras #include <ufs/ext2fs/ext2fs.h>
7694715d8eSBen Gras #include <fstab.h>
7794715d8eSBen Gras #include <stdlib.h>
7894715d8eSBen Gras #include <string.h>
7994715d8eSBen Gras #include <ctype.h>
8094715d8eSBen Gras #include <stdio.h>
8194715d8eSBen Gras #include <time.h>
8294715d8eSBen Gras #include <unistd.h>
8394715d8eSBen Gras #include <signal.h>
8494715d8eSBen Gras 
8594715d8eSBen Gras #include "fsck.h"
8694715d8eSBen Gras #include "extern.h"
8794715d8eSBen Gras #include "fsutil.h"
8894715d8eSBen Gras #include "exitvalues.h"
8994715d8eSBen Gras 
9094715d8eSBen Gras volatile sig_atomic_t	returntosingle = 0;
9194715d8eSBen Gras 
9294715d8eSBen Gras 
9394715d8eSBen Gras static int	argtoi(int, const char *, const char *, int);
9494715d8eSBen Gras static int	checkfilesys(const char *, char *, long, int);
9594715d8eSBen Gras static void	usage(void) __dead;
9694715d8eSBen Gras 
9794715d8eSBen Gras int
main(int argc,char * argv[])9894715d8eSBen Gras main(int argc, char *argv[])
9994715d8eSBen Gras {
10094715d8eSBen Gras 	int ch;
10194715d8eSBen Gras 	int ret = FSCK_EXIT_OK;
10294715d8eSBen Gras 
10394715d8eSBen Gras 	ckfinish = ckfini;
10494715d8eSBen Gras 	sync();
10594715d8eSBen Gras 	skipclean = 1;
10694715d8eSBen Gras 	while ((ch = getopt(argc, argv, "b:dfm:npPqUy")) != -1) {
10794715d8eSBen Gras 		switch (ch) {
10894715d8eSBen Gras 		case 'b':
10994715d8eSBen Gras 			skipclean = 0;
11094715d8eSBen Gras 			bflag = argtoi('b', "number", optarg, 10);
11194715d8eSBen Gras 			printf("Alternate super block location: %d\n", bflag);
11294715d8eSBen Gras 			break;
11394715d8eSBen Gras 
11494715d8eSBen Gras 		case 'd':
11594715d8eSBen Gras 			debug++;
11694715d8eSBen Gras 			break;
11794715d8eSBen Gras 
11894715d8eSBen Gras 		case 'f':
11994715d8eSBen Gras 			skipclean = 0;
12094715d8eSBen Gras 			break;
12194715d8eSBen Gras 
12294715d8eSBen Gras 		case 'm':
12394715d8eSBen Gras 			lfmode = argtoi('m', "mode", optarg, 8);
12494715d8eSBen Gras 			if (lfmode &~ 07777)
12594715d8eSBen Gras 				errexit("bad mode to -m: %o", lfmode);
12694715d8eSBen Gras 			printf("** lost+found creation mode %o\n", lfmode);
12794715d8eSBen Gras 			break;
12894715d8eSBen Gras 
12994715d8eSBen Gras 		case 'n':
13094715d8eSBen Gras 			nflag++;
13194715d8eSBen Gras 			yflag = 0;
13294715d8eSBen Gras 			break;
13394715d8eSBen Gras 
13494715d8eSBen Gras 		case 'p':
13594715d8eSBen Gras 			preen++;
13694715d8eSBen Gras 			break;
13794715d8eSBen Gras 
13894715d8eSBen Gras 		case 'P':
13994715d8eSBen Gras 			/* Progress meter not implemented. */
14094715d8eSBen Gras 			break;
14194715d8eSBen Gras 
14294715d8eSBen Gras 		case 'q':		/* Quiet not implemented */
14394715d8eSBen Gras 			break;
14494715d8eSBen Gras 
14594715d8eSBen Gras #ifndef SMALL
14694715d8eSBen Gras 		case 'U':
14794715d8eSBen Gras 			Uflag++;
14894715d8eSBen Gras 			break;
14994715d8eSBen Gras #endif
15094715d8eSBen Gras 
15194715d8eSBen Gras 		case 'y':
15294715d8eSBen Gras 			yflag++;
15394715d8eSBen Gras 			nflag = 0;
15494715d8eSBen Gras 			break;
15594715d8eSBen Gras 
15694715d8eSBen Gras 		default:
15794715d8eSBen Gras 			usage();
15894715d8eSBen Gras 		}
15994715d8eSBen Gras 	}
16094715d8eSBen Gras 
16194715d8eSBen Gras 	argc -= optind;
16294715d8eSBen Gras 	argv += optind;
16394715d8eSBen Gras 
16494715d8eSBen Gras 	if (!argc)
16594715d8eSBen Gras 		usage();
16694715d8eSBen Gras 
16794715d8eSBen Gras 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
16894715d8eSBen Gras 		(void)signal(SIGINT, catch);
16994715d8eSBen Gras 	if (preen)
17094715d8eSBen Gras 		(void)signal(SIGQUIT, catchquit);
17194715d8eSBen Gras 
17294715d8eSBen Gras 	while (argc-- > 0) {
17394715d8eSBen Gras 		int nret = checkfilesys(blockcheck(*argv++), 0, 0L, 0);
17494715d8eSBen Gras 		if (ret < nret)
17594715d8eSBen Gras 			ret = nret;
17694715d8eSBen Gras 	}
17794715d8eSBen Gras 
17894715d8eSBen Gras 	return returntosingle ? FSCK_EXIT_UNRESOLVED : ret;
17994715d8eSBen Gras }
18094715d8eSBen Gras 
18194715d8eSBen Gras static int
argtoi(int flag,const char * req,const char * str,int base)18294715d8eSBen Gras argtoi(int flag, const char *req, const char *str, int base)
18394715d8eSBen Gras {
18494715d8eSBen Gras 	char *cp;
18594715d8eSBen Gras 	int ret;
18694715d8eSBen Gras 
18794715d8eSBen Gras 	ret = (int)strtol(str, &cp, base);
18894715d8eSBen Gras 	if (cp == str || *cp)
18994715d8eSBen Gras 		errexit("-%c flag requires a %s", flag, req);
19094715d8eSBen Gras 	return (ret);
19194715d8eSBen Gras }
19294715d8eSBen Gras 
19394715d8eSBen Gras /*
19494715d8eSBen Gras  * Check the specified filesystem.
19594715d8eSBen Gras  */
19694715d8eSBen Gras /* ARGSUSED */
19794715d8eSBen Gras static int
checkfilesys(const char * filesys,char * mntpt,long auxdata,int child)19894715d8eSBen Gras checkfilesys(const char *filesys, char *mntpt, long auxdata, int child)
19994715d8eSBen Gras {
20094715d8eSBen Gras 	daddr_t n_bfree;
20194715d8eSBen Gras 	struct dups *dp;
20294715d8eSBen Gras 	struct zlncnt *zlnp;
20394715d8eSBen Gras 	int i;
20494715d8eSBen Gras 
20594715d8eSBen Gras 	if (preen && child)
20694715d8eSBen Gras 		(void)signal(SIGQUIT, voidquit);
20794715d8eSBen Gras 	setcdevname(filesys, preen);
20894715d8eSBen Gras 	if (debug && preen)
20994715d8eSBen Gras 		pwarn("starting\n");
21094715d8eSBen Gras 	switch (setup(filesys)) {
21194715d8eSBen Gras 	case 0:
21294715d8eSBen Gras 		if (preen)
21394715d8eSBen Gras 			pfatal("CAN'T CHECK FILE SYSTEM.");
21494715d8eSBen Gras 	case -1:
21594715d8eSBen Gras 		return FSCK_EXIT_OK;
21694715d8eSBen Gras 	}
21794715d8eSBen Gras 	/*
21894715d8eSBen Gras 	 * 1: scan inodes tallying blocks used
21994715d8eSBen Gras 	 */
22094715d8eSBen Gras 	if (preen == 0) {
22194715d8eSBen Gras 		if (sblock.e2fs.e2fs_rev > E2FS_REV0) {
22294715d8eSBen Gras 			printf("** Last Mounted on %s\n",
22394715d8eSBen Gras 			    sblock.e2fs.e2fs_fsmnt);
22494715d8eSBen Gras 		}
22594715d8eSBen Gras 		if (hotroot())
22694715d8eSBen Gras 			printf("** Root file system\n");
22794715d8eSBen Gras 		printf("** Phase 1 - Check Blocks and Sizes\n");
22894715d8eSBen Gras 	}
22994715d8eSBen Gras 	pass1();
23094715d8eSBen Gras 
23194715d8eSBen Gras 	/*
23294715d8eSBen Gras 	 * 1b: locate first references to duplicates, if any
23394715d8eSBen Gras 	 */
23494715d8eSBen Gras 	if (duplist) {
23594715d8eSBen Gras 		if (preen)
23694715d8eSBen Gras 			pfatal("INTERNAL ERROR: dups with -p");
23794715d8eSBen Gras 		printf("** Phase 1b - Rescan For More DUPS\n");
23894715d8eSBen Gras 		pass1b();
23994715d8eSBen Gras 	}
24094715d8eSBen Gras 
24194715d8eSBen Gras 	/*
24294715d8eSBen Gras 	 * 2: traverse directories from root to mark all connected directories
24394715d8eSBen Gras 	 */
24494715d8eSBen Gras 	if (preen == 0)
24594715d8eSBen Gras 		printf("** Phase 2 - Check Pathnames\n");
24694715d8eSBen Gras 	pass2();
24794715d8eSBen Gras 
24894715d8eSBen Gras 	/*
24994715d8eSBen Gras 	 * 3: scan inodes looking for disconnected directories
25094715d8eSBen Gras 	 */
25194715d8eSBen Gras 	if (preen == 0)
25294715d8eSBen Gras 		printf("** Phase 3 - Check Connectivity\n");
25394715d8eSBen Gras 	pass3();
25494715d8eSBen Gras 
25594715d8eSBen Gras 	/*
25694715d8eSBen Gras 	 * 4: scan inodes looking for disconnected files; check reference counts
25794715d8eSBen Gras 	 */
25894715d8eSBen Gras 	if (preen == 0)
25994715d8eSBen Gras 		printf("** Phase 4 - Check Reference Counts\n");
26094715d8eSBen Gras 	pass4();
26194715d8eSBen Gras 
26294715d8eSBen Gras 	/*
26394715d8eSBen Gras 	 * 5: check and repair resource counts in cylinder groups
26494715d8eSBen Gras 	 */
26594715d8eSBen Gras 	if (preen == 0)
26694715d8eSBen Gras 		printf("** Phase 5 - Check Cyl groups\n");
26794715d8eSBen Gras 	pass5();
26894715d8eSBen Gras 
26994715d8eSBen Gras 	/*
27094715d8eSBen Gras 	 * print out summary statistics
27194715d8eSBen Gras 	 */
27294715d8eSBen Gras 	n_bfree = sblock.e2fs.e2fs_fbcount;
27394715d8eSBen Gras 
27494715d8eSBen Gras 	pwarn("%lld files, %lld used, %lld free\n",
27594715d8eSBen Gras 	    (long long)n_files, (long long)n_blks, (long long)n_bfree);
27694715d8eSBen Gras 	if (debug &&
27794715d8eSBen Gras 		/* 9 reserved and unused inodes in FS */
27894715d8eSBen Gras 	    (n_files -= maxino - 9 - sblock.e2fs.e2fs_ficount))
27994715d8eSBen Gras 		printf("%lld files missing\n", (long long)n_files);
28094715d8eSBen Gras 	if (debug) {
28194715d8eSBen Gras 		for (i = 0; i < sblock.e2fs_ncg; i++)
28294715d8eSBen Gras 			n_blks +=  cgoverhead(i);
28394715d8eSBen Gras 		n_blks += sblock.e2fs.e2fs_first_dblock;
28494715d8eSBen Gras 		if (n_blks -= maxfsblock - n_bfree)
28594715d8eSBen Gras 			printf("%lld blocks missing\n", (long long)n_blks);
28694715d8eSBen Gras 		if (duplist != NULL) {
28794715d8eSBen Gras 			printf("The following duplicate blocks remain:");
28894715d8eSBen Gras 			for (dp = duplist; dp; dp = dp->next)
28994715d8eSBen Gras 				printf(" %lld,", (long long)dp->dup);
29094715d8eSBen Gras 			printf("\n");
29194715d8eSBen Gras 		}
29294715d8eSBen Gras 		if (zlnhead != NULL) {
29394715d8eSBen Gras 			printf("The following zero link count inodes remain:");
29494715d8eSBen Gras 			for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
29594715d8eSBen Gras 				printf(" %llu,",
29694715d8eSBen Gras 				    (unsigned long long)zlnp->zlncnt);
29794715d8eSBen Gras 			printf("\n");
29894715d8eSBen Gras 		}
29994715d8eSBen Gras 	}
30094715d8eSBen Gras 	zlnhead = (struct zlncnt *)0;
30194715d8eSBen Gras 	duplist = (struct dups *)0;
30294715d8eSBen Gras 	muldup = (struct dups *)0;
30394715d8eSBen Gras 	inocleanup();
30494715d8eSBen Gras 	if (fsmodified) {
30594715d8eSBen Gras 		time_t t;
30694715d8eSBen Gras 		(void)time(&t);
30794715d8eSBen Gras 		sblock.e2fs.e2fs_wtime = t;
30894715d8eSBen Gras 		sblock.e2fs.e2fs_lastfsck = t;
30994715d8eSBen Gras 		sbdirty();
31094715d8eSBen Gras 	}
31194715d8eSBen Gras 	ckfini(1);
31294715d8eSBen Gras 	free(blockmap);
31394715d8eSBen Gras 	free(statemap);
31494715d8eSBen Gras 	free((char *)lncntp);
31594715d8eSBen Gras 	if (!fsmodified)
31694715d8eSBen Gras 		return FSCK_EXIT_OK;
31794715d8eSBen Gras 	if (!preen)
31894715d8eSBen Gras 		printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
31994715d8eSBen Gras 	if (rerun)
32094715d8eSBen Gras 		printf("\n***** PLEASE RERUN FSCK *****\n");
321*84d9c625SLionel Sambuc #if !defined(__minix)
32294715d8eSBen Gras 	if (hotroot()) {
32394715d8eSBen Gras 		struct statvfs stfs_buf;
32494715d8eSBen Gras 		/*
32594715d8eSBen Gras 		 * We modified the root.  Do a mount update on
32694715d8eSBen Gras 		 * it, unless it is read-write, so we can continue.
32794715d8eSBen Gras 		 */
32894715d8eSBen Gras 		if (statvfs("/", &stfs_buf) == 0) {
32994715d8eSBen Gras 			long flags = stfs_buf.f_flag;
33094715d8eSBen Gras 			struct ufs_args args;
33194715d8eSBen Gras 
33294715d8eSBen Gras 			if (flags & MNT_RDONLY) {
33394715d8eSBen Gras 				args.fspec = 0;
33494715d8eSBen Gras 				flags |= MNT_UPDATE | MNT_RELOAD;
33594715d8eSBen Gras 				if (mount(MOUNT_EXT2FS, "/", flags,
33694715d8eSBen Gras 				    &args, sizeof args) == 0)
33794715d8eSBen Gras 					return FSCK_EXIT_OK;
33894715d8eSBen Gras 			}
33994715d8eSBen Gras 		}
34094715d8eSBen Gras 		if (!preen)
34194715d8eSBen Gras 			printf("\n***** REBOOT NOW *****\n");
34294715d8eSBen Gras 		sync();
34394715d8eSBen Gras 		return FSCK_EXIT_ROOT_CHANGED;
34494715d8eSBen Gras 	}
345*84d9c625SLionel Sambuc #endif /* !defined(__minix) */
34694715d8eSBen Gras 	return FSCK_EXIT_OK;
34794715d8eSBen Gras }
34894715d8eSBen Gras 
34994715d8eSBen Gras static void
usage(void)35094715d8eSBen Gras usage(void)
35194715d8eSBen Gras {
35294715d8eSBen Gras 
35394715d8eSBen Gras 	(void) fprintf(stderr,
35494715d8eSBen Gras 	    "usage: %s [-dfnpUy] [-b block] [-c level] [-m mode] filesystem ...\n",
35594715d8eSBen Gras 	    getprogname());
35694715d8eSBen Gras 	exit(FSCK_EXIT_USAGE);
35794715d8eSBen Gras }
35894715d8eSBen Gras 
359