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