Home | History | Annotate | Line # | Download | only in fsck_ffs
main.c revision 1.54
      1 /*	$NetBSD: main.c,v 1.54 2005/01/13 19:56:02 christos 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.54 2005/01/13 19:56:02 christos 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 int	main __P((int, char *[]));
     74 
     75 static int	argtoi __P((int, char *, char *, int));
     76 static int	checkfilesys __P((const char *, char *, long, int));
     77 static  void usage __P((void));
     78 
     79 int
     80 main(argc, argv)
     81 	int	argc;
     82 	char	*argv[];
     83 {
     84 	struct rlimit r;
     85 	int ch;
     86 	int ret = 0;
     87 
     88 	if (getrlimit(RLIMIT_DATA, &r) == 0) {
     89 		r.rlim_cur = r.rlim_max;
     90 		(void) setrlimit(RLIMIT_DATA, &r);
     91 	}
     92 	sync();
     93 	skipclean = 1;
     94 	markclean = 1;
     95 	forceimage = 0;
     96 	endian = 0;
     97 	isappleufs = 0;
     98 	while ((ch = getopt(argc, argv, "aB:b:c:dFfm:npPqy")) != -1) {
     99 		switch (ch) {
    100 		case 'a':
    101 			isappleufs = 1;
    102 			break;
    103 
    104 		case 'B':
    105 			if (strcmp(optarg, "be") == 0)
    106 				endian = BIG_ENDIAN;
    107 			else if (strcmp(optarg, "le") == 0)
    108 				endian = LITTLE_ENDIAN;
    109 			else usage();
    110 			break;
    111 
    112 		case 'b':
    113 			skipclean = 0;
    114 			bflag = argtoi('b', "number", optarg, 10);
    115 			printf("Alternate super block location: %d\n", bflag);
    116 			break;
    117 
    118 		case 'c':
    119 			skipclean = 0;
    120 			cvtlevel = argtoi('c', "conversion level", optarg, 10);
    121 			if (cvtlevel > 4) {
    122 				cvtlevel = 4;
    123 				warnx("Using maximum conversion level of %d\n",cvtlevel);
    124 			}
    125 			break;
    126 
    127 		case 'd':
    128 			debug++;
    129 			break;
    130 
    131 		case 'F':
    132 			forceimage = 1;
    133 			break;
    134 
    135 		case 'f':
    136 			skipclean = 0;
    137 			break;
    138 
    139 		case 'm':
    140 			lfmode = argtoi('m', "mode", optarg, 8);
    141 			if (lfmode &~ 07777)
    142 				errx(EEXIT, "bad mode to -m: %o", lfmode);
    143 			printf("** lost+found creation mode %o\n", lfmode);
    144 			break;
    145 
    146 		case 'n':
    147 			nflag++;
    148 			yflag = 0;
    149 			break;
    150 
    151 		case 'p':
    152 			preen++;
    153 			break;
    154 
    155 		case 'P':
    156 			progress = 1;
    157 			break;
    158 
    159 		case 'q':
    160 			quiet++;
    161 			break;
    162 
    163 		case 'y':
    164 			yflag++;
    165 			nflag = 0;
    166 			break;
    167 
    168 		default:
    169 			usage();
    170 		}
    171 	}
    172 
    173 	argc -= optind;
    174 	argv += optind;
    175 
    176 	if (!argc)
    177 		usage();
    178 
    179 	if (debug)
    180 		progress = 0;
    181 
    182 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
    183 		(void)signal(SIGINT, catch);
    184 	if (preen)
    185 		(void)signal(SIGQUIT, catchquit);
    186 #ifdef PROGRESS
    187 	if (progress) {
    188 		progress_ttywidth(0);
    189 		(void)signal(SIGWINCH, progress_ttywidth);
    190 	}
    191 #endif /* ! PROGRESS */
    192 	signal(SIGINFO, infohandler);
    193 
    194 	while (argc-- > 0) {
    195 		const char *path = blockcheck(*argv);
    196 
    197 		if (path == NULL)
    198 			pfatal("Can't check %s\n", *argv);
    199 		else
    200 			(void)checkfilesys(blockcheck(*argv), 0, 0L, 0);
    201 		argv++;
    202 	}
    203 
    204 	if (returntosingle)
    205 		ret = 2;
    206 
    207 	exit(ret);
    208 }
    209 
    210 static int
    211 argtoi(flag, req, str, base)
    212 	int flag;
    213 	char *req, *str;
    214 	int base;
    215 {
    216 	char *cp;
    217 	int ret;
    218 
    219 	ret = (int)strtol(str, &cp, base);
    220 	if (cp == str || *cp)
    221 		errx(EEXIT, "-%c flag requires a %s", flag, req);
    222 	return (ret);
    223 }
    224 
    225 /*
    226  * Check the specified filesystem.
    227  */
    228 /* ARGSUSED */
    229 static int
    230 checkfilesys(filesys, mntpt, auxdata, child)
    231 	const char *filesys;
    232 	char *mntpt;
    233 	long auxdata;
    234 	int child;
    235 {
    236 	daddr_t n_ffree, n_bfree;
    237 	struct dups *dp;
    238 	struct zlncnt *zlnp;
    239 	int cylno;
    240 #ifdef LITE2BORKEN
    241 	int flags;
    242 #endif
    243 #ifdef PROGRESS
    244 	off_t progress_total = 0;
    245 #endif /* PROGRESS */
    246 
    247 	if (preen && child)
    248 		(void)signal(SIGQUIT, voidquit);
    249 	setcdevname(filesys, preen);
    250 	if (debug && preen)
    251 		pwarn("starting\n");
    252 	switch (setup(filesys)) {
    253 	case 0:
    254 		if (preen)
    255 			pfatal("CAN'T CHECK FILE SYSTEM.");
    256 		/* fall through */
    257 	case -1:
    258 		return (0);
    259 	}
    260 	/*
    261 	 * Cleared if any questions answered no. Used to decide if
    262 	 * the superblock should be marked clean.
    263 	 */
    264 	resolved = 1;
    265 
    266 #ifdef PROGRESS
    267 	/*
    268 	 * Pass 1, Pass 4, and Pass 5 all iterate over cylinder
    269 	 * groups.  Account for those now.  We'll never need to
    270 	 * add in Pass 1b, since that pass is never executed when
    271 	 * preening.
    272 	 *
    273 	 * Pass 2 and Pass 3 iterate over directory inodes, but we
    274 	 * don't know how many of those exist until after Pass 1.
    275 	 * We'll add those in after Pass 1 has completed.
    276 	 */
    277 	if (preen)
    278 		progress_total += sblock->fs_ncg * 3;
    279 #endif /* PROGRESS */
    280 
    281 	/*
    282 	 * 1: scan inodes tallying blocks used
    283 	 */
    284 	if (preen == 0) {
    285 		pwarn("** Last Mounted on %s\n", sblock->fs_fsmnt);
    286 		if (hotroot())
    287 			pwarn("** Root file system\n");
    288 		pwarn("** Phase 1 - Check Blocks and Sizes\n");
    289 	}
    290 	pass1();
    291 
    292 #ifdef PROGRESS
    293 	/* Account for number of directory inodes (used twice). */
    294 	if (preen)
    295 		progress_total += inplast * 2;
    296 	progress_switch(progress);
    297 	progress_init(progress_total);
    298 #endif /* PROGRESS */
    299 
    300 
    301 	/*
    302 	 * 1b: locate first references to duplicates, if any
    303 	 */
    304 	if (duplist) {
    305 		if (preen)
    306 			pfatal("INTERNAL ERROR: dups with -p\n");
    307 		if (usedsoftdep)
    308 			pfatal("INTERNAL ERROR: dups with softdep\n");
    309 		pwarn("** Phase 1b - Rescan For More DUPS\n");
    310 		pass1b();
    311 	}
    312 
    313 	/*
    314 	 * 2: traverse directories from root to mark all connected directories
    315 	 */
    316 	if (preen == 0)
    317 		pwarn("** Phase 2 - Check Pathnames\n");
    318 	pass2();
    319 
    320 	/*
    321 	 * 3: scan inodes looking for disconnected directories
    322 	 */
    323 	if (preen == 0)
    324 		pwarn("** Phase 3 - Check Connectivity\n");
    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 	pass4();
    333 
    334 	/*
    335 	 * 5: check and repair resource counts in cylinder groups
    336 	 */
    337 	if (preen == 0)
    338 		pwarn("** Phase 5 - Check Cyl groups\n");
    339 	pass5();
    340 
    341 	/*
    342 	 * print out summary statistics
    343 	 */
    344 	n_ffree = sblock->fs_cstotal.cs_nffree;
    345 	n_bfree = sblock->fs_cstotal.cs_nbfree;
    346 	pwarn("%d files, %lld used, %lld free ",
    347 	    n_files, (long long)n_blks,
    348 	    (long long)(n_ffree + sblock->fs_frag * n_bfree));
    349 	printf("(%lld frags, %lld blocks, %lld.%lld%% fragmentation)\n",
    350 	    (long long)n_ffree, (long long)n_bfree,
    351 	    (long long)(n_ffree * 100 / (daddr_t)sblock->fs_dsize),
    352 	    (long long)(((n_ffree * 1000 + (daddr_t)sblock->fs_dsize / 2)
    353 		/ (daddr_t)sblock->fs_dsize) % 10));
    354 	if (debug &&
    355 	    (n_files -= maxino - ROOTINO - sblock->fs_cstotal.cs_nifree))
    356 		printf("%d files missing\n", n_files);
    357 	if (debug) {
    358 		n_blks += sblock->fs_ncg *
    359 			(cgdmin(sblock, 0) - cgsblock(sblock, 0));
    360 		n_blks += cgsblock(sblock, 0) - cgbase(sblock, 0);
    361 		n_blks += howmany(sblock->fs_cssize, sblock->fs_fsize);
    362 		if (n_blks -= maxfsblock - (n_ffree + sblock->fs_frag * n_bfree))
    363 			printf("%lld blocks missing\n", (long long)n_blks);
    364 		if (duplist != NULL) {
    365 			printf("The following duplicate blocks remain:");
    366 			for (dp = duplist; dp; dp = dp->next)
    367 				printf(" %lld,", (long long)dp->dup);
    368 			printf("\n");
    369 		}
    370 		if (zlnhead != NULL) {
    371 			printf("The following zero link count inodes remain:");
    372 			for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
    373 				printf(" %u,", zlnp->zlncnt);
    374 			printf("\n");
    375 		}
    376 	}
    377 	zlnhead = (struct zlncnt *)0;
    378 	duplist = (struct dups *)0;
    379 	muldup = (struct dups *)0;
    380 	inocleanup();
    381 	if (fsmodified) {
    382 		sblock->fs_time = time(NULL);
    383 		sbdirty();
    384 	}
    385 	if (rerun)
    386 		markclean = 0;
    387 #if LITE2BORKEN
    388 	if (!hotroot()) {
    389 		ckfini();
    390 	} else {
    391 		struct statvfs stfs_buf;
    392 		/*
    393 		 * Check to see if root is mounted read-write.
    394 		 */
    395 		if (statvfs("/", &stfs_buf) == 0)
    396 			flags = stfs_buf.f_flag;
    397 		else
    398 			flags = 0;
    399 		if (markclean)
    400 			markclean = flags & MNT_RDONLY;
    401 		ckfini();
    402 	}
    403 #else
    404 	ckfini();
    405 #endif
    406 	for (cylno = 0; cylno < sblock->fs_ncg; cylno++)
    407 		if (inostathead[cylno].il_stat != NULL)
    408 			free(inostathead[cylno].il_stat);
    409 	free(inostathead);
    410 	inostathead = NULL;
    411 
    412 	if (!resolved || rerun) {
    413 		pwarn("\n***** UNRESOLVED INCONSISTENCIES REMAIN *****\n");
    414 		returntosingle = 1;
    415 	}
    416 	if (!fsmodified)
    417 		return (0);
    418 	if (!preen)
    419 		pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n");
    420 	if (rerun)
    421 		pwarn("\n***** PLEASE RERUN FSCK *****\n");
    422 	if (hotroot()) {
    423 		struct statvfs stfs_buf;
    424 		/*
    425 		 * We modified the root.  Do a mount update on
    426 		 * it, unless it is read-write, so we can continue.
    427 		 */
    428 		if (statvfs("/", &stfs_buf) == 0) {
    429 			long flags = stfs_buf.f_flag;
    430 			struct ufs_args args;
    431 			int ret;
    432 
    433 			if (flags & MNT_RDONLY) {
    434 				args.fspec = 0;
    435 				args.export.ex_flags = 0;
    436 				args.export.ex_root = 0;
    437 				flags |= MNT_UPDATE | MNT_RELOAD;
    438 				ret = mount(MOUNT_FFS, "/", flags, &args);
    439 				if (ret == 0)
    440 					return(0);
    441 			}
    442 		}
    443 		if (!preen)
    444 			pwarn("\n***** REBOOT NOW *****\n");
    445 		sync();
    446 		return (4);
    447 	}
    448 	return (0);
    449 }
    450 
    451 static void
    452 usage()
    453 {
    454 
    455 	(void) fprintf(stderr,
    456 	    "usage: %s [-adFfnpqy] [-B be|le] [-b block] [-c level] [-m mode]"
    457 	    " filesystem ...\n",
    458 	    getprogname());
    459 	exit(1);
    460 }
    461 
    462