1 /*- 2 * Copyright (c) 2007 Doug Rabson 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 /* 28 * Stand-alone ZFS file reader. 29 */ 30 31 #include <stdbool.h> 32 #include <sys/endian.h> 33 #include <sys/stat.h> 34 #include <sys/stdint.h> 35 #include <sys/list.h> 36 #include <sys/zfs_bootenv.h> 37 #include <machine/_inttypes.h> 38 39 #include "zfsimpl.h" 40 #include "zfssubr.c" 41 42 #ifdef HAS_ZSTD_ZFS 43 extern int zstd_init(void); 44 #endif 45 46 struct zfsmount { 47 char *path; 48 const spa_t *spa; 49 objset_phys_t objset; 50 uint64_t rootobj; 51 uint64_t fsid_guid; /* mount's ds_fsid_guid */ 52 STAILQ_ENTRY(zfsmount) next; 53 }; 54 55 typedef STAILQ_HEAD(zfs_mnt_list, zfsmount) zfs_mnt_list_t; 56 static zfs_mnt_list_t zfsmount = STAILQ_HEAD_INITIALIZER(zfsmount); 57 58 /* 59 * The indirect_child_t represents the vdev that we will read from, when we 60 * need to read all copies of the data (e.g. for scrub or reconstruction). 61 * For plain (non-mirror) top-level vdevs (i.e. is_vdev is not a mirror), 62 * ic_vdev is the same as is_vdev. However, for mirror top-level vdevs, 63 * ic_vdev is a child of the mirror. 64 */ 65 typedef struct indirect_child { 66 void *ic_data; 67 vdev_t *ic_vdev; 68 } indirect_child_t; 69 70 /* 71 * The indirect_split_t represents one mapped segment of an i/o to the 72 * indirect vdev. For non-split (contiguously-mapped) blocks, there will be 73 * only one indirect_split_t, with is_split_offset==0 and is_size==io_size. 74 * For split blocks, there will be several of these. 75 */ 76 typedef struct indirect_split { 77 list_node_t is_node; /* link on iv_splits */ 78 79 /* 80 * is_split_offset is the offset into the i/o. 81 * This is the sum of the previous splits' is_size's. 82 */ 83 uint64_t is_split_offset; 84 85 vdev_t *is_vdev; /* top-level vdev */ 86 uint64_t is_target_offset; /* offset on is_vdev */ 87 uint64_t is_size; 88 int is_children; /* number of entries in is_child[] */ 89 90 /* 91 * is_good_child is the child that we are currently using to 92 * attempt reconstruction. 93 */ 94 int is_good_child; 95 96 indirect_child_t is_child[1]; /* variable-length */ 97 } indirect_split_t; 98 99 /* 100 * The indirect_vsd_t is associated with each i/o to the indirect vdev. 101 * It is the "Vdev-Specific Data" in the zio_t's io_vsd. 102 */ 103 typedef struct indirect_vsd { 104 boolean_t iv_split_block; 105 boolean_t iv_reconstruct; 106 107 list_t iv_splits; /* list of indirect_split_t's */ 108 } indirect_vsd_t; 109 110 /* 111 * List of supported read-incompatible ZFS features. Do not add here features 112 * marked as ZFEATURE_FLAG_READONLY_COMPAT, they are irrelevant for read-only! 113 */ 114 static const char *features_for_read[] = { 115 "com.datto:bookmark_v2", 116 "com.datto:encryption", 117 "com.delphix:bookmark_written", 118 "com.delphix:device_removal", 119 "com.delphix:embedded_data", 120 "com.delphix:extensible_dataset", 121 "com.delphix:head_errlog", 122 "com.delphix:hole_birth", 123 "com.joyent:multi_vdev_crash_dump", 124 "com.klarasystems:vdev_zaps_v2", 125 "org.freebsd:zstd_compress", 126 "org.illumos:lz4_compress", 127 "org.illumos:sha512", 128 "org.illumos:skein", 129 "org.open-zfs:large_blocks", 130 "org.openzfs:blake3", 131 "org.zfsonlinux:large_dnode", 132 "com.klarasystems:dynamic_gang_header", 133 NULL 134 }; 135 136 /* 137 * List of all pools, chained through spa_link. 138 */ 139 static spa_list_t zfs_pools; 140 141 static const dnode_phys_t *dnode_cache_obj; 142 static uint64_t dnode_cache_bn; 143 static char *dnode_cache_buf; 144 145 static int zio_read(const spa_t *spa, const blkptr_t *bp, void *buf); 146 static int zio_read_impl(const spa_t *spa, const blkptr_t *bp, void *buf, 147 bool print); 148 static int zfs_get_root(const spa_t *spa, uint64_t *objid); 149 static int zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result); 150 static int zap_lookup(const spa_t *spa, const dnode_phys_t *dnode, 151 const char *name, uint64_t integer_size, uint64_t num_integers, 152 void *value); 153 static int objset_get_dnode(const spa_t *, const objset_phys_t *, uint64_t, 154 dnode_phys_t *); 155 static int dnode_read(const spa_t *, const dnode_phys_t *, off_t, void *, 156 size_t); 157 static int vdev_indirect_read(vdev_t *, const blkptr_t *, void *, off_t, 158 size_t); 159 static int vdev_mirror_read(vdev_t *, const blkptr_t *, void *, off_t, size_t); 160 vdev_indirect_mapping_t *vdev_indirect_mapping_open(spa_t *, objset_phys_t *, 161 uint64_t); 162 vdev_indirect_mapping_entry_phys_t * 163 vdev_indirect_mapping_duplicate_adjacent_entries(vdev_t *, uint64_t, 164 uint64_t, uint64_t *); 165 166 static void 167 zfs_init(void) 168 { 169 STAILQ_INIT(&zfs_pools); 170 171 dnode_cache_buf = malloc(SPA_MAXBLOCKSIZE); 172 173 zfs_init_crc(); 174 #ifdef HAS_ZSTD_ZFS 175 zstd_init(); 176 #endif 177 } 178 179 static int 180 nvlist_check_features_for_read(nvlist_t *nvl) 181 { 182 nvlist_t *features = NULL; 183 nvs_data_t *data; 184 nvp_header_t *nvp; 185 nv_string_t *nvp_name; 186 int rc; 187 188 rc = nvlist_find(nvl, ZPOOL_CONFIG_FEATURES_FOR_READ, 189 DATA_TYPE_NVLIST, NULL, &features, NULL); 190 switch (rc) { 191 case 0: 192 break; /* Continue with checks */ 193 194 case ENOENT: 195 return (0); /* All features are disabled */ 196 197 default: 198 return (rc); /* Error while reading nvlist */ 199 } 200 201 data = (nvs_data_t *)features->nv_data; 202 nvp = &data->nvl_pair; /* first pair in nvlist */ 203 204 while (nvp->encoded_size != 0 && nvp->decoded_size != 0) { 205 int i, found; 206 207 nvp_name = (nv_string_t *)((uintptr_t)nvp + sizeof(*nvp)); 208 found = 0; 209 210 for (i = 0; features_for_read[i] != NULL; i++) { 211 if (memcmp(nvp_name->nv_data, features_for_read[i], 212 nvp_name->nv_size) == 0) { 213 found = 1; 214 break; 215 } 216 } 217 218 if (!found) { 219 printf("ZFS: unsupported feature: %.*s\n", 220 nvp_name->nv_size, nvp_name->nv_data); 221 rc = EIO; 222 } 223 nvp = (nvp_header_t *)((uint8_t *)nvp + nvp->encoded_size); 224 } 225 nvlist_destroy(features); 226 227 return (rc); 228 } 229 230 static int 231 vdev_read_phys(vdev_t *vdev, const blkptr_t *bp, void *buf, 232 off_t offset, size_t size) 233 { 234 size_t psize; 235 int rc; 236 237 if (vdev->v_phys_read == NULL) 238 return (ENOTSUP); 239 240 if (bp) { 241 psize = BP_GET_PSIZE(bp); 242 } else { 243 psize = size; 244 } 245 246 rc = vdev->v_phys_read(vdev, vdev->v_priv, offset, buf, psize); 247 if (rc == 0) { 248 if (bp != NULL) 249 rc = zio_checksum_verify(vdev->v_spa, bp, buf); 250 } 251 252 return (rc); 253 } 254 255 static int 256 vdev_write_phys(vdev_t *vdev, void *buf, off_t offset, size_t size) 257 { 258 if (vdev->v_phys_write == NULL) 259 return (ENOTSUP); 260 261 return (vdev->v_phys_write(vdev, offset, buf, size)); 262 } 263 264 typedef struct remap_segment { 265 vdev_t *rs_vd; 266 uint64_t rs_offset; 267 uint64_t rs_asize; 268 uint64_t rs_split_offset; 269 list_node_t rs_node; 270 } remap_segment_t; 271 272 static remap_segment_t * 273 rs_alloc(vdev_t *vd, uint64_t offset, uint64_t asize, uint64_t split_offset) 274 { 275 remap_segment_t *rs = malloc(sizeof (remap_segment_t)); 276 277 if (rs != NULL) { 278 rs->rs_vd = vd; 279 rs->rs_offset = offset; 280 rs->rs_asize = asize; 281 rs->rs_split_offset = split_offset; 282 } 283 284 return (rs); 285 } 286 287 vdev_indirect_mapping_t * 288 vdev_indirect_mapping_open(spa_t *spa, objset_phys_t *os, 289 uint64_t mapping_object) 290 { 291 vdev_indirect_mapping_t *vim; 292 vdev_indirect_mapping_phys_t *vim_phys; 293 int rc; 294 295 vim = calloc(1, sizeof (*vim)); 296 if (vim == NULL) 297 return (NULL); 298 299 vim->vim_dn = calloc(1, sizeof (*vim->vim_dn)); 300 if (vim->vim_dn == NULL) { 301 free(vim); 302 return (NULL); 303 } 304 305 rc = objset_get_dnode(spa, os, mapping_object, vim->vim_dn); 306 if (rc != 0) { 307 free(vim->vim_dn); 308 free(vim); 309 return (NULL); 310 } 311 312 vim->vim_spa = spa; 313 vim->vim_phys = malloc(sizeof (*vim->vim_phys)); 314 if (vim->vim_phys == NULL) { 315 free(vim->vim_dn); 316 free(vim); 317 return (NULL); 318 } 319 320 vim_phys = (vdev_indirect_mapping_phys_t *)DN_BONUS(vim->vim_dn); 321 *vim->vim_phys = *vim_phys; 322 323 vim->vim_objset = os; 324 vim->vim_object = mapping_object; 325 vim->vim_entries = NULL; 326 327 vim->vim_havecounts = 328 (vim->vim_dn->dn_bonuslen > VDEV_INDIRECT_MAPPING_SIZE_V0); 329 330 return (vim); 331 } 332 333 /* 334 * Compare an offset with an indirect mapping entry; there are three 335 * possible scenarios: 336 * 337 * 1. The offset is "less than" the mapping entry; meaning the 338 * offset is less than the source offset of the mapping entry. In 339 * this case, there is no overlap between the offset and the 340 * mapping entry and -1 will be returned. 341 * 342 * 2. The offset is "greater than" the mapping entry; meaning the 343 * offset is greater than the mapping entry's source offset plus 344 * the entry's size. In this case, there is no overlap between 345 * the offset and the mapping entry and 1 will be returned. 346 * 347 * NOTE: If the offset is actually equal to the entry's offset 348 * plus size, this is considered to be "greater" than the entry, 349 * and this case applies (i.e. 1 will be returned). Thus, the 350 * entry's "range" can be considered to be inclusive at its 351 * start, but exclusive at its end: e.g. [src, src + size). 352 * 353 * 3. The last case to consider is if the offset actually falls 354 * within the mapping entry's range. If this is the case, the 355 * offset is considered to be "equal to" the mapping entry and 356 * 0 will be returned. 357 * 358 * NOTE: If the offset is equal to the entry's source offset, 359 * this case applies and 0 will be returned. If the offset is 360 * equal to the entry's source plus its size, this case does 361 * *not* apply (see "NOTE" above for scenario 2), and 1 will be 362 * returned. 363 */ 364 static int 365 dva_mapping_overlap_compare(const void *v_key, const void *v_array_elem) 366 { 367 const uint64_t *key = v_key; 368 const vdev_indirect_mapping_entry_phys_t *array_elem = 369 v_array_elem; 370 uint64_t src_offset = DVA_MAPPING_GET_SRC_OFFSET(array_elem); 371 372 if (*key < src_offset) { 373 return (-1); 374 } else if (*key < src_offset + DVA_GET_ASIZE(&array_elem->vimep_dst)) { 375 return (0); 376 } else { 377 return (1); 378 } 379 } 380 381 /* 382 * Return array entry. 383 */ 384 static vdev_indirect_mapping_entry_phys_t * 385 vdev_indirect_mapping_entry(vdev_indirect_mapping_t *vim, uint64_t index) 386 { 387 uint64_t size; 388 off_t offset = 0; 389 int rc; 390 391 if (vim->vim_phys->vimp_num_entries == 0) 392 return (NULL); 393 394 if (vim->vim_entries == NULL) { 395 uint64_t bsize; 396 397 bsize = vim->vim_dn->dn_datablkszsec << SPA_MINBLOCKSHIFT; 398 size = vim->vim_phys->vimp_num_entries * 399 sizeof (*vim->vim_entries); 400 if (size > bsize) { 401 size = bsize / sizeof (*vim->vim_entries); 402 size *= sizeof (*vim->vim_entries); 403 } 404 vim->vim_entries = malloc(size); 405 if (vim->vim_entries == NULL) 406 return (NULL); 407 vim->vim_num_entries = size / sizeof (*vim->vim_entries); 408 offset = index * sizeof (*vim->vim_entries); 409 } 410 411 /* We have data in vim_entries */ 412 if (offset == 0) { 413 if (index >= vim->vim_entry_offset && 414 index <= vim->vim_entry_offset + vim->vim_num_entries) { 415 index -= vim->vim_entry_offset; 416 return (&vim->vim_entries[index]); 417 } 418 offset = index * sizeof (*vim->vim_entries); 419 } 420 421 vim->vim_entry_offset = index; 422 size = vim->vim_num_entries * sizeof (*vim->vim_entries); 423 rc = dnode_read(vim->vim_spa, vim->vim_dn, offset, vim->vim_entries, 424 size); 425 if (rc != 0) { 426 /* Read error, invalidate vim_entries. */ 427 free(vim->vim_entries); 428 vim->vim_entries = NULL; 429 return (NULL); 430 } 431 index -= vim->vim_entry_offset; 432 return (&vim->vim_entries[index]); 433 } 434 435 /* 436 * Returns the mapping entry for the given offset. 437 * 438 * It's possible that the given offset will not be in the mapping table 439 * (i.e. no mapping entries contain this offset), in which case, the 440 * return value depends on the "next_if_missing" parameter. 441 * 442 * If the offset is not found in the table and "next_if_missing" is 443 * B_FALSE, then NULL will always be returned. The behavior is intended 444 * to allow consumers to get the entry corresponding to the offset 445 * parameter, iff the offset overlaps with an entry in the table. 446 * 447 * If the offset is not found in the table and "next_if_missing" is 448 * B_TRUE, then the entry nearest to the given offset will be returned, 449 * such that the entry's source offset is greater than the offset 450 * passed in (i.e. the "next" mapping entry in the table is returned, if 451 * the offset is missing from the table). If there are no entries whose 452 * source offset is greater than the passed in offset, NULL is returned. 453 */ 454 static vdev_indirect_mapping_entry_phys_t * 455 vdev_indirect_mapping_entry_for_offset(vdev_indirect_mapping_t *vim, 456 uint64_t offset) 457 { 458 ASSERT(vim->vim_phys->vimp_num_entries > 0); 459 460 vdev_indirect_mapping_entry_phys_t *entry; 461 462 uint64_t last = vim->vim_phys->vimp_num_entries - 1; 463 uint64_t base = 0; 464 465 /* 466 * We don't define these inside of the while loop because we use 467 * their value in the case that offset isn't in the mapping. 468 */ 469 uint64_t mid; 470 int result; 471 472 while (last >= base) { 473 mid = base + ((last - base) >> 1); 474 475 entry = vdev_indirect_mapping_entry(vim, mid); 476 if (entry == NULL) 477 break; 478 result = dva_mapping_overlap_compare(&offset, entry); 479 480 if (result == 0) { 481 break; 482 } else if (result < 0) { 483 last = mid - 1; 484 } else { 485 base = mid + 1; 486 } 487 } 488 return (entry); 489 } 490 491 /* 492 * Given an indirect vdev and an extent on that vdev, it duplicates the 493 * physical entries of the indirect mapping that correspond to the extent 494 * to a new array and returns a pointer to it. In addition, copied_entries 495 * is populated with the number of mapping entries that were duplicated. 496 * 497 * Finally, since we are doing an allocation, it is up to the caller to 498 * free the array allocated in this function. 499 */ 500 vdev_indirect_mapping_entry_phys_t * 501 vdev_indirect_mapping_duplicate_adjacent_entries(vdev_t *vd, uint64_t offset, 502 uint64_t asize, uint64_t *copied_entries) 503 { 504 vdev_indirect_mapping_entry_phys_t *duplicate_mappings = NULL; 505 vdev_indirect_mapping_t *vim = vd->v_mapping; 506 uint64_t entries = 0; 507 508 vdev_indirect_mapping_entry_phys_t *first_mapping = 509 vdev_indirect_mapping_entry_for_offset(vim, offset); 510 ASSERT3P(first_mapping, !=, NULL); 511 512 vdev_indirect_mapping_entry_phys_t *m = first_mapping; 513 while (asize > 0) { 514 uint64_t size = DVA_GET_ASIZE(&m->vimep_dst); 515 uint64_t inner_offset = offset - DVA_MAPPING_GET_SRC_OFFSET(m); 516 uint64_t inner_size = MIN(asize, size - inner_offset); 517 518 offset += inner_size; 519 asize -= inner_size; 520 entries++; 521 m++; 522 } 523 524 size_t copy_length = entries * sizeof (*first_mapping); 525 duplicate_mappings = malloc(copy_length); 526 if (duplicate_mappings != NULL) 527 bcopy(first_mapping, duplicate_mappings, copy_length); 528 else 529 entries = 0; 530 531 *copied_entries = entries; 532 533 return (duplicate_mappings); 534 } 535 536 static vdev_t * 537 vdev_lookup_top(const spa_t *spa, uint64_t vdev) 538 { 539 vdev_t *rvd; 540 vdev_list_t *vlist; 541 542 vlist = &spa->spa_root_vdev->v_children; 543 STAILQ_FOREACH(rvd, vlist, v_childlink) 544 if (rvd->v_id == vdev) 545 break; 546 547 return (rvd); 548 } 549 550 /* 551 * This is a callback for vdev_indirect_remap() which allocates an 552 * indirect_split_t for each split segment and adds it to iv_splits. 553 */ 554 static void 555 vdev_indirect_gather_splits(uint64_t split_offset, vdev_t *vd, uint64_t offset, 556 uint64_t size, void *arg) 557 { 558 int n = 1; 559 zio_t *zio = arg; 560 indirect_vsd_t *iv = zio->io_vsd; 561 562 if (vd->v_read == vdev_indirect_read) 563 return; 564 565 if (vd->v_read == vdev_mirror_read) 566 n = vd->v_nchildren; 567 568 indirect_split_t *is = 569 malloc(offsetof(indirect_split_t, is_child[n])); 570 if (is == NULL) { 571 zio->io_error = ENOMEM; 572 return; 573 } 574 bzero(is, offsetof(indirect_split_t, is_child[n])); 575 576 is->is_children = n; 577 is->is_size = size; 578 is->is_split_offset = split_offset; 579 is->is_target_offset = offset; 580 is->is_vdev = vd; 581 582 /* 583 * Note that we only consider multiple copies of the data for 584 * *mirror* vdevs. We don't for "replacing" or "spare" vdevs, even 585 * though they use the same ops as mirror, because there's only one 586 * "good" copy under the replacing/spare. 587 */ 588 if (vd->v_read == vdev_mirror_read) { 589 int i = 0; 590 vdev_t *kid; 591 592 STAILQ_FOREACH(kid, &vd->v_children, v_childlink) { 593 is->is_child[i++].ic_vdev = kid; 594 } 595 } else { 596 is->is_child[0].ic_vdev = vd; 597 } 598 599 list_insert_tail(&iv->iv_splits, is); 600 } 601 602 static void 603 vdev_indirect_remap(vdev_t *vd, uint64_t offset, uint64_t asize, void *arg) 604 { 605 list_t stack; 606 spa_t *spa = vd->v_spa; 607 zio_t *zio = arg; 608 remap_segment_t *rs; 609 610 list_create(&stack, sizeof (remap_segment_t), 611 offsetof(remap_segment_t, rs_node)); 612 613 rs = rs_alloc(vd, offset, asize, 0); 614 if (rs == NULL) { 615 printf("vdev_indirect_remap: out of memory.\n"); 616 zio->io_error = ENOMEM; 617 } 618 for (; rs != NULL; rs = list_remove_head(&stack)) { 619 vdev_t *v = rs->rs_vd; 620 uint64_t num_entries = 0; 621 /* vdev_indirect_mapping_t *vim = v->v_mapping; */ 622 vdev_indirect_mapping_entry_phys_t *mapping = 623 vdev_indirect_mapping_duplicate_adjacent_entries(v, 624 rs->rs_offset, rs->rs_asize, &num_entries); 625 626 if (num_entries == 0) 627 zio->io_error = ENOMEM; 628 629 for (uint64_t i = 0; i < num_entries; i++) { 630 vdev_indirect_mapping_entry_phys_t *m = &mapping[i]; 631 uint64_t size = DVA_GET_ASIZE(&m->vimep_dst); 632 uint64_t dst_offset = DVA_GET_OFFSET(&m->vimep_dst); 633 uint64_t dst_vdev = DVA_GET_VDEV(&m->vimep_dst); 634 uint64_t inner_offset = rs->rs_offset - 635 DVA_MAPPING_GET_SRC_OFFSET(m); 636 uint64_t inner_size = 637 MIN(rs->rs_asize, size - inner_offset); 638 vdev_t *dst_v = vdev_lookup_top(spa, dst_vdev); 639 640 if (dst_v->v_read == vdev_indirect_read) { 641 remap_segment_t *o; 642 643 o = rs_alloc(dst_v, dst_offset + inner_offset, 644 inner_size, rs->rs_split_offset); 645 if (o == NULL) { 646 printf("vdev_indirect_remap: " 647 "out of memory.\n"); 648 zio->io_error = ENOMEM; 649 break; 650 } 651 652 list_insert_head(&stack, o); 653 } 654 vdev_indirect_gather_splits(rs->rs_split_offset, dst_v, 655 dst_offset + inner_offset, inner_size, arg); 656 657 /* 658 * vdev_indirect_gather_splits can have memory 659 * allocation error, we can not recover from it. 660 */ 661 if (zio->io_error != 0) 662 break; 663 rs->rs_offset += inner_size; 664 rs->rs_asize -= inner_size; 665 rs->rs_split_offset += inner_size; 666 } 667 668 free(mapping); 669 free(rs); 670 if (zio->io_error != 0) 671 break; 672 } 673 674 list_destroy(&stack); 675 } 676 677 static void 678 vdev_indirect_map_free(zio_t *zio) 679 { 680 indirect_vsd_t *iv = zio->io_vsd; 681 indirect_split_t *is; 682 683 while ((is = list_head(&iv->iv_splits)) != NULL) { 684 for (int c = 0; c < is->is_children; c++) { 685 indirect_child_t *ic = &is->is_child[c]; 686 free(ic->ic_data); 687 } 688 list_remove(&iv->iv_splits, is); 689 free(is); 690 } 691 free(iv); 692 } 693 694 static int 695 vdev_indirect_read(vdev_t *vdev, const blkptr_t *bp, void *buf, 696 off_t offset, size_t bytes) 697 { 698 zio_t zio; 699 spa_t *spa = vdev->v_spa; 700 indirect_vsd_t *iv; 701 indirect_split_t *first; 702 int rc = EIO; 703 704 iv = calloc(1, sizeof(*iv)); 705 if (iv == NULL) 706 return (ENOMEM); 707 708 list_create(&iv->iv_splits, 709 sizeof (indirect_split_t), offsetof(indirect_split_t, is_node)); 710 711 bzero(&zio, sizeof(zio)); 712 zio.io_spa = spa; 713 zio.io_bp = (blkptr_t *)bp; 714 zio.io_data = buf; 715 zio.io_size = bytes; 716 zio.io_offset = offset; 717 zio.io_vd = vdev; 718 zio.io_vsd = iv; 719 720 if (vdev->v_mapping == NULL) { 721 vdev_indirect_config_t *vic; 722 723 vic = &vdev->vdev_indirect_config; 724 vdev->v_mapping = vdev_indirect_mapping_open(spa, 725 spa->spa_mos, vic->vic_mapping_object); 726 } 727 728 vdev_indirect_remap(vdev, offset, bytes, &zio); 729 if (zio.io_error != 0) 730 return (zio.io_error); 731 732 first = list_head(&iv->iv_splits); 733 if (first->is_size == zio.io_size) { 734 /* 735 * This is not a split block; we are pointing to the entire 736 * data, which will checksum the same as the original data. 737 * Pass the BP down so that the child i/o can verify the 738 * checksum, and try a different location if available 739 * (e.g. on a mirror). 740 * 741 * While this special case could be handled the same as the 742 * general (split block) case, doing it this way ensures 743 * that the vast majority of blocks on indirect vdevs 744 * (which are not split) are handled identically to blocks 745 * on non-indirect vdevs. This allows us to be less strict 746 * about performance in the general (but rare) case. 747 */ 748 rc = first->is_vdev->v_read(first->is_vdev, zio.io_bp, 749 zio.io_data, first->is_target_offset, bytes); 750 } else { 751 iv->iv_split_block = B_TRUE; 752 /* 753 * Read one copy of each split segment, from the 754 * top-level vdev. Since we don't know the 755 * checksum of each split individually, the child 756 * zio can't ensure that we get the right data. 757 * E.g. if it's a mirror, it will just read from a 758 * random (healthy) leaf vdev. We have to verify 759 * the checksum in vdev_indirect_io_done(). 760 */ 761 for (indirect_split_t *is = list_head(&iv->iv_splits); 762 is != NULL; is = list_next(&iv->iv_splits, is)) { 763 char *ptr = zio.io_data; 764 765 rc = is->is_vdev->v_read(is->is_vdev, zio.io_bp, 766 ptr + is->is_split_offset, is->is_target_offset, 767 is->is_size); 768 } 769 if (zio_checksum_verify(spa, zio.io_bp, zio.io_data)) 770 rc = ECKSUM; 771 else 772 rc = 0; 773 } 774 775 vdev_indirect_map_free(&zio); 776 if (rc == 0) 777 rc = zio.io_error; 778 779 return (rc); 780 } 781 782 static int 783 vdev_disk_read(vdev_t *vdev, const blkptr_t *bp, void *buf, 784 off_t offset, size_t bytes) 785 { 786 787 return (vdev_read_phys(vdev, bp, buf, 788 offset + VDEV_LABEL_START_SIZE, bytes)); 789 } 790 791 static int 792 vdev_missing_read(vdev_t *vdev __unused, const blkptr_t *bp __unused, 793 void *buf __unused, off_t offset __unused, size_t bytes __unused) 794 { 795 796 return (ENOTSUP); 797 } 798 799 static int 800 vdev_mirror_read(vdev_t *vdev, const blkptr_t *bp, void *buf, 801 off_t offset, size_t bytes) 802 { 803 vdev_t *kid; 804 int rc; 805 806 rc = EIO; 807 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) { 808 if (kid->v_state != VDEV_STATE_HEALTHY) 809 continue; 810 rc = kid->v_read(kid, bp, buf, offset, bytes); 811 if (!rc) 812 return (0); 813 } 814 815 return (rc); 816 } 817 818 static int 819 vdev_replacing_read(vdev_t *vdev, const blkptr_t *bp, void *buf, 820 off_t offset, size_t bytes) 821 { 822 vdev_t *kid; 823 824 /* 825 * Here we should have two kids: 826 * First one which is the one we are replacing and we can trust 827 * only this one to have valid data, but it might not be present. 828 * Second one is that one we are replacing with. It is most likely 829 * healthy, but we can't trust it has needed data, so we won't use it. 830 */ 831 kid = STAILQ_FIRST(&vdev->v_children); 832 if (kid == NULL) 833 return (EIO); 834 if (kid->v_state != VDEV_STATE_HEALTHY) 835 return (EIO); 836 return (kid->v_read(kid, bp, buf, offset, bytes)); 837 } 838 839 /* 840 * List of vdevs that were fully initialized from their own label, but later a 841 * newer label was found that obsoleted the stale label, freeing its 842 * configuration tree. We keep those vdevs around, since a new configuration 843 * may include them. 844 */ 845 static vdev_list_t orphans = STAILQ_HEAD_INITIALIZER(orphans); 846 847 static vdev_t * 848 vdev_find(vdev_list_t *list, uint64_t guid) 849 { 850 vdev_t *vdev, *safe; 851 852 STAILQ_FOREACH_SAFE(vdev, list, v_childlink, safe) { 853 if (vdev->v_guid == guid) 854 return (vdev); 855 if ((vdev = vdev_find(&vdev->v_children, guid)) != NULL) 856 return (vdev); 857 } 858 859 return (NULL); 860 } 861 862 static vdev_t * 863 vdev_create(uint64_t guid, vdev_read_t *_read) 864 { 865 vdev_t *vdev; 866 vdev_indirect_config_t *vic; 867 868 if ((vdev = vdev_find(&orphans, guid))) { 869 STAILQ_REMOVE(&orphans, vdev, vdev, v_childlink); 870 return (vdev); 871 } 872 873 vdev = calloc(1, sizeof(vdev_t)); 874 if (vdev != NULL) { 875 STAILQ_INIT(&vdev->v_children); 876 vdev->v_guid = guid; 877 vdev->v_read = _read; 878 879 /* 880 * root vdev has no read function, we use this fact to 881 * skip setting up data we do not need for root vdev. 882 * We only point root vdev from spa. 883 */ 884 if (_read != NULL) { 885 vic = &vdev->vdev_indirect_config; 886 vic->vic_prev_indirect_vdev = UINT64_MAX; 887 } 888 } 889 890 return (vdev); 891 } 892 893 static void 894 vdev_set_initial_state(vdev_t *vdev, const nvlist_t *nvlist) 895 { 896 uint64_t is_offline, is_faulted, is_degraded, is_removed, isnt_present; 897 uint64_t is_log; 898 899 is_offline = is_removed = is_faulted = is_degraded = isnt_present = 0; 900 is_log = 0; 901 (void) nvlist_find(nvlist, ZPOOL_CONFIG_OFFLINE, DATA_TYPE_UINT64, NULL, 902 &is_offline, NULL); 903 (void) nvlist_find(nvlist, ZPOOL_CONFIG_REMOVED, DATA_TYPE_UINT64, NULL, 904 &is_removed, NULL); 905 (void) nvlist_find(nvlist, ZPOOL_CONFIG_FAULTED, DATA_TYPE_UINT64, NULL, 906 &is_faulted, NULL); 907 (void) nvlist_find(nvlist, ZPOOL_CONFIG_DEGRADED, DATA_TYPE_UINT64, 908 NULL, &is_degraded, NULL); 909 (void) nvlist_find(nvlist, ZPOOL_CONFIG_NOT_PRESENT, DATA_TYPE_UINT64, 910 NULL, &isnt_present, NULL); 911 (void) nvlist_find(nvlist, ZPOOL_CONFIG_IS_LOG, DATA_TYPE_UINT64, NULL, 912 &is_log, NULL); 913 914 if (is_offline != 0) 915 vdev->v_state = VDEV_STATE_OFFLINE; 916 else if (is_removed != 0) 917 vdev->v_state = VDEV_STATE_REMOVED; 918 else if (is_faulted != 0) 919 vdev->v_state = VDEV_STATE_FAULTED; 920 else if (is_degraded != 0) 921 vdev->v_state = VDEV_STATE_DEGRADED; 922 else if (isnt_present != 0) 923 vdev->v_state = VDEV_STATE_CANT_OPEN; 924 925 vdev->v_islog = is_log != 0; 926 } 927 928 static int 929 vdev_init(uint64_t guid, const nvlist_t *nvlist, vdev_t **vdevp) 930 { 931 uint64_t id, ashift, asize, nparity; 932 const char *path; 933 const char *type; 934 int len, pathlen; 935 char *name; 936 vdev_t *vdev; 937 938 if (nvlist_find(nvlist, ZPOOL_CONFIG_ID, DATA_TYPE_UINT64, NULL, &id, 939 NULL) || 940 nvlist_find(nvlist, ZPOOL_CONFIG_TYPE, DATA_TYPE_STRING, NULL, 941 &type, &len)) { 942 return (ENOENT); 943 } 944 945 if (memcmp(type, VDEV_TYPE_MIRROR, len) != 0 && 946 memcmp(type, VDEV_TYPE_DISK, len) != 0 && 947 #ifdef ZFS_TEST 948 memcmp(type, VDEV_TYPE_FILE, len) != 0 && 949 #endif 950 memcmp(type, VDEV_TYPE_RAIDZ, len) != 0 && 951 memcmp(type, VDEV_TYPE_INDIRECT, len) != 0 && 952 memcmp(type, VDEV_TYPE_REPLACING, len) != 0 && 953 memcmp(type, VDEV_TYPE_HOLE, len) != 0) { 954 printf("ZFS: can only boot from disk, mirror, raidz1, " 955 "raidz2 and raidz3 vdevs, got: %.*s\n", len, type); 956 return (EIO); 957 } 958 959 if (memcmp(type, VDEV_TYPE_MIRROR, len) == 0) 960 vdev = vdev_create(guid, vdev_mirror_read); 961 else if (memcmp(type, VDEV_TYPE_RAIDZ, len) == 0) 962 vdev = vdev_create(guid, vdev_raidz_read); 963 else if (memcmp(type, VDEV_TYPE_REPLACING, len) == 0) 964 vdev = vdev_create(guid, vdev_replacing_read); 965 else if (memcmp(type, VDEV_TYPE_INDIRECT, len) == 0) { 966 vdev_indirect_config_t *vic; 967 968 vdev = vdev_create(guid, vdev_indirect_read); 969 if (vdev != NULL) { 970 vdev->v_state = VDEV_STATE_HEALTHY; 971 vic = &vdev->vdev_indirect_config; 972 973 nvlist_find(nvlist, 974 ZPOOL_CONFIG_INDIRECT_OBJECT, 975 DATA_TYPE_UINT64, 976 NULL, &vic->vic_mapping_object, NULL); 977 nvlist_find(nvlist, 978 ZPOOL_CONFIG_INDIRECT_BIRTHS, 979 DATA_TYPE_UINT64, 980 NULL, &vic->vic_births_object, NULL); 981 nvlist_find(nvlist, 982 ZPOOL_CONFIG_PREV_INDIRECT_VDEV, 983 DATA_TYPE_UINT64, 984 NULL, &vic->vic_prev_indirect_vdev, NULL); 985 } 986 } else if (memcmp(type, VDEV_TYPE_HOLE, len) == 0) { 987 vdev = vdev_create(guid, vdev_missing_read); 988 } else { 989 vdev = vdev_create(guid, vdev_disk_read); 990 } 991 992 if (vdev == NULL) 993 return (ENOMEM); 994 995 vdev_set_initial_state(vdev, nvlist); 996 vdev->v_id = id; 997 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASHIFT, 998 DATA_TYPE_UINT64, NULL, &ashift, NULL) == 0) 999 vdev->v_ashift = ashift; 1000 1001 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASIZE, 1002 DATA_TYPE_UINT64, NULL, &asize, NULL) == 0) { 1003 vdev->v_psize = asize + 1004 VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE; 1005 } 1006 1007 if (nvlist_find(nvlist, ZPOOL_CONFIG_NPARITY, 1008 DATA_TYPE_UINT64, NULL, &nparity, NULL) == 0) 1009 vdev->v_nparity = nparity; 1010 1011 if (nvlist_find(nvlist, ZPOOL_CONFIG_PATH, 1012 DATA_TYPE_STRING, NULL, &path, &pathlen) == 0) { 1013 char prefix[] = "/dev/"; 1014 1015 len = strlen(prefix); 1016 if (len < pathlen && memcmp(path, prefix, len) == 0) { 1017 path += len; 1018 pathlen -= len; 1019 } 1020 name = malloc(pathlen + 1); 1021 bcopy(path, name, pathlen); 1022 name[pathlen] = '\0'; 1023 vdev->v_name = name; 1024 } else { 1025 name = NULL; 1026 if (memcmp(type, VDEV_TYPE_RAIDZ, len) == 0) { 1027 if (vdev->v_nparity < 1 || 1028 vdev->v_nparity > 3) { 1029 printf("ZFS: invalid raidz parity: %d\n", 1030 vdev->v_nparity); 1031 return (EIO); 1032 } 1033 (void) asprintf(&name, "%.*s%d-%" PRIu64, len, type, 1034 vdev->v_nparity, id); 1035 } else { 1036 (void) asprintf(&name, "%.*s-%" PRIu64, len, type, id); 1037 } 1038 vdev->v_name = name; 1039 } 1040 *vdevp = vdev; 1041 return (0); 1042 } 1043 1044 /* 1045 * Find slot for vdev. We return either NULL to signal to use 1046 * STAILQ_INSERT_HEAD, or we return link element to be used with 1047 * STAILQ_INSERT_AFTER. 1048 */ 1049 static vdev_t * 1050 vdev_find_previous(vdev_t *top_vdev, uint64_t id) 1051 { 1052 vdev_t *v, *previous; 1053 1054 previous = NULL; 1055 STAILQ_FOREACH(v, &top_vdev->v_children, v_childlink) { 1056 if (v->v_id > id) 1057 return (previous); 1058 1059 if (v->v_id == id) 1060 return (v); 1061 1062 if (v->v_id < id) 1063 previous = v; 1064 } 1065 return (previous); 1066 } 1067 1068 static size_t 1069 vdev_child_count(vdev_t *vdev) 1070 { 1071 vdev_t *v; 1072 size_t count; 1073 1074 count = 0; 1075 STAILQ_FOREACH(v, &vdev->v_children, v_childlink) { 1076 count++; 1077 } 1078 return (count); 1079 } 1080 1081 /* 1082 * Insert vdev into top_vdev children list. List is ordered by v_id. 1083 */ 1084 static vdev_t * 1085 vdev_insert(vdev_t *top_vdev, vdev_t *vdev) 1086 { 1087 vdev_t *previous; 1088 size_t count; 1089 1090 /* 1091 * The top level vdev can appear in random order, depending how 1092 * the firmware is presenting the disk devices. 1093 * However, we will insert vdev to create list ordered by v_id, 1094 * so we can use either STAILQ_INSERT_HEAD or STAILQ_INSERT_AFTER 1095 * as STAILQ does not have insert before. 1096 */ 1097 previous = vdev_find_previous(top_vdev, vdev->v_id); 1098 1099 if (previous == NULL) { 1100 STAILQ_INSERT_HEAD(&top_vdev->v_children, vdev, v_childlink); 1101 } else if (previous->v_id == vdev->v_id) { 1102 /* 1103 * This vdev was configured from label config, 1104 * do not insert duplicate. 1105 */ 1106 free(vdev); 1107 return (previous); 1108 } else { 1109 STAILQ_INSERT_AFTER(&top_vdev->v_children, previous, vdev, 1110 v_childlink); 1111 } 1112 1113 count = vdev_child_count(top_vdev); 1114 if (top_vdev->v_nchildren < count) 1115 top_vdev->v_nchildren = count; 1116 return (vdev); 1117 } 1118 1119 static int 1120 vdev_from_nvlist(spa_t *spa, uint64_t top_guid, uint64_t label_guid, 1121 uint64_t txg, const nvlist_t *nvlist) 1122 { 1123 vdev_t *top_vdev, *vdev; 1124 nvlist_t **kids = NULL; 1125 int rc, nkids; 1126 1127 /* Get top vdev. */ 1128 top_vdev = vdev_find(&spa->spa_root_vdev->v_children, top_guid); 1129 if (top_vdev == NULL) { 1130 rc = vdev_init(top_guid, nvlist, &top_vdev); 1131 if (rc != 0) 1132 return (rc); 1133 top_vdev->v_spa = spa; 1134 top_vdev->v_top = top_vdev; 1135 top_vdev->v_label = label_guid; 1136 top_vdev->v_txg = txg; 1137 (void )vdev_insert(spa->spa_root_vdev, top_vdev); 1138 } 1139 1140 /* Add children if there are any. */ 1141 rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY, 1142 &nkids, &kids, NULL); 1143 if (rc == 0) { 1144 for (int i = 0; i < nkids; i++) { 1145 uint64_t guid; 1146 1147 rc = nvlist_find(kids[i], ZPOOL_CONFIG_GUID, 1148 DATA_TYPE_UINT64, NULL, &guid, NULL); 1149 if (rc != 0) 1150 goto done; 1151 1152 rc = vdev_init(guid, kids[i], &vdev); 1153 if (rc != 0) 1154 goto done; 1155 1156 vdev->v_spa = spa; 1157 vdev->v_top = top_vdev; 1158 vdev = vdev_insert(top_vdev, vdev); 1159 } 1160 } else { 1161 /* 1162 * When there are no children, nvlist_find() does return 1163 * error, reset it because leaf devices have no children. 1164 */ 1165 rc = 0; 1166 } 1167 done: 1168 if (kids != NULL) { 1169 for (int i = 0; i < nkids; i++) 1170 nvlist_destroy(kids[i]); 1171 free(kids); 1172 } 1173 1174 return (rc); 1175 } 1176 1177 static void 1178 vdev_set_state(vdev_t *vdev) 1179 { 1180 vdev_t *kid; 1181 int good_kids; 1182 int bad_kids; 1183 1184 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) { 1185 vdev_set_state(kid); 1186 } 1187 1188 /* 1189 * A mirror or raidz is healthy if all its kids are healthy. A 1190 * mirror is degraded if any of its kids is healthy; a raidz 1191 * is degraded if at most nparity kids are offline. 1192 */ 1193 if (STAILQ_FIRST(&vdev->v_children)) { 1194 good_kids = 0; 1195 bad_kids = 0; 1196 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) { 1197 if (kid->v_state == VDEV_STATE_HEALTHY) 1198 good_kids++; 1199 else 1200 bad_kids++; 1201 } 1202 if (bad_kids == 0) { 1203 vdev->v_state = VDEV_STATE_HEALTHY; 1204 } else { 1205 if (vdev->v_read == vdev_mirror_read) { 1206 if (good_kids) { 1207 vdev->v_state = VDEV_STATE_DEGRADED; 1208 } else { 1209 vdev->v_state = VDEV_STATE_OFFLINE; 1210 } 1211 } else if (vdev->v_read == vdev_raidz_read) { 1212 if (bad_kids > vdev->v_nparity) { 1213 vdev->v_state = VDEV_STATE_OFFLINE; 1214 } else { 1215 vdev->v_state = VDEV_STATE_DEGRADED; 1216 } 1217 } 1218 } 1219 } 1220 } 1221 1222 static int 1223 vdev_update_from_nvlist(vdev_t *root, uint64_t top_guid, const nvlist_t *nvlist) 1224 { 1225 vdev_t *vdev; 1226 nvlist_t **kids = NULL; 1227 int rc, nkids; 1228 1229 /* Update top vdev. */ 1230 vdev = vdev_find(&root->v_children, top_guid); 1231 if (vdev != NULL) 1232 vdev_set_initial_state(vdev, nvlist); 1233 1234 /* Update children if there are any. */ 1235 rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY, 1236 &nkids, &kids, NULL); 1237 if (rc == 0) { 1238 for (int i = 0; i < nkids; i++) { 1239 uint64_t guid; 1240 1241 rc = nvlist_find(kids[i], ZPOOL_CONFIG_GUID, 1242 DATA_TYPE_UINT64, NULL, &guid, NULL); 1243 if (rc != 0) 1244 break; 1245 1246 vdev = vdev_find(&root->v_children, guid); 1247 if (vdev != NULL) 1248 vdev_set_initial_state(vdev, kids[i]); 1249 } 1250 } else { 1251 rc = 0; 1252 } 1253 if (kids != NULL) { 1254 for (int i = 0; i < nkids; i++) 1255 nvlist_destroy(kids[i]); 1256 free(kids); 1257 } 1258 1259 return (rc); 1260 } 1261 1262 static void 1263 vdev_free(struct vdev *vdev) 1264 { 1265 struct vdev *kid, *safe; 1266 1267 STAILQ_FOREACH_SAFE(kid, &vdev->v_children, v_childlink, safe) 1268 vdev_free(kid); 1269 if (vdev->v_phys_read != NULL) 1270 STAILQ_INSERT_HEAD(&orphans, vdev, v_childlink); 1271 else 1272 free(vdev); 1273 } 1274 1275 static int 1276 vdev_init_from_nvlist(spa_t *spa, const nvlist_t *nvlist) 1277 { 1278 uint64_t pool_guid, vdev_children; 1279 nvlist_t *vdevs = NULL, **kids = NULL; 1280 int rc, nkids; 1281 1282 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64, 1283 NULL, &pool_guid, NULL) || 1284 nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_CHILDREN, DATA_TYPE_UINT64, 1285 NULL, &vdev_children, NULL) || 1286 nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST, 1287 NULL, &vdevs, NULL)) { 1288 printf("ZFS: can't find vdev details\n"); 1289 return (ENOENT); 1290 } 1291 1292 /* Wrong guid?! */ 1293 if (spa->spa_guid != pool_guid) { 1294 nvlist_destroy(vdevs); 1295 return (EINVAL); 1296 } 1297 1298 spa->spa_root_vdev->v_nchildren = vdev_children; 1299 1300 rc = nvlist_find(vdevs, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY, 1301 &nkids, &kids, NULL); 1302 nvlist_destroy(vdevs); 1303 1304 /* 1305 * MOS config has at least one child for root vdev. 1306 */ 1307 if (rc != 0) 1308 return (rc); 1309 1310 for (int i = 0; i < nkids; i++) { 1311 uint64_t guid; 1312 vdev_t *vdev; 1313 1314 rc = nvlist_find(kids[i], ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64, 1315 NULL, &guid, NULL); 1316 if (rc != 0) 1317 break; 1318 vdev = vdev_find(&spa->spa_root_vdev->v_children, guid); 1319 /* 1320 * Top level vdev is missing, create it. 1321 * XXXGL: how can this happen? 1322 */ 1323 if (vdev == NULL) 1324 rc = vdev_from_nvlist(spa, guid, 0, 0, kids[i]); 1325 else 1326 rc = vdev_update_from_nvlist(spa->spa_root_vdev, guid, 1327 kids[i]); 1328 if (rc != 0) 1329 break; 1330 } 1331 if (kids != NULL) { 1332 for (int i = 0; i < nkids; i++) 1333 nvlist_destroy(kids[i]); 1334 free(kids); 1335 } 1336 1337 /* 1338 * Re-evaluate top-level vdev state. 1339 */ 1340 vdev_set_state(spa->spa_root_vdev); 1341 1342 return (rc); 1343 } 1344 1345 static bool 1346 nvlist_find_child_guid(const nvlist_t *nvlist, uint64_t guid) 1347 { 1348 nvlist_t **kids = NULL; 1349 int nkids, i; 1350 bool rv = false; 1351 1352 if (nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY, 1353 &nkids, &kids, NULL) != 0) 1354 nkids = 0; 1355 1356 for (i = 0; i < nkids; i++) { 1357 uint64_t kid_guid; 1358 1359 if (nvlist_find(kids[i], ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64, 1360 NULL, &kid_guid, NULL) != 0) 1361 break; 1362 if (kid_guid == guid) 1363 rv = true; 1364 else 1365 rv = nvlist_find_child_guid(kids[i], guid); 1366 if (rv) 1367 break; 1368 } 1369 1370 for (i = 0; i < nkids; i++) 1371 nvlist_destroy(kids[i]); 1372 free(kids); 1373 1374 return (rv); 1375 } 1376 1377 static bool 1378 nvlist_find_vdev_guid(const nvlist_t *nvlist, uint64_t guid) 1379 { 1380 nvlist_t *vdevs; 1381 bool rv; 1382 1383 if (nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST, NULL, 1384 &vdevs, NULL) != 0) 1385 return (false); 1386 rv = nvlist_find_child_guid(vdevs, guid); 1387 nvlist_destroy(vdevs); 1388 1389 return (rv); 1390 } 1391 1392 static spa_t * 1393 spa_find_by_guid(uint64_t guid) 1394 { 1395 spa_t *spa; 1396 1397 STAILQ_FOREACH(spa, &zfs_pools, spa_link) 1398 if (spa->spa_guid == guid) 1399 return (spa); 1400 1401 return (NULL); 1402 } 1403 1404 static spa_t * 1405 spa_find_by_name(const char *name) 1406 { 1407 spa_t *spa; 1408 1409 STAILQ_FOREACH(spa, &zfs_pools, spa_link) 1410 if (strcmp(spa->spa_name, name) == 0) 1411 return (spa); 1412 1413 return (NULL); 1414 } 1415 1416 static spa_t * 1417 spa_create(uint64_t guid, const char *name) 1418 { 1419 spa_t *spa; 1420 1421 if ((spa = calloc(1, sizeof(spa_t))) == NULL) 1422 return (NULL); 1423 if ((spa->spa_name = strdup(name)) == NULL) { 1424 free(spa); 1425 return (NULL); 1426 } 1427 spa->spa_uberblock = &spa->spa_uberblock_master; 1428 spa->spa_mos = &spa->spa_mos_master; 1429 spa->spa_guid = guid; 1430 spa->spa_root_vdev = vdev_create(guid, NULL); 1431 if (spa->spa_root_vdev == NULL) { 1432 free(spa->spa_name); 1433 free(spa); 1434 return (NULL); 1435 } 1436 spa->spa_root_vdev->v_name = spa->spa_name; 1437 STAILQ_INSERT_TAIL(&zfs_pools, spa, spa_link); 1438 1439 return (spa); 1440 } 1441 1442 static const char * 1443 state_name(vdev_state_t state) 1444 { 1445 static const char *names[] = { 1446 "UNKNOWN", 1447 "CLOSED", 1448 "OFFLINE", 1449 "REMOVED", 1450 "CANT_OPEN", 1451 "FAULTED", 1452 "DEGRADED", 1453 "ONLINE" 1454 }; 1455 return (names[state]); 1456 } 1457 1458 #ifdef BOOT2 1459 1460 #define pager_printf printf 1461 1462 #else 1463 1464 static int 1465 pager_printf(const char *fmt, ...) 1466 { 1467 char line[80]; 1468 va_list args; 1469 1470 va_start(args, fmt); 1471 vsnprintf(line, sizeof(line), fmt, args); 1472 va_end(args); 1473 return (pager_output(line)); 1474 } 1475 1476 #endif 1477 1478 #define STATUS_FORMAT " %s %s\n" 1479 1480 static int 1481 print_state(int indent, const char *name, vdev_state_t state) 1482 { 1483 int i; 1484 char buf[512]; 1485 1486 buf[0] = 0; 1487 for (i = 0; i < indent; i++) 1488 strcat(buf, " "); 1489 strcat(buf, name); 1490 return (pager_printf(STATUS_FORMAT, buf, state_name(state))); 1491 } 1492 1493 static int 1494 vdev_status(vdev_t *vdev, int indent) 1495 { 1496 vdev_t *kid; 1497 int ret; 1498 1499 if (vdev->v_islog) { 1500 (void) pager_output(" logs\n"); 1501 indent++; 1502 } 1503 1504 ret = print_state(indent, vdev->v_name, vdev->v_state); 1505 if (ret != 0) 1506 return (ret); 1507 1508 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) { 1509 ret = vdev_status(kid, indent + 1); 1510 if (ret != 0) 1511 return (ret); 1512 } 1513 return (ret); 1514 } 1515 1516 static int 1517 spa_status(spa_t *spa) 1518 { 1519 static char bootfs[ZFS_MAXNAMELEN]; 1520 uint64_t rootid; 1521 vdev_list_t *vlist; 1522 vdev_t *vdev; 1523 int good_kids, bad_kids, degraded_kids, ret; 1524 vdev_state_t state; 1525 1526 ret = pager_printf(" pool: %s\n", spa->spa_name); 1527 if (ret != 0) 1528 return (ret); 1529 1530 if (zfs_get_root(spa, &rootid) == 0 && 1531 zfs_rlookup(spa, rootid, bootfs) == 0) { 1532 if (bootfs[0] == '\0') 1533 ret = pager_printf("bootfs: %s\n", spa->spa_name); 1534 else 1535 ret = pager_printf("bootfs: %s/%s\n", spa->spa_name, 1536 bootfs); 1537 if (ret != 0) 1538 return (ret); 1539 } 1540 ret = pager_printf("config:\n\n"); 1541 if (ret != 0) 1542 return (ret); 1543 ret = pager_printf(STATUS_FORMAT, "NAME", "STATE"); 1544 if (ret != 0) 1545 return (ret); 1546 1547 good_kids = 0; 1548 degraded_kids = 0; 1549 bad_kids = 0; 1550 vlist = &spa->spa_root_vdev->v_children; 1551 STAILQ_FOREACH(vdev, vlist, v_childlink) { 1552 if (vdev->v_state == VDEV_STATE_HEALTHY) 1553 good_kids++; 1554 else if (vdev->v_state == VDEV_STATE_DEGRADED) 1555 degraded_kids++; 1556 else 1557 bad_kids++; 1558 } 1559 1560 state = VDEV_STATE_CLOSED; 1561 if (good_kids > 0 && (degraded_kids + bad_kids) == 0) 1562 state = VDEV_STATE_HEALTHY; 1563 else if ((good_kids + degraded_kids) > 0) 1564 state = VDEV_STATE_DEGRADED; 1565 1566 ret = print_state(0, spa->spa_name, state); 1567 if (ret != 0) 1568 return (ret); 1569 1570 STAILQ_FOREACH(vdev, vlist, v_childlink) { 1571 ret = vdev_status(vdev, 1); 1572 if (ret != 0) 1573 return (ret); 1574 } 1575 return (ret); 1576 } 1577 1578 static int 1579 spa_all_status(void) 1580 { 1581 spa_t *spa; 1582 int first = 1, ret = 0; 1583 1584 STAILQ_FOREACH(spa, &zfs_pools, spa_link) { 1585 if (!first) { 1586 ret = pager_printf("\n"); 1587 if (ret != 0) 1588 return (ret); 1589 } 1590 first = 0; 1591 ret = spa_status(spa); 1592 if (ret != 0) 1593 return (ret); 1594 } 1595 return (ret); 1596 } 1597 1598 static uint64_t 1599 vdev_label_offset(uint64_t psize, int l, uint64_t offset) 1600 { 1601 uint64_t label_offset; 1602 1603 if (l < VDEV_LABELS / 2) 1604 label_offset = 0; 1605 else 1606 label_offset = psize - VDEV_LABELS * sizeof (vdev_label_t); 1607 1608 return (offset + l * sizeof (vdev_label_t) + label_offset); 1609 } 1610 1611 static int 1612 vdev_uberblock_compare(const uberblock_t *ub1, const uberblock_t *ub2) 1613 { 1614 unsigned int seq1 = 0; 1615 unsigned int seq2 = 0; 1616 int cmp = AVL_CMP(ub1->ub_txg, ub2->ub_txg); 1617 1618 if (cmp != 0) 1619 return (cmp); 1620 1621 cmp = AVL_CMP(ub1->ub_timestamp, ub2->ub_timestamp); 1622 if (cmp != 0) 1623 return (cmp); 1624 1625 if (MMP_VALID(ub1) && MMP_SEQ_VALID(ub1)) 1626 seq1 = MMP_SEQ(ub1); 1627 1628 if (MMP_VALID(ub2) && MMP_SEQ_VALID(ub2)) 1629 seq2 = MMP_SEQ(ub2); 1630 1631 return (AVL_CMP(seq1, seq2)); 1632 } 1633 1634 static int 1635 uberblock_verify(uberblock_t *ub) 1636 { 1637 if (ub->ub_magic == BSWAP_64((uint64_t)UBERBLOCK_MAGIC)) { 1638 byteswap_uint64_array(ub, sizeof (uberblock_t)); 1639 } 1640 1641 if (ub->ub_magic != UBERBLOCK_MAGIC || 1642 !SPA_VERSION_IS_SUPPORTED(ub->ub_version)) 1643 return (EINVAL); 1644 1645 return (0); 1646 } 1647 1648 static int 1649 vdev_label_read(vdev_t *vd, int l, void *buf, uint64_t offset, 1650 size_t size) 1651 { 1652 blkptr_t bp; 1653 off_t off; 1654 1655 off = vdev_label_offset(vd->v_psize, l, offset); 1656 1657 BP_ZERO(&bp); 1658 BP_SET_LSIZE(&bp, size); 1659 BP_SET_PSIZE(&bp, size); 1660 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL); 1661 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF); 1662 DVA_SET_OFFSET(BP_IDENTITY(&bp), off); 1663 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0); 1664 1665 return (vdev_read_phys(vd, &bp, buf, off, size)); 1666 } 1667 1668 /* 1669 * We do need to be sure we write to correct location. 1670 * Our vdev label does consist of 4 fields: 1671 * pad1 (8k), reserved. 1672 * bootenv (8k), checksummed, previously reserved, may contian garbage. 1673 * vdev_phys (112k), checksummed 1674 * uberblock ring (128k), checksummed. 1675 * 1676 * Since bootenv area may contain garbage, we can not reliably read it, as 1677 * we can get checksum errors. 1678 * Next best thing is vdev_phys - it is just after bootenv. It still may 1679 * be corrupted, but in such case we will miss this one write. 1680 */ 1681 static int 1682 vdev_label_write_validate(vdev_t *vd, int l, uint64_t offset) 1683 { 1684 uint64_t off, o_phys; 1685 void *buf; 1686 size_t size = VDEV_PHYS_SIZE; 1687 int rc; 1688 1689 o_phys = offsetof(vdev_label_t, vl_vdev_phys); 1690 off = vdev_label_offset(vd->v_psize, l, o_phys); 1691 1692 /* off should be 8K from bootenv */ 1693 if (vdev_label_offset(vd->v_psize, l, offset) + VDEV_PAD_SIZE != off) 1694 return (EINVAL); 1695 1696 buf = malloc(size); 1697 if (buf == NULL) 1698 return (ENOMEM); 1699 1700 /* Read vdev_phys */ 1701 rc = vdev_label_read(vd, l, buf, o_phys, size); 1702 free(buf); 1703 return (rc); 1704 } 1705 1706 static int 1707 vdev_label_write(vdev_t *vd, int l, vdev_boot_envblock_t *be, uint64_t offset) 1708 { 1709 zio_checksum_info_t *ci; 1710 zio_cksum_t cksum; 1711 off_t off; 1712 size_t size = VDEV_PAD_SIZE; 1713 int rc; 1714 1715 if (vd->v_phys_write == NULL) 1716 return (ENOTSUP); 1717 1718 off = vdev_label_offset(vd->v_psize, l, offset); 1719 1720 rc = vdev_label_write_validate(vd, l, offset); 1721 if (rc != 0) { 1722 return (rc); 1723 } 1724 1725 ci = &zio_checksum_table[ZIO_CHECKSUM_LABEL]; 1726 be->vbe_zbt.zec_magic = ZEC_MAGIC; 1727 zio_checksum_label_verifier(&be->vbe_zbt.zec_cksum, off); 1728 ci->ci_func[0](be, size, NULL, &cksum); 1729 be->vbe_zbt.zec_cksum = cksum; 1730 1731 return (vdev_write_phys(vd, be, off, size)); 1732 } 1733 1734 static int 1735 vdev_write_bootenv_impl(vdev_t *vdev, vdev_boot_envblock_t *be) 1736 { 1737 vdev_t *kid; 1738 int rv = 0, err; 1739 1740 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) { 1741 if (kid->v_state != VDEV_STATE_HEALTHY) 1742 continue; 1743 err = vdev_write_bootenv_impl(kid, be); 1744 if (err != 0) 1745 rv = err; 1746 } 1747 1748 /* 1749 * Non-leaf vdevs do not have v_phys_write. 1750 */ 1751 if (vdev->v_phys_write == NULL) 1752 return (rv); 1753 1754 for (int l = 0; l < VDEV_LABELS; l++) { 1755 err = vdev_label_write(vdev, l, be, 1756 offsetof(vdev_label_t, vl_be)); 1757 if (err != 0) { 1758 printf("failed to write bootenv to %s label %d: %d\n", 1759 vdev->v_name ? vdev->v_name : "unknown", l, err); 1760 rv = err; 1761 } 1762 } 1763 return (rv); 1764 } 1765 1766 int 1767 vdev_write_bootenv(vdev_t *vdev, nvlist_t *nvl) 1768 { 1769 vdev_boot_envblock_t *be; 1770 nvlist_t nv, *nvp; 1771 uint64_t version; 1772 int rv; 1773 1774 if (nvl->nv_size > sizeof(be->vbe_bootenv)) 1775 return (E2BIG); 1776 1777 version = VB_RAW; 1778 nvp = vdev_read_bootenv(vdev); 1779 if (nvp != NULL) { 1780 nvlist_find(nvp, BOOTENV_VERSION, DATA_TYPE_UINT64, NULL, 1781 &version, NULL); 1782 nvlist_destroy(nvp); 1783 } 1784 1785 be = calloc(1, sizeof(*be)); 1786 if (be == NULL) 1787 return (ENOMEM); 1788 1789 be->vbe_version = version; 1790 switch (version) { 1791 case VB_RAW: 1792 /* 1793 * If there is no envmap, we will just wipe bootenv. 1794 */ 1795 nvlist_find(nvl, GRUB_ENVMAP, DATA_TYPE_STRING, NULL, 1796 be->vbe_bootenv, NULL); 1797 rv = 0; 1798 break; 1799 1800 case VB_NVLIST: 1801 nv.nv_header = nvl->nv_header; 1802 nv.nv_asize = nvl->nv_asize; 1803 nv.nv_size = nvl->nv_size; 1804 1805 bcopy(&nv.nv_header, be->vbe_bootenv, sizeof(nv.nv_header)); 1806 nv.nv_data = be->vbe_bootenv + sizeof(nvs_header_t); 1807 bcopy(nvl->nv_data, nv.nv_data, nv.nv_size); 1808 rv = nvlist_export(&nv); 1809 break; 1810 1811 default: 1812 rv = EINVAL; 1813 break; 1814 } 1815 1816 if (rv == 0) { 1817 be->vbe_version = htobe64(be->vbe_version); 1818 rv = vdev_write_bootenv_impl(vdev, be); 1819 } 1820 free(be); 1821 return (rv); 1822 } 1823 1824 /* 1825 * Read the bootenv area from pool label, return the nvlist from it. 1826 * We return from first successful read. 1827 */ 1828 nvlist_t * 1829 vdev_read_bootenv(vdev_t *vdev) 1830 { 1831 vdev_t *kid; 1832 nvlist_t *benv; 1833 vdev_boot_envblock_t *be; 1834 char *command; 1835 bool ok; 1836 int rv; 1837 1838 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) { 1839 if (kid->v_state != VDEV_STATE_HEALTHY) 1840 continue; 1841 1842 benv = vdev_read_bootenv(kid); 1843 if (benv != NULL) 1844 return (benv); 1845 } 1846 1847 be = malloc(sizeof (*be)); 1848 if (be == NULL) 1849 return (NULL); 1850 1851 rv = 0; 1852 for (int l = 0; l < VDEV_LABELS; l++) { 1853 rv = vdev_label_read(vdev, l, be, 1854 offsetof(vdev_label_t, vl_be), 1855 sizeof (*be)); 1856 if (rv == 0) 1857 break; 1858 } 1859 if (rv != 0) { 1860 free(be); 1861 return (NULL); 1862 } 1863 1864 be->vbe_version = be64toh(be->vbe_version); 1865 switch (be->vbe_version) { 1866 case VB_RAW: 1867 /* 1868 * we have textual data in vbe_bootenv, create nvlist 1869 * with key "envmap". 1870 */ 1871 benv = nvlist_create(NV_UNIQUE_NAME); 1872 if (benv != NULL) { 1873 if (*be->vbe_bootenv == '\0') { 1874 nvlist_add_uint64(benv, BOOTENV_VERSION, 1875 VB_NVLIST); 1876 break; 1877 } 1878 nvlist_add_uint64(benv, BOOTENV_VERSION, VB_RAW); 1879 be->vbe_bootenv[sizeof (be->vbe_bootenv) - 1] = '\0'; 1880 nvlist_add_string(benv, GRUB_ENVMAP, be->vbe_bootenv); 1881 } 1882 break; 1883 1884 case VB_NVLIST: 1885 benv = nvlist_import(be->vbe_bootenv, sizeof(be->vbe_bootenv)); 1886 break; 1887 1888 default: 1889 command = (char *)be; 1890 ok = false; 1891 1892 /* Check for legacy zfsbootcfg command string */ 1893 for (int i = 0; command[i] != '\0'; i++) { 1894 if (iscntrl(command[i])) { 1895 ok = false; 1896 break; 1897 } else { 1898 ok = true; 1899 } 1900 } 1901 benv = nvlist_create(NV_UNIQUE_NAME); 1902 if (benv != NULL) { 1903 if (ok) 1904 nvlist_add_string(benv, FREEBSD_BOOTONCE, 1905 command); 1906 else 1907 nvlist_add_uint64(benv, BOOTENV_VERSION, 1908 VB_NVLIST); 1909 } 1910 break; 1911 } 1912 free(be); 1913 return (benv); 1914 } 1915 1916 static uint64_t 1917 vdev_get_label_asize(nvlist_t *nvl) 1918 { 1919 nvlist_t *vdevs; 1920 uint64_t asize; 1921 const char *type; 1922 int len; 1923 1924 asize = 0; 1925 /* Get vdev tree */ 1926 if (nvlist_find(nvl, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST, 1927 NULL, &vdevs, NULL) != 0) 1928 return (asize); 1929 1930 /* 1931 * Get vdev type. We will calculate asize for raidz, mirror and disk. 1932 * For raidz, the asize is raw size of all children. 1933 */ 1934 if (nvlist_find(vdevs, ZPOOL_CONFIG_TYPE, DATA_TYPE_STRING, 1935 NULL, &type, &len) != 0) 1936 goto done; 1937 1938 if (memcmp(type, VDEV_TYPE_MIRROR, len) != 0 && 1939 memcmp(type, VDEV_TYPE_DISK, len) != 0 && 1940 memcmp(type, VDEV_TYPE_RAIDZ, len) != 0) 1941 goto done; 1942 1943 if (nvlist_find(vdevs, ZPOOL_CONFIG_ASIZE, DATA_TYPE_UINT64, 1944 NULL, &asize, NULL) != 0) 1945 goto done; 1946 1947 if (memcmp(type, VDEV_TYPE_RAIDZ, len) == 0) { 1948 nvlist_t **kids; 1949 int nkids; 1950 1951 if (nvlist_find(vdevs, ZPOOL_CONFIG_CHILDREN, 1952 DATA_TYPE_NVLIST_ARRAY, &nkids, &kids, NULL) != 0) { 1953 asize = 0; 1954 goto done; 1955 } 1956 1957 asize /= nkids; 1958 for (int i = 0; i < nkids; i++) 1959 nvlist_destroy(kids[i]); 1960 free(kids); 1961 } 1962 1963 asize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE; 1964 done: 1965 nvlist_destroy(vdevs); 1966 return (asize); 1967 } 1968 1969 static nvlist_t * 1970 vdev_label_read_config(vdev_t *vd, uint64_t txg) 1971 { 1972 vdev_phys_t *label; 1973 uint64_t best_txg = 0; 1974 uint64_t label_txg = 0; 1975 uint64_t asize; 1976 nvlist_t *nvl = NULL, *tmp; 1977 int error; 1978 1979 label = malloc(sizeof (vdev_phys_t)); 1980 if (label == NULL) 1981 return (NULL); 1982 1983 for (int l = 0; l < VDEV_LABELS; l++) { 1984 if (vdev_label_read(vd, l, label, 1985 offsetof(vdev_label_t, vl_vdev_phys), 1986 sizeof (vdev_phys_t))) 1987 continue; 1988 1989 tmp = nvlist_import(label->vp_nvlist, 1990 sizeof(label->vp_nvlist)); 1991 if (tmp == NULL) 1992 continue; 1993 1994 error = nvlist_find(tmp, ZPOOL_CONFIG_POOL_TXG, 1995 DATA_TYPE_UINT64, NULL, &label_txg, NULL); 1996 if (error != 0 || label_txg == 0) { 1997 nvlist_destroy(nvl); 1998 nvl = tmp; 1999 goto done; 2000 } 2001 2002 if (label_txg <= txg && label_txg > best_txg) { 2003 best_txg = label_txg; 2004 nvlist_destroy(nvl); 2005 nvl = tmp; 2006 tmp = NULL; 2007 2008 /* 2009 * Use asize from pool config. We need this 2010 * because we can get bad value from BIOS. 2011 */ 2012 asize = vdev_get_label_asize(nvl); 2013 if (asize != 0) { 2014 vd->v_psize = asize; 2015 } 2016 } 2017 nvlist_destroy(tmp); 2018 } 2019 2020 if (best_txg == 0) { 2021 nvlist_destroy(nvl); 2022 nvl = NULL; 2023 } 2024 done: 2025 free(label); 2026 return (nvl); 2027 } 2028 2029 static void 2030 vdev_uberblock_load(vdev_t *vd, uberblock_t *ub) 2031 { 2032 uberblock_t *buf; 2033 2034 buf = malloc(VDEV_UBERBLOCK_SIZE(vd)); 2035 if (buf == NULL) 2036 return; 2037 2038 for (int l = 0; l < VDEV_LABELS; l++) { 2039 for (int n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) { 2040 if (vdev_label_read(vd, l, buf, 2041 VDEV_UBERBLOCK_OFFSET(vd, n), 2042 VDEV_UBERBLOCK_SIZE(vd))) 2043 continue; 2044 if (uberblock_verify(buf) != 0) 2045 continue; 2046 2047 if (vdev_uberblock_compare(buf, ub) > 0) 2048 *ub = *buf; 2049 } 2050 } 2051 free(buf); 2052 } 2053 2054 static int 2055 vdev_probe(vdev_phys_read_t *_read, vdev_phys_write_t *_write, void *priv, 2056 spa_t **spap) 2057 { 2058 vdev_t vtmp; 2059 spa_t *spa; 2060 vdev_t *vdev, *top; 2061 nvlist_t *nvl, *vdevs; 2062 uint64_t val; 2063 uint64_t guid, pool_guid, top_guid, txg; 2064 const char *pool_name; 2065 int rc, namelen; 2066 2067 /* 2068 * Load the vdev label and figure out which 2069 * uberblock is most current. 2070 */ 2071 memset(&vtmp, 0, sizeof(vtmp)); 2072 vtmp.v_phys_read = _read; 2073 vtmp.v_phys_write = _write; 2074 vtmp.v_priv = priv; 2075 vtmp.v_psize = P2ALIGN(ldi_get_size(priv), 2076 (uint64_t)sizeof (vdev_label_t)); 2077 2078 /* Test for minimum device size. */ 2079 if (vtmp.v_psize < SPA_MINDEVSIZE) 2080 return (EIO); 2081 2082 nvl = vdev_label_read_config(&vtmp, UINT64_MAX); 2083 if (nvl == NULL) 2084 return (EIO); 2085 2086 if (nvlist_find(nvl, ZPOOL_CONFIG_VERSION, DATA_TYPE_UINT64, 2087 NULL, &val, NULL) != 0) { 2088 nvlist_destroy(nvl); 2089 return (EIO); 2090 } 2091 2092 if (!SPA_VERSION_IS_SUPPORTED(val)) { 2093 printf("ZFS: unsupported ZFS version %u (should be %u)\n", 2094 (unsigned)val, (unsigned)SPA_VERSION); 2095 nvlist_destroy(nvl); 2096 return (EIO); 2097 } 2098 2099 /* Check ZFS features for read */ 2100 rc = nvlist_check_features_for_read(nvl); 2101 if (rc != 0) { 2102 nvlist_destroy(nvl); 2103 return (EIO); 2104 } 2105 2106 if (nvlist_find(nvl, ZPOOL_CONFIG_POOL_STATE, DATA_TYPE_UINT64, 2107 NULL, &val, NULL) != 0) { 2108 nvlist_destroy(nvl); 2109 return (EIO); 2110 } 2111 2112 if (val == POOL_STATE_DESTROYED) { 2113 /* We don't boot only from destroyed pools. */ 2114 nvlist_destroy(nvl); 2115 return (EIO); 2116 } 2117 2118 if (nvlist_find(nvl, ZPOOL_CONFIG_POOL_TXG, DATA_TYPE_UINT64, 2119 NULL, &txg, NULL) != 0 || 2120 txg == 0 || 2121 nvlist_find(nvl, ZPOOL_CONFIG_TOP_GUID, DATA_TYPE_UINT64, 2122 NULL, &top_guid, NULL) != 0 || 2123 nvlist_find(nvl, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64, 2124 NULL, &pool_guid, NULL) != 0 || 2125 nvlist_find(nvl, ZPOOL_CONFIG_POOL_NAME, DATA_TYPE_STRING, 2126 NULL, &pool_name, &namelen) != 0 || 2127 nvlist_find(nvl, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64, 2128 NULL, &guid, NULL) != 0) { 2129 /* 2130 * Cache, spare and replaced devices end up here - just ignore 2131 * them. 2132 */ 2133 nvlist_destroy(nvl); 2134 return (EIO); 2135 } 2136 2137 /* 2138 * Create the pool if this is the first time we've seen it. 2139 */ 2140 spa = spa_find_by_guid(pool_guid); 2141 if (spa == NULL) { 2142 char *name; 2143 2144 name = malloc(namelen + 1); 2145 if (name == NULL) { 2146 nvlist_destroy(nvl); 2147 return (ENOMEM); 2148 } 2149 bcopy(pool_name, name, namelen); 2150 name[namelen] = '\0'; 2151 spa = spa_create(pool_guid, name); 2152 free(name); 2153 if (spa == NULL) { 2154 nvlist_destroy(nvl); 2155 return (ENOMEM); 2156 } 2157 } 2158 2159 /* 2160 * Check if configuration is already known. If configuration is known 2161 * and txg numbers don't match, we got 2x2 scenarios here. First, is 2162 * the label being read right now _newer_ than the one read before. 2163 * Second, is the vdev that provided the stale label _present_ in the 2164 * newer configuration. If neither is true, we completely ignore the 2165 * label. 2166 */ 2167 STAILQ_FOREACH(top, &spa->spa_root_vdev->v_children, v_childlink) 2168 if (top->v_guid == top_guid) { 2169 bool newer, present; 2170 2171 if (top->v_txg == txg) 2172 break; 2173 newer = (top->v_txg < txg); 2174 present = newer ? 2175 nvlist_find_vdev_guid(nvl, top->v_label) : 2176 (vdev_find(&top->v_children, guid) != NULL); 2177 printf("ZFS: pool %s vdev %s %s stale label from " 2178 "0x%jx@0x%jx, %s 0x%jx@0x%jx\n", 2179 spa->spa_name, top->v_name, 2180 present ? "using" : "ignoring", 2181 newer ? top->v_label : guid, 2182 newer ? top->v_txg : txg, 2183 present ? "referred by" : "using", 2184 newer ? guid : top->v_label, 2185 newer ? txg : top->v_txg); 2186 if (newer) { 2187 STAILQ_REMOVE(&spa->spa_root_vdev->v_children, 2188 top, vdev, v_childlink); 2189 vdev_free(top); 2190 break; 2191 } else if (present) { 2192 break; 2193 } else { 2194 nvlist_destroy(nvl); 2195 return (EIO); 2196 } 2197 } 2198 2199 /* 2200 * Get the vdev tree and create our in-core copy of it. 2201 * If we already have a vdev with this guid, this must 2202 * be some kind of alias (overlapping slices, dangerously dedicated 2203 * disks etc). 2204 */ 2205 vdev = vdev_find(&spa->spa_root_vdev->v_children, guid); 2206 /* Has this vdev already been inited? */ 2207 if (vdev && vdev->v_phys_read) { 2208 nvlist_destroy(nvl); 2209 return (EIO); 2210 } 2211 2212 if (nvlist_find(nvl, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST, NULL, 2213 &vdevs, NULL)) { 2214 printf("ZFS: can't find vdev details\n"); 2215 nvlist_destroy(nvl); 2216 return (ENOENT); 2217 } 2218 2219 rc = vdev_from_nvlist(spa, top_guid, guid, txg, vdevs); 2220 nvlist_destroy(vdevs); 2221 nvlist_destroy(nvl); 2222 if (rc != 0) 2223 return (rc); 2224 2225 /* 2226 * We should already have created an incomplete vdev for this 2227 * vdev. Find it and initialise it with our read proc. 2228 */ 2229 vdev = vdev_find(&spa->spa_root_vdev->v_children, guid); 2230 if (vdev != NULL) { 2231 vdev->v_phys_read = _read; 2232 vdev->v_phys_write = _write; 2233 vdev->v_priv = priv; 2234 vdev->v_psize = vtmp.v_psize; 2235 /* 2236 * If no other state is set, mark vdev healthy. 2237 */ 2238 if (vdev->v_state == VDEV_STATE_UNKNOWN) 2239 vdev->v_state = VDEV_STATE_HEALTHY; 2240 } else { 2241 printf("ZFS: inconsistent nvlist contents\n"); 2242 return (EIO); 2243 } 2244 2245 if (vdev->v_islog) 2246 spa->spa_with_log = vdev->v_islog; 2247 2248 /* 2249 * Re-evaluate top-level vdev state. 2250 */ 2251 vdev_set_state(vdev->v_top); 2252 2253 /* 2254 * Ok, we are happy with the pool so far. Lets find 2255 * the best uberblock and then we can actually access 2256 * the contents of the pool. 2257 */ 2258 vdev_uberblock_load(vdev, spa->spa_uberblock); 2259 2260 if (spap != NULL) 2261 *spap = spa; 2262 return (0); 2263 } 2264 2265 static int 2266 ilog2(int n) 2267 { 2268 int v; 2269 2270 for (v = 0; v < 32; v++) 2271 if (n == (1 << v)) 2272 return (v); 2273 return (-1); 2274 } 2275 2276 static inline uint64_t 2277 gbh_nblkptrs(uint64_t size) 2278 { 2279 ASSERT(IS_P2ALIGNED(size, sizeof(blkptr_t))); 2280 return ((size - sizeof(zio_eck_t)) / sizeof(blkptr_t)); 2281 } 2282 2283 static int 2284 zio_read_gang(const spa_t *spa, const blkptr_t *bp, void *buf) 2285 { 2286 blkptr_t gbh_bp; 2287 void *gbuf; 2288 char *pbuf; 2289 uint64_t gangblocksize; 2290 int err, i; 2291 2292 gangblocksize = UINT64_MAX; 2293 for (int dva = 0; dva < BP_GET_NDVAS(bp); dva++) { 2294 vdev_t *vd = vdev_lookup_top(spa, 2295 DVA_GET_VDEV(&bp->blk_dva[dva])); 2296 gangblocksize = MIN(gangblocksize, 1ULL << vd->v_ashift); 2297 } 2298 2299 /* Artificial BP for gang block header. */ 2300 gbh_bp = *bp; 2301 BP_SET_PSIZE(&gbh_bp, gangblocksize); 2302 BP_SET_LSIZE(&gbh_bp, gangblocksize); 2303 BP_SET_CHECKSUM(&gbh_bp, ZIO_CHECKSUM_GANG_HEADER); 2304 BP_SET_COMPRESS(&gbh_bp, ZIO_COMPRESS_OFF); 2305 for (i = 0; i < SPA_DVAS_PER_BP; i++) 2306 DVA_SET_GANG(&gbh_bp.blk_dva[i], 0); 2307 2308 gbuf = malloc(gangblocksize); 2309 if (gbuf == NULL) 2310 return (ENOMEM); 2311 /* Read gang header block using the artificial BP. */ 2312 err = zio_read_impl(spa, &gbh_bp, gbuf, false); 2313 if ((err == EIO || err == ECKSUM) && 2314 gangblocksize > SPA_OLD_GANGBLOCKSIZE) { 2315 /* This might be a legacy gang block header, try again. */ 2316 gangblocksize = SPA_OLD_GANGBLOCKSIZE; 2317 BP_SET_PSIZE(&gbh_bp, gangblocksize); 2318 BP_SET_LSIZE(&gbh_bp, gangblocksize); 2319 err = zio_read(spa, &gbh_bp, gbuf); 2320 } 2321 if (err != 0) { 2322 free(gbuf); 2323 return (EIO); 2324 } 2325 2326 pbuf = buf; 2327 for (i = 0; i < gbh_nblkptrs(gangblocksize); i++) { 2328 blkptr_t *gbp = &((blkptr_t *)gbuf)[i]; 2329 2330 if (BP_IS_HOLE(gbp)) 2331 continue; 2332 if (zio_read(spa, gbp, pbuf)) { 2333 free(gbuf); 2334 return (EIO); 2335 } 2336 pbuf += BP_GET_PSIZE(gbp); 2337 } 2338 2339 free(gbuf); 2340 if (zio_checksum_verify(spa, bp, buf)) 2341 return (EIO); 2342 return (0); 2343 } 2344 2345 static int 2346 zio_read_impl(const spa_t *spa, const blkptr_t *bp, void *buf, bool print) 2347 { 2348 int cpfunc = BP_GET_COMPRESS(bp); 2349 uint64_t align, size; 2350 void *pbuf; 2351 int i, error; 2352 2353 /* 2354 * Process data embedded in block pointer 2355 */ 2356 if (BP_IS_EMBEDDED(bp)) { 2357 ASSERT(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA); 2358 2359 size = BPE_GET_PSIZE(bp); 2360 ASSERT(size <= BPE_PAYLOAD_SIZE); 2361 2362 if (cpfunc != ZIO_COMPRESS_OFF) 2363 pbuf = malloc(size); 2364 else 2365 pbuf = buf; 2366 2367 if (pbuf == NULL) 2368 return (ENOMEM); 2369 2370 decode_embedded_bp_compressed(bp, pbuf); 2371 error = 0; 2372 2373 if (cpfunc != ZIO_COMPRESS_OFF) { 2374 error = zio_decompress_data(cpfunc, pbuf, 2375 size, buf, BP_GET_LSIZE(bp)); 2376 free(pbuf); 2377 } 2378 if (error != 0 && print) 2379 printf("ZFS: i/o error - unable to decompress " 2380 "block pointer data, error %d\n", error); 2381 return (error); 2382 } 2383 2384 error = EIO; 2385 2386 for (i = 0; i < SPA_DVAS_PER_BP; i++) { 2387 const dva_t *dva = &bp->blk_dva[i]; 2388 vdev_t *vdev; 2389 vdev_list_t *vlist; 2390 uint64_t vdevid; 2391 off_t offset; 2392 2393 if (!dva->dva_word[0] && !dva->dva_word[1]) 2394 continue; 2395 2396 vdevid = DVA_GET_VDEV(dva); 2397 offset = DVA_GET_OFFSET(dva); 2398 vlist = &spa->spa_root_vdev->v_children; 2399 STAILQ_FOREACH(vdev, vlist, v_childlink) { 2400 if (vdev->v_id == vdevid) 2401 break; 2402 } 2403 if (!vdev || !vdev->v_read) 2404 continue; 2405 2406 size = BP_GET_PSIZE(bp); 2407 if (vdev->v_read == vdev_raidz_read) { 2408 align = 1ULL << vdev->v_ashift; 2409 if (P2PHASE(size, align) != 0) 2410 size = P2ROUNDUP(size, align); 2411 } 2412 if (size != BP_GET_PSIZE(bp) || cpfunc != ZIO_COMPRESS_OFF) 2413 pbuf = malloc(size); 2414 else 2415 pbuf = buf; 2416 2417 if (pbuf == NULL) { 2418 error = ENOMEM; 2419 break; 2420 } 2421 2422 if (DVA_GET_GANG(dva)) 2423 error = zio_read_gang(spa, bp, pbuf); 2424 else 2425 error = vdev->v_read(vdev, bp, pbuf, offset, size); 2426 if (error == 0) { 2427 if (cpfunc != ZIO_COMPRESS_OFF) 2428 error = zio_decompress_data(cpfunc, pbuf, 2429 BP_GET_PSIZE(bp), buf, BP_GET_LSIZE(bp)); 2430 else if (size != BP_GET_PSIZE(bp)) 2431 bcopy(pbuf, buf, BP_GET_PSIZE(bp)); 2432 } else if (print) { 2433 printf("zio_read error: %d\n", error); 2434 } 2435 if (buf != pbuf) 2436 free(pbuf); 2437 if (error == 0) 2438 break; 2439 } 2440 if (error != 0 && print) 2441 printf("ZFS: i/o error - all block copies unavailable\n"); 2442 2443 return (error); 2444 } 2445 2446 static int 2447 zio_read(const spa_t *spa, const blkptr_t *bp, void *buf) 2448 { 2449 return (zio_read_impl(spa, bp, buf, true)); 2450 } 2451 2452 static int 2453 dnode_read(const spa_t *spa, const dnode_phys_t *dnode, off_t offset, 2454 void *buf, size_t buflen) 2455 { 2456 int ibshift = dnode->dn_indblkshift - SPA_BLKPTRSHIFT; 2457 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT; 2458 int nlevels = dnode->dn_nlevels; 2459 int i, rc; 2460 2461 if (bsize > SPA_MAXBLOCKSIZE) { 2462 printf("ZFS: I/O error - blocks larger than %llu are not " 2463 "supported\n", SPA_MAXBLOCKSIZE); 2464 return (EIO); 2465 } 2466 2467 /* 2468 * Handle odd block sizes, mirrors dmu_read_impl(). Data can't exist 2469 * past the first block, so we'll clip the read to the portion of the 2470 * buffer within bsize and zero out the remainder. 2471 */ 2472 if (dnode->dn_maxblkid == 0) { 2473 size_t newbuflen; 2474 2475 newbuflen = offset > bsize ? 0 : MIN(buflen, bsize - offset); 2476 bzero((char *)buf + newbuflen, buflen - newbuflen); 2477 buflen = newbuflen; 2478 } 2479 2480 /* 2481 * Note: bsize may not be a power of two here so we need to do an 2482 * actual divide rather than a bitshift. 2483 */ 2484 while (buflen > 0) { 2485 uint64_t bn = offset / bsize; 2486 int boff = offset % bsize; 2487 int ibn; 2488 const blkptr_t *indbp; 2489 blkptr_t bp; 2490 2491 if (bn > dnode->dn_maxblkid) 2492 return (EIO); 2493 2494 if (dnode == dnode_cache_obj && bn == dnode_cache_bn) 2495 goto cached; 2496 2497 indbp = dnode->dn_blkptr; 2498 for (i = 0; i < nlevels; i++) { 2499 /* 2500 * Copy the bp from the indirect array so that 2501 * we can re-use the scratch buffer for multi-level 2502 * objects. 2503 */ 2504 ibn = bn >> ((nlevels - i - 1) * ibshift); 2505 ibn &= ((1 << ibshift) - 1); 2506 bp = indbp[ibn]; 2507 if (BP_IS_HOLE(&bp)) { 2508 memset(dnode_cache_buf, 0, bsize); 2509 break; 2510 } 2511 rc = zio_read(spa, &bp, dnode_cache_buf); 2512 if (rc) 2513 return (rc); 2514 indbp = (const blkptr_t *) dnode_cache_buf; 2515 } 2516 dnode_cache_obj = dnode; 2517 dnode_cache_bn = bn; 2518 cached: 2519 2520 /* 2521 * The buffer contains our data block. Copy what we 2522 * need from it and loop. 2523 */ 2524 i = bsize - boff; 2525 if (i > buflen) i = buflen; 2526 memcpy(buf, &dnode_cache_buf[boff], i); 2527 buf = ((char *)buf) + i; 2528 offset += i; 2529 buflen -= i; 2530 } 2531 2532 return (0); 2533 } 2534 2535 /* 2536 * Lookup a value in a microzap directory. 2537 */ 2538 static int 2539 mzap_lookup(const mzap_phys_t *mz, size_t size, const char *name, 2540 uint64_t *value) 2541 { 2542 const mzap_ent_phys_t *mze; 2543 int chunks, i; 2544 2545 /* 2546 * Microzap objects use exactly one block. Read the whole 2547 * thing. 2548 */ 2549 chunks = size / MZAP_ENT_LEN - 1; 2550 for (i = 0; i < chunks; i++) { 2551 mze = &mz->mz_chunk[i]; 2552 if (strcmp(mze->mze_name, name) == 0) { 2553 *value = mze->mze_value; 2554 return (0); 2555 } 2556 } 2557 2558 return (ENOENT); 2559 } 2560 2561 /* 2562 * Compare a name with a zap leaf entry. Return non-zero if the name 2563 * matches. 2564 */ 2565 static int 2566 fzap_name_equal(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, 2567 const char *name) 2568 { 2569 size_t namelen; 2570 const zap_leaf_chunk_t *nc; 2571 const char *p; 2572 2573 namelen = zc->l_entry.le_name_numints; 2574 2575 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk); 2576 p = name; 2577 while (namelen > 0) { 2578 size_t len; 2579 2580 len = namelen; 2581 if (len > ZAP_LEAF_ARRAY_BYTES) 2582 len = ZAP_LEAF_ARRAY_BYTES; 2583 if (memcmp(p, nc->l_array.la_array, len)) 2584 return (0); 2585 p += len; 2586 namelen -= len; 2587 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next); 2588 } 2589 2590 return (1); 2591 } 2592 2593 /* 2594 * Extract a uint64_t value from a zap leaf entry. 2595 */ 2596 static uint64_t 2597 fzap_leaf_value(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc) 2598 { 2599 const zap_leaf_chunk_t *vc; 2600 int i; 2601 uint64_t value; 2602 const uint8_t *p; 2603 2604 vc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_value_chunk); 2605 for (i = 0, value = 0, p = vc->l_array.la_array; i < 8; i++) { 2606 value = (value << 8) | p[i]; 2607 } 2608 2609 return (value); 2610 } 2611 2612 static void 2613 stv(int len, void *addr, uint64_t value) 2614 { 2615 switch (len) { 2616 case 1: 2617 *(uint8_t *)addr = value; 2618 return; 2619 case 2: 2620 *(uint16_t *)addr = value; 2621 return; 2622 case 4: 2623 *(uint32_t *)addr = value; 2624 return; 2625 case 8: 2626 *(uint64_t *)addr = value; 2627 return; 2628 } 2629 } 2630 2631 /* 2632 * Extract a array from a zap leaf entry. 2633 */ 2634 static void 2635 fzap_leaf_array(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, 2636 uint64_t integer_size, uint64_t num_integers, void *buf) 2637 { 2638 uint64_t array_int_len = zc->l_entry.le_value_intlen; 2639 uint64_t value = 0; 2640 uint64_t *u64 = buf; 2641 char *p = buf; 2642 int len = MIN(zc->l_entry.le_value_numints, num_integers); 2643 int chunk = zc->l_entry.le_value_chunk; 2644 int byten = 0; 2645 2646 if (integer_size == 8 && len == 1) { 2647 *u64 = fzap_leaf_value(zl, zc); 2648 return; 2649 } 2650 2651 while (len > 0) { 2652 struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(zl, chunk).l_array; 2653 int i; 2654 2655 ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(zl)); 2656 for (i = 0; i < ZAP_LEAF_ARRAY_BYTES && len > 0; i++) { 2657 value = (value << 8) | la->la_array[i]; 2658 byten++; 2659 if (byten == array_int_len) { 2660 stv(integer_size, p, value); 2661 byten = 0; 2662 len--; 2663 if (len == 0) 2664 return; 2665 p += integer_size; 2666 } 2667 } 2668 chunk = la->la_next; 2669 } 2670 } 2671 2672 static int 2673 fzap_check_size(uint64_t integer_size, uint64_t num_integers) 2674 { 2675 2676 switch (integer_size) { 2677 case 1: 2678 case 2: 2679 case 4: 2680 case 8: 2681 break; 2682 default: 2683 return (EINVAL); 2684 } 2685 2686 if (integer_size * num_integers > ZAP_MAXVALUELEN) 2687 return (E2BIG); 2688 2689 return (0); 2690 } 2691 2692 static void 2693 zap_leaf_free(zap_leaf_t *leaf) 2694 { 2695 free(leaf->l_phys); 2696 free(leaf); 2697 } 2698 2699 static int 2700 zap_get_leaf_byblk(fat_zap_t *zap, uint64_t blk, zap_leaf_t **lp) 2701 { 2702 int bs = FZAP_BLOCK_SHIFT(zap); 2703 int err; 2704 2705 *lp = malloc(sizeof(**lp)); 2706 if (*lp == NULL) 2707 return (ENOMEM); 2708 2709 (*lp)->l_bs = bs; 2710 (*lp)->l_phys = malloc(1 << bs); 2711 2712 if ((*lp)->l_phys == NULL) { 2713 free(*lp); 2714 return (ENOMEM); 2715 } 2716 err = dnode_read(zap->zap_spa, zap->zap_dnode, blk << bs, (*lp)->l_phys, 2717 1 << bs); 2718 if (err != 0) { 2719 zap_leaf_free(*lp); 2720 } 2721 return (err); 2722 } 2723 2724 static int 2725 zap_table_load(fat_zap_t *zap, zap_table_phys_t *tbl, uint64_t idx, 2726 uint64_t *valp) 2727 { 2728 int bs = FZAP_BLOCK_SHIFT(zap); 2729 uint64_t blk = idx >> (bs - 3); 2730 uint64_t off = idx & ((1 << (bs - 3)) - 1); 2731 uint64_t *buf; 2732 int rc; 2733 2734 buf = malloc(1 << zap->zap_block_shift); 2735 if (buf == NULL) 2736 return (ENOMEM); 2737 rc = dnode_read(zap->zap_spa, zap->zap_dnode, (tbl->zt_blk + blk) << bs, 2738 buf, 1 << zap->zap_block_shift); 2739 if (rc == 0) 2740 *valp = buf[off]; 2741 free(buf); 2742 return (rc); 2743 } 2744 2745 static int 2746 zap_idx_to_blk(fat_zap_t *zap, uint64_t idx, uint64_t *valp) 2747 { 2748 if (zap->zap_phys->zap_ptrtbl.zt_numblks == 0) { 2749 *valp = ZAP_EMBEDDED_PTRTBL_ENT(zap, idx); 2750 return (0); 2751 } else { 2752 return (zap_table_load(zap, &zap->zap_phys->zap_ptrtbl, 2753 idx, valp)); 2754 } 2755 } 2756 2757 #define ZAP_HASH_IDX(hash, n) (((n) == 0) ? 0 : ((hash) >> (64 - (n)))) 2758 static int 2759 zap_deref_leaf(fat_zap_t *zap, uint64_t h, zap_leaf_t **lp) 2760 { 2761 uint64_t idx, blk; 2762 int err; 2763 2764 idx = ZAP_HASH_IDX(h, zap->zap_phys->zap_ptrtbl.zt_shift); 2765 err = zap_idx_to_blk(zap, idx, &blk); 2766 if (err != 0) 2767 return (err); 2768 return (zap_get_leaf_byblk(zap, blk, lp)); 2769 } 2770 2771 #define CHAIN_END 0xffff /* end of the chunk chain */ 2772 #define LEAF_HASH(l, h) \ 2773 ((ZAP_LEAF_HASH_NUMENTRIES(l)-1) & \ 2774 ((h) >> \ 2775 (64 - ZAP_LEAF_HASH_SHIFT(l) - (l)->l_phys->l_hdr.lh_prefix_len))) 2776 #define LEAF_HASH_ENTPTR(l, h) (&(l)->l_phys->l_hash[LEAF_HASH(l, h)]) 2777 2778 static int 2779 zap_leaf_lookup(zap_leaf_t *zl, uint64_t hash, const char *name, 2780 uint64_t integer_size, uint64_t num_integers, void *value) 2781 { 2782 int rc; 2783 uint16_t *chunkp; 2784 struct zap_leaf_entry *le; 2785 2786 /* 2787 * Make sure this chunk matches our hash. 2788 */ 2789 if (zl->l_phys->l_hdr.lh_prefix_len > 0 && 2790 zl->l_phys->l_hdr.lh_prefix != 2791 hash >> (64 - zl->l_phys->l_hdr.lh_prefix_len)) 2792 return (EIO); 2793 2794 rc = ENOENT; 2795 for (chunkp = LEAF_HASH_ENTPTR(zl, hash); 2796 *chunkp != CHAIN_END; chunkp = &le->le_next) { 2797 zap_leaf_chunk_t *zc; 2798 uint16_t chunk = *chunkp; 2799 2800 le = ZAP_LEAF_ENTRY(zl, chunk); 2801 if (le->le_hash != hash) 2802 continue; 2803 zc = &ZAP_LEAF_CHUNK(zl, chunk); 2804 if (fzap_name_equal(zl, zc, name)) { 2805 if (zc->l_entry.le_value_intlen > integer_size) { 2806 rc = EINVAL; 2807 } else { 2808 fzap_leaf_array(zl, zc, integer_size, 2809 num_integers, value); 2810 rc = 0; 2811 } 2812 break; 2813 } 2814 } 2815 return (rc); 2816 } 2817 2818 /* 2819 * Lookup a value in a fatzap directory. 2820 */ 2821 static int 2822 fzap_lookup(const spa_t *spa, const dnode_phys_t *dnode, zap_phys_t *zh, 2823 const char *name, uint64_t integer_size, uint64_t num_integers, 2824 void *value) 2825 { 2826 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT; 2827 fat_zap_t z; 2828 zap_leaf_t *zl; 2829 uint64_t hash; 2830 int rc; 2831 2832 if (zh->zap_magic != ZAP_MAGIC) 2833 return (EIO); 2834 2835 if ((rc = fzap_check_size(integer_size, num_integers)) != 0) { 2836 return (rc); 2837 } 2838 2839 z.zap_block_shift = ilog2(bsize); 2840 z.zap_phys = zh; 2841 z.zap_spa = spa; 2842 z.zap_dnode = dnode; 2843 2844 hash = zap_hash(zh->zap_salt, name); 2845 rc = zap_deref_leaf(&z, hash, &zl); 2846 if (rc != 0) 2847 return (rc); 2848 2849 rc = zap_leaf_lookup(zl, hash, name, integer_size, num_integers, value); 2850 2851 zap_leaf_free(zl); 2852 return (rc); 2853 } 2854 2855 /* 2856 * Lookup a name in a zap object and return its value as a uint64_t. 2857 */ 2858 static int 2859 zap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name, 2860 uint64_t integer_size, uint64_t num_integers, void *value) 2861 { 2862 int rc; 2863 zap_phys_t *zap; 2864 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT; 2865 2866 zap = malloc(size); 2867 if (zap == NULL) 2868 return (ENOMEM); 2869 2870 rc = dnode_read(spa, dnode, 0, zap, size); 2871 if (rc) 2872 goto done; 2873 2874 switch (zap->zap_block_type) { 2875 case ZBT_MICRO: 2876 rc = mzap_lookup((const mzap_phys_t *)zap, size, name, value); 2877 break; 2878 case ZBT_HEADER: 2879 rc = fzap_lookup(spa, dnode, zap, name, integer_size, 2880 num_integers, value); 2881 break; 2882 default: 2883 printf("ZFS: invalid zap_type=%" PRIx64 "\n", 2884 zap->zap_block_type); 2885 rc = EIO; 2886 } 2887 done: 2888 free(zap); 2889 return (rc); 2890 } 2891 2892 /* 2893 * List a microzap directory. 2894 */ 2895 static int 2896 mzap_list(const mzap_phys_t *mz, size_t size, 2897 int (*callback)(const char *, uint64_t)) 2898 { 2899 const mzap_ent_phys_t *mze; 2900 int chunks, i, rc; 2901 2902 /* 2903 * Microzap objects use exactly one block. Read the whole 2904 * thing. 2905 */ 2906 rc = 0; 2907 chunks = size / MZAP_ENT_LEN - 1; 2908 for (i = 0; i < chunks; i++) { 2909 mze = &mz->mz_chunk[i]; 2910 if (mze->mze_name[0]) { 2911 rc = callback(mze->mze_name, mze->mze_value); 2912 if (rc != 0) 2913 break; 2914 } 2915 } 2916 2917 return (rc); 2918 } 2919 2920 /* 2921 * List a fatzap directory. 2922 */ 2923 static int 2924 fzap_list(const spa_t *spa, const dnode_phys_t *dnode, zap_phys_t *zh, 2925 int (*callback)(const char *, uint64_t)) 2926 { 2927 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT; 2928 fat_zap_t z; 2929 uint64_t i; 2930 int j, rc; 2931 2932 if (zh->zap_magic != ZAP_MAGIC) 2933 return (EIO); 2934 2935 z.zap_block_shift = ilog2(bsize); 2936 z.zap_phys = zh; 2937 2938 /* 2939 * This assumes that the leaf blocks start at block 1. The 2940 * documentation isn't exactly clear on this. 2941 */ 2942 zap_leaf_t zl; 2943 zl.l_bs = z.zap_block_shift; 2944 zl.l_phys = malloc(bsize); 2945 if (zl.l_phys == NULL) 2946 return (ENOMEM); 2947 2948 for (i = 0; i < zh->zap_num_leafs; i++) { 2949 off_t off = ((off_t)(i + 1)) << zl.l_bs; 2950 char name[256], *p; 2951 uint64_t value; 2952 2953 if (dnode_read(spa, dnode, off, zl.l_phys, bsize)) { 2954 free(zl.l_phys); 2955 return (EIO); 2956 } 2957 2958 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) { 2959 zap_leaf_chunk_t *zc, *nc; 2960 int namelen; 2961 2962 zc = &ZAP_LEAF_CHUNK(&zl, j); 2963 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY) 2964 continue; 2965 namelen = zc->l_entry.le_name_numints; 2966 if (namelen > sizeof(name)) 2967 namelen = sizeof(name); 2968 2969 /* 2970 * Paste the name back together. 2971 */ 2972 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk); 2973 p = name; 2974 while (namelen > 0) { 2975 int len; 2976 len = namelen; 2977 if (len > ZAP_LEAF_ARRAY_BYTES) 2978 len = ZAP_LEAF_ARRAY_BYTES; 2979 memcpy(p, nc->l_array.la_array, len); 2980 p += len; 2981 namelen -= len; 2982 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next); 2983 } 2984 2985 /* 2986 * Assume the first eight bytes of the value are 2987 * a uint64_t. 2988 */ 2989 value = fzap_leaf_value(&zl, zc); 2990 2991 /* printf("%s 0x%jx\n", name, (uintmax_t)value); */ 2992 rc = callback((const char *)name, value); 2993 if (rc != 0) { 2994 free(zl.l_phys); 2995 return (rc); 2996 } 2997 } 2998 } 2999 3000 free(zl.l_phys); 3001 return (0); 3002 } 3003 3004 static int zfs_printf(const char *name, uint64_t value __unused) 3005 { 3006 3007 printf("%s\n", name); 3008 3009 return (0); 3010 } 3011 3012 /* 3013 * List a zap directory. 3014 */ 3015 static int 3016 zap_list(const spa_t *spa, const dnode_phys_t *dnode) 3017 { 3018 zap_phys_t *zap; 3019 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT; 3020 int rc; 3021 3022 zap = malloc(size); 3023 if (zap == NULL) 3024 return (ENOMEM); 3025 3026 rc = dnode_read(spa, dnode, 0, zap, size); 3027 if (rc == 0) { 3028 if (zap->zap_block_type == ZBT_MICRO) 3029 rc = mzap_list((const mzap_phys_t *)zap, size, 3030 zfs_printf); 3031 else 3032 rc = fzap_list(spa, dnode, zap, zfs_printf); 3033 } 3034 free(zap); 3035 return (rc); 3036 } 3037 3038 static int 3039 objset_get_dnode(const spa_t *spa, const objset_phys_t *os, uint64_t objnum, 3040 dnode_phys_t *dnode) 3041 { 3042 off_t offset; 3043 3044 offset = objnum * sizeof(dnode_phys_t); 3045 return dnode_read(spa, &os->os_meta_dnode, offset, 3046 dnode, sizeof(dnode_phys_t)); 3047 } 3048 3049 /* 3050 * Lookup a name in a microzap directory. 3051 */ 3052 static int 3053 mzap_rlookup(const mzap_phys_t *mz, size_t size, char *name, uint64_t value) 3054 { 3055 const mzap_ent_phys_t *mze; 3056 int chunks, i; 3057 3058 /* 3059 * Microzap objects use exactly one block. Read the whole 3060 * thing. 3061 */ 3062 chunks = size / MZAP_ENT_LEN - 1; 3063 for (i = 0; i < chunks; i++) { 3064 mze = &mz->mz_chunk[i]; 3065 if (value == mze->mze_value) { 3066 strcpy(name, mze->mze_name); 3067 return (0); 3068 } 3069 } 3070 3071 return (ENOENT); 3072 } 3073 3074 static void 3075 fzap_name_copy(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, char *name) 3076 { 3077 size_t namelen; 3078 const zap_leaf_chunk_t *nc; 3079 char *p; 3080 3081 namelen = zc->l_entry.le_name_numints; 3082 3083 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk); 3084 p = name; 3085 while (namelen > 0) { 3086 size_t len; 3087 len = namelen; 3088 if (len > ZAP_LEAF_ARRAY_BYTES) 3089 len = ZAP_LEAF_ARRAY_BYTES; 3090 memcpy(p, nc->l_array.la_array, len); 3091 p += len; 3092 namelen -= len; 3093 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next); 3094 } 3095 3096 *p = '\0'; 3097 } 3098 3099 static int 3100 fzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, zap_phys_t *zh, 3101 char *name, uint64_t value) 3102 { 3103 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT; 3104 fat_zap_t z; 3105 uint64_t i; 3106 int j, rc; 3107 3108 if (zh->zap_magic != ZAP_MAGIC) 3109 return (EIO); 3110 3111 z.zap_block_shift = ilog2(bsize); 3112 z.zap_phys = zh; 3113 3114 /* 3115 * This assumes that the leaf blocks start at block 1. The 3116 * documentation isn't exactly clear on this. 3117 */ 3118 zap_leaf_t zl; 3119 zl.l_bs = z.zap_block_shift; 3120 zl.l_phys = malloc(bsize); 3121 if (zl.l_phys == NULL) 3122 return (ENOMEM); 3123 3124 for (i = 0; i < zh->zap_num_leafs; i++) { 3125 off_t off = ((off_t)(i + 1)) << zl.l_bs; 3126 3127 rc = dnode_read(spa, dnode, off, zl.l_phys, bsize); 3128 if (rc != 0) 3129 goto done; 3130 3131 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) { 3132 zap_leaf_chunk_t *zc; 3133 3134 zc = &ZAP_LEAF_CHUNK(&zl, j); 3135 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY) 3136 continue; 3137 if (zc->l_entry.le_value_intlen != 8 || 3138 zc->l_entry.le_value_numints != 1) 3139 continue; 3140 3141 if (fzap_leaf_value(&zl, zc) == value) { 3142 fzap_name_copy(&zl, zc, name); 3143 goto done; 3144 } 3145 } 3146 } 3147 3148 rc = ENOENT; 3149 done: 3150 free(zl.l_phys); 3151 return (rc); 3152 } 3153 3154 static int 3155 zap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, 3156 uint64_t value) 3157 { 3158 zap_phys_t *zap; 3159 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT; 3160 int rc; 3161 3162 zap = malloc(size); 3163 if (zap == NULL) 3164 return (ENOMEM); 3165 3166 rc = dnode_read(spa, dnode, 0, zap, size); 3167 if (rc == 0) { 3168 if (zap->zap_block_type == ZBT_MICRO) 3169 rc = mzap_rlookup((const mzap_phys_t *)zap, size, 3170 name, value); 3171 else 3172 rc = fzap_rlookup(spa, dnode, zap, name, value); 3173 } 3174 free(zap); 3175 return (rc); 3176 } 3177 3178 static int 3179 zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result) 3180 { 3181 char name[256]; 3182 char component[256]; 3183 uint64_t dir_obj, parent_obj, child_dir_zapobj; 3184 dnode_phys_t child_dir_zap, snapnames_zap, dataset, dir, parent; 3185 dsl_dir_phys_t *dd; 3186 dsl_dataset_phys_t *ds; 3187 char *p; 3188 int len; 3189 boolean_t issnap = B_FALSE; 3190 3191 p = &name[sizeof(name) - 1]; 3192 *p = '\0'; 3193 3194 if (objset_get_dnode(spa, spa->spa_mos, objnum, &dataset)) { 3195 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum); 3196 return (EIO); 3197 } 3198 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus; 3199 dir_obj = ds->ds_dir_obj; 3200 if (ds->ds_snapnames_zapobj == 0) 3201 issnap = B_TRUE; 3202 3203 for (;;) { 3204 if (objset_get_dnode(spa, spa->spa_mos, dir_obj, &dir) != 0) 3205 return (EIO); 3206 dd = (dsl_dir_phys_t *)&dir.dn_bonus; 3207 3208 /* Actual loop condition. */ 3209 parent_obj = dd->dd_parent_obj; 3210 if (parent_obj == 0) 3211 break; 3212 3213 if (objset_get_dnode(spa, spa->spa_mos, parent_obj, 3214 &parent) != 0) 3215 return (EIO); 3216 dd = (dsl_dir_phys_t *)&parent.dn_bonus; 3217 if (issnap == B_TRUE) { 3218 /* 3219 * The dataset we are looking up is a snapshot 3220 * the dir_obj is the parent already, we don't want 3221 * the grandparent just yet. Reset to the parent. 3222 */ 3223 dd = (dsl_dir_phys_t *)&dir.dn_bonus; 3224 /* Lookup the dataset to get the snapname ZAP */ 3225 if (objset_get_dnode(spa, spa->spa_mos, 3226 dd->dd_head_dataset_obj, &dataset)) 3227 return (EIO); 3228 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus; 3229 if (objset_get_dnode(spa, spa->spa_mos, 3230 ds->ds_snapnames_zapobj, &snapnames_zap) != 0) 3231 return (EIO); 3232 /* Get the name of the snapshot */ 3233 if (zap_rlookup(spa, &snapnames_zap, component, 3234 objnum) != 0) 3235 return (EIO); 3236 len = strlen(component); 3237 p -= len; 3238 memcpy(p, component, len); 3239 --p; 3240 *p = '@'; 3241 issnap = B_FALSE; 3242 continue; 3243 } 3244 3245 child_dir_zapobj = dd->dd_child_dir_zapobj; 3246 if (objset_get_dnode(spa, spa->spa_mos, child_dir_zapobj, 3247 &child_dir_zap) != 0) 3248 return (EIO); 3249 if (zap_rlookup(spa, &child_dir_zap, component, dir_obj) != 0) 3250 return (EIO); 3251 3252 len = strlen(component); 3253 p -= len; 3254 memcpy(p, component, len); 3255 --p; 3256 *p = '/'; 3257 3258 /* Actual loop iteration. */ 3259 dir_obj = parent_obj; 3260 } 3261 3262 if (*p != '\0') 3263 ++p; 3264 strcpy(result, p); 3265 3266 return (0); 3267 } 3268 3269 static int 3270 zfs_lookup_dataset(const spa_t *spa, const char *name, uint64_t *objnum) 3271 { 3272 char element[256]; 3273 uint64_t dir_obj, child_dir_zapobj; 3274 dnode_phys_t child_dir_zap, snapnames_zap, dir, dataset; 3275 dsl_dir_phys_t *dd; 3276 dsl_dataset_phys_t *ds; 3277 const char *p, *q; 3278 boolean_t issnap = B_FALSE; 3279 3280 if (objset_get_dnode(spa, spa->spa_mos, 3281 DMU_POOL_DIRECTORY_OBJECT, &dir)) 3282 return (EIO); 3283 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, sizeof (dir_obj), 3284 1, &dir_obj)) 3285 return (EIO); 3286 3287 p = name; 3288 for (;;) { 3289 if (objset_get_dnode(spa, spa->spa_mos, dir_obj, &dir)) 3290 return (EIO); 3291 dd = (dsl_dir_phys_t *)&dir.dn_bonus; 3292 3293 while (*p == '/') 3294 p++; 3295 /* Actual loop condition #1. */ 3296 if (*p == '\0') 3297 break; 3298 3299 q = strchr(p, '/'); 3300 if (q) { 3301 memcpy(element, p, q - p); 3302 element[q - p] = '\0'; 3303 p = q + 1; 3304 } else { 3305 strcpy(element, p); 3306 p += strlen(p); 3307 } 3308 3309 if (issnap == B_TRUE) { 3310 if (objset_get_dnode(spa, spa->spa_mos, 3311 dd->dd_head_dataset_obj, &dataset)) 3312 return (EIO); 3313 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus; 3314 if (objset_get_dnode(spa, spa->spa_mos, 3315 ds->ds_snapnames_zapobj, &snapnames_zap) != 0) 3316 return (EIO); 3317 /* Actual loop condition #2. */ 3318 if (zap_lookup(spa, &snapnames_zap, element, 3319 sizeof (dir_obj), 1, &dir_obj) != 0) 3320 return (ENOENT); 3321 *objnum = dir_obj; 3322 return (0); 3323 } else if ((q = strchr(element, '@')) != NULL) { 3324 issnap = B_TRUE; 3325 element[q - element] = '\0'; 3326 p = q + 1; 3327 } 3328 child_dir_zapobj = dd->dd_child_dir_zapobj; 3329 if (objset_get_dnode(spa, spa->spa_mos, child_dir_zapobj, 3330 &child_dir_zap) != 0) 3331 return (EIO); 3332 3333 /* Actual loop condition #2. */ 3334 if (zap_lookup(spa, &child_dir_zap, element, sizeof (dir_obj), 3335 1, &dir_obj) != 0) 3336 return (ENOENT); 3337 } 3338 3339 *objnum = dd->dd_head_dataset_obj; 3340 return (0); 3341 } 3342 3343 #ifndef BOOT2 3344 static int 3345 zfs_list_dataset(const spa_t *spa, uint64_t objnum/*, int pos, char *entry*/) 3346 { 3347 uint64_t dir_obj, child_dir_zapobj; 3348 dnode_phys_t child_dir_zap, dir, dataset; 3349 dsl_dataset_phys_t *ds; 3350 dsl_dir_phys_t *dd; 3351 3352 if (objset_get_dnode(spa, spa->spa_mos, objnum, &dataset)) { 3353 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum); 3354 return (EIO); 3355 } 3356 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus; 3357 dir_obj = ds->ds_dir_obj; 3358 3359 if (objset_get_dnode(spa, spa->spa_mos, dir_obj, &dir)) { 3360 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj); 3361 return (EIO); 3362 } 3363 dd = (dsl_dir_phys_t *)&dir.dn_bonus; 3364 3365 child_dir_zapobj = dd->dd_child_dir_zapobj; 3366 if (objset_get_dnode(spa, spa->spa_mos, child_dir_zapobj, 3367 &child_dir_zap) != 0) { 3368 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj); 3369 return (EIO); 3370 } 3371 3372 return (zap_list(spa, &child_dir_zap) != 0); 3373 } 3374 3375 int 3376 zfs_callback_dataset(const spa_t *spa, uint64_t objnum, 3377 int (*callback)(const char *, uint64_t)) 3378 { 3379 uint64_t dir_obj, child_dir_zapobj; 3380 dnode_phys_t child_dir_zap, dir, dataset; 3381 dsl_dataset_phys_t *ds; 3382 dsl_dir_phys_t *dd; 3383 zap_phys_t *zap; 3384 size_t size; 3385 int err; 3386 3387 err = objset_get_dnode(spa, spa->spa_mos, objnum, &dataset); 3388 if (err != 0) { 3389 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum); 3390 return (err); 3391 } 3392 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus; 3393 dir_obj = ds->ds_dir_obj; 3394 3395 err = objset_get_dnode(spa, spa->spa_mos, dir_obj, &dir); 3396 if (err != 0) { 3397 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj); 3398 return (err); 3399 } 3400 dd = (dsl_dir_phys_t *)&dir.dn_bonus; 3401 3402 child_dir_zapobj = dd->dd_child_dir_zapobj; 3403 err = objset_get_dnode(spa, spa->spa_mos, child_dir_zapobj, 3404 &child_dir_zap); 3405 if (err != 0) { 3406 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj); 3407 return (err); 3408 } 3409 3410 size = child_dir_zap.dn_datablkszsec << SPA_MINBLOCKSHIFT; 3411 zap = malloc(size); 3412 if (zap != NULL) { 3413 err = dnode_read(spa, &child_dir_zap, 0, zap, size); 3414 if (err != 0) 3415 goto done; 3416 3417 if (zap->zap_block_type == ZBT_MICRO) 3418 err = mzap_list((const mzap_phys_t *)zap, size, 3419 callback); 3420 else 3421 err = fzap_list(spa, &child_dir_zap, zap, callback); 3422 } else { 3423 err = ENOMEM; 3424 } 3425 done: 3426 free(zap); 3427 return (err); 3428 } 3429 #endif 3430 3431 /* 3432 * Find the object set given the object number of its dataset object 3433 * and return its details in *objset 3434 */ 3435 static int 3436 zfs_mount_dataset(const spa_t *spa, uint64_t objnum, objset_phys_t *objset, 3437 uint64_t *fsid_guid) 3438 { 3439 dnode_phys_t dataset; 3440 dsl_dataset_phys_t *ds; 3441 3442 if (objset_get_dnode(spa, spa->spa_mos, objnum, &dataset)) { 3443 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum); 3444 return (EIO); 3445 } 3446 3447 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus; 3448 if (zio_read(spa, &ds->ds_bp, objset)) { 3449 printf("ZFS: can't read object set for dataset %ju\n", 3450 (uintmax_t)objnum); 3451 return (EIO); 3452 } 3453 3454 /* 3455 * Capture the dataset's intrinsic on-disk identifier so callers can 3456 * expose it as st_dev (matches the kernel, which derives the fsid from 3457 * dmu_objset_fsid_guid()/ds_fsid_guid). ds_fsid_guid is a 56-bit ID 3458 * that may change to avoid collisions; ds_guid is a never-changing 3459 * 64-bit ID and would be the alternative if absolute stability over 3460 * time were required -- to be decided in review. 3461 */ 3462 if (fsid_guid != NULL) 3463 *fsid_guid = ds->ds_fsid_guid; 3464 3465 return (0); 3466 } 3467 3468 /* 3469 * Find the object set pointed to by the BOOTFS property or the root 3470 * dataset if there is none and return its details in *objset 3471 */ 3472 static int 3473 zfs_get_root(const spa_t *spa, uint64_t *objid) 3474 { 3475 dnode_phys_t dir, propdir; 3476 uint64_t props, bootfs, root; 3477 3478 *objid = 0; 3479 3480 /* 3481 * Start with the MOS directory object. 3482 */ 3483 if (objset_get_dnode(spa, spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir)) { 3484 printf("ZFS: can't read MOS object directory\n"); 3485 return (EIO); 3486 } 3487 3488 /* 3489 * Lookup the pool_props and see if we can find a bootfs. 3490 */ 3491 if (zap_lookup(spa, &dir, DMU_POOL_PROPS, sizeof(props), 1, &props) == 0 && 3492 objset_get_dnode(spa, spa->spa_mos, props, &propdir) == 0 && 3493 zap_lookup(spa, &propdir, "bootfs", sizeof(bootfs), 1, &bootfs) == 0 && 3494 bootfs != 0) { 3495 *objid = bootfs; 3496 return (0); 3497 } 3498 /* 3499 * Lookup the root dataset directory 3500 */ 3501 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, sizeof(root), 1, &root) || 3502 objset_get_dnode(spa, spa->spa_mos, root, &dir)) { 3503 printf("ZFS: can't find root dsl_dir\n"); 3504 return (EIO); 3505 } 3506 3507 /* 3508 * Use the information from the dataset directory's bonus buffer 3509 * to find the dataset object and from that the object set itself. 3510 */ 3511 dsl_dir_phys_t *dd = (dsl_dir_phys_t *)&dir.dn_bonus; 3512 *objid = dd->dd_head_dataset_obj; 3513 return (0); 3514 } 3515 3516 static int 3517 zfs_mount_impl(const spa_t *spa, uint64_t rootobj, struct zfsmount *mount) 3518 { 3519 3520 mount->spa = spa; 3521 3522 /* 3523 * Find the root object set if not explicitly provided 3524 */ 3525 if (rootobj == 0 && zfs_get_root(spa, &rootobj)) { 3526 printf("ZFS: can't find root filesystem\n"); 3527 return (EIO); 3528 } 3529 3530 if (zfs_mount_dataset(spa, rootobj, &mount->objset, 3531 &mount->fsid_guid)) { 3532 printf("ZFS: can't open root filesystem\n"); 3533 return (EIO); 3534 } 3535 3536 mount->rootobj = rootobj; 3537 3538 return (0); 3539 } 3540 3541 /* 3542 * callback function for feature name checks. 3543 */ 3544 static int 3545 check_feature(const char *name, uint64_t value) 3546 { 3547 int i; 3548 3549 if (value == 0) 3550 return (0); 3551 if (name[0] == '\0') 3552 return (0); 3553 3554 for (i = 0; features_for_read[i] != NULL; i++) { 3555 if (strcmp(name, features_for_read[i]) == 0) 3556 return (0); 3557 } 3558 printf("ZFS: unsupported feature: %s\n", name); 3559 return (EIO); 3560 } 3561 3562 /* 3563 * Checks whether the MOS features that are active are supported. 3564 */ 3565 static int 3566 check_mos_features(const spa_t *spa) 3567 { 3568 dnode_phys_t dir; 3569 zap_phys_t *zap; 3570 uint64_t objnum; 3571 size_t size; 3572 int rc; 3573 3574 if ((rc = objset_get_dnode(spa, spa->spa_mos, DMU_OT_OBJECT_DIRECTORY, 3575 &dir)) != 0) 3576 return (rc); 3577 if ((rc = zap_lookup(spa, &dir, DMU_POOL_FEATURES_FOR_READ, 3578 sizeof (objnum), 1, &objnum)) != 0) { 3579 /* 3580 * It is older pool without features. As we have already 3581 * tested the label, just return without raising the error. 3582 */ 3583 return (0); 3584 } 3585 3586 if ((rc = objset_get_dnode(spa, spa->spa_mos, objnum, &dir)) != 0) 3587 return (rc); 3588 3589 if (dir.dn_type != DMU_OTN_ZAP_METADATA) 3590 return (EIO); 3591 3592 size = dir.dn_datablkszsec << SPA_MINBLOCKSHIFT; 3593 zap = malloc(size); 3594 if (zap == NULL) 3595 return (ENOMEM); 3596 3597 if (dnode_read(spa, &dir, 0, zap, size)) { 3598 free(zap); 3599 return (EIO); 3600 } 3601 3602 if (zap->zap_block_type == ZBT_MICRO) 3603 rc = mzap_list((const mzap_phys_t *)zap, size, check_feature); 3604 else 3605 rc = fzap_list(spa, &dir, zap, check_feature); 3606 3607 free(zap); 3608 return (rc); 3609 } 3610 3611 static int 3612 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 3613 { 3614 dnode_phys_t dir; 3615 size_t size; 3616 int rc; 3617 char *nv; 3618 3619 *value = NULL; 3620 if ((rc = objset_get_dnode(spa, spa->spa_mos, obj, &dir)) != 0) 3621 return (rc); 3622 if (dir.dn_type != DMU_OT_PACKED_NVLIST && 3623 dir.dn_bonustype != DMU_OT_PACKED_NVLIST_SIZE) { 3624 return (EIO); 3625 } 3626 3627 if (dir.dn_bonuslen != sizeof (uint64_t)) 3628 return (EIO); 3629 3630 size = *(uint64_t *)DN_BONUS(&dir); 3631 nv = malloc(size); 3632 if (nv == NULL) 3633 return (ENOMEM); 3634 3635 rc = dnode_read(spa, &dir, 0, nv, size); 3636 if (rc != 0) { 3637 free(nv); 3638 nv = NULL; 3639 return (rc); 3640 } 3641 *value = nvlist_import(nv, size); 3642 free(nv); 3643 return (rc); 3644 } 3645 3646 static int 3647 zfs_spa_init(spa_t *spa) 3648 { 3649 struct uberblock checkpoint; 3650 dnode_phys_t dir; 3651 uint64_t config_object; 3652 nvlist_t *nvlist; 3653 int rc; 3654 3655 if (zio_read(spa, &spa->spa_uberblock->ub_rootbp, spa->spa_mos)) { 3656 printf("ZFS: can't read MOS of pool %s\n", spa->spa_name); 3657 return (EIO); 3658 } 3659 if (spa->spa_mos->os_type != DMU_OST_META) { 3660 printf("ZFS: corrupted MOS of pool %s\n", spa->spa_name); 3661 return (EIO); 3662 } 3663 3664 if (objset_get_dnode(spa, &spa->spa_mos_master, 3665 DMU_POOL_DIRECTORY_OBJECT, &dir)) { 3666 printf("ZFS: failed to read pool %s directory object\n", 3667 spa->spa_name); 3668 return (EIO); 3669 } 3670 /* this is allowed to fail, older pools do not have salt */ 3671 rc = zap_lookup(spa, &dir, DMU_POOL_CHECKSUM_SALT, 1, 3672 sizeof (spa->spa_cksum_salt.zcs_bytes), 3673 spa->spa_cksum_salt.zcs_bytes); 3674 3675 rc = check_mos_features(spa); 3676 if (rc != 0) { 3677 printf("ZFS: pool %s is not supported\n", spa->spa_name); 3678 return (rc); 3679 } 3680 3681 rc = zap_lookup(spa, &dir, DMU_POOL_CONFIG, 3682 sizeof (config_object), 1, &config_object); 3683 if (rc != 0) { 3684 printf("ZFS: can not read MOS %s\n", DMU_POOL_CONFIG); 3685 return (EIO); 3686 } 3687 rc = load_nvlist(spa, config_object, &nvlist); 3688 if (rc != 0) { 3689 printf("ZFS: failed to load pool %s nvlist\n", spa->spa_name); 3690 return (rc); 3691 } 3692 3693 rc = zap_lookup(spa, &dir, DMU_POOL_ZPOOL_CHECKPOINT, 3694 sizeof(uint64_t), sizeof(checkpoint) / sizeof(uint64_t), 3695 &checkpoint); 3696 if (rc == 0 && checkpoint.ub_checkpoint_txg != 0) { 3697 memcpy(&spa->spa_uberblock_checkpoint, &checkpoint, 3698 sizeof(checkpoint)); 3699 if (zio_read(spa, &spa->spa_uberblock_checkpoint.ub_rootbp, 3700 &spa->spa_mos_checkpoint)) { 3701 printf("ZFS: can not read checkpoint data.\n"); 3702 return (EIO); 3703 } 3704 } 3705 3706 /* 3707 * Update vdevs from MOS config. Note, we do skip encoding bytes 3708 * here. See also vdev_label_read_config(). 3709 */ 3710 rc = vdev_init_from_nvlist(spa, nvlist); 3711 nvlist_destroy(nvlist); 3712 return (rc); 3713 } 3714 3715 static int 3716 zfs_dnode_stat(const spa_t *spa, dnode_phys_t *dn, struct stat *sb, 3717 uint64_t fsid_guid, uint64_t objnum) 3718 { 3719 3720 /* 3721 * Zero the whole struct first so fields this function does not fill 3722 * (st_nlink, timestamps, st_blocks, ...) hold 0 rather than stack 3723 * garbage. st_dev/st_ino are then overwritten with real values below. 3724 */ 3725 memset(sb, 0, sizeof(*sb)); 3726 3727 if (dn->dn_bonustype != DMU_OT_SA) { 3728 znode_phys_t *zp = (znode_phys_t *)dn->dn_bonus; 3729 3730 sb->st_mode = zp->zp_mode; 3731 sb->st_uid = zp->zp_uid; 3732 sb->st_gid = zp->zp_gid; 3733 sb->st_size = zp->zp_size; 3734 } else { 3735 sa_hdr_phys_t *sahdrp; 3736 int hdrsize; 3737 size_t size = 0; 3738 void *buf = NULL; 3739 3740 if (dn->dn_bonuslen != 0) 3741 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn); 3742 else { 3743 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0) { 3744 blkptr_t *bp = DN_SPILL_BLKPTR(dn); 3745 int error; 3746 3747 size = BP_GET_LSIZE(bp); 3748 buf = malloc(size); 3749 if (buf == NULL) 3750 error = ENOMEM; 3751 else 3752 error = zio_read(spa, bp, buf); 3753 3754 if (error != 0) { 3755 free(buf); 3756 return (error); 3757 } 3758 sahdrp = buf; 3759 } else { 3760 return (EIO); 3761 } 3762 } 3763 hdrsize = SA_HDR_SIZE(sahdrp); 3764 sb->st_mode = *(uint64_t *)((char *)sahdrp + hdrsize + 3765 SA_MODE_OFFSET); 3766 sb->st_uid = *(uint64_t *)((char *)sahdrp + hdrsize + 3767 SA_UID_OFFSET); 3768 sb->st_gid = *(uint64_t *)((char *)sahdrp + hdrsize + 3769 SA_GID_OFFSET); 3770 sb->st_size = *(uint64_t *)((char *)sahdrp + hdrsize + 3771 SA_SIZE_OFFSET); 3772 free(buf); 3773 } 3774 3775 /* 3776 * Populate the identity fields used by veriexec to tell apart the same 3777 * path on different datasets/filesystems. dev_t and ino_t are 64-bit 3778 * on FreeBSD, so the dataset GUID and object number fit directly with 3779 * no hashing or truncation. 3780 */ 3781 sb->st_dev = (dev_t)fsid_guid; 3782 sb->st_ino = (ino_t)objnum; 3783 3784 return (0); 3785 } 3786 3787 static int 3788 zfs_dnode_readlink(const spa_t *spa, dnode_phys_t *dn, char *path, size_t psize) 3789 { 3790 int rc = 0; 3791 3792 if (dn->dn_bonustype == DMU_OT_SA) { 3793 sa_hdr_phys_t *sahdrp = NULL; 3794 size_t size = 0; 3795 void *buf = NULL; 3796 int hdrsize; 3797 char *p; 3798 3799 if (dn->dn_bonuslen != 0) { 3800 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn); 3801 } else { 3802 blkptr_t *bp; 3803 3804 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) == 0) 3805 return (EIO); 3806 bp = DN_SPILL_BLKPTR(dn); 3807 3808 size = BP_GET_LSIZE(bp); 3809 buf = malloc(size); 3810 if (buf == NULL) 3811 rc = ENOMEM; 3812 else 3813 rc = zio_read(spa, bp, buf); 3814 if (rc != 0) { 3815 free(buf); 3816 return (rc); 3817 } 3818 sahdrp = buf; 3819 } 3820 hdrsize = SA_HDR_SIZE(sahdrp); 3821 p = (char *)((uintptr_t)sahdrp + hdrsize + SA_SYMLINK_OFFSET); 3822 memcpy(path, p, psize); 3823 free(buf); 3824 return (0); 3825 } 3826 /* 3827 * Second test is purely to silence bogus compiler 3828 * warning about accessing past the end of dn_bonus. 3829 */ 3830 if (psize + sizeof(znode_phys_t) <= dn->dn_bonuslen && 3831 sizeof(znode_phys_t) <= sizeof(dn->dn_bonus)) { 3832 memcpy(path, &dn->dn_bonus[sizeof(znode_phys_t)], psize); 3833 } else { 3834 rc = dnode_read(spa, dn, 0, path, psize); 3835 } 3836 return (rc); 3837 } 3838 3839 struct obj_list { 3840 uint64_t objnum; 3841 STAILQ_ENTRY(obj_list) entry; 3842 }; 3843 3844 /* 3845 * Lookup a file and return its dnode. 3846 */ 3847 static int 3848 zfs_lookup(const struct zfsmount *mount, const char *upath, 3849 dnode_phys_t *dnode, uint64_t *objnum_out) 3850 { 3851 int rc; 3852 uint64_t objnum; 3853 const spa_t *spa; 3854 dnode_phys_t dn; 3855 const char *p, *q; 3856 char element[256]; 3857 char path[1024]; 3858 int symlinks_followed = 0; 3859 struct stat sb; 3860 struct obj_list *entry, *tentry; 3861 STAILQ_HEAD(, obj_list) on_cache = STAILQ_HEAD_INITIALIZER(on_cache); 3862 3863 spa = mount->spa; 3864 if (mount->objset.os_type != DMU_OST_ZFS) { 3865 printf("ZFS: unexpected object set type %ju\n", 3866 (uintmax_t)mount->objset.os_type); 3867 return (EIO); 3868 } 3869 3870 if ((entry = malloc(sizeof(struct obj_list))) == NULL) 3871 return (ENOMEM); 3872 3873 /* 3874 * Get the root directory dnode. 3875 */ 3876 rc = objset_get_dnode(spa, &mount->objset, MASTER_NODE_OBJ, &dn); 3877 if (rc) { 3878 free(entry); 3879 return (rc); 3880 } 3881 3882 rc = zap_lookup(spa, &dn, ZFS_ROOT_OBJ, sizeof(objnum), 1, &objnum); 3883 if (rc) { 3884 free(entry); 3885 return (rc); 3886 } 3887 entry->objnum = objnum; 3888 STAILQ_INSERT_HEAD(&on_cache, entry, entry); 3889 3890 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn); 3891 if (rc != 0) 3892 goto done; 3893 3894 p = upath; 3895 while (p && *p) { 3896 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn); 3897 if (rc != 0) 3898 goto done; 3899 3900 while (*p == '/') 3901 p++; 3902 if (*p == '\0') 3903 break; 3904 q = p; 3905 while (*q != '\0' && *q != '/') 3906 q++; 3907 3908 /* skip dot */ 3909 if (p + 1 == q && p[0] == '.') { 3910 p++; 3911 continue; 3912 } 3913 /* double dot */ 3914 if (p + 2 == q && p[0] == '.' && p[1] == '.') { 3915 p += 2; 3916 if (STAILQ_FIRST(&on_cache) == 3917 STAILQ_LAST(&on_cache, obj_list, entry)) { 3918 rc = ENOENT; 3919 goto done; 3920 } 3921 entry = STAILQ_FIRST(&on_cache); 3922 STAILQ_REMOVE_HEAD(&on_cache, entry); 3923 free(entry); 3924 objnum = (STAILQ_FIRST(&on_cache))->objnum; 3925 continue; 3926 } 3927 if (q - p + 1 > sizeof(element)) { 3928 rc = ENAMETOOLONG; 3929 goto done; 3930 } 3931 memcpy(element, p, q - p); 3932 element[q - p] = 0; 3933 p = q; 3934 3935 /* Only st_mode is inspected here, so identity is irrelevant. */ 3936 if ((rc = zfs_dnode_stat(spa, &dn, &sb, 0, 0)) != 0) 3937 goto done; 3938 if (!S_ISDIR(sb.st_mode)) { 3939 rc = ENOTDIR; 3940 goto done; 3941 } 3942 3943 rc = zap_lookup(spa, &dn, element, sizeof (objnum), 1, &objnum); 3944 if (rc) 3945 goto done; 3946 objnum = ZFS_DIRENT_OBJ(objnum); 3947 3948 if ((entry = malloc(sizeof(struct obj_list))) == NULL) { 3949 rc = ENOMEM; 3950 goto done; 3951 } 3952 entry->objnum = objnum; 3953 STAILQ_INSERT_HEAD(&on_cache, entry, entry); 3954 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn); 3955 if (rc) 3956 goto done; 3957 3958 /* 3959 * Check for symlink. 3960 */ 3961 /* Only st_mode is inspected here, so identity is irrelevant. */ 3962 rc = zfs_dnode_stat(spa, &dn, &sb, 0, 0); 3963 if (rc) 3964 goto done; 3965 if (S_ISLNK(sb.st_mode)) { 3966 if (symlinks_followed > 10) { 3967 rc = EMLINK; 3968 goto done; 3969 } 3970 symlinks_followed++; 3971 3972 /* 3973 * Read the link value and copy the tail of our 3974 * current path onto the end. 3975 */ 3976 if (sb.st_size + strlen(p) + 1 > sizeof(path)) { 3977 rc = ENAMETOOLONG; 3978 goto done; 3979 } 3980 strcpy(&path[sb.st_size], p); 3981 3982 rc = zfs_dnode_readlink(spa, &dn, path, sb.st_size); 3983 if (rc != 0) 3984 goto done; 3985 3986 /* 3987 * Restart with the new path, starting either at 3988 * the root or at the parent depending whether or 3989 * not the link is relative. 3990 */ 3991 p = path; 3992 if (*p == '/') { 3993 while (STAILQ_FIRST(&on_cache) != 3994 STAILQ_LAST(&on_cache, obj_list, entry)) { 3995 entry = STAILQ_FIRST(&on_cache); 3996 STAILQ_REMOVE_HEAD(&on_cache, entry); 3997 free(entry); 3998 } 3999 } else { 4000 entry = STAILQ_FIRST(&on_cache); 4001 STAILQ_REMOVE_HEAD(&on_cache, entry); 4002 free(entry); 4003 } 4004 objnum = (STAILQ_FIRST(&on_cache))->objnum; 4005 } 4006 } 4007 4008 *dnode = dn; 4009 /* 4010 * objnum tracks the object number of the dnode we just resolved (the 4011 * loader's equivalent of the kernel's z_id/db_object); hand it back so 4012 * the file can expose it as st_ino. Callers that do not need it pass 4013 * NULL. 4014 */ 4015 if (objnum_out != NULL) 4016 *objnum_out = objnum; 4017 done: 4018 STAILQ_FOREACH_SAFE(entry, &on_cache, entry, tentry) 4019 free(entry); 4020 return (rc); 4021 } 4022 4023 /* 4024 * Return either a cached copy of the bootenv, or read each of the vdev children 4025 * looking for the bootenv. Cache what's found and return the results. Returns 0 4026 * when benvp is filled in, and some errno when not. 4027 */ 4028 static int 4029 zfs_get_bootenv_spa(spa_t *spa, nvlist_t **benvp) 4030 { 4031 vdev_t *vd; 4032 nvlist_t *benv = NULL; 4033 4034 if (spa->spa_bootenv == NULL) { 4035 STAILQ_FOREACH(vd, &spa->spa_root_vdev->v_children, 4036 v_childlink) { 4037 benv = vdev_read_bootenv(vd); 4038 4039 if (benv != NULL) 4040 break; 4041 } 4042 spa->spa_bootenv = benv; 4043 } 4044 benv = spa->spa_bootenv; 4045 4046 if (benv == NULL) 4047 return (ENOENT); 4048 4049 *benvp = benv; 4050 return (0); 4051 } 4052 4053 /* 4054 * Store nvlist to pool label bootenv area. Also updates cached pointer in spa. 4055 */ 4056 static int 4057 zfs_set_bootenv_spa(spa_t *spa, nvlist_t *benv) 4058 { 4059 vdev_t *vd; 4060 4061 STAILQ_FOREACH(vd, &spa->spa_root_vdev->v_children, v_childlink) { 4062 vdev_write_bootenv(vd, benv); 4063 } 4064 4065 spa->spa_bootenv = benv; 4066 return (0); 4067 } 4068 4069 /* 4070 * Get bootonce value by key. The bootonce <key, value> pair is removed from the 4071 * bootenv nvlist and the remaining nvlist is committed back to disk. This process 4072 * the bootonce flag since we've reached the point in the boot that we've 'used' 4073 * the BE. For chained boot scenarios, we may reach this point multiple times (but 4074 * only remove it and return 0 the first time). 4075 */ 4076 static int 4077 zfs_get_bootonce_spa(spa_t *spa, const char *key, char *buf, size_t size) 4078 { 4079 nvlist_t *benv; 4080 char *result = NULL; 4081 int result_size, rv; 4082 4083 if ((rv = zfs_get_bootenv_spa(spa, &benv)) != 0) 4084 return (rv); 4085 4086 if ((rv = nvlist_find(benv, key, DATA_TYPE_STRING, NULL, 4087 &result, &result_size)) == 0) { 4088 if (result_size == 0) { 4089 /* ignore empty string */ 4090 rv = ENOENT; 4091 } else if (buf != NULL) { 4092 size = MIN((size_t)result_size + 1, size); 4093 strlcpy(buf, result, size); 4094 } 4095 (void)nvlist_remove(benv, key, DATA_TYPE_STRING); 4096 (void)zfs_set_bootenv_spa(spa, benv); 4097 } 4098 4099 return (rv); 4100 } 4101