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