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