1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  23  * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
  24  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
  25  * Copyright (c) 2014 Integros [integros.com]
  26  */
  27 
  28 #include <sys/zio.h>
  29 #include <sys/spa.h>
  30 #include <sys/dmu.h>
  31 #include <sys/zfs_context.h>
  32 #include <sys/zap.h>
  33 #include <sys/refcount.h>
  34 #include <sys/zap_impl.h>
  35 #include <sys/zap_leaf.h>
  36 #include <sys/avl.h>
  37 #include <sys/arc.h>
  38 #include <sys/dmu_objset.h>
  39 
  40 #ifdef _KERNEL
  41 #include <sys/sunddi.h>
  42 #endif
  43 
  44 extern inline mzap_phys_t *zap_m_phys(zap_t *zap);
  45 
  46 static int mzap_upgrade(zap_t **zapp, dmu_tx_t *tx, zap_flags_t flags);
  47 
  48 uint64_t
  49 zap_getflags(zap_t *zap)
  50 {
  51         if (zap->zap_ismicro)
  52                 return (0);
  53         return (zap_f_phys(zap)->zap_flags);
  54 }
  55 
  56 int
  57 zap_hashbits(zap_t *zap)
  58 {
  59         if (zap_getflags(zap) & ZAP_FLAG_HASH64)
  60                 return (48);
  61         else
  62                 return (28);
  63 }
  64 
  65 uint32_t
  66 zap_maxcd(zap_t *zap)
  67 {
  68         if (zap_getflags(zap) & ZAP_FLAG_HASH64)
  69                 return ((1<<16)-1);
  70         else
  71                 return (-1U);
  72 }
  73 
  74 static uint64_t
  75 zap_hash(zap_name_t *zn)
  76 {
  77         zap_t *zap = zn->zn_zap;
  78         uint64_t h = 0;
  79 
  80         if (zap_getflags(zap) & ZAP_FLAG_PRE_HASHED_KEY) {
  81                 ASSERT(zap_getflags(zap) & ZAP_FLAG_UINT64_KEY);
  82                 h = *(uint64_t *)zn->zn_key_orig;
  83         } else {
  84                 h = zap->zap_salt;
  85                 ASSERT(h != 0);
  86                 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
  87 
  88                 if (zap_getflags(zap) & ZAP_FLAG_UINT64_KEY) {
  89                         int i;
  90                         const uint64_t *wp = zn->zn_key_norm;
  91 
  92                         ASSERT(zn->zn_key_intlen == 8);
  93                         for (i = 0; i < zn->zn_key_norm_numints; wp++, i++) {
  94                                 int j;
  95                                 uint64_t word = *wp;
  96 
  97                                 for (j = 0; j < zn->zn_key_intlen; j++) {
  98                                         h = (h >> 8) ^
  99                                             zfs_crc64_table[(h ^ word) & 0xFF];
 100                                         word >>= NBBY;
 101                                 }
 102                         }
 103                 } else {
 104                         int i, len;
 105                         const uint8_t *cp = zn->zn_key_norm;
 106 
 107                         /*
 108                          * We previously stored the terminating null on
 109                          * disk, but didn't hash it, so we need to
 110                          * continue to not hash it.  (The
 111                          * zn_key_*_numints includes the terminating
 112                          * null for non-binary keys.)
 113                          */
 114                         len = zn->zn_key_norm_numints - 1;
 115 
 116                         ASSERT(zn->zn_key_intlen == 1);
 117                         for (i = 0; i < len; cp++, i++) {
 118                                 h = (h >> 8) ^
 119                                     zfs_crc64_table[(h ^ *cp) & 0xFF];
 120                         }
 121                 }
 122         }
 123         /*
 124          * Don't use all 64 bits, since we need some in the cookie for
 125          * the collision differentiator.  We MUST use the high bits,
 126          * since those are the ones that we first pay attention to when
 127          * chosing the bucket.
 128          */
 129         h &= ~((1ULL << (64 - zap_hashbits(zap))) - 1);
 130 
 131         return (h);
 132 }
 133 
 134 static int
 135 zap_normalize(zap_t *zap, const char *name, char *namenorm)
 136 {
 137         size_t inlen, outlen;
 138         int err;
 139 
 140         ASSERT(!(zap_getflags(zap) & ZAP_FLAG_UINT64_KEY));
 141 
 142         inlen = strlen(name) + 1;
 143         outlen = ZAP_MAXNAMELEN;
 144 
 145         err = 0;
 146         (void) u8_textprep_str((char *)name, &inlen, namenorm, &outlen,
 147             zap->zap_normflags | U8_TEXTPREP_IGNORE_NULL |
 148             U8_TEXTPREP_IGNORE_INVALID, U8_UNICODE_LATEST, &err);
 149 
 150         return (err);
 151 }
 152 
 153 boolean_t
 154 zap_match(zap_name_t *zn, const char *matchname)
 155 {
 156         ASSERT(!(zap_getflags(zn->zn_zap) & ZAP_FLAG_UINT64_KEY));
 157 
 158         if (zn->zn_matchtype == MT_FIRST) {
 159                 char norm[ZAP_MAXNAMELEN];
 160 
 161                 if (zap_normalize(zn->zn_zap, matchname, norm) != 0)
 162                         return (B_FALSE);
 163 
 164                 return (strcmp(zn->zn_key_norm, norm) == 0);
 165         } else {
 166                 /* MT_BEST or MT_EXACT */
 167                 return (strcmp(zn->zn_key_orig, matchname) == 0);
 168         }
 169 }
 170 
 171 void
 172 zap_name_free(zap_name_t *zn)
 173 {
 174         kmem_free(zn, sizeof (zap_name_t));
 175 }
 176 
 177 zap_name_t *
 178 zap_name_alloc(zap_t *zap, const char *key, matchtype_t mt)
 179 {
 180         zap_name_t *zn = kmem_alloc(sizeof (zap_name_t), KM_SLEEP);
 181 
 182         zn->zn_zap = zap;
 183         zn->zn_key_intlen = sizeof (*key);
 184         zn->zn_key_orig = key;
 185         zn->zn_key_orig_numints = strlen(zn->zn_key_orig) + 1;
 186         zn->zn_matchtype = mt;
 187         if (zap->zap_normflags) {
 188                 if (zap_normalize(zap, key, zn->zn_normbuf) != 0) {
 189                         zap_name_free(zn);
 190                         return (NULL);
 191                 }
 192                 zn->zn_key_norm = zn->zn_normbuf;
 193                 zn->zn_key_norm_numints = strlen(zn->zn_key_norm) + 1;
 194         } else {
 195                 if (mt != MT_EXACT) {
 196                         zap_name_free(zn);
 197                         return (NULL);
 198                 }
 199                 zn->zn_key_norm = zn->zn_key_orig;
 200                 zn->zn_key_norm_numints = zn->zn_key_orig_numints;
 201         }
 202 
 203         zn->zn_hash = zap_hash(zn);
 204         return (zn);
 205 }
 206 
 207 zap_name_t *
 208 zap_name_alloc_uint64(zap_t *zap, const uint64_t *key, int numints)
 209 {
 210         zap_name_t *zn = kmem_alloc(sizeof (zap_name_t), KM_SLEEP);
 211 
 212         ASSERT(zap->zap_normflags == 0);
 213         zn->zn_zap = zap;
 214         zn->zn_key_intlen = sizeof (*key);
 215         zn->zn_key_orig = zn->zn_key_norm = key;
 216         zn->zn_key_orig_numints = zn->zn_key_norm_numints = numints;
 217         zn->zn_matchtype = MT_EXACT;
 218 
 219         zn->zn_hash = zap_hash(zn);
 220         return (zn);
 221 }
 222 
 223 static void
 224 mzap_byteswap(mzap_phys_t *buf, size_t size)
 225 {
 226         int i, max;
 227         buf->mz_block_type = BSWAP_64(buf->mz_block_type);
 228         buf->mz_salt = BSWAP_64(buf->mz_salt);
 229         buf->mz_normflags = BSWAP_64(buf->mz_normflags);
 230         max = (size / MZAP_ENT_LEN) - 1;
 231         for (i = 0; i < max; i++) {
 232                 buf->mz_chunk[i].mze_value =
 233                     BSWAP_64(buf->mz_chunk[i].mze_value);
 234                 buf->mz_chunk[i].mze_cd =
 235                     BSWAP_32(buf->mz_chunk[i].mze_cd);
 236         }
 237 }
 238 
 239 void
 240 zap_byteswap(void *buf, size_t size)
 241 {
 242         uint64_t block_type;
 243 
 244         block_type = *(uint64_t *)buf;
 245 
 246         if (block_type == ZBT_MICRO || block_type == BSWAP_64(ZBT_MICRO)) {
 247                 /* ASSERT(magic == ZAP_LEAF_MAGIC); */
 248                 mzap_byteswap(buf, size);
 249         } else {
 250                 fzap_byteswap(buf, size);
 251         }
 252 }
 253 
 254 static int
 255 mze_compare(const void *arg1, const void *arg2)
 256 {
 257         const mzap_ent_t *mze1 = arg1;
 258         const mzap_ent_t *mze2 = arg2;
 259 
 260         if (mze1->mze_hash > mze2->mze_hash)
 261                 return (+1);
 262         if (mze1->mze_hash < mze2->mze_hash)
 263                 return (-1);
 264         if (mze1->mze_cd > mze2->mze_cd)
 265                 return (+1);
 266         if (mze1->mze_cd < mze2->mze_cd)
 267                 return (-1);
 268         return (0);
 269 }
 270 
 271 static void
 272 mze_insert(zap_t *zap, int chunkid, uint64_t hash)
 273 {
 274         mzap_ent_t *mze;
 275 
 276         ASSERT(zap->zap_ismicro);
 277         ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
 278 
 279         mze = kmem_alloc(sizeof (mzap_ent_t), KM_SLEEP);
 280         mze->mze_chunkid = chunkid;
 281         mze->mze_hash = hash;
 282         mze->mze_cd = MZE_PHYS(zap, mze)->mze_cd;
 283         ASSERT(MZE_PHYS(zap, mze)->mze_name[0] != 0);
 284         avl_add(&zap->zap_m.zap_avl, mze);
 285 }
 286 
 287 static mzap_ent_t *
 288 mze_find(zap_name_t *zn)
 289 {
 290         mzap_ent_t mze_tofind;
 291         mzap_ent_t *mze;
 292         avl_index_t idx;
 293         avl_tree_t *avl = &zn->zn_zap->zap_m.zap_avl;
 294 
 295         ASSERT(zn->zn_zap->zap_ismicro);
 296         ASSERT(RW_LOCK_HELD(&zn->zn_zap->zap_rwlock));
 297 
 298         mze_tofind.mze_hash = zn->zn_hash;
 299         mze_tofind.mze_cd = 0;
 300 
 301 again:
 302         mze = avl_find(avl, &mze_tofind, &idx);
 303         if (mze == NULL)
 304                 mze = avl_nearest(avl, idx, AVL_AFTER);
 305         for (; mze && mze->mze_hash == zn->zn_hash; mze = AVL_NEXT(avl, mze)) {
 306                 ASSERT3U(mze->mze_cd, ==, MZE_PHYS(zn->zn_zap, mze)->mze_cd);
 307                 if (zap_match(zn, MZE_PHYS(zn->zn_zap, mze)->mze_name))
 308                         return (mze);
 309         }
 310         if (zn->zn_matchtype == MT_BEST) {
 311                 zn->zn_matchtype = MT_FIRST;
 312                 goto again;
 313         }
 314         return (NULL);
 315 }
 316 
 317 static uint32_t
 318 mze_find_unused_cd(zap_t *zap, uint64_t hash)
 319 {
 320         mzap_ent_t mze_tofind;
 321         mzap_ent_t *mze;
 322         avl_index_t idx;
 323         avl_tree_t *avl = &zap->zap_m.zap_avl;
 324         uint32_t cd;
 325 
 326         ASSERT(zap->zap_ismicro);
 327         ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
 328 
 329         mze_tofind.mze_hash = hash;
 330         mze_tofind.mze_cd = 0;
 331 
 332         cd = 0;
 333         for (mze = avl_find(avl, &mze_tofind, &idx);
 334             mze && mze->mze_hash == hash; mze = AVL_NEXT(avl, mze)) {
 335                 if (mze->mze_cd != cd)
 336                         break;
 337                 cd++;
 338         }
 339 
 340         return (cd);
 341 }
 342 
 343 static void
 344 mze_remove(zap_t *zap, mzap_ent_t *mze)
 345 {
 346         ASSERT(zap->zap_ismicro);
 347         ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
 348 
 349         avl_remove(&zap->zap_m.zap_avl, mze);
 350         kmem_free(mze, sizeof (mzap_ent_t));
 351 }
 352 
 353 static void
 354 mze_destroy(zap_t *zap)
 355 {
 356         mzap_ent_t *mze;
 357         void *avlcookie = NULL;
 358 
 359         while (mze = avl_destroy_nodes(&zap->zap_m.zap_avl, &avlcookie))
 360                 kmem_free(mze, sizeof (mzap_ent_t));
 361         avl_destroy(&zap->zap_m.zap_avl);
 362 }
 363 
 364 static zap_t *
 365 mzap_open(objset_t *os, uint64_t obj, dmu_buf_t *db)
 366 {
 367         zap_t *winner;
 368         zap_t *zap;
 369         int i;
 370 
 371         ASSERT3U(MZAP_ENT_LEN, ==, sizeof (mzap_ent_phys_t));
 372 
 373         zap = kmem_zalloc(sizeof (zap_t), KM_SLEEP);
 374         rw_init(&zap->zap_rwlock, 0, 0, 0);
 375         rw_enter(&zap->zap_rwlock, RW_WRITER);
 376         zap->zap_objset = os;
 377         zap->zap_object = obj;
 378         zap->zap_dbuf = db;
 379 
 380         if (*(uint64_t *)db->db_data != ZBT_MICRO) {
 381                 mutex_init(&zap->zap_f.zap_num_entries_mtx, 0, 0, 0);
 382                 zap->zap_f.zap_block_shift = highbit64(db->db_size) - 1;
 383         } else {
 384                 zap->zap_ismicro = TRUE;
 385         }
 386 
 387         /*
 388          * Make sure that zap_ismicro is set before we let others see
 389          * it, because zap_lockdir() checks zap_ismicro without the lock
 390          * held.
 391          */
 392         dmu_buf_init_user(&zap->zap_dbu, zap_evict, &zap->zap_dbuf);
 393         winner = dmu_buf_set_user(db, &zap->zap_dbu);
 394 
 395         if (winner != NULL) {
 396                 rw_exit(&zap->zap_rwlock);
 397                 rw_destroy(&zap->zap_rwlock);
 398                 if (!zap->zap_ismicro)
 399                         mutex_destroy(&zap->zap_f.zap_num_entries_mtx);
 400                 kmem_free(zap, sizeof (zap_t));
 401                 return (winner);
 402         }
 403 
 404         if (zap->zap_ismicro) {
 405                 zap->zap_salt = zap_m_phys(zap)->mz_salt;
 406                 zap->zap_normflags = zap_m_phys(zap)->mz_normflags;
 407                 zap->zap_m.zap_num_chunks = db->db_size / MZAP_ENT_LEN - 1;
 408                 avl_create(&zap->zap_m.zap_avl, mze_compare,
 409                     sizeof (mzap_ent_t), offsetof(mzap_ent_t, mze_node));
 410 
 411                 for (i = 0; i < zap->zap_m.zap_num_chunks; i++) {
 412                         mzap_ent_phys_t *mze =
 413                             &zap_m_phys(zap)->mz_chunk[i];
 414                         if (mze->mze_name[0]) {
 415                                 zap_name_t *zn;
 416 
 417                                 zap->zap_m.zap_num_entries++;
 418                                 zn = zap_name_alloc(zap, mze->mze_name,
 419                                     MT_EXACT);
 420                                 mze_insert(zap, i, zn->zn_hash);
 421                                 zap_name_free(zn);
 422                         }
 423                 }
 424         } else {
 425                 zap->zap_salt = zap_f_phys(zap)->zap_salt;
 426                 zap->zap_normflags = zap_f_phys(zap)->zap_normflags;
 427 
 428                 ASSERT3U(sizeof (struct zap_leaf_header), ==,
 429                     2*ZAP_LEAF_CHUNKSIZE);
 430 
 431                 /*
 432                  * The embedded pointer table should not overlap the
 433                  * other members.
 434                  */
 435                 ASSERT3P(&ZAP_EMBEDDED_PTRTBL_ENT(zap, 0), >,
 436                     &zap_f_phys(zap)->zap_salt);
 437 
 438                 /*
 439                  * The embedded pointer table should end at the end of
 440                  * the block
 441                  */
 442                 ASSERT3U((uintptr_t)&ZAP_EMBEDDED_PTRTBL_ENT(zap,
 443                     1<<ZAP_EMBEDDED_PTRTBL_SHIFT(zap)) -
 444                     (uintptr_t)zap_f_phys(zap), ==,
 445                     zap->zap_dbuf->db_size);
 446         }
 447         rw_exit(&zap->zap_rwlock);
 448         return (zap);
 449 }
 450 
 451 int
 452 zap_lockdir(objset_t *os, uint64_t obj, dmu_tx_t *tx,
 453     krw_t lti, boolean_t fatreader, boolean_t adding, zap_t **zapp)
 454 {
 455         zap_t *zap;
 456         dmu_buf_t *db;
 457         krw_t lt;
 458         int err;
 459 
 460         *zapp = NULL;
 461 
 462         err = dmu_buf_hold(os, obj, 0, NULL, &db, DMU_READ_NO_PREFETCH);
 463         if (err)
 464                 return (err);
 465 
 466 #ifdef ZFS_DEBUG
 467         {
 468                 dmu_object_info_t doi;
 469                 dmu_object_info_from_db(db, &doi);
 470                 ASSERT3U(DMU_OT_BYTESWAP(doi.doi_type), ==, DMU_BSWAP_ZAP);
 471         }
 472 #endif
 473 
 474         zap = dmu_buf_get_user(db);
 475         if (zap == NULL)
 476                 zap = mzap_open(os, obj, db);
 477 
 478         /*
 479          * We're checking zap_ismicro without the lock held, in order to
 480          * tell what type of lock we want.  Once we have some sort of
 481          * lock, see if it really is the right type.  In practice this
 482          * can only be different if it was upgraded from micro to fat,
 483          * and micro wanted WRITER but fat only needs READER.
 484          */
 485         lt = (!zap->zap_ismicro && fatreader) ? RW_READER : lti;
 486         rw_enter(&zap->zap_rwlock, lt);
 487         if (lt != ((!zap->zap_ismicro && fatreader) ? RW_READER : lti)) {
 488                 /* it was upgraded, now we only need reader */
 489                 ASSERT(lt == RW_WRITER);
 490                 ASSERT(RW_READER ==
 491                     (!zap->zap_ismicro && fatreader) ? RW_READER : lti);
 492                 rw_downgrade(&zap->zap_rwlock);
 493                 lt = RW_READER;
 494         }
 495 
 496         zap->zap_objset = os;
 497 
 498         if (lt == RW_WRITER)
 499                 dmu_buf_will_dirty(db, tx);
 500 
 501         ASSERT3P(zap->zap_dbuf, ==, db);
 502 
 503         ASSERT(!zap->zap_ismicro ||
 504             zap->zap_m.zap_num_entries <= zap->zap_m.zap_num_chunks);
 505         if (zap->zap_ismicro && tx && adding &&
 506             zap->zap_m.zap_num_entries == zap->zap_m.zap_num_chunks) {
 507                 uint64_t newsz = db->db_size + SPA_MINBLOCKSIZE;
 508                 if (newsz > MZAP_MAX_BLKSZ) {
 509                         dprintf("upgrading obj %llu: num_entries=%u\n",
 510                             obj, zap->zap_m.zap_num_entries);
 511                         *zapp = zap;
 512                         return (mzap_upgrade(zapp, tx, 0));
 513                 }
 514                 err = dmu_object_set_blocksize(os, obj, newsz, 0, tx);
 515                 ASSERT0(err);
 516                 zap->zap_m.zap_num_chunks =
 517                     db->db_size / MZAP_ENT_LEN - 1;
 518         }
 519 
 520         *zapp = zap;
 521         return (0);
 522 }
 523 
 524 void
 525 zap_unlockdir(zap_t *zap)
 526 {
 527         rw_exit(&zap->zap_rwlock);
 528         dmu_buf_rele(zap->zap_dbuf, NULL);
 529 }
 530 
 531 static int
 532 mzap_upgrade(zap_t **zapp, dmu_tx_t *tx, zap_flags_t flags)
 533 {
 534         mzap_phys_t *mzp;
 535         int i, sz, nchunks;
 536         int err = 0;
 537         zap_t *zap = *zapp;
 538 
 539         ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
 540 
 541         sz = zap->zap_dbuf->db_size;
 542         mzp = zio_buf_alloc(sz);
 543         bcopy(zap->zap_dbuf->db_data, mzp, sz);
 544         nchunks = zap->zap_m.zap_num_chunks;
 545 
 546         if (!flags) {
 547                 err = dmu_object_set_blocksize(zap->zap_objset, zap->zap_object,
 548                     1ULL << fzap_default_block_shift, 0, tx);
 549                 if (err) {
 550                         zio_buf_free(mzp, sz);
 551                         return (err);
 552                 }
 553         }
 554 
 555         dprintf("upgrading obj=%llu with %u chunks\n",
 556             zap->zap_object, nchunks);
 557         /* XXX destroy the avl later, so we can use the stored hash value */
 558         mze_destroy(zap);
 559 
 560         fzap_upgrade(zap, tx, flags);
 561 
 562         for (i = 0; i < nchunks; i++) {
 563                 mzap_ent_phys_t *mze = &mzp->mz_chunk[i];
 564                 zap_name_t *zn;
 565                 if (mze->mze_name[0] == 0)
 566                         continue;
 567                 dprintf("adding %s=%llu\n",
 568                     mze->mze_name, mze->mze_value);
 569                 zn = zap_name_alloc(zap, mze->mze_name, MT_EXACT);
 570                 err = fzap_add_cd(zn, 8, 1, &mze->mze_value, mze->mze_cd, tx);
 571                 zap = zn->zn_zap;    /* fzap_add_cd() may change zap */
 572                 zap_name_free(zn);
 573                 if (err)
 574                         break;
 575         }
 576         zio_buf_free(mzp, sz);
 577         *zapp = zap;
 578         return (err);
 579 }
 580 
 581 void
 582 mzap_create_impl(objset_t *os, uint64_t obj, int normflags, zap_flags_t flags,
 583     dmu_tx_t *tx)
 584 {
 585         dmu_buf_t *db;
 586         mzap_phys_t *zp;
 587 
 588         VERIFY(0 == dmu_buf_hold(os, obj, 0, FTAG, &db, DMU_READ_NO_PREFETCH));
 589 
 590 #ifdef ZFS_DEBUG
 591         {
 592                 dmu_object_info_t doi;
 593                 dmu_object_info_from_db(db, &doi);
 594                 ASSERT3U(DMU_OT_BYTESWAP(doi.doi_type), ==, DMU_BSWAP_ZAP);
 595         }
 596 #endif
 597 
 598         dmu_buf_will_dirty(db, tx);
 599         zp = db->db_data;
 600         zp->mz_block_type = ZBT_MICRO;
 601         zp->mz_salt = ((uintptr_t)db ^ (uintptr_t)tx ^ (obj << 1)) | 1ULL;
 602         zp->mz_normflags = normflags;
 603         dmu_buf_rele(db, FTAG);
 604 
 605         if (flags != 0) {
 606                 zap_t *zap;
 607                 /* Only fat zap supports flags; upgrade immediately. */
 608                 VERIFY(0 == zap_lockdir(os, obj, tx, RW_WRITER,
 609                     B_FALSE, B_FALSE, &zap));
 610                 VERIFY3U(0, ==, mzap_upgrade(&zap, tx, flags));
 611                 zap_unlockdir(zap);
 612         }
 613 }
 614 
 615 int
 616 zap_create_claim(objset_t *os, uint64_t obj, dmu_object_type_t ot,
 617     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
 618 {
 619         return (zap_create_claim_norm(os, obj,
 620             0, ot, bonustype, bonuslen, tx));
 621 }
 622 
 623 int
 624 zap_create_claim_norm(objset_t *os, uint64_t obj, int normflags,
 625     dmu_object_type_t ot,
 626     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
 627 {
 628         int err;
 629 
 630         err = dmu_object_claim(os, obj, ot, 0, bonustype, bonuslen, tx);
 631         if (err != 0)
 632                 return (err);
 633         mzap_create_impl(os, obj, normflags, 0, tx);
 634         return (0);
 635 }
 636 
 637 uint64_t
 638 zap_create(objset_t *os, dmu_object_type_t ot,
 639     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
 640 {
 641         return (zap_create_norm(os, 0, ot, bonustype, bonuslen, tx));
 642 }
 643 
 644 uint64_t
 645 zap_create_norm(objset_t *os, int normflags, dmu_object_type_t ot,
 646     dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx)
 647 {
 648         uint64_t obj = dmu_object_alloc(os, ot, 0, bonustype, bonuslen, tx);
 649 
 650         mzap_create_impl(os, obj, normflags, 0, tx);
 651         return (obj);
 652 }
 653 
 654 uint64_t
 655 zap_create_flags(objset_t *os, int normflags, zap_flags_t flags,
 656     dmu_object_type_t ot, int leaf_blockshift, int indirect_blockshift,
 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         ASSERT(leaf_blockshift >= SPA_MINBLOCKSHIFT &&
 662             leaf_blockshift <= SPA_OLD_MAXBLOCKSHIFT &&
 663             indirect_blockshift >= SPA_MINBLOCKSHIFT &&
 664             indirect_blockshift <= SPA_OLD_MAXBLOCKSHIFT);
 665 
 666         VERIFY(dmu_object_set_blocksize(os, obj,
 667             1ULL << leaf_blockshift, indirect_blockshift, tx) == 0);
 668 
 669         mzap_create_impl(os, obj, normflags, flags, tx);
 670         return (obj);
 671 }
 672 
 673 int
 674 zap_destroy(objset_t *os, uint64_t zapobj, dmu_tx_t *tx)
 675 {
 676         /*
 677          * dmu_object_free will free the object number and free the
 678          * data.  Freeing the data will cause our pageout function to be
 679          * called, which will destroy our data (zap_leaf_t's and zap_t).
 680          */
 681 
 682         return (dmu_object_free(os, zapobj, tx));
 683 }
 684 
 685 void
 686 zap_evict(void *dbu)
 687 {
 688         zap_t *zap = dbu;
 689 
 690         rw_destroy(&zap->zap_rwlock);
 691 
 692         if (zap->zap_ismicro)
 693                 mze_destroy(zap);
 694         else
 695                 mutex_destroy(&zap->zap_f.zap_num_entries_mtx);
 696 
 697         kmem_free(zap, sizeof (zap_t));
 698 }
 699 
 700 int
 701 zap_count(objset_t *os, uint64_t zapobj, uint64_t *count)
 702 {
 703         zap_t *zap;
 704         int err;
 705 
 706         err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
 707         if (err)
 708                 return (err);
 709         if (!zap->zap_ismicro) {
 710                 err = fzap_count(zap, count);
 711         } else {
 712                 *count = zap->zap_m.zap_num_entries;
 713         }
 714         zap_unlockdir(zap);
 715         return (err);
 716 }
 717 
 718 /*
 719  * zn may be NULL; if not specified, it will be computed if needed.
 720  * See also the comment above zap_entry_normalization_conflict().
 721  */
 722 static boolean_t
 723 mzap_normalization_conflict(zap_t *zap, zap_name_t *zn, mzap_ent_t *mze)
 724 {
 725         mzap_ent_t *other;
 726         int direction = AVL_BEFORE;
 727         boolean_t allocdzn = B_FALSE;
 728 
 729         if (zap->zap_normflags == 0)
 730                 return (B_FALSE);
 731 
 732 again:
 733         for (other = avl_walk(&zap->zap_m.zap_avl, mze, direction);
 734             other && other->mze_hash == mze->mze_hash;
 735             other = avl_walk(&zap->zap_m.zap_avl, other, direction)) {
 736 
 737                 if (zn == NULL) {
 738                         zn = zap_name_alloc(zap, MZE_PHYS(zap, mze)->mze_name,
 739                             MT_FIRST);
 740                         allocdzn = B_TRUE;
 741                 }
 742                 if (zap_match(zn, MZE_PHYS(zap, other)->mze_name)) {
 743                         if (allocdzn)
 744                                 zap_name_free(zn);
 745                         return (B_TRUE);
 746                 }
 747         }
 748 
 749         if (direction == AVL_BEFORE) {
 750                 direction = AVL_AFTER;
 751                 goto again;
 752         }
 753 
 754         if (allocdzn)
 755                 zap_name_free(zn);
 756         return (B_FALSE);
 757 }
 758 
 759 /*
 760  * Routines for manipulating attributes.
 761  */
 762 
 763 int
 764 zap_lookup(objset_t *os, uint64_t zapobj, const char *name,
 765     uint64_t integer_size, uint64_t num_integers, void *buf)
 766 {
 767         return (zap_lookup_norm(os, zapobj, name, integer_size,
 768             num_integers, buf, MT_EXACT, NULL, 0, NULL));
 769 }
 770 
 771 int
 772 zap_lookup_norm(objset_t *os, uint64_t zapobj, const char *name,
 773     uint64_t integer_size, uint64_t num_integers, void *buf,
 774     matchtype_t mt, char *realname, int rn_len,
 775     boolean_t *ncp)
 776 {
 777         zap_t *zap;
 778         int err;
 779         mzap_ent_t *mze;
 780         zap_name_t *zn;
 781 
 782         err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
 783         if (err)
 784                 return (err);
 785         zn = zap_name_alloc(zap, name, mt);
 786         if (zn == NULL) {
 787                 zap_unlockdir(zap);
 788                 return (SET_ERROR(ENOTSUP));
 789         }
 790 
 791         if (!zap->zap_ismicro) {
 792                 err = fzap_lookup(zn, integer_size, num_integers, buf,
 793                     realname, rn_len, ncp);
 794         } else {
 795                 mze = mze_find(zn);
 796                 if (mze == NULL) {
 797                         err = SET_ERROR(ENOENT);
 798                 } else {
 799                         if (num_integers < 1) {
 800                                 err = SET_ERROR(EOVERFLOW);
 801                         } else if (integer_size != 8) {
 802                                 err = SET_ERROR(EINVAL);
 803                         } else {
 804                                 *(uint64_t *)buf =
 805                                     MZE_PHYS(zap, mze)->mze_value;
 806                                 (void) strlcpy(realname,
 807                                     MZE_PHYS(zap, mze)->mze_name, rn_len);
 808                                 if (ncp) {
 809                                         *ncp = mzap_normalization_conflict(zap,
 810                                             zn, mze);
 811                                 }
 812                         }
 813                 }
 814         }
 815         zap_name_free(zn);
 816         zap_unlockdir(zap);
 817         return (err);
 818 }
 819 
 820 int
 821 zap_prefetch_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
 822     int key_numints)
 823 {
 824         zap_t *zap;
 825         int err;
 826         zap_name_t *zn;
 827 
 828         err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
 829         if (err)
 830                 return (err);
 831         zn = zap_name_alloc_uint64(zap, key, key_numints);
 832         if (zn == NULL) {
 833                 zap_unlockdir(zap);
 834                 return (SET_ERROR(ENOTSUP));
 835         }
 836 
 837         fzap_prefetch(zn);
 838         zap_name_free(zn);
 839         zap_unlockdir(zap);
 840         return (err);
 841 }
 842 
 843 int
 844 zap_lookup_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
 845     int key_numints, uint64_t integer_size, uint64_t num_integers, void *buf)
 846 {
 847         zap_t *zap;
 848         int err;
 849         zap_name_t *zn;
 850 
 851         err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
 852         if (err)
 853                 return (err);
 854         zn = zap_name_alloc_uint64(zap, key, key_numints);
 855         if (zn == NULL) {
 856                 zap_unlockdir(zap);
 857                 return (SET_ERROR(ENOTSUP));
 858         }
 859 
 860         err = fzap_lookup(zn, integer_size, num_integers, buf,
 861             NULL, 0, NULL);
 862         zap_name_free(zn);
 863         zap_unlockdir(zap);
 864         return (err);
 865 }
 866 
 867 int
 868 zap_contains(objset_t *os, uint64_t zapobj, const char *name)
 869 {
 870         int err = zap_lookup_norm(os, zapobj, name, 0,
 871             0, NULL, MT_EXACT, NULL, 0, NULL);
 872         if (err == EOVERFLOW || err == EINVAL)
 873                 err = 0; /* found, but skipped reading the value */
 874         return (err);
 875 }
 876 
 877 int
 878 zap_length(objset_t *os, uint64_t zapobj, const char *name,
 879     uint64_t *integer_size, uint64_t *num_integers)
 880 {
 881         zap_t *zap;
 882         int err;
 883         mzap_ent_t *mze;
 884         zap_name_t *zn;
 885 
 886         err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
 887         if (err)
 888                 return (err);
 889         zn = zap_name_alloc(zap, name, MT_EXACT);
 890         if (zn == NULL) {
 891                 zap_unlockdir(zap);
 892                 return (SET_ERROR(ENOTSUP));
 893         }
 894         if (!zap->zap_ismicro) {
 895                 err = fzap_length(zn, integer_size, num_integers);
 896         } else {
 897                 mze = mze_find(zn);
 898                 if (mze == NULL) {
 899                         err = SET_ERROR(ENOENT);
 900                 } else {
 901                         if (integer_size)
 902                                 *integer_size = 8;
 903                         if (num_integers)
 904                                 *num_integers = 1;
 905                 }
 906         }
 907         zap_name_free(zn);
 908         zap_unlockdir(zap);
 909         return (err);
 910 }
 911 
 912 int
 913 zap_length_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
 914     int key_numints, uint64_t *integer_size, uint64_t *num_integers)
 915 {
 916         zap_t *zap;
 917         int err;
 918         zap_name_t *zn;
 919 
 920         err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
 921         if (err)
 922                 return (err);
 923         zn = zap_name_alloc_uint64(zap, key, key_numints);
 924         if (zn == NULL) {
 925                 zap_unlockdir(zap);
 926                 return (SET_ERROR(ENOTSUP));
 927         }
 928         err = fzap_length(zn, integer_size, num_integers);
 929         zap_name_free(zn);
 930         zap_unlockdir(zap);
 931         return (err);
 932 }
 933 
 934 static void
 935 mzap_addent(zap_name_t *zn, uint64_t value)
 936 {
 937         int i;
 938         zap_t *zap = zn->zn_zap;
 939         int start = zap->zap_m.zap_alloc_next;
 940         uint32_t cd;
 941 
 942         ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
 943 
 944 #ifdef ZFS_DEBUG
 945         for (i = 0; i < zap->zap_m.zap_num_chunks; i++) {
 946                 mzap_ent_phys_t *mze = &zap_m_phys(zap)->mz_chunk[i];
 947                 ASSERT(strcmp(zn->zn_key_orig, mze->mze_name) != 0);
 948         }
 949 #endif
 950 
 951         cd = mze_find_unused_cd(zap, zn->zn_hash);
 952         /* given the limited size of the microzap, this can't happen */
 953         ASSERT(cd < zap_maxcd(zap));
 954 
 955 again:
 956         for (i = start; i < zap->zap_m.zap_num_chunks; i++) {
 957                 mzap_ent_phys_t *mze = &zap_m_phys(zap)->mz_chunk[i];
 958                 if (mze->mze_name[0] == 0) {
 959                         mze->mze_value = value;
 960                         mze->mze_cd = cd;
 961                         (void) strcpy(mze->mze_name, zn->zn_key_orig);
 962                         zap->zap_m.zap_num_entries++;
 963                         zap->zap_m.zap_alloc_next = i+1;
 964                         if (zap->zap_m.zap_alloc_next ==
 965                             zap->zap_m.zap_num_chunks)
 966                                 zap->zap_m.zap_alloc_next = 0;
 967                         mze_insert(zap, i, zn->zn_hash);
 968                         return;
 969                 }
 970         }
 971         if (start != 0) {
 972                 start = 0;
 973                 goto again;
 974         }
 975         ASSERT(!"out of entries!");
 976 }
 977 
 978 int
 979 zap_add(objset_t *os, uint64_t zapobj, const char *key,
 980     int integer_size, uint64_t num_integers,
 981     const void *val, dmu_tx_t *tx)
 982 {
 983         zap_t *zap;
 984         int err;
 985         mzap_ent_t *mze;
 986         const uint64_t *intval = val;
 987         zap_name_t *zn;
 988 
 989         err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
 990         if (err)
 991                 return (err);
 992         zn = zap_name_alloc(zap, key, MT_EXACT);
 993         if (zn == NULL) {
 994                 zap_unlockdir(zap);
 995                 return (SET_ERROR(ENOTSUP));
 996         }
 997         if (!zap->zap_ismicro) {
 998                 err = fzap_add(zn, integer_size, num_integers, val, tx);
 999                 zap = zn->zn_zap;    /* fzap_add() may change zap */
1000         } else if (integer_size != 8 || num_integers != 1 ||
1001             strlen(key) >= MZAP_NAME_LEN) {
1002                 err = mzap_upgrade(&zn->zn_zap, tx, 0);
1003                 if (err == 0)
1004                         err = fzap_add(zn, integer_size, num_integers, val, tx);
1005                 zap = zn->zn_zap;    /* fzap_add() may change zap */
1006         } else {
1007                 mze = mze_find(zn);
1008                 if (mze != NULL) {
1009                         err = SET_ERROR(EEXIST);
1010                 } else {
1011                         mzap_addent(zn, *intval);
1012                 }
1013         }
1014         ASSERT(zap == zn->zn_zap);
1015         zap_name_free(zn);
1016         if (zap != NULL)        /* may be NULL if fzap_add() failed */
1017                 zap_unlockdir(zap);
1018         return (err);
1019 }
1020 
1021 int
1022 zap_add_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1023     int key_numints, int integer_size, uint64_t num_integers,
1024     const void *val, dmu_tx_t *tx)
1025 {
1026         zap_t *zap;
1027         int err;
1028         zap_name_t *zn;
1029 
1030         err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
1031         if (err)
1032                 return (err);
1033         zn = zap_name_alloc_uint64(zap, key, key_numints);
1034         if (zn == NULL) {
1035                 zap_unlockdir(zap);
1036                 return (SET_ERROR(ENOTSUP));
1037         }
1038         err = fzap_add(zn, integer_size, num_integers, val, tx);
1039         zap = zn->zn_zap;    /* fzap_add() may change zap */
1040         zap_name_free(zn);
1041         if (zap != NULL)        /* may be NULL if fzap_add() failed */
1042                 zap_unlockdir(zap);
1043         return (err);
1044 }
1045 
1046 int
1047 zap_update(objset_t *os, uint64_t zapobj, const char *name,
1048     int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
1049 {
1050         zap_t *zap;
1051         mzap_ent_t *mze;
1052         uint64_t oldval;
1053         const uint64_t *intval = val;
1054         zap_name_t *zn;
1055         int err;
1056 
1057 #ifdef ZFS_DEBUG
1058         /*
1059          * If there is an old value, it shouldn't change across the
1060          * lockdir (eg, due to bprewrite's xlation).
1061          */
1062         if (integer_size == 8 && num_integers == 1)
1063                 (void) zap_lookup(os, zapobj, name, 8, 1, &oldval);
1064 #endif
1065 
1066         err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
1067         if (err)
1068                 return (err);
1069         zn = zap_name_alloc(zap, name, MT_EXACT);
1070         if (zn == NULL) {
1071                 zap_unlockdir(zap);
1072                 return (SET_ERROR(ENOTSUP));
1073         }
1074         if (!zap->zap_ismicro) {
1075                 err = fzap_update(zn, integer_size, num_integers, val, tx);
1076                 zap = zn->zn_zap;    /* fzap_update() may change zap */
1077         } else if (integer_size != 8 || num_integers != 1 ||
1078             strlen(name) >= MZAP_NAME_LEN) {
1079                 dprintf("upgrading obj %llu: intsz=%u numint=%llu name=%s\n",
1080                     zapobj, integer_size, num_integers, name);
1081                 err = mzap_upgrade(&zn->zn_zap, tx, 0);
1082                 if (err == 0)
1083                         err = fzap_update(zn, integer_size, num_integers,
1084                             val, tx);
1085                 zap = zn->zn_zap;    /* fzap_update() may change zap */
1086         } else {
1087                 mze = mze_find(zn);
1088                 if (mze != NULL) {
1089                         ASSERT3U(MZE_PHYS(zap, mze)->mze_value, ==, oldval);
1090                         MZE_PHYS(zap, mze)->mze_value = *intval;
1091                 } else {
1092                         mzap_addent(zn, *intval);
1093                 }
1094         }
1095         ASSERT(zap == zn->zn_zap);
1096         zap_name_free(zn);
1097         if (zap != NULL)        /* may be NULL if fzap_upgrade() failed */
1098                 zap_unlockdir(zap);
1099         return (err);
1100 }
1101 
1102 int
1103 zap_update_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1104     int key_numints,
1105     int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
1106 {
1107         zap_t *zap;
1108         zap_name_t *zn;
1109         int err;
1110 
1111         err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, TRUE, &zap);
1112         if (err)
1113                 return (err);
1114         zn = zap_name_alloc_uint64(zap, key, key_numints);
1115         if (zn == NULL) {
1116                 zap_unlockdir(zap);
1117                 return (SET_ERROR(ENOTSUP));
1118         }
1119         err = fzap_update(zn, integer_size, num_integers, val, tx);
1120         zap = zn->zn_zap;    /* fzap_update() may change zap */
1121         zap_name_free(zn);
1122         if (zap != NULL)        /* may be NULL if fzap_upgrade() failed */
1123                 zap_unlockdir(zap);
1124         return (err);
1125 }
1126 
1127 int
1128 zap_remove(objset_t *os, uint64_t zapobj, const char *name, dmu_tx_t *tx)
1129 {
1130         return (zap_remove_norm(os, zapobj, name, MT_EXACT, tx));
1131 }
1132 
1133 int
1134 zap_remove_norm(objset_t *os, uint64_t zapobj, const char *name,
1135     matchtype_t mt, dmu_tx_t *tx)
1136 {
1137         zap_t *zap;
1138         int err;
1139         mzap_ent_t *mze;
1140         zap_name_t *zn;
1141 
1142         err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, &zap);
1143         if (err)
1144                 return (err);
1145         zn = zap_name_alloc(zap, name, mt);
1146         if (zn == NULL) {
1147                 zap_unlockdir(zap);
1148                 return (SET_ERROR(ENOTSUP));
1149         }
1150         if (!zap->zap_ismicro) {
1151                 err = fzap_remove(zn, tx);
1152         } else {
1153                 mze = mze_find(zn);
1154                 if (mze == NULL) {
1155                         err = SET_ERROR(ENOENT);
1156                 } else {
1157                         zap->zap_m.zap_num_entries--;
1158                         bzero(&zap_m_phys(zap)->mz_chunk[mze->mze_chunkid],
1159                             sizeof (mzap_ent_phys_t));
1160                         mze_remove(zap, mze);
1161                 }
1162         }
1163         zap_name_free(zn);
1164         zap_unlockdir(zap);
1165         return (err);
1166 }
1167 
1168 int
1169 zap_remove_uint64(objset_t *os, uint64_t zapobj, const uint64_t *key,
1170     int key_numints, dmu_tx_t *tx)
1171 {
1172         zap_t *zap;
1173         int err;
1174         zap_name_t *zn;
1175 
1176         err = zap_lockdir(os, zapobj, tx, RW_WRITER, TRUE, FALSE, &zap);
1177         if (err)
1178                 return (err);
1179         zn = zap_name_alloc_uint64(zap, key, key_numints);
1180         if (zn == NULL) {
1181                 zap_unlockdir(zap);
1182                 return (SET_ERROR(ENOTSUP));
1183         }
1184         err = fzap_remove(zn, tx);
1185         zap_name_free(zn);
1186         zap_unlockdir(zap);
1187         return (err);
1188 }
1189 
1190 /*
1191  * Routines for iterating over the attributes.
1192  */
1193 
1194 void
1195 zap_cursor_init_serialized(zap_cursor_t *zc, objset_t *os, uint64_t zapobj,
1196     uint64_t serialized)
1197 {
1198         zc->zc_objset = os;
1199         zc->zc_zap = NULL;
1200         zc->zc_leaf = NULL;
1201         zc->zc_zapobj = zapobj;
1202         zc->zc_serialized = serialized;
1203         zc->zc_hash = 0;
1204         zc->zc_cd = 0;
1205 }
1206 
1207 void
1208 zap_cursor_init(zap_cursor_t *zc, objset_t *os, uint64_t zapobj)
1209 {
1210         zap_cursor_init_serialized(zc, os, zapobj, 0);
1211 }
1212 
1213 void
1214 zap_cursor_fini(zap_cursor_t *zc)
1215 {
1216         if (zc->zc_zap) {
1217                 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
1218                 zap_unlockdir(zc->zc_zap);
1219                 zc->zc_zap = NULL;
1220         }
1221         if (zc->zc_leaf) {
1222                 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
1223                 zap_put_leaf(zc->zc_leaf);
1224                 zc->zc_leaf = NULL;
1225         }
1226         zc->zc_objset = NULL;
1227 }
1228 
1229 uint64_t
1230 zap_cursor_serialize(zap_cursor_t *zc)
1231 {
1232         if (zc->zc_hash == -1ULL)
1233                 return (-1ULL);
1234         if (zc->zc_zap == NULL)
1235                 return (zc->zc_serialized);
1236         ASSERT((zc->zc_hash & zap_maxcd(zc->zc_zap)) == 0);
1237         ASSERT(zc->zc_cd < zap_maxcd(zc->zc_zap));
1238 
1239         /*
1240          * We want to keep the high 32 bits of the cursor zero if we can, so
1241          * that 32-bit programs can access this.  So usually use a small
1242          * (28-bit) hash value so we can fit 4 bits of cd into the low 32-bits
1243          * of the cursor.
1244          *
1245          * [ collision differentiator | zap_hashbits()-bit hash value ]
1246          */
1247         return ((zc->zc_hash >> (64 - zap_hashbits(zc->zc_zap))) |
1248             ((uint64_t)zc->zc_cd << zap_hashbits(zc->zc_zap)));
1249 }
1250 
1251 int
1252 zap_cursor_retrieve(zap_cursor_t *zc, zap_attribute_t *za)
1253 {
1254         int err;
1255         avl_index_t idx;
1256         mzap_ent_t mze_tofind;
1257         mzap_ent_t *mze;
1258 
1259         if (zc->zc_hash == -1ULL)
1260                 return (SET_ERROR(ENOENT));
1261 
1262         if (zc->zc_zap == NULL) {
1263                 int hb;
1264                 err = zap_lockdir(zc->zc_objset, zc->zc_zapobj, NULL,
1265                     RW_READER, TRUE, FALSE, &zc->zc_zap);
1266                 if (err)
1267                         return (err);
1268 
1269                 /*
1270                  * To support zap_cursor_init_serialized, advance, retrieve,
1271                  * we must add to the existing zc_cd, which may already
1272                  * be 1 due to the zap_cursor_advance.
1273                  */
1274                 ASSERT(zc->zc_hash == 0);
1275                 hb = zap_hashbits(zc->zc_zap);
1276                 zc->zc_hash = zc->zc_serialized << (64 - hb);
1277                 zc->zc_cd += zc->zc_serialized >> hb;
1278                 if (zc->zc_cd >= zap_maxcd(zc->zc_zap)) /* corrupt serialized */
1279                         zc->zc_cd = 0;
1280         } else {
1281                 rw_enter(&zc->zc_zap->zap_rwlock, RW_READER);
1282         }
1283         if (!zc->zc_zap->zap_ismicro) {
1284                 err = fzap_cursor_retrieve(zc->zc_zap, zc, za);
1285         } else {
1286                 mze_tofind.mze_hash = zc->zc_hash;
1287                 mze_tofind.mze_cd = zc->zc_cd;
1288 
1289                 mze = avl_find(&zc->zc_zap->zap_m.zap_avl, &mze_tofind, &idx);
1290                 if (mze == NULL) {
1291                         mze = avl_nearest(&zc->zc_zap->zap_m.zap_avl,
1292                             idx, AVL_AFTER);
1293                 }
1294                 if (mze) {
1295                         mzap_ent_phys_t *mzep = MZE_PHYS(zc->zc_zap, mze);
1296                         ASSERT3U(mze->mze_cd, ==, mzep->mze_cd);
1297                         za->za_normalization_conflict =
1298                             mzap_normalization_conflict(zc->zc_zap, NULL, mze);
1299                         za->za_integer_length = 8;
1300                         za->za_num_integers = 1;
1301                         za->za_first_integer = mzep->mze_value;
1302                         (void) strcpy(za->za_name, mzep->mze_name);
1303                         zc->zc_hash = mze->mze_hash;
1304                         zc->zc_cd = mze->mze_cd;
1305                         err = 0;
1306                 } else {
1307                         zc->zc_hash = -1ULL;
1308                         err = SET_ERROR(ENOENT);
1309                 }
1310         }
1311         rw_exit(&zc->zc_zap->zap_rwlock);
1312         return (err);
1313 }
1314 
1315 void
1316 zap_cursor_advance(zap_cursor_t *zc)
1317 {
1318         if (zc->zc_hash == -1ULL)
1319                 return;
1320         zc->zc_cd++;
1321 }
1322 
1323 int
1324 zap_get_stats(objset_t *os, uint64_t zapobj, zap_stats_t *zs)
1325 {
1326         int err;
1327         zap_t *zap;
1328 
1329         err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
1330         if (err)
1331                 return (err);
1332 
1333         bzero(zs, sizeof (zap_stats_t));
1334 
1335         if (zap->zap_ismicro) {
1336                 zs->zs_blocksize = zap->zap_dbuf->db_size;
1337                 zs->zs_num_entries = zap->zap_m.zap_num_entries;
1338                 zs->zs_num_blocks = 1;
1339         } else {
1340                 fzap_get_stats(zap, zs);
1341         }
1342         zap_unlockdir(zap);
1343         return (0);
1344 }
1345 
1346 int
1347 zap_count_write(objset_t *os, uint64_t zapobj, const char *name, int add,
1348     uint64_t *towrite, uint64_t *tooverwrite)
1349 {
1350         zap_t *zap;
1351         int err = 0;
1352 
1353         /*
1354          * Since, we don't have a name, we cannot figure out which blocks will
1355          * be affected in this operation. So, account for the worst case :
1356          * - 3 blocks overwritten: target leaf, ptrtbl block, header block
1357          * - 4 new blocks written if adding:
1358          *      - 2 blocks for possibly split leaves,
1359          *      - 2 grown ptrtbl blocks
1360          *
1361          * This also accomodates the case where an add operation to a fairly
1362          * large microzap results in a promotion to fatzap.
1363          */
1364         if (name == NULL) {
1365                 *towrite += (3 + (add ? 4 : 0)) * SPA_OLD_MAXBLOCKSIZE;
1366                 return (err);
1367         }
1368 
1369         /*
1370          * We lock the zap with adding == FALSE. Because, if we pass
1371          * the actual value of add, it could trigger a mzap_upgrade().
1372          * At present we are just evaluating the possibility of this operation
1373          * and hence we donot want to trigger an upgrade.
1374          */
1375         err = zap_lockdir(os, zapobj, NULL, RW_READER, TRUE, FALSE, &zap);
1376         if (err)
1377                 return (err);
1378 
1379         if (!zap->zap_ismicro) {
1380                 zap_name_t *zn = zap_name_alloc(zap, name, MT_EXACT);
1381                 if (zn) {
1382                         err = fzap_count_write(zn, add, towrite,
1383                             tooverwrite);
1384                         zap_name_free(zn);
1385                 } else {
1386                         /*
1387                          * We treat this case as similar to (name == NULL)
1388                          */
1389                         *towrite += (3 + (add ? 4 : 0)) * SPA_OLD_MAXBLOCKSIZE;
1390                 }
1391         } else {
1392                 /*
1393                  * We are here if (name != NULL) and this is a micro-zap.
1394                  * We account for the header block depending on whether it
1395                  * is freeable.
1396                  *
1397                  * Incase of an add-operation it is hard to find out
1398                  * if this add will promote this microzap to fatzap.
1399                  * Hence, we consider the worst case and account for the
1400                  * blocks assuming this microzap would be promoted to a
1401                  * fatzap.
1402                  *
1403                  * 1 block overwritten  : header block
1404                  * 4 new blocks written : 2 new split leaf, 2 grown
1405                  *                      ptrtbl blocks
1406                  */
1407                 if (dmu_buf_freeable(zap->zap_dbuf))
1408                         *tooverwrite += MZAP_MAX_BLKSZ;
1409                 else
1410                         *towrite += MZAP_MAX_BLKSZ;
1411 
1412                 if (add) {
1413                         *towrite += 4 * MZAP_MAX_BLKSZ;
1414                 }
1415         }
1416 
1417         zap_unlockdir(zap);
1418         return (err);
1419 }