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