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