Home | History | Annotate | Line # | Download | only in fsck_ffs
setup.c revision 1.41
      1 /*	$NetBSD: setup.c,v 1.41 2001/01/26 17:37:16 thorpej 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.41 2001/01/26 17:37:16 thorpej 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 struct bufarea asblk;
     70 struct fs *altsblock;
     71 #define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
     72 
     73 static void badsb __P((int, char *));
     74 static int calcsb __P((const char *, int, struct fs *));
     75 static struct disklabel *getdisklabel __P((const char *, int));
     76 static int readsb __P((int));
     77 
     78 /*
     79  * Read in a superblock finding an alternate if necessary.
     80  * Return 1 if successful, 0 if unsuccessful, -1 if filesystem
     81  * is already clean (preen mode only).
     82  */
     83 int
     84 setup(dev)
     85 	const char *dev;
     86 {
     87 	long cg, size, asked, i, j;
     88 	long bmapsize;
     89 	struct disklabel *lp;
     90 	off_t sizepb;
     91 	struct stat statb;
     92 	struct fs proto;
     93 	int doskipclean;
     94 	u_int64_t maxfilesize;
     95 
     96 	havesb = 0;
     97 	fswritefd = -1;
     98 	doskipclean = skipclean;
     99 	if (stat(dev, &statb) < 0) {
    100 		printf("Can't stat %s: %s\n", dev, strerror(errno));
    101 		return (0);
    102 	}
    103 	if (!S_ISCHR(statb.st_mode)) {
    104 		pfatal("%s is not a character device", dev);
    105 		if (reply("CONTINUE") == 0)
    106 			return (0);
    107 	}
    108 	if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
    109 		printf("Can't open %s: %s\n", dev, strerror(errno));
    110 		return (0);
    111 	}
    112 	if (preen == 0)
    113 		printf("** %s", dev);
    114 	if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) {
    115 		fswritefd = -1;
    116 		if (preen)
    117 			pfatal("NO WRITE ACCESS");
    118 		printf(" (NO WRITE)");
    119 	}
    120 	if (preen == 0)
    121 		printf("\n");
    122 	fsmodified = 0;
    123 	lfdir = 0;
    124 	initbarea(&sblk);
    125 	initbarea(&asblk);
    126 	sblk.b_un.b_buf = malloc(SBSIZE);
    127 	sblock = malloc(SBSIZE);
    128 	asblk.b_un.b_buf = malloc(SBSIZE);
    129 	altsblock = malloc(SBSIZE);
    130 	if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL ||
    131 		sblock == NULL || altsblock == NULL)
    132 		errx(EEXIT, "cannot allocate space for superblock");
    133 	if ((lp = getdisklabel(NULL, fsreadfd)) != NULL)
    134 		dev_bsize = secsize = lp->d_secsize;
    135 	else
    136 		dev_bsize = secsize = DEV_BSIZE;
    137 	/*
    138 	 * Read in the superblock, looking for alternates if necessary
    139 	 */
    140 	if (readsb(1) == 0) {
    141 		if (bflag || preen || 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_interleave < 1 ||
    203 	    sblock->fs_interleave > sblock->fs_nsect) {
    204 		pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK",
    205 			sblock->fs_interleave);
    206 		sblock->fs_interleave = 1;
    207 		if (preen)
    208 			printf(" (FIXED)\n");
    209 		if (preen || reply("SET TO DEFAULT") == 1) {
    210 			sbdirty();
    211 			dirty(&asblk);
    212 		}
    213 	}
    214 	if (sblock->fs_npsect < sblock->fs_nsect ||
    215 	    sblock->fs_npsect > sblock->fs_nsect*2) {
    216 		pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK",
    217 			sblock->fs_npsect);
    218 		sblock->fs_npsect = sblock->fs_nsect;
    219 		if (preen)
    220 			printf(" (FIXED)\n");
    221 		if (preen || reply("SET TO DEFAULT") == 1) {
    222 			sbdirty();
    223 			dirty(&asblk);
    224 		}
    225 	}
    226 	if (sblock->fs_bmask != ~(sblock->fs_bsize - 1)) {
    227 		pwarn("INCORRECT BMASK=0x%x IN SUPERBLOCK",
    228 			sblock->fs_bmask);
    229 		sblock->fs_bmask = ~(sblock->fs_bsize - 1);
    230 		if (preen)
    231 			printf(" (FIXED)\n");
    232 		if (preen || reply("FIX") == 1) {
    233 			sbdirty();
    234 			dirty(&asblk);
    235 		}
    236 	}
    237 	if (sblock->fs_fmask != ~(sblock->fs_fsize - 1)) {
    238 		pwarn("INCORRECT FMASK=0x%x IN SUPERBLOCK",
    239 			sblock->fs_fmask);
    240 		sblock->fs_fmask = ~(sblock->fs_fsize - 1);
    241 		if (preen)
    242 			printf(" (FIXED)\n");
    243 		if (preen || reply("FIX") == 1) {
    244 			sbdirty();
    245 			dirty(&asblk);
    246 		}
    247 	}
    248 	if (sblock->fs_inodefmt >= FS_44INODEFMT) {
    249 		if (sblock->fs_maxfilesize != maxfilesize) {
    250 			pwarn("INCORRECT MAXFILESIZE=%lld IN SUPERBLOCK",
    251 			    (unsigned long long)sblock->fs_maxfilesize);
    252 			sblock->fs_maxfilesize = maxfilesize;
    253 			if (preen)
    254 				printf(" (FIXED)\n");
    255 			if (preen || reply("FIX") == 1) {
    256 				sbdirty();
    257 				dirty(&asblk);
    258 			}
    259 		}
    260 		if (sblock->fs_maxsymlinklen != MAXSYMLINKLEN) {
    261 			pwarn("INCORRECT MAXSYMLINKLEN=%d IN SUPERBLOCK",
    262 				sblock->fs_maxsymlinklen);
    263 			sblock->fs_maxsymlinklen = MAXSYMLINKLEN;
    264 			if (preen)
    265 				printf(" (FIXED)\n");
    266 			if (preen || reply("FIX") == 1) {
    267 				sbdirty();
    268 				dirty(&asblk);
    269 			}
    270 		}
    271 		if (sblock->fs_qbmask != ~sblock->fs_bmask) {
    272 			pwarn("INCORRECT QBMASK=%llx IN SUPERBLOCK",
    273 			    (unsigned long long)sblock->fs_qbmask);
    274 			sblock->fs_qbmask = ~sblock->fs_bmask;
    275 			if (preen)
    276 				printf(" (FIXED)\n");
    277 			if (preen || reply("FIX") == 1) {
    278 				sbdirty();
    279 				dirty(&asblk);
    280 			}
    281 		}
    282 		if (sblock->fs_qfmask != ~sblock->fs_fmask) {
    283 			pwarn("INCORRECT QFMASK=%llx IN SUPERBLOCK",
    284 			    (unsigned long long)sblock->fs_qfmask);
    285 			sblock->fs_qfmask = ~sblock->fs_fmask;
    286 			if (preen)
    287 				printf(" (FIXED)\n");
    288 			if (preen || reply("FIX") == 1) {
    289 				sbdirty();
    290 				dirty(&asblk);
    291 			}
    292 		}
    293 		newinofmt = 1;
    294 	} else {
    295 		sblock->fs_qbmask = ~sblock->fs_bmask;
    296 		sblock->fs_qfmask = ~sblock->fs_fmask;
    297 		newinofmt = 0;
    298 	}
    299 	/*
    300 	 * Convert to new inode format.
    301 	 */
    302 	if (cvtlevel >= 2 && sblock->fs_inodefmt < FS_44INODEFMT) {
    303 		if (preen)
    304 			pwarn("CONVERTING TO NEW INODE FORMAT\n");
    305 		else if (!reply("CONVERT TO NEW INODE FORMAT"))
    306 			return(0);
    307 		doinglevel2++;
    308 		sblock->fs_inodefmt = FS_44INODEFMT;
    309 		sblock->fs_maxfilesize = maxfilesize;
    310 		sblock->fs_maxsymlinklen = MAXSYMLINKLEN;
    311 		sblock->fs_qbmask = ~sblock->fs_bmask;
    312 		sblock->fs_qfmask = ~sblock->fs_fmask;
    313 		sbdirty();
    314 		dirty(&asblk);
    315 	}
    316 	/*
    317 	 * Convert to new cylinder group format.
    318 	 */
    319 	if (cvtlevel >= 1 && sblock->fs_postblformat == FS_42POSTBLFMT) {
    320 		if (preen)
    321 			pwarn("CONVERTING TO NEW CYLINDER GROUP FORMAT\n");
    322 		else if (!reply("CONVERT TO NEW CYLINDER GROUP FORMAT"))
    323 			return(0);
    324 		doinglevel1++;
    325 		sblock->fs_postblformat = FS_DYNAMICPOSTBLFMT;
    326 		sblock->fs_nrpos = 8;
    327 		sblock->fs_postbloff =
    328 		    (char *)(&sblock->fs_opostbl[0][0]) -
    329 		    (char *)(&sblock->fs_firstfield);
    330 		sblock->fs_rotbloff = &sblock->fs_space[0] -
    331 		    (u_char *)(&sblock->fs_firstfield);
    332 		sblock->fs_cgsize =
    333 			fragroundup(sblock, CGSIZE(sblock));
    334 		sbdirty();
    335 		dirty(&asblk);
    336 	}
    337 	if (asblk.b_dirty && !bflag) {
    338 		memmove((struct fs*)sblk.b_un.b_fs, sblock, SBSIZE);
    339 		if (needswap)
    340 			ffs_sb_swap(sblock, (struct fs*)sblk.b_un.b_fs, 1);
    341 		memmove(asblk.b_un.b_fs, sblk.b_un.b_fs, (size_t)sblock->fs_sbsize);
    342 		flush(fswritefd, &asblk);
    343 	}
    344 	/*
    345 	 * read in the summary info.
    346 	 */
    347 	asked = 0;
    348 	for (i = 0, j = 0; i < sblock->fs_cssize; i += sblock->fs_bsize, j++) {
    349 		size = sblock->fs_cssize - i < sblock->fs_bsize ?
    350 		    sblock->fs_cssize - i : sblock->fs_bsize;
    351 		sblock->fs_csp[j] = (struct csum *)calloc(1, (unsigned)size);
    352 		if (bread(fsreadfd, (char *)sblock->fs_csp[j],
    353 		    fsbtodb(sblock, sblock->fs_csaddr + j * sblock->fs_frag),
    354 		    size) != 0 && !asked) {
    355 			pfatal("BAD SUMMARY INFORMATION");
    356 			if (reply("CONTINUE") == 0) {
    357 				markclean = 0;
    358 				exit(EEXIT);
    359 			}
    360 			asked++;
    361 		}
    362 		/*
    363 		 * The following assumes that struct csum is made of
    364 		 * u_int32_t
    365 		 */
    366 		if (doswap) {
    367 			int k;
    368 			u_int32_t *cd = (u_int32_t *)sblock->fs_csp[j];
    369 
    370 			for (k = 0; k < size / sizeof(u_int32_t); k++)
    371 				cd[k] = bswap32(cd[k]);
    372 			bwrite(fswritefd, (char *)sblock->fs_csp[j],
    373 			    fsbtodb(sblock,
    374 				sblock->fs_csaddr + j * sblock->fs_frag),
    375 			    size);
    376 		}
    377 		if (needswap) {
    378 			int k;
    379 			u_int32_t *cd = (u_int32_t *)sblock->fs_csp[j];
    380 
    381 			for (k = 0; k < size / sizeof(u_int32_t); k++)
    382 				cd[k] = bswap32(cd[k]);
    383 		}
    384 	}
    385 	/*
    386 	 * allocate and initialize the necessary maps
    387 	 */
    388 	bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
    389 	blockmap = calloc((unsigned)bmapsize, sizeof (char));
    390 	if (blockmap == NULL) {
    391 		printf("cannot alloc %u bytes for blockmap\n",
    392 		    (unsigned)bmapsize);
    393 		goto badsblabel;
    394 	}
    395 	statemap = calloc((unsigned)(maxino + 1), sizeof(char));
    396 	if (statemap == NULL) {
    397 		printf("cannot alloc %u bytes for statemap\n",
    398 		    (unsigned)(maxino + 1));
    399 		goto badsblabel;
    400 	}
    401 	typemap = calloc((unsigned)(maxino + 1), sizeof(char));
    402 	if (typemap == NULL) {
    403 		printf("cannot alloc %u bytes for typemap\n",
    404 		    (unsigned)(maxino + 1));
    405 		goto badsblabel;
    406 	}
    407 	lncntp = (int16_t *)calloc((unsigned)(maxino + 1), sizeof(int16_t));
    408 	if (lncntp == NULL) {
    409 		printf("cannot alloc %u bytes for lncntp\n",
    410 		    (unsigned)((maxino + 1) * sizeof(int16_t)));
    411 		goto badsblabel;
    412 	}
    413 	/*
    414 	 * cs_ndir may be inaccurate, particularly if we're using the -b
    415 	 * option, so set a minimum to prevent bogus subdirectory reconnects
    416 	 * and really inefficient directory scans.
    417 	 * Also set a maximum in case the value is too large.
    418 	 */
    419 	numdirs = sblock->fs_cstotal.cs_ndir;
    420 	if (numdirs < 1024)
    421 		numdirs = 1024;
    422 	if (numdirs > maxino + 1)
    423 		numdirs = maxino + 1;
    424 	inplast = 0;
    425 	listmax = numdirs + 10;
    426 	inpsort = (struct inoinfo **)calloc((unsigned)listmax,
    427 	    sizeof(struct inoinfo *));
    428 	inphead = (struct inoinfo **)calloc((unsigned)numdirs,
    429 	    sizeof(struct inoinfo *));
    430 	if (inpsort == NULL || inphead == NULL) {
    431 		printf("cannot alloc %u bytes for inphead\n",
    432 		    (unsigned)(numdirs * sizeof(struct inoinfo *)));
    433 		goto badsblabel;
    434 	}
    435 	cgrp = malloc(sblock->fs_cgsize);
    436 	if (cgrp == NULL) {
    437 		printf("cannot alloc %u bytes for cylinder group\n",
    438 		    sblock->fs_cgsize);
    439 		goto badsblabel;
    440 	}
    441 	bufinit();
    442 	if (sblock->fs_flags & FS_DOSOFTDEP)
    443 		usedsoftdep = 1;
    444 	else
    445 		usedsoftdep = 0;
    446 	return (1);
    447 
    448 badsblabel:
    449 	markclean=0;
    450 	ckfini();
    451 	return (0);
    452 }
    453 
    454 /*
    455  * Read in the super block and its summary info.
    456  */
    457 static int
    458 readsb(listerr)
    459 	int listerr;
    460 {
    461 	ufs_daddr_t super = bflag ? bflag : SBOFF / dev_bsize;
    462 	struct fs *fs;
    463 
    464 	if (bread(fsreadfd, (char *)sblk.b_un.b_fs, super, (long)SBSIZE) != 0)
    465 		return (0);
    466 	sblk.b_bno = super;
    467 	sblk.b_size = SBSIZE;
    468 
    469 	fs = sblk.b_un.b_fs;
    470 	/* auto detect byte order */
    471 	if( fs->fs_magic == FS_MAGIC) {
    472 			if (endian == 0 || BYTE_ORDER == endian) {
    473 				needswap = 0;
    474 				doswap = do_blkswap = do_dirswap = 0;
    475 			} else {
    476 				needswap = 1;
    477 				doswap = do_blkswap = do_dirswap = 1;
    478 			}
    479 	} else if (fs->fs_magic == bswap32(FS_MAGIC)) {
    480 		if (endian == 0 || BYTE_ORDER != endian) {
    481 			needswap = 1;
    482 			doswap = do_blkswap = do_dirswap = 0;
    483 		} else {
    484 			needswap = 0;
    485 			doswap = do_blkswap = do_dirswap = 1;
    486 		}
    487 	} else {
    488 		badsb(listerr, "MAGIC NUMBER WRONG");
    489 		return (0);
    490 	}
    491 	if (doswap) {
    492 		if (preen)
    493 			errx(EEXIT, "incompatible options -B and -p");
    494 		if (nflag)
    495 			errx(EEXIT, "incompatible options -B and -n");
    496 		if (endian == LITTLE_ENDIAN) {
    497 			if (!reply("CONVERT TO LITTLE ENDIAN"))
    498 				return 0;
    499 		} else if (endian == BIG_ENDIAN) {
    500 			if (!reply("CONVERT TO BIG ENDIAN"))
    501 				return 0;
    502 		} else
    503 			pfatal("INTERNAL ERROR: unknown endian");
    504 	}
    505 	if (needswap)
    506 		printf("** Swapped byte order\n");
    507 	/* swap SB byte order if asked */
    508 	if (doswap)
    509 		ffs_sb_swap(sblk.b_un.b_fs, sblk.b_un.b_fs, needswap);
    510 
    511 	memmove(sblock, sblk.b_un.b_fs, SBSIZE);
    512 	if (needswap)
    513 		ffs_sb_swap(sblk.b_un.b_fs, sblock, 0);
    514 
    515 	/*
    516 	 * run a few consistency checks of the super block
    517 	 */
    518 	if (sblock->fs_ncg < 1)
    519 		{ badsb(listerr, "NCG OUT OF RANGE"); return (0); }
    520 	if (sblock->fs_cpg < 1)
    521 		{ badsb(listerr, "CPG OUT OF RANGE"); return (0); }
    522 	if (sblock->fs_ncg * sblock->fs_cpg < sblock->fs_ncyl ||
    523 	    (sblock->fs_ncg - 1) * sblock->fs_cpg >= sblock->fs_ncyl)
    524 		{ badsb(listerr, "NCYL LESS THAN NCG*CPG"); return (0); }
    525 	if (sblock->fs_sbsize > SBSIZE)
    526 		{ badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); }
    527 	/*
    528 	 * Compute block size that the filesystem is based on,
    529 	 * according to fsbtodb, and adjust superblock block number
    530 	 * so we can tell if this is an alternate later.
    531 	 */
    532 	super *= dev_bsize;
    533 	dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
    534 	sblk.b_bno = super / dev_bsize;
    535 
    536 	if (bflag) {
    537 		havesb = 1;
    538 		return (1);
    539 	}
    540 	/*
    541 	 * Set all possible fields that could differ, then do check
    542 	 * of whole super block against an alternate super block.
    543 	 * When an alternate super-block is specified this check is skipped.
    544 	 */
    545 	getblk(&asblk, cgsblock(sblock, sblock->fs_ncg - 1), sblock->fs_sbsize);
    546 	if (asblk.b_errs)
    547 		return (0);
    548 	/* swap SB byte order if asked */
    549 	if (doswap)
    550 		ffs_sb_swap(asblk.b_un.b_fs, asblk.b_un.b_fs, needswap);
    551 
    552 	memmove(altsblock, asblk.b_un.b_fs, sblock->fs_sbsize);
    553 	if (needswap)
    554 		ffs_sb_swap(asblk.b_un.b_fs, altsblock, 0);
    555 	if (cmpsblks(sblock, altsblock)) {
    556 		if (debug) {
    557 			long *nlp, *olp, *endlp;
    558 
    559 			printf("superblock mismatches\n");
    560 			nlp = (long *)altsblock;
    561 			olp = (long *)sblock;
    562 			endlp = olp + (sblock->fs_sbsize / sizeof *olp);
    563 			for ( ; olp < endlp; olp++, nlp++) {
    564 				if (*olp == *nlp)
    565 					continue;
    566 				printf("offset %ld, original %lx, alternate %lx\n",
    567 				    (long)(olp - (long *)sblock), *olp, *nlp);
    568 			}
    569 		}
    570 		badsb(listerr,
    571 		"VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
    572 		return (0);
    573 	}
    574 	/* Now we know the SB is valid, we can write it back if needed */
    575 	if (doswap) {
    576 		sbdirty();
    577 		dirty(&asblk);
    578 	}
    579 	havesb = 1;
    580 	return (1);
    581 }
    582 
    583 int
    584 cmpsblks(const struct fs *sb, struct fs *asb)
    585 {
    586 
    587 	asb->fs_firstfield = sb->fs_firstfield;
    588 	asb->fs_fscktime = sb->fs_fscktime;
    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_csp, sb->fs_csp, sizeof sb->fs_csp);
    603 	asb->fs_maxcluster = sb->fs_maxcluster;
    604 	memmove(asb->fs_fsmnt, sb->fs_fsmnt, sizeof sb->fs_fsmnt);
    605 	memmove(asb->fs_sparecon,
    606 		sb->fs_sparecon, sizeof sb->fs_sparecon);
    607 	/*
    608 	 * The following should not have to be copied.
    609 	 */
    610 	asb->fs_fsbtodb = sb->fs_fsbtodb;
    611 	asb->fs_interleave = sb->fs_interleave;
    612 	asb->fs_npsect = sb->fs_npsect;
    613 	asb->fs_nrpos = sb->fs_nrpos;
    614 	asb->fs_state = sb->fs_state;
    615 	asb->fs_qbmask = sb->fs_qbmask;
    616 	asb->fs_qfmask = sb->fs_qfmask;
    617 	asb->fs_state = sb->fs_state;
    618 	asb->fs_maxfilesize = sb->fs_maxfilesize;
    619 
    620 	return (memcmp(sb, asb, (int)sb->fs_sbsize));
    621 }
    622 
    623 static void
    624 badsb(listerr, s)
    625 	int listerr;
    626 	char *s;
    627 {
    628 
    629 	if (!listerr)
    630 		return;
    631 	if (preen)
    632 		printf("%s: ", cdevname());
    633 	pfatal("BAD SUPER BLOCK: %s\n", s);
    634 }
    635 
    636 /*
    637  * Calculate a prototype superblock based on information in the disk label.
    638  * When done the cgsblock macro can be calculated and the fs_ncg field
    639  * can be used. Do NOT attempt to use other macros without verifying that
    640  * their needed information is available!
    641  */
    642 static int
    643 calcsb(dev, devfd, fs)
    644 	const char *dev;
    645 	int devfd;
    646 	struct fs *fs;
    647 {
    648 	struct disklabel *lp;
    649 	struct partition *pp;
    650 	char *cp;
    651 	int i;
    652 
    653 	cp = strchr(dev, '\0') - 1;
    654 	if ((cp == (char *)-1 || (*cp < 'a' || *cp > 'h')) && !isdigit(*cp)) {
    655 		pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
    656 		return (0);
    657 	}
    658 	lp = getdisklabel(dev, devfd);
    659 	if (isdigit(*cp))
    660 		pp = &lp->d_partitions[0];
    661 	else
    662 		pp = &lp->d_partitions[*cp - 'a'];
    663 	if (pp->p_fstype != FS_BSDFFS) {
    664 		pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n",
    665 			dev, pp->p_fstype < FSMAXTYPES ?
    666 			fstypenames[pp->p_fstype] : "unknown");
    667 		return (0);
    668 	}
    669 	/* avoid divide by 0 */
    670 	if (pp->p_fsize == 0 || pp->p_frag == 0)
    671 		return (0);
    672 	memset(fs, 0, sizeof(struct fs));
    673 	fs->fs_fsize = pp->p_fsize;
    674 	fs->fs_frag = pp->p_frag;
    675 	fs->fs_cpg = pp->p_cpg;
    676 	fs->fs_size = pp->p_size;
    677 	fs->fs_ntrak = lp->d_ntracks;
    678 	fs->fs_nsect = lp->d_nsectors;
    679 	fs->fs_spc = lp->d_secpercyl;
    680 	fs->fs_nspf = fs->fs_fsize / lp->d_secsize;
    681 	fs->fs_sblkno = roundup(
    682 		howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize),
    683 		fs->fs_frag);
    684 	fs->fs_cgmask = 0xffffffff;
    685 	for (i = fs->fs_ntrak; i > 1; i >>= 1)
    686 		fs->fs_cgmask <<= 1;
    687 	if (!POWEROF2(fs->fs_ntrak))
    688 		fs->fs_cgmask <<= 1;
    689 	fs->fs_cgoffset = roundup(
    690 		howmany(fs->fs_nsect, NSPF(fs)), fs->fs_frag);
    691 	fs->fs_fpg = (fs->fs_cpg * fs->fs_spc) / NSPF(fs);
    692 	fs->fs_ncg = howmany(fs->fs_size / fs->fs_spc, fs->fs_cpg);
    693 	for (fs->fs_fsbtodb = 0, i = NSPF(fs); i > 1; i >>= 1)
    694 		fs->fs_fsbtodb++;
    695 	dev_bsize = lp->d_secsize;
    696 	return (1);
    697 }
    698 
    699 static struct disklabel *
    700 getdisklabel(s, fd)
    701 	const char *s;
    702 	int	fd;
    703 {
    704 	static struct disklabel lab;
    705 
    706 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
    707 		if (s == NULL)
    708 			return ((struct disklabel *)NULL);
    709 		pwarn("ioctl (GCINFO): %s\n", strerror(errno));
    710 		errx(EEXIT, "%s: can't read disk label", s);
    711 	}
    712 	return (&lab);
    713 }
    714