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