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