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