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