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