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