Home | History | Annotate | Line # | Download | only in fsck_ffs
main.c revision 1.80
      1 /*	$NetBSD: main.c,v 1.80 2011/08/29 14:34:59 joerg 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\
     35  The Regents of the University of California.  All rights reserved.");
     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.80 2011/08/29 14:34:59 joerg 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 <errno.h>
     59 #include <fstab.h>
     60 #include <string.h>
     61 #include <time.h>
     62 #include <stdio.h>
     63 #include <stdlib.h>
     64 #include <unistd.h>
     65 #include <signal.h>
     66 
     67 #include "fsck.h"
     68 #include "extern.h"
     69 #include "fsutil.h"
     70 #include "exitvalues.h"
     71 #include "snapshot.h"
     72 
     73 int	progress = 0;
     74 volatile sig_atomic_t	returntosingle = 0;
     75 
     76 static int	argtoi(int, const char *, const char *, int);
     77 static int	checkfilesys(const char *, const char *, int);
     78 __dead static void	usage(void);
     79 
     80 int
     81 main(int argc, char *argv[])
     82 {
     83 	struct rlimit r;
     84 	int ch;
     85 	int ret = FSCK_EXIT_OK;
     86 	char *snap_backup = NULL;
     87 	int snap_internal = 0;
     88 
     89 	ckfinish = ckfini;
     90 
     91 	if (getrlimit(RLIMIT_DATA, &r) == 0) {
     92 		r.rlim_cur = r.rlim_max;
     93 		(void) setrlimit(RLIMIT_DATA, &r);
     94 	}
     95 	sync();
     96 	skipclean = 1;
     97 	markclean = 1;
     98 	forceimage = 0;
     99 	endian = 0;
    100 	isappleufs = 0;
    101 	while ((ch = getopt(argc, argv, "aB:b:c:dFfm:npPqUyx:X")) != -1) {
    102 		switch (ch) {
    103 		case 'a':
    104 			isappleufs = 1;
    105 			break;
    106 
    107 		case 'B':
    108 			if (strcmp(optarg, "be") == 0)
    109 				endian = BIG_ENDIAN;
    110 			else if (strcmp(optarg, "le") == 0)
    111 				endian = LITTLE_ENDIAN;
    112 			else usage();
    113 			break;
    114 
    115 		case 'b':
    116 			skipclean = 0;
    117 			bflag = argtoi('b', "number", optarg, 10);
    118 			printf("Alternate super block location: %d\n", bflag);
    119 			break;
    120 
    121 		case 'c':
    122 			skipclean = 0;
    123 			cvtlevel = argtoi('c', "conversion level", optarg, 10);
    124 			if (cvtlevel > 4) {
    125 				cvtlevel = 4;
    126 				warnx("Using maximum conversion level of %d\n",
    127 				    cvtlevel);
    128 			}
    129 			break;
    130 
    131 		case 'd':
    132 			debug++;
    133 			break;
    134 
    135 		case 'F':
    136 			forceimage = 1;
    137 			break;
    138 
    139 		case 'f':
    140 			skipclean = 0;
    141 			break;
    142 
    143 		case 'm':
    144 			lfmode = argtoi('m', "mode", optarg, 8);
    145 			if (lfmode &~ 07777)
    146 				errx(FSCK_EXIT_USAGE, "bad mode to -m: %o",
    147 				    lfmode);
    148 			printf("** lost+found creation mode %o\n", lfmode);
    149 			break;
    150 
    151 		case 'n':
    152 			nflag++;
    153 			yflag = 0;
    154 			break;
    155 
    156 		case 'p':
    157 			preen++;
    158 			break;
    159 
    160 		case 'P':
    161 			progress = 1;
    162 			break;
    163 
    164 		case 'q':
    165 			quiet++;
    166 			break;
    167 #ifndef SMALL
    168 		case 'U':
    169 			Uflag++;
    170 			break;
    171 #endif
    172 
    173 		case 'y':
    174 			yflag++;
    175 			nflag = 0;
    176 			break;
    177 		case 'x':
    178 			snap_backup = optarg;
    179 			break;
    180 		case 'X':
    181 			snap_internal = 1;
    182 			break;
    183 
    184 		default:
    185 			usage();
    186 		}
    187 	}
    188 
    189 	if (snap_backup || snap_internal) {
    190 		if (!nflag || yflag) {
    191 			warnx("Cannot use -x or -X without -n\n");
    192 			snap_backup = NULL;
    193 			snap_internal = 0;
    194 		}
    195 	}
    196 
    197 
    198 	argc -= optind;
    199 	argv += optind;
    200 
    201 	if (!argc)
    202 		usage();
    203 
    204 	if (debug)
    205 		progress = 0;
    206 
    207 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
    208 		(void)signal(SIGINT, catch);
    209 	if (preen)
    210 		(void)signal(SIGQUIT, catchquit);
    211 #ifdef PROGRESS
    212 	if (progress) {
    213 		progress_ttywidth(0);
    214 		(void)signal(SIGWINCH, progress_ttywidth);
    215 	}
    216 #endif /* ! PROGRESS */
    217 	signal(SIGINFO, infohandler);
    218 
    219 	while (argc-- > 0) {
    220 		int nret;
    221 		char *path;
    222 
    223 		if (!forceimage)
    224 			path = strdup(blockcheck(*argv));
    225 		else
    226 			path = strdup(*argv);
    227 
    228 		if (path == NULL)
    229 			pfatal("Can't check %s\n", *argv);
    230 
    231 		if (snap_backup || snap_internal) {
    232 			char *snap_dev;
    233 			int snapfd;
    234 
    235 			snapfd = snap_open(*argv, snap_backup, NULL, &snap_dev);
    236 			if (snapfd < 0) {
    237 				warn("can't take snapshot of %s", *argv);
    238 				goto next;
    239 			}
    240 			nret = checkfilesys(blockcheck(snap_dev), path, 0);
    241 			if (ret < nret)
    242 				ret = nret;
    243 			close(snapfd);
    244 		} else {
    245 			nret = checkfilesys(path, path, 0);
    246 			if (ret < nret)
    247 				ret = nret;
    248 		}
    249 next:
    250 		free(path);
    251 		argv++;
    252 	}
    253 
    254 	return returntosingle ? FSCK_EXIT_UNRESOLVED : ret;
    255 }
    256 
    257 static int
    258 argtoi(int flag, const char *req, const char *str, int base)
    259 {
    260 	char *cp;
    261 	int ret;
    262 
    263 	ret = (int)strtol(str, &cp, base);
    264 	if (cp == str || *cp)
    265 		errx(FSCK_EXIT_USAGE, "-%c flag requires a %s",
    266 		    flag, req);
    267 	return (ret);
    268 }
    269 
    270 /*
    271  * Check the specified filesystem.
    272  */
    273 /* ARGSUSED */
    274 static int
    275 checkfilesys(const char *filesys, const char *origfs, int child)
    276 {
    277 	daddr_t n_ffree, n_bfree;
    278 	struct dups *dp;
    279 	struct zlncnt *zlnp;
    280 	int cylno;
    281 #ifdef LITE2BORKEN
    282 	int flags;
    283 #endif
    284 #ifdef PROGRESS
    285 	/*
    286 	 * In prune mode, how far does the progress bar travel during
    287 	 * each pass?  (In non-prune mode, each pass has a separate
    288 	 * progress bar that travels from 0 to 100%.)
    289 	 *
    290 	 * The numbers below are percentages, intended to correspond
    291 	 * roughly to the cumulative time up to the end of each pass.
    292 	 * They don't have to be accurate.  In reality, on a large
    293 	 * file system, Pass 1 and Pass 2 together are likely to use
    294 	 * significantly more than the 95% reflected below, so users
    295 	 * will get a pleasant surprise when the last 5% of the progress
    296 	 * bar runs more quickly than they had expected.
    297 	 */
    298 	static int progress_limits[] = {0, 20, 95, 96, 97, 100};
    299 #endif /* PROGRESS */
    300 
    301 	if (preen && child)
    302 		(void)signal(SIGQUIT, voidquit);
    303 	setcdevname(filesys, preen);
    304 	if (debug && preen)
    305 		pwarn("starting\n");
    306 	switch (setup(filesys, origfs)) {
    307 	case 0:
    308 		if (preen)
    309 			pfatal("CAN'T CHECK FILE SYSTEM.");
    310 		/* fall through */
    311 	case -1:
    312 		return FSCK_EXIT_OK;
    313 	}
    314 	/*
    315 	 * Cleared if any questions answered no. Used to decide if
    316 	 * the superblock should be marked clean.
    317 	 */
    318 	resolved = 1;
    319 
    320 #ifdef PROGRESS
    321 	progress_switch(progress);
    322 	progress_init();
    323 #endif /* PROGRESS */
    324 
    325 	/*
    326 	 * 1: scan inodes tallying blocks used
    327 	 */
    328 	if (preen == 0) {
    329 		pwarn("** Last Mounted on %s\n", sblock->fs_fsmnt);
    330 		if (hotroot())
    331 			pwarn("** Root file system\n");
    332 		pwarn("** Phase 1 - Check Blocks and Sizes\n");
    333 	}
    334 #ifdef PROGRESS
    335 	if (preen)
    336 		progress_setrange(0, progress_limits[1]);
    337 #endif /* PROGRESS */
    338 	pass1();
    339 
    340 	/*
    341 	 * 1b: locate first references to duplicates, if any
    342 	 */
    343 	if (duplist) {
    344 		if (preen)
    345 			pfatal("INTERNAL ERROR: dups with -p\n");
    346 		if (usedsoftdep)
    347 			pfatal("INTERNAL ERROR: dups with softdep\n");
    348 		pwarn("** Phase 1b - Rescan For More DUPS\n");
    349 		pass1b();
    350 	}
    351 
    352 	/*
    353 	 * 2: traverse directories from root to mark all connected directories
    354 	 */
    355 	if (preen == 0)
    356 		pwarn("** Phase 2 - Check Pathnames\n");
    357 #ifdef PROGRESS
    358 	if (preen)
    359 		progress_sethighlim(progress_limits[2]);
    360 #endif /* PROGRESS */
    361 	pass2();
    362 
    363 	/*
    364 	 * 3: scan inodes looking for disconnected directories
    365 	 */
    366 	if (preen == 0)
    367 		pwarn("** Phase 3 - Check Connectivity\n");
    368 #ifdef PROGRESS
    369 	if (preen)
    370 		progress_sethighlim(progress_limits[3]);
    371 #endif /* PROGRESS */
    372 	pass3();
    373 
    374 	/*
    375 	 * 4: scan inodes looking for disconnected files; check reference counts
    376 	 */
    377 	if (preen == 0)
    378 		pwarn("** Phase 4 - Check Reference Counts\n");
    379 #ifdef PROGRESS
    380 	if (preen)
    381 		progress_sethighlim(progress_limits[4]);
    382 #endif /* PROGRESS */
    383 	pass4();
    384 
    385 	/*
    386 	 * 5: check and repair resource counts in cylinder groups
    387 	 */
    388 	if (preen == 0)
    389 		pwarn("** Phase 5 - Check Cyl groups\n");
    390 #ifdef PROGRESS
    391 	if (preen)
    392 		progress_sethighlim(progress_limits[5]);
    393 #endif /* PROGRESS */
    394 	pass5();
    395 	if (uquot_user_hash != NULL) {
    396 		if (preen == 0)
    397 			pwarn("** Phase 6 - Check Quotas\n");
    398 		pass6();
    399 	}
    400 
    401 	/*
    402 	 * print out summary statistics
    403 	 */
    404 	n_ffree = sblock->fs_cstotal.cs_nffree;
    405 	n_bfree = sblock->fs_cstotal.cs_nbfree;
    406 	pwarn("%llu files, %lld used, %lld free ",
    407 	    (unsigned long long)n_files, (long long)n_blks,
    408 	    (long long)(n_ffree + sblock->fs_frag * n_bfree));
    409 	printf("(%lld frags, %lld blocks, %lld.%lld%% fragmentation)\n",
    410 	    (long long)n_ffree, (long long)n_bfree,
    411 	    (long long)(n_ffree * 100 / (daddr_t)sblock->fs_dsize),
    412 	    (long long)(((n_ffree * 1000 + (daddr_t)sblock->fs_dsize / 2)
    413 		/ (daddr_t)sblock->fs_dsize) % 10));
    414 	if (debug &&
    415 	    (n_files -= maxino - ROOTINO - sblock->fs_cstotal.cs_nifree))
    416 		printf("%llu files missing\n", (unsigned long long)n_files);
    417 	if (debug) {
    418 		n_blks += sblock->fs_ncg *
    419 			(cgdmin(sblock, 0) - cgsblock(sblock, 0));
    420 		n_blks += cgsblock(sblock, 0) - cgbase(sblock, 0);
    421 		n_blks += howmany(sblock->fs_cssize, sblock->fs_fsize);
    422 		if (n_blks -= maxfsblock - (n_ffree + sblock->fs_frag * n_bfree))
    423 			printf("%lld blocks missing\n", (long long)n_blks);
    424 		if (duplist != NULL) {
    425 			printf("The following duplicate blocks remain:");
    426 			for (dp = duplist; dp; dp = dp->next)
    427 				printf(" %lld,", (long long)dp->dup);
    428 			printf("\n");
    429 		}
    430 		if (zlnhead != NULL) {
    431 			printf("The following zero link count inodes remain:");
    432 			for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
    433 				printf(" %llu,",
    434 				    (unsigned long long)zlnp->zlncnt);
    435 			printf("\n");
    436 		}
    437 	}
    438 	zlnhead = (struct zlncnt *)0;
    439 	duplist = (struct dups *)0;
    440 	muldup = (struct dups *)0;
    441 	inocleanup();
    442 	if (fsmodified) {
    443 		sblock->fs_time = time(NULL);
    444 		sbdirty();
    445 	}
    446 	if (rerun)
    447 		markclean = 0;
    448 #if LITE2BORKEN
    449 	if (!hotroot()) {
    450 		ckfini(1);
    451 	} else {
    452 		struct statvfs stfs_buf;
    453 		/*
    454 		 * Check to see if root is mounted read-write.
    455 		 */
    456 		if (statvfs("/", &stfs_buf) == 0)
    457 			flags = stfs_buf.f_flag;
    458 		else
    459 			flags = 0;
    460 		if (markclean)
    461 			markclean = flags & MNT_RDONLY;
    462 		ckfini(1);
    463 	}
    464 #else
    465 	ckfini(1);
    466 #endif
    467 	for (cylno = 0; cylno < sblock->fs_ncg; cylno++)
    468 		if (inostathead[cylno].il_stat != NULL)
    469 			free(inostathead[cylno].il_stat);
    470 	free(inostathead);
    471 	inostathead = NULL;
    472 
    473 	if (!resolved || rerun) {
    474 		pwarn("\n***** UNRESOLVED INCONSISTENCIES REMAIN *****\n");
    475 		returntosingle = 1;
    476 	}
    477 	if (!fsmodified)
    478 		return FSCK_EXIT_OK;
    479 	if (!preen)
    480 		pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n");
    481 	if (rerun)
    482 		pwarn("\n***** PLEASE RERUN FSCK *****\n");
    483 	if (hotroot()) {
    484 		struct statvfs stfs_buf;
    485 		/*
    486 		 * We modified the root.  Do a mount update on
    487 		 * it, unless it is read-write, so we can continue.
    488 		 */
    489 		if (statvfs("/", &stfs_buf) == 0) {
    490 			long flags = stfs_buf.f_flag;
    491 			struct ufs_args args;
    492 
    493 			if (flags & MNT_RDONLY) {
    494 				args.fspec = 0;
    495 				flags |= MNT_UPDATE | MNT_RELOAD;
    496 				if (mount(MOUNT_FFS, "/", flags,
    497 				    &args, sizeof args) == 0)
    498 					return FSCK_EXIT_OK;
    499 			}
    500 		}
    501 		if (!preen)
    502 			pwarn("\n***** REBOOT NOW *****\n");
    503 		sync();
    504 		return FSCK_EXIT_ROOT_CHANGED;
    505 	}
    506 	return FSCK_EXIT_OK;
    507 }
    508 
    509 static void
    510 usage(void)
    511 {
    512 
    513 	(void) fprintf(stderr,
    514 	    "usage: %s [-adFfPpqUX] [-B byteorder] [-b block] [-c level] "
    515 	    "[-m mode]\n"
    516 	    "\t[-x snap-backup] [-y | -n] filesystem ...\n",
    517 	    getprogname());
    518 	exit(FSCK_EXIT_USAGE);
    519 }
    520