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