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