Home | History | Annotate | Line # | Download | only in fsck_ffs
setup.c revision 1.31
      1 /*	$NetBSD: setup.c,v 1.31 1997/09/20 06:16:33 lukem 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 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)setup.c	8.10 (Berkeley) 5/9/95";
     40 #else
     41 __RCSID("$NetBSD: setup.c,v 1.31 1997/09/20 06:16:33 lukem Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #define DKTYPENAMES
     46 #include <sys/param.h>
     47 #include <sys/time.h>
     48 #include <sys/stat.h>
     49 #include <sys/ioctl.h>
     50 #include <sys/disklabel.h>
     51 #include <sys/file.h>
     52 
     53 #include <ufs/ufs/dinode.h>
     54 #include <ufs/ffs/fs.h>
     55 
     56 #include <ctype.h>
     57 #include <err.h>
     58 #include <errno.h>
     59 #include <stdio.h>
     60 #include <stdlib.h>
     61 #include <string.h>
     62 
     63 #include "fsck.h"
     64 #include "extern.h"
     65 #include "fsutil.h"
     66 
     67 struct bufarea asblk;
     68 #define altsblock (*asblk.b_un.b_fs)
     69 #define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
     70 
     71 static void badsb __P((int, char *));
     72 static int calcsb __P((char *, int, struct fs *));
     73 static struct disklabel *getdisklabel __P((char *, int));
     74 static int readsb __P((int));
     75 
     76 /*
     77  * Read in a superblock finding an alternate if necessary.
     78  * Return 1 if successful, 0 if unsuccessful, -1 if filesystem
     79  * is already clean (preen mode only).
     80  */
     81 int
     82 setup(dev)
     83 	char *dev;
     84 {
     85 	long cg, size, asked, i, j;
     86 	long bmapsize;
     87 	struct disklabel *lp;
     88 	off_t sizepb;
     89 	struct stat statb;
     90 	struct fs proto;
     91 	int doskipclean;
     92 	u_int64_t maxfilesize;
     93 
     94 	havesb = 0;
     95 	fswritefd = -1;
     96 	doskipclean = skipclean;
     97 	if (stat(dev, &statb) < 0) {
     98 		printf("Can't stat %s: %s\n", dev, strerror(errno));
     99 		return (0);
    100 	}
    101 	if (!S_ISCHR(statb.st_mode)) {
    102 		pfatal("%s is not a character device", dev);
    103 		if (reply("CONTINUE") == 0)
    104 			return (0);
    105 	}
    106 	if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
    107 		printf("Can't open %s: %s\n", dev, strerror(errno));
    108 		return (0);
    109 	}
    110 	if (preen == 0)
    111 		printf("** %s", dev);
    112 	if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) {
    113 		fswritefd = -1;
    114 		if (preen)
    115 			pfatal("NO WRITE ACCESS");
    116 		printf(" (NO WRITE)");
    117 	}
    118 	if (preen == 0)
    119 		printf("\n");
    120 	fsmodified = 0;
    121 	lfdir = 0;
    122 	initbarea(&sblk);
    123 	initbarea(&asblk);
    124 	sblk.b_un.b_buf = malloc(SBSIZE);
    125 	asblk.b_un.b_buf = malloc(SBSIZE);
    126 	if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
    127 		errx(EEXIT, "cannot allocate space for superblock");
    128 	if ((lp = getdisklabel(NULL, fsreadfd)) != NULL)
    129 		dev_bsize = secsize = lp->d_secsize;
    130 	else
    131 		dev_bsize = secsize = DEV_BSIZE;
    132 	/*
    133 	 * Read in the superblock, looking for alternates if necessary
    134 	 */
    135 	if (readsb(1) == 0) {
    136 		skipclean = 0;
    137 		if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
    138 			return(0);
    139 		if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
    140 			return (0);
    141 		for (cg = 0; cg < proto.fs_ncg; cg++) {
    142 			bflag = fsbtodb(&proto, cgsblock(&proto, cg));
    143 			if (readsb(0) != 0)
    144 				break;
    145 		}
    146 		if (cg >= proto.fs_ncg) {
    147 			printf("%s %s\n%s %s\n%s %s\n",
    148 				"SEARCH FOR ALTERNATE SUPER-BLOCK",
    149 				"FAILED. YOU MUST USE THE",
    150 				"-b OPTION TO FSCK_FFS TO SPECIFY THE",
    151 				"LOCATION OF AN ALTERNATE",
    152 				"SUPER-BLOCK TO SUPPLY NEEDED",
    153 				"INFORMATION; SEE fsck_ffs(8).");
    154 			return(0);
    155 		}
    156 		doskipclean = 0;
    157 		pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
    158 	}
    159 	if (debug)
    160 		printf("clean = %d\n", sblock.fs_clean);
    161 	if (sblock.fs_clean & FS_ISCLEAN) {
    162 		if (doskipclean) {
    163 			pwarn("%sile system is clean; not checking\n",
    164 			    preen ? "f" : "** F");
    165 			return (-1);
    166 		}
    167 		if (!preen)
    168 			pwarn("** File system is already clean\n");
    169 	}
    170 	maxfsblock = sblock.fs_size;
    171 	maxino = sblock.fs_ncg * sblock.fs_ipg;
    172 	sizepb = sblock.fs_bsize;
    173 	maxfilesize = sblock.fs_bsize * NDADDR - 1;
    174 	for (i = 0; i < NIADDR; i++) {
    175 		sizepb *= NINDIR(&sblock);
    176 		maxfilesize += sizepb;
    177 	}
    178 	/*
    179 	 * Check and potentially fix certain fields in the super block.
    180 	 */
    181 	if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
    182 		pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
    183 		if (reply("SET TO DEFAULT") == 1) {
    184 			sblock.fs_optim = FS_OPTTIME;
    185 			sbdirty();
    186 		}
    187 	}
    188 	if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
    189 		pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
    190 			sblock.fs_minfree);
    191 		if (reply("SET TO DEFAULT") == 1) {
    192 			sblock.fs_minfree = 10;
    193 			sbdirty();
    194 		}
    195 	}
    196 	if (sblock.fs_interleave < 1 ||
    197 	    sblock.fs_interleave > sblock.fs_nsect) {
    198 		pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK",
    199 			sblock.fs_interleave);
    200 		sblock.fs_interleave = 1;
    201 		if (preen)
    202 			printf(" (FIXED)\n");
    203 		if (preen || reply("SET TO DEFAULT") == 1) {
    204 			sbdirty();
    205 			dirty(&asblk);
    206 		}
    207 	}
    208 	if (sblock.fs_npsect < sblock.fs_nsect ||
    209 	    sblock.fs_npsect > sblock.fs_nsect*2) {
    210 		pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK",
    211 			sblock.fs_npsect);
    212 		sblock.fs_npsect = sblock.fs_nsect;
    213 		if (preen)
    214 			printf(" (FIXED)\n");
    215 		if (preen || reply("SET TO DEFAULT") == 1) {
    216 			sbdirty();
    217 			dirty(&asblk);
    218 		}
    219 	}
    220 	if (sblock.fs_bmask != ~(sblock.fs_bsize - 1)) {
    221 		pwarn("INCORRECT BMASK=0x%x IN SUPERBLOCK",
    222 			sblock.fs_bmask);
    223 		sblock.fs_bmask = ~(sblock.fs_bsize - 1);
    224 		if (preen)
    225 			printf(" (FIXED)\n");
    226 		if (preen || reply("FIX") == 1) {
    227 			sbdirty();
    228 			dirty(&asblk);
    229 		}
    230 	}
    231 	if (sblock.fs_fmask != ~(sblock.fs_fsize - 1)) {
    232 		pwarn("INCORRECT FMASK=0x%x IN SUPERBLOCK",
    233 			sblock.fs_fmask);
    234 		sblock.fs_fmask = ~(sblock.fs_fsize - 1);
    235 		if (preen)
    236 			printf(" (FIXED)\n");
    237 		if (preen || reply("FIX") == 1) {
    238 			sbdirty();
    239 			dirty(&asblk);
    240 		}
    241 	}
    242 	if (sblock.fs_inodefmt >= FS_44INODEFMT) {
    243 		if (sblock.fs_maxfilesize != maxfilesize) {
    244 			pwarn("INCORRECT MAXFILESIZE=%qd IN SUPERBLOCK",
    245 			    (unsigned long long)sblock.fs_maxfilesize);
    246 			sblock.fs_maxfilesize = maxfilesize;
    247 			if (preen)
    248 				printf(" (FIXED)\n");
    249 			if (preen || reply("FIX") == 1) {
    250 				sbdirty();
    251 				dirty(&asblk);
    252 			}
    253 		}
    254 		if (sblock.fs_maxsymlinklen != MAXSYMLINKLEN) {
    255 			pwarn("INCORRECT MAXSYMLINKLEN=%d IN SUPERBLOCK",
    256 				sblock.fs_maxsymlinklen);
    257 			sblock.fs_maxsymlinklen = MAXSYMLINKLEN;
    258 			if (preen)
    259 				printf(" (FIXED)\n");
    260 			if (preen || reply("FIX") == 1) {
    261 				sbdirty();
    262 				dirty(&asblk);
    263 			}
    264 		}
    265 		if (sblock.fs_qbmask != ~sblock.fs_bmask) {
    266 			pwarn("INCORRECT QBMASK=%qx IN SUPERBLOCK",
    267 			    (unsigned long long)sblock.fs_qbmask);
    268 			sblock.fs_qbmask = ~sblock.fs_bmask;
    269 			if (preen)
    270 				printf(" (FIXED)\n");
    271 			if (preen || reply("FIX") == 1) {
    272 				sbdirty();
    273 				dirty(&asblk);
    274 			}
    275 		}
    276 		if (sblock.fs_qfmask != ~sblock.fs_fmask) {
    277 			pwarn("INCORRECT QFMASK=%qx IN SUPERBLOCK",
    278 			    (unsigned long long)sblock.fs_qfmask);
    279 			sblock.fs_qfmask = ~sblock.fs_fmask;
    280 			if (preen)
    281 				printf(" (FIXED)\n");
    282 			if (preen || reply("FIX") == 1) {
    283 				sbdirty();
    284 				dirty(&asblk);
    285 			}
    286 		}
    287 		newinofmt = 1;
    288 	} else {
    289 		sblock.fs_qbmask = ~sblock.fs_bmask;
    290 		sblock.fs_qfmask = ~sblock.fs_fmask;
    291 		newinofmt = 0;
    292 	}
    293 	/*
    294 	 * Convert to new inode format.
    295 	 */
    296 	if (cvtlevel >= 2 && sblock.fs_inodefmt < FS_44INODEFMT) {
    297 		if (preen)
    298 			pwarn("CONVERTING TO NEW INODE FORMAT\n");
    299 		else if (!reply("CONVERT TO NEW INODE FORMAT"))
    300 			return(0);
    301 		doinglevel2++;
    302 		sblock.fs_inodefmt = FS_44INODEFMT;
    303 		sblock.fs_maxfilesize = maxfilesize;
    304 		sblock.fs_maxsymlinklen = MAXSYMLINKLEN;
    305 		sblock.fs_qbmask = ~sblock.fs_bmask;
    306 		sblock.fs_qfmask = ~sblock.fs_fmask;
    307 		sbdirty();
    308 		dirty(&asblk);
    309 	}
    310 	/*
    311 	 * Convert to new cylinder group format.
    312 	 */
    313 	if (cvtlevel >= 1 && sblock.fs_postblformat == FS_42POSTBLFMT) {
    314 		if (preen)
    315 			pwarn("CONVERTING TO NEW CYLINDER GROUP FORMAT\n");
    316 		else if (!reply("CONVERT TO NEW CYLINDER GROUP FORMAT"))
    317 			return(0);
    318 		doinglevel1++;
    319 		sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT;
    320 		sblock.fs_nrpos = 8;
    321 		sblock.fs_postbloff =
    322 		    (char *)(&sblock.fs_opostbl[0][0]) -
    323 		    (char *)(&sblock.fs_firstfield);
    324 		sblock.fs_rotbloff = &sblock.fs_space[0] -
    325 		    (u_char *)(&sblock.fs_firstfield);
    326 		sblock.fs_cgsize =
    327 			fragroundup(&sblock, CGSIZE(&sblock));
    328 		sbdirty();
    329 		dirty(&asblk);
    330 	}
    331 	if (asblk.b_dirty && !bflag) {
    332 		memmove(&altsblock, &sblock, (size_t)sblock.fs_sbsize);
    333 		flush(fswritefd, &asblk);
    334 	}
    335 	/*
    336 	 * read in the summary info.
    337 	 */
    338 	asked = 0;
    339 	for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
    340 		size = sblock.fs_cssize - i < sblock.fs_bsize ?
    341 		    sblock.fs_cssize - i : sblock.fs_bsize;
    342 		sblock.fs_csp[j] = (struct csum *)calloc(1, (unsigned)size);
    343 		if (bread(fsreadfd, (char *)sblock.fs_csp[j],
    344 		    fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
    345 		    size) != 0 && !asked) {
    346 			pfatal("BAD SUMMARY INFORMATION");
    347 			if (reply("CONTINUE") == 0)
    348 				exit(EEXIT);
    349 			asked++;
    350 		}
    351 	}
    352 	/*
    353 	 * allocate and initialize the necessary maps
    354 	 */
    355 	bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
    356 	blockmap = calloc((unsigned)bmapsize, sizeof (char));
    357 	if (blockmap == NULL) {
    358 		printf("cannot alloc %u bytes for blockmap\n",
    359 		    (unsigned)bmapsize);
    360 		goto badsblabel;
    361 	}
    362 	statemap = calloc((unsigned)(maxino + 1), sizeof(char));
    363 	if (statemap == NULL) {
    364 		printf("cannot alloc %u bytes for statemap\n",
    365 		    (unsigned)(maxino + 1));
    366 		goto badsblabel;
    367 	}
    368 	typemap = calloc((unsigned)(maxino + 1), sizeof(char));
    369 	if (typemap == NULL) {
    370 		printf("cannot alloc %u bytes for typemap\n",
    371 		    (unsigned)(maxino + 1));
    372 		goto badsblabel;
    373 	}
    374 	lncntp = (int16_t *)calloc((unsigned)(maxino + 1), sizeof(int16_t));
    375 	if (lncntp == NULL) {
    376 		printf("cannot alloc %u bytes for lncntp\n",
    377 		    (unsigned)((maxino + 1) * sizeof(int16_t)));
    378 		goto badsblabel;
    379 	}
    380 	numdirs = sblock.fs_cstotal.cs_ndir;
    381 	inplast = 0;
    382 	listmax = numdirs + 10;
    383 	inpsort = (struct inoinfo **)calloc((unsigned)listmax,
    384 	    sizeof(struct inoinfo *));
    385 	inphead = (struct inoinfo **)calloc((unsigned)numdirs,
    386 	    sizeof(struct inoinfo *));
    387 	if (inpsort == NULL || inphead == NULL) {
    388 		printf("cannot alloc %u bytes for inphead\n",
    389 		    (unsigned)(numdirs * sizeof(struct inoinfo *)));
    390 		goto badsblabel;
    391 	}
    392 	bufinit();
    393 	return (1);
    394 
    395 badsblabel:
    396 	ckfini(0);
    397 	return (0);
    398 }
    399 
    400 /*
    401  * Read in the super block and its summary info.
    402  */
    403 static int
    404 readsb(listerr)
    405 	int listerr;
    406 {
    407 	ufs_daddr_t super = bflag ? bflag : SBOFF / dev_bsize;
    408 
    409 	if (bread(fsreadfd, (char *)&sblock, super, (long)SBSIZE) != 0)
    410 		return (0);
    411 	sblk.b_bno = super;
    412 	sblk.b_size = SBSIZE;
    413 	/*
    414 	 * run a few consistency checks of the super block
    415 	 */
    416 	if (sblock.fs_magic != FS_MAGIC)
    417 		{ badsb(listerr, "MAGIC NUMBER WRONG"); return (0); }
    418 	if (sblock.fs_ncg < 1)
    419 		{ badsb(listerr, "NCG OUT OF RANGE"); return (0); }
    420 	if (sblock.fs_cpg < 1)
    421 		{ badsb(listerr, "CPG OUT OF RANGE"); return (0); }
    422 	if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl ||
    423 	    (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl)
    424 		{ badsb(listerr, "NCYL LESS THAN NCG*CPG"); return (0); }
    425 	if (sblock.fs_sbsize > SBSIZE)
    426 		{ badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); }
    427 	/*
    428 	 * Compute block size that the filesystem is based on,
    429 	 * according to fsbtodb, and adjust superblock block number
    430 	 * so we can tell if this is an alternate later.
    431 	 */
    432 	super *= dev_bsize;
    433 	dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
    434 	sblk.b_bno = super / dev_bsize;
    435 	if (bflag) {
    436 		havesb = 1;
    437 		return (1);
    438 	}
    439 	/*
    440 	 * Set all possible fields that could differ, then do check
    441 	 * of whole super block against an alternate super block.
    442 	 * When an alternate super-block is specified this check is skipped.
    443 	 */
    444 	getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize);
    445 	if (asblk.b_errs)
    446 		return (0);
    447 	altsblock.fs_firstfield = sblock.fs_firstfield;
    448 	altsblock.fs_fscktime = sblock.fs_fscktime;
    449 	altsblock.fs_unused_1 = sblock.fs_unused_1;
    450 	altsblock.fs_time = sblock.fs_time;
    451 	altsblock.fs_cstotal = sblock.fs_cstotal;
    452 	altsblock.fs_cgrotor = sblock.fs_cgrotor;
    453 	altsblock.fs_fmod = sblock.fs_fmod;
    454 	altsblock.fs_clean = sblock.fs_clean;
    455 	altsblock.fs_ronly = sblock.fs_ronly;
    456 	altsblock.fs_flags = sblock.fs_flags;
    457 	altsblock.fs_maxcontig = sblock.fs_maxcontig;
    458 	altsblock.fs_minfree = sblock.fs_minfree;
    459 	altsblock.fs_optim = sblock.fs_optim;
    460 	altsblock.fs_rotdelay = sblock.fs_rotdelay;
    461 	altsblock.fs_maxbpg = sblock.fs_maxbpg;
    462 	memmove(altsblock.fs_csp, sblock.fs_csp, sizeof sblock.fs_csp);
    463 	altsblock.fs_maxcluster = sblock.fs_maxcluster;
    464 	memmove(altsblock.fs_fsmnt, sblock.fs_fsmnt, sizeof sblock.fs_fsmnt);
    465 	memmove(altsblock.fs_sparecon,
    466 		sblock.fs_sparecon, sizeof sblock.fs_sparecon);
    467 	/*
    468 	 * The following should not have to be copied.
    469 	 */
    470 	altsblock.fs_fsbtodb = sblock.fs_fsbtodb;
    471 	altsblock.fs_interleave = sblock.fs_interleave;
    472 	altsblock.fs_npsect = sblock.fs_npsect;
    473 	altsblock.fs_nrpos = sblock.fs_nrpos;
    474 	altsblock.fs_state = sblock.fs_state;
    475 	altsblock.fs_qbmask = sblock.fs_qbmask;
    476 	altsblock.fs_qfmask = sblock.fs_qfmask;
    477 	altsblock.fs_state = sblock.fs_state;
    478 	altsblock.fs_maxfilesize = sblock.fs_maxfilesize;
    479 	if (memcmp(&sblock, &altsblock, (int)sblock.fs_sbsize)) {
    480 		if (debug) {
    481 			long *nlp, *olp, *endlp;
    482 
    483 			printf("superblock mismatches\n");
    484 			nlp = (long *)&altsblock;
    485 			olp = (long *)&sblock;
    486 			endlp = olp + (sblock.fs_sbsize / sizeof *olp);
    487 			for ( ; olp < endlp; olp++, nlp++) {
    488 				if (*olp == *nlp)
    489 					continue;
    490 				printf("offset %ld, original %ld, alternate %ld\n",
    491 				    (long)(olp - (long *)&sblock), *olp, *nlp);
    492 			}
    493 		}
    494 		badsb(listerr,
    495 		"VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
    496 		return (0);
    497 	}
    498 	havesb = 1;
    499 	return (1);
    500 }
    501 
    502 static void
    503 badsb(listerr, s)
    504 	int listerr;
    505 	char *s;
    506 {
    507 
    508 	if (!listerr)
    509 		return;
    510 	if (preen)
    511 		printf("%s: ", cdevname());
    512 	pfatal("BAD SUPER BLOCK: %s\n", s);
    513 }
    514 
    515 /*
    516  * Calculate a prototype superblock based on information in the disk label.
    517  * When done the cgsblock macro can be calculated and the fs_ncg field
    518  * can be used. Do NOT attempt to use other macros without verifying that
    519  * their needed information is available!
    520  */
    521 static int
    522 calcsb(dev, devfd, fs)
    523 	char *dev;
    524 	int devfd;
    525 	struct fs *fs;
    526 {
    527 	struct disklabel *lp;
    528 	struct partition *pp;
    529 	char *cp;
    530 	int i;
    531 
    532 	cp = strchr(dev, '\0') - 1;
    533 	if ((cp == (char *)-1 || (*cp < 'a' || *cp > 'h')) && !isdigit(*cp)) {
    534 		pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
    535 		return (0);
    536 	}
    537 	lp = getdisklabel(dev, devfd);
    538 	if (isdigit(*cp))
    539 		pp = &lp->d_partitions[0];
    540 	else
    541 		pp = &lp->d_partitions[*cp - 'a'];
    542 	if (pp->p_fstype != FS_BSDFFS) {
    543 		pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n",
    544 			dev, pp->p_fstype < FSMAXTYPES ?
    545 			fstypenames[pp->p_fstype] : "unknown");
    546 		return (0);
    547 	}
    548 	memset(fs, 0, sizeof(struct fs));
    549 	fs->fs_fsize = pp->p_fsize;
    550 	fs->fs_frag = pp->p_frag;
    551 	fs->fs_cpg = pp->p_cpg;
    552 	fs->fs_size = pp->p_size;
    553 	fs->fs_ntrak = lp->d_ntracks;
    554 	fs->fs_nsect = lp->d_nsectors;
    555 	fs->fs_spc = lp->d_secpercyl;
    556 	fs->fs_nspf = fs->fs_fsize / lp->d_secsize;
    557 	fs->fs_sblkno = roundup(
    558 		howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize),
    559 		fs->fs_frag);
    560 	fs->fs_cgmask = 0xffffffff;
    561 	for (i = fs->fs_ntrak; i > 1; i >>= 1)
    562 		fs->fs_cgmask <<= 1;
    563 	if (!POWEROF2(fs->fs_ntrak))
    564 		fs->fs_cgmask <<= 1;
    565 	fs->fs_cgoffset = roundup(
    566 		howmany(fs->fs_nsect, NSPF(fs)), fs->fs_frag);
    567 	fs->fs_fpg = (fs->fs_cpg * fs->fs_spc) / NSPF(fs);
    568 	fs->fs_ncg = howmany(fs->fs_size / fs->fs_spc, fs->fs_cpg);
    569 	for (fs->fs_fsbtodb = 0, i = NSPF(fs); i > 1; i >>= 1)
    570 		fs->fs_fsbtodb++;
    571 	dev_bsize = lp->d_secsize;
    572 	return (1);
    573 }
    574 
    575 static struct disklabel *
    576 getdisklabel(s, fd)
    577 	char *s;
    578 	int	fd;
    579 {
    580 	static struct disklabel lab;
    581 
    582 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
    583 		if (s == NULL)
    584 			return ((struct disklabel *)NULL);
    585 		pwarn("ioctl (GCINFO): %s\n", strerror(errno));
    586 		errx(EEXIT, "%s: can't read disk label", s);
    587 	}
    588 	return (&lab);
    589 }
    590