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