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