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