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