Home | History | Annotate | Line # | Download | only in fsck_ext2fs
main.c revision 1.16
      1  1.16   bouyer /*	$NetBSD: main.c,v 1.16 2004/03/22 19:46:53 bouyer Exp $	*/
      2   1.1   bouyer 
      3   1.1   bouyer /*
      4   1.1   bouyer  * Copyright (c) 1980, 1986, 1993
      5   1.1   bouyer  *	The Regents of the University of California.  All rights reserved.
      6   1.1   bouyer  *
      7   1.1   bouyer  * Redistribution and use in source and binary forms, with or without
      8   1.1   bouyer  * modification, are permitted provided that the following conditions
      9   1.1   bouyer  * are met:
     10   1.1   bouyer  * 1. Redistributions of source code must retain the above copyright
     11   1.1   bouyer  *    notice, this list of conditions and the following disclaimer.
     12   1.1   bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1   bouyer  *    notice, this list of conditions and the following disclaimer in the
     14   1.1   bouyer  *    documentation and/or other materials provided with the distribution.
     15  1.12      agc  * 3. Neither the name of the University nor the names of its contributors
     16  1.12      agc  *    may be used to endorse or promote products derived from this software
     17  1.12      agc  *    without specific prior written permission.
     18  1.12      agc  *
     19  1.12      agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  1.12      agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.12      agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.12      agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  1.12      agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.12      agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.12      agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.12      agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.12      agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.12      agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.12      agc  * SUCH DAMAGE.
     30  1.12      agc  */
     31  1.12      agc 
     32  1.12      agc /*
     33  1.12      agc  * Copyright (c) 1997 Manuel Bouyer.
     34  1.12      agc  *
     35  1.12      agc  * Redistribution and use in source and binary forms, with or without
     36  1.12      agc  * modification, are permitted provided that the following conditions
     37  1.12      agc  * are met:
     38  1.12      agc  * 1. Redistributions of source code must retain the above copyright
     39  1.12      agc  *    notice, this list of conditions and the following disclaimer.
     40  1.12      agc  * 2. Redistributions in binary form must reproduce the above copyright
     41  1.12      agc  *    notice, this list of conditions and the following disclaimer in the
     42  1.12      agc  *    documentation and/or other materials provided with the distribution.
     43   1.1   bouyer  * 3. All advertising materials mentioning features or use of this software
     44   1.1   bouyer  *    must display the following acknowledgement:
     45  1.13   bouyer  *	This product includes software developed by Manuel Bouyer.
     46  1.13   bouyer  * 4. The name of the author may not be used to endorse or promote products
     47  1.13   bouyer  *    derived from this software without specific prior written permission.
     48   1.1   bouyer  *
     49  1.16   bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     50  1.16   bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     51  1.16   bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     52  1.16   bouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     53  1.16   bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     54  1.16   bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     55  1.16   bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     56  1.16   bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     57  1.16   bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     58  1.16   bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     59   1.1   bouyer  */
     60   1.1   bouyer 
     61   1.2    lukem #include <sys/cdefs.h>
     62   1.1   bouyer #ifndef lint
     63   1.2    lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1993\n\
     64   1.2    lukem 	The Regents of the University of California.  All rights reserved.\n");
     65   1.1   bouyer #endif /* not lint */
     66   1.1   bouyer 
     67   1.1   bouyer #ifndef lint
     68   1.1   bouyer #if 0
     69   1.1   bouyer static char sccsid[] = "@(#)main.c	8.2 (Berkeley) 1/23/94";
     70   1.1   bouyer #else
     71  1.16   bouyer __RCSID("$NetBSD: main.c,v 1.16 2004/03/22 19:46:53 bouyer Exp $");
     72   1.1   bouyer #endif
     73   1.1   bouyer #endif /* not lint */
     74   1.1   bouyer 
     75   1.1   bouyer #include <sys/param.h>
     76   1.1   bouyer #include <sys/time.h>
     77   1.1   bouyer #include <sys/mount.h>
     78   1.4     fvdl #include <ufs/ufs/ufsmount.h>
     79   1.1   bouyer #include <ufs/ext2fs/ext2fs_dinode.h>
     80   1.1   bouyer #include <ufs/ext2fs/ext2fs.h>
     81   1.1   bouyer #include <fstab.h>
     82   1.1   bouyer #include <stdlib.h>
     83   1.1   bouyer #include <string.h>
     84   1.1   bouyer #include <ctype.h>
     85   1.1   bouyer #include <stdio.h>
     86   1.5   kleink #include <time.h>
     87   1.1   bouyer #include <unistd.h>
     88   1.1   bouyer 
     89   1.1   bouyer #include "fsck.h"
     90   1.1   bouyer #include "extern.h"
     91   1.1   bouyer #include "fsutil.h"
     92   1.1   bouyer 
     93   1.1   bouyer int	returntosingle;
     94   1.1   bouyer 
     95   1.1   bouyer int	main __P((int, char *[]));
     96   1.1   bouyer 
     97   1.1   bouyer static int	argtoi __P((int, char *, char *, int));
     98   1.6  mycroft static int	checkfilesys __P((const char *, char *, long, int));
     99   1.1   bouyer static  void usage __P((void));
    100   1.1   bouyer 
    101   1.1   bouyer int
    102   1.1   bouyer main(argc, argv)
    103   1.1   bouyer 	int	argc;
    104   1.1   bouyer 	char	*argv[];
    105   1.1   bouyer {
    106   1.1   bouyer 	int ch;
    107   1.1   bouyer 	int ret = 0;
    108   1.1   bouyer 
    109   1.1   bouyer 	sync();
    110   1.1   bouyer 	skipclean = 1;
    111  1.14      dsl 	while ((ch = getopt(argc, argv, "b:c:dfm:npqy")) != -1) {
    112   1.1   bouyer 		switch (ch) {
    113   1.1   bouyer 		case 'b':
    114   1.1   bouyer 			skipclean = 0;
    115   1.1   bouyer 			bflag = argtoi('b', "number", optarg, 10);
    116   1.1   bouyer 			printf("Alternate super block location: %d\n", bflag);
    117   1.1   bouyer 			break;
    118   1.1   bouyer 
    119   1.1   bouyer 		case 'd':
    120   1.1   bouyer 			debug++;
    121   1.1   bouyer 			break;
    122   1.1   bouyer 
    123   1.1   bouyer 		case 'f':
    124   1.1   bouyer 			skipclean = 0;
    125   1.1   bouyer 			break;
    126   1.1   bouyer 
    127   1.1   bouyer 		case 'm':
    128   1.1   bouyer 			lfmode = argtoi('m', "mode", optarg, 8);
    129   1.1   bouyer 			if (lfmode &~ 07777)
    130   1.1   bouyer 				errexit("bad mode to -m: %o\n", lfmode);
    131   1.1   bouyer 			printf("** lost+found creation mode %o\n", lfmode);
    132   1.1   bouyer 			break;
    133   1.1   bouyer 
    134   1.1   bouyer 		case 'n':
    135   1.1   bouyer 			nflag++;
    136   1.1   bouyer 			yflag = 0;
    137   1.1   bouyer 			break;
    138   1.1   bouyer 
    139   1.1   bouyer 		case 'p':
    140   1.1   bouyer 			preen++;
    141  1.14      dsl 			break;
    142  1.14      dsl 
    143  1.14      dsl 		case 'q':		/* Quiet not implemented */
    144   1.1   bouyer 			break;
    145   1.1   bouyer 
    146   1.1   bouyer 		case 'y':
    147   1.1   bouyer 			yflag++;
    148   1.1   bouyer 			nflag = 0;
    149   1.1   bouyer 			break;
    150   1.1   bouyer 
    151   1.1   bouyer 		default:
    152   1.1   bouyer 			usage();
    153   1.1   bouyer 		}
    154   1.1   bouyer 	}
    155   1.1   bouyer 
    156   1.1   bouyer 	argc -= optind;
    157   1.1   bouyer 	argv += optind;
    158   1.1   bouyer 
    159   1.1   bouyer 	if (!argc)
    160   1.1   bouyer 		usage();
    161   1.1   bouyer 
    162   1.1   bouyer 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
    163   1.1   bouyer 		(void)signal(SIGINT, catch);
    164   1.1   bouyer 	if (preen)
    165   1.1   bouyer 		(void)signal(SIGQUIT, catchquit);
    166   1.1   bouyer 
    167   1.1   bouyer 	while (argc-- > 0)
    168   1.1   bouyer 		(void)checkfilesys(blockcheck(*argv++), 0, 0L, 0);
    169   1.1   bouyer 
    170   1.1   bouyer 	if (returntosingle)
    171   1.1   bouyer 		ret = 2;
    172   1.1   bouyer 
    173   1.1   bouyer 	exit(ret);
    174   1.1   bouyer }
    175   1.1   bouyer 
    176   1.1   bouyer static int
    177   1.1   bouyer argtoi(flag, req, str, base)
    178   1.1   bouyer 	int flag;
    179   1.1   bouyer 	char *req, *str;
    180   1.1   bouyer 	int base;
    181   1.1   bouyer {
    182   1.1   bouyer 	char *cp;
    183   1.1   bouyer 	int ret;
    184   1.1   bouyer 
    185   1.1   bouyer 	ret = (int)strtol(str, &cp, base);
    186   1.1   bouyer 	if (cp == str || *cp)
    187   1.1   bouyer 		errexit("-%c flag requires a %s\n", flag, req);
    188   1.1   bouyer 	return (ret);
    189   1.1   bouyer }
    190   1.1   bouyer 
    191   1.1   bouyer /*
    192   1.1   bouyer  * Check the specified filesystem.
    193   1.1   bouyer  */
    194   1.1   bouyer /* ARGSUSED */
    195   1.1   bouyer static int
    196   1.1   bouyer checkfilesys(filesys, mntpt, auxdata, child)
    197   1.6  mycroft 	const char *filesys;
    198   1.6  mycroft 	char *mntpt;
    199   1.1   bouyer 	long auxdata;
    200   1.1   bouyer 	int child;
    201   1.1   bouyer {
    202   1.1   bouyer 	daddr_t n_bfree;
    203   1.1   bouyer 	struct dups *dp;
    204   1.1   bouyer 	struct zlncnt *zlnp;
    205   1.8   bouyer 	int i;
    206   1.1   bouyer 
    207   1.1   bouyer 	if (preen && child)
    208   1.1   bouyer 		(void)signal(SIGQUIT, voidquit);
    209   1.1   bouyer 	setcdevname(filesys, preen);
    210   1.1   bouyer 	if (debug && preen)
    211   1.1   bouyer 		pwarn("starting\n");
    212   1.1   bouyer 	switch (setup(filesys)) {
    213   1.1   bouyer 	case 0:
    214   1.1   bouyer 		if (preen)
    215   1.1   bouyer 			pfatal("CAN'T CHECK FILE SYSTEM.");
    216   1.1   bouyer 	case -1:
    217   1.1   bouyer 		return (0);
    218   1.1   bouyer 	}
    219   1.1   bouyer 	/*
    220   1.1   bouyer 	 * 1: scan inodes tallying blocks used
    221   1.1   bouyer 	 */
    222   1.1   bouyer 	if (preen == 0) {
    223   1.7   bouyer 		if (sblock.e2fs.e2fs_rev > E2FS_REV0) {
    224   1.7   bouyer 			printf("** Last Mounted on %s\n",
    225   1.7   bouyer 			    sblock.e2fs.e2fs_fsmnt);
    226   1.7   bouyer 		}
    227   1.1   bouyer 		if (hotroot())
    228   1.1   bouyer 			printf("** Root file system\n");
    229   1.1   bouyer 		printf("** Phase 1 - Check Blocks and Sizes\n");
    230   1.1   bouyer 	}
    231   1.1   bouyer 	pass1();
    232   1.1   bouyer 
    233   1.1   bouyer 	/*
    234   1.1   bouyer 	 * 1b: locate first references to duplicates, if any
    235   1.1   bouyer 	 */
    236   1.1   bouyer 	if (duplist) {
    237   1.1   bouyer 		if (preen)
    238   1.1   bouyer 			pfatal("INTERNAL ERROR: dups with -p");
    239   1.1   bouyer 		printf("** Phase 1b - Rescan For More DUPS\n");
    240   1.1   bouyer 		pass1b();
    241   1.1   bouyer 	}
    242   1.1   bouyer 
    243   1.1   bouyer 	/*
    244   1.1   bouyer 	 * 2: traverse directories from root to mark all connected directories
    245   1.1   bouyer 	 */
    246   1.1   bouyer 	if (preen == 0)
    247   1.1   bouyer 		printf("** Phase 2 - Check Pathnames\n");
    248   1.1   bouyer 	pass2();
    249   1.1   bouyer 
    250   1.1   bouyer 	/*
    251   1.1   bouyer 	 * 3: scan inodes looking for disconnected directories
    252   1.1   bouyer 	 */
    253   1.1   bouyer 	if (preen == 0)
    254   1.1   bouyer 		printf("** Phase 3 - Check Connectivity\n");
    255   1.1   bouyer 	pass3();
    256   1.1   bouyer 
    257   1.1   bouyer 	/*
    258   1.1   bouyer 	 * 4: scan inodes looking for disconnected files; check reference counts
    259   1.1   bouyer 	 */
    260   1.1   bouyer 	if (preen == 0)
    261   1.1   bouyer 		printf("** Phase 4 - Check Reference Counts\n");
    262   1.1   bouyer 	pass4();
    263   1.1   bouyer 
    264   1.1   bouyer 	/*
    265   1.1   bouyer 	 * 5: check and repair resource counts in cylinder groups
    266   1.1   bouyer 	 */
    267   1.1   bouyer 	if (preen == 0)
    268   1.1   bouyer 		printf("** Phase 5 - Check Cyl groups\n");
    269   1.1   bouyer 	pass5();
    270   1.1   bouyer 
    271   1.1   bouyer 	/*
    272   1.1   bouyer 	 * print out summary statistics
    273   1.1   bouyer 	 */
    274   1.1   bouyer 	n_bfree = sblock.e2fs.e2fs_fbcount;
    275   1.1   bouyer 
    276  1.11     fvdl 	pwarn("%lld files, %lld used, %lld free\n",
    277  1.11     fvdl 	    (long long)n_files, (long long)n_blks, (long long)n_bfree);
    278   1.1   bouyer 	if (debug &&
    279   1.1   bouyer 		/* 9 reserved and unused inodes in FS */
    280   1.1   bouyer 	    (n_files -= maxino - 9 - sblock.e2fs.e2fs_ficount))
    281  1.11     fvdl 		printf("%lld files missing\n", (long long)n_files);
    282   1.1   bouyer 	if (debug) {
    283   1.8   bouyer 		for (i = 0; i < sblock.e2fs_ncg; i++)
    284   1.8   bouyer 			n_blks +=  cgoverhead(i);
    285   1.1   bouyer 		n_blks += sblock.e2fs.e2fs_first_dblock;
    286   1.1   bouyer 		if (n_blks -= maxfsblock - n_bfree)
    287  1.11     fvdl 			printf("%lld blocks missing\n", (long long)n_blks);
    288   1.1   bouyer 		if (duplist != NULL) {
    289   1.1   bouyer 			printf("The following duplicate blocks remain:");
    290   1.1   bouyer 			for (dp = duplist; dp; dp = dp->next)
    291  1.11     fvdl 				printf(" %lld,", (long long)dp->dup);
    292   1.1   bouyer 			printf("\n");
    293   1.1   bouyer 		}
    294   1.1   bouyer 		if (zlnhead != NULL) {
    295   1.1   bouyer 			printf("The following zero link count inodes remain:");
    296   1.1   bouyer 			for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
    297   1.1   bouyer 				printf(" %u,", zlnp->zlncnt);
    298   1.1   bouyer 			printf("\n");
    299   1.1   bouyer 		}
    300   1.1   bouyer 	}
    301   1.1   bouyer 	zlnhead = (struct zlncnt *)0;
    302   1.1   bouyer 	duplist = (struct dups *)0;
    303   1.1   bouyer 	muldup = (struct dups *)0;
    304   1.1   bouyer 	inocleanup();
    305   1.1   bouyer 	if (fsmodified) {
    306   1.1   bouyer 		time_t t;
    307   1.1   bouyer 		(void)time(&t);
    308   1.1   bouyer 		sblock.e2fs.e2fs_wtime = t;
    309   1.1   bouyer 		sblock.e2fs.e2fs_lastfsck = t;
    310   1.1   bouyer 		sbdirty();
    311   1.1   bouyer 	}
    312   1.1   bouyer 	ckfini(1);
    313   1.1   bouyer 	free(blockmap);
    314   1.1   bouyer 	free(statemap);
    315   1.1   bouyer 	free((char *)lncntp);
    316   1.1   bouyer 	if (!fsmodified)
    317   1.1   bouyer 		return (0);
    318   1.1   bouyer 	if (!preen)
    319   1.1   bouyer 		printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
    320   1.1   bouyer 	if (rerun)
    321   1.1   bouyer 		printf("\n***** PLEASE RERUN FSCK *****\n");
    322   1.1   bouyer 	if (hotroot()) {
    323   1.1   bouyer 		struct statfs stfs_buf;
    324   1.1   bouyer 		/*
    325   1.1   bouyer 		 * We modified the root.  Do a mount update on
    326   1.1   bouyer 		 * it, unless it is read-write, so we can continue.
    327   1.1   bouyer 		 */
    328   1.1   bouyer 		if (statfs("/", &stfs_buf) == 0) {
    329   1.1   bouyer 			long flags = stfs_buf.f_flags;
    330   1.1   bouyer 			struct ufs_args args;
    331   1.1   bouyer 			int ret;
    332   1.1   bouyer 
    333   1.1   bouyer 			if (flags & MNT_RDONLY) {
    334   1.1   bouyer 				args.fspec = 0;
    335   1.1   bouyer 				args.export.ex_flags = 0;
    336   1.1   bouyer 				args.export.ex_root = 0;
    337   1.1   bouyer 				flags |= MNT_UPDATE | MNT_RELOAD;
    338   1.1   bouyer 				ret = mount(MOUNT_EXT2FS, "/", flags, &args);
    339   1.1   bouyer 				if (ret == 0)
    340   1.1   bouyer 					return(0);
    341   1.1   bouyer 			}
    342   1.1   bouyer 		}
    343   1.1   bouyer 		if (!preen)
    344   1.1   bouyer 			printf("\n***** REBOOT NOW *****\n");
    345   1.1   bouyer 		sync();
    346   1.1   bouyer 		return (4);
    347   1.1   bouyer 	}
    348   1.1   bouyer 	return (0);
    349   1.1   bouyer }
    350   1.1   bouyer 
    351   1.1   bouyer static void
    352   1.1   bouyer usage()
    353   1.1   bouyer {
    354  1.10      cgd 
    355   1.1   bouyer 	(void) fprintf(stderr,
    356  1.15     jmmv 	    "usage: %s [-dfnpy] [-b block] [-c level] [-m mode] filesystem ...\n",
    357  1.10      cgd 	    getprogname());
    358   1.1   bouyer 	exit(1);
    359   1.1   bouyer }
    360   1.1   bouyer 
    361