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