setup.c revision 8871
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
357585Sbdestatic const char sccsid[] = "@(#)setup.c	8.2 (Berkeley) 2/21/94";
361558Srgrimes#endif /* not lint */
371558Srgrimes
381558Srgrimes#define DKTYPENAMES
391558Srgrimes#include <sys/param.h>
401558Srgrimes#include <sys/time.h>
411558Srgrimes#include <ufs/ufs/dinode.h>
421558Srgrimes#include <ufs/ffs/fs.h>
431558Srgrimes#include <sys/stat.h>
441558Srgrimes#include <sys/ioctl.h>
451558Srgrimes#include <sys/disklabel.h>
461558Srgrimes#include <sys/file.h>
471558Srgrimes#include <errno.h>
487585Sbde#include <stdio.h>
491558Srgrimes#include <stdlib.h>
501558Srgrimes#include <string.h>
511558Srgrimes#include <ctype.h>
521558Srgrimes#include "fsck.h"
531558Srgrimes
541558Srgrimesstruct bufarea asblk;
551558Srgrimes#define altsblock (*asblk.b_un.b_fs)
561558Srgrimes#define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
571558Srgrimes
587585Sbdestatic int	readsb __P((int listerr));
597585Sbdestatic void	badsb __P((int listerr, char *s));
607585Sbdestatic int	calcsb __P((char *dev, int devfd, struct fs *fs));
617585Sbdestatic struct disklabel * getdisklabel __P((char *s, int fd));
621558Srgrimes
637585Sbde
647585Sbdeint
651558Srgrimessetup(dev)
661558Srgrimes	char *dev;
671558Srgrimes{
681558Srgrimes	long cg, size, asked, i, j;
691558Srgrimes	long bmapsize;
701558Srgrimes	struct disklabel *lp;
711558Srgrimes	off_t sizepb;
721558Srgrimes	struct stat statb;
731558Srgrimes	struct fs proto;
741558Srgrimes
751558Srgrimes	havesb = 0;
761558Srgrimes	fswritefd = -1;
771558Srgrimes	if (stat(dev, &statb) < 0) {
781558Srgrimes		printf("Can't stat %s: %s\n", dev, strerror(errno));
791558Srgrimes		return (0);
801558Srgrimes	}
811558Srgrimes	if ((statb.st_mode & S_IFMT) != S_IFCHR) {
821558Srgrimes		pfatal("%s is not a character device", dev);
831558Srgrimes		if (reply("CONTINUE") == 0)
841558Srgrimes			return (0);
851558Srgrimes	}
861558Srgrimes	if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
871558Srgrimes		printf("Can't open %s: %s\n", dev, strerror(errno));
881558Srgrimes		return (0);
891558Srgrimes	}
901558Srgrimes	if (preen == 0)
911558Srgrimes		printf("** %s", dev);
921558Srgrimes	if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) {
931558Srgrimes		fswritefd = -1;
941558Srgrimes		if (preen)
951558Srgrimes			pfatal("NO WRITE ACCESS");
961558Srgrimes		printf(" (NO WRITE)");
971558Srgrimes	}
981558Srgrimes	if (preen == 0)
991558Srgrimes		printf("\n");
1001558Srgrimes	fsmodified = 0;
1011558Srgrimes	lfdir = 0;
1021558Srgrimes	initbarea(&sblk);
1031558Srgrimes	initbarea(&asblk);
1041558Srgrimes	sblk.b_un.b_buf = malloc(SBSIZE);
1051558Srgrimes	asblk.b_un.b_buf = malloc(SBSIZE);
1061558Srgrimes	if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
1071558Srgrimes		errexit("cannot allocate space for superblock\n");
1087585Sbde	lp = getdisklabel((char *)NULL, fsreadfd);
1097585Sbde	if (lp)
1101558Srgrimes		dev_bsize = secsize = lp->d_secsize;
1111558Srgrimes	else
1121558Srgrimes		dev_bsize = secsize = DEV_BSIZE;
1131558Srgrimes	/*
1141558Srgrimes	 * Read in the superblock, looking for alternates if necessary
1151558Srgrimes	 */
1161558Srgrimes	if (readsb(1) == 0) {
1171558Srgrimes		if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
1181558Srgrimes			return(0);
1191558Srgrimes		if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
1201558Srgrimes			return (0);
1211558Srgrimes		for (cg = 0; cg < proto.fs_ncg; cg++) {
1221558Srgrimes			bflag = fsbtodb(&proto, cgsblock(&proto, cg));
1231558Srgrimes			if (readsb(0) != 0)
1241558Srgrimes				break;
1251558Srgrimes		}
1261558Srgrimes		if (cg >= proto.fs_ncg) {
1271558Srgrimes			printf("%s %s\n%s %s\n%s %s\n",
1281558Srgrimes				"SEARCH FOR ALTERNATE SUPER-BLOCK",
1291558Srgrimes				"FAILED. YOU MUST USE THE",
1301558Srgrimes				"-b OPTION TO FSCK TO SPECIFY THE",
1311558Srgrimes				"LOCATION OF AN ALTERNATE",
1321558Srgrimes				"SUPER-BLOCK TO SUPPLY NEEDED",
1331558Srgrimes				"INFORMATION; SEE fsck(8).");
1346280Sbde			bflag = 0;
1351558Srgrimes			return(0);
1361558Srgrimes		}
1371558Srgrimes		pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
1386280Sbde		bflag = 0;
1391558Srgrimes	}
1401558Srgrimes	maxfsblock = sblock.fs_size;
1411558Srgrimes	maxino = sblock.fs_ncg * sblock.fs_ipg;
1421558Srgrimes	/*
1431558Srgrimes	 * Check and potentially fix certain fields in the super block.
1441558Srgrimes	 */
1451558Srgrimes	if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
1461558Srgrimes		pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
1471558Srgrimes		if (reply("SET TO DEFAULT") == 1) {
1481558Srgrimes			sblock.fs_optim = FS_OPTTIME;
1491558Srgrimes			sbdirty();
1501558Srgrimes		}
1511558Srgrimes	}
1521558Srgrimes	if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
1531558Srgrimes		pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
1541558Srgrimes			sblock.fs_minfree);
1551558Srgrimes		if (reply("SET TO DEFAULT") == 1) {
1561558Srgrimes			sblock.fs_minfree = 10;
1571558Srgrimes			sbdirty();
1581558Srgrimes		}
1591558Srgrimes	}
1608871Srgrimes	if (sblock.fs_interleave < 1 ||
1611558Srgrimes	    sblock.fs_interleave > sblock.fs_nsect) {
1621558Srgrimes		pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK",
1631558Srgrimes			sblock.fs_interleave);
1641558Srgrimes		sblock.fs_interleave = 1;
1651558Srgrimes		if (preen)
1661558Srgrimes			printf(" (FIXED)\n");
1671558Srgrimes		if (preen || reply("SET TO DEFAULT") == 1) {
1681558Srgrimes			sbdirty();
1691558Srgrimes			dirty(&asblk);
1701558Srgrimes		}
1711558Srgrimes	}
1728871Srgrimes	if (sblock.fs_npsect < sblock.fs_nsect ||
1731558Srgrimes	    sblock.fs_npsect > sblock.fs_nsect*2) {
1741558Srgrimes		pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK",
1751558Srgrimes			sblock.fs_npsect);
1761558Srgrimes		sblock.fs_npsect = sblock.fs_nsect;
1771558Srgrimes		if (preen)
1781558Srgrimes			printf(" (FIXED)\n");
1791558Srgrimes		if (preen || reply("SET TO DEFAULT") == 1) {
1801558Srgrimes			sbdirty();
1811558Srgrimes			dirty(&asblk);
1821558Srgrimes		}
1831558Srgrimes	}
1841558Srgrimes	if (sblock.fs_inodefmt >= FS_44INODEFMT) {
1851558Srgrimes		newinofmt = 1;
1861558Srgrimes	} else {
1871558Srgrimes		sblock.fs_qbmask = ~sblock.fs_bmask;
1881558Srgrimes		sblock.fs_qfmask = ~sblock.fs_fmask;
1891558Srgrimes		newinofmt = 0;
1901558Srgrimes	}
1911558Srgrimes	/*
1921558Srgrimes	 * Convert to new inode format.
1931558Srgrimes	 */
1941558Srgrimes	if (cvtlevel >= 2 && sblock.fs_inodefmt < FS_44INODEFMT) {
1951558Srgrimes		if (preen)
1961558Srgrimes			pwarn("CONVERTING TO NEW INODE FORMAT\n");
1971558Srgrimes		else if (!reply("CONVERT TO NEW INODE FORMAT"))
1981558Srgrimes			return(0);
1991558Srgrimes		doinglevel2++;
2001558Srgrimes		sblock.fs_inodefmt = FS_44INODEFMT;
2011558Srgrimes		sizepb = sblock.fs_bsize;
2021558Srgrimes		sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1;
2031558Srgrimes		for (i = 0; i < NIADDR; i++) {
2041558Srgrimes			sizepb *= NINDIR(&sblock);
2051558Srgrimes			sblock.fs_maxfilesize += sizepb;
2061558Srgrimes		}
2071558Srgrimes		sblock.fs_maxsymlinklen = MAXSYMLINKLEN;
2081558Srgrimes		sblock.fs_qbmask = ~sblock.fs_bmask;
2091558Srgrimes		sblock.fs_qfmask = ~sblock.fs_fmask;
2101558Srgrimes		sbdirty();
2111558Srgrimes		dirty(&asblk);
2121558Srgrimes	}
2131558Srgrimes	/*
2141558Srgrimes	 * Convert to new cylinder group format.
2151558Srgrimes	 */
2161558Srgrimes	if (cvtlevel >= 1 && sblock.fs_postblformat == FS_42POSTBLFMT) {
2171558Srgrimes		if (preen)
2181558Srgrimes			pwarn("CONVERTING TO NEW CYLINDER GROUP FORMAT\n");
2191558Srgrimes		else if (!reply("CONVERT TO NEW CYLINDER GROUP FORMAT"))
2201558Srgrimes			return(0);
2211558Srgrimes		doinglevel1++;
2221558Srgrimes		sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT;
2231558Srgrimes		sblock.fs_nrpos = 8;
2241558Srgrimes		sblock.fs_postbloff =
2251558Srgrimes		    (char *)(&sblock.fs_opostbl[0][0]) -
2261558Srgrimes		    (char *)(&sblock.fs_link);
2271558Srgrimes		sblock.fs_rotbloff = &sblock.fs_space[0] -
2281558Srgrimes		    (u_char *)(&sblock.fs_link);
2291558Srgrimes		sblock.fs_cgsize =
2301558Srgrimes			fragroundup(&sblock, CGSIZE(&sblock));
2311558Srgrimes		sbdirty();
2321558Srgrimes		dirty(&asblk);
2331558Srgrimes	}
2341558Srgrimes	if (asblk.b_dirty) {
2351558Srgrimes		bcopy((char *)&sblock, (char *)&altsblock,
2361558Srgrimes			(size_t)sblock.fs_sbsize);
2371558Srgrimes		flush(fswritefd, &asblk);
2381558Srgrimes	}
2391558Srgrimes	/*
2401558Srgrimes	 * read in the summary info.
2411558Srgrimes	 */
2421558Srgrimes	asked = 0;
2431558Srgrimes	for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
2441558Srgrimes		size = sblock.fs_cssize - i < sblock.fs_bsize ?
2451558Srgrimes		    sblock.fs_cssize - i : sblock.fs_bsize;
2461558Srgrimes		sblock.fs_csp[j] = (struct csum *)calloc(1, (unsigned)size);
2471558Srgrimes		if (bread(fsreadfd, (char *)sblock.fs_csp[j],
2481558Srgrimes		    fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
2491558Srgrimes		    size) != 0 && !asked) {
2501558Srgrimes			pfatal("BAD SUMMARY INFORMATION");
2511558Srgrimes			if (reply("CONTINUE") == 0)
2521558Srgrimes				errexit("");
2531558Srgrimes			asked++;
2541558Srgrimes		}
2551558Srgrimes	}
2561558Srgrimes	/*
2571558Srgrimes	 * allocate and initialize the necessary maps
2581558Srgrimes	 */
2591558Srgrimes	bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(short));
2601558Srgrimes	blockmap = calloc((unsigned)bmapsize, sizeof (char));
2611558Srgrimes	if (blockmap == NULL) {
2621558Srgrimes		printf("cannot alloc %u bytes for blockmap\n",
2631558Srgrimes		    (unsigned)bmapsize);
2641558Srgrimes		goto badsb;
2651558Srgrimes	}
2661558Srgrimes	statemap = calloc((unsigned)(maxino + 1), sizeof(char));
2671558Srgrimes	if (statemap == NULL) {
2681558Srgrimes		printf("cannot alloc %u bytes for statemap\n",
2691558Srgrimes		    (unsigned)(maxino + 1));
2701558Srgrimes		goto badsb;
2711558Srgrimes	}
2721558Srgrimes	typemap = calloc((unsigned)(maxino + 1), sizeof(char));
2731558Srgrimes	if (typemap == NULL) {
2741558Srgrimes		printf("cannot alloc %u bytes for typemap\n",
2751558Srgrimes		    (unsigned)(maxino + 1));
2761558Srgrimes		goto badsb;
2771558Srgrimes	}
2781558Srgrimes	lncntp = (short *)calloc((unsigned)(maxino + 1), sizeof(short));
2791558Srgrimes	if (lncntp == NULL) {
2808871Srgrimes		printf("cannot alloc %u bytes for lncntp\n",
2811558Srgrimes		    (unsigned)(maxino + 1) * sizeof(short));
2821558Srgrimes		goto badsb;
2831558Srgrimes	}
2841558Srgrimes	numdirs = sblock.fs_cstotal.cs_ndir;
2851558Srgrimes	inplast = 0;
2861558Srgrimes	listmax = numdirs + 10;
2871558Srgrimes	inpsort = (struct inoinfo **)calloc((unsigned)listmax,
2881558Srgrimes	    sizeof(struct inoinfo *));
2891558Srgrimes	inphead = (struct inoinfo **)calloc((unsigned)numdirs,
2901558Srgrimes	    sizeof(struct inoinfo *));
2911558Srgrimes	if (inpsort == NULL || inphead == NULL) {
2928871Srgrimes		printf("cannot alloc %u bytes for inphead\n",
2931558Srgrimes		    (unsigned)numdirs * sizeof(struct inoinfo *));
2941558Srgrimes		goto badsb;
2951558Srgrimes	}
2961558Srgrimes	bufinit();
2971558Srgrimes	return (1);
2981558Srgrimes
2991558Srgrimesbadsb:
3001558Srgrimes	ckfini();
3011558Srgrimes	return (0);
3021558Srgrimes}
3031558Srgrimes
3041558Srgrimes/*
3051558Srgrimes * Read in the super block and its summary info.
3061558Srgrimes */
3077585Sbdestatic int
3081558Srgrimesreadsb(listerr)
3091558Srgrimes	int listerr;
3101558Srgrimes{
3111558Srgrimes	daddr_t super = bflag ? bflag : SBOFF / dev_bsize;
3121558Srgrimes
3131558Srgrimes	if (bread(fsreadfd, (char *)&sblock, super, (long)SBSIZE) != 0)
3141558Srgrimes		return (0);
3151558Srgrimes	sblk.b_bno = super;
3161558Srgrimes	sblk.b_size = SBSIZE;
3171558Srgrimes	/*
3181558Srgrimes	 * run a few consistency checks of the super block
3191558Srgrimes	 */
3201558Srgrimes	if (sblock.fs_magic != FS_MAGIC)
3211558Srgrimes		{ badsb(listerr, "MAGIC NUMBER WRONG"); return (0); }
3221558Srgrimes	if (sblock.fs_ncg < 1)
3231558Srgrimes		{ badsb(listerr, "NCG OUT OF RANGE"); return (0); }
3241558Srgrimes	if (sblock.fs_cpg < 1)
3251558Srgrimes		{ badsb(listerr, "CPG OUT OF RANGE"); return (0); }
3261558Srgrimes	if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl ||
3271558Srgrimes	    (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl)
3281558Srgrimes		{ badsb(listerr, "NCYL LESS THAN NCG*CPG"); return (0); }
3291558Srgrimes	if (sblock.fs_sbsize > SBSIZE)
3301558Srgrimes		{ badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); }
3311558Srgrimes	/*
3321558Srgrimes	 * Compute block size that the filesystem is based on,
3331558Srgrimes	 * according to fsbtodb, and adjust superblock block number
3341558Srgrimes	 * so we can tell if this is an alternate later.
3351558Srgrimes	 */
3361558Srgrimes	super *= dev_bsize;
3371558Srgrimes	dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
3381558Srgrimes	sblk.b_bno = super / dev_bsize;
3391558Srgrimes	if (bflag) {
3401558Srgrimes		havesb = 1;
3411558Srgrimes		return (1);
3421558Srgrimes	}
3431558Srgrimes	/*
3441558Srgrimes	 * Set all possible fields that could differ, then do check
3451558Srgrimes	 * of whole super block against an alternate super block.
3461558Srgrimes	 * When an alternate super-block is specified this check is skipped.
3471558Srgrimes	 */
3481558Srgrimes	getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize);
3491558Srgrimes	if (asblk.b_errs)
3501558Srgrimes		return (0);
3511558Srgrimes	altsblock.fs_link = sblock.fs_link;
3521558Srgrimes	altsblock.fs_rlink = sblock.fs_rlink;
3531558Srgrimes	altsblock.fs_time = sblock.fs_time;
3541558Srgrimes	altsblock.fs_cstotal = sblock.fs_cstotal;
3551558Srgrimes	altsblock.fs_cgrotor = sblock.fs_cgrotor;
3561558Srgrimes	altsblock.fs_fmod = sblock.fs_fmod;
3571558Srgrimes	altsblock.fs_clean = sblock.fs_clean;
3581558Srgrimes	altsblock.fs_ronly = sblock.fs_ronly;
3591558Srgrimes	altsblock.fs_flags = sblock.fs_flags;
3601558Srgrimes	altsblock.fs_maxcontig = sblock.fs_maxcontig;
3611558Srgrimes	altsblock.fs_minfree = sblock.fs_minfree;
3621558Srgrimes	altsblock.fs_optim = sblock.fs_optim;
3631558Srgrimes	altsblock.fs_rotdelay = sblock.fs_rotdelay;
3641558Srgrimes	altsblock.fs_maxbpg = sblock.fs_maxbpg;
3651558Srgrimes	bcopy((char *)sblock.fs_csp, (char *)altsblock.fs_csp,
3661558Srgrimes		sizeof sblock.fs_csp);
3671558Srgrimes	bcopy((char *)sblock.fs_fsmnt, (char *)altsblock.fs_fsmnt,
3681558Srgrimes		sizeof sblock.fs_fsmnt);
3691558Srgrimes	bcopy((char *)sblock.fs_sparecon, (char *)altsblock.fs_sparecon,
3701558Srgrimes		sizeof sblock.fs_sparecon);
3711558Srgrimes	/*
3721558Srgrimes	 * The following should not have to be copied.
3731558Srgrimes	 */
3741558Srgrimes	altsblock.fs_fsbtodb = sblock.fs_fsbtodb;
3751558Srgrimes	altsblock.fs_interleave = sblock.fs_interleave;
3761558Srgrimes	altsblock.fs_npsect = sblock.fs_npsect;
3771558Srgrimes	altsblock.fs_nrpos = sblock.fs_nrpos;
3781558Srgrimes	altsblock.fs_qbmask = sblock.fs_qbmask;
3791558Srgrimes	altsblock.fs_qfmask = sblock.fs_qfmask;
3801558Srgrimes	altsblock.fs_state = sblock.fs_state;
3811558Srgrimes	altsblock.fs_maxfilesize = sblock.fs_maxfilesize;
3821558Srgrimes	if (bcmp((char *)&sblock, (char *)&altsblock, (int)sblock.fs_sbsize)) {
3831558Srgrimes		badsb(listerr,
3841558Srgrimes		"VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
3851558Srgrimes		return (0);
3861558Srgrimes	}
3871558Srgrimes	havesb = 1;
3881558Srgrimes	return (1);
3891558Srgrimes}
3901558Srgrimes
3917585Sbdestatic void
3921558Srgrimesbadsb(listerr, s)
3931558Srgrimes	int listerr;
3941558Srgrimes	char *s;
3951558Srgrimes{
3961558Srgrimes
3971558Srgrimes	if (!listerr)
3981558Srgrimes		return;
3991558Srgrimes	if (preen)
4001558Srgrimes		printf("%s: ", cdevname);
4011558Srgrimes	pfatal("BAD SUPER BLOCK: %s\n", s);
4021558Srgrimes}
4031558Srgrimes
4041558Srgrimes/*
4051558Srgrimes * Calculate a prototype superblock based on information in the disk label.
4061558Srgrimes * When done the cgsblock macro can be calculated and the fs_ncg field
4071558Srgrimes * can be used. Do NOT attempt to use other macros without verifying that
4081558Srgrimes * their needed information is available!
4091558Srgrimes */
4107585Sbdeint
4111558Srgrimescalcsb(dev, devfd, fs)
4121558Srgrimes	char *dev;
4131558Srgrimes	int devfd;
4141558Srgrimes	register struct fs *fs;
4151558Srgrimes{
4161558Srgrimes	register struct disklabel *lp;
4171558Srgrimes	register struct partition *pp;
4181558Srgrimes	register char *cp;
4191558Srgrimes	int i;
4201558Srgrimes
4211558Srgrimes	cp = index(dev, '\0') - 1;
4227585Sbde	if (cp == (char *)-1 || ((*cp < 'a' || *cp > 'h') && !isdigit(*cp))) {
4231558Srgrimes		pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
4241558Srgrimes		return (0);
4251558Srgrimes	}
4261558Srgrimes	lp = getdisklabel(dev, devfd);
4271558Srgrimes	if (isdigit(*cp))
4281558Srgrimes		pp = &lp->d_partitions[0];
4291558Srgrimes	else
4301558Srgrimes		pp = &lp->d_partitions[*cp - 'a'];
4311558Srgrimes	if (pp->p_fstype != FS_BSDFFS) {
4321558Srgrimes		pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n",
4331558Srgrimes			dev, pp->p_fstype < FSMAXTYPES ?
4341558Srgrimes			fstypenames[pp->p_fstype] : "unknown");
4351558Srgrimes		return (0);
4361558Srgrimes	}
4371558Srgrimes	bzero((char *)fs, sizeof(struct fs));
4381558Srgrimes	fs->fs_fsize = pp->p_fsize;
4391558Srgrimes	fs->fs_frag = pp->p_frag;
4401558Srgrimes	fs->fs_cpg = pp->p_cpg;
4411558Srgrimes	fs->fs_size = pp->p_size;
4421558Srgrimes	fs->fs_ntrak = lp->d_ntracks;
4431558Srgrimes	fs->fs_nsect = lp->d_nsectors;
4441558Srgrimes	fs->fs_spc = lp->d_secpercyl;
4451558Srgrimes	fs->fs_nspf = fs->fs_fsize / lp->d_secsize;
4461558Srgrimes	fs->fs_sblkno = roundup(
4471558Srgrimes		howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize),
4481558Srgrimes		fs->fs_frag);
4491558Srgrimes	fs->fs_cgmask = 0xffffffff;
4501558Srgrimes	for (i = fs->fs_ntrak; i > 1; i >>= 1)
4511558Srgrimes		fs->fs_cgmask <<= 1;
4521558Srgrimes	if (!POWEROF2(fs->fs_ntrak))
4531558Srgrimes		fs->fs_cgmask <<= 1;
4541558Srgrimes	fs->fs_cgoffset = roundup(
4551558Srgrimes		howmany(fs->fs_nsect, NSPF(fs)), fs->fs_frag);
4561558Srgrimes	fs->fs_fpg = (fs->fs_cpg * fs->fs_spc) / NSPF(fs);
4571558Srgrimes	fs->fs_ncg = howmany(fs->fs_size / fs->fs_spc, fs->fs_cpg);
4581558Srgrimes	for (fs->fs_fsbtodb = 0, i = NSPF(fs); i > 1; i >>= 1)
4591558Srgrimes		fs->fs_fsbtodb++;
4601558Srgrimes	dev_bsize = lp->d_secsize;
4611558Srgrimes	return (1);
4621558Srgrimes}
4631558Srgrimes
4641558Srgrimesstruct disklabel *
4651558Srgrimesgetdisklabel(s, fd)
4661558Srgrimes	char *s;
4671558Srgrimes	int	fd;
4681558Srgrimes{
4691558Srgrimes	static struct disklabel lab;
4701558Srgrimes
4711558Srgrimes	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
4721558Srgrimes		if (s == NULL)
4731558Srgrimes			return ((struct disklabel *)NULL);
4741558Srgrimes		pwarn("ioctl (GCINFO): %s\n", strerror(errno));
4751558Srgrimes		errexit("%s: can't read disk label\n", s);
4761558Srgrimes	}
4771558Srgrimes	return (&lab);
4781558Srgrimes}
479