xref: /netbsd-src/sbin/fsck_ffs/main.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: main.c,v 1.66 2007/07/16 17:06:52 pooka 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 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1993\n\
35 	The Regents of the University of California.  All rights reserved.\n");
36 #endif /* not lint */
37 
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)main.c	8.6 (Berkeley) 5/14/95";
41 #else
42 __RCSID("$NetBSD: main.c,v 1.66 2007/07/16 17:06:52 pooka Exp $");
43 #endif
44 #endif /* not lint */
45 
46 #include <sys/param.h>
47 #include <sys/time.h>
48 #include <sys/mount.h>
49 #include <sys/resource.h>
50 
51 #include <ufs/ufs/dinode.h>
52 #include <ufs/ufs/ufsmount.h>
53 #include <ufs/ffs/fs.h>
54 #include <ufs/ffs/ffs_extern.h>
55 
56 #include <ctype.h>
57 #include <err.h>
58 #include <fstab.h>
59 #include <string.h>
60 #include <time.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <unistd.h>
64 #include <signal.h>
65 
66 #include "fsck.h"
67 #include "extern.h"
68 #include "fsutil.h"
69 
70 int	returntosingle;
71 int	progress = 0;
72 
73 static int	argtoi(int, const char *, const char *, int);
74 static int	checkfilesys(const char *, char *, long, int);
75 static void	usage(void);
76 
77 int
78 main(int argc, char *argv[])
79 {
80 	struct rlimit r;
81 	int ch;
82 	int ret = 0;
83 
84 	if (getrlimit(RLIMIT_DATA, &r) == 0) {
85 		r.rlim_cur = r.rlim_max;
86 		(void) setrlimit(RLIMIT_DATA, &r);
87 	}
88 	sync();
89 	skipclean = 1;
90 	markclean = 1;
91 	forceimage = 0;
92 	endian = 0;
93 	isappleufs = 0;
94 	while ((ch = getopt(argc, argv, "aB:b:c:dFfm:npPqy")) != -1) {
95 		switch (ch) {
96 		case 'a':
97 			isappleufs = 1;
98 			break;
99 
100 		case 'B':
101 			if (strcmp(optarg, "be") == 0)
102 				endian = BIG_ENDIAN;
103 			else if (strcmp(optarg, "le") == 0)
104 				endian = LITTLE_ENDIAN;
105 			else usage();
106 			break;
107 
108 		case 'b':
109 			skipclean = 0;
110 			bflag = argtoi('b', "number", optarg, 10);
111 			printf("Alternate super block location: %d\n", bflag);
112 			break;
113 
114 		case 'c':
115 			skipclean = 0;
116 			cvtlevel = argtoi('c', "conversion level", optarg, 10);
117 			if (cvtlevel > 4) {
118 				cvtlevel = 4;
119 				warnx("Using maximum conversion level of %d\n",cvtlevel);
120 			}
121 			break;
122 
123 		case 'd':
124 			debug++;
125 			break;
126 
127 		case 'F':
128 			forceimage = 1;
129 			break;
130 
131 		case 'f':
132 			skipclean = 0;
133 			break;
134 
135 		case 'm':
136 			lfmode = argtoi('m', "mode", optarg, 8);
137 			if (lfmode &~ 07777)
138 				errx(EEXIT, "bad mode to -m: %o", lfmode);
139 			printf("** lost+found creation mode %o\n", lfmode);
140 			break;
141 
142 		case 'n':
143 			nflag++;
144 			yflag = 0;
145 			break;
146 
147 		case 'p':
148 			preen++;
149 			break;
150 
151 		case 'P':
152 			progress = 1;
153 			break;
154 
155 		case 'q':
156 			quiet++;
157 			break;
158 
159 		case 'y':
160 			yflag++;
161 			nflag = 0;
162 			break;
163 
164 		default:
165 			usage();
166 		}
167 	}
168 
169 	argc -= optind;
170 	argv += optind;
171 
172 	if (!argc)
173 		usage();
174 
175 	if (debug)
176 		progress = 0;
177 
178 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
179 		(void)signal(SIGINT, catch);
180 	if (preen)
181 		(void)signal(SIGQUIT, catchquit);
182 #ifdef PROGRESS
183 	if (progress) {
184 		progress_ttywidth(0);
185 		(void)signal(SIGWINCH, progress_ttywidth);
186 	}
187 #endif /* ! PROGRESS */
188 	signal(SIGINFO, infohandler);
189 
190 	while (argc-- > 0) {
191 		const char *path = blockcheck(*argv);
192 
193 		if (path == NULL)
194 			pfatal("Can't check %s\n", *argv);
195 		else
196 			(void)checkfilesys(blockcheck(*argv), 0, 0L, 0);
197 		argv++;
198 	}
199 
200 	if (returntosingle)
201 		ret = 2;
202 
203 	exit(ret);
204 }
205 
206 static int
207 argtoi(int flag, const char *req, const char *str, int base)
208 {
209 	char *cp;
210 	int ret;
211 
212 	ret = (int)strtol(str, &cp, base);
213 	if (cp == str || *cp)
214 		errx(EEXIT, "-%c flag requires a %s", flag, req);
215 	return (ret);
216 }
217 
218 /*
219  * Check the specified filesystem.
220  */
221 /* ARGSUSED */
222 static int
223 checkfilesys(const char *filesys, char *mntpt, long auxdata, int child)
224 {
225 	daddr_t n_ffree, n_bfree;
226 	struct dups *dp;
227 	struct zlncnt *zlnp;
228 	int cylno;
229 #ifdef LITE2BORKEN
230 	int flags;
231 #endif
232 #ifdef PROGRESS
233 	/*
234 	 * In prune mode, how far does the progress bar travel during
235 	 * each pass?  (In non-prune mode, each pass has a separate
236 	 * progress bar that travels from 0 to 100%.)
237 	 *
238 	 * The numbers below are percentages, intended to correspond
239 	 * roughly to the cumulative time up to the end of each pass.
240 	 * They don't have to be accurate.  In reality, on a large
241 	 * file system, Pass 1 and Pass 2 together are likely to use
242 	 * significantly more than the 95% reflected below, so users
243 	 * will get a pleasant surprise when the last 5% of the progress
244 	 * bar runs more quickly than they had expected.
245 	 */
246 	static int progress_limits[] = {0, 20, 95, 96, 97, 100};
247 #endif /* PROGRESS */
248 
249 	if (preen && child)
250 		(void)signal(SIGQUIT, voidquit);
251 	setcdevname(filesys, preen);
252 	if (debug && preen)
253 		pwarn("starting\n");
254 	switch (setup(filesys)) {
255 	case 0:
256 		if (preen)
257 			pfatal("CAN'T CHECK FILE SYSTEM.");
258 		/* fall through */
259 	case -1:
260 		return (0);
261 	}
262 	/*
263 	 * Cleared if any questions answered no. Used to decide if
264 	 * the superblock should be marked clean.
265 	 */
266 	resolved = 1;
267 
268 #ifdef PROGRESS
269 	progress_switch(progress);
270 	progress_init();
271 #endif /* PROGRESS */
272 
273 	/*
274 	 * 1: scan inodes tallying blocks used
275 	 */
276 	if (preen == 0) {
277 		pwarn("** Last Mounted on %s\n", sblock->fs_fsmnt);
278 		if (hotroot())
279 			pwarn("** Root file system\n");
280 		pwarn("** Phase 1 - Check Blocks and Sizes\n");
281 	}
282 #ifdef PROGRESS
283 	if (preen)
284 		progress_setrange(0, progress_limits[1]);
285 #endif /* PROGRESS */
286 	pass1();
287 
288 	/*
289 	 * 1b: locate first references to duplicates, if any
290 	 */
291 	if (duplist) {
292 		if (preen)
293 			pfatal("INTERNAL ERROR: dups with -p\n");
294 		if (usedsoftdep)
295 			pfatal("INTERNAL ERROR: dups with softdep\n");
296 		pwarn("** Phase 1b - Rescan For More DUPS\n");
297 		pass1b();
298 	}
299 
300 	/*
301 	 * 2: traverse directories from root to mark all connected directories
302 	 */
303 	if (preen == 0)
304 		pwarn("** Phase 2 - Check Pathnames\n");
305 #ifdef PROGRESS
306 	if (preen)
307 		progress_sethighlim(progress_limits[2]);
308 #endif /* PROGRESS */
309 	pass2();
310 
311 	/*
312 	 * 3: scan inodes looking for disconnected directories
313 	 */
314 	if (preen == 0)
315 		pwarn("** Phase 3 - Check Connectivity\n");
316 #ifdef PROGRESS
317 	if (preen)
318 		progress_sethighlim(progress_limits[3]);
319 #endif /* PROGRESS */
320 	pass3();
321 
322 	/*
323 	 * 4: scan inodes looking for disconnected files; check reference counts
324 	 */
325 	if (preen == 0)
326 		pwarn("** Phase 4 - Check Reference Counts\n");
327 #ifdef PROGRESS
328 	if (preen)
329 		progress_sethighlim(progress_limits[4]);
330 #endif /* PROGRESS */
331 	pass4();
332 
333 	/*
334 	 * 5: check and repair resource counts in cylinder groups
335 	 */
336 	if (preen == 0)
337 		pwarn("** Phase 5 - Check Cyl groups\n");
338 #ifdef PROGRESS
339 	if (preen)
340 		progress_sethighlim(progress_limits[5]);
341 #endif /* PROGRESS */
342 	pass5();
343 
344 	/*
345 	 * print out summary statistics
346 	 */
347 	n_ffree = sblock->fs_cstotal.cs_nffree;
348 	n_bfree = sblock->fs_cstotal.cs_nbfree;
349 	pwarn("%llu files, %lld used, %lld free ",
350 	    (unsigned long long)n_files, (long long)n_blks,
351 	    (long long)(n_ffree + sblock->fs_frag * n_bfree));
352 	printf("(%lld frags, %lld blocks, %lld.%lld%% fragmentation)\n",
353 	    (long long)n_ffree, (long long)n_bfree,
354 	    (long long)(n_ffree * 100 / (daddr_t)sblock->fs_dsize),
355 	    (long long)(((n_ffree * 1000 + (daddr_t)sblock->fs_dsize / 2)
356 		/ (daddr_t)sblock->fs_dsize) % 10));
357 	if (debug &&
358 	    (n_files -= maxino - ROOTINO - sblock->fs_cstotal.cs_nifree))
359 		printf("%llu files missing\n", (unsigned long long)n_files);
360 	if (debug) {
361 		n_blks += sblock->fs_ncg *
362 			(cgdmin(sblock, 0) - cgsblock(sblock, 0));
363 		n_blks += cgsblock(sblock, 0) - cgbase(sblock, 0);
364 		n_blks += howmany(sblock->fs_cssize, sblock->fs_fsize);
365 		if (n_blks -= maxfsblock - (n_ffree + sblock->fs_frag * n_bfree))
366 			printf("%lld blocks missing\n", (long long)n_blks);
367 		if (duplist != NULL) {
368 			printf("The following duplicate blocks remain:");
369 			for (dp = duplist; dp; dp = dp->next)
370 				printf(" %lld,", (long long)dp->dup);
371 			printf("\n");
372 		}
373 		if (zlnhead != NULL) {
374 			printf("The following zero link count inodes remain:");
375 			for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
376 				printf(" %llu,",
377 				    (unsigned long long)zlnp->zlncnt);
378 			printf("\n");
379 		}
380 	}
381 	zlnhead = (struct zlncnt *)0;
382 	duplist = (struct dups *)0;
383 	muldup = (struct dups *)0;
384 	inocleanup();
385 	if (fsmodified) {
386 		sblock->fs_time = time(NULL);
387 		sbdirty();
388 	}
389 	if (rerun)
390 		markclean = 0;
391 #if LITE2BORKEN
392 	if (!hotroot()) {
393 		ckfini();
394 	} else {
395 		struct statvfs stfs_buf;
396 		/*
397 		 * Check to see if root is mounted read-write.
398 		 */
399 		if (statvfs("/", &stfs_buf) == 0)
400 			flags = stfs_buf.f_flag;
401 		else
402 			flags = 0;
403 		if (markclean)
404 			markclean = flags & MNT_RDONLY;
405 		ckfini();
406 	}
407 #else
408 	ckfini();
409 #endif
410 	for (cylno = 0; cylno < sblock->fs_ncg; cylno++)
411 		if (inostathead[cylno].il_stat != NULL)
412 			free(inostathead[cylno].il_stat);
413 	free(inostathead);
414 	inostathead = NULL;
415 
416 	if (!resolved || rerun) {
417 		pwarn("\n***** UNRESOLVED INCONSISTENCIES REMAIN *****\n");
418 		returntosingle = 1;
419 	}
420 	if (!fsmodified)
421 		return (0);
422 	if (!preen)
423 		pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n");
424 	if (rerun)
425 		pwarn("\n***** PLEASE RERUN FSCK *****\n");
426 	if (hotroot()) {
427 		struct statvfs stfs_buf;
428 		/*
429 		 * We modified the root.  Do a mount update on
430 		 * it, unless it is read-write, so we can continue.
431 		 */
432 		if (statvfs("/", &stfs_buf) == 0) {
433 			long flags = stfs_buf.f_flag;
434 			struct ufs_args args;
435 			int ret;
436 
437 			if (flags & MNT_RDONLY) {
438 				args.fspec = 0;
439 				flags |= MNT_UPDATE | MNT_RELOAD;
440 				ret = mount(MOUNT_FFS, "/", flags,
441 				    &args, sizeof args);
442 				if (ret == 0)
443 					return(0);
444 			}
445 		}
446 		if (!preen)
447 			pwarn("\n***** REBOOT NOW *****\n");
448 		sync();
449 		return (4);
450 	}
451 	return (0);
452 }
453 
454 static void
455 usage(void)
456 {
457 
458 	(void) fprintf(stderr,
459 	    "usage: %s [-adFfnPpqy] [-B be|le] [-b block] [-c level] [-m mode]"
460 	    " filesystem ...\n",
461 	    getprogname());
462 	exit(1);
463 }
464 
465