Home | History | Annotate | Line # | Download | only in newfs
mkfs.c revision 1.86
      1 /*	$NetBSD: mkfs.c,v 1.86 2004/03/07 12:26:38 dsl Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1980, 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 2002 Networks Associates Technology, Inc.
     34  * All rights reserved.
     35  *
     36  * This software was developed for the FreeBSD Project by Marshall
     37  * Kirk McKusick and Network Associates Laboratories, the Security
     38  * Research Division of Network Associates, Inc. under DARPA/SPAWAR
     39  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
     40  * research program
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. All advertising materials mentioning features or use of this software
     51  *    must display the following acknowledgement:
     52  *	This product includes software developed by the University of
     53  *	California, Berkeley and its contributors.
     54  * 4. Neither the name of the University nor the names of its contributors
     55  *    may be used to endorse or promote products derived from this software
     56  *    without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  * SUCH DAMAGE.
     69  */
     70 
     71 #include <sys/cdefs.h>
     72 #ifndef lint
     73 #if 0
     74 static char sccsid[] = "@(#)mkfs.c	8.11 (Berkeley) 5/3/95";
     75 #else
     76 __RCSID("$NetBSD: mkfs.c,v 1.86 2004/03/07 12:26:38 dsl Exp $");
     77 #endif
     78 #endif /* not lint */
     79 
     80 #include <sys/param.h>
     81 #include <sys/mman.h>
     82 #include <sys/time.h>
     83 #include <sys/resource.h>
     84 #include <ufs/ufs/dinode.h>
     85 #include <ufs/ufs/dir.h>
     86 #include <ufs/ufs/ufs_bswap.h>
     87 #include <ufs/ffs/fs.h>
     88 #include <ufs/ffs/ffs_extern.h>
     89 #include <sys/disklabel.h>
     90 
     91 #include <err.h>
     92 #include <errno.h>
     93 #include <string.h>
     94 #include <unistd.h>
     95 #include <stdlib.h>
     96 #include <stddef.h>
     97 
     98 #ifndef STANDALONE
     99 #include <stdio.h>
    100 #endif
    101 
    102 #include "extern.h"
    103 
    104 union dinode {
    105 	struct ufs1_dinode dp1;
    106 	struct ufs2_dinode dp2;
    107 };
    108 
    109 static void initcg(int, const struct timeval *);
    110 static int fsinit(const struct timeval *, mode_t, uid_t, gid_t);
    111 static int makedir(struct direct *, int);
    112 static daddr_t alloc(int, int);
    113 static void iput(union dinode *, ino_t);
    114 static void rdfs(daddr_t, int, void *);
    115 static void wtfs(daddr_t, int, void *);
    116 static int isblock(struct fs *, unsigned char *, int);
    117 static void clrblock(struct fs *, unsigned char *, int);
    118 static void setblock(struct fs *, unsigned char *, int);
    119 static int ilog2(int);
    120 static void zap_old_sblock(int);
    121 #ifdef MFS
    122 static void calc_memfree(void);
    123 static void *mkfs_malloc(size_t size);
    124 #endif
    125 
    126 static int count_digits(uint64_t);
    127 
    128 /*
    129  * make file system for cylinder-group style file systems
    130  */
    131 #define	UMASK		0755
    132 #define	POWEROF2(num)	(((num) & ((num) - 1)) == 0)
    133 
    134 union {
    135 	struct fs fs;
    136 	char pad[SBLOCKSIZE];
    137 } fsun;
    138 #define	sblock	fsun.fs
    139 
    140 struct	csum *fscs_0;		/* first block of cylinder summaries */
    141 struct	csum *fscs_next;	/* place for next summary */
    142 struct	csum *fscs_end;		/* end of summary buffer */
    143 struct	csum *fscs_reset;	/* place for next summary after write */
    144 uint	fs_csaddr;		/* fragment number to write to */
    145 
    146 union {
    147 	struct cg cg;
    148 	char pad[MAXBSIZE];
    149 } cgun;
    150 #define	acg	cgun.cg
    151 
    152 #define DIP(dp, field) \
    153 	((sblock.fs_magic == FS_UFS1_MAGIC) ? \
    154 	(dp)->dp1.di_##field : (dp)->dp2.di_##field)
    155 
    156 char *iobuf;
    157 int iobufsize;
    158 
    159 int	fsi, fso;
    160 
    161 void
    162 mkfs(struct partition *pp, const char *fsys, int fi, int fo,
    163     mode_t mfsmode, uid_t mfsuid, gid_t mfsgid)
    164 {
    165 	uint fragsperinodeblk, ncg;
    166 	uint cgzero;
    167 	uint64_t inodeblks, cgall;
    168 	int32_t cylno, i, csfrags;
    169 	struct timeval tv;
    170 	long long sizepb;
    171 	int nprintcols, printcolwidth;
    172 
    173 #ifndef STANDALONE
    174 	gettimeofday(&tv, NULL);
    175 #endif
    176 #ifdef MFS
    177 	if (mfs && !Nflag) {
    178 		calc_memfree();
    179 		if (fssize * sectorsize > memleft)
    180 			fssize = memleft / sectorsize;
    181 		if ((membase = mkfs_malloc(fssize * sectorsize)) == 0)
    182 			exit(12);
    183 	}
    184 #endif
    185 	fsi = fi;
    186 	fso = fo;
    187 	if (Oflag == 0) {
    188 		sblock.fs_old_inodefmt = FS_42INODEFMT;
    189 		sblock.fs_maxsymlinklen = 0;
    190 		sblock.fs_old_flags = 0;
    191 	} else {
    192 		sblock.fs_old_inodefmt = FS_44INODEFMT;
    193 		sblock.fs_maxsymlinklen = (Oflag == 1 ? MAXSYMLINKLEN_UFS1 :
    194 		    MAXSYMLINKLEN_UFS2);
    195 		sblock.fs_old_flags = FS_FLAGS_UPDATED;
    196 		sblock.fs_flags = 0;
    197 	}
    198 
    199 	/*
    200 	 * collect and verify the filesystem density info
    201 	 */
    202 	sblock.fs_avgfilesize = avgfilesize;
    203 	sblock.fs_avgfpdir = avgfpdir;
    204 	if (sblock.fs_avgfilesize <= 0) {
    205 		printf("illegal expected average file size %d\n",
    206 		    sblock.fs_avgfilesize);
    207 		exit(14);
    208 	}
    209 	if (sblock.fs_avgfpdir <= 0) {
    210 		printf("illegal expected number of files per directory %d\n",
    211 		    sblock.fs_avgfpdir);
    212 		exit(15);
    213 	}
    214 	/*
    215 	 * collect and verify the block and fragment sizes
    216 	 */
    217 	sblock.fs_bsize = bsize;
    218 	sblock.fs_fsize = fsize;
    219 	if (!POWEROF2(sblock.fs_bsize)) {
    220 		printf("block size must be a power of 2, not %d\n",
    221 		    sblock.fs_bsize);
    222 		exit(16);
    223 	}
    224 	if (!POWEROF2(sblock.fs_fsize)) {
    225 		printf("fragment size must be a power of 2, not %d\n",
    226 		    sblock.fs_fsize);
    227 		exit(17);
    228 	}
    229 	if (sblock.fs_fsize < sectorsize) {
    230 		printf("fragment size %d is too small, minimum is %d\n",
    231 		    sblock.fs_fsize, sectorsize);
    232 		exit(18);
    233 	}
    234 	if (sblock.fs_bsize < MINBSIZE) {
    235 		printf("block size %d is too small, minimum is %d\n",
    236 		    sblock.fs_bsize, MINBSIZE);
    237 		exit(19);
    238 	}
    239 	if (sblock.fs_bsize > MAXBSIZE) {
    240 		printf("block size %d is too large, maximum is %d\n",
    241 		    sblock.fs_bsize, MAXBSIZE);
    242 		exit(19);
    243 	}
    244 	if (sblock.fs_bsize < sblock.fs_fsize) {
    245 		printf("block size (%d) cannot be smaller than fragment size (%d)\n",
    246 		    sblock.fs_bsize, sblock.fs_fsize);
    247 		exit(20);
    248 	}
    249 
    250 	if (maxbsize < bsize || !POWEROF2(maxbsize)) {
    251 		sblock.fs_maxbsize = sblock.fs_bsize;
    252 	} else if (sblock.fs_maxbsize > FS_MAXCONTIG * sblock.fs_bsize) {
    253 		sblock.fs_maxbsize = FS_MAXCONTIG * sblock.fs_bsize;
    254 	} else {
    255 		sblock.fs_maxbsize = maxbsize;
    256 	}
    257 	sblock.fs_maxcontig = maxcontig;
    258 	if (sblock.fs_maxcontig < sblock.fs_maxbsize / sblock.fs_bsize) {
    259 		sblock.fs_maxcontig = sblock.fs_maxbsize / sblock.fs_bsize;
    260 		printf("Maxcontig raised to %d\n", sblock.fs_maxbsize);
    261 	}
    262 	if (sblock.fs_maxcontig > 1)
    263 		sblock.fs_contigsumsize = MIN(sblock.fs_maxcontig,FS_MAXCONTIG);
    264 
    265 	sblock.fs_bmask = ~(sblock.fs_bsize - 1);
    266 	sblock.fs_fmask = ~(sblock.fs_fsize - 1);
    267 	sblock.fs_qbmask = ~sblock.fs_bmask;
    268 	sblock.fs_qfmask = ~sblock.fs_fmask;
    269 	for (sblock.fs_bshift = 0, i = sblock.fs_bsize; i > 1; i >>= 1)
    270 		sblock.fs_bshift++;
    271 	for (sblock.fs_fshift = 0, i = sblock.fs_fsize; i > 1; i >>= 1)
    272 		sblock.fs_fshift++;
    273 	sblock.fs_frag = numfrags(&sblock, sblock.fs_bsize);
    274 	for (sblock.fs_fragshift = 0, i = sblock.fs_frag; i > 1; i >>= 1)
    275 		sblock.fs_fragshift++;
    276 	if (sblock.fs_frag > MAXFRAG) {
    277 		printf("fragment size %d is too small, "
    278 			"minimum with block size %d is %d\n",
    279 		    sblock.fs_fsize, sblock.fs_bsize,
    280 		    sblock.fs_bsize / MAXFRAG);
    281 		exit(21);
    282 	}
    283 	sblock.fs_fsbtodb = ilog2(sblock.fs_fsize / sectorsize);
    284 	sblock.fs_size = dbtofsb(&sblock, fssize);
    285 	if (Oflag <= 1) {
    286 		if (sblock.fs_size >= 1ull << 31) {
    287 			printf("Too many fragments (0x%" PRIx64
    288 			    ") for a UFS1 filesystem\n", sblock.fs_size);
    289 			exit(22);
    290 		}
    291 		sblock.fs_magic = FS_UFS1_MAGIC;
    292 		sblock.fs_sblockloc = SBLOCK_UFS1;
    293 		sblock.fs_nindir = sblock.fs_bsize / sizeof(int32_t);
    294 		sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs1_dinode);
    295 		sblock.fs_maxsymlinklen = ((NDADDR + NIADDR) *
    296 		    sizeof (int32_t));
    297 		sblock.fs_old_inodefmt = FS_44INODEFMT;
    298 		sblock.fs_old_cgoffset = 0;
    299 		sblock.fs_old_cgmask = 0xffffffff;
    300 		sblock.fs_old_size = sblock.fs_size;
    301 		sblock.fs_old_rotdelay = 0;
    302 		sblock.fs_old_rps = 60;
    303 		sblock.fs_old_nspf = sblock.fs_fsize / sectorsize;
    304 		sblock.fs_old_cpg = 1;
    305 		sblock.fs_old_interleave = 1;
    306 		sblock.fs_old_trackskew = 0;
    307 		sblock.fs_old_cpc = 0;
    308 		sblock.fs_old_postblformat = FS_DYNAMICPOSTBLFMT;
    309 		sblock.fs_old_nrpos = 1;
    310 	} else {
    311 		sblock.fs_magic = FS_UFS2_MAGIC;
    312 		sblock.fs_sblockloc = SBLOCK_UFS2;
    313 		sblock.fs_nindir = sblock.fs_bsize / sizeof(int64_t);
    314 		sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs2_dinode);
    315 		sblock.fs_maxsymlinklen = ((NDADDR + NIADDR) *
    316 		    sizeof (int64_t));
    317 	}
    318 
    319 	sblock.fs_sblkno =
    320 	    roundup(howmany(sblock.fs_sblockloc + SBLOCKSIZE, sblock.fs_fsize),
    321 		sblock.fs_frag);
    322 	sblock.fs_cblkno = (daddr_t)(sblock.fs_sblkno +
    323 	    roundup(howmany(SBLOCKSIZE, sblock.fs_fsize), sblock.fs_frag));
    324 	sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag;
    325 	sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1;
    326 	for (sizepb = sblock.fs_bsize, i = 0; i < NIADDR; i++) {
    327 		sizepb *= NINDIR(&sblock);
    328 		sblock.fs_maxfilesize += sizepb;
    329 	}
    330 
    331 	/*
    332 	 * Calculate the number of blocks to put into each cylinder group.
    333 	 *
    334 	 * The cylinder group size is limited because the data structure
    335 	 * must fit into a single block.
    336 	 * We try to have as few cylinder groups as possible, with a proviso
    337 	 * that we create at least MINCYLGRPS (==4) except for small
    338 	 * filesystems.
    339 	 *
    340 	 * This algorithm works out how many blocks of inodes would be
    341 	 * needed to fill the entire volume at the specified density.
    342 	 * It then looks at how big the 'cylinder block' would have to
    343 	 * be and, assuming that it is linearly related to the number
    344 	 * of inodes and blocks how many cylinder groups are needed to
    345 	 * keep the cylinder block below the filesystem block size.
    346 	 *
    347 	 * The cylinder groups are then all created with the average size.
    348 	 *
    349 	 * Space taken by the red tape on cylinder groups other than the
    350 	 * first is ignored.
    351 	 */
    352 
    353 	/* There must be space for 1 inode block and 2 data blocks */
    354 	if (sblock.fs_size < sblock.fs_iblkno + 3 * sblock.fs_frag) {
    355 		printf("Filesystem size %lld < minimum size of %d\n",
    356 		    (long long)sblock.fs_size, sblock.fs_iblkno + 3 * sblock.fs_frag);
    357 		exit(23);
    358 	}
    359 	if (num_inodes != 0)
    360 		inodeblks = howmany(num_inodes, INOPB(&sblock));
    361 	else {
    362 		/*
    363 		 * Calculate 'per inode block' so we can allocate less than
    364 		 * 1 fragment per inode - useful for /dev.
    365 		 */
    366 		fragsperinodeblk = MAX(numfrags(&sblock,
    367 					density * INOPB(&sblock)), 1);
    368 		inodeblks = (sblock.fs_size - sblock.fs_iblkno) /
    369 			(sblock.fs_frag + fragsperinodeblk);
    370 	}
    371 	if (inodeblks == 0)
    372 		inodeblks = 1;
    373 	/* Ensure that there are at least 2 data blocks (or we fail below) */
    374 	if (inodeblks > (sblock.fs_size - sblock.fs_iblkno)/sblock.fs_frag - 2)
    375 		inodeblks = (sblock.fs_size-sblock.fs_iblkno)/sblock.fs_frag-2;
    376 	/* Even UFS2 limits number of inodes to 2^31 (fs_ipg is int32_t) */
    377 	if (inodeblks * INOPB(&sblock) >= 1ull << 31)
    378 		inodeblks = ((1ull << 31) - NBBY) / INOPB(&sblock);
    379 	/*
    380 	 * See what would happen if we tried to use 1 cylinder group.
    381 	 * Assume space linear, so work out number of cylinder groups needed.
    382 	 * Subtract one from the allowed size to compensate for rounding
    383 	 * a number of bits up to a complete byte.
    384 	 */
    385 	cgzero = CGSIZE_IF(&sblock, 0, 0);
    386 	cgall = CGSIZE_IF(&sblock, inodeblks * INOPB(&sblock), sblock.fs_size);
    387 	ncg = howmany(cgall - cgzero, sblock.fs_bsize - cgzero - 1);
    388 	if (ncg < MINCYLGRPS) {
    389 		/*
    390 		 * We would like to allocate MINCLYGRPS cylinder groups,
    391 		 * but for small file sytems (especially ones with a lot
    392 		 * of inodes) this is not desirable (or possible).
    393 		 */
    394 		i = sblock.fs_size / 2 / (sblock.fs_iblkno +
    395 						inodeblks * sblock.fs_frag);
    396 		if (i > ncg)
    397 			ncg = i;
    398 		if (ncg > MINCYLGRPS)
    399 			ncg = MINCYLGRPS;
    400 		if (ncg > inodeblks)
    401 			ncg = inodeblks;
    402 	}
    403 	/*
    404 	 * Put an equal number of blocks in each cylinder group.
    405 	 * Round up so we don't have more fragments in the last CG than
    406 	 * the earlier ones (does that matter?), but kill a block if the
    407 	 * CGSIZE becomes too big (only happens if there are a lot of CGs).
    408 	 */
    409 	sblock.fs_fpg = roundup(howmany(sblock.fs_size, ncg), sblock.fs_frag);
    410 	i = CGSIZE_IF(&sblock, inodeblks * INOPB(&sblock) / ncg, sblock.fs_fpg);
    411 	if (i > sblock.fs_bsize)
    412 		sblock.fs_fpg -= (i - sblock.fs_bsize) * NBBY;
    413 	/* ... and recalculate how many cylinder groups we now need */
    414 	ncg = howmany(sblock.fs_size, sblock.fs_fpg);
    415 	inodeblks /= ncg;
    416 	if (inodeblks == 0)
    417 		inodeblks = 1;
    418 	sblock.fs_ipg = inodeblks * INOPB(&sblock);
    419 	/* Sanity check on our sums... */
    420 	if (CGSIZE(&sblock) > sblock.fs_bsize) {
    421 		printf("CGSIZE miscalculated %d > %d\n",
    422 		    (int)CGSIZE(&sblock), sblock.fs_bsize);
    423 		exit(24);
    424 	}
    425 	/* Check that the last cylinder group has enough space for the inodes */
    426 	i = sblock.fs_size - sblock.fs_fpg * (ncg - 1ull);
    427 	if (i < sblock.fs_iblkno + inodeblks * sblock.fs_frag) {
    428 		/*
    429 		 * Since we make all the cylinder groups the same size, the
    430 		 * last will only be small if there are a large number of
    431 		 * cylinder groups. If we pull even a fragment from each
    432 		 * of the other groups then the last CG will be overfull.
    433 		 * So we just kill the last CG.
    434 		 */
    435 		ncg--;
    436 		sblock.fs_size -= i;
    437 	}
    438 	sblock.fs_ncg = ncg;
    439 
    440 	sblock.fs_cgsize = fragroundup(&sblock, CGSIZE(&sblock));
    441 	sblock.fs_dblkno = sblock.fs_iblkno + sblock.fs_ipg / INOPF(&sblock);
    442 	if (Oflag <= 1) {
    443 		sblock.fs_old_spc = sblock.fs_fpg * sblock.fs_old_nspf;
    444 		sblock.fs_old_nsect = sblock.fs_old_spc;
    445 		sblock.fs_old_npsect = sblock.fs_old_spc;
    446 		sblock.fs_old_ncyl = sblock.fs_ncg;
    447 	}
    448 
    449 	/*
    450 	 * Cylinder group summary information for each cylinder is written
    451 	 * into the first cylinder group.
    452 	 * Write this fragment by fragment, but doing the first CG last
    453 	 * (after we've taken stuff off for the structure itself and the
    454 	 * root directory.
    455 	 */
    456 	sblock.fs_csaddr = cgdmin(&sblock, 0);
    457 	sblock.fs_cssize =
    458 	    fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum));
    459 	if (512 % sizeof *fscs_0)
    460 		errx(1, "cylinder group summary doesn't fit in sectors");
    461 	fscs_0 = calloc(1, 2 * sblock.fs_fsize);
    462 	if (fscs_0 == NULL)
    463 		exit(39);
    464 	fs_csaddr = sblock.fs_csaddr;
    465 	fscs_next = fscs_0;
    466 	fscs_end = (void *)((char *)fscs_0 + 2 * sblock.fs_fsize);
    467 	fscs_reset = (void *)((char *)fscs_0 + sblock.fs_fsize);
    468 	/*
    469 	 * fill in remaining fields of the super block
    470 	 */
    471 	sblock.fs_sbsize = fragroundup(&sblock, sizeof(struct fs));
    472 	if (sblock.fs_sbsize > SBLOCKSIZE)
    473 		sblock.fs_sbsize = SBLOCKSIZE;
    474 	sblock.fs_minfree = minfree;
    475 	sblock.fs_maxcontig = maxcontig;
    476 	sblock.fs_maxbpg = maxbpg;
    477 	sblock.fs_optim = opt;
    478 	sblock.fs_cgrotor = 0;
    479 	sblock.fs_pendingblocks = 0;
    480 	sblock.fs_pendinginodes = 0;
    481 	sblock.fs_cstotal.cs_ndir = 0;
    482 	sblock.fs_cstotal.cs_nbfree = 0;
    483 	sblock.fs_cstotal.cs_nifree = 0;
    484 	sblock.fs_cstotal.cs_nffree = 0;
    485 	sblock.fs_fmod = 0;
    486 	sblock.fs_ronly = 0;
    487 	sblock.fs_state = 0;
    488 	sblock.fs_clean = FS_ISCLEAN;
    489 	sblock.fs_ronly = 0;
    490 	sblock.fs_id[0] = (long)tv.tv_sec;	/* XXXfvdl huh? */
    491 	sblock.fs_id[1] = arc4random() & INT32_MAX;
    492 	sblock.fs_fsmnt[0] = '\0';
    493 	csfrags = howmany(sblock.fs_cssize, sblock.fs_fsize);
    494 	sblock.fs_dsize = sblock.fs_size - sblock.fs_sblkno -
    495 	    sblock.fs_ncg * (sblock.fs_dblkno - sblock.fs_sblkno);
    496 	sblock.fs_cstotal.cs_nbfree =
    497 	    fragstoblks(&sblock, sblock.fs_dsize) -
    498 	    howmany(csfrags, sblock.fs_frag);
    499 	sblock.fs_cstotal.cs_nffree =
    500 	    fragnum(&sblock, sblock.fs_size) +
    501 	    (fragnum(&sblock, csfrags) > 0 ?
    502 	    sblock.fs_frag - fragnum(&sblock, csfrags) : 0);
    503 	sblock.fs_cstotal.cs_nifree = sblock.fs_ncg * sblock.fs_ipg - ROOTINO;
    504 	sblock.fs_cstotal.cs_ndir = 0;
    505 	sblock.fs_dsize -= csfrags;
    506 	sblock.fs_time = tv.tv_sec;
    507 	if (Oflag <= 1) {
    508 		sblock.fs_old_time = tv.tv_sec;
    509 		sblock.fs_old_dsize = sblock.fs_dsize;
    510 		sblock.fs_old_csaddr = sblock.fs_csaddr;
    511 		sblock.fs_old_cstotal.cs_ndir = sblock.fs_cstotal.cs_ndir;
    512 		sblock.fs_old_cstotal.cs_nbfree = sblock.fs_cstotal.cs_nbfree;
    513 		sblock.fs_old_cstotal.cs_nifree = sblock.fs_cstotal.cs_nifree;
    514 		sblock.fs_old_cstotal.cs_nffree = sblock.fs_cstotal.cs_nffree;
    515 	}
    516 	/*
    517 	 * Dump out summary information about file system.
    518 	 */
    519 	if (!mfs || Nflag) {
    520 #define	B2MBFACTOR (1 / (1024.0 * 1024.0))
    521 		printf("%s: %.1fMB (%lld sectors) block size %d, "
    522 		       "fragment size %d\n",
    523 		    fsys, (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR,
    524 		    (long long)fsbtodb(&sblock, sblock.fs_size),
    525 		    sblock.fs_bsize, sblock.fs_fsize);
    526 		printf("\tusing %d cylinder groups of %.2fMB, %d blks, "
    527 		       "%d inodes.\n",
    528 		    sblock.fs_ncg,
    529 		    (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR,
    530 		    sblock.fs_fpg / sblock.fs_frag, sblock.fs_ipg);
    531 #undef B2MBFACTOR
    532 	}
    533 	/*
    534 	 * Now determine how wide each column will be, and calculate how
    535 	 * many columns will fit in a 80 char line.
    536 	 */
    537 	printcolwidth = count_digits(
    538 			fsbtodb(&sblock, cgsblock(&sblock, sblock.fs_ncg -1)));
    539 	nprintcols = 80 / (printcolwidth + 2);
    540 
    541 	/*
    542 	 * allocate space for superblock, cylinder group map, and
    543 	 * two sets of inode blocks.
    544 	 */
    545 	if (sblock.fs_bsize < SBLOCKSIZE)
    546 		iobufsize = SBLOCKSIZE + 3 * sblock.fs_bsize;
    547 	else
    548 		iobufsize = 4 * sblock.fs_bsize;
    549 	if ((iobuf = malloc(iobufsize)) == 0) {
    550 		printf("Cannot allocate I/O buffer\n");
    551 		exit(38);
    552 	}
    553 	memset(iobuf, 0, iobufsize);
    554 
    555 	/*
    556 	 * We now start writing to the filesystem
    557 	 */
    558 
    559 	/*
    560 	 * Validate the given file system size.
    561 	 * Verify that its last block can actually be accessed.
    562 	 * Convert to file system fragment sized units.
    563 	 */
    564 	if (fssize <= 0) {
    565 		printf("preposterous size %lld\n", (long long)fssize);
    566 		exit(13);
    567 	}
    568 	wtfs(fssize - 1, sectorsize, iobuf);
    569 
    570 	/*
    571 	 * Ensure there is nothing that looks like a filesystem
    572 	 * superbock anywhere other than where ours will be.
    573 	 * If fsck finds the wrong one all hell breaks loose!
    574 	 */
    575 	for (i = 0; ; i++) {
    576 		static const int sblocklist[] = SBLOCKSEARCH;
    577 		int sblkoff = sblocklist[i];
    578 		int sz;
    579 		if (sblkoff == -1)
    580 			break;
    581 		/* Remove main superblock */
    582 		zap_old_sblock(sblkoff);
    583 		/* and all possible locations for the first alternate */
    584 		sblkoff += SBLOCKSIZE;
    585 		for (sz = SBLOCKSIZE; sz <= 0x10000; sz <<= 1)
    586 			zap_old_sblock(roundup(sblkoff, sz));
    587 	}
    588 
    589 	if (isappleufs) {
    590 		struct appleufslabel appleufs;
    591 		ffs_appleufs_set(&appleufs, appleufs_volname, tv.tv_sec, 0);
    592 		wtfs(APPLEUFS_LABEL_OFFSET/sectorsize, APPLEUFS_LABEL_SIZE,
    593 		    &appleufs);
    594 	} else {
    595 		struct appleufslabel appleufs;
    596 		/* Look for and zap any existing valid apple ufs labels */
    597 		rdfs(APPLEUFS_LABEL_OFFSET/sectorsize, APPLEUFS_LABEL_SIZE,
    598 		    &appleufs);
    599 		if (ffs_appleufs_validate(fsys, &appleufs, NULL) == 0) {
    600 			memset(&appleufs, 0, sizeof(appleufs));
    601 			wtfs(APPLEUFS_LABEL_OFFSET/sectorsize, APPLEUFS_LABEL_SIZE,
    602 			    &appleufs);
    603 		}
    604 	}
    605 
    606 	/*
    607 	 * Make a copy of the superblock into the buffer that we will be
    608 	 * writing out in each cylinder group.
    609 	 */
    610 	memcpy(iobuf, &sblock, sizeof sblock);
    611 	if (needswap)
    612 		ffs_sb_swap(&sblock, (struct fs *)iobuf);
    613 
    614 	if (!mfs || Nflag)
    615 		printf("super-block backups (for fsck -b #) at:");
    616 	for (cylno = 0; cylno < sblock.fs_ncg; cylno++) {
    617 		initcg(cylno, &tv);
    618 		if (mfs && !Nflag)
    619 			continue;
    620 		if (cylno % nprintcols == 0)
    621 			printf("\n");
    622 		printf(" %*lld,", printcolwidth,
    623 			(long long)fsbtodb(&sblock, cgsblock(&sblock, cylno)));
    624 		fflush(stdout);
    625 	}
    626 	if (!mfs || Nflag)
    627 		printf("\n");
    628 	if (Nflag)
    629 		exit(0);
    630 
    631 	/*
    632 	 * Now construct the initial file system,
    633 	 */
    634 	if (fsinit(&tv, mfsmode, mfsuid, mfsgid) == 0 && mfs)
    635 		errx(1, "Error making filesystem");
    636 	sblock.fs_time = tv.tv_sec;
    637 	if (Oflag <= 1) {
    638 		sblock.fs_old_cstotal.cs_ndir = sblock.fs_cstotal.cs_ndir;
    639 		sblock.fs_old_cstotal.cs_nbfree = sblock.fs_cstotal.cs_nbfree;
    640 		sblock.fs_old_cstotal.cs_nifree = sblock.fs_cstotal.cs_nifree;
    641 		sblock.fs_old_cstotal.cs_nffree = sblock.fs_cstotal.cs_nffree;
    642 	}
    643 	/*
    644 	 * Write out the super-block and zeros until the first cg info
    645 	 */
    646 	memset(iobuf, 0, iobufsize);
    647         memcpy(iobuf, &sblock, sizeof sblock);
    648 	if (needswap)
    649 		ffs_sb_swap(&sblock, (struct fs *)iobuf);
    650         wtfs(sblock.fs_sblockloc / sectorsize,
    651 	    cgsblock(&sblock, 0) * sblock.fs_fsize - sblock.fs_sblockloc,
    652 	    iobuf);
    653 
    654 	/* Write out first and last cylinder summary sectors */
    655 	if (needswap)
    656 		ffs_csum_swap(fscs_0, fscs_0, sblock.fs_fsize);
    657 	wtfs(fsbtodb(&sblock, sblock.fs_csaddr), sblock.fs_fsize, fscs_0);
    658 
    659 	if (fscs_next > fscs_reset) {
    660 		if (needswap)
    661 			ffs_csum_swap(fscs_reset, fscs_reset, sblock.fs_fsize);
    662 		fs_csaddr++;
    663 		wtfs(fsbtodb(&sblock, fs_csaddr), sblock.fs_fsize, fscs_reset);
    664 	}
    665 
    666 	/*
    667 	 * Update information about this partion in pack
    668 	 * label, to that it may be updated on disk.
    669 	 */
    670 	if (pp == NULL)
    671 		return;
    672 	if (isappleufs)
    673 		pp->p_fstype = FS_APPLEUFS;
    674 	else
    675 		pp->p_fstype = FS_BSDFFS;
    676 	pp->p_fsize = sblock.fs_fsize;
    677 	pp->p_frag = sblock.fs_frag;
    678 	pp->p_cpg = sblock.fs_fpg;
    679 }
    680 
    681 /*
    682  * Initialize a cylinder group.
    683  */
    684 void
    685 initcg(int cylno, const struct timeval *tv)
    686 {
    687 	daddr_t cbase, dmax;
    688 	int32_t i, j, d, dlower, dupper, blkno;
    689 	struct ufs1_dinode *dp1;
    690 	struct ufs2_dinode *dp2;
    691 	int start;
    692 
    693 	/*
    694 	 * Determine block bounds for cylinder group.
    695 	 * Allow space for super block summary information in first
    696 	 * cylinder group.
    697 	 */
    698 	cbase = cgbase(&sblock, cylno);
    699 	dmax = cbase + sblock.fs_fpg;
    700 	if (dmax > sblock.fs_size)
    701 		dmax = sblock.fs_size;
    702 	dlower = cgsblock(&sblock, cylno) - cbase;
    703 	dupper = cgdmin(&sblock, cylno) - cbase;
    704 	if (cylno == 0) {
    705 		dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
    706 		if (dupper >= cgstart(&sblock, cylno + 1)) {
    707 			printf("\rToo many cylinder groups to fit summary "
    708 				"information into first cylinder group\n");
    709 			exit(40);
    710 		}
    711 	}
    712 	memset(&acg, 0, sblock.fs_cgsize);
    713 	acg.cg_magic = CG_MAGIC;
    714 	acg.cg_cgx = cylno;
    715 	acg.cg_ndblk = dmax - cbase;
    716 	if (sblock.fs_contigsumsize > 0)
    717 		acg.cg_nclusterblks = acg.cg_ndblk >> sblock.fs_fragshift;
    718 	start = &acg.cg_space[0] - (u_char *)(&acg.cg_firstfield);
    719 	if (Oflag == 2) {
    720 		acg.cg_time = tv->tv_sec;
    721 		acg.cg_niblk = sblock.fs_ipg;
    722 		acg.cg_initediblk = sblock.fs_ipg < 2 * INOPB(&sblock) ?
    723 		    sblock.fs_ipg : 2 * INOPB(&sblock);
    724 		acg.cg_iusedoff = start;
    725 	} else {
    726 		acg.cg_old_ncyl = sblock.fs_old_cpg;
    727 		acg.cg_old_time = tv->tv_sec;
    728 		acg.cg_old_niblk = sblock.fs_ipg;
    729 		acg.cg_old_btotoff = start;
    730 		acg.cg_old_boff = acg.cg_old_btotoff +
    731 		    sblock.fs_old_cpg * sizeof(int32_t);
    732 		acg.cg_iusedoff = acg.cg_old_boff +
    733 		    sblock.fs_old_cpg * sizeof(u_int16_t);
    734 	}
    735 	acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, CHAR_BIT);
    736 	if (sblock.fs_contigsumsize <= 0) {
    737 		acg.cg_nextfreeoff = acg.cg_freeoff +
    738 		   howmany(sblock.fs_fpg, CHAR_BIT);
    739 	} else {
    740 		acg.cg_clustersumoff = acg.cg_freeoff +
    741 		    howmany(sblock.fs_fpg, CHAR_BIT) - sizeof(int32_t);
    742 		if (isappleufs) {
    743 			/* Apple PR2216969 gives rationale for this change.
    744 			 * I believe they were mistaken, but we need to
    745 			 * duplicate it for compatibility.  -- dbj (at) NetBSD.org
    746 			 */
    747 			acg.cg_clustersumoff += sizeof(int32_t);
    748 		}
    749 		acg.cg_clustersumoff =
    750 		    roundup(acg.cg_clustersumoff, sizeof(int32_t));
    751 		acg.cg_clusteroff = acg.cg_clustersumoff +
    752 		    (sblock.fs_contigsumsize + 1) * sizeof(int32_t);
    753 		acg.cg_nextfreeoff = acg.cg_clusteroff +
    754 		    howmany(fragstoblks(&sblock, sblock.fs_fpg), CHAR_BIT);
    755 	}
    756 	if (acg.cg_nextfreeoff > sblock.fs_cgsize) {
    757 		printf("Panic: cylinder group too big\n");
    758 		exit(37);
    759 	}
    760 	acg.cg_cs.cs_nifree += sblock.fs_ipg;
    761 	if (cylno == 0)
    762 		for (i = 0; i < ROOTINO; i++) {
    763 			setbit(cg_inosused(&acg, 0), i);
    764 			acg.cg_cs.cs_nifree--;
    765 		}
    766 	if (cylno > 0) {
    767 		/*
    768 		 * In cylno 0, beginning space is reserved
    769 		 * for boot and super blocks.
    770 		 */
    771 		for (d = 0, blkno = 0; d < dlower;) {
    772 			setblock(&sblock, cg_blksfree(&acg, 0), blkno);
    773 			if (sblock.fs_contigsumsize > 0)
    774 				setbit(cg_clustersfree(&acg, 0), blkno);
    775 			acg.cg_cs.cs_nbfree++;
    776 			d += sblock.fs_frag;
    777 			blkno++;
    778 		}
    779 	}
    780 	if ((i = (dupper & (sblock.fs_frag - 1))) != 0) {
    781 		acg.cg_frsum[sblock.fs_frag - i]++;
    782 		for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) {
    783 			setbit(cg_blksfree(&acg, 0), dupper);
    784 			acg.cg_cs.cs_nffree++;
    785 		}
    786 	}
    787 	for (d = dupper, blkno = dupper >> sblock.fs_fragshift;
    788 	     d + sblock.fs_frag <= acg.cg_ndblk; ) {
    789 		setblock(&sblock, cg_blksfree(&acg, 0), blkno);
    790 		if (sblock.fs_contigsumsize > 0)
    791 			setbit(cg_clustersfree(&acg, 0), blkno);
    792 		acg.cg_cs.cs_nbfree++;
    793 		d += sblock.fs_frag;
    794 		blkno++;
    795 	}
    796 	if (d < acg.cg_ndblk) {
    797 		acg.cg_frsum[acg.cg_ndblk - d]++;
    798 		for (; d < acg.cg_ndblk; d++) {
    799 			setbit(cg_blksfree(&acg, 0), d);
    800 			acg.cg_cs.cs_nffree++;
    801 		}
    802 	}
    803 	if (sblock.fs_contigsumsize > 0) {
    804 		int32_t *sump = cg_clustersum(&acg, 0);
    805 		u_char *mapp = cg_clustersfree(&acg, 0);
    806 		int map = *mapp++;
    807 		int bit = 1;
    808 		int run = 0;
    809 
    810 		for (i = 0; i < acg.cg_nclusterblks; i++) {
    811 			if ((map & bit) != 0) {
    812 				run++;
    813 			} else if (run != 0) {
    814 				if (run > sblock.fs_contigsumsize)
    815 					run = sblock.fs_contigsumsize;
    816 				sump[run]++;
    817 				run = 0;
    818 			}
    819 			if ((i & (CHAR_BIT - 1)) != (CHAR_BIT - 1)) {
    820 				bit <<= 1;
    821 			} else {
    822 				map = *mapp++;
    823 				bit = 1;
    824 			}
    825 		}
    826 		if (run != 0) {
    827 			if (run > sblock.fs_contigsumsize)
    828 				run = sblock.fs_contigsumsize;
    829 			sump[run]++;
    830 		}
    831 	}
    832 	*fscs_next++ = acg.cg_cs;
    833 	if (fscs_next == fscs_end) {
    834 		if (needswap)
    835 			ffs_csum_swap(fscs_reset, fscs_reset, sblock.fs_fsize);
    836 		fs_csaddr++;
    837 		wtfs(fsbtodb(&sblock, fs_csaddr), sblock.fs_fsize, fscs_reset);
    838 		fscs_next = fscs_reset;
    839 		memset(fscs_next, 0, sblock.fs_fsize);
    840 	}
    841 	/*
    842 	 * Write out the duplicate super block, the cylinder group map
    843 	 * and two blocks worth of inodes in a single write.
    844 	 */
    845 	start = sblock.fs_bsize > SBLOCKSIZE ? sblock.fs_bsize : SBLOCKSIZE;
    846 	memcpy(&iobuf[start], &acg, sblock.fs_cgsize);
    847 	if (needswap)
    848 		ffs_cg_swap(&acg, (struct cg*)&iobuf[start], &sblock);
    849 	start += sblock.fs_bsize;
    850 	dp1 = (struct ufs1_dinode *)(&iobuf[start]);
    851 	dp2 = (struct ufs2_dinode *)(&iobuf[start]);
    852 	for (i = MIN(sblock.fs_ipg, 2) * INOPB(&sblock); i != 0; i--) {
    853 		if (sblock.fs_magic == FS_UFS1_MAGIC) {
    854 			/* No need to swap, it'll stay random */
    855 			dp1->di_gen = arc4random() & INT32_MAX;
    856 			dp1++;
    857 		} else {
    858 			dp2->di_gen = arc4random() & INT32_MAX;
    859 			dp2++;
    860 		}
    861 	}
    862 	wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)), iobufsize, iobuf);
    863 	/*
    864 	 * For the old file system, we have to initialize all the inodes.
    865 	 */
    866 	if (Oflag <= 1) {
    867 		for (i = 2 * sblock.fs_frag;
    868 		     i < sblock.fs_ipg / INOPF(&sblock);
    869 		     i += sblock.fs_frag) {
    870 			dp1 = (struct ufs1_dinode *)(&iobuf[start]);
    871 			for (j = 0; j < INOPB(&sblock); j++) {
    872 				dp1->di_gen = arc4random() & INT32_MAX;
    873 				dp1++;
    874 			}
    875 			wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i),
    876 			    sblock.fs_bsize, &iobuf[start]);
    877 		}
    878 	}
    879 }
    880 
    881 /*
    882  * initialize the file system
    883  */
    884 
    885 #ifdef LOSTDIR
    886 #define	PREDEFDIR 3
    887 #else
    888 #define	PREDEFDIR 2
    889 #endif
    890 
    891 struct direct root_dir[] = {
    892 	{ ROOTINO, sizeof(struct direct), DT_DIR, 1, "." },
    893 	{ ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
    894 #ifdef LOSTDIR
    895 	{ LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 10, "lost+found" },
    896 #endif
    897 };
    898 struct odirect {
    899 	u_int32_t d_ino;
    900 	u_int16_t d_reclen;
    901 	u_int16_t d_namlen;
    902 	u_char	d_name[MAXNAMLEN + 1];
    903 } oroot_dir[] = {
    904 	{ ROOTINO, sizeof(struct direct), 1, "." },
    905 	{ ROOTINO, sizeof(struct direct), 2, ".." },
    906 #ifdef LOSTDIR
    907 	{ LOSTFOUNDINO, sizeof(struct direct), 10, "lost+found" },
    908 #endif
    909 };
    910 #ifdef LOSTDIR
    911 struct direct lost_found_dir[] = {
    912 	{ LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 1, "." },
    913 	{ ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
    914 	{ 0, DIRBLKSIZ, 0, 0, 0 },
    915 };
    916 struct odirect olost_found_dir[] = {
    917 	{ LOSTFOUNDINO, sizeof(struct direct), 1, "." },
    918 	{ ROOTINO, sizeof(struct direct), 2, ".." },
    919 	{ 0, DIRBLKSIZ, 0, 0 },
    920 };
    921 #endif
    922 char buf[MAXBSIZE];
    923 static void copy_dir(struct direct *, struct direct *);
    924 
    925 int
    926 fsinit(const struct timeval *tv, mode_t mfsmode, uid_t mfsuid, gid_t mfsgid)
    927 {
    928 	union dinode node;
    929 #ifdef LOSTDIR
    930 	int i;
    931 	int dirblksiz = DIRBLKSIZ;
    932 	if (isappleufs)
    933 		dirblksiz = APPLEUFS_DIRBLKSIZ;
    934 #endif
    935 
    936 	/*
    937 	 * initialize the node
    938 	 */
    939 
    940 #ifdef LOSTDIR
    941 	/*
    942 	 * create the lost+found directory
    943 	 */
    944 	memset(&node, 0, sizeof(node));
    945 	if (Oflag == 0) {
    946 		(void)makedir((struct direct *)olost_found_dir, 2);
    947 		for (i = dirblksiz; i < sblock.fs_bsize; i += dirblksiz)
    948 			copy_dir((struct direct*)&olost_found_dir[2],
    949 				(struct direct*)&buf[i]);
    950 	} else {
    951 		(void)makedir(lost_found_dir, 2);
    952 		for (i = dirblksiz; i < sblock.fs_bsize; i += dirblksiz)
    953 			copy_dir(&lost_found_dir[2], (struct direct*)&buf[i]);
    954 	}
    955 	if (sblock.fs_magic == FS_UFS1_MAGIC) {
    956 		node.dp1.di_atime = tv->tv_sec;
    957 		node.dp1.di_atimensec = tv->tv_usec * 1000;
    958 		node.dp1.di_mtime = tv->tv_sec;
    959 		node.dp1.di_mtimensec = tv->tv_usec * 1000;
    960 		node.dp1.di_ctime = tv->tv_sec;
    961 		node.dp1.di_ctimensec = tv->tv_usec * 1000;
    962 		node.dp1.di_mode = IFDIR | UMASK;
    963 		node.dp1.di_nlink = 2;
    964 		node.dp1.di_size = sblock.fs_bsize;
    965 		node.dp1.di_db[0] = alloc(node.dp1.di_size, node.dp1.di_mode);
    966 		if (node.dp1.di_db[0] == 0)
    967 			return (0);
    968 		node.dp1.di_blocks = btodb(fragroundup(&sblock,
    969 		    node.dp1.di_size));
    970 		node.dp1.di_uid = geteuid();
    971 		node.dp1.di_gid = getegid();
    972 		wtfs(fsbtodb(&sblock, node.dp1.di_db[0]), node.dp1.di_size,
    973 		    buf);
    974 	} else {
    975 		node.dp2.di_atime = tv->tv_sec;
    976 		node.dp2.di_atimensec = tv->tv_usec * 1000;
    977 		node.dp2.di_mtime = tv->tv_sec;
    978 		node.dp2.di_mtimensec = tv->tv_usec * 1000;
    979 		node.dp2.di_ctime = tv->tv_sec;
    980 		node.dp2.di_ctimensec = tv->tv_usec * 1000;
    981 		node.dp2.di_birthtime = tv->tv_sec;
    982 		node.dp2.di_birthnsec = tv->tv_usec * 1000;
    983 		node.dp2.di_mode = IFDIR | UMASK;
    984 		node.dp2.di_nlink = 2;
    985 		node.dp2.di_size = sblock.fs_bsize;
    986 		node.dp2.di_db[0] = alloc(node.dp2.di_size, node.dp2.di_mode);
    987 		if (node.dp2.di_db[0] == 0)
    988 			return (0);
    989 		node.dp2.di_blocks = btodb(fragroundup(&sblock,
    990 		    node.dp2.di_size));
    991 		node.dp2.di_uid = geteuid();
    992 		node.dp2.di_gid = getegid();
    993 		wtfs(fsbtodb(&sblock, node.dp2.di_db[0]), node.dp2.di_size,
    994 		    buf);
    995 	}
    996 	iput(&node, LOSTFOUNDINO);
    997 #endif
    998 	/*
    999 	 * create the root directory
   1000 	 */
   1001 	memset(&node, 0, sizeof(node));
   1002 	if (Oflag <= 1) {
   1003 		if (mfs) {
   1004 			node.dp1.di_mode = IFDIR | mfsmode;
   1005 			node.dp1.di_uid = mfsuid;
   1006 			node.dp1.di_gid = mfsgid;
   1007 		} else {
   1008 			node.dp1.di_mode = IFDIR | UMASK;
   1009 			node.dp1.di_uid = geteuid();
   1010 			node.dp1.di_gid = getegid();
   1011 		}
   1012 		node.dp1.di_nlink = PREDEFDIR;
   1013 		if (Oflag == 0)
   1014 			node.dp1.di_size = makedir((struct direct *)oroot_dir,
   1015 			    PREDEFDIR);
   1016 		else
   1017 			node.dp1.di_size = makedir(root_dir, PREDEFDIR);
   1018 		node.dp1.di_db[0] = alloc(sblock.fs_fsize, node.dp1.di_mode);
   1019 		if (node.dp1.di_db[0] == 0)
   1020 			return (0);
   1021 		node.dp1.di_blocks = btodb(fragroundup(&sblock,
   1022 		    node.dp1.di_size));
   1023 		wtfs(fsbtodb(&sblock, node.dp1.di_db[0]), sblock.fs_fsize, buf);
   1024 	} else {
   1025 		if (mfs) {
   1026 			node.dp2.di_mode = IFDIR | mfsmode;
   1027 			node.dp2.di_uid = mfsuid;
   1028 			node.dp2.di_gid = mfsgid;
   1029 		} else {
   1030 			node.dp2.di_mode = IFDIR | UMASK;
   1031 			node.dp2.di_uid = geteuid();
   1032 			node.dp2.di_gid = getegid();
   1033 		}
   1034 		node.dp2.di_atime = tv->tv_sec;
   1035 		node.dp2.di_atimensec = tv->tv_usec * 1000;
   1036 		node.dp2.di_mtime = tv->tv_sec;
   1037 		node.dp2.di_mtimensec = tv->tv_usec * 1000;
   1038 		node.dp2.di_ctime = tv->tv_sec;
   1039 		node.dp2.di_ctimensec = tv->tv_usec * 1000;
   1040 		node.dp2.di_birthtime = tv->tv_sec;
   1041 		node.dp2.di_birthnsec = tv->tv_usec * 1000;
   1042 		node.dp2.di_nlink = PREDEFDIR;
   1043 		node.dp2.di_size = makedir(root_dir, PREDEFDIR);
   1044 		node.dp2.di_db[0] = alloc(sblock.fs_fsize, node.dp2.di_mode);
   1045 		if (node.dp2.di_db[0] == 0)
   1046 			return (0);
   1047 		node.dp2.di_blocks = btodb(fragroundup(&sblock,
   1048 		    node.dp2.di_size));
   1049 		wtfs(fsbtodb(&sblock, node.dp2.di_db[0]), sblock.fs_fsize, buf);
   1050 	}
   1051 	iput(&node, ROOTINO);
   1052 	return (1);
   1053 }
   1054 
   1055 /*
   1056  * construct a set of directory entries in "buf".
   1057  * return size of directory.
   1058  */
   1059 int
   1060 makedir(struct direct *protodir, int entries)
   1061 {
   1062 	char *cp;
   1063 	int i, spcleft;
   1064 	int dirblksiz = DIRBLKSIZ;
   1065 	if (isappleufs)
   1066 		dirblksiz = APPLEUFS_DIRBLKSIZ;
   1067 
   1068 	memset(buf, 0, DIRBLKSIZ);
   1069 	spcleft = dirblksiz;
   1070 	for (cp = buf, i = 0; i < entries - 1; i++) {
   1071 		protodir[i].d_reclen = DIRSIZ(Oflag == 0, &protodir[i], 0);
   1072 		copy_dir(&protodir[i], (struct direct*)cp);
   1073 		cp += protodir[i].d_reclen;
   1074 		spcleft -= protodir[i].d_reclen;
   1075 	}
   1076 	protodir[i].d_reclen = spcleft;
   1077 	copy_dir(&protodir[i], (struct direct*)cp);
   1078 	return (dirblksiz);
   1079 }
   1080 
   1081 /*
   1082  * allocate a block or frag
   1083  */
   1084 daddr_t
   1085 alloc(int size, int mode)
   1086 {
   1087 	int i, frag;
   1088 	daddr_t d, blkno;
   1089 
   1090 	rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, &acg);
   1091 	/* fs -> host byte order */
   1092 	if (needswap)
   1093 		ffs_cg_swap(&acg, &acg, &sblock);
   1094 	if (acg.cg_magic != CG_MAGIC) {
   1095 		printf("cg 0: bad magic number\n");
   1096 		return (0);
   1097 	}
   1098 	if (acg.cg_cs.cs_nbfree == 0) {
   1099 		printf("first cylinder group ran out of space\n");
   1100 		return (0);
   1101 	}
   1102 	for (d = 0; d < acg.cg_ndblk; d += sblock.fs_frag)
   1103 		if (isblock(&sblock, cg_blksfree(&acg, 0),
   1104 		    d >> sblock.fs_fragshift))
   1105 			goto goth;
   1106 	printf("internal error: can't find block in cyl 0\n");
   1107 	return (0);
   1108 goth:
   1109 	blkno = fragstoblks(&sblock, d);
   1110 	clrblock(&sblock, cg_blksfree(&acg, 0), blkno);
   1111 	if (sblock.fs_contigsumsize > 0)
   1112 		clrbit(cg_clustersfree(&acg, 0), blkno);
   1113 	acg.cg_cs.cs_nbfree--;
   1114 	sblock.fs_cstotal.cs_nbfree--;
   1115 	fscs_0->cs_nbfree--;
   1116 	if (mode & IFDIR) {
   1117 		acg.cg_cs.cs_ndir++;
   1118 		sblock.fs_cstotal.cs_ndir++;
   1119 		fscs_0->cs_ndir++;
   1120 	}
   1121 	if (size != sblock.fs_bsize) {
   1122 		frag = howmany(size, sblock.fs_fsize);
   1123 		fscs_0->cs_nffree += sblock.fs_frag - frag;
   1124 		sblock.fs_cstotal.cs_nffree += sblock.fs_frag - frag;
   1125 		acg.cg_cs.cs_nffree += sblock.fs_frag - frag;
   1126 		acg.cg_frsum[sblock.fs_frag - frag]++;
   1127 		for (i = frag; i < sblock.fs_frag; i++)
   1128 			setbit(cg_blksfree(&acg, 0), d + i);
   1129 	}
   1130 	/* host -> fs byte order */
   1131 	if (needswap)
   1132 		ffs_cg_swap(&acg, &acg, &sblock);
   1133 	wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, &acg);
   1134 	return (d);
   1135 }
   1136 
   1137 /*
   1138  * Allocate an inode on the disk
   1139  */
   1140 static void
   1141 iput(union dinode *ip, ino_t ino)
   1142 {
   1143 	daddr_t d;
   1144 	int c, i;
   1145 	struct ufs1_dinode *dp1;
   1146 	struct ufs2_dinode *dp2;
   1147 
   1148 	c = ino_to_cg(&sblock, ino);
   1149 	rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, &acg);
   1150 	/* fs -> host byte order */
   1151 	if (needswap)
   1152 		ffs_cg_swap(&acg, &acg, &sblock);
   1153 	if (acg.cg_magic != CG_MAGIC) {
   1154 		printf("cg 0: bad magic number\n");
   1155 		exit(31);
   1156 	}
   1157 	acg.cg_cs.cs_nifree--;
   1158 	setbit(cg_inosused(&acg, 0), ino);
   1159 	/* host -> fs byte order */
   1160 	if (needswap)
   1161 		ffs_cg_swap(&acg, &acg, &sblock);
   1162 	wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, &acg);
   1163 	sblock.fs_cstotal.cs_nifree--;
   1164 	fscs_0->cs_nifree--;
   1165 	if (ino >= sblock.fs_ipg * sblock.fs_ncg) {
   1166 		printf("fsinit: inode value out of range (%d).\n", ino);
   1167 		exit(32);
   1168 	}
   1169 	d = fsbtodb(&sblock, ino_to_fsba(&sblock, ino));
   1170 	rdfs(d, sblock.fs_bsize, (char *)iobuf);
   1171 	if (sblock.fs_magic == FS_UFS1_MAGIC) {
   1172 		dp1 = (struct ufs1_dinode *)iobuf;
   1173 		dp1 += ino_to_fsbo(&sblock, ino);
   1174 		if (needswap) {
   1175 			ffs_dinode1_swap(&ip->dp1, dp1);
   1176 			/* ffs_dinode1_swap() doesn't swap blocks addrs */
   1177 			for (i=0; i<NDADDR + NIADDR; i++)
   1178 			    dp1->di_db[i] = bswap32(ip->dp1.di_db[i]);
   1179 		} else
   1180 			*dp1 = ip->dp1;
   1181 		dp1->di_gen = arc4random() & INT32_MAX;
   1182 	} else {
   1183 		dp2 = (struct ufs2_dinode *)iobuf;
   1184 		dp2 += ino_to_fsbo(&sblock, ino);
   1185 		if (needswap) {
   1186 			ffs_dinode2_swap(&ip->dp2, dp2);
   1187 			for (i=0; i<NDADDR + NIADDR; i++)
   1188 			    dp2->di_db[i] = bswap32(ip->dp2.di_db[i]);
   1189 		} else
   1190 			*dp2 = ip->dp2;
   1191 		dp2->di_gen = arc4random() & INT32_MAX;
   1192 	}
   1193 	wtfs(d, sblock.fs_bsize, iobuf);
   1194 }
   1195 
   1196 /*
   1197  * read a block from the file system
   1198  */
   1199 void
   1200 rdfs(daddr_t bno, int size, void *bf)
   1201 {
   1202 	int n;
   1203 	off_t offset;
   1204 
   1205 #ifdef MFS
   1206 	if (mfs) {
   1207 		if (Nflag)
   1208 			memset(bf, 0, size);
   1209 		else
   1210 			memmove(bf, membase + bno * sectorsize, size);
   1211 		return;
   1212 	}
   1213 #endif
   1214 	offset = bno;
   1215 	n = pread(fsi, bf, size, offset * sectorsize);
   1216 	if (n != size) {
   1217 		printf("rdfs: read error for sector %lld: %s\n",
   1218 		    (long long)bno, strerror(errno));
   1219 		exit(34);
   1220 	}
   1221 }
   1222 
   1223 /*
   1224  * write a block to the file system
   1225  */
   1226 void
   1227 wtfs(daddr_t bno, int size, void *bf)
   1228 {
   1229 	int n;
   1230 	off_t offset;
   1231 
   1232 	if (Nflag)
   1233 		return;
   1234 #ifdef MFS
   1235 	if (mfs) {
   1236 		memmove(membase + bno * sectorsize, bf, size);
   1237 		return;
   1238 	}
   1239 #endif
   1240 	offset = bno;
   1241 	n = pwrite(fso, bf, size, offset * sectorsize);
   1242 	if (n != size) {
   1243 		printf("wtfs: write error for sector %lld: %s\n",
   1244 		    (long long)bno, strerror(errno));
   1245 		exit(36);
   1246 	}
   1247 }
   1248 
   1249 /*
   1250  * check if a block is available
   1251  */
   1252 int
   1253 isblock(struct fs *fs, unsigned char *cp, int h)
   1254 {
   1255 	unsigned char mask;
   1256 
   1257 	switch (fs->fs_fragshift) {
   1258 	case 3:
   1259 		return (cp[h] == 0xff);
   1260 	case 2:
   1261 		mask = 0x0f << ((h & 0x1) << 2);
   1262 		return ((cp[h >> 1] & mask) == mask);
   1263 	case 1:
   1264 		mask = 0x03 << ((h & 0x3) << 1);
   1265 		return ((cp[h >> 2] & mask) == mask);
   1266 	case 0:
   1267 		mask = 0x01 << (h & 0x7);
   1268 		return ((cp[h >> 3] & mask) == mask);
   1269 	default:
   1270 #ifdef STANDALONE
   1271 		printf("isblock bad fs_fragshift %d\n", fs->fs_fragshift);
   1272 #else
   1273 		fprintf(stderr, "isblock bad fs_fragshift %d\n",
   1274 		    fs->fs_fragshift);
   1275 #endif
   1276 		return (0);
   1277 	}
   1278 }
   1279 
   1280 /*
   1281  * take a block out of the map
   1282  */
   1283 void
   1284 clrblock(struct fs *fs, unsigned char *cp, int h)
   1285 {
   1286 	switch ((fs)->fs_fragshift) {
   1287 	case 3:
   1288 		cp[h] = 0;
   1289 		return;
   1290 	case 2:
   1291 		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
   1292 		return;
   1293 	case 1:
   1294 		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
   1295 		return;
   1296 	case 0:
   1297 		cp[h >> 3] &= ~(0x01 << (h & 0x7));
   1298 		return;
   1299 	default:
   1300 #ifdef STANDALONE
   1301 		printf("clrblock bad fs_fragshift %d\n", fs->fs_fragshift);
   1302 #else
   1303 		fprintf(stderr, "clrblock bad fs_fragshift %d\n",
   1304 		    fs->fs_fragshift);
   1305 #endif
   1306 		return;
   1307 	}
   1308 }
   1309 
   1310 /*
   1311  * put a block into the map
   1312  */
   1313 void
   1314 setblock(struct fs *fs, unsigned char *cp, int h)
   1315 {
   1316 	switch (fs->fs_fragshift) {
   1317 	case 3:
   1318 		cp[h] = 0xff;
   1319 		return;
   1320 	case 2:
   1321 		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
   1322 		return;
   1323 	case 1:
   1324 		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
   1325 		return;
   1326 	case 0:
   1327 		cp[h >> 3] |= (0x01 << (h & 0x7));
   1328 		return;
   1329 	default:
   1330 #ifdef STANDALONE
   1331 		printf("setblock bad fs_frag %d\n", fs->fs_fragshift);
   1332 #else
   1333 		fprintf(stderr, "setblock bad fs_fragshift %d\n",
   1334 		    fs->fs_fragshift);
   1335 #endif
   1336 		return;
   1337 	}
   1338 }
   1339 
   1340 /* copy a direntry to a buffer, in fs byte order */
   1341 static void
   1342 copy_dir(struct direct *dir, struct direct *dbuf)
   1343 {
   1344 	memcpy(dbuf, dir, DIRSIZ(Oflag == 0, dir, 0));
   1345 	if (needswap) {
   1346 		dbuf->d_ino = bswap32(dir->d_ino);
   1347 		dbuf->d_reclen = bswap16(dir->d_reclen);
   1348 		if (Oflag == 0)
   1349 			((struct odirect*)dbuf)->d_namlen =
   1350 				bswap16(((struct odirect*)dir)->d_namlen);
   1351 	}
   1352 }
   1353 
   1354 /* Determine how many digits are needed to print a given integer */
   1355 static int
   1356 count_digits(uint64_t num)
   1357 {
   1358 	int ndig;
   1359 
   1360 	for (ndig = 1; num > 9; num /= 10, ndig++);
   1361 
   1362 	return (ndig);
   1363 }
   1364 
   1365 static int
   1366 ilog2(int val)
   1367 {
   1368 	u_int n;
   1369 
   1370 	for (n = 0; n < sizeof(n) * CHAR_BIT; n++)
   1371 		if (1 << n == val)
   1372 			return (n);
   1373 	errx(1, "ilog2: %d is not a power of 2\n", val);
   1374 }
   1375 
   1376 static void
   1377 zap_old_sblock(int sblkoff)
   1378 {
   1379 	static int cg0_data;
   1380 	uint32_t oldfs[SBLOCKSIZE / 4];
   1381 	static const struct fsm {
   1382 		uint32_t	offset;
   1383 		uint32_t	magic;
   1384 		uint32_t	mask;
   1385 	} fs_magics[] = {
   1386 		{offsetof(struct fs, fs_magic)/4, FS_UFS1_MAGIC, ~0u},
   1387 		{offsetof(struct fs, fs_magic)/4, FS_UFS2_MAGIC, ~0u},
   1388 		{0, 0x70162, ~0u},		/* LFS_MAGIC */
   1389 		{14, 0xef53, 0xffff},		/* EXT2FS (little) */
   1390 		{14, 0xef530000, 0xffff0000},	/* EXT2FS (big) */
   1391 		{~0u},
   1392 	};
   1393 	const struct fsm *fsm;
   1394 
   1395 	if (Nflag)
   1396 		return;
   1397 
   1398 	if (sblkoff == 0)	/* Why did UFS2 add support for this?  sigh. */
   1399 		return;
   1400 
   1401 	if (cg0_data == 0)
   1402 		/* For FFSv1 this could include all the inodes. */
   1403 		cg0_data = cgsblock(&sblock, 0) * sblock.fs_fsize + iobufsize;
   1404 
   1405 	/* Ignore anything that is beyond our filesystem */
   1406 	if ((sblkoff + SBLOCKSIZE)/sectorsize >= fssize)
   1407 		return;
   1408 	/* Zero anything inside our filesystem... */
   1409 	if (sblkoff >= sblock.fs_sblockloc) {
   1410 		/* ...unless we will write that area anyway */
   1411 		if (sblkoff >= cg0_data)
   1412 			wtfs(sblkoff / sectorsize,
   1413 			    roundup(sizeof sblock, sectorsize), iobuf);
   1414 		return;
   1415 	}
   1416 
   1417 	/* The sector might contain boot code, so we must validate it */
   1418 	rdfs(sblkoff/sectorsize, sizeof oldfs, &oldfs);
   1419 	for (fsm = fs_magics; ; fsm++) {
   1420 		uint32_t v;
   1421 		if (fsm->mask == 0)
   1422 			return;
   1423 		v = oldfs[fsm->offset];
   1424 		if ((v & fsm->mask) == fsm->magic ||
   1425 		    (bswap32(v) & fsm->mask) == fsm->magic)
   1426 			break;
   1427 	}
   1428 
   1429 	/* Just zap the magic number */
   1430 	oldfs[fsm->offset] = 0;
   1431 	wtfs(sblkoff/sectorsize, sizeof oldfs, &oldfs);
   1432 }
   1433 
   1434 
   1435 #ifdef MFS
   1436 /*
   1437  * XXX!
   1438  * Attempt to guess how much more space is available for process data.  The
   1439  * heuristic we use is
   1440  *
   1441  *	max_data_limit - (sbrk(0) - etext) - 128kB
   1442  *
   1443  * etext approximates that start address of the data segment, and the 128kB
   1444  * allows some slop for both segment gap between text and data, and for other
   1445  * (libc) malloc usage.
   1446  */
   1447 static void
   1448 calc_memfree(void)
   1449 {
   1450 	extern char etext;
   1451 	struct rlimit rlp;
   1452 	u_long base;
   1453 
   1454 	base = (u_long)sbrk(0) - (u_long)&etext;
   1455 	if (getrlimit(RLIMIT_DATA, &rlp) < 0)
   1456 		perror("getrlimit");
   1457 	rlp.rlim_cur = rlp.rlim_max;
   1458 	if (setrlimit(RLIMIT_DATA, &rlp) < 0)
   1459 		perror("setrlimit");
   1460 	memleft = rlp.rlim_max - base - (128 * 1024);
   1461 }
   1462 
   1463 /*
   1464  * Internal version of malloc that trims the requested size if not enough
   1465  * memory is available.
   1466  */
   1467 static void *
   1468 mkfs_malloc(size_t size)
   1469 {
   1470 	u_long pgsz;
   1471 
   1472 	if (size == 0)
   1473 		return (NULL);
   1474 	if (memleft == 0)
   1475 		calc_memfree();
   1476 
   1477 	pgsz = getpagesize() - 1;
   1478 	size = (size + pgsz) &~ pgsz;
   1479 	if (size > memleft)
   1480 		size = memleft;
   1481 	memleft -= size;
   1482 	return (mmap(0, size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
   1483 	    -1, 0));
   1484 }
   1485 #endif	/* MFS */
   1486