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