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