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