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