Home | History | Annotate | Line # | Download | only in fsck_ffs
main.c revision 1.89
      1  1.89    martin /*	$NetBSD: main.c,v 1.89 2020/04/06 09:54:24 martin 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.89    martin __RCSID("$NetBSD: main.c,v 1.89 2020/04/06 09:54:24 martin 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.87     joerg struct bufarea bufhead;
     74  1.87     joerg struct bufarea sblk;
     75  1.87     joerg struct bufarea asblk;
     76  1.87     joerg struct bufarea cgblk;
     77  1.87     joerg struct bufarea appleufsblk;
     78  1.87     joerg struct bufarea *pdirbp;
     79  1.87     joerg struct bufarea *pbp;
     80  1.87     joerg struct fs *sblock;
     81  1.87     joerg struct fs *altsblock;
     82  1.87     joerg struct cg *cgrp;
     83  1.87     joerg struct fs *sblocksave;
     84  1.87     joerg struct dups *duplist;
     85  1.87     joerg struct dups *muldup;
     86  1.87     joerg struct zlncnt *zlnhead;
     87  1.87     joerg struct inoinfo **inphead, **inpsort;
     88  1.87     joerg long numdirs, dirhash, listmax, inplast;
     89  1.87     joerg struct uquot_hash *uquot_user_hash;
     90  1.87     joerg struct uquot_hash *uquot_group_hash;
     91  1.87     joerg uint8_t q2h_hash_shift;
     92  1.87     joerg uint16_t q2h_hash_mask;
     93  1.87     joerg struct inostatlist *inostathead;
     94  1.87     joerg long	dev_bsize;
     95  1.87     joerg long	secsize;
     96  1.87     joerg char	nflag;
     97  1.87     joerg char	yflag;
     98  1.87     joerg int	Uflag;
     99  1.87     joerg int	bflag;
    100  1.87     joerg int	debug;
    101  1.87     joerg int	zflag;
    102  1.87     joerg int	cvtlevel;
    103  1.87     joerg int	doinglevel1;
    104  1.87     joerg int	doinglevel2;
    105  1.87     joerg int	newinofmt;
    106  1.87     joerg char	usedsoftdep;
    107  1.87     joerg int	preen;
    108  1.87     joerg int	forceimage;
    109  1.87     joerg int	is_ufs2;
    110  1.87     joerg int	markclean;
    111  1.87     joerg char	havesb;
    112  1.87     joerg char	skipclean;
    113  1.87     joerg int	fsmodified;
    114  1.87     joerg int	fsreadfd;
    115  1.87     joerg int	fswritefd;
    116  1.87     joerg int	rerun;
    117  1.87     joerg char	resolved;
    118  1.88       mrg #ifndef NO_FFS_EI
    119  1.87     joerg int	endian;
    120  1.87     joerg int	doswap;
    121  1.87     joerg int	needswap;
    122  1.87     joerg int	do_blkswap;
    123  1.87     joerg int	do_dirswap;
    124  1.89    martin #endif
    125  1.89    martin #ifndef NO_APPLE_UFS
    126  1.87     joerg int	isappleufs;
    127  1.89    martin #endif
    128  1.87     joerg daddr_t maxfsblock;
    129  1.87     joerg char	*blockmap;
    130  1.87     joerg ino_t	maxino;
    131  1.87     joerg int	dirblksiz;
    132  1.87     joerg daddr_t n_blks;
    133  1.87     joerg ino_t n_files;
    134  1.87     joerg long countdirs;
    135  1.87     joerg int	got_siginfo;
    136  1.87     joerg struct	ufs1_dinode ufs1_zino;
    137  1.87     joerg struct	ufs2_dinode ufs2_zino;
    138  1.87     joerg 
    139  1.53  christos int	progress = 0;
    140  1.23  christos 
    141  1.58  christos static int	argtoi(int, const char *, const char *, int);
    142  1.69    bouyer static int	checkfilesys(const char *, const char *, int);
    143  1.80     joerg __dead static void	usage(void);
    144  1.23  christos 
    145  1.14       cgd int
    146  1.56   xtraeme main(int argc, char *argv[])
    147   1.1       cgd {
    148  1.29    mjacob 	struct rlimit r;
    149   1.1       cgd 	int ch;
    150  1.67  christos 	int ret = FSCK_EXIT_OK;
    151  1.69    bouyer 	char *snap_backup = NULL;
    152  1.69    bouyer 	int snap_internal = 0;
    153   1.1       cgd 
    154  1.79  christos 	ckfinish = ckfini;
    155  1.79  christos 
    156  1.29    mjacob 	if (getrlimit(RLIMIT_DATA, &r) == 0) {
    157  1.29    mjacob 		r.rlim_cur = r.rlim_max;
    158  1.29    mjacob 		(void) setrlimit(RLIMIT_DATA, &r);
    159  1.29    mjacob 	}
    160   1.1       cgd 	sync();
    161  1.16   mycroft 	skipclean = 1;
    162  1.31    bouyer 	markclean = 1;
    163  1.40     lukem 	forceimage = 0;
    164  1.84       rin #ifndef NO_FFS_EI
    165  1.31    bouyer 	endian = 0;
    166  1.84       rin #endif
    167  1.84       rin #ifndef NO_APPLE_UFS
    168  1.42       dbj 	isappleufs = 0;
    169  1.84       rin #endif
    170  1.85  christos 	while ((ch = getopt(argc, argv, "aB:b:c:dFfm:npPqUyx:Xz")) != -1) {
    171   1.1       cgd 		switch (ch) {
    172  1.84       rin #ifndef NO_APPLE_UFS
    173  1.42       dbj 		case 'a':
    174  1.42       dbj 			isappleufs = 1;
    175  1.42       dbj 			break;
    176  1.84       rin #endif
    177  1.42       dbj 
    178  1.84       rin #ifndef NO_FFS_EI
    179  1.31    bouyer 		case 'B':
    180  1.31    bouyer 			if (strcmp(optarg, "be") == 0)
    181  1.31    bouyer 				endian = BIG_ENDIAN;
    182  1.31    bouyer 			else if (strcmp(optarg, "le") == 0)
    183  1.31    bouyer 				endian = LITTLE_ENDIAN;
    184  1.84       rin 			else
    185  1.84       rin 				usage();
    186  1.31    bouyer 			break;
    187  1.84       rin #endif
    188  1.31    bouyer 
    189   1.1       cgd 		case 'b':
    190  1.16   mycroft 			skipclean = 0;
    191   1.1       cgd 			bflag = argtoi('b', "number", optarg, 10);
    192   1.1       cgd 			printf("Alternate super block location: %d\n", bflag);
    193   1.1       cgd 			break;
    194   1.1       cgd 
    195   1.1       cgd 		case 'c':
    196  1.16   mycroft 			skipclean = 0;
    197  1.13   mycroft 			cvtlevel = argtoi('c', "conversion level", optarg, 10);
    198  1.49       dbj 			if (cvtlevel > 4) {
    199  1.49       dbj 				cvtlevel = 4;
    200  1.83  christos 				warnx("Using maximum conversion level of %d",
    201  1.67  christos 				    cvtlevel);
    202  1.49       dbj 			}
    203   1.1       cgd 			break;
    204  1.13   mycroft 
    205   1.1       cgd 		case 'd':
    206   1.1       cgd 			debug++;
    207  1.17       cgd 			break;
    208  1.17       cgd 
    209  1.40     lukem 		case 'F':
    210  1.40     lukem 			forceimage = 1;
    211  1.40     lukem 			break;
    212  1.40     lukem 
    213  1.17       cgd 		case 'f':
    214  1.17       cgd 			skipclean = 0;
    215   1.1       cgd 			break;
    216   1.1       cgd 
    217   1.1       cgd 		case 'm':
    218   1.1       cgd 			lfmode = argtoi('m', "mode", optarg, 8);
    219   1.1       cgd 			if (lfmode &~ 07777)
    220  1.67  christos 				errx(FSCK_EXIT_USAGE, "bad mode to -m: %o",
    221  1.67  christos 				    lfmode);
    222   1.1       cgd 			printf("** lost+found creation mode %o\n", lfmode);
    223   1.1       cgd 			break;
    224   1.1       cgd 
    225   1.1       cgd 		case 'n':
    226   1.1       cgd 			nflag++;
    227   1.1       cgd 			yflag = 0;
    228   1.1       cgd 			break;
    229   1.1       cgd 
    230  1.23  christos 		case 'p':
    231  1.23  christos 			preen++;
    232  1.23  christos 			break;
    233  1.23  christos 
    234  1.53  christos 		case 'P':
    235  1.53  christos 			progress = 1;
    236  1.53  christos 			break;
    237  1.53  christos 
    238  1.47       dsl 		case 'q':
    239  1.47       dsl 			quiet++;
    240  1.47       dsl 			break;
    241  1.71  christos #ifndef SMALL
    242  1.71  christos 		case 'U':
    243  1.71  christos 			Uflag++;
    244  1.71  christos 			break;
    245  1.71  christos #endif
    246  1.47       dsl 
    247  1.69    bouyer 		case 'x':
    248  1.69    bouyer 			snap_backup = optarg;
    249  1.69    bouyer 			break;
    250  1.85  christos 
    251  1.69    bouyer 		case 'X':
    252  1.69    bouyer 			snap_internal = 1;
    253  1.69    bouyer 			break;
    254   1.1       cgd 
    255  1.85  christos 		case 'y':
    256  1.85  christos 			yflag++;
    257  1.85  christos 			nflag = 0;
    258  1.85  christos 			break;
    259  1.85  christos 
    260  1.85  christos 		case 'z':
    261  1.85  christos 			zflag++;
    262  1.85  christos 			break;
    263  1.85  christos 
    264   1.1       cgd 		default:
    265  1.23  christos 			usage();
    266   1.1       cgd 		}
    267   1.1       cgd 	}
    268  1.23  christos 
    269  1.69    bouyer 	if (snap_backup || snap_internal) {
    270  1.69    bouyer 		if (!nflag || yflag) {
    271  1.82  christos 			warnx("Cannot use -x or -X without -n");
    272  1.69    bouyer 			snap_backup = NULL;
    273  1.69    bouyer 			snap_internal = 0;
    274  1.69    bouyer 		}
    275  1.69    bouyer 	}
    276  1.69    bouyer 
    277  1.69    bouyer 
    278   1.1       cgd 	argc -= optind;
    279   1.1       cgd 	argv += optind;
    280  1.23  christos 
    281  1.23  christos 	if (!argc)
    282  1.23  christos 		usage();
    283  1.23  christos 
    284  1.53  christos 	if (debug)
    285  1.53  christos 		progress = 0;
    286  1.53  christos 
    287   1.1       cgd 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
    288   1.1       cgd 		(void)signal(SIGINT, catch);
    289   1.1       cgd 	if (preen)
    290   1.1       cgd 		(void)signal(SIGQUIT, catchquit);
    291  1.54  christos #ifdef PROGRESS
    292  1.53  christos 	if (progress) {
    293  1.53  christos 		progress_ttywidth(0);
    294  1.53  christos 		(void)signal(SIGWINCH, progress_ttywidth);
    295  1.53  christos 	}
    296  1.54  christos #endif /* ! PROGRESS */
    297  1.41     lukem 	signal(SIGINFO, infohandler);
    298  1.20  christos 
    299  1.45      fvdl 	while (argc-- > 0) {
    300  1.69    bouyer 		int nret;
    301  1.77     njoly 		char *path;
    302  1.77     njoly 
    303  1.77     njoly 		if (!forceimage)
    304  1.77     njoly 			path = strdup(blockcheck(*argv));
    305  1.77     njoly 		else
    306  1.77     njoly 			path = strdup(*argv);
    307  1.45      fvdl 
    308  1.45      fvdl 		if (path == NULL)
    309  1.45      fvdl 			pfatal("Can't check %s\n", *argv);
    310  1.69    bouyer 
    311  1.69    bouyer 		if (snap_backup || snap_internal) {
    312  1.69    bouyer 			char *snap_dev;
    313  1.69    bouyer 			int snapfd;
    314  1.69    bouyer 
    315  1.76   hannken 			snapfd = snap_open(*argv, snap_backup, NULL, &snap_dev);
    316  1.69    bouyer 			if (snapfd < 0) {
    317  1.76   hannken 				warn("can't take snapshot of %s", *argv);
    318  1.69    bouyer 				goto next;
    319  1.69    bouyer 			}
    320  1.69    bouyer 			nret = checkfilesys(blockcheck(snap_dev), path, 0);
    321  1.69    bouyer 			if (ret < nret)
    322  1.69    bouyer 				ret = nret;
    323  1.69    bouyer 			close(snapfd);
    324  1.69    bouyer 		} else {
    325  1.69    bouyer 			nret = checkfilesys(path, path, 0);
    326  1.67  christos 			if (ret < nret)
    327  1.67  christos 				ret = nret;
    328  1.69    bouyer 		}
    329  1.69    bouyer next:
    330  1.69    bouyer 		free(path);
    331  1.45      fvdl 		argv++;
    332  1.45      fvdl 	}
    333  1.20  christos 
    334  1.67  christos 	return returntosingle ? FSCK_EXIT_UNRESOLVED : ret;
    335   1.1       cgd }
    336   1.1       cgd 
    337  1.23  christos static int
    338  1.58  christos argtoi(int flag, const char *req, const char *str, int base)
    339   1.1       cgd {
    340   1.1       cgd 	char *cp;
    341   1.1       cgd 	int ret;
    342   1.1       cgd 
    343   1.1       cgd 	ret = (int)strtol(str, &cp, base);
    344   1.1       cgd 	if (cp == str || *cp)
    345  1.67  christos 		errx(FSCK_EXIT_USAGE, "-%c flag requires a %s",
    346  1.67  christos 		    flag, req);
    347   1.1       cgd 	return (ret);
    348   1.1       cgd }
    349   1.1       cgd 
    350   1.1       cgd /*
    351   1.1       cgd  * Check the specified filesystem.
    352   1.1       cgd  */
    353   1.1       cgd /* ARGSUSED */
    354  1.23  christos static int
    355  1.69    bouyer checkfilesys(const char *filesys, const char *origfs, int child)
    356   1.1       cgd {
    357  1.43      fvdl 	daddr_t n_ffree, n_bfree;
    358   1.1       cgd 	struct dups *dp;
    359   1.1       cgd 	struct zlncnt *zlnp;
    360  1.45      fvdl 	int cylno;
    361  1.27     lukem #ifdef LITE2BORKEN
    362  1.36   thorpej 	int flags;
    363  1.27     lukem #endif
    364  1.54  christos #ifdef PROGRESS
    365  1.61       apb 	/*
    366  1.61       apb 	 * In prune mode, how far does the progress bar travel during
    367  1.61       apb 	 * each pass?  (In non-prune mode, each pass has a separate
    368  1.61       apb 	 * progress bar that travels from 0 to 100%.)
    369  1.61       apb 	 *
    370  1.61       apb 	 * The numbers below are percentages, intended to correspond
    371  1.61       apb 	 * roughly to the cumulative time up to the end of each pass.
    372  1.61       apb 	 * They don't have to be accurate.  In reality, on a large
    373  1.61       apb 	 * file system, Pass 1 and Pass 2 together are likely to use
    374  1.61       apb 	 * significantly more than the 95% reflected below, so users
    375  1.61       apb 	 * will get a pleasant surprise when the last 5% of the progress
    376  1.61       apb 	 * bar runs more quickly than they had expected.
    377  1.61       apb 	 */
    378  1.61       apb 	static int progress_limits[] = {0, 20, 95, 96, 97, 100};
    379  1.54  christos #endif /* PROGRESS */
    380   1.1       cgd 
    381   1.1       cgd 	if (preen && child)
    382   1.1       cgd 		(void)signal(SIGQUIT, voidquit);
    383  1.20  christos 	setcdevname(filesys, preen);
    384   1.1       cgd 	if (debug && preen)
    385   1.1       cgd 		pwarn("starting\n");
    386  1.69    bouyer 	switch (setup(filesys, origfs)) {
    387  1.16   mycroft 	case 0:
    388   1.1       cgd 		if (preen)
    389   1.1       cgd 			pfatal("CAN'T CHECK FILE SYSTEM.");
    390  1.25     lukem 		/* fall through */
    391  1.16   mycroft 	case -1:
    392  1.67  christos 		return FSCK_EXIT_OK;
    393   1.1       cgd 	}
    394  1.13   mycroft 	/*
    395  1.34      fvdl 	 * Cleared if any questions answered no. Used to decide if
    396  1.34      fvdl 	 * the superblock should be marked clean.
    397  1.34      fvdl 	 */
    398  1.34      fvdl 	resolved = 1;
    399  1.53  christos 
    400  1.54  christos #ifdef PROGRESS
    401  1.61       apb 	progress_switch(progress);
    402  1.61       apb 	progress_init();
    403  1.54  christos #endif /* PROGRESS */
    404  1.53  christos 
    405  1.34      fvdl 	/*
    406  1.13   mycroft 	 * 1: scan inodes tallying blocks used
    407  1.13   mycroft 	 */
    408  1.13   mycroft 	if (preen == 0) {
    409  1.47       dsl 		pwarn("** Last Mounted on %s\n", sblock->fs_fsmnt);
    410  1.20  christos 		if (hotroot())
    411  1.47       dsl 			pwarn("** Root file system\n");
    412  1.47       dsl 		pwarn("** Phase 1 - Check Blocks and Sizes\n");
    413  1.13   mycroft 	}
    414  1.54  christos #ifdef PROGRESS
    415  1.53  christos 	if (preen)
    416  1.62       apb 		progress_setrange(0, progress_limits[1]);
    417  1.54  christos #endif /* PROGRESS */
    418  1.61       apb 	pass1();
    419  1.53  christos 
    420   1.1       cgd 	/*
    421  1.13   mycroft 	 * 1b: locate first references to duplicates, if any
    422   1.1       cgd 	 */
    423  1.13   mycroft 	if (duplist) {
    424  1.35   mycroft 		if (preen)
    425  1.35   mycroft 			pfatal("INTERNAL ERROR: dups with -p\n");
    426  1.35   mycroft 		if (usedsoftdep)
    427  1.35   mycroft 			pfatal("INTERNAL ERROR: dups with softdep\n");
    428  1.47       dsl 		pwarn("** Phase 1b - Rescan For More DUPS\n");
    429  1.13   mycroft 		pass1b();
    430  1.13   mycroft 	}
    431   1.9       cgd 
    432  1.13   mycroft 	/*
    433  1.13   mycroft 	 * 2: traverse directories from root to mark all connected directories
    434  1.13   mycroft 	 */
    435  1.13   mycroft 	if (preen == 0)
    436  1.47       dsl 		pwarn("** Phase 2 - Check Pathnames\n");
    437  1.61       apb #ifdef PROGRESS
    438  1.61       apb 	if (preen)
    439  1.61       apb 		progress_sethighlim(progress_limits[2]);
    440  1.61       apb #endif /* PROGRESS */
    441  1.13   mycroft 	pass2();
    442   1.1       cgd 
    443  1.13   mycroft 	/*
    444  1.13   mycroft 	 * 3: scan inodes looking for disconnected directories
    445  1.13   mycroft 	 */
    446  1.13   mycroft 	if (preen == 0)
    447  1.47       dsl 		pwarn("** Phase 3 - Check Connectivity\n");
    448  1.61       apb #ifdef PROGRESS
    449  1.61       apb 	if (preen)
    450  1.61       apb 		progress_sethighlim(progress_limits[3]);
    451  1.61       apb #endif /* PROGRESS */
    452  1.13   mycroft 	pass3();
    453   1.1       cgd 
    454  1.13   mycroft 	/*
    455  1.13   mycroft 	 * 4: scan inodes looking for disconnected files; check reference counts
    456  1.13   mycroft 	 */
    457  1.13   mycroft 	if (preen == 0)
    458  1.47       dsl 		pwarn("** Phase 4 - Check Reference Counts\n");
    459  1.61       apb #ifdef PROGRESS
    460  1.61       apb 	if (preen)
    461  1.61       apb 		progress_sethighlim(progress_limits[4]);
    462  1.61       apb #endif /* PROGRESS */
    463  1.13   mycroft 	pass4();
    464   1.5   mycroft 
    465  1.13   mycroft 	/*
    466  1.13   mycroft 	 * 5: check and repair resource counts in cylinder groups
    467  1.13   mycroft 	 */
    468  1.13   mycroft 	if (preen == 0)
    469  1.47       dsl 		pwarn("** Phase 5 - Check Cyl groups\n");
    470  1.61       apb #ifdef PROGRESS
    471  1.61       apb 	if (preen)
    472  1.61       apb 		progress_sethighlim(progress_limits[5]);
    473  1.61       apb #endif /* PROGRESS */
    474  1.13   mycroft 	pass5();
    475  1.78    bouyer 	if (uquot_user_hash != NULL) {
    476  1.78    bouyer 		if (preen == 0)
    477  1.78    bouyer 			pwarn("** Phase 6 - Check Quotas\n");
    478  1.78    bouyer 		pass6();
    479  1.78    bouyer 	}
    480   1.1       cgd 
    481   1.1       cgd 	/*
    482   1.1       cgd 	 * print out summary statistics
    483   1.1       cgd 	 */
    484  1.31    bouyer 	n_ffree = sblock->fs_cstotal.cs_nffree;
    485  1.31    bouyer 	n_bfree = sblock->fs_cstotal.cs_nbfree;
    486  1.59  christos 	pwarn("%llu files, %lld used, %lld free ",
    487  1.59  christos 	    (unsigned long long)n_files, (long long)n_blks,
    488  1.44       mrg 	    (long long)(n_ffree + sblock->fs_frag * n_bfree));
    489  1.43      fvdl 	printf("(%lld frags, %lld blocks, %lld.%lld%% fragmentation)\n",
    490  1.43      fvdl 	    (long long)n_ffree, (long long)n_bfree,
    491  1.44       mrg 	    (long long)(n_ffree * 100 / (daddr_t)sblock->fs_dsize),
    492  1.44       mrg 	    (long long)(((n_ffree * 1000 + (daddr_t)sblock->fs_dsize / 2)
    493  1.44       mrg 		/ (daddr_t)sblock->fs_dsize) % 10));
    494   1.1       cgd 	if (debug &&
    495  1.81  dholland 	    (n_files -= maxino - UFS_ROOTINO - sblock->fs_cstotal.cs_nifree))
    496  1.59  christos 		printf("%llu files missing\n", (unsigned long long)n_files);
    497   1.1       cgd 	if (debug) {
    498  1.31    bouyer 		n_blks += sblock->fs_ncg *
    499  1.31    bouyer 			(cgdmin(sblock, 0) - cgsblock(sblock, 0));
    500  1.31    bouyer 		n_blks += cgsblock(sblock, 0) - cgbase(sblock, 0);
    501  1.31    bouyer 		n_blks += howmany(sblock->fs_cssize, sblock->fs_fsize);
    502  1.31    bouyer 		if (n_blks -= maxfsblock - (n_ffree + sblock->fs_frag * n_bfree))
    503  1.43      fvdl 			printf("%lld blocks missing\n", (long long)n_blks);
    504   1.1       cgd 		if (duplist != NULL) {
    505   1.1       cgd 			printf("The following duplicate blocks remain:");
    506   1.1       cgd 			for (dp = duplist; dp; dp = dp->next)
    507  1.43      fvdl 				printf(" %lld,", (long long)dp->dup);
    508   1.1       cgd 			printf("\n");
    509   1.1       cgd 		}
    510   1.1       cgd 		if (zlnhead != NULL) {
    511   1.1       cgd 			printf("The following zero link count inodes remain:");
    512   1.1       cgd 			for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
    513  1.59  christos 				printf(" %llu,",
    514  1.59  christos 				    (unsigned long long)zlnp->zlncnt);
    515   1.1       cgd 			printf("\n");
    516   1.1       cgd 		}
    517   1.1       cgd 	}
    518   1.1       cgd 	zlnhead = (struct zlncnt *)0;
    519   1.1       cgd 	duplist = (struct dups *)0;
    520  1.13   mycroft 	muldup = (struct dups *)0;
    521   1.1       cgd 	inocleanup();
    522   1.1       cgd 	if (fsmodified) {
    523  1.45      fvdl 		sblock->fs_time = time(NULL);
    524   1.1       cgd 		sbdirty();
    525  1.13   mycroft 	}
    526  1.34      fvdl 	if (rerun)
    527  1.34      fvdl 		markclean = 0;
    528  1.27     lukem #if LITE2BORKEN
    529  1.25     lukem 	if (!hotroot()) {
    530  1.79  christos 		ckfini(1);
    531  1.25     lukem 	} else {
    532  1.50  christos 		struct statvfs stfs_buf;
    533  1.25     lukem 		/*
    534  1.25     lukem 		 * Check to see if root is mounted read-write.
    535  1.25     lukem 		 */
    536  1.50  christos 		if (statvfs("/", &stfs_buf) == 0)
    537  1.50  christos 			flags = stfs_buf.f_flag;
    538  1.25     lukem 		else
    539  1.25     lukem 			flags = 0;
    540  1.31    bouyer 		if (markclean)
    541  1.31    bouyer 			markclean = flags & MNT_RDONLY;
    542  1.79  christos 		ckfini(1);
    543  1.25     lukem 	}
    544  1.27     lukem #else
    545  1.79  christos 	ckfini(1);
    546  1.27     lukem #endif
    547  1.45      fvdl 	for (cylno = 0; cylno < sblock->fs_ncg; cylno++)
    548  1.45      fvdl 		if (inostathead[cylno].il_stat != NULL)
    549  1.45      fvdl 			free(inostathead[cylno].il_stat);
    550  1.45      fvdl 	free(inostathead);
    551  1.45      fvdl 	inostathead = NULL;
    552  1.45      fvdl 
    553  1.52       dbj 	if (!resolved || rerun) {
    554  1.52       dbj 		pwarn("\n***** UNRESOLVED INCONSISTENCIES REMAIN *****\n");
    555  1.52       dbj 		returntosingle = 1;
    556  1.52       dbj 	}
    557   1.1       cgd 	if (!fsmodified)
    558  1.67  christos 		return FSCK_EXIT_OK;
    559  1.13   mycroft 	if (!preen)
    560  1.47       dsl 		pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n");
    561  1.22   thorpej 	if (rerun)
    562  1.47       dsl 		pwarn("\n***** PLEASE RERUN FSCK *****\n");
    563  1.20  christos 	if (hotroot()) {
    564  1.50  christos 		struct statvfs stfs_buf;
    565  1.13   mycroft 		/*
    566  1.13   mycroft 		 * We modified the root.  Do a mount update on
    567  1.13   mycroft 		 * it, unless it is read-write, so we can continue.
    568  1.13   mycroft 		 */
    569  1.50  christos 		if (statvfs("/", &stfs_buf) == 0) {
    570  1.50  christos 			long flags = stfs_buf.f_flag;
    571  1.13   mycroft 			struct ufs_args args;
    572  1.13   mycroft 
    573  1.13   mycroft 			if (flags & MNT_RDONLY) {
    574  1.13   mycroft 				args.fspec = 0;
    575  1.13   mycroft 				flags |= MNT_UPDATE | MNT_RELOAD;
    576  1.67  christos 				if (mount(MOUNT_FFS, "/", flags,
    577  1.67  christos 				    &args, sizeof args) == 0)
    578  1.67  christos 					return FSCK_EXIT_OK;
    579  1.13   mycroft 			}
    580  1.13   mycroft 		}
    581  1.13   mycroft 		if (!preen)
    582  1.47       dsl 			pwarn("\n***** REBOOT NOW *****\n");
    583   1.1       cgd 		sync();
    584  1.67  christos 		return FSCK_EXIT_ROOT_CHANGED;
    585   1.1       cgd 	}
    586  1.67  christos 	return FSCK_EXIT_OK;
    587   1.1       cgd }
    588  1.23  christos 
    589  1.23  christos static void
    590  1.56   xtraeme usage(void)
    591  1.23  christos {
    592  1.38       cgd 
    593  1.23  christos 	(void) fprintf(stderr,
    594  1.84       rin 	    "usage: %s [-"
    595  1.84       rin #ifndef NO_APPLE_UFS
    596  1.84       rin 	    "a"
    597  1.84       rin #endif
    598  1.84       rin 	    "dFfPpqUX] "
    599  1.84       rin #ifndef NO_FFS_EI
    600  1.84       rin 	    "[-B byteorder] "
    601  1.84       rin #endif
    602  1.84       rin 	    "[-b block] [-c level] [-m mode]\n"
    603  1.73       wiz 	    "\t[-x snap-backup] [-y | -n] filesystem ...\n",
    604  1.38       cgd 	    getprogname());
    605  1.67  christos 	exit(FSCK_EXIT_USAGE);
    606  1.23  christos }
    607