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