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