Home | History | Annotate | Line # | Download | only in newfs
mkfs.c revision 1.87.2.1.4.1
      1 /*	$NetBSD: mkfs.c,v 1.87.2.1.4.1 2005/11/06 00:43:15 riz 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.87.2.1.4.1 2005/11/06 00:43:15 riz 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;			/* size to end of 2nd inode block */
    158 int iobuf_memsize;		/* Actual buffer size */
    159 
    160 int	fsi, fso;
    161 
    162 void
    163 mkfs(struct partition *pp, const char *fsys, int fi, int fo,
    164     mode_t mfsmode, uid_t mfsuid, gid_t mfsgid)
    165 {
    166 	uint fragsperinodeblk, ncg;
    167 	uint cgzero;
    168 	uint64_t inodeblks, cgall;
    169 	int32_t cylno, i, csfrags;
    170 	struct timeval tv;
    171 	long long sizepb;
    172 	int nprintcols, printcolwidth;
    173 
    174 #ifndef STANDALONE
    175 	gettimeofday(&tv, NULL);
    176 #endif
    177 #ifdef MFS
    178 	if (mfs && !Nflag) {
    179 		calc_memfree();
    180 		if (fssize * sectorsize > memleft)
    181 			fssize = memleft / sectorsize;
    182 		if ((membase = mkfs_malloc(fssize * sectorsize)) == 0)
    183 			exit(12);
    184 	}
    185 #endif
    186 	fsi = fi;
    187 	fso = fo;
    188 	if (Oflag == 0) {
    189 		sblock.fs_old_inodefmt = FS_42INODEFMT;
    190 		sblock.fs_maxsymlinklen = 0;
    191 		sblock.fs_old_flags = 0;
    192 	} else {
    193 		sblock.fs_old_inodefmt = FS_44INODEFMT;
    194 		sblock.fs_maxsymlinklen = (Oflag == 1 ? MAXSYMLINKLEN_UFS1 :
    195 		    MAXSYMLINKLEN_UFS2);
    196 		sblock.fs_old_flags = FS_FLAGS_UPDATED;
    197 		if (isappleufs)
    198 			sblock.fs_old_flags = 0;
    199 		sblock.fs_flags = 0;
    200 	}
    201 
    202 	/*
    203 	 * collect and verify the filesystem density info
    204 	 */
    205 	sblock.fs_avgfilesize = avgfilesize;
    206 	sblock.fs_avgfpdir = avgfpdir;
    207 	if (sblock.fs_avgfilesize <= 0) {
    208 		printf("illegal expected average file size %d\n",
    209 		    sblock.fs_avgfilesize);
    210 		exit(14);
    211 	}
    212 	if (sblock.fs_avgfpdir <= 0) {
    213 		printf("illegal expected number of files per directory %d\n",
    214 		    sblock.fs_avgfpdir);
    215 		exit(15);
    216 	}
    217 	/*
    218 	 * collect and verify the block and fragment sizes
    219 	 */
    220 	sblock.fs_bsize = bsize;
    221 	sblock.fs_fsize = fsize;
    222 	if (!POWEROF2(sblock.fs_bsize)) {
    223 		printf("block size must be a power of 2, not %d\n",
    224 		    sblock.fs_bsize);
    225 		exit(16);
    226 	}
    227 	if (!POWEROF2(sblock.fs_fsize)) {
    228 		printf("fragment size must be a power of 2, not %d\n",
    229 		    sblock.fs_fsize);
    230 		exit(17);
    231 	}
    232 	if (sblock.fs_fsize < sectorsize) {
    233 		printf("fragment size %d is too small, minimum is %d\n",
    234 		    sblock.fs_fsize, sectorsize);
    235 		exit(18);
    236 	}
    237 	if (sblock.fs_bsize < MINBSIZE) {
    238 		printf("block size %d is too small, minimum is %d\n",
    239 		    sblock.fs_bsize, MINBSIZE);
    240 		exit(19);
    241 	}
    242 	if (sblock.fs_bsize > MAXBSIZE) {
    243 		printf("block size %d is too large, maximum is %d\n",
    244 		    sblock.fs_bsize, MAXBSIZE);
    245 		exit(19);
    246 	}
    247 	if (sblock.fs_bsize < sblock.fs_fsize) {
    248 		printf("block size (%d) cannot be smaller than fragment size (%d)\n",
    249 		    sblock.fs_bsize, sblock.fs_fsize);
    250 		exit(20);
    251 	}
    252 
    253 	if (maxbsize < bsize || !POWEROF2(maxbsize)) {
    254 		sblock.fs_maxbsize = sblock.fs_bsize;
    255 	} else if (sblock.fs_maxbsize > FS_MAXCONTIG * sblock.fs_bsize) {
    256 		sblock.fs_maxbsize = FS_MAXCONTIG * sblock.fs_bsize;
    257 	} else {
    258 		sblock.fs_maxbsize = maxbsize;
    259 	}
    260 	sblock.fs_maxcontig = maxcontig;
    261 	if (sblock.fs_maxcontig < sblock.fs_maxbsize / sblock.fs_bsize) {
    262 		sblock.fs_maxcontig = sblock.fs_maxbsize / sblock.fs_bsize;
    263 		printf("Maxcontig raised to %d\n", sblock.fs_maxbsize);
    264 	}
    265 	if (sblock.fs_maxcontig > 1)
    266 		sblock.fs_contigsumsize = MIN(sblock.fs_maxcontig,FS_MAXCONTIG);
    267 
    268 	sblock.fs_bmask = ~(sblock.fs_bsize - 1);
    269 	sblock.fs_fmask = ~(sblock.fs_fsize - 1);
    270 	sblock.fs_qbmask = ~sblock.fs_bmask;
    271 	sblock.fs_qfmask = ~sblock.fs_fmask;
    272 	for (sblock.fs_bshift = 0, i = sblock.fs_bsize; i > 1; i >>= 1)
    273 		sblock.fs_bshift++;
    274 	for (sblock.fs_fshift = 0, i = sblock.fs_fsize; i > 1; i >>= 1)
    275 		sblock.fs_fshift++;
    276 	sblock.fs_frag = numfrags(&sblock, sblock.fs_bsize);
    277 	for (sblock.fs_fragshift = 0, i = sblock.fs_frag; i > 1; i >>= 1)
    278 		sblock.fs_fragshift++;
    279 	if (sblock.fs_frag > MAXFRAG) {
    280 		printf("fragment size %d is too small, "
    281 			"minimum with block size %d is %d\n",
    282 		    sblock.fs_fsize, sblock.fs_bsize,
    283 		    sblock.fs_bsize / MAXFRAG);
    284 		exit(21);
    285 	}
    286 	sblock.fs_fsbtodb = ilog2(sblock.fs_fsize / sectorsize);
    287 	sblock.fs_size = dbtofsb(&sblock, fssize);
    288 	if (Oflag <= 1) {
    289 		if (sblock.fs_size >= 1ull << 31) {
    290 			printf("Too many fragments (0x%" PRIx64
    291 			    ") for a UFS1 filesystem\n", sblock.fs_size);
    292 			exit(22);
    293 		}
    294 		sblock.fs_magic = FS_UFS1_MAGIC;
    295 		sblock.fs_sblockloc = SBLOCK_UFS1;
    296 		sblock.fs_nindir = sblock.fs_bsize / sizeof(int32_t);
    297 		sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs1_dinode);
    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 	}
    316 
    317 	sblock.fs_sblkno =
    318 	    roundup(howmany(sblock.fs_sblockloc + SBLOCKSIZE, sblock.fs_fsize),
    319 		sblock.fs_frag);
    320 	sblock.fs_cblkno = (daddr_t)(sblock.fs_sblkno +
    321 	    roundup(howmany(SBLOCKSIZE, sblock.fs_fsize), sblock.fs_frag));
    322 	sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag;
    323 	sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1;
    324 	for (sizepb = sblock.fs_bsize, i = 0; i < NIADDR; i++) {
    325 		sizepb *= NINDIR(&sblock);
    326 		sblock.fs_maxfilesize += sizepb;
    327 	}
    328 
    329 	/*
    330 	 * Calculate the number of blocks to put into each cylinder group.
    331 	 *
    332 	 * The cylinder group size is limited because the data structure
    333 	 * must fit into a single block.
    334 	 * We try to have as few cylinder groups as possible, with a proviso
    335 	 * that we create at least MINCYLGRPS (==4) except for small
    336 	 * filesystems.
    337 	 *
    338 	 * This algorithm works out how many blocks of inodes would be
    339 	 * needed to fill the entire volume at the specified density.
    340 	 * It then looks at how big the 'cylinder block' would have to
    341 	 * be and, assuming that it is linearly related to the number
    342 	 * of inodes and blocks how many cylinder groups are needed to
    343 	 * keep the cylinder block below the filesystem block size.
    344 	 *
    345 	 * The cylinder groups are then all created with the average size.
    346 	 *
    347 	 * Space taken by the red tape on cylinder groups other than the
    348 	 * first is ignored.
    349 	 */
    350 
    351 	/* There must be space for 1 inode block and 2 data blocks */
    352 	if (sblock.fs_size < sblock.fs_iblkno + 3 * sblock.fs_frag) {
    353 		printf("Filesystem size %lld < minimum size of %d\n",
    354 		    (long long)sblock.fs_size, sblock.fs_iblkno + 3 * sblock.fs_frag);
    355 		exit(23);
    356 	}
    357 	if (num_inodes != 0)
    358 		inodeblks = howmany(num_inodes, INOPB(&sblock));
    359 	else {
    360 		/*
    361 		 * Calculate 'per inode block' so we can allocate less than
    362 		 * 1 fragment per inode - useful for /dev.
    363 		 */
    364 		fragsperinodeblk = MAX(numfrags(&sblock,
    365 					density * INOPB(&sblock)), 1);
    366 		inodeblks = (sblock.fs_size - sblock.fs_iblkno) /
    367 			(sblock.fs_frag + fragsperinodeblk);
    368 	}
    369 	if (inodeblks == 0)
    370 		inodeblks = 1;
    371 	/* Ensure that there are at least 2 data blocks (or we fail below) */
    372 	if (inodeblks > (sblock.fs_size - sblock.fs_iblkno)/sblock.fs_frag - 2)
    373 		inodeblks = (sblock.fs_size-sblock.fs_iblkno)/sblock.fs_frag-2;
    374 	/* Even UFS2 limits number of inodes to 2^31 (fs_ipg is int32_t) */
    375 	if (inodeblks * INOPB(&sblock) >= 1ull << 31)
    376 		inodeblks = ((1ull << 31) - NBBY) / INOPB(&sblock);
    377 	/*
    378 	 * See what would happen if we tried to use 1 cylinder group.
    379 	 * Assume space linear, so work out number of cylinder groups needed.
    380 	 * Subtract one from the allowed size to compensate for rounding
    381 	 * a number of bits up to a complete byte.
    382 	 */
    383 	cgzero = CGSIZE_IF(&sblock, 0, 0);
    384 	cgall = CGSIZE_IF(&sblock, inodeblks * INOPB(&sblock), sblock.fs_size);
    385 	ncg = howmany(cgall - cgzero, sblock.fs_bsize - cgzero - 1);
    386 	if (ncg < MINCYLGRPS) {
    387 		/*
    388 		 * We would like to allocate MINCLYGRPS cylinder groups,
    389 		 * but for small file sytems (especially ones with a lot
    390 		 * of inodes) this is not desirable (or possible).
    391 		 */
    392 		i = sblock.fs_size / 2 / (sblock.fs_iblkno +
    393 						inodeblks * sblock.fs_frag);
    394 		if (i > ncg)
    395 			ncg = i;
    396 		if (ncg > MINCYLGRPS)
    397 			ncg = MINCYLGRPS;
    398 		if (ncg > inodeblks)
    399 			ncg = inodeblks;
    400 	}
    401 	/*
    402 	 * Put an equal number of blocks in each cylinder group.
    403 	 * Round up so we don't have more fragments in the last CG than
    404 	 * the earlier ones (does that matter?), but kill a block if the
    405 	 * CGSIZE becomes too big (only happens if there are a lot of CGs).
    406 	 */
    407 	sblock.fs_fpg = roundup(howmany(sblock.fs_size, ncg), sblock.fs_frag);
    408 	i = CGSIZE_IF(&sblock, inodeblks * INOPB(&sblock) / ncg, sblock.fs_fpg);
    409 	if (i > sblock.fs_bsize)
    410 		sblock.fs_fpg -= (i - sblock.fs_bsize) * NBBY;
    411 	/* ... and recalculate how many cylinder groups we now need */
    412 	ncg = howmany(sblock.fs_size, sblock.fs_fpg);
    413 	inodeblks /= ncg;
    414 	if (inodeblks == 0)
    415 		inodeblks = 1;
    416 	sblock.fs_ipg = inodeblks * INOPB(&sblock);
    417 	/* Sanity check on our sums... */
    418 	if (CGSIZE(&sblock) > sblock.fs_bsize) {
    419 		printf("CGSIZE miscalculated %d > %d\n",
    420 		    (int)CGSIZE(&sblock), sblock.fs_bsize);
    421 		exit(24);
    422 	}
    423 	/* Check that the last cylinder group has enough space for the inodes */
    424 	i = sblock.fs_size - sblock.fs_fpg * (ncg - 1ull);
    425 	if (i < sblock.fs_iblkno + inodeblks * sblock.fs_frag) {
    426 		/*
    427 		 * Since we make all the cylinder groups the same size, the
    428 		 * last will only be small if there are a large number of
    429 		 * cylinder groups. If we pull even a fragment from each
    430 		 * of the other groups then the last CG will be overfull.
    431 		 * So we just kill the last CG.
    432 		 */
    433 		ncg--;
    434 		sblock.fs_size -= i;
    435 	}
    436 	sblock.fs_ncg = ncg;
    437 
    438 	sblock.fs_cgsize = fragroundup(&sblock, CGSIZE(&sblock));
    439 	sblock.fs_dblkno = sblock.fs_iblkno + sblock.fs_ipg / INOPF(&sblock);
    440 	if (Oflag <= 1) {
    441 		sblock.fs_old_spc = sblock.fs_fpg * sblock.fs_old_nspf;
    442 		sblock.fs_old_nsect = sblock.fs_old_spc;
    443 		sblock.fs_old_npsect = sblock.fs_old_spc;
    444 		sblock.fs_old_ncyl = sblock.fs_ncg;
    445 	}
    446 
    447 	/*
    448 	 * Cylinder group summary information for each cylinder is written
    449 	 * into the first cylinder group.
    450 	 * Write this fragment by fragment, but doing the first CG last
    451 	 * (after we've taken stuff off for the structure itself and the
    452 	 * root directory.
    453 	 */
    454 	sblock.fs_csaddr = cgdmin(&sblock, 0);
    455 	sblock.fs_cssize =
    456 	    fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum));
    457 	if (512 % sizeof *fscs_0)
    458 		errx(1, "cylinder group summary doesn't fit in sectors");
    459 	fscs_0 = mmap(0, 2 * sblock.fs_fsize, PROT_READ|PROT_WRITE,
    460 			MAP_ANON|MAP_PRIVATE, -1, 0);
    461 	if (fscs_0 == NULL)
    462 		exit(39);
    463 	memset(fscs_0, 0, 2 * sblock.fs_fsize);
    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 	iobuf_memsize = iobufsize;
    550 	if (!mfs && sblock.fs_magic == FS_UFS1_MAGIC) {
    551 		/* A larger buffer so we can write multiple inode blks */
    552 		iobuf_memsize += 14 * sblock.fs_bsize;
    553 	}
    554 	for (;;) {
    555 		iobuf = mmap(0, iobuf_memsize, PROT_READ|PROT_WRITE,
    556 				MAP_ANON|MAP_PRIVATE, -1, 0);
    557 		if (iobuf != NULL)
    558 			break;
    559 		if (iobuf_memsize != iobufsize) {
    560 			/* Try again with the smaller size */
    561 			iobuf_memsize = iobufsize;
    562 			continue;
    563 		}
    564 		printf("Cannot allocate I/O buffer\n");
    565 		exit(38);
    566 	}
    567 	memset(iobuf, 0, iobuf_memsize);
    568 
    569 	/*
    570 	 * We now start writing to the filesystem
    571 	 */
    572 
    573 	/*
    574 	 * Validate the given file system size.
    575 	 * Verify that its last block can actually be accessed.
    576 	 * Convert to file system fragment sized units.
    577 	 */
    578 	if (fssize <= 0) {
    579 		printf("preposterous size %lld\n", (long long)fssize);
    580 		exit(13);
    581 	}
    582 	wtfs(fssize - 1, sectorsize, iobuf);
    583 
    584 	/*
    585 	 * Ensure there is nothing that looks like a filesystem
    586 	 * superbock anywhere other than where ours will be.
    587 	 * If fsck finds the wrong one all hell breaks loose!
    588 	 */
    589 	for (i = 0; ; i++) {
    590 		static const int sblocklist[] = SBLOCKSEARCH;
    591 		int sblkoff = sblocklist[i];
    592 		int sz;
    593 		if (sblkoff == -1)
    594 			break;
    595 		/* Remove main superblock */
    596 		zap_old_sblock(sblkoff);
    597 		/* and all possible locations for the first alternate */
    598 		sblkoff += SBLOCKSIZE;
    599 		for (sz = SBLOCKSIZE; sz <= 0x10000; sz <<= 1)
    600 			zap_old_sblock(roundup(sblkoff, sz));
    601 	}
    602 
    603 	if (isappleufs) {
    604 		struct appleufslabel appleufs;
    605 		ffs_appleufs_set(&appleufs, appleufs_volname, tv.tv_sec, 0);
    606 		wtfs(APPLEUFS_LABEL_OFFSET/sectorsize, APPLEUFS_LABEL_SIZE,
    607 		    &appleufs);
    608 	} else {
    609 		struct appleufslabel appleufs;
    610 		/* Look for and zap any existing valid apple ufs labels */
    611 		rdfs(APPLEUFS_LABEL_OFFSET/sectorsize, APPLEUFS_LABEL_SIZE,
    612 		    &appleufs);
    613 		if (ffs_appleufs_validate(fsys, &appleufs, NULL) == 0) {
    614 			memset(&appleufs, 0, sizeof(appleufs));
    615 			wtfs(APPLEUFS_LABEL_OFFSET/sectorsize, APPLEUFS_LABEL_SIZE,
    616 			    &appleufs);
    617 		}
    618 	}
    619 
    620 	/*
    621 	 * Make a copy of the superblock into the buffer that we will be
    622 	 * writing out in each cylinder group.
    623 	 */
    624 	memcpy(iobuf, &sblock, sizeof sblock);
    625 	if (needswap)
    626 		ffs_sb_swap(&sblock, (struct fs *)iobuf);
    627 	if ((sblock.fs_old_flags & FS_FLAGS_UPDATED) == 0)
    628 		memset(iobuf + offsetof(struct fs, fs_old_postbl_start),
    629 		    0xff, 256);
    630 
    631 	if (!mfs || Nflag)
    632 		printf("super-block backups (for fsck -b #) at:");
    633 	for (cylno = 0; cylno < sblock.fs_ncg; cylno++) {
    634 		initcg(cylno, &tv);
    635 		if (mfs && !Nflag)
    636 			continue;
    637 		if (cylno % nprintcols == 0)
    638 			printf("\n");
    639 		printf(" %*lld,", printcolwidth,
    640 			(long long)fsbtodb(&sblock, cgsblock(&sblock, cylno)));
    641 		fflush(stdout);
    642 	}
    643 	if (!mfs || Nflag)
    644 		printf("\n");
    645 	if (Nflag)
    646 		exit(0);
    647 
    648 	/*
    649 	 * Now construct the initial file system,
    650 	 */
    651 	if (fsinit(&tv, mfsmode, mfsuid, mfsgid) == 0 && mfs)
    652 		errx(1, "Error making filesystem");
    653 	sblock.fs_time = tv.tv_sec;
    654 	if (Oflag <= 1) {
    655 		sblock.fs_old_cstotal.cs_ndir = sblock.fs_cstotal.cs_ndir;
    656 		sblock.fs_old_cstotal.cs_nbfree = sblock.fs_cstotal.cs_nbfree;
    657 		sblock.fs_old_cstotal.cs_nifree = sblock.fs_cstotal.cs_nifree;
    658 		sblock.fs_old_cstotal.cs_nffree = sblock.fs_cstotal.cs_nffree;
    659 	}
    660 	/*
    661 	 * Write out the super-block and zeros until the first cg info
    662 	 */
    663 	i = cgsblock(&sblock, 0) * sblock.fs_fsize - sblock.fs_sblockloc,
    664 	memset(iobuf, 0, i);
    665 	memcpy(iobuf, &sblock, sizeof sblock);
    666 	if (needswap)
    667 		ffs_sb_swap(&sblock, (struct fs *)iobuf);
    668 	if ((sblock.fs_old_flags & FS_FLAGS_UPDATED) == 0)
    669 		memset(iobuf + offsetof(struct fs, fs_old_postbl_start),
    670 		    0xff, 256);
    671 	wtfs(sblock.fs_sblockloc / sectorsize, i, iobuf);
    672 
    673 	/* Write out first and last cylinder summary sectors */
    674 	if (needswap)
    675 		ffs_csum_swap(fscs_0, fscs_0, sblock.fs_fsize);
    676 	wtfs(fsbtodb(&sblock, sblock.fs_csaddr), sblock.fs_fsize, fscs_0);
    677 
    678 	if (fscs_next > fscs_reset) {
    679 		if (needswap)
    680 			ffs_csum_swap(fscs_reset, fscs_reset, sblock.fs_fsize);
    681 		fs_csaddr++;
    682 		wtfs(fsbtodb(&sblock, fs_csaddr), sblock.fs_fsize, fscs_reset);
    683 	}
    684 
    685 	/* mfs doesn't need these permanently allocated */
    686 	munmap(iobuf, iobuf_memsize);
    687 	munmap(fscs_0, 2 * sblock.fs_fsize);
    688 
    689 	/*
    690 	 * Update information about this partion in pack
    691 	 * label, to that it may be updated on disk.
    692 	 */
    693 	if (pp == NULL)
    694 		return;
    695 	if (isappleufs)
    696 		pp->p_fstype = FS_APPLEUFS;
    697 	else
    698 		pp->p_fstype = FS_BSDFFS;
    699 	pp->p_fsize = sblock.fs_fsize;
    700 	pp->p_frag = sblock.fs_frag;
    701 	pp->p_cpg = sblock.fs_fpg;
    702 }
    703 
    704 /*
    705  * Initialize a cylinder group.
    706  */
    707 void
    708 initcg(int cylno, const struct timeval *tv)
    709 {
    710 	daddr_t cbase, dmax;
    711 	int32_t i, d, dlower, dupper, blkno;
    712 	struct ufs1_dinode *dp1;
    713 	struct ufs2_dinode *dp2;
    714 	int start;
    715 
    716 	/*
    717 	 * Determine block bounds for cylinder group.
    718 	 * Allow space for super block summary information in first
    719 	 * cylinder group.
    720 	 */
    721 	cbase = cgbase(&sblock, cylno);
    722 	dmax = cbase + sblock.fs_fpg;
    723 	if (dmax > sblock.fs_size)
    724 		dmax = sblock.fs_size;
    725 	dlower = cgsblock(&sblock, cylno) - cbase;
    726 	dupper = cgdmin(&sblock, cylno) - cbase;
    727 	if (cylno == 0) {
    728 		dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
    729 		if (dupper >= cgstart(&sblock, cylno + 1)) {
    730 			printf("\rToo many cylinder groups to fit summary "
    731 				"information into first cylinder group\n");
    732 			exit(40);
    733 		}
    734 	}
    735 	memset(&acg, 0, sblock.fs_cgsize);
    736 	acg.cg_magic = CG_MAGIC;
    737 	acg.cg_cgx = cylno;
    738 	acg.cg_ndblk = dmax - cbase;
    739 	if (sblock.fs_contigsumsize > 0)
    740 		acg.cg_nclusterblks = acg.cg_ndblk >> sblock.fs_fragshift;
    741 	start = &acg.cg_space[0] - (u_char *)(&acg.cg_firstfield);
    742 	if (Oflag == 2) {
    743 		acg.cg_time = tv->tv_sec;
    744 		acg.cg_niblk = sblock.fs_ipg;
    745 		acg.cg_initediblk = sblock.fs_ipg < 2 * INOPB(&sblock) ?
    746 		    sblock.fs_ipg : 2 * INOPB(&sblock);
    747 		acg.cg_iusedoff = start;
    748 	} else {
    749 		acg.cg_old_ncyl = sblock.fs_old_cpg;
    750 		if ((sblock.fs_old_flags & FS_FLAGS_UPDATED) == 0 &&
    751 		    (cylno == sblock.fs_ncg - 1))
    752 			acg.cg_old_ncyl =
    753 			    sblock.fs_old_ncyl % sblock.fs_old_cpg;
    754 		acg.cg_old_time = tv->tv_sec;
    755 		acg.cg_old_niblk = sblock.fs_ipg;
    756 		acg.cg_old_btotoff = start;
    757 		acg.cg_old_boff = acg.cg_old_btotoff +
    758 		    sblock.fs_old_cpg * sizeof(int32_t);
    759 		acg.cg_iusedoff = acg.cg_old_boff +
    760 		    sblock.fs_old_cpg * sizeof(u_int16_t);
    761 	}
    762 	acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, CHAR_BIT);
    763 	if (sblock.fs_contigsumsize <= 0) {
    764 		acg.cg_nextfreeoff = acg.cg_freeoff +
    765 		   howmany(sblock.fs_fpg, CHAR_BIT);
    766 	} else {
    767 		acg.cg_clustersumoff = acg.cg_freeoff +
    768 		    howmany(sblock.fs_fpg, CHAR_BIT) - sizeof(int32_t);
    769 		if (isappleufs) {
    770 			/* Apple PR2216969 gives rationale for this change.
    771 			 * I believe they were mistaken, but we need to
    772 			 * duplicate it for compatibility.  -- dbj (at) NetBSD.org
    773 			 */
    774 			acg.cg_clustersumoff += sizeof(int32_t);
    775 		}
    776 		acg.cg_clustersumoff =
    777 		    roundup(acg.cg_clustersumoff, sizeof(int32_t));
    778 		acg.cg_clusteroff = acg.cg_clustersumoff +
    779 		    (sblock.fs_contigsumsize + 1) * sizeof(int32_t);
    780 		acg.cg_nextfreeoff = acg.cg_clusteroff +
    781 		    howmany(fragstoblks(&sblock, sblock.fs_fpg), CHAR_BIT);
    782 	}
    783 	if (acg.cg_nextfreeoff > sblock.fs_cgsize) {
    784 		printf("Panic: cylinder group too big\n");
    785 		exit(37);
    786 	}
    787 	acg.cg_cs.cs_nifree += sblock.fs_ipg;
    788 	if (cylno == 0)
    789 		for (i = 0; i < ROOTINO; i++) {
    790 			setbit(cg_inosused(&acg, 0), i);
    791 			acg.cg_cs.cs_nifree--;
    792 		}
    793 	if (cylno > 0) {
    794 		/*
    795 		 * In cylno 0, beginning space is reserved
    796 		 * for boot and super blocks.
    797 		 */
    798 		for (d = 0, blkno = 0; d < dlower;) {
    799 			setblock(&sblock, cg_blksfree(&acg, 0), blkno);
    800 			if (sblock.fs_contigsumsize > 0)
    801 				setbit(cg_clustersfree(&acg, 0), blkno);
    802 			acg.cg_cs.cs_nbfree++;
    803 			if (Oflag <= 1) {
    804 				int cn = old_cbtocylno(&sblock, d);
    805 				old_cg_blktot(&acg, 0)[cn]++;
    806 				old_cg_blks(&sblock, &acg,
    807 				    cn, 0)[old_cbtorpos(&sblock, d)]++;
    808 			}
    809 			d += sblock.fs_frag;
    810 			blkno++;
    811 		}
    812 	}
    813 	if ((i = (dupper & (sblock.fs_frag - 1))) != 0) {
    814 		acg.cg_frsum[sblock.fs_frag - i]++;
    815 		for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) {
    816 			setbit(cg_blksfree(&acg, 0), dupper);
    817 			acg.cg_cs.cs_nffree++;
    818 		}
    819 	}
    820 	for (d = dupper, blkno = dupper >> sblock.fs_fragshift;
    821 	     d + sblock.fs_frag <= acg.cg_ndblk; ) {
    822 		setblock(&sblock, cg_blksfree(&acg, 0), blkno);
    823 		if (sblock.fs_contigsumsize > 0)
    824 			setbit(cg_clustersfree(&acg, 0), blkno);
    825 		acg.cg_cs.cs_nbfree++;
    826 		if (Oflag <= 1) {
    827 			int cn = old_cbtocylno(&sblock, d);
    828 			old_cg_blktot(&acg, 0)[cn]++;
    829 			old_cg_blks(&sblock, &acg,
    830 			    cn, 0)[old_cbtorpos(&sblock, d)]++;
    831 		}
    832 		d += sblock.fs_frag;
    833 		blkno++;
    834 	}
    835 	if (d < acg.cg_ndblk) {
    836 		acg.cg_frsum[acg.cg_ndblk - d]++;
    837 		for (; d < acg.cg_ndblk; d++) {
    838 			setbit(cg_blksfree(&acg, 0), d);
    839 			acg.cg_cs.cs_nffree++;
    840 		}
    841 	}
    842 	if (sblock.fs_contigsumsize > 0) {
    843 		int32_t *sump = cg_clustersum(&acg, 0);
    844 		u_char *mapp = cg_clustersfree(&acg, 0);
    845 		int map = *mapp++;
    846 		int bit = 1;
    847 		int run = 0;
    848 
    849 		for (i = 0; i < acg.cg_nclusterblks; i++) {
    850 			if ((map & bit) != 0) {
    851 				run++;
    852 			} else if (run != 0) {
    853 				if (run > sblock.fs_contigsumsize)
    854 					run = sblock.fs_contigsumsize;
    855 				sump[run]++;
    856 				run = 0;
    857 			}
    858 			if ((i & (CHAR_BIT - 1)) != (CHAR_BIT - 1)) {
    859 				bit <<= 1;
    860 			} else {
    861 				map = *mapp++;
    862 				bit = 1;
    863 			}
    864 		}
    865 		if (run != 0) {
    866 			if (run > sblock.fs_contigsumsize)
    867 				run = sblock.fs_contigsumsize;
    868 			sump[run]++;
    869 		}
    870 	}
    871 	*fscs_next++ = acg.cg_cs;
    872 	if (fscs_next == fscs_end) {
    873 		/* write block of cylinder group summary info into cyl 0 */
    874 		if (needswap)
    875 			ffs_csum_swap(fscs_reset, fscs_reset, sblock.fs_fsize);
    876 		fs_csaddr++;
    877 		wtfs(fsbtodb(&sblock, fs_csaddr), sblock.fs_fsize, fscs_reset);
    878 		fscs_next = fscs_reset;
    879 		memset(fscs_next, 0, sblock.fs_fsize);
    880 	}
    881 	/*
    882 	 * Write out the duplicate super block, the cylinder group map
    883 	 * and two blocks worth of inodes in a single write.
    884 	 */
    885 	start = sblock.fs_bsize > SBLOCKSIZE ? sblock.fs_bsize : SBLOCKSIZE;
    886 	memcpy(&iobuf[start], &acg, sblock.fs_cgsize);
    887 	if (needswap)
    888 		ffs_cg_swap(&acg, (struct cg*)&iobuf[start], &sblock);
    889 	start += sblock.fs_bsize;
    890 	dp1 = (struct ufs1_dinode *)(&iobuf[start]);
    891 	dp2 = (struct ufs2_dinode *)(&iobuf[start]);
    892 	for (i = MIN(sblock.fs_ipg, 2) * INOPB(&sblock); i != 0; i--) {
    893 		if (sblock.fs_magic == FS_UFS1_MAGIC) {
    894 			/* No need to swap, it'll stay random */
    895 			dp1->di_gen = arc4random() & INT32_MAX;
    896 			dp1++;
    897 		} else {
    898 			dp2->di_gen = arc4random() & INT32_MAX;
    899 			dp2++;
    900 		}
    901 	}
    902 	wtfs(fsbtodb(&sblock, cgsblock(&sblock, cylno)), iobufsize, iobuf);
    903 	/*
    904 	 * For the old file system, we have to initialize all the inodes.
    905 	 */
    906 	if (sblock.fs_magic != FS_UFS1_MAGIC)
    907 		return;
    908 
    909 	/* Write 'd' (usually 16 * fs_frag) file-system fragments at once */
    910 	d = (iobuf_memsize - start) / sblock.fs_bsize * sblock.fs_frag;
    911 	dupper = sblock.fs_ipg / INOPF(&sblock);
    912 	for (i = 2 * sblock.fs_frag; i < dupper; i += d) {
    913 		if (d > dupper - i)
    914 			d = dupper - i;
    915 		dp1 = (struct ufs1_dinode *)(&iobuf[start]);
    916 		do
    917 			dp1->di_gen = arc4random() & INT32_MAX;
    918 		while ((char *)++dp1 < &iobuf[iobuf_memsize]);
    919 		wtfs(fsbtodb(&sblock, cgimin(&sblock, cylno) + i),
    920 		    d * sblock.fs_bsize / sblock.fs_frag, &iobuf[start]);
    921 	}
    922 }
    923 
    924 /*
    925  * initialize the file system
    926  */
    927 
    928 #ifdef LOSTDIR
    929 #define	PREDEFDIR 3
    930 #else
    931 #define	PREDEFDIR 2
    932 #endif
    933 
    934 struct direct root_dir[] = {
    935 	{ ROOTINO, sizeof(struct direct), DT_DIR, 1, "." },
    936 	{ ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
    937 #ifdef LOSTDIR
    938 	{ LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 10, "lost+found" },
    939 #endif
    940 };
    941 struct odirect {
    942 	u_int32_t d_ino;
    943 	u_int16_t d_reclen;
    944 	u_int16_t d_namlen;
    945 	u_char	d_name[MAXNAMLEN + 1];
    946 } oroot_dir[] = {
    947 	{ ROOTINO, sizeof(struct direct), 1, "." },
    948 	{ ROOTINO, sizeof(struct direct), 2, ".." },
    949 #ifdef LOSTDIR
    950 	{ LOSTFOUNDINO, sizeof(struct direct), 10, "lost+found" },
    951 #endif
    952 };
    953 #ifdef LOSTDIR
    954 struct direct lost_found_dir[] = {
    955 	{ LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 1, "." },
    956 	{ ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
    957 	{ 0, DIRBLKSIZ, 0, 0, 0 },
    958 };
    959 struct odirect olost_found_dir[] = {
    960 	{ LOSTFOUNDINO, sizeof(struct direct), 1, "." },
    961 	{ ROOTINO, sizeof(struct direct), 2, ".." },
    962 	{ 0, DIRBLKSIZ, 0, 0 },
    963 };
    964 #endif
    965 char buf[MAXBSIZE];
    966 static void copy_dir(struct direct *, struct direct *);
    967 
    968 int
    969 fsinit(const struct timeval *tv, mode_t mfsmode, uid_t mfsuid, gid_t mfsgid)
    970 {
    971 	union dinode node;
    972 #ifdef LOSTDIR
    973 	int i;
    974 	int dirblksiz = DIRBLKSIZ;
    975 	if (isappleufs)
    976 		dirblksiz = APPLEUFS_DIRBLKSIZ;
    977 #endif
    978 
    979 	/*
    980 	 * initialize the node
    981 	 */
    982 
    983 #ifdef LOSTDIR
    984 	/*
    985 	 * create the lost+found directory
    986 	 */
    987 	memset(&node, 0, sizeof(node));
    988 	if (Oflag == 0) {
    989 		(void)makedir((struct direct *)olost_found_dir, 2);
    990 		for (i = dirblksiz; i < sblock.fs_bsize; i += dirblksiz)
    991 			copy_dir((struct direct*)&olost_found_dir[2],
    992 				(struct direct*)&buf[i]);
    993 	} else {
    994 		(void)makedir(lost_found_dir, 2);
    995 		for (i = dirblksiz; i < sblock.fs_bsize; i += dirblksiz)
    996 			copy_dir(&lost_found_dir[2], (struct direct*)&buf[i]);
    997 	}
    998 	if (sblock.fs_magic == FS_UFS1_MAGIC) {
    999 		node.dp1.di_atime = tv->tv_sec;
   1000 		node.dp1.di_atimensec = tv->tv_usec * 1000;
   1001 		node.dp1.di_mtime = tv->tv_sec;
   1002 		node.dp1.di_mtimensec = tv->tv_usec * 1000;
   1003 		node.dp1.di_ctime = tv->tv_sec;
   1004 		node.dp1.di_ctimensec = tv->tv_usec * 1000;
   1005 		node.dp1.di_mode = IFDIR | UMASK;
   1006 		node.dp1.di_nlink = 2;
   1007 		node.dp1.di_size = sblock.fs_bsize;
   1008 		node.dp1.di_db[0] = alloc(node.dp1.di_size, node.dp1.di_mode);
   1009 		if (node.dp1.di_db[0] == 0)
   1010 			return (0);
   1011 		node.dp1.di_blocks = btodb(fragroundup(&sblock,
   1012 		    node.dp1.di_size));
   1013 		node.dp1.di_uid = geteuid();
   1014 		node.dp1.di_gid = getegid();
   1015 		wtfs(fsbtodb(&sblock, node.dp1.di_db[0]), node.dp1.di_size,
   1016 		    buf);
   1017 	} else {
   1018 		node.dp2.di_atime = tv->tv_sec;
   1019 		node.dp2.di_atimensec = tv->tv_usec * 1000;
   1020 		node.dp2.di_mtime = tv->tv_sec;
   1021 		node.dp2.di_mtimensec = tv->tv_usec * 1000;
   1022 		node.dp2.di_ctime = tv->tv_sec;
   1023 		node.dp2.di_ctimensec = tv->tv_usec * 1000;
   1024 		node.dp2.di_birthtime = tv->tv_sec;
   1025 		node.dp2.di_birthnsec = tv->tv_usec * 1000;
   1026 		node.dp2.di_mode = IFDIR | UMASK;
   1027 		node.dp2.di_nlink = 2;
   1028 		node.dp2.di_size = sblock.fs_bsize;
   1029 		node.dp2.di_db[0] = alloc(node.dp2.di_size, node.dp2.di_mode);
   1030 		if (node.dp2.di_db[0] == 0)
   1031 			return (0);
   1032 		node.dp2.di_blocks = btodb(fragroundup(&sblock,
   1033 		    node.dp2.di_size));
   1034 		node.dp2.di_uid = geteuid();
   1035 		node.dp2.di_gid = getegid();
   1036 		wtfs(fsbtodb(&sblock, node.dp2.di_db[0]), node.dp2.di_size,
   1037 		    buf);
   1038 	}
   1039 	iput(&node, LOSTFOUNDINO);
   1040 #endif
   1041 	/*
   1042 	 * create the root directory
   1043 	 */
   1044 	memset(&node, 0, sizeof(node));
   1045 	if (Oflag <= 1) {
   1046 		if (mfs) {
   1047 			node.dp1.di_mode = IFDIR | mfsmode;
   1048 			node.dp1.di_uid = mfsuid;
   1049 			node.dp1.di_gid = mfsgid;
   1050 		} else {
   1051 			node.dp1.di_mode = IFDIR | UMASK;
   1052 			node.dp1.di_uid = geteuid();
   1053 			node.dp1.di_gid = getegid();
   1054 		}
   1055 		node.dp1.di_nlink = PREDEFDIR;
   1056 		if (Oflag == 0)
   1057 			node.dp1.di_size = makedir((struct direct *)oroot_dir,
   1058 			    PREDEFDIR);
   1059 		else
   1060 			node.dp1.di_size = makedir(root_dir, PREDEFDIR);
   1061 		node.dp1.di_db[0] = alloc(sblock.fs_fsize, node.dp1.di_mode);
   1062 		if (node.dp1.di_db[0] == 0)
   1063 			return (0);
   1064 		node.dp1.di_blocks = btodb(fragroundup(&sblock,
   1065 		    node.dp1.di_size));
   1066 		wtfs(fsbtodb(&sblock, node.dp1.di_db[0]), sblock.fs_fsize, buf);
   1067 	} else {
   1068 		if (mfs) {
   1069 			node.dp2.di_mode = IFDIR | mfsmode;
   1070 			node.dp2.di_uid = mfsuid;
   1071 			node.dp2.di_gid = mfsgid;
   1072 		} else {
   1073 			node.dp2.di_mode = IFDIR | UMASK;
   1074 			node.dp2.di_uid = geteuid();
   1075 			node.dp2.di_gid = getegid();
   1076 		}
   1077 		node.dp2.di_atime = tv->tv_sec;
   1078 		node.dp2.di_atimensec = tv->tv_usec * 1000;
   1079 		node.dp2.di_mtime = tv->tv_sec;
   1080 		node.dp2.di_mtimensec = tv->tv_usec * 1000;
   1081 		node.dp2.di_ctime = tv->tv_sec;
   1082 		node.dp2.di_ctimensec = tv->tv_usec * 1000;
   1083 		node.dp2.di_birthtime = tv->tv_sec;
   1084 		node.dp2.di_birthnsec = tv->tv_usec * 1000;
   1085 		node.dp2.di_nlink = PREDEFDIR;
   1086 		node.dp2.di_size = makedir(root_dir, PREDEFDIR);
   1087 		node.dp2.di_db[0] = alloc(sblock.fs_fsize, node.dp2.di_mode);
   1088 		if (node.dp2.di_db[0] == 0)
   1089 			return (0);
   1090 		node.dp2.di_blocks = btodb(fragroundup(&sblock,
   1091 		    node.dp2.di_size));
   1092 		wtfs(fsbtodb(&sblock, node.dp2.di_db[0]), sblock.fs_fsize, buf);
   1093 	}
   1094 	iput(&node, ROOTINO);
   1095 	return (1);
   1096 }
   1097 
   1098 /*
   1099  * construct a set of directory entries in "buf".
   1100  * return size of directory.
   1101  */
   1102 int
   1103 makedir(struct direct *protodir, int entries)
   1104 {
   1105 	char *cp;
   1106 	int i, spcleft;
   1107 	int dirblksiz = DIRBLKSIZ;
   1108 	if (isappleufs)
   1109 		dirblksiz = APPLEUFS_DIRBLKSIZ;
   1110 
   1111 	memset(buf, 0, DIRBLKSIZ);
   1112 	spcleft = dirblksiz;
   1113 	for (cp = buf, i = 0; i < entries - 1; i++) {
   1114 		protodir[i].d_reclen = DIRSIZ(Oflag == 0, &protodir[i], 0);
   1115 		copy_dir(&protodir[i], (struct direct*)cp);
   1116 		cp += protodir[i].d_reclen;
   1117 		spcleft -= protodir[i].d_reclen;
   1118 	}
   1119 	protodir[i].d_reclen = spcleft;
   1120 	copy_dir(&protodir[i], (struct direct*)cp);
   1121 	return (dirblksiz);
   1122 }
   1123 
   1124 /*
   1125  * allocate a block or frag
   1126  */
   1127 daddr_t
   1128 alloc(int size, int mode)
   1129 {
   1130 	int i, frag;
   1131 	daddr_t d, blkno;
   1132 
   1133 	rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, &acg);
   1134 	/* fs -> host byte order */
   1135 	if (needswap)
   1136 		ffs_cg_swap(&acg, &acg, &sblock);
   1137 	if (acg.cg_magic != CG_MAGIC) {
   1138 		printf("cg 0: bad magic number\n");
   1139 		return (0);
   1140 	}
   1141 	if (acg.cg_cs.cs_nbfree == 0) {
   1142 		printf("first cylinder group ran out of space\n");
   1143 		return (0);
   1144 	}
   1145 	for (d = 0; d < acg.cg_ndblk; d += sblock.fs_frag)
   1146 		if (isblock(&sblock, cg_blksfree(&acg, 0),
   1147 		    d >> sblock.fs_fragshift))
   1148 			goto goth;
   1149 	printf("internal error: can't find block in cyl 0\n");
   1150 	return (0);
   1151 goth:
   1152 	blkno = fragstoblks(&sblock, d);
   1153 	clrblock(&sblock, cg_blksfree(&acg, 0), blkno);
   1154 	if (sblock.fs_contigsumsize > 0)
   1155 		clrbit(cg_clustersfree(&acg, 0), blkno);
   1156 	acg.cg_cs.cs_nbfree--;
   1157 	sblock.fs_cstotal.cs_nbfree--;
   1158 	fscs_0->cs_nbfree--;
   1159 	if (mode & IFDIR) {
   1160 		acg.cg_cs.cs_ndir++;
   1161 		sblock.fs_cstotal.cs_ndir++;
   1162 		fscs_0->cs_ndir++;
   1163 	}
   1164 	if (Oflag <= 1) {
   1165 		int cn = old_cbtocylno(&sblock, d);
   1166 		old_cg_blktot(&acg, 0)[cn]--;
   1167 		old_cg_blks(&sblock, &acg,
   1168 		    cn, 0)[old_cbtorpos(&sblock, d)]--;
   1169 	}
   1170 	if (size != sblock.fs_bsize) {
   1171 		frag = howmany(size, sblock.fs_fsize);
   1172 		fscs_0->cs_nffree += sblock.fs_frag - frag;
   1173 		sblock.fs_cstotal.cs_nffree += sblock.fs_frag - frag;
   1174 		acg.cg_cs.cs_nffree += sblock.fs_frag - frag;
   1175 		acg.cg_frsum[sblock.fs_frag - frag]++;
   1176 		for (i = frag; i < sblock.fs_frag; i++)
   1177 			setbit(cg_blksfree(&acg, 0), d + i);
   1178 	}
   1179 	/* host -> fs byte order */
   1180 	if (needswap)
   1181 		ffs_cg_swap(&acg, &acg, &sblock);
   1182 	wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, &acg);
   1183 	return (d);
   1184 }
   1185 
   1186 /*
   1187  * Allocate an inode on the disk
   1188  */
   1189 static void
   1190 iput(union dinode *ip, ino_t ino)
   1191 {
   1192 	daddr_t d;
   1193 	int c, i;
   1194 	struct ufs1_dinode *dp1;
   1195 	struct ufs2_dinode *dp2;
   1196 
   1197 	c = ino_to_cg(&sblock, ino);
   1198 	rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, &acg);
   1199 	/* fs -> host byte order */
   1200 	if (needswap)
   1201 		ffs_cg_swap(&acg, &acg, &sblock);
   1202 	if (acg.cg_magic != CG_MAGIC) {
   1203 		printf("cg 0: bad magic number\n");
   1204 		exit(31);
   1205 	}
   1206 	acg.cg_cs.cs_nifree--;
   1207 	setbit(cg_inosused(&acg, 0), ino);
   1208 	/* host -> fs byte order */
   1209 	if (needswap)
   1210 		ffs_cg_swap(&acg, &acg, &sblock);
   1211 	wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, &acg);
   1212 	sblock.fs_cstotal.cs_nifree--;
   1213 	fscs_0->cs_nifree--;
   1214 	if (ino >= sblock.fs_ipg * sblock.fs_ncg) {
   1215 		printf("fsinit: inode value out of range (%d).\n", ino);
   1216 		exit(32);
   1217 	}
   1218 	d = fsbtodb(&sblock, ino_to_fsba(&sblock, ino));
   1219 	rdfs(d, sblock.fs_bsize, (char *)iobuf);
   1220 	if (sblock.fs_magic == FS_UFS1_MAGIC) {
   1221 		dp1 = (struct ufs1_dinode *)iobuf;
   1222 		dp1 += ino_to_fsbo(&sblock, ino);
   1223 		if (needswap) {
   1224 			ffs_dinode1_swap(&ip->dp1, dp1);
   1225 			/* ffs_dinode1_swap() doesn't swap blocks addrs */
   1226 			for (i=0; i<NDADDR + NIADDR; i++)
   1227 			    dp1->di_db[i] = bswap32(ip->dp1.di_db[i]);
   1228 		} else
   1229 			*dp1 = ip->dp1;
   1230 		dp1->di_gen = arc4random() & INT32_MAX;
   1231 	} else {
   1232 		dp2 = (struct ufs2_dinode *)iobuf;
   1233 		dp2 += ino_to_fsbo(&sblock, ino);
   1234 		if (needswap) {
   1235 			ffs_dinode2_swap(&ip->dp2, dp2);
   1236 			for (i=0; i<NDADDR + NIADDR; i++)
   1237 			    dp2->di_db[i] = bswap32(ip->dp2.di_db[i]);
   1238 		} else
   1239 			*dp2 = ip->dp2;
   1240 		dp2->di_gen = arc4random() & INT32_MAX;
   1241 	}
   1242 	wtfs(d, sblock.fs_bsize, iobuf);
   1243 }
   1244 
   1245 /*
   1246  * read a block from the file system
   1247  */
   1248 void
   1249 rdfs(daddr_t bno, int size, void *bf)
   1250 {
   1251 	int n;
   1252 	off_t offset;
   1253 
   1254 #ifdef MFS
   1255 	if (mfs) {
   1256 		if (Nflag)
   1257 			memset(bf, 0, size);
   1258 		else
   1259 			memmove(bf, membase + bno * sectorsize, size);
   1260 		return;
   1261 	}
   1262 #endif
   1263 	offset = bno;
   1264 	n = pread(fsi, bf, size, offset * sectorsize);
   1265 	if (n != size) {
   1266 		printf("rdfs: read error for sector %lld: %s\n",
   1267 		    (long long)bno, strerror(errno));
   1268 		exit(34);
   1269 	}
   1270 }
   1271 
   1272 /*
   1273  * write a block to the file system
   1274  */
   1275 void
   1276 wtfs(daddr_t bno, int size, void *bf)
   1277 {
   1278 	int n;
   1279 	off_t offset;
   1280 
   1281 	if (Nflag)
   1282 		return;
   1283 #ifdef MFS
   1284 	if (mfs) {
   1285 		memmove(membase + bno * sectorsize, bf, size);
   1286 		return;
   1287 	}
   1288 #endif
   1289 	offset = bno;
   1290 	n = pwrite(fso, bf, size, offset * sectorsize);
   1291 	if (n != size) {
   1292 		printf("wtfs: write error for sector %lld: %s\n",
   1293 		    (long long)bno, strerror(errno));
   1294 		exit(36);
   1295 	}
   1296 }
   1297 
   1298 /*
   1299  * check if a block is available
   1300  */
   1301 int
   1302 isblock(struct fs *fs, unsigned char *cp, int h)
   1303 {
   1304 	unsigned char mask;
   1305 
   1306 	switch (fs->fs_fragshift) {
   1307 	case 3:
   1308 		return (cp[h] == 0xff);
   1309 	case 2:
   1310 		mask = 0x0f << ((h & 0x1) << 2);
   1311 		return ((cp[h >> 1] & mask) == mask);
   1312 	case 1:
   1313 		mask = 0x03 << ((h & 0x3) << 1);
   1314 		return ((cp[h >> 2] & mask) == mask);
   1315 	case 0:
   1316 		mask = 0x01 << (h & 0x7);
   1317 		return ((cp[h >> 3] & mask) == mask);
   1318 	default:
   1319 #ifdef STANDALONE
   1320 		printf("isblock bad fs_fragshift %d\n", fs->fs_fragshift);
   1321 #else
   1322 		fprintf(stderr, "isblock bad fs_fragshift %d\n",
   1323 		    fs->fs_fragshift);
   1324 #endif
   1325 		return (0);
   1326 	}
   1327 }
   1328 
   1329 /*
   1330  * take a block out of the map
   1331  */
   1332 void
   1333 clrblock(struct fs *fs, unsigned char *cp, int h)
   1334 {
   1335 	switch ((fs)->fs_fragshift) {
   1336 	case 3:
   1337 		cp[h] = 0;
   1338 		return;
   1339 	case 2:
   1340 		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
   1341 		return;
   1342 	case 1:
   1343 		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
   1344 		return;
   1345 	case 0:
   1346 		cp[h >> 3] &= ~(0x01 << (h & 0x7));
   1347 		return;
   1348 	default:
   1349 #ifdef STANDALONE
   1350 		printf("clrblock bad fs_fragshift %d\n", fs->fs_fragshift);
   1351 #else
   1352 		fprintf(stderr, "clrblock bad fs_fragshift %d\n",
   1353 		    fs->fs_fragshift);
   1354 #endif
   1355 		return;
   1356 	}
   1357 }
   1358 
   1359 /*
   1360  * put a block into the map
   1361  */
   1362 void
   1363 setblock(struct fs *fs, unsigned char *cp, int h)
   1364 {
   1365 	switch (fs->fs_fragshift) {
   1366 	case 3:
   1367 		cp[h] = 0xff;
   1368 		return;
   1369 	case 2:
   1370 		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
   1371 		return;
   1372 	case 1:
   1373 		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
   1374 		return;
   1375 	case 0:
   1376 		cp[h >> 3] |= (0x01 << (h & 0x7));
   1377 		return;
   1378 	default:
   1379 #ifdef STANDALONE
   1380 		printf("setblock bad fs_frag %d\n", fs->fs_fragshift);
   1381 #else
   1382 		fprintf(stderr, "setblock bad fs_fragshift %d\n",
   1383 		    fs->fs_fragshift);
   1384 #endif
   1385 		return;
   1386 	}
   1387 }
   1388 
   1389 /* copy a direntry to a buffer, in fs byte order */
   1390 static void
   1391 copy_dir(struct direct *dir, struct direct *dbuf)
   1392 {
   1393 	memcpy(dbuf, dir, DIRSIZ(Oflag == 0, dir, 0));
   1394 	if (needswap) {
   1395 		dbuf->d_ino = bswap32(dir->d_ino);
   1396 		dbuf->d_reclen = bswap16(dir->d_reclen);
   1397 		if (Oflag == 0)
   1398 			((struct odirect*)dbuf)->d_namlen =
   1399 				bswap16(((struct odirect*)dir)->d_namlen);
   1400 	}
   1401 }
   1402 
   1403 /* Determine how many digits are needed to print a given integer */
   1404 static int
   1405 count_digits(uint64_t num)
   1406 {
   1407 	int ndig;
   1408 
   1409 	for (ndig = 1; num > 9; num /= 10, ndig++);
   1410 
   1411 	return (ndig);
   1412 }
   1413 
   1414 static int
   1415 ilog2(int val)
   1416 {
   1417 	u_int n;
   1418 
   1419 	for (n = 0; n < sizeof(n) * CHAR_BIT; n++)
   1420 		if (1 << n == val)
   1421 			return (n);
   1422 	errx(1, "ilog2: %d is not a power of 2\n", val);
   1423 }
   1424 
   1425 static void
   1426 zap_old_sblock(int sblkoff)
   1427 {
   1428 	static int cg0_data;
   1429 	uint32_t oldfs[SBLOCKSIZE / 4];
   1430 	static const struct fsm {
   1431 		uint32_t	offset;
   1432 		uint32_t	magic;
   1433 		uint32_t	mask;
   1434 	} fs_magics[] = {
   1435 		{offsetof(struct fs, fs_magic)/4, FS_UFS1_MAGIC, ~0u},
   1436 		{offsetof(struct fs, fs_magic)/4, FS_UFS2_MAGIC, ~0u},
   1437 		{0, 0x70162, ~0u},		/* LFS_MAGIC */
   1438 		{14, 0xef53, 0xffff},		/* EXT2FS (little) */
   1439 		{14, 0xef530000, 0xffff0000},	/* EXT2FS (big) */
   1440 		{~0u},
   1441 	};
   1442 	const struct fsm *fsm;
   1443 
   1444 	if (Nflag)
   1445 		return;
   1446 
   1447 	if (sblkoff == 0)	/* Why did UFS2 add support for this?  sigh. */
   1448 		return;
   1449 
   1450 	if (cg0_data == 0)
   1451 		/* For FFSv1 this could include all the inodes. */
   1452 		cg0_data = cgsblock(&sblock, 0) * sblock.fs_fsize + iobufsize;
   1453 
   1454 	/* Ignore anything that is beyond our filesystem */
   1455 	if ((sblkoff + SBLOCKSIZE)/sectorsize >= fssize)
   1456 		return;
   1457 	/* Zero anything inside our filesystem... */
   1458 	if (sblkoff >= sblock.fs_sblockloc) {
   1459 		/* ...unless we will write that area anyway */
   1460 		if (sblkoff >= cg0_data)
   1461 			wtfs(sblkoff / sectorsize,
   1462 			    roundup(sizeof sblock, sectorsize), iobuf);
   1463 		return;
   1464 	}
   1465 
   1466 	/* The sector might contain boot code, so we must validate it */
   1467 	rdfs(sblkoff/sectorsize, sizeof oldfs, &oldfs);
   1468 	for (fsm = fs_magics; ; fsm++) {
   1469 		uint32_t v;
   1470 		if (fsm->mask == 0)
   1471 			return;
   1472 		v = oldfs[fsm->offset];
   1473 		if ((v & fsm->mask) == fsm->magic ||
   1474 		    (bswap32(v) & fsm->mask) == fsm->magic)
   1475 			break;
   1476 	}
   1477 
   1478 	/* Just zap the magic number */
   1479 	oldfs[fsm->offset] = 0;
   1480 	wtfs(sblkoff/sectorsize, sizeof oldfs, &oldfs);
   1481 }
   1482 
   1483 
   1484 #ifdef MFS
   1485 /*
   1486  * XXX!
   1487  * Attempt to guess how much more space is available for process data.  The
   1488  * heuristic we use is
   1489  *
   1490  *	max_data_limit - (sbrk(0) - etext) - 128kB
   1491  *
   1492  * etext approximates that start address of the data segment, and the 128kB
   1493  * allows some slop for both segment gap between text and data, and for other
   1494  * (libc) malloc usage.
   1495  */
   1496 static void
   1497 calc_memfree(void)
   1498 {
   1499 	extern char etext;
   1500 	struct rlimit rlp;
   1501 	u_long base;
   1502 
   1503 	base = (u_long)sbrk(0) - (u_long)&etext;
   1504 	if (getrlimit(RLIMIT_DATA, &rlp) < 0)
   1505 		perror("getrlimit");
   1506 	rlp.rlim_cur = rlp.rlim_max;
   1507 	if (setrlimit(RLIMIT_DATA, &rlp) < 0)
   1508 		perror("setrlimit");
   1509 	memleft = rlp.rlim_max - base - (128 * 1024);
   1510 }
   1511 
   1512 /*
   1513  * Internal version of malloc that trims the requested size if not enough
   1514  * memory is available.
   1515  */
   1516 static void *
   1517 mkfs_malloc(size_t size)
   1518 {
   1519 	u_long pgsz;
   1520 
   1521 	if (size == 0)
   1522 		return (NULL);
   1523 	if (memleft == 0)
   1524 		calc_memfree();
   1525 
   1526 	pgsz = getpagesize() - 1;
   1527 	size = (size + pgsz) &~ pgsz;
   1528 	if (size > memleft)
   1529 		size = memleft;
   1530 	memleft -= size;
   1531 	return (mmap(0, size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
   1532 	    -1, 0));
   1533 }
   1534 #endif	/* MFS */
   1535