Home | History | Annotate | Line # | Download | only in fsck_ffs
main.c revision 1.1.1.2
      1 /*
      2  * Copyright (c) 1980, 1986, 1993
      3  *	The Regents of the University of California.  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 static char copyright[] =
     36 "@(#) Copyright (c) 1980, 1986, 1993\n\
     37 	The Regents of the University of California.  All rights reserved.\n";
     38 #endif /* not lint */
     39 
     40 #ifndef lint
     41 static char sccsid[] = "@(#)main.c	8.2 (Berkeley) 1/23/94";
     42 #endif /* not lint */
     43 
     44 #include <sys/param.h>
     45 #include <sys/time.h>
     46 #include <sys/mount.h>
     47 #include <ufs/ufs/dinode.h>
     48 #include <ufs/ffs/fs.h>
     49 #include <fstab.h>
     50 #include <stdlib.h>
     51 #include <string.h>
     52 #include <ctype.h>
     53 #include <stdio.h>
     54 #include "fsck.h"
     55 
     56 void	catch(), catchquit(), voidquit();
     57 int	returntosingle;
     58 
     59 main(argc, argv)
     60 	int	argc;
     61 	char	*argv[];
     62 {
     63 	int ch;
     64 	int ret, maxrun = 0;
     65 	extern int docheck(), checkfilesys();
     66 	extern char *optarg, *blockcheck();
     67 	extern int optind;
     68 
     69 	sync();
     70 	while ((ch = getopt(argc, argv, "dpnNyYb:c:l:m:")) != EOF) {
     71 		switch (ch) {
     72 		case 'p':
     73 			preen++;
     74 			break;
     75 
     76 		case 'b':
     77 			bflag = argtoi('b', "number", optarg, 10);
     78 			printf("Alternate super block location: %d\n", bflag);
     79 			break;
     80 
     81 		case 'c':
     82 			cvtlevel = argtoi('c', "conversion level", optarg, 10);
     83 			break;
     84 
     85 		case 'd':
     86 			debug++;
     87 			break;
     88 
     89 		case 'l':
     90 			maxrun = argtoi('l', "number", optarg, 10);
     91 			break;
     92 
     93 		case 'm':
     94 			lfmode = argtoi('m', "mode", optarg, 8);
     95 			if (lfmode &~ 07777)
     96 				errexit("bad mode to -m: %o\n", lfmode);
     97 			printf("** lost+found creation mode %o\n", lfmode);
     98 			break;
     99 
    100 		case 'n':
    101 		case 'N':
    102 			nflag++;
    103 			yflag = 0;
    104 			break;
    105 
    106 		case 'y':
    107 		case 'Y':
    108 			yflag++;
    109 			nflag = 0;
    110 			break;
    111 
    112 		default:
    113 			errexit("%c option?\n", ch);
    114 		}
    115 	}
    116 	argc -= optind;
    117 	argv += optind;
    118 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
    119 		(void)signal(SIGINT, catch);
    120 	if (preen)
    121 		(void)signal(SIGQUIT, catchquit);
    122 	if (argc) {
    123 		while (argc-- > 0)
    124 			(void)checkfilesys(blockcheck(*argv++), 0, 0L, 0);
    125 		exit(0);
    126 	}
    127 	ret = checkfstab(preen, maxrun, docheck, checkfilesys);
    128 	if (returntosingle)
    129 		exit(2);
    130 	exit(ret);
    131 }
    132 
    133 argtoi(flag, req, str, base)
    134 	int flag;
    135 	char *req, *str;
    136 	int base;
    137 {
    138 	char *cp;
    139 	int ret;
    140 
    141 	ret = (int)strtol(str, &cp, base);
    142 	if (cp == str || *cp)
    143 		errexit("-%c flag requires a %s\n", flag, req);
    144 	return (ret);
    145 }
    146 
    147 /*
    148  * Determine whether a filesystem should be checked.
    149  */
    150 docheck(fsp)
    151 	register struct fstab *fsp;
    152 {
    153 
    154 	if (strcmp(fsp->fs_vfstype, "ufs") ||
    155 	    (strcmp(fsp->fs_type, FSTAB_RW) &&
    156 	     strcmp(fsp->fs_type, FSTAB_RO)) ||
    157 	    fsp->fs_passno == 0)
    158 		return (0);
    159 	return (1);
    160 }
    161 
    162 /*
    163  * Check the specified filesystem.
    164  */
    165 /* ARGSUSED */
    166 checkfilesys(filesys, mntpt, auxdata, child)
    167 	char *filesys, *mntpt;
    168 	long auxdata;
    169 {
    170 	daddr_t n_ffree, n_bfree;
    171 	struct dups *dp;
    172 	struct zlncnt *zlnp;
    173 	int cylno;
    174 
    175 	if (preen && child)
    176 		(void)signal(SIGQUIT, voidquit);
    177 	cdevname = filesys;
    178 	if (debug && preen)
    179 		pwarn("starting\n");
    180 	if (setup(filesys) == 0) {
    181 		if (preen)
    182 			pfatal("CAN'T CHECK FILE SYSTEM.");
    183 		return (0);
    184 	}
    185 	/*
    186 	 * 1: scan inodes tallying blocks used
    187 	 */
    188 	if (preen == 0) {
    189 		printf("** Last Mounted on %s\n", sblock.fs_fsmnt);
    190 		if (hotroot)
    191 			printf("** Root file system\n");
    192 		printf("** Phase 1 - Check Blocks and Sizes\n");
    193 	}
    194 	pass1();
    195 
    196 	/*
    197 	 * 1b: locate first references to duplicates, if any
    198 	 */
    199 	if (duplist) {
    200 		if (preen)
    201 			pfatal("INTERNAL ERROR: dups with -p");
    202 		printf("** Phase 1b - Rescan For More DUPS\n");
    203 		pass1b();
    204 	}
    205 
    206 	/*
    207 	 * 2: traverse directories from root to mark all connected directories
    208 	 */
    209 	if (preen == 0)
    210 		printf("** Phase 2 - Check Pathnames\n");
    211 	pass2();
    212 
    213 	/*
    214 	 * 3: scan inodes looking for disconnected directories
    215 	 */
    216 	if (preen == 0)
    217 		printf("** Phase 3 - Check Connectivity\n");
    218 	pass3();
    219 
    220 	/*
    221 	 * 4: scan inodes looking for disconnected files; check reference counts
    222 	 */
    223 	if (preen == 0)
    224 		printf("** Phase 4 - Check Reference Counts\n");
    225 	pass4();
    226 
    227 	/*
    228 	 * 5: check and repair resource counts in cylinder groups
    229 	 */
    230 	if (preen == 0)
    231 		printf("** Phase 5 - Check Cyl groups\n");
    232 	pass5();
    233 
    234 	/*
    235 	 * print out summary statistics
    236 	 */
    237 	n_ffree = sblock.fs_cstotal.cs_nffree;
    238 	n_bfree = sblock.fs_cstotal.cs_nbfree;
    239 	pwarn("%ld files, %ld used, %ld free ",
    240 	    n_files, n_blks, n_ffree + sblock.fs_frag * n_bfree);
    241 	printf("(%ld frags, %ld blocks, %d.%d%% fragmentation)\n",
    242 	    n_ffree, n_bfree, (n_ffree * 100) / sblock.fs_dsize,
    243 	    ((n_ffree * 1000 + sblock.fs_dsize / 2) / sblock.fs_dsize) % 10);
    244 	if (debug &&
    245 	    (n_files -= maxino - ROOTINO - sblock.fs_cstotal.cs_nifree))
    246 		printf("%ld files missing\n", n_files);
    247 	if (debug) {
    248 		n_blks += sblock.fs_ncg *
    249 			(cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
    250 		n_blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
    251 		n_blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
    252 		if (n_blks -= maxfsblock - (n_ffree + sblock.fs_frag * n_bfree))
    253 			printf("%ld 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(" %ld,", 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(" %lu,", 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 		(void)time(&sblock.fs_time);
    273 		sbdirty();
    274 	}
    275 	if (cvtlevel && sblk.b_dirty) {
    276 		/*
    277 		 * Write out the duplicate super blocks
    278 		 */
    279 		for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
    280 			bwrite(fswritefd, (char *)&sblock,
    281 			    fsbtodb(&sblock, cgsblock(&sblock, cylno)), SBSIZE);
    282 	}
    283 	ckfini();
    284 	free(blockmap);
    285 	free(statemap);
    286 	free((char *)lncntp);
    287 	if (!fsmodified)
    288 		return (0);
    289 	if (!preen)
    290 		printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
    291 	if (hotroot) {
    292 		struct statfs stfs_buf;
    293 		/*
    294 		 * We modified the root.  Do a mount update on
    295 		 * it, unless it is read-write, so we can continue.
    296 		 */
    297 		if (statfs("/", &stfs_buf) == 0) {
    298 			long flags = stfs_buf.f_flags;
    299 			struct ufs_args args;
    300 			int ret;
    301 
    302 			if (flags & MNT_RDONLY) {
    303 				args.fspec = 0;
    304 				args.export.ex_flags = 0;
    305 				args.export.ex_root = 0;
    306 				flags |= MNT_UPDATE | MNT_RELOAD;
    307 				ret = mount(MOUNT_UFS, "/", flags, &args);
    308 				if (ret == 0)
    309 					return(0);
    310 			}
    311 		}
    312 		if (!preen)
    313 			printf("\n***** REBOOT NOW *****\n");
    314 		sync();
    315 		return (4);
    316 	}
    317 	return (0);
    318 }
    319