Home | History | Annotate | Line # | Download | only in ffs
mkfs.c revision 1.6
      1 /*	$NetBSD: mkfs.c,v 1.6 2002/01/08 06:00:14 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.6 2002/01/08 06:00:14 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/dinode.h>
     59 #include <ufs/ufs/dir.h>
     60 #include <ufs/ufs/ufs_bswap.h>
     61 #include <ufs/ffs/fs.h>
     62 
     63 #include "ffs/ufs_inode.h"
     64 #include "ffs/ffs_extern.h"
     65 #include "ffs/newfs_extern.h"
     66 
     67 static void initcg(int, time_t, const fsinfo_t *);
     68 static int32_t calcipg(int32_t, int32_t, off_t *);
     69 static void swap_cg(struct cg *, struct cg *);
     70 
     71 static int count_digits(int);
     72 
     73 /*
     74  * make file system for cylinder-group style file systems
     75  */
     76 
     77 /*
     78  * We limit the size of the inode map to be no more than a
     79  * third of the cylinder group space, since we must leave at
     80  * least an equal amount of space for the block map.
     81  *
     82  * N.B.: MAXIPG must be a multiple of INOPB(fs).
     83  */
     84 #define MAXIPG(fs)	roundup((fs)->fs_bsize * NBBY / 3, INOPB(fs))
     85 
     86 #define UMASK		0755
     87 #define POWEROF2(num)	(((num) & ((num) - 1)) == 0)
     88 
     89 union {
     90 	struct fs fs;
     91 	char pad[SBSIZE];
     92 } fsun;
     93 #define	sblock	fsun.fs
     94 
     95 union {
     96 	struct cg cg;
     97 	char pad[MAXBSIZE];
     98 } cgun;
     99 #define	acg	cgun.cg
    100 
    101 struct dinode zino[MAXBSIZE / DINODE_SIZE];
    102 
    103 char writebuf[MAXBSIZE];
    104 
    105 static	int	Oflag;		/* format as an 4.3BSD file system */
    106 static	int	fssize;		/* file system size */
    107 static	int	ntracks;	/* # tracks/cylinder */
    108 static	int	nsectors;	/* # sectors/track */
    109 static	int	nphyssectors;	/* # sectors/track including spares */
    110 static	int	secpercyl;	/* sectors per cylinder */
    111 static	int	sectorsize;	/* bytes/sector */
    112 static	int	rpm;		/* revolutions/minute of drive */
    113 static	int	interleave;	/* hardware sector interleave */
    114 static	int	trackskew;	/* sector 0 skew, per track */
    115 static	int	fsize;		/* fragment size */
    116 static	int	bsize;		/* block size */
    117 static	int	cpg;		/* cylinders/cylinder group */
    118 static	int	cpgflg;		/* cylinders/cylinder group flag was given */
    119 static	int	minfree;	/* free space threshold */
    120 static	int	opt;		/* optimization preference (space or time) */
    121 static	int	density;	/* number of bytes per inode */
    122 static	int	maxcontig;	/* max contiguous blocks to allocate */
    123 static	int	rotdelay;	/* rotational delay between blocks */
    124 static	int	maxbpg;		/* maximum blocks per file in a cyl group */
    125 static	int	nrpos;		/* # of distinguished rotational positions */
    126 static	int	bbsize;		/* boot block size */
    127 static	int	sbsize;		/* superblock size */
    128 static	int	avgfilesize;	/* expected average file size */
    129 static	int	avgfpdir;	/* expected number of files per directory */
    130 
    131 
    132 struct fs *
    133 ffs_mkfs(const char *fsys, const fsinfo_t *fsopts)
    134 {
    135 	int32_t i, mincpc, mincpg, inospercg;
    136 	int32_t cylno, rpos, blk, j, warned = 0;
    137 	int32_t used, mincpgcnt, bpcg;
    138 	off_t usedb;
    139 	int32_t mapcramped, inodecramped;
    140 	int32_t postblsize, rotblsize, totalsbsize;
    141 	long long sizepb;
    142 	void *space;
    143 	int size, blks;
    144 	int nprintcols, printcolwidth;
    145 
    146 	Oflag = 0;
    147 	fssize =	fsopts->size / fsopts->sectorsize;
    148 	ntracks =	fsopts->ntracks;
    149 	nsectors =	fsopts->nsectors;
    150 	nphyssectors =	fsopts->nsectors;	/* XXX: no trackspares */
    151 	secpercyl =	nsectors * ntracks;
    152 	sectorsize =	fsopts->sectorsize;
    153 	rpm =		fsopts->rpm;
    154 	interleave =	1;				/* XXX: HCD */
    155 	trackskew =	0;				/* XXX: HCD */
    156 	fsize =		fsopts->fsize;
    157 	bsize =		fsopts->bsize;
    158 	cpg =		fsopts->cpg;
    159 	cpgflg =	1;
    160 	minfree =	fsopts->minfree;
    161 	opt =		fsopts->optimization;
    162 	density =	fsopts->density;
    163 	maxcontig =	fsopts->maxcontig;
    164 	rotdelay =	fsopts->rotdelay;
    165 	maxbpg =	fsopts->maxbpg;
    166 	nrpos =		fsopts->nrpos;
    167 	bbsize =	BBSIZE;
    168 	sbsize =	SBSIZE;
    169 	avgfilesize = 	fsopts->avgfilesize;
    170 	avgfpdir = 	fsopts->avgfpdir;
    171 
    172 	if (Oflag) {
    173 		sblock.fs_inodefmt = FS_42INODEFMT;
    174 		sblock.fs_maxsymlinklen = 0;
    175 	} else {
    176 		sblock.fs_inodefmt = FS_44INODEFMT;
    177 		sblock.fs_maxsymlinklen = MAXSYMLINKLEN;
    178 	}
    179 	/*
    180 	 * Validate the given file system size.
    181 	 * Verify that its last block can actually be accessed.
    182 	 */
    183 	if (fssize <= 0)
    184 		printf("preposterous size %d\n", fssize), exit(13);
    185 	ffs_wtfs(fssize - 1, sectorsize, (char *)&sblock, fsopts);
    186 
    187 	/*
    188 	 * collect and verify the sector and track info
    189 	 */
    190 	sblock.fs_nsect = nsectors;
    191 	sblock.fs_ntrak = ntracks;
    192 	if (sblock.fs_ntrak <= 0)
    193 		printf("preposterous ntrak %d\n", sblock.fs_ntrak), exit(14);
    194 	if (sblock.fs_nsect <= 0)
    195 		printf("preposterous nsect %d\n", sblock.fs_nsect), exit(15);
    196 	/*
    197 	 * collect and verify the filesystem density info
    198 	 */
    199 	sblock.fs_avgfilesize = avgfilesize;
    200 	sblock.fs_avgfpdir = avgfpdir;
    201 	if (sblock.fs_avgfilesize <= 0)
    202 		printf("illegal expected average file size %d\n",
    203 		    sblock.fs_avgfilesize), exit(14);
    204 	if (sblock.fs_avgfpdir <= 0)
    205 		printf("illegal expected number of files per directory %d\n",
    206 		    sblock.fs_avgfpdir), exit(15);
    207 	/*
    208 	 * collect and verify the block and fragment sizes
    209 	 */
    210 	sblock.fs_bsize = bsize;
    211 	sblock.fs_fsize = fsize;
    212 	if (!POWEROF2(sblock.fs_bsize)) {
    213 		printf("block size must be a power of 2, not %d\n",
    214 		    sblock.fs_bsize);
    215 		exit(16);
    216 	}
    217 	if (!POWEROF2(sblock.fs_fsize)) {
    218 		printf("fragment size must be a power of 2, not %d\n",
    219 		    sblock.fs_fsize);
    220 		exit(17);
    221 	}
    222 	if (sblock.fs_fsize < sectorsize) {
    223 		printf("fragment size %d is too small, minimum is %d\n",
    224 		    sblock.fs_fsize, sectorsize);
    225 		exit(18);
    226 	}
    227 	if (sblock.fs_bsize < MINBSIZE) {
    228 		printf("block size %d is too small, minimum is %d\n",
    229 		    sblock.fs_bsize, MINBSIZE);
    230 		exit(19);
    231 	}
    232 	if (sblock.fs_bsize < sblock.fs_fsize) {
    233 		printf("block size (%d) cannot be smaller than fragment size (%d)\n",
    234 		    sblock.fs_bsize, sblock.fs_fsize);
    235 		exit(20);
    236 	}
    237 	sblock.fs_bmask = ~(sblock.fs_bsize - 1);
    238 	sblock.fs_fmask = ~(sblock.fs_fsize - 1);
    239 	sblock.fs_qbmask = ~sblock.fs_bmask;
    240 	sblock.fs_qfmask = ~sblock.fs_fmask;
    241 	for (sblock.fs_bshift = 0, i = sblock.fs_bsize; i > 1; i >>= 1)
    242 		sblock.fs_bshift++;
    243 	for (sblock.fs_fshift = 0, i = sblock.fs_fsize; i > 1; i >>= 1)
    244 		sblock.fs_fshift++;
    245 	sblock.fs_frag = numfrags(&sblock, sblock.fs_bsize);
    246 	for (sblock.fs_fragshift = 0, i = sblock.fs_frag; i > 1; i >>= 1)
    247 		sblock.fs_fragshift++;
    248 	if (sblock.fs_frag > MAXFRAG) {
    249 		printf("fragment size %d is too small, "
    250 			"minimum with block size %d is %d\n",
    251 		    sblock.fs_fsize, sblock.fs_bsize,
    252 		    sblock.fs_bsize / MAXFRAG);
    253 		exit(21);
    254 	}
    255 	sblock.fs_nrpos = nrpos;
    256 	sblock.fs_nindir = sblock.fs_bsize / sizeof(daddr_t);
    257 	sblock.fs_inopb = sblock.fs_bsize / DINODE_SIZE;
    258 	sblock.fs_nspf = sblock.fs_fsize / sectorsize;
    259 	for (sblock.fs_fsbtodb = 0, i = NSPF(&sblock); i > 1; i >>= 1)
    260 		sblock.fs_fsbtodb++;
    261 	sblock.fs_sblkno =
    262 	    roundup(howmany(bbsize + sbsize, sblock.fs_fsize), sblock.fs_frag);
    263 	sblock.fs_cblkno = (daddr_t)(sblock.fs_sblkno +
    264 	    roundup(howmany(sbsize, sblock.fs_fsize), sblock.fs_frag));
    265 	sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag;
    266 	sblock.fs_cgoffset = roundup(
    267 	    howmany(sblock.fs_nsect, NSPF(&sblock)), sblock.fs_frag);
    268 	for (sblock.fs_cgmask = 0xffffffff, i = sblock.fs_ntrak; i > 1; i >>= 1)
    269 		sblock.fs_cgmask <<= 1;
    270 	if (!POWEROF2(sblock.fs_ntrak))
    271 		sblock.fs_cgmask <<= 1;
    272 	sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1;
    273 	for (sizepb = sblock.fs_bsize, i = 0; i < NIADDR; i++) {
    274 		sizepb *= NINDIR(&sblock);
    275 		sblock.fs_maxfilesize += sizepb;
    276 	}
    277 	/*
    278 	 * Validate specified/determined secpercyl
    279 	 * and calculate minimum cylinders per group.
    280 	 */
    281 	sblock.fs_spc = secpercyl;
    282 	for (sblock.fs_cpc = NSPB(&sblock), i = sblock.fs_spc;
    283 	     sblock.fs_cpc > 1 && (i & 1) == 0;
    284 	     sblock.fs_cpc >>= 1, i >>= 1)
    285 		/* void */;
    286 	mincpc = sblock.fs_cpc;
    287 	bpcg = sblock.fs_spc * sectorsize;
    288 	inospercg = roundup(bpcg / DINODE_SIZE, INOPB(&sblock));
    289 	if (inospercg > MAXIPG(&sblock))
    290 		inospercg = MAXIPG(&sblock);
    291 	used = (sblock.fs_iblkno + inospercg / INOPF(&sblock)) * NSPF(&sblock);
    292 	mincpgcnt = howmany(sblock.fs_cgoffset * (~sblock.fs_cgmask) + used,
    293 	    sblock.fs_spc);
    294 	mincpg = roundup(mincpgcnt, mincpc);
    295 	/*
    296 	 * Ensure that cylinder group with mincpg has enough space
    297 	 * for block maps.
    298 	 */
    299 	sblock.fs_cpg = mincpg;
    300 	sblock.fs_ipg = inospercg;
    301 	if (maxcontig > 1)
    302 		sblock.fs_contigsumsize = MIN(maxcontig, FS_MAXCONTIG);
    303 	mapcramped = 0;
    304 	while (CGSIZE(&sblock) > sblock.fs_bsize) {
    305 		mapcramped = 1;
    306 		if (sblock.fs_bsize < MAXBSIZE) {
    307 			sblock.fs_bsize <<= 1;
    308 			if ((i & 1) == 0) {
    309 				i >>= 1;
    310 			} else {
    311 				sblock.fs_cpc <<= 1;
    312 				mincpc <<= 1;
    313 				mincpg = roundup(mincpgcnt, mincpc);
    314 				sblock.fs_cpg = mincpg;
    315 			}
    316 			sblock.fs_frag <<= 1;
    317 			sblock.fs_fragshift += 1;
    318 			if (sblock.fs_frag <= MAXFRAG)
    319 				continue;
    320 		}
    321 		if (sblock.fs_fsize == sblock.fs_bsize) {
    322 			printf("There is no block size that");
    323 			printf(" can support this disk\n");
    324 			exit(22);
    325 		}
    326 		sblock.fs_frag >>= 1;
    327 		sblock.fs_fragshift -= 1;
    328 		sblock.fs_fsize <<= 1;
    329 		sblock.fs_nspf <<= 1;
    330 	}
    331 	/*
    332 	 * Ensure that cylinder group with mincpg has enough space for inodes.
    333 	 */
    334 	inodecramped = 0;
    335 	inospercg = calcipg(mincpg, bpcg, &usedb);
    336 	sblock.fs_ipg = inospercg;
    337 	while (inospercg > MAXIPG(&sblock)) {
    338 		inodecramped = 1;
    339 		if (mincpc == 1 || sblock.fs_frag == 1 ||
    340 		    sblock.fs_bsize == MINBSIZE)
    341 			break;
    342 		printf("With a block size of %d %s %d\n", sblock.fs_bsize,
    343 		       "minimum bytes per inode is",
    344 		       (int)((mincpg * (off_t)bpcg - usedb)
    345 			     / MAXIPG(&sblock) + 1));
    346 		sblock.fs_bsize >>= 1;
    347 		sblock.fs_frag >>= 1;
    348 		sblock.fs_fragshift -= 1;
    349 		mincpc >>= 1;
    350 		sblock.fs_cpg = roundup(mincpgcnt, mincpc);
    351 		if (CGSIZE(&sblock) > sblock.fs_bsize) {
    352 			sblock.fs_bsize <<= 1;
    353 			break;
    354 		}
    355 		mincpg = sblock.fs_cpg;
    356 		inospercg = calcipg(mincpg, bpcg, &usedb);
    357 		sblock.fs_ipg = inospercg;
    358 	}
    359 	if (inodecramped) {
    360 		if (inospercg > MAXIPG(&sblock)) {
    361 			printf("Minimum bytes per inode is %d\n",
    362 			       (int)((mincpg * (off_t)bpcg - usedb)
    363 				     / MAXIPG(&sblock) + 1));
    364 		} else if (!mapcramped) {
    365 			printf("With %d bytes per inode, ", density);
    366 			printf("minimum cylinders per group is %d\n", mincpg);
    367 		}
    368 	}
    369 	if (mapcramped) {
    370 		printf("With %d sectors per cylinder, ", sblock.fs_spc);
    371 		printf("minimum cylinders per group is %d\n", mincpg);
    372 	}
    373 	if (inodecramped || mapcramped) {
    374 		if (sblock.fs_bsize != bsize)
    375 			printf("%s to be changed from %d to %d\n",
    376 			    "This requires the block size",
    377 			    bsize, sblock.fs_bsize);
    378 		if (sblock.fs_fsize != fsize)
    379 			printf("\t%s to be changed from %d to %d\n",
    380 			    "and the fragment size",
    381 			    fsize, sblock.fs_fsize);
    382 		exit(23);
    383 	}
    384 	/*
    385 	 * Calculate the number of cylinders per group
    386 	 */
    387 	sblock.fs_cpg = cpg;
    388 	if (sblock.fs_cpg % mincpc != 0) {
    389 		printf("%s groups must have a multiple of %d cylinders\n",
    390 			cpgflg ? "Cylinder" : "Warning: cylinder", mincpc);
    391 		sblock.fs_cpg = roundup(sblock.fs_cpg, mincpc);
    392 		if (!cpgflg)
    393 			cpg = sblock.fs_cpg;
    394 	}
    395 	/*
    396 	 * Must ensure there is enough space for inodes.
    397 	 */
    398 	sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb);
    399 	while (sblock.fs_ipg > MAXIPG(&sblock)) {
    400 		inodecramped = 1;
    401 		sblock.fs_cpg -= mincpc;
    402 		sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb);
    403 	}
    404 	/*
    405 	 * Must ensure there is enough space to hold block map.
    406 	 */
    407 	while (CGSIZE(&sblock) > sblock.fs_bsize) {
    408 		mapcramped = 1;
    409 		sblock.fs_cpg -= mincpc;
    410 		sblock.fs_ipg = calcipg(sblock.fs_cpg, bpcg, &usedb);
    411 	}
    412 	sblock.fs_fpg = (sblock.fs_cpg * sblock.fs_spc) / NSPF(&sblock);
    413 	if ((sblock.fs_cpg * sblock.fs_spc) % NSPB(&sblock) != 0) {
    414 		printf("panic (fs_cpg * fs_spc) %% NSPF != 0");
    415 		exit(24);
    416 	}
    417 	if (sblock.fs_cpg < mincpg) {
    418 		printf("cylinder groups must have at least %d cylinders\n",
    419 			mincpg);
    420 		exit(25);
    421 	} else if (sblock.fs_cpg != cpg) {
    422 		if (!cpgflg)
    423 			printf("Warning: ");
    424 		else if (!mapcramped && !inodecramped)
    425 			exit(26);
    426 		if (mapcramped && inodecramped)
    427 			printf("Block size and bytes per inode restrict");
    428 		else if (mapcramped)
    429 			printf("Block size restricts");
    430 		else
    431 			printf("Bytes per inode restrict");
    432 		printf(" cylinders per group to %d.\n", sblock.fs_cpg);
    433 		if (cpgflg)
    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