ffs.c revision 267654
1213239Sgonzo/*	$NetBSD: ffs.c,v 1.44 2009/04/28 22:49:26 joerg Exp $	*/
2213239Sgonzo
3213239Sgonzo/*
4213239Sgonzo * Copyright (c) 2001 Wasabi Systems, Inc.
5213239Sgonzo * All rights reserved.
6213239Sgonzo *
7213239Sgonzo * Written by Luke Mewburn for Wasabi Systems, Inc.
8213239Sgonzo *
9213239Sgonzo * Redistribution and use in source and binary forms, with or without
10213239Sgonzo * modification, are permitted provided that the following conditions
11213239Sgonzo * are met:
12213239Sgonzo * 1. Redistributions of source code must retain the above copyright
13213239Sgonzo *    notice, this list of conditions and the following disclaimer.
14213239Sgonzo * 2. Redistributions in binary form must reproduce the above copyright
15213239Sgonzo *    notice, this list of conditions and the following disclaimer in the
16213239Sgonzo *    documentation and/or other materials provided with the distribution.
17213239Sgonzo * 3. All advertising materials mentioning features or use of this software
18213239Sgonzo *    must display the following acknowledgement:
19213239Sgonzo *      This product includes software developed for the NetBSD Project by
20213239Sgonzo *      Wasabi Systems, Inc.
21213239Sgonzo * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22213239Sgonzo *    or promote products derived from this software without specific prior
23213239Sgonzo *    written permission.
24213239Sgonzo *
25213239Sgonzo * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26213239Sgonzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27213239Sgonzo * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28213239Sgonzo * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29213239Sgonzo * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30213239Sgonzo * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31213239Sgonzo * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32213239Sgonzo * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33213239Sgonzo * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34213239Sgonzo * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35213239Sgonzo * POSSIBILITY OF SUCH DAMAGE.
36213239Sgonzo */
37213239Sgonzo/*
38213239Sgonzo * Copyright (c) 1982, 1986, 1989, 1993
39213239Sgonzo *	The Regents of the University of California.  All rights reserved.
40213239Sgonzo *
41213239Sgonzo * Redistribution and use in source and binary forms, with or without
42213239Sgonzo * modification, are permitted provided that the following conditions
43213239Sgonzo * are met:
44213239Sgonzo * 1. Redistributions of source code must retain the above copyright
45213239Sgonzo *    notice, this list of conditions and the following disclaimer.
46213239Sgonzo * 2. Redistributions in binary form must reproduce the above copyright
47213239Sgonzo *    notice, this list of conditions and the following disclaimer in the
48213239Sgonzo *    documentation and/or other materials provided with the distribution.
49213239Sgonzo * 3. Neither the name of the University nor the names of its contributors
50213239Sgonzo *    may be used to endorse or promote products derived from this software
51213239Sgonzo *    without specific prior written permission.
52213239Sgonzo *
53213239Sgonzo * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54213239Sgonzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55213239Sgonzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56213239Sgonzo * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57213239Sgonzo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58213239Sgonzo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59213239Sgonzo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60213239Sgonzo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61213239Sgonzo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62213239Sgonzo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63213239Sgonzo * SUCH DAMAGE.
64213239Sgonzo *
65213239Sgonzo *	@(#)ffs_alloc.c	8.19 (Berkeley) 7/13/95
66213239Sgonzo */
67213239Sgonzo
68213239Sgonzo#include <sys/cdefs.h>
69213239Sgonzo__FBSDID("$FreeBSD: releng/9.3/usr.sbin/makefs/ffs.c 248293 2013-03-14 22:57:27Z brooks $");
70213239Sgonzo
71213239Sgonzo#include <sys/param.h>
72213239Sgonzo
73213239Sgonzo#include <sys/mount.h>
74213239Sgonzo
75213239Sgonzo#include <assert.h>
76213239Sgonzo#include <errno.h>
77213239Sgonzo#include <fcntl.h>
78213239Sgonzo#include <stdarg.h>
79213239Sgonzo#include <stdint.h>
80213239Sgonzo#include <stdio.h>
81213239Sgonzo#include <stdlib.h>
82213239Sgonzo#include <string.h>
83213239Sgonzo#include <time.h>
84213239Sgonzo#include <unistd.h>
85213239Sgonzo
86213239Sgonzo#include "makefs.h"
87213239Sgonzo#include "ffs.h"
88213239Sgonzo
89213239Sgonzo#if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS
90213239Sgonzo#include <sys/statvfs.h>
91213239Sgonzo#endif
92213239Sgonzo
93213239Sgonzo#include <ufs/ufs/dinode.h>
94213239Sgonzo#include <ufs/ufs/dir.h>
95213239Sgonzo#include <ufs/ffs/fs.h>
96213239Sgonzo
97213239Sgonzo
98213239Sgonzo#include "ffs/ufs_bswap.h"
99213239Sgonzo#include "ffs/ufs_inode.h"
100213239Sgonzo#include "ffs/newfs_extern.h"
101213239Sgonzo#include "ffs/ffs_extern.h"
102213239Sgonzo
103213239Sgonzo#undef DIP
104213239Sgonzo#define DIP(dp, field) \
105213239Sgonzo	((ffs_opts->version == 1) ? \
106213239Sgonzo	(dp)->ffs1_din.di_##field : (dp)->ffs2_din.di_##field)
107213239Sgonzo
108213239Sgonzo/*
109213239Sgonzo * Various file system defaults (cribbed from newfs(8)).
110213239Sgonzo */
111213239Sgonzo#define	DFL_FRAGSIZE		1024		/* fragment size */
112213239Sgonzo#define	DFL_BLKSIZE		8192		/* block size */
113213239Sgonzo#define	DFL_SECSIZE		512		/* sector size */
114213239Sgonzo#define	DFL_CYLSPERGROUP	65536		/* cylinders per group */
115213239Sgonzo#define	DFL_FRAGSPERINODE	4		/* fragments per inode */
116213239Sgonzo#define	DFL_ROTDELAY		0		/* rotational delay */
117213239Sgonzo#define	DFL_NRPOS		1		/* rotational positions */
118213239Sgonzo#define	DFL_RPM			3600		/* rpm of disk */
119213239Sgonzo#define	DFL_NSECTORS		64		/* # of sectors */
120213239Sgonzo#define	DFL_NTRACKS		16		/* # of tracks */
121213239Sgonzo
122213239Sgonzo
123213239Sgonzotypedef struct {
124213239Sgonzo	u_char		*buf;		/* buf for directory */
125213239Sgonzo	doff_t		size;		/* full size of buf */
126213239Sgonzo	doff_t		cur;		/* offset of current entry */
127213239Sgonzo} dirbuf_t;
128213239Sgonzo
129213239Sgonzo
130213239Sgonzostatic	int	ffs_create_image(const char *, fsinfo_t *);
131213239Sgonzostatic	void	ffs_dump_fsinfo(fsinfo_t *);
132213239Sgonzostatic	void	ffs_dump_dirbuf(dirbuf_t *, const char *, int);
133213239Sgonzostatic	void	ffs_make_dirbuf(dirbuf_t *, const char *, fsnode *, int);
134213239Sgonzostatic	int	ffs_populate_dir(const char *, fsnode *, fsinfo_t *);
135213239Sgonzostatic	void	ffs_size_dir(fsnode *, fsinfo_t *);
136213239Sgonzostatic	void	ffs_validate(const char *, fsnode *, fsinfo_t *);
137213239Sgonzostatic	void	ffs_write_file(union dinode *, uint32_t, void *, fsinfo_t *);
138213239Sgonzostatic	void	ffs_write_inode(union dinode *, uint32_t, const fsinfo_t *);
139213239Sgonzostatic  void	*ffs_build_dinode1(struct ufs1_dinode *, dirbuf_t *, fsnode *,
140213239Sgonzo				 fsnode *, fsinfo_t *);
141213239Sgonzostatic  void	*ffs_build_dinode2(struct ufs2_dinode *, dirbuf_t *, fsnode *,
142213239Sgonzo				 fsnode *, fsinfo_t *);
143213239Sgonzo
144213239Sgonzo
145213239Sgonzo
146213239Sgonzoint	sectorsize;		/* XXX: for buf.c::getblk() */
147213239Sgonzo
148213239Sgonzo	/* publicly visible functions */
149213239Sgonzo
150213239Sgonzovoid
151213239Sgonzoffs_prep_opts(fsinfo_t *fsopts)
152213239Sgonzo{
153213239Sgonzo	ffs_opt_t *ffs_opts;
154213239Sgonzo
155213239Sgonzo	if ((ffs_opts = calloc(1, sizeof(ffs_opt_t))) == NULL)
156213239Sgonzo		err(1, "Allocating memory for ffs_options");
157213239Sgonzo
158213239Sgonzo	fsopts->fs_specific = ffs_opts;
159213239Sgonzo
160213239Sgonzo	ffs_opts->bsize= -1;
161213239Sgonzo	ffs_opts->fsize= -1;
162213239Sgonzo	ffs_opts->cpg= -1;
163213239Sgonzo	ffs_opts->density= -1;
164213239Sgonzo	ffs_opts->minfree= -1;
165213239Sgonzo	ffs_opts->optimization= -1;
166213239Sgonzo	ffs_opts->maxcontig= -1;
167213239Sgonzo	ffs_opts->maxbpg= -1;
168213239Sgonzo	ffs_opts->avgfilesize= -1;
169213239Sgonzo	ffs_opts->avgfpdir= -1;
170213239Sgonzo	ffs_opts->version = 1;
171213239Sgonzo}
172213239Sgonzo
173213239Sgonzovoid
174213239Sgonzoffs_cleanup_opts(fsinfo_t *fsopts)
175213239Sgonzo{
176213239Sgonzo	if (fsopts->fs_specific)
177213239Sgonzo		free(fsopts->fs_specific);
178213239Sgonzo}
179213239Sgonzo
180213239Sgonzoint
181213239Sgonzoffs_parse_opts(const char *option, fsinfo_t *fsopts)
182213239Sgonzo{
183213239Sgonzo	ffs_opt_t	*ffs_opts = fsopts->fs_specific;
184213239Sgonzo
185213239Sgonzo	option_t ffs_options[] = {
186213239Sgonzo		{ "bsize",	&ffs_opts->bsize,	1,	INT_MAX,
187213239Sgonzo					"block size" },
188213239Sgonzo		{ "fsize",	&ffs_opts->fsize,	1,	INT_MAX,
189213239Sgonzo					"fragment size" },
190213239Sgonzo		{ "density",	&ffs_opts->density,	1,	INT_MAX,
191213239Sgonzo					"bytes per inode" },
192213239Sgonzo		{ "minfree",	&ffs_opts->minfree,	0,	99,
193213239Sgonzo					"minfree" },
194213239Sgonzo		{ "maxbpf",	&ffs_opts->maxbpg,	1,	INT_MAX,
195213239Sgonzo					"max blocks per file in a cg" },
196213239Sgonzo		{ "avgfilesize", &ffs_opts->avgfilesize,1,	INT_MAX,
197213239Sgonzo					"expected average file size" },
198213239Sgonzo		{ "avgfpdir",	&ffs_opts->avgfpdir,	1,	INT_MAX,
199213239Sgonzo					"expected # of files per directory" },
200213239Sgonzo		{ "extent",	&ffs_opts->maxbsize,	1,	INT_MAX,
201213239Sgonzo					"maximum # extent size" },
202213239Sgonzo		{ "maxbpcg",	&ffs_opts->maxblkspercg,1,	INT_MAX,
203213239Sgonzo					"max # of blocks per group" },
204213239Sgonzo		{ "version",	&ffs_opts->version,	1,	2,
205213239Sgonzo					"UFS version" },
206213239Sgonzo		{ .name = NULL }
207213239Sgonzo	};
208213239Sgonzo
209213239Sgonzo	char	*var, *val;
210213239Sgonzo	int	rv;
211213239Sgonzo
212213239Sgonzo	assert(option != NULL);
213213239Sgonzo	assert(fsopts != NULL);
214213239Sgonzo	assert(ffs_opts != NULL);
215213239Sgonzo
216213239Sgonzo	if (debug & DEBUG_FS_PARSE_OPTS)
217213239Sgonzo		printf("ffs_parse_opts: got `%s'\n", option);
218213239Sgonzo
219213239Sgonzo	if ((var = strdup(option)) == NULL)
220213239Sgonzo		err(1, "Allocating memory for copy of option string");
221213239Sgonzo	rv = 0;
222213239Sgonzo
223213239Sgonzo	if ((val = strchr(var, '=')) == NULL) {
224213239Sgonzo		warnx("Option `%s' doesn't contain a value", var);
225213239Sgonzo		goto leave_ffs_parse_opts;
226213239Sgonzo	}
227213239Sgonzo	*val++ = '\0';
228213239Sgonzo
229213239Sgonzo	if (strcmp(var, "optimization") == 0) {
230213239Sgonzo		if (strcmp(val, "time") == 0) {
231213239Sgonzo			ffs_opts->optimization = FS_OPTTIME;
232213239Sgonzo		} else if (strcmp(val, "space") == 0) {
233213239Sgonzo			ffs_opts->optimization = FS_OPTSPACE;
234213239Sgonzo		} else {
235213239Sgonzo			warnx("Invalid optimization `%s'", val);
236213239Sgonzo			goto leave_ffs_parse_opts;
237213239Sgonzo		}
238213239Sgonzo		rv = 1;
239213239Sgonzo	} else if (strcmp(var, "label") == 0) {
240213239Sgonzo		strlcpy(ffs_opts->label, val, sizeof(ffs_opts->label));
241213239Sgonzo		rv = 1;
242213239Sgonzo	} else
243213239Sgonzo		rv = set_option(ffs_options, var, val);
244213239Sgonzo
245213239Sgonzo leave_ffs_parse_opts:
246213239Sgonzo	if (var)
247213239Sgonzo		free(var);
248213239Sgonzo	return (rv);
249213239Sgonzo}
250213239Sgonzo
251213239Sgonzo
252213239Sgonzovoid
253213239Sgonzoffs_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts)
254213239Sgonzo{
255213239Sgonzo	struct fs	*superblock;
256213239Sgonzo	struct timeval	start;
257213239Sgonzo
258213239Sgonzo	assert(image != NULL);
259213239Sgonzo	assert(dir != NULL);
260213239Sgonzo	assert(root != NULL);
261213239Sgonzo	assert(fsopts != NULL);
262213239Sgonzo
263213239Sgonzo	if (debug & DEBUG_FS_MAKEFS)
264213239Sgonzo		printf("ffs_makefs: image %s directory %s root %p\n",
265213239Sgonzo		    image, dir, root);
266213239Sgonzo
267213239Sgonzo		/* validate tree and options */
268213239Sgonzo	TIMER_START(start);
269213239Sgonzo	ffs_validate(dir, root, fsopts);
270213239Sgonzo	TIMER_RESULTS(start, "ffs_validate");
271213239Sgonzo
272213239Sgonzo	printf("Calculated size of `%s': %lld bytes, %lld inodes\n",
273213239Sgonzo	    image, (long long)fsopts->size, (long long)fsopts->inodes);
274213239Sgonzo
275213239Sgonzo		/* create image */
276213239Sgonzo	TIMER_START(start);
277213239Sgonzo	if (ffs_create_image(image, fsopts) == -1)
278213239Sgonzo		errx(1, "Image file `%s' not created.", image);
279213239Sgonzo	TIMER_RESULTS(start, "ffs_create_image");
280213239Sgonzo
281213239Sgonzo	fsopts->curinode = ROOTINO;
282213239Sgonzo
283213239Sgonzo	if (debug & DEBUG_FS_MAKEFS)
284213239Sgonzo		putchar('\n');
285213239Sgonzo
286213239Sgonzo		/* populate image */
287213239Sgonzo	printf("Populating `%s'\n", image);
288213239Sgonzo	TIMER_START(start);
289213239Sgonzo	if (! ffs_populate_dir(dir, root, fsopts))
290213239Sgonzo		errx(1, "Image file `%s' not populated.", image);
291213239Sgonzo	TIMER_RESULTS(start, "ffs_populate_dir");
292213239Sgonzo
293213239Sgonzo		/* ensure no outstanding buffers remain */
294213239Sgonzo	if (debug & DEBUG_FS_MAKEFS)
295213239Sgonzo		bcleanup();
296213239Sgonzo
297213239Sgonzo		/* update various superblock parameters */
298213239Sgonzo	superblock = fsopts->superblock;
299213239Sgonzo	superblock->fs_fmod = 0;
300213239Sgonzo	superblock->fs_old_cstotal.cs_ndir   = superblock->fs_cstotal.cs_ndir;
301213239Sgonzo	superblock->fs_old_cstotal.cs_nbfree = superblock->fs_cstotal.cs_nbfree;
302213239Sgonzo	superblock->fs_old_cstotal.cs_nifree = superblock->fs_cstotal.cs_nifree;
303213239Sgonzo	superblock->fs_old_cstotal.cs_nffree = superblock->fs_cstotal.cs_nffree;
304213239Sgonzo
305213239Sgonzo		/* write out superblock; image is now complete */
306213239Sgonzo	ffs_write_superblock(fsopts->superblock, fsopts);
307213239Sgonzo	if (close(fsopts->fd) == -1)
308213239Sgonzo		err(1, "Closing `%s'", image);
309213239Sgonzo	fsopts->fd = -1;
310213239Sgonzo	printf("Image `%s' complete\n", image);
311213239Sgonzo}
312213239Sgonzo
313213239Sgonzo	/* end of public functions */
314213239Sgonzo
315213239Sgonzo
316213239Sgonzostatic void
317213239Sgonzoffs_validate(const char *dir, fsnode *root, fsinfo_t *fsopts)
318213239Sgonzo{
319213239Sgonzo	int32_t	ncg = 1;
320213239Sgonzo#if notyet
321213239Sgonzo	int32_t	spc, nspf, ncyl, fssize;
322213239Sgonzo#endif
323213239Sgonzo	ffs_opt_t	*ffs_opts = fsopts->fs_specific;
324213239Sgonzo
325213239Sgonzo	assert(dir != NULL);
326213239Sgonzo	assert(root != NULL);
327213239Sgonzo	assert(fsopts != NULL);
328213239Sgonzo	assert(ffs_opts != NULL);
329213239Sgonzo
330213239Sgonzo	if (debug & DEBUG_FS_VALIDATE) {
331213239Sgonzo		printf("ffs_validate: before defaults set:\n");
332213239Sgonzo		ffs_dump_fsinfo(fsopts);
333213239Sgonzo	}
334213239Sgonzo
335213239Sgonzo		/* set FFS defaults */
336213239Sgonzo	if (fsopts->sectorsize == -1)
337213239Sgonzo		fsopts->sectorsize = DFL_SECSIZE;
338213239Sgonzo	if (ffs_opts->fsize == -1)
339213239Sgonzo		ffs_opts->fsize = MAX(DFL_FRAGSIZE, fsopts->sectorsize);
340213239Sgonzo	if (ffs_opts->bsize == -1)
341213239Sgonzo		ffs_opts->bsize = MIN(DFL_BLKSIZE, 8 * ffs_opts->fsize);
342213239Sgonzo	if (ffs_opts->cpg == -1)
343213239Sgonzo		ffs_opts->cpg = DFL_CYLSPERGROUP;
344213239Sgonzo	else
345213239Sgonzo		ffs_opts->cpgflg = 1;
346213239Sgonzo				/* fsopts->density is set below */
347213239Sgonzo	if (ffs_opts->nsectors == -1)
348213239Sgonzo		ffs_opts->nsectors = DFL_NSECTORS;
349213239Sgonzo	if (ffs_opts->minfree == -1)
350213239Sgonzo		ffs_opts->minfree = MINFREE;
351213239Sgonzo	if (ffs_opts->optimization == -1)
352213239Sgonzo		ffs_opts->optimization = DEFAULTOPT;
353213239Sgonzo	if (ffs_opts->maxcontig == -1)
354213239Sgonzo		ffs_opts->maxcontig =
355213239Sgonzo		    MAX(1, MIN(MAXPHYS, FFS_MAXBSIZE) / ffs_opts->bsize);
356213239Sgonzo	/* XXX ondisk32 */
357213239Sgonzo	if (ffs_opts->maxbpg == -1)
358213239Sgonzo		ffs_opts->maxbpg = ffs_opts->bsize / sizeof(int32_t);
359213239Sgonzo	if (ffs_opts->avgfilesize == -1)
360213239Sgonzo		ffs_opts->avgfilesize = AVFILESIZ;
361213239Sgonzo	if (ffs_opts->avgfpdir == -1)
362213239Sgonzo		ffs_opts->avgfpdir = AFPDIR;
363213239Sgonzo
364213239Sgonzo		/* calculate size of tree */
365213239Sgonzo	ffs_size_dir(root, fsopts);
366213239Sgonzo	fsopts->inodes += ROOTINO;		/* include first two inodes */
367213239Sgonzo
368213239Sgonzo	if (debug & DEBUG_FS_VALIDATE)
369213239Sgonzo		printf("ffs_validate: size of tree: %lld bytes, %lld inodes\n",
370213239Sgonzo		    (long long)fsopts->size, (long long)fsopts->inodes);
371213239Sgonzo
372213239Sgonzo		/* add requested slop */
373213239Sgonzo	fsopts->size += fsopts->freeblocks;
374213239Sgonzo	fsopts->inodes += fsopts->freefiles;
375213239Sgonzo	if (fsopts->freefilepc > 0)
376213239Sgonzo		fsopts->inodes =
377213239Sgonzo		    fsopts->inodes * (100 + fsopts->freefilepc) / 100;
378213286Sgonzo	if (fsopts->freeblockpc > 0)
379213286Sgonzo		fsopts->size =
380213239Sgonzo		    fsopts->size * (100 + fsopts->freeblockpc) / 100;
381213239Sgonzo
382213239Sgonzo		/* add space needed for superblocks */
383213239Sgonzo	/*
384213239Sgonzo	 * The old SBOFF (SBLOCK_UFS1) is used here because makefs is
385213239Sgonzo	 * typically used for small filesystems where space matters.
386213239Sgonzo	 * XXX make this an option.
387213239Sgonzo	 */
388213239Sgonzo	fsopts->size += (SBLOCK_UFS1 + SBLOCKSIZE) * ncg;
389213239Sgonzo		/* add space needed to store inodes, x3 for blockmaps, etc */
390213239Sgonzo	if (ffs_opts->version == 1)
391213239Sgonzo		fsopts->size += ncg * DINODE1_SIZE *
392213239Sgonzo		    roundup(fsopts->inodes / ncg,
393213239Sgonzo			ffs_opts->bsize / DINODE1_SIZE);
394213239Sgonzo	else
395213239Sgonzo		fsopts->size += ncg * DINODE2_SIZE *
396213239Sgonzo		    roundup(fsopts->inodes / ncg,
397213239Sgonzo			ffs_opts->bsize / DINODE2_SIZE);
398213239Sgonzo
399213239Sgonzo		/* add minfree */
400213239Sgonzo	if (ffs_opts->minfree > 0)
401213239Sgonzo		fsopts->size =
402213239Sgonzo		    fsopts->size * (100 + ffs_opts->minfree) / 100;
403213239Sgonzo	/*
404213239Sgonzo	 * XXX	any other fs slop to add, such as csum's, bitmaps, etc ??
405213239Sgonzo	 */
406213239Sgonzo
407213239Sgonzo	if (fsopts->size < fsopts->minsize)	/* ensure meets minimum size */
408213239Sgonzo		fsopts->size = fsopts->minsize;
409213286Sgonzo
410213286Sgonzo		/* round up to the next block */
411213239Sgonzo	fsopts->size = roundup(fsopts->size, ffs_opts->bsize);
412213239Sgonzo
413213239Sgonzo		/* calculate density if necessary */
414213239Sgonzo	if (ffs_opts->density == -1)
415213239Sgonzo		ffs_opts->density = fsopts->size / fsopts->inodes + 1;
416213239Sgonzo
417213239Sgonzo	if (debug & DEBUG_FS_VALIDATE) {
418213239Sgonzo		printf("ffs_validate: after defaults set:\n");
419213239Sgonzo		ffs_dump_fsinfo(fsopts);
420213239Sgonzo		printf("ffs_validate: dir %s; %lld bytes, %lld inodes\n",
421213239Sgonzo		    dir, (long long)fsopts->size, (long long)fsopts->inodes);
422213239Sgonzo	}
423213239Sgonzo	sectorsize = fsopts->sectorsize;	/* XXX - see earlier */
424213239Sgonzo
425213239Sgonzo		/* now check calculated sizes vs requested sizes */
426213239Sgonzo	if (fsopts->maxsize > 0 && fsopts->size > fsopts->maxsize) {
427213239Sgonzo		errx(1, "`%s' size of %lld is larger than the maxsize of %lld.",
428213239Sgonzo		    dir, (long long)fsopts->size, (long long)fsopts->maxsize);
429213239Sgonzo	}
430213239Sgonzo}
431213239Sgonzo
432213239Sgonzo
433213239Sgonzostatic void
434213239Sgonzoffs_dump_fsinfo(fsinfo_t *f)
435213239Sgonzo{
436213239Sgonzo
437213239Sgonzo	ffs_opt_t	*fs = f->fs_specific;
438213239Sgonzo
439213239Sgonzo	printf("fsopts at %p\n", f);
440213239Sgonzo
441213239Sgonzo	printf("\tsize %lld, inodes %lld, curinode %u\n",
442213239Sgonzo	    (long long)f->size, (long long)f->inodes, f->curinode);
443213239Sgonzo
444213239Sgonzo	printf("\tminsize %lld, maxsize %lld\n",
445213239Sgonzo	    (long long)f->minsize, (long long)f->maxsize);
446213239Sgonzo	printf("\tfree files %lld, freefile %% %d\n",
447	    (long long)f->freefiles, f->freefilepc);
448	printf("\tfree blocks %lld, freeblock %% %d\n",
449	    (long long)f->freeblocks, f->freeblockpc);
450	printf("\tneedswap %d, sectorsize %d\n", f->needswap, f->sectorsize);
451
452	printf("\tbsize %d, fsize %d, cpg %d, density %d\n",
453	    fs->bsize, fs->fsize, fs->cpg, fs->density);
454	printf("\tnsectors %d, rpm %d, minfree %d\n",
455	    fs->nsectors, fs->rpm, fs->minfree);
456	printf("\tmaxcontig %d, maxbpg %d\n",
457	    fs->maxcontig, fs->maxbpg);
458	printf("\toptimization %s\n",
459	    fs->optimization == FS_OPTSPACE ? "space" : "time");
460}
461
462
463static int
464ffs_create_image(const char *image, fsinfo_t *fsopts)
465{
466#if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS
467	struct statvfs	sfs;
468#endif
469	struct fs	*fs;
470	char	*buf;
471	int	i, bufsize;
472	off_t	bufrem;
473
474	assert (image != NULL);
475	assert (fsopts != NULL);
476
477		/* create image */
478	if ((fsopts->fd = open(image, O_RDWR | O_CREAT | O_TRUNC, 0666))
479	    == -1) {
480		warn("Can't open `%s' for writing", image);
481		return (-1);
482	}
483
484		/* zero image */
485#if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS
486	if (fstatvfs(fsopts->fd, &sfs) == -1) {
487#endif
488		bufsize = 8192;
489#if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS
490		warn("can't fstatvfs `%s', using default %d byte chunk",
491		    image, bufsize);
492	} else
493		bufsize = sfs.f_iosize;
494#endif
495	bufrem = fsopts->size;
496	if (fsopts->sparse) {
497		if (ftruncate(fsopts->fd, bufrem) == -1) {
498			warn("sparse option disabled.\n");
499			fsopts->sparse = 0;
500		}
501	}
502	if (fsopts->sparse) {
503		/* File truncated at bufrem. Remaining is 0 */
504		bufrem = 0;
505		buf = NULL;
506	} else {
507		if (debug & DEBUG_FS_CREATE_IMAGE)
508			printf("zero-ing image `%s', %lld sectors, "
509			    "using %d byte chunks\n", image, (long long)bufrem,
510			    bufsize);
511		if ((buf = calloc(1, bufsize)) == NULL) {
512			warn("Can't create buffer for sector");
513			return (-1);
514		}
515	}
516	while (bufrem > 0) {
517		i = write(fsopts->fd, buf, MIN(bufsize, bufrem));
518		if (i == -1) {
519			warn("zeroing image, %lld bytes to go",
520			    (long long)bufrem);
521			free(buf);
522			return (-1);
523		}
524		bufrem -= i;
525	}
526	if (buf)
527		free(buf);
528
529		/* make the file system */
530	if (debug & DEBUG_FS_CREATE_IMAGE)
531		printf("calling mkfs(\"%s\", ...)\n", image);
532	fs = ffs_mkfs(image, fsopts);
533	fsopts->superblock = (void *)fs;
534	if (debug & DEBUG_FS_CREATE_IMAGE) {
535		time_t t;
536
537		t = (time_t)((struct fs *)fsopts->superblock)->fs_time;
538		printf("mkfs returned %p; fs_time %s",
539		    fsopts->superblock, ctime(&t));
540		printf("fs totals: nbfree %lld, nffree %lld, nifree %lld, ndir %lld\n",
541		    (long long)fs->fs_cstotal.cs_nbfree,
542		    (long long)fs->fs_cstotal.cs_nffree,
543		    (long long)fs->fs_cstotal.cs_nifree,
544		    (long long)fs->fs_cstotal.cs_ndir);
545	}
546
547	if (fs->fs_cstotal.cs_nifree + ROOTINO < fsopts->inodes) {
548		warnx(
549		"Image file `%s' has %lld free inodes; %lld are required.",
550		    image,
551		    (long long)(fs->fs_cstotal.cs_nifree + ROOTINO),
552		    (long long)fsopts->inodes);
553		return (-1);
554	}
555	return (fsopts->fd);
556}
557
558
559static void
560ffs_size_dir(fsnode *root, fsinfo_t *fsopts)
561{
562	struct direct	tmpdir;
563	fsnode *	node;
564	int		curdirsize, this;
565	ffs_opt_t	*ffs_opts = fsopts->fs_specific;
566
567	/* node may be NULL (empty directory) */
568	assert(fsopts != NULL);
569	assert(ffs_opts != NULL);
570
571	if (debug & DEBUG_FS_SIZE_DIR)
572		printf("ffs_size_dir: entry: bytes %lld inodes %lld\n",
573		    (long long)fsopts->size, (long long)fsopts->inodes);
574
575#define	ADDDIRENT(e) do {						\
576	tmpdir.d_namlen = strlen((e));					\
577	this = DIRSIZ_SWAP(0, &tmpdir, 0);					\
578	if (debug & DEBUG_FS_SIZE_DIR_ADD_DIRENT)			\
579		printf("ADDDIRENT: was: %s (%d) this %d cur %d\n",	\
580		    e, tmpdir.d_namlen, this, curdirsize);		\
581	if (this + curdirsize > roundup(curdirsize, DIRBLKSIZ))		\
582		curdirsize = roundup(curdirsize, DIRBLKSIZ);		\
583	curdirsize += this;						\
584	if (debug & DEBUG_FS_SIZE_DIR_ADD_DIRENT)			\
585		printf("ADDDIRENT: now: %s (%d) this %d cur %d\n",	\
586		    e, tmpdir.d_namlen, this, curdirsize);		\
587} while (0);
588
589	/*
590	 * XXX	this needs to take into account extra space consumed
591	 *	by indirect blocks, etc.
592	 */
593#define	ADDSIZE(x) do {							\
594	fsopts->size += roundup((x), ffs_opts->fsize);			\
595} while (0);
596
597	curdirsize = 0;
598	for (node = root; node != NULL; node = node->next) {
599		ADDDIRENT(node->name);
600		if (node == root) {			/* we're at "." */
601			assert(strcmp(node->name, ".") == 0);
602			ADDDIRENT("..");
603		} else if ((node->inode->flags & FI_SIZED) == 0) {
604				/* don't count duplicate names */
605			node->inode->flags |= FI_SIZED;
606			if (debug & DEBUG_FS_SIZE_DIR_NODE)
607				printf("ffs_size_dir: `%s' size %lld\n",
608				    node->name,
609				    (long long)node->inode->st.st_size);
610			fsopts->inodes++;
611			if (node->type == S_IFREG)
612				ADDSIZE(node->inode->st.st_size);
613			if (node->type == S_IFLNK) {
614				int	slen;
615
616				slen = strlen(node->symlink) + 1;
617				if (slen >= (ffs_opts->version == 1 ?
618						MAXSYMLINKLEN_UFS1 :
619						MAXSYMLINKLEN_UFS2))
620					ADDSIZE(slen);
621			}
622		}
623		if (node->type == S_IFDIR)
624			ffs_size_dir(node->child, fsopts);
625	}
626	ADDSIZE(curdirsize);
627
628	if (debug & DEBUG_FS_SIZE_DIR)
629		printf("ffs_size_dir: exit: size %lld inodes %lld\n",
630		    (long long)fsopts->size, (long long)fsopts->inodes);
631}
632
633static void *
634ffs_build_dinode1(struct ufs1_dinode *dinp, dirbuf_t *dbufp, fsnode *cur,
635		 fsnode *root, fsinfo_t *fsopts)
636{
637	int slen;
638	void *membuf;
639
640	memset(dinp, 0, sizeof(*dinp));
641	dinp->di_mode = cur->inode->st.st_mode;
642	dinp->di_nlink = cur->inode->nlink;
643	dinp->di_size = cur->inode->st.st_size;
644	dinp->di_atime = cur->inode->st.st_atime;
645	dinp->di_mtime = cur->inode->st.st_mtime;
646	dinp->di_ctime = cur->inode->st.st_ctime;
647#if HAVE_STRUCT_STAT_ST_MTIMENSEC
648	dinp->di_atimensec = cur->inode->st.st_atimensec;
649	dinp->di_mtimensec = cur->inode->st.st_mtimensec;
650	dinp->di_ctimensec = cur->inode->st.st_ctimensec;
651#endif
652#if HAVE_STRUCT_STAT_ST_FLAGS
653	dinp->di_flags = cur->inode->st.st_flags;
654#endif
655#if HAVE_STRUCT_STAT_ST_GEN
656	dinp->di_gen = cur->inode->st.st_gen;
657#endif
658	dinp->di_uid = cur->inode->st.st_uid;
659	dinp->di_gid = cur->inode->st.st_gid;
660		/* not set: di_db, di_ib, di_blocks, di_spare */
661
662	membuf = NULL;
663	if (cur == root) {			/* "."; write dirbuf */
664		membuf = dbufp->buf;
665		dinp->di_size = dbufp->size;
666	} else if (S_ISBLK(cur->type) || S_ISCHR(cur->type)) {
667		dinp->di_size = 0;	/* a device */
668		dinp->di_rdev =
669		    ufs_rw32(cur->inode->st.st_rdev, fsopts->needswap);
670	} else if (S_ISLNK(cur->type)) {	/* symlink */
671		slen = strlen(cur->symlink);
672		if (slen < MAXSYMLINKLEN_UFS1) {	/* short link */
673			memcpy(dinp->di_db, cur->symlink, slen);
674		} else
675			membuf = cur->symlink;
676		dinp->di_size = slen;
677	}
678	return membuf;
679}
680
681static void *
682ffs_build_dinode2(struct ufs2_dinode *dinp, dirbuf_t *dbufp, fsnode *cur,
683		 fsnode *root, fsinfo_t *fsopts)
684{
685	int slen;
686	void *membuf;
687
688	memset(dinp, 0, sizeof(*dinp));
689	dinp->di_mode = cur->inode->st.st_mode;
690	dinp->di_nlink = cur->inode->nlink;
691	dinp->di_size = cur->inode->st.st_size;
692	dinp->di_atime = cur->inode->st.st_atime;
693	dinp->di_mtime = cur->inode->st.st_mtime;
694	dinp->di_ctime = cur->inode->st.st_ctime;
695#if HAVE_STRUCT_STAT_ST_MTIMENSEC
696	dinp->di_atimensec = cur->inode->st.st_atimensec;
697	dinp->di_mtimensec = cur->inode->st.st_mtimensec;
698	dinp->di_ctimensec = cur->inode->st.st_ctimensec;
699#endif
700#if HAVE_STRUCT_STAT_ST_FLAGS
701	dinp->di_flags = cur->inode->st.st_flags;
702#endif
703#if HAVE_STRUCT_STAT_ST_GEN
704	dinp->di_gen = cur->inode->st.st_gen;
705#endif
706#if HAVE_STRUCT_STAT_BIRTHTIME
707	dinp->di_birthtime = cur->inode->st.st_birthtime;
708	dinp->di_birthnsec = cur->inode->st.st_birthtimensec;
709#endif
710	dinp->di_uid = cur->inode->st.st_uid;
711	dinp->di_gid = cur->inode->st.st_gid;
712		/* not set: di_db, di_ib, di_blocks, di_spare */
713
714	membuf = NULL;
715	if (cur == root) {			/* "."; write dirbuf */
716		membuf = dbufp->buf;
717		dinp->di_size = dbufp->size;
718	} else if (S_ISBLK(cur->type) || S_ISCHR(cur->type)) {
719		dinp->di_size = 0;	/* a device */
720		dinp->di_rdev =
721		    ufs_rw64(cur->inode->st.st_rdev, fsopts->needswap);
722	} else if (S_ISLNK(cur->type)) {	/* symlink */
723		slen = strlen(cur->symlink);
724		if (slen < MAXSYMLINKLEN_UFS2) {	/* short link */
725			memcpy(dinp->di_db, cur->symlink, slen);
726		} else
727			membuf = cur->symlink;
728		dinp->di_size = slen;
729	}
730	return membuf;
731}
732
733static int
734ffs_populate_dir(const char *dir, fsnode *root, fsinfo_t *fsopts)
735{
736	fsnode		*cur;
737	dirbuf_t	dirbuf;
738	union dinode	din;
739	void		*membuf;
740	char		path[MAXPATHLEN + 1];
741	ffs_opt_t	*ffs_opts = fsopts->fs_specific;
742
743	assert(dir != NULL);
744	assert(root != NULL);
745	assert(fsopts != NULL);
746	assert(ffs_opts != NULL);
747
748	(void)memset(&dirbuf, 0, sizeof(dirbuf));
749
750	if (debug & DEBUG_FS_POPULATE)
751		printf("ffs_populate_dir: PASS 1  dir %s node %p\n", dir, root);
752
753		/*
754		 * pass 1: allocate inode numbers, build directory `file'
755		 */
756	for (cur = root; cur != NULL; cur = cur->next) {
757		if ((cur->inode->flags & FI_ALLOCATED) == 0) {
758			cur->inode->flags |= FI_ALLOCATED;
759			if (cur == root && cur->parent != NULL)
760				cur->inode->ino = cur->parent->inode->ino;
761			else {
762				cur->inode->ino = fsopts->curinode;
763				fsopts->curinode++;
764			}
765		}
766		ffs_make_dirbuf(&dirbuf, cur->name, cur, fsopts->needswap);
767		if (cur == root) {		/* we're at "."; add ".." */
768			ffs_make_dirbuf(&dirbuf, "..",
769			    cur->parent == NULL ? cur : cur->parent->first,
770			    fsopts->needswap);
771			root->inode->nlink++;	/* count my parent's link */
772		} else if (cur->child != NULL)
773			root->inode->nlink++;	/* count my child's link */
774
775		/*
776		 * XXX	possibly write file and long symlinks here,
777		 *	ensuring that blocks get written before inodes?
778		 *	otoh, this isn't a real filesystem, so who
779		 *	cares about ordering? :-)
780		 */
781	}
782	if (debug & DEBUG_FS_POPULATE_DIRBUF)
783		ffs_dump_dirbuf(&dirbuf, dir, fsopts->needswap);
784
785		/*
786		 * pass 2: write out dirbuf, then non-directories at this level
787		 */
788	if (debug & DEBUG_FS_POPULATE)
789		printf("ffs_populate_dir: PASS 2  dir %s\n", dir);
790	for (cur = root; cur != NULL; cur = cur->next) {
791		if (cur->inode->flags & FI_WRITTEN)
792			continue;		/* skip hard-linked entries */
793		cur->inode->flags |= FI_WRITTEN;
794
795		if (cur->contents == NULL) {
796			if (snprintf(path, sizeof(path), "%s/%s/%s", cur->root,
797			    cur->path, cur->name) >= (int)sizeof(path))
798				errx(1, "Pathname too long.");
799		}
800
801		if (cur->child != NULL)
802			continue;		/* child creates own inode */
803
804				/* build on-disk inode */
805		if (ffs_opts->version == 1)
806			membuf = ffs_build_dinode1(&din.ffs1_din, &dirbuf, cur,
807			    root, fsopts);
808		else
809			membuf = ffs_build_dinode2(&din.ffs2_din, &dirbuf, cur,
810			    root, fsopts);
811
812		if (debug & DEBUG_FS_POPULATE_NODE) {
813			printf("ffs_populate_dir: writing ino %d, %s",
814			    cur->inode->ino, inode_type(cur->type));
815			if (cur->inode->nlink > 1)
816				printf(", nlink %d", cur->inode->nlink);
817			putchar('\n');
818		}
819
820		if (membuf != NULL) {
821			ffs_write_file(&din, cur->inode->ino, membuf, fsopts);
822		} else if (S_ISREG(cur->type)) {
823			ffs_write_file(&din, cur->inode->ino,
824			    (cur->contents) ?  cur->contents : path, fsopts);
825		} else {
826			assert (! S_ISDIR(cur->type));
827			ffs_write_inode(&din, cur->inode->ino, fsopts);
828		}
829	}
830
831		/*
832		 * pass 3: write out sub-directories
833		 */
834	if (debug & DEBUG_FS_POPULATE)
835		printf("ffs_populate_dir: PASS 3  dir %s\n", dir);
836	for (cur = root; cur != NULL; cur = cur->next) {
837		if (cur->child == NULL)
838			continue;
839		if (snprintf(path, sizeof(path), "%s/%s", dir, cur->name)
840		    >= sizeof(path))
841			errx(1, "Pathname too long.");
842		if (! ffs_populate_dir(path, cur->child, fsopts))
843			return (0);
844	}
845
846	if (debug & DEBUG_FS_POPULATE)
847		printf("ffs_populate_dir: DONE dir %s\n", dir);
848
849		/* cleanup */
850	if (dirbuf.buf != NULL)
851		free(dirbuf.buf);
852	return (1);
853}
854
855
856static void
857ffs_write_file(union dinode *din, uint32_t ino, void *buf, fsinfo_t *fsopts)
858{
859	int 	isfile, ffd;
860	char	*fbuf, *p;
861	off_t	bufleft, chunk, offset;
862	ssize_t nread;
863	struct inode	in;
864	struct buf *	bp;
865	ffs_opt_t	*ffs_opts = fsopts->fs_specific;
866
867	assert (din != NULL);
868	assert (buf != NULL);
869	assert (fsopts != NULL);
870	assert (ffs_opts != NULL);
871
872	isfile = S_ISREG(DIP(din, mode));
873	fbuf = NULL;
874	ffd = -1;
875	p = NULL;
876
877	in.i_fs = (struct fs *)fsopts->superblock;
878
879	if (debug & DEBUG_FS_WRITE_FILE) {
880		printf(
881		    "ffs_write_file: ino %u, din %p, isfile %d, %s, size %lld",
882		    ino, din, isfile, inode_type(DIP(din, mode) & S_IFMT),
883		    (long long)DIP(din, size));
884		if (isfile)
885			printf(", file '%s'\n", (char *)buf);
886		else
887			printf(", buffer %p\n", buf);
888	}
889
890	in.i_number = ino;
891	in.i_size = DIP(din, size);
892	if (ffs_opts->version == 1)
893		memcpy(&in.i_din.ffs1_din, &din->ffs1_din,
894		    sizeof(in.i_din.ffs1_din));
895	else
896		memcpy(&in.i_din.ffs2_din, &din->ffs2_din,
897		    sizeof(in.i_din.ffs2_din));
898	in.i_fd = fsopts->fd;
899
900	if (DIP(din, size) == 0)
901		goto write_inode_and_leave;		/* mmm, cheating */
902
903	if (isfile) {
904		if ((fbuf = malloc(ffs_opts->bsize)) == NULL)
905			err(1, "Allocating memory for write buffer");
906		if ((ffd = open((char *)buf, O_RDONLY, 0444)) == -1) {
907			warn("Can't open `%s' for reading", (char *)buf);
908			goto leave_ffs_write_file;
909		}
910	} else {
911		p = buf;
912	}
913
914	chunk = 0;
915	for (bufleft = DIP(din, size); bufleft > 0; bufleft -= chunk) {
916		chunk = MIN(bufleft, ffs_opts->bsize);
917		if (!isfile)
918			;
919		else if ((nread = read(ffd, fbuf, chunk)) == -1)
920			err(EXIT_FAILURE, "Reading `%s', %lld bytes to go",
921			    (char *)buf, (long long)bufleft);
922		else if (nread != chunk)
923			errx(EXIT_FAILURE, "Reading `%s', %lld bytes to go, "
924			    "read %zd bytes, expected %ju bytes, does "
925			    "metalog size= attribute mismatch source size?",
926			    (char *)buf, (long long)bufleft, nread,
927			    (uintmax_t)chunk);
928		else
929			p = fbuf;
930		offset = DIP(din, size) - bufleft;
931		if (debug & DEBUG_FS_WRITE_FILE_BLOCK)
932			printf(
933		"ffs_write_file: write %p offset %lld size %lld left %lld\n",
934			    p, (long long)offset,
935			    (long long)chunk, (long long)bufleft);
936	/*
937	 * XXX	if holey support is desired, do the check here
938	 *
939	 * XXX	might need to write out last bit in fragroundup
940	 *	sized chunk. however, ffs_balloc() handles this for us
941	 */
942		errno = ffs_balloc(&in, offset, chunk, &bp);
943 bad_ffs_write_file:
944		if (errno != 0)
945			err(1,
946			    "Writing inode %d (%s), bytes %lld + %lld",
947			    ino,
948			    isfile ? (char *)buf :
949			      inode_type(DIP(din, mode) & S_IFMT),
950			    (long long)offset, (long long)chunk);
951		memcpy(bp->b_data, p, chunk);
952		errno = bwrite(bp);
953		if (errno != 0)
954			goto bad_ffs_write_file;
955		brelse(bp);
956		if (!isfile)
957			p += chunk;
958	}
959
960 write_inode_and_leave:
961	ffs_write_inode(&in.i_din, in.i_number, fsopts);
962
963 leave_ffs_write_file:
964	if (fbuf)
965		free(fbuf);
966	if (ffd != -1)
967		close(ffd);
968}
969
970
971static void
972ffs_dump_dirbuf(dirbuf_t *dbuf, const char *dir, int needswap)
973{
974	doff_t		i;
975	struct direct	*de;
976	uint16_t	reclen;
977
978	assert (dbuf != NULL);
979	assert (dir != NULL);
980	printf("ffs_dump_dirbuf: dir %s size %d cur %d\n",
981	    dir, dbuf->size, dbuf->cur);
982
983	for (i = 0; i < dbuf->size; ) {
984		de = (struct direct *)(dbuf->buf + i);
985		reclen = ufs_rw16(de->d_reclen, needswap);
986		printf(
987	    " inode %4d %7s offset %4d reclen %3d namlen %3d name %s\n",
988		    ufs_rw32(de->d_ino, needswap),
989		    inode_type(DTTOIF(de->d_type)), i, reclen,
990		    de->d_namlen, de->d_name);
991		i += reclen;
992		assert(reclen > 0);
993	}
994}
995
996static void
997ffs_make_dirbuf(dirbuf_t *dbuf, const char *name, fsnode *node, int needswap)
998{
999	struct direct	de, *dp;
1000	uint16_t	llen, reclen;
1001	u_char		*newbuf;
1002
1003	assert (dbuf != NULL);
1004	assert (name != NULL);
1005	assert (node != NULL);
1006					/* create direct entry */
1007	(void)memset(&de, 0, sizeof(de));
1008	de.d_ino = ufs_rw32(node->inode->ino, needswap);
1009	de.d_type = IFTODT(node->type);
1010	de.d_namlen = (uint8_t)strlen(name);
1011	strcpy(de.d_name, name);
1012	reclen = DIRSIZ_SWAP(0, &de, needswap);
1013	de.d_reclen = ufs_rw16(reclen, needswap);
1014
1015	dp = (struct direct *)(dbuf->buf + dbuf->cur);
1016	llen = 0;
1017	if (dp != NULL)
1018		llen = DIRSIZ_SWAP(0, dp, needswap);
1019
1020	if (debug & DEBUG_FS_MAKE_DIRBUF)
1021		printf(
1022		    "ffs_make_dirbuf: dbuf siz %d cur %d lastlen %d\n"
1023		    "  ino %d type %d reclen %d namlen %d name %.30s\n",
1024		    dbuf->size, dbuf->cur, llen,
1025		    ufs_rw32(de.d_ino, needswap), de.d_type, reclen,
1026		    de.d_namlen, de.d_name);
1027
1028	if (reclen + dbuf->cur + llen > roundup(dbuf->size, DIRBLKSIZ)) {
1029		if (debug & DEBUG_FS_MAKE_DIRBUF)
1030			printf("ffs_make_dirbuf: growing buf to %d\n",
1031			    dbuf->size + DIRBLKSIZ);
1032		if ((newbuf = realloc(dbuf->buf, dbuf->size + DIRBLKSIZ)) == NULL)
1033			err(1, "Allocating memory for directory buffer");
1034		dbuf->buf = newbuf;
1035		dbuf->size += DIRBLKSIZ;
1036		memset(dbuf->buf + dbuf->size - DIRBLKSIZ, 0, DIRBLKSIZ);
1037		dbuf->cur = dbuf->size - DIRBLKSIZ;
1038	} else if (dp) {			/* shrink end of previous */
1039		dp->d_reclen = ufs_rw16(llen,needswap);
1040		dbuf->cur += llen;
1041	}
1042	dp = (struct direct *)(dbuf->buf + dbuf->cur);
1043	memcpy(dp, &de, reclen);
1044	dp->d_reclen = ufs_rw16(dbuf->size - dbuf->cur, needswap);
1045}
1046
1047/*
1048 * cribbed from sys/ufs/ffs/ffs_alloc.c
1049 */
1050static void
1051ffs_write_inode(union dinode *dp, uint32_t ino, const fsinfo_t *fsopts)
1052{
1053	char 		*buf;
1054	struct ufs1_dinode *dp1;
1055	struct ufs2_dinode *dp2, *dip;
1056	struct cg	*cgp;
1057	struct fs	*fs;
1058	int		cg, cgino, i;
1059	daddr_t		d;
1060	char		sbbuf[FFS_MAXBSIZE];
1061	int32_t		initediblk;
1062	ffs_opt_t	*ffs_opts = fsopts->fs_specific;
1063
1064	assert (dp != NULL);
1065	assert (ino > 0);
1066	assert (fsopts != NULL);
1067	assert (ffs_opts != NULL);
1068
1069	fs = (struct fs *)fsopts->superblock;
1070	cg = ino_to_cg(fs, ino);
1071	cgino = ino % fs->fs_ipg;
1072	if (debug & DEBUG_FS_WRITE_INODE)
1073		printf("ffs_write_inode: din %p ino %u cg %d cgino %d\n",
1074		    dp, ino, cg, cgino);
1075
1076	ffs_rdfs(fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize, &sbbuf,
1077	    fsopts);
1078	cgp = (struct cg *)sbbuf;
1079	if (!cg_chkmagic_swap(cgp, fsopts->needswap))
1080		errx(1, "ffs_write_inode: cg %d: bad magic number", cg);
1081
1082	assert (isclr(cg_inosused_swap(cgp, fsopts->needswap), cgino));
1083
1084	buf = malloc(fs->fs_bsize);
1085	if (buf == NULL)
1086		errx(1, "ffs_write_inode: cg %d: can't alloc inode block", cg);
1087
1088	dp1 = (struct ufs1_dinode *)buf;
1089	dp2 = (struct ufs2_dinode *)buf;
1090
1091	if (fs->fs_cstotal.cs_nifree == 0)
1092		errx(1, "ffs_write_inode: fs out of inodes for ino %u",
1093		    ino);
1094	if (fs->fs_cs(fs, cg).cs_nifree == 0)
1095		errx(1,
1096		    "ffs_write_inode: cg %d out of inodes for ino %u",
1097		    cg, ino);
1098	setbit(cg_inosused_swap(cgp, fsopts->needswap), cgino);
1099	ufs_add32(cgp->cg_cs.cs_nifree, -1, fsopts->needswap);
1100	fs->fs_cstotal.cs_nifree--;
1101	fs->fs_cs(fs, cg).cs_nifree--;
1102	if (S_ISDIR(DIP(dp, mode))) {
1103		ufs_add32(cgp->cg_cs.cs_ndir, 1, fsopts->needswap);
1104		fs->fs_cstotal.cs_ndir++;
1105		fs->fs_cs(fs, cg).cs_ndir++;
1106	}
1107
1108	/*
1109	 * Initialize inode blocks on the fly for UFS2.
1110	 */
1111	initediblk = ufs_rw32(cgp->cg_initediblk, fsopts->needswap);
1112	if (ffs_opts->version == 2 && cgino + INOPB(fs) > initediblk &&
1113	    initediblk < ufs_rw32(cgp->cg_niblk, fsopts->needswap)) {
1114		memset(buf, 0, fs->fs_bsize);
1115		dip = (struct ufs2_dinode *)buf;
1116		srandom(time(NULL));
1117		for (i = 0; i < INOPB(fs); i++) {
1118			dip->di_gen = random() / 2 + 1;
1119			dip++;
1120		}
1121		ffs_wtfs(fsbtodb(fs, ino_to_fsba(fs,
1122				  cg * fs->fs_ipg + initediblk)),
1123		    fs->fs_bsize, buf, fsopts);
1124		initediblk += INOPB(fs);
1125		cgp->cg_initediblk = ufs_rw32(initediblk, fsopts->needswap);
1126	}
1127
1128
1129	ffs_wtfs(fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize, &sbbuf,
1130	    fsopts);
1131
1132					/* now write inode */
1133	d = fsbtodb(fs, ino_to_fsba(fs, ino));
1134	ffs_rdfs(d, fs->fs_bsize, buf, fsopts);
1135	if (fsopts->needswap) {
1136		if (ffs_opts->version == 1)
1137			ffs_dinode1_swap(&dp->ffs1_din,
1138			    &dp1[ino_to_fsbo(fs, ino)]);
1139		else
1140			ffs_dinode2_swap(&dp->ffs2_din,
1141			    &dp2[ino_to_fsbo(fs, ino)]);
1142	} else {
1143		if (ffs_opts->version == 1)
1144			dp1[ino_to_fsbo(fs, ino)] = dp->ffs1_din;
1145		else
1146			dp2[ino_to_fsbo(fs, ino)] = dp->ffs2_din;
1147	}
1148	ffs_wtfs(d, fs->fs_bsize, buf, fsopts);
1149	free(buf);
1150}
1151
1152void
1153panic(const char *fmt, ...)
1154{
1155	va_list ap;
1156
1157	va_start(ap, fmt);
1158	vwarnx(fmt, ap);
1159	va_end(ap);
1160	exit(1);
1161}
1162