Home | History | Annotate | Line # | Download | only in ffs
mkfs.c revision 1.3
      1 /*	$NetBSD: mkfs.c,v 1.3 2002/01/07 05:07:51 lukem Exp $	*/
      2 /* From NetBSD: mkfs.c,v 1.55 2001/09/06 02:16:01 lukem Exp $ */
      3 
      4 /*
      5  * Copyright (c) 1980, 1989, 1993
      6  *	The Regents of the University of California.  All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 #ifndef lint
     39 #if 0
     40 static char sccsid[] = "@(#)mkfs.c	8.11 (Berkeley) 5/3/95";
     41 #else
     42 __RCSID("$NetBSD: mkfs.c,v 1.3 2002/01/07 05:07:51 lukem Exp $");
     43 #endif
     44 #endif /* not lint */
     45 
     46 #include <sys/param.h>
     47 #include <sys/time.h>
     48 #include <sys/resource.h>
     49 
     50 #include <err.h>
     51 #include <stdio.h>
     52 #include <stdlib.h>
     53 #include <string.h>
     54 #include <unistd.h>
     55 
     56 #include "makefs.h"
     57 
     58 #include "ufs/ufs/dir.h"
     59 #include "ufs/ufs/inode.h"
     60 #include "ufs/ufs/ufs_bswap.h"
     61 #include "ufs/ffs/fs.h"
     62 
     63 #include "ffs/ffs_extern.h"
     64 #include "ffs/newfs_extern.h"
     65 
     66 static void initcg(int, time_t, const fsinfo_t *);
     67 static int32_t calcipg(int32_t, int32_t, off_t *);
     68 static void swap_cg(struct cg *, struct cg *);
     69 
     70 static int count_digits(int);
     71 
     72 /*
     73  * make file system for cylinder-group style file systems
     74  */
     75 
     76 /*
     77  * We limit the size of the inode map to be no more than a
     78  * third of the cylinder group space, since we must leave at
     79  * least an equal amount of space for the block map.
     80  *
     81  * N.B.: MAXIPG must be a multiple of INOPB(fs).
     82  */
     83 #define MAXIPG(fs)	roundup((fs)->fs_bsize * NBBY / 3, INOPB(fs))
     84 
     85 #define UMASK		0755
     86 #define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
     87 
     88 union {
     89 	struct fs fs;
     90 	char pad[SBSIZE];
     91 } fsun;
     92 #define	sblock	fsun.fs
     93 
     94 union {
     95 	struct cg cg;
     96 	char pad[MAXBSIZE];
     97 } cgun;
     98 #define	acg	cgun.cg
     99 
    100 struct dinode zino[MAXBSIZE / DINODE_SIZE];
    101 
    102 char writebuf[MAXBSIZE];
    103 
    104 static	int	Oflag;		/* format as an 4.3BSD file system */
    105 static	int	fssize;		/* file system size */
    106 static	int	ntracks;	/* # tracks/cylinder */
    107 static	int	nsectors;	/* # sectors/track */
    108 static	int	nphyssectors;	/* # sectors/track including spares */
    109 static	int	secpercyl;	/* sectors per cylinder */
    110 static	int	sectorsize;	/* bytes/sector */
    111 static	int	rpm;		/* revolutions/minute of drive */
    112 static	int	interleave;	/* hardware sector interleave */
    113 static	int	trackskew;	/* sector 0 skew, per track */
    114 static	int	fsize;		/* fragment size */
    115 static	int	bsize;		/* block size */
    116 static	int	cpg;		/* cylinders/cylinder group */
    117 static	int	cpgflg;		/* cylinders/cylinder group flag was given */
    118 static	int	minfree;	/* free space threshold */
    119 static	int	opt;		/* optimization preference (space or time) */
    120 static	int	density;	/* number of bytes per inode */
    121 static	int	maxcontig;	/* max contiguous blocks to allocate */
    122 static	int	rotdelay;	/* rotational delay between blocks */
    123 static	int	maxbpg;		/* maximum blocks per file in a cyl group */
    124 static	int	nrpos;		/* # of distinguished rotational positions */
    125 static	int	bbsize;		/* boot block size */
    126 static	int	sbsize;		/* superblock size */
    127 static	int	avgfilesize;	/* expected average file size */
    128 static	int	avgfpdir;	/* expected number of files per directory */
    129 
    130 
    131 struct fs *
    132 ffs_mkfs(const char *fsys, const fsinfo_t *fsopts)
    133 {
    134 	int32_t i, mincpc, mincpg, inospercg;
    135 	int32_t cylno, rpos, blk, j, warned = 0;
    136 	int32_t used, mincpgcnt, bpcg;
    137 	off_t usedb;
    138 	int32_t mapcramped, inodecramped;
    139 	int32_t postblsize, rotblsize, totalsbsize;
    140 	long long sizepb;
    141 	void *space;
    142 	int size, blks;
    143 	int nprintcols, printcolwidth;
    144 
    145 	Oflag = 0;
    146 	fssize =	fsopts->size / fsopts->sectorsize;
    147 	ntracks =	fsopts->ntracks;
    148 	nsectors =	fsopts->nsectors;
    149 	nphyssectors =	fsopts->nsectors;	/* XXX: no trackspares */
    150 	secpercyl =	nsectors * ntracks;
    151 	sectorsize =	fsopts->sectorsize;
    152 	rpm =		fsopts->rpm;
    153 	interleave =	1;				/* XXX: HCD */
    154 	trackskew =	0;				/* XXX: HCD */
    155 	fsize =		fsopts->fsize;
    156 	bsize =		fsopts->bsize;
    157 	cpg =		fsopts->cpg;
    158 	cpgflg =	1;
    159 	minfree =	fsopts->minfree;
    160 	opt =		fsopts->optimization;
    161 	density =	fsopts->density;
    162 	maxcontig =	fsopts->maxcontig;
    163 	rotdelay =	fsopts->rotdelay;
    164 	maxbpg =	fsopts->maxbpg;
    165 	nrpos =		fsopts->nrpos;
    166 	bbsize =	BBSIZE;
    167 	sbsize =	SBSIZE;
    168 	avgfilesize = 	fsopts->avgfilesize;
    169 	avgfpdir = 	fsopts->avgfpdir;
    170 
    171 	if (Oflag) {
    172 		sblock.fs_inodefmt = FS_42INODEFMT;
    173 		sblock.fs_maxsymlinklen = 0;
    174 	} else {
    175 		sblock.fs_inodefmt = FS_44INODEFMT;
    176 		sblock.fs_maxsymlinklen = MAXSYMLINKLEN;
    177 	}
    178 	/*
    179 	 * Validate the given file system size.
    180 	 * Verify that its last block can actually be accessed.
    181 	 */
    182 	if (fssize <= 0)
    183 		printf("preposterous size %d\n", fssize), exit(13);
    184 	ffs_wtfs(fssize - 1, sectorsize, (char *)&sblock, fsopts);
    185 
    186 	/*
    187 	 * collect and verify the sector and track info
    188 	 */
    189 	sblock.fs_nsect = nsectors;
    190 	sblock.fs_ntrak = ntracks;
    191 	if (sblock.fs_ntrak <= 0)
    192 		printf("preposterous ntrak %d\n", sblock.fs_ntrak), exit(14);
    193 	if (sblock.fs_nsect <= 0)
    194 		printf("preposterous nsect %d\n", sblock.fs_nsect), exit(15);
    195 	/*
    196 	 * collect and verify the filesystem density info
    197 	 */
    198 	sblock.fs_avgfilesize = avgfilesize;
    199 	sblock.fs_avgfpdir = avgfpdir;
    200 	if (sblock.fs_avgfilesize <= 0)
    201 		printf("illegal expected average file size %d\n",
    202 		    sblock.fs_avgfilesize), exit(14);
    203 	if (sblock.fs_avgfpdir <= 0)
    204 		printf("illegal expected number of files per directory %d\n",
    205 		    sblock.fs_avgfpdir), exit(15);
    206 	/*
    207 	 * collect and verify the block and fragment sizes
    208 	 */
    209 	sblock.fs_bsize = bsize;
    210 	sblock.fs_fsize = fsize;
    211 	if (!POWEROF2(sblock.fs_bsize)) {
    212 		printf("block size must be a power of 2, not %d\n",
    213 		    sblock.fs_bsize);
    214 		exit(16);
    215 	}
    216 	if (!POWEROF2(sblock.fs_fsize)) {
    217 		printf("fragment size must be a power of 2, not %d\n",
    218 		    sblock.fs_fsize);
    219 		exit(17);
    220 	}
    221 	if (sblock.fs_fsize < sectorsize) {
    222 		printf("fragment size %d is too small, minimum is %d\n",
    223 		    sblock.fs_fsize, sectorsize);
    224 		exit(18);
    225 	}
    226 	if (sblock.fs_bsize < MINBSIZE) {
    227 		printf("block size %d is too small, minimum is %d\n",
    228 		    sblock.fs_bsize, MINBSIZE);
    229 		exit(19);
    230 	}
    231 	if (sblock.fs_bsize < sblock.fs_fsize) {
    232 		printf("block size (%d) cannot be smaller than fragment size (%d)\n",
    233 		    sblock.fs_bsize, sblock.fs_fsize);
    234 		exit(20);
    235 	}
    236 	sblock.fs_bmask = ~(sblock.fs_bsize - 1);
    237 	sblock.fs_fmask = ~(sblock.fs_fsize - 1);
    238 	sblock.fs_qbmask = ~sblock.fs_bmask;
    239 	sblock.fs_qfmask = ~sblock.fs_fmask;
    240 	for (sblock.fs_bshift = 0, i = sblock.fs_bsize; i > 1; i >>= 1)
    241 		sblock.fs_bshift++;
    242 	for (sblock.fs_fshift = 0, i = sblock.fs_fsize; i > 1; i >>= 1)
    243 		sblock.fs_fshift++;
    244 	sblock.fs_frag = numfrags(&sblock, sblock.fs_bsize);
    245 	for (sblock.fs_fragshift = 0, i = sblock.fs_frag; i > 1; i >>= 1)
    246 		sblock.fs_fragshift++;
    247 	if (sblock.fs_frag > MAXFRAG) {
    248 		printf("fragment size %d is too small, "
    249 			"minimum with block size %d is %d\n",
    250 		    sblock.fs_fsize, sblock.fs_bsize,
    251 		    sblock.fs_bsize / MAXFRAG);
    252 		exit(21);
    253 	}
    254 	sblock.fs_nrpos = nrpos;
    255 	sblock.fs_nindir = sblock.fs_bsize / sizeof(daddr_t);
    256 	sblock.fs_inopb = sblock.fs_bsize / DINODE_SIZE;
    257 	sblock.fs_nspf = sblock.fs_fsize / sectorsize;
    258 	for (sblock.fs_fsbtodb = 0, i = NSPF(&sblock); i > 1; i >>= 1)
    259 		sblock.fs_fsbtodb++;
    260 	sblock.fs_sblkno =
    261 	    roundup(howmany(bbsize + sbsize, sblock.fs_fsize), sblock.fs_frag);
    262 	sblock.fs_cblkno = (daddr_t)(sblock.fs_sblkno +
    263 	    roundup(howmany(sbsize, sblock.fs_fsize), sblock.fs_frag));
    264 	sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag;
    265 	sblock.fs_cgoffset = roundup(
    266 	    howmany(sblock.fs_nsect, NSPF(&sblock)), sblock.fs_frag);
    267 	for (sblock.fs_cgmask = 0xffffffff, i = sblock.fs_ntrak; i > 1; i >>= 1)
    268 		sblock.fs_cgmask <<= 1;
    269 	if (!POWEROF2(sblock.fs_ntrak))
    270 		sblock.fs_cgmask <<= 1;
    271 	sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1;
    272 	for (sizepb = sblock.fs_bsize, i = 0; i < NIADDR; i++) {
    273 		sizepb *= NINDIR(&sblock);
    274 		sblock.fs_maxfilesize += sizepb;
    275 	}
    276 	/*
    277 	 * Validate specified/determined secpercyl
    278 	 * and calculate minimum cylinders per group.
    279 	 */
    280 	sblock.fs_spc = secpercyl;
    281 	for (sblock.fs_cpc = NSPB(&sblock), i = sblock.fs_spc;
    282 	     sblock.fs_cpc > 1 && (i & 1) == 0;
    283 	     sblock.fs_cpc >>= 1, i >>= 1)
    284 		/* void */;
    285 	mincpc = sblock.fs_cpc;
    286 	bpcg = sblock.fs_spc * sectorsize;
    287 	inospercg = roundup(bpcg / DINODE_SIZE, INOPB(&sblock));
    288 	if (inospercg > MAXIPG(&sblock))
    289 		inospercg = MAXIPG(&sblock);
    290 	used = (sblock.fs_iblkno + inospercg / INOPF(&sblock)) * NSPF(&sblock);
    291 	mincpgcnt = howmany(sblock.fs_cgoffset * (~sblock.fs_cgmask) + used,
    292 	    sblock.fs_spc);
    293 	mincpg = roundup(mincpgcnt, mincpc);
    294 	/*
    295 	 * Ensure that cylinder group with mincpg has enough space
    296 	 * for block maps.
    297 	 */
    298 	sblock.fs_cpg = mincpg;
    299 	sblock.fs_ipg = inospercg;
    300 	if (maxcontig > 1)
    301 		sblock.fs_contigsumsize = MIN(maxcontig, FS_MAXCONTIG);
    302 	mapcramped = 0;
    303 	while (CGSIZE(&sblock) > sblock.fs_bsize) {
    304 		mapcramped = 1;
    305 		if (sblock.fs_bsize < MAXBSIZE) {
    306 			sblock.fs_bsize <<= 1;
    307 			if ((i & 1) == 0) {
    308 				i >>= 1;
    309 			} else {
    310 				sblock.fs_cpc <<= 1;
    311 				mincpc <<= 1;
    312 				mincpg = roundup(mincpgcnt, mincpc);
    313 				sblock.fs_cpg = mincpg;
    314 			}
    315 			sblock.fs_frag <<= 1;
    316 			sblock.fs_fragshift += 1;
    317 			if (sblock.fs_frag <= MAXFRAG)
    318 				continue;
    319 		}
    320 		if (sblock.fs_fsize == sblock.fs_bsize) {
    321 			printf("There is no block size that");
    322 			printf(" can support this disk\n");
    323 			exit(22);
    324 		}
    325 		sblock.fs_frag >>= 1;
    326 		sblock.fs_fragshift -= 1;
    327 		sblock.fs_fsize <<= 1;
    328 		sblock.fs_nspf <<= 1;
    329 	}
    330 	/*
    331 	 * Ensure that cylinder group with mincpg has enough space for inodes.
    332 	 */
    333 	inodecramped = 0;
    334 	inospercg = calcipg(mincpg, bpcg, &usedb);
    335 	sblock.fs_ipg = inospercg;
    336 	while (inospercg > MAXIPG(&sblock)) {
    337 		inodecramped = 1;
    338 		if (mincpc == 1 || sblock.fs_frag == 1 ||
    339 		    sblock.fs_bsize == MINBSIZE)
    340 			break;
    341 		printf("With a block size of %d %s %d\n", sblock.fs_bsize,
    342 		       "minimum bytes per inode is",
    343 		       (int)((mincpg * (off_t)bpcg - usedb)
    344 			     / MAXIPG(&sblock) + 1));
    345 		sblock.fs_bsize >>= 1;
    346 		sblock.fs_frag >>= 1;
    347 		sblock.fs_fragshift -= 1;
    348 		mincpc >>= 1;
    349 		sblock.fs_cpg = roundup(mincpgcnt, mincpc);
    350 		if (CGSIZE(&sblock) > sblock.fs_bsize) {
    351 			sblock.fs_bsize <<= 1;
    352 			break;
    353 		}
    354 		mincpg = sblock.fs_cpg;
    355 		inospercg = calcipg(mincpg, bpcg, &usedb);
    356 		sblock.fs_ipg = inospercg;
    357 	}
    358 	if (inodecramped) {
    359 		if (inospercg > MAXIPG(&sblock)) {
    360 			printf("Minimum bytes per inode is %d\n",
    361 			       (int)((mincpg * (off_t)bpcg - usedb)
    362 				     / MAXIPG(&sblock) + 1));
    363 		} else if (!mapcramped) {
    364 			printf("With %d bytes per inode, ", density);
    365 			printf("minimum cylinders per group is %d\n", mincpg);
    366 		}
    367 	}
    368 	if (mapcramped) {
    369 		printf("With %d sectors per cylinder, ", sblock.fs_spc);
    370 		printf("minimum cylinders per group is %d\n", mincpg);
    371 	}
    372 	if (inodecramped || mapcramped) {
    373 		if (sblock.fs_bsize != bsize)
    374 			printf("%s to be changed from %d to %d\n",
    375 			    "This requires the block size",
    376 			    bsize, sblock.fs_bsize);
    377 		if (sblock.fs_fsize != fsize)
    378 			printf("\t%s to be changed from %d to %d\n",
    379 			    "and the fragment size",
    380 			    fsize, sblock.fs_fsize);
    381 		exit(23);
    382 	}
    383 	/*
    384 	 * Calculate the number of cylinders per group
    385 	 */
    386 	sblock.fs_cpg = cpg;
    387 	if (sblock.fs_cpg % mincpc != 0) {
    388 		printf("%s groups must have a multiple of %d cylinders\n",
    389 			cpgflg ? "Cylinder" : "Warning: cylinder", mincpc);
    390 		sblock.fs_cpg = roundup(sblock.fs_cpg, mincpc);
    391 		if (!cpgflg)
    392 			cpg = sblock.fs_cpg;
    393 	}
    394 	/*
    395 	 * Must ensure there is enough space for inodes.
    396 	 */
    397 	sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb);
    398 	while (sblock.fs_ipg > MAXIPG(&sblock)) {
    399 		inodecramped = 1;
    400 		sblock.fs_cpg -= mincpc;
    401 		sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb);
    402 	}
    403 	/*
    404 	 * Must ensure there is enough space to hold block map.
    405 	 */
    406 	while (CGSIZE(&sblock) > sblock.fs_bsize) {
    407 		mapcramped = 1;
    408 		sblock.fs_cpg -= mincpc;
    409 		sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb);
    410 	}
    411 	sblock.fs_fpg = (sblock.fs_cpg * sblock.fs_spc) / NSPF(&sblock);
    412 	if ((sblock.fs_cpg * sblock.fs_spc) % NSPB(&sblock) != 0) {
    413 		printf("panic (fs_cpg * fs_spc) %% NSPF != 0");
    414 		exit(24);
    415 	}
    416 	if (sblock.fs_cpg < mincpg) {
    417 		printf("cylinder groups must have at least %d cylinders\n",
    418 			mincpg);
    419 		exit(25);
    420 	} else if (sblock.fs_cpg != cpg) {
    421 		if (!cpgflg)
    422 			printf("Warning: ");
    423 		else if (!mapcramped && !inodecramped)
    424 			exit(26);
    425 		if (mapcramped && inodecramped)
    426 			printf("Block size and bytes per inode restrict");
    427 		else if (mapcramped)
    428 			printf("Block size restricts");
    429 		else
    430 			printf("Bytes per inode restrict");
    431 		printf(" cylinders per group to %d.\n", sblock.fs_cpg);
    432 		if (cpgflg)
    433 			exit(27);
    434 	}
    435 	sblock.fs_cgsize = fragroundup(&sblock, CGSIZE(&sblock));
    436 	/*
    437 	 * Now have size for file system and nsect and ntrak.
    438 	 * Determine number of cylinders and blocks in the file system.
    439 	 */
    440 	sblock.fs_size = fssize = dbtofsb(&sblock, fssize);
    441 	sblock.fs_ncyl = fssize * NSPF(&sblock) / sblock.fs_spc;
    442 	if (fssize * NSPF(&sblock) > sblock.fs_ncyl * sblock.fs_spc) {
    443 		sblock.fs_ncyl++;
    444 		warned = 1;
    445 	}
    446 	if (sblock.fs_ncyl < 1) {
    447 		printf("file systems must have at least one cylinder\n");
    448 		exit(28);
    449 	}
    450 	/*
    451 	 * Determine feasability/values of rotational layout tables.
    452 	 *
    453 	 * The size of the rotational layout tables is limited by the
    454 	 * size of the superblock, SBSIZE. The amount of space available
    455 	 * for tables is calculated as (SBSIZE - sizeof (struct fs)).
    456 	 * The size of these tables is inversely proportional to the block
    457 	 * size of the file system. The size increases if sectors per track
    458 	 * are not powers of two, because more cylinders must be described
    459 	 * by the tables before the rotational pattern repeats (fs_cpc).
    460 	 */
    461 	sblock.fs_interleave = interleave;
    462 	sblock.fs_trackskew = trackskew;
    463 	sblock.fs_npsect = nphyssectors;
    464 	sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT;
    465 	sblock.fs_sbsize = fragroundup(&sblock, sizeof(struct fs));
    466 	if (sblock.fs_ntrak == 1) {
    467 		sblock.fs_cpc = 0;
    468 		goto next;
    469 	}
    470 	postblsize = sblock.fs_nrpos * sblock.fs_cpc * sizeof(int16_t);
    471 	rotblsize = sblock.fs_cpc * sblock.fs_spc / NSPB(&sblock);
    472 	totalsbsize = sizeof(struct fs) + rotblsize;
    473 	if (sblock.fs_nrpos == 8 && sblock.fs_cpc <= 16) {
    474 		/* use old static table space */
    475 		sblock.fs_postbloff = (char *)(&sblock.fs_opostbl[0][0]) -
    476 		    (char *)(&sblock.fs_firstfield);
    477 		sblock.fs_rotbloff = &sblock.fs_space[0] -
    478 		    (u_char *)(&sblock.fs_firstfield);
    479 	} else {
    480 		/* use dynamic table space */
    481 		sblock.fs_postbloff = &sblock.fs_space[0] -
    482 		    (u_char *)(&sblock.fs_firstfield);
    483 		sblock.fs_rotbloff = sblock.fs_postbloff + postblsize;
    484 		totalsbsize += postblsize;
    485 	}
    486 	if (totalsbsize > SBSIZE ||
    487 	    sblock.fs_nsect > (1 << NBBY) * NSPB(&sblock)) {
    488 		printf("%s %s %d %s %d.%s",
    489 		    "Warning: insufficient space in super block for\n",
    490 		    "rotational layout tables with nsect", sblock.fs_nsect,
    491 		    "and ntrak", sblock.fs_ntrak,
    492 		    "\nFile system performance may be impaired.\n");
    493 		sblock.fs_cpc = 0;
    494 		goto next;
    495 	}
    496 	sblock.fs_sbsize = fragroundup(&sblock, totalsbsize);
    497 	/*
    498 	 * calculate the available blocks for each rotational position
    499 	 */
    500 	for (cylno = 0; cylno < sblock.fs_cpc; cylno++)
    501 		for (rpos = 0; rpos < sblock.fs_nrpos; rpos++)
    502 			fs_postbl(&sblock, cylno)[rpos] = -1;
    503 	for (i = (rotblsize - 1) * sblock.fs_frag;
    504 	     i >= 0; i -= sblock.fs_frag) {
    505 		cylno = cbtocylno(&sblock, i);
    506 		rpos = cbtorpos(&sblock, i);
    507 		blk = fragstoblks(&sblock, i);
    508 		if (fs_postbl(&sblock, cylno)[rpos] == -1)
    509 			fs_rotbl(&sblock)[blk] = 0;
    510 		else
    511 			fs_rotbl(&sblock)[blk] = fs_postbl(&sblock, cylno)[rpos] - blk;
    512 		fs_postbl(&sblock, cylno)[rpos] = blk;
    513 	}
    514 next:
    515 	/*
    516 	 * Compute/validate number of cylinder groups.
    517 	 */
    518 	sblock.fs_ncg = sblock.fs_ncyl / sblock.fs_cpg;
    519 	if (sblock.fs_ncyl % sblock.fs_cpg)
    520 		sblock.fs_ncg++;
    521 	sblock.fs_dblkno = sblock.fs_iblkno + sblock.fs_ipg / INOPF(&sblock);
    522 	i = MIN(~sblock.fs_cgmask, sblock.fs_ncg - 1);
    523 	if (cgdmin(&sblock, i) - cgbase(&sblock, i) >= sblock.fs_fpg) {
    524 		printf("inode blocks/cyl group (%d) >= data blocks (%d)\n",
    525 		    cgdmin(&sblock, i) - cgbase(&sblock, i) / sblock.fs_frag,
    526 		    sblock.fs_fpg / sblock.fs_frag);
    527 		printf("number of cylinders per cylinder group (%d) %s.\n",
    528 		    sblock.fs_cpg, "must be increased");
    529 		exit(29);
    530 	}
    531 	j = sblock.fs_ncg - 1;
    532 	if ((i = fssize - j * sblock.fs_fpg) < sblock.fs_fpg &&
    533 	    cgdmin(&sblock, j) - cgbase(&sblock, j) > i) {
    534 		if (j == 0) {
    535 			printf("File system must have at least %d sectors\n",
    536 			    NSPF(&sblock) *
    537 			    (cgdmin(&sblock, 0) + 3 * sblock.fs_frag));
    538 			exit(30);
    539 		}
    540 		printf("Warning: inode blocks/cyl group (%d) >= "
    541 			"data blocks (%d) in last\n",
    542 		    (cgdmin(&sblock, j) - cgbase(&sblock, j)) / sblock.fs_frag,
    543 		    i / sblock.fs_frag);
    544 		printf("    cylinder group. This implies %d sector(s) "
    545 			"cannot be allocated.\n",
    546 		    i * NSPF(&sblock));
    547 		sblock.fs_ncg--;
    548 		sblock.fs_ncyl -= sblock.fs_ncyl % sblock.fs_cpg;
    549 		sblock.fs_size = fssize = sblock.fs_ncyl * sblock.fs_spc /
    550 		    NSPF(&sblock);
    551 		warned = 0;
    552 	}
    553 	if (warned) {
    554 		printf("Warning: %d sector(s) in last cylinder unallocated\n",
    555 		    sblock.fs_spc -
    556 		    (fssize * NSPF(&sblock) - (sblock.fs_ncyl - 1)
    557 		    * sblock.fs_spc));
    558 	}
    559 	/*
    560 	 * fill in remaining fields of the super block
    561 	 */
    562 	sblock.fs_csaddr = cgdmin(&sblock, 0);
    563 	sblock.fs_cssize =
    564 	    fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum));
    565 	/*
    566 	 * The superblock fields 'fs_csmask' and 'fs_csshift' are no
    567 	 * longer used. However, we still initialise them so that the
    568 	 * filesystem remains compatible with old kernels.
    569 	 */
    570 	i = sblock.fs_bsize / sizeof(struct csum);
    571 	sblock.fs_csmask = ~(i - 1);
    572 	for (sblock.fs_csshift = 0; i > 1; i >>= 1)
    573 		sblock.fs_csshift++;
    574 
    575 	/*
    576 	 * Setup memory for temporary in-core cylgroup summaries.
    577 	 * Cribbed from ffs_mountfs().
    578 	 */
    579 	size = sblock.fs_cssize;
    580 	blks = howmany(size, sblock.fs_fsize);
    581 	if (sblock.fs_contigsumsize > 0)
    582 		size += sblock.fs_ncg * sizeof(int32_t);
    583 	if ((space = (char *)calloc(1, size)) == NULL)
    584 		err(1, "memory allocation error for cg summaries");
    585 	sblock.fs_csp = space;
    586 	space = (char *)space + sblock.fs_cssize;
    587 	if (sblock.fs_contigsumsize > 0) {
    588 		int32_t *lp;
    589 
    590 		sblock.fs_maxcluster = lp = space;
    591 		for (i = 0; i < sblock.fs_ncg; i++)
    592 			*lp++ = sblock.fs_contigsumsize;
    593 	}
    594 
    595 	sblock.fs_magic = FS_MAGIC;
    596 	sblock.fs_rotdelay = rotdelay;
    597 	sblock.fs_minfree = minfree;
    598 	sblock.fs_maxcontig = maxcontig;
    599 	sblock.fs_maxbpg = maxbpg;
    600 	sblock.fs_rps = rpm / 60;
    601 	sblock.fs_optim = opt;
    602 	sblock.fs_cgrotor = 0;
    603 	sblock.fs_cstotal.cs_ndir = 0;
    604 	sblock.fs_cstotal.cs_nbfree = 0;
    605 	sblock.fs_cstotal.cs_nifree = 0;
    606 	sblock.fs_cstotal.cs_nffree = 0;
    607 	sblock.fs_fmod = 0;
    608 	sblock.fs_clean = FS_ISCLEAN;
    609 	sblock.fs_ronly = 0;
    610 
    611 	/*
    612 	 * Dump out summary information about file system.
    613 	 */
    614 	printf("%s:\t%d sectors in %d %s of %d tracks, %d sectors\n",
    615 		    fsys, sblock.fs_size * NSPF(&sblock), sblock.fs_ncyl,
    616 		    "cylinders", sblock.fs_ntrak, sblock.fs_nsect);
    617 #define B2MBFACTOR (1 / (1024.0 * 1024.0))
    618 	printf("\t%.1fMB in %d cyl groups (%d c/g, %.2fMB/g, %d i/g)\n",
    619 		    (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR,
    620 		    sblock.fs_ncg, sblock.fs_cpg,
    621 		    (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR,
    622 		    sblock.fs_ipg);
    623 #undef B2MBFACTOR
    624 	/*
    625 	 * Now determine how wide each column will be, and calculate how
    626 	 * many columns will fit in a 76 char line. 76 is the width of the
    627 	 * subwindows in sysinst.
    628 	 */
    629 	printcolwidth = count_digits(
    630 			fsbtodb(&sblock, cgsblock(&sblock, sblock.fs_ncg -1)));
    631 	nprintcols = 76 / (printcolwidth + 2);
    632 	/*
    633 	 * Now build the cylinders group blocks and
    634 	 * then print out indices of cylinder groups.
    635 	 */
    636 		printf("super-block backups (for fsck -b #) at:");
    637 	for (cylno = 0; cylno < sblock.fs_ncg; cylno++) {
    638 		initcg(cylno, start_time.tv_sec, fsopts);
    639 		if (cylno % nprintcols == 0)
    640 			printf("\n");
    641 		printf(" %*d,", printcolwidth,
    642 				fsbtodb(&sblock, cgsblock(&sblock, cylno)));
    643 		fflush(stdout);
    644 	}
    645 	printf("\n");
    646 
    647 	/*
    648 	 * Now construct the initial file system,
    649 	 * then write out the super-block.
    650 	 */
    651 	sblock.fs_time = start_time.tv_sec;
    652 	if (fsopts->needswap)
    653 		sblock.fs_flags |= FS_SWAPPED;
    654 	ffs_write_superblock(&sblock, fsopts);
    655 	return (&sblock);
    656 }
    657 
    658 /*
    659  * Write out the superblock and its duplicates,
    660  * and the cylinder group summaries
    661  */
    662 void
    663 ffs_write_superblock(struct fs *fs, const fsinfo_t *fsopts)
    664 {
    665 	int	cylno, size, blks, i, saveflag;
    666 	void	*space;
    667 	char	*wrbuf;
    668 
    669 	saveflag = fs->fs_flags & FS_INTERNAL;
    670 	fs->fs_flags &= ~FS_INTERNAL;
    671 
    672 			/* Write out the master super block */
    673 	memcpy(writebuf, fs, sbsize);
    674 	if (fsopts->needswap)
    675 		ffs_sb_swap(fs, (struct fs*)writebuf);
    676 	ffs_wtfs((int)SBOFF / sectorsize, sbsize, writebuf, fsopts);
    677 
    678 			/* Write out the duplicate super blocks */
    679 	for (cylno = 0; cylno < sblock.fs_ncg; cylno++)
    680 		ffs_wtfs(fsbtodb(fs, cgsblock(fs, cylno)),
    681 		    sbsize, writebuf, fsopts);
    682 
    683 			/* Write out the cylinder group summaries */
    684 	size = fs->fs_cssize;
    685 	blks = howmany(size, fs->fs_fsize);
    686 	space = (void *)fs->fs_csp;
    687 	if ((wrbuf = malloc(size)) == NULL)
    688 		err(1, "ffs_write_superblock: malloc %d", size);
    689 	for (i = 0; i < blks; i+= fs->fs_frag) {
    690 		size = fs->fs_bsize;
    691 		if (i + fs->fs_frag > blks)
    692 			size = (blks - i) * fs->fs_fsize;
    693 		if (fsopts->needswap)
    694 			ffs_csum_swap((struct csum *)space,
    695 			    (struct csum *)wrbuf, size);
    696 		else
    697 			memcpy(wrbuf, space, (u_int)size);
    698 		ffs_wtfs(fsbtodb(fs, fs->fs_csaddr + i), size, wrbuf, fsopts);
    699 		space = (char *)space + size;
    700 	}
    701 	free(wrbuf);
    702 	fs->fs_flags |= saveflag;
    703 }
    704 
    705 
    706 /*
    707  * Initialize a cylinder group.
    708  */
    709 static void
    710 initcg(int cylno, time_t utime, const fsinfo_t *fsopts)
    711 {
    712 	daddr_t cbase, d, dlower, dupper, dmax, blkno;
    713 	int32_t i;
    714 
    715 	/*
    716 	 * Determine block bounds for cylinder group.
    717 	 * Allow space for super block summary information in first
    718 	 * cylinder group.
    719 	 */
    720 	cbase = cgbase(&sblock, cylno);
    721 	dmax = cbase + sblock.fs_fpg;
    722 	if (dmax > sblock.fs_size)
    723 		dmax = sblock.fs_size;
    724 	dlower = cgsblock(&sblock, cylno) - cbase;
    725 	dupper = cgdmin(&sblock, cylno) - cbase;
    726 	if (cylno == 0)
    727 		dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
    728 	memset(&acg, 0, sblock.fs_cgsize);
    729 	acg.cg_time = utime;
    730 	acg.cg_magic = CG_MAGIC;
    731 	acg.cg_cgx = cylno;
    732 	if (cylno == sblock.fs_ncg - 1)
    733 		acg.cg_ncyl = sblock.fs_ncyl % sblock.fs_cpg;
    734 	else
    735 		acg.cg_ncyl = sblock.fs_cpg;
    736 	acg.cg_niblk = sblock.fs_ipg;
    737 	acg.cg_ndblk = dmax - cbase;
    738 	if (sblock.fs_contigsumsize > 0)
    739 		acg.cg_nclusterblks = acg.cg_ndblk / sblock.fs_frag;
    740 	acg.cg_btotoff = &acg.cg_space[0] - (u_char *)(&acg.cg_firstfield);
    741 	acg.cg_boff = acg.cg_btotoff + sblock.fs_cpg * sizeof(int32_t);
    742 	acg.cg_iusedoff = acg.cg_boff +
    743 		sblock.fs_cpg * sblock.fs_nrpos * sizeof(int16_t);
    744 	acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, NBBY);
    745 	if (sblock.fs_contigsumsize <= 0) {
    746 		acg.cg_nextfreeoff = acg.cg_freeoff +
    747 		   howmany(sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY);
    748 	} else {
    749 		acg.cg_clustersumoff = acg.cg_freeoff + howmany
    750 		    (sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock), NBBY) -
    751 		    sizeof(int32_t);
    752 		acg.cg_clustersumoff =
    753 		    roundup(acg.cg_clustersumoff, sizeof(int32_t));
    754 		acg.cg_clusteroff = acg.cg_clustersumoff +
    755 		    (sblock.fs_contigsumsize + 1) * sizeof(int32_t);
    756 		acg.cg_nextfreeoff = acg.cg_clusteroff + howmany
    757 		    (sblock.fs_cpg * sblock.fs_spc / NSPB(&sblock), NBBY);
    758 	}
    759 	if (acg.cg_nextfreeoff > sblock.fs_cgsize) {
    760 		printf("Panic: cylinder group too big\n");
    761 		exit(37);
    762 	}
    763 	acg.cg_cs.cs_nifree += sblock.fs_ipg;
    764 	if (cylno == 0)
    765 		for (i = 0; i < ROOTINO; i++) {
    766 			setbit(cg_inosused(&acg, 0), i);
    767 			acg.cg_cs.cs_nifree--;
    768 		}
    769 	for (i = 0; i < sblock.fs_ipg / INOPF(&sblock); i += sblock.fs_frag)
    770 		ffs_wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i),
    771 		    sblock.fs_bsize, (char *)zino, fsopts);
    772 	if (cylno > 0) {
    773 		/*
    774 		 * In cylno 0, beginning space is reserved
    775 		 * for boot and super blocks.
    776 		 */
    777 		for (d = 0; d < dlower; d += sblock.fs_frag) {
    778 			blkno = d / sblock.fs_frag;
    779 			ffs_setblock(&sblock, cg_blksfree(&acg, 0), blkno);
    780 			if (sblock.fs_contigsumsize > 0)
    781 				setbit(cg_clustersfree(&acg, 0), blkno);
    782 			acg.cg_cs.cs_nbfree++;
    783 			cg_blktot(&acg, 0)[cbtocylno(&sblock, d)]++;
    784 			cg_blks(&sblock, &acg, cbtocylno(&sblock, d), 0)
    785 			    [cbtorpos(&sblock, d)]++;
    786 		}
    787 		sblock.fs_dsize += dlower;
    788 	}
    789 	sblock.fs_dsize += acg.cg_ndblk - dupper;
    790 	if ((i = (dupper % sblock.fs_frag)) != 0) {
    791 		acg.cg_frsum[sblock.fs_frag - i]++;
    792 		for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) {
    793 			setbit(cg_blksfree(&acg, 0), dupper);
    794 			acg.cg_cs.cs_nffree++;
    795 		}
    796 	}
    797 	for (d = dupper; d + sblock.fs_frag <= dmax - cbase; ) {
    798 		blkno = d / sblock.fs_frag;
    799 		ffs_setblock(&sblock, cg_blksfree(&acg, 0), blkno);
    800 		if (sblock.fs_contigsumsize > 0)
    801 			setbit(cg_clustersfree(&acg, 0), blkno);
    802 		acg.cg_cs.cs_nbfree++;
    803 		cg_blktot(&acg, 0)[cbtocylno(&sblock, d)]++;
    804 		cg_blks(&sblock, &acg, cbtocylno(&sblock, d), 0)
    805 		    [cbtorpos(&sblock, d)]++;
    806 		d += sblock.fs_frag;
    807 	}
    808 	if (d < dmax - cbase) {
    809 		acg.cg_frsum[dmax - cbase - d]++;
    810 		for (; d < dmax - cbase; d++) {
    811 			setbit(cg_blksfree(&acg, 0), d);
    812 			acg.cg_cs.cs_nffree++;
    813 		}
    814 	}
    815 	if (sblock.fs_contigsumsize > 0) {
    816 		int32_t *sump = cg_clustersum(&acg, 0);
    817 		u_char *mapp = cg_clustersfree(&acg, 0);
    818 		int map = *mapp++;
    819 		int bit = 1;
    820 		int run = 0;
    821 
    822 		for (i = 0; i < acg.cg_nclusterblks; i++) {
    823 			if ((map & bit) != 0) {
    824 				run++;
    825 			} else if (run != 0) {
    826 				if (run > sblock.fs_contigsumsize)
    827 					run = sblock.fs_contigsumsize;
    828 				sump[run]++;
    829 				run = 0;
    830 			}
    831 			if ((i & (NBBY - 1)) != (NBBY - 1)) {
    832 				bit <<= 1;
    833 			} else {
    834 				map = *mapp++;
    835 				bit = 1;
    836 			}
    837 		}
    838 		if (run != 0) {
    839 			if (run > sblock.fs_contigsumsize)
    840 				run = sblock.fs_contigsumsize;
    841 			sump[run]++;
    842 		}
    843 	}
    844 	sblock.fs_cstotal.cs_ndir += acg.cg_cs.cs_ndir;
    845 	sblock.fs_cstotal.cs_nffree += acg.cg_cs.cs_nffree;
    846 	sblock.fs_cstotal.cs_nbfree += acg.cg_cs.cs_nbfree;
    847 	sblock.fs_cstotal.cs_nifree += acg.cg_cs.cs_nifree;
    848 	sblock.fs_cs(&sblock, cylno) = acg.cg_cs;
    849 	memcpy(writebuf, &acg, sblock.fs_bsize);
    850 	if (fsopts->needswap)
    851 		swap_cg(&acg, (struct cg*)writebuf);
    852 	ffs_wtfs(fsbtodb(&sblock, cgtod(&sblock, cylno)),
    853 		sblock.fs_bsize,
    854 		writebuf, fsopts);
    855 }
    856 
    857 /*
    858  * Calculate number of inodes per group.
    859  */
    860 static int32_t
    861 calcipg(int32_t cylpg, int32_t bpcg, off_t *usedbp)
    862 {
    863 	int i;
    864 	int32_t ipg, new_ipg, ncg, ncyl;
    865 	off_t usedb;
    866 
    867 	/*
    868 	 * Prepare to scale by fssize / (number of sectors in cylinder groups).
    869 	 * Note that fssize is still in sectors, not file system blocks.
    870 	 */
    871 	ncyl = howmany(fssize, secpercyl);
    872 	ncg = howmany(ncyl, cylpg);
    873 	/*
    874 	 * Iterate a few times to allow for ipg depending on itself.
    875 	 */
    876 	ipg = 0;
    877 	for (i = 0; i < 10; i++) {
    878 		usedb = (sblock.fs_iblkno + ipg / INOPF(&sblock))
    879 			* NSPF(&sblock) * (off_t)sectorsize;
    880 		if (cylpg * (long long)bpcg < usedb) {
    881 			warnx("Too many inodes per cyl group!");
    882 			return (MAXIPG(&sblock)+1);
    883 		}
    884 		new_ipg = (cylpg * (long long)bpcg - usedb) /
    885 		    ((long long)density * fssize / (ncg * secpercyl * cylpg));
    886 		if (new_ipg <= 0)
    887 			new_ipg = 1;		/* ensure ipg > 0 */
    888 		new_ipg = roundup(new_ipg, INOPB(&sblock));
    889 		if (new_ipg == ipg)
    890 			break;
    891 		ipg = new_ipg;
    892 	}
    893 	*usedbp = usedb;
    894 	return (ipg);
    895 }
    896 
    897 
    898 /*
    899  * read a block from the file system
    900  */
    901 void
    902 ffs_rdfs(daddr_t bno, int size, void *bf, const fsinfo_t *fsopts)
    903 {
    904 	int n;
    905 	off_t offset;
    906 
    907 	offset = bno;
    908 	offset *= fsopts->sectorsize;
    909 	if (lseek(fsopts->fd, offset, SEEK_SET) < 0)
    910 		err(1, "ffs_rdfs: seek error: %d\n", bno);
    911 	n = read(fsopts->fd, bf, size);
    912 	if (n == -1)
    913 		err(1, "ffs_rdfs: read error bno %d size %d\n", bno, size);
    914 	else if (n != size)
    915 		errx(1,
    916 		    "ffs_rdfs: read error bno %d size %d: short read of %d\n",
    917 		    bno, size, n);
    918 }
    919 
    920 /*
    921  * write a block to the file system
    922  */
    923 void
    924 ffs_wtfs(daddr_t bno, int size, void *bf, const fsinfo_t *fsopts)
    925 {
    926 	int n;
    927 	off_t offset;
    928 
    929 	offset = bno;
    930 	offset *= fsopts->sectorsize;
    931 	if (lseek(fsopts->fd, offset, SEEK_SET) < 0)
    932 		err(1, "ffs_wtfs: seek error: %d\n", bno);
    933 	n = write(fsopts->fd, bf, size);
    934 	if (n == -1)
    935 		err(1, "ffs_wtfs: write error bno %d size %d\n", bno, size);
    936 	else if (n != size)
    937 		errx(1,
    938 		    "ffs_wtfs: write error bno %d size %d: short write of %d\n",
    939 		    bno, size, n);
    940 }
    941 
    942 /* swap byte order of cylinder group */
    943 static void
    944 swap_cg(struct cg *o, struct cg *n)
    945 {
    946 	int i, btotsize, fbsize;
    947 	u_int32_t *n32, *o32;
    948 	u_int16_t *n16, *o16;
    949 
    950 	n->cg_firstfield = bswap32(o->cg_firstfield);
    951 	n->cg_magic = bswap32(o->cg_magic);
    952 	n->cg_time = bswap32(o->cg_time);
    953 	n->cg_cgx = bswap32(o->cg_cgx);
    954 	n->cg_ncyl = bswap16(o->cg_ncyl);
    955 	n->cg_niblk = bswap16(o->cg_niblk);
    956 	n->cg_ndblk = bswap32(o->cg_ndblk);
    957 	n->cg_cs.cs_ndir = bswap32(o->cg_cs.cs_ndir);
    958 	n->cg_cs.cs_nbfree = bswap32(o->cg_cs.cs_nbfree);
    959 	n->cg_cs.cs_nifree = bswap32(o->cg_cs.cs_nifree);
    960 	n->cg_cs.cs_nffree = bswap32(o->cg_cs.cs_nffree);
    961 	n->cg_rotor = bswap32(o->cg_rotor);
    962 	n->cg_frotor = bswap32(o->cg_frotor);
    963 	n->cg_irotor = bswap32(o->cg_irotor);
    964 	n->cg_btotoff = bswap32(o->cg_btotoff);
    965 	n->cg_boff = bswap32(o->cg_boff);
    966 	n->cg_iusedoff = bswap32(o->cg_iusedoff);
    967 	n->cg_freeoff = bswap32(o->cg_freeoff);
    968 	n->cg_nextfreeoff = bswap32(o->cg_nextfreeoff);
    969 	n->cg_clustersumoff = bswap32(o->cg_clustersumoff);
    970 	n->cg_clusteroff = bswap32(o->cg_clusteroff);
    971 	n->cg_nclusterblks = bswap32(o->cg_nclusterblks);
    972 	for (i=0; i < MAXFRAG; i++)
    973 		n->cg_frsum[i] = bswap32(o->cg_frsum[i]);
    974 
    975 	/* alays new format */
    976 	if (n->cg_magic == CG_MAGIC) {
    977 		btotsize = n->cg_boff - n->cg_btotoff;
    978 		fbsize = n->cg_iusedoff - n->cg_boff;
    979 		n32 = (u_int32_t*)((u_int8_t*)n + n->cg_btotoff);
    980 		o32 = (u_int32_t*)((u_int8_t*)o + n->cg_btotoff);
    981 		n16 = (u_int16_t*)((u_int8_t*)n + n->cg_boff);
    982 		o16 = (u_int16_t*)((u_int8_t*)o + n->cg_boff);
    983 	} else {
    984 		btotsize = bswap32(n->cg_boff) - bswap32(n->cg_btotoff);
    985 		fbsize = bswap32(n->cg_iusedoff) - bswap32(n->cg_boff);
    986 		n32 = (u_int32_t*)((u_int8_t*)n + bswap32(n->cg_btotoff));
    987 		o32 = (u_int32_t*)((u_int8_t*)o + bswap32(n->cg_btotoff));
    988 		n16 = (u_int16_t*)((u_int8_t*)n + bswap32(n->cg_boff));
    989 		o16 = (u_int16_t*)((u_int8_t*)o + bswap32(n->cg_boff));
    990 	}
    991 	for (i=0; i < btotsize / sizeof(u_int32_t); i++)
    992 		n32[i] = bswap32(o32[i]);
    993 
    994 	for (i=0; i < fbsize/sizeof(u_int16_t); i++)
    995 		n16[i] = bswap16(o16[i]);
    996 
    997 	if (n->cg_magic == CG_MAGIC) {
    998 		n32 = (u_int32_t*)((u_int8_t*)n + n->cg_clustersumoff);
    999 		o32 = (u_int32_t*)((u_int8_t*)o + n->cg_clustersumoff);
   1000 	} else {
   1001 		n32 = (u_int32_t*)((u_int8_t*)n + bswap32(n->cg_clustersumoff));
   1002 		o32 = (u_int32_t*)((u_int8_t*)o + bswap32(n->cg_clustersumoff));
   1003 	}
   1004 	for (i = 1; i < sblock.fs_contigsumsize + 1; i++)
   1005 		n32[i] = bswap32(o32[i]);
   1006 }
   1007 
   1008 /* Determine how many digits are needed to print a given integer */
   1009 static int
   1010 count_digits(int num)
   1011 {
   1012 	int ndig;
   1013 
   1014 	for(ndig = 1; num > 9; num /=10, ndig++);
   1015 
   1016 	return (ndig);
   1017 }
   1018