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