xref: /netbsd-src/usr.sbin/makefs/ffs.c (revision 7022656d8ab2142ec648610e6c63c908b926ed5b)
1*7022656dSriastradh /*	$NetBSD: ffs.c,v 1.76 2024/06/17 23:53:42 riastradh Exp $	*/
26325773eSlukem 
36325773eSlukem /*
41226af2bSlukem  * Copyright (c) 2001 Wasabi Systems, Inc.
56325773eSlukem  * All rights reserved.
66325773eSlukem  *
76325773eSlukem  * Written by Luke Mewburn for Wasabi Systems, Inc.
86325773eSlukem  *
96325773eSlukem  * Redistribution and use in source and binary forms, with or without
106325773eSlukem  * modification, are permitted provided that the following conditions
116325773eSlukem  * are met:
126325773eSlukem  * 1. Redistributions of source code must retain the above copyright
136325773eSlukem  *    notice, this list of conditions and the following disclaimer.
146325773eSlukem  * 2. Redistributions in binary form must reproduce the above copyright
156325773eSlukem  *    notice, this list of conditions and the following disclaimer in the
166325773eSlukem  *    documentation and/or other materials provided with the distribution.
176325773eSlukem  * 3. All advertising materials mentioning features or use of this software
186325773eSlukem  *    must display the following acknowledgement:
196325773eSlukem  *      This product includes software developed for the NetBSD Project by
206325773eSlukem  *      Wasabi Systems, Inc.
216325773eSlukem  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
226325773eSlukem  *    or promote products derived from this software without specific prior
236325773eSlukem  *    written permission.
246325773eSlukem  *
256325773eSlukem  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
266325773eSlukem  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
276325773eSlukem  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
286325773eSlukem  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
296325773eSlukem  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
306325773eSlukem  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
316325773eSlukem  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
326325773eSlukem  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
336325773eSlukem  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
346325773eSlukem  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
356325773eSlukem  * POSSIBILITY OF SUCH DAMAGE.
366325773eSlukem  */
376325773eSlukem /*
386325773eSlukem  * Copyright (c) 1982, 1986, 1989, 1993
396325773eSlukem  *	The Regents of the University of California.  All rights reserved.
406325773eSlukem  *
416325773eSlukem  * Redistribution and use in source and binary forms, with or without
426325773eSlukem  * modification, are permitted provided that the following conditions
436325773eSlukem  * are met:
446325773eSlukem  * 1. Redistributions of source code must retain the above copyright
456325773eSlukem  *    notice, this list of conditions and the following disclaimer.
466325773eSlukem  * 2. Redistributions in binary form must reproduce the above copyright
476325773eSlukem  *    notice, this list of conditions and the following disclaimer in the
486325773eSlukem  *    documentation and/or other materials provided with the distribution.
49326b2259Sagc  * 3. Neither the name of the University nor the names of its contributors
506325773eSlukem  *    may be used to endorse or promote products derived from this software
516325773eSlukem  *    without specific prior written permission.
526325773eSlukem  *
536325773eSlukem  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
546325773eSlukem  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
556325773eSlukem  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
566325773eSlukem  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
576325773eSlukem  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
586325773eSlukem  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
596325773eSlukem  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
606325773eSlukem  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
616325773eSlukem  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
626325773eSlukem  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
636325773eSlukem  * SUCH DAMAGE.
646325773eSlukem  *
656325773eSlukem  *	@(#)ffs_alloc.c	8.19 (Berkeley) 7/13/95
666325773eSlukem  */
676325773eSlukem 
68b2f78261Sjmc #if HAVE_NBTOOL_CONFIG_H
69b2f78261Sjmc #include "nbtool_config.h"
70b2f78261Sjmc #endif
71b2f78261Sjmc 
72cafb53fcSlukem #include <sys/cdefs.h>
739fbd8888Stv #if defined(__RCSID) && !defined(__lint)
74*7022656dSriastradh __RCSID("$NetBSD: ffs.c,v 1.76 2024/06/17 23:53:42 riastradh Exp $");
75cafb53fcSlukem #endif	/* !__lint */
76cafb53fcSlukem 
776325773eSlukem #include <sys/param.h>
78b2f78261Sjmc 
79b2f78261Sjmc #if !HAVE_NBTOOL_CONFIG_H
806325773eSlukem #include <sys/mount.h>
81b2f78261Sjmc #endif
826325773eSlukem 
836325773eSlukem #include <assert.h>
846325773eSlukem #include <errno.h>
856325773eSlukem #include <fcntl.h>
866325773eSlukem #include <stdarg.h>
876325773eSlukem #include <stdio.h>
886325773eSlukem #include <stdlib.h>
896325773eSlukem #include <string.h>
906325773eSlukem #include <unistd.h>
91e4989541Schristos #include <util.h>
926325773eSlukem 
936325773eSlukem #include "makefs.h"
942406596eSjmc #include "ffs.h"
956325773eSlukem 
967a47cf14Sjmc #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS
972446aaf1Sdbj #include <sys/statvfs.h>
982446aaf1Sdbj #endif
992446aaf1Sdbj 
100557afc60Slukem #include <ufs/ufs/dinode.h>
101557afc60Slukem #include <ufs/ufs/dir.h>
102557afc60Slukem #include <ufs/ffs/fs.h>
103557afc60Slukem #include <ufs/ufs/ufs_bswap.h>
1046325773eSlukem 
105944794a5Slukem #include "ffs/ufs_inode.h"
1066325773eSlukem #include "ffs/newfs_extern.h"
1076325773eSlukem #include "ffs/ffs_extern.h"
1086325773eSlukem 
10942614ed3Sfvdl #undef DIP
11042614ed3Sfvdl #define DIP(dp, field) \
1112406596eSjmc 	((ffs_opts->version == 1) ? \
11242614ed3Sfvdl 	(dp)->ffs1_din.di_##field : (dp)->ffs2_din.di_##field)
11342614ed3Sfvdl 
1146325773eSlukem /*
1156325773eSlukem  * Various file system defaults (cribbed from newfs(8)).
1166325773eSlukem  */
1176325773eSlukem #define	DFL_FRAGSIZE		1024		/* fragment size */
1186325773eSlukem #define	DFL_BLKSIZE		8192		/* block size */
1196325773eSlukem #define	DFL_SECSIZE		512		/* sector size */
120aa99e59fSlukem #define	DFL_CYLSPERGROUP	65536		/* cylinders per group */
1216d5ff7c4Slukem #define	DFL_FRAGSPERINODE	4		/* fragments per inode */
1226325773eSlukem #define	DFL_ROTDELAY		0		/* rotational delay */
1236325773eSlukem #define	DFL_NRPOS		1		/* rotational positions */
1246325773eSlukem #define	DFL_RPM			3600		/* rpm of disk */
1256325773eSlukem #define	DFL_NSECTORS		64		/* # of sectors */
1266325773eSlukem #define	DFL_NTRACKS		16		/* # of tracks */
1276325773eSlukem 
1286325773eSlukem 
1296325773eSlukem typedef struct {
1306325773eSlukem 	u_char		*buf;		/* buf for directory */
1316325773eSlukem 	doff_t		size;		/* full size of buf */
1326325773eSlukem 	doff_t		cur;		/* offset of current entry */
1336325773eSlukem } dirbuf_t;
1346325773eSlukem 
1356325773eSlukem 
1366325773eSlukem static	int	ffs_create_image(const char *, fsinfo_t *);
1376325773eSlukem static	void	ffs_dump_fsinfo(fsinfo_t *);
1386325773eSlukem static	void	ffs_dump_dirbuf(dirbuf_t *, const char *, int);
1396325773eSlukem static	void	ffs_make_dirbuf(dirbuf_t *, const char *, fsnode *, int);
1406325773eSlukem static	int	ffs_populate_dir(const char *, fsnode *, fsinfo_t *);
1416325773eSlukem static	void	ffs_size_dir(fsnode *, fsinfo_t *);
1426325773eSlukem static	void	ffs_validate(const char *, fsnode *, fsinfo_t *);
14342614ed3Sfvdl static	void	ffs_write_file(union dinode *, uint32_t, void *, fsinfo_t *);
14442614ed3Sfvdl static	void	ffs_write_inode(union dinode *, uint32_t, const fsinfo_t *);
14542614ed3Sfvdl static  void	*ffs_build_dinode1(struct ufs1_dinode *, dirbuf_t *, fsnode *,
14642614ed3Sfvdl 				 fsnode *, fsinfo_t *);
14742614ed3Sfvdl static  void	*ffs_build_dinode2(struct ufs2_dinode *, dirbuf_t *, fsnode *,
14842614ed3Sfvdl 				 fsnode *, fsinfo_t *);
14942614ed3Sfvdl 
1506325773eSlukem 
1516325773eSlukem 
152374cb9beSwiz 	/* publicly visible functions */
1532406596eSjmc void
ffs_prep_opts(fsinfo_t * fsopts)1542406596eSjmc ffs_prep_opts(fsinfo_t *fsopts)
1552406596eSjmc {
156e4989541Schristos 	ffs_opt_t *ffs_opts = ecalloc(1, sizeof(*ffs_opts));
1572406596eSjmc 
158e4989541Schristos 	const option_t ffs_options[] = {
159e4989541Schristos 	    { 'b', "bsize", &ffs_opts->bsize, OPT_INT32,
160e4989541Schristos 	      1, INT_MAX, "block size" },
161e4989541Schristos 	    { 'f', "fsize", &ffs_opts->fsize, OPT_INT32,
162e4989541Schristos 	      1, INT_MAX, "fragment size" },
163e4989541Schristos 	    { 'd', "density", &ffs_opts->density, OPT_INT32,
164e4989541Schristos 	      1, INT_MAX, "bytes per inode" },
165e4989541Schristos 	    { 'm', "minfree", &ffs_opts->minfree, OPT_INT32,
166e4989541Schristos 	      0, 99, "minfree" },
167d79005d7Schristos 	    { 'M', "maxbpg", &ffs_opts->maxbpg, OPT_INT32,
168e4989541Schristos 	      1, INT_MAX, "max blocks per file in a cg" },
169e4989541Schristos 	    { 'a', "avgfilesize", &ffs_opts->avgfilesize, OPT_INT32,
170e4989541Schristos 	      1, INT_MAX, "expected average file size" },
171e4989541Schristos 	    { 'n', "avgfpdir", &ffs_opts->avgfpdir, OPT_INT32,
172e4989541Schristos 	      1, INT_MAX, "expected # of files per directory" },
173e4989541Schristos 	    { 'x', "extent", &ffs_opts->maxbsize, OPT_INT32,
174e4989541Schristos 	      1, INT_MAX, "maximum # extent size" },
175e4989541Schristos 	    { 'g', "maxbpcg", &ffs_opts->maxblkspercg, OPT_INT32,
176e4989541Schristos 	      1, INT_MAX, "max # of blocks per group" },
177e4989541Schristos 	    { 'v', "version", &ffs_opts->version, OPT_INT32,
178e4989541Schristos 	      1, 2, "UFS version" },
17950d02345Schristos 	    { 'o', "optimization", NULL, OPT_STRBUF,
18050d02345Schristos 	      0, 0, "Optimization (time|space)" },
181e4989541Schristos 	    { 'l', "label", ffs_opts->label, OPT_STRARRAY,
182e4989541Schristos 	      1, sizeof(ffs_opts->label), "UFS label" },
18387ba0e2aSchs 	    { 'e', "extattr", &ffs_opts->extattr, OPT_INT32,
18487ba0e2aSchs 	      0, 1, "extattr support" },
185e4989541Schristos 	    { .name = NULL }
186e4989541Schristos 	};
1872406596eSjmc 
1882406596eSjmc 	ffs_opts->bsize= -1;
1892406596eSjmc 	ffs_opts->fsize= -1;
1902406596eSjmc 	ffs_opts->cpg= -1;
1912406596eSjmc 	ffs_opts->density= -1;
1922406596eSjmc 	ffs_opts->minfree= -1;
1932406596eSjmc 	ffs_opts->optimization= -1;
1942406596eSjmc 	ffs_opts->maxcontig= -1;
1952406596eSjmc 	ffs_opts->maxbpg= -1;
1962406596eSjmc 	ffs_opts->avgfilesize= -1;
1972406596eSjmc 	ffs_opts->avgfpdir= -1;
1982406596eSjmc 	ffs_opts->version = 1;
19987ba0e2aSchs 	ffs_opts->extattr = 1;
200e4989541Schristos 
201e4989541Schristos 	fsopts->fs_specific = ffs_opts;
202e4989541Schristos 	fsopts->fs_options = copy_opts(ffs_options);
2032406596eSjmc }
2042406596eSjmc 
2052406596eSjmc void
ffs_cleanup_opts(fsinfo_t * fsopts)2062406596eSjmc ffs_cleanup_opts(fsinfo_t *fsopts)
2072406596eSjmc {
2082406596eSjmc 	free(fsopts->fs_specific);
209e4989541Schristos 	free(fsopts->fs_options);
2102406596eSjmc }
2112406596eSjmc 
2126325773eSlukem int
ffs_parse_opts(const char * option,fsinfo_t * fsopts)2136325773eSlukem ffs_parse_opts(const char *option, fsinfo_t *fsopts)
2146325773eSlukem {
2152406596eSjmc 	ffs_opt_t	*ffs_opts = fsopts->fs_specific;
216e4989541Schristos 	option_t *ffs_options = fsopts->fs_options;
21750d02345Schristos 	char buf[1024];
2186325773eSlukem 
219562664d1Schristos 	int	rv;
2206325773eSlukem 
2216325773eSlukem 	assert(option != NULL);
2226325773eSlukem 	assert(fsopts != NULL);
2232406596eSjmc 	assert(ffs_opts != NULL);
2246325773eSlukem 
2256325773eSlukem 	if (debug & DEBUG_FS_PARSE_OPTS)
2266325773eSlukem 		printf("ffs_parse_opts: got `%s'\n", option);
2276325773eSlukem 
22850d02345Schristos 	rv = set_option(ffs_options, option, buf, sizeof(buf));
229562664d1Schristos 	if (rv == -1)
2301c35cd38Schristos 		return 0;
2316325773eSlukem 
232562664d1Schristos 	if (ffs_options[rv].name == NULL)
2336981fea5Schristos 		abort();
2346981fea5Schristos 
23550d02345Schristos 	switch (ffs_options[rv].letter) {
23650d02345Schristos 	case 'o':
23750d02345Schristos 		if (strcmp(buf, "time") == 0) {
2382406596eSjmc 			ffs_opts->optimization = FS_OPTTIME;
23950d02345Schristos 		} else if (strcmp(buf, "space") == 0) {
2402406596eSjmc 			ffs_opts->optimization = FS_OPTSPACE;
2416325773eSlukem 		} else {
24250d02345Schristos 			warnx("Invalid optimization `%s'", buf);
2431c35cd38Schristos 			return 0;
2446325773eSlukem 		}
24550d02345Schristos 		break;
24650d02345Schristos 	default:
24750d02345Schristos 		break;
2481c35cd38Schristos 	}
249562664d1Schristos 	return 1;
2506325773eSlukem }
2516325773eSlukem 
2526325773eSlukem 
2536325773eSlukem void
ffs_makefs(const char * image,const char * dir,fsnode * root,fsinfo_t * fsopts)2546325773eSlukem ffs_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts)
2556325773eSlukem {
2563a06421eSlukem 	struct fs	*superblock;
2576325773eSlukem 	struct timeval	start;
2586325773eSlukem 
2596325773eSlukem 	assert(image != NULL);
2606325773eSlukem 	assert(dir != NULL);
2616325773eSlukem 	assert(root != NULL);
2626325773eSlukem 	assert(fsopts != NULL);
2636325773eSlukem 
2646325773eSlukem 	if (debug & DEBUG_FS_MAKEFS)
2656325773eSlukem 		printf("ffs_makefs: image %s directory %s root %p\n",
2666325773eSlukem 		    image, dir, root);
2676325773eSlukem 
2686325773eSlukem 		/* validate tree and options */
2696325773eSlukem 	TIMER_START(start);
2706325773eSlukem 	ffs_validate(dir, root, fsopts);
2716325773eSlukem 	TIMER_RESULTS(start, "ffs_validate");
2726325773eSlukem 
27372b9ef30Slukem 	printf("Calculated size of `%s': %lld bytes, %lld inodes\n",
27472b9ef30Slukem 	    image, (long long)fsopts->size, (long long)fsopts->inodes);
275aa99e59fSlukem 
2766325773eSlukem 		/* create image */
2776325773eSlukem 	TIMER_START(start);
2786325773eSlukem 	if (ffs_create_image(image, fsopts) == -1)
279ec5d1d82Stsutsui 		errx(EXIT_FAILURE, "Image file `%s' not created.", image);
2806325773eSlukem 	TIMER_RESULTS(start, "ffs_create_image");
2816325773eSlukem 
282dcd34a91Sdholland 	fsopts->curinode = UFS_ROOTINO;
2836325773eSlukem 
2846325773eSlukem 	if (debug & DEBUG_FS_MAKEFS)
2856325773eSlukem 		putchar('\n');
2866325773eSlukem 
2876325773eSlukem 		/* populate image */
28872b9ef30Slukem 	printf("Populating `%s'\n", image);
2896325773eSlukem 	TIMER_START(start);
2906325773eSlukem 	if (! ffs_populate_dir(dir, root, fsopts))
291ec5d1d82Stsutsui 		errx(EXIT_FAILURE, "Image file `%s' not populated.", image);
2926325773eSlukem 	TIMER_RESULTS(start, "ffs_populate_dir");
2936325773eSlukem 
2946325773eSlukem 		/* ensure no outstanding buffers remain */
2956325773eSlukem 	if (debug & DEBUG_FS_MAKEFS)
2966325773eSlukem 		bcleanup();
2976325773eSlukem 
2983a06421eSlukem 		/* update various superblock parameters */
2993a06421eSlukem 	superblock = fsopts->superblock;
3003a06421eSlukem 	superblock->fs_fmod = 0;
3013a06421eSlukem 	superblock->fs_old_cstotal.cs_ndir   = superblock->fs_cstotal.cs_ndir;
3023a06421eSlukem 	superblock->fs_old_cstotal.cs_nbfree = superblock->fs_cstotal.cs_nbfree;
3033a06421eSlukem 	superblock->fs_old_cstotal.cs_nifree = superblock->fs_cstotal.cs_nifree;
3043a06421eSlukem 	superblock->fs_old_cstotal.cs_nffree = superblock->fs_cstotal.cs_nffree;
30523c8fa8cSlukem 
3063a06421eSlukem 		/* write out superblock; image is now complete */
3076325773eSlukem 	ffs_write_superblock(fsopts->superblock, fsopts);
30823c8fa8cSlukem 	if (close(fsopts->fd) == -1)
309ec5d1d82Stsutsui 		err(EXIT_FAILURE, "Closing `%s'", image);
31023c8fa8cSlukem 	fsopts->fd = -1;
31172b9ef30Slukem 	printf("Image `%s' complete\n", image);
3126325773eSlukem }
3136325773eSlukem 
3146325773eSlukem 	/* end of public functions */
3156325773eSlukem 
3166325773eSlukem 
3176325773eSlukem static void
ffs_validate(const char * dir,fsnode * root,fsinfo_t * fsopts)3186325773eSlukem ffs_validate(const char *dir, fsnode *root, fsinfo_t *fsopts)
3196325773eSlukem {
320aa99e59fSlukem 	int32_t	ncg = 1;
321aa99e59fSlukem #if notyet
322aa99e59fSlukem 	int32_t	spc, nspf, ncyl, fssize;
323aa99e59fSlukem #endif
3242406596eSjmc 	ffs_opt_t	*ffs_opts = fsopts->fs_specific;
3256325773eSlukem 
3266325773eSlukem 	assert(dir != NULL);
3276325773eSlukem 	assert(root != NULL);
3286325773eSlukem 	assert(fsopts != NULL);
3292406596eSjmc 	assert(ffs_opts != NULL);
3306325773eSlukem 
3316325773eSlukem 	if (debug & DEBUG_FS_VALIDATE) {
3326325773eSlukem 		printf("ffs_validate: before defaults set:\n");
3336325773eSlukem 		ffs_dump_fsinfo(fsopts);
3346325773eSlukem 	}
3356325773eSlukem 
3366325773eSlukem 		/* set FFS defaults */
337f4821030Slukem 	if (fsopts->sectorsize == -1)
3386325773eSlukem 		fsopts->sectorsize = DFL_SECSIZE;
3392406596eSjmc 	if (ffs_opts->fsize == -1)
3402406596eSjmc 		ffs_opts->fsize = MAX(DFL_FRAGSIZE, fsopts->sectorsize);
3412406596eSjmc 	if (ffs_opts->bsize == -1)
3422406596eSjmc 		ffs_opts->bsize = MIN(DFL_BLKSIZE, 8 * ffs_opts->fsize);
3432406596eSjmc 	if (ffs_opts->cpg == -1)
3442406596eSjmc 		ffs_opts->cpg = DFL_CYLSPERGROUP;
345aa99e59fSlukem 	else
3462406596eSjmc 		ffs_opts->cpgflg = 1;
3476325773eSlukem 				/* fsopts->density is set below */
3482406596eSjmc 	if (ffs_opts->nsectors == -1)
3492406596eSjmc 		ffs_opts->nsectors = DFL_NSECTORS;
3502406596eSjmc 	if (ffs_opts->minfree == -1)
3512406596eSjmc 		ffs_opts->minfree = MINFREE;
3522406596eSjmc 	if (ffs_opts->optimization == -1)
3532406596eSjmc 		ffs_opts->optimization = DEFAULTOPT;
3542406596eSjmc 	if (ffs_opts->maxcontig == -1)
3552406596eSjmc 		ffs_opts->maxcontig =
3562406596eSjmc 		    MAX(1, MIN(MAXPHYS, FFS_MAXBSIZE) / ffs_opts->bsize);
357a3ff3a30Sfvdl 	/* XXX ondisk32 */
3582406596eSjmc 	if (ffs_opts->maxbpg == -1)
3592406596eSjmc 		ffs_opts->maxbpg = ffs_opts->bsize / sizeof(int32_t);
3602406596eSjmc 	if (ffs_opts->avgfilesize == -1)
3612406596eSjmc 		ffs_opts->avgfilesize = AVFILESIZ;
3622406596eSjmc 	if (ffs_opts->avgfpdir == -1)
3632406596eSjmc 		ffs_opts->avgfpdir = AFPDIR;
3646325773eSlukem 
3656325773eSlukem 		/* calculate size of tree */
3666325773eSlukem 	ffs_size_dir(root, fsopts);
367dcd34a91Sdholland 	fsopts->inodes += UFS_ROOTINO;		/* include first two inodes */
3686325773eSlukem 
3696325773eSlukem 	if (debug & DEBUG_FS_VALIDATE)
3706325773eSlukem 		printf("ffs_validate: size of tree: %lld bytes, %lld inodes\n",
3716325773eSlukem 		    (long long)fsopts->size, (long long)fsopts->inodes);
3726325773eSlukem 
3736325773eSlukem 		/* add requested slop */
3746325773eSlukem 	fsopts->size += fsopts->freeblocks;
3756325773eSlukem 	fsopts->inodes += fsopts->freefiles;
3766325773eSlukem 	if (fsopts->freefilepc > 0)
3776325773eSlukem 		fsopts->inodes =
3786325773eSlukem 		    fsopts->inodes * (100 + fsopts->freefilepc) / 100;
3796325773eSlukem 	if (fsopts->freeblockpc > 0)
3806325773eSlukem 		fsopts->size =
3816325773eSlukem 		    fsopts->size * (100 + fsopts->freeblockpc) / 100;
3826325773eSlukem 
3836325773eSlukem 		/* add space needed for superblocks */
38442614ed3Sfvdl 	/*
38542614ed3Sfvdl 	 * The old SBOFF (SBLOCK_UFS1) is used here because makefs is
38642614ed3Sfvdl 	 * typically used for small filesystems where space matters.
38742614ed3Sfvdl 	 * XXX make this an option.
38842614ed3Sfvdl 	 */
38942614ed3Sfvdl 	fsopts->size += (SBLOCK_UFS1 + SBLOCKSIZE) * ncg;
3906325773eSlukem 		/* add space needed to store inodes, x3 for blockmaps, etc */
3912406596eSjmc 	if (ffs_opts->version == 1)
392f8ab7a49Smycroft 		fsopts->size += ncg * DINODE1_SIZE *
3932406596eSjmc 		    roundup(fsopts->inodes / ncg,
3942406596eSjmc 			ffs_opts->bsize / DINODE1_SIZE);
39542614ed3Sfvdl 	else
396f8ab7a49Smycroft 		fsopts->size += ncg * DINODE2_SIZE *
3972406596eSjmc 		    roundup(fsopts->inodes / ncg,
3982406596eSjmc 			ffs_opts->bsize / DINODE2_SIZE);
399aa99e59fSlukem 
4006325773eSlukem 		/* add minfree */
4012406596eSjmc 	if (ffs_opts->minfree > 0)
4026325773eSlukem 		fsopts->size =
4032406596eSjmc 		    fsopts->size * (100 + ffs_opts->minfree) / 100;
4046325773eSlukem 	/*
405fc2aa2b3Smycroft 	 * XXX	any other fs slop to add, such as csum's, bitmaps, etc ??
4066325773eSlukem 	 */
4076325773eSlukem 
4086325773eSlukem 	if (fsopts->size < fsopts->minsize)	/* ensure meets minimum size */
4096325773eSlukem 		fsopts->size = fsopts->minsize;
4106325773eSlukem 
411d74b2fc0Slukem 		/* round up to the next block */
4122406596eSjmc 	fsopts->size = roundup(fsopts->size, ffs_opts->bsize);
4136325773eSlukem 
4146325773eSlukem 		/* calculate density if necessary */
4152406596eSjmc 	if (ffs_opts->density == -1)
4162406596eSjmc 		ffs_opts->density = fsopts->size / fsopts->inodes + 1;
4176325773eSlukem 
4186325773eSlukem 	if (debug & DEBUG_FS_VALIDATE) {
4196325773eSlukem 		printf("ffs_validate: after defaults set:\n");
4206325773eSlukem 		ffs_dump_fsinfo(fsopts);
4216325773eSlukem 		printf("ffs_validate: dir %s; %lld bytes, %lld inodes\n",
4226325773eSlukem 		    dir, (long long)fsopts->size, (long long)fsopts->inodes);
4236325773eSlukem 	}
4246325773eSlukem 		/* now check calculated sizes vs requested sizes */
4256325773eSlukem 	if (fsopts->maxsize > 0 && fsopts->size > fsopts->maxsize) {
426ec5d1d82Stsutsui 		errx(EXIT_FAILURE,
427ec5d1d82Stsutsui 		    "`%s' size of %lld is larger than the maxsize of %lld.",
4286325773eSlukem 		    dir, (long long)fsopts->size, (long long)fsopts->maxsize);
4296325773eSlukem 	}
4306325773eSlukem }
4316325773eSlukem 
4326325773eSlukem 
4336325773eSlukem static void
ffs_dump_fsinfo(fsinfo_t * f)4346325773eSlukem ffs_dump_fsinfo(fsinfo_t *f)
4356325773eSlukem {
4366325773eSlukem 
4372406596eSjmc 	ffs_opt_t	*fs = f->fs_specific;
4382406596eSjmc 
4396325773eSlukem 	printf("fsopts at %p\n", f);
4406325773eSlukem 
4416325773eSlukem 	printf("\tsize %lld, inodes %lld, curinode %u\n",
4426325773eSlukem 	    (long long)f->size, (long long)f->inodes, f->curinode);
4436325773eSlukem 
4446325773eSlukem 	printf("\tminsize %lld, maxsize %lld\n",
4456325773eSlukem 	    (long long)f->minsize, (long long)f->maxsize);
4466325773eSlukem 	printf("\tfree files %lld, freefile %% %d\n",
4476325773eSlukem 	    (long long)f->freefiles, f->freefilepc);
4486325773eSlukem 	printf("\tfree blocks %lld, freeblock %% %d\n",
4496325773eSlukem 	    (long long)f->freeblocks, f->freeblockpc);
4506325773eSlukem 	printf("\tneedswap %d, sectorsize %d\n", f->needswap, f->sectorsize);
4516325773eSlukem 
4526325773eSlukem 	printf("\tbsize %d, fsize %d, cpg %d, density %d\n",
4532406596eSjmc 	    fs->bsize, fs->fsize, fs->cpg, fs->density);
45442614ed3Sfvdl 	printf("\tnsectors %d, rpm %d, minfree %d\n",
4552406596eSjmc 	    fs->nsectors, fs->rpm, fs->minfree);
45642614ed3Sfvdl 	printf("\tmaxcontig %d, maxbpg %d\n",
4572406596eSjmc 	    fs->maxcontig, fs->maxbpg);
4586325773eSlukem 	printf("\toptimization %s\n",
4592406596eSjmc 	    fs->optimization == FS_OPTSPACE ? "space" : "time");
4606325773eSlukem }
4616325773eSlukem 
4626325773eSlukem 
4636325773eSlukem static int
ffs_create_image(const char * image,fsinfo_t * fsopts)4646325773eSlukem ffs_create_image(const char *image, fsinfo_t *fsopts)
4656325773eSlukem {
4667a47cf14Sjmc #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS
4676bd1d6d4Schristos 	struct statvfs	sfs;
4689fbd8888Stv #endif
4696325773eSlukem 	struct fs	*fs;
4706325773eSlukem 	char	*buf;
4716325773eSlukem 	int	i, bufsize;
4726325773eSlukem 	off_t	bufrem;
473911dc957Schristos 	time_t	tstamp;
474c5e90147Schristos 	int	oflags = O_RDWR | O_CREAT;
4756325773eSlukem 
4766325773eSlukem 	assert (image != NULL);
4776325773eSlukem 	assert (fsopts != NULL);
4786325773eSlukem 
4796325773eSlukem 		/* create image */
480c5e90147Schristos 	if (fsopts->offset == 0)
481c5e90147Schristos 		oflags |= O_TRUNC;
482c5e90147Schristos 	if ((fsopts->fd = open(image, oflags, 0666)) == -1) {
4836325773eSlukem 		warn("Can't open `%s' for writing", image);
4846325773eSlukem 		return (-1);
4856325773eSlukem 	}
4866325773eSlukem 
4876325773eSlukem 		/* zero image */
4887a47cf14Sjmc #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS
4896bd1d6d4Schristos 	if (fstatvfs(fsopts->fd, &sfs) == -1) {
4909fbd8888Stv #endif
4916325773eSlukem 		bufsize = 8192;
4927a47cf14Sjmc #if HAVE_STRUCT_STATVFS_F_IOSIZE && HAVE_FSTATVFS
4936bd1d6d4Schristos 		warn("can't fstatvfs `%s', using default %d byte chunk",
4946325773eSlukem 		    image, bufsize);
4956325773eSlukem 	} else
4966325773eSlukem 		bufsize = sfs.f_iosize;
4979fbd8888Stv #endif
4986325773eSlukem 	bufrem = fsopts->size;
49907f6254fSsjg 
50007f6254fSsjg 	if (fsopts->sparse) {
50107f6254fSsjg 		if (ftruncate(fsopts->fd, bufrem) == -1) {
50207f6254fSsjg 			printf ("ERROR in truncate. Sparse option disabled\n");
50307f6254fSsjg 			fsopts->sparse = 0;
50407f6254fSsjg 		} else {
50507f6254fSsjg 			bufrem = 0; /* File truncated at bufrem. Remaining is 0 */
50607f6254fSsjg 			buf = NULL;
50707f6254fSsjg 		}
50807f6254fSsjg 	}
50907f6254fSsjg 
510c5e90147Schristos 	if (fsopts->offset != 0)
511c5e90147Schristos 		if (lseek(fsopts->fd, fsopts->offset, SEEK_SET) == -1) {
512c5e90147Schristos 			warn("can't seek");
513c5e90147Schristos 			return -1;
514c5e90147Schristos 		}
515c5e90147Schristos 
51607f6254fSsjg 	if ((debug & DEBUG_FS_CREATE_IMAGE) && fsopts->sparse == 0)
5176325773eSlukem 		printf(
5186325773eSlukem 		    "zero-ing image `%s', %lld sectors, using %d byte chunks\n",
5196325773eSlukem 		    image, (long long)bufrem, bufsize);
520e4989541Schristos 	if (bufrem > 0)
521e4989541Schristos 		buf = ecalloc(1, bufsize);
5226325773eSlukem 	while (bufrem > 0) {
5236325773eSlukem 		i = write(fsopts->fd, buf, MIN(bufsize, bufrem));
5246325773eSlukem 		if (i == -1) {
5256325773eSlukem 			warn("zeroing image, %lld bytes to go",
5266325773eSlukem 			    (long long)bufrem);
527b06fd00aSrtr 			free(buf);
5286325773eSlukem 			return (-1);
5296325773eSlukem 		}
5306325773eSlukem 		bufrem -= i;
5316325773eSlukem 	}
53207f6254fSsjg 	if (buf)
533b06fd00aSrtr 		free(buf);
5346325773eSlukem 
5356325773eSlukem 		/* make the file system */
5366325773eSlukem 	if (debug & DEBUG_FS_CREATE_IMAGE)
5376325773eSlukem 		printf("calling mkfs(\"%s\", ...)\n", image);
538911dc957Schristos 
539f9f791fdSchristos 	if (stampst.st_ino)
540911dc957Schristos 		tstamp = stampst.st_ctime;
541911dc957Schristos 	else
542911dc957Schristos 		tstamp = start_time.tv_sec;
543911dc957Schristos 
544911dc957Schristos 	srandom(tstamp);
545911dc957Schristos 
546911dc957Schristos 	fs = ffs_mkfs(image, fsopts, tstamp);
5476325773eSlukem 	fsopts->superblock = (void *)fs;
5486325773eSlukem 	if (debug & DEBUG_FS_CREATE_IMAGE) {
5496325773eSlukem 		time_t t;
5506325773eSlukem 
55142614ed3Sfvdl 		t = (time_t)((struct fs *)fsopts->superblock)->fs_time;
5526325773eSlukem 		printf("mkfs returned %p; fs_time %s",
5536325773eSlukem 		    fsopts->superblock, ctime(&t));
55442614ed3Sfvdl 		printf("fs totals: nbfree %lld, nffree %lld, nifree %lld, ndir %lld\n",
55542614ed3Sfvdl 		    (long long)fs->fs_cstotal.cs_nbfree,
55642614ed3Sfvdl 		    (long long)fs->fs_cstotal.cs_nffree,
55742614ed3Sfvdl 		    (long long)fs->fs_cstotal.cs_nifree,
55842614ed3Sfvdl 		    (long long)fs->fs_cstotal.cs_ndir);
5596325773eSlukem 	}
5606325773eSlukem 
561dcd34a91Sdholland 	if ((off_t)(fs->fs_cstotal.cs_nifree + UFS_ROOTINO) < fsopts->inodes) {
5626325773eSlukem 		warnx(
5636d5ff7c4Slukem 		"Image file `%s' has %lld free inodes; %lld are required.",
5646325773eSlukem 		    image,
565dcd34a91Sdholland 		    (long long)(fs->fs_cstotal.cs_nifree + UFS_ROOTINO),
5666325773eSlukem 		    (long long)fsopts->inodes);
5676325773eSlukem 		return (-1);
5686325773eSlukem 	}
5696325773eSlukem 	return (fsopts->fd);
5706325773eSlukem }
5716325773eSlukem 
5726325773eSlukem 
5736325773eSlukem static void
ffs_size_dir(fsnode * root,fsinfo_t * fsopts)5746325773eSlukem ffs_size_dir(fsnode *root, fsinfo_t *fsopts)
5756325773eSlukem {
5766325773eSlukem 	struct direct	tmpdir;
5776325773eSlukem 	fsnode *	node;
5786325773eSlukem 	int		curdirsize, this;
5792406596eSjmc 	ffs_opt_t	*ffs_opts = fsopts->fs_specific;
5806325773eSlukem 
5816325773eSlukem 	/* node may be NULL (empty directory) */
5826325773eSlukem 	assert(fsopts != NULL);
5832406596eSjmc 	assert(ffs_opts != NULL);
5846325773eSlukem 
5856325773eSlukem 	if (debug & DEBUG_FS_SIZE_DIR)
5866325773eSlukem 		printf("ffs_size_dir: entry: bytes %lld inodes %lld\n",
5876325773eSlukem 		    (long long)fsopts->size, (long long)fsopts->inodes);
5886325773eSlukem 
5896325773eSlukem #define	ADDDIRENT(e) do {						\
5906325773eSlukem 	tmpdir.d_namlen = strlen((e));					\
5915a420c1bSdholland 	this = UFS_DIRSIZ(0, &tmpdir, 0);				\
5926325773eSlukem 	if (debug & DEBUG_FS_SIZE_DIR_ADD_DIRENT)			\
5936325773eSlukem 		printf("ADDDIRENT: was: %s (%d) this %d cur %d\n",	\
5946325773eSlukem 		    e, tmpdir.d_namlen, this, curdirsize);		\
5955a420c1bSdholland 	if (this + curdirsize > roundup(curdirsize, UFS_DIRBLKSIZ))	\
5965a420c1bSdholland 		curdirsize = roundup(curdirsize, UFS_DIRBLKSIZ);	\
5976325773eSlukem 	curdirsize += this;						\
5986325773eSlukem 	if (debug & DEBUG_FS_SIZE_DIR_ADD_DIRENT)			\
5996325773eSlukem 		printf("ADDDIRENT: now: %s (%d) this %d cur %d\n",	\
6006325773eSlukem 		    e, tmpdir.d_namlen, this, curdirsize);		\
6016325773eSlukem } while (0);
6026325773eSlukem 
6036325773eSlukem 	/*
6046d5ff7c4Slukem 	 * XXX	this needs to take into account extra space consumed
6056325773eSlukem 	 *	by indirect blocks, etc.
6066325773eSlukem 	 */
6076325773eSlukem #define	ADDSIZE(x) do {							\
6082406596eSjmc 	fsopts->size += roundup((x), ffs_opts->fsize);			\
6096325773eSlukem } while (0);
6106325773eSlukem 
6116325773eSlukem 	curdirsize = 0;
6126325773eSlukem 	for (node = root; node != NULL; node = node->next) {
6136325773eSlukem 		ADDDIRENT(node->name);
6146325773eSlukem 		if (node == root) {			/* we're at "." */
6156325773eSlukem 			assert(strcmp(node->name, ".") == 0);
6166325773eSlukem 			ADDDIRENT("..");
6176d5ff7c4Slukem 		} else if ((node->inode->flags & FI_SIZED) == 0) {
618ebbf3dddSlukem 				/* don't count duplicate names */
619ebbf3dddSlukem 			node->inode->flags |= FI_SIZED;
6206325773eSlukem 			if (debug & DEBUG_FS_SIZE_DIR_NODE)
6216d5ff7c4Slukem 				printf("ffs_size_dir: `%s' size %lld\n",
6226325773eSlukem 				    node->name,
623ebbf3dddSlukem 				    (long long)node->inode->st.st_size);
6246325773eSlukem 			fsopts->inodes++;
6256325773eSlukem 			if (node->type == S_IFREG)
626ebbf3dddSlukem 				ADDSIZE(node->inode->st.st_size);
6276325773eSlukem 			if (node->type == S_IFLNK) {
628b825b96bSchristos 				size_t	slen;
6296325773eSlukem 
6306325773eSlukem 				slen = strlen(node->symlink) + 1;
6312406596eSjmc 				if (slen >= (ffs_opts->version == 1 ?
632dcd34a91Sdholland 						UFS1_MAXSYMLINKLEN :
633dcd34a91Sdholland 						UFS2_MAXSYMLINKLEN))
6346325773eSlukem 					ADDSIZE(slen);
6356325773eSlukem 			}
6366325773eSlukem 		}
6376325773eSlukem 		if (node->type == S_IFDIR)
6386325773eSlukem 			ffs_size_dir(node->child, fsopts);
6396325773eSlukem 	}
6406325773eSlukem 	ADDSIZE(curdirsize);
6416325773eSlukem 
6426325773eSlukem 	if (debug & DEBUG_FS_SIZE_DIR)
6436325773eSlukem 		printf("ffs_size_dir: exit: size %lld inodes %lld\n",
6446325773eSlukem 		    (long long)fsopts->size, (long long)fsopts->inodes);
6456325773eSlukem }
6466325773eSlukem 
64742614ed3Sfvdl static void *
ffs_build_dinode1(struct ufs1_dinode * dinp,dirbuf_t * dbufp,fsnode * cur,fsnode * root,fsinfo_t * fsopts)64842614ed3Sfvdl ffs_build_dinode1(struct ufs1_dinode *dinp, dirbuf_t *dbufp, fsnode *cur,
64942614ed3Sfvdl 		 fsnode *root, fsinfo_t *fsopts)
65042614ed3Sfvdl {
651b825b96bSchristos 	size_t slen;
65242614ed3Sfvdl 	void *membuf;
653f9f791fdSchristos 	struct stat *st = stampst.st_ino ? &stampst : &cur->inode->st;
65442614ed3Sfvdl 
65542614ed3Sfvdl 	memset(dinp, 0, sizeof(*dinp));
65642614ed3Sfvdl 	dinp->di_mode = cur->inode->st.st_mode;
65742614ed3Sfvdl 	dinp->di_nlink = cur->inode->nlink;
65842614ed3Sfvdl 	dinp->di_size = cur->inode->st.st_size;
65942614ed3Sfvdl #if HAVE_STRUCT_STAT_ST_FLAGS
66042614ed3Sfvdl 	dinp->di_flags = cur->inode->st.st_flags;
66142614ed3Sfvdl #endif
66242614ed3Sfvdl #if HAVE_STRUCT_STAT_ST_GEN
66342614ed3Sfvdl 	dinp->di_gen = cur->inode->st.st_gen;
66442614ed3Sfvdl #endif
66542614ed3Sfvdl 	dinp->di_uid = cur->inode->st.st_uid;
66642614ed3Sfvdl 	dinp->di_gid = cur->inode->st.st_gid;
66761db082aSchristos 
66861db082aSchristos 	dinp->di_atime = st->st_atime;
66961db082aSchristos 	dinp->di_mtime = st->st_mtime;
67061db082aSchristos 	dinp->di_ctime = st->st_ctime;
67161db082aSchristos #if HAVE_STRUCT_STAT_ST_MTIMENSEC
67261db082aSchristos 	dinp->di_atimensec = st->st_atimensec;
67361db082aSchristos 	dinp->di_mtimensec = st->st_mtimensec;
67461db082aSchristos 	dinp->di_ctimensec = st->st_ctimensec;
67561db082aSchristos #endif
67642614ed3Sfvdl 		/* not set: di_db, di_ib, di_blocks, di_spare */
67742614ed3Sfvdl 
67842614ed3Sfvdl 	membuf = NULL;
67942614ed3Sfvdl 	if (cur == root) {			/* "."; write dirbuf */
68042614ed3Sfvdl 		membuf = dbufp->buf;
68142614ed3Sfvdl 		dinp->di_size = dbufp->size;
68242614ed3Sfvdl 	} else if (S_ISBLK(cur->type) || S_ISCHR(cur->type)) {
68342614ed3Sfvdl 		dinp->di_size = 0;	/* a device */
68442614ed3Sfvdl 		dinp->di_rdev =
68542614ed3Sfvdl 		    ufs_rw32(cur->inode->st.st_rdev, fsopts->needswap);
68642614ed3Sfvdl 	} else if (S_ISLNK(cur->type)) {	/* symlink */
68742614ed3Sfvdl 		slen = strlen(cur->symlink);
688dcd34a91Sdholland 		if (slen < UFS1_MAXSYMLINKLEN) {	/* short link */
68942614ed3Sfvdl 			memcpy(dinp->di_db, cur->symlink, slen);
69042614ed3Sfvdl 		} else
69142614ed3Sfvdl 			membuf = cur->symlink;
69242614ed3Sfvdl 		dinp->di_size = slen;
69342614ed3Sfvdl 	}
69442614ed3Sfvdl 	return membuf;
69542614ed3Sfvdl }
69642614ed3Sfvdl 
69742614ed3Sfvdl static void *
ffs_build_dinode2(struct ufs2_dinode * dinp,dirbuf_t * dbufp,fsnode * cur,fsnode * root,fsinfo_t * fsopts)69842614ed3Sfvdl ffs_build_dinode2(struct ufs2_dinode *dinp, dirbuf_t *dbufp, fsnode *cur,
69942614ed3Sfvdl 		 fsnode *root, fsinfo_t *fsopts)
70042614ed3Sfvdl {
701b825b96bSchristos 	size_t slen;
70242614ed3Sfvdl 	void *membuf;
703f9f791fdSchristos 	struct stat *st = stampst.st_ino ? &stampst : &cur->inode->st;
70442614ed3Sfvdl 
70542614ed3Sfvdl 	memset(dinp, 0, sizeof(*dinp));
70642614ed3Sfvdl 	dinp->di_mode = cur->inode->st.st_mode;
70742614ed3Sfvdl 	dinp->di_nlink = cur->inode->nlink;
70842614ed3Sfvdl 	dinp->di_size = cur->inode->st.st_size;
70942614ed3Sfvdl #if HAVE_STRUCT_STAT_ST_FLAGS
71042614ed3Sfvdl 	dinp->di_flags = cur->inode->st.st_flags;
71142614ed3Sfvdl #endif
71242614ed3Sfvdl #if HAVE_STRUCT_STAT_ST_GEN
71342614ed3Sfvdl 	dinp->di_gen = cur->inode->st.st_gen;
71442614ed3Sfvdl #endif
71542614ed3Sfvdl 	dinp->di_uid = cur->inode->st.st_uid;
71642614ed3Sfvdl 	dinp->di_gid = cur->inode->st.st_gid;
71761db082aSchristos 
71861db082aSchristos 	dinp->di_atime = st->st_atime;
71961db082aSchristos 	dinp->di_mtime = st->st_mtime;
72061db082aSchristos 	dinp->di_ctime = st->st_ctime;
72161db082aSchristos #if HAVE_STRUCT_STAT_ST_MTIMENSEC
72261db082aSchristos 	dinp->di_atimensec = st->st_atimensec;
72361db082aSchristos 	dinp->di_mtimensec = st->st_mtimensec;
72461db082aSchristos 	dinp->di_ctimensec = st->st_ctimensec;
72561db082aSchristos #endif
72661db082aSchristos #if HAVE_STRUCT_STAT_BIRTHTIME
72761db082aSchristos 	dinp->di_birthtime = st->st_birthtime;
72861db082aSchristos 	dinp->di_birthnsec = st->st_birthtimensec;
72961db082aSchristos #endif
73042614ed3Sfvdl 		/* not set: di_db, di_ib, di_blocks, di_spare */
73142614ed3Sfvdl 
73242614ed3Sfvdl 	membuf = NULL;
73342614ed3Sfvdl 	if (cur == root) {			/* "."; write dirbuf */
73442614ed3Sfvdl 		membuf = dbufp->buf;
73542614ed3Sfvdl 		dinp->di_size = dbufp->size;
73642614ed3Sfvdl 	} else if (S_ISBLK(cur->type) || S_ISCHR(cur->type)) {
73742614ed3Sfvdl 		dinp->di_size = 0;	/* a device */
73842614ed3Sfvdl 		dinp->di_rdev =
73942614ed3Sfvdl 		    ufs_rw64(cur->inode->st.st_rdev, fsopts->needswap);
74042614ed3Sfvdl 	} else if (S_ISLNK(cur->type)) {	/* symlink */
74142614ed3Sfvdl 		slen = strlen(cur->symlink);
742dcd34a91Sdholland 		if (slen < UFS2_MAXSYMLINKLEN) {	/* short link */
74342614ed3Sfvdl 			memcpy(dinp->di_db, cur->symlink, slen);
74442614ed3Sfvdl 		} else
74542614ed3Sfvdl 			membuf = cur->symlink;
74642614ed3Sfvdl 		dinp->di_size = slen;
74742614ed3Sfvdl 	}
74842614ed3Sfvdl 	return membuf;
74942614ed3Sfvdl }
7506325773eSlukem 
7516325773eSlukem static int
ffs_populate_dir(const char * dir,fsnode * root,fsinfo_t * fsopts)7526325773eSlukem ffs_populate_dir(const char *dir, fsnode *root, fsinfo_t *fsopts)
7536325773eSlukem {
7546325773eSlukem 	fsnode		*cur;
7556325773eSlukem 	dirbuf_t	dirbuf;
75642614ed3Sfvdl 	union dinode	din;
7576325773eSlukem 	void		*membuf;
7586325773eSlukem 	char		path[MAXPATHLEN + 1];
7592406596eSjmc 	ffs_opt_t	*ffs_opts = fsopts->fs_specific;
7606325773eSlukem 
7616325773eSlukem 	assert(dir != NULL);
7626325773eSlukem 	assert(root != NULL);
7636325773eSlukem 	assert(fsopts != NULL);
7642406596eSjmc 	assert(ffs_opts != NULL);
7656325773eSlukem 
7666325773eSlukem 	(void)memset(&dirbuf, 0, sizeof(dirbuf));
7676325773eSlukem 
7686325773eSlukem 	if (debug & DEBUG_FS_POPULATE)
7696325773eSlukem 		printf("ffs_populate_dir: PASS 1  dir %s node %p\n", dir, root);
7706325773eSlukem 
7716325773eSlukem 		/*
7726325773eSlukem 		 * pass 1: allocate inode numbers, build directory `file'
7736325773eSlukem 		 */
7746325773eSlukem 	for (cur = root; cur != NULL; cur = cur->next) {
775ebbf3dddSlukem 		if ((cur->inode->flags & FI_ALLOCATED) == 0) {
776ebbf3dddSlukem 			cur->inode->flags |= FI_ALLOCATED;
777ebbf3dddSlukem 			if (cur == root && cur->parent != NULL)
778ebbf3dddSlukem 				cur->inode->ino = cur->parent->inode->ino;
7796325773eSlukem 			else {
780ebbf3dddSlukem 				cur->inode->ino = fsopts->curinode;
7816325773eSlukem 				fsopts->curinode++;
7826325773eSlukem 			}
783ebbf3dddSlukem 		}
7846325773eSlukem 		ffs_make_dirbuf(&dirbuf, cur->name, cur, fsopts->needswap);
7856325773eSlukem 		if (cur == root) {		/* we're at "."; add ".." */
7866325773eSlukem 			ffs_make_dirbuf(&dirbuf, "..",
7876325773eSlukem 			    cur->parent == NULL ? cur : cur->parent->first,
7886325773eSlukem 			    fsopts->needswap);
789ebbf3dddSlukem 			root->inode->nlink++;	/* count my parent's link */
7906325773eSlukem 		} else if (cur->child != NULL)
791ebbf3dddSlukem 			root->inode->nlink++;	/* count my child's link */
7926325773eSlukem 
7936325773eSlukem 		/*
7946325773eSlukem 		 * XXX	possibly write file and long symlinks here,
7956325773eSlukem 		 *	ensuring that blocks get written before inodes?
7966325773eSlukem 		 *	otoh, this isn't a real filesystem, so who
7976325773eSlukem 		 *	cares about ordering? :-)
7986325773eSlukem 		 */
7996325773eSlukem 	}
8006325773eSlukem 	if (debug & DEBUG_FS_POPULATE_DIRBUF)
8016325773eSlukem 		ffs_dump_dirbuf(&dirbuf, dir, fsopts->needswap);
8026325773eSlukem 
8036325773eSlukem 		/*
8046325773eSlukem 		 * pass 2: write out dirbuf, then non-directories at this level
8056325773eSlukem 		 */
8066325773eSlukem 	if (debug & DEBUG_FS_POPULATE)
8076325773eSlukem 		printf("ffs_populate_dir: PASS 2  dir %s\n", dir);
8086325773eSlukem 	for (cur = root; cur != NULL; cur = cur->next) {
809ebbf3dddSlukem 		if (cur->inode->flags & FI_WRITTEN)
8106325773eSlukem 			continue;		/* skip hard-linked entries */
811ebbf3dddSlukem 		cur->inode->flags |= FI_WRITTEN;
8126325773eSlukem 
813f1cc0951Schristos 		if ((size_t)snprintf(path, sizeof(path), "%s/%s/%s", cur->root,
814f1cc0951Schristos 		    cur->path, cur->name) >= sizeof(path))
815ec5d1d82Stsutsui 			errx(EXIT_FAILURE, "Pathname too long.");
8166325773eSlukem 
8176325773eSlukem 		if (cur->child != NULL)
8186325773eSlukem 			continue;		/* child creates own inode */
8196325773eSlukem 
8206325773eSlukem 				/* build on-disk inode */
8212406596eSjmc 		if (ffs_opts->version == 1)
82242614ed3Sfvdl 			membuf = ffs_build_dinode1(&din.ffs1_din, &dirbuf, cur,
82342614ed3Sfvdl 			    root, fsopts);
82442614ed3Sfvdl 		else
82542614ed3Sfvdl 			membuf = ffs_build_dinode2(&din.ffs2_din, &dirbuf, cur,
82642614ed3Sfvdl 			    root, fsopts);
8276325773eSlukem 
8286325773eSlukem 		if (debug & DEBUG_FS_POPULATE_NODE) {
829*7022656dSriastradh 			printf("ffs_populate_dir: writing ino %lld, %s",
830*7022656dSriastradh 			    (long long)cur->inode->ino, inode_type(cur->type));
831ebbf3dddSlukem 			if (cur->inode->nlink > 1)
832ebbf3dddSlukem 				printf(", nlink %d", cur->inode->nlink);
8336325773eSlukem 			putchar('\n');
8346325773eSlukem 		}
8356325773eSlukem 
8366325773eSlukem 		if (membuf != NULL) {
837ebbf3dddSlukem 			ffs_write_file(&din, cur->inode->ino, membuf, fsopts);
8386325773eSlukem 		} else if (S_ISREG(cur->type)) {
839ebbf3dddSlukem 			ffs_write_file(&din, cur->inode->ino, path, fsopts);
8406325773eSlukem 		} else {
8416325773eSlukem 			assert (! S_ISDIR(cur->type));
842ebbf3dddSlukem 			ffs_write_inode(&din, cur->inode->ino, fsopts);
8436325773eSlukem 		}
8446325773eSlukem 	}
8456325773eSlukem 
8466325773eSlukem 		/*
8476325773eSlukem 		 * pass 3: write out sub-directories
8486325773eSlukem 		 */
8496325773eSlukem 	if (debug & DEBUG_FS_POPULATE)
8506325773eSlukem 		printf("ffs_populate_dir: PASS 3  dir %s\n", dir);
8516325773eSlukem 	for (cur = root; cur != NULL; cur = cur->next) {
8526325773eSlukem 		if (cur->child == NULL)
8536325773eSlukem 			continue;
854b825b96bSchristos 		if ((size_t)snprintf(path, sizeof(path), "%s/%s", dir,
855b825b96bSchristos 		    cur->name) >= sizeof(path))
856ec5d1d82Stsutsui 			errx(EXIT_FAILURE, "Pathname too long.");
8576325773eSlukem 		if (! ffs_populate_dir(path, cur->child, fsopts))
8586325773eSlukem 			return (0);
8596325773eSlukem 	}
8606325773eSlukem 
8616325773eSlukem 	if (debug & DEBUG_FS_POPULATE)
8626325773eSlukem 		printf("ffs_populate_dir: DONE dir %s\n", dir);
8636325773eSlukem 
8646325773eSlukem 		/* cleanup */
8656325773eSlukem 	if (dirbuf.buf != NULL)
8666325773eSlukem 		free(dirbuf.buf);
8676325773eSlukem 	return (1);
8686325773eSlukem }
8696325773eSlukem 
8706325773eSlukem 
8716325773eSlukem static void
ffs_write_file(union dinode * din,uint32_t ino,void * buf,fsinfo_t * fsopts)87242614ed3Sfvdl ffs_write_file(union dinode *din, uint32_t ino, void *buf, fsinfo_t *fsopts)
8736325773eSlukem {
8746325773eSlukem 	int 	isfile, ffd;
8756325773eSlukem 	char	*fbuf, *p;
8766325773eSlukem 	off_t	bufleft, chunk, offset;
8777af558ebSdyoung 	ssize_t nread;
8786325773eSlukem 	struct inode	in;
8796325773eSlukem 	struct buf *	bp;
8802406596eSjmc 	ffs_opt_t	*ffs_opts = fsopts->fs_specific;
881d84c38aeSchristos 	struct vnode vp = { fsopts, NULL };
8826325773eSlukem 
8836325773eSlukem 	assert (din != NULL);
8846325773eSlukem 	assert (buf != NULL);
8856325773eSlukem 	assert (fsopts != NULL);
8862406596eSjmc 	assert (ffs_opts != NULL);
8876325773eSlukem 
88842614ed3Sfvdl 	isfile = S_ISREG(DIP(din, mode));
8896325773eSlukem 	fbuf = NULL;
8906325773eSlukem 	ffd = -1;
891c8e07a4eSlukem 	p = NULL;
8926325773eSlukem 
89342614ed3Sfvdl 	in.i_fs = (struct fs *)fsopts->superblock;
894d84c38aeSchristos 	in.i_devvp = &vp;
89542614ed3Sfvdl 
8966325773eSlukem 	if (debug & DEBUG_FS_WRITE_FILE) {
8976325773eSlukem 		printf(
8986325773eSlukem 		    "ffs_write_file: ino %u, din %p, isfile %d, %s, size %lld",
89942614ed3Sfvdl 		    ino, din, isfile, inode_type(DIP(din, mode) & S_IFMT),
90042614ed3Sfvdl 		    (long long)DIP(din, size));
9016325773eSlukem 		if (isfile)
9026325773eSlukem 			printf(", file '%s'\n", (char *)buf);
9036325773eSlukem 		else
9046325773eSlukem 			printf(", buffer %p\n", buf);
9056325773eSlukem 	}
9066325773eSlukem 
9076325773eSlukem 	in.i_number = ino;
90842614ed3Sfvdl 	in.i_size = DIP(din, size);
9092406596eSjmc 	if (ffs_opts->version == 1)
91042614ed3Sfvdl 		memcpy(&in.i_din.ffs1_din, &din->ffs1_din,
91142614ed3Sfvdl 		    sizeof(in.i_din.ffs1_din));
91242614ed3Sfvdl 	else
91342614ed3Sfvdl 		memcpy(&in.i_din.ffs2_din, &din->ffs2_din,
91442614ed3Sfvdl 		    sizeof(in.i_din.ffs2_din));
9156325773eSlukem 
91642614ed3Sfvdl 	if (DIP(din, size) == 0)
9176325773eSlukem 		goto write_inode_and_leave;		/* mmm, cheating */
9186325773eSlukem 
9196325773eSlukem 	if (isfile) {
920e4989541Schristos 		fbuf = emalloc(ffs_opts->bsize);
9216325773eSlukem 		if ((ffd = open((char *)buf, O_RDONLY, 0444)) == -1) {
9226325773eSlukem 			warn("Can't open `%s' for reading", (char *)buf);
9236325773eSlukem 			goto leave_ffs_write_file;
9246325773eSlukem 		}
9256325773eSlukem 	} else {
9266325773eSlukem 		p = buf;
9276325773eSlukem 	}
9286325773eSlukem 
9296325773eSlukem 	chunk = 0;
93042614ed3Sfvdl 	for (bufleft = DIP(din, size); bufleft > 0; bufleft -= chunk) {
9312406596eSjmc 		chunk = MIN(bufleft, ffs_opts->bsize);
9327af558ebSdyoung 		if (!isfile)
9337af558ebSdyoung 			;
9347af558ebSdyoung 		else if ((nread = read(ffd, fbuf, chunk)) == -1)
9357af558ebSdyoung 			err(EXIT_FAILURE, "Reading `%s', %lld bytes to go",
9366325773eSlukem 			    (char *)buf, (long long)bufleft);
9377af558ebSdyoung 		else if (nread != chunk)
9387af558ebSdyoung 			errx(EXIT_FAILURE, "Reading `%s', %lld bytes to go, "
9397af558ebSdyoung 			    "read %zd bytes, expected %ju bytes, does "
9407af558ebSdyoung 			    "metalog size= attribute mismatch source size?",
9417af558ebSdyoung 			    (char *)buf, (long long)bufleft, nread,
9427af558ebSdyoung 			    (uintmax_t)chunk);
9437af558ebSdyoung 		else
9446325773eSlukem 			p = fbuf;
94542614ed3Sfvdl 		offset = DIP(din, size) - bufleft;
9466325773eSlukem 		if (debug & DEBUG_FS_WRITE_FILE_BLOCK)
9476325773eSlukem 			printf(
9486325773eSlukem 		"ffs_write_file: write %p offset %lld size %lld left %lld\n",
9496325773eSlukem 			    p, (long long)offset,
9506325773eSlukem 			    (long long)chunk, (long long)bufleft);
9516325773eSlukem 	/*
9526325773eSlukem 	 * XXX	if holey support is desired, do the check here
9536325773eSlukem 	 *
9546325773eSlukem 	 * XXX	might need to write out last bit in fragroundup
9556325773eSlukem 	 *	sized chunk. however, ffs_balloc() handles this for us
9566325773eSlukem 	 */
9576325773eSlukem 		errno = ffs_balloc(&in, offset, chunk, &bp);
958aa99e59fSlukem  bad_ffs_write_file:
9596325773eSlukem 		if (errno != 0)
960ec5d1d82Stsutsui 			err(EXIT_FAILURE,
961aa99e59fSlukem 			    "Writing inode %d (%s), bytes %lld + %lld",
962aa99e59fSlukem 			    ino,
963aa99e59fSlukem 			    isfile ? (char *)buf :
96442614ed3Sfvdl 			      inode_type(DIP(din, mode) & S_IFMT),
9656325773eSlukem 			    (long long)offset, (long long)chunk);
9666325773eSlukem 		memcpy(bp->b_data, p, chunk);
9676325773eSlukem 		errno = bwrite(bp);
9686325773eSlukem 		if (errno != 0)
969aa99e59fSlukem 			goto bad_ffs_write_file;
9706325773eSlukem 		if (!isfile)
9716325773eSlukem 			p += chunk;
9726325773eSlukem 	}
9736325773eSlukem 
9746325773eSlukem  write_inode_and_leave:
97542614ed3Sfvdl 	ffs_write_inode(&in.i_din, in.i_number, fsopts);
9766325773eSlukem 
9776325773eSlukem  leave_ffs_write_file:
9786325773eSlukem 	if (fbuf)
9796325773eSlukem 		free(fbuf);
9806325773eSlukem 	if (ffd != -1)
9816325773eSlukem 		close(ffd);
9826325773eSlukem }
9836325773eSlukem 
9846325773eSlukem 
9856325773eSlukem static void
ffs_dump_dirbuf(dirbuf_t * dbuf,const char * dir,int needswap)9866325773eSlukem ffs_dump_dirbuf(dirbuf_t *dbuf, const char *dir, int needswap)
9876325773eSlukem {
9886325773eSlukem 	doff_t		i;
9896325773eSlukem 	struct direct	*de;
990ebbf3dddSlukem 	uint16_t	reclen;
9916325773eSlukem 
9926325773eSlukem 	assert (dbuf != NULL);
9936325773eSlukem 	assert (dir != NULL);
9946325773eSlukem 	printf("ffs_dump_dirbuf: dir %s size %d cur %d\n",
9956325773eSlukem 	    dir, dbuf->size, dbuf->cur);
9966325773eSlukem 
9976325773eSlukem 	for (i = 0; i < dbuf->size; ) {
9986325773eSlukem 		de = (struct direct *)(dbuf->buf + i);
9996325773eSlukem 		reclen = ufs_rw16(de->d_reclen, needswap);
10006325773eSlukem 		printf(
10016325773eSlukem 	    " inode %4d %7s offset %4d reclen %3d namlen %3d name %s\n",
1002327a5715Sthorpej 		    ufs_rw32(de->d_fileno, needswap),
10036325773eSlukem 		    inode_type(DTTOIF(de->d_type)), i, reclen,
10046325773eSlukem 		    de->d_namlen, de->d_name);
10056325773eSlukem 		i += reclen;
10066325773eSlukem 		assert(reclen > 0);
10076325773eSlukem 	}
10086325773eSlukem }
10096325773eSlukem 
10106325773eSlukem static void
ffs_make_dirbuf(dirbuf_t * dbuf,const char * name,fsnode * node,int needswap)10116325773eSlukem ffs_make_dirbuf(dirbuf_t *dbuf, const char *name, fsnode *node, int needswap)
10126325773eSlukem {
10136325773eSlukem 	struct direct	de, *dp;
1014ebbf3dddSlukem 	uint16_t	llen, reclen;
1015046051d4Sfvdl 	u_char		*newbuf;
10166325773eSlukem 
10176325773eSlukem 	assert (dbuf != NULL);
10186325773eSlukem 	assert (name != NULL);
10196325773eSlukem 	assert (node != NULL);
10206325773eSlukem 					/* create direct entry */
10216325773eSlukem 	(void)memset(&de, 0, sizeof(de));
1022327a5715Sthorpej 	de.d_fileno = ufs_rw32(node->inode->ino, needswap);
10236325773eSlukem 	de.d_type = IFTODT(node->type);
1024ebbf3dddSlukem 	de.d_namlen = (uint8_t)strlen(name);
10256325773eSlukem 	strcpy(de.d_name, name);
10265a420c1bSdholland 	reclen = UFS_DIRSIZ(0, &de, needswap);
10276325773eSlukem 	de.d_reclen = ufs_rw16(reclen, needswap);
10286325773eSlukem 
10296325773eSlukem 	dp = (struct direct *)(dbuf->buf + dbuf->cur);
10306325773eSlukem 	llen = 0;
10316325773eSlukem 	if (dp != NULL)
10325a420c1bSdholland 		llen = UFS_DIRSIZ(0, dp, needswap);
10336325773eSlukem 
10346325773eSlukem 	if (debug & DEBUG_FS_MAKE_DIRBUF)
10356325773eSlukem 		printf(
10366325773eSlukem 		    "ffs_make_dirbuf: dbuf siz %d cur %d lastlen %d\n"
10376325773eSlukem 		    "  ino %d type %d reclen %d namlen %d name %.30s\n",
10386325773eSlukem 		    dbuf->size, dbuf->cur, llen,
1039327a5715Sthorpej 		    ufs_rw32(de.d_fileno, needswap), de.d_type, reclen,
10406325773eSlukem 		    de.d_namlen, de.d_name);
10416325773eSlukem 
10425a420c1bSdholland 	if (reclen + dbuf->cur + llen > roundup(dbuf->size, UFS_DIRBLKSIZ)) {
10436325773eSlukem 		if (debug & DEBUG_FS_MAKE_DIRBUF)
10446325773eSlukem 			printf("ffs_make_dirbuf: growing buf to %d\n",
10455a420c1bSdholland 			    dbuf->size + UFS_DIRBLKSIZ);
10465a420c1bSdholland 		newbuf = erealloc(dbuf->buf, dbuf->size + UFS_DIRBLKSIZ);
1047e99b62a1Sitojun 		dbuf->buf = newbuf;
10485a420c1bSdholland 		dbuf->size += UFS_DIRBLKSIZ;
10495a420c1bSdholland 		memset(dbuf->buf + dbuf->size - UFS_DIRBLKSIZ, 0, UFS_DIRBLKSIZ);
10505a420c1bSdholland 		dbuf->cur = dbuf->size - UFS_DIRBLKSIZ;
10512e3cc3a1Schristos 	} else if (dp) {			/* shrink end of previous */
10526325773eSlukem 		dp->d_reclen = ufs_rw16(llen,needswap);
10536325773eSlukem 		dbuf->cur += llen;
10546325773eSlukem 	}
10556325773eSlukem 	dp = (struct direct *)(dbuf->buf + dbuf->cur);
10566325773eSlukem 	memcpy(dp, &de, reclen);
10576325773eSlukem 	dp->d_reclen = ufs_rw16(dbuf->size - dbuf->cur, needswap);
10586325773eSlukem }
10596325773eSlukem 
10606325773eSlukem /*
10616325773eSlukem  * cribbed from sys/ufs/ffs/ffs_alloc.c
10626325773eSlukem  */
10636325773eSlukem static void
ffs_write_inode(union dinode * dp,uint32_t ino,const fsinfo_t * fsopts)106442614ed3Sfvdl ffs_write_inode(union dinode *dp, uint32_t ino, const fsinfo_t *fsopts)
10656325773eSlukem {
106642614ed3Sfvdl 	char 		*buf;
106742614ed3Sfvdl 	struct ufs1_dinode *dp1;
106842614ed3Sfvdl 	struct ufs2_dinode *dp2, *dip;
10696325773eSlukem 	struct cg	*cgp;
10706325773eSlukem 	struct fs	*fs;
1071f298a94bSchs 	uint32_t	cg, cgino, i;
10726325773eSlukem 	daddr_t		d;
1073be48f412Slukem 	char		sbbuf[FFS_MAXBSIZE];
1074b825b96bSchristos 	uint32_t	initediblk;
10752406596eSjmc 	ffs_opt_t	*ffs_opts = fsopts->fs_specific;
10766325773eSlukem 
107742614ed3Sfvdl 	assert (dp != NULL);
10786325773eSlukem 	assert (ino > 0);
10796325773eSlukem 	assert (fsopts != NULL);
10802406596eSjmc 	assert (ffs_opts != NULL);
10816325773eSlukem 
10826325773eSlukem 	fs = (struct fs *)fsopts->superblock;
10836325773eSlukem 	cg = ino_to_cg(fs, ino);
10846325773eSlukem 	cgino = ino % fs->fs_ipg;
10856325773eSlukem 	if (debug & DEBUG_FS_WRITE_INODE)
10866325773eSlukem 		printf("ffs_write_inode: din %p ino %u cg %d cgino %d\n",
108742614ed3Sfvdl 		    dp, ino, cg, cgino);
10886325773eSlukem 
10892737439dSdholland 	ffs_rdfs(FFS_FSBTODB(fs, cgtod(fs, cg)), (int)fs->fs_cgsize, &sbbuf,
10906325773eSlukem 	    fsopts);
10916325773eSlukem 	cgp = (struct cg *)sbbuf;
10926325773eSlukem 	if (!cg_chkmagic(cgp, fsopts->needswap))
1093ec5d1d82Stsutsui 		errx(EXIT_FAILURE,
1094ec5d1d82Stsutsui 		    "ffs_write_inode: cg %d: bad magic number", cg);
10956325773eSlukem 
10966325773eSlukem 	assert (isclr(cg_inosused(cgp, fsopts->needswap), cgino));
10976325773eSlukem 
1098e4989541Schristos 	buf = emalloc(fs->fs_bsize);
109942614ed3Sfvdl 	dp1 = (struct ufs1_dinode *)buf;
110042614ed3Sfvdl 	dp2 = (struct ufs2_dinode *)buf;
110142614ed3Sfvdl 
11026325773eSlukem 	if (fs->fs_cstotal.cs_nifree == 0)
1103ec5d1d82Stsutsui 		errx(EXIT_FAILURE,
1104ec5d1d82Stsutsui 		    "ffs_write_inode: fs out of inodes for ino %u", ino);
11056325773eSlukem 	if (fs->fs_cs(fs, cg).cs_nifree == 0)
1106ec5d1d82Stsutsui 		errx(EXIT_FAILURE,
11076325773eSlukem 		    "ffs_write_inode: cg %d out of inodes for ino %u",
11086325773eSlukem 		    cg, ino);
11096325773eSlukem 	setbit(cg_inosused(cgp, fsopts->needswap), cgino);
11106325773eSlukem 	ufs_add32(cgp->cg_cs.cs_nifree, -1, fsopts->needswap);
11116325773eSlukem 	fs->fs_cstotal.cs_nifree--;
11126325773eSlukem 	fs->fs_cs(fs, cg).cs_nifree--;
111342614ed3Sfvdl 	if (S_ISDIR(DIP(dp, mode))) {
11146325773eSlukem 		ufs_add32(cgp->cg_cs.cs_ndir, 1, fsopts->needswap);
11156325773eSlukem 		fs->fs_cstotal.cs_ndir++;
11166325773eSlukem 		fs->fs_cs(fs, cg).cs_ndir++;
11176325773eSlukem 	}
111842614ed3Sfvdl 
111942614ed3Sfvdl 	/*
112042614ed3Sfvdl 	 * Initialize inode blocks on the fly for UFS2.
112142614ed3Sfvdl 	 */
112242614ed3Sfvdl 	initediblk = ufs_rw32(cgp->cg_initediblk, fsopts->needswap);
1123743f435aSchristos 	while (ffs_opts->version == 2 &&
1124f1333577Sdholland 	    (uint32_t)(cgino + FFS_INOPB(fs)) > initediblk &&
112542614ed3Sfvdl 	    initediblk < ufs_rw32(cgp->cg_niblk, fsopts->needswap)) {
112642614ed3Sfvdl 		memset(buf, 0, fs->fs_bsize);
112742614ed3Sfvdl 		dip = (struct ufs2_dinode *)buf;
1128f1333577Sdholland 		for (i = 0; i < FFS_INOPB(fs); i++) {
1129bd84ee9cSfvdl 			dip->di_gen = random() / 2 + 1;
113042614ed3Sfvdl 			dip++;
113142614ed3Sfvdl 		}
11322737439dSdholland 		ffs_wtfs(FFS_FSBTODB(fs, ino_to_fsba(fs,
113342614ed3Sfvdl 				  cg * fs->fs_ipg + initediblk)),
113442614ed3Sfvdl 		    fs->fs_bsize, buf, fsopts);
1135f1333577Sdholland 		initediblk += FFS_INOPB(fs);
113642614ed3Sfvdl 		cgp->cg_initediblk = ufs_rw32(initediblk, fsopts->needswap);
113742614ed3Sfvdl 	}
113842614ed3Sfvdl 
113942614ed3Sfvdl 
11402737439dSdholland 	ffs_wtfs(FFS_FSBTODB(fs, cgtod(fs, cg)), (int)fs->fs_cgsize, &sbbuf,
11416325773eSlukem 	    fsopts);
11426325773eSlukem 
11436325773eSlukem 					/* now write inode */
11442737439dSdholland 	d = FFS_FSBTODB(fs, ino_to_fsba(fs, ino));
114542614ed3Sfvdl 	ffs_rdfs(d, fs->fs_bsize, buf, fsopts);
114642614ed3Sfvdl 	if (fsopts->needswap) {
11472406596eSjmc 		if (ffs_opts->version == 1)
114842614ed3Sfvdl 			ffs_dinode1_swap(&dp->ffs1_din,
114942614ed3Sfvdl 			    &dp1[ino_to_fsbo(fs, ino)]);
11506325773eSlukem 		else
115142614ed3Sfvdl 			ffs_dinode2_swap(&dp->ffs2_din,
115242614ed3Sfvdl 			    &dp2[ino_to_fsbo(fs, ino)]);
115342614ed3Sfvdl 	} else {
11542406596eSjmc 		if (ffs_opts->version == 1)
115542614ed3Sfvdl 			dp1[ino_to_fsbo(fs, ino)] = dp->ffs1_din;
115642614ed3Sfvdl 		else
115742614ed3Sfvdl 			dp2[ino_to_fsbo(fs, ino)] = dp->ffs2_din;
115842614ed3Sfvdl 	}
115942614ed3Sfvdl 	ffs_wtfs(d, fs->fs_bsize, buf, fsopts);
116042614ed3Sfvdl 	free(buf);
11616325773eSlukem }
11626325773eSlukem 
11636325773eSlukem void
panic(const char * fmt,...)11646325773eSlukem panic(const char *fmt, ...)
11656325773eSlukem {
11666325773eSlukem 	va_list ap;
11676325773eSlukem 
11686325773eSlukem 	va_start(ap, fmt);
11696325773eSlukem 	vwarnx(fmt, ap);
11706325773eSlukem 	va_end(ap);
1171ec5d1d82Stsutsui 	exit(EXIT_FAILURE);
11726325773eSlukem }
1173