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