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