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