xref: /titanic_52/usr/src/cmd/fs.d/ufs/fsck/main.c (revision bdfc6d18da790deeec2e0eb09c625902defe2498)
1 /*
2  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
7 /*	  All Rights Reserved  	*/
8 
9 
10 /*
11  * Copyright (c) 1980, 1986, 1990 The Regents of the University of California.
12  * All rights reserved.
13  *
14  * Redistribution and use in source and binary forms are permitted
15  * provided that: (1) source distributions retain this entire copyright
16  * notice and comment, and (2) distributions including binaries display
17  * the following acknowledgement:  ``This product includes software
18  * developed by the University of California, Berkeley and its contributors''
19  * in the documentation or other materials provided with the distribution
20  * and in all advertising materials mentioning features or use of this
21  * software. Neither the name of the University nor the names of its
22  * contributors may be used to endorse or promote products derived
23  * from this software without specific prior written permission.
24  * THIS SOFTWARE IS PROVIDED '`AS IS'' AND WITHOUT ANY EXPRESS OR
25  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
26  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27  */
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 /*
32  * In-core structures:
33  * blockmap[]
34  *	A bitmap of block usage very similar to what's on disk, but
35  *	for the entire filesystem rather than just a cylinder group.
36  *	Zero indicates free, one indicates allocated.  Note that this
37  *	is opposite the interpretation of a cylinder group's free block
38  *	bitmap.
39  *
40  * statemap[]
41  *	Tracks what is known about each inode in the filesystem.
42  *	The fundamental state value is one of USTATE, FSTATE, DSTATE,
43  *	or SSTATE (unallocated, file, directory, shadow/acl).
44  *
45  *	There are optional modifying attributes as well: INZLINK,
46  *	INFOUND, INCLEAR, INORPHAN, and INDELAYD.  The IN prefix
47  *	stands for inode.  INZLINK declares that no links (di_nlink ==
48  *	0) to the inode have been found.  It is used instead of
49  *	examining di_nlink because we've always got the statemap[] in
50  *	memory, and on average the odds are against having any given
51  *	inode in the cache.  INFOUND flags that an inode was
52  *	encountered during the descent of the filesystem.  In other
53  *	words, it's reachable, either by name or by being an acl or
54  *	attribute.  INCLEAR declares an intent to call clri() on an
55  *	inode.
56  *
57  *	INORPHAN indicates that the inode has already been seen once
58  *	in pass3 and determined to be an orphan, so any additional
59  *	encounters don't need to waste cycles redetermining that status.
60  *	It also means we don't ask the user about doing something to the
61  *	inode N times.
62  *
63  *	INDELAYD marks inodes that pass1 determined needed to be truncated.
64  *	They can't be truncated during that pass, because it depends on
65  *	having a stable world for building the block and inode tables from.
66  *
67  *	The IN flags rarely used directly, but instead are
68  *	pre-combined through the {D,F,S}ZLINK, DFOUND, and
69  *	{D,F,S}CLEAR convenience macros.  This mainly matters when
70  *	trying to use grep on the source.
71  *
72  *	Three state-test macros are provided: S_IS_DUNFOUND(),
73  *	S_IS_DVALID(), and S_IS_ZLINK().  The first is true when an
74  *	inode's state indicates that it is either a simple directory
75  *	(DSTATE without the INFOUND or INCLEAR modifiers) or a
76  *	directory with the INZLINK modifier set.  By definition, if a
77  *	directory has zero links, then it can't be found.  As for
78  *	S_IS_DVALID(), it decides if a directory inode is alive.
79  *	Effectively, this translates to whether or not it's been
80  *	flagged for clearing.  If not, then it's valid for current
81  *	purposes.  This is true even if INZLINK is set, as we may find
82  *	a reference to it later.  Finally, S_IS_ZLINK() just picks out
83  *	the INZLINK flag from the state.
84  *
85  *	The S_*() macros all work on a state value.  To simplify a
86  *	bit, the INO_IS_{DUNFOUND,DVALID}() macros take an inode
87  *	number argument.  The inode is looked up in the statemap[] and
88  *	the result handed off to the corresponding S_*() macro.  This
89  *	is partly a holdover from working with different data
90  *	structures (with the same net intent) in the BSD fsck.
91  *
92  * lncntp
93  *	Each entry is initialized to the di_link from the on-disk
94  *	inode.  Each time we find one of those links, we decrement it.
95  *	Once all the traversing is done, we should have a zero.  If we
96  *	have a positive value, then some reference disappeared
97  *	(probably from a directory that got nuked); deal with it by
98  *	fixing the count.  If we have a negative value, then we found
99  *	an extra reference.  This is a can't-happen, except in the
100  *	special case of when we reconnect a directory to its parent or
101  *	to lost+found.  An exact match between lncntp[] and the on-disk
102  *      inode means it's completely unreferenced.
103  *
104  * aclphead
105  *	This is a hash table of the acl inodes in the filesystem.
106  *
107  * aclpsort
108  *	The same acls as in aclphead, but as a simple linear array.
109  *	It is used to hold the acl pointers for sorting and scanning
110  *	in pass3b.
111  */
112 
113 #include <stdio.h>
114 #include <stdlib.h>
115 #include <unistd.h>
116 #include <sys/types.h>
117 #include <sys/param.h>
118 #include <sys/int_types.h>
119 #include <sys/mntent.h>
120 #include <sys/fs/ufs_fs.h>
121 #include <sys/vnode.h>
122 #include <sys/fs/ufs_inode.h>
123 #include <sys/stat.h>
124 #include <fcntl.h>
125 #include <sys/wait.h>
126 #include <sys/mnttab.h>
127 #include <signal.h>
128 #include <string.h>
129 #include <sys/vfstab.h>
130 #include <sys/statvfs.h>
131 #include <sys/filio.h>
132 #include <ustat.h>
133 #include <errno.h>
134 #include "fsck.h"
135 
136 static void usage(void);
137 static long argtol(int, char *, char *, int);
138 static void checkfilesys(char *);
139 static void check_sanity(char *);
140 static void report_limbo(const void *, VISIT, int);
141 
142 #define	QUICK_CHECK	'm'	/* are things ok according to superblock? */
143 #define	ALL_no		'n'	/* auto-answer interactive questions `no' */
144 #define	ALL_NO		'N'	/* auto-answer interactive questions `no' */
145 #define	UFS_OPTS	'o'	/* ufs-specific options, see subopts[] */
146 #define	ECHO_CMD	'V'	/* echo the command line */
147 #define	ALL_yes		'y'	/* auto-answer interactive questions `yes' */
148 #define	ALL_YES		'Y'	/* auto-answer interactive questions `yes' */
149 #define	VERBOSE		'v'	/* be chatty */
150 
151 static char *subopts[] = {
152 #define	PREEN		0	/* non-interactive mode (parent is parallel) */
153 	"p",
154 #define	BLOCK		1	/* alternate superblock */
155 	"b",
156 #define	DEBUG		2	/* yammer */
157 	"d",
158 #define	ONLY_WRITES	3	/* check all writable filesystems */
159 	"w",
160 #define	FORCE		4	/* force checking, even if clean */
161 	"f",
162 	NULL
163 };
164 
165 /*
166  * Filesystems that are `magical' - if they exist in vfstab,
167  * then they have to be mounted for the system to have gotten
168  * far enough to be able to run fsck.  Thus, don't get all
169  * bent out of shape if we're asked to check it and it is mounted.
170  */
171 char *magic_fs[] = {
172 	"",			/* MAGIC_NONE, for normal filesystems */
173 	"/",			/* MAGIC_ROOT */
174 	"/usr",			/* MAGIC_USR */
175 	"/var",			/* MAGIC_VAR */
176 	NULL			/* MAGIC_LIMIT */
177 };
178 
179 void
180 main(int argc, char *argv[])
181 {
182 	int c;
183 	int wflag = 0;
184 	char *suboptions, *value;
185 	struct rlimit rlimit;
186 	extern int optind;
187 	extern char *optarg;
188 
189 	while ((c = getopt(argc, argv, "mnNo:VvyY")) != EOF) {
190 		switch (c) {
191 
192 		case QUICK_CHECK:
193 			mflag++;
194 			break;
195 
196 		case ALL_no:
197 		case ALL_NO:
198 			nflag++;
199 			yflag = 0;
200 			break;
201 
202 		case VERBOSE:
203 			verbose++;
204 			break;
205 
206 		case UFS_OPTS:
207 			/*
208 			 * ufs specific options.
209 			 */
210 			if (optarg == NULL) {
211 				usage();
212 				/*
213 				 * lint does not believe this, nor does it
214 				 * believe #pragma does_not_return(usage)
215 				 */
216 				/* NOTREACHED */
217 			}
218 			suboptions = optarg;
219 			while (*suboptions != '\0') {
220 				switch (getsubopt(&suboptions, subopts,
221 				    &value)) {
222 
223 				case PREEN:
224 					preen++;
225 					break;
226 
227 				case BLOCK:
228 					bflag = argtol(BLOCK, "block",
229 					    value, 10);
230 					(void) printf("Alternate super block "
231 					    "location: %ld.\n",
232 					    (long)bflag);
233 					break;
234 
235 				case DEBUG:
236 					debug++;
237 					verbose++;
238 					break;
239 
240 				case ONLY_WRITES:
241 					/* check only writable filesystems */
242 					wflag++;
243 					break;
244 
245 				case FORCE:
246 					fflag++;
247 					break;
248 
249 				default:
250 					usage();
251 				}
252 			}
253 			break;
254 
255 		case ECHO_CMD:
256 			{
257 				int	opt_count;
258 				char	*opt_text;
259 
260 				(void) printf("fsck -F ufs ");
261 				for (opt_count = 1; opt_count < argc;
262 								opt_count++) {
263 					opt_text = argv[opt_count];
264 					if (opt_text)
265 						(void) printf("%s ", opt_text);
266 				}
267 				(void) printf("\n");
268 			}
269 			break;
270 
271 		case ALL_yes:
272 		case ALL_YES:
273 			yflag++;
274 			nflag = 0;
275 			break;
276 
277 		default:
278 			usage();
279 		}
280 	}
281 	argc -= optind;
282 	argv += optind;
283 
284 	if (argc == 0)
285 		usage();
286 
287 	rflag++; /* check raw devices where we can */
288 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
289 		(void) signal(SIGINT, catch);
290 	if (preen)
291 		(void) signal(SIGQUIT, catchquit);
292 
293 	/*
294 	 * Push up our allowed memory limit so we can cope
295 	 * with huge file systems.
296 	 */
297 	if (getrlimit(RLIMIT_DATA, &rlimit) == 0) {
298 		rlimit.rlim_cur = rlimit.rlim_max;
299 		(void) setrlimit(RLIMIT_DATA, &rlimit);
300 	}
301 
302 	/*
303 	 * There are a lot of places where we just exit if a problem is
304 	 * found.  This means that we won't necessarily check everything
305 	 * we were asked to.  It would be nice to do everything, and
306 	 * then provide a summary when we're done.  However, the
307 	 * interface doesn't really allow us to do that in any useful
308 	 * way.  So, we'll just bail on the first unrecoverable
309 	 * problem encountered.  If we've been run by the generic
310 	 * wrapper, we were only given one filesystem to check, so the
311 	 * multi-fs case implies being run manually; that means the
312 	 * user can rerun us on the remaining filesystems when it's
313 	 * convenient for them.
314 	 */
315 	while (argc-- > 0) {
316 		if (wflag && !writable(*argv)) {
317 			(void) fprintf(stderr, "not writeable '%s'\n", *argv);
318 			argv++;
319 			if (exitstat == 0)
320 				exitstat = EXBADPARM;
321 		} else {
322 			checkfilesys(*argv++);
323 		}
324 	}
325 	if (interrupted)
326 		exitstat = EXSIGNAL;
327 	exit(exitstat);
328 }
329 
330 /*
331  * A relatively intelligent strtol().  Note that if str is NULL, we'll
332  * exit, so ret does not actually need to be pre-initialized.  Lint
333  * doesn't believe this, and it's harmless enough to make lint happy here.
334  */
335 static long
336 argtol(int flag, char *req, char *str, int base)
337 {
338 	char *cp = str;
339 	long ret = -1;
340 
341 	errno = 0;
342 	if (str != NULL)
343 		ret = strtol(str, &cp, base);
344 	if (cp == str || *cp) {
345 		(void) fprintf(stderr, "-%c flag requires a %s\n", flag, req);
346 		exit(EXBADPARM);
347 	}
348 	if (errno != 0) {
349 		(void) fprintf(stderr, "-%c %s value out of range\n",
350 		    flag, req);
351 	}
352 
353 	return (ret);
354 }
355 
356 /*
357  * Check the specified file system.
358  */
359 static void
360 checkfilesys(char *filesys)
361 {
362 	daddr32_t n_ffree, n_bfree;
363 	char *devstr;
364 	fsck_ino_t files;
365 	daddr32_t blks;
366 	fsck_ino_t inumber;
367 	int zlinks_printed;
368 	fsck_ino_t limbo_victim;
369 	double dbl_nffree, dbl_dsize;
370 	int quiet_dups;
371 
372 	mountfd = -1;
373 	hotroot = 0;
374 	mountedfs = M_NOMNT;
375 	reattached_dir = 0;
376 	broke_dir_link = 0;
377 	iscorrupt = 1;		/* assume failure in setup() */
378 	islog = 0;
379 	islogok = 0;
380 	overflowed_lf = 0;
381 	errorlocked = is_errorlocked(filesys);
382 	limbo_dirs = NULL;
383 
384 	if ((devstr = setup(filesys)) == NULL) {
385 		if (!iscorrupt) {
386 			return;
387 		}
388 
389 		if (preen)
390 			pfatal("CAN'T CHECK FILE SYSTEM.");
391 		if ((exitstat == 0) && (mflag))
392 			exitstat = EXUMNTCHK;
393 		exit(exitstat);
394 	} else {
395 		devname = devstr;
396 	}
397 
398 	if (mflag) {
399 		check_sanity(filesys);
400 		/* NOTREACHED */
401 	}
402 
403 	if (debug)
404 		printclean();
405 
406 	iscorrupt = 0;		/* setup() succeeded, assume good filesystem */
407 
408 	/*
409 	 * 1: scan inodes tallying blocks used
410 	 */
411 	if (!preen) {
412 		/* hotroot is reported as such in setup() if debug is on */
413 		if (mountedfs != M_NOMNT)
414 			(void) printf("** Currently Mounted on %s\n",
415 			    sblock.fs_fsmnt);
416 		else
417 			(void) printf("** Last Mounted on %s\n",
418 			    sblock.fs_fsmnt);
419 		(void) printf("** Phase 1 - Check Blocks and Sizes\n");
420 	}
421 	pass1();
422 
423 	/*
424 	 * 1b: locate first references to duplicates, if any
425 	 */
426 	if (have_dups()) {
427 		if (preen)
428 			pfatal("INTERNAL ERROR: dups with -o p");
429 		(void) printf("** Phase 1b - Rescan For More DUPS\n");
430 		pass1b();
431 	}
432 
433 	/*
434 	 * 2a: check for duplicate name entries inside each directory.
435 	 */
436 #if 0
437 	/*
438 	 * XXX This is disabled for performance reasons.  It should become
439 	 * an optional test.  Don't forget to update the printed label for
440 	 * pass2b() when this is re-enabled.
441 	 */
442 	if (!preen)
443 		(void) printf("** Phase 2a - Check Duplicated Names\n");
444 	pass2a();
445 #endif
446 	/*
447 	 * 2b: traverse directories from root to mark all connected directories
448 	 */
449 	if (!preen)
450 		(void) printf("** Phase 2 - Check Pathnames\n");
451 	pass2b();
452 
453 	/*
454 	 * 3a: scan inodes looking for disconnected directories.
455 	 */
456 	if (!preen)
457 		(void) printf("** Phase 3a - Check Connectivity\n");
458 	pass3a();
459 
460 	/*
461 	 * 3b: check acls
462 	 */
463 	if (!preen)
464 		(void) printf("** Phase 3b - Verify Shadows/ACLs\n");
465 	pass3b();
466 
467 	/*
468 	 * 4: scan inodes looking for disconnected files; check reference counts
469 	 */
470 	if (!preen)
471 		(void) printf("** Phase 4 - Check Reference Counts\n");
472 	pass4();
473 
474 	/*
475 	 * 5: check and repair resource counts in cylinder groups
476 	 */
477 	if (!preen)
478 		(void) printf("** Phase 5 - Check Cylinder Groups\n");
479 recount:
480 	pass5();
481 
482 	if (overflowed_lf) {
483 		iscorrupt = 1;
484 	}
485 
486 	if (mountedfs == M_RW) {
487 		iscorrupt = 1;
488 	}
489 
490 	if (have_dups()) {
491 		quiet_dups = (reply("LIST REMAINING DUPS") == 0);
492 		if (report_dups(quiet_dups) > 0)
493 			iscorrupt = 1;
494 
495 		(void) printf("WARNING: DATA LOSS MAY HAVE OCCURRED DUE TO "
496 		    "DUP BLOCKS.\nVERIFY FILE CONTENTS BEFORE USING.\n");
497 	}
498 
499 	if (limbo_dirs != NULL) {
500 		/*
501 		 * Don't force iscorrupt, as this is sufficiently
502 		 * harmless that the filesystem can be mounted and
503 		 * used.  We just leak some inodes and/or blocks.
504 		 */
505 		pwarn("Orphan directories not cleared or reconnected:\n");
506 
507 		twalk(limbo_dirs, report_limbo);
508 
509 		while (limbo_dirs != NULL) {
510 			limbo_victim = *(fsck_ino_t *)limbo_dirs;
511 			if (limbo_victim != NULL) {
512 				(void) tdelete((void *)limbo_victim,
513 				    &limbo_dirs,
514 				    ino_t_cmp);
515 			}
516 		}
517 
518 		rerun = 1;
519 	}
520 
521 	if (iscorrupt) {
522 		(void) printf("FILESYSTEM MAY STILL BE INCONSISTENT.\n");
523 		rerun = 1;
524 	}
525 
526 	/*
527 	 * iscorrupt must be stable at this point.
528 	 * updateclean() returns true when it had to discard the log.
529 	 * This can only happen once, since sblock.fs_logbno gets
530 	 * cleared as part of that operation.
531 	 */
532 	if (updateclean()) {
533 		if (!preen)
534 			(void) printf(
535 			    "Log was discarded, updating cyl groups\n");
536 		goto recount;
537 	}
538 
539 	if (debug)
540 		printclean();
541 
542 	ckfini();
543 
544 	/*
545 	 * print out summary statistics
546 	 */
547 	n_ffree = sblock.fs_cstotal.cs_nffree;
548 	n_bfree = sblock.fs_cstotal.cs_nbfree;
549 	files = maxino - UFSROOTINO - sblock.fs_cstotal.cs_nifree - n_files;
550 	blks = n_blks +
551 	    sblock.fs_ncg * (cgdmin(&sblock, 0) - cgsblock(&sblock, 0));
552 	blks += cgsblock(&sblock, 0) - cgbase(&sblock, 0);
553 	blks += howmany(sblock.fs_cssize, sblock.fs_fsize);
554 	blks = maxfsblock - (n_ffree + sblock.fs_frag * n_bfree) - blks;
555 	if (debug && (files > 0 || blks > 0)) {
556 		countdirs = sblock.fs_cstotal.cs_ndir - countdirs;
557 		pwarn("Reclaimed: %d directories, %d files, %lld fragments\n",
558 		    countdirs, files - countdirs,
559 		    (longlong_t)blks);
560 	}
561 
562 	dbl_nffree = (double)n_ffree;
563 	dbl_dsize = (double)sblock.fs_dsize;
564 
565 	if (!verbose) {
566 		/*
567 		 * Done as one big string to try for a single write,
568 		 * so the output doesn't get interleaved with other
569 		 * preening fscks.
570 		 */
571 		pwarn("%ld files, %lld used, %lld free "
572 		    "(%lld frags, %lld blocks, %.1f%% fragmentation)\n",
573 		    (long)n_files, (longlong_t)n_blks,
574 		    (longlong_t)n_ffree + sblock.fs_frag * n_bfree,
575 		    (longlong_t)n_ffree, (longlong_t)n_bfree,
576 		    (dbl_nffree * 100.0) / dbl_dsize);
577 	} else {
578 		pwarn("\nFilesystem summary:\n");
579 		pwarn("Inodes in use: %ld\n", (long)n_files);
580 		pwarn("Blocks in use: %lld\n", (longlong_t)n_blks);
581 		pwarn("Total free fragments: %lld\n",
582 		    (longlong_t)n_ffree + sblock.fs_frag * n_bfree);
583 		pwarn("Free fragments not in blocks: %lld\n",
584 		    (longlong_t)n_ffree);
585 		pwarn("Total free blocks: %lld\n", (longlong_t)n_bfree);
586 		pwarn("Fragment/block fragmentation: %.1f%%\n",
587 		    (dbl_nffree * 100.0) / dbl_dsize);
588 		pwarn("");
589 
590 		if (files < 0)
591 			pwarn("%d inodes missing\n", -files);
592 		if (blks < 0)
593 			pwarn("%lld blocks missing\n", -(longlong_t)blks);
594 
595 		zlinks_printed = 0;
596 		for (inumber = UFSROOTINO; inumber < maxino; inumber++) {
597 			if (S_IS_ZLINK(statemap[inumber])) {
598 				if (zlinks_printed == 0) {
599 					pwarn("The following zero "
600 					    "link count inodes remain:");
601 				}
602 				if (zlinks_printed) {
603 					if ((zlinks_printed % 9) == 0)
604 						(void) puts(",\n");
605 					else
606 						(void) puts(", ");
607 				}
608 				(void) printf("%u", inumber);
609 				zlinks_printed++;
610 			}
611 		}
612 		if ((zlinks_printed != 0) && ((zlinks_printed % 9) != 0))
613 			(void) putchar('\n');
614 	}
615 
616 	/*
617 	 * Clean up after ourselves, so we can do the next filesystem.
618 	 */
619 	free_dup_state();
620 	inocleanup();
621 	free(blockmap);
622 	free(statemap);
623 	free((void *)lncntp);
624 	lncntp = NULL;
625 	blockmap = NULL;
626 	statemap = NULL;
627 	if (iscorrupt && exitstat == 0)
628 		exitstat = EXFNDERRS;
629 	if (fsmodified)
630 		(void) printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
631 	if (overflowed_lf)
632 		(void) printf("\n***** %s FULL, MUST REMOVE ENTRIES *****\n",
633 		    lfname);
634 	if (reattached_dir) {
635 		(void) printf("ORPHANED DIRECTORIES REATTACHED; DIR LINK "
636 		    "COUNTS MAY NOT BE CORRECT.\n");
637 		rerun = 1;
638 	}
639 	if (broke_dir_link) {
640 		(void) printf(
641 		    "DIRECTORY HARDLINK BROKEN; LOOPS MAY STILL EXIST.\n");
642 		rerun = 1;
643 	}
644 	if (iscorrupt)
645 		(void) printf("***** FILE SYSTEM IS BAD *****\n");
646 
647 	if (rerun)
648 		(void) printf("\n***** PLEASE RERUN FSCK *****\n");
649 
650 	if ((exitstat == 0) &&
651 	    (((mountedfs != M_NOMNT) && !errorlocked) || hotroot)) {
652 		exitstat = EXROOTOKAY;
653 	}
654 
655 	if ((exitstat == 0) && rerun)
656 		exitstat = EXFNDERRS;
657 
658 	if (mountedfs != M_NOMNT) {
659 		if (!fsmodified)
660 			return;
661 		/*
662 		 * _FIOFFS is much more effective than a simple sync().
663 		 * Note that the original fswritefd was discarded in
664 		 * ckfini().
665 		 */
666 		fswritefd = open(devstr, O_RDWR, 0);
667 		if (fswritefd != -1) {
668 			(void) ioctl(fswritefd, _FIOFFS, NULL);
669 			(void) close(fswritefd);
670 		}
671 
672 		if (!preen)
673 			(void) printf("\n***** REBOOT NOW *****\n");
674 
675 		exitstat = EXREBOOTNOW;
676 	}
677 }
678 
679 /*
680  * fsck -m: does the filesystem pass cursory examination
681  *
682  * XXX This is very redundant with setup().  The right thing would be
683  *     for setup() to modify its behaviour when mflag is set (less
684  *     chatty, exit instead of return, etc).
685  */
686 void
687 check_sanity(char *filename)
688 {
689 	struct stat64 stbd, stbr;
690 	char *devname;
691 	struct ustat usb;
692 	char vfsfilename[MAXPATHLEN];
693 	struct vfstab vfsbuf;
694 	FILE *vfstab;
695 	struct statvfs vfs_stat;
696 	int found_magic[MAGIC_LIMIT];
697 	int magic_cnt;
698 	int is_magic = 0;
699 	int is_block;
700 
701 	(void) memset((void *)found_magic, 0, sizeof (found_magic));
702 
703 	if (stat64(filename, &stbd) < 0) {
704 		(void) fprintf(stderr,
705 		"ufs fsck: sanity check failed : cannot stat %s\n", filename);
706 		exit(EXNOSTAT);
707 	}
708 
709 	if ((stbd.st_mode & S_IFMT) == S_IFBLK) {
710 		is_block = 1;
711 	} else if ((stbd.st_mode & S_IFMT) == S_IFCHR) {
712 		is_block = 0;
713 	} else {
714 		/*
715 		 * In !mflag mode, we allow checking the contents
716 		 * of a file.  Since this is intended primarily for
717 		 * speeding up boot-time checks and allowing for a
718 		 * file complicates the ok-input tests, we'll disallow
719 		 * that option.
720 		 */
721 		(void) fprintf(stderr,
722 			"ufs fsck: sanity check failed: "
723 			"%s not block or character device\n",
724 			filename);
725 		exit(EXNOSTAT);
726 	}
727 
728 	/*
729 	 * Determine if this is the root file system via vfstab. Give up
730 	 * silently on failures. The whole point of this is to be tolerant
731 	 * of the magic file systems being already mounted.
732 	 */
733 	if ((vfstab = fopen(VFSTAB, "r")) != 0) {
734 		for (magic_cnt = 0; magic_cnt < MAGIC_LIMIT; magic_cnt++) {
735 			if (magic_cnt == MAGIC_NONE)
736 				continue;
737 			if (getvfsfile(vfstab, &vfsbuf,
738 			    magic_fs[magic_cnt]) == 0) {
739 				if (is_block)
740 					devname = vfsbuf.vfs_special;
741 				else
742 					devname = vfsbuf.vfs_fsckdev;
743 				if (stat64(devname, &stbr) == 0) {
744 					if (stbr.st_rdev == stbd.st_rdev) {
745 						found_magic[magic_cnt] = 1;
746 						is_magic = magic_cnt;
747 						break;
748 					}
749 				}
750 			}
751 		}
752 	}
753 
754 	/*
755 	 * Only works if filename is a block device or if
756 	 * character and block device has the same dev_t value.
757 	 * This is currently true, but nothing really forces it.
758 	 */
759 	if (!is_magic && (ustat(stbd.st_rdev, &usb) == 0)) {
760 		(void) fprintf(stderr,
761 		    "ufs fsck: sanity check: %s already mounted\n", filename);
762 		exit(EXMOUNTED);
763 	}
764 
765 	if (is_magic) {
766 		(void) strcpy(vfsfilename, magic_fs[is_magic]);
767 		if (statvfs(vfsfilename, &vfs_stat) != 0) {
768 			(void) fprintf(stderr, "ufs fsck: Cannot stat %s\n",
769 			    vfsfilename);
770 			exit(EXNOSTAT);
771 		}
772 
773 		if (!(vfs_stat.f_flag & ST_RDONLY)) {
774 			/*
775 			 * The file system is mounted read/write
776 			 * We need to exit saying this. If it's only
777 			 * mounted readonly, we can continue.
778 			 */
779 
780 			(void) fprintf(stderr,
781 				"ufs fsck: sanity check:"
782 				"%s already mounted read/write\n",
783 				filename);
784 			exit(EXMOUNTED);
785 		}
786 	}
787 
788 	/*
789 	 * We know that at boot, the ufs root file system is mounted
790 	 * read-only first.  After fsck runs, it is remounted as
791 	 * read-write.  Therefore, we do not need to check for different
792 	 * values for fs_state between the root file system and the
793 	 * rest of the file systems.
794 	 */
795 	if (islog && !islogok) {
796 		(void) fprintf(stderr,
797 		    "ufs fsck: sanity check: %s needs checking\n", filename);
798 		exit(EXUMNTCHK);
799 	}
800 	if ((sblock.fs_state + (long)sblock.fs_time == FSOKAY) &&
801 		(sblock.fs_clean == FSCLEAN || sblock.fs_clean == FSSTABLE ||
802 		(sblock.fs_clean == FSLOG && islog))) {
803 		(void) fprintf(stderr,
804 		    "ufs fsck: sanity check: %s okay\n", filename);
805 	} else {
806 		(void) fprintf(stderr,
807 		    "ufs fsck: sanity check: %s needs checking\n", filename);
808 		exit(EXUMNTCHK);
809 	}
810 	exit(EXOKAY);
811 }
812 
813 caddr_t
814 hasvfsopt(struct vfstab *vfs, char *opt)
815 {
816 	struct mnttab mtab;
817 
818 	if (vfs->vfs_mntopts == NULL)
819 		return (NULL);
820 	mtab.mnt_mntopts = vfs->vfs_mntopts;
821 	return (hasmntopt(&mtab, opt));
822 }
823 
824 void
825 usage(void)
826 {
827 	(void) fprintf(stderr,
828 	    "ufs usage: fsck [-F ufs] [-m] [-n] [-V] [-v] [-y] "
829 	    "[-o p,b=#,w,f] [special ....]\n");
830 
831 	exit(EXBADPARM);
832 }
833 
834 /*ARGSUSED*/
835 static void
836 report_limbo(const void *node, VISIT order, int level)
837 {
838 	fsck_ino_t ino = *(fsck_ino_t *)node;
839 
840 	if ((order == postorder) || (order == leaf)) {
841 		(void) printf("    Inode %d\n", ino);
842 	}
843 }
844