Home | History | Annotate | Line # | Download | only in fsck_lfs
setup.c revision 1.7
      1 /* $NetBSD: setup.c,v 1.7 2000/10/04 11:37:54 jdolecek 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 /* #define DKTYPENAMES */
     37 #define FSTYPENAMES
     38 #include <sys/param.h>
     39 #include <sys/time.h>
     40 #include <sys/stat.h>
     41 #include <sys/ioctl.h>
     42 #include <sys/disklabel.h>
     43 #include <sys/file.h>
     44 
     45 #include <ufs/ufs/dinode.h>
     46 #include <sys/mount.h>		/* XXX ufs/lfs/lfs.h should include this for
     47 				 * us */
     48 #include <ufs/lfs/lfs.h>
     49 #include <ufs/lfs/lfs_extern.h>
     50 
     51 #include <ctype.h>
     52 #include <err.h>
     53 #include <errno.h>
     54 #include <stdio.h>
     55 #include <stdlib.h>
     56 #include <string.h>
     57 
     58 #include "fsck.h"
     59 #include "extern.h"
     60 #include "fsutil.h"
     61 
     62 struct bufarea  asblk;
     63 daddr_t        *din_table;
     64 SEGUSE         *seg_table;
     65 #define altsblock (*asblk.b_un.b_fs)
     66 #define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
     67 
     68 void            badsb(int, char *);
     69 int             calcsb(const char *, int, struct lfs *);
     70 static struct disklabel *getdisklabel(const char *, int);
     71 static int      readsb(int);
     72 int             lfs_maxino(void);
     73 
     74 static daddr_t  try_verify(struct lfs *, struct lfs *);
     75 
     76 #ifdef DKTYPENAMES
     77 int             useless(void);
     78 
     79 int
     80 useless(void)
     81 {
     82 	char          **foo = (char **)dktypenames;
     83 	char          **bar = (char **)fscknames;
     84 
     85 	return foo - bar;
     86 }
     87 #endif
     88 
     89 static daddr_t
     90 try_verify(struct lfs * osb, struct lfs * nsb)
     91 {
     92 	daddr_t         daddr;
     93 	SEGSUM         *sp;
     94 	char            summary[LFS_SUMMARY_SIZE];
     95 	int             bc, flag;
     96 
     97 	daddr = osb->lfs_offset;
     98 	while (daddr != nsb->lfs_offset) {
     99 		flag = 0;
    100 oncemore:
    101 		/* Read in summary block */
    102 		bread(fsreadfd, summary, daddr, LFS_SUMMARY_SIZE);
    103 		sp = (SEGSUM *)summary;
    104 
    105 		/*
    106 		 * Could be a superblock instead of a segment summary.
    107 		 * XXX should use gseguse, but right now we need to do more
    108 		 * setup before we can...fix this
    109 		 */
    110 		if (sp->ss_magic != SS_MAGIC ||
    111 		  sp->ss_sumsum != cksum(&sp->ss_datasum, LFS_SUMMARY_SIZE -
    112 					 sizeof(sp->ss_sumsum))) {
    113 			if (flag == 0) {
    114 				daddr += LFS_SBPAD / dev_bsize;
    115 				goto oncemore;
    116 			}
    117 			return 0x0;
    118 		}
    119 		bc = check_summary(osb, sp, daddr);
    120 		if (bc == 0)
    121 			break;
    122 		daddr += (LFS_SUMMARY_SIZE + bc) / dev_bsize;
    123 		if (datosn(osb, daddr) != datosn(osb, daddr + (LFS_SUMMARY_SIZE + (1 << osb->lfs_bshift)) / dev_bsize)) {
    124 			daddr = ((SEGSUM *)summary)->ss_next;
    125 		}
    126 	}
    127 	return daddr;
    128 }
    129 
    130 int
    131 setup(const char *dev)
    132 {
    133 	long            bmapsize;
    134 	struct disklabel *lp;
    135 #if 0
    136 	long            i;
    137 	off_t           sizepb;
    138 #endif
    139 	struct stat     statb;
    140 	daddr_t         daddr;
    141 	struct lfs      proto;
    142 	int             doskipclean;
    143 	u_int64_t       maxfilesize;
    144 	struct lfs     *sb0, *sb1, *osb, *nsb;
    145 	struct dinode  *idinode;
    146 
    147 	havesb = 0;
    148 	fswritefd = -1;
    149 	doskipclean = skipclean;
    150 	if (stat(dev, &statb) < 0) {
    151 		printf("Can't stat %s: %s\n", dev, strerror(errno));
    152 		return (0);
    153 	}
    154 	if (!S_ISCHR(statb.st_mode)) {
    155 		pfatal("%s is not a character device", dev);
    156 		if (reply("CONTINUE") == 0)
    157 			return (0);
    158 	}
    159 	if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
    160 		printf("Can't open %s: %s\n", dev, strerror(errno));
    161 		return (0);
    162 	}
    163 	if (preen == 0)
    164 		printf("** %s", dev);
    165 	if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) {
    166 		fswritefd = -1;
    167 		if (preen)
    168 			pfatal("NO WRITE ACCESS");
    169 		printf(" (NO WRITE)");
    170 	}
    171 	if (preen == 0)
    172 		printf("\n");
    173 	fsmodified = 0;
    174 	lfdir = 0;
    175 	initbarea(&sblk);
    176 	initbarea(&asblk);
    177 	sblk.b_un.b_buf = malloc(LFS_SBPAD);
    178 	asblk.b_un.b_buf = malloc(LFS_SBPAD);
    179 	if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
    180 		errexit("cannot allocate space for superblock\n");
    181 	if ((lp = getdisklabel((char *) NULL, fsreadfd)) != NULL)
    182 		dev_bsize = secsize = lp->d_secsize;
    183 	else
    184 		dev_bsize = secsize = DEV_BSIZE;
    185 
    186 	/*
    187 	 * Read in the superblock, looking for alternates if necessary
    188 	 */
    189 	if (readsb(1) == 0) {
    190 		if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
    191 			return (0);
    192 		if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
    193 			return (0);
    194 #if 0				/* XXX find the LFS way to do this */
    195 		for (cg = 0; cg < proto.lfs_ncg; cg++) {
    196 			bflag = fsbtodb(&proto, cgsblock(&proto, cg));
    197 			if (readsb(0) != 0)
    198 				break;
    199 		}
    200 		if (cg >= proto.lfs_ncg) {
    201 			printf("%s %s\n%s %s\n%s %s\n",
    202 			       "SEARCH FOR ALTERNATE SUPER-BLOCK",
    203 			       "FAILED. YOU MUST USE THE",
    204 			       "-b OPTION TO FSCK_FFS TO SPECIFY THE",
    205 			       "LOCATION OF AN ALTERNATE",
    206 			       "SUPER-BLOCK TO SUPPLY NEEDED",
    207 			       "INFORMATION; SEE fsck_ffs(8).");
    208 			return (0);
    209 		}
    210 #else
    211 		pwarn("XXX Can't look for alternate superblocks yet\n");
    212 		return (0);
    213 #endif
    214 		doskipclean = 0;
    215 		pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
    216 	}
    217 	bufinit();
    218 
    219 	if (bflag == 0x0 && idaddr == 0x0) {
    220 		/*
    221 		 * Even if that superblock read in properly, it may not
    222 		 * be guaranteed to point to a complete checkpoint.
    223 		 * Read in the second superblock too, and take whichever
    224 		 * of the two is *less* recent. --ks
    225 		 */
    226 		sb0 = malloc(sizeof(*sb0));
    227 		sb1 = malloc(sizeof(*sb1));
    228 		memcpy(sb0, &sblock, sizeof(*sb0));
    229 		bflag = sblock.lfs_sboffs[1];
    230 		if (readsb(1) == 0) {
    231 			pwarn("COULDN'T READ ALT SUPERBLOCK AT BLK %d", bflag);
    232 			if (reply("ASSUME PRIMARY SUPERBLOCK IS GOOD") == 0) {
    233 				return (0);
    234 			} else {/* use primary as good */
    235 				memcpy(&sblock, sb0, sizeof(*sb0));	/* XXX cheating? */
    236 			}
    237 		} else {
    238 			memcpy(sb1, &sblock, sizeof(*sb1));
    239 			if (debug)
    240 				pwarn("sb0 %d, sb1 %d\n", sb0->lfs_tstamp, sblock.lfs_tstamp);
    241 			/*
    242 			 * Verify the checkpoint of the newer superblock,
    243 			 * if the timestamp of the two superblocks is
    244 			 * different.  XXX use lfs_offset instead, discover
    245 			 * how to quickly discover "newness" based on that.
    246 			 */
    247 			if (sb0->lfs_tstamp != sb1->lfs_tstamp) {
    248 				if (sb0->lfs_tstamp > sb1->lfs_tstamp) {
    249 					osb = sb1;
    250 					nsb = sb0;
    251 				} else {
    252 					osb = sb0;
    253 					nsb = sb1;
    254 				}
    255 				daddr = try_verify(osb, nsb);
    256 
    257 				if (debug)
    258 					printf("done.\n");
    259 				if (daddr == nsb->lfs_offset) {
    260 					pwarn("Checkpoint verified, recovered %d seconds of data\n", nsb->lfs_tstamp - osb->lfs_tstamp);
    261 					memcpy(&sblock, nsb, sizeof(*nsb));
    262 					sbdirty();
    263 				} else {
    264 					pwarn("Checkpoint invalid, lost %d seconds of data\n", nsb->lfs_tstamp - osb->lfs_tstamp);
    265 					memcpy(&sblock, osb, sizeof(*osb));
    266 				}
    267 			}
    268 		}
    269 		free(sb0);
    270 		free(sb1);
    271 	}
    272 	if (idaddr == 0x0)
    273 		idaddr = sblock.lfs_idaddr;
    274 	if (debug) {
    275 		printf("dev_bsize = %lu\n", dev_bsize);
    276 		printf("lfs_bsize = %lu\n", (unsigned long)sblock.lfs_bsize);
    277 		printf("lfs_fsize = %lu\n", (unsigned long)sblock.lfs_fsize);
    278 		printf("lfs_frag  = %lu\n", (unsigned long)sblock.lfs_frag);
    279 		printf("INOPB(fs) = %lu\n", (unsigned long) INOPB(&sblock));
    280 		/* printf("fsbtodb(fs,1) = %lu\n",fsbtodb(&sblock,1)); */
    281 	}
    282 #if 0				/* FFS-specific fs-clean check */
    283 	if (debug)
    284 		printf("clean = %d\n", sblock.lfs_clean);
    285 	if (sblock.lfs_clean & FS_ISCLEAN) {
    286 		if (doskipclean) {
    287 			pwarn("%sile system is clean; not checking\n",
    288 			      preen ? "f" : "** F");
    289 			return (-1);
    290 		}
    291 		if (!preen)
    292 			pwarn("** File system is already clean\n");
    293 	}
    294 	maxino = sblock.lfs_ncg * sblock.lfs_ipg;
    295 #else
    296 	initbarea(&iblk);
    297 	iblk.b_un.b_buf = malloc(sblock.lfs_bsize);
    298 	if (bread(fsreadfd, (char *)iblk.b_un.b_buf, idaddr,
    299 		  (long)sblock.lfs_bsize) != 0) {
    300 		printf("Couldn't read disk block %d\n", idaddr);
    301 		exit(1);
    302 	}
    303 	idinode = lfs_difind(&sblock, sblock.lfs_ifile, &ifblock);
    304 	maxino = ((idinode->di_size
    305 	    - (sblock.lfs_cleansz + sblock.lfs_segtabsz) * sblock.lfs_bsize)
    306 		  / sblock.lfs_bsize) * sblock.lfs_ifpb;
    307 	if (debug)
    308 		printf("maxino=%d\n", maxino);
    309 	din_table = (daddr_t *)malloc(maxino * sizeof(*din_table));
    310 	memset(din_table, 0, maxino * sizeof(*din_table));
    311 	seg_table = (SEGUSE *)malloc(sblock.lfs_nseg * sizeof(SEGUSE));
    312 	memset(seg_table, 0, sblock.lfs_nseg * sizeof(SEGUSE));
    313 #endif
    314 	maxfsblock = sblock.lfs_size * (sblock.lfs_bsize / dev_bsize);
    315 #if 0
    316 	sizepb = sblock.lfs_bsize;
    317 	maxfilesize = sblock.lfs_bsize * NDADDR - 1;
    318 	for (i = 0; i < NIADDR; i++) {
    319 		sizepb *= NINDIR(&sblock);
    320 		maxfilesize += sizepb;
    321 	}
    322 	maxfilesize++;		/* XXX */
    323 #else				/* LFS way */
    324 	{
    325 		u_quad_t        maxtable[] = {
    326 			 /* 1 */ -1,
    327 			 /* 2 */ -1,
    328 			 /* 4 */ -1,
    329 			 /* 8 */ -1,
    330 			 /* 16 */ -1,
    331 			 /* 32 */ -1,
    332 			 /* 64 */ -1,
    333 			 /* 128 */ -1,
    334 			 /* 256 */ -1,
    335 			 /* 512 */ NDADDR + 128 + 128 * 128 + 128 * 128 * 128,
    336 			 /* 1024 */ NDADDR + 256 + 256 * 256 + 256 * 256 * 256,
    337 			 /* 2048 */ NDADDR + 512 + 512 * 512 + 512 * 512 * 512,
    338 			 /* 4096 */ NDADDR + 1024 + 1024 * 1024 + 1024 * 1024 * 1024,
    339 			 /* 8192 */ 1 << 31,
    340 			 /* 16 K */ 1 << 31,
    341 			 /* 32 K */ 1 << 31,
    342 		};
    343 		maxfilesize = maxtable[sblock.lfs_bshift] << sblock.lfs_bshift;
    344 	}
    345 #endif
    346 	if ((sblock.lfs_minfree < 0 || sblock.lfs_minfree > 99)) {
    347 		pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
    348 		       sblock.lfs_minfree);
    349 		if (reply("SET TO DEFAULT") == 1) {
    350 			sblock.lfs_minfree = 10;
    351 			sbdirty();
    352 		}
    353 	}
    354 	/* XXX used to be ~(sblock.lfs_bsize - 1) */
    355 	if (sblock.lfs_bmask != sblock.lfs_bsize - 1) {
    356 		pwarn("INCORRECT BMASK=%x IN SUPERBLOCK (should be %x)",
    357 		      (unsigned int)sblock.lfs_bmask,
    358 		      (unsigned int)sblock.lfs_bsize - 1);
    359 		sblock.lfs_bmask = sblock.lfs_bsize - 1;
    360 		if (preen)
    361 			printf(" (FIXED)\n");
    362 		if (preen || reply("FIX") == 1) {
    363 			sbdirty();
    364 			dirty(&asblk);
    365 		}
    366 	}
    367 #if 0				/* FFS-specific checks */
    368 	if (sblock.lfs_fmask != ~(sblock.lfs_fsize - 1)) {
    369 		pwarn("INCORRECT FMASK=%x IN SUPERBLOCK",
    370 		      sblock.lfs_fmask);
    371 		sblock.lfs_fmask = ~(sblock.lfs_fsize - 1);
    372 		if (preen)
    373 			printf(" (FIXED)\n");
    374 		if (preen || reply("FIX") == 1) {
    375 			sbdirty();
    376 			dirty(&asblk);
    377 		}
    378 	}
    379 #endif
    380 	if (sblock.lfs_maxfilesize != maxfilesize) {
    381 		pwarn("INCORRECT MAXFILESIZE=%qu IN SUPERBLOCK (should be %qu)",
    382 		      (unsigned long long)sblock.lfs_maxfilesize,
    383 		      (unsigned long long)maxfilesize);
    384 		sblock.lfs_maxfilesize = maxfilesize;
    385 		if (preen)
    386 			printf(" (FIXED)\n");
    387 		if (preen || reply("FIX") == 1) {
    388 			sbdirty();
    389 			dirty(&asblk);
    390 		}
    391 	}
    392 	if (sblock.lfs_maxsymlinklen != MAXSYMLINKLEN) {
    393 		pwarn("INCORRECT MAXSYMLINKLEN=%d IN SUPERBLOCK",
    394 		      sblock.lfs_maxsymlinklen);
    395 		sblock.lfs_maxsymlinklen = MAXSYMLINKLEN;
    396 		if (preen)
    397 			printf(" (FIXED)\n");
    398 		if (preen || reply("FIX") == 1) {
    399 			sbdirty();
    400 			dirty(&asblk);
    401 		}
    402 	}
    403 	newinofmt = 1;
    404 	/*
    405 	 * allocate and initialize the necessary maps
    406 	 */
    407 #ifndef VERBOSE_BLOCKMAP
    408 	bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
    409 	blockmap = calloc((unsigned)bmapsize, sizeof(char));
    410 #else
    411 	bmapsize = maxfsblock * sizeof(ino_t);
    412 	blockmap = (ino_t *)calloc(maxfsblock, sizeof(ino_t));
    413 #endif
    414 	if (blockmap == NULL) {
    415 		printf("cannot alloc %u bytes for blockmap\n",
    416 		       (unsigned)bmapsize);
    417 		goto badsblabel;
    418 	}
    419 
    420 	statemap = calloc((unsigned)maxino, sizeof(char));
    421 	if (statemap == NULL) {
    422 		printf("cannot alloc %u bytes for statemap\n",
    423 		       (unsigned)maxino);
    424 		goto badsblabel;
    425 	}
    426 	typemap = calloc((unsigned)maxino, sizeof(char));
    427 	if (typemap == NULL) {
    428 		printf("cannot alloc %u bytes for typemap\n",
    429 		       (unsigned)maxino);
    430 		goto badsblabel;
    431 	}
    432 	lncntp = (int16_t *)calloc((unsigned)maxino, sizeof(int16_t));
    433 	if (lncntp == NULL) {
    434 		printf("cannot alloc %lu bytes for lncntp\n",
    435 		       (unsigned long)maxino * sizeof(int16_t));
    436 		goto badsblabel;
    437 	}
    438 	return (1);
    439 
    440 badsblabel:
    441 	ckfini(0);
    442 	return (0);
    443 }
    444 
    445 /*
    446  * Read in the LFS super block and its summary info.
    447  */
    448 static int
    449 readsb(int listerr)
    450 {
    451 	daddr_t         super = bflag ? bflag : LFS_LABELPAD / dev_bsize;
    452 	u_int32_t       checksum;
    453 
    454 	if (bread(fsreadfd, (char *) &sblock, super, (long) LFS_SBPAD) != 0)
    455 		return (0);
    456 
    457 	sblk.b_bno = super;
    458 	sblk.b_size = LFS_SBPAD;
    459 	/*
    460 	 * run a few consistency checks of the super block
    461 	 */
    462 	if (sblock.lfs_magic != LFS_MAGIC) {
    463 		badsb(listerr, "MAGIC NUMBER WRONG");
    464 		return (0);
    465 	}
    466 	/* checksum */
    467 	checksum = lfs_sb_cksum(&(sblock.lfs_dlfs));
    468 	if (sblock.lfs_cksum != checksum) {
    469 		printf("Superblock checksum (%lu)does not match computed checksum %lu\n",
    470 		(unsigned long)sblock.lfs_cksum, (unsigned long) checksum);
    471 	}
    472 #if 0				/* XXX - replace these checks with
    473 				 * appropriate LFS sanity checks */
    474 	if (sblock.lfs_ncg < 1) {
    475 		badsb(listerr, "NCG OUT OF RANGE");
    476 		return (0);
    477 	}
    478 	if (sblock.lfs_cpg < 1) {
    479 		badsb(listerr, "CPG OUT OF RANGE");
    480 		return (0);
    481 	}
    482 	if (sblock.lfs_ncg * sblock.lfs_cpg < sblock.lfs_ncyl ||
    483 	    (sblock.lfs_ncg - 1) * sblock.lfs_cpg >= sblock.lfs_ncyl) {
    484 		badsb(listerr, "NCYL LESS THAN NCG*CPG");
    485 		return (0);
    486 	}
    487 	if (sblock.lfs_sbsize > SBSIZE) {
    488 		badsb(listerr, "SIZE PREPOSTEROUSLY LARGE");
    489 		return (0);
    490 	}
    491 #endif
    492 	/*
    493 	 * Compute block size that the filesystem is based on,
    494 	 * according to fsbtodb, and adjust superblock block number
    495 	 * so we can tell if this is an alternate later.
    496 	 */
    497 	super *= dev_bsize;
    498 #if 0
    499 	dev_bsize = sblock.lfs_bsize / fsbtodb(&sblock, 1);
    500 #endif
    501 	sblk.b_bno = super / dev_bsize;
    502 	if (bflag) {
    503 		havesb = 1;
    504 		return (1);
    505 	}
    506 #if 0				/* XXX - for now skip the alt. superblock
    507 				 * test as well */
    508 	/*
    509 	 * Set all possible fields that could differ, then do check
    510 	 * of whole super block against an alternate super block.
    511 	 * When an alternate super-block is specified this check is skipped.
    512 	 */
    513 	getblk(&asblk, cgsblock(&sblock, sblock.lfs_ncg - 1), sblock.lfs_sbsize);
    514 	if (asblk.b_errs)
    515 		return (0);
    516 	altsblock.lfs_firstfield = sblock.lfs_firstfield;
    517 	altsblock.lfs_fscktime = sblock.lfs_fscktime;
    518 	altsblock.lfs_time = sblock.lfs_time;
    519 	altsblock.lfs_cstotal = sblock.lfs_cstotal;
    520 	altsblock.lfs_cgrotor = sblock.lfs_cgrotor;
    521 	altsblock.lfs_fmod = sblock.lfs_fmod;
    522 	altsblock.lfs_clean = sblock.lfs_clean;
    523 	altsblock.lfs_ronly = sblock.lfs_ronly;
    524 	altsblock.lfs_flags = sblock.lfs_flags;
    525 	altsblock.lfs_maxcontig = sblock.lfs_maxcontig;
    526 	altsblock.lfs_minfree = sblock.lfs_minfree;
    527 	altsblock.lfs_optim = sblock.lfs_optim;
    528 	altsblock.lfs_rotdelay = sblock.lfs_rotdelay;
    529 	altsblock.lfs_maxbpg = sblock.lfs_maxbpg;
    530 	memcpy(altsblock.lfs_csp, sblock.lfs_csp,
    531 	       sizeof sblock.lfs_csp);
    532 	altsblock.lfs_maxcluster = sblock.lfs_maxcluster;
    533 	memcpy(altsblock.lfs_fsmnt, sblock.lfs_fsmnt,
    534 	       sizeof sblock.lfs_fsmnt);
    535 	memcpy(altsblock.lfs_sparecon, sblock.lfs_sparecon,
    536 	       sizeof sblock.lfs_sparecon);
    537 	/*
    538 	 * The following should not have to be copied.
    539 	 */
    540 	altsblock.lfs_fsbtodb = sblock.lfs_fsbtodb;
    541 	altsblock.lfs_interleave = sblock.lfs_interleave;
    542 	altsblock.lfs_npsect = sblock.lfs_npsect;
    543 	altsblock.lfs_nrpos = sblock.lfs_nrpos;
    544 	altsblock.lfs_state = sblock.lfs_state;
    545 	altsblock.lfs_qbmask = sblock.lfs_qbmask;
    546 	altsblock.lfs_qfmask = sblock.lfs_qfmask;
    547 	altsblock.lfs_state = sblock.lfs_state;
    548 	altsblock.lfs_maxfilesize = sblock.lfs_maxfilesize;
    549 	if (memcmp(&sblock, &altsblock, (int)sblock.lfs_sbsize)) {
    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.lfs_sbsize / sizeof *olp);
    557 			for (; olp < endlp; olp++, nlp++) {
    558 				if (*olp == *nlp)
    559 					continue;
    560 				printf("offset %d, original %ld, alternate %ld\n",
    561 				       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 #endif
    569 	havesb = 1;
    570 	return (1);
    571 }
    572 
    573 void
    574 badsb(int listerr, char *s)
    575 {
    576 
    577 	if (!listerr)
    578 		return;
    579 	if (preen)
    580 		printf("%s: ", cdevname());
    581 	pfatal("BAD SUPER BLOCK: %s\n", s);
    582 }
    583 
    584 /*
    585  * Calculate a prototype superblock based on information in the disk label.
    586  * When done the cgsblock macro can be calculated and the fs_ncg field
    587  * can be used. Do NOT attempt to use other macros without verifying that
    588  * their needed information is available!
    589  */
    590 int
    591 calcsb(const char *dev, int devfd, struct lfs * fs)
    592 {
    593 	register struct disklabel *lp;
    594 	register struct partition *pp;
    595 	register char  *cp;
    596 	int             i;
    597 
    598 	cp = strchr(dev, '\0') - 1;
    599 	if ((cp == (char *) -1 || (*cp < 'a' || *cp > 'h')) && !isdigit(*cp)) {
    600 		pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
    601 		return (0);
    602 	}
    603 	lp = getdisklabel(dev, devfd);
    604 	if (lp == NULL) {
    605 		dev_bsize = DEV_BSIZE;
    606 	} else {
    607 		if (isdigit(*cp))
    608 			pp = &lp->d_partitions[0];
    609 		else
    610 			pp = &lp->d_partitions[*cp - 'a'];
    611 		if (pp->p_fstype != FS_BSDLFS) {
    612 			pfatal("%s: NOT LABELED AS AN LFS FILE SYSTEM (%s)\n",
    613 			       dev, pp->p_fstype < FSMAXTYPES ?
    614 			       fstypenames[pp->p_fstype] : "unknown");
    615 			return (0);
    616 		}
    617 		memset(fs, 0, sizeof(struct lfs));
    618 		fs->lfs_fsize = pp->p_fsize;
    619 		fs->lfs_frag = pp->p_frag;
    620 		fs->lfs_size = pp->p_size;
    621 		fs->lfs_nspf = fs->lfs_fsize / lp->d_secsize;
    622 		dev_bsize = lp->d_secsize;
    623 		for (fs->lfs_fsbtodb = 0, i = fs->lfs_nspf; i > 1; i >>= 1)
    624 			fs->lfs_fsbtodb++;
    625 	}
    626 	return (1);
    627 }
    628 
    629 static struct disklabel *
    630 getdisklabel(const char *s, int fd)
    631 {
    632 	static struct disklabel lab;
    633 
    634 	if (ioctl(fd, DIOCGDINFO, (char *) &lab) < 0) {
    635 		if (s == NULL)
    636 			return ((struct disklabel *) NULL);
    637 		pwarn("ioctl (GCINFO): %s\n", strerror(errno));
    638 #if 0
    639 		errexit("%s: can't read disk label\n", s);
    640 #else
    641 		return NULL;
    642 #endif
    643 	}
    644 	return (&lab);
    645 }
    646