Home | History | Annotate | Line # | Download | only in fsck_ffs
main.c revision 1.24
      1  1.24     lukem /*	$NetBSD: main.c,v 1.24 1997/09/14 14:36:32 lukem 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.15       cgd static char sccsid[] = "@(#)main.c	8.2 (Berkeley) 1/23/94";
     45  1.15       cgd #else
     46  1.24     lukem __RCSID("$NetBSD: main.c,v 1.24 1997/09/14 14:36:32 lukem 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.13   mycroft #include <ufs/ufs/dinode.h>
     54  1.13   mycroft #include <ufs/ffs/fs.h>
     55   1.1       cgd #include <fstab.h>
     56   1.1       cgd #include <stdlib.h>
     57   1.1       cgd #include <string.h>
     58   1.1       cgd #include <ctype.h>
     59   1.1       cgd #include <stdio.h>
     60  1.14       cgd #include <unistd.h>
     61  1.14       cgd 
     62   1.1       cgd #include "fsck.h"
     63  1.14       cgd #include "extern.h"
     64  1.21  christos #include "fsutil.h"
     65   1.1       cgd 
     66   1.1       cgd int	returntosingle;
     67  1.23  christos 
     68  1.20  christos int	main __P((int, char *[]));
     69   1.1       cgd 
     70  1.23  christos static int	argtoi __P((int, char *, char *, int));
     71  1.23  christos static int	checkfilesys __P((char *, char *, long, int));
     72  1.23  christos static  void usage __P((void));
     73  1.23  christos 
     74  1.23  christos 
     75  1.14       cgd int
     76   1.1       cgd main(argc, argv)
     77   1.1       cgd 	int	argc;
     78   1.1       cgd 	char	*argv[];
     79   1.1       cgd {
     80   1.1       cgd 	int ch;
     81  1.20  christos 	int ret = 0;
     82  1.20  christos 	extern char *optarg;
     83   1.1       cgd 	extern int optind;
     84   1.1       cgd 
     85   1.1       cgd 	sync();
     86  1.16   mycroft 	skipclean = 1;
     87  1.24     lukem 	while ((ch = getopt(argc, argv, "b:c:dfm:npy")) != -1) {
     88   1.1       cgd 		switch (ch) {
     89   1.1       cgd 		case 'b':
     90  1.16   mycroft 			skipclean = 0;
     91   1.1       cgd 			bflag = argtoi('b', "number", optarg, 10);
     92   1.1       cgd 			printf("Alternate super block location: %d\n", bflag);
     93   1.1       cgd 			break;
     94   1.1       cgd 
     95   1.1       cgd 		case 'c':
     96  1.16   mycroft 			skipclean = 0;
     97  1.13   mycroft 			cvtlevel = argtoi('c', "conversion level", optarg, 10);
     98   1.1       cgd 			break;
     99  1.13   mycroft 
    100   1.1       cgd 		case 'd':
    101   1.1       cgd 			debug++;
    102  1.17       cgd 			break;
    103  1.17       cgd 
    104  1.17       cgd 		case 'f':
    105  1.17       cgd 			skipclean = 0;
    106   1.1       cgd 			break;
    107   1.1       cgd 
    108   1.1       cgd 		case 'm':
    109   1.1       cgd 			lfmode = argtoi('m', "mode", optarg, 8);
    110   1.1       cgd 			if (lfmode &~ 07777)
    111   1.1       cgd 				errexit("bad mode to -m: %o\n", lfmode);
    112   1.1       cgd 			printf("** lost+found creation mode %o\n", lfmode);
    113   1.1       cgd 			break;
    114   1.1       cgd 
    115   1.1       cgd 		case 'n':
    116   1.1       cgd 			nflag++;
    117   1.1       cgd 			yflag = 0;
    118   1.1       cgd 			break;
    119   1.1       cgd 
    120  1.23  christos 		case 'p':
    121  1.23  christos 			preen++;
    122  1.23  christos 			break;
    123  1.23  christos 
    124   1.1       cgd 		case 'y':
    125   1.1       cgd 			yflag++;
    126   1.1       cgd 			nflag = 0;
    127   1.1       cgd 			break;
    128   1.1       cgd 
    129   1.1       cgd 		default:
    130  1.23  christos 			usage();
    131   1.1       cgd 		}
    132   1.1       cgd 	}
    133  1.23  christos 
    134   1.1       cgd 	argc -= optind;
    135   1.1       cgd 	argv += optind;
    136  1.23  christos 
    137  1.23  christos 	if (!argc)
    138  1.23  christos 		usage();
    139  1.23  christos 
    140   1.1       cgd 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
    141   1.1       cgd 		(void)signal(SIGINT, catch);
    142   1.1       cgd 	if (preen)
    143   1.1       cgd 		(void)signal(SIGQUIT, catchquit);
    144  1.20  christos 
    145  1.23  christos 	while (argc-- > 0)
    146  1.23  christos 		(void)checkfilesys(blockcheck(*argv++), 0, 0L, 0);
    147  1.20  christos 
    148   1.1       cgd 	if (returntosingle)
    149  1.20  christos 		ret = 2;
    150  1.20  christos 
    151   1.1       cgd 	exit(ret);
    152   1.1       cgd }
    153   1.1       cgd 
    154  1.23  christos static int
    155   1.1       cgd argtoi(flag, req, str, base)
    156   1.1       cgd 	int flag;
    157   1.1       cgd 	char *req, *str;
    158   1.1       cgd 	int base;
    159   1.1       cgd {
    160   1.1       cgd 	char *cp;
    161   1.1       cgd 	int ret;
    162   1.1       cgd 
    163   1.1       cgd 	ret = (int)strtol(str, &cp, base);
    164   1.1       cgd 	if (cp == str || *cp)
    165   1.1       cgd 		errexit("-%c flag requires a %s\n", flag, req);
    166   1.1       cgd 	return (ret);
    167   1.1       cgd }
    168   1.1       cgd 
    169   1.1       cgd /*
    170   1.1       cgd  * Check the specified filesystem.
    171   1.1       cgd  */
    172   1.1       cgd /* ARGSUSED */
    173  1.23  christos static int
    174   1.1       cgd checkfilesys(filesys, mntpt, auxdata, child)
    175   1.1       cgd 	char *filesys, *mntpt;
    176   1.1       cgd 	long auxdata;
    177  1.14       cgd 	int child;
    178   1.1       cgd {
    179   1.1       cgd 	daddr_t n_ffree, n_bfree;
    180   1.1       cgd 	struct dups *dp;
    181   1.1       cgd 	struct zlncnt *zlnp;
    182  1.13   mycroft 	int cylno;
    183   1.1       cgd 
    184   1.1       cgd 	if (preen && child)
    185   1.1       cgd 		(void)signal(SIGQUIT, voidquit);
    186  1.20  christos 	setcdevname(filesys, preen);
    187   1.1       cgd 	if (debug && preen)
    188   1.1       cgd 		pwarn("starting\n");
    189  1.16   mycroft 	switch (setup(filesys)) {
    190  1.16   mycroft 	case 0:
    191   1.1       cgd 		if (preen)
    192   1.1       cgd 			pfatal("CAN'T CHECK FILE SYSTEM.");
    193  1.16   mycroft 	case -1:
    194   1.1       cgd 		return (0);
    195   1.1       cgd 	}
    196  1.13   mycroft 	/*
    197  1.13   mycroft 	 * 1: scan inodes tallying blocks used
    198  1.13   mycroft 	 */
    199  1.13   mycroft 	if (preen == 0) {
    200  1.13   mycroft 		printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
    201  1.20  christos 		if (hotroot())
    202  1.13   mycroft 			printf("** Root file system\n");
    203  1.13   mycroft 		printf("** Phase 1 - Check Blocks and Sizes\n");
    204  1.13   mycroft 	}
    205  1.13   mycroft 	pass1();
    206   1.1       cgd 
    207   1.1       cgd 	/*
    208  1.13   mycroft 	 * 1b: locate first references to duplicates, if any
    209   1.1       cgd 	 */
    210  1.13   mycroft 	if (duplist) {
    211  1.13   mycroft 		if (preen)
    212  1.13   mycroft 			pfatal("INTERNAL ERROR: dups with -p");
    213  1.13   mycroft 		printf("** Phase 1b - Rescan For More DUPS\n");
    214  1.13   mycroft 		pass1b();
    215  1.13   mycroft 	}
    216   1.9       cgd 
    217  1.13   mycroft 	/*
    218  1.13   mycroft 	 * 2: traverse directories from root to mark all connected directories
    219  1.13   mycroft 	 */
    220  1.13   mycroft 	if (preen == 0)
    221  1.13   mycroft 		printf("** Phase 2 - Check Pathnames\n");
    222  1.13   mycroft 	pass2();
    223   1.1       cgd 
    224  1.13   mycroft 	/*
    225  1.13   mycroft 	 * 3: scan inodes looking for disconnected directories
    226  1.13   mycroft 	 */
    227  1.13   mycroft 	if (preen == 0)
    228  1.13   mycroft 		printf("** Phase 3 - Check Connectivity\n");
    229  1.13   mycroft 	pass3();
    230   1.1       cgd 
    231  1.13   mycroft 	/*
    232  1.13   mycroft 	 * 4: scan inodes looking for disconnected files; check reference counts
    233  1.13   mycroft 	 */
    234  1.13   mycroft 	if (preen == 0)
    235  1.13   mycroft 		printf("** Phase 4 - Check Reference Counts\n");
    236  1.13   mycroft 	pass4();
    237   1.5   mycroft 
    238  1.13   mycroft 	/*
    239  1.13   mycroft 	 * 5: check and repair resource counts in cylinder groups
    240  1.13   mycroft 	 */
    241  1.13   mycroft 	if (preen == 0)
    242  1.13   mycroft 		printf("** Phase 5 - Check Cyl groups\n");
    243  1.13   mycroft 	pass5();
    244   1.1       cgd 
    245   1.1       cgd 	/*
    246   1.1       cgd 	 * print out summary statistics
    247   1.1       cgd 	 */
    248   1.1       cgd 	n_ffree = sblock.fs_cstotal.cs_nffree;
    249   1.1       cgd 	n_bfree = sblock.fs_cstotal.cs_nbfree;
    250  1.20  christos 	pwarn("%d files, %d used, %d free ",
    251   1.1       cgd 	    n_files, n_blks, n_ffree + sblock.fs_frag * n_bfree);
    252  1.20  christos 	printf("(%d frags, %d blocks, %d.%d%% fragmentation)\n",
    253  1.13   mycroft 	    n_ffree, n_bfree, (n_ffree * 100) / sblock.fs_dsize,
    254  1.13   mycroft 	    ((n_ffree * 1000 + sblock.fs_dsize / 2) / sblock.fs_dsize) % 10);
    255   1.1       cgd 	if (debug &&
    256   1.1       cgd 	    (n_files -= maxino - ROOTINO - sblock.fs_cstotal.cs_nifree))
    257  1.20  christos 		printf("%d files missing\n", n_files);
    258   1.1       cgd 	if (debug) {
    259   1.1       cgd 		n_blks += sblock.fs_ncg *
    260   1.1       cgd 			(cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
    261   1.1       cgd 		n_blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
    262   1.1       cgd 		n_blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
    263   1.1       cgd 		if (n_blks -= maxfsblock - (n_ffree + sblock.fs_frag * n_bfree))
    264  1.20  christos 			printf("%d blocks missing\n", n_blks);
    265   1.1       cgd 		if (duplist != NULL) {
    266   1.1       cgd 			printf("The following duplicate blocks remain:");
    267   1.1       cgd 			for (dp = duplist; dp; dp = dp->next)
    268  1.20  christos 				printf(" %d,", dp->dup);
    269   1.1       cgd 			printf("\n");
    270   1.1       cgd 		}
    271   1.1       cgd 		if (zlnhead != NULL) {
    272   1.1       cgd 			printf("The following zero link count inodes remain:");
    273   1.1       cgd 			for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
    274  1.20  christos 				printf(" %u,", zlnp->zlncnt);
    275   1.1       cgd 			printf("\n");
    276   1.1       cgd 		}
    277   1.1       cgd 	}
    278   1.1       cgd 	zlnhead = (struct zlncnt *)0;
    279   1.1       cgd 	duplist = (struct dups *)0;
    280  1.13   mycroft 	muldup = (struct dups *)0;
    281   1.1       cgd 	inocleanup();
    282   1.1       cgd 	if (fsmodified) {
    283   1.1       cgd 		(void)time(&sblock.fs_time);
    284   1.1       cgd 		sbdirty();
    285   1.1       cgd 	}
    286  1.13   mycroft 	if (cvtlevel && sblk.b_dirty) {
    287  1.13   mycroft 		/*
    288  1.13   mycroft 		 * Write out the duplicate super blocks
    289  1.13   mycroft 		 */
    290  1.13   mycroft 		for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
    291  1.13   mycroft 			bwrite(fswritefd, (char *)&sblock,
    292  1.13   mycroft 			    fsbtodb(&sblock, cgsblock(&sblock, cylno)), SBSIZE);
    293  1.13   mycroft 	}
    294  1.16   mycroft 	ckfini(1);
    295   1.1       cgd 	free(blockmap);
    296   1.1       cgd 	free(statemap);
    297   1.1       cgd 	free((char *)lncntp);
    298   1.1       cgd 	if (!fsmodified)
    299   1.1       cgd 		return (0);
    300  1.13   mycroft 	if (!preen)
    301   1.1       cgd 		printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
    302  1.22   thorpej 	if (rerun)
    303  1.22   thorpej 		printf("\n***** PLEASE RERUN FSCK *****\n");
    304  1.20  christos 	if (hotroot()) {
    305  1.13   mycroft 		struct statfs stfs_buf;
    306  1.13   mycroft 		/*
    307  1.13   mycroft 		 * We modified the root.  Do a mount update on
    308  1.13   mycroft 		 * it, unless it is read-write, so we can continue.
    309  1.13   mycroft 		 */
    310  1.13   mycroft 		if (statfs("/", &stfs_buf) == 0) {
    311  1.13   mycroft 			long flags = stfs_buf.f_flags;
    312  1.13   mycroft 			struct ufs_args args;
    313  1.13   mycroft 			int ret;
    314  1.13   mycroft 
    315  1.13   mycroft 			if (flags & MNT_RDONLY) {
    316  1.13   mycroft 				args.fspec = 0;
    317  1.13   mycroft 				args.export.ex_flags = 0;
    318  1.13   mycroft 				args.export.ex_root = 0;
    319  1.13   mycroft 				flags |= MNT_UPDATE | MNT_RELOAD;
    320  1.19       jtc 				ret = mount(MOUNT_FFS, "/", flags, &args);
    321  1.13   mycroft 				if (ret == 0)
    322  1.13   mycroft 					return(0);
    323  1.13   mycroft 			}
    324  1.13   mycroft 		}
    325  1.13   mycroft 		if (!preen)
    326  1.13   mycroft 			printf("\n***** REBOOT NOW *****\n");
    327   1.1       cgd 		sync();
    328   1.1       cgd 		return (4);
    329   1.1       cgd 	}
    330   1.1       cgd 	return (0);
    331   1.1       cgd }
    332  1.23  christos 
    333  1.23  christos static void
    334  1.23  christos usage()
    335  1.23  christos {
    336  1.23  christos 	extern char *__progname;
    337  1.23  christos 
    338  1.23  christos 	(void) fprintf(stderr,
    339  1.23  christos 	    "Usage: %s [-dfnpy] [-b block] [-c level] [-m mode] filesystem ...\n",
    340  1.23  christos 	    __progname);
    341  1.23  christos 	exit(1);
    342  1.23  christos }
    343  1.23  christos 
    344