xref: /freebsd/usr.sbin/makefs/zfs.c (revision 187084dd)
1240afd8cSMark Johnston /*-
2240afd8cSMark Johnston  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3240afd8cSMark Johnston  *
4240afd8cSMark Johnston  * Copyright (c) 2022 The FreeBSD Foundation
5240afd8cSMark Johnston  *
6240afd8cSMark Johnston  * This software was developed by Mark Johnston under sponsorship from
7240afd8cSMark Johnston  * the FreeBSD Foundation.
8240afd8cSMark Johnston  *
9240afd8cSMark Johnston  * Redistribution and use in source and binary forms, with or without
10240afd8cSMark Johnston  * modification, are permitted provided that the following conditions are
11240afd8cSMark Johnston  * met:
12240afd8cSMark Johnston  * 1. Redistributions of source code must retain the above copyright
13240afd8cSMark Johnston  *    notice, this list of conditions and the following disclaimer.
14240afd8cSMark Johnston  * 2. Redistributions in binary form must reproduce the above copyright
15240afd8cSMark Johnston  *    notice, this list of conditions and the following disclaimer in
16240afd8cSMark Johnston  *    the documentation and/or other materials provided with the distribution.
17240afd8cSMark Johnston  *
18240afd8cSMark Johnston  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19240afd8cSMark Johnston  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20240afd8cSMark Johnston  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21240afd8cSMark Johnston  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22240afd8cSMark Johnston  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23240afd8cSMark Johnston  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24240afd8cSMark Johnston  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25240afd8cSMark Johnston  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26240afd8cSMark Johnston  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27240afd8cSMark Johnston  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28240afd8cSMark Johnston  * SUCH DAMAGE.
29240afd8cSMark Johnston  */
30240afd8cSMark Johnston 
31240afd8cSMark Johnston #include <sys/param.h>
32240afd8cSMark Johnston #include <sys/errno.h>
33240afd8cSMark Johnston #include <sys/queue.h>
34240afd8cSMark Johnston 
35240afd8cSMark Johnston #include <assert.h>
36240afd8cSMark Johnston #include <fcntl.h>
37187084ddSMark Johnston #include <stdalign.h>
38240afd8cSMark Johnston #include <stdbool.h>
39240afd8cSMark Johnston #include <stddef.h>
40240afd8cSMark Johnston #include <stdlib.h>
41240afd8cSMark Johnston #include <string.h>
42240afd8cSMark Johnston #include <unistd.h>
43240afd8cSMark Johnston 
44240afd8cSMark Johnston #include <util.h>
45240afd8cSMark Johnston 
46240afd8cSMark Johnston #include "makefs.h"
47240afd8cSMark Johnston #include "zfs.h"
48240afd8cSMark Johnston 
49240afd8cSMark Johnston #define	VDEV_LABEL_SPACE	\
50240afd8cSMark Johnston 	((off_t)(VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE))
51240afd8cSMark Johnston _Static_assert(VDEV_LABEL_SPACE <= MINDEVSIZE, "");
52240afd8cSMark Johnston 
53240afd8cSMark Johnston #define	MINMSSIZE		((off_t)1 << 24) /* 16MB */
54240afd8cSMark Johnston #define	DFLTMSSIZE		((off_t)1 << 29) /* 512MB */
55240afd8cSMark Johnston #define	MAXMSSIZE		((off_t)1 << 34) /* 16GB */
56240afd8cSMark Johnston 
57240afd8cSMark Johnston #define	INDIR_LEVELS		6
58240afd8cSMark Johnston /* Indirect blocks are always 128KB. */
59240afd8cSMark Johnston #define	BLKPTR_PER_INDIR	(MAXBLOCKSIZE / sizeof(blkptr_t))
60240afd8cSMark Johnston 
61240afd8cSMark Johnston struct dnode_cursor {
62240afd8cSMark Johnston 	char		inddir[INDIR_LEVELS][MAXBLOCKSIZE];
63240afd8cSMark Johnston 	off_t		indloc;
64240afd8cSMark Johnston 	off_t		indspace;
65240afd8cSMark Johnston 	dnode_phys_t	*dnode;
66240afd8cSMark Johnston 	off_t		dataoff;
67240afd8cSMark Johnston 	off_t		datablksz;
68240afd8cSMark Johnston };
69240afd8cSMark Johnston 
70240afd8cSMark Johnston void
71240afd8cSMark Johnston zfs_prep_opts(fsinfo_t *fsopts)
72240afd8cSMark Johnston {
73187084ddSMark Johnston 	size_t align;
74187084ddSMark Johnston 
75187084ddSMark Johnston 	align = alignof(uint64_t);
76187084ddSMark Johnston 	zfs_opt_t *zfs = aligned_alloc(align, roundup2(sizeof(*zfs), align));
77187084ddSMark Johnston 	if (zfs == NULL)
78187084ddSMark Johnston 		err(1, "aligned_alloc");
79187084ddSMark Johnston 	memset(zfs, 0, sizeof(*zfs));
80240afd8cSMark Johnston 
81240afd8cSMark Johnston 	const option_t zfs_options[] = {
82240afd8cSMark Johnston 		{ '\0', "bootfs", &zfs->bootfs, OPT_STRPTR,
83240afd8cSMark Johnston 		  0, 0, "Bootable dataset" },
84240afd8cSMark Johnston 		{ '\0', "mssize", &zfs->mssize, OPT_INT64,
85240afd8cSMark Johnston 		  MINMSSIZE, MAXMSSIZE, "Metaslab size" },
86240afd8cSMark Johnston 		{ '\0', "poolname", &zfs->poolname, OPT_STRPTR,
87240afd8cSMark Johnston 		  0, 0, "ZFS pool name" },
88240afd8cSMark Johnston 		{ '\0', "rootpath", &zfs->rootpath, OPT_STRPTR,
89240afd8cSMark Johnston 		  0, 0, "Prefix for all dataset mount points" },
90240afd8cSMark Johnston 		{ '\0', "ashift", &zfs->ashift, OPT_INT32,
91240afd8cSMark Johnston 		  MINBLOCKSHIFT, MAXBLOCKSHIFT, "ZFS pool ashift" },
92240afd8cSMark Johnston 		{ '\0', "nowarn", &zfs->nowarn, OPT_BOOL,
93240afd8cSMark Johnston 		  0, 0, "Suppress warning about experimental ZFS support" },
94240afd8cSMark Johnston 		{ .name = NULL }
95240afd8cSMark Johnston 	};
96240afd8cSMark Johnston 
97240afd8cSMark Johnston 	STAILQ_INIT(&zfs->datasetdescs);
98240afd8cSMark Johnston 
99240afd8cSMark Johnston 	fsopts->fs_specific = zfs;
100240afd8cSMark Johnston 	fsopts->fs_options = copy_opts(zfs_options);
101240afd8cSMark Johnston }
102240afd8cSMark Johnston 
103240afd8cSMark Johnston int
104240afd8cSMark Johnston zfs_parse_opts(const char *option, fsinfo_t *fsopts)
105240afd8cSMark Johnston {
106240afd8cSMark Johnston 	zfs_opt_t *zfs;
107240afd8cSMark Johnston 	struct dataset_desc *dsdesc;
108240afd8cSMark Johnston 	char buf[BUFSIZ], *opt, *val;
109240afd8cSMark Johnston 	int rv;
110240afd8cSMark Johnston 
111240afd8cSMark Johnston 	zfs = fsopts->fs_specific;
112240afd8cSMark Johnston 
113240afd8cSMark Johnston 	opt = val = estrdup(option);
114240afd8cSMark Johnston 	opt = strsep(&val, "=");
115240afd8cSMark Johnston 	if (strcmp(opt, "fs") == 0) {
116240afd8cSMark Johnston 		if (val == NULL)
117240afd8cSMark Johnston 			errx(1, "invalid filesystem parameters `%s'", option);
118240afd8cSMark Johnston 
119240afd8cSMark Johnston 		/*
120240afd8cSMark Johnston 		 * Dataset descriptions will be parsed later, in dsl_init().
121240afd8cSMark Johnston 		 * Just stash them away for now.
122240afd8cSMark Johnston 		 */
123240afd8cSMark Johnston 		dsdesc = ecalloc(1, sizeof(*dsdesc));
124240afd8cSMark Johnston 		dsdesc->params = estrdup(val);
125240afd8cSMark Johnston 		free(opt);
126240afd8cSMark Johnston 		STAILQ_INSERT_TAIL(&zfs->datasetdescs, dsdesc, next);
127240afd8cSMark Johnston 		return (1);
128240afd8cSMark Johnston 	}
129240afd8cSMark Johnston 	free(opt);
130240afd8cSMark Johnston 
131240afd8cSMark Johnston 	rv = set_option(fsopts->fs_options, option, buf, sizeof(buf));
132240afd8cSMark Johnston 	return (rv == -1 ? 0 : 1);
133240afd8cSMark Johnston }
134240afd8cSMark Johnston 
135240afd8cSMark Johnston static void
136240afd8cSMark Johnston zfs_size_vdev(fsinfo_t *fsopts)
137240afd8cSMark Johnston {
138240afd8cSMark Johnston 	zfs_opt_t *zfs;
139240afd8cSMark Johnston 	off_t asize, mssize, vdevsize, vdevsize1;
140240afd8cSMark Johnston 
141240afd8cSMark Johnston 	zfs = fsopts->fs_specific;
142240afd8cSMark Johnston 
143240afd8cSMark Johnston 	assert(fsopts->maxsize != 0);
144240afd8cSMark Johnston 	assert(zfs->ashift != 0);
145240afd8cSMark Johnston 
146240afd8cSMark Johnston 	/*
147240afd8cSMark Johnston 	 * Figure out how big the vdev should be.
148240afd8cSMark Johnston 	 */
149240afd8cSMark Johnston 	vdevsize = rounddown2(fsopts->maxsize, 1 << zfs->ashift);
150240afd8cSMark Johnston 	if (vdevsize < MINDEVSIZE)
151240afd8cSMark Johnston 		errx(1, "maximum image size is too small");
152240afd8cSMark Johnston 	if (vdevsize < fsopts->minsize || vdevsize > fsopts->maxsize) {
153240afd8cSMark Johnston 		errx(1, "image size bounds must be multiples of %d",
154240afd8cSMark Johnston 		    1 << zfs->ashift);
155240afd8cSMark Johnston 	}
156240afd8cSMark Johnston 	asize = vdevsize - VDEV_LABEL_SPACE;
157240afd8cSMark Johnston 
158240afd8cSMark Johnston 	/*
159240afd8cSMark Johnston 	 * Size metaslabs according to the following heuristic:
160240afd8cSMark Johnston 	 * - provide at least 8 metaslabs,
161240afd8cSMark Johnston 	 * - without using a metaslab size larger than 512MB.
162240afd8cSMark Johnston 	 * This approximates what OpenZFS does without being complicated.  In
163240afd8cSMark Johnston 	 * practice we expect pools to be expanded upon first use, and OpenZFS
164240afd8cSMark Johnston 	 * does not resize metaslabs in that case, so there is no right answer
165240afd8cSMark Johnston 	 * here.  In general we want to provide large metaslabs even if the
166240afd8cSMark Johnston 	 * image size is small, and 512MB is a reasonable size for pools up to
167240afd8cSMark Johnston 	 * several hundred gigabytes.
168240afd8cSMark Johnston 	 *
169240afd8cSMark Johnston 	 * The user may override this heuristic using the "-o mssize" option.
170240afd8cSMark Johnston 	 */
171240afd8cSMark Johnston 	mssize = zfs->mssize;
172240afd8cSMark Johnston 	if (mssize == 0) {
173240afd8cSMark Johnston 		mssize = MAX(MIN(asize / 8, DFLTMSSIZE), MINMSSIZE);
174240afd8cSMark Johnston 		if (!powerof2(mssize))
175240afd8cSMark Johnston 			mssize = 1l << (flsll(mssize) - 1);
176240afd8cSMark Johnston 	}
177240afd8cSMark Johnston 	if (!powerof2(mssize))
178240afd8cSMark Johnston 		errx(1, "metaslab size must be a power of 2");
179240afd8cSMark Johnston 
180240afd8cSMark Johnston 	/*
181240afd8cSMark Johnston 	 * If we have some slop left over, try to cover it by resizing the vdev,
182240afd8cSMark Johnston 	 * subject to the maxsize and minsize parameters.
183240afd8cSMark Johnston 	 */
184240afd8cSMark Johnston 	if (asize % mssize != 0) {
185240afd8cSMark Johnston 		vdevsize1 = rounddown2(asize, mssize) + VDEV_LABEL_SPACE;
186240afd8cSMark Johnston 		if (vdevsize1 < fsopts->minsize)
187240afd8cSMark Johnston 			vdevsize1 = roundup2(asize, mssize) + VDEV_LABEL_SPACE;
188240afd8cSMark Johnston 		if (vdevsize1 <= fsopts->maxsize)
189240afd8cSMark Johnston 			vdevsize = vdevsize1;
190240afd8cSMark Johnston 	}
191240afd8cSMark Johnston 	asize = vdevsize - VDEV_LABEL_SPACE;
192240afd8cSMark Johnston 
193240afd8cSMark Johnston 	zfs->asize = asize;
194240afd8cSMark Johnston 	zfs->vdevsize = vdevsize;
195240afd8cSMark Johnston 	zfs->mssize = mssize;
196240afd8cSMark Johnston 	zfs->msshift = flsll(mssize) - 1;
197240afd8cSMark Johnston 	zfs->mscount = asize / mssize;
198240afd8cSMark Johnston }
199240afd8cSMark Johnston 
200240afd8cSMark Johnston /*
201240afd8cSMark Johnston  * Validate options and set some default values.
202240afd8cSMark Johnston  */
203240afd8cSMark Johnston static void
204240afd8cSMark Johnston zfs_check_opts(fsinfo_t *fsopts)
205240afd8cSMark Johnston {
206240afd8cSMark Johnston 	zfs_opt_t *zfs;
207240afd8cSMark Johnston 
208240afd8cSMark Johnston 	zfs = fsopts->fs_specific;
209240afd8cSMark Johnston 
210240afd8cSMark Johnston 	if (fsopts->offset != 0)
211240afd8cSMark Johnston 		errx(1, "unhandled offset option");
212240afd8cSMark Johnston 	if (fsopts->maxsize == 0)
213240afd8cSMark Johnston 		errx(1, "an image size must be specified");
214240afd8cSMark Johnston 
215240afd8cSMark Johnston 	if (zfs->poolname == NULL)
216240afd8cSMark Johnston 		errx(1, "a pool name must be specified");
217240afd8cSMark Johnston 
218240afd8cSMark Johnston 	if (zfs->rootpath == NULL)
219240afd8cSMark Johnston 		easprintf(&zfs->rootpath, "/%s", zfs->poolname);
220240afd8cSMark Johnston 	if (zfs->rootpath[0] != '/')
221240afd8cSMark Johnston 		errx(1, "mountpoint `%s' must be absolute", zfs->rootpath);
222240afd8cSMark Johnston 
223240afd8cSMark Johnston 	if (zfs->ashift == 0)
224240afd8cSMark Johnston 		zfs->ashift = 12;
225240afd8cSMark Johnston 
226240afd8cSMark Johnston 	zfs_size_vdev(fsopts);
227240afd8cSMark Johnston }
228240afd8cSMark Johnston 
229240afd8cSMark Johnston void
230240afd8cSMark Johnston zfs_cleanup_opts(fsinfo_t *fsopts)
231240afd8cSMark Johnston {
232240afd8cSMark Johnston 	struct dataset_desc *d, *tmp;
233240afd8cSMark Johnston 	zfs_opt_t *zfs;
234240afd8cSMark Johnston 
235240afd8cSMark Johnston 	zfs = fsopts->fs_specific;
236240afd8cSMark Johnston 	free(zfs->rootpath);
237240afd8cSMark Johnston 	free(zfs->bootfs);
238240afd8cSMark Johnston 	free(__DECONST(void *, zfs->poolname));
239240afd8cSMark Johnston 	STAILQ_FOREACH_SAFE(d, &zfs->datasetdescs, next, tmp) {
240240afd8cSMark Johnston 		free(d->params);
241240afd8cSMark Johnston 		free(d);
242240afd8cSMark Johnston 	}
243240afd8cSMark Johnston 	free(zfs);
244240afd8cSMark Johnston 	free(fsopts->fs_options);
245240afd8cSMark Johnston }
246240afd8cSMark Johnston 
247240afd8cSMark Johnston static size_t
248240afd8cSMark Johnston nvlist_size(const nvlist_t *nvl)
249240afd8cSMark Johnston {
250240afd8cSMark Johnston 	return (sizeof(nvl->nv_header) + nvl->nv_size);
251240afd8cSMark Johnston }
252240afd8cSMark Johnston 
253240afd8cSMark Johnston static void
254240afd8cSMark Johnston nvlist_copy(const nvlist_t *nvl, char *buf, size_t sz)
255240afd8cSMark Johnston {
256240afd8cSMark Johnston 	assert(sz >= nvlist_size(nvl));
257240afd8cSMark Johnston 
258240afd8cSMark Johnston 	memcpy(buf, &nvl->nv_header, sizeof(nvl->nv_header));
259240afd8cSMark Johnston 	memcpy(buf + sizeof(nvl->nv_header), nvl->nv_data, nvl->nv_size);
260240afd8cSMark Johnston }
261240afd8cSMark Johnston 
262240afd8cSMark Johnston static nvlist_t *
263240afd8cSMark Johnston pool_config_nvcreate(zfs_opt_t *zfs)
264240afd8cSMark Johnston {
265240afd8cSMark Johnston 	nvlist_t *featuresnv, *poolnv;
266240afd8cSMark Johnston 
267240afd8cSMark Johnston 	poolnv = nvlist_create(NV_UNIQUE_NAME);
268240afd8cSMark Johnston 	nvlist_add_uint64(poolnv, ZPOOL_CONFIG_POOL_TXG, TXG);
269240afd8cSMark Johnston 	nvlist_add_uint64(poolnv, ZPOOL_CONFIG_VERSION, SPA_VERSION);
270240afd8cSMark Johnston 	nvlist_add_uint64(poolnv, ZPOOL_CONFIG_POOL_STATE, POOL_STATE_EXPORTED);
271240afd8cSMark Johnston 	nvlist_add_string(poolnv, ZPOOL_CONFIG_POOL_NAME, zfs->poolname);
272240afd8cSMark Johnston 	nvlist_add_uint64(poolnv, ZPOOL_CONFIG_POOL_GUID, zfs->poolguid);
273240afd8cSMark Johnston 	nvlist_add_uint64(poolnv, ZPOOL_CONFIG_TOP_GUID, zfs->vdevguid);
274240afd8cSMark Johnston 	nvlist_add_uint64(poolnv, ZPOOL_CONFIG_GUID, zfs->vdevguid);
275240afd8cSMark Johnston 	nvlist_add_uint64(poolnv, ZPOOL_CONFIG_VDEV_CHILDREN, 1);
276240afd8cSMark Johnston 
277240afd8cSMark Johnston 	featuresnv = nvlist_create(NV_UNIQUE_NAME);
278240afd8cSMark Johnston 	nvlist_add_nvlist(poolnv, ZPOOL_CONFIG_FEATURES_FOR_READ, featuresnv);
279240afd8cSMark Johnston 	nvlist_destroy(featuresnv);
280240afd8cSMark Johnston 
281240afd8cSMark Johnston 	return (poolnv);
282240afd8cSMark Johnston }
283240afd8cSMark Johnston 
284240afd8cSMark Johnston static nvlist_t *
285240afd8cSMark Johnston pool_disk_vdev_config_nvcreate(zfs_opt_t *zfs)
286240afd8cSMark Johnston {
287240afd8cSMark Johnston 	nvlist_t *diskvdevnv;
288240afd8cSMark Johnston 
289240afd8cSMark Johnston 	assert(zfs->objarrid != 0);
290240afd8cSMark Johnston 
291240afd8cSMark Johnston 	diskvdevnv = nvlist_create(NV_UNIQUE_NAME);
292240afd8cSMark Johnston 	nvlist_add_string(diskvdevnv, ZPOOL_CONFIG_TYPE, VDEV_TYPE_DISK);
293240afd8cSMark Johnston 	nvlist_add_uint64(diskvdevnv, ZPOOL_CONFIG_ASHIFT, zfs->ashift);
294240afd8cSMark Johnston 	nvlist_add_uint64(diskvdevnv, ZPOOL_CONFIG_ASIZE, zfs->asize);
295240afd8cSMark Johnston 	nvlist_add_uint64(diskvdevnv, ZPOOL_CONFIG_GUID, zfs->vdevguid);
296240afd8cSMark Johnston 	nvlist_add_uint64(diskvdevnv, ZPOOL_CONFIG_ID, 0);
297240afd8cSMark Johnston 	nvlist_add_string(diskvdevnv, ZPOOL_CONFIG_PATH, "/dev/null");
298240afd8cSMark Johnston 	nvlist_add_uint64(diskvdevnv, ZPOOL_CONFIG_WHOLE_DISK, 1);
299240afd8cSMark Johnston 	nvlist_add_uint64(diskvdevnv, ZPOOL_CONFIG_CREATE_TXG, TXG);
300240afd8cSMark Johnston 	nvlist_add_uint64(diskvdevnv, ZPOOL_CONFIG_METASLAB_ARRAY,
301240afd8cSMark Johnston 	    zfs->objarrid);
302240afd8cSMark Johnston 	nvlist_add_uint64(diskvdevnv, ZPOOL_CONFIG_METASLAB_SHIFT,
303240afd8cSMark Johnston 	    zfs->msshift);
304240afd8cSMark Johnston 
305240afd8cSMark Johnston 	return (diskvdevnv);
306240afd8cSMark Johnston }
307240afd8cSMark Johnston 
308240afd8cSMark Johnston static nvlist_t *
309240afd8cSMark Johnston pool_root_vdev_config_nvcreate(zfs_opt_t *zfs)
310240afd8cSMark Johnston {
311240afd8cSMark Johnston 	nvlist_t *diskvdevnv, *rootvdevnv;
312240afd8cSMark Johnston 
313240afd8cSMark Johnston 	diskvdevnv = pool_disk_vdev_config_nvcreate(zfs);
314240afd8cSMark Johnston 	rootvdevnv = nvlist_create(NV_UNIQUE_NAME);
315240afd8cSMark Johnston 
316240afd8cSMark Johnston 	nvlist_add_uint64(rootvdevnv, ZPOOL_CONFIG_ID, 0);
317240afd8cSMark Johnston 	nvlist_add_uint64(rootvdevnv, ZPOOL_CONFIG_GUID, zfs->poolguid);
318240afd8cSMark Johnston 	nvlist_add_string(rootvdevnv, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT);
319240afd8cSMark Johnston 	nvlist_add_uint64(rootvdevnv, ZPOOL_CONFIG_CREATE_TXG, TXG);
320240afd8cSMark Johnston 	nvlist_add_nvlist_array(rootvdevnv, ZPOOL_CONFIG_CHILDREN, &diskvdevnv,
321240afd8cSMark Johnston 	    1);
322240afd8cSMark Johnston 	nvlist_destroy(diskvdevnv);
323240afd8cSMark Johnston 
324240afd8cSMark Johnston 	return (rootvdevnv);
325240afd8cSMark Johnston }
326240afd8cSMark Johnston 
327240afd8cSMark Johnston /*
328240afd8cSMark Johnston  * Create the pool's "config" object, which contains an nvlist describing pool
329240afd8cSMark Johnston  * parameters and the vdev topology.  It is similar but not identical to the
330240afd8cSMark Johnston  * nvlist stored in vdev labels.  The main difference is that vdev labels do not
331240afd8cSMark Johnston  * describe the full vdev tree and in particular do not contain the "root"
332240afd8cSMark Johnston  * meta-vdev.
333240afd8cSMark Johnston  */
334240afd8cSMark Johnston static void
335240afd8cSMark Johnston pool_init_objdir_config(zfs_opt_t *zfs, zfs_zap_t *objdir)
336240afd8cSMark Johnston {
337240afd8cSMark Johnston 	dnode_phys_t *dnode;
338240afd8cSMark Johnston 	nvlist_t *poolconfig, *vdevconfig;
339240afd8cSMark Johnston 	void *configbuf;
340240afd8cSMark Johnston 	uint64_t dnid;
341240afd8cSMark Johnston 	off_t configloc, configblksz;
342240afd8cSMark Johnston 	int error;
343240afd8cSMark Johnston 
344240afd8cSMark Johnston 	dnode = objset_dnode_bonus_alloc(zfs->mos, DMU_OT_PACKED_NVLIST,
345240afd8cSMark Johnston 	    DMU_OT_PACKED_NVLIST_SIZE, sizeof(uint64_t), &dnid);
346240afd8cSMark Johnston 
347240afd8cSMark Johnston 	poolconfig = pool_config_nvcreate(zfs);
348240afd8cSMark Johnston 
349240afd8cSMark Johnston 	vdevconfig = pool_root_vdev_config_nvcreate(zfs);
350240afd8cSMark Johnston 	nvlist_add_nvlist(poolconfig, ZPOOL_CONFIG_VDEV_TREE, vdevconfig);
351240afd8cSMark Johnston 	nvlist_destroy(vdevconfig);
352240afd8cSMark Johnston 
353240afd8cSMark Johnston 	error = nvlist_export(poolconfig);
354240afd8cSMark Johnston 	if (error != 0)
355240afd8cSMark Johnston 		errc(1, error, "nvlist_export");
356240afd8cSMark Johnston 
357240afd8cSMark Johnston 	configblksz = nvlist_size(poolconfig);
358240afd8cSMark Johnston 	configloc = objset_space_alloc(zfs, zfs->mos, &configblksz);
359240afd8cSMark Johnston 	configbuf = ecalloc(1, configblksz);
360240afd8cSMark Johnston 	nvlist_copy(poolconfig, configbuf, configblksz);
361240afd8cSMark Johnston 
362240afd8cSMark Johnston 	vdev_pwrite_dnode_data(zfs, dnode, configbuf, configblksz, configloc);
363240afd8cSMark Johnston 
364240afd8cSMark Johnston 	dnode->dn_datablkszsec = configblksz >> MINBLOCKSHIFT;
365240afd8cSMark Johnston 	dnode->dn_flags = DNODE_FLAG_USED_BYTES;
366240afd8cSMark Johnston 	*(uint64_t *)DN_BONUS(dnode) = nvlist_size(poolconfig);
367240afd8cSMark Johnston 
368240afd8cSMark Johnston 	zap_add_uint64(objdir, DMU_POOL_CONFIG, dnid);
369240afd8cSMark Johnston 
370240afd8cSMark Johnston 	nvlist_destroy(poolconfig);
371240afd8cSMark Johnston 	free(configbuf);
372240afd8cSMark Johnston }
373240afd8cSMark Johnston 
374240afd8cSMark Johnston /*
375240afd8cSMark Johnston  * Add objects block pointer list objects, used for deferred frees.  We don't do
376240afd8cSMark Johnston  * anything with them, but they need to be present or OpenZFS will refuse to
377240afd8cSMark Johnston  * import the pool.
378240afd8cSMark Johnston  */
379240afd8cSMark Johnston static void
380240afd8cSMark Johnston pool_init_objdir_bplists(zfs_opt_t *zfs __unused, zfs_zap_t *objdir)
381240afd8cSMark Johnston {
382240afd8cSMark Johnston 	uint64_t dnid;
383240afd8cSMark Johnston 
384240afd8cSMark Johnston 	(void)objset_dnode_bonus_alloc(zfs->mos, DMU_OT_BPOBJ, DMU_OT_BPOBJ_HDR,
385240afd8cSMark Johnston 	    BPOBJ_SIZE_V2, &dnid);
386240afd8cSMark Johnston 	zap_add_uint64(objdir, DMU_POOL_FREE_BPOBJ, dnid);
387240afd8cSMark Johnston 
388240afd8cSMark Johnston 	(void)objset_dnode_bonus_alloc(zfs->mos, DMU_OT_BPOBJ, DMU_OT_BPOBJ_HDR,
389240afd8cSMark Johnston 	    BPOBJ_SIZE_V2, &dnid);
390240afd8cSMark Johnston 	zap_add_uint64(objdir, DMU_POOL_SYNC_BPLIST, dnid);
391240afd8cSMark Johnston }
392240afd8cSMark Johnston 
393240afd8cSMark Johnston /*
394240afd8cSMark Johnston  * Add required feature metadata objects.  We don't know anything about ZFS
395240afd8cSMark Johnston  * features, so the objects are just empty ZAPs.
396240afd8cSMark Johnston  */
397240afd8cSMark Johnston static void
398240afd8cSMark Johnston pool_init_objdir_feature_maps(zfs_opt_t *zfs, zfs_zap_t *objdir)
399240afd8cSMark Johnston {
400240afd8cSMark Johnston 	dnode_phys_t *dnode;
401240afd8cSMark Johnston 	uint64_t dnid;
402240afd8cSMark Johnston 
403240afd8cSMark Johnston 	dnode = objset_dnode_alloc(zfs->mos, DMU_OTN_ZAP_METADATA, &dnid);
404240afd8cSMark Johnston 	zap_add_uint64(objdir, DMU_POOL_FEATURES_FOR_READ, dnid);
405240afd8cSMark Johnston 	zap_write(zfs, zap_alloc(zfs->mos, dnode));
406240afd8cSMark Johnston 
407240afd8cSMark Johnston 	dnode = objset_dnode_alloc(zfs->mos, DMU_OTN_ZAP_METADATA, &dnid);
408240afd8cSMark Johnston 	zap_add_uint64(objdir, DMU_POOL_FEATURES_FOR_WRITE, dnid);
409240afd8cSMark Johnston 	zap_write(zfs, zap_alloc(zfs->mos, dnode));
410240afd8cSMark Johnston 
411240afd8cSMark Johnston 	dnode = objset_dnode_alloc(zfs->mos, DMU_OTN_ZAP_METADATA, &dnid);
412240afd8cSMark Johnston 	zap_add_uint64(objdir, DMU_POOL_FEATURE_DESCRIPTIONS, dnid);
413240afd8cSMark Johnston 	zap_write(zfs, zap_alloc(zfs->mos, dnode));
414240afd8cSMark Johnston }
415240afd8cSMark Johnston 
416240afd8cSMark Johnston static void
417240afd8cSMark Johnston pool_init_objdir_dsl(zfs_opt_t *zfs, zfs_zap_t *objdir)
418240afd8cSMark Johnston {
419240afd8cSMark Johnston 	zap_add_uint64(objdir, DMU_POOL_ROOT_DATASET,
420240afd8cSMark Johnston 	    dsl_dir_id(zfs->rootdsldir));
421240afd8cSMark Johnston }
422240afd8cSMark Johnston 
423240afd8cSMark Johnston static void
424240afd8cSMark Johnston pool_init_objdir_poolprops(zfs_opt_t *zfs, zfs_zap_t *objdir)
425240afd8cSMark Johnston {
426240afd8cSMark Johnston 	dnode_phys_t *dnode;
427240afd8cSMark Johnston 	uint64_t id;
428240afd8cSMark Johnston 
429240afd8cSMark Johnston 	dnode = objset_dnode_alloc(zfs->mos, DMU_OT_POOL_PROPS, &id);
430240afd8cSMark Johnston 	zap_add_uint64(objdir, DMU_POOL_PROPS, id);
431240afd8cSMark Johnston 
432240afd8cSMark Johnston 	zfs->poolprops = zap_alloc(zfs->mos, dnode);
433240afd8cSMark Johnston }
434240afd8cSMark Johnston 
435240afd8cSMark Johnston /*
436240afd8cSMark Johnston  * Initialize the MOS object directory, the root of virtually all of the pool's
437240afd8cSMark Johnston  * data and metadata.
438240afd8cSMark Johnston  */
439240afd8cSMark Johnston static void
440240afd8cSMark Johnston pool_init_objdir(zfs_opt_t *zfs)
441240afd8cSMark Johnston {
442240afd8cSMark Johnston 	zfs_zap_t *zap;
443240afd8cSMark Johnston 	dnode_phys_t *objdir;
444240afd8cSMark Johnston 
445240afd8cSMark Johnston 	objdir = objset_dnode_lookup(zfs->mos, DMU_POOL_DIRECTORY_OBJECT);
446240afd8cSMark Johnston 
447240afd8cSMark Johnston 	zap = zap_alloc(zfs->mos, objdir);
448240afd8cSMark Johnston 	pool_init_objdir_config(zfs, zap);
449240afd8cSMark Johnston 	pool_init_objdir_bplists(zfs, zap);
450240afd8cSMark Johnston 	pool_init_objdir_feature_maps(zfs, zap);
451240afd8cSMark Johnston 	pool_init_objdir_dsl(zfs, zap);
452240afd8cSMark Johnston 	pool_init_objdir_poolprops(zfs, zap);
453240afd8cSMark Johnston 	zap_write(zfs, zap);
454240afd8cSMark Johnston }
455240afd8cSMark Johnston 
456240afd8cSMark Johnston /*
457240afd8cSMark Johnston  * Initialize the meta-object set (MOS) and immediately write out several
458240afd8cSMark Johnston  * special objects whose contents are already finalized, including the object
459240afd8cSMark Johnston  * directory.
460240afd8cSMark Johnston  *
461240afd8cSMark Johnston  * Once the MOS is finalized, it'll look roughly like this:
462240afd8cSMark Johnston  *
463240afd8cSMark Johnston  *	object directory (ZAP)
464240afd8cSMark Johnston  *	|-> vdev config object (nvlist)
465240afd8cSMark Johnston  *	|-> features for read
466240afd8cSMark Johnston  *	|-> features for write
467240afd8cSMark Johnston  *	|-> feature descriptions
468240afd8cSMark Johnston  *	|-> sync bplist
469240afd8cSMark Johnston  *	|-> free bplist
470240afd8cSMark Johnston  *	|-> pool properties
471240afd8cSMark Johnston  *	L-> root DSL directory
472240afd8cSMark Johnston  *	    |-> DSL child directory (ZAP)
473240afd8cSMark Johnston  *	    |   |-> $MOS (DSL dir)
474240afd8cSMark Johnston  *	    |   |   |-> child map
475240afd8cSMark Johnston  *	    |   |   L-> props (ZAP)
476240afd8cSMark Johnston  *	    |   |-> $FREE (DSL dir)
477240afd8cSMark Johnston  *	    |   |   |-> child map
478240afd8cSMark Johnston  *	    |   |   L-> props (ZAP)
479240afd8cSMark Johnston  *	    |   |-> $ORIGIN (DSL dir)
480240afd8cSMark Johnston  *	    |   |   |-> child map
481240afd8cSMark Johnston  *	    |   |   |-> dataset
482240afd8cSMark Johnston  *	    |   |   |   L-> deadlist
483240afd8cSMark Johnston  *	    |   |   |-> snapshot
484240afd8cSMark Johnston  *	    |   |   |   |-> deadlist
485240afd8cSMark Johnston  *	    |   |   |   L-> snapshot names
486240afd8cSMark Johnston  *	    |   |   |-> props (ZAP)
487240afd8cSMark Johnston  *	    |   |   L-> clones (ZAP)
488240afd8cSMark Johnston  *	    |   |-> dataset 1 (DSL dir)
489240afd8cSMark Johnston  *	    |   |   |-> DSL dataset
490240afd8cSMark Johnston  *	    |   |   |   |-> snapshot names
491240afd8cSMark Johnston  *	    |   |   |   L-> deadlist
492240afd8cSMark Johnston  *	    |   |   |-> child map
493240afd8cSMark Johnston  *	    |   |   |   L-> ...
494240afd8cSMark Johnston  *	    |   |   L-> props
495240afd8cSMark Johnston  *	    |   |-> dataset 2
496240afd8cSMark Johnston  *	    |   |   L-> ...
497240afd8cSMark Johnston  *	    |   |-> ...
498240afd8cSMark Johnston  *	    |   L-> dataset n
499240afd8cSMark Johnston  *	    |-> DSL root dataset
500240afd8cSMark Johnston  *	    |   |-> snapshot names
501240afd8cSMark Johnston  *	    |   L-> deadlist
502240afd8cSMark Johnston  *	    L-> props (ZAP)
503240afd8cSMark Johnston  *	space map object array
504240afd8cSMark Johnston  *	|-> space map 1
505240afd8cSMark Johnston  *	|-> space map 2
506240afd8cSMark Johnston  *	|-> ...
507240afd8cSMark Johnston  *	L-> space map n (zfs->mscount)
508240afd8cSMark Johnston  *
509240afd8cSMark Johnston  * The space map object array is pointed to by the "msarray" property in the
510240afd8cSMark Johnston  * pool configuration.
511240afd8cSMark Johnston  */
512240afd8cSMark Johnston static void
513240afd8cSMark Johnston pool_init(zfs_opt_t *zfs)
514240afd8cSMark Johnston {
515240afd8cSMark Johnston 	uint64_t dnid;
516240afd8cSMark Johnston 
517240afd8cSMark Johnston 	zfs->poolguid = ((uint64_t)random() << 32) | random();
518240afd8cSMark Johnston 	zfs->vdevguid = ((uint64_t)random() << 32) | random();
519240afd8cSMark Johnston 
520240afd8cSMark Johnston 	zfs->mos = objset_alloc(zfs, DMU_OST_META);
521240afd8cSMark Johnston 
522240afd8cSMark Johnston 	(void)objset_dnode_alloc(zfs->mos, DMU_OT_OBJECT_DIRECTORY, &dnid);
523240afd8cSMark Johnston 	assert(dnid == DMU_POOL_DIRECTORY_OBJECT);
524240afd8cSMark Johnston 
525240afd8cSMark Johnston 	(void)objset_dnode_alloc(zfs->mos, DMU_OT_OBJECT_ARRAY, &zfs->objarrid);
526240afd8cSMark Johnston 
527240afd8cSMark Johnston 	dsl_init(zfs);
528240afd8cSMark Johnston 
529240afd8cSMark Johnston 	pool_init_objdir(zfs);
530240afd8cSMark Johnston }
531240afd8cSMark Johnston 
532240afd8cSMark Johnston static void
533240afd8cSMark Johnston pool_labels_write(zfs_opt_t *zfs)
534240afd8cSMark Johnston {
535240afd8cSMark Johnston 	uberblock_t *ub;
536240afd8cSMark Johnston 	vdev_label_t *label;
537240afd8cSMark Johnston 	nvlist_t *poolconfig, *vdevconfig;
538240afd8cSMark Johnston 	int error;
539240afd8cSMark Johnston 
540240afd8cSMark Johnston 	label = ecalloc(1, sizeof(*label));
541240afd8cSMark Johnston 
542240afd8cSMark Johnston 	/*
543240afd8cSMark Johnston 	 * Assemble the vdev configuration and store it in the label.
544240afd8cSMark Johnston 	 */
545240afd8cSMark Johnston 	poolconfig = pool_config_nvcreate(zfs);
546240afd8cSMark Johnston 	vdevconfig = pool_disk_vdev_config_nvcreate(zfs);
547240afd8cSMark Johnston 	nvlist_add_nvlist(poolconfig, ZPOOL_CONFIG_VDEV_TREE, vdevconfig);
548240afd8cSMark Johnston 	nvlist_destroy(vdevconfig);
549240afd8cSMark Johnston 
550240afd8cSMark Johnston 	error = nvlist_export(poolconfig);
551240afd8cSMark Johnston 	if (error != 0)
552240afd8cSMark Johnston 		errc(1, error, "nvlist_export");
553240afd8cSMark Johnston 	nvlist_copy(poolconfig, label->vl_vdev_phys.vp_nvlist,
554240afd8cSMark Johnston 	    sizeof(label->vl_vdev_phys.vp_nvlist));
555240afd8cSMark Johnston 	nvlist_destroy(poolconfig);
556240afd8cSMark Johnston 
557240afd8cSMark Johnston 	/*
558240afd8cSMark Johnston 	 * Fill out the uberblock.  Just make each one the same.  The embedded
559240afd8cSMark Johnston 	 * checksum is calculated in vdev_label_write().
560240afd8cSMark Johnston 	 */
561240afd8cSMark Johnston 	for (size_t uoff = 0; uoff < sizeof(label->vl_uberblock);
562240afd8cSMark Johnston 	    uoff += (1 << zfs->ashift)) {
563240afd8cSMark Johnston 		ub = (uberblock_t *)(&label->vl_uberblock[0] + uoff);
564240afd8cSMark Johnston 		ub->ub_magic = UBERBLOCK_MAGIC;
565240afd8cSMark Johnston 		ub->ub_version = SPA_VERSION;
566240afd8cSMark Johnston 		ub->ub_txg = TXG;
567240afd8cSMark Johnston 		ub->ub_guid_sum = zfs->poolguid + zfs->vdevguid;
568240afd8cSMark Johnston 		ub->ub_timestamp = 0;
569240afd8cSMark Johnston 
570240afd8cSMark Johnston 		ub->ub_software_version = SPA_VERSION;
571240afd8cSMark Johnston 		ub->ub_mmp_magic = MMP_MAGIC;
572240afd8cSMark Johnston 		ub->ub_mmp_delay = 0;
573240afd8cSMark Johnston 		ub->ub_mmp_config = 0;
574240afd8cSMark Johnston 		ub->ub_checkpoint_txg = 0;
575240afd8cSMark Johnston 		objset_root_blkptr_copy(zfs->mos, &ub->ub_rootbp);
576240afd8cSMark Johnston 	}
577240afd8cSMark Johnston 
578240afd8cSMark Johnston 	/*
579240afd8cSMark Johnston 	 * Write out four copies of the label: two at the beginning of the vdev
580240afd8cSMark Johnston 	 * and two at the end.
581240afd8cSMark Johnston 	 */
582240afd8cSMark Johnston 	for (int i = 0; i < VDEV_LABELS; i++)
583240afd8cSMark Johnston 		vdev_label_write(zfs, i, label);
584240afd8cSMark Johnston 
585240afd8cSMark Johnston 	free(label);
586240afd8cSMark Johnston }
587240afd8cSMark Johnston 
588240afd8cSMark Johnston static void
589240afd8cSMark Johnston pool_fini(zfs_opt_t *zfs)
590240afd8cSMark Johnston {
591240afd8cSMark Johnston 	zap_write(zfs, zfs->poolprops);
592240afd8cSMark Johnston 	dsl_write(zfs);
593240afd8cSMark Johnston 	objset_write(zfs, zfs->mos);
594240afd8cSMark Johnston 	pool_labels_write(zfs);
595240afd8cSMark Johnston }
596240afd8cSMark Johnston 
597240afd8cSMark Johnston struct dnode_cursor *
598240afd8cSMark Johnston dnode_cursor_init(zfs_opt_t *zfs, zfs_objset_t *os, dnode_phys_t *dnode,
599240afd8cSMark Johnston     off_t size, off_t blksz)
600240afd8cSMark Johnston {
601240afd8cSMark Johnston 	struct dnode_cursor *c;
602240afd8cSMark Johnston 	uint64_t nbppindir, indlevel, ndatablks, nindblks;
603240afd8cSMark Johnston 
604240afd8cSMark Johnston 	assert(dnode->dn_nblkptr == 1);
605240afd8cSMark Johnston 	assert(blksz <= MAXBLOCKSIZE);
606240afd8cSMark Johnston 
607240afd8cSMark Johnston 	if (blksz == 0) {
608240afd8cSMark Johnston 		/* Must be between 1<<ashift and 128KB. */
609240afd8cSMark Johnston 		blksz = MIN(MAXBLOCKSIZE, MAX(1 << zfs->ashift,
610240afd8cSMark Johnston 		    powerof2(size) ? size : (1ul << flsll(size))));
611240afd8cSMark Johnston 	}
612240afd8cSMark Johnston 	assert(powerof2(blksz));
613240afd8cSMark Johnston 
614240afd8cSMark Johnston 	/*
615240afd8cSMark Johnston 	 * Do we need indirect blocks?  Figure out how many levels are needed
616240afd8cSMark Johnston 	 * (indlevel == 1 means no indirect blocks) and how much space is needed
617240afd8cSMark Johnston 	 * (it has to be allocated up-front to break the dependency cycle
618240afd8cSMark Johnston 	 * described in objset_write()).
619240afd8cSMark Johnston 	 */
620240afd8cSMark Johnston 	ndatablks = size == 0 ? 0 : howmany(size, blksz);
621240afd8cSMark Johnston 	nindblks = 0;
622240afd8cSMark Johnston 	for (indlevel = 1, nbppindir = 1; ndatablks > nbppindir; indlevel++) {
623240afd8cSMark Johnston 		nbppindir *= BLKPTR_PER_INDIR;
624240afd8cSMark Johnston 		nindblks += howmany(ndatablks, indlevel * nbppindir);
625240afd8cSMark Johnston 	}
626240afd8cSMark Johnston 	assert(indlevel < INDIR_LEVELS);
627240afd8cSMark Johnston 
628240afd8cSMark Johnston 	dnode->dn_nlevels = (uint8_t)indlevel;
629240afd8cSMark Johnston 	dnode->dn_maxblkid = ndatablks > 0 ? ndatablks - 1 : 0;
630240afd8cSMark Johnston 	dnode->dn_datablkszsec = blksz >> MINBLOCKSHIFT;
631240afd8cSMark Johnston 
632240afd8cSMark Johnston 	c = ecalloc(1, sizeof(*c));
633240afd8cSMark Johnston 	if (nindblks > 0) {
634240afd8cSMark Johnston 		c->indspace = nindblks * MAXBLOCKSIZE;
635240afd8cSMark Johnston 		c->indloc = objset_space_alloc(zfs, os, &c->indspace);
636240afd8cSMark Johnston 	}
637240afd8cSMark Johnston 	c->dnode = dnode;
638240afd8cSMark Johnston 	c->dataoff = 0;
639240afd8cSMark Johnston 	c->datablksz = blksz;
640240afd8cSMark Johnston 
641240afd8cSMark Johnston 	return (c);
642240afd8cSMark Johnston }
643240afd8cSMark Johnston 
644240afd8cSMark Johnston static void
645240afd8cSMark Johnston _dnode_cursor_flush(zfs_opt_t *zfs, struct dnode_cursor *c, int levels)
646240afd8cSMark Johnston {
647240afd8cSMark Johnston 	blkptr_t *bp, *pbp;
648240afd8cSMark Johnston 	void *buf;
649240afd8cSMark Johnston 	uint64_t fill;
650240afd8cSMark Johnston 	off_t blkid, blksz, loc;
651240afd8cSMark Johnston 
652240afd8cSMark Johnston 	assert(levels > 0);
653240afd8cSMark Johnston 	assert(levels <= c->dnode->dn_nlevels - 1);
654240afd8cSMark Johnston 
655240afd8cSMark Johnston 	blksz = MAXBLOCKSIZE;
656240afd8cSMark Johnston 	blkid = (c->dataoff / c->datablksz) / BLKPTR_PER_INDIR;
657240afd8cSMark Johnston 	for (int level = 1; level <= levels; level++) {
658240afd8cSMark Johnston 		buf = c->inddir[level - 1];
659240afd8cSMark Johnston 
660240afd8cSMark Johnston 		if (level == c->dnode->dn_nlevels - 1) {
661240afd8cSMark Johnston 			pbp = &c->dnode->dn_blkptr[0];
662240afd8cSMark Johnston 		} else {
663240afd8cSMark Johnston 			uint64_t iblkid;
664240afd8cSMark Johnston 
665240afd8cSMark Johnston 			iblkid = blkid & (BLKPTR_PER_INDIR - 1);
666240afd8cSMark Johnston 			pbp = (blkptr_t *)
667240afd8cSMark Johnston 			    &c->inddir[level][iblkid * sizeof(blkptr_t)];
668240afd8cSMark Johnston 		}
669240afd8cSMark Johnston 
670240afd8cSMark Johnston 		/*
671240afd8cSMark Johnston 		 * Space for indirect blocks is allocated up-front; see the
672240afd8cSMark Johnston 		 * comment in objset_write().
673240afd8cSMark Johnston 		 */
674240afd8cSMark Johnston 		loc = c->indloc;
675240afd8cSMark Johnston 		c->indloc += blksz;
676240afd8cSMark Johnston 		assert(c->indspace >= blksz);
677240afd8cSMark Johnston 		c->indspace -= blksz;
678240afd8cSMark Johnston 
679240afd8cSMark Johnston 		bp = buf;
680240afd8cSMark Johnston 		fill = 0;
681240afd8cSMark Johnston 		for (size_t i = 0; i < BLKPTR_PER_INDIR; i++)
682240afd8cSMark Johnston 			fill += BP_GET_FILL(&bp[i]);
683240afd8cSMark Johnston 
684240afd8cSMark Johnston 		vdev_pwrite_dnode_indir(zfs, c->dnode, level, fill, buf, blksz,
685240afd8cSMark Johnston 		    loc, pbp);
686240afd8cSMark Johnston 		memset(buf, 0, MAXBLOCKSIZE);
687240afd8cSMark Johnston 
688240afd8cSMark Johnston 		blkid /= BLKPTR_PER_INDIR;
689240afd8cSMark Johnston 	}
690240afd8cSMark Johnston }
691240afd8cSMark Johnston 
692240afd8cSMark Johnston blkptr_t *
693240afd8cSMark Johnston dnode_cursor_next(zfs_opt_t *zfs, struct dnode_cursor *c, off_t off)
694240afd8cSMark Johnston {
695240afd8cSMark Johnston 	off_t blkid, l1id;
696240afd8cSMark Johnston 	int levels;
697240afd8cSMark Johnston 
698240afd8cSMark Johnston 	if (c->dnode->dn_nlevels == 1) {
699240afd8cSMark Johnston 		assert(off < MAXBLOCKSIZE);
700240afd8cSMark Johnston 		return (&c->dnode->dn_blkptr[0]);
701240afd8cSMark Johnston 	}
702240afd8cSMark Johnston 
703240afd8cSMark Johnston 	assert(off % c->datablksz == 0);
704240afd8cSMark Johnston 
705240afd8cSMark Johnston 	/* Do we need to flush any full indirect blocks? */
706240afd8cSMark Johnston 	if (off > 0) {
707240afd8cSMark Johnston 		blkid = off / c->datablksz;
708240afd8cSMark Johnston 		for (levels = 0; levels < c->dnode->dn_nlevels - 1; levels++) {
709240afd8cSMark Johnston 			if (blkid % BLKPTR_PER_INDIR != 0)
710240afd8cSMark Johnston 				break;
711240afd8cSMark Johnston 			blkid /= BLKPTR_PER_INDIR;
712240afd8cSMark Johnston 		}
713240afd8cSMark Johnston 		if (levels > 0)
714240afd8cSMark Johnston 			_dnode_cursor_flush(zfs, c, levels);
715240afd8cSMark Johnston 	}
716240afd8cSMark Johnston 
717240afd8cSMark Johnston 	c->dataoff = off;
718240afd8cSMark Johnston 	l1id = (off / c->datablksz) & (BLKPTR_PER_INDIR - 1);
719240afd8cSMark Johnston 	return ((blkptr_t *)&c->inddir[0][l1id * sizeof(blkptr_t)]);
720240afd8cSMark Johnston }
721240afd8cSMark Johnston 
722240afd8cSMark Johnston void
723240afd8cSMark Johnston dnode_cursor_finish(zfs_opt_t *zfs, struct dnode_cursor *c)
724240afd8cSMark Johnston {
725240afd8cSMark Johnston 	int levels;
726240afd8cSMark Johnston 
727240afd8cSMark Johnston 	levels = c->dnode->dn_nlevels - 1;
728240afd8cSMark Johnston 	if (levels > 0)
729240afd8cSMark Johnston 		_dnode_cursor_flush(zfs, c, levels);
730240afd8cSMark Johnston 	assert(c->indspace == 0);
731240afd8cSMark Johnston 	free(c);
732240afd8cSMark Johnston }
733240afd8cSMark Johnston 
734240afd8cSMark Johnston void
735240afd8cSMark Johnston zfs_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts)
736240afd8cSMark Johnston {
737240afd8cSMark Johnston 	zfs_opt_t *zfs;
738240afd8cSMark Johnston 	int dirfd;
739240afd8cSMark Johnston 
740240afd8cSMark Johnston 	zfs = fsopts->fs_specific;
741240afd8cSMark Johnston 
742240afd8cSMark Johnston 	/*
743240afd8cSMark Johnston 	 * Use a fixed seed to provide reproducible pseudo-random numbers for
744240afd8cSMark Johnston 	 * on-disk structures when needed (e.g., GUIDs, ZAP hash salts).
745240afd8cSMark Johnston 	 */
746240afd8cSMark Johnston 	srandom(1729);
747240afd8cSMark Johnston 
748240afd8cSMark Johnston 	zfs_check_opts(fsopts);
749240afd8cSMark Johnston 
750240afd8cSMark Johnston 	if (!zfs->nowarn) {
751240afd8cSMark Johnston 		fprintf(stderr,
752240afd8cSMark Johnston 		    "ZFS support is currently considered experimental. "
753240afd8cSMark Johnston 		    "Do not use it for anything critical.\n");
754240afd8cSMark Johnston 	}
755240afd8cSMark Johnston 
756240afd8cSMark Johnston 	dirfd = open(dir, O_DIRECTORY | O_RDONLY);
757240afd8cSMark Johnston 	if (dirfd < 0)
758240afd8cSMark Johnston 		err(1, "open(%s)", dir);
759240afd8cSMark Johnston 
760240afd8cSMark Johnston 	vdev_init(zfs, image);
761240afd8cSMark Johnston 	pool_init(zfs);
762240afd8cSMark Johnston 	fs_build(zfs, dirfd, root);
763240afd8cSMark Johnston 	pool_fini(zfs);
764240afd8cSMark Johnston 	vdev_fini(zfs);
765240afd8cSMark Johnston }
766