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