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