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