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