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