FreeBSD ZFS
The Zettabyte File System

zap.c

Go to the documentation of this file.
00001 /*
00002  * CDDL HEADER START
00003  *
00004  * The contents of this file are subject to the terms of the
00005  * Common Development and Distribution License (the "License").
00006  * You may not use this file except in compliance with the License.
00007  *
00008  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
00009  * or http://www.opensolaris.org/os/licensing.
00010  * See the License for the specific language governing permissions
00011  * and limitations under the License.
00012  *
00013  * When distributing Covered Code, include this CDDL HEADER in each
00014  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
00015  * If applicable, add the following below this CDDL HEADER, with the
00016  * fields enclosed by brackets "[]" replaced with your own identifying
00017  * information: Portions Copyright [yyyy] [name of copyright owner]
00018  *
00019  * CDDL HEADER END
00020  */
00021 /*
00022  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
00023  * Copyright (c) 2012 by Delphix. All rights reserved.
00024  */
00025 
00043 #include <sys/spa.h>
00044 #include <sys/dmu.h>
00045 #include <sys/zfs_context.h>
00046 #include <sys/zfs_znode.h>
00047 #include <sys/fs/zfs.h>
00048 #include <sys/zap.h>
00049 #include <sys/refcount.h>
00050 #include <sys/zap_impl.h>
00051 #include <sys/zap_leaf.h>
00052 
00053 int fzap_default_block_shift = 14; /* 16k blocksize */
00054 
00055 static void zap_leaf_pageout(dmu_buf_t *db, void *vl);
00056 static uint64_t zap_allocate_blocks(zap_t *zap, int nblocks);
00057 
00058 
00059 void
00060 fzap_byteswap(void *vbuf, size_t size)
00061 {
00062         uint64_t block_type;
00063 
00064         block_type = *(uint64_t *)vbuf;
00065 
00066         if (block_type == ZBT_LEAF || block_type == BSWAP_64(ZBT_LEAF))
00067                 zap_leaf_byteswap(vbuf, size);
00068         else {
00069                 /* it's a ptrtbl block */
00070                 byteswap_uint64_array(vbuf, size);
00071         }
00072 }
00073 
00074 void
00075 fzap_upgrade(zap_t *zap, dmu_tx_t *tx, zap_flags_t flags)
00076 {
00077         dmu_buf_t *db;
00078         zap_leaf_t *l;
00079         int i;
00080         zap_phys_t *zp;
00081 
00082         ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
00083         zap->zap_ismicro = FALSE;
00084 
00085         (void) dmu_buf_update_user(zap->zap_dbuf, zap, zap,
00086             &zap->zap_f.zap_phys, zap_evict);
00087 
00088         mutex_init(&zap->zap_f.zap_num_entries_mtx, 0, 0, 0);
00089         zap->zap_f.zap_block_shift = highbit(zap->zap_dbuf->db_size) - 1;
00090 
00091         zp = zap->zap_f.zap_phys;
00092         /*
00093          * explicitly zero it since it might be coming from an
00094          * initialized microzap
00095          */
00096         bzero(zap->zap_dbuf->db_data, zap->zap_dbuf->db_size);
00097         zp->zap_block_type = ZBT_HEADER;
00098         zp->zap_magic = ZAP_MAGIC;
00099 
00100         zp->zap_ptrtbl.zt_shift = ZAP_EMBEDDED_PTRTBL_SHIFT(zap);
00101 
00102         zp->zap_freeblk = 2;            /* block 1 will be the first leaf */
00103         zp->zap_num_leafs = 1;
00104         zp->zap_num_entries = 0;
00105         zp->zap_salt = zap->zap_salt;
00106         zp->zap_normflags = zap->zap_normflags;
00107         zp->zap_flags = flags;
00108 
00109         /* block 1 will be the first leaf */
00110         for (i = 0; i < (1<<zp->zap_ptrtbl.zt_shift); i++)
00111                 ZAP_EMBEDDED_PTRTBL_ENT(zap, i) = 1;
00112 
00113         /*
00114          * set up block 1 - the first leaf
00115          */
00116         VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object,
00117             1<<FZAP_BLOCK_SHIFT(zap), FTAG, &db, DMU_READ_NO_PREFETCH));
00118         dmu_buf_will_dirty(db, tx);
00119 
00120         l = kmem_zalloc(sizeof (zap_leaf_t), KM_SLEEP);
00121         l->l_dbuf = db;
00122         l->l_phys = db->db_data;
00123 
00124         zap_leaf_init(l, zp->zap_normflags != 0);
00125 
00126         kmem_free(l, sizeof (zap_leaf_t));
00127         dmu_buf_rele(db, FTAG);
00128 }
00129 
00130 static int
00131 zap_tryupgradedir(zap_t *zap, dmu_tx_t *tx)
00132 {
00133         if (RW_WRITE_HELD(&zap->zap_rwlock))
00134                 return (1);
00135         if (rw_tryupgrade(&zap->zap_rwlock)) {
00136                 dmu_buf_will_dirty(zap->zap_dbuf, tx);
00137                 return (1);
00138         }
00139         return (0);
00140 }
00141 
00146 static int
00147 zap_table_grow(zap_t *zap, zap_table_phys_t *tbl,
00148     void (*transfer_func)(const uint64_t *src, uint64_t *dst, int n),
00149     dmu_tx_t *tx)
00150 {
00151         uint64_t b, newblk;
00152         dmu_buf_t *db_old, *db_new;
00153         int err;
00154         int bs = FZAP_BLOCK_SHIFT(zap);
00155         int hepb = 1<<(bs-4);
00156         /* hepb = half the number of entries in a block */
00157 
00158         ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
00159         ASSERT(tbl->zt_blk != 0);
00160         ASSERT(tbl->zt_numblks > 0);
00161 
00162         if (tbl->zt_nextblk != 0) {
00163                 newblk = tbl->zt_nextblk;
00164         } else {
00165                 newblk = zap_allocate_blocks(zap, tbl->zt_numblks * 2);
00166                 tbl->zt_nextblk = newblk;
00167                 ASSERT0(tbl->zt_blks_copied);
00168                 dmu_prefetch(zap->zap_objset, zap->zap_object,
00169                     tbl->zt_blk << bs, tbl->zt_numblks << bs);
00170         }
00171 
00172         /*
00173          * Copy the ptrtbl from the old to new location.
00174          */
00175 
00176         b = tbl->zt_blks_copied;
00177         err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
00178             (tbl->zt_blk + b) << bs, FTAG, &db_old, DMU_READ_NO_PREFETCH);
00179         if (err)
00180                 return (err);
00181 
00182         /* first half of entries in old[b] go to new[2*b+0] */
00183         VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object,
00184             (newblk + 2*b+0) << bs, FTAG, &db_new, DMU_READ_NO_PREFETCH));
00185         dmu_buf_will_dirty(db_new, tx);
00186         transfer_func(db_old->db_data, db_new->db_data, hepb);
00187         dmu_buf_rele(db_new, FTAG);
00188 
00189         /* second half of entries in old[b] go to new[2*b+1] */
00190         VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object,
00191             (newblk + 2*b+1) << bs, FTAG, &db_new, DMU_READ_NO_PREFETCH));
00192         dmu_buf_will_dirty(db_new, tx);
00193         transfer_func((uint64_t *)db_old->db_data + hepb,
00194             db_new->db_data, hepb);
00195         dmu_buf_rele(db_new, FTAG);
00196 
00197         dmu_buf_rele(db_old, FTAG);
00198 
00199         tbl->zt_blks_copied++;
00200 
00201         dprintf("copied block %llu of %llu\n",
00202             tbl->zt_blks_copied, tbl->zt_numblks);
00203 
00204         if (tbl->zt_blks_copied == tbl->zt_numblks) {
00205                 (void) dmu_free_range(zap->zap_objset, zap->zap_object,
00206                     tbl->zt_blk << bs, tbl->zt_numblks << bs, tx);
00207 
00208                 tbl->zt_blk = newblk;
00209                 tbl->zt_numblks *= 2;
00210                 tbl->zt_shift++;
00211                 tbl->zt_nextblk = 0;
00212                 tbl->zt_blks_copied = 0;
00213 
00214                 dprintf("finished; numblocks now %llu (%lluk entries)\n",
00215                     tbl->zt_numblks, 1<<(tbl->zt_shift-10));
00216         }
00217 
00218         return (0);
00219 }
00220 
00221 static int
00222 zap_table_store(zap_t *zap, zap_table_phys_t *tbl, uint64_t idx, uint64_t val,
00223     dmu_tx_t *tx)
00224 {
00225         int err;
00226         uint64_t blk, off;
00227         int bs = FZAP_BLOCK_SHIFT(zap);
00228         dmu_buf_t *db;
00229 
00230         ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
00231         ASSERT(tbl->zt_blk != 0);
00232 
00233         dprintf("storing %llx at index %llx\n", val, idx);
00234 
00235         blk = idx >> (bs-3);
00236         off = idx & ((1<<(bs-3))-1);
00237 
00238         err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
00239             (tbl->zt_blk + blk) << bs, FTAG, &db, DMU_READ_NO_PREFETCH);
00240         if (err)
00241                 return (err);
00242         dmu_buf_will_dirty(db, tx);
00243 
00244         if (tbl->zt_nextblk != 0) {
00245                 uint64_t idx2 = idx * 2;
00246                 uint64_t blk2 = idx2 >> (bs-3);
00247                 uint64_t off2 = idx2 & ((1<<(bs-3))-1);
00248                 dmu_buf_t *db2;
00249 
00250                 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
00251                     (tbl->zt_nextblk + blk2) << bs, FTAG, &db2,
00252                     DMU_READ_NO_PREFETCH);
00253                 if (err) {
00254                         dmu_buf_rele(db, FTAG);
00255                         return (err);
00256                 }
00257                 dmu_buf_will_dirty(db2, tx);
00258                 ((uint64_t *)db2->db_data)[off2] = val;
00259                 ((uint64_t *)db2->db_data)[off2+1] = val;
00260                 dmu_buf_rele(db2, FTAG);
00261         }
00262 
00263         ((uint64_t *)db->db_data)[off] = val;
00264         dmu_buf_rele(db, FTAG);
00265 
00266         return (0);
00267 }
00268 
00269 static int
00270 zap_table_load(zap_t *zap, zap_table_phys_t *tbl, uint64_t idx, uint64_t *valp)
00271 {
00272         uint64_t blk, off;
00273         int err;
00274         dmu_buf_t *db;
00275         int bs = FZAP_BLOCK_SHIFT(zap);
00276 
00277         ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
00278 
00279         blk = idx >> (bs-3);
00280         off = idx & ((1<<(bs-3))-1);
00281 
00282         err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
00283             (tbl->zt_blk + blk) << bs, FTAG, &db, DMU_READ_NO_PREFETCH);
00284         if (err)
00285                 return (err);
00286         *valp = ((uint64_t *)db->db_data)[off];
00287         dmu_buf_rele(db, FTAG);
00288 
00289         if (tbl->zt_nextblk != 0) {
00290                 /*
00291                  * read the nextblk for the sake of i/o error checking,
00292                  * so that zap_table_load() will catch errors for
00293                  * zap_table_store.
00294                  */
00295                 blk = (idx*2) >> (bs-3);
00296 
00297                 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
00298                     (tbl->zt_nextblk + blk) << bs, FTAG, &db,
00299                     DMU_READ_NO_PREFETCH);
00300                 dmu_buf_rele(db, FTAG);
00301         }
00302         return (err);
00303 }
00304 
00309 static void
00310 zap_ptrtbl_transfer(const uint64_t *src, uint64_t *dst, int n)
00311 {
00312         int i;
00313         for (i = 0; i < n; i++) {
00314                 uint64_t lb = src[i];
00315                 dst[2*i+0] = lb;
00316                 dst[2*i+1] = lb;
00317         }
00318 }
00319 
00320 static int
00321 zap_grow_ptrtbl(zap_t *zap, dmu_tx_t *tx)
00322 {
00323         /*
00324          * The pointer table should never use more hash bits than we
00325          * have (otherwise we'd be using useless zero bits to index it).
00326          * If we are within 2 bits of running out, stop growing, since
00327          * this is already an aberrant condition.
00328          */
00329         if (zap->zap_f.zap_phys->zap_ptrtbl.zt_shift >= zap_hashbits(zap) - 2)
00330                 return (ENOSPC);
00331 
00332         if (zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks == 0) {
00333                 /*
00334                  * We are outgrowing the "embedded" ptrtbl (the one
00335                  * stored in the header block).  Give it its own entire
00336                  * block, which will double the size of the ptrtbl.
00337                  */
00338                 uint64_t newblk;
00339                 dmu_buf_t *db_new;
00340                 int err;
00341 
00342                 ASSERT3U(zap->zap_f.zap_phys->zap_ptrtbl.zt_shift, ==,
00343                     ZAP_EMBEDDED_PTRTBL_SHIFT(zap));
00344                 ASSERT0(zap->zap_f.zap_phys->zap_ptrtbl.zt_blk);
00345 
00346                 newblk = zap_allocate_blocks(zap, 1);
00347                 err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
00348                     newblk << FZAP_BLOCK_SHIFT(zap), FTAG, &db_new,
00349                     DMU_READ_NO_PREFETCH);
00350                 if (err)
00351                         return (err);
00352                 dmu_buf_will_dirty(db_new, tx);
00353                 zap_ptrtbl_transfer(&ZAP_EMBEDDED_PTRTBL_ENT(zap, 0),
00354                     db_new->db_data, 1 << ZAP_EMBEDDED_PTRTBL_SHIFT(zap));
00355                 dmu_buf_rele(db_new, FTAG);
00356 
00357                 zap->zap_f.zap_phys->zap_ptrtbl.zt_blk = newblk;
00358                 zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks = 1;
00359                 zap->zap_f.zap_phys->zap_ptrtbl.zt_shift++;
00360 
00361                 ASSERT3U(1ULL << zap->zap_f.zap_phys->zap_ptrtbl.zt_shift, ==,
00362                     zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks <<
00363                     (FZAP_BLOCK_SHIFT(zap)-3));
00364 
00365                 return (0);
00366         } else {
00367                 return (zap_table_grow(zap, &zap->zap_f.zap_phys->zap_ptrtbl,
00368                     zap_ptrtbl_transfer, tx));
00369         }
00370 }
00371 
00372 static void
00373 zap_increment_num_entries(zap_t *zap, int delta, dmu_tx_t *tx)
00374 {
00375         dmu_buf_will_dirty(zap->zap_dbuf, tx);
00376         mutex_enter(&zap->zap_f.zap_num_entries_mtx);
00377         ASSERT(delta > 0 || zap->zap_f.zap_phys->zap_num_entries >= -delta);
00378         zap->zap_f.zap_phys->zap_num_entries += delta;
00379         mutex_exit(&zap->zap_f.zap_num_entries_mtx);
00380 }
00381 
00382 static uint64_t
00383 zap_allocate_blocks(zap_t *zap, int nblocks)
00384 {
00385         uint64_t newblk;
00386         ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
00387         newblk = zap->zap_f.zap_phys->zap_freeblk;
00388         zap->zap_f.zap_phys->zap_freeblk += nblocks;
00389         return (newblk);
00390 }
00391 
00392 static zap_leaf_t *
00393 zap_create_leaf(zap_t *zap, dmu_tx_t *tx)
00394 {
00395         void *winner;
00396         zap_leaf_t *l = kmem_alloc(sizeof (zap_leaf_t), KM_SLEEP);
00397 
00398         ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
00399 
00400         rw_init(&l->l_rwlock, 0, 0, 0);
00401         rw_enter(&l->l_rwlock, RW_WRITER);
00402         l->l_blkid = zap_allocate_blocks(zap, 1);
00403         l->l_dbuf = NULL;
00404         l->l_phys = NULL;
00405 
00406         VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object,
00407             l->l_blkid << FZAP_BLOCK_SHIFT(zap), NULL, &l->l_dbuf,
00408             DMU_READ_NO_PREFETCH));
00409         winner = dmu_buf_set_user(l->l_dbuf, l, &l->l_phys, zap_leaf_pageout);
00410         ASSERT(winner == NULL);
00411         dmu_buf_will_dirty(l->l_dbuf, tx);
00412 
00413         zap_leaf_init(l, zap->zap_normflags != 0);
00414 
00415         zap->zap_f.zap_phys->zap_num_leafs++;
00416 
00417         return (l);
00418 }
00419 
00420 int
00421 fzap_count(zap_t *zap, uint64_t *count)
00422 {
00423         ASSERT(!zap->zap_ismicro);
00424         mutex_enter(&zap->zap_f.zap_num_entries_mtx); /* unnecessary */
00425         *count = zap->zap_f.zap_phys->zap_num_entries;
00426         mutex_exit(&zap->zap_f.zap_num_entries_mtx);
00427         return (0);
00428 }
00429 
00430 /*
00431  * Routines for obtaining zap_leaf_t's
00432  */
00433 
00434 void
00435 zap_put_leaf(zap_leaf_t *l)
00436 {
00437         rw_exit(&l->l_rwlock);
00438         dmu_buf_rele(l->l_dbuf, NULL);
00439 }
00440 
00441 _NOTE(ARGSUSED(0))
00442 static void
00443 zap_leaf_pageout(dmu_buf_t *db, void *vl)
00444 {
00445         zap_leaf_t *l = vl;
00446 
00447         rw_destroy(&l->l_rwlock);
00448         kmem_free(l, sizeof (zap_leaf_t));
00449 }
00450 
00451 static zap_leaf_t *
00452 zap_open_leaf(uint64_t blkid, dmu_buf_t *db)
00453 {
00454         zap_leaf_t *l, *winner;
00455 
00456         ASSERT(blkid != 0);
00457 
00458         l = kmem_alloc(sizeof (zap_leaf_t), KM_SLEEP);
00459         rw_init(&l->l_rwlock, 0, 0, 0);
00460         rw_enter(&l->l_rwlock, RW_WRITER);
00461         l->l_blkid = blkid;
00462         l->l_bs = highbit(db->db_size)-1;
00463         l->l_dbuf = db;
00464         l->l_phys = NULL;
00465 
00466         winner = dmu_buf_set_user(db, l, &l->l_phys, zap_leaf_pageout);
00467 
00468         rw_exit(&l->l_rwlock);
00469         if (winner != NULL) {
00470                 /* someone else set it first */
00471                 zap_leaf_pageout(NULL, l);
00472                 l = winner;
00473         }
00474 
00475         /*
00476          * lhr_pad was previously used for the next leaf in the leaf
00477          * chain.  There should be no chained leafs (as we have removed
00478          * support for them).
00479          */
00480         ASSERT0(l->l_phys->l_hdr.lh_pad1);
00481 
00482         /*
00483          * There should be more hash entries than there can be
00484          * chunks to put in the hash table
00485          */
00486         ASSERT3U(ZAP_LEAF_HASH_NUMENTRIES(l), >, ZAP_LEAF_NUMCHUNKS(l) / 3);
00487 
00488         /* The chunks should begin at the end of the hash table */
00489         ASSERT3P(&ZAP_LEAF_CHUNK(l, 0), ==,
00490             &l->l_phys->l_hash[ZAP_LEAF_HASH_NUMENTRIES(l)]);
00491 
00492         /* The chunks should end at the end of the block */
00493         ASSERT3U((uintptr_t)&ZAP_LEAF_CHUNK(l, ZAP_LEAF_NUMCHUNKS(l)) -
00494             (uintptr_t)l->l_phys, ==, l->l_dbuf->db_size);
00495 
00496         return (l);
00497 }
00498 
00499 static int
00500 zap_get_leaf_byblk(zap_t *zap, uint64_t blkid, dmu_tx_t *tx, krw_t lt,
00501     zap_leaf_t **lp)
00502 {
00503         dmu_buf_t *db;
00504         zap_leaf_t *l;
00505         int bs = FZAP_BLOCK_SHIFT(zap);
00506         int err;
00507 
00508         ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
00509 
00510         err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
00511             blkid << bs, NULL, &db, DMU_READ_NO_PREFETCH);
00512         if (err)
00513                 return (err);
00514 
00515         ASSERT3U(db->db_object, ==, zap->zap_object);
00516         ASSERT3U(db->db_offset, ==, blkid << bs);
00517         ASSERT3U(db->db_size, ==, 1 << bs);
00518         ASSERT(blkid != 0);
00519 
00520         l = dmu_buf_get_user(db);
00521 
00522         if (l == NULL)
00523                 l = zap_open_leaf(blkid, db);
00524 
00525         rw_enter(&l->l_rwlock, lt);
00526         /*
00527          * Must lock before dirtying, otherwise l->l_phys could change,
00528          * causing ASSERT below to fail.
00529          */
00530         if (lt == RW_WRITER)
00531                 dmu_buf_will_dirty(db, tx);
00532         ASSERT3U(l->l_blkid, ==, blkid);
00533         ASSERT3P(l->l_dbuf, ==, db);
00534         ASSERT3P(l->l_phys, ==, l->l_dbuf->db_data);
00535         ASSERT3U(l->l_phys->l_hdr.lh_block_type, ==, ZBT_LEAF);
00536         ASSERT3U(l->l_phys->l_hdr.lh_magic, ==, ZAP_LEAF_MAGIC);
00537 
00538         *lp = l;
00539         return (0);
00540 }
00541 
00542 static int
00543 zap_idx_to_blk(zap_t *zap, uint64_t idx, uint64_t *valp)
00544 {
00545         ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
00546 
00547         if (zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks == 0) {
00548                 ASSERT3U(idx, <,
00549                     (1ULL << zap->zap_f.zap_phys->zap_ptrtbl.zt_shift));
00550                 *valp = ZAP_EMBEDDED_PTRTBL_ENT(zap, idx);
00551                 return (0);
00552         } else {
00553                 return (zap_table_load(zap, &zap->zap_f.zap_phys->zap_ptrtbl,
00554                     idx, valp));
00555         }
00556 }
00557 
00558 static int
00559 zap_set_idx_to_blk(zap_t *zap, uint64_t idx, uint64_t blk, dmu_tx_t *tx)
00560 {
00561         ASSERT(tx != NULL);
00562         ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
00563 
00564         if (zap->zap_f.zap_phys->zap_ptrtbl.zt_blk == 0) {
00565                 ZAP_EMBEDDED_PTRTBL_ENT(zap, idx) = blk;
00566                 return (0);
00567         } else {
00568                 return (zap_table_store(zap, &zap->zap_f.zap_phys->zap_ptrtbl,
00569                     idx, blk, tx));
00570         }
00571 }
00572 
00573 static int
00574 zap_deref_leaf(zap_t *zap, uint64_t h, dmu_tx_t *tx, krw_t lt, zap_leaf_t **lp)
00575 {
00576         uint64_t idx, blk;
00577         int err;
00578 
00579         ASSERT(zap->zap_dbuf == NULL ||
00580             zap->zap_f.zap_phys == zap->zap_dbuf->db_data);
00581         ASSERT3U(zap->zap_f.zap_phys->zap_magic, ==, ZAP_MAGIC);
00582         idx = ZAP_HASH_IDX(h, zap->zap_f.zap_phys->zap_ptrtbl.zt_shift);
00583         err = zap_idx_to_blk(zap, idx, &blk);
00584         if (err != 0)
00585                 return (err);
00586         err = zap_get_leaf_byblk(zap, blk, tx, lt, lp);
00587 
00588         ASSERT(err || ZAP_HASH_IDX(h, (*lp)->l_phys->l_hdr.lh_prefix_len) ==
00589             (*lp)->l_phys->l_hdr.lh_prefix);
00590         return (err);
00591 }
00592 
00593 static int
00594 zap_expand_leaf(zap_name_t *zn, zap_leaf_t *l, dmu_tx_t *tx, zap_leaf_t **lp)
00595 {
00596         zap_t *zap = zn->zn_zap;
00597         uint64_t hash = zn->zn_hash;
00598         zap_leaf_t *nl;
00599         int prefix_diff, i, err;
00600         uint64_t sibling;
00601         int old_prefix_len = l->l_phys->l_hdr.lh_prefix_len;
00602 
00603         ASSERT3U(old_prefix_len, <=, zap->zap_f.zap_phys->zap_ptrtbl.zt_shift);
00604         ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
00605 
00606         ASSERT3U(ZAP_HASH_IDX(hash, old_prefix_len), ==,
00607             l->l_phys->l_hdr.lh_prefix);
00608 
00609         if (zap_tryupgradedir(zap, tx) == 0 ||
00610             old_prefix_len == zap->zap_f.zap_phys->zap_ptrtbl.zt_shift) {
00611                 /* We failed to upgrade, or need to grow the pointer table */
00612                 objset_t *os = zap->zap_objset;
00613                 uint64_t object = zap->zap_object;
00614 
00615                 zap_put_leaf(l);
00616                 zap_unlockdir(zap);
00617                 err = zap_lockdir(os, object, tx, RW_WRITER,
00618                     FALSE, FALSE, &zn->zn_zap);
00619                 zap = zn->zn_zap;
00620                 if (err)
00621                         return (err);
00622                 ASSERT(!zap->zap_ismicro);
00623 
00624                 while (old_prefix_len ==
00625                     zap->zap_f.zap_phys->zap_ptrtbl.zt_shift) {
00626                         err = zap_grow_ptrtbl(zap, tx);
00627                         if (err)
00628                                 return (err);
00629                 }
00630 
00631                 err = zap_deref_leaf(zap, hash, tx, RW_WRITER, &l);
00632                 if (err)
00633                         return (err);
00634 
00635                 if (l->l_phys->l_hdr.lh_prefix_len != old_prefix_len) {
00636                         /* it split while our locks were down */
00637                         *lp = l;
00638                         return (0);
00639                 }
00640         }
00641         ASSERT(RW_WRITE_HELD(&zap->zap_rwlock));
00642         ASSERT3U(old_prefix_len, <, zap->zap_f.zap_phys->zap_ptrtbl.zt_shift);
00643         ASSERT3U(ZAP_HASH_IDX(hash, old_prefix_len), ==,
00644             l->l_phys->l_hdr.lh_prefix);
00645 
00646         prefix_diff = zap->zap_f.zap_phys->zap_ptrtbl.zt_shift -
00647             (old_prefix_len + 1);
00648         sibling = (ZAP_HASH_IDX(hash, old_prefix_len + 1) | 1) << prefix_diff;
00649 
00650         /* check for i/o errors before doing zap_leaf_split */
00651         for (i = 0; i < (1ULL<<prefix_diff); i++) {
00652                 uint64_t blk;
00653                 err = zap_idx_to_blk(zap, sibling+i, &blk);
00654                 if (err)
00655                         return (err);
00656                 ASSERT3U(blk, ==, l->l_blkid);
00657         }
00658 
00659         nl = zap_create_leaf(zap, tx);
00660         zap_leaf_split(l, nl, zap->zap_normflags != 0);
00661 
00662         /* set sibling pointers */
00663         for (i = 0; i < (1ULL << prefix_diff); i++) {
00664                 err = zap_set_idx_to_blk(zap, sibling+i, nl->l_blkid, tx);
00665                 ASSERT0(err); /* we checked for i/o errors above */
00666         }
00667 
00668         if (hash & (1ULL << (64 - l->l_phys->l_hdr.lh_prefix_len))) {
00669                 /* we want the sibling */
00670                 zap_put_leaf(l);
00671                 *lp = nl;
00672         } else {
00673                 zap_put_leaf(nl);
00674                 *lp = l;
00675         }
00676 
00677         return (0);
00678 }
00679 
00680 static void
00681 zap_put_leaf_maybe_grow_ptrtbl(zap_name_t *zn, zap_leaf_t *l, dmu_tx_t *tx)
00682 {
00683         zap_t *zap = zn->zn_zap;
00684         int shift = zap->zap_f.zap_phys->zap_ptrtbl.zt_shift;
00685         int leaffull = (l->l_phys->l_hdr.lh_prefix_len == shift &&
00686             l->l_phys->l_hdr.lh_nfree < ZAP_LEAF_LOW_WATER);
00687 
00688         zap_put_leaf(l);
00689 
00690         if (leaffull || zap->zap_f.zap_phys->zap_ptrtbl.zt_nextblk) {
00691                 int err;
00692 
00693                 /*
00694                  * We are in the middle of growing the pointer table, or
00695                  * this leaf will soon make us grow it.
00696                  */
00697                 if (zap_tryupgradedir(zap, tx) == 0) {
00698                         objset_t *os = zap->zap_objset;
00699                         uint64_t zapobj = zap->zap_object;
00700 
00701                         zap_unlockdir(zap);
00702                         err = zap_lockdir(os, zapobj, tx,
00703                             RW_WRITER, FALSE, FALSE, &zn->zn_zap);
00704                         zap = zn->zn_zap;
00705                         if (err)
00706                                 return;
00707                 }
00708 
00709                 /* could have finished growing while our locks were down */
00710                 if (zap->zap_f.zap_phys->zap_ptrtbl.zt_shift == shift)
00711                         (void) zap_grow_ptrtbl(zap, tx);
00712         }
00713 }
00714 
00715 static int
00716 fzap_checkname(zap_name_t *zn)
00717 {
00718         if (zn->zn_key_orig_numints * zn->zn_key_intlen > ZAP_MAXNAMELEN)
00719                 return (ENAMETOOLONG);
00720         return (0);
00721 }
00722 
00723 static int
00724 fzap_checksize(uint64_t integer_size, uint64_t num_integers)
00725 {
00726         /* Only integer sizes supported by C */
00727         switch (integer_size) {
00728         case 1:
00729         case 2:
00730         case 4:
00731         case 8:
00732                 break;
00733         default:
00734                 return (EINVAL);
00735         }
00736 
00737         if (integer_size * num_integers > ZAP_MAXVALUELEN)
00738                 return (E2BIG);
00739 
00740         return (0);
00741 }
00742 
00743 static int
00744 fzap_check(zap_name_t *zn, uint64_t integer_size, uint64_t num_integers)
00745 {
00746         int err;
00747 
00748         if ((err = fzap_checkname(zn)) != 0)
00749                 return (err);
00750         return (fzap_checksize(integer_size, num_integers));
00751 }
00752 
00756 int
00757 fzap_lookup(zap_name_t *zn,
00758     uint64_t integer_size, uint64_t num_integers, void *buf,
00759     char *realname, int rn_len, boolean_t *ncp)
00760 {
00761         zap_leaf_t *l;
00762         int err;
00763         zap_entry_handle_t zeh;
00764 
00765         if ((err = fzap_checkname(zn)) != 0)
00766                 return (err);
00767 
00768         err = zap_deref_leaf(zn->zn_zap, zn->zn_hash, NULL, RW_READER, &l);
00769         if (err != 0)
00770                 return (err);
00771         err = zap_leaf_lookup(l, zn, &zeh);
00772         if (err == 0) {
00773                 if ((err = fzap_checksize(integer_size, num_integers)) != 0) {
00774                         zap_put_leaf(l);
00775                         return (err);
00776                 }
00777 
00778                 err = zap_entry_read(&zeh, integer_size, num_integers, buf);
00779                 (void) zap_entry_read_name(zn->zn_zap, &zeh, rn_len, realname);
00780                 if (ncp) {
00781                         *ncp = zap_entry_normalization_conflict(&zeh,
00782                             zn, NULL, zn->zn_zap);
00783                 }
00784         }
00785 
00786         zap_put_leaf(l);
00787         return (err);
00788 }
00789 
00790 int
00791 fzap_add_cd(zap_name_t *zn,
00792     uint64_t integer_size, uint64_t num_integers,
00793     const void *val, uint32_t cd, dmu_tx_t *tx)
00794 {
00795         zap_leaf_t *l;
00796         int err;
00797         zap_entry_handle_t zeh;
00798         zap_t *zap = zn->zn_zap;
00799 
00800         ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
00801         ASSERT(!zap->zap_ismicro);
00802         ASSERT(fzap_check(zn, integer_size, num_integers) == 0);
00803 
00804         err = zap_deref_leaf(zap, zn->zn_hash, tx, RW_WRITER, &l);
00805         if (err != 0)
00806                 return (err);
00807 retry:
00808         err = zap_leaf_lookup(l, zn, &zeh);
00809         if (err == 0) {
00810                 err = EEXIST;
00811                 goto out;
00812         }
00813         if (err != ENOENT)
00814                 goto out;
00815 
00816         err = zap_entry_create(l, zn, cd,
00817             integer_size, num_integers, val, &zeh);
00818 
00819         if (err == 0) {
00820                 zap_increment_num_entries(zap, 1, tx);
00821         } else if (err == EAGAIN) {
00822                 err = zap_expand_leaf(zn, l, tx, &l);
00823                 zap = zn->zn_zap;       /* zap_expand_leaf() may change zap */
00824                 if (err == 0)
00825                         goto retry;
00826         }
00827 
00828 out:
00829         if (zap != NULL)
00830                 zap_put_leaf_maybe_grow_ptrtbl(zn, l, tx);
00831         return (err);
00832 }
00833 
00834 int
00835 fzap_add(zap_name_t *zn,
00836     uint64_t integer_size, uint64_t num_integers,
00837     const void *val, dmu_tx_t *tx)
00838 {
00839         int err = fzap_check(zn, integer_size, num_integers);
00840         if (err != 0)
00841                 return (err);
00842 
00843         return (fzap_add_cd(zn, integer_size, num_integers,
00844             val, ZAP_NEED_CD, tx));
00845 }
00846 
00847 int
00848 fzap_update(zap_name_t *zn,
00849     int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx)
00850 {
00851         zap_leaf_t *l;
00852         int err, create;
00853         zap_entry_handle_t zeh;
00854         zap_t *zap = zn->zn_zap;
00855 
00856         ASSERT(RW_LOCK_HELD(&zap->zap_rwlock));
00857         err = fzap_check(zn, integer_size, num_integers);
00858         if (err != 0)
00859                 return (err);
00860 
00861         err = zap_deref_leaf(zap, zn->zn_hash, tx, RW_WRITER, &l);
00862         if (err != 0)
00863                 return (err);
00864 retry:
00865         err = zap_leaf_lookup(l, zn, &zeh);
00866         create = (err == ENOENT);
00867         ASSERT(err == 0 || err == ENOENT);
00868 
00869         if (create) {
00870                 err = zap_entry_create(l, zn, ZAP_NEED_CD,
00871                     integer_size, num_integers, val, &zeh);
00872                 if (err == 0)
00873                         zap_increment_num_entries(zap, 1, tx);
00874         } else {
00875                 err = zap_entry_update(&zeh, integer_size, num_integers, val);
00876         }
00877 
00878         if (err == EAGAIN) {
00879                 err = zap_expand_leaf(zn, l, tx, &l);
00880                 zap = zn->zn_zap;       /* zap_expand_leaf() may change zap */
00881                 if (err == 0)
00882                         goto retry;
00883         }
00884 
00885         if (zap != NULL)
00886                 zap_put_leaf_maybe_grow_ptrtbl(zn, l, tx);
00887         return (err);
00888 }
00889 
00890 int
00891 fzap_length(zap_name_t *zn,
00892     uint64_t *integer_size, uint64_t *num_integers)
00893 {
00894         zap_leaf_t *l;
00895         int err;
00896         zap_entry_handle_t zeh;
00897 
00898         err = zap_deref_leaf(zn->zn_zap, zn->zn_hash, NULL, RW_READER, &l);
00899         if (err != 0)
00900                 return (err);
00901         err = zap_leaf_lookup(l, zn, &zeh);
00902         if (err != 0)
00903                 goto out;
00904 
00905         if (integer_size)
00906                 *integer_size = zeh.zeh_integer_size;
00907         if (num_integers)
00908                 *num_integers = zeh.zeh_num_integers;
00909 out:
00910         zap_put_leaf(l);
00911         return (err);
00912 }
00913 
00914 int
00915 fzap_remove(zap_name_t *zn, dmu_tx_t *tx)
00916 {
00917         zap_leaf_t *l;
00918         int err;
00919         zap_entry_handle_t zeh;
00920 
00921         err = zap_deref_leaf(zn->zn_zap, zn->zn_hash, tx, RW_WRITER, &l);
00922         if (err != 0)
00923                 return (err);
00924         err = zap_leaf_lookup(l, zn, &zeh);
00925         if (err == 0) {
00926                 zap_entry_remove(&zeh);
00927                 zap_increment_num_entries(zn->zn_zap, -1, tx);
00928         }
00929         zap_put_leaf(l);
00930         return (err);
00931 }
00932 
00933 void
00934 fzap_prefetch(zap_name_t *zn)
00935 {
00936         uint64_t idx, blk;
00937         zap_t *zap = zn->zn_zap;
00938         int bs;
00939 
00940         idx = ZAP_HASH_IDX(zn->zn_hash,
00941             zap->zap_f.zap_phys->zap_ptrtbl.zt_shift);
00942         if (zap_idx_to_blk(zap, idx, &blk) != 0)
00943                 return;
00944         bs = FZAP_BLOCK_SHIFT(zap);
00945         dmu_prefetch(zap->zap_objset, zap->zap_object, blk << bs, 1 << bs);
00946 }
00947 
00948 /*
00949  * Helper functions for consumers.
00950  */
00951 
00952 uint64_t
00953 zap_create_link(objset_t *os, dmu_object_type_t ot, uint64_t parent_obj,
00954     const char *name, dmu_tx_t *tx)
00955 {
00956         uint64_t new_obj;
00957 
00958         VERIFY((new_obj = zap_create(os, ot, DMU_OT_NONE, 0, tx)) > 0);
00959         VERIFY(zap_add(os, parent_obj, name, sizeof (uint64_t), 1, &new_obj,
00960             tx) == 0);
00961 
00962         return (new_obj);
00963 }
00964 
00965 int
00966 zap_value_search(objset_t *os, uint64_t zapobj, uint64_t value, uint64_t mask,
00967     char *name)
00968 {
00969         zap_cursor_t zc;
00970         zap_attribute_t *za;
00971         int err;
00972 
00973         if (mask == 0)
00974                 mask = -1ULL;
00975 
00976         za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
00977         for (zap_cursor_init(&zc, os, zapobj);
00978             (err = zap_cursor_retrieve(&zc, za)) == 0;
00979             zap_cursor_advance(&zc)) {
00980                 if ((za->za_first_integer & mask) == (value & mask)) {
00981                         (void) strcpy(name, za->za_name);
00982                         break;
00983                 }
00984         }
00985         zap_cursor_fini(&zc);
00986         kmem_free(za, sizeof (zap_attribute_t));
00987         return (err);
00988 }
00989 
00990 int
00991 zap_join(objset_t *os, uint64_t fromobj, uint64_t intoobj, dmu_tx_t *tx)
00992 {
00993         zap_cursor_t zc;
00994         zap_attribute_t za;
00995         int err;
00996 
00997         for (zap_cursor_init(&zc, os, fromobj);
00998             zap_cursor_retrieve(&zc, &za) == 0;
00999             (void) zap_cursor_advance(&zc)) {
01000                 if (za.za_integer_length != 8 || za.za_num_integers != 1)
01001                         return (EINVAL);
01002                 err = zap_add(os, intoobj, za.za_name,
01003                     8, 1, &za.za_first_integer, tx);
01004                 if (err)
01005                         return (err);
01006         }
01007         zap_cursor_fini(&zc);
01008         return (0);
01009 }
01010 
01011 int
01012 zap_join_key(objset_t *os, uint64_t fromobj, uint64_t intoobj,
01013     uint64_t value, dmu_tx_t *tx)
01014 {
01015         zap_cursor_t zc;
01016         zap_attribute_t za;
01017         int err;
01018 
01019         for (zap_cursor_init(&zc, os, fromobj);
01020             zap_cursor_retrieve(&zc, &za) == 0;
01021             (void) zap_cursor_advance(&zc)) {
01022                 if (za.za_integer_length != 8 || za.za_num_integers != 1)
01023                         return (EINVAL);
01024                 err = zap_add(os, intoobj, za.za_name,
01025                     8, 1, &value, tx);
01026                 if (err)
01027                         return (err);
01028         }
01029         zap_cursor_fini(&zc);
01030         return (0);
01031 }
01032 
01033 int
01034 zap_join_increment(objset_t *os, uint64_t fromobj, uint64_t intoobj,
01035     dmu_tx_t *tx)
01036 {
01037         zap_cursor_t zc;
01038         zap_attribute_t za;
01039         int err;
01040 
01041         for (zap_cursor_init(&zc, os, fromobj);
01042             zap_cursor_retrieve(&zc, &za) == 0;
01043             (void) zap_cursor_advance(&zc)) {
01044                 uint64_t delta = 0;
01045 
01046                 if (za.za_integer_length != 8 || za.za_num_integers != 1)
01047                         return (EINVAL);
01048 
01049                 err = zap_lookup(os, intoobj, za.za_name, 8, 1, &delta);
01050                 if (err != 0 && err != ENOENT)
01051                         return (err);
01052                 delta += za.za_first_integer;
01053                 err = zap_update(os, intoobj, za.za_name, 8, 1, &delta, tx);
01054                 if (err)
01055                         return (err);
01056         }
01057         zap_cursor_fini(&zc);
01058         return (0);
01059 }
01060 
01061 int
01062 zap_add_int(objset_t *os, uint64_t obj, uint64_t value, dmu_tx_t *tx)
01063 {
01064         char name[20];
01065 
01066         (void) snprintf(name, sizeof (name), "%llx", (longlong_t)value);
01067         return (zap_add(os, obj, name, 8, 1, &value, tx));
01068 }
01069 
01070 int
01071 zap_remove_int(objset_t *os, uint64_t obj, uint64_t value, dmu_tx_t *tx)
01072 {
01073         char name[20];
01074 
01075         (void) snprintf(name, sizeof (name), "%llx", (longlong_t)value);
01076         return (zap_remove(os, obj, name, tx));
01077 }
01078 
01079 int
01080 zap_lookup_int(objset_t *os, uint64_t obj, uint64_t value)
01081 {
01082         char name[20];
01083 
01084         (void) snprintf(name, sizeof (name), "%llx", (longlong_t)value);
01085         return (zap_lookup(os, obj, name, 8, 1, &value));
01086 }
01087 
01088 int
01089 zap_add_int_key(objset_t *os, uint64_t obj,
01090     uint64_t key, uint64_t value, dmu_tx_t *tx)
01091 {
01092         char name[20];
01093 
01094         (void) snprintf(name, sizeof (name), "%llx", (longlong_t)key);
01095         return (zap_add(os, obj, name, 8, 1, &value, tx));
01096 }
01097 
01098 int
01099 zap_update_int_key(objset_t *os, uint64_t obj,
01100     uint64_t key, uint64_t value, dmu_tx_t *tx)
01101 {
01102         char name[20];
01103 
01104         (void) snprintf(name, sizeof (name), "%llx", (longlong_t)key);
01105         return (zap_update(os, obj, name, 8, 1, &value, tx));
01106 }
01107 
01108 int
01109 zap_lookup_int_key(objset_t *os, uint64_t obj, uint64_t key, uint64_t *valuep)
01110 {
01111         char name[20];
01112 
01113         (void) snprintf(name, sizeof (name), "%llx", (longlong_t)key);
01114         return (zap_lookup(os, obj, name, 8, 1, valuep));
01115 }
01116 
01117 int
01118 zap_increment(objset_t *os, uint64_t obj, const char *name, int64_t delta,
01119     dmu_tx_t *tx)
01120 {
01121         uint64_t value = 0;
01122         int err;
01123 
01124         if (delta == 0)
01125                 return (0);
01126 
01127         err = zap_lookup(os, obj, name, 8, 1, &value);
01128         if (err != 0 && err != ENOENT)
01129                 return (err);
01130         value += delta;
01131         if (value == 0)
01132                 err = zap_remove(os, obj, name, tx);
01133         else
01134                 err = zap_update(os, obj, name, 8, 1, &value, tx);
01135         return (err);
01136 }
01137 
01138 int
01139 zap_increment_int(objset_t *os, uint64_t obj, uint64_t key, int64_t delta,
01140     dmu_tx_t *tx)
01141 {
01142         char name[20];
01143 
01144         (void) snprintf(name, sizeof (name), "%llx", (longlong_t)key);
01145         return (zap_increment(os, obj, name, delta, tx));
01146 }
01147 
01148 /*
01149  * Routines for iterating over the attributes.
01150  */
01151 
01152 int
01153 fzap_cursor_retrieve(zap_t *zap, zap_cursor_t *zc, zap_attribute_t *za)
01154 {
01155         int err = ENOENT;
01156         zap_entry_handle_t zeh;
01157         zap_leaf_t *l;
01158 
01159         /* retrieve the next entry at or after zc_hash/zc_cd */
01160         /* if no entry, return ENOENT */
01161 
01162         if (zc->zc_leaf &&
01163             (ZAP_HASH_IDX(zc->zc_hash,
01164             zc->zc_leaf->l_phys->l_hdr.lh_prefix_len) !=
01165             zc->zc_leaf->l_phys->l_hdr.lh_prefix)) {
01166                 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
01167                 zap_put_leaf(zc->zc_leaf);
01168                 zc->zc_leaf = NULL;
01169         }
01170 
01171 again:
01172         if (zc->zc_leaf == NULL) {
01173                 err = zap_deref_leaf(zap, zc->zc_hash, NULL, RW_READER,
01174                     &zc->zc_leaf);
01175                 if (err != 0)
01176                         return (err);
01177         } else {
01178                 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER);
01179         }
01180         l = zc->zc_leaf;
01181 
01182         err = zap_leaf_lookup_closest(l, zc->zc_hash, zc->zc_cd, &zeh);
01183 
01184         if (err == ENOENT) {
01185                 uint64_t nocare =
01186                     (1ULL << (64 - l->l_phys->l_hdr.lh_prefix_len)) - 1;
01187                 zc->zc_hash = (zc->zc_hash & ~nocare) + nocare + 1;
01188                 zc->zc_cd = 0;
01189                 if (l->l_phys->l_hdr.lh_prefix_len == 0 || zc->zc_hash == 0) {
01190                         zc->zc_hash = -1ULL;
01191                 } else {
01192                         zap_put_leaf(zc->zc_leaf);
01193                         zc->zc_leaf = NULL;
01194                         goto again;
01195                 }
01196         }
01197 
01198         if (err == 0) {
01199                 zc->zc_hash = zeh.zeh_hash;
01200                 zc->zc_cd = zeh.zeh_cd;
01201                 za->za_integer_length = zeh.zeh_integer_size;
01202                 za->za_num_integers = zeh.zeh_num_integers;
01203                 if (zeh.zeh_num_integers == 0) {
01204                         za->za_first_integer = 0;
01205                 } else {
01206                         err = zap_entry_read(&zeh, 8, 1, &za->za_first_integer);
01207                         ASSERT(err == 0 || err == EOVERFLOW);
01208                 }
01209                 err = zap_entry_read_name(zap, &zeh,
01210                     sizeof (za->za_name), za->za_name);
01211                 ASSERT(err == 0);
01212 
01213                 za->za_normalization_conflict =
01214                     zap_entry_normalization_conflict(&zeh,
01215                     NULL, za->za_name, zap);
01216         }
01217         rw_exit(&zc->zc_leaf->l_rwlock);
01218         return (err);
01219 }
01220 
01221 static void
01222 zap_stats_ptrtbl(zap_t *zap, uint64_t *tbl, int len, zap_stats_t *zs)
01223 {
01224         int i, err;
01225         uint64_t lastblk = 0;
01226 
01227         /*
01228          * NB: if a leaf has more pointers than an entire ptrtbl block
01229          * can hold, then it'll be accounted for more than once, since
01230          * we won't have lastblk.
01231          */
01232         for (i = 0; i < len; i++) {
01233                 zap_leaf_t *l;
01234 
01235                 if (tbl[i] == lastblk)
01236                         continue;
01237                 lastblk = tbl[i];
01238 
01239                 err = zap_get_leaf_byblk(zap, tbl[i], NULL, RW_READER, &l);
01240                 if (err == 0) {
01241                         zap_leaf_stats(zap, l, zs);
01242                         zap_put_leaf(l);
01243                 }
01244         }
01245 }
01246 
01247 int
01248 fzap_cursor_move_to_key(zap_cursor_t *zc, zap_name_t *zn)
01249 {
01250         int err;
01251         zap_leaf_t *l;
01252         zap_entry_handle_t zeh;
01253 
01254         if (zn->zn_key_orig_numints * zn->zn_key_intlen > ZAP_MAXNAMELEN)
01255                 return (ENAMETOOLONG);
01256 
01257         err = zap_deref_leaf(zc->zc_zap, zn->zn_hash, NULL, RW_READER, &l);
01258         if (err != 0)
01259                 return (err);
01260 
01261         err = zap_leaf_lookup(l, zn, &zeh);
01262         if (err != 0)
01263                 return (err);
01264 
01265         zc->zc_leaf = l;
01266         zc->zc_hash = zeh.zeh_hash;
01267         zc->zc_cd = zeh.zeh_cd;
01268 
01269         return (err);
01270 }
01271 
01272 void
01273 fzap_get_stats(zap_t *zap, zap_stats_t *zs)
01274 {
01275         int bs = FZAP_BLOCK_SHIFT(zap);
01276         zs->zs_blocksize = 1ULL << bs;
01277 
01278         /*
01279          * Set zap_phys_t fields
01280          */
01281         zs->zs_num_leafs = zap->zap_f.zap_phys->zap_num_leafs;
01282         zs->zs_num_entries = zap->zap_f.zap_phys->zap_num_entries;
01283         zs->zs_num_blocks = zap->zap_f.zap_phys->zap_freeblk;
01284         zs->zs_block_type = zap->zap_f.zap_phys->zap_block_type;
01285         zs->zs_magic = zap->zap_f.zap_phys->zap_magic;
01286         zs->zs_salt = zap->zap_f.zap_phys->zap_salt;
01287 
01288         /*
01289          * Set zap_ptrtbl fields
01290          */
01291         zs->zs_ptrtbl_len = 1ULL << zap->zap_f.zap_phys->zap_ptrtbl.zt_shift;
01292         zs->zs_ptrtbl_nextblk = zap->zap_f.zap_phys->zap_ptrtbl.zt_nextblk;
01293         zs->zs_ptrtbl_blks_copied =
01294             zap->zap_f.zap_phys->zap_ptrtbl.zt_blks_copied;
01295         zs->zs_ptrtbl_zt_blk = zap->zap_f.zap_phys->zap_ptrtbl.zt_blk;
01296         zs->zs_ptrtbl_zt_numblks = zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks;
01297         zs->zs_ptrtbl_zt_shift = zap->zap_f.zap_phys->zap_ptrtbl.zt_shift;
01298 
01299         if (zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks == 0) {
01300                 /* the ptrtbl is entirely in the header block. */
01301                 zap_stats_ptrtbl(zap, &ZAP_EMBEDDED_PTRTBL_ENT(zap, 0),
01302                     1 << ZAP_EMBEDDED_PTRTBL_SHIFT(zap), zs);
01303         } else {
01304                 int b;
01305 
01306                 dmu_prefetch(zap->zap_objset, zap->zap_object,
01307                     zap->zap_f.zap_phys->zap_ptrtbl.zt_blk << bs,
01308                     zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks << bs);
01309 
01310                 for (b = 0; b < zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks;
01311                     b++) {
01312                         dmu_buf_t *db;
01313                         int err;
01314 
01315                         err = dmu_buf_hold(zap->zap_objset, zap->zap_object,
01316                             (zap->zap_f.zap_phys->zap_ptrtbl.zt_blk + b) << bs,
01317                             FTAG, &db, DMU_READ_NO_PREFETCH);
01318                         if (err == 0) {
01319                                 zap_stats_ptrtbl(zap, db->db_data,
01320                                     1<<(bs-3), zs);
01321                                 dmu_buf_rele(db, FTAG);
01322                         }
01323                 }
01324         }
01325 }
01326 
01327 int
01328 fzap_count_write(zap_name_t *zn, int add, uint64_t *towrite,
01329     uint64_t *tooverwrite)
01330 {
01331         zap_t *zap = zn->zn_zap;
01332         zap_leaf_t *l;
01333         int err;
01334 
01335         /*
01336          * Account for the header block of the fatzap.
01337          */
01338         if (!add && dmu_buf_freeable(zap->zap_dbuf)) {
01339                 *tooverwrite += zap->zap_dbuf->db_size;
01340         } else {
01341                 *towrite += zap->zap_dbuf->db_size;
01342         }
01343 
01344         /*
01345          * Account for the pointer table blocks.
01346          * If we are adding we need to account for the following cases :
01347          * - If the pointer table is embedded, this operation could force an
01348          *   external pointer table.
01349          * - If this already has an external pointer table this operation
01350          *   could extend the table.
01351          */
01352         if (add) {
01353                 if (zap->zap_f.zap_phys->zap_ptrtbl.zt_blk == 0)
01354                         *towrite += zap->zap_dbuf->db_size;
01355                 else
01356                         *towrite += (zap->zap_dbuf->db_size * 3);
01357         }
01358 
01359         /*
01360          * Now, check if the block containing leaf is freeable
01361          * and account accordingly.
01362          */
01363         err = zap_deref_leaf(zap, zn->zn_hash, NULL, RW_READER, &l);
01364         if (err != 0) {
01365                 return (err);
01366         }
01367 
01368         if (!add && dmu_buf_freeable(l->l_dbuf)) {
01369                 *tooverwrite += l->l_dbuf->db_size;
01370         } else {
01371                 /*
01372                  * If this an add operation, the leaf block could split.
01373                  * Hence, we need to account for an additional leaf block.
01374                  */
01375                 *towrite += (add ? 2 : 1) * l->l_dbuf->db_size;
01376         }
01377 
01378         zap_put_leaf(l);
01379         return (0);
01380 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines