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