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