xref: /freebsd/usr.sbin/makefs/ffs.c (revision aa90fbed151de512ab6e59f75df009533a15751f)
1881e506bSEd Maste /*	$NetBSD: ffs.c,v 1.45 2011/10/09 22:49:26 christos Exp $	*/
2d347a0daSSam Leffler 
3df57947fSPedro F. Giffuni /*-
4df57947fSPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
5df57947fSPedro F. Giffuni  *
6d347a0daSSam Leffler  * Copyright (c) 2001 Wasabi Systems, Inc.
7d347a0daSSam Leffler  * All rights reserved.
8d347a0daSSam Leffler  *
9d347a0daSSam Leffler  * Written by Luke Mewburn for Wasabi Systems, Inc.
10d347a0daSSam Leffler  *
11d347a0daSSam Leffler  * Redistribution and use in source and binary forms, with or without
12d347a0daSSam Leffler  * modification, are permitted provided that the following conditions
13d347a0daSSam Leffler  * are met:
14d347a0daSSam Leffler  * 1. Redistributions of source code must retain the above copyright
15d347a0daSSam Leffler  *    notice, this list of conditions and the following disclaimer.
16d347a0daSSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
17d347a0daSSam Leffler  *    notice, this list of conditions and the following disclaimer in the
18d347a0daSSam Leffler  *    documentation and/or other materials provided with the distribution.
19d347a0daSSam Leffler  * 3. All advertising materials mentioning features or use of this software
20d347a0daSSam Leffler  *    must display the following acknowledgement:
21d347a0daSSam Leffler  *      This product includes software developed for the NetBSD Project by
22d347a0daSSam Leffler  *      Wasabi Systems, Inc.
23d347a0daSSam Leffler  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
24d347a0daSSam Leffler  *    or promote products derived from this software without specific prior
25d347a0daSSam Leffler  *    written permission.
26d347a0daSSam Leffler  *
27d347a0daSSam Leffler  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
28d347a0daSSam Leffler  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29d347a0daSSam Leffler  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30d347a0daSSam Leffler  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
31d347a0daSSam Leffler  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32d347a0daSSam Leffler  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33d347a0daSSam Leffler  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34d347a0daSSam Leffler  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35d347a0daSSam Leffler  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36d347a0daSSam Leffler  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37d347a0daSSam Leffler  * POSSIBILITY OF SUCH DAMAGE.
38d347a0daSSam Leffler  */
39d347a0daSSam Leffler /*
40d347a0daSSam Leffler  * Copyright (c) 1982, 1986, 1989, 1993
41d347a0daSSam Leffler  *	The Regents of the University of California.  All rights reserved.
42d347a0daSSam Leffler  *
43d347a0daSSam Leffler  * Redistribution and use in source and binary forms, with or without
44d347a0daSSam Leffler  * modification, are permitted provided that the following conditions
45d347a0daSSam Leffler  * are met:
46d347a0daSSam Leffler  * 1. Redistributions of source code must retain the above copyright
47d347a0daSSam Leffler  *    notice, this list of conditions and the following disclaimer.
48d347a0daSSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
49d347a0daSSam Leffler  *    notice, this list of conditions and the following disclaimer in the
50d347a0daSSam Leffler  *    documentation and/or other materials provided with the distribution.
51d347a0daSSam Leffler  * 3. Neither the name of the University nor the names of its contributors
52d347a0daSSam Leffler  *    may be used to endorse or promote products derived from this software
53d347a0daSSam Leffler  *    without specific prior written permission.
54d347a0daSSam Leffler  *
55d347a0daSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56d347a0daSSam Leffler  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57d347a0daSSam Leffler  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58d347a0daSSam Leffler  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59d347a0daSSam Leffler  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60d347a0daSSam Leffler  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61d347a0daSSam Leffler  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62d347a0daSSam Leffler  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63d347a0daSSam Leffler  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64d347a0daSSam Leffler  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65d347a0daSSam Leffler  * SUCH DAMAGE.
66d347a0daSSam Leffler  */
67d347a0daSSam Leffler 
68d347a0daSSam Leffler #include <sys/cdefs.h>
695f401182SAlex Richardson #if HAVE_NBTOOL_CONFIG_H
705f401182SAlex Richardson #include "nbtool_config.h"
715f401182SAlex Richardson #endif
725f401182SAlex Richardson 
73d347a0daSSam Leffler #include <sys/param.h>
74d347a0daSSam Leffler 
75d347a0daSSam Leffler #include <sys/mount.h>
76d347a0daSSam Leffler 
77d347a0daSSam Leffler #include <assert.h>
78d347a0daSSam Leffler #include <errno.h>
79d347a0daSSam Leffler #include <fcntl.h>
80d347a0daSSam Leffler #include <stdarg.h>
8101a0f853SOlivier Houchard #include <stdint.h>
82d347a0daSSam Leffler #include <stdio.h>
83d347a0daSSam Leffler #include <stdlib.h>
84d347a0daSSam Leffler #include <string.h>
857649cb00SKirk McKusick #include <time.h>
86d347a0daSSam Leffler #include <unistd.h>
875f5598b1SEd Maste #include <util.h>
88d347a0daSSam Leffler 
8901a0f853SOlivier Houchard #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS
9001a0f853SOlivier Houchard #include <sys/statvfs.h>
9101a0f853SOlivier Houchard #endif
92d347a0daSSam Leffler 
93d347a0daSSam Leffler #include <ufs/ufs/dinode.h>
94d347a0daSSam Leffler #include <ufs/ufs/dir.h>
95d347a0daSSam Leffler #include <ufs/ffs/fs.h>
96d347a0daSSam Leffler 
97d347a0daSSam Leffler #include "ffs/ufs_bswap.h"
98d347a0daSSam Leffler #include "ffs/ufs_inode.h"
99d347a0daSSam Leffler #include "ffs/newfs_extern.h"
100d347a0daSSam Leffler #include "ffs/ffs_extern.h"
101d347a0daSSam Leffler 
102d485c77fSKonstantin Belousov #undef clrbuf
103d485c77fSKonstantin Belousov #include "makefs.h"
104d485c77fSKonstantin Belousov #include "ffs.h"
105d485c77fSKonstantin Belousov 
106d347a0daSSam Leffler #undef DIP
107d347a0daSSam Leffler #define DIP(dp, field) \
10801a0f853SOlivier Houchard 	((ffs_opts->version == 1) ? \
109*aa90fbedSKirk McKusick 	(dp)->dp1.di_##field : (dp)->dp2.di_##field)
110d347a0daSSam Leffler 
111d347a0daSSam Leffler /*
112d347a0daSSam Leffler  * Various file system defaults (cribbed from newfs(8)).
113d347a0daSSam Leffler  */
114c93ff841SEd Maste #define	DFL_FRAGSIZE		4096		/* fragment size */
115c93ff841SEd Maste #define	DFL_BLKSIZE		32768		/* block size */
116d347a0daSSam Leffler #define	DFL_SECSIZE		512		/* sector size */
117d347a0daSSam Leffler #define	DFL_CYLSPERGROUP	65536		/* cylinders per group */
118d347a0daSSam Leffler #define	DFL_FRAGSPERINODE	4		/* fragments per inode */
119d347a0daSSam Leffler #define	DFL_ROTDELAY		0		/* rotational delay */
120d347a0daSSam Leffler #define	DFL_NRPOS		1		/* rotational positions */
121d347a0daSSam Leffler #define	DFL_RPM			3600		/* rpm of disk */
122d347a0daSSam Leffler #define	DFL_NSECTORS		64		/* # of sectors */
123d347a0daSSam Leffler #define	DFL_NTRACKS		16		/* # of tracks */
124d347a0daSSam Leffler 
125d347a0daSSam Leffler 
126d347a0daSSam Leffler typedef struct {
127d347a0daSSam Leffler 	u_char		*buf;		/* buf for directory */
128d347a0daSSam Leffler 	doff_t		size;		/* full size of buf */
129d347a0daSSam Leffler 	doff_t		cur;		/* offset of current entry */
130d347a0daSSam Leffler } dirbuf_t;
131d347a0daSSam Leffler 
132d347a0daSSam Leffler 
133d347a0daSSam Leffler static	int	ffs_create_image(const char *, fsinfo_t *);
134d347a0daSSam Leffler static	void	ffs_dump_fsinfo(fsinfo_t *);
135d347a0daSSam Leffler static	void	ffs_dump_dirbuf(dirbuf_t *, const char *, int);
136d347a0daSSam Leffler static	void	ffs_make_dirbuf(dirbuf_t *, const char *, fsnode *, int);
137d347a0daSSam Leffler static	int	ffs_populate_dir(const char *, fsnode *, fsinfo_t *);
138d347a0daSSam Leffler static	void	ffs_size_dir(fsnode *, fsinfo_t *);
139d347a0daSSam Leffler static	void	ffs_validate(const char *, fsnode *, fsinfo_t *);
140d347a0daSSam Leffler static	void	ffs_write_file(union dinode *, uint32_t, void *, fsinfo_t *);
141d347a0daSSam Leffler static	void	ffs_write_inode(union dinode *, uint32_t, const fsinfo_t *);
142d347a0daSSam Leffler static  void	*ffs_build_dinode1(struct ufs1_dinode *, dirbuf_t *, fsnode *,
143d347a0daSSam Leffler 				 fsnode *, fsinfo_t *);
144d347a0daSSam Leffler static  void	*ffs_build_dinode2(struct ufs2_dinode *, dirbuf_t *, fsnode *,
145d347a0daSSam Leffler 				 fsnode *, fsinfo_t *);
146d347a0daSSam Leffler 
147d347a0daSSam Leffler 
1483df5ecacSUlrich Spörlein 	/* publicly visible functions */
149d347a0daSSam Leffler 
15001a0f853SOlivier Houchard void
15101a0f853SOlivier Houchard ffs_prep_opts(fsinfo_t *fsopts)
15201a0f853SOlivier Houchard {
1535f5598b1SEd Maste 	ffs_opt_t *ffs_opts = ecalloc(1, sizeof(*ffs_opts));
15401a0f853SOlivier Houchard 
155776c6824SEd Maste 	const option_t ffs_options[] = {
156776c6824SEd Maste 	    { 'b', "bsize", &ffs_opts->bsize, OPT_INT32,
157776c6824SEd Maste 	      1, INT_MAX, "block size" },
158776c6824SEd Maste 	    { 'f', "fsize", &ffs_opts->fsize, OPT_INT32,
159776c6824SEd Maste 	      1, INT_MAX, "fragment size" },
160776c6824SEd Maste 	    { 'd', "density", &ffs_opts->density, OPT_INT32,
161776c6824SEd Maste 	      1, INT_MAX, "bytes per inode" },
162776c6824SEd Maste 	    { 'm', "minfree", &ffs_opts->minfree, OPT_INT32,
163776c6824SEd Maste 	      0, 99, "minfree" },
164776c6824SEd Maste 	    { 'M', "maxbpg", &ffs_opts->maxbpg, OPT_INT32,
165776c6824SEd Maste 	      1, INT_MAX, "max blocks per file in a cg" },
166776c6824SEd Maste 	    { 'a', "avgfilesize", &ffs_opts->avgfilesize, OPT_INT32,
167776c6824SEd Maste 	      1, INT_MAX, "expected average file size" },
168776c6824SEd Maste 	    { 'n', "avgfpdir", &ffs_opts->avgfpdir, OPT_INT32,
169776c6824SEd Maste 	      1, INT_MAX, "expected # of files per directory" },
170776c6824SEd Maste 	    { 'x', "extent", &ffs_opts->maxbsize, OPT_INT32,
171776c6824SEd Maste 	      1, INT_MAX, "maximum # extent size" },
172776c6824SEd Maste 	    { 'g', "maxbpcg", &ffs_opts->maxblkspercg, OPT_INT32,
173776c6824SEd Maste 	      1, INT_MAX, "max # of blocks per group" },
174776c6824SEd Maste 	    { 'v', "version", &ffs_opts->version, OPT_INT32,
175776c6824SEd Maste 	      1, 2, "UFS version" },
176776c6824SEd Maste 	    { 'o', "optimization", NULL, OPT_STRBUF,
177776c6824SEd Maste 	      0, 0, "Optimization (time|space)" },
178776c6824SEd Maste 	    { 'l', "label", ffs_opts->label, OPT_STRARRAY,
179776c6824SEd Maste 	      1, sizeof(ffs_opts->label), "UFS label" },
180d91e6117SEmmanuel Vadot 	    { 's', "softupdates", &ffs_opts->softupdates, OPT_INT32,
181d91e6117SEmmanuel Vadot 	      0, 1, "enable softupdates" },
182776c6824SEd Maste 	    { .name = NULL }
183776c6824SEd Maste 	};
18401a0f853SOlivier Houchard 
18501a0f853SOlivier Houchard 	ffs_opts->bsize= -1;
18601a0f853SOlivier Houchard 	ffs_opts->fsize= -1;
18701a0f853SOlivier Houchard 	ffs_opts->cpg= -1;
18801a0f853SOlivier Houchard 	ffs_opts->density= -1;
189afb6a168SNathan Whitehorn 	ffs_opts->min_inodes= false;
19001a0f853SOlivier Houchard 	ffs_opts->minfree= -1;
19101a0f853SOlivier Houchard 	ffs_opts->optimization= -1;
19201a0f853SOlivier Houchard 	ffs_opts->maxcontig= -1;
19301a0f853SOlivier Houchard 	ffs_opts->maxbpg= -1;
19401a0f853SOlivier Houchard 	ffs_opts->avgfilesize= -1;
19501a0f853SOlivier Houchard 	ffs_opts->avgfpdir= -1;
19601a0f853SOlivier Houchard 	ffs_opts->version = 1;
197d91e6117SEmmanuel Vadot 	ffs_opts->softupdates = 0;
198776c6824SEd Maste 
199776c6824SEd Maste 	fsopts->fs_specific = ffs_opts;
200776c6824SEd Maste 	fsopts->fs_options = copy_opts(ffs_options);
20101a0f853SOlivier Houchard }
20201a0f853SOlivier Houchard 
20301a0f853SOlivier Houchard void
20401a0f853SOlivier Houchard ffs_cleanup_opts(fsinfo_t *fsopts)
20501a0f853SOlivier Houchard {
20601a0f853SOlivier Houchard 	free(fsopts->fs_specific);
207776c6824SEd Maste 	free(fsopts->fs_options);
20801a0f853SOlivier Houchard }
20901a0f853SOlivier Houchard 
210d347a0daSSam Leffler int
211d347a0daSSam Leffler ffs_parse_opts(const char *option, fsinfo_t *fsopts)
212d347a0daSSam Leffler {
21301a0f853SOlivier Houchard 	ffs_opt_t	*ffs_opts = fsopts->fs_specific;
214776c6824SEd Maste 	option_t *ffs_options = fsopts->fs_options;
215776c6824SEd Maste 	char buf[1024];
21601a0f853SOlivier Houchard 
217d347a0daSSam Leffler 	int	rv;
218d347a0daSSam Leffler 
219d347a0daSSam Leffler 	assert(option != NULL);
220d347a0daSSam Leffler 	assert(fsopts != NULL);
22101a0f853SOlivier Houchard 	assert(ffs_opts != NULL);
222d347a0daSSam Leffler 
223d347a0daSSam Leffler 	if (debug & DEBUG_FS_PARSE_OPTS)
224d347a0daSSam Leffler 		printf("ffs_parse_opts: got `%s'\n", option);
225d347a0daSSam Leffler 
226776c6824SEd Maste 	rv = set_option(ffs_options, option, buf, sizeof(buf));
227776c6824SEd Maste 	if (rv == -1)
228776c6824SEd Maste 		return 0;
229d347a0daSSam Leffler 
230776c6824SEd Maste 	if (ffs_options[rv].name == NULL)
231776c6824SEd Maste 		abort();
232d347a0daSSam Leffler 
233776c6824SEd Maste 	switch (ffs_options[rv].letter) {
234776c6824SEd Maste 	case 'o':
235776c6824SEd Maste 		if (strcmp(buf, "time") == 0) {
23601a0f853SOlivier Houchard 			ffs_opts->optimization = FS_OPTTIME;
237776c6824SEd Maste 		} else if (strcmp(buf, "space") == 0) {
23801a0f853SOlivier Houchard 			ffs_opts->optimization = FS_OPTSPACE;
239d347a0daSSam Leffler 		} else {
240776c6824SEd Maste 			warnx("Invalid optimization `%s'", buf);
241776c6824SEd Maste 			return 0;
242d347a0daSSam Leffler 		}
243776c6824SEd Maste 		break;
244776c6824SEd Maste 	default:
245776c6824SEd Maste 		break;
246776c6824SEd Maste 	}
247776c6824SEd Maste 	return 1;
248d347a0daSSam Leffler }
249d347a0daSSam Leffler 
250d347a0daSSam Leffler 
251d347a0daSSam Leffler void
252d347a0daSSam Leffler ffs_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts)
253d347a0daSSam Leffler {
254d347a0daSSam Leffler 	struct fs	*superblock;
255d347a0daSSam Leffler 	struct timeval	start;
256d347a0daSSam Leffler 
257d347a0daSSam Leffler 	assert(image != NULL);
258d347a0daSSam Leffler 	assert(dir != NULL);
259d347a0daSSam Leffler 	assert(root != NULL);
260d347a0daSSam Leffler 	assert(fsopts != NULL);
261d347a0daSSam Leffler 
262d347a0daSSam Leffler 	if (debug & DEBUG_FS_MAKEFS)
263d347a0daSSam Leffler 		printf("ffs_makefs: image %s directory %s root %p\n",
264d347a0daSSam Leffler 		    image, dir, root);
265d347a0daSSam Leffler 
266afb6a168SNathan Whitehorn 		/* if user wants no free space, use minimum number of inodes */
267afb6a168SNathan Whitehorn 	if (fsopts->minsize == 0 && fsopts->freeblockpc == 0 &&
268afb6a168SNathan Whitehorn 	    fsopts->freeblocks == 0)
269afb6a168SNathan Whitehorn 		((ffs_opt_t *)fsopts->fs_specific)->min_inodes = true;
270afb6a168SNathan Whitehorn 
271d347a0daSSam Leffler 		/* validate tree and options */
272d347a0daSSam Leffler 	TIMER_START(start);
273d347a0daSSam Leffler 	ffs_validate(dir, root, fsopts);
274d347a0daSSam Leffler 	TIMER_RESULTS(start, "ffs_validate");
275d347a0daSSam Leffler 
276d347a0daSSam Leffler 	printf("Calculated size of `%s': %lld bytes, %lld inodes\n",
277d347a0daSSam Leffler 	    image, (long long)fsopts->size, (long long)fsopts->inodes);
278d347a0daSSam Leffler 
279d347a0daSSam Leffler 		/* create image */
280d347a0daSSam Leffler 	TIMER_START(start);
281d347a0daSSam Leffler 	if (ffs_create_image(image, fsopts) == -1)
282d347a0daSSam Leffler 		errx(1, "Image file `%s' not created.", image);
283d347a0daSSam Leffler 	TIMER_RESULTS(start, "ffs_create_image");
284d347a0daSSam Leffler 
2851dc349abSEd Maste 	fsopts->curinode = UFS_ROOTINO;
286d347a0daSSam Leffler 
287d347a0daSSam Leffler 	if (debug & DEBUG_FS_MAKEFS)
288d347a0daSSam Leffler 		putchar('\n');
289d347a0daSSam Leffler 
290d347a0daSSam Leffler 		/* populate image */
291d347a0daSSam Leffler 	printf("Populating `%s'\n", image);
292d347a0daSSam Leffler 	TIMER_START(start);
293d347a0daSSam Leffler 	if (! ffs_populate_dir(dir, root, fsopts))
294d347a0daSSam Leffler 		errx(1, "Image file `%s' not populated.", image);
295d347a0daSSam Leffler 	TIMER_RESULTS(start, "ffs_populate_dir");
296d347a0daSSam Leffler 
297d347a0daSSam Leffler 		/* ensure no outstanding buffers remain */
298d347a0daSSam Leffler 	if (debug & DEBUG_FS_MAKEFS)
299d347a0daSSam Leffler 		bcleanup();
300d347a0daSSam Leffler 
301d347a0daSSam Leffler 		/* update various superblock parameters */
302d347a0daSSam Leffler 	superblock = fsopts->superblock;
303d347a0daSSam Leffler 	superblock->fs_fmod = 0;
304d347a0daSSam Leffler 	superblock->fs_old_cstotal.cs_ndir   = superblock->fs_cstotal.cs_ndir;
305d347a0daSSam Leffler 	superblock->fs_old_cstotal.cs_nbfree = superblock->fs_cstotal.cs_nbfree;
306d347a0daSSam Leffler 	superblock->fs_old_cstotal.cs_nifree = superblock->fs_cstotal.cs_nifree;
307d347a0daSSam Leffler 	superblock->fs_old_cstotal.cs_nffree = superblock->fs_cstotal.cs_nffree;
308d347a0daSSam Leffler 
309d347a0daSSam Leffler 		/* write out superblock; image is now complete */
310d347a0daSSam Leffler 	ffs_write_superblock(fsopts->superblock, fsopts);
311d347a0daSSam Leffler 	if (close(fsopts->fd) == -1)
312d347a0daSSam Leffler 		err(1, "Closing `%s'", image);
313d347a0daSSam Leffler 	fsopts->fd = -1;
314d347a0daSSam Leffler 	printf("Image `%s' complete\n", image);
315d347a0daSSam Leffler }
316d347a0daSSam Leffler 
317d347a0daSSam Leffler 	/* end of public functions */
318d347a0daSSam Leffler 
319d347a0daSSam Leffler 
320d347a0daSSam Leffler static void
321d347a0daSSam Leffler ffs_validate(const char *dir, fsnode *root, fsinfo_t *fsopts)
322d347a0daSSam Leffler {
3235f401182SAlex Richardson #ifdef notyet
324d347a0daSSam Leffler 	int32_t	spc, nspf, ncyl, fssize;
325d347a0daSSam Leffler #endif
32601a0f853SOlivier Houchard 	ffs_opt_t	*ffs_opts = fsopts->fs_specific;
327d347a0daSSam Leffler 
328d347a0daSSam Leffler 	assert(dir != NULL);
329d347a0daSSam Leffler 	assert(root != NULL);
330d347a0daSSam Leffler 	assert(fsopts != NULL);
33101a0f853SOlivier Houchard 	assert(ffs_opts != NULL);
332d347a0daSSam Leffler 
333d347a0daSSam Leffler 	if (debug & DEBUG_FS_VALIDATE) {
334d347a0daSSam Leffler 		printf("ffs_validate: before defaults set:\n");
335d347a0daSSam Leffler 		ffs_dump_fsinfo(fsopts);
336d347a0daSSam Leffler 	}
337d347a0daSSam Leffler 
338d347a0daSSam Leffler 		/* set FFS defaults */
339d347a0daSSam Leffler 	if (fsopts->sectorsize == -1)
340d347a0daSSam Leffler 		fsopts->sectorsize = DFL_SECSIZE;
341176b9e0dSEd Maste 	if (fsopts->sectorsize != DFL_SECSIZE)
342176b9e0dSEd Maste 		warnx("sectorsize %d may produce nonfunctional image",
343176b9e0dSEd Maste 		    fsopts->sectorsize);
34401a0f853SOlivier Houchard 	if (ffs_opts->fsize == -1)
34501a0f853SOlivier Houchard 		ffs_opts->fsize = MAX(DFL_FRAGSIZE, fsopts->sectorsize);
34601a0f853SOlivier Houchard 	if (ffs_opts->bsize == -1)
34701a0f853SOlivier Houchard 		ffs_opts->bsize = MIN(DFL_BLKSIZE, 8 * ffs_opts->fsize);
34801a0f853SOlivier Houchard 	if (ffs_opts->cpg == -1)
34901a0f853SOlivier Houchard 		ffs_opts->cpg = DFL_CYLSPERGROUP;
350d347a0daSSam Leffler 	else
35101a0f853SOlivier Houchard 		ffs_opts->cpgflg = 1;
352d347a0daSSam Leffler 				/* fsopts->density is set below */
35301a0f853SOlivier Houchard 	if (ffs_opts->nsectors == -1)
35401a0f853SOlivier Houchard 		ffs_opts->nsectors = DFL_NSECTORS;
35501a0f853SOlivier Houchard 	if (ffs_opts->minfree == -1)
35601a0f853SOlivier Houchard 		ffs_opts->minfree = MINFREE;
35701a0f853SOlivier Houchard 	if (ffs_opts->optimization == -1)
35801a0f853SOlivier Houchard 		ffs_opts->optimization = DEFAULTOPT;
35901a0f853SOlivier Houchard 	if (ffs_opts->maxcontig == -1)
36001a0f853SOlivier Houchard 		ffs_opts->maxcontig =
36101a0f853SOlivier Houchard 		    MAX(1, MIN(MAXPHYS, FFS_MAXBSIZE) / ffs_opts->bsize);
362d347a0daSSam Leffler 	/* XXX ondisk32 */
36301a0f853SOlivier Houchard 	if (ffs_opts->maxbpg == -1)
36401a0f853SOlivier Houchard 		ffs_opts->maxbpg = ffs_opts->bsize / sizeof(int32_t);
36501a0f853SOlivier Houchard 	if (ffs_opts->avgfilesize == -1)
36601a0f853SOlivier Houchard 		ffs_opts->avgfilesize = AVFILESIZ;
36701a0f853SOlivier Houchard 	if (ffs_opts->avgfpdir == -1)
36801a0f853SOlivier Houchard 		ffs_opts->avgfpdir = AFPDIR;
369d347a0daSSam Leffler 
3709d5a327fSDavide Italiano 	if (fsopts->maxsize > 0 &&
3719d5a327fSDavide Italiano 	    roundup(fsopts->minsize, ffs_opts->bsize) > fsopts->maxsize)
3725ad283b3SJuli Mallett 		errx(1, "`%s' minsize of %lld rounded up to ffs bsize of %d "
3735ad283b3SJuli Mallett 		    "exceeds maxsize %lld.  Lower bsize, or round the minimum "
3745ad283b3SJuli Mallett 		    "and maximum sizes to bsize.", dir,
3755ad283b3SJuli Mallett 		    (long long)fsopts->minsize, ffs_opts->bsize,
3765ad283b3SJuli Mallett 		    (long long)fsopts->maxsize);
3775ad283b3SJuli Mallett 
378d347a0daSSam Leffler 		/* calculate size of tree */
379d347a0daSSam Leffler 	ffs_size_dir(root, fsopts);
3801dc349abSEd Maste 	fsopts->inodes += UFS_ROOTINO;		/* include first two inodes */
381d347a0daSSam Leffler 
382d347a0daSSam Leffler 	if (debug & DEBUG_FS_VALIDATE)
383d347a0daSSam Leffler 		printf("ffs_validate: size of tree: %lld bytes, %lld inodes\n",
384d347a0daSSam Leffler 		    (long long)fsopts->size, (long long)fsopts->inodes);
385d347a0daSSam Leffler 
386d347a0daSSam Leffler 		/* add requested slop */
387d347a0daSSam Leffler 	fsopts->size += fsopts->freeblocks;
388d347a0daSSam Leffler 	fsopts->inodes += fsopts->freefiles;
389d347a0daSSam Leffler 	if (fsopts->freefilepc > 0)
390d347a0daSSam Leffler 		fsopts->inodes =
391d347a0daSSam Leffler 		    fsopts->inodes * (100 + fsopts->freefilepc) / 100;
392d347a0daSSam Leffler 	if (fsopts->freeblockpc > 0)
393d347a0daSSam Leffler 		fsopts->size =
394d347a0daSSam Leffler 		    fsopts->size * (100 + fsopts->freeblockpc) / 100;
395d347a0daSSam Leffler 
396d347a0daSSam Leffler 	/*
397ecdc04d0SAlan Somers 	 * Add space needed for superblock, cylblock and to store inodes.
398ecdc04d0SAlan Somers 	 * All of those segments are aligned to the block size.
399ecdc04d0SAlan Somers 	 * XXX: This has to match calculations done in ffs_mkfs.
400d347a0daSSam Leffler 	 */
401ecdc04d0SAlan Somers 	if (ffs_opts->version == 1) {
402ecdc04d0SAlan Somers 		fsopts->size +=
403ecdc04d0SAlan Somers 		    roundup(SBLOCK_UFS1 + SBLOCKSIZE, ffs_opts->bsize);
404ecdc04d0SAlan Somers 		fsopts->size += roundup(SBLOCKSIZE, ffs_opts->bsize);
405ecdc04d0SAlan Somers 		fsopts->size += ffs_opts->bsize;
406ecdc04d0SAlan Somers 		fsopts->size +=  DINODE1_SIZE *
407ecdc04d0SAlan Somers 		    roundup(fsopts->inodes, ffs_opts->bsize / DINODE1_SIZE);
408ecdc04d0SAlan Somers 	} else {
409ecdc04d0SAlan Somers 		fsopts->size +=
410ecdc04d0SAlan Somers 		    roundup(SBLOCK_UFS2 + SBLOCKSIZE, ffs_opts->bsize);
411ecdc04d0SAlan Somers 		fsopts->size += roundup(SBLOCKSIZE, ffs_opts->bsize);
412ecdc04d0SAlan Somers 		fsopts->size += ffs_opts->bsize;
413ecdc04d0SAlan Somers 		fsopts->size += DINODE2_SIZE *
414ecdc04d0SAlan Somers 		    roundup(fsopts->inodes, ffs_opts->bsize / DINODE2_SIZE);
415ecdc04d0SAlan Somers 	}
416d347a0daSSam Leffler 
417d347a0daSSam Leffler 		/* add minfree */
41801a0f853SOlivier Houchard 	if (ffs_opts->minfree > 0)
419d347a0daSSam Leffler 		fsopts->size =
42001a0f853SOlivier Houchard 		    fsopts->size * (100 + ffs_opts->minfree) / 100;
421d347a0daSSam Leffler 	/*
422d347a0daSSam Leffler 	 * XXX	any other fs slop to add, such as csum's, bitmaps, etc ??
423d347a0daSSam Leffler 	 */
424d347a0daSSam Leffler 
425d347a0daSSam Leffler 	if (fsopts->size < fsopts->minsize)	/* ensure meets minimum size */
426d347a0daSSam Leffler 		fsopts->size = fsopts->minsize;
427d347a0daSSam Leffler 
428d347a0daSSam Leffler 		/* round up to the next block */
42901a0f853SOlivier Houchard 	fsopts->size = roundup(fsopts->size, ffs_opts->bsize);
430d347a0daSSam Leffler 
431a08b904cSAdrian Chadd 		/* round up to requested block size, if any */
432a08b904cSAdrian Chadd 	if (fsopts->roundup > 0)
433a08b904cSAdrian Chadd 		fsopts->size = roundup(fsopts->size, fsopts->roundup);
434a08b904cSAdrian Chadd 
435afb6a168SNathan Whitehorn 		/* calculate density to just fit inodes if no free space */
43601a0f853SOlivier Houchard 	if (ffs_opts->density == -1)
43701a0f853SOlivier Houchard 		ffs_opts->density = fsopts->size / fsopts->inodes + 1;
438d347a0daSSam Leffler 
439d347a0daSSam Leffler 	if (debug & DEBUG_FS_VALIDATE) {
440d347a0daSSam Leffler 		printf("ffs_validate: after defaults set:\n");
441d347a0daSSam Leffler 		ffs_dump_fsinfo(fsopts);
442d347a0daSSam Leffler 		printf("ffs_validate: dir %s; %lld bytes, %lld inodes\n",
443d347a0daSSam Leffler 		    dir, (long long)fsopts->size, (long long)fsopts->inodes);
444d347a0daSSam Leffler 	}
44501a0f853SOlivier Houchard 		/* now check calculated sizes vs requested sizes */
44601a0f853SOlivier Houchard 	if (fsopts->maxsize > 0 && fsopts->size > fsopts->maxsize) {
44701a0f853SOlivier Houchard 		errx(1, "`%s' size of %lld is larger than the maxsize of %lld.",
44801a0f853SOlivier Houchard 		    dir, (long long)fsopts->size, (long long)fsopts->maxsize);
44901a0f853SOlivier Houchard 	}
450d347a0daSSam Leffler }
451d347a0daSSam Leffler 
452d347a0daSSam Leffler 
453d347a0daSSam Leffler static void
454d347a0daSSam Leffler ffs_dump_fsinfo(fsinfo_t *f)
455d347a0daSSam Leffler {
456d347a0daSSam Leffler 
45701a0f853SOlivier Houchard 	ffs_opt_t	*fs = f->fs_specific;
45801a0f853SOlivier Houchard 
459d347a0daSSam Leffler 	printf("fsopts at %p\n", f);
460d347a0daSSam Leffler 
461d347a0daSSam Leffler 	printf("\tsize %lld, inodes %lld, curinode %u\n",
462d347a0daSSam Leffler 	    (long long)f->size, (long long)f->inodes, f->curinode);
463d347a0daSSam Leffler 
464d347a0daSSam Leffler 	printf("\tminsize %lld, maxsize %lld\n",
465d347a0daSSam Leffler 	    (long long)f->minsize, (long long)f->maxsize);
466d347a0daSSam Leffler 	printf("\tfree files %lld, freefile %% %d\n",
467d347a0daSSam Leffler 	    (long long)f->freefiles, f->freefilepc);
468d347a0daSSam Leffler 	printf("\tfree blocks %lld, freeblock %% %d\n",
469d347a0daSSam Leffler 	    (long long)f->freeblocks, f->freeblockpc);
470d347a0daSSam Leffler 	printf("\tneedswap %d, sectorsize %d\n", f->needswap, f->sectorsize);
471d347a0daSSam Leffler 
472d347a0daSSam Leffler 	printf("\tbsize %d, fsize %d, cpg %d, density %d\n",
47301a0f853SOlivier Houchard 	    fs->bsize, fs->fsize, fs->cpg, fs->density);
474d347a0daSSam Leffler 	printf("\tnsectors %d, rpm %d, minfree %d\n",
47501a0f853SOlivier Houchard 	    fs->nsectors, fs->rpm, fs->minfree);
476d347a0daSSam Leffler 	printf("\tmaxcontig %d, maxbpg %d\n",
47701a0f853SOlivier Houchard 	    fs->maxcontig, fs->maxbpg);
478d347a0daSSam Leffler 	printf("\toptimization %s\n",
47901a0f853SOlivier Houchard 	    fs->optimization == FS_OPTSPACE ? "space" : "time");
480d347a0daSSam Leffler }
481d347a0daSSam Leffler 
482d347a0daSSam Leffler 
483d347a0daSSam Leffler static int
484d347a0daSSam Leffler ffs_create_image(const char *image, fsinfo_t *fsopts)
485d347a0daSSam Leffler {
48601a0f853SOlivier Houchard #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS
487d347a0daSSam Leffler 	struct statvfs	sfs;
488d347a0daSSam Leffler #endif
489d347a0daSSam Leffler 	struct fs	*fs;
490d347a0daSSam Leffler 	char	*buf;
491d347a0daSSam Leffler 	int	i, bufsize;
492d347a0daSSam Leffler 	off_t	bufrem;
493b79f050aSEd Maste 	int	oflags = O_RDWR | O_CREAT;
4947b03d164SEd Maste 	time_t	tstamp;
495d347a0daSSam Leffler 
496d347a0daSSam Leffler 	assert (image != NULL);
497d347a0daSSam Leffler 	assert (fsopts != NULL);
498d347a0daSSam Leffler 
499d347a0daSSam Leffler 		/* create image */
500b79f050aSEd Maste 	if (fsopts->offset == 0)
501b79f050aSEd Maste 		oflags |= O_TRUNC;
5026160cc37SEd Maste 	if ((fsopts->fd = open(image, oflags, 0666)) == -1) {
503d347a0daSSam Leffler 		warn("Can't open `%s' for writing", image);
504d347a0daSSam Leffler 		return (-1);
505d347a0daSSam Leffler 	}
506d347a0daSSam Leffler 
507d347a0daSSam Leffler 		/* zero image */
50801a0f853SOlivier Houchard #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS
509d347a0daSSam Leffler 	if (fstatvfs(fsopts->fd, &sfs) == -1) {
510d347a0daSSam Leffler #endif
511d347a0daSSam Leffler 		bufsize = 8192;
51201a0f853SOlivier Houchard #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS
513d347a0daSSam Leffler 		warn("can't fstatvfs `%s', using default %d byte chunk",
514d347a0daSSam Leffler 		    image, bufsize);
515d347a0daSSam Leffler 	} else
516d347a0daSSam Leffler 		bufsize = sfs.f_iosize;
517d347a0daSSam Leffler #endif
518d347a0daSSam Leffler 	bufrem = fsopts->size;
51999c841b1SHiroki Sato 	if (fsopts->sparse) {
52099c841b1SHiroki Sato 		if (ftruncate(fsopts->fd, bufrem) == -1) {
521870952f5SEd Maste 			warn("sparse option disabled.");
52299c841b1SHiroki Sato 			fsopts->sparse = 0;
52399c841b1SHiroki Sato 		}
52499c841b1SHiroki Sato 	}
52599c841b1SHiroki Sato 	if (fsopts->sparse) {
52699c841b1SHiroki Sato 		/* File truncated at bufrem. Remaining is 0 */
52799c841b1SHiroki Sato 		bufrem = 0;
52899c841b1SHiroki Sato 		buf = NULL;
52999c841b1SHiroki Sato 	} else {
530d347a0daSSam Leffler 		if (debug & DEBUG_FS_CREATE_IMAGE)
53199c841b1SHiroki Sato 			printf("zero-ing image `%s', %lld sectors, "
53299c841b1SHiroki Sato 			    "using %d byte chunks\n", image, (long long)bufrem,
53399c841b1SHiroki Sato 			    bufsize);
5345f5598b1SEd Maste 		buf = ecalloc(1, bufsize);
53599c841b1SHiroki Sato 	}
536b79f050aSEd Maste 
537b79f050aSEd Maste 	if (fsopts->offset != 0)
538b79f050aSEd Maste 		if (lseek(fsopts->fd, fsopts->offset, SEEK_SET) == -1) {
539b79f050aSEd Maste 			warn("can't seek");
5405d7af8bbSEd Maste 			free(buf);
541b79f050aSEd Maste 			return -1;
542b79f050aSEd Maste 		}
543b79f050aSEd Maste 
544d347a0daSSam Leffler 	while (bufrem > 0) {
545d347a0daSSam Leffler 		i = write(fsopts->fd, buf, MIN(bufsize, bufrem));
546d347a0daSSam Leffler 		if (i == -1) {
547d347a0daSSam Leffler 			warn("zeroing image, %lld bytes to go",
548d347a0daSSam Leffler 			    (long long)bufrem);
54901a0f853SOlivier Houchard 			free(buf);
550d347a0daSSam Leffler 			return (-1);
551d347a0daSSam Leffler 		}
552d347a0daSSam Leffler 		bufrem -= i;
553d347a0daSSam Leffler 	}
55499c841b1SHiroki Sato 	if (buf)
55501a0f853SOlivier Houchard 		free(buf);
556d347a0daSSam Leffler 
557d347a0daSSam Leffler 		/* make the file system */
558d347a0daSSam Leffler 	if (debug & DEBUG_FS_CREATE_IMAGE)
559d347a0daSSam Leffler 		printf("calling mkfs(\"%s\", ...)\n", image);
5607b03d164SEd Maste 
5617b03d164SEd Maste 	if (stampst.st_ino != 0)
5627b03d164SEd Maste 		tstamp = stampst.st_ctime;
5637b03d164SEd Maste 	else
5647b03d164SEd Maste 		tstamp = start_time.tv_sec;
5657b03d164SEd Maste 
5667b03d164SEd Maste 	srandom(tstamp);
5677b03d164SEd Maste 
5687b03d164SEd Maste 	fs = ffs_mkfs(image, fsopts, tstamp);
569d347a0daSSam Leffler 	fsopts->superblock = (void *)fs;
570d347a0daSSam Leffler 	if (debug & DEBUG_FS_CREATE_IMAGE) {
571d347a0daSSam Leffler 		time_t t;
572d347a0daSSam Leffler 
573d347a0daSSam Leffler 		t = (time_t)((struct fs *)fsopts->superblock)->fs_time;
574d347a0daSSam Leffler 		printf("mkfs returned %p; fs_time %s",
575d347a0daSSam Leffler 		    fsopts->superblock, ctime(&t));
576d347a0daSSam Leffler 		printf("fs totals: nbfree %lld, nffree %lld, nifree %lld, ndir %lld\n",
577d347a0daSSam Leffler 		    (long long)fs->fs_cstotal.cs_nbfree,
578d347a0daSSam Leffler 		    (long long)fs->fs_cstotal.cs_nffree,
579d347a0daSSam Leffler 		    (long long)fs->fs_cstotal.cs_nifree,
580d347a0daSSam Leffler 		    (long long)fs->fs_cstotal.cs_ndir);
581d347a0daSSam Leffler 	}
582d347a0daSSam Leffler 
583cc1a53bcSMark Johnston 	if (fs->fs_cstotal.cs_nifree + (off_t)UFS_ROOTINO < fsopts->inodes) {
584d347a0daSSam Leffler 		warnx(
585d347a0daSSam Leffler 		"Image file `%s' has %lld free inodes; %lld are required.",
586d347a0daSSam Leffler 		    image,
5871dc349abSEd Maste 		    (long long)(fs->fs_cstotal.cs_nifree + UFS_ROOTINO),
588d347a0daSSam Leffler 		    (long long)fsopts->inodes);
589d347a0daSSam Leffler 		return (-1);
590d347a0daSSam Leffler 	}
591d347a0daSSam Leffler 	return (fsopts->fd);
592d347a0daSSam Leffler }
593d347a0daSSam Leffler 
594d347a0daSSam Leffler 
595d347a0daSSam Leffler static void
596d347a0daSSam Leffler ffs_size_dir(fsnode *root, fsinfo_t *fsopts)
597d347a0daSSam Leffler {
598d347a0daSSam Leffler 	struct direct	tmpdir;
599d347a0daSSam Leffler 	fsnode *	node;
600d347a0daSSam Leffler 	int		curdirsize, this;
60101a0f853SOlivier Houchard 	ffs_opt_t	*ffs_opts = fsopts->fs_specific;
602d347a0daSSam Leffler 
603d347a0daSSam Leffler 	/* node may be NULL (empty directory) */
604d347a0daSSam Leffler 	assert(fsopts != NULL);
60501a0f853SOlivier Houchard 	assert(ffs_opts != NULL);
606d347a0daSSam Leffler 
607d347a0daSSam Leffler 	if (debug & DEBUG_FS_SIZE_DIR)
608d347a0daSSam Leffler 		printf("ffs_size_dir: entry: bytes %lld inodes %lld\n",
609d347a0daSSam Leffler 		    (long long)fsopts->size, (long long)fsopts->inodes);
610d347a0daSSam Leffler 
611d347a0daSSam Leffler #define	ADDDIRENT(e) do {						\
612d347a0daSSam Leffler 	tmpdir.d_namlen = strlen((e));					\
613d347a0daSSam Leffler 	this = DIRSIZ_SWAP(0, &tmpdir, 0);					\
614d347a0daSSam Leffler 	if (debug & DEBUG_FS_SIZE_DIR_ADD_DIRENT)			\
615d347a0daSSam Leffler 		printf("ADDDIRENT: was: %s (%d) this %d cur %d\n",	\
616d347a0daSSam Leffler 		    e, tmpdir.d_namlen, this, curdirsize);		\
617d347a0daSSam Leffler 	if (this + curdirsize > roundup(curdirsize, DIRBLKSIZ))		\
618d347a0daSSam Leffler 		curdirsize = roundup(curdirsize, DIRBLKSIZ);		\
619d347a0daSSam Leffler 	curdirsize += this;						\
620d347a0daSSam Leffler 	if (debug & DEBUG_FS_SIZE_DIR_ADD_DIRENT)			\
621d347a0daSSam Leffler 		printf("ADDDIRENT: now: %s (%d) this %d cur %d\n",	\
622d347a0daSSam Leffler 		    e, tmpdir.d_namlen, this, curdirsize);		\
623d347a0daSSam Leffler } while (0);
624d347a0daSSam Leffler 
625d347a0daSSam Leffler #define	ADDSIZE(x) do {							\
626ecdc04d0SAlan Somers 	if ((size_t)(x) < UFS_NDADDR * (size_t)ffs_opts->bsize) {	\
62701a0f853SOlivier Houchard 		fsopts->size += roundup((x), ffs_opts->fsize);		\
628ecdc04d0SAlan Somers 	} else {							\
629ecdc04d0SAlan Somers 		/* Count space consumed by indirecttion blocks. */	\
630ecdc04d0SAlan Somers 		fsopts->size +=	ffs_opts->bsize *			\
631ecdc04d0SAlan Somers 		    (howmany((x), UFS_NDADDR * ffs_opts->bsize) - 1);	\
632ecdc04d0SAlan Somers 		/*							\
633ecdc04d0SAlan Somers 		 * If the file is big enough to use indirect blocks,	\
634ecdc04d0SAlan Somers 		 * we allocate bsize block for trailing data.		\
635ecdc04d0SAlan Somers 		 */							\
636ecdc04d0SAlan Somers 		fsopts->size += roundup((x), ffs_opts->bsize);		\
637ecdc04d0SAlan Somers 	}								\
638d347a0daSSam Leffler } while (0);
639d347a0daSSam Leffler 
640d347a0daSSam Leffler 	curdirsize = 0;
641d347a0daSSam Leffler 	for (node = root; node != NULL; node = node->next) {
642d347a0daSSam Leffler 		ADDDIRENT(node->name);
643d347a0daSSam Leffler 		if (node == root) {			/* we're at "." */
644d347a0daSSam Leffler 			assert(strcmp(node->name, ".") == 0);
645d347a0daSSam Leffler 			ADDDIRENT("..");
646d347a0daSSam Leffler 		} else if ((node->inode->flags & FI_SIZED) == 0) {
647d347a0daSSam Leffler 				/* don't count duplicate names */
648d347a0daSSam Leffler 			node->inode->flags |= FI_SIZED;
649d347a0daSSam Leffler 			if (debug & DEBUG_FS_SIZE_DIR_NODE)
650d347a0daSSam Leffler 				printf("ffs_size_dir: `%s' size %lld\n",
651d347a0daSSam Leffler 				    node->name,
652d347a0daSSam Leffler 				    (long long)node->inode->st.st_size);
653d347a0daSSam Leffler 			fsopts->inodes++;
654d347a0daSSam Leffler 			if (node->type == S_IFREG)
655d347a0daSSam Leffler 				ADDSIZE(node->inode->st.st_size);
656d347a0daSSam Leffler 			if (node->type == S_IFLNK) {
657856d87c5SEd Maste 				size_t slen;
658d347a0daSSam Leffler 
659d347a0daSSam Leffler 				slen = strlen(node->symlink) + 1;
66001a0f853SOlivier Houchard 				if (slen >= (ffs_opts->version == 1 ?
6611dc349abSEd Maste 						UFS1_MAXSYMLINKLEN :
6621dc349abSEd Maste 						UFS2_MAXSYMLINKLEN))
663d347a0daSSam Leffler 					ADDSIZE(slen);
664d347a0daSSam Leffler 			}
665d347a0daSSam Leffler 		}
666d347a0daSSam Leffler 		if (node->type == S_IFDIR)
667d347a0daSSam Leffler 			ffs_size_dir(node->child, fsopts);
668d347a0daSSam Leffler 	}
669d347a0daSSam Leffler 	ADDSIZE(curdirsize);
670d347a0daSSam Leffler 
671d347a0daSSam Leffler 	if (debug & DEBUG_FS_SIZE_DIR)
672d347a0daSSam Leffler 		printf("ffs_size_dir: exit: size %lld inodes %lld\n",
673d347a0daSSam Leffler 		    (long long)fsopts->size, (long long)fsopts->inodes);
674d347a0daSSam Leffler }
675d347a0daSSam Leffler 
676d347a0daSSam Leffler static void *
677d347a0daSSam Leffler ffs_build_dinode1(struct ufs1_dinode *dinp, dirbuf_t *dbufp, fsnode *cur,
678d347a0daSSam Leffler 		 fsnode *root, fsinfo_t *fsopts)
679d347a0daSSam Leffler {
680856d87c5SEd Maste 	size_t slen;
681d347a0daSSam Leffler 	void *membuf;
6827b03d164SEd Maste 	struct stat *st = stampst.st_ino != 0 ? &stampst : &cur->inode->st;
683d347a0daSSam Leffler 
684d347a0daSSam Leffler 	memset(dinp, 0, sizeof(*dinp));
685d347a0daSSam Leffler 	dinp->di_mode = cur->inode->st.st_mode;
686d347a0daSSam Leffler 	dinp->di_nlink = cur->inode->nlink;
687d347a0daSSam Leffler 	dinp->di_size = cur->inode->st.st_size;
688d347a0daSSam Leffler #if HAVE_STRUCT_STAT_ST_FLAGS
689d347a0daSSam Leffler 	dinp->di_flags = cur->inode->st.st_flags;
690d347a0daSSam Leffler #endif
6914ad8d733SMarcel Moolenaar 	dinp->di_gen = random();
692d347a0daSSam Leffler 	dinp->di_uid = cur->inode->st.st_uid;
693d347a0daSSam Leffler 	dinp->di_gid = cur->inode->st.st_gid;
6947b03d164SEd Maste 
6957b03d164SEd Maste 	dinp->di_atime = st->st_atime;
6967b03d164SEd Maste 	dinp->di_mtime = st->st_mtime;
6977b03d164SEd Maste 	dinp->di_ctime = st->st_ctime;
6987b03d164SEd Maste #if HAVE_STRUCT_STAT_ST_MTIMENSEC
6997b03d164SEd Maste 	dinp->di_atimensec = st->st_atimensec;
7007b03d164SEd Maste 	dinp->di_mtimensec = st->st_mtimensec;
7017b03d164SEd Maste 	dinp->di_ctimensec = st->st_ctimensec;
7027b03d164SEd Maste #endif
703d347a0daSSam Leffler 		/* not set: di_db, di_ib, di_blocks, di_spare */
704d347a0daSSam Leffler 
705d347a0daSSam Leffler 	membuf = NULL;
706d347a0daSSam Leffler 	if (cur == root) {			/* "."; write dirbuf */
707d347a0daSSam Leffler 		membuf = dbufp->buf;
708d347a0daSSam Leffler 		dinp->di_size = dbufp->size;
709d347a0daSSam Leffler 	} else if (S_ISBLK(cur->type) || S_ISCHR(cur->type)) {
710d347a0daSSam Leffler 		dinp->di_size = 0;	/* a device */
711d347a0daSSam Leffler 		dinp->di_rdev =
712d347a0daSSam Leffler 		    ufs_rw32(cur->inode->st.st_rdev, fsopts->needswap);
713d347a0daSSam Leffler 	} else if (S_ISLNK(cur->type)) {	/* symlink */
714d347a0daSSam Leffler 		slen = strlen(cur->symlink);
7151dc349abSEd Maste 		if (slen < UFS1_MAXSYMLINKLEN) {	/* short link */
7165b13fa79SJessica Clarke 			memcpy(dinp->di_shortlink, cur->symlink, slen);
717d347a0daSSam Leffler 		} else
718d347a0daSSam Leffler 			membuf = cur->symlink;
719d347a0daSSam Leffler 		dinp->di_size = slen;
720d347a0daSSam Leffler 	}
721d347a0daSSam Leffler 	return membuf;
722d347a0daSSam Leffler }
723d347a0daSSam Leffler 
724d347a0daSSam Leffler static void *
725d347a0daSSam Leffler ffs_build_dinode2(struct ufs2_dinode *dinp, dirbuf_t *dbufp, fsnode *cur,
726d347a0daSSam Leffler 		 fsnode *root, fsinfo_t *fsopts)
727d347a0daSSam Leffler {
728856d87c5SEd Maste 	size_t slen;
729d347a0daSSam Leffler 	void *membuf;
7307b03d164SEd Maste 	struct stat *st = stampst.st_ino != 0 ? &stampst : &cur->inode->st;
731d347a0daSSam Leffler 
732d347a0daSSam Leffler 	memset(dinp, 0, sizeof(*dinp));
733d347a0daSSam Leffler 	dinp->di_mode = cur->inode->st.st_mode;
734d347a0daSSam Leffler 	dinp->di_nlink = cur->inode->nlink;
735d347a0daSSam Leffler 	dinp->di_size = cur->inode->st.st_size;
736d347a0daSSam Leffler #if HAVE_STRUCT_STAT_ST_FLAGS
737d347a0daSSam Leffler 	dinp->di_flags = cur->inode->st.st_flags;
738d347a0daSSam Leffler #endif
7394ad8d733SMarcel Moolenaar 	dinp->di_gen = random();
740d347a0daSSam Leffler 	dinp->di_uid = cur->inode->st.st_uid;
741d347a0daSSam Leffler 	dinp->di_gid = cur->inode->st.st_gid;
7427b03d164SEd Maste 
7437b03d164SEd Maste 	dinp->di_atime = st->st_atime;
7447b03d164SEd Maste 	dinp->di_mtime = st->st_mtime;
7457b03d164SEd Maste 	dinp->di_ctime = st->st_ctime;
7461e9f67e2SAlex Richardson #if HAVE_STRUCT_STAT_BIRTHTIME
7471e9f67e2SAlex Richardson 	dinp->di_birthtime = st->st_birthtime;
7481e9f67e2SAlex Richardson #else
7491e9f67e2SAlex Richardson 	dinp->di_birthtime = st->st_ctime;
7501e9f67e2SAlex Richardson #endif
7517b03d164SEd Maste #if HAVE_STRUCT_STAT_ST_MTIMENSEC
7527b03d164SEd Maste 	dinp->di_atimensec = st->st_atimensec;
7537b03d164SEd Maste 	dinp->di_mtimensec = st->st_mtimensec;
7547b03d164SEd Maste 	dinp->di_ctimensec = st->st_ctimensec;
7557b03d164SEd Maste #if HAVE_STRUCT_STAT_BIRTHTIME
7567b03d164SEd Maste 	dinp->di_birthnsec = st->st_birthtimensec;
7571e9f67e2SAlex Richardson #else
7581e9f67e2SAlex Richardson 	dinp->di_birthnsec = st->st_ctimensec;
7597b03d164SEd Maste #endif
7601e9f67e2SAlex Richardson #endif
7611e9f67e2SAlex Richardson 
762d347a0daSSam Leffler 		/* not set: di_db, di_ib, di_blocks, di_spare */
763d347a0daSSam Leffler 
764d347a0daSSam Leffler 	membuf = NULL;
765d347a0daSSam Leffler 	if (cur == root) {			/* "."; write dirbuf */
766d347a0daSSam Leffler 		membuf = dbufp->buf;
767d347a0daSSam Leffler 		dinp->di_size = dbufp->size;
768d347a0daSSam Leffler 	} else if (S_ISBLK(cur->type) || S_ISCHR(cur->type)) {
769d347a0daSSam Leffler 		dinp->di_size = 0;	/* a device */
770d347a0daSSam Leffler 		dinp->di_rdev =
771d347a0daSSam Leffler 		    ufs_rw64(cur->inode->st.st_rdev, fsopts->needswap);
772d347a0daSSam Leffler 	} else if (S_ISLNK(cur->type)) {	/* symlink */
773d347a0daSSam Leffler 		slen = strlen(cur->symlink);
7741dc349abSEd Maste 		if (slen < UFS2_MAXSYMLINKLEN) {	/* short link */
7755b13fa79SJessica Clarke 			memcpy(dinp->di_shortlink, cur->symlink, slen);
776d347a0daSSam Leffler 		} else
777d347a0daSSam Leffler 			membuf = cur->symlink;
778d347a0daSSam Leffler 		dinp->di_size = slen;
779d347a0daSSam Leffler 	}
780d347a0daSSam Leffler 	return membuf;
781d347a0daSSam Leffler }
782d347a0daSSam Leffler 
783d347a0daSSam Leffler static int
784d347a0daSSam Leffler ffs_populate_dir(const char *dir, fsnode *root, fsinfo_t *fsopts)
785d347a0daSSam Leffler {
786d347a0daSSam Leffler 	fsnode		*cur;
787d347a0daSSam Leffler 	dirbuf_t	dirbuf;
788d347a0daSSam Leffler 	union dinode	din;
789d347a0daSSam Leffler 	void		*membuf;
790d347a0daSSam Leffler 	char		path[MAXPATHLEN + 1];
79101a0f853SOlivier Houchard 	ffs_opt_t	*ffs_opts = fsopts->fs_specific;
792d347a0daSSam Leffler 
793d347a0daSSam Leffler 	assert(dir != NULL);
794d347a0daSSam Leffler 	assert(root != NULL);
795d347a0daSSam Leffler 	assert(fsopts != NULL);
79601a0f853SOlivier Houchard 	assert(ffs_opts != NULL);
797d347a0daSSam Leffler 
798d347a0daSSam Leffler 	(void)memset(&dirbuf, 0, sizeof(dirbuf));
799d347a0daSSam Leffler 
800d347a0daSSam Leffler 	if (debug & DEBUG_FS_POPULATE)
801d347a0daSSam Leffler 		printf("ffs_populate_dir: PASS 1  dir %s node %p\n", dir, root);
802d347a0daSSam Leffler 
803d347a0daSSam Leffler 		/*
804d347a0daSSam Leffler 		 * pass 1: allocate inode numbers, build directory `file'
805d347a0daSSam Leffler 		 */
806d347a0daSSam Leffler 	for (cur = root; cur != NULL; cur = cur->next) {
807d347a0daSSam Leffler 		if ((cur->inode->flags & FI_ALLOCATED) == 0) {
808d347a0daSSam Leffler 			cur->inode->flags |= FI_ALLOCATED;
809d347a0daSSam Leffler 			if (cur == root && cur->parent != NULL)
810d347a0daSSam Leffler 				cur->inode->ino = cur->parent->inode->ino;
811d347a0daSSam Leffler 			else {
812d347a0daSSam Leffler 				cur->inode->ino = fsopts->curinode;
813d347a0daSSam Leffler 				fsopts->curinode++;
814d347a0daSSam Leffler 			}
815d347a0daSSam Leffler 		}
816d347a0daSSam Leffler 		ffs_make_dirbuf(&dirbuf, cur->name, cur, fsopts->needswap);
817d347a0daSSam Leffler 		if (cur == root) {		/* we're at "."; add ".." */
818d347a0daSSam Leffler 			ffs_make_dirbuf(&dirbuf, "..",
819d347a0daSSam Leffler 			    cur->parent == NULL ? cur : cur->parent->first,
820d347a0daSSam Leffler 			    fsopts->needswap);
821d347a0daSSam Leffler 			root->inode->nlink++;	/* count my parent's link */
822d347a0daSSam Leffler 		} else if (cur->child != NULL)
823d347a0daSSam Leffler 			root->inode->nlink++;	/* count my child's link */
824d347a0daSSam Leffler 
825d347a0daSSam Leffler 		/*
826d347a0daSSam Leffler 		 * XXX	possibly write file and long symlinks here,
827d347a0daSSam Leffler 		 *	ensuring that blocks get written before inodes?
828d347a0daSSam Leffler 		 *	otoh, this isn't a real filesystem, so who
829d347a0daSSam Leffler 		 *	cares about ordering? :-)
830d347a0daSSam Leffler 		 */
831d347a0daSSam Leffler 	}
832d347a0daSSam Leffler 	if (debug & DEBUG_FS_POPULATE_DIRBUF)
833d347a0daSSam Leffler 		ffs_dump_dirbuf(&dirbuf, dir, fsopts->needswap);
834d347a0daSSam Leffler 
835d347a0daSSam Leffler 		/*
836d347a0daSSam Leffler 		 * pass 2: write out dirbuf, then non-directories at this level
837d347a0daSSam Leffler 		 */
838d347a0daSSam Leffler 	if (debug & DEBUG_FS_POPULATE)
839d347a0daSSam Leffler 		printf("ffs_populate_dir: PASS 2  dir %s\n", dir);
840d347a0daSSam Leffler 	for (cur = root; cur != NULL; cur = cur->next) {
841d347a0daSSam Leffler 		if (cur->inode->flags & FI_WRITTEN)
842d347a0daSSam Leffler 			continue;		/* skip hard-linked entries */
843d347a0daSSam Leffler 		cur->inode->flags |= FI_WRITTEN;
844d347a0daSSam Leffler 
845484b5c25SMarcel Moolenaar 		if (cur->contents == NULL) {
846688aaa09SJung-uk Kim 			if (snprintf(path, sizeof(path), "%s/%s/%s", cur->root,
847688aaa09SJung-uk Kim 			    cur->path, cur->name) >= (int)sizeof(path))
848d347a0daSSam Leffler 				errx(1, "Pathname too long.");
849484b5c25SMarcel Moolenaar 		}
850d347a0daSSam Leffler 
851d347a0daSSam Leffler 		if (cur->child != NULL)
852d347a0daSSam Leffler 			continue;		/* child creates own inode */
853d347a0daSSam Leffler 
854d347a0daSSam Leffler 				/* build on-disk inode */
85501a0f853SOlivier Houchard 		if (ffs_opts->version == 1)
856*aa90fbedSKirk McKusick 			membuf = ffs_build_dinode1(&din.dp1, &dirbuf, cur,
857d347a0daSSam Leffler 			    root, fsopts);
858d347a0daSSam Leffler 		else
859*aa90fbedSKirk McKusick 			membuf = ffs_build_dinode2(&din.dp2, &dirbuf, cur,
860d347a0daSSam Leffler 			    root, fsopts);
861d347a0daSSam Leffler 
862d347a0daSSam Leffler 		if (debug & DEBUG_FS_POPULATE_NODE) {
863d347a0daSSam Leffler 			printf("ffs_populate_dir: writing ino %d, %s",
864d347a0daSSam Leffler 			    cur->inode->ino, inode_type(cur->type));
865d347a0daSSam Leffler 			if (cur->inode->nlink > 1)
866d347a0daSSam Leffler 				printf(", nlink %d", cur->inode->nlink);
867d347a0daSSam Leffler 			putchar('\n');
868d347a0daSSam Leffler 		}
869d347a0daSSam Leffler 
870d347a0daSSam Leffler 		if (membuf != NULL) {
871d347a0daSSam Leffler 			ffs_write_file(&din, cur->inode->ino, membuf, fsopts);
872d347a0daSSam Leffler 		} else if (S_ISREG(cur->type)) {
873484b5c25SMarcel Moolenaar 			ffs_write_file(&din, cur->inode->ino,
874484b5c25SMarcel Moolenaar 			    (cur->contents) ?  cur->contents : path, fsopts);
875d347a0daSSam Leffler 		} else {
876d347a0daSSam Leffler 			assert (! S_ISDIR(cur->type));
877d347a0daSSam Leffler 			ffs_write_inode(&din, cur->inode->ino, fsopts);
878d347a0daSSam Leffler 		}
879d347a0daSSam Leffler 	}
880d347a0daSSam Leffler 
881d347a0daSSam Leffler 		/*
882d347a0daSSam Leffler 		 * pass 3: write out sub-directories
883d347a0daSSam Leffler 		 */
884d347a0daSSam Leffler 	if (debug & DEBUG_FS_POPULATE)
885d347a0daSSam Leffler 		printf("ffs_populate_dir: PASS 3  dir %s\n", dir);
886d347a0daSSam Leffler 	for (cur = root; cur != NULL; cur = cur->next) {
887d347a0daSSam Leffler 		if (cur->child == NULL)
888d347a0daSSam Leffler 			continue;
889243a297aSEd Maste 		if ((size_t)snprintf(path, sizeof(path), "%s/%s", dir,
890243a297aSEd Maste 		    cur->name) >= sizeof(path))
891d347a0daSSam Leffler 			errx(1, "Pathname too long.");
892d347a0daSSam Leffler 		if (! ffs_populate_dir(path, cur->child, fsopts))
893d347a0daSSam Leffler 			return (0);
894d347a0daSSam Leffler 	}
895d347a0daSSam Leffler 
896d347a0daSSam Leffler 	if (debug & DEBUG_FS_POPULATE)
897d347a0daSSam Leffler 		printf("ffs_populate_dir: DONE dir %s\n", dir);
898d347a0daSSam Leffler 
899d347a0daSSam Leffler 		/* cleanup */
900d347a0daSSam Leffler 	if (dirbuf.buf != NULL)
901d347a0daSSam Leffler 		free(dirbuf.buf);
902d347a0daSSam Leffler 	return (1);
903d347a0daSSam Leffler }
904d347a0daSSam Leffler 
905d347a0daSSam Leffler 
906d347a0daSSam Leffler static void
907d347a0daSSam Leffler ffs_write_file(union dinode *din, uint32_t ino, void *buf, fsinfo_t *fsopts)
908d347a0daSSam Leffler {
909d347a0daSSam Leffler 	int	isfile, ffd;
910d347a0daSSam Leffler 	char	*fbuf, *p;
911d347a0daSSam Leffler 	off_t	bufleft, chunk, offset;
91201a0f853SOlivier Houchard 	ssize_t nread;
913d347a0daSSam Leffler 	struct inode	in;
914d485c77fSKonstantin Belousov 	struct m_buf *	bp;
91501a0f853SOlivier Houchard 	ffs_opt_t	*ffs_opts = fsopts->fs_specific;
916d485c77fSKonstantin Belousov 	struct m_vnode vp = { fsopts, NULL };
917d347a0daSSam Leffler 
918d347a0daSSam Leffler 	assert (din != NULL);
919d347a0daSSam Leffler 	assert (buf != NULL);
920d347a0daSSam Leffler 	assert (fsopts != NULL);
92101a0f853SOlivier Houchard 	assert (ffs_opts != NULL);
922d347a0daSSam Leffler 
923d347a0daSSam Leffler 	isfile = S_ISREG(DIP(din, mode));
924d347a0daSSam Leffler 	fbuf = NULL;
925d347a0daSSam Leffler 	ffd = -1;
92601a0f853SOlivier Houchard 	p = NULL;
927d347a0daSSam Leffler 
928d347a0daSSam Leffler 	in.i_fs = (struct fs *)fsopts->superblock;
929d485c77fSKonstantin Belousov 	in.i_devvp = (void *)&vp;
930d347a0daSSam Leffler 
931d347a0daSSam Leffler 	if (debug & DEBUG_FS_WRITE_FILE) {
932d347a0daSSam Leffler 		printf(
933d347a0daSSam Leffler 		    "ffs_write_file: ino %u, din %p, isfile %d, %s, size %lld",
934d347a0daSSam Leffler 		    ino, din, isfile, inode_type(DIP(din, mode) & S_IFMT),
935d347a0daSSam Leffler 		    (long long)DIP(din, size));
936d347a0daSSam Leffler 		if (isfile)
937d347a0daSSam Leffler 			printf(", file '%s'\n", (char *)buf);
938d347a0daSSam Leffler 		else
939d347a0daSSam Leffler 			printf(", buffer %p\n", buf);
940d347a0daSSam Leffler 	}
941d347a0daSSam Leffler 
942d347a0daSSam Leffler 	in.i_number = ino;
943d347a0daSSam Leffler 	in.i_size = DIP(din, size);
94401a0f853SOlivier Houchard 	if (ffs_opts->version == 1)
945*aa90fbedSKirk McKusick 		memcpy(&in.i_din.dp1, &din->dp1,
946*aa90fbedSKirk McKusick 		    sizeof(in.i_din.dp1));
947d347a0daSSam Leffler 	else
948*aa90fbedSKirk McKusick 		memcpy(&in.i_din.dp2, &din->dp2,
949*aa90fbedSKirk McKusick 		    sizeof(in.i_din.dp2));
950d347a0daSSam Leffler 
951d347a0daSSam Leffler 	if (DIP(din, size) == 0)
952d347a0daSSam Leffler 		goto write_inode_and_leave;		/* mmm, cheating */
953d347a0daSSam Leffler 
954d347a0daSSam Leffler 	if (isfile) {
9555f5598b1SEd Maste 		fbuf = emalloc(ffs_opts->bsize);
95645e40560SEd Maste 		if ((ffd = open((char *)buf, O_RDONLY)) == -1) {
957781e7818SMaxim Sobolev 			err(EXIT_FAILURE, "Can't open `%s' for reading", (char *)buf);
958d347a0daSSam Leffler 		}
959d347a0daSSam Leffler 	} else {
960d347a0daSSam Leffler 		p = buf;
961d347a0daSSam Leffler 	}
962d347a0daSSam Leffler 
963d347a0daSSam Leffler 	chunk = 0;
964d347a0daSSam Leffler 	for (bufleft = DIP(din, size); bufleft > 0; bufleft -= chunk) {
96501a0f853SOlivier Houchard 		chunk = MIN(bufleft, ffs_opts->bsize);
96601a0f853SOlivier Houchard 		if (!isfile)
96701a0f853SOlivier Houchard 			;
96801a0f853SOlivier Houchard 		else if ((nread = read(ffd, fbuf, chunk)) == -1)
96901a0f853SOlivier Houchard 			err(EXIT_FAILURE, "Reading `%s', %lld bytes to go",
970d347a0daSSam Leffler 			    (char *)buf, (long long)bufleft);
97101a0f853SOlivier Houchard 		else if (nread != chunk)
97201a0f853SOlivier Houchard 			errx(EXIT_FAILURE, "Reading `%s', %lld bytes to go, "
97301a0f853SOlivier Houchard 			    "read %zd bytes, expected %ju bytes, does "
97401a0f853SOlivier Houchard 			    "metalog size= attribute mismatch source size?",
97501a0f853SOlivier Houchard 			    (char *)buf, (long long)bufleft, nread,
97601a0f853SOlivier Houchard 			    (uintmax_t)chunk);
97701a0f853SOlivier Houchard 		else
978d347a0daSSam Leffler 			p = fbuf;
979d347a0daSSam Leffler 		offset = DIP(din, size) - bufleft;
980d347a0daSSam Leffler 		if (debug & DEBUG_FS_WRITE_FILE_BLOCK)
981d347a0daSSam Leffler 			printf(
982d347a0daSSam Leffler 		"ffs_write_file: write %p offset %lld size %lld left %lld\n",
983d347a0daSSam Leffler 			    p, (long long)offset,
984d347a0daSSam Leffler 			    (long long)chunk, (long long)bufleft);
985d347a0daSSam Leffler 	/*
986d347a0daSSam Leffler 	 * XXX	if holey support is desired, do the check here
987d347a0daSSam Leffler 	 *
988d347a0daSSam Leffler 	 * XXX	might need to write out last bit in fragroundup
989d347a0daSSam Leffler 	 *	sized chunk. however, ffs_balloc() handles this for us
990d347a0daSSam Leffler 	 */
991d347a0daSSam Leffler 		errno = ffs_balloc(&in, offset, chunk, &bp);
992d347a0daSSam Leffler  bad_ffs_write_file:
993d347a0daSSam Leffler 		if (errno != 0)
994d347a0daSSam Leffler 			err(1,
995d347a0daSSam Leffler 			    "Writing inode %d (%s), bytes %lld + %lld",
996d347a0daSSam Leffler 			    ino,
997d347a0daSSam Leffler 			    isfile ? (char *)buf :
998d347a0daSSam Leffler 			      inode_type(DIP(din, mode) & S_IFMT),
999d347a0daSSam Leffler 			    (long long)offset, (long long)chunk);
1000d347a0daSSam Leffler 		memcpy(bp->b_data, p, chunk);
1001d347a0daSSam Leffler 		errno = bwrite(bp);
1002d347a0daSSam Leffler 		if (errno != 0)
1003d347a0daSSam Leffler 			goto bad_ffs_write_file;
1004d347a0daSSam Leffler 		if (!isfile)
1005d347a0daSSam Leffler 			p += chunk;
1006d347a0daSSam Leffler 	}
1007d347a0daSSam Leffler 
1008d347a0daSSam Leffler  write_inode_and_leave:
1009d347a0daSSam Leffler 	ffs_write_inode(&in.i_din, in.i_number, fsopts);
1010d347a0daSSam Leffler 	if (fbuf)
1011d347a0daSSam Leffler 		free(fbuf);
1012d347a0daSSam Leffler 	if (ffd != -1)
1013d347a0daSSam Leffler 		close(ffd);
1014d347a0daSSam Leffler }
1015d347a0daSSam Leffler 
1016d347a0daSSam Leffler 
1017d347a0daSSam Leffler static void
1018d347a0daSSam Leffler ffs_dump_dirbuf(dirbuf_t *dbuf, const char *dir, int needswap)
1019d347a0daSSam Leffler {
1020d347a0daSSam Leffler 	doff_t		i;
1021d347a0daSSam Leffler 	struct direct	*de;
1022d347a0daSSam Leffler 	uint16_t	reclen;
1023d347a0daSSam Leffler 
1024d347a0daSSam Leffler 	assert (dbuf != NULL);
1025d347a0daSSam Leffler 	assert (dir != NULL);
1026d347a0daSSam Leffler 	printf("ffs_dump_dirbuf: dir %s size %d cur %d\n",
1027d347a0daSSam Leffler 	    dir, dbuf->size, dbuf->cur);
1028d347a0daSSam Leffler 
1029d347a0daSSam Leffler 	for (i = 0; i < dbuf->size; ) {
1030d347a0daSSam Leffler 		de = (struct direct *)(dbuf->buf + i);
1031d347a0daSSam Leffler 		reclen = ufs_rw16(de->d_reclen, needswap);
1032d347a0daSSam Leffler 		printf(
1033d347a0daSSam Leffler 	    " inode %4d %7s offset %4d reclen %3d namlen %3d name %s\n",
1034d347a0daSSam Leffler 		    ufs_rw32(de->d_ino, needswap),
1035d347a0daSSam Leffler 		    inode_type(DTTOIF(de->d_type)), i, reclen,
1036d347a0daSSam Leffler 		    de->d_namlen, de->d_name);
1037d347a0daSSam Leffler 		i += reclen;
1038d347a0daSSam Leffler 		assert(reclen > 0);
1039d347a0daSSam Leffler 	}
1040d347a0daSSam Leffler }
1041d347a0daSSam Leffler 
1042d347a0daSSam Leffler static void
1043d347a0daSSam Leffler ffs_make_dirbuf(dirbuf_t *dbuf, const char *name, fsnode *node, int needswap)
1044d347a0daSSam Leffler {
1045d347a0daSSam Leffler 	struct direct	de, *dp;
1046d347a0daSSam Leffler 	uint16_t	llen, reclen;
104701a0f853SOlivier Houchard 	u_char		*newbuf;
1048d347a0daSSam Leffler 
1049d347a0daSSam Leffler 	assert (dbuf != NULL);
1050d347a0daSSam Leffler 	assert (name != NULL);
1051d347a0daSSam Leffler 	assert (node != NULL);
1052d347a0daSSam Leffler 					/* create direct entry */
1053d347a0daSSam Leffler 	(void)memset(&de, 0, sizeof(de));
1054d347a0daSSam Leffler 	de.d_ino = ufs_rw32(node->inode->ino, needswap);
1055d347a0daSSam Leffler 	de.d_type = IFTODT(node->type);
1056d347a0daSSam Leffler 	de.d_namlen = (uint8_t)strlen(name);
1057d347a0daSSam Leffler 	strcpy(de.d_name, name);
1058d347a0daSSam Leffler 	reclen = DIRSIZ_SWAP(0, &de, needswap);
1059d347a0daSSam Leffler 	de.d_reclen = ufs_rw16(reclen, needswap);
1060d347a0daSSam Leffler 
1061d347a0daSSam Leffler 	dp = (struct direct *)(dbuf->buf + dbuf->cur);
1062d347a0daSSam Leffler 	llen = 0;
1063d347a0daSSam Leffler 	if (dp != NULL)
1064d347a0daSSam Leffler 		llen = DIRSIZ_SWAP(0, dp, needswap);
1065d347a0daSSam Leffler 
1066d347a0daSSam Leffler 	if (debug & DEBUG_FS_MAKE_DIRBUF)
1067d347a0daSSam Leffler 		printf(
1068d347a0daSSam Leffler 		    "ffs_make_dirbuf: dbuf siz %d cur %d lastlen %d\n"
1069d347a0daSSam Leffler 		    "  ino %d type %d reclen %d namlen %d name %.30s\n",
1070d347a0daSSam Leffler 		    dbuf->size, dbuf->cur, llen,
1071d347a0daSSam Leffler 		    ufs_rw32(de.d_ino, needswap), de.d_type, reclen,
1072d347a0daSSam Leffler 		    de.d_namlen, de.d_name);
1073d347a0daSSam Leffler 
1074d347a0daSSam Leffler 	if (reclen + dbuf->cur + llen > roundup(dbuf->size, DIRBLKSIZ)) {
1075d347a0daSSam Leffler 		if (debug & DEBUG_FS_MAKE_DIRBUF)
1076d347a0daSSam Leffler 			printf("ffs_make_dirbuf: growing buf to %d\n",
1077d347a0daSSam Leffler 			    dbuf->size + DIRBLKSIZ);
10785f5598b1SEd Maste 		newbuf = erealloc(dbuf->buf, dbuf->size + DIRBLKSIZ);
1079d347a0daSSam Leffler 		dbuf->buf = newbuf;
1080d347a0daSSam Leffler 		dbuf->size += DIRBLKSIZ;
1081d347a0daSSam Leffler 		memset(dbuf->buf + dbuf->size - DIRBLKSIZ, 0, DIRBLKSIZ);
1082d347a0daSSam Leffler 		dbuf->cur = dbuf->size - DIRBLKSIZ;
108301a0f853SOlivier Houchard 	} else if (dp) {			/* shrink end of previous */
1084d347a0daSSam Leffler 		dp->d_reclen = ufs_rw16(llen,needswap);
1085d347a0daSSam Leffler 		dbuf->cur += llen;
1086d347a0daSSam Leffler 	}
1087d347a0daSSam Leffler 	dp = (struct direct *)(dbuf->buf + dbuf->cur);
1088d347a0daSSam Leffler 	memcpy(dp, &de, reclen);
1089d347a0daSSam Leffler 	dp->d_reclen = ufs_rw16(dbuf->size - dbuf->cur, needswap);
1090d347a0daSSam Leffler }
1091d347a0daSSam Leffler 
1092d347a0daSSam Leffler /*
1093d347a0daSSam Leffler  * cribbed from sys/ufs/ffs/ffs_alloc.c
1094d347a0daSSam Leffler  */
1095d347a0daSSam Leffler static void
1096d347a0daSSam Leffler ffs_write_inode(union dinode *dp, uint32_t ino, const fsinfo_t *fsopts)
1097d347a0daSSam Leffler {
1098d347a0daSSam Leffler 	char		*buf;
1099d347a0daSSam Leffler 	struct ufs1_dinode *dp1;
1100d347a0daSSam Leffler 	struct ufs2_dinode *dp2, *dip;
1101d347a0daSSam Leffler 	struct cg	*cgp;
1102d347a0daSSam Leffler 	struct fs	*fs;
11033afe6a68SEd Maste 	int		cg, cgino;
11043afe6a68SEd Maste 	uint32_t	i;
1105d347a0daSSam Leffler 	daddr_t		d;
1106d347a0daSSam Leffler 	char		sbbuf[FFS_MAXBSIZE];
11073afe6a68SEd Maste 	uint32_t	initediblk;
110801a0f853SOlivier Houchard 	ffs_opt_t	*ffs_opts = fsopts->fs_specific;
1109d347a0daSSam Leffler 
1110d347a0daSSam Leffler 	assert (dp != NULL);
1111d347a0daSSam Leffler 	assert (ino > 0);
1112d347a0daSSam Leffler 	assert (fsopts != NULL);
111301a0f853SOlivier Houchard 	assert (ffs_opts != NULL);
1114d347a0daSSam Leffler 
1115d347a0daSSam Leffler 	fs = (struct fs *)fsopts->superblock;
1116d347a0daSSam Leffler 	cg = ino_to_cg(fs, ino);
1117d347a0daSSam Leffler 	cgino = ino % fs->fs_ipg;
1118d347a0daSSam Leffler 	if (debug & DEBUG_FS_WRITE_INODE)
1119d347a0daSSam Leffler 		printf("ffs_write_inode: din %p ino %u cg %d cgino %d\n",
1120d347a0daSSam Leffler 		    dp, ino, cg, cgino);
1121d347a0daSSam Leffler 
1122d347a0daSSam Leffler 	ffs_rdfs(fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize, &sbbuf,
1123d347a0daSSam Leffler 	    fsopts);
1124d347a0daSSam Leffler 	cgp = (struct cg *)sbbuf;
1125d347a0daSSam Leffler 	if (!cg_chkmagic_swap(cgp, fsopts->needswap))
1126d347a0daSSam Leffler 		errx(1, "ffs_write_inode: cg %d: bad magic number", cg);
1127d347a0daSSam Leffler 
1128d347a0daSSam Leffler 	assert (isclr(cg_inosused_swap(cgp, fsopts->needswap), cgino));
1129d347a0daSSam Leffler 
11305f5598b1SEd Maste 	buf = emalloc(fs->fs_bsize);
1131d347a0daSSam Leffler 	dp1 = (struct ufs1_dinode *)buf;
1132d347a0daSSam Leffler 	dp2 = (struct ufs2_dinode *)buf;
1133d347a0daSSam Leffler 
1134d347a0daSSam Leffler 	if (fs->fs_cstotal.cs_nifree == 0)
1135d347a0daSSam Leffler 		errx(1, "ffs_write_inode: fs out of inodes for ino %u",
1136d347a0daSSam Leffler 		    ino);
1137d347a0daSSam Leffler 	if (fs->fs_cs(fs, cg).cs_nifree == 0)
1138d347a0daSSam Leffler 		errx(1,
1139d347a0daSSam Leffler 		    "ffs_write_inode: cg %d out of inodes for ino %u",
1140d347a0daSSam Leffler 		    cg, ino);
1141d347a0daSSam Leffler 	setbit(cg_inosused_swap(cgp, fsopts->needswap), cgino);
1142d347a0daSSam Leffler 	ufs_add32(cgp->cg_cs.cs_nifree, -1, fsopts->needswap);
1143d347a0daSSam Leffler 	fs->fs_cstotal.cs_nifree--;
1144d347a0daSSam Leffler 	fs->fs_cs(fs, cg).cs_nifree--;
1145d347a0daSSam Leffler 	if (S_ISDIR(DIP(dp, mode))) {
1146d347a0daSSam Leffler 		ufs_add32(cgp->cg_cs.cs_ndir, 1, fsopts->needswap);
1147d347a0daSSam Leffler 		fs->fs_cstotal.cs_ndir++;
1148d347a0daSSam Leffler 		fs->fs_cs(fs, cg).cs_ndir++;
1149d347a0daSSam Leffler 	}
1150d347a0daSSam Leffler 
1151d347a0daSSam Leffler 	/*
1152d347a0daSSam Leffler 	 * Initialize inode blocks on the fly for UFS2.
1153d347a0daSSam Leffler 	 */
1154d347a0daSSam Leffler 	initediblk = ufs_rw32(cgp->cg_initediblk, fsopts->needswap);
11553836e359SMark Johnston 	while (ffs_opts->version == 2 && cgino + INOPB(fs) > initediblk &&
1156d347a0daSSam Leffler 	    initediblk < ufs_rw32(cgp->cg_niblk, fsopts->needswap)) {
1157d347a0daSSam Leffler 		memset(buf, 0, fs->fs_bsize);
1158d347a0daSSam Leffler 		dip = (struct ufs2_dinode *)buf;
1159d347a0daSSam Leffler 		for (i = 0; i < INOPB(fs); i++) {
116040d39b4bSPedro F. Giffuni 			dip->di_gen = random();
1161d347a0daSSam Leffler 			dip++;
1162d347a0daSSam Leffler 		}
1163d347a0daSSam Leffler 		ffs_wtfs(fsbtodb(fs, ino_to_fsba(fs,
1164d347a0daSSam Leffler 				  cg * fs->fs_ipg + initediblk)),
1165d347a0daSSam Leffler 		    fs->fs_bsize, buf, fsopts);
1166d347a0daSSam Leffler 		initediblk += INOPB(fs);
1167d347a0daSSam Leffler 		cgp->cg_initediblk = ufs_rw32(initediblk, fsopts->needswap);
1168d347a0daSSam Leffler 	}
1169d347a0daSSam Leffler 
1170d347a0daSSam Leffler 
1171d347a0daSSam Leffler 	ffs_wtfs(fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize, &sbbuf,
1172d347a0daSSam Leffler 	    fsopts);
1173d347a0daSSam Leffler 
1174d347a0daSSam Leffler 					/* now write inode */
1175d347a0daSSam Leffler 	d = fsbtodb(fs, ino_to_fsba(fs, ino));
1176d347a0daSSam Leffler 	ffs_rdfs(d, fs->fs_bsize, buf, fsopts);
1177d347a0daSSam Leffler 	if (fsopts->needswap) {
117801a0f853SOlivier Houchard 		if (ffs_opts->version == 1)
1179*aa90fbedSKirk McKusick 			ffs_dinode1_swap(&dp->dp1,
1180d347a0daSSam Leffler 			    &dp1[ino_to_fsbo(fs, ino)]);
1181d347a0daSSam Leffler 		else
1182*aa90fbedSKirk McKusick 			ffs_dinode2_swap(&dp->dp2,
1183d347a0daSSam Leffler 			    &dp2[ino_to_fsbo(fs, ino)]);
1184d347a0daSSam Leffler 	} else {
118501a0f853SOlivier Houchard 		if (ffs_opts->version == 1)
1186*aa90fbedSKirk McKusick 			dp1[ino_to_fsbo(fs, ino)] = dp->dp1;
1187d347a0daSSam Leffler 		else
1188*aa90fbedSKirk McKusick 			dp2[ino_to_fsbo(fs, ino)] = dp->dp2;
1189d347a0daSSam Leffler 	}
1190d347a0daSSam Leffler 	ffs_wtfs(d, fs->fs_bsize, buf, fsopts);
1191d347a0daSSam Leffler 	free(buf);
1192d347a0daSSam Leffler }
1193d347a0daSSam Leffler 
1194d347a0daSSam Leffler void
1195d347a0daSSam Leffler panic(const char *fmt, ...)
1196d347a0daSSam Leffler {
1197d347a0daSSam Leffler 	va_list ap;
1198d347a0daSSam Leffler 
1199d347a0daSSam Leffler 	va_start(ap, fmt);
1200d347a0daSSam Leffler 	vwarnx(fmt, ap);
1201d347a0daSSam Leffler 	va_end(ap);
1202d347a0daSSam Leffler 	exit(1);
1203d347a0daSSam Leffler }
1204