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