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