1 /*- 2 * Copyright (c) 2007 Doug Rabson 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 /* 31 * Stand-alone ZFS file reader. 32 */ 33 34 #include <sys/stat.h> 35 #include <sys/stdint.h> 36 37 #include "zfsimpl.h" 38 #include "zfssubr.c" 39 40 41 struct zfsmount { 42 const spa_t *spa; 43 objset_phys_t objset; 44 uint64_t rootobj; 45 }; 46 static struct zfsmount zfsmount __unused; 47 48 /* 49 * List of all vdevs, chained through v_alllink. 50 */ 51 static vdev_list_t zfs_vdevs; 52 53 /* 54 * List of ZFS features supported for read 55 */ 56 static const char *features_for_read[] = { 57 "org.illumos:lz4_compress", 58 "com.delphix:hole_birth", 59 "com.delphix:extensible_dataset", 60 "com.delphix:embedded_data", 61 "org.open-zfs:large_blocks", 62 "org.illumos:sha512", 63 "org.illumos:skein", 64 "org.zfsonlinux:large_dnode", 65 NULL 66 }; 67 68 /* 69 * List of all pools, chained through spa_link. 70 */ 71 static spa_list_t zfs_pools; 72 73 static const dnode_phys_t *dnode_cache_obj; 74 static uint64_t dnode_cache_bn; 75 static char *dnode_cache_buf; 76 static char *zap_scratch; 77 static char *zfs_temp_buf, *zfs_temp_end, *zfs_temp_ptr; 78 79 #define TEMP_SIZE (1024 * 1024) 80 81 static int zio_read(const spa_t *spa, const blkptr_t *bp, void *buf); 82 static int zfs_get_root(const spa_t *spa, uint64_t *objid); 83 static int zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result); 84 static int zap_lookup(const spa_t *spa, const dnode_phys_t *dnode, 85 const char *name, uint64_t integer_size, uint64_t num_integers, 86 void *value); 87 88 static void 89 zfs_init(void) 90 { 91 STAILQ_INIT(&zfs_vdevs); 92 STAILQ_INIT(&zfs_pools); 93 94 zfs_temp_buf = malloc(TEMP_SIZE); 95 zfs_temp_end = zfs_temp_buf + TEMP_SIZE; 96 zfs_temp_ptr = zfs_temp_buf; 97 dnode_cache_buf = malloc(SPA_MAXBLOCKSIZE); 98 zap_scratch = malloc(SPA_MAXBLOCKSIZE); 99 100 zfs_init_crc(); 101 } 102 103 static void * 104 zfs_alloc(size_t size) 105 { 106 char *ptr; 107 108 if (zfs_temp_ptr + size > zfs_temp_end) { 109 printf("ZFS: out of temporary buffer space\n"); 110 for (;;) ; 111 } 112 ptr = zfs_temp_ptr; 113 zfs_temp_ptr += size; 114 115 return (ptr); 116 } 117 118 static void 119 zfs_free(void *ptr, size_t size) 120 { 121 122 zfs_temp_ptr -= size; 123 if (zfs_temp_ptr != ptr) { 124 printf("ZFS: zfs_alloc()/zfs_free() mismatch\n"); 125 for (;;) ; 126 } 127 } 128 129 static int 130 xdr_int(const unsigned char **xdr, int *ip) 131 { 132 *ip = ((*xdr)[0] << 24) 133 | ((*xdr)[1] << 16) 134 | ((*xdr)[2] << 8) 135 | ((*xdr)[3] << 0); 136 (*xdr) += 4; 137 return (0); 138 } 139 140 static int 141 xdr_u_int(const unsigned char **xdr, u_int *ip) 142 { 143 *ip = ((*xdr)[0] << 24) 144 | ((*xdr)[1] << 16) 145 | ((*xdr)[2] << 8) 146 | ((*xdr)[3] << 0); 147 (*xdr) += 4; 148 return (0); 149 } 150 151 static int 152 xdr_uint64_t(const unsigned char **xdr, uint64_t *lp) 153 { 154 u_int hi, lo; 155 156 xdr_u_int(xdr, &hi); 157 xdr_u_int(xdr, &lo); 158 *lp = (((uint64_t) hi) << 32) | lo; 159 return (0); 160 } 161 162 static int 163 nvlist_find(const unsigned char *nvlist, const char *name, int type, 164 int* elementsp, void *valuep) 165 { 166 const unsigned char *p, *pair; 167 int junk; 168 int encoded_size, decoded_size; 169 170 p = nvlist; 171 xdr_int(&p, &junk); 172 xdr_int(&p, &junk); 173 174 pair = p; 175 xdr_int(&p, &encoded_size); 176 xdr_int(&p, &decoded_size); 177 while (encoded_size && decoded_size) { 178 int namelen, pairtype, elements; 179 const char *pairname; 180 181 xdr_int(&p, &namelen); 182 pairname = (const char*) p; 183 p += roundup(namelen, 4); 184 xdr_int(&p, &pairtype); 185 186 if (!memcmp(name, pairname, namelen) && type == pairtype) { 187 xdr_int(&p, &elements); 188 if (elementsp) 189 *elementsp = elements; 190 if (type == DATA_TYPE_UINT64) { 191 xdr_uint64_t(&p, (uint64_t *) valuep); 192 return (0); 193 } else if (type == DATA_TYPE_STRING) { 194 int len; 195 xdr_int(&p, &len); 196 (*(const char**) valuep) = (const char*) p; 197 return (0); 198 } else if (type == DATA_TYPE_NVLIST 199 || type == DATA_TYPE_NVLIST_ARRAY) { 200 (*(const unsigned char**) valuep) = 201 (const unsigned char*) p; 202 return (0); 203 } else { 204 return (EIO); 205 } 206 } else { 207 /* 208 * Not the pair we are looking for, skip to the next one. 209 */ 210 p = pair + encoded_size; 211 } 212 213 pair = p; 214 xdr_int(&p, &encoded_size); 215 xdr_int(&p, &decoded_size); 216 } 217 218 return (EIO); 219 } 220 221 static int 222 nvlist_check_features_for_read(const unsigned char *nvlist) 223 { 224 const unsigned char *p, *pair; 225 int junk; 226 int encoded_size, decoded_size; 227 int rc; 228 229 rc = 0; 230 231 p = nvlist; 232 xdr_int(&p, &junk); 233 xdr_int(&p, &junk); 234 235 pair = p; 236 xdr_int(&p, &encoded_size); 237 xdr_int(&p, &decoded_size); 238 while (encoded_size && decoded_size) { 239 int namelen, pairtype; 240 const char *pairname; 241 int i, found; 242 243 found = 0; 244 245 xdr_int(&p, &namelen); 246 pairname = (const char*) p; 247 p += roundup(namelen, 4); 248 xdr_int(&p, &pairtype); 249 250 for (i = 0; features_for_read[i] != NULL; i++) { 251 if (!memcmp(pairname, features_for_read[i], namelen)) { 252 found = 1; 253 break; 254 } 255 } 256 257 if (!found) { 258 printf("ZFS: unsupported feature: %s\n", pairname); 259 rc = EIO; 260 } 261 262 p = pair + encoded_size; 263 264 pair = p; 265 xdr_int(&p, &encoded_size); 266 xdr_int(&p, &decoded_size); 267 } 268 269 return (rc); 270 } 271 272 /* 273 * Return the next nvlist in an nvlist array. 274 */ 275 static const unsigned char * 276 nvlist_next(const unsigned char *nvlist) 277 { 278 const unsigned char *p, *pair; 279 int junk; 280 int encoded_size, decoded_size; 281 282 p = nvlist; 283 xdr_int(&p, &junk); 284 xdr_int(&p, &junk); 285 286 pair = p; 287 xdr_int(&p, &encoded_size); 288 xdr_int(&p, &decoded_size); 289 while (encoded_size && decoded_size) { 290 p = pair + encoded_size; 291 292 pair = p; 293 xdr_int(&p, &encoded_size); 294 xdr_int(&p, &decoded_size); 295 } 296 297 return p; 298 } 299 300 #ifdef TEST 301 302 static const unsigned char * 303 nvlist_print(const unsigned char *nvlist, unsigned int indent) 304 { 305 static const char* typenames[] = { 306 "DATA_TYPE_UNKNOWN", 307 "DATA_TYPE_BOOLEAN", 308 "DATA_TYPE_BYTE", 309 "DATA_TYPE_INT16", 310 "DATA_TYPE_UINT16", 311 "DATA_TYPE_INT32", 312 "DATA_TYPE_UINT32", 313 "DATA_TYPE_INT64", 314 "DATA_TYPE_UINT64", 315 "DATA_TYPE_STRING", 316 "DATA_TYPE_BYTE_ARRAY", 317 "DATA_TYPE_INT16_ARRAY", 318 "DATA_TYPE_UINT16_ARRAY", 319 "DATA_TYPE_INT32_ARRAY", 320 "DATA_TYPE_UINT32_ARRAY", 321 "DATA_TYPE_INT64_ARRAY", 322 "DATA_TYPE_UINT64_ARRAY", 323 "DATA_TYPE_STRING_ARRAY", 324 "DATA_TYPE_HRTIME", 325 "DATA_TYPE_NVLIST", 326 "DATA_TYPE_NVLIST_ARRAY", 327 "DATA_TYPE_BOOLEAN_VALUE", 328 "DATA_TYPE_INT8", 329 "DATA_TYPE_UINT8", 330 "DATA_TYPE_BOOLEAN_ARRAY", 331 "DATA_TYPE_INT8_ARRAY", 332 "DATA_TYPE_UINT8_ARRAY" 333 }; 334 335 unsigned int i, j; 336 const unsigned char *p, *pair; 337 int junk; 338 int encoded_size, decoded_size; 339 340 p = nvlist; 341 xdr_int(&p, &junk); 342 xdr_int(&p, &junk); 343 344 pair = p; 345 xdr_int(&p, &encoded_size); 346 xdr_int(&p, &decoded_size); 347 while (encoded_size && decoded_size) { 348 int namelen, pairtype, elements; 349 const char *pairname; 350 351 xdr_int(&p, &namelen); 352 pairname = (const char*) p; 353 p += roundup(namelen, 4); 354 xdr_int(&p, &pairtype); 355 356 for (i = 0; i < indent; i++) 357 printf(" "); 358 printf("%s %s", typenames[pairtype], pairname); 359 360 xdr_int(&p, &elements); 361 switch (pairtype) { 362 case DATA_TYPE_UINT64: { 363 uint64_t val; 364 xdr_uint64_t(&p, &val); 365 printf(" = 0x%jx\n", (uintmax_t)val); 366 break; 367 } 368 369 case DATA_TYPE_STRING: { 370 int len; 371 xdr_int(&p, &len); 372 printf(" = \"%s\"\n", p); 373 break; 374 } 375 376 case DATA_TYPE_NVLIST: 377 printf("\n"); 378 nvlist_print(p, indent + 1); 379 break; 380 381 case DATA_TYPE_NVLIST_ARRAY: 382 for (j = 0; j < elements; j++) { 383 printf("[%d]\n", j); 384 p = nvlist_print(p, indent + 1); 385 if (j != elements - 1) { 386 for (i = 0; i < indent; i++) 387 printf(" "); 388 printf("%s %s", typenames[pairtype], pairname); 389 } 390 } 391 break; 392 393 default: 394 printf("\n"); 395 } 396 397 p = pair + encoded_size; 398 399 pair = p; 400 xdr_int(&p, &encoded_size); 401 xdr_int(&p, &decoded_size); 402 } 403 404 return p; 405 } 406 407 #endif 408 409 static int 410 vdev_read_phys(vdev_t *vdev, const blkptr_t *bp, void *buf, 411 off_t offset, size_t size) 412 { 413 size_t psize; 414 int rc; 415 416 if (!vdev->v_phys_read) 417 return (EIO); 418 419 if (bp) { 420 psize = BP_GET_PSIZE(bp); 421 } else { 422 psize = size; 423 } 424 425 /*printf("ZFS: reading %zu bytes at 0x%jx to %p\n", psize, (uintmax_t)offset, buf);*/ 426 rc = vdev->v_phys_read(vdev, vdev->v_read_priv, offset, buf, psize); 427 if (rc) 428 return (rc); 429 if (bp && zio_checksum_verify(vdev->spa, bp, buf)) 430 return (EIO); 431 432 return (0); 433 } 434 435 static int 436 vdev_disk_read(vdev_t *vdev, const blkptr_t *bp, void *buf, 437 off_t offset, size_t bytes) 438 { 439 440 return (vdev_read_phys(vdev, bp, buf, 441 offset + VDEV_LABEL_START_SIZE, bytes)); 442 } 443 444 445 static int 446 vdev_mirror_read(vdev_t *vdev, const blkptr_t *bp, void *buf, 447 off_t offset, size_t bytes) 448 { 449 vdev_t *kid; 450 int rc; 451 452 rc = EIO; 453 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) { 454 if (kid->v_state != VDEV_STATE_HEALTHY) 455 continue; 456 rc = kid->v_read(kid, bp, buf, offset, bytes); 457 if (!rc) 458 return (0); 459 } 460 461 return (rc); 462 } 463 464 static int 465 vdev_replacing_read(vdev_t *vdev, const blkptr_t *bp, void *buf, 466 off_t offset, size_t bytes) 467 { 468 vdev_t *kid; 469 470 /* 471 * Here we should have two kids: 472 * First one which is the one we are replacing and we can trust 473 * only this one to have valid data, but it might not be present. 474 * Second one is that one we are replacing with. It is most likely 475 * healthy, but we can't trust it has needed data, so we won't use it. 476 */ 477 kid = STAILQ_FIRST(&vdev->v_children); 478 if (kid == NULL) 479 return (EIO); 480 if (kid->v_state != VDEV_STATE_HEALTHY) 481 return (EIO); 482 return (kid->v_read(kid, bp, buf, offset, bytes)); 483 } 484 485 static vdev_t * 486 vdev_find(uint64_t guid) 487 { 488 vdev_t *vdev; 489 490 STAILQ_FOREACH(vdev, &zfs_vdevs, v_alllink) 491 if (vdev->v_guid == guid) 492 return (vdev); 493 494 return (0); 495 } 496 497 static vdev_t * 498 vdev_create(uint64_t guid, vdev_read_t *_read) 499 { 500 vdev_t *vdev; 501 502 vdev = malloc(sizeof(vdev_t)); 503 memset(vdev, 0, sizeof(vdev_t)); 504 STAILQ_INIT(&vdev->v_children); 505 vdev->v_guid = guid; 506 vdev->v_state = VDEV_STATE_OFFLINE; 507 vdev->v_read = _read; 508 vdev->v_phys_read = 0; 509 vdev->v_read_priv = 0; 510 STAILQ_INSERT_TAIL(&zfs_vdevs, vdev, v_alllink); 511 512 return (vdev); 513 } 514 515 static int 516 vdev_init_from_nvlist(const unsigned char *nvlist, vdev_t *pvdev, 517 vdev_t **vdevp, int is_newer) 518 { 519 int rc; 520 uint64_t guid, id, ashift, nparity; 521 const char *type; 522 const char *path; 523 vdev_t *vdev, *kid; 524 const unsigned char *kids; 525 int nkids, i, is_new; 526 uint64_t is_offline, is_faulted, is_degraded, is_removed, isnt_present; 527 528 if (nvlist_find(nvlist, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64, 529 NULL, &guid) 530 || nvlist_find(nvlist, ZPOOL_CONFIG_ID, DATA_TYPE_UINT64, NULL, &id) 531 || nvlist_find(nvlist, ZPOOL_CONFIG_TYPE, DATA_TYPE_STRING, 532 NULL, &type)) { 533 printf("ZFS: can't find vdev details\n"); 534 return (ENOENT); 535 } 536 537 if (strcmp(type, VDEV_TYPE_MIRROR) 538 && strcmp(type, VDEV_TYPE_DISK) 539 #ifdef ZFS_TEST 540 && strcmp(type, VDEV_TYPE_FILE) 541 #endif 542 && strcmp(type, VDEV_TYPE_RAIDZ) 543 && strcmp(type, VDEV_TYPE_REPLACING)) { 544 printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n"); 545 return (EIO); 546 } 547 548 is_offline = is_removed = is_faulted = is_degraded = isnt_present = 0; 549 550 nvlist_find(nvlist, ZPOOL_CONFIG_OFFLINE, DATA_TYPE_UINT64, NULL, 551 &is_offline); 552 nvlist_find(nvlist, ZPOOL_CONFIG_REMOVED, DATA_TYPE_UINT64, NULL, 553 &is_removed); 554 nvlist_find(nvlist, ZPOOL_CONFIG_FAULTED, DATA_TYPE_UINT64, NULL, 555 &is_faulted); 556 nvlist_find(nvlist, ZPOOL_CONFIG_DEGRADED, DATA_TYPE_UINT64, NULL, 557 &is_degraded); 558 nvlist_find(nvlist, ZPOOL_CONFIG_NOT_PRESENT, DATA_TYPE_UINT64, NULL, 559 &isnt_present); 560 561 vdev = vdev_find(guid); 562 if (!vdev) { 563 is_new = 1; 564 565 if (!strcmp(type, VDEV_TYPE_MIRROR)) 566 vdev = vdev_create(guid, vdev_mirror_read); 567 else if (!strcmp(type, VDEV_TYPE_RAIDZ)) 568 vdev = vdev_create(guid, vdev_raidz_read); 569 else if (!strcmp(type, VDEV_TYPE_REPLACING)) 570 vdev = vdev_create(guid, vdev_replacing_read); 571 else 572 vdev = vdev_create(guid, vdev_disk_read); 573 574 vdev->v_id = id; 575 vdev->v_top = pvdev != NULL ? pvdev : vdev; 576 if (nvlist_find(nvlist, ZPOOL_CONFIG_ASHIFT, 577 DATA_TYPE_UINT64, NULL, &ashift) == 0) { 578 vdev->v_ashift = ashift; 579 } else { 580 vdev->v_ashift = 0; 581 } 582 if (nvlist_find(nvlist, ZPOOL_CONFIG_NPARITY, 583 DATA_TYPE_UINT64, NULL, &nparity) == 0) { 584 vdev->v_nparity = nparity; 585 } else { 586 vdev->v_nparity = 0; 587 } 588 if (nvlist_find(nvlist, ZPOOL_CONFIG_PATH, 589 DATA_TYPE_STRING, NULL, &path) == 0) { 590 if (strncmp(path, "/dev/", 5) == 0) 591 path += 5; 592 vdev->v_name = strdup(path); 593 } else { 594 if (!strcmp(type, "raidz")) { 595 if (vdev->v_nparity == 1) 596 vdev->v_name = "raidz1"; 597 else if (vdev->v_nparity == 2) 598 vdev->v_name = "raidz2"; 599 else if (vdev->v_nparity == 3) 600 vdev->v_name = "raidz3"; 601 else { 602 printf("ZFS: can only boot from disk, mirror, raidz1, raidz2 and raidz3 vdevs\n"); 603 return (EIO); 604 } 605 } else { 606 vdev->v_name = strdup(type); 607 } 608 } 609 } else { 610 is_new = 0; 611 } 612 613 if (is_new || is_newer) { 614 /* 615 * This is either new vdev or we've already seen this vdev, 616 * but from an older vdev label, so let's refresh its state 617 * from the newer label. 618 */ 619 if (is_offline) 620 vdev->v_state = VDEV_STATE_OFFLINE; 621 else if (is_removed) 622 vdev->v_state = VDEV_STATE_REMOVED; 623 else if (is_faulted) 624 vdev->v_state = VDEV_STATE_FAULTED; 625 else if (is_degraded) 626 vdev->v_state = VDEV_STATE_DEGRADED; 627 else if (isnt_present) 628 vdev->v_state = VDEV_STATE_CANT_OPEN; 629 } 630 631 rc = nvlist_find(nvlist, ZPOOL_CONFIG_CHILDREN, DATA_TYPE_NVLIST_ARRAY, 632 &nkids, &kids); 633 /* 634 * Its ok if we don't have any kids. 635 */ 636 if (rc == 0) { 637 vdev->v_nchildren = nkids; 638 for (i = 0; i < nkids; i++) { 639 rc = vdev_init_from_nvlist(kids, vdev, &kid, is_newer); 640 if (rc) 641 return (rc); 642 if (is_new) 643 STAILQ_INSERT_TAIL(&vdev->v_children, kid, 644 v_childlink); 645 kids = nvlist_next(kids); 646 } 647 } else { 648 vdev->v_nchildren = 0; 649 } 650 651 if (vdevp) 652 *vdevp = vdev; 653 return (0); 654 } 655 656 static void 657 vdev_set_state(vdev_t *vdev) 658 { 659 vdev_t *kid; 660 int good_kids; 661 int bad_kids; 662 663 /* 664 * A mirror or raidz is healthy if all its kids are healthy. A 665 * mirror is degraded if any of its kids is healthy; a raidz 666 * is degraded if at most nparity kids are offline. 667 */ 668 if (STAILQ_FIRST(&vdev->v_children)) { 669 good_kids = 0; 670 bad_kids = 0; 671 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) { 672 if (kid->v_state == VDEV_STATE_HEALTHY) 673 good_kids++; 674 else 675 bad_kids++; 676 } 677 if (bad_kids == 0) { 678 vdev->v_state = VDEV_STATE_HEALTHY; 679 } else { 680 if (vdev->v_read == vdev_mirror_read) { 681 if (good_kids) { 682 vdev->v_state = VDEV_STATE_DEGRADED; 683 } else { 684 vdev->v_state = VDEV_STATE_OFFLINE; 685 } 686 } else if (vdev->v_read == vdev_raidz_read) { 687 if (bad_kids > vdev->v_nparity) { 688 vdev->v_state = VDEV_STATE_OFFLINE; 689 } else { 690 vdev->v_state = VDEV_STATE_DEGRADED; 691 } 692 } 693 } 694 } 695 } 696 697 static spa_t * 698 spa_find_by_guid(uint64_t guid) 699 { 700 spa_t *spa; 701 702 STAILQ_FOREACH(spa, &zfs_pools, spa_link) 703 if (spa->spa_guid == guid) 704 return (spa); 705 706 return (0); 707 } 708 709 static spa_t * 710 spa_find_by_name(const char *name) 711 { 712 spa_t *spa; 713 714 STAILQ_FOREACH(spa, &zfs_pools, spa_link) 715 if (!strcmp(spa->spa_name, name)) 716 return (spa); 717 718 return (0); 719 } 720 721 #ifdef BOOT2 722 static spa_t * 723 spa_get_primary(void) 724 { 725 726 return (STAILQ_FIRST(&zfs_pools)); 727 } 728 729 static vdev_t * 730 spa_get_primary_vdev(const spa_t *spa) 731 { 732 vdev_t *vdev; 733 vdev_t *kid; 734 735 if (spa == NULL) 736 spa = spa_get_primary(); 737 if (spa == NULL) 738 return (NULL); 739 vdev = STAILQ_FIRST(&spa->spa_vdevs); 740 if (vdev == NULL) 741 return (NULL); 742 for (kid = STAILQ_FIRST(&vdev->v_children); kid != NULL; 743 kid = STAILQ_FIRST(&vdev->v_children)) 744 vdev = kid; 745 return (vdev); 746 } 747 #endif 748 749 static spa_t * 750 spa_create(uint64_t guid, const char *name) 751 { 752 spa_t *spa; 753 754 if ((spa = malloc(sizeof(spa_t))) == NULL) 755 return (NULL); 756 memset(spa, 0, sizeof(spa_t)); 757 if ((spa->spa_name = strdup(name)) == NULL) { 758 free(spa); 759 return (NULL); 760 } 761 STAILQ_INIT(&spa->spa_vdevs); 762 spa->spa_guid = guid; 763 STAILQ_INSERT_TAIL(&zfs_pools, spa, spa_link); 764 765 return (spa); 766 } 767 768 static const char * 769 state_name(vdev_state_t state) 770 { 771 static const char* names[] = { 772 "UNKNOWN", 773 "CLOSED", 774 "OFFLINE", 775 "REMOVED", 776 "CANT_OPEN", 777 "FAULTED", 778 "DEGRADED", 779 "ONLINE" 780 }; 781 return names[state]; 782 } 783 784 #ifdef BOOT2 785 786 #define pager_printf printf 787 788 #else 789 790 static int 791 pager_printf(const char *fmt, ...) 792 { 793 char line[80]; 794 va_list args; 795 796 va_start(args, fmt); 797 vsprintf(line, fmt, args); 798 va_end(args); 799 800 return (pager_output(line)); 801 } 802 803 #endif 804 805 #define STATUS_FORMAT " %s %s\n" 806 807 static int 808 print_state(int indent, const char *name, vdev_state_t state) 809 { 810 char buf[512]; 811 int i; 812 813 buf[0] = 0; 814 for (i = 0; i < indent; i++) 815 strcat(buf, " "); 816 strcat(buf, name); 817 818 return (pager_printf(STATUS_FORMAT, buf, state_name(state))); 819 } 820 821 static int 822 vdev_status(vdev_t *vdev, int indent) 823 { 824 vdev_t *kid; 825 int ret; 826 ret = print_state(indent, vdev->v_name, vdev->v_state); 827 if (ret != 0) 828 return (ret); 829 830 STAILQ_FOREACH(kid, &vdev->v_children, v_childlink) { 831 ret = vdev_status(kid, indent + 1); 832 if (ret != 0) 833 return (ret); 834 } 835 return (ret); 836 } 837 838 static int 839 spa_status(spa_t *spa) 840 { 841 static char bootfs[ZFS_MAXNAMELEN]; 842 uint64_t rootid; 843 vdev_t *vdev; 844 int good_kids, bad_kids, degraded_kids, ret; 845 vdev_state_t state; 846 847 ret = pager_printf(" pool: %s\n", spa->spa_name); 848 if (ret != 0) 849 return (ret); 850 851 if (zfs_get_root(spa, &rootid) == 0 && 852 zfs_rlookup(spa, rootid, bootfs) == 0) { 853 if (bootfs[0] == '\0') 854 ret = pager_printf("bootfs: %s\n", spa->spa_name); 855 else 856 ret = pager_printf("bootfs: %s/%s\n", spa->spa_name, 857 bootfs); 858 if (ret != 0) 859 return (ret); 860 } 861 ret = pager_printf("config:\n\n"); 862 if (ret != 0) 863 return (ret); 864 ret = pager_printf(STATUS_FORMAT, "NAME", "STATE"); 865 if (ret != 0) 866 return (ret); 867 868 good_kids = 0; 869 degraded_kids = 0; 870 bad_kids = 0; 871 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) { 872 if (vdev->v_state == VDEV_STATE_HEALTHY) 873 good_kids++; 874 else if (vdev->v_state == VDEV_STATE_DEGRADED) 875 degraded_kids++; 876 else 877 bad_kids++; 878 } 879 880 state = VDEV_STATE_CLOSED; 881 if (good_kids > 0 && (degraded_kids + bad_kids) == 0) 882 state = VDEV_STATE_HEALTHY; 883 else if ((good_kids + degraded_kids) > 0) 884 state = VDEV_STATE_DEGRADED; 885 886 ret = print_state(0, spa->spa_name, state); 887 if (ret != 0) 888 return (ret); 889 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) { 890 ret = vdev_status(vdev, 1); 891 if (ret != 0) 892 return (ret); 893 } 894 return (ret); 895 } 896 897 static int 898 spa_all_status(void) 899 { 900 spa_t *spa; 901 int first = 1, ret = 0; 902 903 STAILQ_FOREACH(spa, &zfs_pools, spa_link) { 904 if (!first) { 905 ret = pager_printf("\n"); 906 if (ret != 0) 907 return (ret); 908 } 909 first = 0; 910 ret = spa_status(spa); 911 if (ret != 0) 912 return (ret); 913 } 914 return (ret); 915 } 916 917 static uint64_t 918 vdev_label_offset(uint64_t psize, int l, uint64_t offset) 919 { 920 uint64_t label_offset; 921 922 if (l < VDEV_LABELS / 2) 923 label_offset = 0; 924 else 925 label_offset = psize - VDEV_LABELS * sizeof (vdev_label_t); 926 927 return (offset + l * sizeof (vdev_label_t) + label_offset); 928 } 929 930 static int 931 vdev_probe(vdev_phys_read_t *_read, void *read_priv, spa_t **spap) 932 { 933 vdev_t vtmp; 934 vdev_phys_t *vdev_label = (vdev_phys_t *) zap_scratch; 935 vdev_phys_t *tmp_label; 936 spa_t *spa; 937 vdev_t *vdev, *top_vdev, *pool_vdev; 938 off_t off; 939 blkptr_t bp; 940 const unsigned char *nvlist = NULL; 941 uint64_t val; 942 uint64_t guid; 943 uint64_t best_txg = 0; 944 uint64_t pool_txg, pool_guid; 945 uint64_t psize; 946 const char *pool_name; 947 const unsigned char *vdevs; 948 const unsigned char *features; 949 int i, l, rc, is_newer; 950 char *upbuf; 951 const struct uberblock *up; 952 953 /* 954 * Load the vdev label and figure out which 955 * uberblock is most current. 956 */ 957 memset(&vtmp, 0, sizeof(vtmp)); 958 vtmp.v_phys_read = _read; 959 vtmp.v_read_priv = read_priv; 960 psize = P2ALIGN(ldi_get_size(read_priv), 961 (uint64_t)sizeof (vdev_label_t)); 962 963 /* Test for minimum pool size. */ 964 if (psize < SPA_MINDEVSIZE) 965 return (EIO); 966 967 tmp_label = zfs_alloc(sizeof(vdev_phys_t)); 968 969 for (l = 0; l < VDEV_LABELS; l++) { 970 off = vdev_label_offset(psize, l, 971 offsetof(vdev_label_t, vl_vdev_phys)); 972 973 BP_ZERO(&bp); 974 BP_SET_LSIZE(&bp, sizeof(vdev_phys_t)); 975 BP_SET_PSIZE(&bp, sizeof(vdev_phys_t)); 976 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL); 977 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF); 978 DVA_SET_OFFSET(BP_IDENTITY(&bp), off); 979 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0); 980 981 if (vdev_read_phys(&vtmp, &bp, tmp_label, off, 0)) 982 continue; 983 984 if (tmp_label->vp_nvlist[0] != NV_ENCODE_XDR) 985 continue; 986 987 nvlist = (const unsigned char *) tmp_label->vp_nvlist + 4; 988 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_TXG, 989 DATA_TYPE_UINT64, NULL, &pool_txg) != 0) 990 continue; 991 992 if (best_txg <= pool_txg) { 993 best_txg = pool_txg; 994 memcpy(vdev_label, tmp_label, sizeof (vdev_phys_t)); 995 } 996 } 997 998 zfs_free(tmp_label, sizeof (vdev_phys_t)); 999 1000 if (best_txg == 0) 1001 return (EIO); 1002 1003 if (vdev_label->vp_nvlist[0] != NV_ENCODE_XDR) 1004 return (EIO); 1005 1006 nvlist = (const unsigned char *) vdev_label->vp_nvlist + 4; 1007 1008 if (nvlist_find(nvlist, ZPOOL_CONFIG_VERSION, DATA_TYPE_UINT64, 1009 NULL, &val) != 0) { 1010 return (EIO); 1011 } 1012 1013 if (!SPA_VERSION_IS_SUPPORTED(val)) { 1014 printf("ZFS: unsupported ZFS version %u (should be %u)\n", 1015 (unsigned) val, (unsigned) SPA_VERSION); 1016 return (EIO); 1017 } 1018 1019 /* Check ZFS features for read */ 1020 if (nvlist_find(nvlist, ZPOOL_CONFIG_FEATURES_FOR_READ, 1021 DATA_TYPE_NVLIST, NULL, &features) == 0 && 1022 nvlist_check_features_for_read(features) != 0) { 1023 return (EIO); 1024 } 1025 1026 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_STATE, DATA_TYPE_UINT64, 1027 NULL, &val) != 0) { 1028 return (EIO); 1029 } 1030 1031 if (val == POOL_STATE_DESTROYED) { 1032 /* We don't boot only from destroyed pools. */ 1033 return (EIO); 1034 } 1035 1036 if (nvlist_find(nvlist, ZPOOL_CONFIG_POOL_TXG, DATA_TYPE_UINT64, 1037 NULL, &pool_txg) != 0 || 1038 nvlist_find(nvlist, ZPOOL_CONFIG_POOL_GUID, DATA_TYPE_UINT64, 1039 NULL, &pool_guid) != 0 || 1040 nvlist_find(nvlist, ZPOOL_CONFIG_POOL_NAME, DATA_TYPE_STRING, 1041 NULL, &pool_name) != 0) { 1042 /* 1043 * Cache and spare devices end up here - just ignore 1044 * them. 1045 */ 1046 /*printf("ZFS: can't find pool details\n");*/ 1047 return (EIO); 1048 } 1049 1050 if (nvlist_find(nvlist, ZPOOL_CONFIG_IS_LOG, DATA_TYPE_UINT64, 1051 NULL, &val) == 0 && val != 0) { 1052 return (EIO); 1053 } 1054 1055 /* 1056 * Create the pool if this is the first time we've seen it. 1057 */ 1058 spa = spa_find_by_guid(pool_guid); 1059 if (spa == NULL) { 1060 spa = spa_create(pool_guid, pool_name); 1061 if (spa == NULL) 1062 return (ENOMEM); 1063 } 1064 if (pool_txg > spa->spa_txg) { 1065 spa->spa_txg = pool_txg; 1066 is_newer = 1; 1067 } else { 1068 is_newer = 0; 1069 } 1070 1071 /* 1072 * Get the vdev tree and create our in-core copy of it. 1073 * If we already have a vdev with this guid, this must 1074 * be some kind of alias (overlapping slices, dangerously dedicated 1075 * disks etc). 1076 */ 1077 if (nvlist_find(nvlist, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64, 1078 NULL, &guid) != 0) { 1079 return (EIO); 1080 } 1081 vdev = vdev_find(guid); 1082 if (vdev && vdev->v_phys_read) /* Has this vdev already been inited? */ 1083 return (EIO); 1084 1085 if (nvlist_find(nvlist, ZPOOL_CONFIG_VDEV_TREE, DATA_TYPE_NVLIST, 1086 NULL, &vdevs)) { 1087 return (EIO); 1088 } 1089 1090 rc = vdev_init_from_nvlist(vdevs, NULL, &top_vdev, is_newer); 1091 if (rc != 0) 1092 return (rc); 1093 1094 /* 1095 * Add the toplevel vdev to the pool if its not already there. 1096 */ 1097 STAILQ_FOREACH(pool_vdev, &spa->spa_vdevs, v_childlink) 1098 if (top_vdev == pool_vdev) 1099 break; 1100 if (!pool_vdev && top_vdev) { 1101 top_vdev->spa = spa; 1102 STAILQ_INSERT_TAIL(&spa->spa_vdevs, top_vdev, v_childlink); 1103 } 1104 1105 /* 1106 * We should already have created an incomplete vdev for this 1107 * vdev. Find it and initialise it with our read proc. 1108 */ 1109 vdev = vdev_find(guid); 1110 if (vdev) { 1111 vdev->v_phys_read = _read; 1112 vdev->v_read_priv = read_priv; 1113 vdev->v_state = VDEV_STATE_HEALTHY; 1114 } else { 1115 printf("ZFS: inconsistent nvlist contents\n"); 1116 return (EIO); 1117 } 1118 1119 /* 1120 * Re-evaluate top-level vdev state. 1121 */ 1122 vdev_set_state(top_vdev); 1123 1124 /* 1125 * Ok, we are happy with the pool so far. Lets find 1126 * the best uberblock and then we can actually access 1127 * the contents of the pool. 1128 */ 1129 upbuf = zfs_alloc(VDEV_UBERBLOCK_SIZE(vdev)); 1130 up = (const struct uberblock *)upbuf; 1131 for (l = 0; l < VDEV_LABELS; l++) { 1132 for (i = 0; i < VDEV_UBERBLOCK_COUNT(vdev); i++) { 1133 off = vdev_label_offset(psize, l, 1134 VDEV_UBERBLOCK_OFFSET(vdev, i)); 1135 BP_ZERO(&bp); 1136 DVA_SET_OFFSET(&bp.blk_dva[0], off); 1137 BP_SET_LSIZE(&bp, VDEV_UBERBLOCK_SIZE(vdev)); 1138 BP_SET_PSIZE(&bp, VDEV_UBERBLOCK_SIZE(vdev)); 1139 BP_SET_CHECKSUM(&bp, ZIO_CHECKSUM_LABEL); 1140 BP_SET_COMPRESS(&bp, ZIO_COMPRESS_OFF); 1141 ZIO_SET_CHECKSUM(&bp.blk_cksum, off, 0, 0, 0); 1142 1143 if (vdev_read_phys(vdev, &bp, upbuf, off, 0)) 1144 continue; 1145 1146 if (up->ub_magic != UBERBLOCK_MAGIC) 1147 continue; 1148 if (up->ub_txg < spa->spa_txg) 1149 continue; 1150 if (up->ub_txg > spa->spa_uberblock.ub_txg || 1151 (up->ub_txg == spa->spa_uberblock.ub_txg && 1152 up->ub_timestamp > 1153 spa->spa_uberblock.ub_timestamp)) { 1154 spa->spa_uberblock = *up; 1155 } 1156 } 1157 } 1158 zfs_free(upbuf, VDEV_UBERBLOCK_SIZE(vdev)); 1159 1160 vdev->spa = spa; 1161 if (spap != NULL) 1162 *spap = spa; 1163 return (0); 1164 } 1165 1166 static int 1167 ilog2(int n) 1168 { 1169 int v; 1170 1171 for (v = 0; v < 32; v++) 1172 if (n == (1 << v)) 1173 return v; 1174 return -1; 1175 } 1176 1177 static int 1178 zio_read_gang(const spa_t *spa, const blkptr_t *bp, void *buf) 1179 { 1180 blkptr_t gbh_bp; 1181 zio_gbh_phys_t zio_gb; 1182 char *pbuf; 1183 int i; 1184 1185 /* Artificial BP for gang block header. */ 1186 gbh_bp = *bp; 1187 BP_SET_PSIZE(&gbh_bp, SPA_GANGBLOCKSIZE); 1188 BP_SET_LSIZE(&gbh_bp, SPA_GANGBLOCKSIZE); 1189 BP_SET_CHECKSUM(&gbh_bp, ZIO_CHECKSUM_GANG_HEADER); 1190 BP_SET_COMPRESS(&gbh_bp, ZIO_COMPRESS_OFF); 1191 for (i = 0; i < SPA_DVAS_PER_BP; i++) 1192 DVA_SET_GANG(&gbh_bp.blk_dva[i], 0); 1193 1194 /* Read gang header block using the artificial BP. */ 1195 if (zio_read(spa, &gbh_bp, &zio_gb)) 1196 return (EIO); 1197 1198 pbuf = buf; 1199 for (i = 0; i < SPA_GBH_NBLKPTRS; i++) { 1200 blkptr_t *gbp = &zio_gb.zg_blkptr[i]; 1201 1202 if (BP_IS_HOLE(gbp)) 1203 continue; 1204 if (zio_read(spa, gbp, pbuf)) 1205 return (EIO); 1206 pbuf += BP_GET_PSIZE(gbp); 1207 } 1208 1209 if (zio_checksum_verify(spa, bp, buf)) 1210 return (EIO); 1211 return (0); 1212 } 1213 1214 static int 1215 zio_read(const spa_t *spa, const blkptr_t *bp, void *buf) 1216 { 1217 int cpfunc = BP_GET_COMPRESS(bp); 1218 uint64_t align, size; 1219 void *pbuf; 1220 int i, error; 1221 1222 /* 1223 * Process data embedded in block pointer 1224 */ 1225 if (BP_IS_EMBEDDED(bp)) { 1226 ASSERT(BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA); 1227 1228 size = BPE_GET_PSIZE(bp); 1229 ASSERT(size <= BPE_PAYLOAD_SIZE); 1230 1231 if (cpfunc != ZIO_COMPRESS_OFF) 1232 pbuf = zfs_alloc(size); 1233 else 1234 pbuf = buf; 1235 1236 decode_embedded_bp_compressed(bp, pbuf); 1237 error = 0; 1238 1239 if (cpfunc != ZIO_COMPRESS_OFF) { 1240 error = zio_decompress_data(cpfunc, pbuf, 1241 size, buf, BP_GET_LSIZE(bp)); 1242 zfs_free(pbuf, size); 1243 } 1244 if (error != 0) 1245 printf("ZFS: i/o error - unable to decompress block pointer data, error %d\n", 1246 error); 1247 return (error); 1248 } 1249 1250 error = EIO; 1251 1252 for (i = 0; i < SPA_DVAS_PER_BP; i++) { 1253 const dva_t *dva = &bp->blk_dva[i]; 1254 vdev_t *vdev; 1255 int vdevid; 1256 off_t offset; 1257 1258 if (!dva->dva_word[0] && !dva->dva_word[1]) 1259 continue; 1260 1261 vdevid = DVA_GET_VDEV(dva); 1262 offset = DVA_GET_OFFSET(dva); 1263 STAILQ_FOREACH(vdev, &spa->spa_vdevs, v_childlink) { 1264 if (vdev->v_id == vdevid) 1265 break; 1266 } 1267 if (!vdev || !vdev->v_read) 1268 continue; 1269 1270 size = BP_GET_PSIZE(bp); 1271 if (vdev->v_read == vdev_raidz_read) { 1272 align = 1ULL << vdev->v_top->v_ashift; 1273 if (P2PHASE(size, align) != 0) 1274 size = P2ROUNDUP(size, align); 1275 } 1276 if (size != BP_GET_PSIZE(bp) || cpfunc != ZIO_COMPRESS_OFF) 1277 pbuf = zfs_alloc(size); 1278 else 1279 pbuf = buf; 1280 1281 if (DVA_GET_GANG(dva)) 1282 error = zio_read_gang(spa, bp, pbuf); 1283 else 1284 error = vdev->v_read(vdev, bp, pbuf, offset, size); 1285 if (error == 0) { 1286 if (cpfunc != ZIO_COMPRESS_OFF) 1287 error = zio_decompress_data(cpfunc, pbuf, 1288 BP_GET_PSIZE(bp), buf, BP_GET_LSIZE(bp)); 1289 else if (size != BP_GET_PSIZE(bp)) 1290 bcopy(pbuf, buf, BP_GET_PSIZE(bp)); 1291 } 1292 if (buf != pbuf) 1293 zfs_free(pbuf, size); 1294 if (error == 0) 1295 break; 1296 } 1297 if (error != 0) 1298 printf("ZFS: i/o error - all block copies unavailable\n"); 1299 return (error); 1300 } 1301 1302 static int 1303 dnode_read(const spa_t *spa, const dnode_phys_t *dnode, off_t offset, void *buf, size_t buflen) 1304 { 1305 int ibshift = dnode->dn_indblkshift - SPA_BLKPTRSHIFT; 1306 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT; 1307 int nlevels = dnode->dn_nlevels; 1308 int i, rc; 1309 1310 if (bsize > SPA_MAXBLOCKSIZE) { 1311 printf("ZFS: I/O error - blocks larger than %llu are not " 1312 "supported\n", SPA_MAXBLOCKSIZE); 1313 return (EIO); 1314 } 1315 1316 /* 1317 * Note: bsize may not be a power of two here so we need to do an 1318 * actual divide rather than a bitshift. 1319 */ 1320 while (buflen > 0) { 1321 uint64_t bn = offset / bsize; 1322 int boff = offset % bsize; 1323 int ibn; 1324 const blkptr_t *indbp; 1325 blkptr_t bp; 1326 1327 if (bn > dnode->dn_maxblkid) 1328 return (EIO); 1329 1330 if (dnode == dnode_cache_obj && bn == dnode_cache_bn) 1331 goto cached; 1332 1333 indbp = dnode->dn_blkptr; 1334 for (i = 0; i < nlevels; i++) { 1335 /* 1336 * Copy the bp from the indirect array so that 1337 * we can re-use the scratch buffer for multi-level 1338 * objects. 1339 */ 1340 ibn = bn >> ((nlevels - i - 1) * ibshift); 1341 ibn &= ((1 << ibshift) - 1); 1342 bp = indbp[ibn]; 1343 if (BP_IS_HOLE(&bp)) { 1344 memset(dnode_cache_buf, 0, bsize); 1345 break; 1346 } 1347 rc = zio_read(spa, &bp, dnode_cache_buf); 1348 if (rc) 1349 return (rc); 1350 indbp = (const blkptr_t *) dnode_cache_buf; 1351 } 1352 dnode_cache_obj = dnode; 1353 dnode_cache_bn = bn; 1354 cached: 1355 1356 /* 1357 * The buffer contains our data block. Copy what we 1358 * need from it and loop. 1359 */ 1360 i = bsize - boff; 1361 if (i > buflen) i = buflen; 1362 memcpy(buf, &dnode_cache_buf[boff], i); 1363 buf = ((char*) buf) + i; 1364 offset += i; 1365 buflen -= i; 1366 } 1367 1368 return (0); 1369 } 1370 1371 /* 1372 * Lookup a value in a microzap directory. Assumes that the zap 1373 * scratch buffer contains the directory contents. 1374 */ 1375 static int 1376 mzap_lookup(const dnode_phys_t *dnode, const char *name, uint64_t *value) 1377 { 1378 const mzap_phys_t *mz; 1379 const mzap_ent_phys_t *mze; 1380 size_t size; 1381 int chunks, i; 1382 1383 /* 1384 * Microzap objects use exactly one block. Read the whole 1385 * thing. 1386 */ 1387 size = dnode->dn_datablkszsec * 512; 1388 1389 mz = (const mzap_phys_t *) zap_scratch; 1390 chunks = size / MZAP_ENT_LEN - 1; 1391 1392 for (i = 0; i < chunks; i++) { 1393 mze = &mz->mz_chunk[i]; 1394 if (!strcmp(mze->mze_name, name)) { 1395 *value = mze->mze_value; 1396 return (0); 1397 } 1398 } 1399 1400 return (ENOENT); 1401 } 1402 1403 /* 1404 * Compare a name with a zap leaf entry. Return non-zero if the name 1405 * matches. 1406 */ 1407 static int 1408 fzap_name_equal(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, const char *name) 1409 { 1410 size_t namelen; 1411 const zap_leaf_chunk_t *nc; 1412 const char *p; 1413 1414 namelen = zc->l_entry.le_name_numints; 1415 1416 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk); 1417 p = name; 1418 while (namelen > 0) { 1419 size_t len; 1420 len = namelen; 1421 if (len > ZAP_LEAF_ARRAY_BYTES) 1422 len = ZAP_LEAF_ARRAY_BYTES; 1423 if (memcmp(p, nc->l_array.la_array, len)) 1424 return (0); 1425 p += len; 1426 namelen -= len; 1427 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next); 1428 } 1429 1430 return 1; 1431 } 1432 1433 /* 1434 * Extract a uint64_t value from a zap leaf entry. 1435 */ 1436 static uint64_t 1437 fzap_leaf_value(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc) 1438 { 1439 const zap_leaf_chunk_t *vc; 1440 int i; 1441 uint64_t value; 1442 const uint8_t *p; 1443 1444 vc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_value_chunk); 1445 for (i = 0, value = 0, p = vc->l_array.la_array; i < 8; i++) { 1446 value = (value << 8) | p[i]; 1447 } 1448 1449 return value; 1450 } 1451 1452 static void 1453 stv(int len, void *addr, uint64_t value) 1454 { 1455 switch (len) { 1456 case 1: 1457 *(uint8_t *)addr = value; 1458 return; 1459 case 2: 1460 *(uint16_t *)addr = value; 1461 return; 1462 case 4: 1463 *(uint32_t *)addr = value; 1464 return; 1465 case 8: 1466 *(uint64_t *)addr = value; 1467 return; 1468 } 1469 } 1470 1471 /* 1472 * Extract a array from a zap leaf entry. 1473 */ 1474 static void 1475 fzap_leaf_array(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, 1476 uint64_t integer_size, uint64_t num_integers, void *buf) 1477 { 1478 uint64_t array_int_len = zc->l_entry.le_value_intlen; 1479 uint64_t value = 0; 1480 uint64_t *u64 = buf; 1481 char *p = buf; 1482 int len = MIN(zc->l_entry.le_value_numints, num_integers); 1483 int chunk = zc->l_entry.le_value_chunk; 1484 int byten = 0; 1485 1486 if (integer_size == 8 && len == 1) { 1487 *u64 = fzap_leaf_value(zl, zc); 1488 return; 1489 } 1490 1491 while (len > 0) { 1492 struct zap_leaf_array *la = &ZAP_LEAF_CHUNK(zl, chunk).l_array; 1493 int i; 1494 1495 ASSERT3U(chunk, <, ZAP_LEAF_NUMCHUNKS(zl)); 1496 for (i = 0; i < ZAP_LEAF_ARRAY_BYTES && len > 0; i++) { 1497 value = (value << 8) | la->la_array[i]; 1498 byten++; 1499 if (byten == array_int_len) { 1500 stv(integer_size, p, value); 1501 byten = 0; 1502 len--; 1503 if (len == 0) 1504 return; 1505 p += integer_size; 1506 } 1507 } 1508 chunk = la->la_next; 1509 } 1510 } 1511 1512 /* 1513 * Lookup a value in a fatzap directory. Assumes that the zap scratch 1514 * buffer contains the directory header. 1515 */ 1516 static int 1517 fzap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name, 1518 uint64_t integer_size, uint64_t num_integers, void *value) 1519 { 1520 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT; 1521 zap_phys_t zh = *(zap_phys_t *) zap_scratch; 1522 fat_zap_t z; 1523 uint64_t *ptrtbl; 1524 uint64_t hash; 1525 int rc; 1526 1527 if (zh.zap_magic != ZAP_MAGIC) 1528 return (EIO); 1529 1530 z.zap_block_shift = ilog2(bsize); 1531 z.zap_phys = (zap_phys_t *) zap_scratch; 1532 1533 /* 1534 * Figure out where the pointer table is and read it in if necessary. 1535 */ 1536 if (zh.zap_ptrtbl.zt_blk) { 1537 rc = dnode_read(spa, dnode, zh.zap_ptrtbl.zt_blk * bsize, 1538 zap_scratch, bsize); 1539 if (rc) 1540 return (rc); 1541 ptrtbl = (uint64_t *) zap_scratch; 1542 } else { 1543 ptrtbl = &ZAP_EMBEDDED_PTRTBL_ENT(&z, 0); 1544 } 1545 1546 hash = zap_hash(zh.zap_salt, name); 1547 1548 zap_leaf_t zl; 1549 zl.l_bs = z.zap_block_shift; 1550 1551 off_t off = ptrtbl[hash >> (64 - zh.zap_ptrtbl.zt_shift)] << zl.l_bs; 1552 zap_leaf_chunk_t *zc; 1553 1554 rc = dnode_read(spa, dnode, off, zap_scratch, bsize); 1555 if (rc) 1556 return (rc); 1557 1558 zl.l_phys = (zap_leaf_phys_t *) zap_scratch; 1559 1560 /* 1561 * Make sure this chunk matches our hash. 1562 */ 1563 if (zl.l_phys->l_hdr.lh_prefix_len > 0 1564 && zl.l_phys->l_hdr.lh_prefix 1565 != hash >> (64 - zl.l_phys->l_hdr.lh_prefix_len)) 1566 return (ENOENT); 1567 1568 /* 1569 * Hash within the chunk to find our entry. 1570 */ 1571 int shift = (64 - ZAP_LEAF_HASH_SHIFT(&zl) - zl.l_phys->l_hdr.lh_prefix_len); 1572 int h = (hash >> shift) & ((1 << ZAP_LEAF_HASH_SHIFT(&zl)) - 1); 1573 h = zl.l_phys->l_hash[h]; 1574 if (h == 0xffff) 1575 return (ENOENT); 1576 zc = &ZAP_LEAF_CHUNK(&zl, h); 1577 while (zc->l_entry.le_hash != hash) { 1578 if (zc->l_entry.le_next == 0xffff) { 1579 zc = NULL; 1580 break; 1581 } 1582 zc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_next); 1583 } 1584 if (fzap_name_equal(&zl, zc, name)) { 1585 if (zc->l_entry.le_value_intlen * zc->l_entry.le_value_numints > 1586 integer_size * num_integers) 1587 return (E2BIG); 1588 fzap_leaf_array(&zl, zc, integer_size, num_integers, value); 1589 return (0); 1590 } 1591 1592 return (ENOENT); 1593 } 1594 1595 /* 1596 * Lookup a name in a zap object and return its value as a uint64_t. 1597 */ 1598 static int 1599 zap_lookup(const spa_t *spa, const dnode_phys_t *dnode, const char *name, 1600 uint64_t integer_size, uint64_t num_integers, void *value) 1601 { 1602 int rc; 1603 uint64_t zap_type; 1604 size_t size = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT; 1605 1606 rc = dnode_read(spa, dnode, 0, zap_scratch, size); 1607 if (rc) 1608 return (rc); 1609 1610 zap_type = *(uint64_t *) zap_scratch; 1611 if (zap_type == ZBT_MICRO) 1612 return mzap_lookup(dnode, name, value); 1613 else if (zap_type == ZBT_HEADER) { 1614 return fzap_lookup(spa, dnode, name, integer_size, 1615 num_integers, value); 1616 } 1617 printf("ZFS: invalid zap_type=%d\n", (int)zap_type); 1618 return (EIO); 1619 } 1620 1621 /* 1622 * List a microzap directory. Assumes that the zap scratch buffer contains 1623 * the directory contents. 1624 */ 1625 static int 1626 mzap_list(const dnode_phys_t *dnode, int (*callback)(const char *, uint64_t)) 1627 { 1628 const mzap_phys_t *mz; 1629 const mzap_ent_phys_t *mze; 1630 size_t size; 1631 int chunks, i, rc; 1632 1633 /* 1634 * Microzap objects use exactly one block. Read the whole 1635 * thing. 1636 */ 1637 size = dnode->dn_datablkszsec * 512; 1638 mz = (const mzap_phys_t *) zap_scratch; 1639 chunks = size / MZAP_ENT_LEN - 1; 1640 1641 for (i = 0; i < chunks; i++) { 1642 mze = &mz->mz_chunk[i]; 1643 if (mze->mze_name[0]) { 1644 rc = callback(mze->mze_name, mze->mze_value); 1645 if (rc != 0) 1646 return (rc); 1647 } 1648 } 1649 1650 return (0); 1651 } 1652 1653 /* 1654 * List a fatzap directory. Assumes that the zap scratch buffer contains 1655 * the directory header. 1656 */ 1657 static int 1658 fzap_list(const spa_t *spa, const dnode_phys_t *dnode, int (*callback)(const char *, uint64_t)) 1659 { 1660 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT; 1661 zap_phys_t zh = *(zap_phys_t *) zap_scratch; 1662 fat_zap_t z; 1663 int i, j, rc; 1664 1665 if (zh.zap_magic != ZAP_MAGIC) 1666 return (EIO); 1667 1668 z.zap_block_shift = ilog2(bsize); 1669 z.zap_phys = (zap_phys_t *) zap_scratch; 1670 1671 /* 1672 * This assumes that the leaf blocks start at block 1. The 1673 * documentation isn't exactly clear on this. 1674 */ 1675 zap_leaf_t zl; 1676 zl.l_bs = z.zap_block_shift; 1677 for (i = 0; i < zh.zap_num_leafs; i++) { 1678 off_t off = (i + 1) << zl.l_bs; 1679 char name[256], *p; 1680 uint64_t value; 1681 1682 if (dnode_read(spa, dnode, off, zap_scratch, bsize)) 1683 return (EIO); 1684 1685 zl.l_phys = (zap_leaf_phys_t *) zap_scratch; 1686 1687 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) { 1688 zap_leaf_chunk_t *zc, *nc; 1689 int namelen; 1690 1691 zc = &ZAP_LEAF_CHUNK(&zl, j); 1692 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY) 1693 continue; 1694 namelen = zc->l_entry.le_name_numints; 1695 if (namelen > sizeof(name)) 1696 namelen = sizeof(name); 1697 1698 /* 1699 * Paste the name back together. 1700 */ 1701 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk); 1702 p = name; 1703 while (namelen > 0) { 1704 int len; 1705 len = namelen; 1706 if (len > ZAP_LEAF_ARRAY_BYTES) 1707 len = ZAP_LEAF_ARRAY_BYTES; 1708 memcpy(p, nc->l_array.la_array, len); 1709 p += len; 1710 namelen -= len; 1711 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next); 1712 } 1713 1714 /* 1715 * Assume the first eight bytes of the value are 1716 * a uint64_t. 1717 */ 1718 value = fzap_leaf_value(&zl, zc); 1719 1720 //printf("%s 0x%jx\n", name, (uintmax_t)value); 1721 rc = callback((const char *)name, value); 1722 if (rc != 0) 1723 return (rc); 1724 } 1725 } 1726 1727 return (0); 1728 } 1729 1730 static int zfs_printf(const char *name, uint64_t value __unused) 1731 { 1732 1733 printf("%s\n", name); 1734 1735 return (0); 1736 } 1737 1738 /* 1739 * List a zap directory. 1740 */ 1741 static int 1742 zap_list(const spa_t *spa, const dnode_phys_t *dnode) 1743 { 1744 uint64_t zap_type; 1745 size_t size = dnode->dn_datablkszsec * 512; 1746 1747 if (dnode_read(spa, dnode, 0, zap_scratch, size)) 1748 return (EIO); 1749 1750 zap_type = *(uint64_t *) zap_scratch; 1751 if (zap_type == ZBT_MICRO) 1752 return mzap_list(dnode, zfs_printf); 1753 else 1754 return fzap_list(spa, dnode, zfs_printf); 1755 } 1756 1757 static int 1758 objset_get_dnode(const spa_t *spa, const objset_phys_t *os, uint64_t objnum, dnode_phys_t *dnode) 1759 { 1760 off_t offset; 1761 1762 offset = objnum * sizeof(dnode_phys_t); 1763 return dnode_read(spa, &os->os_meta_dnode, offset, 1764 dnode, sizeof(dnode_phys_t)); 1765 } 1766 1767 static int 1768 mzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value) 1769 { 1770 const mzap_phys_t *mz; 1771 const mzap_ent_phys_t *mze; 1772 size_t size; 1773 int chunks, i; 1774 1775 /* 1776 * Microzap objects use exactly one block. Read the whole 1777 * thing. 1778 */ 1779 size = dnode->dn_datablkszsec * 512; 1780 1781 mz = (const mzap_phys_t *) zap_scratch; 1782 chunks = size / MZAP_ENT_LEN - 1; 1783 1784 for (i = 0; i < chunks; i++) { 1785 mze = &mz->mz_chunk[i]; 1786 if (value == mze->mze_value) { 1787 strcpy(name, mze->mze_name); 1788 return (0); 1789 } 1790 } 1791 1792 return (ENOENT); 1793 } 1794 1795 static void 1796 fzap_name_copy(const zap_leaf_t *zl, const zap_leaf_chunk_t *zc, char *name) 1797 { 1798 size_t namelen; 1799 const zap_leaf_chunk_t *nc; 1800 char *p; 1801 1802 namelen = zc->l_entry.le_name_numints; 1803 1804 nc = &ZAP_LEAF_CHUNK(zl, zc->l_entry.le_name_chunk); 1805 p = name; 1806 while (namelen > 0) { 1807 size_t len; 1808 len = namelen; 1809 if (len > ZAP_LEAF_ARRAY_BYTES) 1810 len = ZAP_LEAF_ARRAY_BYTES; 1811 memcpy(p, nc->l_array.la_array, len); 1812 p += len; 1813 namelen -= len; 1814 nc = &ZAP_LEAF_CHUNK(zl, nc->l_array.la_next); 1815 } 1816 1817 *p = '\0'; 1818 } 1819 1820 static int 1821 fzap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value) 1822 { 1823 int bsize = dnode->dn_datablkszsec << SPA_MINBLOCKSHIFT; 1824 zap_phys_t zh = *(zap_phys_t *) zap_scratch; 1825 fat_zap_t z; 1826 int i, j; 1827 1828 if (zh.zap_magic != ZAP_MAGIC) 1829 return (EIO); 1830 1831 z.zap_block_shift = ilog2(bsize); 1832 z.zap_phys = (zap_phys_t *) zap_scratch; 1833 1834 /* 1835 * This assumes that the leaf blocks start at block 1. The 1836 * documentation isn't exactly clear on this. 1837 */ 1838 zap_leaf_t zl; 1839 zl.l_bs = z.zap_block_shift; 1840 for (i = 0; i < zh.zap_num_leafs; i++) { 1841 off_t off = (i + 1) << zl.l_bs; 1842 1843 if (dnode_read(spa, dnode, off, zap_scratch, bsize)) 1844 return (EIO); 1845 1846 zl.l_phys = (zap_leaf_phys_t *) zap_scratch; 1847 1848 for (j = 0; j < ZAP_LEAF_NUMCHUNKS(&zl); j++) { 1849 zap_leaf_chunk_t *zc; 1850 1851 zc = &ZAP_LEAF_CHUNK(&zl, j); 1852 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY) 1853 continue; 1854 if (zc->l_entry.le_value_intlen != 8 || 1855 zc->l_entry.le_value_numints != 1) 1856 continue; 1857 1858 if (fzap_leaf_value(&zl, zc) == value) { 1859 fzap_name_copy(&zl, zc, name); 1860 return (0); 1861 } 1862 } 1863 } 1864 1865 return (ENOENT); 1866 } 1867 1868 static int 1869 zap_rlookup(const spa_t *spa, const dnode_phys_t *dnode, char *name, uint64_t value) 1870 { 1871 int rc; 1872 uint64_t zap_type; 1873 size_t size = dnode->dn_datablkszsec * 512; 1874 1875 rc = dnode_read(spa, dnode, 0, zap_scratch, size); 1876 if (rc) 1877 return (rc); 1878 1879 zap_type = *(uint64_t *) zap_scratch; 1880 if (zap_type == ZBT_MICRO) 1881 return mzap_rlookup(spa, dnode, name, value); 1882 else 1883 return fzap_rlookup(spa, dnode, name, value); 1884 } 1885 1886 static int 1887 zfs_rlookup(const spa_t *spa, uint64_t objnum, char *result) 1888 { 1889 char name[256]; 1890 char component[256]; 1891 uint64_t dir_obj, parent_obj, child_dir_zapobj; 1892 dnode_phys_t child_dir_zap, dataset, dir, parent; 1893 dsl_dir_phys_t *dd; 1894 dsl_dataset_phys_t *ds; 1895 char *p; 1896 int len; 1897 1898 p = &name[sizeof(name) - 1]; 1899 *p = '\0'; 1900 1901 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) { 1902 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum); 1903 return (EIO); 1904 } 1905 ds = (dsl_dataset_phys_t *)&dataset.dn_bonus; 1906 dir_obj = ds->ds_dir_obj; 1907 1908 for (;;) { 1909 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir) != 0) 1910 return (EIO); 1911 dd = (dsl_dir_phys_t *)&dir.dn_bonus; 1912 1913 /* Actual loop condition. */ 1914 parent_obj = dd->dd_parent_obj; 1915 if (parent_obj == 0) 1916 break; 1917 1918 if (objset_get_dnode(spa, &spa->spa_mos, parent_obj, &parent) != 0) 1919 return (EIO); 1920 dd = (dsl_dir_phys_t *)&parent.dn_bonus; 1921 child_dir_zapobj = dd->dd_child_dir_zapobj; 1922 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0) 1923 return (EIO); 1924 if (zap_rlookup(spa, &child_dir_zap, component, dir_obj) != 0) 1925 return (EIO); 1926 1927 len = strlen(component); 1928 p -= len; 1929 memcpy(p, component, len); 1930 --p; 1931 *p = '/'; 1932 1933 /* Actual loop iteration. */ 1934 dir_obj = parent_obj; 1935 } 1936 1937 if (*p != '\0') 1938 ++p; 1939 strcpy(result, p); 1940 1941 return (0); 1942 } 1943 1944 static int 1945 zfs_lookup_dataset(const spa_t *spa, const char *name, uint64_t *objnum) 1946 { 1947 char element[256]; 1948 uint64_t dir_obj, child_dir_zapobj; 1949 dnode_phys_t child_dir_zap, dir; 1950 dsl_dir_phys_t *dd; 1951 const char *p, *q; 1952 1953 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir)) 1954 return (EIO); 1955 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, sizeof (dir_obj), 1956 1, &dir_obj)) 1957 return (EIO); 1958 1959 p = name; 1960 for (;;) { 1961 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir)) 1962 return (EIO); 1963 dd = (dsl_dir_phys_t *)&dir.dn_bonus; 1964 1965 while (*p == '/') 1966 p++; 1967 /* Actual loop condition #1. */ 1968 if (*p == '\0') 1969 break; 1970 1971 q = strchr(p, '/'); 1972 if (q) { 1973 memcpy(element, p, q - p); 1974 element[q - p] = '\0'; 1975 p = q + 1; 1976 } else { 1977 strcpy(element, p); 1978 p += strlen(p); 1979 } 1980 1981 child_dir_zapobj = dd->dd_child_dir_zapobj; 1982 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0) 1983 return (EIO); 1984 1985 /* Actual loop condition #2. */ 1986 if (zap_lookup(spa, &child_dir_zap, element, sizeof (dir_obj), 1987 1, &dir_obj) != 0) 1988 return (ENOENT); 1989 } 1990 1991 *objnum = dd->dd_head_dataset_obj; 1992 return (0); 1993 } 1994 1995 #ifndef BOOT2 1996 static int 1997 zfs_list_dataset(const spa_t *spa, uint64_t objnum/*, int pos, char *entry*/) 1998 { 1999 uint64_t dir_obj, child_dir_zapobj; 2000 dnode_phys_t child_dir_zap, dir, dataset; 2001 dsl_dataset_phys_t *ds; 2002 dsl_dir_phys_t *dd; 2003 2004 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) { 2005 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum); 2006 return (EIO); 2007 } 2008 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus; 2009 dir_obj = ds->ds_dir_obj; 2010 2011 if (objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir)) { 2012 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj); 2013 return (EIO); 2014 } 2015 dd = (dsl_dir_phys_t *)&dir.dn_bonus; 2016 2017 child_dir_zapobj = dd->dd_child_dir_zapobj; 2018 if (objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap) != 0) { 2019 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj); 2020 return (EIO); 2021 } 2022 2023 return (zap_list(spa, &child_dir_zap) != 0); 2024 } 2025 2026 int 2027 zfs_callback_dataset(const spa_t *spa, uint64_t objnum, int (*callback)(const char *, uint64_t)) 2028 { 2029 uint64_t dir_obj, child_dir_zapobj, zap_type; 2030 dnode_phys_t child_dir_zap, dir, dataset; 2031 dsl_dataset_phys_t *ds; 2032 dsl_dir_phys_t *dd; 2033 int err; 2034 2035 err = objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset); 2036 if (err != 0) { 2037 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum); 2038 return (err); 2039 } 2040 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus; 2041 dir_obj = ds->ds_dir_obj; 2042 2043 err = objset_get_dnode(spa, &spa->spa_mos, dir_obj, &dir); 2044 if (err != 0) { 2045 printf("ZFS: can't find dirobj %ju\n", (uintmax_t)dir_obj); 2046 return (err); 2047 } 2048 dd = (dsl_dir_phys_t *)&dir.dn_bonus; 2049 2050 child_dir_zapobj = dd->dd_child_dir_zapobj; 2051 err = objset_get_dnode(spa, &spa->spa_mos, child_dir_zapobj, &child_dir_zap); 2052 if (err != 0) { 2053 printf("ZFS: can't find child zap %ju\n", (uintmax_t)dir_obj); 2054 return (err); 2055 } 2056 2057 err = dnode_read(spa, &child_dir_zap, 0, zap_scratch, child_dir_zap.dn_datablkszsec * 512); 2058 if (err != 0) 2059 return (err); 2060 2061 zap_type = *(uint64_t *) zap_scratch; 2062 if (zap_type == ZBT_MICRO) 2063 return mzap_list(&child_dir_zap, callback); 2064 else 2065 return fzap_list(spa, &child_dir_zap, callback); 2066 } 2067 #endif 2068 2069 /* 2070 * Find the object set given the object number of its dataset object 2071 * and return its details in *objset 2072 */ 2073 static int 2074 zfs_mount_dataset(const spa_t *spa, uint64_t objnum, objset_phys_t *objset) 2075 { 2076 dnode_phys_t dataset; 2077 dsl_dataset_phys_t *ds; 2078 2079 if (objset_get_dnode(spa, &spa->spa_mos, objnum, &dataset)) { 2080 printf("ZFS: can't find dataset %ju\n", (uintmax_t)objnum); 2081 return (EIO); 2082 } 2083 2084 ds = (dsl_dataset_phys_t *) &dataset.dn_bonus; 2085 if (zio_read(spa, &ds->ds_bp, objset)) { 2086 printf("ZFS: can't read object set for dataset %ju\n", 2087 (uintmax_t)objnum); 2088 return (EIO); 2089 } 2090 2091 return (0); 2092 } 2093 2094 /* 2095 * Find the object set pointed to by the BOOTFS property or the root 2096 * dataset if there is none and return its details in *objset 2097 */ 2098 static int 2099 zfs_get_root(const spa_t *spa, uint64_t *objid) 2100 { 2101 dnode_phys_t dir, propdir; 2102 uint64_t props, bootfs, root; 2103 2104 *objid = 0; 2105 2106 /* 2107 * Start with the MOS directory object. 2108 */ 2109 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, &dir)) { 2110 printf("ZFS: can't read MOS object directory\n"); 2111 return (EIO); 2112 } 2113 2114 /* 2115 * Lookup the pool_props and see if we can find a bootfs. 2116 */ 2117 if (zap_lookup(spa, &dir, DMU_POOL_PROPS, sizeof (props), 1, &props) == 0 2118 && objset_get_dnode(spa, &spa->spa_mos, props, &propdir) == 0 2119 && zap_lookup(spa, &propdir, "bootfs", sizeof (bootfs), 1, &bootfs) == 0 2120 && bootfs != 0) 2121 { 2122 *objid = bootfs; 2123 return (0); 2124 } 2125 /* 2126 * Lookup the root dataset directory 2127 */ 2128 if (zap_lookup(spa, &dir, DMU_POOL_ROOT_DATASET, sizeof (root), 1, &root) 2129 || objset_get_dnode(spa, &spa->spa_mos, root, &dir)) { 2130 printf("ZFS: can't find root dsl_dir\n"); 2131 return (EIO); 2132 } 2133 2134 /* 2135 * Use the information from the dataset directory's bonus buffer 2136 * to find the dataset object and from that the object set itself. 2137 */ 2138 dsl_dir_phys_t *dd = (dsl_dir_phys_t *) &dir.dn_bonus; 2139 *objid = dd->dd_head_dataset_obj; 2140 return (0); 2141 } 2142 2143 static int 2144 zfs_mount(const spa_t *spa, uint64_t rootobj, struct zfsmount *mount) 2145 { 2146 2147 mount->spa = spa; 2148 2149 /* 2150 * Find the root object set if not explicitly provided 2151 */ 2152 if (rootobj == 0 && zfs_get_root(spa, &rootobj)) { 2153 printf("ZFS: can't find root filesystem\n"); 2154 return (EIO); 2155 } 2156 2157 if (zfs_mount_dataset(spa, rootobj, &mount->objset)) { 2158 printf("ZFS: can't open root filesystem\n"); 2159 return (EIO); 2160 } 2161 2162 mount->rootobj = rootobj; 2163 2164 return (0); 2165 } 2166 2167 /* 2168 * callback function for feature name checks. 2169 */ 2170 static int 2171 check_feature(const char *name, uint64_t value) 2172 { 2173 int i; 2174 2175 if (value == 0) 2176 return (0); 2177 if (name[0] == '\0') 2178 return (0); 2179 2180 for (i = 0; features_for_read[i] != NULL; i++) { 2181 if (strcmp(name, features_for_read[i]) == 0) 2182 return (0); 2183 } 2184 printf("ZFS: unsupported feature: %s\n", name); 2185 return (EIO); 2186 } 2187 2188 /* 2189 * Checks whether the MOS features that are active are supported. 2190 */ 2191 static int 2192 check_mos_features(const spa_t *spa) 2193 { 2194 dnode_phys_t dir; 2195 uint64_t objnum, zap_type; 2196 size_t size; 2197 int rc; 2198 2199 if ((rc = objset_get_dnode(spa, &spa->spa_mos, DMU_OT_OBJECT_DIRECTORY, 2200 &dir)) != 0) 2201 return (rc); 2202 if ((rc = zap_lookup(spa, &dir, DMU_POOL_FEATURES_FOR_READ, 2203 sizeof (objnum), 1, &objnum)) != 0) { 2204 /* 2205 * It is older pool without features. As we have already 2206 * tested the label, just return without raising the error. 2207 */ 2208 return (0); 2209 } 2210 2211 if ((rc = objset_get_dnode(spa, &spa->spa_mos, objnum, &dir)) != 0) 2212 return (rc); 2213 2214 if (dir.dn_type != DMU_OTN_ZAP_METADATA) 2215 return (EIO); 2216 2217 size = dir.dn_datablkszsec * 512; 2218 if (dnode_read(spa, &dir, 0, zap_scratch, size)) 2219 return (EIO); 2220 2221 zap_type = *(uint64_t *) zap_scratch; 2222 if (zap_type == ZBT_MICRO) 2223 rc = mzap_list(&dir, check_feature); 2224 else 2225 rc = fzap_list(spa, &dir, check_feature); 2226 2227 return (rc); 2228 } 2229 2230 static int 2231 zfs_spa_init(spa_t *spa) 2232 { 2233 dnode_phys_t dir; 2234 int rc; 2235 2236 if (zio_read(spa, &spa->spa_uberblock.ub_rootbp, &spa->spa_mos)) { 2237 printf("ZFS: can't read MOS of pool %s\n", spa->spa_name); 2238 return (EIO); 2239 } 2240 if (spa->spa_mos.os_type != DMU_OST_META) { 2241 printf("ZFS: corrupted MOS of pool %s\n", spa->spa_name); 2242 return (EIO); 2243 } 2244 2245 if (objset_get_dnode(spa, &spa->spa_mos, DMU_POOL_DIRECTORY_OBJECT, 2246 &dir)) { 2247 printf("ZFS: failed to read pool %s directory object\n", 2248 spa->spa_name); 2249 return (EIO); 2250 } 2251 /* this is allowed to fail, older pools do not have salt */ 2252 rc = zap_lookup(spa, &dir, DMU_POOL_CHECKSUM_SALT, 1, 2253 sizeof (spa->spa_cksum_salt.zcs_bytes), 2254 spa->spa_cksum_salt.zcs_bytes); 2255 2256 rc = check_mos_features(spa); 2257 if (rc != 0) { 2258 printf("ZFS: pool %s is not supported\n", spa->spa_name); 2259 } 2260 2261 return (rc); 2262 } 2263 2264 static int 2265 zfs_dnode_stat(const spa_t *spa, dnode_phys_t *dn, struct stat *sb) 2266 { 2267 2268 if (dn->dn_bonustype != DMU_OT_SA) { 2269 znode_phys_t *zp = (znode_phys_t *)dn->dn_bonus; 2270 2271 sb->st_mode = zp->zp_mode; 2272 sb->st_uid = zp->zp_uid; 2273 sb->st_gid = zp->zp_gid; 2274 sb->st_size = zp->zp_size; 2275 } else { 2276 sa_hdr_phys_t *sahdrp; 2277 int hdrsize; 2278 size_t size = 0; 2279 void *buf = NULL; 2280 2281 if (dn->dn_bonuslen != 0) 2282 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn); 2283 else { 2284 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0) { 2285 blkptr_t *bp = DN_SPILL_BLKPTR(dn); 2286 int error; 2287 2288 size = BP_GET_LSIZE(bp); 2289 buf = zfs_alloc(size); 2290 error = zio_read(spa, bp, buf); 2291 if (error != 0) { 2292 zfs_free(buf, size); 2293 return (error); 2294 } 2295 sahdrp = buf; 2296 } else { 2297 return (EIO); 2298 } 2299 } 2300 hdrsize = SA_HDR_SIZE(sahdrp); 2301 sb->st_mode = *(uint64_t *)((char *)sahdrp + hdrsize + 2302 SA_MODE_OFFSET); 2303 sb->st_uid = *(uint64_t *)((char *)sahdrp + hdrsize + 2304 SA_UID_OFFSET); 2305 sb->st_gid = *(uint64_t *)((char *)sahdrp + hdrsize + 2306 SA_GID_OFFSET); 2307 sb->st_size = *(uint64_t *)((char *)sahdrp + hdrsize + 2308 SA_SIZE_OFFSET); 2309 if (buf != NULL) 2310 zfs_free(buf, size); 2311 } 2312 2313 return (0); 2314 } 2315 2316 static int 2317 zfs_dnode_readlink(const spa_t *spa, dnode_phys_t *dn, char *path, size_t psize) 2318 { 2319 int rc = 0; 2320 2321 if (dn->dn_bonustype == DMU_OT_SA) { 2322 sa_hdr_phys_t *sahdrp = NULL; 2323 size_t size = 0; 2324 void *buf = NULL; 2325 int hdrsize; 2326 char *p; 2327 2328 if (dn->dn_bonuslen != 0) 2329 sahdrp = (sa_hdr_phys_t *)DN_BONUS(dn); 2330 else { 2331 blkptr_t *bp; 2332 2333 if ((dn->dn_flags & DNODE_FLAG_SPILL_BLKPTR) == 0) 2334 return (EIO); 2335 bp = DN_SPILL_BLKPTR(dn); 2336 2337 size = BP_GET_LSIZE(bp); 2338 buf = zfs_alloc(size); 2339 rc = zio_read(spa, bp, buf); 2340 if (rc != 0) { 2341 zfs_free(buf, size); 2342 return (rc); 2343 } 2344 sahdrp = buf; 2345 } 2346 hdrsize = SA_HDR_SIZE(sahdrp); 2347 p = (char *)((uintptr_t)sahdrp + hdrsize + SA_SYMLINK_OFFSET); 2348 memcpy(path, p, psize); 2349 if (buf != NULL) 2350 zfs_free(buf, size); 2351 return (0); 2352 } 2353 /* 2354 * Second test is purely to silence bogus compiler 2355 * warning about accessing past the end of dn_bonus. 2356 */ 2357 if (psize + sizeof(znode_phys_t) <= dn->dn_bonuslen && 2358 sizeof(znode_phys_t) <= sizeof(dn->dn_bonus)) { 2359 memcpy(path, &dn->dn_bonus[sizeof(znode_phys_t)], psize); 2360 } else { 2361 rc = dnode_read(spa, dn, 0, path, psize); 2362 } 2363 return (rc); 2364 } 2365 2366 struct obj_list { 2367 uint64_t objnum; 2368 STAILQ_ENTRY(obj_list) entry; 2369 }; 2370 2371 /* 2372 * Lookup a file and return its dnode. 2373 */ 2374 static int 2375 zfs_lookup(const struct zfsmount *mount, const char *upath, dnode_phys_t *dnode) 2376 { 2377 int rc; 2378 uint64_t objnum; 2379 const spa_t *spa; 2380 dnode_phys_t dn; 2381 const char *p, *q; 2382 char element[256]; 2383 char path[1024]; 2384 int symlinks_followed = 0; 2385 struct stat sb; 2386 struct obj_list *entry, *tentry; 2387 STAILQ_HEAD(, obj_list) on_cache = STAILQ_HEAD_INITIALIZER(on_cache); 2388 2389 spa = mount->spa; 2390 if (mount->objset.os_type != DMU_OST_ZFS) { 2391 printf("ZFS: unexpected object set type %ju\n", 2392 (uintmax_t)mount->objset.os_type); 2393 return (EIO); 2394 } 2395 2396 if ((entry = malloc(sizeof(struct obj_list))) == NULL) 2397 return (ENOMEM); 2398 2399 /* 2400 * Get the root directory dnode. 2401 */ 2402 rc = objset_get_dnode(spa, &mount->objset, MASTER_NODE_OBJ, &dn); 2403 if (rc) { 2404 free(entry); 2405 return (rc); 2406 } 2407 2408 rc = zap_lookup(spa, &dn, ZFS_ROOT_OBJ, sizeof (objnum), 1, &objnum); 2409 if (rc) { 2410 free(entry); 2411 return (rc); 2412 } 2413 entry->objnum = objnum; 2414 STAILQ_INSERT_HEAD(&on_cache, entry, entry); 2415 2416 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn); 2417 if (rc != 0) 2418 goto done; 2419 2420 p = upath; 2421 while (p && *p) { 2422 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn); 2423 if (rc != 0) 2424 goto done; 2425 2426 while (*p == '/') 2427 p++; 2428 if (*p == '\0') 2429 break; 2430 q = p; 2431 while (*q != '\0' && *q != '/') 2432 q++; 2433 2434 /* skip dot */ 2435 if (p + 1 == q && p[0] == '.') { 2436 p++; 2437 continue; 2438 } 2439 /* double dot */ 2440 if (p + 2 == q && p[0] == '.' && p[1] == '.') { 2441 p += 2; 2442 if (STAILQ_FIRST(&on_cache) == 2443 STAILQ_LAST(&on_cache, obj_list, entry)) { 2444 rc = ENOENT; 2445 goto done; 2446 } 2447 entry = STAILQ_FIRST(&on_cache); 2448 STAILQ_REMOVE_HEAD(&on_cache, entry); 2449 free(entry); 2450 objnum = (STAILQ_FIRST(&on_cache))->objnum; 2451 continue; 2452 } 2453 if (q - p + 1 > sizeof(element)) { 2454 rc = ENAMETOOLONG; 2455 goto done; 2456 } 2457 memcpy(element, p, q - p); 2458 element[q - p] = 0; 2459 p = q; 2460 2461 if ((rc = zfs_dnode_stat(spa, &dn, &sb)) != 0) 2462 goto done; 2463 if (!S_ISDIR(sb.st_mode)) { 2464 rc = ENOTDIR; 2465 goto done; 2466 } 2467 2468 rc = zap_lookup(spa, &dn, element, sizeof (objnum), 1, &objnum); 2469 if (rc) 2470 goto done; 2471 objnum = ZFS_DIRENT_OBJ(objnum); 2472 2473 if ((entry = malloc(sizeof(struct obj_list))) == NULL) { 2474 rc = ENOMEM; 2475 goto done; 2476 } 2477 entry->objnum = objnum; 2478 STAILQ_INSERT_HEAD(&on_cache, entry, entry); 2479 rc = objset_get_dnode(spa, &mount->objset, objnum, &dn); 2480 if (rc) 2481 goto done; 2482 2483 /* 2484 * Check for symlink. 2485 */ 2486 rc = zfs_dnode_stat(spa, &dn, &sb); 2487 if (rc) 2488 goto done; 2489 if (S_ISLNK(sb.st_mode)) { 2490 if (symlinks_followed > 10) { 2491 rc = EMLINK; 2492 goto done; 2493 } 2494 symlinks_followed++; 2495 2496 /* 2497 * Read the link value and copy the tail of our 2498 * current path onto the end. 2499 */ 2500 if (sb.st_size + strlen(p) + 1 > sizeof(path)) { 2501 rc = ENAMETOOLONG; 2502 goto done; 2503 } 2504 strcpy(&path[sb.st_size], p); 2505 2506 rc = zfs_dnode_readlink(spa, &dn, path, sb.st_size); 2507 if (rc != 0) 2508 goto done; 2509 2510 /* 2511 * Restart with the new path, starting either at 2512 * the root or at the parent depending whether or 2513 * not the link is relative. 2514 */ 2515 p = path; 2516 if (*p == '/') { 2517 while (STAILQ_FIRST(&on_cache) != 2518 STAILQ_LAST(&on_cache, obj_list, entry)) { 2519 entry = STAILQ_FIRST(&on_cache); 2520 STAILQ_REMOVE_HEAD(&on_cache, entry); 2521 free(entry); 2522 } 2523 } else { 2524 entry = STAILQ_FIRST(&on_cache); 2525 STAILQ_REMOVE_HEAD(&on_cache, entry); 2526 free(entry); 2527 } 2528 objnum = (STAILQ_FIRST(&on_cache))->objnum; 2529 } 2530 } 2531 2532 *dnode = dn; 2533 done: 2534 STAILQ_FOREACH_SAFE(entry, &on_cache, entry, tentry) 2535 free(entry); 2536 return (rc); 2537 } 2538