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