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) 2013 by Delphix. All rights reserved.
  24  */
  25 
  26 /*
  27  * This file contains the top half of the zfs directory structure
  28  * implementation. The bottom half is in zap_leaf.c.
  29  *
  30  * The zdir is an extendable hash data structure. There is a table of
  31  * pointers to buckets (zap_t->zd_data->zd_leafs). The buckets are
  32  * each a constant size and hold a variable number of directory entries.
  33  * The buckets (aka "leaf nodes") are implemented in zap_leaf.c.
  34  *
  35  * The pointer table holds a power of 2 number of pointers.
  36  * (1<<zap_t->zd_data->zd_phys->zd_prefix_len).  The bucket pointed to
  37  * by the pointer at index i in the table holds entries whose hash value
  38  * has a zd_prefix_len - bit prefix
  39  */
  40 
  41 #include <sys/spa.h>
  42 #include <sys/dmu.h>
  43 #include <sys/zfs_context.h>
  44 #include <sys/zfs_znode.h>
  45 #include <sys/fs/zfs.h>
  46 #include <sys/zap.h>
  47 #include <sys/refcount.h>
  48 #include <sys/zap_impl.h>
  49 #include <sys/zap_leaf.h>
  50 
  51 int fzap_default_block_shift = 14; /* 16k blocksize */
  52 
  53 static uint64_t zap_allocate_blocks(zap_t *zap, int nblocks);
  54 
  55 void
  56 fzap_byteswap(void *vbuf, size_t size)
  57 {
  58         uint64_t block_type;
  59 
  60         block_type = *(uint64_t *)vbuf;
  61 
  62         if (block_type == ZBT_LEAF || block_type == BSWAP_64(ZBT_LEAF))
  63                 zap_leaf_byteswap(vbuf, size);
  64         else {
  65                 /* it's a ptrtbl block */
  66                 byteswap_uint64_array(vbuf, size);
  67         }
  68 }
  69 
  70 void
  71 fzap_upgrade(zap_t *zap, dmu_tx_t *tx, zap_flags_t flags)
  72 {
  73         dmu_buf_t *db;
  74         zap_leaf_t *l;
  75         int i;
  76         zap_phys_t *zp;
  77 
  78         ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
  79         zap->zap_ismicro = FALSE;
  80 
  81         zap->db_evict.evict_func = zap_evict;
  82 
  83         mutex_init(&zap->zap_f.zap_num_entries_mtx, 0, 0, 0);
  84         zap->zap_f.zap_block_shift = highbit(zap->zap_dbuf->db_size) - 1;
  85 
  86         zp = zap->zap_f_phys;
  87         /*
  88          * explicitly zero it since it might be coming from an
  89          * initialized microzap
  90          */
  91         bzero(zap->zap_dbuf->db_data, zap->zap_dbuf->db_size);
  92         zp->zap_block_type = ZBT_HEADER;
  93         zp->zap_magic = ZAP_MAGIC;
  94 
  95         zp->zap_ptrtbl.zt_shift = ZAP_EMBEDDED_PTRTBL_SHIFT(zap);
  96 
  97         zp->zap_freeblk = 2;         /* block 1 will be the first leaf */
  98         zp->zap_num_leafs = 1;
  99         zp->zap_num_entries = 0;
 100         zp->zap_salt = zap->zap_salt;
 101         zp->zap_normflags = zap->zap_normflags;
 102         zp->zap_flags = flags;
 103 
 104         /* block 1 will be the first leaf */
 105         for (i = 0; i < (1<<zp->zap_ptrtbl.zt_shift); i++)
 106                 ZAP_EMBEDDED_PTRTBL_ENT(zap, i) = 1;
 107 
 108         /*
 109          * set up block 1 - the first leaf
 110          */
 111         VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object,
 112             1<<FZAP_BLOCK_SHIFT(zap), FTAG, &db, DMU_READ_NO_PREFETCH));
 113         dmu_buf_will_dirty(db, tx);
 114 
 115         l = kmem_zalloc(sizeof (zap_leaf_t), KM_SLEEP);
 116         l->l_dbuf = db;
 117 
 118         zap_leaf_init(l, zp->zap_normflags != 0);
 119 
 120         kmem_free(l, sizeof (zap_leaf_t));
 121         dmu_buf_rele(db, FTAG);
 122 }
 123 
 124 static int
 125 zap_tryupgradedir(zap_t *zap, dmu_tx_t *tx)
 126 {
 127         if (RW_WRITE_HELD(&zap->zap_rwlock))
 128                 return (1);
 129         if (rw_tryupgrade(&zap->zap_rwlock)) {
 130                 dmu_buf_will_dirty(zap->zap_dbuf, tx);
 131                 return (1);
 132         }
 133         return (0);
 134 }
 135 
 136 /*
 137  * Generic routines for dealing with the pointer & cookie tables.
 138  */
 139 
 140 static int
 141 zap_table_grow(zap_t *zap, zap_table_phys_t *tbl,
 142     void (*transfer_func)(const uint64_t *src, uint64_t *dst, int n),
 143     dmu_tx_t *tx)
 144 {
 145         uint64_t b, newblk;
 146         dmu_buf_t *db_old, *db_new;
 147         int err;
 148         int bs = FZAP_BLOCK_SHIFT(zap);
 149         int hepb = 1<<(bs-4);
 150         /* hepb = half the number of entries in a block */
 151 
 152         ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
 153         ASSERT(tbl->zt_blk != 0);
 154         ASSERT(tbl->zt_numblks > 0);
 155 
 156         if (tbl->zt_nextblk != 0) {
 157                 newblk = tbl->zt_nextblk;
 158         } else {
 159                 newblk = zap_allocate_blocks(zap, tbl->zt_numblks * 2);
 160                 tbl->zt_nextblk = newblk;
 161                 ASSERT0(tbl->zt_blks_copied);
 162                 dmu_prefetch(zap->zap_objset, zap->zap_object,
 163                     tbl->zt_blk << bs, tbl->zt_numblks << bs);
 164         }
 165 
 166         /*
 167          * Copy the ptrtbl from the old to new location.
 168          */
 169 
 170         b = tbl->zt_blks_copied;
 171         err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
 172             (tbl->zt_blk + b) << bs, FTAG, &db_old, DMU_READ_NO_PREFETCH);
 173         if (err)
 174                 return (err);
 175 
 176         /* first half of entries in old[b] go to new[2*b+0] */
 177         VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object,
 178             (newblk + 2*b+0) << bs, FTAG, &db_new, DMU_READ_NO_PREFETCH));
 179         dmu_buf_will_dirty(db_new, tx);
 180         transfer_func(db_old->db_data, db_new->db_data, hepb);
 181         dmu_buf_rele(db_new, FTAG);
 182 
 183         /* second half of entries in old[b] go to new[2*b+1] */
 184         VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object,
 185             (newblk + 2*b+1) << bs, FTAG, &db_new, DMU_READ_NO_PREFETCH));
 186         dmu_buf_will_dirty(db_new, tx);
 187         transfer_func((uint64_t *)db_old->db_data + hepb,
 188             db_new->db_data, hepb);
 189         dmu_buf_rele(db_new, FTAG);
 190 
 191         dmu_buf_rele(db_old, FTAG);
 192 
 193         tbl->zt_blks_copied++;
 194 
 195         dprintf("copied block %llu of %llu\n",
 196             tbl->zt_blks_copied, tbl->zt_numblks);
 197 
 198         if (tbl->zt_blks_copied == tbl->zt_numblks) {
 199                 (void) dmu_free_range(zap->zap_objset, zap->zap_object,
 200                     tbl->zt_blk << bs, tbl->zt_numblks << bs, tx);
 201 
 202                 tbl->zt_blk = newblk;
 203                 tbl->zt_numblks *= 2;
 204                 tbl->zt_shift++;
 205                 tbl->zt_nextblk = 0;
 206                 tbl->zt_blks_copied = 0;
 207 
 208                 dprintf("finished; numblocks now %llu (%lluk entries)\n",
 209                     tbl->zt_numblks, 1<<(tbl->zt_shift-10));
 210         }
 211 
 212         return (0);
 213 }
 214 
 215 static int
 216 zap_table_store(zap_t *zap, zap_table_phys_t *tbl, uint64_t idx, uint64_t val,
 217     dmu_tx_t *tx)
 218 {
 219         int err;
 220         uint64_t blk, off;
 221         int bs = FZAP_BLOCK_SHIFT(zap);
 222         dmu_buf_t *db;
 223 
 224         ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
 225         ASSERT(tbl->zt_blk != 0);
 226 
 227         dprintf("storing %llx at index %llx\n", val, idx);
 228 
 229         blk = idx >> (bs-3);
 230         off = idx & ((1<<(bs-3))-1);
 231 
 232         err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
 233             (tbl->zt_blk + blk) << bs, FTAG, &db, DMU_READ_NO_PREFETCH);
 234         if (err)
 235                 return (err);
 236         dmu_buf_will_dirty(db, tx);
 237 
 238         if (tbl->zt_nextblk != 0) {
 239                 uint64_t idx2 = idx * 2;
 240                 uint64_t blk2 = idx2 >> (bs-3);
 241                 uint64_t off2 = idx2 & ((1<<(bs-3))-1);
 242                 dmu_buf_t *db2;
 243 
 244                 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
 245                     (tbl->zt_nextblk + blk2) << bs, FTAG, &db2,
 246                     DMU_READ_NO_PREFETCH);
 247                 if (err) {
 248                         dmu_buf_rele(db, FTAG);
 249                         return (err);
 250                 }
 251                 dmu_buf_will_dirty(db2, tx);
 252                 ((uint64_t *)db2->db_data)[off2] = val;
 253                 ((uint64_t *)db2->db_data)[off2+1] = val;
 254                 dmu_buf_rele(db2, FTAG);
 255         }
 256 
 257         ((uint64_t *)db->db_data)[off] = val;
 258         dmu_buf_rele(db, FTAG);
 259 
 260         return (0);
 261 }
 262 
 263 static int
 264 zap_table_load(zap_t *zap, zap_table_phys_t *tbl, uint64_t idx, uint64_t *valp)
 265 {
 266         uint64_t blk, off;
 267         int err;
 268         dmu_buf_t *db;
 269         int bs = FZAP_BLOCK_SHIFT(zap);
 270 
 271         ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
 272 
 273         blk = idx >> (bs-3);
 274         off = idx & ((1<<(bs-3))-1);
 275 
 276         err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
 277             (tbl->zt_blk + blk) << bs, FTAG, &db, DMU_READ_NO_PREFETCH);
 278         if (err)
 279                 return (err);
 280         *valp = ((uint64_t *)db->db_data)[off];
 281         dmu_buf_rele(db, FTAG);
 282 
 283         if (tbl->zt_nextblk != 0) {
 284                 /*
 285                  * read the nextblk for the sake of i/o error checking,
 286                  * so that zap_table_load() will catch errors for
 287                  * zap_table_store.
 288                  */
 289                 blk = (idx*2) >> (bs-3);
 290 
 291                 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
 292                     (tbl->zt_nextblk + blk) << bs, FTAG, &db,
 293                     DMU_READ_NO_PREFETCH);
 294                 dmu_buf_rele(db, FTAG);
 295         }
 296         return (err);
 297 }
 298 
 299 /*
 300  * Routines for growing the ptrtbl.
 301  */
 302 
 303 static void
 304 zap_ptrtbl_transfer(const uint64_t *src, uint64_t *dst, int n)
 305 {
 306         int i;
 307         for (i = 0; i < n; i++) {
 308                 uint64_t lb = src[i];
 309                 dst[2*i+0] = lb;
 310                 dst[2*i+1] = lb;
 311         }
 312 }
 313 
 314 static int
 315 zap_grow_ptrtbl(zap_t *zap, dmu_tx_t *tx)
 316 {
 317         /*
 318          * The pointer table should never use more hash bits than we
 319          * have (otherwise we'd be using useless zero bits to index it).
 320          * If we are within 2 bits of running out, stop growing, since
 321          * this is already an aberrant condition.
 322          */
 323         if (zap->zap_f_phys->zap_ptrtbl.zt_shift >= zap_hashbits(zap) - 2)
 324                 return (SET_ERROR(ENOSPC));
 325 
 326         if (zap->zap_f_phys->zap_ptrtbl.zt_numblks == 0) {
 327                 /*
 328                  * We are outgrowing the "embedded" ptrtbl (the one
 329                  * stored in the header block).  Give it its own entire
 330                  * block, which will double the size of the ptrtbl.
 331                  */
 332                 uint64_t newblk;
 333                 dmu_buf_t *db_new;
 334                 int err;
 335 
 336                 ASSERT3U(zap->zap_f_phys->zap_ptrtbl.zt_shift, ==,
 337                     ZAP_EMBEDDED_PTRTBL_SHIFT(zap));
 338                 ASSERT0(zap->zap_f_phys->zap_ptrtbl.zt_blk);
 339 
 340                 newblk = zap_allocate_blocks(zap, 1);
 341                 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
 342                     newblk << FZAP_BLOCK_SHIFT(zap), FTAG, &db_new,
 343                     DMU_READ_NO_PREFETCH);
 344                 if (err)
 345                         return (err);
 346                 dmu_buf_will_dirty(db_new, tx);
 347                 zap_ptrtbl_transfer(&ZAP_EMBEDDED_PTRTBL_ENT(zap, 0),
 348                     db_new->db_data, 1 << ZAP_EMBEDDED_PTRTBL_SHIFT(zap));
 349                 dmu_buf_rele(db_new, FTAG);
 350 
 351                 zap->zap_f_phys->zap_ptrtbl.zt_blk = newblk;
 352                 zap->zap_f_phys->zap_ptrtbl.zt_numblks = 1;
 353                 zap->zap_f_phys->zap_ptrtbl.zt_shift++;
 354 
 355                 ASSERT3U(1ULL << zap->zap_f_phys->zap_ptrtbl.zt_shift, ==,
 356                     zap->zap_f_phys->zap_ptrtbl.zt_numblks <<
 357                     (FZAP_BLOCK_SHIFT(zap)-3));
 358 
 359                 return (0);
 360         } else {
 361                 return (zap_table_grow(zap, &zap->zap_f_phys->zap_ptrtbl,
 362                     zap_ptrtbl_transfer, tx));
 363         }
 364 }
 365 
 366 static void
 367 zap_increment_num_entries(zap_t *zap, int delta, dmu_tx_t *tx)
 368 {
 369         dmu_buf_will_dirty(zap->zap_dbuf, tx);
 370         mutex_enter(&zap->zap_f.zap_num_entries_mtx);
 371         ASSERT(delta > 0 || zap->zap_f_phys->zap_num_entries >= -delta);
 372         zap->zap_f_phys->zap_num_entries += delta;
 373         mutex_exit(&zap->zap_f.zap_num_entries_mtx);
 374 }
 375 
 376 static uint64_t
 377 zap_allocate_blocks(zap_t *zap, int nblocks)
 378 {
 379         uint64_t newblk;
 380         ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
 381         newblk = zap->zap_f_phys->zap_freeblk;
 382         zap->zap_f_phys->zap_freeblk += nblocks;
 383         return (newblk);
 384 }
 385 
 386 static void
 387 zap_leaf_pageout(dmu_buf_user_t *dbu)
 388 {
 389         zap_leaf_t *l = (zap_leaf_t *)dbu;
 390 
 391         rw_destroy(&l->l_rwlock);
 392         kmem_free(l, sizeof (zap_leaf_t));
 393 }
 394 
 395 static zap_leaf_t *
 396 zap_create_leaf(zap_t *zap, dmu_tx_t *tx)
 397 {
 398         void *winner;
 399         zap_leaf_t *l = kmem_alloc(sizeof (zap_leaf_t), KM_SLEEP);
 400 
 401         ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
 402 
 403         rw_init(&l->l_rwlock, 0, 0, 0);
 404         rw_enter(&l->l_rwlock, RW_WRITER);
 405         l->l_blkid = zap_allocate_blocks(zap, 1);
 406         l->l_dbuf = NULL;
 407 
 408         VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object,
 409             l->l_blkid << FZAP_BLOCK_SHIFT(zap), NULL, &l->l_dbuf,
 410             DMU_READ_NO_PREFETCH));
 411         dmu_buf_init_user(&l->db_evict, zap_leaf_pageout);
 412         winner = (zap_leaf_t *)dmu_buf_set_user(l->l_dbuf, &l->db_evict);
 413         ASSERT(winner == NULL);
 414         dmu_buf_will_dirty(l->l_dbuf, tx);
 415 
 416         zap_leaf_init(l, zap->zap_normflags != 0);
 417 
 418         zap->zap_f_phys->zap_num_leafs++;
 419 
 420         return (l);
 421 }
 422 
 423 int
 424 fzap_count(zap_t *zap, uint64_t *count)
 425 {
 426         ASSERT(!zap->zap_ismicro);
 427         mutex_enter(&zap->zap_f.zap_num_entries_mtx); /* unnecessary */
 428         *count = zap->zap_f_phys->zap_num_entries;
 429         mutex_exit(&zap->zap_f.zap_num_entries_mtx);
 430         return (0);
 431 }
 432 
 433 /*
 434  * Routines for obtaining zap_leaf_t's
 435  */
 436 
 437 void
 438 zap_put_leaf(zap_leaf_t *l)
 439 {
 440         rw_exit(&l->l_rwlock);
 441         dmu_buf_rele(l->l_dbuf, NULL);
 442 }
 443 
 444 static zap_leaf_t *
 445 zap_open_leaf(uint64_t blkid, dmu_buf_t *db)
 446 {
 447         zap_leaf_t *l, *winner;
 448 
 449         ASSERT(blkid != 0);
 450 
 451         l = kmem_alloc(sizeof (zap_leaf_t), KM_SLEEP);
 452         rw_init(&l->l_rwlock, 0, 0, 0);
 453         rw_enter(&l->l_rwlock, RW_WRITER);
 454         l->l_blkid = blkid;
 455         l->l_bs = highbit(db->db_size)-1;
 456         l->l_dbuf = db;
 457 
 458         dmu_buf_init_user(&l->db_evict, zap_leaf_pageout);
 459         winner = (zap_leaf_t *)dmu_buf_set_user(db, &l->db_evict);
 460 
 461         rw_exit(&l->l_rwlock);
 462         if (winner != NULL) {
 463                 /* someone else set it first */
 464                 zap_leaf_pageout(&l->db_evict);
 465                 l = winner;
 466         }
 467 
 468         /*
 469          * lhr_pad was previously used for the next leaf in the leaf
 470          * chain.  There should be no chained leafs (as we have removed
 471          * support for them).
 472          */
 473         ASSERT0(l->l_phys->l_hdr.lh_pad1);
 474 
 475         /*
 476          * There should be more hash entries than there can be
 477          * chunks to put in the hash table
 478          */
 479         ASSERT3U(ZAP_LEAF_HASH_NUMENTRIES(l), >, ZAP_LEAF_NUMCHUNKS(l) / 3);
 480 
 481         /* The chunks should begin at the end of the hash table */
 482         ASSERT3P(&ZAP_LEAF_CHUNK(l, 0), ==,
 483             &l->l_phys->l_hash[ZAP_LEAF_HASH_NUMENTRIES(l)]);
 484 
 485         /* The chunks should end at the end of the block */
 486         ASSERT3U((uintptr_t)&ZAP_LEAF_CHUNK(l, ZAP_LEAF_NUMCHUNKS(l)) -
 487             (uintptr_t)l->l_phys, ==, l->l_dbuf->db_size);
 488 
 489         return (l);
 490 }
 491 
 492 static int
 493 zap_get_leaf_byblk(zap_t *zap, uint64_t blkid, dmu_tx_t *tx, krw_t lt,
 494     zap_leaf_t **lp)
 495 {
 496         dmu_buf_t *db;
 497         zap_leaf_t *l;
 498         int bs = FZAP_BLOCK_SHIFT(zap);
 499         int err;
 500 
 501         ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
 502 
 503         err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
 504             blkid << bs, NULL, &db, DMU_READ_NO_PREFETCH);
 505         if (err)
 506                 return (err);
 507 
 508         ASSERT3U(db->db_object, ==, zap->zap_object);
 509         ASSERT3U(db->db_offset, ==, blkid << bs);
 510         ASSERT3U(db->db_size, ==, 1 << bs);
 511         ASSERT(blkid != 0);
 512 
 513         l = (zap_leaf_t *)dmu_buf_get_user(db);
 514 
 515         if (l == NULL)
 516                 l = zap_open_leaf(blkid, db);
 517 
 518         rw_enter(&l->l_rwlock, lt);
 519         /*
 520          * Must lock before dirtying, otherwise l->l_phys could change,
 521          * causing ASSERT below to fail.
 522          */
 523         if (lt == RW_WRITER)
 524                 dmu_buf_will_dirty(db, tx);
 525         ASSERT3U(l->l_blkid, ==, blkid);
 526         ASSERT3P(l->l_dbuf, ==, db);
 527         ASSERT3P(l->l_phys, ==, l->l_dbuf->db_data);
 528         ASSERT3U(l->l_phys->l_hdr.lh_block_type, ==, ZBT_LEAF);
 529         ASSERT3U(l->l_phys->l_hdr.lh_magic, ==, ZAP_LEAF_MAGIC);
 530 
 531         *lp = l;
 532         return (0);
 533 }
 534 
 535 static int
 536 zap_idx_to_blk(zap_t *zap, uint64_t idx, uint64_t *valp)
 537 {
 538         ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
 539 
 540         if (zap->zap_f_phys->zap_ptrtbl.zt_numblks == 0) {
 541                 ASSERT3U(idx, <,
 542                     (1ULL << zap->zap_f_phys->zap_ptrtbl.zt_shift));
 543                 *valp = ZAP_EMBEDDED_PTRTBL_ENT(zap, idx);
 544                 return (0);
 545         } else {
 546                 return (zap_table_load(zap, &zap->zap_f_phys->zap_ptrtbl,
 547                     idx, valp));
 548         }
 549 }
 550 
 551 static int
 552 zap_set_idx_to_blk(zap_t *zap, uint64_t idx, uint64_t blk, dmu_tx_t *tx)
 553 {
 554         ASSERT(tx != NULL);
 555         ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
 556 
 557         if (zap->zap_f_phys->zap_ptrtbl.zt_blk == 0) {
 558                 ZAP_EMBEDDED_PTRTBL_ENT(zap, idx) = blk;
 559                 return (0);
 560         } else {
 561                 return (zap_table_store(zap, &zap->zap_f_phys->zap_ptrtbl,
 562                     idx, blk, tx));
 563         }
 564 }
 565 
 566 static int
 567 zap_deref_leaf(zap_t *zap, uint64_t h, dmu_tx_t *tx, krw_t lt, zap_leaf_t **lp)
 568 {
 569         uint64_t idx, blk;
 570         int err;
 571 
 572         ASSERT(zap->zap_dbuf == NULL ||
 573             zap->zap_f_phys == zap->zap_dbuf->db_data);
 574         ASSERT3U(zap->zap_f_phys->zap_magic, ==, ZAP_MAGIC);
 575         idx = ZAP_HASH_IDX(h, zap->zap_f_phys->zap_ptrtbl.zt_shift);
 576         err = zap_idx_to_blk(zap, idx, &blk);
 577         if (err != 0)
 578                 return (err);
 579         err = zap_get_leaf_byblk(zap, blk, tx, lt, lp);
 580 
 581         ASSERT(err || ZAP_HASH_IDX(h, (*lp)->l_phys->l_hdr.lh_prefix_len) ==
 582             (*lp)->l_phys->l_hdr.lh_prefix);
 583         return (err);
 584 }
 585 
 586 static int
 587 zap_expand_leaf(zap_name_t *zn, zap_leaf_t *l, dmu_tx_t *tx, zap_leaf_t **lp)
 588 {
 589         zap_t *zap = zn->zn_zap;
 590         uint64_t hash = zn->zn_hash;
 591         zap_leaf_t *nl;
 592         int prefix_diff, i, err;
 593         uint64_t sibling;
 594         int old_prefix_len = l->l_phys->l_hdr.lh_prefix_len;
 595 
 596         ASSERT3U(old_prefix_len, <=, zap->zap_f_phys->zap_ptrtbl.zt_shift);
 597         ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
 598 
 599         ASSERT3U(ZAP_HASH_IDX(hash, old_prefix_len), ==,
 600             l->l_phys->l_hdr.lh_prefix);
 601 
 602         if (zap_tryupgradedir(zap, tx) == 0 ||
 603             old_prefix_len == zap->zap_f_phys->zap_ptrtbl.zt_shift) {
 604                 /* We failed to upgrade, or need to grow the pointer table */
 605                 objset_t *os = zap->zap_objset;
 606                 uint64_t object = zap->zap_object;
 607 
 608                 zap_put_leaf(l);
 609                 zap_unlockdir(zap);
 610                 err = zap_lockdir(os, object, tx, RW_WRITER,
 611                     FALSE, FALSE, &zn->zn_zap);
 612                 zap = zn->zn_zap;
 613                 if (err)
 614                         return (err);
 615                 ASSERT(!zap->zap_ismicro);
 616 
 617                 while (old_prefix_len ==
 618                     zap->zap_f_phys->zap_ptrtbl.zt_shift) {
 619                         err = zap_grow_ptrtbl(zap, tx);
 620                         if (err)
 621                                 return (err);
 622                 }
 623 
 624                 err = zap_deref_leaf(zap, hash, tx, RW_WRITER, &l);
 625                 if (err)
 626                         return (err);
 627 
 628                 if (l->l_phys->l_hdr.lh_prefix_len != old_prefix_len) {
 629                         /* it split while our locks were down */
 630                         *lp = l;
 631                         return (0);
 632                 }
 633         }
 634         ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
 635         ASSERT3U(old_prefix_len, <, zap->zap_f_phys->zap_ptrtbl.zt_shift);
 636         ASSERT3U(ZAP_HASH_IDX(hash, old_prefix_len), ==,
 637             l->l_phys->l_hdr.lh_prefix);
 638 
 639         prefix_diff = zap->zap_f_phys->zap_ptrtbl.zt_shift -
 640             (old_prefix_len + 1);
 641         sibling = (ZAP_HASH_IDX(hash, old_prefix_len + 1) | 1) << prefix_diff;
 642 
 643         /* check for i/o errors before doing zap_leaf_split */
 644         for (i = 0; i < (1ULL<<prefix_diff); i++) {
 645                 uint64_t blk;
 646                 err = zap_idx_to_blk(zap, sibling+i, &blk);
 647                 if (err)
 648                         return (err);
 649                 ASSERT3U(blk, ==, l->l_blkid);
 650         }
 651 
 652         nl = zap_create_leaf(zap, tx);
 653         zap_leaf_split(l, nl, zap->zap_normflags != 0);
 654 
 655         /* set sibling pointers */
 656         for (i = 0; i < (1ULL << prefix_diff); i++) {
 657                 err = zap_set_idx_to_blk(zap, sibling+i, nl->l_blkid, tx);
 658                 ASSERT0(err); /* we checked for i/o errors above */
 659         }
 660 
 661         if (hash & (1ULL << (64 - l->l_phys->l_hdr.lh_prefix_len))) {
 662                 /* we want the sibling */
 663                 zap_put_leaf(l);
 664                 *lp = nl;
 665         } else {
 666                 zap_put_leaf(nl);
 667                 *lp = l;
 668         }
 669 
 670         return (0);
 671 }
 672 
 673 static void
 674 zap_put_leaf_maybe_grow_ptrtbl(zap_name_t *zn, zap_leaf_t *l, dmu_tx_t *tx)
 675 {
 676         zap_t *zap = zn->zn_zap;
 677         int shift = zap->zap_f_phys->zap_ptrtbl.zt_shift;
 678         int leaffull = (l->l_phys->l_hdr.lh_prefix_len == shift &&
 679             l->l_phys->l_hdr.lh_nfree < ZAP_LEAF_LOW_WATER);
 680 
 681         zap_put_leaf(l);
 682 
 683         if (leaffull || zap->zap_f_phys->zap_ptrtbl.zt_nextblk) {
 684                 int err;
 685 
 686                 /*
 687                  * We are in the middle of growing the pointer table, or
 688                  * this leaf will soon make us grow it.
 689                  */
 690                 if (zap_tryupgradedir(zap, tx) == 0) {
 691                         objset_t *os = zap->zap_objset;
 692                         uint64_t zapobj = zap->zap_object;
 693 
 694                         zap_unlockdir(zap);
 695                         err = zap_lockdir(os, zapobj, tx,
 696                             RW_WRITER, FALSE, FALSE, &zn->zn_zap);
 697                         zap = zn->zn_zap;
 698                         if (err)
 699                                 return;
 700                 }
 701 
 702                 /* could have finished growing while our locks were down */
 703                 if (zap->zap_f_phys->zap_ptrtbl.zt_shift == shift)
 704                         (void) zap_grow_ptrtbl(zap, tx);
 705         }
 706 }
 707 
 708 static int
 709 fzap_checkname(zap_name_t *zn)
 710 {
 711         if (zn->zn_key_orig_numints * zn->zn_key_intlen > ZAP_MAXNAMELEN)
 712                 return (SET_ERROR(ENAMETOOLONG));
 713         return (0);
 714 }
 715 
 716 static int
 717 fzap_checksize(uint64_t integer_size, uint64_t num_integers)
 718 {
 719         /* Only integer sizes supported by C */
 720         switch (integer_size) {
 721         case 1:
 722         case 2:
 723         case 4:
 724         case 8:
 725                 break;
 726         default:
 727                 return (SET_ERROR(EINVAL));
 728         }
 729 
 730         if (integer_size * num_integers > ZAP_MAXVALUELEN)
 731                 return (E2BIG);
 732 
 733         return (0);
 734 }
 735 
 736 static int
 737 fzap_check(zap_name_t *zn, uint64_t integer_size, uint64_t num_integers)
 738 {
 739         int err;
 740 
 741         if ((err = fzap_checkname(zn)) != 0)
 742                 return (err);
 743         return (fzap_checksize(integer_size, num_integers));
 744 }
 745 
 746 /*
 747  * Routines for manipulating attributes.
 748  */
 749 int
 750 fzap_lookup(zap_name_t *zn,
 751     uint64_t integer_size, uint64_t num_integers, void *buf,
 752     char *realname, int rn_len, boolean_t *ncp)
 753 {
 754         zap_leaf_t *l;
 755         int err;
 756         zap_entry_handle_t zeh;
 757 
 758         if ((err = fzap_checkname(zn)) != 0)
 759                 return (err);
 760 
 761         err = zap_deref_leaf(zn->zn_zap, zn->zn_hash, NULL, RW_READER, &l);
 762         if (err != 0)
 763                 return (err);
 764         err = zap_leaf_lookup(l, zn, &zeh);
 765         if (err == 0) {
 766                 if ((err = fzap_checksize(integer_size, num_integers)) != 0) {
 767                         zap_put_leaf(l);
 768                         return (err);
 769                 }
 770 
 771                 err = zap_entry_read(&zeh, integer_size, num_integers, buf);
 772                 (void) zap_entry_read_name(zn->zn_zap, &zeh, rn_len, realname);
 773                 if (ncp) {
 774                         *ncp = zap_entry_normalization_conflict(&zeh,
 775                             zn, NULL, zn->zn_zap);
 776                 }
 777         }
 778 
 779         zap_put_leaf(l);
 780         return (err);
 781 }
 782 
 783 int
 784 fzap_add_cd(zap_name_t *zn,
 785     uint64_t integer_size, uint64_t num_integers,
 786     const void *val, uint32_t cd, dmu_tx_t *tx)
 787 {
 788         zap_leaf_t *l;
 789         int err;
 790         zap_entry_handle_t zeh;
 791         zap_t *zap = zn->zn_zap;
 792 
 793         ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
 794         ASSERT(!zap->zap_ismicro);
 795         ASSERT(fzap_check(zn, integer_size, num_integers) == 0);
 796 
 797         err = zap_deref_leaf(zap, zn->zn_hash, tx, RW_WRITER, &l);
 798         if (err != 0)
 799                 return (err);
 800 retry:
 801         err = zap_leaf_lookup(l, zn, &zeh);
 802         if (err == 0) {
 803                 err = SET_ERROR(EEXIST);
 804                 goto out;
 805         }
 806         if (err != ENOENT)
 807                 goto out;
 808 
 809         err = zap_entry_create(l, zn, cd,
 810             integer_size, num_integers, val, &zeh);
 811 
 812         if (err == 0) {
 813                 zap_increment_num_entries(zap, 1, tx);
 814         } else if (err == EAGAIN) {
 815                 err = zap_expand_leaf(zn, l, tx, &l);
 816                 zap = zn->zn_zap;    /* zap_expand_leaf() may change zap */
 817                 if (err == 0)
 818                         goto retry;
 819         }
 820 
 821 out:
 822         if (zap != NULL)
 823                 zap_put_leaf_maybe_grow_ptrtbl(zn, l, tx);
 824         return (err);
 825 }
 826 
 827 int
 828 fzap_add(zap_name_t *zn,
 829     uint64_t integer_size, uint64_t num_integers,
 830     const void *val, dmu_tx_t *tx)
 831 {
 832         int err = fzap_check(zn, integer_size, num_integers);
 833         if (err != 0)
 834                 return (err);
 835 
 836         return (fzap_add_cd(zn, integer_size, num_integers,
 837             val, ZAP_NEED_CD, tx));
 838 }
 839 
 840 int
 841 fzap_update(zap_name_t *zn,
 842     int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
 843 {
 844         zap_leaf_t *l;
 845         int err, create;
 846         zap_entry_handle_t zeh;
 847         zap_t *zap = zn->zn_zap;
 848 
 849         ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
 850         err = fzap_check(zn, integer_size, num_integers);
 851         if (err != 0)
 852                 return (err);
 853 
 854         err = zap_deref_leaf(zap, zn->zn_hash, tx, RW_WRITER, &l);
 855         if (err != 0)
 856                 return (err);
 857 retry:
 858         err = zap_leaf_lookup(l, zn, &zeh);
 859         create = (err == ENOENT);
 860         ASSERT(err == 0 || err == ENOENT);
 861 
 862         if (create) {
 863                 err = zap_entry_create(l, zn, ZAP_NEED_CD,
 864                     integer_size, num_integers, val, &zeh);
 865                 if (err == 0)
 866                         zap_increment_num_entries(zap, 1, tx);
 867         } else {
 868                 err = zap_entry_update(&zeh, integer_size, num_integers, val);
 869         }
 870 
 871         if (err == EAGAIN) {
 872                 err = zap_expand_leaf(zn, l, tx, &l);
 873                 zap = zn->zn_zap;    /* zap_expand_leaf() may change zap */
 874                 if (err == 0)
 875                         goto retry;
 876         }
 877 
 878         if (zap != NULL)
 879                 zap_put_leaf_maybe_grow_ptrtbl(zn, l, tx);
 880         return (err);
 881 }
 882 
 883 int
 884 fzap_length(zap_name_t *zn,
 885     uint64_t *integer_size, uint64_t *num_integers)
 886 {
 887         zap_leaf_t *l;
 888         int err;
 889         zap_entry_handle_t zeh;
 890 
 891         err = zap_deref_leaf(zn->zn_zap, zn->zn_hash, NULL, RW_READER, &l);
 892         if (err != 0)
 893                 return (err);
 894         err = zap_leaf_lookup(l, zn, &zeh);
 895         if (err != 0)
 896                 goto out;
 897 
 898         if (integer_size)
 899                 *integer_size = zeh.zeh_integer_size;
 900         if (num_integers)
 901                 *num_integers = zeh.zeh_num_integers;
 902 out:
 903         zap_put_leaf(l);
 904         return (err);
 905 }
 906 
 907 int
 908 fzap_remove(zap_name_t *zn, dmu_tx_t *tx)
 909 {
 910         zap_leaf_t *l;
 911         int err;
 912         zap_entry_handle_t zeh;
 913 
 914         err = zap_deref_leaf(zn->zn_zap, zn->zn_hash, tx, RW_WRITER, &l);
 915         if (err != 0)
 916                 return (err);
 917         err = zap_leaf_lookup(l, zn, &zeh);
 918         if (err == 0) {
 919                 zap_entry_remove(&zeh);
 920                 zap_increment_num_entries(zn->zn_zap, -1, tx);
 921         }
 922         zap_put_leaf(l);
 923         return (err);
 924 }
 925 
 926 void
 927 fzap_prefetch(zap_name_t *zn)
 928 {
 929         uint64_t idx, blk;
 930         zap_t *zap = zn->zn_zap;
 931         int bs;
 932 
 933         idx = ZAP_HASH_IDX(zn->zn_hash,
 934             zap->zap_f_phys->zap_ptrtbl.zt_shift);
 935         if (zap_idx_to_blk(zap, idx, &blk) != 0)
 936                 return;
 937         bs = FZAP_BLOCK_SHIFT(zap);
 938         dmu_prefetch(zap->zap_objset, zap->zap_object, blk << bs, 1 << bs);
 939 }
 940 
 941 /*
 942  * Helper functions for consumers.
 943  */
 944 
 945 uint64_t
 946 zap_create_link(objset_t *os, dmu_object_type_t ot, uint64_t parent_obj,
 947     const char *name, dmu_tx_t *tx)
 948 {
 949         uint64_t new_obj;
 950 
 951         VERIFY((new_obj = zap_create(os, ot, DMU_OT_NONE, 0, tx)) > 0);
 952         VERIFY(zap_add(os, parent_obj, name, sizeof (uint64_t), 1, &new_obj,
 953             tx) == 0);
 954 
 955         return (new_obj);
 956 }
 957 
 958 int
 959 zap_value_search(objset_t *os, uint64_t zapobj, uint64_t value, uint64_t mask,
 960     char *name)
 961 {
 962         zap_cursor_t zc;
 963         zap_attribute_t *za;
 964         int err;
 965 
 966         if (mask == 0)
 967                 mask = -1ULL;
 968 
 969         za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
 970         for (zap_cursor_init(&zc, os, zapobj);
 971             (err = zap_cursor_retrieve(&zc, za)) == 0;
 972             zap_cursor_advance(&zc)) {
 973                 if ((za->za_first_integer & mask) == (value & mask)) {
 974                         (void) strcpy(name, za->za_name);
 975                         break;
 976                 }
 977         }
 978         zap_cursor_fini(&zc);
 979         kmem_free(za, sizeof (zap_attribute_t));
 980         return (err);
 981 }
 982 
 983 int
 984 zap_join(objset_t *os, uint64_t fromobj, uint64_t intoobj, dmu_tx_t *tx)
 985 {
 986         zap_cursor_t zc;
 987         zap_attribute_t za;
 988         int err;
 989 
 990         for (zap_cursor_init(&zc, os, fromobj);
 991             zap_cursor_retrieve(&zc, &za) == 0;
 992             (void) zap_cursor_advance(&zc)) {
 993                 if (za.za_integer_length != 8 || za.za_num_integers != 1)
 994                         return (SET_ERROR(EINVAL));
 995                 err = zap_add(os, intoobj, za.za_name,
 996                     8, 1, &za.za_first_integer, tx);
 997                 if (err)
 998                         return (err);
 999         }
1000         zap_cursor_fini(&zc);
1001         return (0);
1002 }
1003 
1004 int
1005 zap_join_key(objset_t *os, uint64_t fromobj, uint64_t intoobj,
1006     uint64_t value, dmu_tx_t *tx)
1007 {
1008         zap_cursor_t zc;
1009         zap_attribute_t za;
1010         int err;
1011 
1012         for (zap_cursor_init(&zc, os, fromobj);
1013             zap_cursor_retrieve(&zc, &za) == 0;
1014             (void) zap_cursor_advance(&zc)) {
1015                 if (za.za_integer_length != 8 || za.za_num_integers != 1)
1016                         return (SET_ERROR(EINVAL));
1017                 err = zap_add(os, intoobj, za.za_name,
1018                     8, 1, &value, tx);
1019                 if (err)
1020                         return (err);
1021         }
1022         zap_cursor_fini(&zc);
1023         return (0);
1024 }
1025 
1026 int
1027 zap_join_increment(objset_t *os, uint64_t fromobj, uint64_t intoobj,
1028     dmu_tx_t *tx)
1029 {
1030         zap_cursor_t zc;
1031         zap_attribute_t za;
1032         int err;
1033 
1034         for (zap_cursor_init(&zc, os, fromobj);
1035             zap_cursor_retrieve(&zc, &za) == 0;
1036             (void) zap_cursor_advance(&zc)) {
1037                 uint64_t delta = 0;
1038 
1039                 if (za.za_integer_length != 8 || za.za_num_integers != 1)
1040                         return (SET_ERROR(EINVAL));
1041 
1042                 err = zap_lookup(os, intoobj, za.za_name, 8, 1, &delta);
1043                 if (err != 0 && err != ENOENT)
1044                         return (err);
1045                 delta += za.za_first_integer;
1046                 err = zap_update(os, intoobj, za.za_name, 8, 1, &delta, tx);
1047                 if (err)
1048                         return (err);
1049         }
1050         zap_cursor_fini(&zc);
1051         return (0);
1052 }
1053 
1054 int
1055 zap_add_int(objset_t *os, uint64_t obj, uint64_t value, dmu_tx_t *tx)
1056 {
1057         char name[20];
1058 
1059         (void) snprintf(name, sizeof (name), "%llx", (longlong_t)value);
1060         return (zap_add(os, obj, name, 8, 1, &value, tx));
1061 }
1062 
1063 int
1064 zap_remove_int(objset_t *os, uint64_t obj, uint64_t value, dmu_tx_t *tx)
1065 {
1066         char name[20];
1067 
1068         (void) snprintf(name, sizeof (name), "%llx", (longlong_t)value);
1069         return (zap_remove(os, obj, name, tx));
1070 }
1071 
1072 int
1073 zap_lookup_int(objset_t *os, uint64_t obj, uint64_t value)
1074 {
1075         char name[20];
1076 
1077         (void) snprintf(name, sizeof (name), "%llx", (longlong_t)value);
1078         return (zap_lookup(os, obj, name, 8, 1, &value));
1079 }
1080 
1081 int
1082 zap_add_int_key(objset_t *os, uint64_t obj,
1083     uint64_t key, uint64_t value, dmu_tx_t *tx)
1084 {
1085         char name[20];
1086 
1087         (void) snprintf(name, sizeof (name), "%llx", (longlong_t)key);
1088         return (zap_add(os, obj, name, 8, 1, &value, tx));
1089 }
1090 
1091 int
1092 zap_update_int_key(objset_t *os, uint64_t obj,
1093     uint64_t key, uint64_t value, dmu_tx_t *tx)
1094 {
1095         char name[20];
1096 
1097         (void) snprintf(name, sizeof (name), "%llx", (longlong_t)key);
1098         return (zap_update(os, obj, name, 8, 1, &value, tx));
1099 }
1100 
1101 int
1102 zap_lookup_int_key(objset_t *os, uint64_t obj, uint64_t key, uint64_t *valuep)
1103 {
1104         char name[20];
1105 
1106         (void) snprintf(name, sizeof (name), "%llx", (longlong_t)key);
1107         return (zap_lookup(os, obj, name, 8, 1, valuep));
1108 }
1109 
1110 int
1111 zap_increment(objset_t *os, uint64_t obj, const char *name, int64_t delta,
1112     dmu_tx_t *tx)
1113 {
1114         uint64_t value = 0;
1115         int err;
1116 
1117         if (delta == 0)
1118                 return (0);
1119 
1120         err = zap_lookup(os, obj, name, 8, 1, &value);
1121         if (err != 0 && err != ENOENT)
1122                 return (err);
1123         value += delta;
1124         if (value == 0)
1125                 err = zap_remove(os, obj, name, tx);
1126         else
1127                 err = zap_update(os, obj, name, 8, 1, &value, tx);
1128         return (err);
1129 }
1130 
1131 int
1132 zap_increment_int(objset_t *os, uint64_t obj, uint64_t key, int64_t delta,
1133     dmu_tx_t *tx)
1134 {
1135         char name[20];
1136 
1137         (void) snprintf(name, sizeof (name), "%llx", (longlong_t)key);
1138         return (zap_increment(os, obj, name, delta, tx));
1139 }
1140 
1141 /*
1142  * Routines for iterating over the attributes.
1143  */
1144 
1145 int
1146 fzap_cursor_retrieve(zap_t *zap, zap_cursor_t *zc, zap_attribute_t *za)
1147 {
1148         int err = ENOENT;
1149         zap_entry_handle_t zeh;
1150         zap_leaf_t *l;
1151 
1152         /* retrieve the next entry at or after zc_hash/zc_cd */
1153         /* if no entry, return ENOENT */
1154 
1155         if (zc->zc_leaf &&
1156             (ZAP_HASH_IDX(zc->zc_hash,
1157             zc->zc_leaf->l_phys->l_hdr.lh_prefix_len) !=
1158             zc->zc_leaf->l_phys->l_hdr.lh_prefix)) {
1159                 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
1160                 zap_put_leaf(zc->zc_leaf);
1161                 zc->zc_leaf = NULL;
1162         }
1163 
1164 again:
1165         if (zc->zc_leaf == NULL) {
1166                 err = zap_deref_leaf(zap, zc->zc_hash, NULL, RW_READER,
1167                     &zc->zc_leaf);
1168                 if (err != 0)
1169                         return (err);
1170         } else {
1171                 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
1172         }
1173         l = zc->zc_leaf;
1174 
1175         err = zap_leaf_lookup_closest(l, zc->zc_hash, zc->zc_cd, &zeh);
1176 
1177         if (err == ENOENT) {
1178                 uint64_t nocare =
1179                     (1ULL << (64 - l->l_phys->l_hdr.lh_prefix_len)) - 1;
1180                 zc->zc_hash = (zc->zc_hash & ~nocare) + nocare + 1;
1181                 zc->zc_cd = 0;
1182                 if (l->l_phys->l_hdr.lh_prefix_len == 0 || zc->zc_hash == 0) {
1183                         zc->zc_hash = -1ULL;
1184                 } else {
1185                         zap_put_leaf(zc->zc_leaf);
1186                         zc->zc_leaf = NULL;
1187                         goto again;
1188                 }
1189         }
1190 
1191         if (err == 0) {
1192                 zc->zc_hash = zeh.zeh_hash;
1193                 zc->zc_cd = zeh.zeh_cd;
1194                 za->za_integer_length = zeh.zeh_integer_size;
1195                 za->za_num_integers = zeh.zeh_num_integers;
1196                 if (zeh.zeh_num_integers == 0) {
1197                         za->za_first_integer = 0;
1198                 } else {
1199                         err = zap_entry_read(&zeh, 8, 1, &za->za_first_integer);
1200                         ASSERT(err == 0 || err == EOVERFLOW);
1201                 }
1202                 err = zap_entry_read_name(zap, &zeh,
1203                     sizeof (za->za_name), za->za_name);
1204                 ASSERT(err == 0);
1205 
1206                 za->za_normalization_conflict =
1207                     zap_entry_normalization_conflict(&zeh,
1208                     NULL, za->za_name, zap);
1209         }
1210         rw_exit(&zc->zc_leaf->l_rwlock);
1211         return (err);
1212 }
1213 
1214 static void
1215 zap_stats_ptrtbl(zap_t *zap, uint64_t *tbl, int len, zap_stats_t *zs)
1216 {
1217         int i, err;
1218         uint64_t lastblk = 0;
1219 
1220         /*
1221          * NB: if a leaf has more pointers than an entire ptrtbl block
1222          * can hold, then it'll be accounted for more than once, since
1223          * we won't have lastblk.
1224          */
1225         for (i = 0; i < len; i++) {
1226                 zap_leaf_t *l;
1227 
1228                 if (tbl[i] == lastblk)
1229                         continue;
1230                 lastblk = tbl[i];
1231 
1232                 err = zap_get_leaf_byblk(zap, tbl[i], NULL, RW_READER, &l);
1233                 if (err == 0) {
1234                         zap_leaf_stats(zap, l, zs);
1235                         zap_put_leaf(l);
1236                 }
1237         }
1238 }
1239 
1240 int
1241 fzap_cursor_move_to_key(zap_cursor_t *zc, zap_name_t *zn)
1242 {
1243         int err;
1244         zap_leaf_t *l;
1245         zap_entry_handle_t zeh;
1246 
1247         if (zn->zn_key_orig_numints * zn->zn_key_intlen > ZAP_MAXNAMELEN)
1248                 return (SET_ERROR(ENAMETOOLONG));
1249 
1250         err = zap_deref_leaf(zc->zc_zap, zn->zn_hash, NULL, RW_READER, &l);
1251         if (err != 0)
1252                 return (err);
1253 
1254         err = zap_leaf_lookup(l, zn, &zeh);
1255         if (err != 0)
1256                 return (err);
1257 
1258         zc->zc_leaf = l;
1259         zc->zc_hash = zeh.zeh_hash;
1260         zc->zc_cd = zeh.zeh_cd;
1261 
1262         return (err);
1263 }
1264 
1265 void
1266 fzap_get_stats(zap_t *zap, zap_stats_t *zs)
1267 {
1268         int bs = FZAP_BLOCK_SHIFT(zap);
1269         zs->zs_blocksize = 1ULL << bs;
1270 
1271         /*
1272          * Set zap_phys_t fields
1273          */
1274         zs->zs_num_leafs = zap->zap_f_phys->zap_num_leafs;
1275         zs->zs_num_entries = zap->zap_f_phys->zap_num_entries;
1276         zs->zs_num_blocks = zap->zap_f_phys->zap_freeblk;
1277         zs->zs_block_type = zap->zap_f_phys->zap_block_type;
1278         zs->zs_magic = zap->zap_f_phys->zap_magic;
1279         zs->zs_salt = zap->zap_f_phys->zap_salt;
1280 
1281         /*
1282          * Set zap_ptrtbl fields
1283          */
1284         zs->zs_ptrtbl_len = 1ULL << zap->zap_f_phys->zap_ptrtbl.zt_shift;
1285         zs->zs_ptrtbl_nextblk = zap->zap_f_phys->zap_ptrtbl.zt_nextblk;
1286         zs->zs_ptrtbl_blks_copied =
1287             zap->zap_f_phys->zap_ptrtbl.zt_blks_copied;
1288         zs->zs_ptrtbl_zt_blk = zap->zap_f_phys->zap_ptrtbl.zt_blk;
1289         zs->zs_ptrtbl_zt_numblks = zap->zap_f_phys->zap_ptrtbl.zt_numblks;
1290         zs->zs_ptrtbl_zt_shift = zap->zap_f_phys->zap_ptrtbl.zt_shift;
1291 
1292         if (zap->zap_f_phys->zap_ptrtbl.zt_numblks == 0) {
1293                 /* the ptrtbl is entirely in the header block. */
1294                 zap_stats_ptrtbl(zap, &ZAP_EMBEDDED_PTRTBL_ENT(zap, 0),
1295                     1 << ZAP_EMBEDDED_PTRTBL_SHIFT(zap), zs);
1296         } else {
1297                 int b;
1298 
1299                 dmu_prefetch(zap->zap_objset, zap->zap_object,
1300                     zap->zap_f_phys->zap_ptrtbl.zt_blk << bs,
1301                     zap->zap_f_phys->zap_ptrtbl.zt_numblks << bs);
1302 
1303                 for (b = 0; b < zap->zap_f_phys->zap_ptrtbl.zt_numblks;
1304                     b++) {
1305                         dmu_buf_t *db;
1306                         int err;
1307 
1308                         err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
1309                             (zap->zap_f_phys->zap_ptrtbl.zt_blk + b) << bs,
1310                             FTAG, &db, DMU_READ_NO_PREFETCH);
1311                         if (err == 0) {
1312                                 zap_stats_ptrtbl(zap, db->db_data,
1313                                     1<<(bs-3), zs);
1314                                 dmu_buf_rele(db, FTAG);
1315                         }
1316                 }
1317         }
1318 }
1319 
1320 int
1321 fzap_count_write(zap_name_t *zn, int add, uint64_t *towrite,
1322     uint64_t *tooverwrite)
1323 {
1324         zap_t *zap = zn->zn_zap;
1325         zap_leaf_t *l;
1326         int err;
1327 
1328         /*
1329          * Account for the header block of the fatzap.
1330          */
1331         if (!add && dmu_buf_freeable(zap->zap_dbuf)) {
1332                 *tooverwrite += zap->zap_dbuf->db_size;
1333         } else {
1334                 *towrite += zap->zap_dbuf->db_size;
1335         }
1336 
1337         /*
1338          * Account for the pointer table blocks.
1339          * If we are adding we need to account for the following cases :
1340          * - If the pointer table is embedded, this operation could force an
1341          *   external pointer table.
1342          * - If this already has an external pointer table this operation
1343          *   could extend the table.
1344          */
1345         if (add) {
1346                 if (zap->zap_f_phys->zap_ptrtbl.zt_blk == 0)
1347                         *towrite += zap->zap_dbuf->db_size;
1348                 else
1349                         *towrite += (zap->zap_dbuf->db_size * 3);
1350         }
1351 
1352         /*
1353          * Now, check if the block containing leaf is freeable
1354          * and account accordingly.
1355          */
1356         err = zap_deref_leaf(zap, zn->zn_hash, NULL, RW_READER, &l);
1357         if (err != 0) {
1358                 return (err);
1359         }
1360 
1361         if (!add && dmu_buf_freeable(l->l_dbuf)) {
1362                 *tooverwrite += l->l_dbuf->db_size;
1363         } else {
1364                 /*
1365                  * If this an add operation, the leaf block could split.
1366                  * Hence, we need to account for an additional leaf block.
1367                  */
1368                 *towrite += (add ? 2 : 1) * l->l_dbuf->db_size;
1369         }
1370 
1371         zap_put_leaf(l);
1372         return (0);
1373 }