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