main.c revision 70050
1226031Sstas/*
2226031Sstas * Copyright (c) 1980, 1986, 1993
3226031Sstas *	The Regents of the University of California.  All rights reserved.
4226031Sstas *
5226031Sstas * Redistribution and use in source and binary forms, with or without
6226031Sstas * modification, are permitted provided that the following conditions
7226031Sstas * are met:
8226031Sstas * 1. Redistributions of source code must retain the above copyright
9226031Sstas *    notice, this list of conditions and the following disclaimer.
10226031Sstas * 2. Redistributions in binary form must reproduce the above copyright
11226031Sstas *    notice, this list of conditions and the following disclaimer in the
12226031Sstas *    documentation and/or other materials provided with the distribution.
13226031Sstas * 3. All advertising materials mentioning features or use of this software
14226031Sstas *    must display the following acknowledgement:
15226031Sstas *	This product includes software developed by the University of
16226031Sstas *	California, Berkeley and its contributors.
17226031Sstas * 4. Neither the name of the University nor the names of its contributors
18226031Sstas *    may be used to endorse or promote products derived from this software
19226031Sstas *    without specific prior written permission.
20226031Sstas *
21226031Sstas * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22226031Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23226031Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24226031Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25226031Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26226031Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27226031Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28226031Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29226031Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30226031Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31226031Sstas * SUCH DAMAGE.
32226031Sstas */
33226031Sstas
34226031Sstas#ifndef lint
35226031Sstasstatic const char copyright[] =
36226031Sstas"@(#) Copyright (c) 1980, 1986, 1993\n\
37226031Sstas	The Regents of the University of California.  All rights reserved.\n";
38226031Sstas#endif /* not lint */
39226031Sstas
40226031Sstas#ifndef lint
41226031Sstas#if 0
42226031Sstasstatic char sccsid[] = "@(#)main.c	8.6 (Berkeley) 5/14/95";
43226031Sstas#endif
44226031Sstasstatic const char rcsid[] =
45226031Sstas  "$FreeBSD: head/sbin/fsck_ffs/main.c 70050 2000-12-15 14:23:55Z iedowse $";
46226031Sstas#endif /* not lint */
47226031Sstas
48226031Sstas#include <sys/param.h>
49226031Sstas#include <sys/stat.h>
50226031Sstas#include <sys/time.h>
51226031Sstas#include <sys/mount.h>
52226031Sstas#include <sys/resource.h>
53226031Sstas
54226031Sstas#include <ufs/ufs/dinode.h>
55226031Sstas#include <ufs/ufs/ufsmount.h>
56226031Sstas#include <ufs/ffs/fs.h>
57226031Sstas
58226031Sstas#include <err.h>
59226031Sstas#include <errno.h>
60226031Sstas#include <fstab.h>
61226031Sstas#include <paths.h>
62226031Sstas
63226031Sstas#include "fsck.h"
64226031Sstas
65226031Sstasint returntosingle;
66226031Sstas
67226031Sstasstatic void usage __P((void));
68226031Sstasstatic int argtoi __P((int flag, char *req, char *str, int base));
69226031Sstasstatic int docheck __P((struct fstab *fsp));
70226031Sstasstatic int checkfilesys __P((char *filesys, char *mntpt, long auxdata,
71226031Sstas		int child));
72226031Sstasstatic struct statfs *getmntpt __P((const char *));
73226031Sstasint main __P((int argc, char *argv[]));
74226031Sstas
75226031Sstasint
76226031Sstasmain(argc, argv)
77226031Sstas	int	argc;
78226031Sstas	char	*argv[];
79226031Sstas{
80226031Sstas	int ch;
81226031Sstas	struct rlimit rlimit;
82226031Sstas	int ret = 0;
83226031Sstas
84226031Sstas	sync();
85226031Sstas	skipclean = 1;
86226031Sstas	markclean = 1;
87226031Sstas	while ((ch = getopt(argc, argv, "b:c:dfm:npy")) != -1) {
88226031Sstas		switch (ch) {
89226031Sstas		case 'b':
90226031Sstas			skipclean = 0;
91226031Sstas			bflag = argtoi('b', "number", optarg, 10);
92226031Sstas			printf("Alternate super block location: %d\n", bflag);
93226031Sstas			break;
94226031Sstas
95226031Sstas		case 'c':
96226031Sstas			skipclean = 0;
97226031Sstas			cvtlevel = argtoi('c', "conversion level", optarg, 10);
98226031Sstas			break;
99226031Sstas
100226031Sstas		case 'd':
101226031Sstas			debug++;
102226031Sstas			break;
103226031Sstas
104226031Sstas		case 'f':
105226031Sstas			skipclean = 0;
106226031Sstas			break;
107226031Sstas
108226031Sstas		case 'm':
109226031Sstas			lfmode = argtoi('m', "mode", optarg, 8);
110226031Sstas			if (lfmode &~ 07777)
111226031Sstas				errx(EEXIT, "bad mode to -m: %o", lfmode);
112226031Sstas			printf("** lost+found creation mode %o\n", lfmode);
113226031Sstas			break;
114226031Sstas
115226031Sstas		case 'n':
116226031Sstas			nflag++;
117226031Sstas			yflag = 0;
118226031Sstas			break;
119226031Sstas
120226031Sstas		case 'p':
121226031Sstas			preen++;
122226031Sstas			break;
123226031Sstas
124226031Sstas		case 'y':
125226031Sstas			yflag++;
126226031Sstas			nflag = 0;
127226031Sstas			break;
128226031Sstas
129226031Sstas		default:
130226031Sstas			usage();
131226031Sstas		}
132226031Sstas	}
133226031Sstas	argc -= optind;
134226031Sstas	argv += optind;
135226031Sstas
136226031Sstas	if (!argc)
137226031Sstas		usage();
138226031Sstas
139226031Sstas	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
140226031Sstas		(void)signal(SIGINT, catch);
141226031Sstas	if (preen)
142226031Sstas		(void)signal(SIGQUIT, catchquit);
143226031Sstas	signal(SIGINFO, infohandler);
144226031Sstas	/*
145226031Sstas	 * Push up our allowed memory limit so we can cope
146226031Sstas	 * with huge filesystems.
147226031Sstas	 */
148226031Sstas	if (getrlimit(RLIMIT_DATA, &rlimit) == 0) {
149226031Sstas		rlimit.rlim_cur = rlimit.rlim_max;
150226031Sstas		(void)setrlimit(RLIMIT_DATA, &rlimit);
151226031Sstas	}
152226031Sstas	while (argc-- > 0)
153226031Sstas		(void)checkfilesys(blockcheck(*argv++), 0, 0L, 0);
154226031Sstas
155226031Sstas	if (returntosingle)
156226031Sstas		ret = 2;
157226031Sstas	exit(ret);
158226031Sstas}
159226031Sstas
160226031Sstasstatic int
161226031Sstasargtoi(flag, req, str, base)
162226031Sstas	int flag;
163226031Sstas	char *req, *str;
164226031Sstas	int base;
165226031Sstas{
166226031Sstas	char *cp;
167226031Sstas	int ret;
168226031Sstas
169226031Sstas	ret = (int)strtol(str, &cp, base);
170226031Sstas	if (cp == str || *cp)
171234027Sstas		errx(EEXIT, "-%c flag requires a %s", flag, req);
172226031Sstas	return (ret);
173226031Sstas}
174
175/*
176 * Check the specified filesystem.
177 */
178/* ARGSUSED */
179static int
180checkfilesys(filesys, mntpt, auxdata, child)
181	char *filesys, *mntpt;
182	long auxdata;
183	int child;
184{
185	ufs_daddr_t n_ffree, n_bfree;
186	struct dups *dp;
187	struct statfs *mntbuf;
188	struct zlncnt *zlnp;
189	int cylno;
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	 * Get the mount point information of the filesystem, if
212	 * it is available.
213	 */
214	mntbuf = getmntpt(filesys);
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 (mntbuf != NULL && mntbuf->f_flags & MNT_ROOTFS)
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
321	/*
322	 * Check to see if the filesystem is mounted read-write.
323	 */
324	if (mntbuf != NULL && (mntbuf->f_flags & MNT_RDONLY) == 0)
325		resolved = 0;
326	ckfini(resolved);
327
328	for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
329		if (inostathead[cylno].il_stat != NULL)
330			free((char *)inostathead[cylno].il_stat);
331	free((char *)inostathead);
332	inostathead = NULL;
333	if (fsmodified && !preen)
334		printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
335	if (rerun)
336		printf("\n***** PLEASE RERUN FSCK *****\n");
337	if (mntbuf != NULL) {
338		struct ufs_args args;
339		int ret;
340		/*
341		 * We modified a mounted filesystem.  Do a mount update on
342		 * it unless it is read-write, so we can continue using it
343		 * as safely as possible.
344		 */
345		if (mntbuf->f_flags & MNT_RDONLY) {
346			args.fspec = 0;
347			args.export.ex_flags = 0;
348			args.export.ex_root = 0;
349			ret = mount("ufs", mntbuf->f_mntonname,
350			    mntbuf->f_flags | MNT_UPDATE | MNT_RELOAD, &args);
351			if (ret == 0)
352				return (0);
353			pwarn("mount reload of '%s' failed: %s\n\n",
354			    mntbuf->f_mntonname, strerror(errno));
355		}
356		if (!fsmodified)
357			return (0);
358		if (!preen)
359			printf("\n***** REBOOT NOW *****\n");
360		sync();
361		return (4);
362	}
363	return (0);
364}
365
366/*
367 * Get the directory that the device is mounted on.
368 */
369static struct statfs *
370getmntpt(name)
371	const char *name;
372{
373	struct stat devstat, mntdevstat;
374	char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
375	char *devname;
376	struct statfs *mntbuf;
377	int i, mntsize;
378
379	if (stat(name, &devstat) != 0 ||
380	    !(S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode)))
381		return (NULL);
382	mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
383	for (i = 0; i < mntsize; i++) {
384		if (strcmp(mntbuf[i].f_fstypename, "ufs") != 0)
385			continue;
386		devname = mntbuf[i].f_mntfromname;
387		if (*devname != '/') {
388			strcpy(device, _PATH_DEV);
389			strcat(device, devname);
390			devname = device;
391		}
392		if (stat(devname, &mntdevstat) == 0 &&
393		    mntdevstat.st_rdev == devstat.st_rdev)
394			return (&mntbuf[i]);
395	}
396	return (NULL);
397}
398
399static void
400usage()
401{
402        extern char *__progname;
403
404        (void) fprintf(stderr,
405            "Usage: %s [-dfnpy] [-B be|le] [-b block] [-c level] [-m mode] "
406                        "filesystem ...\n",
407            __progname);
408        exit(1);
409}
410