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