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