xref: /netbsd-src/sbin/newfs/mkfs.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: mkfs.c,v 1.130 2020/08/20 15:54:11 riastradh 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.130 2020/08/20 15:54:11 riastradh 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/ufs/quota2.h>
88 #include <ufs/ffs/fs.h>
89 #include <ufs/ffs/ffs_extern.h>
90 #include <sys/ioctl.h>
91 #include <sys/disklabel.h>
92 
93 #include <err.h>
94 #include <errno.h>
95 #include <string.h>
96 #include <unistd.h>
97 #include <stdlib.h>
98 #include <stddef.h>
99 
100 #ifndef STANDALONE
101 #include <stdio.h>
102 #endif
103 
104 #include "extern.h"
105 
106 union dinode {
107 	struct ufs1_dinode dp1;
108 	struct ufs2_dinode dp2;
109 };
110 
111 static void initcg(int, const struct timeval *);
112 static int fsinit(const struct timeval *, mode_t, uid_t, gid_t);
113 union Buffer;
114 static int makedir(union Buffer *, struct direct *, int);
115 static daddr_t alloc(int, int);
116 static void iput(union dinode *, ino_t);
117 static void rdfs(daddr_t, int, void *);
118 static void wtfs(daddr_t, int, void *);
119 static int isblock(struct fs *, unsigned char *, int);
120 static void clrblock(struct fs *, unsigned char *, int);
121 static void setblock(struct fs *, unsigned char *, int);
122 static int ilog2(int);
123 static void zap_old_sblock(int);
124 #ifdef MFS
125 static void *mkfs_malloc(size_t size);
126 #endif
127 
128 /*
129  * make file system for cylinder-group style file systems
130  */
131 #define	UMASK		0755
132 
133 union {
134 	struct fs fs;
135 	char data[SBLOCKSIZE];
136 } *fsun;
137 #define	sblock	fsun->fs
138 
139 union Buffer {
140 	struct quota2_header q2h;
141 	char data[MAXBSIZE];
142 };
143 
144 struct	csum *fscs_0;		/* first block of cylinder summaries */
145 struct	csum *fscs_next;	/* place for next summary */
146 struct	csum *fscs_end;		/* end of summary buffer */
147 struct	csum *fscs_reset;	/* place for next summary after write */
148 uint	fs_csaddr;		/* fragment number to write to */
149 
150 union {
151 	struct cg cg;
152 	char pad[MAXBSIZE];
153 } *cgun;
154 #define	acg	cgun->cg
155 
156 #define DIP(dp, field) \
157 	((sblock.fs_magic == FS_UFS1_MAGIC) ? \
158 	(dp)->dp1.di_##field : (dp)->dp2.di_##field)
159 
160 #define EXT2FS_SBOFF	1024	/* XXX: SBOFF in <ufs/ext2fs/ext2fs.h> */
161 
162 char *iobuf;
163 int iobufsize;			/* size to end of 2nd inode block */
164 int iobuf_memsize;		/* Actual buffer size */
165 
166 int	fsi, fso;
167 
168 static void
169 fserr(int num)
170 {
171 #ifdef GARBAGE
172 	extern int Gflag;
173 
174 	if (Gflag)
175 		return;
176 #endif
177 	exit(num);
178 }
179 
180 void
181 mkfs(const char *fsys, int fi, int fo,
182     mode_t mfsmode, uid_t mfsuid, gid_t mfsgid)
183 {
184 	uint fragsperinodeblk, ncg, u;
185 	uint cgzero;
186 	uint64_t inodeblks, cgall;
187 	int32_t cylno, i, csfrags;
188 	int inodes_per_cg;
189 	struct timeval tv;
190 	long long sizepb;
191 	int len, col, delta, fld_width, max_cols;
192 	struct winsize winsize;
193 
194 #ifndef STANDALONE
195 	gettimeofday(&tv, NULL);
196 #endif
197 #ifdef MFS
198 	if (mfs && !Nflag) {
199 		if ((membase = mkfs_malloc(fssize * sectorsize)) == NULL)
200 			exit(12);
201 	}
202 #endif
203 	if ((fsun = aligned_alloc(DEV_BSIZE, sizeof(*fsun))) == NULL)
204 		exit(12);
205 	memset(fsun, 0, sizeof(*fsun));
206 	if ((cgun = aligned_alloc(DEV_BSIZE, sizeof(*cgun))) == NULL)
207 		exit(12);
208 	memset(cgun, 0, sizeof(*cgun));
209 
210 	fsi = fi;
211 	fso = fo;
212 	if (Oflag == 0) {
213 		sblock.fs_old_inodefmt = FS_42INODEFMT;
214 		sblock.fs_maxsymlinklen = 0;
215 		sblock.fs_old_flags = 0;
216 	} else {
217 		sblock.fs_old_inodefmt = FS_44INODEFMT;
218 		sblock.fs_maxsymlinklen = (Oflag == 1 ? UFS1_MAXSYMLINKLEN :
219 		    UFS2_MAXSYMLINKLEN);
220 		sblock.fs_old_flags = FS_FLAGS_UPDATED;
221 		if (isappleufs)
222 			sblock.fs_old_flags = 0;
223 		sblock.fs_flags = 0;
224 	}
225 
226 	/*
227 	 * collect and verify the filesystem density info
228 	 */
229 	sblock.fs_avgfilesize = avgfilesize;
230 	sblock.fs_avgfpdir = avgfpdir;
231 	if (sblock.fs_avgfilesize <= 0) {
232 		printf("illegal expected average file size %d\n",
233 		    sblock.fs_avgfilesize);
234 		fserr(14);
235 	}
236 	if (sblock.fs_avgfpdir <= 0) {
237 		printf("illegal expected number of files per directory %d\n",
238 		    sblock.fs_avgfpdir);
239 		fserr(15);
240 	}
241 	/*
242 	 * collect and verify the block and fragment sizes
243 	 */
244 	sblock.fs_bsize = bsize;
245 	sblock.fs_fsize = fsize;
246 	if (!powerof2(sblock.fs_bsize)) {
247 		printf("block size must be a power of 2, not %d\n",
248 		    sblock.fs_bsize);
249 		fserr(16);
250 	}
251 	if (!powerof2(sblock.fs_fsize)) {
252 		printf("fragment size must be a power of 2, not %d\n",
253 		    sblock.fs_fsize);
254 		fserr(17);
255 	}
256 	if (sblock.fs_fsize < sectorsize) {
257 		printf("fragment size %d is too small, minimum is %d\n",
258 		    sblock.fs_fsize, sectorsize);
259 		fserr(18);
260 	}
261 	if (sblock.fs_bsize < MINBSIZE) {
262 		printf("block size %d is too small, minimum is %d\n",
263 		    sblock.fs_bsize, MINBSIZE);
264 		fserr(19);
265 	}
266 	if (sblock.fs_bsize > MAXBSIZE) {
267 		printf("block size %d is too large, maximum is %d\n",
268 		    sblock.fs_bsize, MAXBSIZE);
269 		fserr(19);
270 	}
271 	if (sblock.fs_bsize < sblock.fs_fsize) {
272 		printf("block size (%d) cannot be smaller than fragment size (%d)\n",
273 		    sblock.fs_bsize, sblock.fs_fsize);
274 		fserr(20);
275 	}
276 
277 	if (maxbsize < bsize || !powerof2(maxbsize)) {
278 		sblock.fs_maxbsize = sblock.fs_bsize;
279 	} else if (sblock.fs_maxbsize > FS_MAXCONTIG * sblock.fs_bsize) {
280 		sblock.fs_maxbsize = FS_MAXCONTIG * sblock.fs_bsize;
281 	} else {
282 		sblock.fs_maxbsize = maxbsize;
283 	}
284 	sblock.fs_maxcontig = maxcontig;
285 	if (sblock.fs_maxcontig < sblock.fs_maxbsize / sblock.fs_bsize) {
286 		sblock.fs_maxcontig = sblock.fs_maxbsize / sblock.fs_bsize;
287 		if (verbosity > 0)
288 			printf("Maxcontig raised to %d\n", sblock.fs_maxbsize);
289 	}
290 	if (sblock.fs_maxcontig > 1)
291 		sblock.fs_contigsumsize = MIN(sblock.fs_maxcontig,FS_MAXCONTIG);
292 
293 	sblock.fs_bmask = ~(sblock.fs_bsize - 1);
294 	sblock.fs_fmask = ~(sblock.fs_fsize - 1);
295 	sblock.fs_qbmask = ~sblock.fs_bmask;
296 	sblock.fs_qfmask = ~sblock.fs_fmask;
297 	for (sblock.fs_bshift = 0, i = sblock.fs_bsize; i > 1; i >>= 1)
298 		sblock.fs_bshift++;
299 	for (sblock.fs_fshift = 0, i = sblock.fs_fsize; i > 1; i >>= 1)
300 		sblock.fs_fshift++;
301 	sblock.fs_frag = ffs_numfrags(&sblock, sblock.fs_bsize);
302 	for (sblock.fs_fragshift = 0, i = sblock.fs_frag; i > 1; i >>= 1)
303 		sblock.fs_fragshift++;
304 	if (sblock.fs_frag > MAXFRAG) {
305 		printf("fragment size %d is too small, "
306 			"minimum with block size %d is %d\n",
307 		    sblock.fs_fsize, sblock.fs_bsize,
308 		    sblock.fs_bsize / MAXFRAG);
309 		fserr(21);
310 	}
311 	sblock.fs_fsbtodb = ilog2(sblock.fs_fsize / sectorsize);
312 	sblock.fs_size = FFS_DBTOFSB(&sblock, fssize);
313 	if (Oflag <= 1) {
314 		if ((uint64_t)sblock.fs_size >= 1ull << 31) {
315 			printf("Too many fragments (0x%" PRIx64
316 			    ") for a FFSv1 filesystem\n", sblock.fs_size);
317 			fserr(22);
318 		}
319 		sblock.fs_magic = FS_UFS1_MAGIC;
320 		sblock.fs_sblockloc = SBLOCK_UFS1;
321 		sblock.fs_nindir = sblock.fs_bsize / sizeof(int32_t);
322 		sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs1_dinode);
323 		sblock.fs_old_cgoffset = 0;
324 		sblock.fs_old_cgmask = 0xffffffff;
325 		sblock.fs_old_size = sblock.fs_size;
326 		sblock.fs_old_rotdelay = 0;
327 		sblock.fs_old_rps = 60;
328 		sblock.fs_old_nspf = sblock.fs_fsize / sectorsize;
329 		sblock.fs_old_cpg = 1;
330 		sblock.fs_old_interleave = 1;
331 		sblock.fs_old_trackskew = 0;
332 		sblock.fs_old_cpc = 0;
333 		sblock.fs_old_postblformat = FS_DYNAMICPOSTBLFMT;
334 		sblock.fs_old_nrpos = 1;
335 	} else {
336 		sblock.fs_magic = FS_UFS2_MAGIC;
337 		sblock.fs_sblockloc = SBLOCK_UFS2;
338 		sblock.fs_nindir = sblock.fs_bsize / sizeof(int64_t);
339 		sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs2_dinode);
340 	}
341 
342 	sblock.fs_sblkno =
343 	    roundup(howmany(sblock.fs_sblockloc + SBLOCKSIZE, sblock.fs_fsize),
344 		sblock.fs_frag);
345 	sblock.fs_cblkno = (daddr_t)(sblock.fs_sblkno +
346 	    roundup(howmany(SBLOCKSIZE, sblock.fs_fsize), sblock.fs_frag));
347 	sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag;
348 	sblock.fs_maxfilesize = sblock.fs_bsize * UFS_NDADDR - 1;
349 	for (sizepb = sblock.fs_bsize, i = 0; i < UFS_NIADDR; i++) {
350 		sizepb *= FFS_NINDIR(&sblock);
351 		sblock.fs_maxfilesize += sizepb;
352 	}
353 
354 	/*
355 	 * Calculate the number of blocks to put into each cylinder group.
356 	 *
357 	 * The cylinder group size is limited because the data structure
358 	 * must fit into a single block.
359 	 * We try to have as few cylinder groups as possible, with a proviso
360 	 * that we create at least MINCYLGRPS (==4) except for small
361 	 * filesystems.
362 	 *
363 	 * This algorithm works out how many blocks of inodes would be
364 	 * needed to fill the entire volume at the specified density.
365 	 * It then looks at how big the 'cylinder block' would have to
366 	 * be and, assuming that it is linearly related to the number
367 	 * of inodes and blocks how many cylinder groups are needed to
368 	 * keep the cylinder block below the filesystem block size.
369 	 *
370 	 * The cylinder groups are then all created with the average size.
371 	 *
372 	 * Space taken by the red tape on cylinder groups other than the
373 	 * first is ignored.
374 	 */
375 
376 	/* There must be space for 1 inode block and 2 data blocks */
377 	if (sblock.fs_size < sblock.fs_iblkno + 3 * sblock.fs_frag) {
378 		printf("Filesystem size %lld < minimum size of %d\n",
379 		    (long long)sblock.fs_size, sblock.fs_iblkno + 3 * sblock.fs_frag);
380 		fserr(23);
381 	}
382 	if (num_inodes != 0)
383 		inodeblks = howmany(num_inodes, FFS_INOPB(&sblock));
384 	else {
385 		/*
386 		 * Calculate 'per inode block' so we can allocate less than
387 		 * 1 fragment per inode - useful for /dev.
388 		 */
389 		fragsperinodeblk = MAX(ffs_numfrags(&sblock,
390 					(uint64_t)density * FFS_INOPB(&sblock)), 1);
391 		inodeblks = (sblock.fs_size - sblock.fs_iblkno) /
392 			(sblock.fs_frag + fragsperinodeblk);
393 	}
394 	if (inodeblks == 0)
395 		inodeblks = 1;
396 	/* Ensure that there are at least 2 data blocks (or we fail below) */
397 	if (inodeblks > (uint64_t)(sblock.fs_size - sblock.fs_iblkno)/sblock.fs_frag - 2)
398 		inodeblks = (sblock.fs_size-sblock.fs_iblkno)/sblock.fs_frag-2;
399 	/* Even UFS2 limits number of inodes to 2^31 (fs_ipg is int32_t) */
400 	if (inodeblks * FFS_INOPB(&sblock) >= 1ull << 31)
401 		inodeblks = ((1ull << 31) - NBBY) / FFS_INOPB(&sblock);
402 	/*
403 	 * See what would happen if we tried to use 1 cylinder group.
404 	 * Assume space linear, so work out number of cylinder groups needed.
405 	 */
406 	cgzero = CGSIZE_IF(&sblock, 0, 0);
407 	cgall = CGSIZE_IF(&sblock, inodeblks * FFS_INOPB(&sblock), sblock.fs_size);
408 	ncg = howmany(cgall - cgzero, sblock.fs_bsize - cgzero);
409 	if (ncg < MINCYLGRPS) {
410 		/*
411 		 * We would like to allocate MINCLYGRPS cylinder groups,
412 		 * but for small file sytems (especially ones with a lot
413 		 * of inodes) this is not desirable (or possible).
414 		 */
415 		u = sblock.fs_size / 2 / (sblock.fs_iblkno +
416 						inodeblks * sblock.fs_frag);
417 		if (u > ncg)
418 			ncg = u;
419 		if (ncg > MINCYLGRPS)
420 			ncg = MINCYLGRPS;
421 		if (ncg > inodeblks)
422 			ncg = inodeblks;
423 	}
424 	/*
425 	 * Put an equal number of blocks in each cylinder group.
426 	 * Round up so we don't have more fragments in the last CG than
427 	 * the earlier ones (does that matter?), but kill a block if the
428 	 * CGSIZE becomes too big (only happens if there are a lot of CGs).
429 	 */
430 	sblock.fs_fpg = roundup(howmany(sblock.fs_size, ncg), sblock.fs_frag);
431 	/* Round up the fragments/group so the bitmap bytes are full */
432 	sblock.fs_fpg = roundup(sblock.fs_fpg, NBBY);
433 	inodes_per_cg = ((inodeblks - 1) / ncg + 1) * FFS_INOPB(&sblock);
434 
435 	i = CGSIZE_IF(&sblock, inodes_per_cg, sblock.fs_fpg);
436 	if (i > sblock.fs_bsize) {
437 		sblock.fs_fpg -= (i - sblock.fs_bsize) * NBBY;
438 		/* ... and recalculate how many cylinder groups we now need */
439 		ncg = howmany(sblock.fs_size, sblock.fs_fpg);
440 		inodes_per_cg = ((inodeblks - 1) / ncg + 1) * FFS_INOPB(&sblock);
441 	}
442 	sblock.fs_ipg = inodes_per_cg;
443 	/* Sanity check on our sums... */
444 	if ((int)CGSIZE(&sblock) > sblock.fs_bsize) {
445 		printf("CGSIZE miscalculated %d > %d\n",
446 		    (int)CGSIZE(&sblock), sblock.fs_bsize);
447 		fserr(24);
448 	}
449 
450 	sblock.fs_dblkno = sblock.fs_iblkno + sblock.fs_ipg / FFS_INOPF(&sblock);
451 	/* Check that the last cylinder group has enough space for the inodes */
452 	i = sblock.fs_size - sblock.fs_fpg * (ncg - 1ull);
453 	if (i < sblock.fs_dblkno) {
454 		/*
455 		 * Since we make all the cylinder groups the same size, the
456 		 * last will only be small if there are a large number of
457 		 * cylinder groups. If we pull even a fragment from each
458 		 * of the other groups then the last CG will be overfull.
459 		 * So we just kill the last CG.
460 		 */
461 		ncg--;
462 		sblock.fs_size -= i;
463 	}
464 	sblock.fs_ncg = ncg;
465 
466 	sblock.fs_cgsize = ffs_fragroundup(&sblock, CGSIZE(&sblock));
467 	if (Oflag <= 1) {
468 		sblock.fs_old_spc = sblock.fs_fpg * sblock.fs_old_nspf;
469 		sblock.fs_old_nsect = sblock.fs_old_spc;
470 		sblock.fs_old_npsect = sblock.fs_old_spc;
471 		sblock.fs_old_ncyl = sblock.fs_ncg;
472 	}
473 
474 	/*
475 	 * Cylinder group summary information for each cylinder is written
476 	 * into the first cylinder group.
477 	 * Write this fragment by fragment, but doing the first CG last
478 	 * (after we've taken stuff off for the structure itself and the
479 	 * root directory.
480 	 */
481 	sblock.fs_csaddr = cgdmin(&sblock, 0);
482 	sblock.fs_cssize =
483 	    ffs_fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum));
484 	if (512 % sizeof *fscs_0)
485 		errx(1, "cylinder group summary doesn't fit in sectors");
486 	fscs_0 = mmap(0, 2 * sblock.fs_fsize, PROT_READ|PROT_WRITE,
487 			MAP_ANON|MAP_PRIVATE, -1, 0);
488 	if (fscs_0 == MAP_FAILED)
489 		exit(39);
490 	memset(fscs_0, 0, 2 * sblock.fs_fsize);
491 	fs_csaddr = sblock.fs_csaddr;
492 	fscs_next = fscs_0;
493 	fscs_end = (void *)((char *)fscs_0 + 2 * sblock.fs_fsize);
494 	fscs_reset = (void *)((char *)fscs_0 + sblock.fs_fsize);
495 	/*
496 	 * fill in remaining fields of the super block
497 	 */
498 	sblock.fs_sbsize = ffs_fragroundup(&sblock, sizeof(struct fs));
499 	if (sblock.fs_sbsize > SBLOCKSIZE)
500 		sblock.fs_sbsize = SBLOCKSIZE;
501 	sblock.fs_minfree = minfree;
502 	sblock.fs_maxcontig = maxcontig;
503 	sblock.fs_maxbpg = maxbpg;
504 	sblock.fs_optim = opt;
505 	sblock.fs_cgrotor = 0;
506 	sblock.fs_pendingblocks = 0;
507 	sblock.fs_pendinginodes = 0;
508 	sblock.fs_cstotal.cs_ndir = 0;
509 	sblock.fs_cstotal.cs_nbfree = 0;
510 	sblock.fs_cstotal.cs_nifree = 0;
511 	sblock.fs_cstotal.cs_nffree = 0;
512 	sblock.fs_fmod = 0;
513 	sblock.fs_ronly = 0;
514 	sblock.fs_state = 0;
515 	sblock.fs_clean = FS_ISCLEAN;
516 	sblock.fs_ronly = 0;
517 	sblock.fs_id[0] = (long)tv.tv_sec;	/* XXXfvdl huh? */
518 	sblock.fs_id[1] = arc4random() & INT32_MAX;
519 	sblock.fs_fsmnt[0] = '\0';
520 	csfrags = howmany(sblock.fs_cssize, sblock.fs_fsize);
521 	sblock.fs_dsize = sblock.fs_size - sblock.fs_sblkno -
522 	    sblock.fs_ncg * (sblock.fs_dblkno - sblock.fs_sblkno);
523 	sblock.fs_cstotal.cs_nbfree =
524 	    ffs_fragstoblks(&sblock, sblock.fs_dsize) -
525 	    howmany(csfrags, sblock.fs_frag);
526 	sblock.fs_cstotal.cs_nffree =
527 	    ffs_fragnum(&sblock, sblock.fs_size) +
528 	    (ffs_fragnum(&sblock, csfrags) > 0 ?
529 	    sblock.fs_frag - ffs_fragnum(&sblock, csfrags) : 0);
530 	sblock.fs_cstotal.cs_nifree = sblock.fs_ncg * sblock.fs_ipg - UFS_ROOTINO;
531 	sblock.fs_cstotal.cs_ndir = 0;
532 	sblock.fs_dsize -= csfrags;
533 	sblock.fs_time = tv.tv_sec;
534 	if (Oflag <= 1) {
535 		sblock.fs_old_time = tv.tv_sec;
536 		sblock.fs_old_dsize = sblock.fs_dsize;
537 		sblock.fs_old_csaddr = sblock.fs_csaddr;
538 		sblock.fs_old_cstotal.cs_ndir = sblock.fs_cstotal.cs_ndir;
539 		sblock.fs_old_cstotal.cs_nbfree = sblock.fs_cstotal.cs_nbfree;
540 		sblock.fs_old_cstotal.cs_nifree = sblock.fs_cstotal.cs_nifree;
541 		sblock.fs_old_cstotal.cs_nffree = sblock.fs_cstotal.cs_nffree;
542 	}
543 	/* add quota data in superblock */
544 	if (quotas) {
545 		sblock.fs_flags |= FS_DOQUOTA2;
546 		sblock.fs_quota_magic = Q2_HEAD_MAGIC;
547 		sblock.fs_quota_flags = quotas;
548 	}
549 	/*
550 	 * Dump out summary information about file system.
551 	 */
552 	if (verbosity > 0) {
553 #define	B2MBFACTOR (1 / (1024.0 * 1024.0))
554 		printf("%s: %.1fMB (%lld sectors) block size %d, "
555 		       "fragment size %d\n",
556 		    fsys, (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR,
557 		    (long long)FFS_FSBTODB(&sblock, sblock.fs_size),
558 		    sblock.fs_bsize, sblock.fs_fsize);
559 		printf("\tusing %d cylinder groups of %.2fMB, %d blks, "
560 		       "%d inodes.\n",
561 		    sblock.fs_ncg,
562 		    (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR,
563 		    sblock.fs_fpg / sblock.fs_frag, sblock.fs_ipg);
564 #undef B2MBFACTOR
565 	}
566 
567 	/*
568 	 * allocate space for superblock, cylinder group map, and
569 	 * two sets of inode blocks.
570 	 */
571 	if (sblock.fs_bsize < SBLOCKSIZE)
572 		iobufsize = SBLOCKSIZE + 3 * sblock.fs_bsize;
573 	else
574 		iobufsize = 4 * sblock.fs_bsize;
575 	iobuf_memsize = iobufsize;
576 	if (!mfs && sblock.fs_magic == FS_UFS1_MAGIC) {
577 		/* A larger buffer so we can write multiple inode blks */
578 		iobuf_memsize += 14 * sblock.fs_bsize;
579 	}
580 	for (;;) {
581 		iobuf = mmap(0, iobuf_memsize, PROT_READ|PROT_WRITE,
582 				MAP_ANON|MAP_PRIVATE, -1, 0);
583 		if (iobuf != MAP_FAILED)
584 			break;
585 		if (iobuf_memsize != iobufsize) {
586 			/* Try again with the smaller size */
587 			iobuf_memsize = iobufsize;
588 			continue;
589 		}
590 		printf("Cannot allocate I/O buffer\n");
591 		exit(38);
592 	}
593 	memset(iobuf, 0, iobuf_memsize);
594 
595 	/*
596 	 * We now start writing to the filesystem
597 	 */
598 
599 	if (!Nflag) {
600 		/*
601 		 * Validate the given file system size.
602 		 * Verify that its last block can actually be accessed.
603 		 * Convert to file system fragment sized units.
604 		 */
605 		if (fssize <= 0) {
606 			printf("preposterous size %lld\n", (long long)fssize);
607 			fserr(13);
608 		}
609 		wtfs(fssize - 1, sectorsize, iobuf);
610 
611 		/*
612 		 * Ensure there is nothing that looks like a filesystem
613 		 * superbock anywhere other than where ours will be.
614 		 * If fsck finds the wrong one all hell breaks loose!
615 		 */
616 		for (i = 0; ; i++) {
617 			static const int sblocklist[] = SBLOCKSEARCH;
618 			int sblkoff = sblocklist[i];
619 			int sz;
620 			if (sblkoff == -1)
621 				break;
622 			/* Remove main superblock */
623 			zap_old_sblock(sblkoff);
624 			/* and all possible locations for the first alternate */
625 			sblkoff += SBLOCKSIZE;
626 			for (sz = SBLOCKSIZE; sz <= 0x10000; sz <<= 1)
627 				zap_old_sblock(roundup(sblkoff, sz));
628 		}
629 		/*
630 		 * Also zap possible Ext2fs magic leftover to prevent
631 		 * kernel vfs_mountroot() and bootloaders from mis-recognizing
632 		 * this file system as Ext2fs.
633 		 */
634 		zap_old_sblock(EXT2FS_SBOFF);
635 
636 #ifndef NO_APPLE_UFS
637 		if (isappleufs) {
638 			struct appleufslabel appleufs __aligned(DEV_BSIZE);
639 			ffs_appleufs_set(&appleufs, appleufs_volname,
640 			    tv.tv_sec, 0);
641 			wtfs(APPLEUFS_LABEL_OFFSET/sectorsize,
642 			    APPLEUFS_LABEL_SIZE, &appleufs);
643 		} else if (APPLEUFS_LABEL_SIZE % sectorsize == 0) {
644 			struct appleufslabel appleufs;
645 			/* Look for & zap any existing valid apple ufs labels */
646 			rdfs(APPLEUFS_LABEL_OFFSET/sectorsize,
647 			    APPLEUFS_LABEL_SIZE, &appleufs);
648 			if (ffs_appleufs_validate(fsys, &appleufs, NULL) == 0) {
649 				memset(&appleufs, 0, sizeof(appleufs));
650 				wtfs(APPLEUFS_LABEL_OFFSET/sectorsize,
651 				    APPLEUFS_LABEL_SIZE, &appleufs);
652 			}
653 		}
654 #endif
655 	}
656 
657 	/*
658 	 * Make a copy of the superblock into the buffer that we will be
659 	 * writing out in each cylinder group.
660 	 */
661 	memcpy(iobuf, &sblock, sizeof sblock);
662 	if (needswap)
663 		ffs_sb_swap(&sblock, (struct fs *)iobuf);
664 	if ((sblock.fs_old_flags & FS_FLAGS_UPDATED) == 0)
665 		memset(iobuf + offsetof(struct fs, fs_old_postbl_start),
666 		    0xff, 256);
667 
668 	if (verbosity >= 3)
669 		printf("super-block backups (for fsck_ffs -b #) at:\n");
670 	/* If we are printing more than one line of numbers, line up columns */
671 	fld_width = verbosity < 4 ? 1 : snprintf(NULL, 0, "%" PRIu64,
672 		(uint64_t)FFS_FSBTODB(&sblock, cgsblock(&sblock, sblock.fs_ncg-1)));
673 	/* Get terminal width */
674 	if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) == 0)
675 		max_cols = winsize.ws_col;
676 	else
677 		max_cols = 80;
678 	if (Nflag && verbosity == 3)
679 		/* Leave space to add " ..." after one row of numbers */
680 		max_cols -= 4;
681 #define BASE 0x10000	/* For some fixed-point maths */
682 	col = 0;
683 	delta = verbosity > 2 ? 0 : max_cols * BASE / sblock.fs_ncg;
684 	for (cylno = 0; cylno < sblock.fs_ncg; cylno++) {
685 		fflush(stdout);
686 		initcg(cylno, &tv);
687 		if (verbosity < 2)
688 			continue;
689 		if (delta > 0) {
690 			if (Nflag)
691 				/* No point doing dots for -N */
692 				break;
693 			/* Print dots scaled to end near RH margin */
694 			for (col += delta; col > BASE; col -= BASE)
695 				printf(".");
696 			continue;
697 		}
698 		/* Print superblock numbers */
699 		len = printf("%s%*" PRIu64 ",", col ? " " : "", fld_width,
700 		    (uint64_t)FFS_FSBTODB(&sblock, cgsblock(&sblock, cylno)));
701 		col += len;
702 		if (col + len < max_cols)
703 			/* Next number fits */
704 			continue;
705 		/* Next number won't fit, need a newline */
706 		if (verbosity <= 3) {
707 			/* Print dots for subsequent cylinder groups */
708 			delta = sblock.fs_ncg - cylno - 1;
709 			if (delta != 0) {
710 				if (Nflag) {
711 					printf(" ...");
712 					break;
713 				}
714 				delta = max_cols * BASE / delta;
715 			}
716 		}
717 		col = 0;
718 		printf("\n");
719 	}
720 #undef BASE
721 	if (col > 0)
722 		printf("\n");
723 	if (Nflag)
724 		exit(0);
725 
726 	/*
727 	 * Now construct the initial file system,
728 	 */
729 	if (fsinit(&tv, mfsmode, mfsuid, mfsgid) == 0 && mfs)
730 		errx(1, "Error making filesystem");
731 	sblock.fs_time = tv.tv_sec;
732 	if (Oflag <= 1) {
733 		sblock.fs_old_cstotal.cs_ndir = sblock.fs_cstotal.cs_ndir;
734 		sblock.fs_old_cstotal.cs_nbfree = sblock.fs_cstotal.cs_nbfree;
735 		sblock.fs_old_cstotal.cs_nifree = sblock.fs_cstotal.cs_nifree;
736 		sblock.fs_old_cstotal.cs_nffree = sblock.fs_cstotal.cs_nffree;
737 	}
738 	/*
739 	 * Write out the super-block and zeros until the first cg info
740 	 */
741 	i = cgsblock(&sblock, 0) * sblock.fs_fsize - sblock.fs_sblockloc;
742 	if ((size_t)i < sizeof(sblock))
743 		errx(1, "No space for superblock");
744 	memcpy(iobuf, &sblock, sizeof(sblock));
745 	memset(iobuf + sizeof(sblock), 0, i - sizeof(sblock));
746 	if (needswap)
747 		ffs_sb_swap(&sblock, (struct fs *)iobuf);
748 	if ((sblock.fs_old_flags & FS_FLAGS_UPDATED) == 0)
749 		memset(iobuf + offsetof(struct fs, fs_old_postbl_start),
750 		    0xff, 256);
751 	wtfs(sblock.fs_sblockloc / sectorsize, i, iobuf);
752 
753 	/* Write out first and last cylinder summary sectors */
754 	if (needswap)
755 		ffs_csum_swap(fscs_0, fscs_0, sblock.fs_fsize);
756 	wtfs(FFS_FSBTODB(&sblock, sblock.fs_csaddr), sblock.fs_fsize, fscs_0);
757 
758 	if (fscs_next > fscs_reset) {
759 		if (needswap)
760 			ffs_csum_swap(fscs_reset, fscs_reset, sblock.fs_fsize);
761 		fs_csaddr++;
762 		wtfs(FFS_FSBTODB(&sblock, fs_csaddr), sblock.fs_fsize, fscs_reset);
763 	}
764 
765 	/* mfs doesn't need these permanently allocated */
766 	munmap(iobuf, iobuf_memsize);
767 	munmap(fscs_0, 2 * sblock.fs_fsize);
768 }
769 
770 /*
771  * Initialize a cylinder group.
772  */
773 void
774 initcg(int cylno, const struct timeval *tv)
775 {
776 	daddr_t cbase, dmax;
777 	int32_t i, d, dlower, dupper, blkno;
778 	uint32_t u;
779 	struct ufs1_dinode *dp1;
780 	struct ufs2_dinode *dp2;
781 	int start;
782 
783 	/*
784 	 * Determine block bounds for cylinder group.
785 	 * Allow space for super block summary information in first
786 	 * cylinder group.
787 	 */
788 	cbase = cgbase(&sblock, cylno);
789 	dmax = cbase + sblock.fs_fpg;
790 	if (dmax > sblock.fs_size)
791 		dmax = sblock.fs_size;
792 	dlower = cgsblock(&sblock, cylno) - cbase;
793 	dupper = cgdmin(&sblock, cylno) - cbase;
794 	if (cylno == 0) {
795 		dupper += howmany(sblock.fs_cssize, sblock.fs_fsize);
796 		if (dupper >= cgstart(&sblock, cylno + 1)) {
797 			printf("\rToo many cylinder groups to fit summary "
798 				"information into first cylinder group\n");
799 			fserr(40);
800 		}
801 	}
802 	memset(&acg, 0, sblock.fs_cgsize);
803 	acg.cg_magic = CG_MAGIC;
804 	acg.cg_cgx = cylno;
805 	acg.cg_ndblk = dmax - cbase;
806 	if (sblock.fs_contigsumsize > 0)
807 		acg.cg_nclusterblks = acg.cg_ndblk >> sblock.fs_fragshift;
808 	start = &acg.cg_space[0] - (u_char *)(&acg.cg_firstfield);
809 	if (Oflag == 2) {
810 		acg.cg_time = tv->tv_sec;
811 		acg.cg_niblk = sblock.fs_ipg;
812 		acg.cg_initediblk = sblock.fs_ipg < 2 * FFS_INOPB(&sblock) ?
813 		    sblock.fs_ipg : 2 * FFS_INOPB(&sblock);
814 		acg.cg_iusedoff = start;
815 	} else {
816 		acg.cg_old_ncyl = sblock.fs_old_cpg;
817 		if ((sblock.fs_old_flags & FS_FLAGS_UPDATED) == 0 &&
818 		    (cylno == sblock.fs_ncg - 1))
819 			acg.cg_old_ncyl =
820 			    sblock.fs_old_ncyl % sblock.fs_old_cpg;
821 		acg.cg_old_time = tv->tv_sec;
822 		acg.cg_old_niblk = sblock.fs_ipg;
823 		acg.cg_old_btotoff = start;
824 		acg.cg_old_boff = acg.cg_old_btotoff +
825 		    sblock.fs_old_cpg * sizeof(int32_t);
826 		acg.cg_iusedoff = acg.cg_old_boff +
827 		    sblock.fs_old_cpg * sizeof(u_int16_t);
828 	}
829 	acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, CHAR_BIT);
830 	if (sblock.fs_contigsumsize <= 0) {
831 		acg.cg_nextfreeoff = acg.cg_freeoff +
832 		   howmany(sblock.fs_fpg, CHAR_BIT);
833 	} else {
834 		acg.cg_clustersumoff = acg.cg_freeoff +
835 		    howmany(sblock.fs_fpg, CHAR_BIT) - sizeof(int32_t);
836 		if (isappleufs) {
837 			/* Apple PR2216969 gives rationale for this change.
838 			 * I believe they were mistaken, but we need to
839 			 * duplicate it for compatibility.  -- dbj@NetBSD.org
840 			 */
841 			acg.cg_clustersumoff += sizeof(int32_t);
842 		}
843 		acg.cg_clustersumoff =
844 		    roundup(acg.cg_clustersumoff, sizeof(int32_t));
845 		acg.cg_clusteroff = acg.cg_clustersumoff +
846 		    (sblock.fs_contigsumsize + 1) * sizeof(int32_t);
847 		acg.cg_nextfreeoff = acg.cg_clusteroff +
848 		    howmany(ffs_fragstoblks(&sblock, sblock.fs_fpg), CHAR_BIT);
849 	}
850 	if (acg.cg_nextfreeoff > sblock.fs_cgsize) {
851 		printf("Panic: cylinder group too big\n");
852 		fserr(37);
853 	}
854 	acg.cg_cs.cs_nifree += sblock.fs_ipg;
855 	if (cylno == 0)
856 		for (u = 0; u < UFS_ROOTINO; u++) {
857 			setbit(cg_inosused(&acg, 0), u);
858 			acg.cg_cs.cs_nifree--;
859 		}
860 	if (cylno > 0) {
861 		/*
862 		 * In cylno 0, beginning space is reserved
863 		 * for boot and super blocks.
864 		 */
865 		for (d = 0, blkno = 0; d < dlower;) {
866 			setblock(&sblock, cg_blksfree(&acg, 0), blkno);
867 			if (sblock.fs_contigsumsize > 0)
868 				setbit(cg_clustersfree(&acg, 0), blkno);
869 			acg.cg_cs.cs_nbfree++;
870 			if (Oflag <= 1) {
871 				int cn = old_cbtocylno(&sblock, d);
872 				old_cg_blktot(&acg, 0)[cn]++;
873 				old_cg_blks(&sblock, &acg,
874 				    cn, 0)[old_cbtorpos(&sblock, d)]++;
875 			}
876 			d += sblock.fs_frag;
877 			blkno++;
878 		}
879 	}
880 	if ((i = (dupper & (sblock.fs_frag - 1))) != 0) {
881 		acg.cg_frsum[sblock.fs_frag - i]++;
882 		for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) {
883 			setbit(cg_blksfree(&acg, 0), dupper);
884 			acg.cg_cs.cs_nffree++;
885 		}
886 	}
887 	for (d = dupper, blkno = dupper >> sblock.fs_fragshift;
888 	     d + sblock.fs_frag <= acg.cg_ndblk; ) {
889 		setblock(&sblock, cg_blksfree(&acg, 0), blkno);
890 		if (sblock.fs_contigsumsize > 0)
891 			setbit(cg_clustersfree(&acg, 0), blkno);
892 		acg.cg_cs.cs_nbfree++;
893 		if (Oflag <= 1) {
894 			int cn = old_cbtocylno(&sblock, d);
895 			old_cg_blktot(&acg, 0)[cn]++;
896 			old_cg_blks(&sblock, &acg,
897 			    cn, 0)[old_cbtorpos(&sblock, d)]++;
898 		}
899 		d += sblock.fs_frag;
900 		blkno++;
901 	}
902 	if (d < acg.cg_ndblk) {
903 		acg.cg_frsum[acg.cg_ndblk - d]++;
904 		for (; d < acg.cg_ndblk; d++) {
905 			setbit(cg_blksfree(&acg, 0), d);
906 			acg.cg_cs.cs_nffree++;
907 		}
908 	}
909 	if (sblock.fs_contigsumsize > 0) {
910 		int32_t *sump = cg_clustersum(&acg, 0);
911 		u_char *mapp = cg_clustersfree(&acg, 0);
912 		int map = *mapp++;
913 		int bit = 1;
914 		int run = 0;
915 
916 		for (i = 0; i < acg.cg_nclusterblks; i++) {
917 			if ((map & bit) != 0) {
918 				run++;
919 			} else if (run != 0) {
920 				if (run > sblock.fs_contigsumsize)
921 					run = sblock.fs_contigsumsize;
922 				sump[run]++;
923 				run = 0;
924 			}
925 			if ((i & (CHAR_BIT - 1)) != (CHAR_BIT - 1)) {
926 				bit <<= 1;
927 			} else {
928 				map = *mapp++;
929 				bit = 1;
930 			}
931 		}
932 		if (run != 0) {
933 			if (run > sblock.fs_contigsumsize)
934 				run = sblock.fs_contigsumsize;
935 			sump[run]++;
936 		}
937 	}
938 	*fscs_next++ = acg.cg_cs;
939 	if (fscs_next == fscs_end) {
940 		/* write block of cylinder group summary info into cyl 0 */
941 		if (needswap)
942 			ffs_csum_swap(fscs_reset, fscs_reset, sblock.fs_fsize);
943 		fs_csaddr++;
944 		wtfs(FFS_FSBTODB(&sblock, fs_csaddr), sblock.fs_fsize, fscs_reset);
945 		fscs_next = fscs_reset;
946 		memset(fscs_next, 0, sblock.fs_fsize);
947 	}
948 	/*
949 	 * Write out the duplicate super block, the cylinder group map
950 	 * and two blocks worth of inodes in a single write.
951 	 */
952 	start = sblock.fs_bsize > SBLOCKSIZE ? sblock.fs_bsize : SBLOCKSIZE;
953 	memcpy(&iobuf[start], &acg, sblock.fs_cgsize);
954 	if (needswap)
955 		ffs_cg_swap(&acg, (struct cg*)&iobuf[start], &sblock);
956 	start += sblock.fs_bsize;
957 	dp1 = (struct ufs1_dinode *)(&iobuf[start]);
958 	dp2 = (struct ufs2_dinode *)(&iobuf[start]);
959 	for (i = MIN(sblock.fs_ipg, 2) * FFS_INOPB(&sblock); i != 0; i--) {
960 		if (sblock.fs_magic == FS_UFS1_MAGIC) {
961 			/* No need to swap, it'll stay random */
962 			dp1->di_gen = arc4random() & INT32_MAX;
963 			dp1++;
964 		} else {
965 			dp2->di_gen = arc4random() & INT32_MAX;
966 			dp2++;
967 		}
968 	}
969 	wtfs(FFS_FSBTODB(&sblock, cgsblock(&sblock, cylno)), iobufsize, iobuf);
970 	/*
971 	 * For the old file system, we have to initialize all the inodes.
972 	 */
973 	if (sblock.fs_magic != FS_UFS1_MAGIC)
974 		return;
975 
976 	/* Write 'd' (usually 16 * fs_frag) file-system fragments at once */
977 	d = (iobuf_memsize - start) / sblock.fs_bsize * sblock.fs_frag;
978 	dupper = sblock.fs_ipg / FFS_INOPF(&sblock);
979 	for (i = 2 * sblock.fs_frag; i < dupper; i += d) {
980 		if (d > dupper - i)
981 			d = dupper - i;
982 		dp1 = (struct ufs1_dinode *)(&iobuf[start]);
983 		do
984 			dp1->di_gen = arc4random() & INT32_MAX;
985 		while ((char *)++dp1 < &iobuf[iobuf_memsize]);
986 		wtfs(FFS_FSBTODB(&sblock, cgimin(&sblock, cylno) + i),
987 		    d * sblock.fs_bsize / sblock.fs_frag, &iobuf[start]);
988 	}
989 }
990 
991 /*
992  * initialize the file system
993  */
994 
995 #ifdef LOSTDIR
996 #define	PREDEFDIR 3
997 #else
998 #define	PREDEFDIR 2
999 #endif
1000 
1001 struct direct root_dir[] = {
1002 	{ UFS_ROOTINO, sizeof(struct direct), DT_DIR, 1, "." },
1003 	{ UFS_ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
1004 #ifdef LOSTDIR
1005 	{ LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 10, "lost+found" },
1006 #endif
1007 };
1008 struct odirect {
1009 	u_int32_t d_ino;
1010 	u_int16_t d_reclen;
1011 	u_int16_t d_namlen;
1012 	u_char	d_name[FFS_MAXNAMLEN + 1];
1013 } oroot_dir[] = {
1014 	{ UFS_ROOTINO, sizeof(struct direct), 1, "." },
1015 	{ UFS_ROOTINO, sizeof(struct direct), 2, ".." },
1016 #ifdef LOSTDIR
1017 	{ LOSTFOUNDINO, sizeof(struct direct), 10, "lost+found" },
1018 #endif
1019 };
1020 #ifdef LOSTDIR
1021 struct direct lost_found_dir[] = {
1022 	{ LOSTFOUNDINO, sizeof(struct direct), DT_DIR, 1, "." },
1023 	{ UFS_ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." },
1024 	{ 0, DIRBLKSIZ, 0, 0, 0 },
1025 };
1026 struct odirect olost_found_dir[] = {
1027 	{ LOSTFOUNDINO, sizeof(struct direct), 1, "." },
1028 	{ UFS_ROOTINO, sizeof(struct direct), 2, ".." },
1029 	{ 0, DIRBLKSIZ, 0, 0 },
1030 };
1031 #endif
1032 
1033 static void copy_dir(struct direct *, struct direct *);
1034 
1035 int
1036 fsinit(const struct timeval *tv, mode_t mfsmode, uid_t mfsuid, gid_t mfsgid)
1037 {
1038 	union dinode node;
1039 	union Buffer buf __aligned(DEV_BSIZE);
1040 	int i;
1041 	int qblocks = 0;
1042 	int qinos = 0;
1043 	uint8_t q2h_hash_shift;
1044 	uint16_t q2h_hash_mask;
1045 #ifdef LOSTDIR
1046 	int dirblksiz = DIRBLKSIZ;
1047 	if (isappleufs)
1048 		dirblksiz = APPLEUFS_DIRBLKSIZ;
1049 	int nextino = LOSTFOUNDINO+1;
1050 #else
1051 	int nextino = UFS_ROOTINO+1;
1052 #endif
1053 
1054 	/*
1055 	 * initialize the node
1056 	 */
1057 
1058 #ifdef LOSTDIR
1059 	/*
1060 	 * create the lost+found directory
1061 	 */
1062 	memset(&node, 0, sizeof(node));
1063 	if (Oflag == 0) {
1064 		(void)makedir(&buf, (struct direct *)olost_found_dir, 2);
1065 		for (i = dirblksiz; i < sblock.fs_bsize; i += dirblksiz)
1066 			copy_dir((struct direct*)&olost_found_dir[2],
1067 				(struct direct*)&buf[i]);
1068 	} else {
1069 		(void)makedir(&buf, lost_found_dir, 2);
1070 		for (i = dirblksiz; i < sblock.fs_bsize; i += dirblksiz)
1071 			copy_dir(&lost_found_dir[2], (struct direct*)&buf[i]);
1072 	}
1073 	if (sblock.fs_magic == FS_UFS1_MAGIC) {
1074 		node.dp1.di_atime = tv->tv_sec;
1075 		node.dp1.di_atimensec = tv->tv_usec * 1000;
1076 		node.dp1.di_mtime = tv->tv_sec;
1077 		node.dp1.di_mtimensec = tv->tv_usec * 1000;
1078 		node.dp1.di_ctime = tv->tv_sec;
1079 		node.dp1.di_ctimensec = tv->tv_usec * 1000;
1080 		node.dp1.di_mode = IFDIR | UMASK;
1081 		node.dp1.di_nlink = 2;
1082 		node.dp1.di_size = sblock.fs_bsize;
1083 		node.dp1.di_db[0] = alloc(node.dp1.di_size, node.dp1.di_mode);
1084 		if (node.dp1.di_db[0] == 0)
1085 			return (0);
1086 		node.dp1.di_blocks = btodb(ffs_fragroundup(&sblock,
1087 		    node.dp1.di_size));
1088 		qblocks += node.dp1.di_blocks;
1089 		node.dp1.di_uid = geteuid();
1090 		node.dp1.di_gid = getegid();
1091 		wtfs(FFS_FSBTODB(&sblock, node.dp1.di_db[0]), node.dp1.di_size,
1092 		    buf);
1093 	} else {
1094 		node.dp2.di_atime = tv->tv_sec;
1095 		node.dp2.di_atimensec = tv->tv_usec * 1000;
1096 		node.dp2.di_mtime = tv->tv_sec;
1097 		node.dp2.di_mtimensec = tv->tv_usec * 1000;
1098 		node.dp2.di_ctime = tv->tv_sec;
1099 		node.dp2.di_ctimensec = tv->tv_usec * 1000;
1100 		node.dp2.di_birthtime = tv->tv_sec;
1101 		node.dp2.di_birthnsec = tv->tv_usec * 1000;
1102 		node.dp2.di_mode = IFDIR | UMASK;
1103 		node.dp2.di_nlink = 2;
1104 		node.dp2.di_size = sblock.fs_bsize;
1105 		node.dp2.di_db[0] = alloc(node.dp2.di_size, node.dp2.di_mode);
1106 		if (node.dp2.di_db[0] == 0)
1107 			return (0);
1108 		node.dp2.di_blocks = btodb(ffs_fragroundup(&sblock,
1109 		    node.dp2.di_size));
1110 		qblocks += node.dp2.di_blocks;
1111 		node.dp2.di_uid = geteuid();
1112 		node.dp2.di_gid = getegid();
1113 		wtfs(FFS_FSBTODB(&sblock, node.dp2.di_db[0]), node.dp2.di_size,
1114 		    buf);
1115 	}
1116 	qinos++;
1117 	iput(&node, LOSTFOUNDINO);
1118 #endif
1119 	/*
1120 	 * create the root directory
1121 	 */
1122 	memset(&node, 0, sizeof(node));
1123 	if (Oflag <= 1) {
1124 		if (mfs) {
1125 			node.dp1.di_mode = IFDIR | mfsmode;
1126 			node.dp1.di_uid = mfsuid;
1127 			node.dp1.di_gid = mfsgid;
1128 		} else {
1129 			node.dp1.di_mode = IFDIR | UMASK;
1130 			node.dp1.di_uid = geteuid();
1131 			node.dp1.di_gid = getegid();
1132 		}
1133 		node.dp1.di_nlink = PREDEFDIR;
1134 		if (Oflag == 0)
1135 			node.dp1.di_size = makedir(&buf,
1136 			    (struct direct *)oroot_dir, PREDEFDIR);
1137 		else
1138 			node.dp1.di_size = makedir(&buf, root_dir, PREDEFDIR);
1139 		node.dp1.di_db[0] = alloc(sblock.fs_fsize, node.dp1.di_mode);
1140 		if (node.dp1.di_db[0] == 0)
1141 			return (0);
1142 		node.dp1.di_blocks = btodb(ffs_fragroundup(&sblock,
1143 		    node.dp1.di_size));
1144 		qblocks += node.dp1.di_blocks;
1145 		wtfs(FFS_FSBTODB(&sblock, node.dp1.di_db[0]), sblock.fs_fsize, &buf);
1146 	} else {
1147 		if (mfs) {
1148 			node.dp2.di_mode = IFDIR | mfsmode;
1149 			node.dp2.di_uid = mfsuid;
1150 			node.dp2.di_gid = mfsgid;
1151 		} else {
1152 			node.dp2.di_mode = IFDIR | UMASK;
1153 			node.dp2.di_uid = geteuid();
1154 			node.dp2.di_gid = getegid();
1155 		}
1156 		node.dp2.di_atime = tv->tv_sec;
1157 		node.dp2.di_atimensec = tv->tv_usec * 1000;
1158 		node.dp2.di_mtime = tv->tv_sec;
1159 		node.dp2.di_mtimensec = tv->tv_usec * 1000;
1160 		node.dp2.di_ctime = tv->tv_sec;
1161 		node.dp2.di_ctimensec = tv->tv_usec * 1000;
1162 		node.dp2.di_birthtime = tv->tv_sec;
1163 		node.dp2.di_birthnsec = tv->tv_usec * 1000;
1164 		node.dp2.di_nlink = PREDEFDIR;
1165 		node.dp2.di_size = makedir(&buf, root_dir, PREDEFDIR);
1166 		node.dp2.di_db[0] = alloc(sblock.fs_fsize, node.dp2.di_mode);
1167 		if (node.dp2.di_db[0] == 0)
1168 			return (0);
1169 		node.dp2.di_blocks = btodb(ffs_fragroundup(&sblock,
1170 		    node.dp2.di_size));
1171 		qblocks += node.dp2.di_blocks;
1172 		wtfs(FFS_FSBTODB(&sblock, node.dp2.di_db[0]), sblock.fs_fsize, &buf);
1173 	}
1174 	qinos++;
1175 	iput(&node, UFS_ROOTINO);
1176 	/*
1177 	 * compute the size of the hash table
1178 	 * We know the smallest block size is 4k, so we can use 2k
1179 	 * for the hash table; as an entry is 8 bytes we can store
1180 	 * 256 entries. So let start q2h_hash_shift at 8
1181 	 */
1182 	for (q2h_hash_shift = 8;
1183 	    q2h_hash_shift < 15;
1184 	    q2h_hash_shift++) {
1185 		if ((sizeof(uint64_t) << (q2h_hash_shift + 1)) +
1186 		    sizeof(struct quota2_header) > (u_int)sblock.fs_bsize)
1187 			break;
1188 	}
1189 	q2h_hash_mask = (1 << q2h_hash_shift) - 1;
1190 	for (i = 0; i < MAXQUOTAS; i++) {
1191 		struct quota2_header *q2h;
1192 		struct quota2_entry *q2e;
1193 		uint64_t offset;
1194 		uid_t uid = (i == USRQUOTA ? geteuid() : getegid());
1195 
1196 		if ((quotas & FS_Q2_DO_TYPE(i)) == 0)
1197 			continue;
1198 		quota2_create_blk0(sblock.fs_bsize, &buf, q2h_hash_shift,
1199 		    i, needswap);
1200 		/* grab an entry from header for root dir */
1201 		q2h = &buf.q2h;
1202 		offset = ufs_rw64(q2h->q2h_free, needswap);
1203 		q2e = (void *)((char *)&buf + offset);
1204 		q2h->q2h_free = q2e->q2e_next;
1205 		memcpy(q2e, &q2h->q2h_defentry, sizeof(*q2e));
1206 		q2e->q2e_uid = ufs_rw32(uid, needswap);
1207 		q2e->q2e_val[QL_BLOCK].q2v_cur = ufs_rw64(qblocks, needswap);
1208 		q2e->q2e_val[QL_FILE].q2v_cur = ufs_rw64(qinos, needswap);
1209 		/* add to the hash entry */
1210 		q2e->q2e_next = q2h->q2h_entries[uid & q2h_hash_mask];
1211 		q2h->q2h_entries[uid & q2h_hash_mask] =
1212 		    ufs_rw64(offset, needswap);
1213 
1214 		memset(&node, 0, sizeof(node));
1215 		if (sblock.fs_magic == FS_UFS1_MAGIC) {
1216 			node.dp1.di_atime = tv->tv_sec;
1217 			node.dp1.di_atimensec = tv->tv_usec * 1000;
1218 			node.dp1.di_mtime = tv->tv_sec;
1219 			node.dp1.di_mtimensec = tv->tv_usec * 1000;
1220 			node.dp1.di_ctime = tv->tv_sec;
1221 			node.dp1.di_ctimensec = tv->tv_usec * 1000;
1222 			node.dp1.di_mode = IFREG;
1223 			node.dp1.di_nlink = 1;
1224 			node.dp1.di_size = sblock.fs_bsize;
1225 			node.dp1.di_db[0] =
1226 			    alloc(node.dp1.di_size, node.dp1.di_mode);
1227 			if (node.dp1.di_db[0] == 0)
1228 				return (0);
1229 			node.dp1.di_blocks = btodb(ffs_fragroundup(&sblock,
1230 			    node.dp1.di_size));
1231 			node.dp1.di_uid = geteuid();
1232 			node.dp1.di_gid = getegid();
1233 			wtfs(FFS_FSBTODB(&sblock, node.dp1.di_db[0]),
1234 			     node.dp1.di_size, &buf);
1235 		} else {
1236 			node.dp2.di_atime = tv->tv_sec;
1237 			node.dp2.di_atimensec = tv->tv_usec * 1000;
1238 			node.dp2.di_mtime = tv->tv_sec;
1239 			node.dp2.di_mtimensec = tv->tv_usec * 1000;
1240 			node.dp2.di_ctime = tv->tv_sec;
1241 			node.dp2.di_ctimensec = tv->tv_usec * 1000;
1242 			node.dp2.di_birthtime = tv->tv_sec;
1243 			node.dp2.di_birthnsec = tv->tv_usec * 1000;
1244 			node.dp2.di_mode = IFREG;
1245 			node.dp2.di_nlink = 1;
1246 			node.dp2.di_size = sblock.fs_bsize;
1247 			node.dp2.di_db[0] =
1248 			    alloc(node.dp2.di_size, node.dp2.di_mode);
1249 			if (node.dp2.di_db[0] == 0)
1250 				return (0);
1251 			node.dp2.di_blocks = btodb(ffs_fragroundup(&sblock,
1252 			    node.dp2.di_size));
1253 			node.dp2.di_uid = geteuid();
1254 			node.dp2.di_gid = getegid();
1255 			wtfs(FFS_FSBTODB(&sblock, node.dp2.di_db[0]),
1256 			    node.dp2.di_size, &buf);
1257 		}
1258 		iput(&node, nextino);
1259 		sblock.fs_quotafile[i] = nextino;
1260 		nextino++;
1261 	}
1262 	return (1);
1263 }
1264 
1265 /*
1266  * construct a set of directory entries in "buf".
1267  * return size of directory.
1268  */
1269 int
1270 makedir(union Buffer *buf, struct direct *protodir, int entries)
1271 {
1272 	char *cp;
1273 	int i, spcleft;
1274 	int dirblksiz = UFS_DIRBLKSIZ;
1275 	if (isappleufs)
1276 		dirblksiz = APPLEUFS_DIRBLKSIZ;
1277 
1278 	memset(buf, 0, dirblksiz);
1279 	spcleft = dirblksiz;
1280 	for (cp = buf->data, i = 0; i < entries - 1; i++) {
1281 		protodir[i].d_reclen = UFS_DIRSIZ(Oflag == 0, &protodir[i], 0);
1282 		copy_dir(&protodir[i], (struct direct*)cp);
1283 		cp += protodir[i].d_reclen;
1284 		spcleft -= protodir[i].d_reclen;
1285 	}
1286 	protodir[i].d_reclen = spcleft;
1287 	copy_dir(&protodir[i], (struct direct*)cp);
1288 	return (dirblksiz);
1289 }
1290 
1291 /*
1292  * allocate a block or frag
1293  */
1294 daddr_t
1295 alloc(int size, int mode)
1296 {
1297 	int i, frag;
1298 	daddr_t d, blkno;
1299 
1300 	rdfs(FFS_FSBTODB(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, &acg);
1301 	/* fs -> host byte order */
1302 	if (needswap)
1303 		ffs_cg_swap(&acg, &acg, &sblock);
1304 	if (acg.cg_magic != CG_MAGIC) {
1305 		printf("cg 0: bad magic number\n");
1306 		return (0);
1307 	}
1308 	if (acg.cg_cs.cs_nbfree == 0) {
1309 		printf("first cylinder group ran out of space\n");
1310 		return (0);
1311 	}
1312 	for (d = 0; d < acg.cg_ndblk; d += sblock.fs_frag)
1313 		if (isblock(&sblock, cg_blksfree(&acg, 0),
1314 		    d >> sblock.fs_fragshift))
1315 			goto goth;
1316 	printf("internal error: can't find block in cyl 0\n");
1317 	return (0);
1318 goth:
1319 	blkno = ffs_fragstoblks(&sblock, d);
1320 	clrblock(&sblock, cg_blksfree(&acg, 0), blkno);
1321 	if (sblock.fs_contigsumsize > 0)
1322 		clrbit(cg_clustersfree(&acg, 0), blkno);
1323 	acg.cg_cs.cs_nbfree--;
1324 	sblock.fs_cstotal.cs_nbfree--;
1325 	fscs_0->cs_nbfree--;
1326 	if (mode & IFDIR) {
1327 		acg.cg_cs.cs_ndir++;
1328 		sblock.fs_cstotal.cs_ndir++;
1329 		fscs_0->cs_ndir++;
1330 	}
1331 	if (Oflag <= 1) {
1332 		int cn = old_cbtocylno(&sblock, d);
1333 		old_cg_blktot(&acg, 0)[cn]--;
1334 		old_cg_blks(&sblock, &acg,
1335 		    cn, 0)[old_cbtorpos(&sblock, d)]--;
1336 	}
1337 	if (size != sblock.fs_bsize) {
1338 		frag = howmany(size, sblock.fs_fsize);
1339 		fscs_0->cs_nffree += sblock.fs_frag - frag;
1340 		sblock.fs_cstotal.cs_nffree += sblock.fs_frag - frag;
1341 		acg.cg_cs.cs_nffree += sblock.fs_frag - frag;
1342 		acg.cg_frsum[sblock.fs_frag - frag]++;
1343 		for (i = frag; i < sblock.fs_frag; i++)
1344 			setbit(cg_blksfree(&acg, 0), d + i);
1345 	}
1346 	/* host -> fs byte order */
1347 	if (needswap)
1348 		ffs_cg_swap(&acg, &acg, &sblock);
1349 	wtfs(FFS_FSBTODB(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, &acg);
1350 	return (d);
1351 }
1352 
1353 /*
1354  * Allocate an inode on the disk
1355  */
1356 static void
1357 iput(union dinode *ip, ino_t ino)
1358 {
1359 	daddr_t d;
1360 	int i;
1361 	struct ufs1_dinode *dp1;
1362 	struct ufs2_dinode *dp2;
1363 
1364 	rdfs(FFS_FSBTODB(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, &acg);
1365 	/* fs -> host byte order */
1366 	if (needswap)
1367 		ffs_cg_swap(&acg, &acg, &sblock);
1368 	if (acg.cg_magic != CG_MAGIC) {
1369 		printf("cg 0: bad magic number\n");
1370 		fserr(31);
1371 	}
1372 	acg.cg_cs.cs_nifree--;
1373 	setbit(cg_inosused(&acg, 0), ino);
1374 	/* host -> fs byte order */
1375 	if (needswap)
1376 		ffs_cg_swap(&acg, &acg, &sblock);
1377 	wtfs(FFS_FSBTODB(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, &acg);
1378 	sblock.fs_cstotal.cs_nifree--;
1379 	fscs_0->cs_nifree--;
1380 	if (ino >= (ino_t)(sblock.fs_ipg * sblock.fs_ncg)) {
1381 		printf("fsinit: inode value out of range (%llu).\n",
1382 		    (unsigned long long)ino);
1383 		fserr(32);
1384 	}
1385 	d = FFS_FSBTODB(&sblock, ino_to_fsba(&sblock, ino));
1386 	rdfs(d, sblock.fs_bsize, (char *)iobuf);
1387 	if (sblock.fs_magic == FS_UFS1_MAGIC) {
1388 		dp1 = (struct ufs1_dinode *)iobuf;
1389 		dp1 += ino_to_fsbo(&sblock, ino);
1390 		if (needswap) {
1391 			ffs_dinode1_swap(&ip->dp1, dp1);
1392 			/* ffs_dinode1_swap() doesn't swap blocks addrs */
1393 			for (i=0; i<UFS_NDADDR; i++)
1394 			    dp1->di_db[i] = bswap32(ip->dp1.di_db[i]);
1395 			for (i=0; i<UFS_NIADDR; i++)
1396 			    dp1->di_ib[i] = bswap32(ip->dp1.di_ib[i]);
1397 		} else
1398 			*dp1 = ip->dp1;
1399 		dp1->di_gen = arc4random() & INT32_MAX;
1400 	} else {
1401 		dp2 = (struct ufs2_dinode *)iobuf;
1402 		dp2 += ino_to_fsbo(&sblock, ino);
1403 		if (needswap) {
1404 			ffs_dinode2_swap(&ip->dp2, dp2);
1405 			for (i=0; i<UFS_NDADDR; i++)
1406 			    dp2->di_db[i] = bswap64(ip->dp2.di_db[i]);
1407 			for (i=0; i<UFS_NIADDR; i++)
1408 			    dp2->di_ib[i] = bswap64(ip->dp2.di_ib[i]);
1409 		} else
1410 			*dp2 = ip->dp2;
1411 		dp2->di_gen = arc4random() & INT32_MAX;
1412 	}
1413 	wtfs(d, sblock.fs_bsize, iobuf);
1414 }
1415 
1416 /*
1417  * read a block from the file system
1418  */
1419 void
1420 rdfs(daddr_t bno, int size, void *bf)
1421 {
1422 	int n;
1423 	off_t offset;
1424 
1425 #ifdef MFS
1426 	if (mfs) {
1427 		if (Nflag)
1428 			memset(bf, 0, size);
1429 		else
1430 			memmove(bf, membase + bno * sectorsize, size);
1431 		return;
1432 	}
1433 #endif
1434 	offset = bno;
1435 	n = pread(fsi, bf, size, offset * sectorsize);
1436 	if (n != size) {
1437 		printf("rdfs: read error for sector %lld: %s\n",
1438 		    (long long)bno, strerror(errno));
1439 		exit(34);
1440 	}
1441 }
1442 
1443 /*
1444  * write a block to the file system
1445  */
1446 void
1447 wtfs(daddr_t bno, int size, void *bf)
1448 {
1449 	int n;
1450 	off_t offset;
1451 
1452 	if (Nflag)
1453 		return;
1454 #ifdef MFS
1455 	if (mfs) {
1456 		memmove(membase + bno * sectorsize, bf, size);
1457 		return;
1458 	}
1459 #endif
1460 	offset = bno;
1461 	n = pwrite(fso, bf, size, offset * sectorsize);
1462 	if (n != size) {
1463 		printf("wtfs: write error for sector %lld: %s\n",
1464 		    (long long)bno, strerror(errno));
1465 		exit(36);
1466 	}
1467 }
1468 
1469 /*
1470  * check if a block is available
1471  */
1472 int
1473 isblock(struct fs *fs, unsigned char *cp, int h)
1474 {
1475 	unsigned char mask;
1476 
1477 	switch (fs->fs_fragshift) {
1478 	case 3:
1479 		return (cp[h] == 0xff);
1480 	case 2:
1481 		mask = 0x0f << ((h & 0x1) << 2);
1482 		return ((cp[h >> 1] & mask) == mask);
1483 	case 1:
1484 		mask = 0x03 << ((h & 0x3) << 1);
1485 		return ((cp[h >> 2] & mask) == mask);
1486 	case 0:
1487 		mask = 0x01 << (h & 0x7);
1488 		return ((cp[h >> 3] & mask) == mask);
1489 	default:
1490 #ifdef STANDALONE
1491 		printf("isblock bad fs_fragshift %d\n", fs->fs_fragshift);
1492 #else
1493 		fprintf(stderr, "isblock bad fs_fragshift %d\n",
1494 		    fs->fs_fragshift);
1495 #endif
1496 		return (0);
1497 	}
1498 }
1499 
1500 /*
1501  * take a block out of the map
1502  */
1503 void
1504 clrblock(struct fs *fs, unsigned char *cp, int h)
1505 {
1506 	switch ((fs)->fs_fragshift) {
1507 	case 3:
1508 		cp[h] = 0;
1509 		return;
1510 	case 2:
1511 		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
1512 		return;
1513 	case 1:
1514 		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
1515 		return;
1516 	case 0:
1517 		cp[h >> 3] &= ~(0x01 << (h & 0x7));
1518 		return;
1519 	default:
1520 #ifdef STANDALONE
1521 		printf("clrblock bad fs_fragshift %d\n", fs->fs_fragshift);
1522 #else
1523 		fprintf(stderr, "clrblock bad fs_fragshift %d\n",
1524 		    fs->fs_fragshift);
1525 #endif
1526 		return;
1527 	}
1528 }
1529 
1530 /*
1531  * put a block into the map
1532  */
1533 void
1534 setblock(struct fs *fs, unsigned char *cp, int h)
1535 {
1536 	switch (fs->fs_fragshift) {
1537 	case 3:
1538 		cp[h] = 0xff;
1539 		return;
1540 	case 2:
1541 		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
1542 		return;
1543 	case 1:
1544 		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
1545 		return;
1546 	case 0:
1547 		cp[h >> 3] |= (0x01 << (h & 0x7));
1548 		return;
1549 	default:
1550 #ifdef STANDALONE
1551 		printf("setblock bad fs_frag %d\n", fs->fs_fragshift);
1552 #else
1553 		fprintf(stderr, "setblock bad fs_fragshift %d\n",
1554 		    fs->fs_fragshift);
1555 #endif
1556 		return;
1557 	}
1558 }
1559 
1560 /* copy a direntry to a buffer, in fs byte order */
1561 static void
1562 copy_dir(struct direct *dir, struct direct *dbuf)
1563 {
1564 	memcpy(dbuf, dir, UFS_DIRSIZ(Oflag == 0, dir, 0));
1565 	if (needswap) {
1566 		dbuf->d_ino = bswap32(dir->d_ino);
1567 		dbuf->d_reclen = bswap16(dir->d_reclen);
1568 		if (Oflag == 0)
1569 			((struct odirect*)dbuf)->d_namlen =
1570 				bswap16(((struct odirect*)dir)->d_namlen);
1571 	}
1572 }
1573 
1574 static int
1575 ilog2(int val)
1576 {
1577 	u_int n;
1578 
1579 	for (n = 0; n < sizeof(n) * CHAR_BIT; n++)
1580 		if (1 << n == val)
1581 			return (n);
1582 	errx(1, "ilog2: %d is not a power of 2", val);
1583 }
1584 
1585 static void
1586 zap_old_sblock(int sblkoff)
1587 {
1588 	static int cg0_data;
1589 	uint32_t oldfs[SBLOCKSIZE / 4] __aligned(DEV_BSIZE);
1590 	static const struct fsm {
1591 		uint32_t	offset;
1592 		uint32_t	magic;
1593 		uint32_t	mask;
1594 	} fs_magics[] = {
1595 		{offsetof(struct fs, fs_magic)/4, FS_UFS1_MAGIC, ~0u},
1596 		{offsetof(struct fs, fs_magic)/4, FS_UFS2_MAGIC, ~0u},
1597 		{0, 0x70162, ~0u},		/* LFS_MAGIC */
1598 		{14, 0xef53, 0xffff},		/* EXT2FS (little) */
1599 		{14, 0xef530000, 0xffff0000},	/* EXT2FS (big) */
1600 		{.offset = ~0u},
1601 	};
1602 	const struct fsm *fsm;
1603 
1604 	if (Nflag)
1605 		return;
1606 
1607 	if (sblkoff == 0)	/* Why did UFS2 add support for this?  sigh. */
1608 		return;
1609 
1610 	if (cg0_data == 0)
1611 		/* For FFSv1 this could include all the inodes. */
1612 		cg0_data = cgsblock(&sblock, 0) * sblock.fs_fsize + iobufsize;
1613 
1614 	/* Ignore anything that is beyond our filesystem */
1615 	if ((sblkoff + SBLOCKSIZE)/sectorsize >= fssize)
1616 		return;
1617 	/* Zero anything inside our filesystem... */
1618 	if (sblkoff >= sblock.fs_sblockloc) {
1619 		/* ...unless we will write that area anyway */
1620 		if (sblkoff >= cg0_data)
1621 			wtfs(sblkoff / sectorsize,
1622 			    roundup(sizeof sblock, sectorsize), iobuf);
1623 		return;
1624 	}
1625 
1626 	/* The sector might contain boot code, so we must validate it */
1627 	rdfs(sblkoff/sectorsize, sizeof oldfs, &oldfs);
1628 	for (fsm = fs_magics; ; fsm++) {
1629 		uint32_t v;
1630 		if (fsm->mask == 0)
1631 			return;
1632 		v = oldfs[fsm->offset];
1633 		if ((v & fsm->mask) == fsm->magic ||
1634 		    (bswap32(v) & fsm->mask) == fsm->magic)
1635 			break;
1636 	}
1637 
1638 	/* Just zap the magic number */
1639 	oldfs[fsm->offset] = 0;
1640 	wtfs(sblkoff/sectorsize, sizeof oldfs, &oldfs);
1641 }
1642 
1643 
1644 #ifdef MFS
1645 /*
1646  * Internal version of malloc that trims the requested size if not enough
1647  * memory is available.
1648  */
1649 static void *
1650 mkfs_malloc(size_t size)
1651 {
1652 	u_long pgsz;
1653 	caddr_t *memory, *extra;
1654 	size_t exsize = 128 * 1024;
1655 
1656 	if (size == 0)
1657 		return (NULL);
1658 
1659 	pgsz = getpagesize() - 1;
1660 	size = (size + pgsz) &~ pgsz;
1661 
1662 	/* try to map requested size */
1663 	memory = mmap(0, size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
1664 	    -1, 0);
1665 	if (memory == MAP_FAILED)
1666 		return NULL;
1667 
1668 	/* try to map something extra */
1669 	extra = mmap(0, exsize, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
1670 	    -1, 0);
1671 	if (extra != MAP_FAILED)
1672 		munmap(extra, exsize);
1673 
1674 	/* if extra memory couldn't be mapped, reduce original request accordingly */
1675 	if (extra == MAP_FAILED) {
1676 		munmap(memory, size);
1677 		size -= exsize;
1678 		memory = mmap(0, size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
1679 		    -1, 0);
1680 		if (memory == MAP_FAILED)
1681 			return NULL;
1682 	}
1683 
1684 	return memory;
1685 }
1686 #endif	/* MFS */
1687