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