Home | History | Annotate | Line # | Download | only in fsck_ffs
main.c revision 1.62.2.1
      1 /*	$NetBSD: main.c,v 1.62.2.1 2009/03/24 21:33:59 bouyer 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. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1993\n\
     35 	The Regents of the University of California.  All rights reserved.\n");
     36 #endif /* not lint */
     37 
     38 #ifndef lint
     39 #if 0
     40 static char sccsid[] = "@(#)main.c	8.6 (Berkeley) 5/14/95";
     41 #else
     42 __RCSID("$NetBSD: main.c,v 1.62.2.1 2009/03/24 21:33:59 bouyer Exp $");
     43 #endif
     44 #endif /* not lint */
     45 
     46 #include <sys/param.h>
     47 #include <sys/time.h>
     48 #include <sys/mount.h>
     49 #include <sys/resource.h>
     50 
     51 #include <ufs/ufs/dinode.h>
     52 #include <ufs/ufs/ufsmount.h>
     53 #include <ufs/ffs/fs.h>
     54 #include <ufs/ffs/ffs_extern.h>
     55 
     56 #include <ctype.h>
     57 #include <err.h>
     58 #include <fstab.h>
     59 #include <string.h>
     60 #include <time.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 int	progress = 0;
     72 
     73 static int	argtoi(int, const char *, const char *, int);
     74 static int	checkfilesys(const char *, char *, long, int);
     75 static void	usage(void);
     76 
     77 int
     78 main(int argc, char *argv[])
     79 {
     80 	struct rlimit r;
     81 	int ch;
     82 	int ret = 0;
     83 
     84 	if (getrlimit(RLIMIT_DATA, &r) == 0) {
     85 		r.rlim_cur = r.rlim_max;
     86 		(void) setrlimit(RLIMIT_DATA, &r);
     87 	}
     88 	sync();
     89 	skipclean = 1;
     90 	markclean = 1;
     91 	forceimage = 0;
     92 	endian = 0;
     93 	isappleufs = 0;
     94 	while ((ch = getopt(argc, argv, "aB:b:c:dFfm:npPqUy")) != -1) {
     95 		switch (ch) {
     96 		case 'a':
     97 			isappleufs = 1;
     98 			break;
     99 
    100 		case 'B':
    101 			if (strcmp(optarg, "be") == 0)
    102 				endian = BIG_ENDIAN;
    103 			else if (strcmp(optarg, "le") == 0)
    104 				endian = LITTLE_ENDIAN;
    105 			else usage();
    106 			break;
    107 
    108 		case 'b':
    109 			skipclean = 0;
    110 			bflag = argtoi('b', "number", optarg, 10);
    111 			printf("Alternate super block location: %d\n", bflag);
    112 			break;
    113 
    114 		case 'c':
    115 			skipclean = 0;
    116 			cvtlevel = argtoi('c', "conversion level", optarg, 10);
    117 			if (cvtlevel > 4) {
    118 				cvtlevel = 4;
    119 				warnx("Using maximum conversion level of %d\n",cvtlevel);
    120 			}
    121 			break;
    122 
    123 		case 'd':
    124 			debug++;
    125 			break;
    126 
    127 		case 'F':
    128 			forceimage = 1;
    129 			break;
    130 
    131 		case 'f':
    132 			skipclean = 0;
    133 			break;
    134 
    135 		case 'm':
    136 			lfmode = argtoi('m', "mode", optarg, 8);
    137 			if (lfmode &~ 07777)
    138 				errx(EEXIT, "bad mode to -m: %o", lfmode);
    139 			printf("** lost+found creation mode %o\n", lfmode);
    140 			break;
    141 
    142 		case 'n':
    143 			nflag++;
    144 			yflag = 0;
    145 			break;
    146 
    147 		case 'p':
    148 			preen++;
    149 			break;
    150 
    151 		case 'P':
    152 			progress = 1;
    153 			break;
    154 
    155 		case 'q':
    156 			quiet++;
    157 			break;
    158 #ifndef SMALL
    159 		case 'U':
    160 			Uflag++;
    161 			break;
    162 #endif
    163 
    164 		case 'y':
    165 			yflag++;
    166 			nflag = 0;
    167 			break;
    168 
    169 		default:
    170 			usage();
    171 		}
    172 	}
    173 
    174 	argc -= optind;
    175 	argv += optind;
    176 
    177 	if (!argc)
    178 		usage();
    179 
    180 	if (debug)
    181 		progress = 0;
    182 
    183 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
    184 		(void)signal(SIGINT, catch);
    185 	if (preen)
    186 		(void)signal(SIGQUIT, catchquit);
    187 #ifdef PROGRESS
    188 	if (progress) {
    189 		progress_ttywidth(0);
    190 		(void)signal(SIGWINCH, progress_ttywidth);
    191 	}
    192 #endif /* ! PROGRESS */
    193 	signal(SIGINFO, infohandler);
    194 
    195 	while (argc-- > 0) {
    196 		const char *path = blockcheck(*argv);
    197 
    198 		if (path == NULL)
    199 			pfatal("Can't check %s\n", *argv);
    200 		else
    201 			(void)checkfilesys(blockcheck(*argv), 0, 0L, 0);
    202 		argv++;
    203 	}
    204 
    205 	if (returntosingle)
    206 		ret = 2;
    207 
    208 	exit(ret);
    209 }
    210 
    211 static int
    212 argtoi(int flag, const char *req, const char *str, int base)
    213 {
    214 	char *cp;
    215 	int ret;
    216 
    217 	ret = (int)strtol(str, &cp, base);
    218 	if (cp == str || *cp)
    219 		errx(EEXIT, "-%c flag requires a %s", flag, req);
    220 	return (ret);
    221 }
    222 
    223 /*
    224  * Check the specified filesystem.
    225  */
    226 /* ARGSUSED */
    227 static int
    228 checkfilesys(const char *filesys, char *mntpt, long auxdata, int child)
    229 {
    230 	daddr_t n_ffree, n_bfree;
    231 	struct dups *dp;
    232 	struct zlncnt *zlnp;
    233 	int cylno;
    234 #ifdef LITE2BORKEN
    235 	int flags;
    236 #endif
    237 #ifdef PROGRESS
    238 	/*
    239 	 * In prune mode, how far does the progress bar travel during
    240 	 * each pass?  (In non-prune mode, each pass has a separate
    241 	 * progress bar that travels from 0 to 100%.)
    242 	 *
    243 	 * The numbers below are percentages, intended to correspond
    244 	 * roughly to the cumulative time up to the end of each pass.
    245 	 * They don't have to be accurate.  In reality, on a large
    246 	 * file system, Pass 1 and Pass 2 together are likely to use
    247 	 * significantly more than the 95% reflected below, so users
    248 	 * will get a pleasant surprise when the last 5% of the progress
    249 	 * bar runs more quickly than they had expected.
    250 	 */
    251 	static int progress_limits[] = {0, 20, 95, 96, 97, 100};
    252 #endif /* PROGRESS */
    253 
    254 	if (preen && child)
    255 		(void)signal(SIGQUIT, voidquit);
    256 	setcdevname(filesys, preen);
    257 	if (debug && preen)
    258 		pwarn("starting\n");
    259 	switch (setup(filesys)) {
    260 	case 0:
    261 		if (preen)
    262 			pfatal("CAN'T CHECK FILE SYSTEM.");
    263 		/* fall through */
    264 	case -1:
    265 		return (0);
    266 	}
    267 	/*
    268 	 * Cleared if any questions answered no. Used to decide if
    269 	 * the superblock should be marked clean.
    270 	 */
    271 	resolved = 1;
    272 
    273 #ifdef PROGRESS
    274 	progress_switch(progress);
    275 	progress_init();
    276 #endif /* PROGRESS */
    277 
    278 	/*
    279 	 * 1: scan inodes tallying blocks used
    280 	 */
    281 	if (preen == 0) {
    282 		pwarn("** Last Mounted on %s\n", sblock->fs_fsmnt);
    283 		if (hotroot())
    284 			pwarn("** Root file system\n");
    285 		pwarn("** Phase 1 - Check Blocks and Sizes\n");
    286 	}
    287 #ifdef PROGRESS
    288 	if (preen)
    289 		progress_setrange(0, progress_limits[1]);
    290 #endif /* PROGRESS */
    291 	pass1();
    292 
    293 	/*
    294 	 * 1b: locate first references to duplicates, if any
    295 	 */
    296 	if (duplist) {
    297 		if (preen)
    298 			pfatal("INTERNAL ERROR: dups with -p\n");
    299 		if (usedsoftdep)
    300 			pfatal("INTERNAL ERROR: dups with softdep\n");
    301 		pwarn("** Phase 1b - Rescan For More DUPS\n");
    302 		pass1b();
    303 	}
    304 
    305 	/*
    306 	 * 2: traverse directories from root to mark all connected directories
    307 	 */
    308 	if (preen == 0)
    309 		pwarn("** Phase 2 - Check Pathnames\n");
    310 #ifdef PROGRESS
    311 	if (preen)
    312 		progress_sethighlim(progress_limits[2]);
    313 #endif /* PROGRESS */
    314 	pass2();
    315 
    316 	/*
    317 	 * 3: scan inodes looking for disconnected directories
    318 	 */
    319 	if (preen == 0)
    320 		pwarn("** Phase 3 - Check Connectivity\n");
    321 #ifdef PROGRESS
    322 	if (preen)
    323 		progress_sethighlim(progress_limits[3]);
    324 #endif /* PROGRESS */
    325 	pass3();
    326 
    327 	/*
    328 	 * 4: scan inodes looking for disconnected files; check reference counts
    329 	 */
    330 	if (preen == 0)
    331 		pwarn("** Phase 4 - Check Reference Counts\n");
    332 #ifdef PROGRESS
    333 	if (preen)
    334 		progress_sethighlim(progress_limits[4]);
    335 #endif /* PROGRESS */
    336 	pass4();
    337 
    338 	/*
    339 	 * 5: check and repair resource counts in cylinder groups
    340 	 */
    341 	if (preen == 0)
    342 		pwarn("** Phase 5 - Check Cyl groups\n");
    343 #ifdef PROGRESS
    344 	if (preen)
    345 		progress_sethighlim(progress_limits[5]);
    346 #endif /* PROGRESS */
    347 	pass5();
    348 
    349 	/*
    350 	 * print out summary statistics
    351 	 */
    352 	n_ffree = sblock->fs_cstotal.cs_nffree;
    353 	n_bfree = sblock->fs_cstotal.cs_nbfree;
    354 	pwarn("%llu files, %lld used, %lld free ",
    355 	    (unsigned long long)n_files, (long long)n_blks,
    356 	    (long long)(n_ffree + sblock->fs_frag * n_bfree));
    357 	printf("(%lld frags, %lld blocks, %lld.%lld%% fragmentation)\n",
    358 	    (long long)n_ffree, (long long)n_bfree,
    359 	    (long long)(n_ffree * 100 / (daddr_t)sblock->fs_dsize),
    360 	    (long long)(((n_ffree * 1000 + (daddr_t)sblock->fs_dsize / 2)
    361 		/ (daddr_t)sblock->fs_dsize) % 10));
    362 	if (debug &&
    363 	    (n_files -= maxino - ROOTINO - sblock->fs_cstotal.cs_nifree))
    364 		printf("%llu files missing\n", (unsigned long long)n_files);
    365 	if (debug) {
    366 		n_blks += sblock->fs_ncg *
    367 			(cgdmin(sblock, 0) - cgsblock(sblock, 0));
    368 		n_blks += cgsblock(sblock, 0) - cgbase(sblock, 0);
    369 		n_blks += howmany(sblock->fs_cssize, sblock->fs_fsize);
    370 		if (n_blks -= maxfsblock - (n_ffree + sblock->fs_frag * n_bfree))
    371 			printf("%lld blocks missing\n", (long long)n_blks);
    372 		if (duplist != NULL) {
    373 			printf("The following duplicate blocks remain:");
    374 			for (dp = duplist; dp; dp = dp->next)
    375 				printf(" %lld,", (long long)dp->dup);
    376 			printf("\n");
    377 		}
    378 		if (zlnhead != NULL) {
    379 			printf("The following zero link count inodes remain:");
    380 			for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
    381 				printf(" %llu,",
    382 				    (unsigned long long)zlnp->zlncnt);
    383 			printf("\n");
    384 		}
    385 	}
    386 	zlnhead = (struct zlncnt *)0;
    387 	duplist = (struct dups *)0;
    388 	muldup = (struct dups *)0;
    389 	inocleanup();
    390 	if (fsmodified) {
    391 		sblock->fs_time = time(NULL);
    392 		sbdirty();
    393 	}
    394 	if (rerun)
    395 		markclean = 0;
    396 #if LITE2BORKEN
    397 	if (!hotroot()) {
    398 		ckfini();
    399 	} else {
    400 		struct statvfs stfs_buf;
    401 		/*
    402 		 * Check to see if root is mounted read-write.
    403 		 */
    404 		if (statvfs("/", &stfs_buf) == 0)
    405 			flags = stfs_buf.f_flag;
    406 		else
    407 			flags = 0;
    408 		if (markclean)
    409 			markclean = flags & MNT_RDONLY;
    410 		ckfini();
    411 	}
    412 #else
    413 	ckfini();
    414 #endif
    415 	for (cylno = 0; cylno < sblock->fs_ncg; cylno++)
    416 		if (inostathead[cylno].il_stat != NULL)
    417 			free(inostathead[cylno].il_stat);
    418 	free(inostathead);
    419 	inostathead = NULL;
    420 
    421 	if (!resolved || rerun) {
    422 		pwarn("\n***** UNRESOLVED INCONSISTENCIES REMAIN *****\n");
    423 		returntosingle = 1;
    424 	}
    425 	if (!fsmodified)
    426 		return (0);
    427 	if (!preen)
    428 		pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n");
    429 	if (rerun)
    430 		pwarn("\n***** PLEASE RERUN FSCK *****\n");
    431 	if (hotroot()) {
    432 		struct statvfs stfs_buf;
    433 		/*
    434 		 * We modified the root.  Do a mount update on
    435 		 * it, unless it is read-write, so we can continue.
    436 		 */
    437 		if (statvfs("/", &stfs_buf) == 0) {
    438 			long flags = stfs_buf.f_flag;
    439 			struct ufs_args args;
    440 			int ret;
    441 
    442 			if (flags & MNT_RDONLY) {
    443 				args.fspec = 0;
    444 				flags |= MNT_UPDATE | MNT_RELOAD;
    445 				ret = mount(MOUNT_FFS, "/", flags, &args);
    446 				if (ret == 0)
    447 					return(0);
    448 			}
    449 		}
    450 		if (!preen)
    451 			pwarn("\n***** REBOOT NOW *****\n");
    452 		sync();
    453 		return (4);
    454 	}
    455 	return (0);
    456 }
    457 
    458 static void
    459 usage(void)
    460 {
    461 
    462 	(void) fprintf(stderr,
    463 	    "usage: %s [-adFfnPpqy] [-B be|le] [-b block] [-c level] [-m mode]"
    464 	    " filesystem ...\n",
    465 	    getprogname());
    466 	exit(1);
    467 }
    468 
    469