main.c revision 1.12
1/*	$NetBSD: main.c,v 1.12 2003/08/07 10:04:16 agc Exp $	*/
2
3/*
4 * Copyright (c) 1980, 1986, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32/*
33 * Copyright (c) 1997 Manuel Bouyer.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 *    notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 *    notice, this list of conditions and the following disclaimer in the
42 *    documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 *    must display the following acknowledgement:
45 *	This product includes software developed by the University of
46 *	California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 *    may be used to endorse or promote products derived from this software
49 *    without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 */
63
64#include <sys/cdefs.h>
65#ifndef lint
66__COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1993\n\
67	The Regents of the University of California.  All rights reserved.\n");
68#endif /* not lint */
69
70#ifndef lint
71#if 0
72static char sccsid[] = "@(#)main.c	8.2 (Berkeley) 1/23/94";
73#else
74__RCSID("$NetBSD: main.c,v 1.12 2003/08/07 10:04:16 agc Exp $");
75#endif
76#endif /* not lint */
77
78#include <sys/param.h>
79#include <sys/time.h>
80#include <sys/mount.h>
81#include <ufs/ufs/ufsmount.h>
82#include <ufs/ext2fs/ext2fs_dinode.h>
83#include <ufs/ext2fs/ext2fs.h>
84#include <fstab.h>
85#include <stdlib.h>
86#include <string.h>
87#include <ctype.h>
88#include <stdio.h>
89#include <time.h>
90#include <unistd.h>
91
92#include "fsck.h"
93#include "extern.h"
94#include "fsutil.h"
95
96int	returntosingle;
97
98int	main __P((int, char *[]));
99
100static int	argtoi __P((int, char *, char *, int));
101static int	checkfilesys __P((const char *, char *, long, int));
102static  void usage __P((void));
103
104int
105main(argc, argv)
106	int	argc;
107	char	*argv[];
108{
109	int ch;
110	int ret = 0;
111
112	sync();
113	skipclean = 1;
114	while ((ch = getopt(argc, argv, "b:c:dfm:npy")) != -1) {
115		switch (ch) {
116		case 'b':
117			skipclean = 0;
118			bflag = argtoi('b', "number", optarg, 10);
119			printf("Alternate super block location: %d\n", bflag);
120			break;
121
122		case 'd':
123			debug++;
124			break;
125
126		case 'f':
127			skipclean = 0;
128			break;
129
130		case 'm':
131			lfmode = argtoi('m', "mode", optarg, 8);
132			if (lfmode &~ 07777)
133				errexit("bad mode to -m: %o\n", lfmode);
134			printf("** lost+found creation mode %o\n", lfmode);
135			break;
136
137		case 'n':
138			nflag++;
139			yflag = 0;
140			break;
141
142		case 'p':
143			preen++;
144			break;
145
146		case 'y':
147			yflag++;
148			nflag = 0;
149			break;
150
151		default:
152			usage();
153		}
154	}
155
156	argc -= optind;
157	argv += optind;
158
159	if (!argc)
160		usage();
161
162	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
163		(void)signal(SIGINT, catch);
164	if (preen)
165		(void)signal(SIGQUIT, catchquit);
166
167	while (argc-- > 0)
168		(void)checkfilesys(blockcheck(*argv++), 0, 0L, 0);
169
170	if (returntosingle)
171		ret = 2;
172
173	exit(ret);
174}
175
176static int
177argtoi(flag, req, str, base)
178	int flag;
179	char *req, *str;
180	int base;
181{
182	char *cp;
183	int ret;
184
185	ret = (int)strtol(str, &cp, base);
186	if (cp == str || *cp)
187		errexit("-%c flag requires a %s\n", flag, req);
188	return (ret);
189}
190
191/*
192 * Check the specified filesystem.
193 */
194/* ARGSUSED */
195static int
196checkfilesys(filesys, mntpt, auxdata, child)
197	const char *filesys;
198	char *mntpt;
199	long auxdata;
200	int child;
201{
202	daddr_t n_bfree;
203	struct dups *dp;
204	struct zlncnt *zlnp;
205	int i;
206
207	if (preen && child)
208		(void)signal(SIGQUIT, voidquit);
209	setcdevname(filesys, preen);
210	if (debug && preen)
211		pwarn("starting\n");
212	switch (setup(filesys)) {
213	case 0:
214		if (preen)
215			pfatal("CAN'T CHECK FILE SYSTEM.");
216	case -1:
217		return (0);
218	}
219	/*
220	 * 1: scan inodes tallying blocks used
221	 */
222	if (preen == 0) {
223		if (sblock.e2fs.e2fs_rev > E2FS_REV0) {
224			printf("** Last Mounted on %s\n",
225			    sblock.e2fs.e2fs_fsmnt);
226		}
227		if (hotroot())
228			printf("** Root file system\n");
229		printf("** Phase 1 - Check Blocks and Sizes\n");
230	}
231	pass1();
232
233	/*
234	 * 1b: locate first references to duplicates, if any
235	 */
236	if (duplist) {
237		if (preen)
238			pfatal("INTERNAL ERROR: dups with -p");
239		printf("** Phase 1b - Rescan For More DUPS\n");
240		pass1b();
241	}
242
243	/*
244	 * 2: traverse directories from root to mark all connected directories
245	 */
246	if (preen == 0)
247		printf("** Phase 2 - Check Pathnames\n");
248	pass2();
249
250	/*
251	 * 3: scan inodes looking for disconnected directories
252	 */
253	if (preen == 0)
254		printf("** Phase 3 - Check Connectivity\n");
255	pass3();
256
257	/*
258	 * 4: scan inodes looking for disconnected files; check reference counts
259	 */
260	if (preen == 0)
261		printf("** Phase 4 - Check Reference Counts\n");
262	pass4();
263
264	/*
265	 * 5: check and repair resource counts in cylinder groups
266	 */
267	if (preen == 0)
268		printf("** Phase 5 - Check Cyl groups\n");
269	pass5();
270
271	/*
272	 * print out summary statistics
273	 */
274	n_bfree = sblock.e2fs.e2fs_fbcount;
275
276	pwarn("%lld files, %lld used, %lld free\n",
277	    (long long)n_files, (long long)n_blks, (long long)n_bfree);
278	if (debug &&
279		/* 9 reserved and unused inodes in FS */
280	    (n_files -= maxino - 9 - sblock.e2fs.e2fs_ficount))
281		printf("%lld files missing\n", (long long)n_files);
282	if (debug) {
283		for (i = 0; i < sblock.e2fs_ncg; i++)
284			n_blks +=  cgoverhead(i);
285		n_blks += sblock.e2fs.e2fs_first_dblock;
286		if (n_blks -= maxfsblock - n_bfree)
287			printf("%lld blocks missing\n", (long long)n_blks);
288		if (duplist != NULL) {
289			printf("The following duplicate blocks remain:");
290			for (dp = duplist; dp; dp = dp->next)
291				printf(" %lld,", (long long)dp->dup);
292			printf("\n");
293		}
294		if (zlnhead != NULL) {
295			printf("The following zero link count inodes remain:");
296			for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
297				printf(" %u,", zlnp->zlncnt);
298			printf("\n");
299		}
300	}
301	zlnhead = (struct zlncnt *)0;
302	duplist = (struct dups *)0;
303	muldup = (struct dups *)0;
304	inocleanup();
305	if (fsmodified) {
306		time_t t;
307		(void)time(&t);
308		sblock.e2fs.e2fs_wtime = t;
309		sblock.e2fs.e2fs_lastfsck = t;
310		sbdirty();
311	}
312	ckfini(1);
313	free(blockmap);
314	free(statemap);
315	free((char *)lncntp);
316	if (!fsmodified)
317		return (0);
318	if (!preen)
319		printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
320	if (rerun)
321		printf("\n***** PLEASE RERUN FSCK *****\n");
322	if (hotroot()) {
323		struct statfs stfs_buf;
324		/*
325		 * We modified the root.  Do a mount update on
326		 * it, unless it is read-write, so we can continue.
327		 */
328		if (statfs("/", &stfs_buf) == 0) {
329			long flags = stfs_buf.f_flags;
330			struct ufs_args args;
331			int ret;
332
333			if (flags & MNT_RDONLY) {
334				args.fspec = 0;
335				args.export.ex_flags = 0;
336				args.export.ex_root = 0;
337				flags |= MNT_UPDATE | MNT_RELOAD;
338				ret = mount(MOUNT_EXT2FS, "/", flags, &args);
339				if (ret == 0)
340					return(0);
341			}
342		}
343		if (!preen)
344			printf("\n***** REBOOT NOW *****\n");
345		sync();
346		return (4);
347	}
348	return (0);
349}
350
351static void
352usage()
353{
354
355	(void) fprintf(stderr,
356	    "Usage: %s [-dfnpy] [-b block] [-c level] [-m mode] filesystem ...\n",
357	    getprogname());
358	exit(1);
359}
360
361