xref: /freebsd/usr.sbin/makefs/ffs.c (revision c321779676d0722f5d0a65c30e8b23b8a6547364)
1 /*	$NetBSD: ffs.c,v 1.45 2011/10/09 22:49:26 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 2001 Wasabi Systems, Inc.
5  * All rights reserved.
6  *
7  * Written by Luke Mewburn for Wasabi Systems, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed for the NetBSD Project by
20  *      Wasabi Systems, Inc.
21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22  *    or promote products derived from this software without specific prior
23  *    written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 /*
38  * Copyright (c) 1982, 1986, 1989, 1993
39  *	The Regents of the University of California.  All rights reserved.
40  *
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
44  * 1. Redistributions of source code must retain the above copyright
45  *    notice, this list of conditions and the following disclaimer.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in the
48  *    documentation and/or other materials provided with the distribution.
49  * 3. Neither the name of the University nor the names of its contributors
50  *    may be used to endorse or promote products derived from this software
51  *    without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63  * SUCH DAMAGE.
64  *
65  *	@(#)ffs_alloc.c	8.19 (Berkeley) 7/13/95
66  */
67 
68 #include <sys/cdefs.h>
69 __FBSDID("$FreeBSD$");
70 
71 #include <sys/param.h>
72 
73 #include <sys/mount.h>
74 
75 #include <assert.h>
76 #include <errno.h>
77 #include <fcntl.h>
78 #include <stdarg.h>
79 #include <stdint.h>
80 #include <stdio.h>
81 #include <stdlib.h>
82 #include <string.h>
83 #include <time.h>
84 #include <unistd.h>
85 #include <util.h>
86 
87 #include "makefs.h"
88 #include "ffs.h"
89 
90 #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS
91 #include <sys/statvfs.h>
92 #endif
93 
94 #include <ufs/ufs/dinode.h>
95 #include <ufs/ufs/dir.h>
96 #include <ufs/ffs/fs.h>
97 
98 
99 #include "ffs/ufs_bswap.h"
100 #include "ffs/ufs_inode.h"
101 #include "ffs/newfs_extern.h"
102 #include "ffs/ffs_extern.h"
103 
104 #undef DIP
105 #define DIP(dp, field) \
106 	((ffs_opts->version == 1) ? \
107 	(dp)->ffs1_din.di_##field : (dp)->ffs2_din.di_##field)
108 
109 /*
110  * Various file system defaults (cribbed from newfs(8)).
111  */
112 #define	DFL_FRAGSIZE		1024		/* fragment size */
113 #define	DFL_BLKSIZE		8192		/* block size */
114 #define	DFL_SECSIZE		512		/* sector size */
115 #define	DFL_CYLSPERGROUP	65536		/* cylinders per group */
116 #define	DFL_FRAGSPERINODE	4		/* fragments per inode */
117 #define	DFL_ROTDELAY		0		/* rotational delay */
118 #define	DFL_NRPOS		1		/* rotational positions */
119 #define	DFL_RPM			3600		/* rpm of disk */
120 #define	DFL_NSECTORS		64		/* # of sectors */
121 #define	DFL_NTRACKS		16		/* # of tracks */
122 
123 
124 typedef struct {
125 	u_char		*buf;		/* buf for directory */
126 	doff_t		size;		/* full size of buf */
127 	doff_t		cur;		/* offset of current entry */
128 } dirbuf_t;
129 
130 
131 static	int	ffs_create_image(const char *, fsinfo_t *);
132 static	void	ffs_dump_fsinfo(fsinfo_t *);
133 static	void	ffs_dump_dirbuf(dirbuf_t *, const char *, int);
134 static	void	ffs_make_dirbuf(dirbuf_t *, const char *, fsnode *, int);
135 static	int	ffs_populate_dir(const char *, fsnode *, fsinfo_t *);
136 static	void	ffs_size_dir(fsnode *, fsinfo_t *);
137 static	void	ffs_validate(const char *, fsnode *, fsinfo_t *);
138 static	void	ffs_write_file(union dinode *, uint32_t, void *, fsinfo_t *);
139 static	void	ffs_write_inode(union dinode *, uint32_t, const fsinfo_t *);
140 static  void	*ffs_build_dinode1(struct ufs1_dinode *, dirbuf_t *, fsnode *,
141 				 fsnode *, fsinfo_t *);
142 static  void	*ffs_build_dinode2(struct ufs2_dinode *, dirbuf_t *, fsnode *,
143 				 fsnode *, fsinfo_t *);
144 
145 
146 
147 	/* publicly visible functions */
148 
149 void
150 ffs_prep_opts(fsinfo_t *fsopts)
151 {
152 	ffs_opt_t *ffs_opts = ecalloc(1, sizeof(*ffs_opts));
153 
154 	const option_t ffs_options[] = {
155 	    { 'b', "bsize", &ffs_opts->bsize, OPT_INT32,
156 	      1, INT_MAX, "block size" },
157 	    { 'f', "fsize", &ffs_opts->fsize, OPT_INT32,
158 	      1, INT_MAX, "fragment size" },
159 	    { 'd', "density", &ffs_opts->density, OPT_INT32,
160 	      1, INT_MAX, "bytes per inode" },
161 	    { 'm', "minfree", &ffs_opts->minfree, OPT_INT32,
162 	      0, 99, "minfree" },
163 	    { 'M', "maxbpg", &ffs_opts->maxbpg, OPT_INT32,
164 	      1, INT_MAX, "max blocks per file in a cg" },
165 	    { 'a', "avgfilesize", &ffs_opts->avgfilesize, OPT_INT32,
166 	      1, INT_MAX, "expected average file size" },
167 	    { 'n', "avgfpdir", &ffs_opts->avgfpdir, OPT_INT32,
168 	      1, INT_MAX, "expected # of files per directory" },
169 	    { 'x', "extent", &ffs_opts->maxbsize, OPT_INT32,
170 	      1, INT_MAX, "maximum # extent size" },
171 	    { 'g', "maxbpcg", &ffs_opts->maxblkspercg, OPT_INT32,
172 	      1, INT_MAX, "max # of blocks per group" },
173 	    { 'v', "version", &ffs_opts->version, OPT_INT32,
174 	      1, 2, "UFS version" },
175 	    { 'o', "optimization", NULL, OPT_STRBUF,
176 	      0, 0, "Optimization (time|space)" },
177 	    { 'l', "label", ffs_opts->label, OPT_STRARRAY,
178 	      1, sizeof(ffs_opts->label), "UFS label" },
179 	    { .name = NULL }
180 	};
181 
182 	ffs_opts->bsize= -1;
183 	ffs_opts->fsize= -1;
184 	ffs_opts->cpg= -1;
185 	ffs_opts->density= -1;
186 	ffs_opts->minfree= -1;
187 	ffs_opts->optimization= -1;
188 	ffs_opts->maxcontig= -1;
189 	ffs_opts->maxbpg= -1;
190 	ffs_opts->avgfilesize= -1;
191 	ffs_opts->avgfpdir= -1;
192 	ffs_opts->version = 1;
193 
194 	fsopts->fs_specific = ffs_opts;
195 	fsopts->fs_options = copy_opts(ffs_options);
196 }
197 
198 void
199 ffs_cleanup_opts(fsinfo_t *fsopts)
200 {
201 	free(fsopts->fs_specific);
202 	free(fsopts->fs_options);
203 }
204 
205 int
206 ffs_parse_opts(const char *option, fsinfo_t *fsopts)
207 {
208 	ffs_opt_t	*ffs_opts = fsopts->fs_specific;
209 	option_t *ffs_options = fsopts->fs_options;
210 	char buf[1024];
211 
212 	int	rv;
213 
214 	assert(option != NULL);
215 	assert(fsopts != NULL);
216 	assert(ffs_opts != NULL);
217 
218 	if (debug & DEBUG_FS_PARSE_OPTS)
219 		printf("ffs_parse_opts: got `%s'\n", option);
220 
221 	rv = set_option(ffs_options, option, buf, sizeof(buf));
222 	if (rv == -1)
223 		return 0;
224 
225 	if (ffs_options[rv].name == NULL)
226 		abort();
227 
228 	switch (ffs_options[rv].letter) {
229 	case 'o':
230 		if (strcmp(buf, "time") == 0) {
231 			ffs_opts->optimization = FS_OPTTIME;
232 		} else if (strcmp(buf, "space") == 0) {
233 			ffs_opts->optimization = FS_OPTSPACE;
234 		} else {
235 			warnx("Invalid optimization `%s'", buf);
236 			return 0;
237 		}
238 		break;
239 	default:
240 		break;
241 	}
242 	return 1;
243 }
244 
245 
246 void
247 ffs_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts)
248 {
249 	struct fs	*superblock;
250 	struct timeval	start;
251 
252 	assert(image != NULL);
253 	assert(dir != NULL);
254 	assert(root != NULL);
255 	assert(fsopts != NULL);
256 
257 	if (debug & DEBUG_FS_MAKEFS)
258 		printf("ffs_makefs: image %s directory %s root %p\n",
259 		    image, dir, root);
260 
261 		/* validate tree and options */
262 	TIMER_START(start);
263 	ffs_validate(dir, root, fsopts);
264 	TIMER_RESULTS(start, "ffs_validate");
265 
266 	printf("Calculated size of `%s': %lld bytes, %lld inodes\n",
267 	    image, (long long)fsopts->size, (long long)fsopts->inodes);
268 
269 		/* create image */
270 	TIMER_START(start);
271 	if (ffs_create_image(image, fsopts) == -1)
272 		errx(1, "Image file `%s' not created.", image);
273 	TIMER_RESULTS(start, "ffs_create_image");
274 
275 	fsopts->curinode = UFS_ROOTINO;
276 
277 	if (debug & DEBUG_FS_MAKEFS)
278 		putchar('\n');
279 
280 		/* populate image */
281 	printf("Populating `%s'\n", image);
282 	TIMER_START(start);
283 	if (! ffs_populate_dir(dir, root, fsopts))
284 		errx(1, "Image file `%s' not populated.", image);
285 	TIMER_RESULTS(start, "ffs_populate_dir");
286 
287 		/* ensure no outstanding buffers remain */
288 	if (debug & DEBUG_FS_MAKEFS)
289 		bcleanup();
290 
291 		/* update various superblock parameters */
292 	superblock = fsopts->superblock;
293 	superblock->fs_fmod = 0;
294 	superblock->fs_old_cstotal.cs_ndir   = superblock->fs_cstotal.cs_ndir;
295 	superblock->fs_old_cstotal.cs_nbfree = superblock->fs_cstotal.cs_nbfree;
296 	superblock->fs_old_cstotal.cs_nifree = superblock->fs_cstotal.cs_nifree;
297 	superblock->fs_old_cstotal.cs_nffree = superblock->fs_cstotal.cs_nffree;
298 
299 		/* write out superblock; image is now complete */
300 	ffs_write_superblock(fsopts->superblock, fsopts);
301 	if (close(fsopts->fd) == -1)
302 		err(1, "Closing `%s'", image);
303 	fsopts->fd = -1;
304 	printf("Image `%s' complete\n", image);
305 }
306 
307 	/* end of public functions */
308 
309 
310 static void
311 ffs_validate(const char *dir, fsnode *root, fsinfo_t *fsopts)
312 {
313 	int32_t	ncg = 1;
314 #if notyet
315 	int32_t	spc, nspf, ncyl, fssize;
316 #endif
317 	ffs_opt_t	*ffs_opts = fsopts->fs_specific;
318 
319 	assert(dir != NULL);
320 	assert(root != NULL);
321 	assert(fsopts != NULL);
322 	assert(ffs_opts != NULL);
323 
324 	if (debug & DEBUG_FS_VALIDATE) {
325 		printf("ffs_validate: before defaults set:\n");
326 		ffs_dump_fsinfo(fsopts);
327 	}
328 
329 		/* set FFS defaults */
330 	if (fsopts->sectorsize == -1)
331 		fsopts->sectorsize = DFL_SECSIZE;
332 	if (ffs_opts->fsize == -1)
333 		ffs_opts->fsize = MAX(DFL_FRAGSIZE, fsopts->sectorsize);
334 	if (ffs_opts->bsize == -1)
335 		ffs_opts->bsize = MIN(DFL_BLKSIZE, 8 * ffs_opts->fsize);
336 	if (ffs_opts->cpg == -1)
337 		ffs_opts->cpg = DFL_CYLSPERGROUP;
338 	else
339 		ffs_opts->cpgflg = 1;
340 				/* fsopts->density is set below */
341 	if (ffs_opts->nsectors == -1)
342 		ffs_opts->nsectors = DFL_NSECTORS;
343 	if (ffs_opts->minfree == -1)
344 		ffs_opts->minfree = MINFREE;
345 	if (ffs_opts->optimization == -1)
346 		ffs_opts->optimization = DEFAULTOPT;
347 	if (ffs_opts->maxcontig == -1)
348 		ffs_opts->maxcontig =
349 		    MAX(1, MIN(MAXPHYS, FFS_MAXBSIZE) / ffs_opts->bsize);
350 	/* XXX ondisk32 */
351 	if (ffs_opts->maxbpg == -1)
352 		ffs_opts->maxbpg = ffs_opts->bsize / sizeof(int32_t);
353 	if (ffs_opts->avgfilesize == -1)
354 		ffs_opts->avgfilesize = AVFILESIZ;
355 	if (ffs_opts->avgfpdir == -1)
356 		ffs_opts->avgfpdir = AFPDIR;
357 
358 	if (fsopts->maxsize > 0 &&
359 	    roundup(fsopts->minsize, ffs_opts->bsize) > fsopts->maxsize)
360 		errx(1, "`%s' minsize of %lld rounded up to ffs bsize of %d "
361 		    "exceeds maxsize %lld.  Lower bsize, or round the minimum "
362 		    "and maximum sizes to bsize.", dir,
363 		    (long long)fsopts->minsize, ffs_opts->bsize,
364 		    (long long)fsopts->maxsize);
365 
366 		/* calculate size of tree */
367 	ffs_size_dir(root, fsopts);
368 	fsopts->inodes += UFS_ROOTINO;		/* include first two inodes */
369 
370 	if (debug & DEBUG_FS_VALIDATE)
371 		printf("ffs_validate: size of tree: %lld bytes, %lld inodes\n",
372 		    (long long)fsopts->size, (long long)fsopts->inodes);
373 
374 		/* add requested slop */
375 	fsopts->size += fsopts->freeblocks;
376 	fsopts->inodes += fsopts->freefiles;
377 	if (fsopts->freefilepc > 0)
378 		fsopts->inodes =
379 		    fsopts->inodes * (100 + fsopts->freefilepc) / 100;
380 	if (fsopts->freeblockpc > 0)
381 		fsopts->size =
382 		    fsopts->size * (100 + fsopts->freeblockpc) / 100;
383 
384 		/* add space needed for superblocks */
385 	/*
386 	 * The old SBOFF (SBLOCK_UFS1) is used here because makefs is
387 	 * typically used for small filesystems where space matters.
388 	 * XXX make this an option.
389 	 */
390 	fsopts->size += (SBLOCK_UFS1 + SBLOCKSIZE) * ncg;
391 		/* add space needed to store inodes, x3 for blockmaps, etc */
392 	if (ffs_opts->version == 1)
393 		fsopts->size += ncg * DINODE1_SIZE *
394 		    roundup(fsopts->inodes / ncg,
395 			ffs_opts->bsize / DINODE1_SIZE);
396 	else
397 		fsopts->size += ncg * DINODE2_SIZE *
398 		    roundup(fsopts->inodes / ncg,
399 			ffs_opts->bsize / DINODE2_SIZE);
400 
401 		/* add minfree */
402 	if (ffs_opts->minfree > 0)
403 		fsopts->size =
404 		    fsopts->size * (100 + ffs_opts->minfree) / 100;
405 	/*
406 	 * XXX	any other fs slop to add, such as csum's, bitmaps, etc ??
407 	 */
408 
409 	if (fsopts->size < fsopts->minsize)	/* ensure meets minimum size */
410 		fsopts->size = fsopts->minsize;
411 
412 		/* round up to the next block */
413 	fsopts->size = roundup(fsopts->size, ffs_opts->bsize);
414 
415 		/* round up to requested block size, if any */
416 	if (fsopts->roundup > 0)
417 		fsopts->size = roundup(fsopts->size, fsopts->roundup);
418 
419 		/* calculate density if necessary */
420 	if (ffs_opts->density == -1)
421 		ffs_opts->density = fsopts->size / fsopts->inodes + 1;
422 
423 	if (debug & DEBUG_FS_VALIDATE) {
424 		printf("ffs_validate: after defaults set:\n");
425 		ffs_dump_fsinfo(fsopts);
426 		printf("ffs_validate: dir %s; %lld bytes, %lld inodes\n",
427 		    dir, (long long)fsopts->size, (long long)fsopts->inodes);
428 	}
429 		/* now check calculated sizes vs requested sizes */
430 	if (fsopts->maxsize > 0 && fsopts->size > fsopts->maxsize) {
431 		errx(1, "`%s' size of %lld is larger than the maxsize of %lld.",
432 		    dir, (long long)fsopts->size, (long long)fsopts->maxsize);
433 	}
434 }
435 
436 
437 static void
438 ffs_dump_fsinfo(fsinfo_t *f)
439 {
440 
441 	ffs_opt_t	*fs = f->fs_specific;
442 
443 	printf("fsopts at %p\n", f);
444 
445 	printf("\tsize %lld, inodes %lld, curinode %u\n",
446 	    (long long)f->size, (long long)f->inodes, f->curinode);
447 
448 	printf("\tminsize %lld, maxsize %lld\n",
449 	    (long long)f->minsize, (long long)f->maxsize);
450 	printf("\tfree files %lld, freefile %% %d\n",
451 	    (long long)f->freefiles, f->freefilepc);
452 	printf("\tfree blocks %lld, freeblock %% %d\n",
453 	    (long long)f->freeblocks, f->freeblockpc);
454 	printf("\tneedswap %d, sectorsize %d\n", f->needswap, f->sectorsize);
455 
456 	printf("\tbsize %d, fsize %d, cpg %d, density %d\n",
457 	    fs->bsize, fs->fsize, fs->cpg, fs->density);
458 	printf("\tnsectors %d, rpm %d, minfree %d\n",
459 	    fs->nsectors, fs->rpm, fs->minfree);
460 	printf("\tmaxcontig %d, maxbpg %d\n",
461 	    fs->maxcontig, fs->maxbpg);
462 	printf("\toptimization %s\n",
463 	    fs->optimization == FS_OPTSPACE ? "space" : "time");
464 }
465 
466 
467 static int
468 ffs_create_image(const char *image, fsinfo_t *fsopts)
469 {
470 #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS
471 	struct statvfs	sfs;
472 #endif
473 	struct fs	*fs;
474 	char	*buf;
475 	int	i, bufsize;
476 	off_t	bufrem;
477 	time_t	tstamp;
478 
479 	assert (image != NULL);
480 	assert (fsopts != NULL);
481 
482 		/* create image */
483 	if ((fsopts->fd = open(image, O_RDWR | O_CREAT | O_TRUNC, 0666))
484 	    == -1) {
485 		warn("Can't open `%s' for writing", image);
486 		return (-1);
487 	}
488 
489 		/* zero image */
490 #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS
491 	if (fstatvfs(fsopts->fd, &sfs) == -1) {
492 #endif
493 		bufsize = 8192;
494 #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS
495 		warn("can't fstatvfs `%s', using default %d byte chunk",
496 		    image, bufsize);
497 	} else
498 		bufsize = sfs.f_iosize;
499 #endif
500 	bufrem = fsopts->size;
501 	if (fsopts->sparse) {
502 		if (ftruncate(fsopts->fd, bufrem) == -1) {
503 			warn("sparse option disabled.");
504 			fsopts->sparse = 0;
505 		}
506 	}
507 	if (fsopts->sparse) {
508 		/* File truncated at bufrem. Remaining is 0 */
509 		bufrem = 0;
510 		buf = NULL;
511 	} else {
512 		if (debug & DEBUG_FS_CREATE_IMAGE)
513 			printf("zero-ing image `%s', %lld sectors, "
514 			    "using %d byte chunks\n", image, (long long)bufrem,
515 			    bufsize);
516 		buf = ecalloc(1, bufsize);
517 	}
518 	while (bufrem > 0) {
519 		i = write(fsopts->fd, buf, MIN(bufsize, bufrem));
520 		if (i == -1) {
521 			warn("zeroing image, %lld bytes to go",
522 			    (long long)bufrem);
523 			free(buf);
524 			return (-1);
525 		}
526 		bufrem -= i;
527 	}
528 	if (buf)
529 		free(buf);
530 
531 		/* make the file system */
532 	if (debug & DEBUG_FS_CREATE_IMAGE)
533 		printf("calling mkfs(\"%s\", ...)\n", image);
534 
535 	if (stampst.st_ino != 0)
536 		tstamp = stampst.st_ctime;
537 	else
538 		tstamp = start_time.tv_sec;
539 
540 	srandom(tstamp);
541 
542 	fs = ffs_mkfs(image, fsopts, tstamp);
543 	fsopts->superblock = (void *)fs;
544 	if (debug & DEBUG_FS_CREATE_IMAGE) {
545 		time_t t;
546 
547 		t = (time_t)((struct fs *)fsopts->superblock)->fs_time;
548 		printf("mkfs returned %p; fs_time %s",
549 		    fsopts->superblock, ctime(&t));
550 		printf("fs totals: nbfree %lld, nffree %lld, nifree %lld, ndir %lld\n",
551 		    (long long)fs->fs_cstotal.cs_nbfree,
552 		    (long long)fs->fs_cstotal.cs_nffree,
553 		    (long long)fs->fs_cstotal.cs_nifree,
554 		    (long long)fs->fs_cstotal.cs_ndir);
555 	}
556 
557 	if (fs->fs_cstotal.cs_nifree + UFS_ROOTINO < fsopts->inodes) {
558 		warnx(
559 		"Image file `%s' has %lld free inodes; %lld are required.",
560 		    image,
561 		    (long long)(fs->fs_cstotal.cs_nifree + UFS_ROOTINO),
562 		    (long long)fsopts->inodes);
563 		return (-1);
564 	}
565 	return (fsopts->fd);
566 }
567 
568 
569 static void
570 ffs_size_dir(fsnode *root, fsinfo_t *fsopts)
571 {
572 	struct direct	tmpdir;
573 	fsnode *	node;
574 	int		curdirsize, this;
575 	ffs_opt_t	*ffs_opts = fsopts->fs_specific;
576 
577 	/* node may be NULL (empty directory) */
578 	assert(fsopts != NULL);
579 	assert(ffs_opts != NULL);
580 
581 	if (debug & DEBUG_FS_SIZE_DIR)
582 		printf("ffs_size_dir: entry: bytes %lld inodes %lld\n",
583 		    (long long)fsopts->size, (long long)fsopts->inodes);
584 
585 #define	ADDDIRENT(e) do {						\
586 	tmpdir.d_namlen = strlen((e));					\
587 	this = DIRSIZ_SWAP(0, &tmpdir, 0);					\
588 	if (debug & DEBUG_FS_SIZE_DIR_ADD_DIRENT)			\
589 		printf("ADDDIRENT: was: %s (%d) this %d cur %d\n",	\
590 		    e, tmpdir.d_namlen, this, curdirsize);		\
591 	if (this + curdirsize > roundup(curdirsize, DIRBLKSIZ))		\
592 		curdirsize = roundup(curdirsize, DIRBLKSIZ);		\
593 	curdirsize += this;						\
594 	if (debug & DEBUG_FS_SIZE_DIR_ADD_DIRENT)			\
595 		printf("ADDDIRENT: now: %s (%d) this %d cur %d\n",	\
596 		    e, tmpdir.d_namlen, this, curdirsize);		\
597 } while (0);
598 
599 	/*
600 	 * XXX	this needs to take into account extra space consumed
601 	 *	by indirect blocks, etc.
602 	 */
603 #define	ADDSIZE(x) do {							\
604 	fsopts->size += roundup((x), ffs_opts->fsize);			\
605 } while (0);
606 
607 	curdirsize = 0;
608 	for (node = root; node != NULL; node = node->next) {
609 		ADDDIRENT(node->name);
610 		if (node == root) {			/* we're at "." */
611 			assert(strcmp(node->name, ".") == 0);
612 			ADDDIRENT("..");
613 		} else if ((node->inode->flags & FI_SIZED) == 0) {
614 				/* don't count duplicate names */
615 			node->inode->flags |= FI_SIZED;
616 			if (debug & DEBUG_FS_SIZE_DIR_NODE)
617 				printf("ffs_size_dir: `%s' size %lld\n",
618 				    node->name,
619 				    (long long)node->inode->st.st_size);
620 			fsopts->inodes++;
621 			if (node->type == S_IFREG)
622 				ADDSIZE(node->inode->st.st_size);
623 			if (node->type == S_IFLNK) {
624 				int	slen;
625 
626 				slen = strlen(node->symlink) + 1;
627 				if (slen >= (ffs_opts->version == 1 ?
628 						UFS1_MAXSYMLINKLEN :
629 						UFS2_MAXSYMLINKLEN))
630 					ADDSIZE(slen);
631 			}
632 		}
633 		if (node->type == S_IFDIR)
634 			ffs_size_dir(node->child, fsopts);
635 	}
636 	ADDSIZE(curdirsize);
637 
638 	if (debug & DEBUG_FS_SIZE_DIR)
639 		printf("ffs_size_dir: exit: size %lld inodes %lld\n",
640 		    (long long)fsopts->size, (long long)fsopts->inodes);
641 }
642 
643 static void *
644 ffs_build_dinode1(struct ufs1_dinode *dinp, dirbuf_t *dbufp, fsnode *cur,
645 		 fsnode *root, fsinfo_t *fsopts)
646 {
647 	int slen;
648 	void *membuf;
649 	struct stat *st = stampst.st_ino != 0 ? &stampst : &cur->inode->st;
650 
651 	memset(dinp, 0, sizeof(*dinp));
652 	dinp->di_mode = cur->inode->st.st_mode;
653 	dinp->di_nlink = cur->inode->nlink;
654 	dinp->di_size = cur->inode->st.st_size;
655 #if HAVE_STRUCT_STAT_ST_FLAGS
656 	dinp->di_flags = cur->inode->st.st_flags;
657 #endif
658 	dinp->di_gen = random();
659 	dinp->di_uid = cur->inode->st.st_uid;
660 	dinp->di_gid = cur->inode->st.st_gid;
661 
662 	dinp->di_atime = st->st_atime;
663 	dinp->di_mtime = st->st_mtime;
664 	dinp->di_ctime = st->st_ctime;
665 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
666 	dinp->di_atimensec = st->st_atimensec;
667 	dinp->di_mtimensec = st->st_mtimensec;
668 	dinp->di_ctimensec = st->st_ctimensec;
669 #endif
670 		/* not set: di_db, di_ib, di_blocks, di_spare */
671 
672 	membuf = NULL;
673 	if (cur == root) {			/* "."; write dirbuf */
674 		membuf = dbufp->buf;
675 		dinp->di_size = dbufp->size;
676 	} else if (S_ISBLK(cur->type) || S_ISCHR(cur->type)) {
677 		dinp->di_size = 0;	/* a device */
678 		dinp->di_rdev =
679 		    ufs_rw32(cur->inode->st.st_rdev, fsopts->needswap);
680 	} else if (S_ISLNK(cur->type)) {	/* symlink */
681 		slen = strlen(cur->symlink);
682 		if (slen < UFS1_MAXSYMLINKLEN) {	/* short link */
683 			memcpy(dinp->di_db, cur->symlink, slen);
684 		} else
685 			membuf = cur->symlink;
686 		dinp->di_size = slen;
687 	}
688 	return membuf;
689 }
690 
691 static void *
692 ffs_build_dinode2(struct ufs2_dinode *dinp, dirbuf_t *dbufp, fsnode *cur,
693 		 fsnode *root, fsinfo_t *fsopts)
694 {
695 	int slen;
696 	void *membuf;
697 	struct stat *st = stampst.st_ino != 0 ? &stampst : &cur->inode->st;
698 
699 	memset(dinp, 0, sizeof(*dinp));
700 	dinp->di_mode = cur->inode->st.st_mode;
701 	dinp->di_nlink = cur->inode->nlink;
702 	dinp->di_size = cur->inode->st.st_size;
703 #if HAVE_STRUCT_STAT_ST_FLAGS
704 	dinp->di_flags = cur->inode->st.st_flags;
705 #endif
706 	dinp->di_gen = random();
707 	dinp->di_uid = cur->inode->st.st_uid;
708 	dinp->di_gid = cur->inode->st.st_gid;
709 
710 	dinp->di_atime = st->st_atime;
711 	dinp->di_mtime = st->st_mtime;
712 	dinp->di_ctime = st->st_ctime;
713 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
714 	dinp->di_atimensec = st->st_atimensec;
715 	dinp->di_mtimensec = st->st_mtimensec;
716 	dinp->di_ctimensec = st->st_ctimensec;
717 #endif
718 #if HAVE_STRUCT_STAT_BIRTHTIME
719 	dinp->di_birthtime = st->st_birthtime;
720 	dinp->di_birthnsec = st->st_birthtimensec;
721 #endif
722 		/* not set: di_db, di_ib, di_blocks, di_spare */
723 
724 	membuf = NULL;
725 	if (cur == root) {			/* "."; write dirbuf */
726 		membuf = dbufp->buf;
727 		dinp->di_size = dbufp->size;
728 	} else if (S_ISBLK(cur->type) || S_ISCHR(cur->type)) {
729 		dinp->di_size = 0;	/* a device */
730 		dinp->di_rdev =
731 		    ufs_rw64(cur->inode->st.st_rdev, fsopts->needswap);
732 	} else if (S_ISLNK(cur->type)) {	/* symlink */
733 		slen = strlen(cur->symlink);
734 		if (slen < UFS2_MAXSYMLINKLEN) {	/* short link */
735 			memcpy(dinp->di_db, cur->symlink, slen);
736 		} else
737 			membuf = cur->symlink;
738 		dinp->di_size = slen;
739 	}
740 	return membuf;
741 }
742 
743 static int
744 ffs_populate_dir(const char *dir, fsnode *root, fsinfo_t *fsopts)
745 {
746 	fsnode		*cur;
747 	dirbuf_t	dirbuf;
748 	union dinode	din;
749 	void		*membuf;
750 	char		path[MAXPATHLEN + 1];
751 	ffs_opt_t	*ffs_opts = fsopts->fs_specific;
752 
753 	assert(dir != NULL);
754 	assert(root != NULL);
755 	assert(fsopts != NULL);
756 	assert(ffs_opts != NULL);
757 
758 	(void)memset(&dirbuf, 0, sizeof(dirbuf));
759 
760 	if (debug & DEBUG_FS_POPULATE)
761 		printf("ffs_populate_dir: PASS 1  dir %s node %p\n", dir, root);
762 
763 		/*
764 		 * pass 1: allocate inode numbers, build directory `file'
765 		 */
766 	for (cur = root; cur != NULL; cur = cur->next) {
767 		if ((cur->inode->flags & FI_ALLOCATED) == 0) {
768 			cur->inode->flags |= FI_ALLOCATED;
769 			if (cur == root && cur->parent != NULL)
770 				cur->inode->ino = cur->parent->inode->ino;
771 			else {
772 				cur->inode->ino = fsopts->curinode;
773 				fsopts->curinode++;
774 			}
775 		}
776 		ffs_make_dirbuf(&dirbuf, cur->name, cur, fsopts->needswap);
777 		if (cur == root) {		/* we're at "."; add ".." */
778 			ffs_make_dirbuf(&dirbuf, "..",
779 			    cur->parent == NULL ? cur : cur->parent->first,
780 			    fsopts->needswap);
781 			root->inode->nlink++;	/* count my parent's link */
782 		} else if (cur->child != NULL)
783 			root->inode->nlink++;	/* count my child's link */
784 
785 		/*
786 		 * XXX	possibly write file and long symlinks here,
787 		 *	ensuring that blocks get written before inodes?
788 		 *	otoh, this isn't a real filesystem, so who
789 		 *	cares about ordering? :-)
790 		 */
791 	}
792 	if (debug & DEBUG_FS_POPULATE_DIRBUF)
793 		ffs_dump_dirbuf(&dirbuf, dir, fsopts->needswap);
794 
795 		/*
796 		 * pass 2: write out dirbuf, then non-directories at this level
797 		 */
798 	if (debug & DEBUG_FS_POPULATE)
799 		printf("ffs_populate_dir: PASS 2  dir %s\n", dir);
800 	for (cur = root; cur != NULL; cur = cur->next) {
801 		if (cur->inode->flags & FI_WRITTEN)
802 			continue;		/* skip hard-linked entries */
803 		cur->inode->flags |= FI_WRITTEN;
804 
805 		if (cur->contents == NULL) {
806 			if (snprintf(path, sizeof(path), "%s/%s/%s", cur->root,
807 			    cur->path, cur->name) >= (int)sizeof(path))
808 				errx(1, "Pathname too long.");
809 		}
810 
811 		if (cur->child != NULL)
812 			continue;		/* child creates own inode */
813 
814 				/* build on-disk inode */
815 		if (ffs_opts->version == 1)
816 			membuf = ffs_build_dinode1(&din.ffs1_din, &dirbuf, cur,
817 			    root, fsopts);
818 		else
819 			membuf = ffs_build_dinode2(&din.ffs2_din, &dirbuf, cur,
820 			    root, fsopts);
821 
822 		if (debug & DEBUG_FS_POPULATE_NODE) {
823 			printf("ffs_populate_dir: writing ino %d, %s",
824 			    cur->inode->ino, inode_type(cur->type));
825 			if (cur->inode->nlink > 1)
826 				printf(", nlink %d", cur->inode->nlink);
827 			putchar('\n');
828 		}
829 
830 		if (membuf != NULL) {
831 			ffs_write_file(&din, cur->inode->ino, membuf, fsopts);
832 		} else if (S_ISREG(cur->type)) {
833 			ffs_write_file(&din, cur->inode->ino,
834 			    (cur->contents) ?  cur->contents : path, fsopts);
835 		} else {
836 			assert (! S_ISDIR(cur->type));
837 			ffs_write_inode(&din, cur->inode->ino, fsopts);
838 		}
839 	}
840 
841 		/*
842 		 * pass 3: write out sub-directories
843 		 */
844 	if (debug & DEBUG_FS_POPULATE)
845 		printf("ffs_populate_dir: PASS 3  dir %s\n", dir);
846 	for (cur = root; cur != NULL; cur = cur->next) {
847 		if (cur->child == NULL)
848 			continue;
849 		if (snprintf(path, sizeof(path), "%s/%s", dir, cur->name)
850 		    >= sizeof(path))
851 			errx(1, "Pathname too long.");
852 		if (! ffs_populate_dir(path, cur->child, fsopts))
853 			return (0);
854 	}
855 
856 	if (debug & DEBUG_FS_POPULATE)
857 		printf("ffs_populate_dir: DONE dir %s\n", dir);
858 
859 		/* cleanup */
860 	if (dirbuf.buf != NULL)
861 		free(dirbuf.buf);
862 	return (1);
863 }
864 
865 
866 static void
867 ffs_write_file(union dinode *din, uint32_t ino, void *buf, fsinfo_t *fsopts)
868 {
869 	int 	isfile, ffd;
870 	char	*fbuf, *p;
871 	off_t	bufleft, chunk, offset;
872 	ssize_t nread;
873 	struct inode	in;
874 	struct buf *	bp;
875 	ffs_opt_t	*ffs_opts = fsopts->fs_specific;
876 
877 	assert (din != NULL);
878 	assert (buf != NULL);
879 	assert (fsopts != NULL);
880 	assert (ffs_opts != NULL);
881 
882 	isfile = S_ISREG(DIP(din, mode));
883 	fbuf = NULL;
884 	ffd = -1;
885 	p = NULL;
886 
887 	in.i_fs = (struct fs *)fsopts->superblock;
888 
889 	if (debug & DEBUG_FS_WRITE_FILE) {
890 		printf(
891 		    "ffs_write_file: ino %u, din %p, isfile %d, %s, size %lld",
892 		    ino, din, isfile, inode_type(DIP(din, mode) & S_IFMT),
893 		    (long long)DIP(din, size));
894 		if (isfile)
895 			printf(", file '%s'\n", (char *)buf);
896 		else
897 			printf(", buffer %p\n", buf);
898 	}
899 
900 	in.i_number = ino;
901 	in.i_size = DIP(din, size);
902 	if (ffs_opts->version == 1)
903 		memcpy(&in.i_din.ffs1_din, &din->ffs1_din,
904 		    sizeof(in.i_din.ffs1_din));
905 	else
906 		memcpy(&in.i_din.ffs2_din, &din->ffs2_din,
907 		    sizeof(in.i_din.ffs2_din));
908 	in.i_fd = fsopts->fd;
909 
910 	if (DIP(din, size) == 0)
911 		goto write_inode_and_leave;		/* mmm, cheating */
912 
913 	if (isfile) {
914 		fbuf = emalloc(ffs_opts->bsize);
915 		if ((ffd = open((char *)buf, O_RDONLY, 0444)) == -1) {
916 			warn("Can't open `%s' for reading", (char *)buf);
917 			goto leave_ffs_write_file;
918 		}
919 	} else {
920 		p = buf;
921 	}
922 
923 	chunk = 0;
924 	for (bufleft = DIP(din, size); bufleft > 0; bufleft -= chunk) {
925 		chunk = MIN(bufleft, ffs_opts->bsize);
926 		if (!isfile)
927 			;
928 		else if ((nread = read(ffd, fbuf, chunk)) == -1)
929 			err(EXIT_FAILURE, "Reading `%s', %lld bytes to go",
930 			    (char *)buf, (long long)bufleft);
931 		else if (nread != chunk)
932 			errx(EXIT_FAILURE, "Reading `%s', %lld bytes to go, "
933 			    "read %zd bytes, expected %ju bytes, does "
934 			    "metalog size= attribute mismatch source size?",
935 			    (char *)buf, (long long)bufleft, nread,
936 			    (uintmax_t)chunk);
937 		else
938 			p = fbuf;
939 		offset = DIP(din, size) - bufleft;
940 		if (debug & DEBUG_FS_WRITE_FILE_BLOCK)
941 			printf(
942 		"ffs_write_file: write %p offset %lld size %lld left %lld\n",
943 			    p, (long long)offset,
944 			    (long long)chunk, (long long)bufleft);
945 	/*
946 	 * XXX	if holey support is desired, do the check here
947 	 *
948 	 * XXX	might need to write out last bit in fragroundup
949 	 *	sized chunk. however, ffs_balloc() handles this for us
950 	 */
951 		errno = ffs_balloc(&in, offset, chunk, &bp);
952  bad_ffs_write_file:
953 		if (errno != 0)
954 			err(1,
955 			    "Writing inode %d (%s), bytes %lld + %lld",
956 			    ino,
957 			    isfile ? (char *)buf :
958 			      inode_type(DIP(din, mode) & S_IFMT),
959 			    (long long)offset, (long long)chunk);
960 		memcpy(bp->b_data, p, chunk);
961 		errno = bwrite(bp);
962 		if (errno != 0)
963 			goto bad_ffs_write_file;
964 		brelse(bp, 0);
965 		if (!isfile)
966 			p += chunk;
967 	}
968 
969  write_inode_and_leave:
970 	ffs_write_inode(&in.i_din, in.i_number, fsopts);
971 
972  leave_ffs_write_file:
973 	if (fbuf)
974 		free(fbuf);
975 	if (ffd != -1)
976 		close(ffd);
977 }
978 
979 
980 static void
981 ffs_dump_dirbuf(dirbuf_t *dbuf, const char *dir, int needswap)
982 {
983 	doff_t		i;
984 	struct direct	*de;
985 	uint16_t	reclen;
986 
987 	assert (dbuf != NULL);
988 	assert (dir != NULL);
989 	printf("ffs_dump_dirbuf: dir %s size %d cur %d\n",
990 	    dir, dbuf->size, dbuf->cur);
991 
992 	for (i = 0; i < dbuf->size; ) {
993 		de = (struct direct *)(dbuf->buf + i);
994 		reclen = ufs_rw16(de->d_reclen, needswap);
995 		printf(
996 	    " inode %4d %7s offset %4d reclen %3d namlen %3d name %s\n",
997 		    ufs_rw32(de->d_ino, needswap),
998 		    inode_type(DTTOIF(de->d_type)), i, reclen,
999 		    de->d_namlen, de->d_name);
1000 		i += reclen;
1001 		assert(reclen > 0);
1002 	}
1003 }
1004 
1005 static void
1006 ffs_make_dirbuf(dirbuf_t *dbuf, const char *name, fsnode *node, int needswap)
1007 {
1008 	struct direct	de, *dp;
1009 	uint16_t	llen, reclen;
1010 	u_char		*newbuf;
1011 
1012 	assert (dbuf != NULL);
1013 	assert (name != NULL);
1014 	assert (node != NULL);
1015 					/* create direct entry */
1016 	(void)memset(&de, 0, sizeof(de));
1017 	de.d_ino = ufs_rw32(node->inode->ino, needswap);
1018 	de.d_type = IFTODT(node->type);
1019 	de.d_namlen = (uint8_t)strlen(name);
1020 	strcpy(de.d_name, name);
1021 	reclen = DIRSIZ_SWAP(0, &de, needswap);
1022 	de.d_reclen = ufs_rw16(reclen, needswap);
1023 
1024 	dp = (struct direct *)(dbuf->buf + dbuf->cur);
1025 	llen = 0;
1026 	if (dp != NULL)
1027 		llen = DIRSIZ_SWAP(0, dp, needswap);
1028 
1029 	if (debug & DEBUG_FS_MAKE_DIRBUF)
1030 		printf(
1031 		    "ffs_make_dirbuf: dbuf siz %d cur %d lastlen %d\n"
1032 		    "  ino %d type %d reclen %d namlen %d name %.30s\n",
1033 		    dbuf->size, dbuf->cur, llen,
1034 		    ufs_rw32(de.d_ino, needswap), de.d_type, reclen,
1035 		    de.d_namlen, de.d_name);
1036 
1037 	if (reclen + dbuf->cur + llen > roundup(dbuf->size, DIRBLKSIZ)) {
1038 		if (debug & DEBUG_FS_MAKE_DIRBUF)
1039 			printf("ffs_make_dirbuf: growing buf to %d\n",
1040 			    dbuf->size + DIRBLKSIZ);
1041 		newbuf = erealloc(dbuf->buf, dbuf->size + DIRBLKSIZ);
1042 		dbuf->buf = newbuf;
1043 		dbuf->size += DIRBLKSIZ;
1044 		memset(dbuf->buf + dbuf->size - DIRBLKSIZ, 0, DIRBLKSIZ);
1045 		dbuf->cur = dbuf->size - DIRBLKSIZ;
1046 	} else if (dp) {			/* shrink end of previous */
1047 		dp->d_reclen = ufs_rw16(llen,needswap);
1048 		dbuf->cur += llen;
1049 	}
1050 	dp = (struct direct *)(dbuf->buf + dbuf->cur);
1051 	memcpy(dp, &de, reclen);
1052 	dp->d_reclen = ufs_rw16(dbuf->size - dbuf->cur, needswap);
1053 }
1054 
1055 /*
1056  * cribbed from sys/ufs/ffs/ffs_alloc.c
1057  */
1058 static void
1059 ffs_write_inode(union dinode *dp, uint32_t ino, const fsinfo_t *fsopts)
1060 {
1061 	char 		*buf;
1062 	struct ufs1_dinode *dp1;
1063 	struct ufs2_dinode *dp2, *dip;
1064 	struct cg	*cgp;
1065 	struct fs	*fs;
1066 	int		cg, cgino, i;
1067 	daddr_t		d;
1068 	char		sbbuf[FFS_MAXBSIZE];
1069 	int32_t		initediblk;
1070 	ffs_opt_t	*ffs_opts = fsopts->fs_specific;
1071 
1072 	assert (dp != NULL);
1073 	assert (ino > 0);
1074 	assert (fsopts != NULL);
1075 	assert (ffs_opts != NULL);
1076 
1077 	fs = (struct fs *)fsopts->superblock;
1078 	cg = ino_to_cg(fs, ino);
1079 	cgino = ino % fs->fs_ipg;
1080 	if (debug & DEBUG_FS_WRITE_INODE)
1081 		printf("ffs_write_inode: din %p ino %u cg %d cgino %d\n",
1082 		    dp, ino, cg, cgino);
1083 
1084 	ffs_rdfs(fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize, &sbbuf,
1085 	    fsopts);
1086 	cgp = (struct cg *)sbbuf;
1087 	if (!cg_chkmagic_swap(cgp, fsopts->needswap))
1088 		errx(1, "ffs_write_inode: cg %d: bad magic number", cg);
1089 
1090 	assert (isclr(cg_inosused_swap(cgp, fsopts->needswap), cgino));
1091 
1092 	buf = emalloc(fs->fs_bsize);
1093 	dp1 = (struct ufs1_dinode *)buf;
1094 	dp2 = (struct ufs2_dinode *)buf;
1095 
1096 	if (fs->fs_cstotal.cs_nifree == 0)
1097 		errx(1, "ffs_write_inode: fs out of inodes for ino %u",
1098 		    ino);
1099 	if (fs->fs_cs(fs, cg).cs_nifree == 0)
1100 		errx(1,
1101 		    "ffs_write_inode: cg %d out of inodes for ino %u",
1102 		    cg, ino);
1103 	setbit(cg_inosused_swap(cgp, fsopts->needswap), cgino);
1104 	ufs_add32(cgp->cg_cs.cs_nifree, -1, fsopts->needswap);
1105 	fs->fs_cstotal.cs_nifree--;
1106 	fs->fs_cs(fs, cg).cs_nifree--;
1107 	if (S_ISDIR(DIP(dp, mode))) {
1108 		ufs_add32(cgp->cg_cs.cs_ndir, 1, fsopts->needswap);
1109 		fs->fs_cstotal.cs_ndir++;
1110 		fs->fs_cs(fs, cg).cs_ndir++;
1111 	}
1112 
1113 	/*
1114 	 * Initialize inode blocks on the fly for UFS2.
1115 	 */
1116 	initediblk = ufs_rw32(cgp->cg_initediblk, fsopts->needswap);
1117 	if (ffs_opts->version == 2 && cgino + INOPB(fs) > initediblk &&
1118 	    initediblk < ufs_rw32(cgp->cg_niblk, fsopts->needswap)) {
1119 		memset(buf, 0, fs->fs_bsize);
1120 		dip = (struct ufs2_dinode *)buf;
1121 		for (i = 0; i < INOPB(fs); i++) {
1122 			dip->di_gen = random();
1123 			dip++;
1124 		}
1125 		ffs_wtfs(fsbtodb(fs, ino_to_fsba(fs,
1126 				  cg * fs->fs_ipg + initediblk)),
1127 		    fs->fs_bsize, buf, fsopts);
1128 		initediblk += INOPB(fs);
1129 		cgp->cg_initediblk = ufs_rw32(initediblk, fsopts->needswap);
1130 	}
1131 
1132 
1133 	ffs_wtfs(fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize, &sbbuf,
1134 	    fsopts);
1135 
1136 					/* now write inode */
1137 	d = fsbtodb(fs, ino_to_fsba(fs, ino));
1138 	ffs_rdfs(d, fs->fs_bsize, buf, fsopts);
1139 	if (fsopts->needswap) {
1140 		if (ffs_opts->version == 1)
1141 			ffs_dinode1_swap(&dp->ffs1_din,
1142 			    &dp1[ino_to_fsbo(fs, ino)]);
1143 		else
1144 			ffs_dinode2_swap(&dp->ffs2_din,
1145 			    &dp2[ino_to_fsbo(fs, ino)]);
1146 	} else {
1147 		if (ffs_opts->version == 1)
1148 			dp1[ino_to_fsbo(fs, ino)] = dp->ffs1_din;
1149 		else
1150 			dp2[ino_to_fsbo(fs, ino)] = dp->ffs2_din;
1151 	}
1152 	ffs_wtfs(d, fs->fs_bsize, buf, fsopts);
1153 	free(buf);
1154 }
1155 
1156 void
1157 panic(const char *fmt, ...)
1158 {
1159 	va_list ap;
1160 
1161 	va_start(ap, fmt);
1162 	vwarnx(fmt, ap);
1163 	va_end(ap);
1164 	exit(1);
1165 }
1166