Home | History | Annotate | Line # | Download | only in fsck_ext2fs
main.c revision 1.5
      1 /*	$NetBSD: main.c,v 1.5 1998/04/01 15:24:12 kleink Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997 Manuel Bouyer.
      5  * Copyright (c) 1980, 1986, 1993
      6  *	The Regents of the University of California.  All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 #ifndef lint
     39 __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1993\n\
     40 	The Regents of the University of California.  All rights reserved.\n");
     41 #endif /* not lint */
     42 
     43 #ifndef lint
     44 #if 0
     45 static char sccsid[] = "@(#)main.c	8.2 (Berkeley) 1/23/94";
     46 #else
     47 __RCSID("$NetBSD: main.c,v 1.5 1998/04/01 15:24:12 kleink Exp $");
     48 #endif
     49 #endif /* not lint */
     50 
     51 #include <sys/param.h>
     52 #include <sys/time.h>
     53 #include <sys/mount.h>
     54 #include <ufs/ufs/ufsmount.h>
     55 #include <ufs/ext2fs/ext2fs_dinode.h>
     56 #include <ufs/ext2fs/ext2fs.h>
     57 #include <fstab.h>
     58 #include <stdlib.h>
     59 #include <string.h>
     60 #include <ctype.h>
     61 #include <stdio.h>
     62 #include <time.h>
     63 #include <unistd.h>
     64 
     65 #include "fsck.h"
     66 #include "extern.h"
     67 #include "fsutil.h"
     68 
     69 int	returntosingle;
     70 
     71 int	main __P((int, char *[]));
     72 
     73 static int	argtoi __P((int, char *, char *, int));
     74 static int	checkfilesys __P((char *, char *, long, int));
     75 static  void usage __P((void));
     76 
     77 
     78 int
     79 main(argc, argv)
     80 	int	argc;
     81 	char	*argv[];
     82 {
     83 	int ch;
     84 	int ret = 0;
     85 
     86 	sync();
     87 	skipclean = 1;
     88 	while ((ch = getopt(argc, argv, "b:c:dfm:npy")) != -1) {
     89 		switch (ch) {
     90 		case 'b':
     91 			skipclean = 0;
     92 			bflag = argtoi('b', "number", optarg, 10);
     93 			printf("Alternate super block location: %d\n", bflag);
     94 			break;
     95 
     96 		case 'd':
     97 			debug++;
     98 			break;
     99 
    100 		case 'f':
    101 			skipclean = 0;
    102 			break;
    103 
    104 		case 'm':
    105 			lfmode = argtoi('m', "mode", optarg, 8);
    106 			if (lfmode &~ 07777)
    107 				errexit("bad mode to -m: %o\n", lfmode);
    108 			printf("** lost+found creation mode %o\n", lfmode);
    109 			break;
    110 
    111 		case 'n':
    112 			nflag++;
    113 			yflag = 0;
    114 			break;
    115 
    116 		case 'p':
    117 			preen++;
    118 			break;
    119 
    120 		case 'y':
    121 			yflag++;
    122 			nflag = 0;
    123 			break;
    124 
    125 		default:
    126 			usage();
    127 		}
    128 	}
    129 
    130 	argc -= optind;
    131 	argv += optind;
    132 
    133 	if (!argc)
    134 		usage();
    135 
    136 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
    137 		(void)signal(SIGINT, catch);
    138 	if (preen)
    139 		(void)signal(SIGQUIT, catchquit);
    140 
    141 	while (argc-- > 0)
    142 		(void)checkfilesys(blockcheck(*argv++), 0, 0L, 0);
    143 
    144 	if (returntosingle)
    145 		ret = 2;
    146 
    147 	exit(ret);
    148 }
    149 
    150 static int
    151 argtoi(flag, req, str, base)
    152 	int flag;
    153 	char *req, *str;
    154 	int base;
    155 {
    156 	char *cp;
    157 	int ret;
    158 
    159 	ret = (int)strtol(str, &cp, base);
    160 	if (cp == str || *cp)
    161 		errexit("-%c flag requires a %s\n", flag, req);
    162 	return (ret);
    163 }
    164 
    165 /*
    166  * Check the specified filesystem.
    167  */
    168 /* ARGSUSED */
    169 static int
    170 checkfilesys(filesys, mntpt, auxdata, child)
    171 	char *filesys, *mntpt;
    172 	long auxdata;
    173 	int child;
    174 {
    175 	daddr_t n_bfree;
    176 	struct dups *dp;
    177 	struct zlncnt *zlnp;
    178 
    179 	if (preen && child)
    180 		(void)signal(SIGQUIT, voidquit);
    181 	setcdevname(filesys, preen);
    182 	if (debug && preen)
    183 		pwarn("starting\n");
    184 	switch (setup(filesys)) {
    185 	case 0:
    186 		if (preen)
    187 			pfatal("CAN'T CHECK FILE SYSTEM.");
    188 	case -1:
    189 		return (0);
    190 	}
    191 	/*
    192 	 * 1: scan inodes tallying blocks used
    193 	 */
    194 	if (preen == 0) {
    195 		if (hotroot())
    196 			printf("** Root file system\n");
    197 		printf("** Phase 1 - Check Blocks and Sizes\n");
    198 	}
    199 	pass1();
    200 
    201 	/*
    202 	 * 1b: locate first references to duplicates, if any
    203 	 */
    204 	if (duplist) {
    205 		if (preen)
    206 			pfatal("INTERNAL ERROR: dups with -p");
    207 		printf("** Phase 1b - Rescan For More DUPS\n");
    208 		pass1b();
    209 	}
    210 
    211 	/*
    212 	 * 2: traverse directories from root to mark all connected directories
    213 	 */
    214 	if (preen == 0)
    215 		printf("** Phase 2 - Check Pathnames\n");
    216 	pass2();
    217 
    218 	/*
    219 	 * 3: scan inodes looking for disconnected directories
    220 	 */
    221 	if (preen == 0)
    222 		printf("** Phase 3 - Check Connectivity\n");
    223 	pass3();
    224 
    225 	/*
    226 	 * 4: scan inodes looking for disconnected files; check reference counts
    227 	 */
    228 	if (preen == 0)
    229 		printf("** Phase 4 - Check Reference Counts\n");
    230 	pass4();
    231 
    232 	/*
    233 	 * 5: check and repair resource counts in cylinder groups
    234 	 */
    235 	if (preen == 0)
    236 		printf("** Phase 5 - Check Cyl groups\n");
    237 	pass5();
    238 
    239 	/*
    240 	 * print out summary statistics
    241 	 */
    242 	n_bfree = sblock.e2fs.e2fs_fbcount;
    243 
    244 	pwarn("%d files, %d used, %d free\n",
    245 	    n_files, n_blks, n_bfree);
    246 	if (debug &&
    247 		/* 9 reserved and unused inodes in FS */
    248 	    (n_files -= maxino - 9 - sblock.e2fs.e2fs_ficount))
    249 		printf("%d files missing\n", n_files);
    250 	if (debug) {
    251 		n_blks += sblock.e2fs_ncg * cgoverhead;
    252 		n_blks += sblock.e2fs.e2fs_first_dblock;
    253 		if (n_blks -= maxfsblock - n_bfree)
    254 			printf("%d blocks missing\n", n_blks);
    255 		if (duplist != NULL) {
    256 			printf("The following duplicate blocks remain:");
    257 			for (dp = duplist; dp; dp = dp->next)
    258 				printf(" %d,", dp->dup);
    259 			printf("\n");
    260 		}
    261 		if (zlnhead != NULL) {
    262 			printf("The following zero link count inodes remain:");
    263 			for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
    264 				printf(" %u,", zlnp->zlncnt);
    265 			printf("\n");
    266 		}
    267 	}
    268 	zlnhead = (struct zlncnt *)0;
    269 	duplist = (struct dups *)0;
    270 	muldup = (struct dups *)0;
    271 	inocleanup();
    272 	if (fsmodified) {
    273 		time_t t;
    274 		(void)time(&t);
    275 		sblock.e2fs.e2fs_wtime = t;
    276 		sblock.e2fs.e2fs_lastfsck = t;
    277 		sbdirty();
    278 	}
    279 	ckfini(1);
    280 	free(blockmap);
    281 	free(statemap);
    282 	free((char *)lncntp);
    283 	if (!fsmodified)
    284 		return (0);
    285 	if (!preen)
    286 		printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
    287 	if (rerun)
    288 		printf("\n***** PLEASE RERUN FSCK *****\n");
    289 	if (hotroot()) {
    290 		struct statfs stfs_buf;
    291 		/*
    292 		 * We modified the root.  Do a mount update on
    293 		 * it, unless it is read-write, so we can continue.
    294 		 */
    295 		if (statfs("/", &stfs_buf) == 0) {
    296 			long flags = stfs_buf.f_flags;
    297 			struct ufs_args args;
    298 			int ret;
    299 
    300 			if (flags & MNT_RDONLY) {
    301 				args.fspec = 0;
    302 				args.export.ex_flags = 0;
    303 				args.export.ex_root = 0;
    304 				flags |= MNT_UPDATE | MNT_RELOAD;
    305 				ret = mount(MOUNT_EXT2FS, "/", flags, &args);
    306 				if (ret == 0)
    307 					return(0);
    308 			}
    309 		}
    310 		if (!preen)
    311 			printf("\n***** REBOOT NOW *****\n");
    312 		sync();
    313 		return (4);
    314 	}
    315 	return (0);
    316 }
    317 
    318 static void
    319 usage()
    320 {
    321 	extern char *__progname;
    322 
    323 	(void) fprintf(stderr,
    324 	    "Usage: %s [-dfnpy] [-b block] [-c level] [-m mode] filesystem ...\n",
    325 	    __progname);
    326 	exit(1);
    327 }
    328 
    329