Home | History | Annotate | Line # | Download | only in fsck_ffs
setup.c revision 1.35
      1 /*	$NetBSD: setup.c,v 1.35 1998/11/12 16:19:48 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. 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.35 1998/11/12 16:19:48 christos 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 #ifdef LITE2BORKEN
    142 		skipclean = 0;
    143 #endif
    144 		if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
    145 			return(0);
    146 		if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
    147 			return (0);
    148 		for (cg = 0; cg < proto.fs_ncg; cg++) {
    149 			bflag = fsbtodb(&proto, cgsblock(&proto, cg));
    150 			if (readsb(0) != 0)
    151 				break;
    152 		}
    153 		if (cg >= proto.fs_ncg) {
    154 			printf("%s %s\n%s %s\n%s %s\n",
    155 				"SEARCH FOR ALTERNATE SUPER-BLOCK",
    156 				"FAILED. YOU MUST USE THE",
    157 				"-b OPTION TO FSCK_FFS TO SPECIFY THE",
    158 				"LOCATION OF AN ALTERNATE",
    159 				"SUPER-BLOCK TO SUPPLY NEEDED",
    160 				"INFORMATION; SEE fsck_ffs(8).");
    161 			return(0);
    162 		}
    163 		doskipclean = 0;
    164 		pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
    165 	}
    166 	if (debug)
    167 		printf("clean = %d\n", sblock->fs_clean);
    168 	if (doswap)
    169 		doskipclean = 0;
    170 	if (sblock->fs_clean & FS_ISCLEAN) {
    171 		if (doskipclean) {
    172 			pwarn("%sile system is clean; not checking\n",
    173 			    preen ? "f" : "** F");
    174 			return (-1);
    175 		}
    176 		if (!preen && !doswap)
    177 			pwarn("** File system is already clean\n");
    178 	}
    179 	maxfsblock = sblock->fs_size;
    180 	maxino = sblock->fs_ncg * sblock->fs_ipg;
    181 	sizepb = sblock->fs_bsize;
    182 	maxfilesize = sblock->fs_bsize * NDADDR - 1;
    183 	for (i = 0; i < NIADDR; i++) {
    184 		sizepb *= NINDIR(sblock);
    185 		maxfilesize += sizepb;
    186 	}
    187 	/*
    188 	 * Check and potentially fix certain fields in the super block.
    189 	 */
    190 	if (sblock->fs_optim != FS_OPTTIME && sblock->fs_optim != FS_OPTSPACE) {
    191 		pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
    192 		if (reply("SET TO DEFAULT") == 1) {
    193 			sblock->fs_optim = FS_OPTTIME;
    194 			sbdirty();
    195 		}
    196 	}
    197 	if ((sblock->fs_minfree < 0 || sblock->fs_minfree > 99)) {
    198 		pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
    199 			sblock->fs_minfree);
    200 		if (reply("SET TO DEFAULT") == 1) {
    201 			sblock->fs_minfree = 10;
    202 			sbdirty();
    203 		}
    204 	}
    205 	if (sblock->fs_interleave < 1 ||
    206 	    sblock->fs_interleave > sblock->fs_nsect) {
    207 		pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK",
    208 			sblock->fs_interleave);
    209 		sblock->fs_interleave = 1;
    210 		if (preen)
    211 			printf(" (FIXED)\n");
    212 		if (preen || reply("SET TO DEFAULT") == 1) {
    213 			sbdirty();
    214 			dirty(&asblk);
    215 		}
    216 	}
    217 	if (sblock->fs_npsect < sblock->fs_nsect ||
    218 	    sblock->fs_npsect > sblock->fs_nsect*2) {
    219 		pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK",
    220 			sblock->fs_npsect);
    221 		sblock->fs_npsect = sblock->fs_nsect;
    222 		if (preen)
    223 			printf(" (FIXED)\n");
    224 		if (preen || reply("SET TO DEFAULT") == 1) {
    225 			sbdirty();
    226 			dirty(&asblk);
    227 		}
    228 	}
    229 	if (sblock->fs_bmask != ~(sblock->fs_bsize - 1)) {
    230 		pwarn("INCORRECT BMASK=0x%x IN SUPERBLOCK",
    231 			sblock->fs_bmask);
    232 		sblock->fs_bmask = ~(sblock->fs_bsize - 1);
    233 		if (preen)
    234 			printf(" (FIXED)\n");
    235 		if (preen || reply("FIX") == 1) {
    236 			sbdirty();
    237 			dirty(&asblk);
    238 		}
    239 	}
    240 	if (sblock->fs_fmask != ~(sblock->fs_fsize - 1)) {
    241 		pwarn("INCORRECT FMASK=0x%x IN SUPERBLOCK",
    242 			sblock->fs_fmask);
    243 		sblock->fs_fmask = ~(sblock->fs_fsize - 1);
    244 		if (preen)
    245 			printf(" (FIXED)\n");
    246 		if (preen || reply("FIX") == 1) {
    247 			sbdirty();
    248 			dirty(&asblk);
    249 		}
    250 	}
    251 	if (sblock->fs_inodefmt >= FS_44INODEFMT) {
    252 		if (sblock->fs_maxfilesize != maxfilesize) {
    253 			pwarn("INCORRECT MAXFILESIZE=%qd IN SUPERBLOCK",
    254 			    (unsigned long long)sblock->fs_maxfilesize);
    255 			sblock->fs_maxfilesize = maxfilesize;
    256 			if (preen)
    257 				printf(" (FIXED)\n");
    258 			if (preen || reply("FIX") == 1) {
    259 				sbdirty();
    260 				dirty(&asblk);
    261 			}
    262 		}
    263 		if (sblock->fs_maxsymlinklen != MAXSYMLINKLEN) {
    264 			pwarn("INCORRECT MAXSYMLINKLEN=%d IN SUPERBLOCK",
    265 				sblock->fs_maxsymlinklen);
    266 			sblock->fs_maxsymlinklen = MAXSYMLINKLEN;
    267 			if (preen)
    268 				printf(" (FIXED)\n");
    269 			if (preen || reply("FIX") == 1) {
    270 				sbdirty();
    271 				dirty(&asblk);
    272 			}
    273 		}
    274 		if (sblock->fs_qbmask != ~sblock->fs_bmask) {
    275 			pwarn("INCORRECT QBMASK=%qx IN SUPERBLOCK",
    276 			    (unsigned long long)sblock->fs_qbmask);
    277 			sblock->fs_qbmask = ~sblock->fs_bmask;
    278 			if (preen)
    279 				printf(" (FIXED)\n");
    280 			if (preen || reply("FIX") == 1) {
    281 				sbdirty();
    282 				dirty(&asblk);
    283 			}
    284 		}
    285 		if (sblock->fs_qfmask != ~sblock->fs_fmask) {
    286 			pwarn("INCORRECT QFMASK=%qx IN SUPERBLOCK",
    287 			    (unsigned long long)sblock->fs_qfmask);
    288 			sblock->fs_qfmask = ~sblock->fs_fmask;
    289 			if (preen)
    290 				printf(" (FIXED)\n");
    291 			if (preen || reply("FIX") == 1) {
    292 				sbdirty();
    293 				dirty(&asblk);
    294 			}
    295 		}
    296 		newinofmt = 1;
    297 	} else {
    298 		sblock->fs_qbmask = ~sblock->fs_bmask;
    299 		sblock->fs_qfmask = ~sblock->fs_fmask;
    300 		newinofmt = 0;
    301 	}
    302 	/*
    303 	 * Convert to new inode format.
    304 	 */
    305 	if (cvtlevel >= 2 && sblock->fs_inodefmt < FS_44INODEFMT) {
    306 		if (preen)
    307 			pwarn("CONVERTING TO NEW INODE FORMAT\n");
    308 		else if (!reply("CONVERT TO NEW INODE FORMAT"))
    309 			return(0);
    310 		doinglevel2++;
    311 		sblock->fs_inodefmt = FS_44INODEFMT;
    312 		sblock->fs_maxfilesize = maxfilesize;
    313 		sblock->fs_maxsymlinklen = MAXSYMLINKLEN;
    314 		sblock->fs_qbmask = ~sblock->fs_bmask;
    315 		sblock->fs_qfmask = ~sblock->fs_fmask;
    316 		sbdirty();
    317 		dirty(&asblk);
    318 	}
    319 	/*
    320 	 * Convert to new cylinder group format.
    321 	 */
    322 	if (cvtlevel >= 1 && sblock->fs_postblformat == FS_42POSTBLFMT) {
    323 		if (preen)
    324 			pwarn("CONVERTING TO NEW CYLINDER GROUP FORMAT\n");
    325 		else if (!reply("CONVERT TO NEW CYLINDER GROUP FORMAT"))
    326 			return(0);
    327 		doinglevel1++;
    328 		sblock->fs_postblformat = FS_DYNAMICPOSTBLFMT;
    329 		sblock->fs_nrpos = 8;
    330 		sblock->fs_postbloff =
    331 		    (char *)(&sblock->fs_opostbl[0][0]) -
    332 		    (char *)(&sblock->fs_firstfield);
    333 		sblock->fs_rotbloff = &sblock->fs_space[0] -
    334 		    (u_char *)(&sblock->fs_firstfield);
    335 		sblock->fs_cgsize =
    336 			fragroundup(sblock, CGSIZE(sblock));
    337 		sbdirty();
    338 		dirty(&asblk);
    339 	}
    340 	if (asblk.b_dirty && !bflag) {
    341 		memmove((struct fs*)sblk.b_un.b_fs, sblock, SBSIZE);
    342 		if (needswap)
    343 			ffs_sb_swap(sblock, (struct fs*)sblk.b_un.b_fs, 1);
    344 		memmove(asblk.b_un.b_fs, sblk.b_un.b_fs, (size_t)sblock->fs_sbsize);
    345 		flush(fswritefd, &asblk);
    346 	}
    347 	/*
    348 	 * read in the summary info.
    349 	 */
    350 	asked = 0;
    351 	for (i = 0, j = 0; i < sblock->fs_cssize; i += sblock->fs_bsize, j++) {
    352 		size = sblock->fs_cssize - i < sblock->fs_bsize ?
    353 		    sblock->fs_cssize - i : sblock->fs_bsize;
    354 		sblock->fs_csp[j] = (struct csum *)calloc(1, (unsigned)size);
    355 		if (bread(fsreadfd, (char *)sblock->fs_csp[j],
    356 		    fsbtodb(sblock, sblock->fs_csaddr + j * sblock->fs_frag),
    357 		    size) != 0 && !asked) {
    358 			pfatal("BAD SUMMARY INFORMATION");
    359 			if (reply("CONTINUE") == 0)
    360 				exit(EEXIT);
    361 			asked++;
    362 		}
    363 		/*
    364 		 * The following routines assumes that struct csum is made of
    365 		 * u_int32_t's
    366 		 */
    367 		if (doswap) {
    368 			int k;
    369 			u_int32_t *cd = (u_int32_t *)sblock->fs_csp[j];
    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, sblock->fs_csaddr + j * sblock->fs_frag),
    374 				size);
    375 		}
    376 		if (needswap) {
    377 			int k;
    378 			u_int32_t *cd = (u_int32_t *)sblock->fs_csp[j];
    379 			for (k = 0; k < size / sizeof(u_int32_t); k++)
    380 				cd[k] = bswap32(cd[k]);
    381 		}
    382 	}
    383 	/*
    384 	 * allocate and initialize the necessary maps
    385 	 */
    386 	bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
    387 	blockmap = calloc((unsigned)bmapsize, sizeof (char));
    388 	if (blockmap == NULL) {
    389 		printf("cannot alloc %u bytes for blockmap\n",
    390 		    (unsigned)bmapsize);
    391 		goto badsblabel;
    392 	}
    393 	statemap = calloc((unsigned)(maxino + 1), sizeof(char));
    394 	if (statemap == NULL) {
    395 		printf("cannot alloc %u bytes for statemap\n",
    396 		    (unsigned)(maxino + 1));
    397 		goto badsblabel;
    398 	}
    399 	typemap = calloc((unsigned)(maxino + 1), sizeof(char));
    400 	if (typemap == NULL) {
    401 		printf("cannot alloc %u bytes for typemap\n",
    402 		    (unsigned)(maxino + 1));
    403 		goto badsblabel;
    404 	}
    405 	lncntp = (int16_t *)calloc((unsigned)(maxino + 1), sizeof(int16_t));
    406 	if (lncntp == NULL) {
    407 		printf("cannot alloc %u bytes for lncntp\n",
    408 		    (unsigned)((maxino + 1) * sizeof(int16_t)));
    409 		goto badsblabel;
    410 	}
    411 	numdirs = sblock->fs_cstotal.cs_ndir;
    412 	inplast = 0;
    413 	listmax = numdirs + 10;
    414 	inpsort = (struct inoinfo **)calloc((unsigned)listmax,
    415 	    sizeof(struct inoinfo *));
    416 	inphead = (struct inoinfo **)calloc((unsigned)numdirs,
    417 	    sizeof(struct inoinfo *));
    418 	if (inpsort == NULL || inphead == NULL) {
    419 		printf("cannot alloc %u bytes for inphead\n",
    420 		    (unsigned)(numdirs * sizeof(struct inoinfo *)));
    421 		goto badsblabel;
    422 	}
    423 	bufinit();
    424 	return (1);
    425 
    426 badsblabel:
    427 	markclean=0;
    428 	ckfini();
    429 	return (0);
    430 }
    431 
    432 /*
    433  * Read in the super block and its summary info.
    434  */
    435 static int
    436 readsb(listerr)
    437 	int listerr;
    438 {
    439 	ufs_daddr_t super = bflag ? bflag : SBOFF / dev_bsize;
    440 	struct fs *fs;
    441 
    442 	if (bread(fsreadfd, (char *)sblk.b_un.b_fs, super, (long)SBSIZE) != 0)
    443 		return (0);
    444 	sblk.b_bno = super;
    445 	sblk.b_size = SBSIZE;
    446 
    447 	fs = sblk.b_un.b_fs;
    448 	/* auto detect byte order */
    449 	if( fs->fs_magic == FS_MAGIC) {
    450 			if (endian == 0 || BYTE_ORDER == endian) {
    451 				needswap = 0;
    452 				doswap = do_blkswap = do_dirswap = 0;
    453 			} else {
    454 				needswap = 1;
    455 				doswap = do_blkswap = do_dirswap = 1;
    456 			}
    457 	} else if (fs->fs_magic == bswap32(FS_MAGIC)) {
    458 		if (endian == 0 || BYTE_ORDER != endian) {
    459 			needswap = 1;
    460 			doswap = do_blkswap = do_dirswap = 0;
    461 		} else {
    462 			needswap = 0;
    463 			doswap = do_blkswap = do_dirswap = 1;
    464 		}
    465 	} else {
    466 		badsb(listerr, "MAGIC NUMBER WRONG");
    467 		return (0);
    468 	}
    469 	if (doswap) {
    470 		if (preen)
    471 			errx(EEXIT, "incompatible options -B and -p");
    472 		if (nflag)
    473 			errx(EEXIT, "incompatible options -B and -n");
    474 		if (endian == LITTLE_ENDIAN) {
    475 			if (!reply("CONVERTING TO LITTLE ENDIAN"))
    476 				return 0;
    477 		} else if (endian == BIG_ENDIAN) {
    478 			if (!reply("CONVERTING TO BIG ENDIAN"))
    479 				return 0;
    480 		} else
    481 			pfatal("INTERNAL ERROR: unknown endian");
    482 	}
    483 	if (needswap)
    484 		printf("** Swapped byte order\n");
    485 	/* swap SB byte order if asked */
    486 	if (doswap)
    487 		ffs_sb_swap(sblk.b_un.b_fs, sblk.b_un.b_fs, needswap);
    488 
    489 	memmove(sblock, sblk.b_un.b_fs, SBSIZE);
    490 	if (needswap)
    491 		ffs_sb_swap(sblk.b_un.b_fs, sblock, 0);
    492 
    493 	/*
    494 	 * run a few consistency checks of the super block
    495 	 */
    496 	if (sblock->fs_ncg < 1)
    497 		{ badsb(listerr, "NCG OUT OF RANGE"); return (0); }
    498 	if (sblock->fs_cpg < 1)
    499 		{ badsb(listerr, "CPG OUT OF RANGE"); return (0); }
    500 	if (sblock->fs_ncg * sblock->fs_cpg < sblock->fs_ncyl ||
    501 	    (sblock->fs_ncg - 1) * sblock->fs_cpg >= sblock->fs_ncyl)
    502 		{ badsb(listerr, "NCYL LESS THAN NCG*CPG"); return (0); }
    503 	if (sblock->fs_sbsize > SBSIZE)
    504 		{ badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); }
    505 	/*
    506 	 * Compute block size that the filesystem is based on,
    507 	 * according to fsbtodb, and adjust superblock block number
    508 	 * so we can tell if this is an alternate later.
    509 	 */
    510 	super *= dev_bsize;
    511 	dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
    512 	sblk.b_bno = super / dev_bsize;
    513 
    514 	if (bflag) {
    515 		havesb = 1;
    516 		return (1);
    517 	}
    518 	/*
    519 	 * Set all possible fields that could differ, then do check
    520 	 * of whole super block against an alternate super block.
    521 	 * When an alternate super-block is specified this check is skipped.
    522 	 */
    523 	getblk(&asblk, cgsblock(sblock, sblock->fs_ncg - 1), sblock->fs_sbsize);
    524 	if (asblk.b_errs)
    525 		return (0);
    526 	/* swap SB byte order if asked */
    527 	if (doswap)
    528 		ffs_sb_swap(asblk.b_un.b_fs, asblk.b_un.b_fs, needswap);
    529 
    530 	memmove(altsblock, asblk.b_un.b_fs, sblock->fs_sbsize);
    531 	if (needswap)
    532 		ffs_sb_swap(asblk.b_un.b_fs, altsblock, 0);
    533 	altsblock->fs_firstfield = sblock->fs_firstfield;
    534 	altsblock->fs_fscktime = sblock->fs_fscktime;
    535 	altsblock->fs_unused_1 = sblock->fs_unused_1;
    536 	altsblock->fs_time = sblock->fs_time;
    537 	altsblock->fs_cstotal = sblock->fs_cstotal;
    538 	altsblock->fs_cgrotor = sblock->fs_cgrotor;
    539 	altsblock->fs_fmod = sblock->fs_fmod;
    540 	altsblock->fs_clean = sblock->fs_clean;
    541 	altsblock->fs_ronly = sblock->fs_ronly;
    542 	altsblock->fs_flags = sblock->fs_flags;
    543 	altsblock->fs_maxcontig = sblock->fs_maxcontig;
    544 	altsblock->fs_minfree = sblock->fs_minfree;
    545 	altsblock->fs_optim = sblock->fs_optim;
    546 	altsblock->fs_rotdelay = sblock->fs_rotdelay;
    547 	altsblock->fs_maxbpg = sblock->fs_maxbpg;
    548 	memmove(altsblock->fs_csp, sblock->fs_csp, sizeof sblock->fs_csp);
    549 	altsblock->fs_maxcluster = sblock->fs_maxcluster;
    550 	memmove(altsblock->fs_fsmnt, sblock->fs_fsmnt, sizeof sblock->fs_fsmnt);
    551 	memmove(altsblock->fs_sparecon,
    552 		sblock->fs_sparecon, sizeof sblock->fs_sparecon);
    553 	/*
    554 	 * The following should not have to be copied.
    555 	 */
    556 	altsblock->fs_fsbtodb = sblock->fs_fsbtodb;
    557 	altsblock->fs_interleave = sblock->fs_interleave;
    558 	altsblock->fs_npsect = sblock->fs_npsect;
    559 	altsblock->fs_nrpos = sblock->fs_nrpos;
    560 	altsblock->fs_state = sblock->fs_state;
    561 	altsblock->fs_qbmask = sblock->fs_qbmask;
    562 	altsblock->fs_qfmask = sblock->fs_qfmask;
    563 	altsblock->fs_state = sblock->fs_state;
    564 	altsblock->fs_maxfilesize = sblock->fs_maxfilesize;
    565 	if (memcmp(sblock, altsblock, (int)sblock->fs_sbsize)) {
    566 		if (debug) {
    567 			long *nlp, *olp, *endlp;
    568 
    569 			printf("superblock mismatches\n");
    570 			nlp = (long *)altsblock;
    571 			olp = (long *)sblock;
    572 			endlp = olp + (sblock->fs_sbsize / sizeof *olp);
    573 			for ( ; olp < endlp; olp++, nlp++) {
    574 				if (*olp == *nlp)
    575 					continue;
    576 				printf("offset %ld, original %lx, alternate %lx\n",
    577 				    (long)(olp - (long *)sblock), *olp, *nlp);
    578 			}
    579 		}
    580 		badsb(listerr,
    581 		"VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
    582 		return (0);
    583 	}
    584 	/* Now we know the SB is valid, we can write it back if needed */
    585 	if (doswap) {
    586 		sbdirty();
    587 		dirty(&asblk);
    588 	}
    589 	havesb = 1;
    590 	return (1);
    591 }
    592 
    593 static void
    594 badsb(listerr, s)
    595 	int listerr;
    596 	char *s;
    597 {
    598 
    599 	if (!listerr)
    600 		return;
    601 	if (preen)
    602 		printf("%s: ", cdevname());
    603 	pfatal("BAD SUPER BLOCK: %s\n", s);
    604 }
    605 
    606 /*
    607  * Calculate a prototype superblock based on information in the disk label.
    608  * When done the cgsblock macro can be calculated and the fs_ncg field
    609  * can be used. Do NOT attempt to use other macros without verifying that
    610  * their needed information is available!
    611  */
    612 static int
    613 calcsb(dev, devfd, fs)
    614 	const char *dev;
    615 	int devfd;
    616 	struct fs *fs;
    617 {
    618 	struct disklabel *lp;
    619 	struct partition *pp;
    620 	char *cp;
    621 	int i;
    622 
    623 	cp = strchr(dev, '\0') - 1;
    624 	if ((cp == (char *)-1 || (*cp < 'a' || *cp > 'h')) && !isdigit(*cp)) {
    625 		pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
    626 		return (0);
    627 	}
    628 	lp = getdisklabel(dev, devfd);
    629 	if (isdigit(*cp))
    630 		pp = &lp->d_partitions[0];
    631 	else
    632 		pp = &lp->d_partitions[*cp - 'a'];
    633 	if (pp->p_fstype != FS_BSDFFS) {
    634 		pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n",
    635 			dev, pp->p_fstype < FSMAXTYPES ?
    636 			fstypenames[pp->p_fstype] : "unknown");
    637 		return (0);
    638 	}
    639 	/* avoid divide by 0 */
    640 	if (pp->p_fsize == 0 || pp->p_frag == 0)
    641 		return (0);
    642 	memset(fs, 0, sizeof(struct fs));
    643 	fs->fs_fsize = pp->p_fsize;
    644 	fs->fs_frag = pp->p_frag;
    645 	fs->fs_cpg = pp->p_cpg;
    646 	fs->fs_size = pp->p_size;
    647 	fs->fs_ntrak = lp->d_ntracks;
    648 	fs->fs_nsect = lp->d_nsectors;
    649 	fs->fs_spc = lp->d_secpercyl;
    650 	fs->fs_nspf = fs->fs_fsize / lp->d_secsize;
    651 	fs->fs_sblkno = roundup(
    652 		howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize),
    653 		fs->fs_frag);
    654 	fs->fs_cgmask = 0xffffffff;
    655 	for (i = fs->fs_ntrak; i > 1; i >>= 1)
    656 		fs->fs_cgmask <<= 1;
    657 	if (!POWEROF2(fs->fs_ntrak))
    658 		fs->fs_cgmask <<= 1;
    659 	fs->fs_cgoffset = roundup(
    660 		howmany(fs->fs_nsect, NSPF(fs)), fs->fs_frag);
    661 	fs->fs_fpg = (fs->fs_cpg * fs->fs_spc) / NSPF(fs);
    662 	fs->fs_ncg = howmany(fs->fs_size / fs->fs_spc, fs->fs_cpg);
    663 	for (fs->fs_fsbtodb = 0, i = NSPF(fs); i > 1; i >>= 1)
    664 		fs->fs_fsbtodb++;
    665 	dev_bsize = lp->d_secsize;
    666 	return (1);
    667 }
    668 
    669 static struct disklabel *
    670 getdisklabel(s, fd)
    671 	const char *s;
    672 	int	fd;
    673 {
    674 	static struct disklabel lab;
    675 
    676 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
    677 		if (s == NULL)
    678 			return ((struct disklabel *)NULL);
    679 		pwarn("ioctl (GCINFO): %s\n", strerror(errno));
    680 		errx(EEXIT, "%s: can't read disk label", s);
    681 	}
    682 	return (&lab);
    683 }
    684