488 489 /* 490 * We're checking zap_ismicro without the lock held, in order to 491 * tell what type of lock we want. Once we have some sort of 492 * lock, see if it really is the right type. In practice this 493 * can only be different if it was upgraded from micro to fat, 494 * and micro wanted WRITER but fat only needs READER. 495 */ 496 lt = (!zap->zap_ismicro && fatreader) ? RW_READER : lti; 497 rw_enter(&zap->zap_rwlock, lt); 498 if (lt != ((!zap->zap_ismicro && fatreader) ? RW_READER : lti)) { 499 /* it was upgraded, now we only need reader */ 500 ASSERT(lt == RW_WRITER); 501 ASSERT(RW_READER == 502 (!zap->zap_ismicro && fatreader) ? RW_READER : lti); 503 rw_downgrade(&zap->zap_rwlock); 504 lt = RW_READER; 505 } 506 507 zap->zap_objset = os; 508 509 if (lt == RW_WRITER) 510 dmu_buf_will_dirty(db, tx); 511 512 ASSERT3P(zap->zap_dbuf, ==, db); 513 514 ASSERT(!zap->zap_ismicro || 515 zap->zap_m.zap_num_entries <= zap->zap_m.zap_num_chunks); 516 if (zap->zap_ismicro && tx && adding && 517 zap->zap_m.zap_num_entries == zap->zap_m.zap_num_chunks) { 518 uint64_t newsz = db->db_size + SPA_MINBLOCKSIZE; 519 if (newsz > MZAP_MAX_BLKSZ) { 520 dprintf("upgrading obj %llu: num_entries=%u\n", 521 obj, zap->zap_m.zap_num_entries); 522 *zapp = zap; 523 return (mzap_upgrade(zapp, tx, 0)); 524 } 525 err = dmu_object_set_blocksize(os, obj, newsz, 0, tx); 526 ASSERT0(err); 527 zap->zap_m.zap_num_chunks = 528 db->db_size / MZAP_ENT_LEN - 1; 529 } 530 531 *zapp = zap; 532 return (0); 533} 534 535void 536zap_unlockdir(zap_t *zap) 537{ 538 rw_exit(&zap->zap_rwlock); 539 dmu_buf_rele(zap->zap_dbuf, NULL); 540} 541 542static int 543mzap_upgrade(zap_t **zapp, dmu_tx_t *tx, zap_flags_t flags) 544{ 545 mzap_phys_t *mzp; 546 int i, sz, nchunks; 547 int err = 0; 548 zap_t *zap = *zapp; 549 550 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock)); 551 552 sz = zap->zap_dbuf->db_size; 553 mzp = zio_buf_alloc(sz); 554 bcopy(zap->zap_dbuf->db_data, mzp, sz); 555 nchunks = zap->zap_m.zap_num_chunks; 556 557 if (!flags) { 558 err = dmu_object_set_blocksize(zap->zap_objset, zap->zap_object, 559 1ULL << fzap_default_block_shift, 0, tx); 560 if (err) { 561 zio_buf_free(mzp, sz); 562 return (err); 563 } 564 } 565 566 dprintf("upgrading obj=%llu with %u chunks\n", 567 zap->zap_object, nchunks); 568 /* XXX destroy the avl later, so we can use the stored hash value */ 569 mze_destroy(zap); 570 571 fzap_upgrade(zap, tx, flags); 572 573 for (i = 0; i < nchunks; i++) { 574 mzap_ent_phys_t *mze = &mzp->mz_chunk[i]; 575 zap_name_t *zn; 576 if (mze->mze_name[0] == 0) 577 continue; 578 dprintf("adding %s=%llu\n", 579 mze->mze_name, mze->mze_value); 580 zn = zap_name_alloc(zap, mze->mze_name, MT_EXACT); 581 err = fzap_add_cd(zn, 8, 1, &mze->mze_value, mze->mze_cd, tx); 582 zap = zn->zn_zap; /* fzap_add_cd() may change zap */ 583 zap_name_free(zn); 584 if (err) 585 break; 586 } 587 zio_buf_free(mzp, sz); 588 *zapp = zap; 589 return (err); 590} 591 592void 593mzap_create_impl(objset_t *os, uint64_t obj, int normflags, zap_flags_t flags, 594 dmu_tx_t *tx) 595{ 596 dmu_buf_t *db; 597 mzap_phys_t *zp; 598 599 VERIFY(0 == dmu_buf_hold(os, obj, 0, FTAG, &db, DMU_READ_NO_PREFETCH)); 600 601#ifdef ZFS_DEBUG 602 { 603 dmu_object_info_t doi; 604 dmu_object_info_from_db(db, &doi); 605 ASSERT3U(DMU_OT_BYTESWAP(doi.doi_type), ==, DMU_BSWAP_ZAP); 606 } 607#endif 608 609 dmu_buf_will_dirty(db, tx); 610 zp = db->db_data; 611 zp->mz_block_type = ZBT_MICRO; 612 zp->mz_salt = ((uintptr_t)db ^ (uintptr_t)tx ^ (obj << 1)) | 1ULL; 613 zp->mz_normflags = normflags; 614 dmu_buf_rele(db, FTAG); 615 616 if (flags != 0) { 617 zap_t *zap; 618 /* Only fat zap supports flags; upgrade immediately. */ 619 VERIFY(0 == zap_lockdir(os, obj, tx, RW_WRITER, 620 B_FALSE, B_FALSE, &zap)); 621 VERIFY3U(0, ==, mzap_upgrade(&zap, tx, flags)); 622 zap_unlockdir(zap); 623 } 624} 625 626int 627zap_create_claim(objset_t *os, uint64_t obj, dmu_object_type_t ot, 628 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 629{ 630 return (zap_create_claim_norm(os, obj, 631 0, ot, bonustype, bonuslen, tx)); 632} 633 634int 635zap_create_claim_norm(objset_t *os, uint64_t obj, int normflags, 636 dmu_object_type_t ot, 637 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 638{ 639 int err; 640 641 err = dmu_object_claim(os, obj, ot, 0, bonustype, bonuslen, tx); 642 if (err != 0) 643 return (err); 644 mzap_create_impl(os, obj, normflags, 0, tx); 645 return (0); 646} 647 648uint64_t 649zap_create(objset_t *os, dmu_object_type_t ot, 650 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 651{ 652 return (zap_create_norm(os, 0, ot, bonustype, bonuslen, tx)); 653} 654 655uint64_t 656zap_create_norm(objset_t *os, int normflags, dmu_object_type_t ot, 657 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 658{ 659 uint64_t obj = dmu_object_alloc(os, ot, 0, bonustype, bonuslen, tx); 660 661 mzap_create_impl(os, obj, normflags, 0, tx); 662 return (obj); 663} 664 665uint64_t 666zap_create_flags(objset_t *os, int normflags, zap_flags_t flags, 667 dmu_object_type_t ot, int leaf_blockshift, int indirect_blockshift, 668 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 669{ 670 uint64_t obj = dmu_object_alloc(os, ot, 0, bonustype, bonuslen, tx); 671 672 ASSERT(leaf_blockshift >= SPA_MINBLOCKSHIFT && 673 leaf_blockshift <= SPA_OLD_MAXBLOCKSHIFT && 674 indirect_blockshift >= SPA_MINBLOCKSHIFT && 675 indirect_blockshift <= SPA_OLD_MAXBLOCKSHIFT); 676 677 VERIFY(dmu_object_set_blocksize(os, obj, 678 1ULL << leaf_blockshift, indirect_blockshift, tx) == 0); 679 680 mzap_create_impl(os, obj, normflags, flags, tx); 681 return (obj); 682} 683 684int 685zap_destroy(objset_t *os, uint64_t zapobj, dmu_tx_t *tx) 686{ 687 /* 688 * dmu_object_free will free the object number and free the 689 * data. Freeing the data will cause our pageout function to be 690 * called, which will destroy our data (zap_leaf_t's and zap_t). 691 */ 692 693 return (dmu_object_free(os, zapobj, tx)); 694} 695 696void 697zap_evict(void *dbu) 698{ 699 zap_t *zap = dbu; 700 701 rw_destroy(&zap->zap_rwlock); 702 703 if (zap->zap_ismicro) 704 mze_destroy(zap); 705 else 706 mutex_destroy(&zap->zap_f.zap_num_entries_mtx); 707 708 kmem_free(zap, sizeof (zap_t)); 709} 710 711int 712zap_count(objset_t *os, uint64_t zapobj, uint64_t *count) 713{ 714 zap_t *zap; 715 int err; 716 717 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap); 718 if (err) 719 return (err); 720 if (!zap->zap_ismicro) { 721 err = fzap_count(zap, count); 722 } else { 723 *count = zap->zap_m.zap_num_entries; 724 } 725 zap_unlockdir(zap); 726 return (err); 727} 728 729/* 730 * zn may be NULL; if not specified, it will be computed if needed. 731 * See also the comment above zap_entry_normalization_conflict(). 732 */ 733static boolean_t 734mzap_normalization_conflict(zap_t *zap, zap_name_t *zn, mzap_ent_t *mze) 735{ 736 mzap_ent_t *other; 737 int direction = AVL_BEFORE; 738 boolean_t allocdzn = B_FALSE; 739 740 if (zap->zap_normflags == 0) 741 return (B_FALSE); 742 743again: 744 for (other = avl_walk(&zap->zap_m.zap_avl, mze, direction); 745 other && other->mze_hash == mze->mze_hash; 746 other = avl_walk(&zap->zap_m.zap_avl, other, direction)) { 747 748 if (zn == NULL) { 749 zn = zap_name_alloc(zap, MZE_PHYS(zap, mze)->mze_name, 750 MT_FIRST); 751 allocdzn = B_TRUE; 752 } 753 if (zap_match(zn, MZE_PHYS(zap, other)->mze_name)) { 754 if (allocdzn) 755 zap_name_free(zn); 756 return (B_TRUE); 757 } 758 } 759 760 if (direction == AVL_BEFORE) { 761 direction = AVL_AFTER; 762 goto again; 763 } 764 765 if (allocdzn) 766 zap_name_free(zn); 767 return (B_FALSE); 768} 769 770/* 771 * Routines for manipulating attributes. 772 */ 773 774int 775zap_lookup(objset_t *os, uint64_t zapobj, const char *name, 776 uint64_t integer_size, uint64_t num_integers, void *buf) 777{ 778 return (zap_lookup_norm(os, zapobj, name, integer_size, 779 num_integers, buf, MT_EXACT, NULL, 0, NULL)); 780} 781 782int 783zap_lookup_norm(objset_t *os, uint64_t zapobj, const char *name, 784 uint64_t integer_size, uint64_t num_integers, void *buf, 785 matchtype_t mt, char *realname, int rn_len, 786 boolean_t *ncp) 787{ 788 zap_t *zap; 789 int err; 790 mzap_ent_t *mze; 791 zap_name_t *zn; 792 793 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap); 794 if (err) 795 return (err); 796 zn = zap_name_alloc(zap, name, mt); 797 if (zn == NULL) { 798 zap_unlockdir(zap); 799 return (SET_ERROR(ENOTSUP)); 800 } 801 802 if (!zap->zap_ismicro) { 803 err = fzap_lookup(zn, integer_size, num_integers, buf, 804 realname, rn_len, ncp); 805 } else { 806 mze = mze_find(zn); 807 if (mze == NULL) { 808 err = SET_ERROR(ENOENT); 809 } else { 810 if (num_integers < 1) { 811 err = SET_ERROR(EOVERFLOW); 812 } else if (integer_size != 8) { 813 err = SET_ERROR(EINVAL); 814 } else { 815 *(uint64_t *)buf = 816 MZE_PHYS(zap, mze)->mze_value; 817 (void) strlcpy(realname, 818 MZE_PHYS(zap, mze)->mze_name, rn_len); 819 if (ncp) { 820 *ncp = mzap_normalization_conflict(zap, 821 zn, mze); 822 } 823 } 824 } 825 } 826 zap_name_free(zn); 827 zap_unlockdir(zap); 828 return (err); 829} 830 831int 832zap_prefetch_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key, 833 int key_numints) 834{ 835 zap_t *zap; 836 int err; 837 zap_name_t *zn; 838 839 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap); 840 if (err) 841 return (err); 842 zn = zap_name_alloc_uint64(zap, key, key_numints); 843 if (zn == NULL) { 844 zap_unlockdir(zap); 845 return (SET_ERROR(ENOTSUP)); 846 } 847 848 fzap_prefetch(zn); 849 zap_name_free(zn); 850 zap_unlockdir(zap); 851 return (err); 852} 853 854int 855zap_lookup_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key, 856 int key_numints, uint64_t integer_size, uint64_t num_integers, void *buf) 857{ 858 zap_t *zap; 859 int err; 860 zap_name_t *zn; 861 862 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap); 863 if (err) 864 return (err); 865 zn = zap_name_alloc_uint64(zap, key, key_numints); 866 if (zn == NULL) { 867 zap_unlockdir(zap); 868 return (SET_ERROR(ENOTSUP)); 869 } 870 871 err = fzap_lookup(zn, integer_size, num_integers, buf, 872 NULL, 0, NULL); 873 zap_name_free(zn); 874 zap_unlockdir(zap); 875 return (err); 876} 877 878int 879zap_contains(objset_t *os, uint64_t zapobj, const char *name) 880{ 881 int err = zap_lookup_norm(os, zapobj, name, 0, 882 0, NULL, MT_EXACT, NULL, 0, NULL); 883 if (err == EOVERFLOW || err == EINVAL) 884 err = 0; /* found, but skipped reading the value */ 885 return (err); 886} 887 888int 889zap_length(objset_t *os, uint64_t zapobj, const char *name, 890 uint64_t *integer_size, uint64_t *num_integers) 891{ 892 zap_t *zap; 893 int err; 894 mzap_ent_t *mze; 895 zap_name_t *zn; 896 897 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap); 898 if (err) 899 return (err); 900 zn = zap_name_alloc(zap, name, MT_EXACT); 901 if (zn == NULL) { 902 zap_unlockdir(zap); 903 return (SET_ERROR(ENOTSUP)); 904 } 905 if (!zap->zap_ismicro) { 906 err = fzap_length(zn, integer_size, num_integers); 907 } else { 908 mze = mze_find(zn); 909 if (mze == NULL) { 910 err = SET_ERROR(ENOENT); 911 } else { 912 if (integer_size) 913 *integer_size = 8; 914 if (num_integers) 915 *num_integers = 1; 916 } 917 } 918 zap_name_free(zn); 919 zap_unlockdir(zap); 920 return (err); 921} 922 923int 924zap_length_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key, 925 int key_numints, uint64_t *integer_size, uint64_t *num_integers) 926{ 927 zap_t *zap; 928 int err; 929 zap_name_t *zn; 930 931 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap); 932 if (err) 933 return (err); 934 zn = zap_name_alloc_uint64(zap, key, key_numints); 935 if (zn == NULL) { 936 zap_unlockdir(zap); 937 return (SET_ERROR(ENOTSUP)); 938 } 939 err = fzap_length(zn, integer_size, num_integers); 940 zap_name_free(zn); 941 zap_unlockdir(zap); 942 return (err); 943} 944 945static void 946mzap_addent(zap_name_t *zn, uint64_t value) 947{ 948 int i; 949 zap_t *zap = zn->zn_zap; 950 int start = zap->zap_m.zap_alloc_next; 951 uint32_t cd; 952 953 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock)); 954 955#ifdef ZFS_DEBUG 956 for (i = 0; i < zap->zap_m.zap_num_chunks; i++) { 957 mzap_ent_phys_t *mze = &zap_m_phys(zap)->mz_chunk[i]; 958 ASSERT(strcmp(zn->zn_key_orig, mze->mze_name) != 0); 959 } 960#endif 961 962 cd = mze_find_unused_cd(zap, zn->zn_hash); 963 /* given the limited size of the microzap, this can't happen */ 964 ASSERT(cd < zap_maxcd(zap)); 965 966again: 967 for (i = start; i < zap->zap_m.zap_num_chunks; i++) { 968 mzap_ent_phys_t *mze = &zap_m_phys(zap)->mz_chunk[i]; 969 if (mze->mze_name[0] == 0) { 970 mze->mze_value = value; 971 mze->mze_cd = cd; 972 (void) strcpy(mze->mze_name, zn->zn_key_orig); 973 zap->zap_m.zap_num_entries++; 974 zap->zap_m.zap_alloc_next = i+1; 975 if (zap->zap_m.zap_alloc_next == 976 zap->zap_m.zap_num_chunks) 977 zap->zap_m.zap_alloc_next = 0; 978 VERIFY(0 == mze_insert(zap, i, zn->zn_hash)); 979 return; 980 } 981 } 982 if (start != 0) { 983 start = 0; 984 goto again; 985 } 986 ASSERT(!"out of entries!"); 987} 988 989int 990zap_add(objset_t *os, uint64_t zapobj, const char *key, 991 int integer_size, uint64_t num_integers, 992 const void *val, dmu_tx_t *tx) 993{ 994 zap_t *zap; 995 int err; 996 mzap_ent_t *mze; 997 const uint64_t *intval = val; 998 zap_name_t *zn; 999 1000 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap); 1001 if (err) 1002 return (err); 1003 zn = zap_name_alloc(zap, key, MT_EXACT); 1004 if (zn == NULL) { 1005 zap_unlockdir(zap); 1006 return (SET_ERROR(ENOTSUP)); 1007 } 1008 if (!zap->zap_ismicro) { 1009 err = fzap_add(zn, integer_size, num_integers, val, tx); 1010 zap = zn->zn_zap; /* fzap_add() may change zap */ 1011 } else if (integer_size != 8 || num_integers != 1 || 1012 strlen(key) >= MZAP_NAME_LEN) { 1013 err = mzap_upgrade(&zn->zn_zap, tx, 0); 1014 if (err == 0) 1015 err = fzap_add(zn, integer_size, num_integers, val, tx); 1016 zap = zn->zn_zap; /* fzap_add() may change zap */ 1017 } else { 1018 mze = mze_find(zn); 1019 if (mze != NULL) { 1020 err = SET_ERROR(EEXIST); 1021 } else { 1022 mzap_addent(zn, *intval); 1023 } 1024 } 1025 ASSERT(zap == zn->zn_zap); 1026 zap_name_free(zn); 1027 if (zap != NULL) /* may be NULL if fzap_add() failed */ 1028 zap_unlockdir(zap); 1029 return (err); 1030} 1031 1032int 1033zap_add_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key, 1034 int key_numints, int integer_size, uint64_t num_integers, 1035 const void *val, dmu_tx_t *tx) 1036{ 1037 zap_t *zap; 1038 int err; 1039 zap_name_t *zn; 1040 1041 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap); 1042 if (err) 1043 return (err); 1044 zn = zap_name_alloc_uint64(zap, key, key_numints); 1045 if (zn == NULL) { 1046 zap_unlockdir(zap); 1047 return (SET_ERROR(ENOTSUP)); 1048 } 1049 err = fzap_add(zn, integer_size, num_integers, val, tx); 1050 zap = zn->zn_zap; /* fzap_add() may change zap */ 1051 zap_name_free(zn); 1052 if (zap != NULL) /* may be NULL if fzap_add() failed */ 1053 zap_unlockdir(zap); 1054 return (err); 1055} 1056 1057int 1058zap_update(objset_t *os, uint64_t zapobj, const char *name, 1059 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx) 1060{ 1061 zap_t *zap; 1062 mzap_ent_t *mze; 1063 uint64_t oldval; 1064 const uint64_t *intval = val; 1065 zap_name_t *zn; 1066 int err; 1067 1068#ifdef ZFS_DEBUG 1069 /* 1070 * If there is an old value, it shouldn't change across the 1071 * lockdir (eg, due to bprewrite's xlation). 1072 */ 1073 if (integer_size == 8 && num_integers == 1) 1074 (void) zap_lookup(os, zapobj, name, 8, 1, &oldval); 1075#endif 1076 1077 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap); 1078 if (err) 1079 return (err); 1080 zn = zap_name_alloc(zap, name, MT_EXACT); 1081 if (zn == NULL) { 1082 zap_unlockdir(zap); 1083 return (SET_ERROR(ENOTSUP)); 1084 } 1085 if (!zap->zap_ismicro) { 1086 err = fzap_update(zn, integer_size, num_integers, val, tx); 1087 zap = zn->zn_zap; /* fzap_update() may change zap */ 1088 } else if (integer_size != 8 || num_integers != 1 || 1089 strlen(name) >= MZAP_NAME_LEN) { 1090 dprintf("upgrading obj %llu: intsz=%u numint=%llu name=%s\n", 1091 zapobj, integer_size, num_integers, name); 1092 err = mzap_upgrade(&zn->zn_zap, tx, 0); 1093 if (err == 0) 1094 err = fzap_update(zn, integer_size, num_integers, 1095 val, tx); 1096 zap = zn->zn_zap; /* fzap_update() may change zap */ 1097 } else { 1098 mze = mze_find(zn); 1099 if (mze != NULL) { 1100 ASSERT3U(MZE_PHYS(zap, mze)->mze_value, ==, oldval); 1101 MZE_PHYS(zap, mze)->mze_value = *intval; 1102 } else { 1103 mzap_addent(zn, *intval); 1104 } 1105 } 1106 ASSERT(zap == zn->zn_zap); 1107 zap_name_free(zn); 1108 if (zap != NULL) /* may be NULL if fzap_upgrade() failed */ 1109 zap_unlockdir(zap); 1110 return (err); 1111} 1112 1113int 1114zap_update_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key, 1115 int key_numints, 1116 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx) 1117{ 1118 zap_t *zap; 1119 zap_name_t *zn; 1120 int err; 1121 1122 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap); 1123 if (err) 1124 return (err); 1125 zn = zap_name_alloc_uint64(zap, key, key_numints); 1126 if (zn == NULL) { 1127 zap_unlockdir(zap); 1128 return (SET_ERROR(ENOTSUP)); 1129 } 1130 err = fzap_update(zn, integer_size, num_integers, val, tx); 1131 zap = zn->zn_zap; /* fzap_update() may change zap */ 1132 zap_name_free(zn); 1133 if (zap != NULL) /* may be NULL if fzap_upgrade() failed */ 1134 zap_unlockdir(zap); 1135 return (err); 1136} 1137 1138int 1139zap_remove(objset_t *os, uint64_t zapobj, const char *name, dmu_tx_t *tx) 1140{ 1141 return (zap_remove_norm(os, zapobj, name, MT_EXACT, tx)); 1142} 1143 1144int 1145zap_remove_norm(objset_t *os, uint64_t zapobj, const char *name, 1146 matchtype_t mt, dmu_tx_t *tx) 1147{ 1148 zap_t *zap; 1149 int err; 1150 mzap_ent_t *mze; 1151 zap_name_t *zn; 1152 1153 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, &zap); 1154 if (err) 1155 return (err); 1156 zn = zap_name_alloc(zap, name, mt); 1157 if (zn == NULL) { 1158 zap_unlockdir(zap); 1159 return (SET_ERROR(ENOTSUP)); 1160 } 1161 if (!zap->zap_ismicro) { 1162 err = fzap_remove(zn, tx); 1163 } else { 1164 mze = mze_find(zn); 1165 if (mze == NULL) { 1166 err = SET_ERROR(ENOENT); 1167 } else { 1168 zap->zap_m.zap_num_entries--; 1169 bzero(&zap_m_phys(zap)->mz_chunk[mze->mze_chunkid], 1170 sizeof (mzap_ent_phys_t)); 1171 mze_remove(zap, mze); 1172 } 1173 } 1174 zap_name_free(zn); 1175 zap_unlockdir(zap); 1176 return (err); 1177} 1178 1179int 1180zap_remove_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key, 1181 int key_numints, dmu_tx_t *tx) 1182{ 1183 zap_t *zap; 1184 int err; 1185 zap_name_t *zn; 1186 1187 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, &zap); 1188 if (err) 1189 return (err); 1190 zn = zap_name_alloc_uint64(zap, key, key_numints); 1191 if (zn == NULL) { 1192 zap_unlockdir(zap); 1193 return (SET_ERROR(ENOTSUP)); 1194 } 1195 err = fzap_remove(zn, tx); 1196 zap_name_free(zn); 1197 zap_unlockdir(zap); 1198 return (err); 1199} 1200 1201/* 1202 * Routines for iterating over the attributes. 1203 */ 1204 1205void 1206zap_cursor_init_serialized(zap_cursor_t *zc, objset_t *os, uint64_t zapobj, 1207 uint64_t serialized) 1208{ 1209 zc->zc_objset = os; 1210 zc->zc_zap = NULL; 1211 zc->zc_leaf = NULL; 1212 zc->zc_zapobj = zapobj; 1213 zc->zc_serialized = serialized; 1214 zc->zc_hash = 0; 1215 zc->zc_cd = 0; 1216} 1217 1218void 1219zap_cursor_init(zap_cursor_t *zc, objset_t *os, uint64_t zapobj) 1220{ 1221 zap_cursor_init_serialized(zc, os, zapobj, 0); 1222} 1223 1224void 1225zap_cursor_fini(zap_cursor_t *zc) 1226{ 1227 if (zc->zc_zap) { 1228 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER); 1229 zap_unlockdir(zc->zc_zap); 1230 zc->zc_zap = NULL; 1231 } 1232 if (zc->zc_leaf) { 1233 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER); 1234 zap_put_leaf(zc->zc_leaf); 1235 zc->zc_leaf = NULL; 1236 } 1237 zc->zc_objset = NULL; 1238} 1239 1240uint64_t 1241zap_cursor_serialize(zap_cursor_t *zc) 1242{ 1243 if (zc->zc_hash == -1ULL) 1244 return (-1ULL); 1245 if (zc->zc_zap == NULL) 1246 return (zc->zc_serialized); 1247 ASSERT((zc->zc_hash & zap_maxcd(zc->zc_zap)) == 0); 1248 ASSERT(zc->zc_cd < zap_maxcd(zc->zc_zap)); 1249 1250 /* 1251 * We want to keep the high 32 bits of the cursor zero if we can, so 1252 * that 32-bit programs can access this. So usually use a small 1253 * (28-bit) hash value so we can fit 4 bits of cd into the low 32-bits 1254 * of the cursor. 1255 * 1256 * [ collision differentiator | zap_hashbits()-bit hash value ] 1257 */ 1258 return ((zc->zc_hash >> (64 - zap_hashbits(zc->zc_zap))) | 1259 ((uint64_t)zc->zc_cd << zap_hashbits(zc->zc_zap))); 1260} 1261 1262int 1263zap_cursor_retrieve(zap_cursor_t *zc, zap_attribute_t *za) 1264{ 1265 int err; 1266 avl_index_t idx; 1267 mzap_ent_t mze_tofind; 1268 mzap_ent_t *mze; 1269 1270 if (zc->zc_hash == -1ULL) 1271 return (SET_ERROR(ENOENT)); 1272 1273 if (zc->zc_zap == NULL) { 1274 int hb; 1275 err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL, 1276 RW_READER, TRUE, FALSE, &zc->zc_zap); 1277 if (err) 1278 return (err); 1279 1280 /* 1281 * To support zap_cursor_init_serialized, advance, retrieve, 1282 * we must add to the existing zc_cd, which may already 1283 * be 1 due to the zap_cursor_advance. 1284 */ 1285 ASSERT(zc->zc_hash == 0); 1286 hb = zap_hashbits(zc->zc_zap); 1287 zc->zc_hash = zc->zc_serialized << (64 - hb); 1288 zc->zc_cd += zc->zc_serialized >> hb; 1289 if (zc->zc_cd >= zap_maxcd(zc->zc_zap)) /* corrupt serialized */ 1290 zc->zc_cd = 0; 1291 } else { 1292 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER); 1293 } 1294 if (!zc->zc_zap->zap_ismicro) { 1295 err = fzap_cursor_retrieve(zc->zc_zap, zc, za); 1296 } else { 1297 mze_tofind.mze_hash = zc->zc_hash; 1298 mze_tofind.mze_cd = zc->zc_cd; 1299 1300 mze = avl_find(&zc->zc_zap->zap_m.zap_avl, &mze_tofind, &idx); 1301 if (mze == NULL) { 1302 mze = avl_nearest(&zc->zc_zap->zap_m.zap_avl, 1303 idx, AVL_AFTER); 1304 } 1305 if (mze) { 1306 mzap_ent_phys_t *mzep = MZE_PHYS(zc->zc_zap, mze); 1307 ASSERT3U(mze->mze_cd, ==, mzep->mze_cd); 1308 za->za_normalization_conflict = 1309 mzap_normalization_conflict(zc->zc_zap, NULL, mze); 1310 za->za_integer_length = 8; 1311 za->za_num_integers = 1; 1312 za->za_first_integer = mzep->mze_value; 1313 (void) strcpy(za->za_name, mzep->mze_name); 1314 zc->zc_hash = mze->mze_hash; 1315 zc->zc_cd = mze->mze_cd; 1316 err = 0; 1317 } else { 1318 zc->zc_hash = -1ULL; 1319 err = SET_ERROR(ENOENT); 1320 } 1321 } 1322 rw_exit(&zc->zc_zap->zap_rwlock); 1323 return (err); 1324} 1325 1326void 1327zap_cursor_advance(zap_cursor_t *zc) 1328{ 1329 if (zc->zc_hash == -1ULL) 1330 return; 1331 zc->zc_cd++; 1332} 1333 1334int 1335zap_cursor_move_to_key(zap_cursor_t *zc, const char *name, matchtype_t mt) 1336{ 1337 int err = 0; 1338 mzap_ent_t *mze; 1339 zap_name_t *zn; 1340 1341 if (zc->zc_zap == NULL) { 1342 err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL, 1343 RW_READER, TRUE, FALSE, &zc->zc_zap); 1344 if (err) 1345 return (err); 1346 } else { 1347 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER); 1348 } 1349 1350 zn = zap_name_alloc(zc->zc_zap, name, mt); 1351 if (zn == NULL) { 1352 rw_exit(&zc->zc_zap->zap_rwlock); 1353 return (SET_ERROR(ENOTSUP)); 1354 } 1355 1356 if (!zc->zc_zap->zap_ismicro) { 1357 err = fzap_cursor_move_to_key(zc, zn); 1358 } else { 1359 mze = mze_find(zn); 1360 if (mze == NULL) { 1361 err = SET_ERROR(ENOENT); 1362 goto out; 1363 } 1364 zc->zc_hash = mze->mze_hash; 1365 zc->zc_cd = mze->mze_cd; 1366 } 1367 1368out: 1369 zap_name_free(zn); 1370 rw_exit(&zc->zc_zap->zap_rwlock); 1371 return (err); 1372} 1373 1374int 1375zap_get_stats(objset_t *os, uint64_t zapobj, zap_stats_t *zs) 1376{ 1377 int err; 1378 zap_t *zap; 1379 1380 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap); 1381 if (err) 1382 return (err); 1383 1384 bzero(zs, sizeof (zap_stats_t)); 1385 1386 if (zap->zap_ismicro) { 1387 zs->zs_blocksize = zap->zap_dbuf->db_size; 1388 zs->zs_num_entries = zap->zap_m.zap_num_entries; 1389 zs->zs_num_blocks = 1; 1390 } else { 1391 fzap_get_stats(zap, zs); 1392 } 1393 zap_unlockdir(zap); 1394 return (0); 1395} 1396 1397int 1398zap_count_write(objset_t *os, uint64_t zapobj, const char *name, int add, 1399 uint64_t *towrite, uint64_t *tooverwrite) 1400{ 1401 zap_t *zap; 1402 int err = 0; 1403 1404 /* 1405 * Since, we don't have a name, we cannot figure out which blocks will 1406 * be affected in this operation. So, account for the worst case : 1407 * - 3 blocks overwritten: target leaf, ptrtbl block, header block 1408 * - 4 new blocks written if adding: 1409 * - 2 blocks for possibly split leaves, 1410 * - 2 grown ptrtbl blocks 1411 * 1412 * This also accomodates the case where an add operation to a fairly 1413 * large microzap results in a promotion to fatzap. 1414 */ 1415 if (name == NULL) { 1416 *towrite += (3 + (add ? 4 : 0)) * SPA_OLD_MAXBLOCKSIZE; 1417 return (err); 1418 } 1419 1420 /* 1421 * We lock the zap with adding == FALSE. Because, if we pass 1422 * the actual value of add, it could trigger a mzap_upgrade(). 1423 * At present we are just evaluating the possibility of this operation 1424 * and hence we donot want to trigger an upgrade. 1425 */ 1426 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap); 1427 if (err) 1428 return (err); 1429 1430 if (!zap->zap_ismicro) { 1431 zap_name_t *zn = zap_name_alloc(zap, name, MT_EXACT); 1432 if (zn) { 1433 err = fzap_count_write(zn, add, towrite, 1434 tooverwrite); 1435 zap_name_free(zn); 1436 } else { 1437 /* 1438 * We treat this case as similar to (name == NULL) 1439 */ 1440 *towrite += (3 + (add ? 4 : 0)) * SPA_OLD_MAXBLOCKSIZE; 1441 } 1442 } else { 1443 /* 1444 * We are here if (name != NULL) and this is a micro-zap. 1445 * We account for the header block depending on whether it 1446 * is freeable. 1447 * 1448 * Incase of an add-operation it is hard to find out 1449 * if this add will promote this microzap to fatzap. 1450 * Hence, we consider the worst case and account for the 1451 * blocks assuming this microzap would be promoted to a 1452 * fatzap. 1453 * 1454 * 1 block overwritten : header block 1455 * 4 new blocks written : 2 new split leaf, 2 grown 1456 * ptrtbl blocks 1457 */ 1458 if (dmu_buf_freeable(zap->zap_dbuf)) 1459 *tooverwrite += MZAP_MAX_BLKSZ; 1460 else 1461 *towrite += MZAP_MAX_BLKSZ; 1462 1463 if (add) { 1464 *towrite += 4 * MZAP_MAX_BLKSZ; 1465 } 1466 } 1467 1468 zap_unlockdir(zap); 1469 return (err); 1470}
| 506 507 /* 508 * We're checking zap_ismicro without the lock held, in order to 509 * tell what type of lock we want. Once we have some sort of 510 * lock, see if it really is the right type. In practice this 511 * can only be different if it was upgraded from micro to fat, 512 * and micro wanted WRITER but fat only needs READER. 513 */ 514 lt = (!zap->zap_ismicro && fatreader) ? RW_READER : lti; 515 rw_enter(&zap->zap_rwlock, lt); 516 if (lt != ((!zap->zap_ismicro && fatreader) ? RW_READER : lti)) { 517 /* it was upgraded, now we only need reader */ 518 ASSERT(lt == RW_WRITER); 519 ASSERT(RW_READER == 520 (!zap->zap_ismicro && fatreader) ? RW_READER : lti); 521 rw_downgrade(&zap->zap_rwlock); 522 lt = RW_READER; 523 } 524 525 zap->zap_objset = os; 526 527 if (lt == RW_WRITER) 528 dmu_buf_will_dirty(db, tx); 529 530 ASSERT3P(zap->zap_dbuf, ==, db); 531 532 ASSERT(!zap->zap_ismicro || 533 zap->zap_m.zap_num_entries <= zap->zap_m.zap_num_chunks); 534 if (zap->zap_ismicro && tx && adding && 535 zap->zap_m.zap_num_entries == zap->zap_m.zap_num_chunks) { 536 uint64_t newsz = db->db_size + SPA_MINBLOCKSIZE; 537 if (newsz > MZAP_MAX_BLKSZ) { 538 dprintf("upgrading obj %llu: num_entries=%u\n", 539 obj, zap->zap_m.zap_num_entries); 540 *zapp = zap; 541 return (mzap_upgrade(zapp, tx, 0)); 542 } 543 err = dmu_object_set_blocksize(os, obj, newsz, 0, tx); 544 ASSERT0(err); 545 zap->zap_m.zap_num_chunks = 546 db->db_size / MZAP_ENT_LEN - 1; 547 } 548 549 *zapp = zap; 550 return (0); 551} 552 553void 554zap_unlockdir(zap_t *zap) 555{ 556 rw_exit(&zap->zap_rwlock); 557 dmu_buf_rele(zap->zap_dbuf, NULL); 558} 559 560static int 561mzap_upgrade(zap_t **zapp, dmu_tx_t *tx, zap_flags_t flags) 562{ 563 mzap_phys_t *mzp; 564 int i, sz, nchunks; 565 int err = 0; 566 zap_t *zap = *zapp; 567 568 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock)); 569 570 sz = zap->zap_dbuf->db_size; 571 mzp = zio_buf_alloc(sz); 572 bcopy(zap->zap_dbuf->db_data, mzp, sz); 573 nchunks = zap->zap_m.zap_num_chunks; 574 575 if (!flags) { 576 err = dmu_object_set_blocksize(zap->zap_objset, zap->zap_object, 577 1ULL << fzap_default_block_shift, 0, tx); 578 if (err) { 579 zio_buf_free(mzp, sz); 580 return (err); 581 } 582 } 583 584 dprintf("upgrading obj=%llu with %u chunks\n", 585 zap->zap_object, nchunks); 586 /* XXX destroy the avl later, so we can use the stored hash value */ 587 mze_destroy(zap); 588 589 fzap_upgrade(zap, tx, flags); 590 591 for (i = 0; i < nchunks; i++) { 592 mzap_ent_phys_t *mze = &mzp->mz_chunk[i]; 593 zap_name_t *zn; 594 if (mze->mze_name[0] == 0) 595 continue; 596 dprintf("adding %s=%llu\n", 597 mze->mze_name, mze->mze_value); 598 zn = zap_name_alloc(zap, mze->mze_name, MT_EXACT); 599 err = fzap_add_cd(zn, 8, 1, &mze->mze_value, mze->mze_cd, tx); 600 zap = zn->zn_zap; /* fzap_add_cd() may change zap */ 601 zap_name_free(zn); 602 if (err) 603 break; 604 } 605 zio_buf_free(mzp, sz); 606 *zapp = zap; 607 return (err); 608} 609 610void 611mzap_create_impl(objset_t *os, uint64_t obj, int normflags, zap_flags_t flags, 612 dmu_tx_t *tx) 613{ 614 dmu_buf_t *db; 615 mzap_phys_t *zp; 616 617 VERIFY(0 == dmu_buf_hold(os, obj, 0, FTAG, &db, DMU_READ_NO_PREFETCH)); 618 619#ifdef ZFS_DEBUG 620 { 621 dmu_object_info_t doi; 622 dmu_object_info_from_db(db, &doi); 623 ASSERT3U(DMU_OT_BYTESWAP(doi.doi_type), ==, DMU_BSWAP_ZAP); 624 } 625#endif 626 627 dmu_buf_will_dirty(db, tx); 628 zp = db->db_data; 629 zp->mz_block_type = ZBT_MICRO; 630 zp->mz_salt = ((uintptr_t)db ^ (uintptr_t)tx ^ (obj << 1)) | 1ULL; 631 zp->mz_normflags = normflags; 632 dmu_buf_rele(db, FTAG); 633 634 if (flags != 0) { 635 zap_t *zap; 636 /* Only fat zap supports flags; upgrade immediately. */ 637 VERIFY(0 == zap_lockdir(os, obj, tx, RW_WRITER, 638 B_FALSE, B_FALSE, &zap)); 639 VERIFY3U(0, ==, mzap_upgrade(&zap, tx, flags)); 640 zap_unlockdir(zap); 641 } 642} 643 644int 645zap_create_claim(objset_t *os, uint64_t obj, dmu_object_type_t ot, 646 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 647{ 648 return (zap_create_claim_norm(os, obj, 649 0, ot, bonustype, bonuslen, tx)); 650} 651 652int 653zap_create_claim_norm(objset_t *os, uint64_t obj, int normflags, 654 dmu_object_type_t ot, 655 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 656{ 657 int err; 658 659 err = dmu_object_claim(os, obj, ot, 0, bonustype, bonuslen, tx); 660 if (err != 0) 661 return (err); 662 mzap_create_impl(os, obj, normflags, 0, tx); 663 return (0); 664} 665 666uint64_t 667zap_create(objset_t *os, dmu_object_type_t ot, 668 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 669{ 670 return (zap_create_norm(os, 0, ot, bonustype, bonuslen, tx)); 671} 672 673uint64_t 674zap_create_norm(objset_t *os, int normflags, dmu_object_type_t ot, 675 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 676{ 677 uint64_t obj = dmu_object_alloc(os, ot, 0, bonustype, bonuslen, tx); 678 679 mzap_create_impl(os, obj, normflags, 0, tx); 680 return (obj); 681} 682 683uint64_t 684zap_create_flags(objset_t *os, int normflags, zap_flags_t flags, 685 dmu_object_type_t ot, int leaf_blockshift, int indirect_blockshift, 686 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 687{ 688 uint64_t obj = dmu_object_alloc(os, ot, 0, bonustype, bonuslen, tx); 689 690 ASSERT(leaf_blockshift >= SPA_MINBLOCKSHIFT && 691 leaf_blockshift <= SPA_OLD_MAXBLOCKSHIFT && 692 indirect_blockshift >= SPA_MINBLOCKSHIFT && 693 indirect_blockshift <= SPA_OLD_MAXBLOCKSHIFT); 694 695 VERIFY(dmu_object_set_blocksize(os, obj, 696 1ULL << leaf_blockshift, indirect_blockshift, tx) == 0); 697 698 mzap_create_impl(os, obj, normflags, flags, tx); 699 return (obj); 700} 701 702int 703zap_destroy(objset_t *os, uint64_t zapobj, dmu_tx_t *tx) 704{ 705 /* 706 * dmu_object_free will free the object number and free the 707 * data. Freeing the data will cause our pageout function to be 708 * called, which will destroy our data (zap_leaf_t's and zap_t). 709 */ 710 711 return (dmu_object_free(os, zapobj, tx)); 712} 713 714void 715zap_evict(void *dbu) 716{ 717 zap_t *zap = dbu; 718 719 rw_destroy(&zap->zap_rwlock); 720 721 if (zap->zap_ismicro) 722 mze_destroy(zap); 723 else 724 mutex_destroy(&zap->zap_f.zap_num_entries_mtx); 725 726 kmem_free(zap, sizeof (zap_t)); 727} 728 729int 730zap_count(objset_t *os, uint64_t zapobj, uint64_t *count) 731{ 732 zap_t *zap; 733 int err; 734 735 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap); 736 if (err) 737 return (err); 738 if (!zap->zap_ismicro) { 739 err = fzap_count(zap, count); 740 } else { 741 *count = zap->zap_m.zap_num_entries; 742 } 743 zap_unlockdir(zap); 744 return (err); 745} 746 747/* 748 * zn may be NULL; if not specified, it will be computed if needed. 749 * See also the comment above zap_entry_normalization_conflict(). 750 */ 751static boolean_t 752mzap_normalization_conflict(zap_t *zap, zap_name_t *zn, mzap_ent_t *mze) 753{ 754 mzap_ent_t *other; 755 int direction = AVL_BEFORE; 756 boolean_t allocdzn = B_FALSE; 757 758 if (zap->zap_normflags == 0) 759 return (B_FALSE); 760 761again: 762 for (other = avl_walk(&zap->zap_m.zap_avl, mze, direction); 763 other && other->mze_hash == mze->mze_hash; 764 other = avl_walk(&zap->zap_m.zap_avl, other, direction)) { 765 766 if (zn == NULL) { 767 zn = zap_name_alloc(zap, MZE_PHYS(zap, mze)->mze_name, 768 MT_FIRST); 769 allocdzn = B_TRUE; 770 } 771 if (zap_match(zn, MZE_PHYS(zap, other)->mze_name)) { 772 if (allocdzn) 773 zap_name_free(zn); 774 return (B_TRUE); 775 } 776 } 777 778 if (direction == AVL_BEFORE) { 779 direction = AVL_AFTER; 780 goto again; 781 } 782 783 if (allocdzn) 784 zap_name_free(zn); 785 return (B_FALSE); 786} 787 788/* 789 * Routines for manipulating attributes. 790 */ 791 792int 793zap_lookup(objset_t *os, uint64_t zapobj, const char *name, 794 uint64_t integer_size, uint64_t num_integers, void *buf) 795{ 796 return (zap_lookup_norm(os, zapobj, name, integer_size, 797 num_integers, buf, MT_EXACT, NULL, 0, NULL)); 798} 799 800int 801zap_lookup_norm(objset_t *os, uint64_t zapobj, const char *name, 802 uint64_t integer_size, uint64_t num_integers, void *buf, 803 matchtype_t mt, char *realname, int rn_len, 804 boolean_t *ncp) 805{ 806 zap_t *zap; 807 int err; 808 mzap_ent_t *mze; 809 zap_name_t *zn; 810 811 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap); 812 if (err) 813 return (err); 814 zn = zap_name_alloc(zap, name, mt); 815 if (zn == NULL) { 816 zap_unlockdir(zap); 817 return (SET_ERROR(ENOTSUP)); 818 } 819 820 if (!zap->zap_ismicro) { 821 err = fzap_lookup(zn, integer_size, num_integers, buf, 822 realname, rn_len, ncp); 823 } else { 824 mze = mze_find(zn); 825 if (mze == NULL) { 826 err = SET_ERROR(ENOENT); 827 } else { 828 if (num_integers < 1) { 829 err = SET_ERROR(EOVERFLOW); 830 } else if (integer_size != 8) { 831 err = SET_ERROR(EINVAL); 832 } else { 833 *(uint64_t *)buf = 834 MZE_PHYS(zap, mze)->mze_value; 835 (void) strlcpy(realname, 836 MZE_PHYS(zap, mze)->mze_name, rn_len); 837 if (ncp) { 838 *ncp = mzap_normalization_conflict(zap, 839 zn, mze); 840 } 841 } 842 } 843 } 844 zap_name_free(zn); 845 zap_unlockdir(zap); 846 return (err); 847} 848 849int 850zap_prefetch_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key, 851 int key_numints) 852{ 853 zap_t *zap; 854 int err; 855 zap_name_t *zn; 856 857 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap); 858 if (err) 859 return (err); 860 zn = zap_name_alloc_uint64(zap, key, key_numints); 861 if (zn == NULL) { 862 zap_unlockdir(zap); 863 return (SET_ERROR(ENOTSUP)); 864 } 865 866 fzap_prefetch(zn); 867 zap_name_free(zn); 868 zap_unlockdir(zap); 869 return (err); 870} 871 872int 873zap_lookup_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key, 874 int key_numints, uint64_t integer_size, uint64_t num_integers, void *buf) 875{ 876 zap_t *zap; 877 int err; 878 zap_name_t *zn; 879 880 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap); 881 if (err) 882 return (err); 883 zn = zap_name_alloc_uint64(zap, key, key_numints); 884 if (zn == NULL) { 885 zap_unlockdir(zap); 886 return (SET_ERROR(ENOTSUP)); 887 } 888 889 err = fzap_lookup(zn, integer_size, num_integers, buf, 890 NULL, 0, NULL); 891 zap_name_free(zn); 892 zap_unlockdir(zap); 893 return (err); 894} 895 896int 897zap_contains(objset_t *os, uint64_t zapobj, const char *name) 898{ 899 int err = zap_lookup_norm(os, zapobj, name, 0, 900 0, NULL, MT_EXACT, NULL, 0, NULL); 901 if (err == EOVERFLOW || err == EINVAL) 902 err = 0; /* found, but skipped reading the value */ 903 return (err); 904} 905 906int 907zap_length(objset_t *os, uint64_t zapobj, const char *name, 908 uint64_t *integer_size, uint64_t *num_integers) 909{ 910 zap_t *zap; 911 int err; 912 mzap_ent_t *mze; 913 zap_name_t *zn; 914 915 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap); 916 if (err) 917 return (err); 918 zn = zap_name_alloc(zap, name, MT_EXACT); 919 if (zn == NULL) { 920 zap_unlockdir(zap); 921 return (SET_ERROR(ENOTSUP)); 922 } 923 if (!zap->zap_ismicro) { 924 err = fzap_length(zn, integer_size, num_integers); 925 } else { 926 mze = mze_find(zn); 927 if (mze == NULL) { 928 err = SET_ERROR(ENOENT); 929 } else { 930 if (integer_size) 931 *integer_size = 8; 932 if (num_integers) 933 *num_integers = 1; 934 } 935 } 936 zap_name_free(zn); 937 zap_unlockdir(zap); 938 return (err); 939} 940 941int 942zap_length_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key, 943 int key_numints, uint64_t *integer_size, uint64_t *num_integers) 944{ 945 zap_t *zap; 946 int err; 947 zap_name_t *zn; 948 949 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap); 950 if (err) 951 return (err); 952 zn = zap_name_alloc_uint64(zap, key, key_numints); 953 if (zn == NULL) { 954 zap_unlockdir(zap); 955 return (SET_ERROR(ENOTSUP)); 956 } 957 err = fzap_length(zn, integer_size, num_integers); 958 zap_name_free(zn); 959 zap_unlockdir(zap); 960 return (err); 961} 962 963static void 964mzap_addent(zap_name_t *zn, uint64_t value) 965{ 966 int i; 967 zap_t *zap = zn->zn_zap; 968 int start = zap->zap_m.zap_alloc_next; 969 uint32_t cd; 970 971 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock)); 972 973#ifdef ZFS_DEBUG 974 for (i = 0; i < zap->zap_m.zap_num_chunks; i++) { 975 mzap_ent_phys_t *mze = &zap_m_phys(zap)->mz_chunk[i]; 976 ASSERT(strcmp(zn->zn_key_orig, mze->mze_name) != 0); 977 } 978#endif 979 980 cd = mze_find_unused_cd(zap, zn->zn_hash); 981 /* given the limited size of the microzap, this can't happen */ 982 ASSERT(cd < zap_maxcd(zap)); 983 984again: 985 for (i = start; i < zap->zap_m.zap_num_chunks; i++) { 986 mzap_ent_phys_t *mze = &zap_m_phys(zap)->mz_chunk[i]; 987 if (mze->mze_name[0] == 0) { 988 mze->mze_value = value; 989 mze->mze_cd = cd; 990 (void) strcpy(mze->mze_name, zn->zn_key_orig); 991 zap->zap_m.zap_num_entries++; 992 zap->zap_m.zap_alloc_next = i+1; 993 if (zap->zap_m.zap_alloc_next == 994 zap->zap_m.zap_num_chunks) 995 zap->zap_m.zap_alloc_next = 0; 996 VERIFY(0 == mze_insert(zap, i, zn->zn_hash)); 997 return; 998 } 999 } 1000 if (start != 0) { 1001 start = 0; 1002 goto again; 1003 } 1004 ASSERT(!"out of entries!"); 1005} 1006 1007int 1008zap_add(objset_t *os, uint64_t zapobj, const char *key, 1009 int integer_size, uint64_t num_integers, 1010 const void *val, dmu_tx_t *tx) 1011{ 1012 zap_t *zap; 1013 int err; 1014 mzap_ent_t *mze; 1015 const uint64_t *intval = val; 1016 zap_name_t *zn; 1017 1018 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap); 1019 if (err) 1020 return (err); 1021 zn = zap_name_alloc(zap, key, MT_EXACT); 1022 if (zn == NULL) { 1023 zap_unlockdir(zap); 1024 return (SET_ERROR(ENOTSUP)); 1025 } 1026 if (!zap->zap_ismicro) { 1027 err = fzap_add(zn, integer_size, num_integers, val, tx); 1028 zap = zn->zn_zap; /* fzap_add() may change zap */ 1029 } else if (integer_size != 8 || num_integers != 1 || 1030 strlen(key) >= MZAP_NAME_LEN) { 1031 err = mzap_upgrade(&zn->zn_zap, tx, 0); 1032 if (err == 0) 1033 err = fzap_add(zn, integer_size, num_integers, val, tx); 1034 zap = zn->zn_zap; /* fzap_add() may change zap */ 1035 } else { 1036 mze = mze_find(zn); 1037 if (mze != NULL) { 1038 err = SET_ERROR(EEXIST); 1039 } else { 1040 mzap_addent(zn, *intval); 1041 } 1042 } 1043 ASSERT(zap == zn->zn_zap); 1044 zap_name_free(zn); 1045 if (zap != NULL) /* may be NULL if fzap_add() failed */ 1046 zap_unlockdir(zap); 1047 return (err); 1048} 1049 1050int 1051zap_add_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key, 1052 int key_numints, int integer_size, uint64_t num_integers, 1053 const void *val, dmu_tx_t *tx) 1054{ 1055 zap_t *zap; 1056 int err; 1057 zap_name_t *zn; 1058 1059 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap); 1060 if (err) 1061 return (err); 1062 zn = zap_name_alloc_uint64(zap, key, key_numints); 1063 if (zn == NULL) { 1064 zap_unlockdir(zap); 1065 return (SET_ERROR(ENOTSUP)); 1066 } 1067 err = fzap_add(zn, integer_size, num_integers, val, tx); 1068 zap = zn->zn_zap; /* fzap_add() may change zap */ 1069 zap_name_free(zn); 1070 if (zap != NULL) /* may be NULL if fzap_add() failed */ 1071 zap_unlockdir(zap); 1072 return (err); 1073} 1074 1075int 1076zap_update(objset_t *os, uint64_t zapobj, const char *name, 1077 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx) 1078{ 1079 zap_t *zap; 1080 mzap_ent_t *mze; 1081 uint64_t oldval; 1082 const uint64_t *intval = val; 1083 zap_name_t *zn; 1084 int err; 1085 1086#ifdef ZFS_DEBUG 1087 /* 1088 * If there is an old value, it shouldn't change across the 1089 * lockdir (eg, due to bprewrite's xlation). 1090 */ 1091 if (integer_size == 8 && num_integers == 1) 1092 (void) zap_lookup(os, zapobj, name, 8, 1, &oldval); 1093#endif 1094 1095 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap); 1096 if (err) 1097 return (err); 1098 zn = zap_name_alloc(zap, name, MT_EXACT); 1099 if (zn == NULL) { 1100 zap_unlockdir(zap); 1101 return (SET_ERROR(ENOTSUP)); 1102 } 1103 if (!zap->zap_ismicro) { 1104 err = fzap_update(zn, integer_size, num_integers, val, tx); 1105 zap = zn->zn_zap; /* fzap_update() may change zap */ 1106 } else if (integer_size != 8 || num_integers != 1 || 1107 strlen(name) >= MZAP_NAME_LEN) { 1108 dprintf("upgrading obj %llu: intsz=%u numint=%llu name=%s\n", 1109 zapobj, integer_size, num_integers, name); 1110 err = mzap_upgrade(&zn->zn_zap, tx, 0); 1111 if (err == 0) 1112 err = fzap_update(zn, integer_size, num_integers, 1113 val, tx); 1114 zap = zn->zn_zap; /* fzap_update() may change zap */ 1115 } else { 1116 mze = mze_find(zn); 1117 if (mze != NULL) { 1118 ASSERT3U(MZE_PHYS(zap, mze)->mze_value, ==, oldval); 1119 MZE_PHYS(zap, mze)->mze_value = *intval; 1120 } else { 1121 mzap_addent(zn, *intval); 1122 } 1123 } 1124 ASSERT(zap == zn->zn_zap); 1125 zap_name_free(zn); 1126 if (zap != NULL) /* may be NULL if fzap_upgrade() failed */ 1127 zap_unlockdir(zap); 1128 return (err); 1129} 1130 1131int 1132zap_update_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key, 1133 int key_numints, 1134 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx) 1135{ 1136 zap_t *zap; 1137 zap_name_t *zn; 1138 int err; 1139 1140 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap); 1141 if (err) 1142 return (err); 1143 zn = zap_name_alloc_uint64(zap, key, key_numints); 1144 if (zn == NULL) { 1145 zap_unlockdir(zap); 1146 return (SET_ERROR(ENOTSUP)); 1147 } 1148 err = fzap_update(zn, integer_size, num_integers, val, tx); 1149 zap = zn->zn_zap; /* fzap_update() may change zap */ 1150 zap_name_free(zn); 1151 if (zap != NULL) /* may be NULL if fzap_upgrade() failed */ 1152 zap_unlockdir(zap); 1153 return (err); 1154} 1155 1156int 1157zap_remove(objset_t *os, uint64_t zapobj, const char *name, dmu_tx_t *tx) 1158{ 1159 return (zap_remove_norm(os, zapobj, name, MT_EXACT, tx)); 1160} 1161 1162int 1163zap_remove_norm(objset_t *os, uint64_t zapobj, const char *name, 1164 matchtype_t mt, dmu_tx_t *tx) 1165{ 1166 zap_t *zap; 1167 int err; 1168 mzap_ent_t *mze; 1169 zap_name_t *zn; 1170 1171 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, &zap); 1172 if (err) 1173 return (err); 1174 zn = zap_name_alloc(zap, name, mt); 1175 if (zn == NULL) { 1176 zap_unlockdir(zap); 1177 return (SET_ERROR(ENOTSUP)); 1178 } 1179 if (!zap->zap_ismicro) { 1180 err = fzap_remove(zn, tx); 1181 } else { 1182 mze = mze_find(zn); 1183 if (mze == NULL) { 1184 err = SET_ERROR(ENOENT); 1185 } else { 1186 zap->zap_m.zap_num_entries--; 1187 bzero(&zap_m_phys(zap)->mz_chunk[mze->mze_chunkid], 1188 sizeof (mzap_ent_phys_t)); 1189 mze_remove(zap, mze); 1190 } 1191 } 1192 zap_name_free(zn); 1193 zap_unlockdir(zap); 1194 return (err); 1195} 1196 1197int 1198zap_remove_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key, 1199 int key_numints, dmu_tx_t *tx) 1200{ 1201 zap_t *zap; 1202 int err; 1203 zap_name_t *zn; 1204 1205 err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, &zap); 1206 if (err) 1207 return (err); 1208 zn = zap_name_alloc_uint64(zap, key, key_numints); 1209 if (zn == NULL) { 1210 zap_unlockdir(zap); 1211 return (SET_ERROR(ENOTSUP)); 1212 } 1213 err = fzap_remove(zn, tx); 1214 zap_name_free(zn); 1215 zap_unlockdir(zap); 1216 return (err); 1217} 1218 1219/* 1220 * Routines for iterating over the attributes. 1221 */ 1222 1223void 1224zap_cursor_init_serialized(zap_cursor_t *zc, objset_t *os, uint64_t zapobj, 1225 uint64_t serialized) 1226{ 1227 zc->zc_objset = os; 1228 zc->zc_zap = NULL; 1229 zc->zc_leaf = NULL; 1230 zc->zc_zapobj = zapobj; 1231 zc->zc_serialized = serialized; 1232 zc->zc_hash = 0; 1233 zc->zc_cd = 0; 1234} 1235 1236void 1237zap_cursor_init(zap_cursor_t *zc, objset_t *os, uint64_t zapobj) 1238{ 1239 zap_cursor_init_serialized(zc, os, zapobj, 0); 1240} 1241 1242void 1243zap_cursor_fini(zap_cursor_t *zc) 1244{ 1245 if (zc->zc_zap) { 1246 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER); 1247 zap_unlockdir(zc->zc_zap); 1248 zc->zc_zap = NULL; 1249 } 1250 if (zc->zc_leaf) { 1251 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER); 1252 zap_put_leaf(zc->zc_leaf); 1253 zc->zc_leaf = NULL; 1254 } 1255 zc->zc_objset = NULL; 1256} 1257 1258uint64_t 1259zap_cursor_serialize(zap_cursor_t *zc) 1260{ 1261 if (zc->zc_hash == -1ULL) 1262 return (-1ULL); 1263 if (zc->zc_zap == NULL) 1264 return (zc->zc_serialized); 1265 ASSERT((zc->zc_hash & zap_maxcd(zc->zc_zap)) == 0); 1266 ASSERT(zc->zc_cd < zap_maxcd(zc->zc_zap)); 1267 1268 /* 1269 * We want to keep the high 32 bits of the cursor zero if we can, so 1270 * that 32-bit programs can access this. So usually use a small 1271 * (28-bit) hash value so we can fit 4 bits of cd into the low 32-bits 1272 * of the cursor. 1273 * 1274 * [ collision differentiator | zap_hashbits()-bit hash value ] 1275 */ 1276 return ((zc->zc_hash >> (64 - zap_hashbits(zc->zc_zap))) | 1277 ((uint64_t)zc->zc_cd << zap_hashbits(zc->zc_zap))); 1278} 1279 1280int 1281zap_cursor_retrieve(zap_cursor_t *zc, zap_attribute_t *za) 1282{ 1283 int err; 1284 avl_index_t idx; 1285 mzap_ent_t mze_tofind; 1286 mzap_ent_t *mze; 1287 1288 if (zc->zc_hash == -1ULL) 1289 return (SET_ERROR(ENOENT)); 1290 1291 if (zc->zc_zap == NULL) { 1292 int hb; 1293 err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL, 1294 RW_READER, TRUE, FALSE, &zc->zc_zap); 1295 if (err) 1296 return (err); 1297 1298 /* 1299 * To support zap_cursor_init_serialized, advance, retrieve, 1300 * we must add to the existing zc_cd, which may already 1301 * be 1 due to the zap_cursor_advance. 1302 */ 1303 ASSERT(zc->zc_hash == 0); 1304 hb = zap_hashbits(zc->zc_zap); 1305 zc->zc_hash = zc->zc_serialized << (64 - hb); 1306 zc->zc_cd += zc->zc_serialized >> hb; 1307 if (zc->zc_cd >= zap_maxcd(zc->zc_zap)) /* corrupt serialized */ 1308 zc->zc_cd = 0; 1309 } else { 1310 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER); 1311 } 1312 if (!zc->zc_zap->zap_ismicro) { 1313 err = fzap_cursor_retrieve(zc->zc_zap, zc, za); 1314 } else { 1315 mze_tofind.mze_hash = zc->zc_hash; 1316 mze_tofind.mze_cd = zc->zc_cd; 1317 1318 mze = avl_find(&zc->zc_zap->zap_m.zap_avl, &mze_tofind, &idx); 1319 if (mze == NULL) { 1320 mze = avl_nearest(&zc->zc_zap->zap_m.zap_avl, 1321 idx, AVL_AFTER); 1322 } 1323 if (mze) { 1324 mzap_ent_phys_t *mzep = MZE_PHYS(zc->zc_zap, mze); 1325 ASSERT3U(mze->mze_cd, ==, mzep->mze_cd); 1326 za->za_normalization_conflict = 1327 mzap_normalization_conflict(zc->zc_zap, NULL, mze); 1328 za->za_integer_length = 8; 1329 za->za_num_integers = 1; 1330 za->za_first_integer = mzep->mze_value; 1331 (void) strcpy(za->za_name, mzep->mze_name); 1332 zc->zc_hash = mze->mze_hash; 1333 zc->zc_cd = mze->mze_cd; 1334 err = 0; 1335 } else { 1336 zc->zc_hash = -1ULL; 1337 err = SET_ERROR(ENOENT); 1338 } 1339 } 1340 rw_exit(&zc->zc_zap->zap_rwlock); 1341 return (err); 1342} 1343 1344void 1345zap_cursor_advance(zap_cursor_t *zc) 1346{ 1347 if (zc->zc_hash == -1ULL) 1348 return; 1349 zc->zc_cd++; 1350} 1351 1352int 1353zap_cursor_move_to_key(zap_cursor_t *zc, const char *name, matchtype_t mt) 1354{ 1355 int err = 0; 1356 mzap_ent_t *mze; 1357 zap_name_t *zn; 1358 1359 if (zc->zc_zap == NULL) { 1360 err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL, 1361 RW_READER, TRUE, FALSE, &zc->zc_zap); 1362 if (err) 1363 return (err); 1364 } else { 1365 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER); 1366 } 1367 1368 zn = zap_name_alloc(zc->zc_zap, name, mt); 1369 if (zn == NULL) { 1370 rw_exit(&zc->zc_zap->zap_rwlock); 1371 return (SET_ERROR(ENOTSUP)); 1372 } 1373 1374 if (!zc->zc_zap->zap_ismicro) { 1375 err = fzap_cursor_move_to_key(zc, zn); 1376 } else { 1377 mze = mze_find(zn); 1378 if (mze == NULL) { 1379 err = SET_ERROR(ENOENT); 1380 goto out; 1381 } 1382 zc->zc_hash = mze->mze_hash; 1383 zc->zc_cd = mze->mze_cd; 1384 } 1385 1386out: 1387 zap_name_free(zn); 1388 rw_exit(&zc->zc_zap->zap_rwlock); 1389 return (err); 1390} 1391 1392int 1393zap_get_stats(objset_t *os, uint64_t zapobj, zap_stats_t *zs) 1394{ 1395 int err; 1396 zap_t *zap; 1397 1398 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap); 1399 if (err) 1400 return (err); 1401 1402 bzero(zs, sizeof (zap_stats_t)); 1403 1404 if (zap->zap_ismicro) { 1405 zs->zs_blocksize = zap->zap_dbuf->db_size; 1406 zs->zs_num_entries = zap->zap_m.zap_num_entries; 1407 zs->zs_num_blocks = 1; 1408 } else { 1409 fzap_get_stats(zap, zs); 1410 } 1411 zap_unlockdir(zap); 1412 return (0); 1413} 1414 1415int 1416zap_count_write(objset_t *os, uint64_t zapobj, const char *name, int add, 1417 uint64_t *towrite, uint64_t *tooverwrite) 1418{ 1419 zap_t *zap; 1420 int err = 0; 1421 1422 /* 1423 * Since, we don't have a name, we cannot figure out which blocks will 1424 * be affected in this operation. So, account for the worst case : 1425 * - 3 blocks overwritten: target leaf, ptrtbl block, header block 1426 * - 4 new blocks written if adding: 1427 * - 2 blocks for possibly split leaves, 1428 * - 2 grown ptrtbl blocks 1429 * 1430 * This also accomodates the case where an add operation to a fairly 1431 * large microzap results in a promotion to fatzap. 1432 */ 1433 if (name == NULL) { 1434 *towrite += (3 + (add ? 4 : 0)) * SPA_OLD_MAXBLOCKSIZE; 1435 return (err); 1436 } 1437 1438 /* 1439 * We lock the zap with adding == FALSE. Because, if we pass 1440 * the actual value of add, it could trigger a mzap_upgrade(). 1441 * At present we are just evaluating the possibility of this operation 1442 * and hence we donot want to trigger an upgrade. 1443 */ 1444 err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap); 1445 if (err) 1446 return (err); 1447 1448 if (!zap->zap_ismicro) { 1449 zap_name_t *zn = zap_name_alloc(zap, name, MT_EXACT); 1450 if (zn) { 1451 err = fzap_count_write(zn, add, towrite, 1452 tooverwrite); 1453 zap_name_free(zn); 1454 } else { 1455 /* 1456 * We treat this case as similar to (name == NULL) 1457 */ 1458 *towrite += (3 + (add ? 4 : 0)) * SPA_OLD_MAXBLOCKSIZE; 1459 } 1460 } else { 1461 /* 1462 * We are here if (name != NULL) and this is a micro-zap. 1463 * We account for the header block depending on whether it 1464 * is freeable. 1465 * 1466 * Incase of an add-operation it is hard to find out 1467 * if this add will promote this microzap to fatzap. 1468 * Hence, we consider the worst case and account for the 1469 * blocks assuming this microzap would be promoted to a 1470 * fatzap. 1471 * 1472 * 1 block overwritten : header block 1473 * 4 new blocks written : 2 new split leaf, 2 grown 1474 * ptrtbl blocks 1475 */ 1476 if (dmu_buf_freeable(zap->zap_dbuf)) 1477 *tooverwrite += MZAP_MAX_BLKSZ; 1478 else 1479 *towrite += MZAP_MAX_BLKSZ; 1480 1481 if (add) { 1482 *towrite += 4 * MZAP_MAX_BLKSZ; 1483 } 1484 } 1485 1486 zap_unlockdir(zap); 1487 return (err); 1488}
|