setup.c revision 41474
11558Srgrimes/*
21558Srgrimes * Copyright (c) 1980, 1986, 1993
31558Srgrimes *	The Regents of the University of California.  All rights reserved.
41558Srgrimes *
51558Srgrimes * Redistribution and use in source and binary forms, with or without
61558Srgrimes * modification, are permitted provided that the following conditions
71558Srgrimes * are met:
81558Srgrimes * 1. Redistributions of source code must retain the above copyright
91558Srgrimes *    notice, this list of conditions and the following disclaimer.
101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111558Srgrimes *    notice, this list of conditions and the following disclaimer in the
121558Srgrimes *    documentation and/or other materials provided with the distribution.
131558Srgrimes * 3. All advertising materials mentioning features or use of this software
141558Srgrimes *    must display the following acknowledgement:
151558Srgrimes *	This product includes software developed by the University of
161558Srgrimes *	California, Berkeley and its contributors.
171558Srgrimes * 4. Neither the name of the University nor the names of its contributors
181558Srgrimes *    may be used to endorse or promote products derived from this software
191558Srgrimes *    without specific prior written permission.
201558Srgrimes *
211558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311558Srgrimes * SUCH DAMAGE.
321558Srgrimes */
331558Srgrimes
341558Srgrimes#ifndef lint
3523675Speterstatic const char sccsid[] = "@(#)setup.c	8.10 (Berkeley) 5/9/95";
361558Srgrimes#endif /* not lint */
371558Srgrimes
381558Srgrimes#define DKTYPENAMES
391558Srgrimes#include <sys/param.h>
401558Srgrimes#include <sys/stat.h>
4141474Sjulian#include <sys/ioctl.h>
421558Srgrimes#include <sys/disklabel.h>
431558Srgrimes#include <sys/file.h>
4423675Speter
4523675Speter#include <ufs/ufs/dinode.h>
4623675Speter#include <ufs/ffs/fs.h>
4723675Speter
4823675Speter#include <ctype.h>
4923675Speter#include <err.h>
501558Srgrimes#include <errno.h>
511558Srgrimes#include <string.h>
5223675Speter
531558Srgrimes#include "fsck.h"
541558Srgrimes
551558Srgrimesstruct bufarea asblk;
561558Srgrimes#define altsblock (*asblk.b_un.b_fs)
571558Srgrimes#define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
581558Srgrimes
5923675Speterstatic void badsb __P((int listerr, char *s));
6023675Speterstatic int calcsb __P((char *dev, int devfd, struct fs *fs));
6123675Speterstatic struct disklabel *getdisklabel __P((char *s, int fd));
6223675Speterstatic int readsb __P((int listerr));
631558Srgrimes
6423675Speter/*
6523675Speter * Read in a superblock finding an alternate if necessary.
6623675Speter * Return 1 if successful, 0 if unsuccessful, -1 if filesystem
6723675Speter * is already clean (preen mode only).
6823675Speter */
697585Sbdeint
701558Srgrimessetup(dev)
711558Srgrimes	char *dev;
721558Srgrimes{
731558Srgrimes	long cg, size, asked, i, j;
7423675Speter	long skipclean, bmapsize;
751558Srgrimes	struct disklabel *lp;
761558Srgrimes	off_t sizepb;
771558Srgrimes	struct stat statb;
781558Srgrimes	struct fs proto;
791558Srgrimes
801558Srgrimes	havesb = 0;
811558Srgrimes	fswritefd = -1;
8241474Sjulian	skipclean = fflag ? 0 : preen;
831558Srgrimes	if (stat(dev, &statb) < 0) {
841558Srgrimes		printf("Can't stat %s: %s\n", dev, strerror(errno));
851558Srgrimes		return (0);
861558Srgrimes	}
871558Srgrimes	if ((statb.st_mode & S_IFMT) != S_IFCHR) {
881558Srgrimes		pfatal("%s is not a character device", dev);
891558Srgrimes		if (reply("CONTINUE") == 0)
901558Srgrimes			return (0);
911558Srgrimes	}
921558Srgrimes	if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
931558Srgrimes		printf("Can't open %s: %s\n", dev, strerror(errno));
941558Srgrimes		return (0);
951558Srgrimes	}
961558Srgrimes	if (preen == 0)
971558Srgrimes		printf("** %s", dev);
981558Srgrimes	if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) {
991558Srgrimes		fswritefd = -1;
1001558Srgrimes		if (preen)
1011558Srgrimes			pfatal("NO WRITE ACCESS");
1021558Srgrimes		printf(" (NO WRITE)");
1031558Srgrimes	}
1041558Srgrimes	if (preen == 0)
1051558Srgrimes		printf("\n");
1061558Srgrimes	fsmodified = 0;
1071558Srgrimes	lfdir = 0;
1081558Srgrimes	initbarea(&sblk);
1091558Srgrimes	initbarea(&asblk);
1101558Srgrimes	sblk.b_un.b_buf = malloc(SBSIZE);
1111558Srgrimes	asblk.b_un.b_buf = malloc(SBSIZE);
1121558Srgrimes	if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
11323675Speter		errx(EEXIT, "cannot allocate space for superblock");
11441474Sjulian	if ((lp = getdisklabel(NULL, fsreadfd)))
1151558Srgrimes		dev_bsize = secsize = lp->d_secsize;
1161558Srgrimes	else
1171558Srgrimes		dev_bsize = secsize = DEV_BSIZE;
1181558Srgrimes	/*
1191558Srgrimes	 * Read in the superblock, looking for alternates if necessary
1201558Srgrimes	 */
1211558Srgrimes	if (readsb(1) == 0) {
12223675Speter		skipclean = 0;
1231558Srgrimes		if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
1241558Srgrimes			return(0);
1251558Srgrimes		if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
1261558Srgrimes			return (0);
1271558Srgrimes		for (cg = 0; cg < proto.fs_ncg; cg++) {
1281558Srgrimes			bflag = fsbtodb(&proto, cgsblock(&proto, cg));
1291558Srgrimes			if (readsb(0) != 0)
1301558Srgrimes				break;
1311558Srgrimes		}
1321558Srgrimes		if (cg >= proto.fs_ncg) {
1331558Srgrimes			printf("%s %s\n%s %s\n%s %s\n",
1341558Srgrimes				"SEARCH FOR ALTERNATE SUPER-BLOCK",
1351558Srgrimes				"FAILED. YOU MUST USE THE",
1361558Srgrimes				"-b OPTION TO FSCK TO SPECIFY THE",
1371558Srgrimes				"LOCATION OF AN ALTERNATE",
1381558Srgrimes				"SUPER-BLOCK TO SUPPLY NEEDED",
1391558Srgrimes				"INFORMATION; SEE fsck(8).");
1406280Sbde			bflag = 0;
1411558Srgrimes			return(0);
1421558Srgrimes		}
1431558Srgrimes		pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
1446280Sbde		bflag = 0;
1451558Srgrimes	}
14641474Sjulian	if (skipclean && sblock.fs_clean) {
14741474Sjulian		pwarn("FILESYSTEM CLEAN; SKIPPING CHECKS\n");
14841474Sjulian		return (-1);
14941474Sjulian	}
1501558Srgrimes	maxfsblock = sblock.fs_size;
1511558Srgrimes	maxino = sblock.fs_ncg * sblock.fs_ipg;
1521558Srgrimes	/*
1531558Srgrimes	 * Check and potentially fix certain fields in the super block.
1541558Srgrimes	 */
1551558Srgrimes	if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
1561558Srgrimes		pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
1571558Srgrimes		if (reply("SET TO DEFAULT") == 1) {
1581558Srgrimes			sblock.fs_optim = FS_OPTTIME;
1591558Srgrimes			sbdirty();
1601558Srgrimes		}
1611558Srgrimes	}
1621558Srgrimes	if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
1631558Srgrimes		pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
1641558Srgrimes			sblock.fs_minfree);
1651558Srgrimes		if (reply("SET TO DEFAULT") == 1) {
1661558Srgrimes			sblock.fs_minfree = 10;
1671558Srgrimes			sbdirty();
1681558Srgrimes		}
1691558Srgrimes	}
1708871Srgrimes	if (sblock.fs_interleave < 1 ||
1711558Srgrimes	    sblock.fs_interleave > sblock.fs_nsect) {
1721558Srgrimes		pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK",
1731558Srgrimes			sblock.fs_interleave);
1741558Srgrimes		sblock.fs_interleave = 1;
1751558Srgrimes		if (preen)
1761558Srgrimes			printf(" (FIXED)\n");
1771558Srgrimes		if (preen || reply("SET TO DEFAULT") == 1) {
1781558Srgrimes			sbdirty();
1791558Srgrimes			dirty(&asblk);
1801558Srgrimes		}
1811558Srgrimes	}
1828871Srgrimes	if (sblock.fs_npsect < sblock.fs_nsect ||
1831558Srgrimes	    sblock.fs_npsect > sblock.fs_nsect*2) {
1841558Srgrimes		pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK",
1851558Srgrimes			sblock.fs_npsect);
1861558Srgrimes		sblock.fs_npsect = sblock.fs_nsect;
1871558Srgrimes		if (preen)
1881558Srgrimes			printf(" (FIXED)\n");
1891558Srgrimes		if (preen || reply("SET TO DEFAULT") == 1) {
1901558Srgrimes			sbdirty();
1911558Srgrimes			dirty(&asblk);
1921558Srgrimes		}
1931558Srgrimes	}
1941558Srgrimes	if (sblock.fs_inodefmt >= FS_44INODEFMT) {
1951558Srgrimes		newinofmt = 1;
1961558Srgrimes	} else {
1971558Srgrimes		sblock.fs_qbmask = ~sblock.fs_bmask;
1981558Srgrimes		sblock.fs_qfmask = ~sblock.fs_fmask;
1991558Srgrimes		newinofmt = 0;
2001558Srgrimes	}
2011558Srgrimes	/*
2021558Srgrimes	 * Convert to new inode format.
2031558Srgrimes	 */
2041558Srgrimes	if (cvtlevel >= 2 && sblock.fs_inodefmt < FS_44INODEFMT) {
2051558Srgrimes		if (preen)
2061558Srgrimes			pwarn("CONVERTING TO NEW INODE FORMAT\n");
2071558Srgrimes		else if (!reply("CONVERT TO NEW INODE FORMAT"))
2081558Srgrimes			return(0);
2091558Srgrimes		doinglevel2++;
2101558Srgrimes		sblock.fs_inodefmt = FS_44INODEFMT;
2111558Srgrimes		sizepb = sblock.fs_bsize;
2121558Srgrimes		sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1;
2131558Srgrimes		for (i = 0; i < NIADDR; i++) {
2141558Srgrimes			sizepb *= NINDIR(&sblock);
2151558Srgrimes			sblock.fs_maxfilesize += sizepb;
2161558Srgrimes		}
2171558Srgrimes		sblock.fs_maxsymlinklen = MAXSYMLINKLEN;
2181558Srgrimes		sblock.fs_qbmask = ~sblock.fs_bmask;
2191558Srgrimes		sblock.fs_qfmask = ~sblock.fs_fmask;
2201558Srgrimes		sbdirty();
2211558Srgrimes		dirty(&asblk);
2221558Srgrimes	}
2231558Srgrimes	/*
2241558Srgrimes	 * Convert to new cylinder group format.
2251558Srgrimes	 */
2261558Srgrimes	if (cvtlevel >= 1 && sblock.fs_postblformat == FS_42POSTBLFMT) {
2271558Srgrimes		if (preen)
2281558Srgrimes			pwarn("CONVERTING TO NEW CYLINDER GROUP FORMAT\n");
2291558Srgrimes		else if (!reply("CONVERT TO NEW CYLINDER GROUP FORMAT"))
2301558Srgrimes			return(0);
2311558Srgrimes		doinglevel1++;
2321558Srgrimes		sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT;
2331558Srgrimes		sblock.fs_nrpos = 8;
2341558Srgrimes		sblock.fs_postbloff =
2351558Srgrimes		    (char *)(&sblock.fs_opostbl[0][0]) -
23623675Speter		    (char *)(&sblock.fs_firstfield);
2371558Srgrimes		sblock.fs_rotbloff = &sblock.fs_space[0] -
23823675Speter		    (u_char *)(&sblock.fs_firstfield);
2391558Srgrimes		sblock.fs_cgsize =
2401558Srgrimes			fragroundup(&sblock, CGSIZE(&sblock));
2411558Srgrimes		sbdirty();
2421558Srgrimes		dirty(&asblk);
2431558Srgrimes	}
24423675Speter	if (asblk.b_dirty && !bflag) {
24523675Speter		memmove(&altsblock, &sblock, (size_t)sblock.fs_sbsize);
2461558Srgrimes		flush(fswritefd, &asblk);
2471558Srgrimes	}
2481558Srgrimes	/*
2491558Srgrimes	 * read in the summary info.
2501558Srgrimes	 */
2511558Srgrimes	asked = 0;
2521558Srgrimes	for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
2531558Srgrimes		size = sblock.fs_cssize - i < sblock.fs_bsize ?
2541558Srgrimes		    sblock.fs_cssize - i : sblock.fs_bsize;
2551558Srgrimes		sblock.fs_csp[j] = (struct csum *)calloc(1, (unsigned)size);
2561558Srgrimes		if (bread(fsreadfd, (char *)sblock.fs_csp[j],
2571558Srgrimes		    fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
2581558Srgrimes		    size) != 0 && !asked) {
2591558Srgrimes			pfatal("BAD SUMMARY INFORMATION");
26034266Sjulian			if (reply("CONTINUE") == 0) {
26134266Sjulian				ckfini(0);
26223675Speter				exit(EEXIT);
26334266Sjulian			}
2641558Srgrimes			asked++;
2651558Srgrimes		}
2661558Srgrimes	}
2671558Srgrimes	/*
2681558Srgrimes	 * allocate and initialize the necessary maps
2691558Srgrimes	 */
2701558Srgrimes	bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(short));
2711558Srgrimes	blockmap = calloc((unsigned)bmapsize, sizeof (char));
2721558Srgrimes	if (blockmap == NULL) {
2731558Srgrimes		printf("cannot alloc %u bytes for blockmap\n",
2741558Srgrimes		    (unsigned)bmapsize);
2751558Srgrimes		goto badsb;
2761558Srgrimes	}
27741474Sjulian	inostathead = calloc((unsigned)(sblock.fs_ncg),
27841474Sjulian	    sizeof(struct inostatlist));
27941474Sjulian	if (inostathead == NULL) {
28041474Sjulian		printf("cannot alloc %u bytes for inostathead\n",
28141474Sjulian		    (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg)));
2821558Srgrimes		goto badsb;
2831558Srgrimes	}
2841558Srgrimes	numdirs = sblock.fs_cstotal.cs_ndir;
28534033Sphk	if (numdirs == 0) {
28634033Sphk		printf("numdirs is zero, try using an alternate superblock\n");
28734033Sphk		goto badsb;
28834033Sphk	}
2891558Srgrimes	inplast = 0;
2901558Srgrimes	listmax = numdirs + 10;
2911558Srgrimes	inpsort = (struct inoinfo **)calloc((unsigned)listmax,
2921558Srgrimes	    sizeof(struct inoinfo *));
2931558Srgrimes	inphead = (struct inoinfo **)calloc((unsigned)numdirs,
2941558Srgrimes	    sizeof(struct inoinfo *));
2951558Srgrimes	if (inpsort == NULL || inphead == NULL) {
2968871Srgrimes		printf("cannot alloc %u bytes for inphead\n",
2971558Srgrimes		    (unsigned)numdirs * sizeof(struct inoinfo *));
2981558Srgrimes		goto badsb;
2991558Srgrimes	}
3001558Srgrimes	bufinit();
30134266Sjulian	if (sblock.fs_flags & FS_DOSOFTDEP)
30234266Sjulian		usedsoftdep = 1;
30334266Sjulian	else
30434266Sjulian		usedsoftdep = 0;
3051558Srgrimes	return (1);
3061558Srgrimes
3071558Srgrimesbadsb:
30823675Speter	ckfini(0);
3091558Srgrimes	return (0);
3101558Srgrimes}
3111558Srgrimes
3121558Srgrimes/*
3131558Srgrimes * Read in the super block and its summary info.
3141558Srgrimes */
3157585Sbdestatic int
3161558Srgrimesreadsb(listerr)
3171558Srgrimes	int listerr;
3181558Srgrimes{
31923675Speter	ufs_daddr_t super = bflag ? bflag : SBOFF / dev_bsize;
3201558Srgrimes
3211558Srgrimes	if (bread(fsreadfd, (char *)&sblock, super, (long)SBSIZE) != 0)
3221558Srgrimes		return (0);
3231558Srgrimes	sblk.b_bno = super;
3241558Srgrimes	sblk.b_size = SBSIZE;
3251558Srgrimes	/*
3261558Srgrimes	 * run a few consistency checks of the super block
3271558Srgrimes	 */
3281558Srgrimes	if (sblock.fs_magic != FS_MAGIC)
3291558Srgrimes		{ badsb(listerr, "MAGIC NUMBER WRONG"); return (0); }
3301558Srgrimes	if (sblock.fs_ncg < 1)
3311558Srgrimes		{ badsb(listerr, "NCG OUT OF RANGE"); return (0); }
3321558Srgrimes	if (sblock.fs_cpg < 1)
3331558Srgrimes		{ badsb(listerr, "CPG OUT OF RANGE"); return (0); }
3341558Srgrimes	if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl ||
3351558Srgrimes	    (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl)
3361558Srgrimes		{ badsb(listerr, "NCYL LESS THAN NCG*CPG"); return (0); }
3371558Srgrimes	if (sblock.fs_sbsize > SBSIZE)
3381558Srgrimes		{ badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); }
3391558Srgrimes	/*
3401558Srgrimes	 * Compute block size that the filesystem is based on,
3411558Srgrimes	 * according to fsbtodb, and adjust superblock block number
3421558Srgrimes	 * so we can tell if this is an alternate later.
3431558Srgrimes	 */
3441558Srgrimes	super *= dev_bsize;
3451558Srgrimes	dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
3461558Srgrimes	sblk.b_bno = super / dev_bsize;
3471558Srgrimes	if (bflag) {
3481558Srgrimes		havesb = 1;
3491558Srgrimes		return (1);
3501558Srgrimes	}
3511558Srgrimes	/*
3521558Srgrimes	 * Set all possible fields that could differ, then do check
3531558Srgrimes	 * of whole super block against an alternate super block.
3541558Srgrimes	 * When an alternate super-block is specified this check is skipped.
3551558Srgrimes	 */
3561558Srgrimes	getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize);
3571558Srgrimes	if (asblk.b_errs)
3581558Srgrimes		return (0);
35923675Speter	altsblock.fs_firstfield = sblock.fs_firstfield;
36023675Speter	altsblock.fs_unused_1 = sblock.fs_unused_1;
3611558Srgrimes	altsblock.fs_time = sblock.fs_time;
3621558Srgrimes	altsblock.fs_cstotal = sblock.fs_cstotal;
3631558Srgrimes	altsblock.fs_cgrotor = sblock.fs_cgrotor;
3641558Srgrimes	altsblock.fs_fmod = sblock.fs_fmod;
3651558Srgrimes	altsblock.fs_clean = sblock.fs_clean;
3661558Srgrimes	altsblock.fs_ronly = sblock.fs_ronly;
3671558Srgrimes	altsblock.fs_flags = sblock.fs_flags;
3681558Srgrimes	altsblock.fs_maxcontig = sblock.fs_maxcontig;
3691558Srgrimes	altsblock.fs_minfree = sblock.fs_minfree;
3701558Srgrimes	altsblock.fs_optim = sblock.fs_optim;
3711558Srgrimes	altsblock.fs_rotdelay = sblock.fs_rotdelay;
3721558Srgrimes	altsblock.fs_maxbpg = sblock.fs_maxbpg;
37323675Speter	memmove(altsblock.fs_csp, sblock.fs_csp, sizeof sblock.fs_csp);
37423675Speter	altsblock.fs_maxcluster = sblock.fs_maxcluster;
37523675Speter	memmove(altsblock.fs_fsmnt, sblock.fs_fsmnt, sizeof sblock.fs_fsmnt);
37623675Speter	memmove(altsblock.fs_sparecon,
37723675Speter		sblock.fs_sparecon, sizeof sblock.fs_sparecon);
3781558Srgrimes	/*
3791558Srgrimes	 * The following should not have to be copied.
3801558Srgrimes	 */
3811558Srgrimes	altsblock.fs_fsbtodb = sblock.fs_fsbtodb;
3821558Srgrimes	altsblock.fs_interleave = sblock.fs_interleave;
3831558Srgrimes	altsblock.fs_npsect = sblock.fs_npsect;
3841558Srgrimes	altsblock.fs_nrpos = sblock.fs_nrpos;
38523675Speter	altsblock.fs_state = sblock.fs_state;
3861558Srgrimes	altsblock.fs_qbmask = sblock.fs_qbmask;
3871558Srgrimes	altsblock.fs_qfmask = sblock.fs_qfmask;
3881558Srgrimes	altsblock.fs_state = sblock.fs_state;
3891558Srgrimes	altsblock.fs_maxfilesize = sblock.fs_maxfilesize;
39023675Speter	if (memcmp(&sblock, &altsblock, (int)sblock.fs_sbsize)) {
39123675Speter		if (debug) {
39223675Speter			long *nlp, *olp, *endlp;
39323675Speter
39423675Speter			printf("superblock mismatches\n");
39523675Speter			nlp = (long *)&altsblock;
39623675Speter			olp = (long *)&sblock;
39723675Speter			endlp = olp + (sblock.fs_sbsize / sizeof *olp);
39823675Speter			for ( ; olp < endlp; olp++, nlp++) {
39923675Speter				if (*olp == *nlp)
40023675Speter					continue;
40137236Sbde				printf(
40237236Sbde				    "offset %d, original %ld, alternate %ld\n",
40323675Speter				    olp - (long *)&sblock, *olp, *nlp);
40423675Speter			}
40523675Speter		}
4061558Srgrimes		badsb(listerr,
4071558Srgrimes		"VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
4081558Srgrimes		return (0);
4091558Srgrimes	}
4101558Srgrimes	havesb = 1;
4111558Srgrimes	return (1);
4121558Srgrimes}
4131558Srgrimes
4147585Sbdestatic void
4151558Srgrimesbadsb(listerr, s)
4161558Srgrimes	int listerr;
4171558Srgrimes	char *s;
4181558Srgrimes{
4191558Srgrimes
4201558Srgrimes	if (!listerr)
4211558Srgrimes		return;
4221558Srgrimes	if (preen)
4231558Srgrimes		printf("%s: ", cdevname);
4241558Srgrimes	pfatal("BAD SUPER BLOCK: %s\n", s);
4251558Srgrimes}
4261558Srgrimes
4271558Srgrimes/*
4281558Srgrimes * Calculate a prototype superblock based on information in the disk label.
4291558Srgrimes * When done the cgsblock macro can be calculated and the fs_ncg field
4301558Srgrimes * can be used. Do NOT attempt to use other macros without verifying that
4311558Srgrimes * their needed information is available!
4321558Srgrimes */
43323675Speterstatic int
4341558Srgrimescalcsb(dev, devfd, fs)
4351558Srgrimes	char *dev;
4361558Srgrimes	int devfd;
4371558Srgrimes	register struct fs *fs;
4381558Srgrimes{
4391558Srgrimes	register struct disklabel *lp;
4401558Srgrimes	register struct partition *pp;
4411558Srgrimes	register char *cp;
4421558Srgrimes	int i;
4431558Srgrimes
44423675Speter	cp = strchr(dev, '\0') - 1;
4457585Sbde	if (cp == (char *)-1 || ((*cp < 'a' || *cp > 'h') && !isdigit(*cp))) {
4461558Srgrimes		pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
4471558Srgrimes		return (0);
4481558Srgrimes	}
4491558Srgrimes	lp = getdisklabel(dev, devfd);
4501558Srgrimes	if (isdigit(*cp))
4511558Srgrimes		pp = &lp->d_partitions[0];
4521558Srgrimes	else
4531558Srgrimes		pp = &lp->d_partitions[*cp - 'a'];
4541558Srgrimes	if (pp->p_fstype != FS_BSDFFS) {
4551558Srgrimes		pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n",
4561558Srgrimes			dev, pp->p_fstype < FSMAXTYPES ?
4571558Srgrimes			fstypenames[pp->p_fstype] : "unknown");
4581558Srgrimes		return (0);
4591558Srgrimes	}
46041474Sjulian	if (pp->p_fsize == 0 || pp->p_frag == 0 ||
46141474Sjulian	    pp->p_cpg == 0 || pp->p_size == 0) {
46241474Sjulian		pfatal("%s: %s: type %s fsize %d, frag %d, cpg %d, size %d\n",
46341474Sjulian		    dev, "INCOMPLETE LABEL", fstypenames[pp->p_fstype],
46441474Sjulian		    pp->p_fsize, pp->p_frag, pp->p_cpg, pp->p_size);
46532622Sbde		return (0);
46632622Sbde	}
46723675Speter	memset(fs, 0, sizeof(struct fs));
4681558Srgrimes	fs->fs_fsize = pp->p_fsize;
4691558Srgrimes	fs->fs_frag = pp->p_frag;
4701558Srgrimes	fs->fs_cpg = pp->p_cpg;
4711558Srgrimes	fs->fs_size = pp->p_size;
4721558Srgrimes	fs->fs_ntrak = lp->d_ntracks;
4731558Srgrimes	fs->fs_nsect = lp->d_nsectors;
4741558Srgrimes	fs->fs_spc = lp->d_secpercyl;
4751558Srgrimes	fs->fs_nspf = fs->fs_fsize / lp->d_secsize;
4761558Srgrimes	fs->fs_sblkno = roundup(
4771558Srgrimes		howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize),
4781558Srgrimes		fs->fs_frag);
4791558Srgrimes	fs->fs_cgmask = 0xffffffff;
4801558Srgrimes	for (i = fs->fs_ntrak; i > 1; i >>= 1)
4811558Srgrimes		fs->fs_cgmask <<= 1;
4821558Srgrimes	if (!POWEROF2(fs->fs_ntrak))
4831558Srgrimes		fs->fs_cgmask <<= 1;
4841558Srgrimes	fs->fs_cgoffset = roundup(
4851558Srgrimes		howmany(fs->fs_nsect, NSPF(fs)), fs->fs_frag);
4861558Srgrimes	fs->fs_fpg = (fs->fs_cpg * fs->fs_spc) / NSPF(fs);
4871558Srgrimes	fs->fs_ncg = howmany(fs->fs_size / fs->fs_spc, fs->fs_cpg);
4881558Srgrimes	for (fs->fs_fsbtodb = 0, i = NSPF(fs); i > 1; i >>= 1)
4891558Srgrimes		fs->fs_fsbtodb++;
4901558Srgrimes	dev_bsize = lp->d_secsize;
4911558Srgrimes	return (1);
4921558Srgrimes}
4931558Srgrimes
49423675Speterstatic struct disklabel *
4951558Srgrimesgetdisklabel(s, fd)
4961558Srgrimes	char *s;
4971558Srgrimes	int	fd;
4981558Srgrimes{
4991558Srgrimes	static struct disklabel lab;
5001558Srgrimes
5011558Srgrimes	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
5021558Srgrimes		if (s == NULL)
5031558Srgrimes			return ((struct disklabel *)NULL);
5041558Srgrimes		pwarn("ioctl (GCINFO): %s\n", strerror(errno));
50523675Speter		errx(EEXIT, "%s: can't read disk label", s);
5061558Srgrimes	}
5071558Srgrimes	return (&lab);
5081558Srgrimes}
509