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 __FBSDID("$FreeBSD$"); 29 30 /* 31 * Stand-alone ZFS file reader. 32 */ 33 34 #include <stdbool.h> 35 #include <sys/endian.h> 36 #include <sys/stat.h> 37 #include <sys/stdint.h> 38 #include <sys/list.h> 39 #include <sys/zfs_bootenv.h> 40 #include <machine/_inttypes.h> 41 42 #include "zfsimpl.h" 43 #include "zfssubr.c" 44 45 extern int zstd_init(void); 46 47 struct zfsmount { 48 const spa_t *spa; 49 objset_phys_t objset; 50 uint64_t rootobj; 51 }; 52 static struct zfsmount zfsmount __unused; 53 54 /* 55 * The indirect_child_t represents the vdev that we will read from, when we 56 * need to read all copies of the data (e.g. for scrub or reconstruction). 57 * For plain (non-mirror) top-level vdevs (i.e. is_vdev is not a mirror), 58 * ic_vdev is the same as is_vdev. However, for mirror top-level vdevs, 59 * ic_vdev is a child of the mirror. 60 */ 61 typedef struct indirect_child { 62 void *ic_data; 63 vdev_t *ic_vdev; 64 } indirect_child_t; 65 66 /* 67 * The indirect_split_t represents one mapped segment of an i/o to the 68 * indirect vdev. For non-split (contiguously-mapped) blocks, there will be 69 * only one indirect_split_t, with is_split_offset==0 and is_size==io_size. 70 * For split blocks, there will be several of these. 71 */ 72 typedef struct indirect_split { 73 list_node_t is_node; /* link on iv_splits */ 74 75 /* 76 * is_split_offset is the offset into the i/o. 77 * This is the sum of the previous splits' is_size's. 78 */ 79 uint64_t is_split_offset; 80 81 vdev_t *is_vdev; /* top-level vdev */ 82 uint64_t is_target_offset; /* offset on is_vdev */ 83 uint64_t is_size; 84 int is_children; /* number of entries in is_child[] */ 85 86 /* 87 * is_good_child is the child that we are currently using to 88 * attempt reconstruction. 89 */ 90 int is_good_child; 91 92 indirect_child_t is_child[1]; /* variable-length */ 93 } indirect_split_t; 94 95 /* 96 * The indirect_vsd_t is associated with each i/o to the indirect vdev. 97 * It is the "Vdev-Specific Data" in the zio_t's io_vsd. 98 */ 99 typedef struct indirect_vsd { 100 boolean_t iv_split_block; 101 boolean_t iv_reconstruct; 102 103 list_t iv_splits; /* list of indirect_split_t's */ 104 } indirect_vsd_t; 105 106 /* 107 * List of all vdevs, chained through v_alllink. 108 */ 109 static vdev_list_t zfs_vdevs; 110 111 /* 112 * List of ZFS features supported for read 113 */ 114 static const char *features_for_read[] = { 115 "org.illumos:lz4_compress", 116 "com.delphix:hole_birth", 117 "com.delphix:extensible_dataset", 118 "com.delphix:embedded_data", 119 "org.open-zfs:large_blocks", 120 "org.illumos:sha512", 121 "org.illumos:skein", 122 "org.zfsonlinux:large_dnode", 123 "com.joyent:multi_vdev_crash_dump", 124 "com.delphix:spacemap_histogram", 125 "com.delphix:zpool_checkpoint", 126 "com.delphix:spacemap_v2", 127 "com.datto:encryption", 128 "org.zfsonlinux:allocation_classes", 129 "com.datto:resilver_defer", 130 "com.delphix:device_removal", 131 "com.delphix:obsolete_counts", 132 "com.intel:allocation_classes", 133 "org.freebsd:zstd_compress", 134 NULL 135 }; 136 137 /* 138 * List of all pools, chained through spa_link. 139 */ 140 static spa_list_t zfs_pools; 141 142 static const dnode_phys_t *dnode_cache_obj; 143 static uint64_t dnode_cache_bn; 144 static char *dnode_cache_buf; 145 146 static int zio_read(const spa_t *spa, const blkptr_t *bp, void *buf); 147 static int zfs_get_root(const spa_t *spa, uint64_t *objid); 148 static int zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result); 149 static int zap_lookup(const spa_t *spa, const dnode_phys_t *dnode, 150 const char *name, uint64_t integer_size, uint64_t num_integers, 151 void *value); 152 static int objset_get_dnode(const spa_t *, const objset_phys_t *, uint64_t, 153 dnode_phys_t *); 154 static int dnode_read(const spa_t *, const dnode_phys_t *, off_t, void *, 155 size_t); 156 static int vdev_indirect_read(vdev_t *, const blkptr_t *, void *, off_t, 157 size_t); 158 static int vdev_mirror_read(vdev_t *, const blkptr_t *, void *, off_t, size_t); 159 vdev_indirect_mapping_t *vdev_indirect_mapping_open(spa_t *, objset_phys_t *, 160 uint64_t); 161 vdev_indirect_mapping_entry_phys_t * 162 vdev_indirect_mapping_duplicate_adjacent_entries(vdev_t *, uint64_t, 163 uint64_t, uint64_t *); 164 165 static void 166 zfs_init(void) 167 { 168 STAILQ_INIT(&zfs_vdevs); 169 STAILQ_INIT(&zfs_pools); 170 171 dnode_cache_buf = malloc(SPA_MAXBLOCKSIZE); 172 173 zfs_init_crc(); 174 zstd_init(); 175 } 176 177 static int 178 nvlist_check_features_for_read(nvlist_t *nvl) 179 { 180 nvlist_t *features = NULL; 181 nvs_data_t *data; 182 nvp_header_t *nvp; 183 nv_string_t *nvp_name; 184 int rc; 185 186 rc = nvlist_find(nvl, ZPOOL_CONFIG_FEATURES_FOR_READ, 187 DATA_TYPE_NVLIST, NULL, &features, NULL); 188 if (rc != 0) 189 return (rc); 190 191 data = (nvs_data_t *)features->nv_data; 192 nvp = &data->nvl_pair; /* first pair in nvlist */ 193 194 while (nvp->encoded_size != 0 && nvp->decoded_size != 0) { 195 int i, found; 196 197 nvp_name = (nv_string_t *)((uintptr_t)nvp + sizeof(*nvp)); 198 found = 0; 199 200 for (i = 0; features_for_read[i] != NULL; i++) { 201 if (memcmp(nvp_name->nv_data, features_for_read[i], 202 nvp_name->nv_size) == 0) { 203 found = 1; 204 break; 205 } 206 } 207 208 if (!found) { 209 printf("ZFS: unsupported feature: %.*s\n", 210 nvp_name->nv_size, nvp_name->nv_data); 211 rc = EIO; 212 } 213 nvp = (nvp_header_t *)((uint8_t *)nvp + nvp->encoded_size); 214 } 215 nvlist_destroy(features); 216 217 return (rc); 218 } 219 220 static int 221 vdev_read_phys(vdev_t *vdev, const blkptr_t *bp, void *buf, 222 off_t offset, size_t size) 223 { 224 size_t psize; 225 int rc; 226 227 if (vdev->v_phys_read == NULL) 228 return (ENOTSUP); 229 230 if (bp) { 231 psize = BP_GET_PSIZE(bp); 232 } else { 233 psize = size; 234 } 235 236 rc = vdev->v_phys_read(vdev, vdev->v_priv, offset, buf, psize); 237 if (rc == 0) { 238 if (bp != NULL) 239 rc = zio_checksum_verify(vdev->v_spa, bp, buf); 240 } 241 242 return (rc); 243 } 244 245 static int 246 vdev_write_phys(vdev_t *vdev, void *buf, off_t offset, size_t size) 247 { 248 if (vdev->v_phys_write == NULL) 249 return (ENOTSUP); 250 251 return (vdev->v_phys_write(vdev, offset, buf, size)); 252 } 253 254 typedef struct remap_segment { 255 vdev_t *rs_vd; 256 uint64_t rs_offset; 257 uint64_t rs_asize; 258 uint64_t rs_split_offset; 259 list_node_t rs_node; 260 } remap_segment_t; 261 262 static remap_segment_t * 263 rs_alloc(vdev_t *vd, uint64_t offset, uint64_t asize, uint64_t split_offset) 264 { 265 remap_segment_t *rs = malloc(sizeof (remap_segment_t)); 266 267 if (rs != NULL) { 268 rs->rs_vd = vd; 269 rs->rs_offset = offset; 270 rs->rs_asize = asize; 271 rs->rs_split_offset = split_offset; 272 } 273 274 return (rs); 275 } 276 277 vdev_indirect_mapping_t * 278 vdev_indirect_mapping_open(spa_t *spa, objset_phys_t *os, 279 uint64_t mapping_object) 280 { 281 vdev_indirect_mapping_t *vim; 282 vdev_indirect_mapping_phys_t *vim_phys; 283 int rc; 284 285 vim = calloc(1, sizeof (*vim)); 286 if (vim == NULL) 287 return (NULL); 288 289 vim->vim_dn = calloc(1, sizeof (*vim->vim_dn)); 290 if (vim->vim_dn == NULL) { 291 free(vim); 292 return (NULL); 293 } 294 295 rc = objset_get_dnode(spa, os, mapping_object, vim->vim_dn); 296 if (rc != 0) { 297 free(vim->vim_dn); 298 free(vim); 299 return (NULL); 300 } 301 302 vim->vim_spa = spa; 303 vim->vim_phys = malloc(sizeof (*vim->vim_phys)); 304 if (vim->vim_phys == NULL) { 305 free(vim->vim_dn); 306 free(vim); 307 return (NULL); 308 } 309 310 vim_phys = (vdev_indirect_mapping_phys_t *)DN_BONUS(vim->vim_dn); 311 *vim->vim_phys = *vim_phys; 312 313 vim->vim_objset = os; 314 vim->vim_object = mapping_object; 315 vim->vim_entries = NULL; 316 317 vim->vim_havecounts = 318 (vim->vim_dn->dn_bonuslen > VDEV_INDIRECT_MAPPING_SIZE_V0); 319 320 return (vim); 321 } 322 323 /* 324 * Compare an offset with an indirect mapping entry; there are three 325 * possible scenarios: 326 * 327 * 1. The offset is "less than" the mapping entry; meaning the 328 * offset is less than the source offset of the mapping entry. In 329 * this case, there is no overlap between the offset and the 330 * mapping entry and -1 will be returned. 331 * 332 * 2. The offset is "greater than" the mapping entry; meaning the 333 * offset is greater than the mapping entry's source offset plus 334 * the entry's size. In this case, there is no overlap between 335 * the offset and the mapping entry and 1 will be returned. 336 * 337 * NOTE: If the offset is actually equal to the entry's offset 338 * plus size, this is considered to be "greater" than the entry, 339 * and this case applies (i.e. 1 will be returned). Thus, the 340 * entry's "range" can be considered to be inclusive at its 341 * start, but exclusive at its end: e.g. [src, src + size). 342 * 343 * 3. The last case to consider is if the offset actually falls 344 * within the mapping entry's range. If this is the case, the 345 * offset is considered to be "equal to" the mapping entry and 346 * 0 will be returned. 347 * 348 * NOTE: If the offset is equal to the entry's source offset, 349 * this case applies and 0 will be returned. If the offset is 350 * equal to the entry's source plus its size, this case does 351 * *not* apply (see "NOTE" above for scenario 2), and 1 will be 352 * returned. 353 */ 354 static int 355 dva_mapping_overlap_compare(const void *v_key, const void *v_array_elem) 356 { 357 const uint64_t *key = v_key; 358 const vdev_indirect_mapping_entry_phys_t *array_elem = 359 v_array_elem; 360 uint64_t src_offset = DVA_MAPPING_GET_SRC_OFFSET(array_elem); 361 362 if (*key < src_offset) { 363 return (-1); 364 } else if (*key < src_offset + DVA_GET_ASIZE(&array_elem->vimep_dst)) { 365 return (0); 366 } else { 367 return (1); 368 } 369 } 370 371 /* 372 * Return array entry. 373 */ 374 static vdev_indirect_mapping_entry_phys_t * 375 vdev_indirect_mapping_entry(vdev_indirect_mapping_t *vim, uint64_t index) 376 { 377 uint64_t size; 378 off_t offset = 0; 379 int rc; 380 381 if (vim->vim_phys->vimp_num_entries == 0) 382 return (NULL); 383 384 if (vim->vim_entries == NULL) { 385 uint64_t bsize; 386 387 bsize = vim->vim_dn->dn_datablkszsec << SPA_MINBLOCKSHIFT; 388 size = vim->vim_phys->vimp_num_entries * 389 sizeof (*vim->vim_entries); 390 if (size > bsize) { 391 size = bsize / sizeof (*vim->vim_entries); 392 size *= sizeof (*vim->vim_entries); 393 } 394 vim->vim_entries = malloc(size); 395 if (vim->vim_entries == NULL) 396 return (NULL); 397 vim->vim_num_entries = size / sizeof (*vim->vim_entries); 398 offset = index * sizeof (*vim->vim_entries); 399 } 400 401 /* We have data in vim_entries */ 402 if (offset == 0) { 403 if (index >= vim->vim_entry_offset && 404 index <= vim->vim_entry_offset + vim->vim_num_entries) { 405 index -= vim->vim_entry_offset; 406 return (&vim->vim_entries[index]); 407 } 408 offset = index * sizeof (*vim->vim_entries); 409 } 410 411 vim->vim_entry_offset = index; 412 size = vim->vim_num_entries * sizeof (*vim->vim_entries); 413 rc = dnode_read(vim->vim_spa, vim->vim_dn, offset, vim->vim_entries, 414 size); 415 if (rc != 0) { 416 /* Read error, invalidate vim_entries. */ 417 free(vim->vim_entries); 418 vim->vim_entries = NULL; 419 return (NULL); 420 } 421 index -= vim->vim_entry_offset; 422 return (&vim->vim_entries[index]); 423 } 424 425 /* 426 * Returns the mapping entry for the given offset. 427 * 428 * It's possible that the given offset will not be in the mapping table 429 * (i.e. no mapping entries contain this offset), in which case, the 430 * return value value depends on the "next_if_missing" parameter. 431 * 432 * If the offset is not found in the table and "next_if_missing" is 433 * B_FALSE, then NULL will always be returned. The behavior is intended 434 * to allow consumers to get the entry corresponding to the offset 435 * parameter, iff the offset overlaps with an entry in the table. 436 * 437 * If the offset is not found in the table and "next_if_missing" is 438 * B_TRUE, then the entry nearest to the given offset will be returned, 439 * such that the entry's source offset is greater than the offset 440 * passed in (i.e. the "next" mapping entry in the table is returned, if 441 * the offset is missing from the table). If there are no entries whose 442 * source offset is greater than the passed in offset, NULL is returned. 443 */ 444 static vdev_indirect_mapping_entry_phys_t * 445 vdev_indirect_mapping_entry_for_offset(vdev_indirect_mapping_t *vim, 446 uint64_t offset) 447 { 448 ASSERT(vim->vim_phys->vimp_num_entries > 0); 449 450 vdev_indirect_mapping_entry_phys_t *entry; 451 452 uint64_t last = vim->vim_phys->vimp_num_entries - 1; 453 uint64_t base = 0; 454 455 /* 456 * We don't define these inside of the while loop because we use 457 * their value in the case that offset isn't in the mapping. 458 */ 459 uint64_t mid; 460 int result; 461 462 while (last >= base) { 463 mid = base + ((last - base) >> 1); 464 465 entry = vdev_indirect_mapping_entry(vim, mid); 466 if (entry == NULL) 467 break; 468 result = dva_mapping_overlap_compare(&offset, entry); 469 470 if (result == 0) { 471 break; 472 } else if (result < 0) { 473 last = mid - 1; 474 } else { 475 base = mid + 1; 476 } 477 } 478 return (entry); 479 } 480 481 /* 482 * Given an indirect vdev and an extent on that vdev, it duplicates the 483 * physical entries of the indirect mapping that correspond to the extent 484 * to a new array and returns a pointer to it. In addition, copied_entries 485 * is populated with the number of mapping entries that were duplicated. 486 * 487 * Finally, since we are doing an allocation, it is up to the caller to 488 * free the array allocated in this function. 489 */ 490 vdev_indirect_mapping_entry_phys_t * 491 vdev_indirect_mapping_duplicate_adjacent_entries(vdev_t *vd, uint64_t offset, 492 uint64_t asize, uint64_t *copied_entries) 493 { 494 vdev_indirect_mapping_entry_phys_t *duplicate_mappings = NULL; 495 vdev_indirect_mapping_t *vim = vd->v_mapping; 496 uint64_t entries = 0; 497 498 vdev_indirect_mapping_entry_phys_t *first_mapping = 499 vdev_indirect_mapping_entry_for_offset(vim, offset); 500 ASSERT3P(first_mapping, !=, NULL); 501 502 vdev_indirect_mapping_entry_phys_t *m = first_mapping; 503 while (asize > 0) { 504 uint64_t size = DVA_GET_ASIZE(&m->vimep_dst); 505 uint64_t inner_offset = offset - DVA_MAPPING_GET_SRC_OFFSET(m); 506 uint64_t inner_size = MIN(asize, size - inner_offset); 507 508 offset += inner_size; 509 asize -= inner_size; 510 entries++; 511 m++; 512 } 513 514 size_t copy_length = entries * sizeof (*first_mapping); 515 duplicate_mappings = malloc(copy_length); 516 if (duplicate_mappings != NULL) 517 bcopy(first_mapping, duplicate_mappings, copy_length); 518 else 519 entries = 0; 520 521 *copied_entries = entries; 522 523 return (duplicate_mappings); 524 } 525 526 static vdev_t * 527 vdev_lookup_top(spa_t *spa, uint64_t vdev) 528 { 529 vdev_t *rvd; 530 vdev_list_t *vlist; 531 532 vlist = &spa->spa_root_vdev->v_children; 533 STAILQ_FOREACH(rvd, vlist, v_childlink) 534 if (rvd->v_id == vdev) 535 break; 536 537 return (rvd); 538 } 539 540 /* 541 * This is a callback for vdev_indirect_remap() which allocates an 542 * indirect_split_t for each split segment and adds it to iv_splits. 543 */ 544 static void 545 vdev_indirect_gather_splits(uint64_t split_offset, vdev_t *vd, uint64_t offset, 546 uint64_t size, void *arg) 547 { 548 int n = 1; 549 zio_t *zio = arg; 550 indirect_vsd_t *iv = zio->io_vsd; 551 552 if (vd->v_read == vdev_indirect_read) 553 return; 554 555 if (vd->v_read == vdev_mirror_read) 556 n = vd->v_nchildren; 557 558 indirect_split_t *is = 559 malloc(offsetof(indirect_split_t, is_child[n])); 560 if (is == NULL) { 561 zio->io_error = ENOMEM; 562 return; 563 } 564 bzero(is, offsetof(indirect_split_t, is_child[n])); 565 566 is->is_children = n; 567 is->is_size = size; 568 is->is_split_offset = split_offset; 569 is->is_target_offset = offset; 570 is->is_vdev = vd; 571 572 /* 573 * Note that we only consider multiple copies of the data for 574 * *mirror* vdevs. We don't for "replacing" or "spare" vdevs, even 575 * though they use the same ops as mirror, because there's only one 576 * "good" copy under the replacing/spare. 577 */ 578 if (vd->v_read == vdev_mirror_read) { 579 int i = 0; 580 vdev_t *kid; 581 582 STAILQ_FOREACH(kid, &vd->v_children, v_childlink) { 583 is->is_child[i++].ic_vdev = kid; 584 } 585 } else { 586 is->is_child[0].ic_vdev = vd; 587 } 588 589 list_insert_tail(&iv->iv_splits, is); 590 } 591 592 static void 593 vdev_indirect_remap(vdev_t *vd, uint64_t offset, uint64_t asize, void *arg) 594 { 595 list_t stack; 596 spa_t *spa = vd->v_spa; 597 zio_t *zio = arg; 598 remap_segment_t *rs; 599 600 list_create(&stack, sizeof (remap_segment_t), 601 offsetof(remap_segment_t, rs_node)); 602 603 rs = rs_alloc(vd, offset, asize, 0); 604 if (rs == NULL) { 605 printf("vdev_indirect_remap: out of memory.\n"); 606 zio->io_error = ENOMEM; 607 } 608 for (; rs != NULL; rs = list_remove_head(&stack)) { 609 vdev_t *v = rs->rs_vd; 610 uint64_t num_entries = 0; 611 /* vdev_indirect_mapping_t *vim = v->v_mapping; */ 612 vdev_indirect_mapping_entry_phys_t *mapping = 613 vdev_indirect_mapping_duplicate_adjacent_entries(v, 614 rs->rs_offset, rs->rs_asize, &num_entries); 615 616 if (num_entries == 0) 617 zio->io_error = ENOMEM; 618 619 for (uint64_t i = 0; i < num_entries; i++) { 620 vdev_indirect_mapping_entry_phys_t *m = &mapping[i]; 621 uint64_t size = DVA_GET_ASIZE(&m->vimep_dst); 622 uint64_t dst_offset = DVA_GET_OFFSET(&m->vimep_dst); 623 uint64_t dst_vdev = DVA_GET_VDEV(&m->vimep_dst); 624 uint64_t inner_offset = rs->rs_offset - 625 DVA_MAPPING_GET_SRC_OFFSET(m); 626 uint64_t inner_size = 627 MIN(rs->rs_asize, size - inner_offset); 628 vdev_t *dst_v = vdev_lookup_top(spa, dst_vdev); 629 630 if (dst_v->v_read == vdev_indirect_read) { 631 remap_segment_t *o; 632 633 o = rs_alloc(dst_v, dst_offset + inner_offset, 634 inner_size, rs->rs_split_offset); 635 if (o == NULL) { 636 printf("vdev_indirect_remap: " 637 "out of memory.\n"); 638 zio->io_error = ENOMEM; 639 break; 640 } 641 642 list_insert_head(&stack, o); 643 } 644 vdev_indirect_gather_splits(rs->rs_split_offset, dst_v, 645 dst_offset + inner_offset, 646 inner_size, arg); 647 648 /* 649 * vdev_indirect_gather_splits can have memory 650 * allocation error, we can not recover from it. 651 */ 652 if (zio->io_error != 0) 653 break; 654 rs->rs_offset += inner_size; 655 rs->rs_asize -= inner_size; 656 rs->rs_split_offset += inner_size; 657 } 658 659 free(mapping); 660 free(rs); 661 if (zio->io_error != 0) 662 break; 663 } 664 665 list_destroy(&stack); 666 } 667 668 static void 669 vdev_indirect_map_free(zio_t *zio) 670 { 671 indirect_vsd_t *iv = zio->io_vsd; 672 indirect_split_t *is; 673 674 while ((is = list_head(&iv->iv_splits)) != NULL) { 675 for (int c = 0; c < is->is_children; c++) { 676 indirect_child_t *ic = &is->is_child[c]; 677 free(ic->ic_data); 678 } 679 list_remove(&iv->iv_splits, is); 680 free(is); 681 } 682 free(iv); 683 } 684 685 static int 686 vdev_indirect_read(vdev_t *vdev, const blkptr_t *bp, void *buf, 687 off_t offset, size_t bytes) 688 { 689 zio_t zio; 690 spa_t *spa = vdev->v_spa; 691 indirect_vsd_t *iv; 692 indirect_split_t *first; 693 int rc = EIO; 694 695 iv = calloc(1, sizeof(*iv)); 696 if (iv == NULL) 697 return (ENOMEM); 698 699 list_create(&iv->iv_splits, 700 sizeof (indirect_split_t), offsetof(indirect_split_t, is_node)); 701 702 bzero(&zio, sizeof(zio)); 703 zio.io_spa = spa; 704 zio.io_bp = (blkptr_t *)bp; 705 zio.io_data = buf; 706 zio.io_size = bytes; 707 zio.io_offset = offset; 708 zio.io_vd = vdev; 709 zio.io_vsd = iv; 710 711 if (vdev->v_mapping == NULL) { 712 vdev_indirect_config_t *vic; 713 714 vic = &vdev->vdev_indirect_config; 715 vdev->v_mapping = vdev_indirect_mapping_open(spa, 716 spa->spa_mos, vic->vic_mapping_object); 717 } 718 719 vdev_indirect_remap(vdev, offset, bytes, &zio); 720 if (zio.io_error != 0) 721 return (zio.io_error); 722 723 first = list_head(&iv->iv_splits); 724 if (first->is_size == zio.io_size) { 725 /* 726 * This is not a split block; we are pointing to the entire 727 * data, which will checksum the same as the original data. 728 * Pass the BP down so that the child i/o can verify the 729 * checksum, and try a different location if available 730 * (e.g. on a mirror). 731 * 732 * While this special case could be handled the same as the 733 * general (split block) case, doing it this way ensures 734 * that the vast majority of blocks on indirect vdevs 735 * (which are not split) are handled identically to blocks 736 * on non-indirect vdevs. This allows us to be less strict 737 * about performance in the general (but rare) case. 738 */ 739 rc = first->is_vdev->v_read(first->is_vdev, zio.io_bp, 740 zio.io_data, first->is_target_offset, bytes); 741 } else { 742 iv->iv_split_block = B_TRUE; 743 /* 744 * Read one copy of each split segment, from the 745 * top-level vdev. Since we don't know the 746 * checksum of each split individually, the child 747 * zio can't ensure that we get the right data. 748 * E.g. if it's a mirror, it will just read from a 749 * random (healthy) leaf vdev. We have to verify 750 * the checksum in vdev_indirect_io_done(). 751 */ 752 for (indirect_split_t *is = list_head(&iv->iv_splits); 753 is != NULL; is = list_next(&iv->iv_splits, is)) { 754 char *ptr = zio.io_data; 755 756 rc = is->is_vdev->v_read(is->is_vdev, zio.io_bp, 757 ptr + is->is_split_offset, is->is_target_offset, 758 is->is_size); 759 } 760 if (zio_checksum_verify(spa, zio.io_bp, zio.io_data)) 761 rc = ECKSUM; 762 else 763 rc = 0; 764 } 765 766 vdev_indirect_map_free(&zio); 767 if (rc == 0) 768 rc = zio.io_error; 769 770 return (rc); 771 } 772 773 static int 774 vdev_disk_read(vdev_t *vdev, const blkptr_t *bp, void *buf, 775 off_t offset, size_t bytes) 776 { 777 778 return (vdev_read_phys(vdev, bp, buf, 779 offset + VDEV_LABEL_START_SIZE, bytes)); 780 } 781 782 static int 783 vdev_missing_read(vdev_t *vdev __unused, const blkptr_t *bp __unused, 784 void *buf __unused, off_t offset __unused, size_t bytes __unused) 785 { 786 787 return (ENOTSUP); 788 } 789 790 static int 791 vdev_mirror_read(vdev_t *vdev, const blkptr_t *bp, void *buf, 792 off_t offset, size_t bytes) 793 { 794 vdev_t *kid; 795 int rc; 796 797 rc = EIO; 798 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) { 799 if (kid->v_state != VDEV_STATE_HEALTHY) 800 continue; 801 rc = kid->v_read(kid, bp, buf, offset, bytes); 802 if (!rc) 803 return (0); 804 } 805 806 return (rc); 807 } 808 809 static int 810 vdev_replacing_read(vdev_t *vdev, const blkptr_t *bp, void *buf, 811 off_t offset, size_t bytes) 812 { 813 vdev_t *kid; 814 815 /* 816 * Here we should have two kids: 817 * First one which is the one we are replacing and we can trust 818 * only this one to have valid data, but it might not be present. 819 * Second one is that one we are replacing with. It is most likely 820 * healthy, but we can't trust it has needed data, so we won't use it. 821 */ 822 kid = STAILQ_FIRST(&vdev->v_children); 823 if (kid == NULL) 824 return (EIO); 825 if (kid->v_state != VDEV_STATE_HEALTHY) 826 return (EIO); 827 return (kid->v_read(kid, bp, buf, offset, bytes)); 828 } 829 830 static vdev_t * 831 vdev_find(uint64_t guid) 832 { 833 vdev_t *vdev; 834 835 STAILQ_FOREACH(vdev, &zfs_vdevs, v_alllink) 836 if (vdev->v_guid == guid) 837 return (vdev); 838 839 return (0); 840 } 841 842 static vdev_t * 843 vdev_create(uint64_t guid, vdev_read_t *_read) 844 { 845 vdev_t *vdev; 846 vdev_indirect_config_t *vic; 847 848 vdev = calloc(1, sizeof(vdev_t)); 849 if (vdev != NULL) { 850 STAILQ_INIT(&vdev->v_children); 851 vdev->v_guid = guid; 852 vdev->v_read = _read; 853 854 /* 855 * root vdev has no read function, we use this fact to 856 * skip setting up data we do not need for root vdev. 857 * We only point root vdev from spa. 858 */ 859 if (_read != NULL) { 860 vic = &vdev->vdev_indirect_config; 861 vic->vic_prev_indirect_vdev = UINT64_MAX; 862 STAILQ_INSERT_TAIL(&zfs_vdevs, vdev, v_alllink); 863 } 864 } 865 866 return (vdev); 867 } 868 869 static void 870 vdev_set_initial_state(vdev_t *vdev, const nvlist_t *nvlist) 871 { 872 uint64_t is_offline, is_faulted, is_degraded, is_removed, isnt_present; 873 uint64_t is_log; 874 875 is_offline = is_removed = is_faulted = is_degraded = isnt_present = 0; 876 is_log = 0; 877 (void) nvlist_find(nvlist, ZPOOL_CONFIG_OFFLINE, DATA_TYPE_UINT64, NULL, 878 &is_offline, NULL); 879 (void) nvlist_find(nvlist, ZPOOL_CONFIG_REMOVED, DATA_TYPE_UINT64, NULL, 880 &is_removed, NULL); 881 (void) nvlist_find(nvlist, ZPOOL_CONFIG_FAULTED, DATA_TYPE_UINT64, NULL, 882 &is_faulted, NULL); 883 (void) nvlist_find(nvlist, ZPOOL_CONFIG_DEGRADED, DATA_TYPE_UINT64, 884 NULL, &is_degraded, NULL); 885 (void) nvlist_find(nvlist, ZPOOL_CONFIG_NOT_PRESENT, DATA_TYPE_UINT64, 886 NULL, &isnt_present, NULL); 887 (void) nvlist_find(nvlist, ZPOOL_CONFIG_IS_LOG, DATA_TYPE_UINT64, NULL, 888 &is_log, NULL); 889 890 if (is_offline != 0) 891 vdev->v_state = VDEV_STATE_OFFLINE; 892 else if (is_removed != 0) 893 vdev->v_state = VDEV_STATE_REMOVED; 894 else if (is_faulted != 0) 895 vdev->v_state = VDEV_STATE_FAULTED; 896 else if (is_degraded != 0) 897 vdev->v_state = VDEV_STATE_DEGRADED; 898 else if (isnt_present != 0) 899 vdev->v_state = VDEV_STATE_CANT_OPEN; 900 901 vdev->v_islog = is_log != 0; 902 } 903 904 static int 905 vdev_init(uint64_t guid, const nvlist_t *nvlist, vdev_t **vdevp) 906 { 907 uint64_t id, ashift, asize, nparity; 908 const char *path; 909 const char *type; 910 int len, pathlen; 911 char *name; 912 vdev_t *vdev; 913 914 if (nvlist_find(nvlist, ZPOOL_CONFIG_ID, DATA_TYPE_UINT64, NULL, &id, 915 NULL) || 916 nvlist_find(nvlist, ZPOOL_CONFIG_TYPE, DATA_TYPE_STRING, NULL, 917 &type, &len)) { 918 return (ENOENT); 919 } 920 921 if (memcmp(type, VDEV_TYPE_MIRROR, len) != 0 && 922 memcmp(type, VDEV_TYPE_DISK, len) != 0 && 923 #ifdef ZFS_TEST 924 memcmp(type, VDEV_TYPE_FILE, len) != 0 && 925 #endif 926 memcmp(type, VDEV_TYPE_RAIDZ, len) != 0 && 927 memcmp(type, VDEV_TYPE_INDIRECT, len) != 0 && 928 memcmp(type, VDEV_TYPE_REPLACING, len) != 0 && 929 memcmp(type, VDEV_TYPE_HOLE, len) != 0) { 930 printf("ZFS: can only boot from disk, mirror, raidz1, " 931 "raidz2 and raidz3 vdevs, got: %.*s\n", len, type); 932 return (EIO); 933 } 934 935 if (memcmp(type, VDEV_TYPE_MIRROR, len) == 0) 936 vdev = vdev_create(guid, vdev_mirror_read); 937 else if (memcmp(type, VDEV_TYPE_RAIDZ, len) == 0) 938 vdev = vdev_create(guid, vdev_raidz_read); 939 else if (memcmp(type, VDEV_TYPE_REPLACING, len) == 0) 940 vdev = vdev_create(guid, vdev_replacing_read); 941 else if (memcmp(type, VDEV_TYPE_INDIRECT, len) == 0) { 942 vdev_indirect_config_t *vic; 943 944 vdev = vdev_create(guid, vdev_indirect_read); 945 if (vdev != NULL) { 946 vdev->v_state = VDEV_STATE_HEALTHY; 947 vic = &vdev->vdev_indirect_config; 948 949 nvlist_find(nvlist, 950 ZPOOL_CONFIG_INDIRECT_OBJECT, 951 DATA_TYPE_UINT64, 952 NULL, &vic->vic_mapping_object, NULL); 953 nvlist_find(nvlist, 954 ZPOOL_CONFIG_INDIRECT_BIRTHS, 955 DATA_TYPE_UINT64, 956 NULL, &vic->vic_births_object, NULL); 957 nvlist_find(nvlist, 958 ZPOOL_CONFIG_PREV_INDIRECT_VDEV, 959 DATA_TYPE_UINT64, 960 NULL, &vic->vic_prev_indirect_vdev, NULL); 961 } 962 } else if (memcmp(type, VDEV_TYPE_HOLE, len) == 0) { 963 vdev = vdev_create(guid, vdev_missing_read); 964 } else { 965 vdev = vdev_create(guid, vdev_disk_read); 966 } 967 968 if (vdev == NULL) 969 return (ENOMEM); 970 971 vdev_set_initial_state(vdev, nvlist); 972 vdev->v_id = id; 973 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASHIFT, 974 DATA_TYPE_UINT64, NULL, &ashift, NULL) == 0) 975 vdev->v_ashift = ashift; 976 977 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASIZE, 978 DATA_TYPE_UINT64, NULL, &asize, NULL) == 0) { 979 vdev->v_psize = asize + 980 VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE; 981 } 982 983 if (nvlist_find(nvlist, ZPOOL_CONFIG_NPARITY, 984 DATA_TYPE_UINT64, NULL, &nparity, NULL) == 0) 985 vdev->v_nparity = nparity; 986 987 if (nvlist_find(nvlist, ZPOOL_CONFIG_PATH, 988 DATA_TYPE_STRING, NULL, &path, &pathlen) == 0) { 989 char prefix[] = "/dev/"; 990 991 len = strlen(prefix); 992 if (len < pathlen && memcmp(path, prefix, len) == 0) { 993 path += len; 994 pathlen -= len; 995 } 996 name = malloc(pathlen + 1); 997 bcopy(path, name, pathlen); 998 name[pathlen] = '\0'; 999 vdev->v_name = name; 1000 } else { 1001 name = NULL; 1002 if (memcmp(type, VDEV_TYPE_RAIDZ, len) == 0) { 1003 if (vdev->v_nparity < 1 || 1004 vdev->v_nparity > 3) { 1005 printf("ZFS: invalid raidz parity: %d\n", 1006 vdev->v_nparity); 1007 return (EIO); 1008 } 1009 (void) asprintf(&name, "%.*s%d-%" PRIu64, len, type, 1010 vdev->v_nparity, id); 1011 } else { 1012 (void) asprintf(&name, "%.*s-%" PRIu64, len, type, id); 1013 } 1014 vdev->v_name = name; 1015 } 1016 *vdevp = vdev; 1017 return (0); 1018 } 1019 1020 /* 1021 * Find slot for vdev. We return either NULL to signal to use 1022 * STAILQ_INSERT_HEAD, or we return link element to be used with 1023 * STAILQ_INSERT_AFTER. 1024 */ 1025 static vdev_t * 1026 vdev_find_previous(vdev_t *top_vdev, vdev_t *vdev) 1027 { 1028 vdev_t *v, *previous; 1029 1030 if (STAILQ_EMPTY(&top_vdev->v_children)) 1031 return (NULL); 1032 1033 previous = NULL; 1034 STAILQ_FOREACH(v, &top_vdev->v_children, v_childlink) { 1035 if (v->v_id > vdev->v_id) 1036 return (previous); 1037 1038 if (v->v_id == vdev->v_id) 1039 return (v); 1040 1041 if (v->v_id < vdev->v_id) 1042 previous = v; 1043 } 1044 return (previous); 1045 } 1046 1047 static size_t 1048 vdev_child_count(vdev_t *vdev) 1049 { 1050 vdev_t *v; 1051 size_t count; 1052 1053 count = 0; 1054 STAILQ_FOREACH(v, &vdev->v_children, v_childlink) { 1055 count++; 1056 } 1057 return (count); 1058 } 1059 1060 /* 1061 * Insert vdev into top_vdev children list. List is ordered by v_id. 1062 */ 1063 static void 1064 vdev_insert(vdev_t *top_vdev, vdev_t *vdev) 1065 { 1066 vdev_t *previous; 1067 size_t count; 1068 1069 /* 1070 * The top level vdev can appear in random order, depending how 1071 * the firmware is presenting the disk devices. 1072 * However, we will insert vdev to create list ordered by v_id, 1073 * so we can use either STAILQ_INSERT_HEAD or STAILQ_INSERT_AFTER 1074 * as STAILQ does not have insert before. 1075 */ 1076 previous = vdev_find_previous(top_vdev, vdev); 1077 1078 if (previous == NULL) { 1079 STAILQ_INSERT_HEAD(&top_vdev->v_children, vdev, v_childlink); 1080 } else if (previous->v_id == vdev->v_id) { 1081 /* 1082 * This vdev was configured from label config, 1083 * do not insert duplicate. 1084 */ 1085 return; 1086 } else { 1087 STAILQ_INSERT_AFTER(&top_vdev->v_children, previous, vdev, 1088 v_childlink); 1089 } 1090 1091 count = vdev_child_count(top_vdev); 1092 if (top_vdev->v_nchildren < count) 1093 top_vdev->v_nchildren = count; 1094 } 1095 1096 static int 1097 vdev_from_nvlist(spa_t *spa, uint64_t top_guid, const nvlist_t *nvlist) 1098 { 1099 vdev_t *top_vdev, *vdev; 1100 nvlist_t **kids = NULL; 1101 int rc, nkids; 1102 1103 /* Get top vdev. */ 1104 top_vdev = vdev_find(top_guid); 1105 if (top_vdev == NULL) { 1106 rc = vdev_init(top_guid, nvlist, &top_vdev); 1107 if (rc != 0) 1108 return (rc); 1109 top_vdev->v_spa = spa; 1110 top_vdev->v_top = top_vdev; 1111 vdev_insert(spa->spa_root_vdev, top_vdev); 1112 } 1113 1114 /* Add children if there are any. */ 1115 rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY, 1116 &nkids, &kids, NULL); 1117 if (rc == 0) { 1118 for (int i = 0; i < nkids; i++) { 1119 uint64_t guid; 1120 1121 rc = nvlist_find(kids[i], ZPOOL_CONFIG_GUID, 1122 DATA_TYPE_UINT64, NULL, &guid, NULL); 1123 if (rc != 0) 1124 goto done; 1125 1126 rc = vdev_init(guid, kids[i], &vdev); 1127 if (rc != 0) 1128 goto done; 1129 1130 vdev->v_spa = spa; 1131 vdev->v_top = top_vdev; 1132 vdev_insert(top_vdev, vdev); 1133 } 1134 } else { 1135 /* 1136 * When there are no children, nvlist_find() does return 1137 * error, reset it because leaf devices have no children. 1138 */ 1139 rc = 0; 1140 } 1141 done: 1142 if (kids != NULL) { 1143 for (int i = 0; i < nkids; i++) 1144 nvlist_destroy(kids[i]); 1145 free(kids); 1146 } 1147 1148 return (rc); 1149 } 1150 1151 static int 1152 vdev_init_from_label(spa_t *spa, const nvlist_t *nvlist) 1153 { 1154 uint64_t pool_guid, top_guid; 1155 nvlist_t *vdevs; 1156 int rc; 1157 1158 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64, 1159 NULL, &pool_guid, NULL) || 1160 nvlist_find(nvlist, ZPOOL_CONFIG_TOP_GUID, DATA_TYPE_UINT64, 1161 NULL, &top_guid, NULL) || 1162 nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST, 1163 NULL, &vdevs, NULL)) { 1164 printf("ZFS: can't find vdev details\n"); 1165 return (ENOENT); 1166 } 1167 1168 rc = vdev_from_nvlist(spa, top_guid, vdevs); 1169 nvlist_destroy(vdevs); 1170 return (rc); 1171 } 1172 1173 static void 1174 vdev_set_state(vdev_t *vdev) 1175 { 1176 vdev_t *kid; 1177 int good_kids; 1178 int bad_kids; 1179 1180 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) { 1181 vdev_set_state(kid); 1182 } 1183 1184 /* 1185 * A mirror or raidz is healthy if all its kids are healthy. A 1186 * mirror is degraded if any of its kids is healthy; a raidz 1187 * is degraded if at most nparity kids are offline. 1188 */ 1189 if (STAILQ_FIRST(&vdev->v_children)) { 1190 good_kids = 0; 1191 bad_kids = 0; 1192 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) { 1193 if (kid->v_state == VDEV_STATE_HEALTHY) 1194 good_kids++; 1195 else 1196 bad_kids++; 1197 } 1198 if (bad_kids == 0) { 1199 vdev->v_state = VDEV_STATE_HEALTHY; 1200 } else { 1201 if (vdev->v_read == vdev_mirror_read) { 1202 if (good_kids) { 1203 vdev->v_state = VDEV_STATE_DEGRADED; 1204 } else { 1205 vdev->v_state = VDEV_STATE_OFFLINE; 1206 } 1207 } else if (vdev->v_read == vdev_raidz_read) { 1208 if (bad_kids > vdev->v_nparity) { 1209 vdev->v_state = VDEV_STATE_OFFLINE; 1210 } else { 1211 vdev->v_state = VDEV_STATE_DEGRADED; 1212 } 1213 } 1214 } 1215 } 1216 } 1217 1218 static int 1219 vdev_update_from_nvlist(uint64_t top_guid, const nvlist_t *nvlist) 1220 { 1221 vdev_t *vdev; 1222 nvlist_t **kids = NULL; 1223 int rc, nkids; 1224 1225 /* Update top vdev. */ 1226 vdev = vdev_find(top_guid); 1227 if (vdev != NULL) 1228 vdev_set_initial_state(vdev, nvlist); 1229 1230 /* Update children if there are any. */ 1231 rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY, 1232 &nkids, &kids, NULL); 1233 if (rc == 0) { 1234 for (int i = 0; i < nkids; i++) { 1235 uint64_t guid; 1236 1237 rc = nvlist_find(kids[i], ZPOOL_CONFIG_GUID, 1238 DATA_TYPE_UINT64, NULL, &guid, NULL); 1239 if (rc != 0) 1240 break; 1241 1242 vdev = vdev_find(guid); 1243 if (vdev != NULL) 1244 vdev_set_initial_state(vdev, kids[i]); 1245 } 1246 } else { 1247 rc = 0; 1248 } 1249 if (kids != NULL) { 1250 for (int i = 0; i < nkids; i++) 1251 nvlist_destroy(kids[i]); 1252 free(kids); 1253 } 1254 1255 return (rc); 1256 } 1257 1258 static int 1259 vdev_init_from_nvlist(spa_t *spa, const nvlist_t *nvlist) 1260 { 1261 uint64_t pool_guid, vdev_children; 1262 nvlist_t *vdevs = NULL, **kids = NULL; 1263 int rc, nkids; 1264 1265 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64, 1266 NULL, &pool_guid, NULL) || 1267 nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_CHILDREN, DATA_TYPE_UINT64, 1268 NULL, &vdev_children, NULL) || 1269 nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST, 1270 NULL, &vdevs, NULL)) { 1271 printf("ZFS: can't find vdev details\n"); 1272 return (ENOENT); 1273 } 1274 1275 /* Wrong guid?! */ 1276 if (spa->spa_guid != pool_guid) { 1277 nvlist_destroy(vdevs); 1278 return (EINVAL); 1279 } 1280 1281 spa->spa_root_vdev->v_nchildren = vdev_children; 1282 1283 rc = nvlist_find(vdevs, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY, 1284 &nkids, &kids, NULL); 1285 nvlist_destroy(vdevs); 1286 1287 /* 1288 * MOS config has at least one child for root vdev. 1289 */ 1290 if (rc != 0) 1291 return (rc); 1292 1293 for (int i = 0; i < nkids; i++) { 1294 uint64_t guid; 1295 vdev_t *vdev; 1296 1297 rc = nvlist_find(kids[i], ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64, 1298 NULL, &guid, NULL); 1299 if (rc != 0) 1300 break; 1301 vdev = vdev_find(guid); 1302 /* 1303 * Top level vdev is missing, create it. 1304 */ 1305 if (vdev == NULL) 1306 rc = vdev_from_nvlist(spa, guid, kids[i]); 1307 else 1308 rc = vdev_update_from_nvlist(guid, kids[i]); 1309 if (rc != 0) 1310 break; 1311 } 1312 if (kids != NULL) { 1313 for (int i = 0; i < nkids; i++) 1314 nvlist_destroy(kids[i]); 1315 free(kids); 1316 } 1317 1318 /* 1319 * Re-evaluate top-level vdev state. 1320 */ 1321 vdev_set_state(spa->spa_root_vdev); 1322 1323 return (rc); 1324 } 1325 1326 static spa_t * 1327 spa_find_by_guid(uint64_t guid) 1328 { 1329 spa_t *spa; 1330 1331 STAILQ_FOREACH(spa, &zfs_pools, spa_link) 1332 if (spa->spa_guid == guid) 1333 return (spa); 1334 1335 return (NULL); 1336 } 1337 1338 static spa_t * 1339 spa_find_by_name(const char *name) 1340 { 1341 spa_t *spa; 1342 1343 STAILQ_FOREACH(spa, &zfs_pools, spa_link) 1344 if (strcmp(spa->spa_name, name) == 0) 1345 return (spa); 1346 1347 return (NULL); 1348 } 1349 1350 static spa_t * 1351 spa_find_by_dev(struct zfs_devdesc *dev) 1352 { 1353 1354 if (dev->dd.d_dev->dv_type != DEVT_ZFS) 1355 return (NULL); 1356 1357 if (dev->pool_guid == 0) 1358 return (STAILQ_FIRST(&zfs_pools)); 1359 1360 return (spa_find_by_guid(dev->pool_guid)); 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 * Note: bsize may not be a power of two here so we need to do an 2336 * actual divide rather than a bitshift. 2337 */ 2338 while (buflen > 0) { 2339 uint64_t bn = offset / bsize; 2340 int boff = offset % bsize; 2341 int ibn; 2342 const blkptr_t *indbp; 2343 blkptr_t bp; 2344 2345 if (bn > dnode->dn_maxblkid) 2346 return (EIO); 2347 2348 if (dnode == dnode_cache_obj && bn == dnode_cache_bn) 2349 goto cached; 2350 2351 indbp = dnode->dn_blkptr; 2352 for (i = 0; i < nlevels; i++) { 2353 /* 2354 * Copy the bp from the indirect array so that 2355 * we can re-use the scratch buffer for multi-level 2356 * objects. 2357 */ 2358 ibn = bn >> ((nlevels - i - 1) * ibshift); 2359 ibn &= ((1 << ibshift) - 1); 2360 bp = indbp[ibn]; 2361 if (BP_IS_HOLE(&bp)) { 2362 memset(dnode_cache_buf, 0, bsize); 2363 break; 2364 } 2365 rc = zio_read(spa, &bp, dnode_cache_buf); 2366 if (rc) 2367 return (rc); 2368 indbp = (const blkptr_t *) dnode_cache_buf; 2369 } 2370 dnode_cache_obj = dnode; 2371 dnode_cache_bn = bn; 2372 cached: 2373 2374 /* 2375 * The buffer contains our data block. Copy what we 2376 * need from it and loop. 2377 */ 2378 i = bsize - boff; 2379 if (i > buflen) i = buflen; 2380 memcpy(buf, &dnode_cache_buf[boff], i); 2381 buf = ((char *)buf) + i; 2382 offset += i; 2383 buflen -= i; 2384 } 2385 2386 return (0); 2387 } 2388 2389 /* 2390 * Lookup a value in a microzap directory. 2391 */ 2392 static int 2393 mzap_lookup(const mzap_phys_t *mz, size_t size, const char *name, 2394 uint64_t *value) 2395 { 2396 const mzap_ent_phys_t *mze; 2397 int chunks, i; 2398 2399 /* 2400 * Microzap objects use exactly one block. Read the whole 2401 * thing. 2402 */ 2403 chunks = size / MZAP_ENT_LEN - 1; 2404 for (i = 0; i < chunks; i++) { 2405 mze = &mz->mz_chunk[i]; 2406 if (strcmp(mze->mze_name, name) == 0) { 2407 *value = mze->mze_value; 2408 return (0); 2409 } 2410 } 2411 2412 return (ENOENT); 2413 } 2414 2415 /* 2416 * Compare a name with a zap leaf entry. Return non-zero if the name 2417 * matches. 2418 */ 2419 static int 2420 fzap_name_equal(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, 2421 const char *name) 2422 { 2423 size_t namelen; 2424 const zap_leaf_chunk_t *nc; 2425 const char *p; 2426 2427 namelen = zc->l_entry.le_name_numints; 2428 2429 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk); 2430 p = name; 2431 while (namelen > 0) { 2432 size_t len; 2433 2434 len = namelen; 2435 if (len > ZAP_LEAF_ARRAY_BYTES) 2436 len = ZAP_LEAF_ARRAY_BYTES; 2437 if (memcmp(p, nc->l_array.la_array, len)) 2438 return (0); 2439 p += len; 2440 namelen -= len; 2441 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next); 2442 } 2443 2444 return (1); 2445 } 2446 2447 /* 2448 * Extract a uint64_t value from a zap leaf entry. 2449 */ 2450 static uint64_t 2451 fzap_leaf_value(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc) 2452 { 2453 const zap_leaf_chunk_t *vc; 2454 int i; 2455 uint64_t value; 2456 const uint8_t *p; 2457 2458 vc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_value_chunk); 2459 for (i = 0, value = 0, p = vc->l_array.la_array; i < 8; i++) { 2460 value = (value << 8) | p[i]; 2461 } 2462 2463 return (value); 2464 } 2465 2466 static void 2467 stv(int len, void *addr, uint64_t value) 2468 { 2469 switch (len) { 2470 case 1: 2471 *(uint8_t *)addr = value; 2472 return; 2473 case 2: 2474 *(uint16_t *)addr = value; 2475 return; 2476 case 4: 2477 *(uint32_t *)addr = value; 2478 return; 2479 case 8: 2480 *(uint64_t *)addr = value; 2481 return; 2482 } 2483 } 2484 2485 /* 2486 * Extract a array from a zap leaf entry. 2487 */ 2488 static void 2489 fzap_leaf_array(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, 2490 uint64_t integer_size, uint64_t num_integers, void *buf) 2491 { 2492 uint64_t array_int_len = zc->l_entry.le_value_intlen; 2493 uint64_t value = 0; 2494 uint64_t *u64 = buf; 2495 char *p = buf; 2496 int len = MIN(zc->l_entry.le_value_numints, num_integers); 2497 int chunk = zc->l_entry.le_value_chunk; 2498 int byten = 0; 2499 2500 if (integer_size == 8 && len == 1) { 2501 *u64 = fzap_leaf_value(zl, zc); 2502 return; 2503 } 2504 2505 while (len > 0) { 2506 struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(zl, chunk).l_array; 2507 int i; 2508 2509 ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(zl)); 2510 for (i = 0; i < ZAP_LEAF_ARRAY_BYTES && len > 0; i++) { 2511 value = (value << 8) | la->la_array[i]; 2512 byten++; 2513 if (byten == array_int_len) { 2514 stv(integer_size, p, value); 2515 byten = 0; 2516 len--; 2517 if (len == 0) 2518 return; 2519 p += integer_size; 2520 } 2521 } 2522 chunk = la->la_next; 2523 } 2524 } 2525 2526 static int 2527 fzap_check_size(uint64_t integer_size, uint64_t num_integers) 2528 { 2529 2530 switch (integer_size) { 2531 case 1: 2532 case 2: 2533 case 4: 2534 case 8: 2535 break; 2536 default: 2537 return (EINVAL); 2538 } 2539 2540 if (integer_size * num_integers > ZAP_MAXVALUELEN) 2541 return (E2BIG); 2542 2543 return (0); 2544 } 2545 2546 static void 2547 zap_leaf_free(zap_leaf_t *leaf) 2548 { 2549 free(leaf->l_phys); 2550 free(leaf); 2551 } 2552 2553 static int 2554 zap_get_leaf_byblk(fat_zap_t *zap, uint64_t blk, zap_leaf_t **lp) 2555 { 2556 int bs = FZAP_BLOCK_SHIFT(zap); 2557 int err; 2558 2559 *lp = malloc(sizeof(**lp)); 2560 if (*lp == NULL) 2561 return (ENOMEM); 2562 2563 (*lp)->l_bs = bs; 2564 (*lp)->l_phys = malloc(1 << bs); 2565 2566 if ((*lp)->l_phys == NULL) { 2567 free(*lp); 2568 return (ENOMEM); 2569 } 2570 err = dnode_read(zap->zap_spa, zap->zap_dnode, blk << bs, (*lp)->l_phys, 2571 1 << bs); 2572 if (err != 0) { 2573 zap_leaf_free(*lp); 2574 } 2575 return (err); 2576 } 2577 2578 static int 2579 zap_table_load(fat_zap_t *zap, zap_table_phys_t *tbl, uint64_t idx, 2580 uint64_t *valp) 2581 { 2582 int bs = FZAP_BLOCK_SHIFT(zap); 2583 uint64_t blk = idx >> (bs - 3); 2584 uint64_t off = idx & ((1 << (bs - 3)) - 1); 2585 uint64_t *buf; 2586 int rc; 2587 2588 buf = malloc(1 << zap->zap_block_shift); 2589 if (buf == NULL) 2590 return (ENOMEM); 2591 rc = dnode_read(zap->zap_spa, zap->zap_dnode, (tbl->zt_blk + blk) << bs, 2592 buf, 1 << zap->zap_block_shift); 2593 if (rc == 0) 2594 *valp = buf[off]; 2595 free(buf); 2596 return (rc); 2597 } 2598 2599 static int 2600 zap_idx_to_blk(fat_zap_t *zap, uint64_t idx, uint64_t *valp) 2601 { 2602 if (zap->zap_phys->zap_ptrtbl.zt_numblks == 0) { 2603 *valp = ZAP_EMBEDDED_PTRTBL_ENT(zap, idx); 2604 return (0); 2605 } else { 2606 return (zap_table_load(zap, &zap->zap_phys->zap_ptrtbl, 2607 idx, valp)); 2608 } 2609 } 2610 2611 #define ZAP_HASH_IDX(hash, n) (((n) == 0) ? 0 : ((hash) >> (64 - (n)))) 2612 static int 2613 zap_deref_leaf(fat_zap_t *zap, uint64_t h, zap_leaf_t **lp) 2614 { 2615 uint64_t idx, blk; 2616 int err; 2617 2618 idx = ZAP_HASH_IDX(h, zap->zap_phys->zap_ptrtbl.zt_shift); 2619 err = zap_idx_to_blk(zap, idx, &blk); 2620 if (err != 0) 2621 return (err); 2622 return (zap_get_leaf_byblk(zap, blk, lp)); 2623 } 2624 2625 #define CHAIN_END 0xffff /* end of the chunk chain */ 2626 #define LEAF_HASH(l, h) \ 2627 ((ZAP_LEAF_HASH_NUMENTRIES(l)-1) & \ 2628 ((h) >> \ 2629 (64 - ZAP_LEAF_HASH_SHIFT(l) - (l)->l_phys->l_hdr.lh_prefix_len))) 2630 #define LEAF_HASH_ENTPTR(l, h) (&(l)->l_phys->l_hash[LEAF_HASH(l, h)]) 2631 2632 static int 2633 zap_leaf_lookup(zap_leaf_t *zl, uint64_t hash, const char *name, 2634 uint64_t integer_size, uint64_t num_integers, void *value) 2635 { 2636 int rc; 2637 uint16_t *chunkp; 2638 struct zap_leaf_entry *le; 2639 2640 /* 2641 * Make sure this chunk matches our hash. 2642 */ 2643 if (zl->l_phys->l_hdr.lh_prefix_len > 0 && 2644 zl->l_phys->l_hdr.lh_prefix != 2645 hash >> (64 - zl->l_phys->l_hdr.lh_prefix_len)) 2646 return (EIO); 2647 2648 rc = ENOENT; 2649 for (chunkp = LEAF_HASH_ENTPTR(zl, hash); 2650 *chunkp != CHAIN_END; chunkp = &le->le_next) { 2651 zap_leaf_chunk_t *zc; 2652 uint16_t chunk = *chunkp; 2653 2654 le = ZAP_LEAF_ENTRY(zl, chunk); 2655 if (le->le_hash != hash) 2656 continue; 2657 zc = &ZAP_LEAF_CHUNK(zl, chunk); 2658 if (fzap_name_equal(zl, zc, name)) { 2659 if (zc->l_entry.le_value_intlen > integer_size) { 2660 rc = EINVAL; 2661 } else { 2662 fzap_leaf_array(zl, zc, integer_size, 2663 num_integers, value); 2664 rc = 0; 2665 } 2666 break; 2667 } 2668 } 2669 return (rc); 2670 } 2671 2672 /* 2673 * Lookup a value in a fatzap directory. 2674 */ 2675 static int 2676 fzap_lookup(const spa_t *spa, const dnode_phys_t *dnode, zap_phys_t *zh, 2677 const char *name, uint64_t integer_size, uint64_t num_integers, 2678 void *value) 2679 { 2680 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT; 2681 fat_zap_t z; 2682 zap_leaf_t *zl; 2683 uint64_t hash; 2684 int rc; 2685 2686 if (zh->zap_magic != ZAP_MAGIC) 2687 return (EIO); 2688 2689 if ((rc = fzap_check_size(integer_size, num_integers)) != 0) { 2690 return (rc); 2691 } 2692 2693 z.zap_block_shift = ilog2(bsize); 2694 z.zap_phys = zh; 2695 z.zap_spa = spa; 2696 z.zap_dnode = dnode; 2697 2698 hash = zap_hash(zh->zap_salt, name); 2699 rc = zap_deref_leaf(&z, hash, &zl); 2700 if (rc != 0) 2701 return (rc); 2702 2703 rc = zap_leaf_lookup(zl, hash, name, integer_size, num_integers, value); 2704 2705 zap_leaf_free(zl); 2706 return (rc); 2707 } 2708 2709 /* 2710 * Lookup a name in a zap object and return its value as a uint64_t. 2711 */ 2712 static int 2713 zap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name, 2714 uint64_t integer_size, uint64_t num_integers, void *value) 2715 { 2716 int rc; 2717 zap_phys_t *zap; 2718 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT; 2719 2720 zap = malloc(size); 2721 if (zap == NULL) 2722 return (ENOMEM); 2723 2724 rc = dnode_read(spa, dnode, 0, zap, size); 2725 if (rc) 2726 goto done; 2727 2728 switch (zap->zap_block_type) { 2729 case ZBT_MICRO: 2730 rc = mzap_lookup((const mzap_phys_t *)zap, size, name, value); 2731 break; 2732 case ZBT_HEADER: 2733 rc = fzap_lookup(spa, dnode, zap, name, integer_size, 2734 num_integers, value); 2735 break; 2736 default: 2737 printf("ZFS: invalid zap_type=%" PRIx64 "\n", 2738 zap->zap_block_type); 2739 rc = EIO; 2740 } 2741 done: 2742 free(zap); 2743 return (rc); 2744 } 2745 2746 /* 2747 * List a microzap directory. 2748 */ 2749 static int 2750 mzap_list(const mzap_phys_t *mz, size_t size, 2751 int (*callback)(const char *, uint64_t)) 2752 { 2753 const mzap_ent_phys_t *mze; 2754 int chunks, i, rc; 2755 2756 /* 2757 * Microzap objects use exactly one block. Read the whole 2758 * thing. 2759 */ 2760 rc = 0; 2761 chunks = size / MZAP_ENT_LEN - 1; 2762 for (i = 0; i < chunks; i++) { 2763 mze = &mz->mz_chunk[i]; 2764 if (mze->mze_name[0]) { 2765 rc = callback(mze->mze_name, mze->mze_value); 2766 if (rc != 0) 2767 break; 2768 } 2769 } 2770 2771 return (rc); 2772 } 2773 2774 /* 2775 * List a fatzap directory. 2776 */ 2777 static int 2778 fzap_list(const spa_t *spa, const dnode_phys_t *dnode, zap_phys_t *zh, 2779 int (*callback)(const char *, uint64_t)) 2780 { 2781 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT; 2782 fat_zap_t z; 2783 uint64_t i; 2784 int j, rc; 2785 2786 if (zh->zap_magic != ZAP_MAGIC) 2787 return (EIO); 2788 2789 z.zap_block_shift = ilog2(bsize); 2790 z.zap_phys = zh; 2791 2792 /* 2793 * This assumes that the leaf blocks start at block 1. The 2794 * documentation isn't exactly clear on this. 2795 */ 2796 zap_leaf_t zl; 2797 zl.l_bs = z.zap_block_shift; 2798 zl.l_phys = malloc(bsize); 2799 if (zl.l_phys == NULL) 2800 return (ENOMEM); 2801 2802 for (i = 0; i < zh->zap_num_leafs; i++) { 2803 off_t off = ((off_t)(i + 1)) << zl.l_bs; 2804 char name[256], *p; 2805 uint64_t value; 2806 2807 if (dnode_read(spa, dnode, off, zl.l_phys, bsize)) { 2808 free(zl.l_phys); 2809 return (EIO); 2810 } 2811 2812 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) { 2813 zap_leaf_chunk_t *zc, *nc; 2814 int namelen; 2815 2816 zc = &ZAP_LEAF_CHUNK(&zl, j); 2817 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY) 2818 continue; 2819 namelen = zc->l_entry.le_name_numints; 2820 if (namelen > sizeof(name)) 2821 namelen = sizeof(name); 2822 2823 /* 2824 * Paste the name back together. 2825 */ 2826 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk); 2827 p = name; 2828 while (namelen > 0) { 2829 int len; 2830 len = namelen; 2831 if (len > ZAP_LEAF_ARRAY_BYTES) 2832 len = ZAP_LEAF_ARRAY_BYTES; 2833 memcpy(p, nc->l_array.la_array, len); 2834 p += len; 2835 namelen -= len; 2836 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next); 2837 } 2838 2839 /* 2840 * Assume the first eight bytes of the value are 2841 * a uint64_t. 2842 */ 2843 value = fzap_leaf_value(&zl, zc); 2844 2845 /* printf("%s 0x%jx\n", name, (uintmax_t)value); */ 2846 rc = callback((const char *)name, value); 2847 if (rc != 0) { 2848 free(zl.l_phys); 2849 return (rc); 2850 } 2851 } 2852 } 2853 2854 free(zl.l_phys); 2855 return (0); 2856 } 2857 2858 static int zfs_printf(const char *name, uint64_t value __unused) 2859 { 2860 2861 printf("%s\n", name); 2862 2863 return (0); 2864 } 2865 2866 /* 2867 * List a zap directory. 2868 */ 2869 static int 2870 zap_list(const spa_t *spa, const dnode_phys_t *dnode) 2871 { 2872 zap_phys_t *zap; 2873 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT; 2874 int rc; 2875 2876 zap = malloc(size); 2877 if (zap == NULL) 2878 return (ENOMEM); 2879 2880 rc = dnode_read(spa, dnode, 0, zap, size); 2881 if (rc == 0) { 2882 if (zap->zap_block_type == ZBT_MICRO) 2883 rc = mzap_list((const mzap_phys_t *)zap, size, 2884 zfs_printf); 2885 else 2886 rc = fzap_list(spa, dnode, zap, zfs_printf); 2887 } 2888 free(zap); 2889 return (rc); 2890 } 2891 2892 static int 2893 objset_get_dnode(const spa_t *spa, const objset_phys_t *os, uint64_t objnum, 2894 dnode_phys_t *dnode) 2895 { 2896 off_t offset; 2897 2898 offset = objnum * sizeof(dnode_phys_t); 2899 return dnode_read(spa, &os->os_meta_dnode, offset, 2900 dnode, sizeof(dnode_phys_t)); 2901 } 2902 2903 /* 2904 * Lookup a name in a microzap directory. 2905 */ 2906 static int 2907 mzap_rlookup(const mzap_phys_t *mz, size_t size, char *name, uint64_t value) 2908 { 2909 const mzap_ent_phys_t *mze; 2910 int chunks, i; 2911 2912 /* 2913 * Microzap objects use exactly one block. Read the whole 2914 * thing. 2915 */ 2916 chunks = size / MZAP_ENT_LEN - 1; 2917 for (i = 0; i < chunks; i++) { 2918 mze = &mz->mz_chunk[i]; 2919 if (value == mze->mze_value) { 2920 strcpy(name, mze->mze_name); 2921 return (0); 2922 } 2923 } 2924 2925 return (ENOENT); 2926 } 2927 2928 static void 2929 fzap_name_copy(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, char *name) 2930 { 2931 size_t namelen; 2932 const zap_leaf_chunk_t *nc; 2933 char *p; 2934 2935 namelen = zc->l_entry.le_name_numints; 2936 2937 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk); 2938 p = name; 2939 while (namelen > 0) { 2940 size_t len; 2941 len = namelen; 2942 if (len > ZAP_LEAF_ARRAY_BYTES) 2943 len = ZAP_LEAF_ARRAY_BYTES; 2944 memcpy(p, nc->l_array.la_array, len); 2945 p += len; 2946 namelen -= len; 2947 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next); 2948 } 2949 2950 *p = '\0'; 2951 } 2952 2953 static int 2954 fzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, zap_phys_t *zh, 2955 char *name, uint64_t value) 2956 { 2957 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT; 2958 fat_zap_t z; 2959 uint64_t i; 2960 int j, rc; 2961 2962 if (zh->zap_magic != ZAP_MAGIC) 2963 return (EIO); 2964 2965 z.zap_block_shift = ilog2(bsize); 2966 z.zap_phys = zh; 2967 2968 /* 2969 * This assumes that the leaf blocks start at block 1. The 2970 * documentation isn't exactly clear on this. 2971 */ 2972 zap_leaf_t zl; 2973 zl.l_bs = z.zap_block_shift; 2974 zl.l_phys = malloc(bsize); 2975 if (zl.l_phys == NULL) 2976 return (ENOMEM); 2977 2978 for (i = 0; i < zh->zap_num_leafs; i++) { 2979 off_t off = ((off_t)(i + 1)) << zl.l_bs; 2980 2981 rc = dnode_read(spa, dnode, off, zl.l_phys, bsize); 2982 if (rc != 0) 2983 goto done; 2984 2985 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) { 2986 zap_leaf_chunk_t *zc; 2987 2988 zc = &ZAP_LEAF_CHUNK(&zl, j); 2989 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY) 2990 continue; 2991 if (zc->l_entry.le_value_intlen != 8 || 2992 zc->l_entry.le_value_numints != 1) 2993 continue; 2994 2995 if (fzap_leaf_value(&zl, zc) == value) { 2996 fzap_name_copy(&zl, zc, name); 2997 goto done; 2998 } 2999 } 3000 } 3001 3002 rc = ENOENT; 3003 done: 3004 free(zl.l_phys); 3005 return (rc); 3006 } 3007 3008 static int 3009 zap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, 3010 uint64_t value) 3011 { 3012 zap_phys_t *zap; 3013 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT; 3014 int rc; 3015 3016 zap = malloc(size); 3017 if (zap == NULL) 3018 return (ENOMEM); 3019 3020 rc = dnode_read(spa, dnode, 0, zap, size); 3021 if (rc == 0) { 3022 if (zap->zap_block_type == ZBT_MICRO) 3023 rc = mzap_rlookup((const mzap_phys_t *)zap, size, 3024 name, value); 3025 else 3026 rc = fzap_rlookup(spa, dnode, zap, name, value); 3027 } 3028 free(zap); 3029 return (rc); 3030 } 3031 3032 static int 3033 zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result) 3034 { 3035 char name[256]; 3036 char component[256]; 3037 uint64_t dir_obj, parent_obj, child_dir_zapobj; 3038 dnode_phys_t child_dir_zap, dataset, dir, parent; 3039 dsl_dir_phys_t *dd; 3040 dsl_dataset_phys_t *ds; 3041 char *p; 3042 int len; 3043 3044 p = &name[sizeof(name) - 1]; 3045 *p = '\0'; 3046 3047 if (objset_get_dnode(spa, spa->spa_mos, objnum, &dataset)) { 3048 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum); 3049 return (EIO); 3050 } 3051 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus; 3052 dir_obj = ds->ds_dir_obj; 3053 3054 for (;;) { 3055 if (objset_get_dnode(spa, spa->spa_mos, dir_obj, &dir) != 0) 3056 return (EIO); 3057 dd = (dsl_dir_phys_t *)&dir.dn_bonus; 3058 3059 /* Actual loop condition. */ 3060 parent_obj = dd->dd_parent_obj; 3061 if (parent_obj == 0) 3062 break; 3063 3064 if (objset_get_dnode(spa, spa->spa_mos, parent_obj, 3065 &parent) != 0) 3066 return (EIO); 3067 dd = (dsl_dir_phys_t *)&parent.dn_bonus; 3068 child_dir_zapobj = dd->dd_child_dir_zapobj; 3069 if (objset_get_dnode(spa, spa->spa_mos, child_dir_zapobj, 3070 &child_dir_zap) != 0) 3071 return (EIO); 3072 if (zap_rlookup(spa, &child_dir_zap, component, dir_obj) != 0) 3073 return (EIO); 3074 3075 len = strlen(component); 3076 p -= len; 3077 memcpy(p, component, len); 3078 --p; 3079 *p = '/'; 3080 3081 /* Actual loop iteration. */ 3082 dir_obj = parent_obj; 3083 } 3084 3085 if (*p != '\0') 3086 ++p; 3087 strcpy(result, p); 3088 3089 return (0); 3090 } 3091 3092 static int 3093 zfs_lookup_dataset(const spa_t *spa, const char *name, uint64_t *objnum) 3094 { 3095 char element[256]; 3096 uint64_t dir_obj, child_dir_zapobj; 3097 dnode_phys_t child_dir_zap, dir; 3098 dsl_dir_phys_t *dd; 3099 const char *p, *q; 3100 3101 if (objset_get_dnode(spa, spa->spa_mos, 3102 DMU_POOL_DIRECTORY_OBJECT, &dir)) 3103 return (EIO); 3104 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, sizeof (dir_obj), 3105 1, &dir_obj)) 3106 return (EIO); 3107 3108 p = name; 3109 for (;;) { 3110 if (objset_get_dnode(spa, spa->spa_mos, dir_obj, &dir)) 3111 return (EIO); 3112 dd = (dsl_dir_phys_t *)&dir.dn_bonus; 3113 3114 while (*p == '/') 3115 p++; 3116 /* Actual loop condition #1. */ 3117 if (*p == '\0') 3118 break; 3119 3120 q = strchr(p, '/'); 3121 if (q) { 3122 memcpy(element, p, q - p); 3123 element[q - p] = '\0'; 3124 p = q + 1; 3125 } else { 3126 strcpy(element, p); 3127 p += strlen(p); 3128 } 3129 3130 child_dir_zapobj = dd->dd_child_dir_zapobj; 3131 if (objset_get_dnode(spa, spa->spa_mos, child_dir_zapobj, 3132 &child_dir_zap) != 0) 3133 return (EIO); 3134 3135 /* Actual loop condition #2. */ 3136 if (zap_lookup(spa, &child_dir_zap, element, sizeof (dir_obj), 3137 1, &dir_obj) != 0) 3138 return (ENOENT); 3139 } 3140 3141 *objnum = dd->dd_head_dataset_obj; 3142 return (0); 3143 } 3144 3145 #ifndef BOOT2 3146 static int 3147 zfs_list_dataset(const spa_t *spa, uint64_t objnum/*, int pos, char *entry*/) 3148 { 3149 uint64_t dir_obj, child_dir_zapobj; 3150 dnode_phys_t child_dir_zap, dir, dataset; 3151 dsl_dataset_phys_t *ds; 3152 dsl_dir_phys_t *dd; 3153 3154 if (objset_get_dnode(spa, spa->spa_mos, objnum, &dataset)) { 3155 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum); 3156 return (EIO); 3157 } 3158 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus; 3159 dir_obj = ds->ds_dir_obj; 3160 3161 if (objset_get_dnode(spa, spa->spa_mos, dir_obj, &dir)) { 3162 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj); 3163 return (EIO); 3164 } 3165 dd = (dsl_dir_phys_t *)&dir.dn_bonus; 3166 3167 child_dir_zapobj = dd->dd_child_dir_zapobj; 3168 if (objset_get_dnode(spa, spa->spa_mos, child_dir_zapobj, 3169 &child_dir_zap) != 0) { 3170 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj); 3171 return (EIO); 3172 } 3173 3174 return (zap_list(spa, &child_dir_zap) != 0); 3175 } 3176 3177 int 3178 zfs_callback_dataset(const spa_t *spa, uint64_t objnum, 3179 int (*callback)(const char *, uint64_t)) 3180 { 3181 uint64_t dir_obj, child_dir_zapobj; 3182 dnode_phys_t child_dir_zap, dir, dataset; 3183 dsl_dataset_phys_t *ds; 3184 dsl_dir_phys_t *dd; 3185 zap_phys_t *zap; 3186 size_t size; 3187 int err; 3188 3189 err = objset_get_dnode(spa, spa->spa_mos, objnum, &dataset); 3190 if (err != 0) { 3191 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum); 3192 return (err); 3193 } 3194 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus; 3195 dir_obj = ds->ds_dir_obj; 3196 3197 err = objset_get_dnode(spa, spa->spa_mos, dir_obj, &dir); 3198 if (err != 0) { 3199 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj); 3200 return (err); 3201 } 3202 dd = (dsl_dir_phys_t *)&dir.dn_bonus; 3203 3204 child_dir_zapobj = dd->dd_child_dir_zapobj; 3205 err = objset_get_dnode(spa, spa->spa_mos, child_dir_zapobj, 3206 &child_dir_zap); 3207 if (err != 0) { 3208 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj); 3209 return (err); 3210 } 3211 3212 size = child_dir_zap.dn_datablkszsec << SPA_MINBLOCKSHIFT; 3213 zap = malloc(size); 3214 if (zap != NULL) { 3215 err = dnode_read(spa, &child_dir_zap, 0, zap, size); 3216 if (err != 0) 3217 goto done; 3218 3219 if (zap->zap_block_type == ZBT_MICRO) 3220 err = mzap_list((const mzap_phys_t *)zap, size, 3221 callback); 3222 else 3223 err = fzap_list(spa, &child_dir_zap, zap, callback); 3224 } else { 3225 err = ENOMEM; 3226 } 3227 done: 3228 free(zap); 3229 return (err); 3230 } 3231 #endif 3232 3233 /* 3234 * Find the object set given the object number of its dataset object 3235 * and return its details in *objset 3236 */ 3237 static int 3238 zfs_mount_dataset(const spa_t *spa, uint64_t objnum, objset_phys_t *objset) 3239 { 3240 dnode_phys_t dataset; 3241 dsl_dataset_phys_t *ds; 3242 3243 if (objset_get_dnode(spa, spa->spa_mos, objnum, &dataset)) { 3244 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum); 3245 return (EIO); 3246 } 3247 3248 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus; 3249 if (zio_read(spa, &ds->ds_bp, objset)) { 3250 printf("ZFS: can't read object set for dataset %ju\n", 3251 (uintmax_t)objnum); 3252 return (EIO); 3253 } 3254 3255 return (0); 3256 } 3257 3258 /* 3259 * Find the object set pointed to by the BOOTFS property or the root 3260 * dataset if there is none and return its details in *objset 3261 */ 3262 static int 3263 zfs_get_root(const spa_t *spa, uint64_t *objid) 3264 { 3265 dnode_phys_t dir, propdir; 3266 uint64_t props, bootfs, root; 3267 3268 *objid = 0; 3269 3270 /* 3271 * Start with the MOS directory object. 3272 */ 3273 if (objset_get_dnode(spa, spa->spa_mos, 3274 DMU_POOL_DIRECTORY_OBJECT, &dir)) { 3275 printf("ZFS: can't read MOS object directory\n"); 3276 return (EIO); 3277 } 3278 3279 /* 3280 * Lookup the pool_props and see if we can find a bootfs. 3281 */ 3282 if (zap_lookup(spa, &dir, DMU_POOL_PROPS, 3283 sizeof(props), 1, &props) == 0 && 3284 objset_get_dnode(spa, spa->spa_mos, props, &propdir) == 0 && 3285 zap_lookup(spa, &propdir, "bootfs", 3286 sizeof(bootfs), 1, &bootfs) == 0 && bootfs != 0) { 3287 *objid = bootfs; 3288 return (0); 3289 } 3290 /* 3291 * Lookup the root dataset directory 3292 */ 3293 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, 3294 sizeof(root), 1, &root) || 3295 objset_get_dnode(spa, spa->spa_mos, root, &dir)) { 3296 printf("ZFS: can't find root dsl_dir\n"); 3297 return (EIO); 3298 } 3299 3300 /* 3301 * Use the information from the dataset directory's bonus buffer 3302 * to find the dataset object and from that the object set itself. 3303 */ 3304 dsl_dir_phys_t *dd = (dsl_dir_phys_t *)&dir.dn_bonus; 3305 *objid = dd->dd_head_dataset_obj; 3306 return (0); 3307 } 3308 3309 static int 3310 zfs_mount(const spa_t *spa, uint64_t rootobj, struct zfsmount *mount) 3311 { 3312 3313 mount->spa = spa; 3314 3315 /* 3316 * Find the root object set if not explicitly provided 3317 */ 3318 if (rootobj == 0 && zfs_get_root(spa, &rootobj)) { 3319 printf("ZFS: can't find root filesystem\n"); 3320 return (EIO); 3321 } 3322 3323 if (zfs_mount_dataset(spa, rootobj, &mount->objset)) { 3324 printf("ZFS: can't open root filesystem\n"); 3325 return (EIO); 3326 } 3327 3328 mount->rootobj = rootobj; 3329 3330 return (0); 3331 } 3332 3333 /* 3334 * callback function for feature name checks. 3335 */ 3336 static int 3337 check_feature(const char *name, uint64_t value) 3338 { 3339 int i; 3340 3341 if (value == 0) 3342 return (0); 3343 if (name[0] == '\0') 3344 return (0); 3345 3346 for (i = 0; features_for_read[i] != NULL; i++) { 3347 if (strcmp(name, features_for_read[i]) == 0) 3348 return (0); 3349 } 3350 printf("ZFS: unsupported feature: %s\n", name); 3351 return (EIO); 3352 } 3353 3354 /* 3355 * Checks whether the MOS features that are active are supported. 3356 */ 3357 static int 3358 check_mos_features(const spa_t *spa) 3359 { 3360 dnode_phys_t dir; 3361 zap_phys_t *zap; 3362 uint64_t objnum; 3363 size_t size; 3364 int rc; 3365 3366 if ((rc = objset_get_dnode(spa, spa->spa_mos, DMU_OT_OBJECT_DIRECTORY, 3367 &dir)) != 0) 3368 return (rc); 3369 if ((rc = zap_lookup(spa, &dir, DMU_POOL_FEATURES_FOR_READ, 3370 sizeof (objnum), 1, &objnum)) != 0) { 3371 /* 3372 * It is older pool without features. As we have already 3373 * tested the label, just return without raising the error. 3374 */ 3375 return (0); 3376 } 3377 3378 if ((rc = objset_get_dnode(spa, spa->spa_mos, objnum, &dir)) != 0) 3379 return (rc); 3380 3381 if (dir.dn_type != DMU_OTN_ZAP_METADATA) 3382 return (EIO); 3383 3384 size = dir.dn_datablkszsec << SPA_MINBLOCKSHIFT; 3385 zap = malloc(size); 3386 if (zap == NULL) 3387 return (ENOMEM); 3388 3389 if (dnode_read(spa, &dir, 0, zap, size)) { 3390 free(zap); 3391 return (EIO); 3392 } 3393 3394 if (zap->zap_block_type == ZBT_MICRO) 3395 rc = mzap_list((const mzap_phys_t *)zap, size, check_feature); 3396 else 3397 rc = fzap_list(spa, &dir, zap, check_feature); 3398 3399 free(zap); 3400 return (rc); 3401 } 3402 3403 static int 3404 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) 3405 { 3406 dnode_phys_t dir; 3407 size_t size; 3408 int rc; 3409 char *nv; 3410 3411 *value = NULL; 3412 if ((rc = objset_get_dnode(spa, spa->spa_mos, obj, &dir)) != 0) 3413 return (rc); 3414 if (dir.dn_type != DMU_OT_PACKED_NVLIST && 3415 dir.dn_bonustype != DMU_OT_PACKED_NVLIST_SIZE) { 3416 return (EIO); 3417 } 3418 3419 if (dir.dn_bonuslen != sizeof (uint64_t)) 3420 return (EIO); 3421 3422 size = *(uint64_t *)DN_BONUS(&dir); 3423 nv = malloc(size); 3424 if (nv == NULL) 3425 return (ENOMEM); 3426 3427 rc = dnode_read(spa, &dir, 0, nv, size); 3428 if (rc != 0) { 3429 free(nv); 3430 nv = NULL; 3431 return (rc); 3432 } 3433 *value = nvlist_import(nv, size); 3434 free(nv); 3435 return (rc); 3436 } 3437 3438 static int 3439 zfs_spa_init(spa_t *spa) 3440 { 3441 struct uberblock checkpoint; 3442 dnode_phys_t dir; 3443 uint64_t config_object; 3444 nvlist_t *nvlist; 3445 int rc; 3446 3447 if (zio_read(spa, &spa->spa_uberblock->ub_rootbp, spa->spa_mos)) { 3448 printf("ZFS: can't read MOS of pool %s\n", spa->spa_name); 3449 return (EIO); 3450 } 3451 if (spa->spa_mos->os_type != DMU_OST_META) { 3452 printf("ZFS: corrupted MOS of pool %s\n", spa->spa_name); 3453 return (EIO); 3454 } 3455 3456 if (objset_get_dnode(spa, &spa->spa_mos_master, 3457 DMU_POOL_DIRECTORY_OBJECT, &dir)) { 3458 printf("ZFS: failed to read pool %s directory object\n", 3459 spa->spa_name); 3460 return (EIO); 3461 } 3462 /* this is allowed to fail, older pools do not have salt */ 3463 rc = zap_lookup(spa, &dir, DMU_POOL_CHECKSUM_SALT, 1, 3464 sizeof (spa->spa_cksum_salt.zcs_bytes), 3465 spa->spa_cksum_salt.zcs_bytes); 3466 3467 rc = check_mos_features(spa); 3468 if (rc != 0) { 3469 printf("ZFS: pool %s is not supported\n", spa->spa_name); 3470 return (rc); 3471 } 3472 3473 rc = zap_lookup(spa, &dir, DMU_POOL_CONFIG, 3474 sizeof (config_object), 1, &config_object); 3475 if (rc != 0) { 3476 printf("ZFS: can not read MOS %s\n", DMU_POOL_CONFIG); 3477 return (EIO); 3478 } 3479 rc = load_nvlist(spa, config_object, &nvlist); 3480 if (rc != 0) 3481 return (rc); 3482 3483 rc = zap_lookup(spa, &dir, DMU_POOL_ZPOOL_CHECKPOINT, 3484 sizeof(uint64_t), sizeof(checkpoint) / sizeof(uint64_t), 3485 &checkpoint); 3486 if (rc == 0 && checkpoint.ub_checkpoint_txg != 0) { 3487 memcpy(&spa->spa_uberblock_checkpoint, &checkpoint, 3488 sizeof(checkpoint)); 3489 if (zio_read(spa, &spa->spa_uberblock_checkpoint.ub_rootbp, 3490 &spa->spa_mos_checkpoint)) { 3491 printf("ZFS: can not read checkpoint data.\n"); 3492 return (EIO); 3493 } 3494 } 3495 3496 /* 3497 * Update vdevs from MOS config. Note, we do skip encoding bytes 3498 * here. See also vdev_label_read_config(). 3499 */ 3500 rc = vdev_init_from_nvlist(spa, nvlist); 3501 nvlist_destroy(nvlist); 3502 return (rc); 3503 } 3504 3505 static int 3506 zfs_dnode_stat(const spa_t *spa, dnode_phys_t *dn, struct stat *sb) 3507 { 3508 3509 if (dn->dn_bonustype != DMU_OT_SA) { 3510 znode_phys_t *zp = (znode_phys_t *)dn->dn_bonus; 3511 3512 sb->st_mode = zp->zp_mode; 3513 sb->st_uid = zp->zp_uid; 3514 sb->st_gid = zp->zp_gid; 3515 sb->st_size = zp->zp_size; 3516 } else { 3517 sa_hdr_phys_t *sahdrp; 3518 int hdrsize; 3519 size_t size = 0; 3520 void *buf = NULL; 3521 3522 if (dn->dn_bonuslen != 0) 3523 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn); 3524 else { 3525 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0) { 3526 blkptr_t *bp = DN_SPILL_BLKPTR(dn); 3527 int error; 3528 3529 size = BP_GET_LSIZE(bp); 3530 buf = malloc(size); 3531 if (buf == NULL) 3532 error = ENOMEM; 3533 else 3534 error = zio_read(spa, bp, buf); 3535 3536 if (error != 0) { 3537 free(buf); 3538 return (error); 3539 } 3540 sahdrp = buf; 3541 } else { 3542 return (EIO); 3543 } 3544 } 3545 hdrsize = SA_HDR_SIZE(sahdrp); 3546 sb->st_mode = *(uint64_t *)((char *)sahdrp + hdrsize + 3547 SA_MODE_OFFSET); 3548 sb->st_uid = *(uint64_t *)((char *)sahdrp + hdrsize + 3549 SA_UID_OFFSET); 3550 sb->st_gid = *(uint64_t *)((char *)sahdrp + hdrsize + 3551 SA_GID_OFFSET); 3552 sb->st_size = *(uint64_t *)((char *)sahdrp + hdrsize + 3553 SA_SIZE_OFFSET); 3554 free(buf); 3555 } 3556 3557 return (0); 3558 } 3559 3560 static int 3561 zfs_dnode_readlink(const spa_t *spa, dnode_phys_t *dn, char *path, size_t psize) 3562 { 3563 int rc = 0; 3564 3565 if (dn->dn_bonustype == DMU_OT_SA) { 3566 sa_hdr_phys_t *sahdrp = NULL; 3567 size_t size = 0; 3568 void *buf = NULL; 3569 int hdrsize; 3570 char *p; 3571 3572 if (dn->dn_bonuslen != 0) { 3573 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn); 3574 } else { 3575 blkptr_t *bp; 3576 3577 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) == 0) 3578 return (EIO); 3579 bp = DN_SPILL_BLKPTR(dn); 3580 3581 size = BP_GET_LSIZE(bp); 3582 buf = malloc(size); 3583 if (buf == NULL) 3584 rc = ENOMEM; 3585 else 3586 rc = zio_read(spa, bp, buf); 3587 if (rc != 0) { 3588 free(buf); 3589 return (rc); 3590 } 3591 sahdrp = buf; 3592 } 3593 hdrsize = SA_HDR_SIZE(sahdrp); 3594 p = (char *)((uintptr_t)sahdrp + hdrsize + SA_SYMLINK_OFFSET); 3595 memcpy(path, p, psize); 3596 free(buf); 3597 return (0); 3598 } 3599 /* 3600 * Second test is purely to silence bogus compiler 3601 * warning about accessing past the end of dn_bonus. 3602 */ 3603 if (psize + sizeof(znode_phys_t) <= dn->dn_bonuslen && 3604 sizeof(znode_phys_t) <= sizeof(dn->dn_bonus)) { 3605 memcpy(path, &dn->dn_bonus[sizeof(znode_phys_t)], psize); 3606 } else { 3607 rc = dnode_read(spa, dn, 0, path, psize); 3608 } 3609 return (rc); 3610 } 3611 3612 struct obj_list { 3613 uint64_t objnum; 3614 STAILQ_ENTRY(obj_list) entry; 3615 }; 3616 3617 /* 3618 * Lookup a file and return its dnode. 3619 */ 3620 static int 3621 zfs_lookup(const struct zfsmount *mount, const char *upath, dnode_phys_t *dnode) 3622 { 3623 int rc; 3624 uint64_t objnum; 3625 const spa_t *spa; 3626 dnode_phys_t dn; 3627 const char *p, *q; 3628 char element[256]; 3629 char path[1024]; 3630 int symlinks_followed = 0; 3631 struct stat sb; 3632 struct obj_list *entry, *tentry; 3633 STAILQ_HEAD(, obj_list) on_cache = STAILQ_HEAD_INITIALIZER(on_cache); 3634 3635 spa = mount->spa; 3636 if (mount->objset.os_type != DMU_OST_ZFS) { 3637 printf("ZFS: unexpected object set type %ju\n", 3638 (uintmax_t)mount->objset.os_type); 3639 return (EIO); 3640 } 3641 3642 if ((entry = malloc(sizeof(struct obj_list))) == NULL) 3643 return (ENOMEM); 3644 3645 /* 3646 * Get the root directory dnode. 3647 */ 3648 rc = objset_get_dnode(spa, &mount->objset, MASTER_NODE_OBJ, &dn); 3649 if (rc) { 3650 free(entry); 3651 return (rc); 3652 } 3653 3654 rc = zap_lookup(spa, &dn, ZFS_ROOT_OBJ, sizeof(objnum), 1, &objnum); 3655 if (rc) { 3656 free(entry); 3657 return (rc); 3658 } 3659 entry->objnum = objnum; 3660 STAILQ_INSERT_HEAD(&on_cache, entry, entry); 3661 3662 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn); 3663 if (rc != 0) 3664 goto done; 3665 3666 p = upath; 3667 while (p && *p) { 3668 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn); 3669 if (rc != 0) 3670 goto done; 3671 3672 while (*p == '/') 3673 p++; 3674 if (*p == '\0') 3675 break; 3676 q = p; 3677 while (*q != '\0' && *q != '/') 3678 q++; 3679 3680 /* skip dot */ 3681 if (p + 1 == q && p[0] == '.') { 3682 p++; 3683 continue; 3684 } 3685 /* double dot */ 3686 if (p + 2 == q && p[0] == '.' && p[1] == '.') { 3687 p += 2; 3688 if (STAILQ_FIRST(&on_cache) == 3689 STAILQ_LAST(&on_cache, obj_list, entry)) { 3690 rc = ENOENT; 3691 goto done; 3692 } 3693 entry = STAILQ_FIRST(&on_cache); 3694 STAILQ_REMOVE_HEAD(&on_cache, entry); 3695 free(entry); 3696 objnum = (STAILQ_FIRST(&on_cache))->objnum; 3697 continue; 3698 } 3699 if (q - p + 1 > sizeof(element)) { 3700 rc = ENAMETOOLONG; 3701 goto done; 3702 } 3703 memcpy(element, p, q - p); 3704 element[q - p] = 0; 3705 p = q; 3706 3707 if ((rc = zfs_dnode_stat(spa, &dn, &sb)) != 0) 3708 goto done; 3709 if (!S_ISDIR(sb.st_mode)) { 3710 rc = ENOTDIR; 3711 goto done; 3712 } 3713 3714 rc = zap_lookup(spa, &dn, element, sizeof (objnum), 1, &objnum); 3715 if (rc) 3716 goto done; 3717 objnum = ZFS_DIRENT_OBJ(objnum); 3718 3719 if ((entry = malloc(sizeof(struct obj_list))) == NULL) { 3720 rc = ENOMEM; 3721 goto done; 3722 } 3723 entry->objnum = objnum; 3724 STAILQ_INSERT_HEAD(&on_cache, entry, entry); 3725 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn); 3726 if (rc) 3727 goto done; 3728 3729 /* 3730 * Check for symlink. 3731 */ 3732 rc = zfs_dnode_stat(spa, &dn, &sb); 3733 if (rc) 3734 goto done; 3735 if (S_ISLNK(sb.st_mode)) { 3736 if (symlinks_followed > 10) { 3737 rc = EMLINK; 3738 goto done; 3739 } 3740 symlinks_followed++; 3741 3742 /* 3743 * Read the link value and copy the tail of our 3744 * current path onto the end. 3745 */ 3746 if (sb.st_size + strlen(p) + 1 > sizeof(path)) { 3747 rc = ENAMETOOLONG; 3748 goto done; 3749 } 3750 strcpy(&path[sb.st_size], p); 3751 3752 rc = zfs_dnode_readlink(spa, &dn, path, sb.st_size); 3753 if (rc != 0) 3754 goto done; 3755 3756 /* 3757 * Restart with the new path, starting either at 3758 * the root or at the parent depending whether or 3759 * not the link is relative. 3760 */ 3761 p = path; 3762 if (*p == '/') { 3763 while (STAILQ_FIRST(&on_cache) != 3764 STAILQ_LAST(&on_cache, obj_list, entry)) { 3765 entry = STAILQ_FIRST(&on_cache); 3766 STAILQ_REMOVE_HEAD(&on_cache, entry); 3767 free(entry); 3768 } 3769 } else { 3770 entry = STAILQ_FIRST(&on_cache); 3771 STAILQ_REMOVE_HEAD(&on_cache, entry); 3772 free(entry); 3773 } 3774 objnum = (STAILQ_FIRST(&on_cache))->objnum; 3775 } 3776 } 3777 3778 *dnode = dn; 3779 done: 3780 STAILQ_FOREACH_SAFE(entry, &on_cache, entry, tentry) 3781 free(entry); 3782 return (rc); 3783 } 3784