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