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