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