Home | History | Annotate | Line # | Download | only in fsck_ext2fs
main.c revision 1.4
      1 /*	$NetBSD: main.c,v 1.4 1998/03/01 02:20:25 fvdl 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.4 1998/03/01 02:20:25 fvdl 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 <unistd.h>
     63 
     64 #include "fsck.h"
     65 #include "extern.h"
     66 #include "fsutil.h"
     67 
     68 int	returntosingle;
     69 
     70 int	main __P((int, char *[]));
     71 
     72 static int	argtoi __P((int, char *, char *, int));
     73 static int	checkfilesys __P((char *, char *, long, int));
     74 static  void usage __P((void));
     75 
     76 
     77 int
     78 main(argc, argv)
     79 	int	argc;
     80 	char	*argv[];
     81 {
     82 	int ch;
     83 	int ret = 0;
     84 
     85 	sync();
     86 	skipclean = 1;
     87 	while ((ch = getopt(argc, argv, "b:c:dfm:npy")) != -1) {
     88 		switch (ch) {
     89 		case 'b':
     90 			skipclean = 0;
     91 			bflag = argtoi('b', "number", optarg, 10);
     92 			printf("Alternate super block location: %d\n", bflag);
     93 			break;
     94 
     95 		case 'd':
     96 			debug++;
     97 			break;
     98 
     99 		case 'f':
    100 			skipclean = 0;
    101 			break;
    102 
    103 		case 'm':
    104 			lfmode = argtoi('m', "mode", optarg, 8);
    105 			if (lfmode &~ 07777)
    106 				errexit("bad mode to -m: %o\n", lfmode);
    107 			printf("** lost+found creation mode %o\n", lfmode);
    108 			break;
    109 
    110 		case 'n':
    111 			nflag++;
    112 			yflag = 0;
    113 			break;
    114 
    115 		case 'p':
    116 			preen++;
    117 			break;
    118 
    119 		case 'y':
    120 			yflag++;
    121 			nflag = 0;
    122 			break;
    123 
    124 		default:
    125 			usage();
    126 		}
    127 	}
    128 
    129 	argc -= optind;
    130 	argv += optind;
    131 
    132 	if (!argc)
    133 		usage();
    134 
    135 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
    136 		(void)signal(SIGINT, catch);
    137 	if (preen)
    138 		(void)signal(SIGQUIT, catchquit);
    139 
    140 	while (argc-- > 0)
    141 		(void)checkfilesys(blockcheck(*argv++), 0, 0L, 0);
    142 
    143 	if (returntosingle)
    144 		ret = 2;
    145 
    146 	exit(ret);
    147 }
    148 
    149 static int
    150 argtoi(flag, req, str, base)
    151 	int flag;
    152 	char *req, *str;
    153 	int base;
    154 {
    155 	char *cp;
    156 	int ret;
    157 
    158 	ret = (int)strtol(str, &cp, base);
    159 	if (cp == str || *cp)
    160 		errexit("-%c flag requires a %s\n", flag, req);
    161 	return (ret);
    162 }
    163 
    164 /*
    165  * Check the specified filesystem.
    166  */
    167 /* ARGSUSED */
    168 static int
    169 checkfilesys(filesys, mntpt, auxdata, child)
    170 	char *filesys, *mntpt;
    171 	long auxdata;
    172 	int child;
    173 {
    174 	daddr_t n_bfree;
    175 	struct dups *dp;
    176 	struct zlncnt *zlnp;
    177 
    178 	if (preen && child)
    179 		(void)signal(SIGQUIT, voidquit);
    180 	setcdevname(filesys, preen);
    181 	if (debug && preen)
    182 		pwarn("starting\n");
    183 	switch (setup(filesys)) {
    184 	case 0:
    185 		if (preen)
    186 			pfatal("CAN'T CHECK FILE SYSTEM.");
    187 	case -1:
    188 		return (0);
    189 	}
    190 	/*
    191 	 * 1: scan inodes tallying blocks used
    192 	 */
    193 	if (preen == 0) {
    194 		if (hotroot())
    195 			printf("** Root file system\n");
    196 		printf("** Phase 1 - Check Blocks and Sizes\n");
    197 	}
    198 	pass1();
    199 
    200 	/*
    201 	 * 1b: locate first references to duplicates, if any
    202 	 */
    203 	if (duplist) {
    204 		if (preen)
    205 			pfatal("INTERNAL ERROR: dups with -p");
    206 		printf("** Phase 1b - Rescan For More DUPS\n");
    207 		pass1b();
    208 	}
    209 
    210 	/*
    211 	 * 2: traverse directories from root to mark all connected directories
    212 	 */
    213 	if (preen == 0)
    214 		printf("** Phase 2 - Check Pathnames\n");
    215 	pass2();
    216 
    217 	/*
    218 	 * 3: scan inodes looking for disconnected directories
    219 	 */
    220 	if (preen == 0)
    221 		printf("** Phase 3 - Check Connectivity\n");
    222 	pass3();
    223 
    224 	/*
    225 	 * 4: scan inodes looking for disconnected files; check reference counts
    226 	 */
    227 	if (preen == 0)
    228 		printf("** Phase 4 - Check Reference Counts\n");
    229 	pass4();
    230 
    231 	/*
    232 	 * 5: check and repair resource counts in cylinder groups
    233 	 */
    234 	if (preen == 0)
    235 		printf("** Phase 5 - Check Cyl groups\n");
    236 	pass5();
    237 
    238 	/*
    239 	 * print out summary statistics
    240 	 */
    241 	n_bfree = sblock.e2fs.e2fs_fbcount;
    242 
    243 	pwarn("%d files, %d used, %d free\n",
    244 	    n_files, n_blks, n_bfree);
    245 	if (debug &&
    246 		/* 9 reserved and unused inodes in FS */
    247 	    (n_files -= maxino - 9 - sblock.e2fs.e2fs_ficount))
    248 		printf("%d files missing\n", n_files);
    249 	if (debug) {
    250 		n_blks += sblock.e2fs_ncg * cgoverhead;
    251 		n_blks += sblock.e2fs.e2fs_first_dblock;
    252 		if (n_blks -= maxfsblock - n_bfree)
    253 			printf("%d blocks missing\n", n_blks);
    254 		if (duplist != NULL) {
    255 			printf("The following duplicate blocks remain:");
    256 			for (dp = duplist; dp; dp = dp->next)
    257 				printf(" %d,", dp->dup);
    258 			printf("\n");
    259 		}
    260 		if (zlnhead != NULL) {
    261 			printf("The following zero link count inodes remain:");
    262 			for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
    263 				printf(" %u,", zlnp->zlncnt);
    264 			printf("\n");
    265 		}
    266 	}
    267 	zlnhead = (struct zlncnt *)0;
    268 	duplist = (struct dups *)0;
    269 	muldup = (struct dups *)0;
    270 	inocleanup();
    271 	if (fsmodified) {
    272 		time_t t;
    273 		(void)time(&t);
    274 		sblock.e2fs.e2fs_wtime = t;
    275 		sblock.e2fs.e2fs_lastfsck = t;
    276 		sbdirty();
    277 	}
    278 	ckfini(1);
    279 	free(blockmap);
    280 	free(statemap);
    281 	free((char *)lncntp);
    282 	if (!fsmodified)
    283 		return (0);
    284 	if (!preen)
    285 		printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
    286 	if (rerun)
    287 		printf("\n***** PLEASE RERUN FSCK *****\n");
    288 	if (hotroot()) {
    289 		struct statfs stfs_buf;
    290 		/*
    291 		 * We modified the root.  Do a mount update on
    292 		 * it, unless it is read-write, so we can continue.
    293 		 */
    294 		if (statfs("/", &stfs_buf) == 0) {
    295 			long flags = stfs_buf.f_flags;
    296 			struct ufs_args args;
    297 			int ret;
    298 
    299 			if (flags & MNT_RDONLY) {
    300 				args.fspec = 0;
    301 				args.export.ex_flags = 0;
    302 				args.export.ex_root = 0;
    303 				flags |= MNT_UPDATE | MNT_RELOAD;
    304 				ret = mount(MOUNT_EXT2FS, "/", flags, &args);
    305 				if (ret == 0)
    306 					return(0);
    307 			}
    308 		}
    309 		if (!preen)
    310 			printf("\n***** REBOOT NOW *****\n");
    311 		sync();
    312 		return (4);
    313 	}
    314 	return (0);
    315 }
    316 
    317 static void
    318 usage()
    319 {
    320 	extern char *__progname;
    321 
    322 	(void) fprintf(stderr,
    323 	    "Usage: %s [-dfnpy] [-b block] [-c level] [-m mode] filesystem ...\n",
    324 	    __progname);
    325 	exit(1);
    326 }
    327 
    328