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