Home | History | Annotate | Line # | Download | only in fsck_ffs
      1 /*	$NetBSD: setup.c,v 1.112 2025/12/18 08:21:44 nia 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 #if 0
     35 static char sccsid[] = "@(#)setup.c	8.10 (Berkeley) 5/9/95";
     36 #else
     37 __RCSID("$NetBSD: setup.c,v 1.112 2025/12/18 08:21:44 nia Exp $");
     38 #endif
     39 #endif /* not lint */
     40 
     41 #include <sys/param.h>
     42 #include <sys/time.h>
     43 #include <sys/stat.h>
     44 #include <sys/ioctl.h>
     45 #include <sys/file.h>
     46 #include <sys/disk.h>
     47 
     48 #include <ufs/ufs/dinode.h>
     49 #include <ufs/ufs/dir.h>
     50 #include <ufs/ufs/ufs_bswap.h>
     51 #include <ufs/ufs/quota2.h>
     52 #include <ufs/ffs/fs.h>
     53 #include <ufs/ffs/ffs_extern.h>
     54 
     55 #include <ctype.h>
     56 #include <err.h>
     57 #include <errno.h>
     58 #include <endian.h>
     59 #include <stdio.h>
     60 #include <stdlib.h>
     61 #include <string.h>
     62 
     63 #include "fsck.h"
     64 #include "extern.h"
     65 #include "fsutil.h"
     66 #include "partutil.h"
     67 #include "exitvalues.h"
     68 
     69 #define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
     70 
     71 static void badsb(int, const char *);
     72 static int calcsb(const char *, int, struct fs *);
     73 static int readsb(int);
     74 #ifndef NO_APPLE_UFS
     75 static int readappleufs(void);
     76 #endif
     77 static int check_snapinum(void);
     78 
     79 int16_t sblkpostbl[256];
     80 
     81 /*
     82  * Read in a superblock finding an alternate if necessary.
     83  * Return 1 if successful, 0 if unsuccessful, -1 if filesystem
     84  * is already clean (preen mode only).
     85  */
     86 int
     87 setup(const char *dev, const char *origdev)
     88 {
     89 	uint32_t cg;
     90 	long size, asked, i, j;
     91 	size_t bmapsize;
     92 	struct disk_geom geo;
     93 	struct dkwedge_info dkw;
     94 	off_t sizepb;
     95 	struct stat statb;
     96 	struct fs proto;
     97 	int doskipclean;
     98 	u_int64_t maxfilesize;
     99 	struct csum *ccsp;
    100 	int fd;
    101 
    102 	havesb = 0;
    103 	fswritefd = -1;
    104 	doskipclean = skipclean;
    105 	if (stat(dev, &statb) < 0) {
    106 		printf("Can't stat %s: %s\n", dev, strerror(errno));
    107 		return (0);
    108 	}
    109 	if (!forceimage && !S_ISCHR(statb.st_mode)) {
    110 		pfatal("%s is not a character device", dev);
    111 		if (reply("CONTINUE") == 0)
    112 			return (0);
    113 	}
    114 	if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
    115 		printf("Can't open %s: %s\n", dev, strerror(errno));
    116 		return (0);
    117 	}
    118 	if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) {
    119 		fswritefd = -1;
    120 		if (preen)
    121 			pfatal("NO WRITE ACCESS");
    122 		printf("** %s (NO WRITE)\n", dev);
    123 		quiet = 0;
    124 	} else
    125 		if (!preen && !quiet)
    126 			printf("** %s\n", dev);
    127 	fsmodified = 0;
    128 	lfdir = 0;
    129 	initbarea(&sblk);
    130 	initbarea(&asblk);
    131 	sblk.b_un.b_buf = aligned_alloc(DEV_BSIZE, SBLOCKSIZE);
    132 	sblock = aligned_alloc(DEV_BSIZE, SBLOCKSIZE);
    133 	asblk.b_un.b_buf = aligned_alloc(DEV_BSIZE, SBLOCKSIZE);
    134 	altsblock = aligned_alloc(DEV_BSIZE, SBLOCKSIZE);
    135 	if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL ||
    136 		sblock == NULL || altsblock == NULL)
    137 		errexit("Cannot allocate space for superblock");
    138 	if (strcmp(dev, origdev) && !forceimage) {
    139 		/*
    140 		 * dev isn't the original fs (for example it's a snapshot)
    141 		 * do getdiskinfo on the original device
    142 		 */
    143 		 fd = open(origdev, O_RDONLY);
    144 		 if (fd < 0) {
    145 			warn("Can't open %s", origdev);
    146 			return (0);
    147 		}
    148 	} else {
    149 		fd = fsreadfd;
    150 	}
    151 	if (!forceimage && getdiskinfo(origdev, fd, NULL, &geo, &dkw) != -1)
    152 		dev_bsize = secsize = geo.dg_secsize;
    153 	else
    154 		dev_bsize = secsize = DEV_BSIZE;
    155 	/*
    156 	 * Read in the superblock, looking for alternates if necessary
    157 	 */
    158 	if (readsb(1) == 0) {
    159 		if (bflag || preen || forceimage ||
    160 		    calcsb(dev, fsreadfd, &proto) == 0)
    161 			return(0);
    162 		if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
    163 			return (0);
    164 		for (cg = 0; cg < proto.fs_ncg; cg++) {
    165 			bflag = FFS_FSBTODB(&proto, cgsblock(&proto, cg));
    166 			if (readsb(0) != 0)
    167 				break;
    168 		}
    169 		if (cg >= proto.fs_ncg) {
    170 			printf("%s %s\n%s %s\n%s %s\n",
    171 				"SEARCH FOR ALTERNATE SUPER-BLOCK",
    172 				"FAILED. YOU MUST USE THE",
    173 				"-b OPTION TO fsck_ffs TO SPECIFY THE",
    174 				"LOCATION OF AN ALTERNATE",
    175 				"SUPER-BLOCK TO SUPPLY NEEDED",
    176 				"INFORMATION; SEE fsck_ffs(8).");
    177 			return(0);
    178 		}
    179 		doskipclean = 0;
    180 		pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
    181 	}
    182 
    183 	if (!quota2_check_doquota())
    184 		doskipclean = 0;
    185 
    186 	/* ffs_superblock_layout() == 2 */
    187 	if (sblock->fs_magic != FS_UFS1_MAGIC ||
    188 	    (sblock->fs_old_flags & FS_FLAGS_UPDATED) != 0) {
    189 		/* can have WAPBL */
    190 		if (check_wapbl() != 0) {
    191 			doskipclean = 0;
    192 		}
    193 		if (sblock->fs_flags & FS_DOWAPBL) {
    194 			if (preen && doskipclean) {
    195 				if (!quiet)
    196 					pwarn("file system is journaled; "
    197 					    "not checking\n");
    198 				return (-1);
    199 			}
    200 			if (!quiet)
    201 				pwarn("** File system is journaled; "
    202 				    "replaying journal\n");
    203 			replay_wapbl();
    204 			doskipclean = 0;
    205 			sblock->fs_flags &= ~FS_DOWAPBL;
    206 			sbdirty();
    207 			/* Although we may have updated the superblock from
    208 			 * the journal, we are still going to do a full check,
    209 			 * so we don't bother to re-read the superblock from
    210 			 * the journal.
    211 			 * XXX, instead we could re-read the superblock and
    212 			 * then not force doskipclean = 0
    213 			 */
    214 		}
    215 	}
    216 	if (debug)
    217 		printf("clean = %d\n", sblock->fs_clean);
    218 
    219 	if (doswap)
    220 		doskipclean = 0;
    221 
    222 	if (sblock->fs_clean & FS_ISCLEAN) {
    223 		if (doskipclean) {
    224 			if (!quiet)
    225 				pwarn("%sile system is clean; not checking\n",
    226 				    preen ? "f" : "** F");
    227 			return (-1);
    228 		}
    229 		if (!preen && !doswap)
    230 			pwarn("** File system is already clean\n");
    231 	}
    232 	maxfsblock = sblock->fs_size;
    233 	maxino = sblock->fs_ncg * sblock->fs_ipg;
    234 	sizepb = sblock->fs_bsize;
    235 	maxfilesize = sblock->fs_bsize * UFS_NDADDR - 1;
    236 	for (i = 0; i < UFS_NIADDR; i++) {
    237 		sizepb *= FFS_NINDIR(sblock);
    238 		maxfilesize += sizepb;
    239 	}
    240 	if ((!is_ufs2 && cvtlevel >= 4) &&
    241 			(sblock->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
    242 		if (preen)
    243 			pwarn("CONVERTING TO NEW SUPERBLOCK LAYOUT\n");
    244 		else if (!reply("CONVERT TO NEW SUPERBLOCK LAYOUT"))
    245 			return(0);
    246 		sblock->fs_old_flags |= FS_FLAGS_UPDATED;
    247 		/* Disable the postbl tables */
    248 		sblock->fs_old_cpc = 0;
    249 		sblock->fs_old_nrpos = 1;
    250 		sblock->fs_old_trackskew = 0;
    251 		/* The other fields have already been updated by
    252 		 * sb_oldfscompat_read
    253 		 */
    254 		sbdirty();
    255 	}
    256 	if (!is_ufs2 && cvtlevel == 3 &&
    257 	    (sblock->fs_old_flags & FS_FLAGS_UPDATED)) {
    258 		if (preen)
    259 			pwarn("DOWNGRADING TO OLD SUPERBLOCK LAYOUT\n");
    260 		else if (!reply("DOWNGRADE TO OLD SUPERBLOCK LAYOUT"))
    261 			return(0);
    262 		sblock->fs_old_flags &= ~FS_FLAGS_UPDATED;
    263 		sb_oldfscompat_write(sblock, sblock);
    264 		sblock->fs_old_flags &= ~FS_FLAGS_UPDATED; /* just in case */
    265 		/* Leave postbl tables disabled, but blank its superblock region anyway */
    266 		sblock->fs_old_postblformat = FS_DYNAMICPOSTBLFMT;
    267 		sblock->fs_old_cpc = 0;
    268 		sblock->fs_old_nrpos = 1;
    269 		sblock->fs_old_trackskew = 0;
    270 		memset(&sblock->fs_old_postbl_start, 0xff, 256);
    271 		sb_oldfscompat_read(sblock, &sblocksave);
    272 		sbdirty();
    273 	}
    274 	/*
    275 	 * Check and potentially fix certain fields in the super block.
    276 	 */
    277 	if (sblock->fs_flags & ~(FS_KNOWN_FLAGS)) {
    278 		pfatal("UNKNOWN FLAGS=0x%08x IN SUPERBLOCK", sblock->fs_flags);
    279 		if (reply("CLEAR") == 1) {
    280 			sblock->fs_flags &= FS_KNOWN_FLAGS;
    281 			sbdirty();
    282 		}
    283 	}
    284 	if (sblock->fs_optim != FS_OPTTIME && sblock->fs_optim != FS_OPTSPACE) {
    285 		pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
    286 		if (reply("SET TO DEFAULT") == 1) {
    287 			sblock->fs_optim = FS_OPTTIME;
    288 			sbdirty();
    289 		}
    290 	}
    291 	if ((sblock->fs_minfree < 0 || sblock->fs_minfree > 99)) {
    292 		pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
    293 			sblock->fs_minfree);
    294 		if (reply("SET TO DEFAULT") == 1) {
    295 			sblock->fs_minfree = 10;
    296 			sbdirty();
    297 		}
    298 	}
    299 	if (!is_ufs2 && sblock->fs_old_postblformat != FS_42POSTBLFMT &&
    300 	    (sblock->fs_old_interleave < 1 ||
    301 	    sblock->fs_old_interleave > sblock->fs_old_nsect)) {
    302 		pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK",
    303 			sblock->fs_old_interleave);
    304 		sblock->fs_old_interleave = 1;
    305 		if (preen)
    306 			printf(" (FIXED)\n");
    307 		if (preen || reply("SET TO DEFAULT") == 1) {
    308 			sbdirty();
    309 			dirty(&asblk);
    310 		}
    311 	}
    312 	if (!is_ufs2 && sblock->fs_old_postblformat != FS_42POSTBLFMT &&
    313 	    (sblock->fs_old_npsect < sblock->fs_old_nsect ||
    314 	    sblock->fs_old_npsect > sblock->fs_old_nsect*2)) {
    315 		pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK",
    316 			sblock->fs_old_npsect);
    317 		sblock->fs_old_npsect = sblock->fs_old_nsect;
    318 		if (preen)
    319 			printf(" (FIXED)\n");
    320 		if (preen || reply("SET TO DEFAULT") == 1) {
    321 			sbdirty();
    322 			dirty(&asblk);
    323 		}
    324 	}
    325 	if (sblock->fs_bmask != ~(sblock->fs_bsize - 1)) {
    326 		pwarn("INCORRECT BMASK=0x%x IN SUPERBLOCK",
    327 			sblock->fs_bmask);
    328 		sblock->fs_bmask = ~(sblock->fs_bsize - 1);
    329 		if (preen)
    330 			printf(" (FIXED)\n");
    331 		if (preen || reply("FIX") == 1) {
    332 			sbdirty();
    333 			dirty(&asblk);
    334 		}
    335 	}
    336 	if (sblock->fs_fmask != ~(sblock->fs_fsize - 1)) {
    337 		pwarn("INCORRECT FMASK=0x%x IN SUPERBLOCK",
    338 			sblock->fs_fmask);
    339 		sblock->fs_fmask = ~(sblock->fs_fsize - 1);
    340 		if (preen)
    341 			printf(" (FIXED)\n");
    342 		if (preen || reply("FIX") == 1) {
    343 			sbdirty();
    344 			dirty(&asblk);
    345 		}
    346 	}
    347 	if (check_snapinum()) {
    348 		if (preen)
    349 			printf(" (FIXED)\n");
    350 		if (preen || reply("FIX") == 1) {
    351 			sbdirty();
    352 			dirty(&asblk);
    353 		}
    354 	}
    355 	if (is_ufs2 || sblock->fs_old_inodefmt >= FS_44INODEFMT) {
    356 		if (sblock->fs_maxfilesize != maxfilesize) {
    357 			pwarn("INCORRECT MAXFILESIZE=%lld IN SUPERBLOCK",
    358 			    (unsigned long long)sblock->fs_maxfilesize);
    359 			sblock->fs_maxfilesize = maxfilesize;
    360 			if (preen)
    361 				printf(" (FIXED)\n");
    362 			if (preen || reply("FIX") == 1) {
    363 				sbdirty();
    364 				dirty(&asblk);
    365 			}
    366 		}
    367 		if ((is_ufs2 && sblock->fs_maxsymlinklen != UFS2_MAXSYMLINKLEN)
    368 		    ||
    369 		   (!is_ufs2 && sblock->fs_maxsymlinklen != UFS1_MAXSYMLINKLEN))
    370 		    {
    371 			pwarn("INCORRECT MAXSYMLINKLEN=%d IN SUPERBLOCK",
    372 				sblock->fs_maxsymlinklen);
    373 			sblock->fs_maxsymlinklen = is_ufs2 ?
    374 			    UFS2_MAXSYMLINKLEN : UFS1_MAXSYMLINKLEN;
    375 			if (preen)
    376 				printf(" (FIXED)\n");
    377 			if (preen || reply("FIX") == 1) {
    378 				sbdirty();
    379 				dirty(&asblk);
    380 			}
    381 		}
    382 		if (sblock->fs_qbmask != ~sblock->fs_bmask) {
    383 			pwarn("INCORRECT QBMASK=%#llx IN SUPERBLOCK",
    384 			    (unsigned long long)sblock->fs_qbmask);
    385 			sblock->fs_qbmask = ~sblock->fs_bmask;
    386 			if (preen)
    387 				printf(" (FIXED)\n");
    388 			if (preen || reply("FIX") == 1) {
    389 				sbdirty();
    390 				dirty(&asblk);
    391 			}
    392 		}
    393 		if (sblock->fs_qfmask != ~sblock->fs_fmask) {
    394 			pwarn("INCORRECT QFMASK=%#llx IN SUPERBLOCK",
    395 			    (unsigned long long)sblock->fs_qfmask);
    396 			sblock->fs_qfmask = ~sblock->fs_fmask;
    397 			if (preen)
    398 				printf(" (FIXED)\n");
    399 			if (preen || reply("FIX") == 1) {
    400 				sbdirty();
    401 				dirty(&asblk);
    402 			}
    403 		}
    404 		newinofmt = 1;
    405 	} else {
    406 		sblock->fs_qbmask = ~sblock->fs_bmask;
    407 		sblock->fs_qfmask = ~sblock->fs_fmask;
    408 		newinofmt = 0;
    409 	}
    410 	/*
    411 	 * Convert to new inode format.
    412 	 */
    413 	if (!is_ufs2 && cvtlevel >= 2 &&
    414 	    sblock->fs_old_inodefmt < FS_44INODEFMT) {
    415 		if (preen)
    416 			pwarn("CONVERTING TO NEW INODE FORMAT\n");
    417 		else if (!reply("CONVERT TO NEW INODE FORMAT"))
    418 			return(0);
    419 		doinglevel2++;
    420 		sblock->fs_old_inodefmt = FS_44INODEFMT;
    421 		sblock->fs_maxfilesize = maxfilesize;
    422 		sblock->fs_maxsymlinklen = UFS1_MAXSYMLINKLEN;
    423 		sblock->fs_qbmask = ~sblock->fs_bmask;
    424 		sblock->fs_qfmask = ~sblock->fs_fmask;
    425 		sbdirty();
    426 		dirty(&asblk);
    427 	}
    428 	/*
    429 	 * Convert to new cylinder group format.
    430 	 */
    431 	if (!is_ufs2 && cvtlevel >= 1 &&
    432 	    sblock->fs_old_postblformat == FS_42POSTBLFMT) {
    433 		if (preen)
    434 			pwarn("CONVERTING TO NEW CYLINDER GROUP FORMAT\n");
    435 		else if (!reply("CONVERT TO NEW CYLINDER GROUP FORMAT"))
    436 			return(0);
    437 		doinglevel1++;
    438 		sblock->fs_old_postblformat = FS_DYNAMICPOSTBLFMT;
    439 		sblock->fs_old_nrpos = 8;
    440 		sblock->fs_old_postbloff =
    441 		    (char *)(&sblock->fs_old_postbl_start) -
    442 		    (char *)(&sblock->fs_firstfield);
    443 		sblock->fs_old_rotbloff =
    444 				(char *)(&sblock->fs_magic+1) -
    445 				(char *)(&sblock->fs_firstfield);
    446 		sblock->fs_cgsize =
    447 			ffs_fragroundup(sblock, CGSIZE(sblock));
    448 		sbdirty();
    449 		dirty(&asblk);
    450 	}
    451 	if (asblk.b_dirty && !bflag) {
    452 		memmove(sblk.b_un.b_fs, sblock, SBLOCKSIZE);
    453 		sb_oldfscompat_write(sblk.b_un.b_fs, sblocksave);
    454 		if (needswap)
    455 			ffs_sb_swap(sblk.b_un.b_fs, sblk.b_un.b_fs);
    456 		memmove(asblk.b_un.b_fs, sblk.b_un.b_fs, (size_t)sblock->fs_sbsize);
    457 		flush(fswritefd, &asblk);
    458 	}
    459 	/*
    460 	 * read in the summary info.
    461 	 */
    462 	asked = 0;
    463 	sblock->fs_csp = (struct csum *)aligned_alloc(DEV_BSIZE,
    464 	    sblock->fs_cssize);
    465 	if (sblock->fs_csp == NULL) {
    466 		pwarn("cannot alloc %u bytes for summary info\n",
    467 		    sblock->fs_cssize);
    468 		goto badsblabel;
    469 	}
    470 	memset(sblock->fs_csp, 0, sblock->fs_cssize);
    471 	for (i = 0, j = 0; i < sblock->fs_cssize; i += sblock->fs_bsize, j++) {
    472 		size = sblock->fs_cssize - i < sblock->fs_bsize ?
    473 		    sblock->fs_cssize - i : sblock->fs_bsize;
    474 		ccsp = (struct csum *)((char *)sblock->fs_csp + i);
    475 		if (bread(fsreadfd, (char *)ccsp,
    476 		    FFS_FSBTODB(sblock, sblock->fs_csaddr + j * sblock->fs_frag),
    477 		    size) != 0 && !asked) {
    478 			pfatal("BAD SUMMARY INFORMATION");
    479 			if (reply("CONTINUE") == 0) {
    480 				markclean = 0;
    481 				exit(FSCK_EXIT_CHECK_FAILED);
    482 			}
    483 			asked++;
    484 		}
    485 		if (doswap) {
    486 			ffs_csum_swap(ccsp, ccsp, size);
    487 			bwrite(fswritefd, (char *)ccsp,
    488 			    FFS_FSBTODB(sblock,
    489 				sblock->fs_csaddr + j * sblock->fs_frag),
    490 			    size);
    491 		}
    492 		if (needswap)
    493 			ffs_csum_swap(ccsp, ccsp, size);
    494 	}
    495 	/*
    496 	 * allocate and initialize the necessary maps
    497 	 */
    498 	bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
    499 	blockmap = aligned_alloc(DEV_BSIZE, bmapsize);
    500 	if (blockmap == NULL) {
    501 		pwarn("cannot alloc %zu bytes for blockmap\n", bmapsize);
    502 		goto badsblabel;
    503 	}
    504 	memset(blockmap, 0, bmapsize);
    505 	inostathead = calloc((unsigned)(sblock->fs_ncg),
    506 	    sizeof(struct inostatlist));
    507 	if (inostathead == NULL) {
    508 		pwarn("cannot alloc %u bytes for inostathead\n",
    509 		    (unsigned)(sizeof(struct inostatlist) * (sblock->fs_ncg)));
    510 		goto badsblabel;
    511 	}
    512 	/*
    513 	 * cs_ndir may be inaccurate, particularly if we're using the -b
    514 	 * option, so set a minimum to prevent bogus subdirectory reconnects
    515 	 * and really inefficient directory scans.
    516 	 * Also set a maximum in case the value is too large.
    517 	 */
    518 	numdirs = sblock->fs_cstotal.cs_ndir;
    519 	if (numdirs < 1024)
    520 		numdirs = 1024;
    521 	if ((ino_t)numdirs > maxino + 1)
    522 		numdirs = maxino + 1;
    523 	dirhash = numdirs;
    524 	inplast = 0;
    525 	listmax = numdirs + 10;
    526 	inpsort = calloc((unsigned)listmax, sizeof(*inpsort));
    527 	inphead = calloc((unsigned)numdirs, sizeof(*inphead));
    528 	if (inpsort == NULL || inphead == NULL) {
    529 		pwarn("cannot alloc %u bytes for inphead\n",
    530 		    (unsigned)(numdirs * sizeof(struct inoinfo *)));
    531 		goto badsblabel;
    532 	}
    533 	cgrp = aligned_alloc(DEV_BSIZE, sblock->fs_cgsize);
    534 	if (cgrp == NULL) {
    535 		pwarn("cannot alloc %u bytes for cylinder group\n",
    536 		    sblock->fs_cgsize);
    537 		goto badsblabel;
    538 	}
    539 	bufinit();
    540 	if (sblock->fs_flags & FS_DOSOFTDEP)
    541 		usedsoftdep = 1;
    542 	else
    543 		usedsoftdep = 0;
    544 
    545 #ifndef NO_APPLE_UFS
    546 	if (!forceimage && dkw.dkw_parent[0])
    547 		if (strcmp(dkw.dkw_ptype, DKW_PTYPE_APPLEUFS) == 0)
    548 			isappleufs = 1;
    549 
    550 	if (readappleufs())
    551 		isappleufs = 1;
    552 #endif
    553 
    554 	if (isappleufs)
    555 		dirblksiz = APPLEUFS_DIRBLKSIZ;
    556 	else
    557 		dirblksiz = UFS_DIRBLKSIZ;
    558 
    559 	if (debug)
    560 #ifndef NO_APPLE_UFS
    561 		printf("isappleufs = %d, dirblksiz = %d\n", isappleufs, dirblksiz);
    562 #else
    563 		printf("dirblksiz = %d\n", dirblksiz);
    564 #endif
    565 
    566 	if (sblock->fs_flags & FS_DOQUOTA2) {
    567 		/* allocate the quota hash table */
    568 		/*
    569 		 * first compute the size of the hash table
    570 		 * We know the smallest block size is 4k, so we can use 2k
    571 		 * for the hash table; as an entry is 8 bytes we can store
    572 		 * 256 entries. So let start q2h_hash_shift at 8
    573 		 */
    574 		for (q2h_hash_shift = 8;
    575 		    q2h_hash_shift < 15;
    576 		    q2h_hash_shift++) {
    577 			if ((sizeof(uint64_t) << (q2h_hash_shift + 1)) +
    578 			    sizeof(struct quota2_header) >
    579 			    (size_t)sblock->fs_bsize)
    580 				break;
    581 		}
    582 		q2h_hash_mask = (1 << q2h_hash_shift) - 1;
    583 		if (debug) {
    584 			printf("quota hash shift %d, %d entries, mask 0x%x\n",
    585 			    q2h_hash_shift, (1 << q2h_hash_shift),
    586 			    q2h_hash_mask);
    587 		}
    588 		uquot_user_hash =
    589 		    calloc((1 << q2h_hash_shift), sizeof(struct uquot_hash));
    590 		uquot_group_hash =
    591 		    calloc((1 << q2h_hash_shift), sizeof(struct uquot_hash));
    592 		if (uquot_user_hash == NULL || uquot_group_hash == NULL)
    593 			errexit("Cannot allocate space for quotas hash\n");
    594 	} else {
    595 		uquot_user_hash = uquot_group_hash = NULL;
    596 		q2h_hash_shift = q2h_hash_mask = 0;
    597 	}
    598 	return (1);
    599 badsblabel:
    600 	markclean=0;
    601 	ckfini(1);
    602 	return (0);
    603 }
    604 
    605 #ifndef NO_APPLE_UFS
    606 static int
    607 readappleufs(void)
    608 {
    609 	daddr_t label = APPLEUFS_LABEL_OFFSET / dev_bsize;
    610 	struct appleufslabel *appleufs;
    611 	int i;
    612 
    613 	/* XXX do we have to deal with APPLEUFS_LABEL_OFFSET not
    614 	 * being block aligned (CD's?)
    615 	 */
    616 	if (APPLEUFS_LABEL_SIZE % dev_bsize != 0)
    617 		return 0;
    618 	if (bread(fsreadfd, (char *)appleufsblk.b_un.b_fs, label,
    619 	    (long)APPLEUFS_LABEL_SIZE) != 0)
    620 		return 0;
    621 	appleufsblk.b_bno = label;
    622 	appleufsblk.b_size = APPLEUFS_LABEL_SIZE;
    623 
    624 	appleufs = appleufsblk.b_un.b_appleufs;
    625 
    626 	if (ntohl(appleufs->ul_magic) != APPLEUFS_LABEL_MAGIC) {
    627 		if (!isappleufs) {
    628 			return 0;
    629 		} else {
    630 			pfatal("MISSING APPLEUFS VOLUME LABEL\n");
    631 			if (reply("FIX") == 0) {
    632 				return 1;
    633 			}
    634 			ffs_appleufs_set(appleufs, NULL, -1, 0);
    635 			appleufsdirty();
    636 		}
    637 	}
    638 
    639 	if (ntohl(appleufs->ul_version) != APPLEUFS_LABEL_VERSION) {
    640 		pwarn("INCORRECT APPLE UFS VERSION NUMBER (%d should be %d)",
    641 			ntohl(appleufs->ul_version),APPLEUFS_LABEL_VERSION);
    642 		if (preen) {
    643 			printf(" (CORRECTED)\n");
    644 		}
    645 		if (preen || reply("CORRECT")) {
    646 			appleufs->ul_version = htonl(APPLEUFS_LABEL_VERSION);
    647 			appleufsdirty();
    648 		}
    649 	}
    650 
    651 	if (ntohs(appleufs->ul_namelen) > APPLEUFS_MAX_LABEL_NAME) {
    652 		pwarn("APPLE UFS LABEL NAME TOO LONG");
    653 		if (preen) {
    654 			printf(" (TRUNCATED)\n");
    655 		}
    656 		if (preen || reply("TRUNCATE")) {
    657 			appleufs->ul_namelen = htons(APPLEUFS_MAX_LABEL_NAME);
    658 			appleufsdirty();
    659 		}
    660 	}
    661 
    662 	if (ntohs(appleufs->ul_namelen) == 0) {
    663 		pwarn("MISSING APPLE UFS LABEL NAME");
    664 		if (preen) {
    665 			printf(" (FIXED)\n");
    666 		}
    667 		if (preen || reply("FIX")) {
    668 			ffs_appleufs_set(appleufs, NULL, -1, 0);
    669 			appleufsdirty();
    670 		}
    671 	}
    672 
    673 	/* Scan name for first illegal character */
    674 	for (i=0;i<ntohs(appleufs->ul_namelen);i++) {
    675 		if ((appleufs->ul_name[i] == '\0') ||
    676 			(appleufs->ul_name[i] == ':') ||
    677 			(appleufs->ul_name[i] == '/')) {
    678 			pwarn("APPLE UFS LABEL NAME CONTAINS ILLEGAL CHARACTER");
    679 			if (preen) {
    680 				printf(" (TRUNCATED)\n");
    681 			}
    682 			if (preen || reply("TRUNCATE")) {
    683 				appleufs->ul_namelen = i+1;
    684 				appleufsdirty();
    685 			}
    686 			break;
    687 		}
    688 	}
    689 
    690 	/* Check the checksum last, because if anything else was wrong,
    691 	 * then the checksum gets reset anyway.
    692 	 */
    693 	appleufs->ul_checksum = 0;
    694 	appleufs->ul_checksum = ffs_appleufs_cksum(appleufs);
    695 	if (appleufsblk.b_un.b_appleufs->ul_checksum != appleufs->ul_checksum) {
    696 		pwarn("INVALID APPLE UFS CHECKSUM (%#04x should be %#04x)",
    697 			appleufsblk.b_un.b_appleufs->ul_checksum, appleufs->ul_checksum);
    698 		if (preen) {
    699 			printf(" (CORRECTED)\n");
    700 		}
    701 		if (preen || reply("CORRECT")) {
    702 			appleufsdirty();
    703 		} else {
    704 			/* put the incorrect checksum back in place */
    705 			appleufs->ul_checksum = appleufsblk.b_un.b_appleufs->ul_checksum;
    706 		}
    707 	}
    708 	return 1;
    709 }
    710 #endif /* !NO_APPLE_UFS */
    711 
    712 /*
    713  * Detect byte order. Return 0 if valid magic found, -1 otherwise.
    714  */
    715 static int
    716 detect_byteorder(struct fs *fs, int sblockoff)
    717 {
    718 	if (sblockoff == SBLOCK_UFS2 && (fs->fs_magic == FS_UFS1_MAGIC ||
    719 	    fs->fs_magic == FS_UFS1_MAGIC_SWAPPED))
    720 		/* Likely to be the first alternate of a fs with 64k blocks */
    721 		return -1;
    722 	if (fs->fs_magic == FS_UFS1_MAGIC || fs->fs_magic == FS_UFS2_MAGIC ||
    723 	    fs->fs_magic == FS_UFS2EA_MAGIC) {
    724 #ifndef NO_FFS_EI
    725 		if (endian == 0 || BYTE_ORDER == endian) {
    726 			needswap = 0;
    727 			doswap = do_blkswap = do_dirswap = 0;
    728 		} else {
    729 			needswap = 1;
    730 			doswap = do_blkswap = do_dirswap = 1;
    731 		}
    732 #endif
    733 		return 0;
    734 	}
    735 #ifndef NO_FFS_EI
    736 	else if (fs->fs_magic == FS_UFS1_MAGIC_SWAPPED ||
    737 		 fs->fs_magic == FS_UFS2_MAGIC_SWAPPED ||
    738 		 fs->fs_magic == FS_UFS2EA_MAGIC_SWAPPED) {
    739 		if (endian == 0 || BYTE_ORDER != endian) {
    740 			needswap = 1;
    741 			doswap = do_blkswap = do_dirswap = 0;
    742 		} else {
    743 			needswap = 0;
    744 			doswap = do_blkswap = do_dirswap = 1;
    745 		}
    746 		return 0;
    747 	}
    748 #endif
    749 	return -1;
    750 }
    751 
    752 /* Update on-disk fs->fs_magic if we are converting */
    753 void
    754 cvt_magic(struct fs *fs)
    755 {
    756 
    757 	if (is_ufs2ea || doing2ea) {
    758 		if (fs->fs_magic == FS_UFS2_MAGIC) {
    759 			fs->fs_magic = FS_UFS2EA_MAGIC;
    760 		}
    761 		if (fs->fs_magic == FS_UFS2_MAGIC_SWAPPED) {
    762 			fs->fs_magic = FS_UFS2EA_MAGIC_SWAPPED;
    763 		}
    764 	}
    765 	if (doing2noea) {
    766 		if (fs->fs_magic == FS_UFS2EA_MAGIC) {
    767 			fs->fs_magic = FS_UFS2_MAGIC;
    768 		}
    769 		if (fs->fs_magic == FS_UFS2EA_MAGIC_SWAPPED) {
    770 			fs->fs_magic = FS_UFS2_MAGIC_SWAPPED;
    771 		}
    772 	}
    773 }
    774 
    775 /*
    776  * Possible superblock locations ordered from most to least likely.
    777  */
    778 static off_t sblock_try[] = SBLOCKSEARCH;
    779 
    780 /*
    781  * Read in the super block and its summary info.
    782  */
    783 static int
    784 readsb(int listerr)
    785 {
    786 	daddr_t super = 0;
    787 	struct fs *fs;
    788 	int i;
    789 
    790 	if (bflag) {
    791 		super = bflag;
    792 		if (bread(fsreadfd, (char *)sblk.b_un.b_fs, super,
    793 		    (long)SBLOCKSIZE) != 0)
    794 			return (0);
    795 		fs = sblk.b_un.b_fs;
    796 		if (detect_byteorder(fs, -1) < 0) {
    797 			badsb(listerr, "MAGIC NUMBER WRONG");
    798 			return (0);
    799 		}
    800 	} else {
    801 		for (i = 0; sblock_try[i] != -1; i++) {
    802 			super = sblock_try[i] / dev_bsize;
    803 			if (bread(fsreadfd, (char *)sblk.b_un.b_fs,
    804 			    super, (long)SBLOCKSIZE) != 0)
    805 				continue;
    806 			fs = sblk.b_un.b_fs;
    807 			if (detect_byteorder(fs, sblock_try[i]) == 0)
    808 				break;
    809 		}
    810 		if (sblock_try[i] == -1) {
    811 			badsb(listerr, "CAN'T FIND SUPERBLOCK");
    812 			return (0);
    813 		}
    814 	}
    815 	if (doswap) {
    816 		if (preen)
    817 			errx(FSCK_EXIT_USAGE,
    818 			    "Incompatible options -B and -p");
    819 		if (nflag)
    820 			errx(FSCK_EXIT_USAGE,
    821 			    "Incompatible options -B and -n");
    822 		if (endian == LITTLE_ENDIAN) {
    823 			if (!reply("CONVERT TO LITTLE ENDIAN"))
    824 				return 0;
    825 		} else if (endian == BIG_ENDIAN) {
    826 			if (!reply("CONVERT TO BIG ENDIAN"))
    827 				return 0;
    828 		} else
    829 			pfatal("INTERNAL ERROR: unknown endian");
    830 	}
    831 	if (needswap)
    832 		pwarn("** Swapped byte order\n");
    833 	/* swap SB byte order if asked */
    834 	if (doswap)
    835 		ffs_sb_swap(sblk.b_un.b_fs, sblk.b_un.b_fs);
    836 
    837 	memmove(sblock, sblk.b_un.b_fs, SBLOCKSIZE);
    838 	if (needswap)
    839 		ffs_sb_swap(sblk.b_un.b_fs, sblock);
    840 	if (sblock->fs_magic == FS_UFS2EA_MAGIC) {
    841 		is_ufs2ea = 1;
    842 		sblock->fs_magic = FS_UFS2_MAGIC;
    843 	}
    844 	is_ufs2 = sblock->fs_magic == FS_UFS2_MAGIC;
    845 
    846 	/* change on-disk magic if asked */
    847 	cvt_magic(fs);
    848 
    849 	/*
    850 	 * run a few consistency checks of the super block
    851 	 */
    852 	if (sblock->fs_sbsize > SBLOCKSIZE)
    853 		{ badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); }
    854 	/*
    855 	 * Compute block size that the filesystem is based on,
    856 	 * according to FFS_FSBTODB, and adjust superblock block number
    857 	 * so we can tell if this is an alternate later.
    858 	 */
    859 	super *= dev_bsize;
    860 	dev_bsize = sblock->fs_fsize / FFS_FSBTODB(sblock, 1);
    861 	sblk.b_bno = super / dev_bsize;
    862 	sblk.b_size = SBLOCKSIZE;
    863 	if (bflag)
    864 		goto out;
    865 	/*
    866 	 * Set all possible fields that could differ, then do check
    867 	 * of whole super block against an alternate super block->
    868 	 * When an alternate super-block is specified this check is skipped.
    869 	 */
    870 	getblk(&asblk, cgsblock(sblock, sblock->fs_ncg - 1), sblock->fs_sbsize);
    871 	if (asblk.b_errs)
    872 		return (0);
    873 	/* swap SB byte order if asked */
    874 	if (doswap)
    875 		ffs_sb_swap(asblk.b_un.b_fs, asblk.b_un.b_fs);
    876 
    877 	memmove(altsblock, asblk.b_un.b_fs, sblock->fs_sbsize);
    878 	if (needswap)
    879 		ffs_sb_swap(asblk.b_un.b_fs, altsblock);
    880 	if (altsblock->fs_magic == FS_UFS2EA_MAGIC) {
    881 		altsblock->fs_magic = FS_UFS2_MAGIC;
    882 	}
    883 	/* change on-disk magic if asked */
    884 	cvt_magic(asblk.b_un.b_fs);
    885 	if (cmpsblks(sblock, altsblock)) {
    886 		if (debug) {
    887 			uint32_t *nlp, *olp, *endlp;
    888 
    889 			printf("superblock mismatches\n");
    890 			nlp = (uint32_t *)altsblock;
    891 			olp = (uint32_t *)sblock;
    892 			endlp = olp + (sblock->fs_sbsize / sizeof *olp);
    893 			for ( ; olp < endlp; olp++, nlp++) {
    894 				if (*olp == *nlp)
    895 					continue;
    896 				printf("offset %#x, original 0x%08x, alternate "
    897 				       "0x%08x\n",
    898 				    (int)((uint8_t *)olp-(uint8_t *)sblock),
    899 				    *olp, *nlp);
    900 			}
    901 		}
    902 		badsb(listerr,
    903 		"VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
    904 /*
    905 		return (0);
    906 */
    907 	}
    908 out:
    909 
    910 	sb_oldfscompat_read(sblock, &sblocksave);
    911 
    912 	/* Now we know the SB is valid, we can write it back if needed */
    913 	if (doswap || doing2ea || doing2noea) {
    914 		sbdirty();
    915 		dirty(&asblk);
    916 	}
    917 	havesb = 1;
    918 	return (1);
    919 }
    920 
    921 int
    922 cmpsblks(const struct fs *sb, struct fs *asb)
    923 {
    924 	if (!is_ufs2 && ((sb->fs_old_flags & FS_FLAGS_UPDATED) == 0)) {
    925 		if (sb->fs_old_postblformat < FS_DYNAMICPOSTBLFMT)
    926 			return cmpsblks42(sb, asb);
    927 		else
    928 			return cmpsblks44(sb, asb);
    929 	}
    930 	if (asb->fs_sblkno != sb->fs_sblkno ||
    931 	    asb->fs_cblkno != sb->fs_cblkno ||
    932 	    asb->fs_iblkno != sb->fs_iblkno ||
    933 	    asb->fs_dblkno != sb->fs_dblkno ||
    934 	    asb->fs_ncg != sb->fs_ncg ||
    935 	    asb->fs_bsize != sb->fs_bsize ||
    936 	    asb->fs_fsize != sb->fs_fsize ||
    937 	    asb->fs_frag != sb->fs_frag ||
    938 	    asb->fs_bmask != sb->fs_bmask ||
    939 	    asb->fs_fmask != sb->fs_fmask ||
    940 	    asb->fs_bshift != sb->fs_bshift ||
    941 	    asb->fs_fshift != sb->fs_fshift ||
    942 	    asb->fs_fragshift != sb->fs_fragshift ||
    943 	    asb->fs_fsbtodb != sb->fs_fsbtodb ||
    944 	    asb->fs_sbsize != sb->fs_sbsize ||
    945 	    asb->fs_nindir != sb->fs_nindir ||
    946 	    asb->fs_inopb != sb->fs_inopb ||
    947 	    asb->fs_cssize != sb->fs_cssize ||
    948 	    asb->fs_ipg != sb->fs_ipg ||
    949 	    asb->fs_fpg != sb->fs_fpg ||
    950 	    asb->fs_magic != sb->fs_magic)
    951 		return 1;
    952 	return 0;
    953 }
    954 
    955 /* BSD 4.2 performed the following superblock comparison
    956  * It should correspond to FS_42POSTBLFMT
    957  * (although note that in 4.2, the fs_old_postblformat
    958  * field didn't exist and the corresponding bits are
    959  * located near the end of the postbl itself, where they
    960  * are not likely to be used.)
    961  */
    962 int
    963 cmpsblks42(const struct fs *sb, struct fs *asb)
    964 {
    965 	asb->fs_firstfield = sb->fs_firstfield; /* fs_link */
    966 	asb->fs_unused_1 = sb->fs_unused_1; /* fs_rlink */
    967 	asb->fs_old_time = sb->fs_old_time; /* fs_time */
    968 	asb->fs_old_cstotal = sb->fs_old_cstotal; /* fs_cstotal */
    969 	asb->fs_cgrotor = sb->fs_cgrotor;
    970 	asb->fs_fmod = sb->fs_fmod;
    971 	asb->fs_clean = sb->fs_clean;
    972 	asb->fs_ronly = sb->fs_ronly;
    973 	asb->fs_old_flags = sb->fs_old_flags;
    974 	asb->fs_maxcontig = sb->fs_maxcontig;
    975 	asb->fs_minfree = sb->fs_minfree;
    976 	asb->fs_old_rotdelay = sb->fs_old_rotdelay;
    977 	asb->fs_maxbpg = sb->fs_maxbpg;
    978 
    979 	/* The former fs_csp, totaling 128 bytes  */
    980 	memmove(asb->fs_ocsp, sb->fs_ocsp, sizeof sb->fs_ocsp);
    981 	asb->fs_contigdirs = sb->fs_contigdirs;
    982 	asb->fs_csp = sb->fs_csp;
    983 	asb->fs_maxcluster = sb->fs_maxcluster;
    984 	asb->fs_active = sb->fs_active;
    985 
    986 	/* The former fs_fsmnt, totaling 512 bytes */
    987 	memmove(asb->fs_fsmnt, sb->fs_fsmnt, sizeof sb->fs_fsmnt);
    988 	memmove(asb->fs_volname, sb->fs_volname, sizeof sb->fs_volname);
    989 
    990 	return memcmp(sb, asb, sb->fs_sbsize);
    991 }
    992 
    993 /* BSD 4.4 performed the following superblock comparison
    994  * This was used in NetBSD through 1.6.1
    995  *
    996  * Note that this implementation is destructive to asb.
    997  */
    998 int
    999 cmpsblks44(const struct fs *sb, struct fs *asb)
   1000 {
   1001 	/*
   1002 	 * "Copy fields which we don't care if they're different in the
   1003 	 * alternate superblocks, as they're either likely to be
   1004 	 * different because they're per-cylinder-group specific, or
   1005 	 * because they're transient details which are only maintained
   1006 	 * in the primary superblock."
   1007 	 */
   1008 	asb->fs_firstfield = sb->fs_firstfield;
   1009 	asb->fs_unused_1 = sb->fs_unused_1;
   1010 	asb->fs_old_time = sb->fs_old_time;
   1011 	asb->fs_old_cstotal = sb->fs_old_cstotal;
   1012 	asb->fs_cgrotor = sb->fs_cgrotor;
   1013 	asb->fs_fmod = sb->fs_fmod;
   1014 	asb->fs_clean = sb->fs_clean;
   1015 	asb->fs_ronly = sb->fs_ronly;
   1016 	asb->fs_old_flags = sb->fs_old_flags;
   1017 	asb->fs_maxcontig = sb->fs_maxcontig;
   1018 	asb->fs_minfree = sb->fs_minfree;
   1019 	asb->fs_optim = sb->fs_optim;
   1020 	asb->fs_old_rotdelay = sb->fs_old_rotdelay;
   1021 	asb->fs_maxbpg = sb->fs_maxbpg;
   1022 
   1023 	/* The former fs_csp and fs_maxcluster, totaling 128 bytes */
   1024 	memmove(asb->fs_ocsp, sb->fs_ocsp, sizeof sb->fs_ocsp);
   1025 	asb->fs_contigdirs = sb->fs_contigdirs;
   1026 	asb->fs_csp = sb->fs_csp;
   1027 	asb->fs_maxcluster = sb->fs_maxcluster;
   1028 	asb->fs_active = sb->fs_active;
   1029 
   1030 	/* The former fs_fsmnt, totaling 512 bytes */
   1031 	memmove(asb->fs_fsmnt, sb->fs_fsmnt, sizeof sb->fs_fsmnt);
   1032 	memmove(asb->fs_volname, sb->fs_volname, sizeof sb->fs_volname);
   1033 
   1034 	/* The former fs_sparecon, totaling 200 bytes */
   1035 	memmove(asb->fs_snapinum,
   1036 		sb->fs_snapinum, sizeof sb->fs_snapinum);
   1037 	asb->fs_avgfilesize = sb->fs_avgfilesize;
   1038 	asb->fs_avgfpdir = sb->fs_avgfpdir;
   1039 	asb->fs_save_cgsize = sb->fs_save_cgsize;
   1040 	memmove(asb->fs_sparecon32,
   1041 		sb->fs_sparecon32, sizeof sb->fs_sparecon32);
   1042 	asb->fs_flags = sb->fs_flags;
   1043 
   1044 	/* Original comment:
   1045 	 * "The following should not have to be copied, but need to be."
   1046 	 */
   1047 	asb->fs_fsbtodb = sb->fs_fsbtodb;
   1048 	asb->fs_old_interleave = sb->fs_old_interleave;
   1049 	asb->fs_old_npsect = sb->fs_old_npsect;
   1050 	asb->fs_old_nrpos = sb->fs_old_nrpos;
   1051 	asb->fs_state = sb->fs_state;
   1052 	asb->fs_qbmask = sb->fs_qbmask;
   1053 	asb->fs_qfmask = sb->fs_qfmask;
   1054 	asb->fs_state = sb->fs_state;
   1055 	asb->fs_maxfilesize = sb->fs_maxfilesize;
   1056 
   1057 	/*
   1058 	 * "Compare the superblocks, effectively checking every other
   1059 	 * field to see if they differ."
   1060 	 */
   1061 	return memcmp(sb, asb, sb->fs_sbsize);
   1062 }
   1063 
   1064 static void
   1065 badsb(int listerr, const char *s)
   1066 {
   1067 
   1068 	if (!listerr)
   1069 		return;
   1070 	if (preen)
   1071 		printf("%s: ", cdevname());
   1072 	pfatal("BAD SUPER BLOCK: %s\n", s);
   1073 }
   1074 
   1075 /*
   1076  * Calculate a prototype superblock based on information in the disk label.
   1077  * When done the cgsblock macro can be calculated and the fs_ncg field
   1078  * can be used. Do NOT attempt to use other macros without verifying that
   1079  * their needed information is available!
   1080  */
   1081 static int
   1082 calcsb(const char *dev, int devfd, struct fs *fs)
   1083 {
   1084 	struct dkwedge_info dkw;
   1085 	struct disk_geom geo;
   1086 	int i, nspf;
   1087 
   1088 	if (getdiskinfo(dev, fsreadfd, NULL, &geo, &dkw) == -1)
   1089 		pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
   1090 	if (dkw.dkw_parent[0] == '\0') {
   1091 		pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
   1092 		return (0);
   1093 	}
   1094 	if (strcmp(dkw.dkw_ptype, DKW_PTYPE_FFS)
   1095 #ifndef NO_APPLE_UFS
   1096 	 && strcmp(dkw.dkw_ptype, DKW_PTYPE_APPLEUFS)
   1097 #endif
   1098 	) {
   1099 		pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n",
   1100 		    dev, dkw.dkw_ptype);
   1101 		return (0);
   1102 	}
   1103 	if (geo.dg_secsize == 0) {
   1104 		pfatal("%s: CANNOT FIGURE OUT SECTOR SIZE\n", dev);
   1105 		return 0;
   1106 	}
   1107 	if (geo.dg_secpercyl == 0) {
   1108 		pfatal("%s: CANNOT FIGURE OUT SECTORS PER CYLINDER\n", dev);
   1109 		return 0;
   1110 	}
   1111 	if (sblk.b_un.b_fs->fs_fsize == 0) {
   1112 		pfatal("%s: CANNOT FIGURE OUT FRAG BLOCK SIZE\n", dev);
   1113 		return 0;
   1114 	}
   1115 	if (sblk.b_un.b_fs->fs_fpg == 0) {
   1116 		pfatal("%s: CANNOT FIGURE OUT FRAGS PER GROUP\n", dev);
   1117 		return 0;
   1118 	}
   1119 	if (sblk.b_un.b_fs->fs_old_cpg == 0) {
   1120 		pfatal("%s: CANNOT FIGURE OUT OLD CYLINDERS PER GROUP\n", dev);
   1121 		return 0;
   1122 	}
   1123 	memcpy(fs, sblk.b_un.b_fs, sizeof(struct fs));
   1124 	nspf = fs->fs_fsize / geo.dg_secsize;
   1125 	fs->fs_old_nspf = nspf;
   1126 	for (fs->fs_fsbtodb = 0, i = nspf; i > 1; i >>= 1)
   1127 		fs->fs_fsbtodb++;
   1128 	dev_bsize = geo.dg_secsize;
   1129 	if (fs->fs_magic == FS_UFS2_MAGIC) {
   1130 		fs->fs_ncg = howmany(fs->fs_size, fs->fs_fpg);
   1131 	} else /* if (fs->fs_magic == FS_UFS1_MAGIC) */ {
   1132 		fs->fs_old_cgmask = 0xffffffff;
   1133 		for (i = geo.dg_ntracks; i > 1; i >>= 1)
   1134 			fs->fs_old_cgmask <<= 1;
   1135 		if (!POWEROF2(geo.dg_ntracks))
   1136 			fs->fs_old_cgmask <<= 1;
   1137 		fs->fs_old_cgoffset = roundup(
   1138 			howmany(geo.dg_nsectors, nspf), fs->fs_frag);
   1139 		fs->fs_fpg = (fs->fs_old_cpg * geo.dg_secpercyl) / nspf;
   1140 		fs->fs_ncg = howmany(fs->fs_size / geo.dg_secpercyl,
   1141 		    fs->fs_old_cpg);
   1142 	}
   1143 	return (1);
   1144 }
   1145 
   1146 /*
   1147  * Test the list of snapshot inode numbers for duplicates and repair.
   1148  */
   1149 static int
   1150 check_snapinum(void)
   1151 {
   1152 	int loc, loc2, res;
   1153 	uint32_t *snapinum = &sblock->fs_snapinum[0];
   1154 
   1155 	res = 0;
   1156 
   1157 	if (isappleufs)
   1158 		return 0;
   1159 
   1160 	for (loc = 0; loc < FSMAXSNAP; loc++) {
   1161 		if (snapinum[loc] == 0)
   1162 			break;
   1163 		for (loc2 = loc + 1; loc2 < FSMAXSNAP; loc2++) {
   1164 			if (snapinum[loc2] == 0 ||
   1165 			    snapinum[loc2] == snapinum[loc])
   1166 				break;
   1167 		}
   1168 		if (loc2 >= FSMAXSNAP || snapinum[loc2] == 0)
   1169 			continue;
   1170 		pwarn("SNAPSHOT INODE %u ALREADY ON LIST%s", snapinum[loc2],
   1171 		    (res ? "" : "\n"));
   1172 		res = 1;
   1173 		for (loc2 = loc + 1; loc2 < FSMAXSNAP; loc2++) {
   1174 			if (snapinum[loc2] == 0)
   1175 				break;
   1176 			snapinum[loc2 - 1] = snapinum[loc2];
   1177 		}
   1178 		snapinum[loc2 - 1] = 0;
   1179 		loc--;
   1180 	}
   1181 
   1182 	return res;
   1183 }
   1184