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