Home | History | Annotate | Line # | Download | only in fsck_ffs
setup.c revision 1.53
      1 /*	$NetBSD: setup.c,v 1.53 2002/06/30 22:57:31 dbj 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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)setup.c	8.10 (Berkeley) 5/9/95";
     40 #else
     41 __RCSID("$NetBSD: setup.c,v 1.53 2002/06/30 22:57:31 dbj Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include <sys/param.h>
     46 #include <sys/time.h>
     47 #include <sys/stat.h>
     48 #include <sys/ioctl.h>
     49 #define FSTYPENAMES
     50 #include <sys/disklabel.h>
     51 #include <sys/file.h>
     52 
     53 #include <ufs/ufs/dinode.h>
     54 #include <ufs/ufs/ufs_bswap.h>
     55 #include <ufs/ffs/fs.h>
     56 #include <ufs/ffs/ffs_extern.h>
     57 
     58 #include <ctype.h>
     59 #include <err.h>
     60 #include <errno.h>
     61 #include <stdio.h>
     62 #include <stdlib.h>
     63 #include <string.h>
     64 
     65 #include "fsck.h"
     66 #include "extern.h"
     67 #include "fsutil.h"
     68 
     69 #define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
     70 
     71 static void badsb __P((int, char *));
     72 static int calcsb __P((const char *, int, struct fs *));
     73 static struct disklabel *getdisklabel __P((const char *, int));
     74 static int readsb __P((int));
     75 
     76 /*
     77  * Read in a superblock finding an alternate if necessary.
     78  * Return 1 if successful, 0 if unsuccessful, -1 if filesystem
     79  * is already clean (preen mode only).
     80  */
     81 int
     82 setup(dev)
     83 	const char *dev;
     84 {
     85 	long cg, size, asked, i, j;
     86 	long bmapsize;
     87 	struct disklabel *lp;
     88 	off_t sizepb;
     89 	struct stat statb;
     90 	struct fs proto;
     91 	int doskipclean;
     92 	u_int64_t maxfilesize;
     93 	struct csum *ccsp;
     94 
     95 	havesb = 0;
     96 	fswritefd = -1;
     97 	doskipclean = skipclean;
     98 	if (stat(dev, &statb) < 0) {
     99 		printf("Can't stat %s: %s\n", dev, strerror(errno));
    100 		return (0);
    101 	}
    102 	if (!forceimage && !S_ISCHR(statb.st_mode)) {
    103 		pfatal("%s is not a character device", dev);
    104 		if (reply("CONTINUE") == 0)
    105 			return (0);
    106 	}
    107 	if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
    108 		printf("Can't open %s: %s\n", dev, strerror(errno));
    109 		return (0);
    110 	}
    111 	if (preen == 0)
    112 		printf("** %s", dev);
    113 	if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) {
    114 		fswritefd = -1;
    115 		if (preen)
    116 			pfatal("NO WRITE ACCESS");
    117 		printf(" (NO WRITE)");
    118 	}
    119 	if (preen == 0)
    120 		printf("\n");
    121 	fsmodified = 0;
    122 	lfdir = 0;
    123 	initbarea(&sblk);
    124 	initbarea(&asblk);
    125 	sblk.b_un.b_buf = malloc(SBSIZE);
    126 	sblock = malloc(SBSIZE);
    127 	asblk.b_un.b_buf = malloc(SBSIZE);
    128 	altsblock = malloc(SBSIZE);
    129 	if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL ||
    130 		sblock == NULL || altsblock == NULL)
    131 		errx(EEXIT, "cannot allocate space for superblock");
    132 	if (!forceimage && (lp = getdisklabel(NULL, fsreadfd)) != NULL)
    133 		dev_bsize = secsize = lp->d_secsize;
    134 	else
    135 		dev_bsize = secsize = DEV_BSIZE;
    136 	/*
    137 	 * Read in the superblock, looking for alternates if necessary
    138 	 */
    139 	if (readsb(1) == 0) {
    140 		if (bflag || preen || forceimage ||
    141 		    calcsb(dev, fsreadfd, &proto) == 0)
    142 			return(0);
    143 		if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
    144 			return (0);
    145 		for (cg = 0; cg < proto.fs_ncg; cg++) {
    146 			bflag = fsbtodb(&proto, cgsblock(&proto, cg));
    147 			if (readsb(0) != 0)
    148 				break;
    149 		}
    150 		if (cg >= proto.fs_ncg) {
    151 			printf("%s %s\n%s %s\n%s %s\n",
    152 				"SEARCH FOR ALTERNATE SUPER-BLOCK",
    153 				"FAILED. YOU MUST USE THE",
    154 				"-b OPTION TO fsck_ffs TO SPECIFY THE",
    155 				"LOCATION OF AN ALTERNATE",
    156 				"SUPER-BLOCK TO SUPPLY NEEDED",
    157 				"INFORMATION; SEE fsck_ffs(8).");
    158 			return(0);
    159 		}
    160 		doskipclean = 0;
    161 		pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
    162 	}
    163 	if (debug)
    164 		printf("clean = %d\n", sblock->fs_clean);
    165 	if (doswap)
    166 		doskipclean = 0;
    167 	if (sblock->fs_clean & FS_ISCLEAN) {
    168 		if (doskipclean) {
    169 			pwarn("%sile system is clean; not checking\n",
    170 			    preen ? "f" : "** F");
    171 			return (-1);
    172 		}
    173 		if (!preen && !doswap)
    174 			pwarn("** File system is already clean\n");
    175 	}
    176 	maxfsblock = sblock->fs_size;
    177 	maxino = sblock->fs_ncg * sblock->fs_ipg;
    178 	sizepb = sblock->fs_bsize;
    179 	maxfilesize = sblock->fs_bsize * NDADDR - 1;
    180 	for (i = 0; i < NIADDR; i++) {
    181 		sizepb *= NINDIR(sblock);
    182 		maxfilesize += sizepb;
    183 	}
    184 	/*
    185 	 * Check and potentially fix certain fields in the super block.
    186 	 */
    187 	if (sblock->fs_optim != FS_OPTTIME && sblock->fs_optim != FS_OPTSPACE) {
    188 		pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
    189 		if (reply("SET TO DEFAULT") == 1) {
    190 			sblock->fs_optim = FS_OPTTIME;
    191 			sbdirty();
    192 		}
    193 	}
    194 	if ((sblock->fs_minfree < 0 || sblock->fs_minfree > 99)) {
    195 		pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
    196 			sblock->fs_minfree);
    197 		if (reply("SET TO DEFAULT") == 1) {
    198 			sblock->fs_minfree = 10;
    199 			sbdirty();
    200 		}
    201 	}
    202 	if (sblock->fs_postblformat != FS_42POSTBLFMT &&
    203 	    (sblock->fs_interleave < 1 ||
    204 	    sblock->fs_interleave > sblock->fs_nsect)) {
    205 		pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK",
    206 			sblock->fs_interleave);
    207 		sblock->fs_interleave = 1;
    208 		if (preen)
    209 			printf(" (FIXED)\n");
    210 		if (preen || reply("SET TO DEFAULT") == 1) {
    211 			sbdirty();
    212 			dirty(&asblk);
    213 		}
    214 	}
    215 	if (sblock->fs_postblformat != FS_42POSTBLFMT &&
    216 	    (sblock->fs_npsect < sblock->fs_nsect ||
    217 	    sblock->fs_npsect > sblock->fs_nsect*2)) {
    218 		pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK",
    219 			sblock->fs_npsect);
    220 		sblock->fs_npsect = sblock->fs_nsect;
    221 		if (preen)
    222 			printf(" (FIXED)\n");
    223 		if (preen || reply("SET TO DEFAULT") == 1) {
    224 			sbdirty();
    225 			dirty(&asblk);
    226 		}
    227 	}
    228 	if (sblock->fs_bmask != ~(sblock->fs_bsize - 1)) {
    229 		pwarn("INCORRECT BMASK=0x%x IN SUPERBLOCK",
    230 			sblock->fs_bmask);
    231 		sblock->fs_bmask = ~(sblock->fs_bsize - 1);
    232 		if (preen)
    233 			printf(" (FIXED)\n");
    234 		if (preen || reply("FIX") == 1) {
    235 			sbdirty();
    236 			dirty(&asblk);
    237 		}
    238 	}
    239 	if (sblock->fs_fmask != ~(sblock->fs_fsize - 1)) {
    240 		pwarn("INCORRECT FMASK=0x%x IN SUPERBLOCK",
    241 			sblock->fs_fmask);
    242 		sblock->fs_fmask = ~(sblock->fs_fsize - 1);
    243 		if (preen)
    244 			printf(" (FIXED)\n");
    245 		if (preen || reply("FIX") == 1) {
    246 			sbdirty();
    247 			dirty(&asblk);
    248 		}
    249 	}
    250 	if (sblock->fs_inodefmt >= FS_44INODEFMT) {
    251 		if (sblock->fs_maxfilesize != maxfilesize) {
    252 			pwarn("INCORRECT MAXFILESIZE=%lld IN SUPERBLOCK",
    253 			    (unsigned long long)sblock->fs_maxfilesize);
    254 			sblock->fs_maxfilesize = maxfilesize;
    255 			if (preen)
    256 				printf(" (FIXED)\n");
    257 			if (preen || reply("FIX") == 1) {
    258 				sbdirty();
    259 				dirty(&asblk);
    260 			}
    261 		}
    262 		if (sblock->fs_maxsymlinklen != MAXSYMLINKLEN) {
    263 			pwarn("INCORRECT MAXSYMLINKLEN=%d IN SUPERBLOCK",
    264 				sblock->fs_maxsymlinklen);
    265 			sblock->fs_maxsymlinklen = MAXSYMLINKLEN;
    266 			if (preen)
    267 				printf(" (FIXED)\n");
    268 			if (preen || reply("FIX") == 1) {
    269 				sbdirty();
    270 				dirty(&asblk);
    271 			}
    272 		}
    273 		if (sblock->fs_qbmask != ~sblock->fs_bmask) {
    274 			pwarn("INCORRECT QBMASK=%llx IN SUPERBLOCK",
    275 			    (unsigned long long)sblock->fs_qbmask);
    276 			sblock->fs_qbmask = ~sblock->fs_bmask;
    277 			if (preen)
    278 				printf(" (FIXED)\n");
    279 			if (preen || reply("FIX") == 1) {
    280 				sbdirty();
    281 				dirty(&asblk);
    282 			}
    283 		}
    284 		if (sblock->fs_qfmask != ~sblock->fs_fmask) {
    285 			pwarn("INCORRECT QFMASK=%llx IN SUPERBLOCK",
    286 			    (unsigned long long)sblock->fs_qfmask);
    287 			sblock->fs_qfmask = ~sblock->fs_fmask;
    288 			if (preen)
    289 				printf(" (FIXED)\n");
    290 			if (preen || reply("FIX") == 1) {
    291 				sbdirty();
    292 				dirty(&asblk);
    293 			}
    294 		}
    295 		newinofmt = 1;
    296 	} else {
    297 		sblock->fs_qbmask = ~sblock->fs_bmask;
    298 		sblock->fs_qfmask = ~sblock->fs_fmask;
    299 		newinofmt = 0;
    300 	}
    301 	/*
    302 	 * Convert to new inode format.
    303 	 */
    304 	if (cvtlevel >= 2 && sblock->fs_inodefmt < FS_44INODEFMT) {
    305 		if (preen)
    306 			pwarn("CONVERTING TO NEW INODE FORMAT\n");
    307 		else if (!reply("CONVERT TO NEW INODE FORMAT"))
    308 			return(0);
    309 		doinglevel2++;
    310 		sblock->fs_inodefmt = FS_44INODEFMT;
    311 		sblock->fs_maxfilesize = maxfilesize;
    312 		sblock->fs_maxsymlinklen = MAXSYMLINKLEN;
    313 		sblock->fs_qbmask = ~sblock->fs_bmask;
    314 		sblock->fs_qfmask = ~sblock->fs_fmask;
    315 		sbdirty();
    316 		dirty(&asblk);
    317 	}
    318 	/*
    319 	 * Convert to new cylinder group format.
    320 	 */
    321 	if (cvtlevel >= 1 && sblock->fs_postblformat == FS_42POSTBLFMT) {
    322 		if (preen)
    323 			pwarn("CONVERTING TO NEW CYLINDER GROUP FORMAT\n");
    324 		else if (!reply("CONVERT TO NEW CYLINDER GROUP FORMAT"))
    325 			return(0);
    326 		doinglevel1++;
    327 		sblock->fs_postblformat = FS_DYNAMICPOSTBLFMT;
    328 		sblock->fs_nrpos = 8;
    329 		if (sblock->fs_npsect < sblock->fs_nsect)
    330 			sblock->fs_npsect = sblock->fs_nsect;
    331 		if (sblock->fs_interleave < 1)
    332 			sblock->fs_interleave = 1;
    333 		sblock->fs_postbloff =
    334 		    (char *)(&sblock->fs_opostbl[0][0]) -
    335 		    (char *)(&sblock->fs_firstfield);
    336 		sblock->fs_rotbloff = &sblock->fs_space[0] -
    337 		    (u_char *)(&sblock->fs_firstfield);
    338 		sblock->fs_cgsize =
    339 			fragroundup(sblock, CGSIZE(sblock));
    340 		sbdirty();
    341 		dirty(&asblk);
    342 	}
    343 	if (asblk.b_dirty && !bflag) {
    344 		memmove((struct fs*)sblk.b_un.b_fs, sblock, SBSIZE);
    345 		if (needswap)
    346 			ffs_sb_swap(sblock, (struct fs*)sblk.b_un.b_fs);
    347 		memmove(asblk.b_un.b_fs, sblk.b_un.b_fs, (size_t)sblock->fs_sbsize);
    348 		flush(fswritefd, &asblk);
    349 	}
    350 	/*
    351 	 * read in the summary info.
    352 	 */
    353 	asked = 0;
    354 	sblock->fs_csp = (struct csum *)calloc(1, sblock->fs_cssize);
    355 	for (i = 0, j = 0; i < sblock->fs_cssize; i += sblock->fs_bsize, j++) {
    356 		size = sblock->fs_cssize - i < sblock->fs_bsize ?
    357 		    sblock->fs_cssize - i : sblock->fs_bsize;
    358 		ccsp = (struct csum *)((char *)sblock->fs_csp + i);
    359 		if (bread(fsreadfd, (char *)ccsp,
    360 		    fsbtodb(sblock, sblock->fs_csaddr + j * sblock->fs_frag),
    361 		    size) != 0 && !asked) {
    362 			pfatal("BAD SUMMARY INFORMATION");
    363 			if (reply("CONTINUE") == 0) {
    364 				markclean = 0;
    365 				exit(EEXIT);
    366 			}
    367 			asked++;
    368 		}
    369 		if (doswap) {
    370 			ffs_csum_swap(ccsp, ccsp, size);
    371 			bwrite(fswritefd, (char *)ccsp,
    372 			    fsbtodb(sblock,
    373 				sblock->fs_csaddr + j * sblock->fs_frag),
    374 			    size);
    375 		}
    376 		if (needswap)
    377 			ffs_csum_swap(ccsp, ccsp, size);
    378 	}
    379 	/*
    380 	 * allocate and initialize the necessary maps
    381 	 */
    382 	bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
    383 	blockmap = calloc((unsigned)bmapsize, sizeof (char));
    384 	if (blockmap == NULL) {
    385 		printf("cannot alloc %u bytes for blockmap\n",
    386 		    (unsigned)bmapsize);
    387 		goto badsblabel;
    388 	}
    389 	statemap = calloc((unsigned)(maxino + 1), sizeof(char));
    390 	if (statemap == NULL) {
    391 		printf("cannot alloc %u bytes for statemap\n",
    392 		    (unsigned)(maxino + 1));
    393 		goto badsblabel;
    394 	}
    395 	typemap = calloc((unsigned)(maxino + 1), sizeof(char));
    396 	if (typemap == NULL) {
    397 		printf("cannot alloc %u bytes for typemap\n",
    398 		    (unsigned)(maxino + 1));
    399 		goto badsblabel;
    400 	}
    401 	lncntp = (int16_t *)calloc((unsigned)(maxino + 1), sizeof(int16_t));
    402 	if (lncntp == NULL) {
    403 		printf("cannot alloc %u bytes for lncntp\n",
    404 		    (unsigned)((maxino + 1) * sizeof(int16_t)));
    405 		goto badsblabel;
    406 	}
    407 	/*
    408 	 * cs_ndir may be inaccurate, particularly if we're using the -b
    409 	 * option, so set a minimum to prevent bogus subdirectory reconnects
    410 	 * and really inefficient directory scans.
    411 	 * Also set a maximum in case the value is too large.
    412 	 */
    413 	numdirs = sblock->fs_cstotal.cs_ndir;
    414 	if (numdirs < 1024)
    415 		numdirs = 1024;
    416 	if (numdirs > maxino + 1)
    417 		numdirs = maxino + 1;
    418 	inplast = 0;
    419 	listmax = numdirs + 10;
    420 	inpsort = (struct inoinfo **)calloc((unsigned)listmax,
    421 	    sizeof(struct inoinfo *));
    422 	inphead = (struct inoinfo **)calloc((unsigned)numdirs,
    423 	    sizeof(struct inoinfo *));
    424 	if (inpsort == NULL || inphead == NULL) {
    425 		printf("cannot alloc %u bytes for inphead\n",
    426 		    (unsigned)(numdirs * sizeof(struct inoinfo *)));
    427 		goto badsblabel;
    428 	}
    429 	cgrp = malloc(sblock->fs_cgsize);
    430 	if (cgrp == NULL) {
    431 		printf("cannot alloc %u bytes for cylinder group\n",
    432 		    sblock->fs_cgsize);
    433 		goto badsblabel;
    434 	}
    435 	bufinit();
    436 	if (sblock->fs_flags & FS_DOSOFTDEP)
    437 		usedsoftdep = 1;
    438 	else
    439 		usedsoftdep = 0;
    440 	return (1);
    441 
    442 badsblabel:
    443 	markclean=0;
    444 	ckfini();
    445 	return (0);
    446 }
    447 
    448 /*
    449  * Read in the super block and its summary info.
    450  */
    451 static int
    452 readsb(listerr)
    453 	int listerr;
    454 {
    455 	ufs_daddr_t super = bflag ? bflag : SBOFF / dev_bsize;
    456 	struct fs *fs;
    457 
    458 	if (bread(fsreadfd, (char *)sblk.b_un.b_fs, super, (long)SBSIZE) != 0)
    459 		return (0);
    460 	sblk.b_bno = super;
    461 	sblk.b_size = SBSIZE;
    462 
    463 	fs = sblk.b_un.b_fs;
    464 	/* auto detect byte order */
    465 	if( fs->fs_magic == FS_MAGIC) {
    466 			if (endian == 0 || BYTE_ORDER == endian) {
    467 				needswap = 0;
    468 				doswap = do_blkswap = do_dirswap = 0;
    469 			} else {
    470 				needswap = 1;
    471 				doswap = do_blkswap = do_dirswap = 1;
    472 			}
    473 	} else if (fs->fs_magic == bswap32(FS_MAGIC)) {
    474 		if (endian == 0 || BYTE_ORDER != endian) {
    475 			needswap = 1;
    476 			doswap = do_blkswap = do_dirswap = 0;
    477 		} else {
    478 			needswap = 0;
    479 			doswap = do_blkswap = do_dirswap = 1;
    480 		}
    481 	} else {
    482 		badsb(listerr, "MAGIC NUMBER WRONG");
    483 		return (0);
    484 	}
    485 	if (doswap) {
    486 		if (preen)
    487 			errx(EEXIT, "incompatible options -B and -p");
    488 		if (nflag)
    489 			errx(EEXIT, "incompatible options -B and -n");
    490 		if (endian == LITTLE_ENDIAN) {
    491 			if (!reply("CONVERT TO LITTLE ENDIAN"))
    492 				return 0;
    493 		} else if (endian == BIG_ENDIAN) {
    494 			if (!reply("CONVERT TO BIG ENDIAN"))
    495 				return 0;
    496 		} else
    497 			pfatal("INTERNAL ERROR: unknown endian");
    498 	}
    499 	if (needswap)
    500 		printf("** Swapped byte order\n");
    501 	/* swap SB byte order if asked */
    502 	if (doswap)
    503 		ffs_sb_swap(sblk.b_un.b_fs, sblk.b_un.b_fs);
    504 
    505 	memmove(sblock, sblk.b_un.b_fs, SBSIZE);
    506 	if (needswap)
    507 		ffs_sb_swap(sblk.b_un.b_fs, sblock);
    508 
    509 	/*
    510 	 * run a few consistency checks of the super block
    511 	 */
    512 	if (sblock->fs_ncg < 1)
    513 		{ badsb(listerr, "NCG OUT OF RANGE"); return (0); }
    514 	if (sblock->fs_cpg < 1)
    515 		{ badsb(listerr, "CPG OUT OF RANGE"); return (0); }
    516 	if (sblock->fs_ncg * sblock->fs_cpg < sblock->fs_ncyl ||
    517 	    (sblock->fs_ncg - 1) * sblock->fs_cpg >= sblock->fs_ncyl)
    518 		{ badsb(listerr, "NCYL LESS THAN NCG*CPG"); return (0); }
    519 	if (sblock->fs_sbsize > SBSIZE)
    520 		{ badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); }
    521 	/*
    522 	 * Compute block size that the filesystem is based on,
    523 	 * according to fsbtodb, and adjust superblock block number
    524 	 * so we can tell if this is an alternate later.
    525 	 */
    526 	super *= dev_bsize;
    527 	dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
    528 	sblk.b_bno = super / dev_bsize;
    529 
    530 	if (bflag) {
    531 		havesb = 1;
    532 		return (1);
    533 	}
    534 	/*
    535 	 * Set all possible fields that could differ, then do check
    536 	 * of whole super block against an alternate super block.
    537 	 * When an alternate super-block is specified this check is skipped.
    538 	 */
    539 	getblk(&asblk, cgsblock(sblock, sblock->fs_ncg - 1), sblock->fs_sbsize);
    540 	if (asblk.b_errs)
    541 		return (0);
    542 	/* swap SB byte order if asked */
    543 	if (doswap)
    544 		ffs_sb_swap(asblk.b_un.b_fs, asblk.b_un.b_fs);
    545 
    546 	memmove(altsblock, asblk.b_un.b_fs, sblock->fs_sbsize);
    547 	if (needswap)
    548 		ffs_sb_swap(asblk.b_un.b_fs, altsblock);
    549 	if (cmpsblks(sblock, altsblock)) {
    550 		if (debug) {
    551 			long *nlp, *olp, *endlp;
    552 
    553 			printf("superblock mismatches\n");
    554 			nlp = (long *)altsblock;
    555 			olp = (long *)sblock;
    556 			endlp = olp + (sblock->fs_sbsize / sizeof *olp);
    557 			for ( ; olp < endlp; olp++, nlp++) {
    558 				if (*olp == *nlp)
    559 					continue;
    560 				printf("offset %ld, original %lx, alternate %lx\n",
    561 				    (long)(olp - (long *)sblock), *olp, *nlp);
    562 			}
    563 		}
    564 		badsb(listerr,
    565 		"VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
    566 		return (0);
    567 	}
    568 	/* Now we know the SB is valid, we can write it back if needed */
    569 	if (doswap) {
    570 		sbdirty();
    571 		dirty(&asblk);
    572 	}
    573 	havesb = 1;
    574 	return (1);
    575 }
    576 
    577 int
    578 cmpsblks(const struct fs *sb, struct fs *asb)
    579 {
    580 
    581 	/*
    582 	 * Copy fields which we don't care if they're different in the
    583 	 * alternate superblocks, as they're either likely to be
    584 	 * different because they're per-cylinder-group specific, or
    585 	 * because they're transient details which are only maintained
    586 	 * in the primary superblock.
    587 	 */
    588 	asb->fs_firstfield = sb->fs_firstfield;
    589 	asb->fs_unused_1 = sb->fs_unused_1;
    590 	asb->fs_time = sb->fs_time;
    591 	asb->fs_cstotal = sb->fs_cstotal;
    592 	asb->fs_cgrotor = sb->fs_cgrotor;
    593 	asb->fs_fmod = sb->fs_fmod;
    594 	asb->fs_clean = sb->fs_clean;
    595 	asb->fs_ronly = sb->fs_ronly;
    596 	asb->fs_flags = sb->fs_flags;
    597 	asb->fs_maxcontig = sb->fs_maxcontig;
    598 	asb->fs_minfree = sb->fs_minfree;
    599 	asb->fs_optim = sb->fs_optim;
    600 	asb->fs_rotdelay = sb->fs_rotdelay;
    601 	asb->fs_maxbpg = sb->fs_maxbpg;
    602 	memmove(asb->fs_ocsp, sb->fs_ocsp, sizeof sb->fs_ocsp);
    603 	asb->fs_contigdirs = sb->fs_contigdirs;
    604 	asb->fs_csp = sb->fs_csp;
    605 	asb->fs_maxcluster = sb->fs_maxcluster;
    606 	memmove(asb->fs_fsmnt, sb->fs_fsmnt, sizeof sb->fs_fsmnt);
    607 	memmove(asb->fs_snapinum,
    608 		sb->fs_snapinum, sizeof sb->fs_snapinum);
    609 	asb->fs_avgfilesize = sb->fs_avgfilesize;
    610 	asb->fs_avgfpdir = sb->fs_avgfpdir;
    611 	asb->fs_pendingblocks = sb->fs_pendingblocks;
    612 	asb->fs_pendinginodes = sb->fs_pendinginodes;
    613 	memmove(asb->fs_sparecon,
    614 		sb->fs_sparecon, sizeof sb->fs_sparecon);
    615 	/*
    616 	 * The following should not have to be copied, but need to be.
    617 	 */
    618 	asb->fs_fsbtodb = sb->fs_fsbtodb;
    619 	asb->fs_interleave = sb->fs_interleave;
    620 	asb->fs_npsect = sb->fs_npsect;
    621 	asb->fs_nrpos = sb->fs_nrpos;
    622 	asb->fs_qbmask = sb->fs_qbmask;
    623 	asb->fs_qfmask = sb->fs_qfmask;
    624 	asb->fs_state = sb->fs_state;
    625 	asb->fs_maxfilesize = sb->fs_maxfilesize;
    626 
    627 	/*
    628 	 * Compare the superblocks, effectively checking every other
    629 	 * field to see if they differ.
    630 	 */
    631 	return (memcmp(sb, asb, (int)sb->fs_sbsize));
    632 }
    633 
    634 static void
    635 badsb(listerr, s)
    636 	int listerr;
    637 	char *s;
    638 {
    639 
    640 	if (!listerr)
    641 		return;
    642 	if (preen)
    643 		printf("%s: ", cdevname());
    644 	pfatal("BAD SUPER BLOCK: %s\n", s);
    645 }
    646 
    647 /*
    648  * Calculate a prototype superblock based on information in the disk label.
    649  * When done the cgsblock macro can be calculated and the fs_ncg field
    650  * can be used. Do NOT attempt to use other macros without verifying that
    651  * their needed information is available!
    652  */
    653 static int
    654 calcsb(dev, devfd, fs)
    655 	const char *dev;
    656 	int devfd;
    657 	struct fs *fs;
    658 {
    659 	struct disklabel *lp;
    660 	struct partition *pp;
    661 	char *cp;
    662 	int i;
    663 
    664 	cp = strchr(dev, '\0') - 1;
    665 	if ((cp == (char *)-1 || (*cp < 'a' || *cp > 'p')) && !isdigit(*cp)) {
    666 		pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
    667 		return (0);
    668 	}
    669 	lp = getdisklabel(dev, devfd);
    670 	if (isdigit(*cp))
    671 		pp = &lp->d_partitions[0];
    672 	else
    673 		pp = &lp->d_partitions[*cp - 'a'];
    674 	if (pp->p_fstype != FS_BSDFFS) {
    675 		pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n",
    676 			dev, pp->p_fstype < FSMAXTYPES ?
    677 			fstypenames[pp->p_fstype] : "unknown");
    678 		return (0);
    679 	}
    680 	/* avoid divide by 0 */
    681 	if (pp->p_fsize == 0 || pp->p_frag == 0)
    682 		return (0);
    683 	memset(fs, 0, sizeof(struct fs));
    684 	fs->fs_fsize = pp->p_fsize;
    685 	fs->fs_frag = pp->p_frag;
    686 	fs->fs_cpg = pp->p_cpg;
    687 	fs->fs_size = pp->p_size;
    688 	fs->fs_ntrak = lp->d_ntracks;
    689 	fs->fs_nsect = lp->d_nsectors;
    690 	fs->fs_spc = lp->d_secpercyl;
    691 	fs->fs_nspf = fs->fs_fsize / lp->d_secsize;
    692 	fs->fs_sblkno = roundup(
    693 		howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize),
    694 		fs->fs_frag);
    695 	fs->fs_cgmask = 0xffffffff;
    696 	for (i = fs->fs_ntrak; i > 1; i >>= 1)
    697 		fs->fs_cgmask <<= 1;
    698 	if (!POWEROF2(fs->fs_ntrak))
    699 		fs->fs_cgmask <<= 1;
    700 	fs->fs_cgoffset = roundup(
    701 		howmany(fs->fs_nsect, NSPF(fs)), fs->fs_frag);
    702 	fs->fs_fpg = (fs->fs_cpg * fs->fs_spc) / NSPF(fs);
    703 	fs->fs_ncg = howmany(fs->fs_size / fs->fs_spc, fs->fs_cpg);
    704 	for (fs->fs_fsbtodb = 0, i = NSPF(fs); i > 1; i >>= 1)
    705 		fs->fs_fsbtodb++;
    706 	dev_bsize = lp->d_secsize;
    707 	return (1);
    708 }
    709 
    710 static struct disklabel *
    711 getdisklabel(s, fd)
    712 	const char *s;
    713 	int	fd;
    714 {
    715 	static struct disklabel lab;
    716 
    717 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
    718 		if (s == NULL)
    719 			return ((struct disklabel *)NULL);
    720 		pwarn("ioctl (GCINFO): %s\n", strerror(errno));
    721 		errx(EEXIT, "%s: can't read disk label", s);
    722 	}
    723 	return (&lab);
    724 }
    725