Home | History | Annotate | Line # | Download | only in fsck_ffs
main.c revision 1.70
      1  1.70    dogcow /*	$NetBSD: main.c,v 1.70 2008/08/30 14:07:32 dogcow Exp $	*/
      2  1.15       cgd 
      3   1.1       cgd /*
      4  1.13   mycroft  * Copyright (c) 1980, 1986, 1993
      5  1.13   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.46       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32  1.24     lukem #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34  1.68     lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1993\
     35  1.68     lukem  The Regents of the University of California.  All rights reserved.");
     36   1.1       cgd #endif /* not lint */
     37   1.1       cgd 
     38   1.1       cgd #ifndef lint
     39  1.15       cgd #if 0
     40  1.25     lukem static char sccsid[] = "@(#)main.c	8.6 (Berkeley) 5/14/95";
     41  1.15       cgd #else
     42  1.70    dogcow __RCSID("$NetBSD: main.c,v 1.70 2008/08/30 14:07:32 dogcow Exp $");
     43  1.15       cgd #endif
     44   1.1       cgd #endif /* not lint */
     45   1.1       cgd 
     46   1.1       cgd #include <sys/param.h>
     47  1.12       cgd #include <sys/time.h>
     48  1.13   mycroft #include <sys/mount.h>
     49  1.29    mjacob #include <sys/resource.h>
     50  1.25     lukem 
     51  1.13   mycroft #include <ufs/ufs/dinode.h>
     52  1.30      fvdl #include <ufs/ufs/ufsmount.h>
     53  1.13   mycroft #include <ufs/ffs/fs.h>
     54  1.31    bouyer #include <ufs/ffs/ffs_extern.h>
     55  1.25     lukem 
     56  1.25     lukem #include <ctype.h>
     57  1.25     lukem #include <err.h>
     58  1.70    dogcow #include <errno.h>
     59   1.1       cgd #include <fstab.h>
     60   1.1       cgd #include <string.h>
     61  1.32    kleink #include <time.h>
     62   1.1       cgd #include <stdio.h>
     63  1.26     lukem #include <stdlib.h>
     64  1.14       cgd #include <unistd.h>
     65  1.64  drochner #include <signal.h>
     66  1.14       cgd 
     67   1.1       cgd #include "fsck.h"
     68  1.14       cgd #include "extern.h"
     69  1.21  christos #include "fsutil.h"
     70  1.67  christos #include "exitvalues.h"
     71  1.69    bouyer #include "snapshot.h"
     72   1.1       cgd 
     73  1.53  christos int	progress = 0;
     74  1.67  christos int	returntosingle = 0;
     75  1.23  christos 
     76  1.58  christos static int	argtoi(int, const char *, const char *, int);
     77  1.69    bouyer static int	checkfilesys(const char *, const char *, int);
     78  1.57   xtraeme static void	usage(void);
     79  1.69    bouyer static char 	*get_snap_device(char *);
     80  1.23  christos 
     81  1.14       cgd int
     82  1.56   xtraeme main(int argc, char *argv[])
     83   1.1       cgd {
     84  1.29    mjacob 	struct rlimit r;
     85   1.1       cgd 	int ch;
     86  1.67  christos 	int ret = FSCK_EXIT_OK;
     87  1.69    bouyer 	char *snap_backup = NULL;
     88  1.69    bouyer 	int snap_internal = 0;
     89   1.1       cgd 
     90  1.29    mjacob 	if (getrlimit(RLIMIT_DATA, &r) == 0) {
     91  1.29    mjacob 		r.rlim_cur = r.rlim_max;
     92  1.29    mjacob 		(void) setrlimit(RLIMIT_DATA, &r);
     93  1.29    mjacob 	}
     94   1.1       cgd 	sync();
     95  1.16   mycroft 	skipclean = 1;
     96  1.31    bouyer 	markclean = 1;
     97  1.40     lukem 	forceimage = 0;
     98  1.31    bouyer 	endian = 0;
     99  1.42       dbj 	isappleufs = 0;
    100  1.69    bouyer 	while ((ch = getopt(argc, argv, "aB:b:c:dFfm:npPqyx:X")) != -1) {
    101   1.1       cgd 		switch (ch) {
    102  1.42       dbj 		case 'a':
    103  1.42       dbj 			isappleufs = 1;
    104  1.42       dbj 			break;
    105  1.42       dbj 
    106  1.31    bouyer 		case 'B':
    107  1.31    bouyer 			if (strcmp(optarg, "be") == 0)
    108  1.31    bouyer 				endian = BIG_ENDIAN;
    109  1.31    bouyer 			else if (strcmp(optarg, "le") == 0)
    110  1.31    bouyer 				endian = LITTLE_ENDIAN;
    111  1.31    bouyer 			else usage();
    112  1.31    bouyer 			break;
    113  1.31    bouyer 
    114   1.1       cgd 		case 'b':
    115  1.16   mycroft 			skipclean = 0;
    116   1.1       cgd 			bflag = argtoi('b', "number", optarg, 10);
    117   1.1       cgd 			printf("Alternate super block location: %d\n", bflag);
    118   1.1       cgd 			break;
    119   1.1       cgd 
    120   1.1       cgd 		case 'c':
    121  1.16   mycroft 			skipclean = 0;
    122  1.13   mycroft 			cvtlevel = argtoi('c', "conversion level", optarg, 10);
    123  1.49       dbj 			if (cvtlevel > 4) {
    124  1.49       dbj 				cvtlevel = 4;
    125  1.67  christos 				warnx("Using maximum conversion level of %d\n",
    126  1.67  christos 				    cvtlevel);
    127  1.49       dbj 			}
    128   1.1       cgd 			break;
    129  1.13   mycroft 
    130   1.1       cgd 		case 'd':
    131   1.1       cgd 			debug++;
    132  1.17       cgd 			break;
    133  1.17       cgd 
    134  1.40     lukem 		case 'F':
    135  1.40     lukem 			forceimage = 1;
    136  1.40     lukem 			break;
    137  1.40     lukem 
    138  1.17       cgd 		case 'f':
    139  1.17       cgd 			skipclean = 0;
    140   1.1       cgd 			break;
    141   1.1       cgd 
    142   1.1       cgd 		case 'm':
    143   1.1       cgd 			lfmode = argtoi('m', "mode", optarg, 8);
    144   1.1       cgd 			if (lfmode &~ 07777)
    145  1.67  christos 				errx(FSCK_EXIT_USAGE, "bad mode to -m: %o",
    146  1.67  christos 				    lfmode);
    147   1.1       cgd 			printf("** lost+found creation mode %o\n", lfmode);
    148   1.1       cgd 			break;
    149   1.1       cgd 
    150   1.1       cgd 		case 'n':
    151   1.1       cgd 			nflag++;
    152   1.1       cgd 			yflag = 0;
    153   1.1       cgd 			break;
    154   1.1       cgd 
    155  1.23  christos 		case 'p':
    156  1.23  christos 			preen++;
    157  1.23  christos 			break;
    158  1.23  christos 
    159  1.53  christos 		case 'P':
    160  1.53  christos 			progress = 1;
    161  1.53  christos 			break;
    162  1.53  christos 
    163  1.47       dsl 		case 'q':
    164  1.47       dsl 			quiet++;
    165  1.47       dsl 			break;
    166  1.47       dsl 
    167   1.1       cgd 		case 'y':
    168   1.1       cgd 			yflag++;
    169   1.1       cgd 			nflag = 0;
    170   1.1       cgd 			break;
    171  1.69    bouyer 		case 'x':
    172  1.69    bouyer 			snap_backup = optarg;
    173  1.69    bouyer 			break;
    174  1.69    bouyer 		case 'X':
    175  1.69    bouyer 			snap_internal = 1;
    176  1.69    bouyer 			break;
    177   1.1       cgd 
    178   1.1       cgd 		default:
    179  1.23  christos 			usage();
    180   1.1       cgd 		}
    181   1.1       cgd 	}
    182  1.23  christos 
    183  1.69    bouyer 	if (snap_backup || snap_internal) {
    184  1.69    bouyer 		if (!nflag || yflag) {
    185  1.69    bouyer 			warnx("Cannot use -x or -X without -n\n");
    186  1.69    bouyer 			snap_backup = NULL;
    187  1.69    bouyer 			snap_internal = 0;
    188  1.69    bouyer 		}
    189  1.69    bouyer 	}
    190  1.69    bouyer 
    191  1.69    bouyer 
    192   1.1       cgd 	argc -= optind;
    193   1.1       cgd 	argv += optind;
    194  1.23  christos 
    195  1.23  christos 	if (!argc)
    196  1.23  christos 		usage();
    197  1.23  christos 
    198  1.53  christos 	if (debug)
    199  1.53  christos 		progress = 0;
    200  1.53  christos 
    201   1.1       cgd 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
    202   1.1       cgd 		(void)signal(SIGINT, catch);
    203   1.1       cgd 	if (preen)
    204   1.1       cgd 		(void)signal(SIGQUIT, catchquit);
    205  1.54  christos #ifdef PROGRESS
    206  1.53  christos 	if (progress) {
    207  1.53  christos 		progress_ttywidth(0);
    208  1.53  christos 		(void)signal(SIGWINCH, progress_ttywidth);
    209  1.53  christos 	}
    210  1.54  christos #endif /* ! PROGRESS */
    211  1.41     lukem 	signal(SIGINFO, infohandler);
    212  1.20  christos 
    213  1.45      fvdl 	while (argc-- > 0) {
    214  1.69    bouyer 		int nret;
    215  1.69    bouyer 		char *path = strdup(blockcheck(*argv));
    216  1.45      fvdl 
    217  1.45      fvdl 		if (path == NULL)
    218  1.45      fvdl 			pfatal("Can't check %s\n", *argv);
    219  1.69    bouyer 
    220  1.69    bouyer 		if (snap_backup || snap_internal) {
    221  1.69    bouyer 			char *mpt;
    222  1.69    bouyer 			char *snap_dev;
    223  1.69    bouyer 			int snapfd;
    224  1.69    bouyer 
    225  1.69    bouyer 			mpt = get_snap_device(*argv);
    226  1.69    bouyer 			if (mpt == NULL)
    227  1.69    bouyer 				goto next;
    228  1.69    bouyer 			snapfd = snap_open(mpt, snap_backup, NULL, &snap_dev);
    229  1.69    bouyer 			if (snapfd < 0) {
    230  1.69    bouyer 				warn("can't take snapshot of %s", mpt);
    231  1.69    bouyer 				free(mpt);
    232  1.69    bouyer 				goto next;
    233  1.69    bouyer 			}
    234  1.69    bouyer 			nret = checkfilesys(blockcheck(snap_dev), path, 0);
    235  1.69    bouyer 			if (ret < nret)
    236  1.69    bouyer 				ret = nret;
    237  1.69    bouyer 			free(mpt);
    238  1.69    bouyer 			close(snapfd);
    239  1.69    bouyer 		} else {
    240  1.69    bouyer 			nret = checkfilesys(path, path, 0);
    241  1.67  christos 			if (ret < nret)
    242  1.67  christos 				ret = nret;
    243  1.69    bouyer 		}
    244  1.69    bouyer next:
    245  1.69    bouyer 		free(path);
    246  1.45      fvdl 		argv++;
    247  1.45      fvdl 	}
    248  1.20  christos 
    249  1.67  christos 	return returntosingle ? FSCK_EXIT_UNRESOLVED : ret;
    250   1.1       cgd }
    251   1.1       cgd 
    252  1.23  christos static int
    253  1.58  christos argtoi(int flag, const char *req, const char *str, int base)
    254   1.1       cgd {
    255   1.1       cgd 	char *cp;
    256   1.1       cgd 	int ret;
    257   1.1       cgd 
    258   1.1       cgd 	ret = (int)strtol(str, &cp, base);
    259   1.1       cgd 	if (cp == str || *cp)
    260  1.67  christos 		errx(FSCK_EXIT_USAGE, "-%c flag requires a %s",
    261  1.67  christos 		    flag, req);
    262   1.1       cgd 	return (ret);
    263   1.1       cgd }
    264   1.1       cgd 
    265   1.1       cgd /*
    266   1.1       cgd  * Check the specified filesystem.
    267   1.1       cgd  */
    268   1.1       cgd /* ARGSUSED */
    269  1.23  christos static int
    270  1.69    bouyer checkfilesys(const char *filesys, const char *origfs, int child)
    271   1.1       cgd {
    272  1.43      fvdl 	daddr_t n_ffree, n_bfree;
    273   1.1       cgd 	struct dups *dp;
    274   1.1       cgd 	struct zlncnt *zlnp;
    275  1.45      fvdl 	int cylno;
    276  1.27     lukem #ifdef LITE2BORKEN
    277  1.36   thorpej 	int flags;
    278  1.27     lukem #endif
    279  1.54  christos #ifdef PROGRESS
    280  1.61       apb 	/*
    281  1.61       apb 	 * In prune mode, how far does the progress bar travel during
    282  1.61       apb 	 * each pass?  (In non-prune mode, each pass has a separate
    283  1.61       apb 	 * progress bar that travels from 0 to 100%.)
    284  1.61       apb 	 *
    285  1.61       apb 	 * The numbers below are percentages, intended to correspond
    286  1.61       apb 	 * roughly to the cumulative time up to the end of each pass.
    287  1.61       apb 	 * They don't have to be accurate.  In reality, on a large
    288  1.61       apb 	 * file system, Pass 1 and Pass 2 together are likely to use
    289  1.61       apb 	 * significantly more than the 95% reflected below, so users
    290  1.61       apb 	 * will get a pleasant surprise when the last 5% of the progress
    291  1.61       apb 	 * bar runs more quickly than they had expected.
    292  1.61       apb 	 */
    293  1.61       apb 	static int progress_limits[] = {0, 20, 95, 96, 97, 100};
    294  1.54  christos #endif /* PROGRESS */
    295   1.1       cgd 
    296   1.1       cgd 	if (preen && child)
    297   1.1       cgd 		(void)signal(SIGQUIT, voidquit);
    298  1.20  christos 	setcdevname(filesys, preen);
    299   1.1       cgd 	if (debug && preen)
    300   1.1       cgd 		pwarn("starting\n");
    301  1.69    bouyer 	switch (setup(filesys, origfs)) {
    302  1.16   mycroft 	case 0:
    303   1.1       cgd 		if (preen)
    304   1.1       cgd 			pfatal("CAN'T CHECK FILE SYSTEM.");
    305  1.25     lukem 		/* fall through */
    306  1.16   mycroft 	case -1:
    307  1.67  christos 		return FSCK_EXIT_OK;
    308   1.1       cgd 	}
    309  1.13   mycroft 	/*
    310  1.34      fvdl 	 * Cleared if any questions answered no. Used to decide if
    311  1.34      fvdl 	 * the superblock should be marked clean.
    312  1.34      fvdl 	 */
    313  1.34      fvdl 	resolved = 1;
    314  1.53  christos 
    315  1.54  christos #ifdef PROGRESS
    316  1.61       apb 	progress_switch(progress);
    317  1.61       apb 	progress_init();
    318  1.54  christos #endif /* PROGRESS */
    319  1.53  christos 
    320  1.34      fvdl 	/*
    321  1.13   mycroft 	 * 1: scan inodes tallying blocks used
    322  1.13   mycroft 	 */
    323  1.13   mycroft 	if (preen == 0) {
    324  1.47       dsl 		pwarn("** Last Mounted on %s\n", sblock->fs_fsmnt);
    325  1.20  christos 		if (hotroot())
    326  1.47       dsl 			pwarn("** Root file system\n");
    327  1.47       dsl 		pwarn("** Phase 1 - Check Blocks and Sizes\n");
    328  1.13   mycroft 	}
    329  1.54  christos #ifdef PROGRESS
    330  1.53  christos 	if (preen)
    331  1.62       apb 		progress_setrange(0, progress_limits[1]);
    332  1.54  christos #endif /* PROGRESS */
    333  1.61       apb 	pass1();
    334  1.53  christos 
    335   1.1       cgd 	/*
    336  1.13   mycroft 	 * 1b: locate first references to duplicates, if any
    337   1.1       cgd 	 */
    338  1.13   mycroft 	if (duplist) {
    339  1.35   mycroft 		if (preen)
    340  1.35   mycroft 			pfatal("INTERNAL ERROR: dups with -p\n");
    341  1.35   mycroft 		if (usedsoftdep)
    342  1.35   mycroft 			pfatal("INTERNAL ERROR: dups with softdep\n");
    343  1.47       dsl 		pwarn("** Phase 1b - Rescan For More DUPS\n");
    344  1.13   mycroft 		pass1b();
    345  1.13   mycroft 	}
    346   1.9       cgd 
    347  1.13   mycroft 	/*
    348  1.13   mycroft 	 * 2: traverse directories from root to mark all connected directories
    349  1.13   mycroft 	 */
    350  1.13   mycroft 	if (preen == 0)
    351  1.47       dsl 		pwarn("** Phase 2 - Check Pathnames\n");
    352  1.61       apb #ifdef PROGRESS
    353  1.61       apb 	if (preen)
    354  1.61       apb 		progress_sethighlim(progress_limits[2]);
    355  1.61       apb #endif /* PROGRESS */
    356  1.13   mycroft 	pass2();
    357   1.1       cgd 
    358  1.13   mycroft 	/*
    359  1.13   mycroft 	 * 3: scan inodes looking for disconnected directories
    360  1.13   mycroft 	 */
    361  1.13   mycroft 	if (preen == 0)
    362  1.47       dsl 		pwarn("** Phase 3 - Check Connectivity\n");
    363  1.61       apb #ifdef PROGRESS
    364  1.61       apb 	if (preen)
    365  1.61       apb 		progress_sethighlim(progress_limits[3]);
    366  1.61       apb #endif /* PROGRESS */
    367  1.13   mycroft 	pass3();
    368   1.1       cgd 
    369  1.13   mycroft 	/*
    370  1.13   mycroft 	 * 4: scan inodes looking for disconnected files; check reference counts
    371  1.13   mycroft 	 */
    372  1.13   mycroft 	if (preen == 0)
    373  1.47       dsl 		pwarn("** Phase 4 - Check Reference Counts\n");
    374  1.61       apb #ifdef PROGRESS
    375  1.61       apb 	if (preen)
    376  1.61       apb 		progress_sethighlim(progress_limits[4]);
    377  1.61       apb #endif /* PROGRESS */
    378  1.13   mycroft 	pass4();
    379   1.5   mycroft 
    380  1.13   mycroft 	/*
    381  1.13   mycroft 	 * 5: check and repair resource counts in cylinder groups
    382  1.13   mycroft 	 */
    383  1.13   mycroft 	if (preen == 0)
    384  1.47       dsl 		pwarn("** Phase 5 - Check Cyl groups\n");
    385  1.61       apb #ifdef PROGRESS
    386  1.61       apb 	if (preen)
    387  1.61       apb 		progress_sethighlim(progress_limits[5]);
    388  1.61       apb #endif /* PROGRESS */
    389  1.13   mycroft 	pass5();
    390   1.1       cgd 
    391   1.1       cgd 	/*
    392   1.1       cgd 	 * print out summary statistics
    393   1.1       cgd 	 */
    394  1.31    bouyer 	n_ffree = sblock->fs_cstotal.cs_nffree;
    395  1.31    bouyer 	n_bfree = sblock->fs_cstotal.cs_nbfree;
    396  1.59  christos 	pwarn("%llu files, %lld used, %lld free ",
    397  1.59  christos 	    (unsigned long long)n_files, (long long)n_blks,
    398  1.44       mrg 	    (long long)(n_ffree + sblock->fs_frag * n_bfree));
    399  1.43      fvdl 	printf("(%lld frags, %lld blocks, %lld.%lld%% fragmentation)\n",
    400  1.43      fvdl 	    (long long)n_ffree, (long long)n_bfree,
    401  1.44       mrg 	    (long long)(n_ffree * 100 / (daddr_t)sblock->fs_dsize),
    402  1.44       mrg 	    (long long)(((n_ffree * 1000 + (daddr_t)sblock->fs_dsize / 2)
    403  1.44       mrg 		/ (daddr_t)sblock->fs_dsize) % 10));
    404   1.1       cgd 	if (debug &&
    405  1.31    bouyer 	    (n_files -= maxino - ROOTINO - sblock->fs_cstotal.cs_nifree))
    406  1.59  christos 		printf("%llu files missing\n", (unsigned long long)n_files);
    407   1.1       cgd 	if (debug) {
    408  1.31    bouyer 		n_blks += sblock->fs_ncg *
    409  1.31    bouyer 			(cgdmin(sblock, 0) - cgsblock(sblock, 0));
    410  1.31    bouyer 		n_blks += cgsblock(sblock, 0) - cgbase(sblock, 0);
    411  1.31    bouyer 		n_blks += howmany(sblock->fs_cssize, sblock->fs_fsize);
    412  1.31    bouyer 		if (n_blks -= maxfsblock - (n_ffree + sblock->fs_frag * n_bfree))
    413  1.43      fvdl 			printf("%lld blocks missing\n", (long long)n_blks);
    414   1.1       cgd 		if (duplist != NULL) {
    415   1.1       cgd 			printf("The following duplicate blocks remain:");
    416   1.1       cgd 			for (dp = duplist; dp; dp = dp->next)
    417  1.43      fvdl 				printf(" %lld,", (long long)dp->dup);
    418   1.1       cgd 			printf("\n");
    419   1.1       cgd 		}
    420   1.1       cgd 		if (zlnhead != NULL) {
    421   1.1       cgd 			printf("The following zero link count inodes remain:");
    422   1.1       cgd 			for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
    423  1.59  christos 				printf(" %llu,",
    424  1.59  christos 				    (unsigned long long)zlnp->zlncnt);
    425   1.1       cgd 			printf("\n");
    426   1.1       cgd 		}
    427   1.1       cgd 	}
    428   1.1       cgd 	zlnhead = (struct zlncnt *)0;
    429   1.1       cgd 	duplist = (struct dups *)0;
    430  1.13   mycroft 	muldup = (struct dups *)0;
    431   1.1       cgd 	inocleanup();
    432   1.1       cgd 	if (fsmodified) {
    433  1.45      fvdl 		sblock->fs_time = time(NULL);
    434   1.1       cgd 		sbdirty();
    435  1.13   mycroft 	}
    436  1.34      fvdl 	if (rerun)
    437  1.34      fvdl 		markclean = 0;
    438  1.27     lukem #if LITE2BORKEN
    439  1.25     lukem 	if (!hotroot()) {
    440  1.31    bouyer 		ckfini();
    441  1.25     lukem 	} else {
    442  1.50  christos 		struct statvfs stfs_buf;
    443  1.25     lukem 		/*
    444  1.25     lukem 		 * Check to see if root is mounted read-write.
    445  1.25     lukem 		 */
    446  1.50  christos 		if (statvfs("/", &stfs_buf) == 0)
    447  1.50  christos 			flags = stfs_buf.f_flag;
    448  1.25     lukem 		else
    449  1.25     lukem 			flags = 0;
    450  1.31    bouyer 		if (markclean)
    451  1.31    bouyer 			markclean = flags & MNT_RDONLY;
    452  1.31    bouyer 		ckfini();
    453  1.25     lukem 	}
    454  1.27     lukem #else
    455  1.31    bouyer 	ckfini();
    456  1.27     lukem #endif
    457  1.45      fvdl 	for (cylno = 0; cylno < sblock->fs_ncg; cylno++)
    458  1.45      fvdl 		if (inostathead[cylno].il_stat != NULL)
    459  1.45      fvdl 			free(inostathead[cylno].il_stat);
    460  1.45      fvdl 	free(inostathead);
    461  1.45      fvdl 	inostathead = NULL;
    462  1.45      fvdl 
    463  1.52       dbj 	if (!resolved || rerun) {
    464  1.52       dbj 		pwarn("\n***** UNRESOLVED INCONSISTENCIES REMAIN *****\n");
    465  1.52       dbj 		returntosingle = 1;
    466  1.52       dbj 	}
    467   1.1       cgd 	if (!fsmodified)
    468  1.67  christos 		return FSCK_EXIT_OK;
    469  1.13   mycroft 	if (!preen)
    470  1.47       dsl 		pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n");
    471  1.22   thorpej 	if (rerun)
    472  1.47       dsl 		pwarn("\n***** PLEASE RERUN FSCK *****\n");
    473  1.20  christos 	if (hotroot()) {
    474  1.50  christos 		struct statvfs stfs_buf;
    475  1.13   mycroft 		/*
    476  1.13   mycroft 		 * We modified the root.  Do a mount update on
    477  1.13   mycroft 		 * it, unless it is read-write, so we can continue.
    478  1.13   mycroft 		 */
    479  1.50  christos 		if (statvfs("/", &stfs_buf) == 0) {
    480  1.50  christos 			long flags = stfs_buf.f_flag;
    481  1.13   mycroft 			struct ufs_args args;
    482  1.13   mycroft 
    483  1.13   mycroft 			if (flags & MNT_RDONLY) {
    484  1.13   mycroft 				args.fspec = 0;
    485  1.13   mycroft 				flags |= MNT_UPDATE | MNT_RELOAD;
    486  1.67  christos 				if (mount(MOUNT_FFS, "/", flags,
    487  1.67  christos 				    &args, sizeof args) == 0)
    488  1.67  christos 					return FSCK_EXIT_OK;
    489  1.13   mycroft 			}
    490  1.13   mycroft 		}
    491  1.13   mycroft 		if (!preen)
    492  1.47       dsl 			pwarn("\n***** REBOOT NOW *****\n");
    493   1.1       cgd 		sync();
    494  1.67  christos 		return FSCK_EXIT_ROOT_CHANGED;
    495   1.1       cgd 	}
    496  1.67  christos 	return FSCK_EXIT_OK;
    497   1.1       cgd }
    498  1.23  christos 
    499  1.23  christos static void
    500  1.56   xtraeme usage(void)
    501  1.23  christos {
    502  1.38       cgd 
    503  1.23  christos 	(void) fprintf(stderr,
    504  1.69    bouyer 	    "usage: %s [-adFfnPpqyX] [-B be|le] [-b block] [-c level] [-m mode]"
    505  1.69    bouyer 	    " [-x snap-backup] filesystem ...\n",
    506  1.38       cgd 	    getprogname());
    507  1.67  christos 	exit(FSCK_EXIT_USAGE);
    508  1.23  christos }
    509  1.23  christos 
    510  1.69    bouyer static
    511  1.69    bouyer char *get_snap_device(char *file)
    512  1.69    bouyer {
    513  1.69    bouyer 	char *mountpoint = NULL;
    514  1.69    bouyer 	struct statvfs *mntbuf, *fs, fsbuf;
    515  1.69    bouyer 	struct stat sb;
    516  1.69    bouyer 
    517  1.69    bouyer 	/* find the mount point */
    518  1.69    bouyer 	if (lstat(file, &sb) == -1) {
    519  1.69    bouyer 		warn("can't stat %s", file);
    520  1.69    bouyer 		return NULL;
    521  1.69    bouyer 	}
    522  1.69    bouyer 	if (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) {
    523  1.69    bouyer 		int mntbufc, i;
    524  1.69    bouyer 		if ((mntbufc = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
    525  1.69    bouyer 			pfatal("can't get mount list: %s\n", strerror(errno));
    526  1.69    bouyer 		for (fs = mntbuf, i = 0;
    527  1.69    bouyer 		     i < mntbufc; i++, fs++) {
    528  1.69    bouyer 			if (strcmp(fs->f_fstypename, "ufs") != 0 &&
    529  1.69    bouyer 			    strcmp(fs->f_fstypename, "ffs") != 0)
    530  1.69    bouyer 				continue;
    531  1.69    bouyer 			if (fs->f_flag & ST_RDONLY) {
    532  1.69    bouyer 				warnx("Cannot use -x or -X "
    533  1.69    bouyer 				     "on read-only filesystem");
    534  1.69    bouyer 				free(mntbuf);
    535  1.69    bouyer 				return NULL;
    536  1.69    bouyer 			}
    537  1.69    bouyer 			if (strcmp(fs->f_mntfromname, unrawname(file)) == 0) {
    538  1.69    bouyer 				mountpoint = strdup(fs->f_mntonname);
    539  1.69    bouyer 				free(mntbuf);
    540  1.69    bouyer 				return mountpoint;
    541  1.69    bouyer 			}
    542  1.69    bouyer 		}
    543  1.69    bouyer 		warnx("Cannot use -x or -X on unmounted device");
    544  1.69    bouyer 		free(mntbuf);
    545  1.69    bouyer 		return NULL;
    546  1.69    bouyer 	}
    547  1.69    bouyer 	if (S_ISDIR(sb.st_mode)) {
    548  1.69    bouyer 		if (statvfs(file, &fsbuf) == -1)
    549  1.69    bouyer 			pfatal("can't statvfs %s: %s\n", file, strerror(errno));
    550  1.69    bouyer 		if (strcmp(fsbuf.f_mntonname, file))
    551  1.69    bouyer 			pfatal("%s is not a mount point\n", file);
    552  1.69    bouyer 		if (fsbuf.f_flag & ST_RDONLY) {
    553  1.69    bouyer 			warnx("Cannot use -x or -X "
    554  1.69    bouyer 			     "on read-only filesystem");
    555  1.69    bouyer 			return NULL;
    556  1.69    bouyer 		}
    557  1.69    bouyer 		mountpoint = strdup(file);
    558  1.69    bouyer 		return mountpoint;
    559  1.69    bouyer 	}
    560  1.69    bouyer 	pfatal("%s is not a mount point\n", file);
    561  1.69    bouyer 	return NULL;
    562  1.69    bouyer }
    563