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