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