1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2022 The FreeBSD Foundation
5 *
6 * This software was developed by Mark Johnston under sponsorship from
7 * the FreeBSD Foundation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are
11 * met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <sys/param.h>
32 #include <sys/errno.h>
33 #include <sys/queue.h>
34
35 #include <assert.h>
36 #include <ctype.h>
37 #include <fcntl.h>
38 #include <stdalign.h>
39 #include <stdbool.h>
40 #include <stddef.h>
41 #include <stdint.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45
46 #include <util.h>
47
48 #include "makefs.h"
49 #include "zfs.h"
50
51 #define VDEV_LABEL_SPACE \
52 ((off_t)(VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE))
53 _Static_assert(VDEV_LABEL_SPACE <= MINDEVSIZE, "");
54
55 #define MINMSSIZE ((off_t)1 << 24) /* 16MB */
56 #define DFLTMSSIZE ((off_t)1 << 29) /* 512MB */
57 #define MAXMSSIZE ((off_t)1 << 34) /* 16GB */
58
59 #define INDIR_LEVELS 6
60 /* Indirect blocks are always 128KB. */
61 #define BLKPTR_PER_INDIR (MAXBLOCKSIZE / sizeof(blkptr_t))
62
63 struct dnode_cursor {
64 char inddir[INDIR_LEVELS][MAXBLOCKSIZE];
65 off_t indloc;
66 off_t indspace;
67 dnode_phys_t *dnode;
68 off_t dataoff;
69 off_t datablksz;
70 };
71
72 void
zfs_prep_opts(fsinfo_t * fsopts)73 zfs_prep_opts(fsinfo_t *fsopts)
74 {
75 zfs_opt_t *zfs;
76 size_t align;
77
78 align = alignof(uint64_t);
79 zfs = aligned_alloc(align, roundup2(sizeof(*zfs), align));
80 if (zfs == NULL)
81 err(1, "aligned_alloc");
82 memset(zfs, 0, sizeof(*zfs));
83
84 const option_t zfs_options[] = {
85 { '\0', "bootfs", &zfs->bootfs, OPT_STRPTR,
86 0, 0, "Bootable dataset" },
87 { '\0', "mssize", &zfs->mssize, OPT_INT64,
88 MINMSSIZE, MAXMSSIZE, "Metaslab size" },
89 { '\0', "path", &zfs->vdevpath, OPT_STRPTR,
90 0, 0, "The path to the device for this vdev" },
91 { '\0', "poolguid", &zfs->poolguid, OPT_INT64,
92 0, INT64_MAX, "ZFS pool GUID" },
93 { '\0', "poolname", &zfs->poolname, OPT_STRPTR,
94 0, 0, "ZFS pool name" },
95 { '\0', "rootpath", &zfs->rootpath, OPT_STRPTR,
96 0, 0, "Prefix for all dataset mount points" },
97 { '\0', "ashift", &zfs->ashift, OPT_INT32,
98 MINBLOCKSHIFT, MAXBLOCKSHIFT, "ZFS pool ashift" },
99 { '\0', "verify-txgs", &zfs->verify_txgs, OPT_BOOL,
100 0, 0, "Make OpenZFS verify data upon import" },
101 { '\0', "nowarn", &zfs->nowarn, OPT_BOOL,
102 0, 0, "Provided for backwards compatibility, ignored" },
103 { .name = NULL }
104 };
105
106 STAILQ_INIT(&zfs->datasetdescs);
107
108 fsopts->fs_specific = zfs;
109 fsopts->fs_options = copy_opts(zfs_options);
110 }
111
112 int
zfs_parse_opts(const char * option,fsinfo_t * fsopts)113 zfs_parse_opts(const char *option, fsinfo_t *fsopts)
114 {
115 zfs_opt_t *zfs;
116 struct dataset_desc *dsdesc;
117 char buf[BUFSIZ], *opt, *val;
118 int rv;
119
120 zfs = fsopts->fs_specific;
121
122 opt = val = estrdup(option);
123 opt = strsep(&val, "=");
124 if (strcmp(opt, "fs") == 0) {
125 if (val == NULL)
126 errx(1, "invalid filesystem parameters `%s'", option);
127
128 /*
129 * Dataset descriptions will be parsed later, in dsl_init().
130 * Just stash them away for now.
131 */
132 dsdesc = ecalloc(1, sizeof(*dsdesc));
133 dsdesc->params = estrdup(val);
134 free(opt);
135 STAILQ_INSERT_TAIL(&zfs->datasetdescs, dsdesc, next);
136 return (1);
137 }
138 free(opt);
139
140 rv = set_option(fsopts->fs_options, option, buf, sizeof(buf));
141 return (rv == -1 ? 0 : 1);
142 }
143
144 static void
zfs_size_vdev(fsinfo_t * fsopts)145 zfs_size_vdev(fsinfo_t *fsopts)
146 {
147 zfs_opt_t *zfs;
148 off_t asize, mssize, vdevsize, vdevsize1;
149
150 zfs = fsopts->fs_specific;
151
152 assert(fsopts->maxsize != 0);
153 assert(zfs->ashift != 0);
154
155 /*
156 * Figure out how big the vdev should be.
157 */
158 vdevsize = rounddown2(fsopts->maxsize, 1 << zfs->ashift);
159 if (vdevsize < MINDEVSIZE)
160 errx(1, "maximum image size is too small");
161 if (vdevsize < fsopts->minsize || vdevsize > fsopts->maxsize) {
162 errx(1, "image size bounds must be multiples of %d",
163 1 << zfs->ashift);
164 }
165 asize = vdevsize - VDEV_LABEL_SPACE;
166
167 /*
168 * Size metaslabs according to the following heuristic:
169 * - provide at least 8 metaslabs,
170 * - without using a metaslab size larger than 512MB.
171 * This approximates what OpenZFS does without being complicated. In
172 * practice we expect pools to be expanded upon first use, and OpenZFS
173 * does not resize metaslabs in that case, so there is no right answer
174 * here. In general we want to provide large metaslabs even if the
175 * image size is small, and 512MB is a reasonable size for pools up to
176 * several hundred gigabytes.
177 *
178 * The user may override this heuristic using the "-o mssize" option.
179 */
180 mssize = zfs->mssize;
181 if (mssize == 0) {
182 mssize = MAX(MIN(asize / 8, DFLTMSSIZE), MINMSSIZE);
183 if (!powerof2(mssize))
184 mssize = 1l << (flsll(mssize) - 1);
185 }
186 if (!powerof2(mssize))
187 errx(1, "metaslab size must be a power of 2");
188
189 /*
190 * If we have some slop left over, try to cover it by resizing the vdev,
191 * subject to the maxsize and minsize parameters.
192 */
193 if (asize % mssize != 0) {
194 vdevsize1 = rounddown2(asize, mssize) + VDEV_LABEL_SPACE;
195 if (vdevsize1 < fsopts->minsize)
196 vdevsize1 = roundup2(asize, mssize) + VDEV_LABEL_SPACE;
197 if (vdevsize1 <= fsopts->maxsize)
198 vdevsize = vdevsize1;
199 }
200 asize = vdevsize - VDEV_LABEL_SPACE;
201
202 zfs->asize = asize;
203 zfs->vdevsize = vdevsize;
204 zfs->mssize = mssize;
205 zfs->msshift = flsll(mssize) - 1;
206 zfs->mscount = asize / mssize;
207 }
208
209 /*
210 * Validate options and set some default values.
211 */
212 static void
zfs_check_opts(fsinfo_t * fsopts)213 zfs_check_opts(fsinfo_t *fsopts)
214 {
215 zfs_opt_t *zfs;
216
217 zfs = fsopts->fs_specific;
218
219 if (fsopts->offset != 0)
220 errx(1, "unhandled offset option");
221 if (fsopts->maxsize == 0)
222 errx(1, "an image size must be specified");
223
224 if (zfs->poolname == NULL)
225 errx(1, "a pool name must be specified");
226 if (!isalpha(zfs->poolname[0]))
227 errx(1, "the pool name must begin with a letter");
228 for (size_t i = 0, len = strlen(zfs->poolname); i < len; i++) {
229 if (!isalnum(zfs->poolname[i]) && zfs->poolname[i] != '_')
230 errx(1, "invalid character '%c' in pool name",
231 zfs->poolname[i]);
232 }
233 if (strcmp(zfs->poolname, "mirror") == 0 ||
234 strcmp(zfs->poolname, "raidz") == 0 ||
235 strcmp(zfs->poolname, "draid") == 0) {
236 errx(1, "pool name '%s' is reserved and cannot be used",
237 zfs->poolname);
238 }
239
240 if (zfs->rootpath == NULL)
241 easprintf(&zfs->rootpath, "/%s", zfs->poolname);
242 if (zfs->rootpath[0] != '/')
243 errx(1, "mountpoint `%s' must be absolute", zfs->rootpath);
244
245 if (zfs->vdevpath == NULL)
246 easprintf(&zfs->vdevpath, "/dev/null");
247 if (zfs->vdevpath[0] != '/')
248 errx(1, "path `%s' must be absolute", zfs->vdevpath);
249
250 if (zfs->ashift == 0)
251 zfs->ashift = 12;
252
253 zfs_size_vdev(fsopts);
254 }
255
256 void
zfs_cleanup_opts(fsinfo_t * fsopts)257 zfs_cleanup_opts(fsinfo_t *fsopts)
258 {
259 struct dataset_desc *d, *tmp;
260 zfs_opt_t *zfs;
261
262 zfs = fsopts->fs_specific;
263 free(zfs->rootpath);
264 free(zfs->vdevpath);
265 free(zfs->bootfs);
266 free(__DECONST(void *, zfs->poolname));
267 STAILQ_FOREACH_SAFE(d, &zfs->datasetdescs, next, tmp) {
268 free(d->params);
269 free(d);
270 }
271 free(zfs);
272 free(fsopts->fs_options);
273 }
274
275 static size_t
nvlist_size(const nvlist_t * nvl)276 nvlist_size(const nvlist_t *nvl)
277 {
278 return (sizeof(nvl->nv_header) + nvl->nv_size);
279 }
280
281 static void
nvlist_copy(const nvlist_t * nvl,char * buf,size_t sz)282 nvlist_copy(const nvlist_t *nvl, char *buf, size_t sz)
283 {
284 assert(sz >= nvlist_size(nvl));
285
286 memcpy(buf, &nvl->nv_header, sizeof(nvl->nv_header));
287 memcpy(buf + sizeof(nvl->nv_header), nvl->nv_data, nvl->nv_size);
288 }
289
290 /*
291 * Avoid returning a GUID of 0, just to avoid the possibility that something
292 * will interpret that as meaning that the GUID is uninitialized.
293 */
294 uint64_t
randomguid(void)295 randomguid(void)
296 {
297 uint64_t ret;
298
299 do {
300 ret = ((uint64_t)random() << 32) | random();
301 } while (ret == 0);
302
303 return (ret);
304 }
305
306 static nvlist_t *
pool_config_nvcreate(zfs_opt_t * zfs)307 pool_config_nvcreate(zfs_opt_t *zfs)
308 {
309 nvlist_t *featuresnv, *poolnv;
310
311 poolnv = nvlist_create(NV_UNIQUE_NAME);
312 nvlist_add_uint64(poolnv, ZPOOL_CONFIG_POOL_TXG, TXG);
313 nvlist_add_uint64(poolnv, ZPOOL_CONFIG_VERSION, SPA_VERSION);
314 nvlist_add_uint64(poolnv, ZPOOL_CONFIG_POOL_STATE, POOL_STATE_EXPORTED);
315 nvlist_add_string(poolnv, ZPOOL_CONFIG_POOL_NAME, zfs->poolname);
316 nvlist_add_uint64(poolnv, ZPOOL_CONFIG_POOL_GUID, zfs->poolguid);
317 nvlist_add_uint64(poolnv, ZPOOL_CONFIG_TOP_GUID, zfs->vdevguid);
318 nvlist_add_uint64(poolnv, ZPOOL_CONFIG_GUID, zfs->vdevguid);
319 nvlist_add_uint64(poolnv, ZPOOL_CONFIG_VDEV_CHILDREN, 1);
320
321 featuresnv = nvlist_create(NV_UNIQUE_NAME);
322 nvlist_add_nvlist(poolnv, ZPOOL_CONFIG_FEATURES_FOR_READ, featuresnv);
323 nvlist_destroy(featuresnv);
324
325 return (poolnv);
326 }
327
328 static nvlist_t *
pool_disk_vdev_config_nvcreate(zfs_opt_t * zfs)329 pool_disk_vdev_config_nvcreate(zfs_opt_t *zfs)
330 {
331 nvlist_t *diskvdevnv;
332
333 assert(zfs->objarrid != 0);
334
335 diskvdevnv = nvlist_create(NV_UNIQUE_NAME);
336 nvlist_add_string(diskvdevnv, ZPOOL_CONFIG_TYPE, VDEV_TYPE_DISK);
337 nvlist_add_uint64(diskvdevnv, ZPOOL_CONFIG_ASHIFT, zfs->ashift);
338 nvlist_add_uint64(diskvdevnv, ZPOOL_CONFIG_ASIZE, zfs->asize);
339 nvlist_add_uint64(diskvdevnv, ZPOOL_CONFIG_GUID, zfs->vdevguid);
340 nvlist_add_uint64(diskvdevnv, ZPOOL_CONFIG_ID, 0);
341 nvlist_add_string(diskvdevnv, ZPOOL_CONFIG_PATH, zfs->vdevpath);
342 nvlist_add_uint64(diskvdevnv, ZPOOL_CONFIG_WHOLE_DISK, 1);
343 nvlist_add_uint64(diskvdevnv, ZPOOL_CONFIG_CREATE_TXG, TXG);
344 nvlist_add_uint64(diskvdevnv, ZPOOL_CONFIG_METASLAB_ARRAY,
345 zfs->objarrid);
346 nvlist_add_uint64(diskvdevnv, ZPOOL_CONFIG_METASLAB_SHIFT,
347 zfs->msshift);
348
349 return (diskvdevnv);
350 }
351
352 static nvlist_t *
pool_root_vdev_config_nvcreate(zfs_opt_t * zfs)353 pool_root_vdev_config_nvcreate(zfs_opt_t *zfs)
354 {
355 nvlist_t *diskvdevnv, *rootvdevnv;
356
357 diskvdevnv = pool_disk_vdev_config_nvcreate(zfs);
358 rootvdevnv = nvlist_create(NV_UNIQUE_NAME);
359
360 nvlist_add_uint64(rootvdevnv, ZPOOL_CONFIG_ID, 0);
361 nvlist_add_uint64(rootvdevnv, ZPOOL_CONFIG_GUID, zfs->poolguid);
362 nvlist_add_string(rootvdevnv, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT);
363 nvlist_add_uint64(rootvdevnv, ZPOOL_CONFIG_CREATE_TXG, TXG);
364 nvlist_add_nvlist_array(rootvdevnv, ZPOOL_CONFIG_CHILDREN, &diskvdevnv,
365 1);
366 nvlist_destroy(diskvdevnv);
367
368 return (rootvdevnv);
369 }
370
371 /*
372 * Create the pool's "config" object, which contains an nvlist describing pool
373 * parameters and the vdev topology. It is similar but not identical to the
374 * nvlist stored in vdev labels. The main difference is that vdev labels do not
375 * describe the full vdev tree and in particular do not contain the "root"
376 * meta-vdev.
377 */
378 static void
pool_init_objdir_config(zfs_opt_t * zfs,zfs_zap_t * objdir)379 pool_init_objdir_config(zfs_opt_t *zfs, zfs_zap_t *objdir)
380 {
381 dnode_phys_t *dnode;
382 nvlist_t *poolconfig, *vdevconfig;
383 void *configbuf;
384 uint64_t dnid;
385 off_t configloc, configblksz;
386 int error;
387
388 dnode = objset_dnode_bonus_alloc(zfs->mos, DMU_OT_PACKED_NVLIST,
389 DMU_OT_PACKED_NVLIST_SIZE, sizeof(uint64_t), &dnid);
390
391 poolconfig = pool_config_nvcreate(zfs);
392
393 vdevconfig = pool_root_vdev_config_nvcreate(zfs);
394 nvlist_add_nvlist(poolconfig, ZPOOL_CONFIG_VDEV_TREE, vdevconfig);
395 nvlist_destroy(vdevconfig);
396
397 error = nvlist_export(poolconfig);
398 if (error != 0)
399 errc(1, error, "nvlist_export");
400
401 configblksz = nvlist_size(poolconfig);
402 configloc = objset_space_alloc(zfs, zfs->mos, &configblksz);
403 configbuf = ecalloc(1, configblksz);
404 nvlist_copy(poolconfig, configbuf, configblksz);
405
406 vdev_pwrite_dnode_data(zfs, dnode, configbuf, configblksz, configloc);
407
408 dnode->dn_datablkszsec = configblksz >> MINBLOCKSHIFT;
409 dnode->dn_flags = DNODE_FLAG_USED_BYTES;
410 *(uint64_t *)DN_BONUS(dnode) = nvlist_size(poolconfig);
411
412 zap_add_uint64(objdir, DMU_POOL_CONFIG, dnid);
413
414 nvlist_destroy(poolconfig);
415 free(configbuf);
416 }
417
418 /*
419 * Add objects block pointer list objects, used for deferred frees. We don't do
420 * anything with them, but they need to be present or OpenZFS will refuse to
421 * import the pool.
422 */
423 static void
pool_init_objdir_bplists(zfs_opt_t * zfs __unused,zfs_zap_t * objdir)424 pool_init_objdir_bplists(zfs_opt_t *zfs __unused, zfs_zap_t *objdir)
425 {
426 uint64_t dnid;
427
428 (void)objset_dnode_bonus_alloc(zfs->mos, DMU_OT_BPOBJ, DMU_OT_BPOBJ_HDR,
429 BPOBJ_SIZE_V2, &dnid);
430 zap_add_uint64(objdir, DMU_POOL_FREE_BPOBJ, dnid);
431
432 (void)objset_dnode_bonus_alloc(zfs->mos, DMU_OT_BPOBJ, DMU_OT_BPOBJ_HDR,
433 BPOBJ_SIZE_V2, &dnid);
434 zap_add_uint64(objdir, DMU_POOL_SYNC_BPLIST, dnid);
435 }
436
437 /*
438 * Add required feature metadata objects. We don't know anything about ZFS
439 * features, so the objects are just empty ZAPs.
440 */
441 static void
pool_init_objdir_feature_maps(zfs_opt_t * zfs,zfs_zap_t * objdir)442 pool_init_objdir_feature_maps(zfs_opt_t *zfs, zfs_zap_t *objdir)
443 {
444 dnode_phys_t *dnode;
445 uint64_t dnid;
446
447 dnode = objset_dnode_alloc(zfs->mos, DMU_OTN_ZAP_METADATA, &dnid);
448 zap_add_uint64(objdir, DMU_POOL_FEATURES_FOR_READ, dnid);
449 zap_write(zfs, zap_alloc(zfs->mos, dnode));
450
451 dnode = objset_dnode_alloc(zfs->mos, DMU_OTN_ZAP_METADATA, &dnid);
452 zap_add_uint64(objdir, DMU_POOL_FEATURES_FOR_WRITE, dnid);
453 zap_write(zfs, zap_alloc(zfs->mos, dnode));
454
455 dnode = objset_dnode_alloc(zfs->mos, DMU_OTN_ZAP_METADATA, &dnid);
456 zap_add_uint64(objdir, DMU_POOL_FEATURE_DESCRIPTIONS, dnid);
457 zap_write(zfs, zap_alloc(zfs->mos, dnode));
458 }
459
460 static void
pool_init_objdir_dsl(zfs_opt_t * zfs,zfs_zap_t * objdir)461 pool_init_objdir_dsl(zfs_opt_t *zfs, zfs_zap_t *objdir)
462 {
463 zap_add_uint64(objdir, DMU_POOL_ROOT_DATASET,
464 dsl_dir_id(zfs->rootdsldir));
465 }
466
467 static void
pool_init_objdir_poolprops(zfs_opt_t * zfs,zfs_zap_t * objdir)468 pool_init_objdir_poolprops(zfs_opt_t *zfs, zfs_zap_t *objdir)
469 {
470 dnode_phys_t *dnode;
471 uint64_t id;
472
473 dnode = objset_dnode_alloc(zfs->mos, DMU_OT_POOL_PROPS, &id);
474 zap_add_uint64(objdir, DMU_POOL_PROPS, id);
475
476 zfs->poolprops = zap_alloc(zfs->mos, dnode);
477 }
478
479 /*
480 * Initialize the MOS object directory, the root of virtually all of the pool's
481 * data and metadata.
482 */
483 static void
pool_init_objdir(zfs_opt_t * zfs)484 pool_init_objdir(zfs_opt_t *zfs)
485 {
486 zfs_zap_t *zap;
487 dnode_phys_t *objdir;
488
489 objdir = objset_dnode_lookup(zfs->mos, DMU_POOL_DIRECTORY_OBJECT);
490
491 zap = zap_alloc(zfs->mos, objdir);
492 pool_init_objdir_config(zfs, zap);
493 pool_init_objdir_bplists(zfs, zap);
494 pool_init_objdir_feature_maps(zfs, zap);
495 pool_init_objdir_dsl(zfs, zap);
496 pool_init_objdir_poolprops(zfs, zap);
497 zap_write(zfs, zap);
498 }
499
500 /*
501 * Initialize the meta-object set (MOS) and immediately write out several
502 * special objects whose contents are already finalized, including the object
503 * directory.
504 *
505 * Once the MOS is finalized, it'll look roughly like this:
506 *
507 * object directory (ZAP)
508 * |-> vdev config object (nvlist)
509 * |-> features for read
510 * |-> features for write
511 * |-> feature descriptions
512 * |-> sync bplist
513 * |-> free bplist
514 * |-> pool properties
515 * L-> root DSL directory
516 * |-> DSL child directory (ZAP)
517 * | |-> $MOS (DSL dir)
518 * | | |-> child map
519 * | | L-> props (ZAP)
520 * | |-> $FREE (DSL dir)
521 * | | |-> child map
522 * | | L-> props (ZAP)
523 * | |-> $ORIGIN (DSL dir)
524 * | | |-> child map
525 * | | |-> dataset
526 * | | | L-> deadlist
527 * | | |-> snapshot
528 * | | | |-> deadlist
529 * | | | L-> snapshot names
530 * | | |-> props (ZAP)
531 * | | L-> clones (ZAP)
532 * | |-> dataset 1 (DSL dir)
533 * | | |-> DSL dataset
534 * | | | |-> snapshot names
535 * | | | L-> deadlist
536 * | | |-> child map
537 * | | | L-> ...
538 * | | L-> props
539 * | |-> dataset 2
540 * | | L-> ...
541 * | |-> ...
542 * | L-> dataset n
543 * |-> DSL root dataset
544 * | |-> snapshot names
545 * | L-> deadlist
546 * L-> props (ZAP)
547 * space map object array
548 * |-> space map 1
549 * |-> space map 2
550 * |-> ...
551 * L-> space map n (zfs->mscount)
552 *
553 * The space map object array is pointed to by the "msarray" property in the
554 * pool configuration.
555 */
556 static void
pool_init(zfs_opt_t * zfs)557 pool_init(zfs_opt_t *zfs)
558 {
559 uint64_t dnid;
560
561 if (zfs->poolguid == 0)
562 zfs->poolguid = randomguid();
563 zfs->vdevguid = randomguid();
564
565 zfs->mos = objset_alloc(zfs, DMU_OST_META);
566
567 (void)objset_dnode_alloc(zfs->mos, DMU_OT_OBJECT_DIRECTORY, &dnid);
568 assert(dnid == DMU_POOL_DIRECTORY_OBJECT);
569
570 (void)objset_dnode_alloc(zfs->mos, DMU_OT_OBJECT_ARRAY, &zfs->objarrid);
571
572 dsl_init(zfs);
573
574 pool_init_objdir(zfs);
575 }
576
577 static void
pool_labels_write(zfs_opt_t * zfs)578 pool_labels_write(zfs_opt_t *zfs)
579 {
580 uberblock_t *ub;
581 vdev_label_t *label;
582 nvlist_t *poolconfig, *vdevconfig;
583 int error;
584
585 label = ecalloc(1, sizeof(*label));
586
587 /*
588 * Assemble the vdev configuration and store it in the label.
589 */
590 poolconfig = pool_config_nvcreate(zfs);
591 vdevconfig = pool_disk_vdev_config_nvcreate(zfs);
592 nvlist_add_nvlist(poolconfig, ZPOOL_CONFIG_VDEV_TREE, vdevconfig);
593 nvlist_destroy(vdevconfig);
594
595 error = nvlist_export(poolconfig);
596 if (error != 0)
597 errc(1, error, "nvlist_export");
598 nvlist_copy(poolconfig, label->vl_vdev_phys.vp_nvlist,
599 sizeof(label->vl_vdev_phys.vp_nvlist));
600 nvlist_destroy(poolconfig);
601
602 /*
603 * Fill out the uberblock. Just make each one the same. The embedded
604 * checksum is calculated in vdev_label_write().
605 */
606 for (size_t uoff = 0; uoff < sizeof(label->vl_uberblock);
607 uoff += ASHIFT_UBERBLOCK_SIZE(zfs->ashift)) {
608 ub = (uberblock_t *)(&label->vl_uberblock[0] + uoff);
609 ub->ub_magic = UBERBLOCK_MAGIC;
610 ub->ub_version = SPA_VERSION;
611
612 /*
613 * Upon import, OpenZFS will perform metadata verification of
614 * the last TXG by default. If all data is written in the same
615 * TXG, it'll all get verified, which can be painfully slow in
616 * some cases, e.g., initial boot in a cloud environment with
617 * slow storage. So, fabricate additional TXGs to avoid this
618 * overhead, unless the user requests otherwise.
619 */
620 ub->ub_txg = TXG;
621 if (!zfs->verify_txgs)
622 ub->ub_txg += TXG_SIZE;
623 ub->ub_guid_sum = zfs->poolguid + zfs->vdevguid;
624 ub->ub_timestamp = 0;
625
626 ub->ub_software_version = SPA_VERSION;
627 ub->ub_mmp_magic = MMP_MAGIC;
628 ub->ub_mmp_delay = 0;
629 ub->ub_mmp_config = 0;
630 ub->ub_checkpoint_txg = 0;
631 objset_root_blkptr_copy(zfs->mos, &ub->ub_rootbp);
632 }
633
634 /*
635 * Write out four copies of the label: two at the beginning of the vdev
636 * and two at the end.
637 */
638 for (int i = 0; i < VDEV_LABELS; i++)
639 vdev_label_write(zfs, i, label);
640
641 free(label);
642 }
643
644 static void
pool_fini(zfs_opt_t * zfs)645 pool_fini(zfs_opt_t *zfs)
646 {
647 zap_write(zfs, zfs->poolprops);
648 dsl_write(zfs);
649 objset_write(zfs, zfs->mos);
650 pool_labels_write(zfs);
651 }
652
653 struct dnode_cursor *
dnode_cursor_init(zfs_opt_t * zfs,zfs_objset_t * os,dnode_phys_t * dnode,off_t size,off_t blksz)654 dnode_cursor_init(zfs_opt_t *zfs, zfs_objset_t *os, dnode_phys_t *dnode,
655 off_t size, off_t blksz)
656 {
657 struct dnode_cursor *c;
658 uint64_t nbppindir, indlevel, ndatablks, nindblks;
659
660 assert(dnode->dn_nblkptr == 1);
661 assert(blksz <= MAXBLOCKSIZE);
662
663 if (blksz == 0) {
664 /* Must be between 1<<ashift and 128KB. */
665 blksz = MIN(MAXBLOCKSIZE, MAX(1 << zfs->ashift,
666 powerof2(size) ? size : (1l << flsll(size))));
667 }
668 assert(powerof2(blksz));
669
670 /*
671 * Do we need indirect blocks? Figure out how many levels are needed
672 * (indlevel == 1 means no indirect blocks) and how much space is needed
673 * (it has to be allocated up-front to break the dependency cycle
674 * described in objset_write()).
675 */
676 ndatablks = size == 0 ? 0 : howmany(size, blksz);
677 nindblks = 0;
678 for (indlevel = 1, nbppindir = 1; ndatablks > nbppindir; indlevel++) {
679 nbppindir *= BLKPTR_PER_INDIR;
680 nindblks += howmany(ndatablks, indlevel * nbppindir);
681 }
682 assert(indlevel < INDIR_LEVELS);
683
684 dnode->dn_nlevels = (uint8_t)indlevel;
685 dnode->dn_maxblkid = ndatablks > 0 ? ndatablks - 1 : 0;
686 dnode->dn_datablkszsec = blksz >> MINBLOCKSHIFT;
687
688 c = ecalloc(1, sizeof(*c));
689 if (nindblks > 0) {
690 c->indspace = nindblks * MAXBLOCKSIZE;
691 c->indloc = objset_space_alloc(zfs, os, &c->indspace);
692 }
693 c->dnode = dnode;
694 c->dataoff = 0;
695 c->datablksz = blksz;
696
697 return (c);
698 }
699
700 static void
_dnode_cursor_flush(zfs_opt_t * zfs,struct dnode_cursor * c,unsigned int levels)701 _dnode_cursor_flush(zfs_opt_t *zfs, struct dnode_cursor *c, unsigned int levels)
702 {
703 blkptr_t *bp, *pbp;
704 void *buf;
705 uint64_t fill;
706 off_t blkid, blksz, loc;
707
708 assert(levels > 0);
709 assert(levels <= c->dnode->dn_nlevels - 1U);
710
711 blksz = MAXBLOCKSIZE;
712 blkid = (c->dataoff / c->datablksz) / BLKPTR_PER_INDIR;
713 for (unsigned int level = 1; level <= levels; level++) {
714 buf = c->inddir[level - 1];
715
716 if (level == c->dnode->dn_nlevels - 1U) {
717 pbp = &c->dnode->dn_blkptr[0];
718 } else {
719 uint64_t iblkid;
720
721 iblkid = blkid & (BLKPTR_PER_INDIR - 1);
722 pbp = (blkptr_t *)
723 &c->inddir[level][iblkid * sizeof(blkptr_t)];
724 }
725
726 /*
727 * Space for indirect blocks is allocated up-front; see the
728 * comment in objset_write().
729 */
730 loc = c->indloc;
731 c->indloc += blksz;
732 assert(c->indspace >= blksz);
733 c->indspace -= blksz;
734
735 bp = buf;
736 fill = 0;
737 for (size_t i = 0; i < BLKPTR_PER_INDIR; i++)
738 fill += BP_GET_FILL(&bp[i]);
739
740 vdev_pwrite_dnode_indir(zfs, c->dnode, level, fill, buf, blksz,
741 loc, pbp);
742 memset(buf, 0, MAXBLOCKSIZE);
743
744 blkid /= BLKPTR_PER_INDIR;
745 }
746 }
747
748 blkptr_t *
dnode_cursor_next(zfs_opt_t * zfs,struct dnode_cursor * c,off_t off)749 dnode_cursor_next(zfs_opt_t *zfs, struct dnode_cursor *c, off_t off)
750 {
751 off_t blkid, l1id;
752 unsigned int levels;
753
754 if (c->dnode->dn_nlevels == 1) {
755 assert(off < MAXBLOCKSIZE);
756 return (&c->dnode->dn_blkptr[0]);
757 }
758
759 assert(off % c->datablksz == 0);
760
761 /* Do we need to flush any full indirect blocks? */
762 if (off > 0) {
763 blkid = off / c->datablksz;
764 for (levels = 0; levels < c->dnode->dn_nlevels - 1U; levels++) {
765 if (blkid % BLKPTR_PER_INDIR != 0)
766 break;
767 blkid /= BLKPTR_PER_INDIR;
768 }
769 if (levels > 0)
770 _dnode_cursor_flush(zfs, c, levels);
771 }
772
773 c->dataoff = off;
774 l1id = (off / c->datablksz) & (BLKPTR_PER_INDIR - 1);
775 return ((blkptr_t *)&c->inddir[0][l1id * sizeof(blkptr_t)]);
776 }
777
778 void
dnode_cursor_finish(zfs_opt_t * zfs,struct dnode_cursor * c)779 dnode_cursor_finish(zfs_opt_t *zfs, struct dnode_cursor *c)
780 {
781 unsigned int levels;
782
783 assert(c->dnode->dn_nlevels > 0);
784 levels = c->dnode->dn_nlevels - 1;
785 if (levels > 0)
786 _dnode_cursor_flush(zfs, c, levels);
787 assert(c->indspace == 0);
788 free(c);
789 }
790
791 void
zfs_makefs(const char * image,const char * dir,fsnode * root,fsinfo_t * fsopts)792 zfs_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts)
793 {
794 zfs_opt_t *zfs;
795 int dirfd;
796
797 zfs = fsopts->fs_specific;
798
799 /*
800 * Use a fixed seed to provide reproducible pseudo-random numbers for
801 * on-disk structures when needed (e.g., GUIDs, ZAP hash salts).
802 */
803 srandom(1729);
804
805 zfs_check_opts(fsopts);
806
807 dirfd = open(dir, O_DIRECTORY | O_RDONLY);
808 if (dirfd < 0)
809 err(1, "open(%s)", dir);
810
811 vdev_init(zfs, image);
812 pool_init(zfs);
813 fs_build(zfs, dirfd, root);
814 pool_fini(zfs);
815 vdev_fini(zfs);
816 }
817