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 2008 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26#pragma ident "%Z%%M% %I% %E% SMI" 27 28 29/* 30 * This file contains the top half of the zfs directory structure 31 * implementation. The bottom half is in zap_leaf.c. 32 * 33 * The zdir is an extendable hash data structure. There is a table of 34 * pointers to buckets (zap_t->zd_data->zd_leafs). The buckets are 35 * each a constant size and hold a variable number of directory entries. 36 * The buckets (aka "leaf nodes") are implemented in zap_leaf.c. 37 * 38 * The pointer table holds a power of 2 number of pointers. 39 * (1<<zap_t->zd_data->zd_phys->zd_prefix_len). The bucket pointed to 40 * by the pointer at index i in the table holds entries whose hash value 41 * has a zd_prefix_len - bit prefix 42 */ 43 44#include <sys/spa.h> 45#include <sys/dmu.h> 46#include <sys/zfs_context.h> 47#include <sys/zfs_znode.h> 48#include <sys/zap.h> 49#include <sys/refcount.h> 50#include <sys/zap_impl.h> 51#include <sys/zap_leaf.h> 52#include <sys/zfs_znode.h> 53 54int fzap_default_block_shift = 14; /* 16k blocksize */ 55 56static void zap_leaf_pageout(dmu_buf_t *db, void *vl); 57static uint64_t zap_allocate_blocks(zap_t *zap, int nblocks); 58 59 60void 61fzap_byteswap(void *vbuf, size_t size) 62{ 63 uint64_t block_type; 64 65 block_type = *(uint64_t *)vbuf; 66 67 if (block_type == ZBT_LEAF || block_type == BSWAP_64(ZBT_LEAF)) 68 zap_leaf_byteswap(vbuf, size); 69 else { 70 /* it's a ptrtbl block */ 71 byteswap_uint64_array(vbuf, size); 72 } 73} 74 75void 76fzap_upgrade(zap_t *zap, dmu_tx_t *tx) 77{ 78 dmu_buf_t *db; 79 zap_leaf_t *l; 80 int i; 81 zap_phys_t *zp; 82 83 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock)); 84 zap->zap_ismicro = FALSE; 85 86 (void) dmu_buf_update_user(zap->zap_dbuf, zap, zap, 87 &zap->zap_f.zap_phys, zap_evict); 88 89 mutex_init(&zap->zap_f.zap_num_entries_mtx, NULL, MUTEX_DEFAULT, 0); 90 zap->zap_f.zap_block_shift = highbit(zap->zap_dbuf->db_size) - 1; 91 92 zp = zap->zap_f.zap_phys; 93 /* 94 * explicitly zero it since it might be coming from an 95 * initialized microzap 96 */ 97 bzero(zap->zap_dbuf->db_data, zap->zap_dbuf->db_size); 98 zp->zap_block_type = ZBT_HEADER; 99 zp->zap_magic = ZAP_MAGIC; 100 101 zp->zap_ptrtbl.zt_shift = ZAP_EMBEDDED_PTRTBL_SHIFT(zap); 102 103 zp->zap_freeblk = 2; /* block 1 will be the first leaf */ 104 zp->zap_num_leafs = 1; 105 zp->zap_num_entries = 0; 106 zp->zap_salt = zap->zap_salt; 107 zp->zap_normflags = zap->zap_normflags; 108 109 /* block 1 will be the first leaf */ 110 for (i = 0; i < (1<<zp->zap_ptrtbl.zt_shift); i++) 111 ZAP_EMBEDDED_PTRTBL_ENT(zap, i) = 1; 112 113 /* 114 * set up block 1 - the first leaf 115 */ 116 VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object, 117 1<<FZAP_BLOCK_SHIFT(zap), FTAG, &db)); 118 dmu_buf_will_dirty(db, tx); 119 120 l = kmem_zalloc(sizeof (zap_leaf_t), KM_SLEEP); 121 l->l_dbuf = db; 122 l->l_phys = db->db_data; 123 124 zap_leaf_init(l, zp->zap_normflags != 0); 125 126 kmem_free(l, sizeof (zap_leaf_t)); 127 dmu_buf_rele(db, FTAG); 128} 129 130static int 131zap_tryupgradedir(zap_t *zap, dmu_tx_t *tx) 132{ 133 if (RW_WRITE_HELD(&zap->zap_rwlock)) 134 return (1); 135 if (rw_tryupgrade(&zap->zap_rwlock)) { 136 dmu_buf_will_dirty(zap->zap_dbuf, tx); 137 return (1); 138 } 139 return (0); 140} 141 142/* 143 * Generic routines for dealing with the pointer & cookie tables. 144 */ 145 146static int 147zap_table_grow(zap_t *zap, zap_table_phys_t *tbl, 148 void (*transfer_func)(const uint64_t *src, uint64_t *dst, int n), 149 dmu_tx_t *tx) 150{ 151 uint64_t b, newblk; 152 dmu_buf_t *db_old, *db_new; 153 int err; 154 int bs = FZAP_BLOCK_SHIFT(zap); 155 int hepb = 1<<(bs-4); 156 /* hepb = half the number of entries in a block */ 157 158 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock)); 159 ASSERT(tbl->zt_blk != 0); 160 ASSERT(tbl->zt_numblks > 0); 161 162 if (tbl->zt_nextblk != 0) { 163 newblk = tbl->zt_nextblk; 164 } else { 165 newblk = zap_allocate_blocks(zap, tbl->zt_numblks * 2); 166 tbl->zt_nextblk = newblk; 167 ASSERT3U(tbl->zt_blks_copied, ==, 0); 168 dmu_prefetch(zap->zap_objset, zap->zap_object, 169 tbl->zt_blk << bs, tbl->zt_numblks << bs); 170 } 171 172 /* 173 * Copy the ptrtbl from the old to new location. 174 */ 175 176 b = tbl->zt_blks_copied; 177 err = dmu_buf_hold(zap->zap_objset, zap->zap_object, 178 (tbl->zt_blk + b) << bs, FTAG, &db_old); 179 if (err) 180 return (err); 181 182 /* first half of entries in old[b] go to new[2*b+0] */ 183 VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object, 184 (newblk + 2*b+0) << bs, FTAG, &db_new)); 185 dmu_buf_will_dirty(db_new, tx); 186 transfer_func(db_old->db_data, db_new->db_data, hepb); 187 dmu_buf_rele(db_new, FTAG); 188 189 /* second half of entries in old[b] go to new[2*b+1] */ 190 VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object, 191 (newblk + 2*b+1) << bs, FTAG, &db_new)); 192 dmu_buf_will_dirty(db_new, tx); 193 transfer_func((uint64_t *)db_old->db_data + hepb, 194 db_new->db_data, hepb); 195 dmu_buf_rele(db_new, FTAG); 196 197 dmu_buf_rele(db_old, FTAG); 198 199 tbl->zt_blks_copied++; 200 201 dprintf("copied block %llu of %llu\n", 202 tbl->zt_blks_copied, tbl->zt_numblks); 203 204 if (tbl->zt_blks_copied == tbl->zt_numblks) { 205 (void) dmu_free_range(zap->zap_objset, zap->zap_object, 206 tbl->zt_blk << bs, tbl->zt_numblks << bs, tx); 207 208 tbl->zt_blk = newblk; 209 tbl->zt_numblks *= 2; 210 tbl->zt_shift++; 211 tbl->zt_nextblk = 0; 212 tbl->zt_blks_copied = 0; 213 214 dprintf("finished; numblocks now %llu (%lluk entries)\n", 215 tbl->zt_numblks, 1<<(tbl->zt_shift-10)); 216 } 217 218 return (0); 219} 220 221static int 222zap_table_store(zap_t *zap, zap_table_phys_t *tbl, uint64_t idx, uint64_t val, 223 dmu_tx_t *tx) 224{ 225 int err; 226 uint64_t blk, off; 227 int bs = FZAP_BLOCK_SHIFT(zap); 228 dmu_buf_t *db; 229 230 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock)); 231 ASSERT(tbl->zt_blk != 0); 232 233 dprintf("storing %llx at index %llx\n", val, idx); 234 235 blk = idx >> (bs-3); 236 off = idx & ((1<<(bs-3))-1); 237 238 err = dmu_buf_hold(zap->zap_objset, zap->zap_object, 239 (tbl->zt_blk + blk) << bs, FTAG, &db); 240 if (err) 241 return (err); 242 dmu_buf_will_dirty(db, tx); 243 244 if (tbl->zt_nextblk != 0) { 245 uint64_t idx2 = idx * 2; 246 uint64_t blk2 = idx2 >> (bs-3); 247 uint64_t off2 = idx2 & ((1<<(bs-3))-1); 248 dmu_buf_t *db2; 249 250 err = dmu_buf_hold(zap->zap_objset, zap->zap_object, 251 (tbl->zt_nextblk + blk2) << bs, FTAG, &db2); 252 if (err) { 253 dmu_buf_rele(db, FTAG); 254 return (err); 255 } 256 dmu_buf_will_dirty(db2, tx); 257 ((uint64_t *)db2->db_data)[off2] = val; 258 ((uint64_t *)db2->db_data)[off2+1] = val; 259 dmu_buf_rele(db2, FTAG); 260 } 261 262 ((uint64_t *)db->db_data)[off] = val; 263 dmu_buf_rele(db, FTAG); 264 265 return (0); 266} 267 268static int 269zap_table_load(zap_t *zap, zap_table_phys_t *tbl, uint64_t idx, uint64_t *valp) 270{ 271 uint64_t blk, off; 272 int err; 273 dmu_buf_t *db; 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 err = dmu_buf_hold(zap->zap_objset, zap->zap_object, 282 (tbl->zt_blk + blk) << bs, FTAG, &db); 283 if (err) 284 return (err); 285 *valp = ((uint64_t *)db->db_data)[off]; 286 dmu_buf_rele(db, FTAG); 287 288 if (tbl->zt_nextblk != 0) { 289 /* 290 * read the nextblk for the sake of i/o error checking, 291 * so that zap_table_load() will catch errors for 292 * zap_table_store. 293 */ 294 blk = (idx*2) >> (bs-3); 295 296 err = dmu_buf_hold(zap->zap_objset, zap->zap_object, 297 (tbl->zt_nextblk + blk) << bs, FTAG, &db); 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 /* In case things go horribly wrong. */ 322 if (zap->zap_f.zap_phys->zap_ptrtbl.zt_shift >= ZAP_HASHBITS-2) 323 return (ENOSPC); 324 325 if (zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks == 0) { 326 /* 327 * We are outgrowing the "embedded" ptrtbl (the one 328 * stored in the header block). Give it its own entire 329 * block, which will double the size of the ptrtbl. 330 */ 331 uint64_t newblk; 332 dmu_buf_t *db_new; 333 int err; 334 335 ASSERT3U(zap->zap_f.zap_phys->zap_ptrtbl.zt_shift, ==, 336 ZAP_EMBEDDED_PTRTBL_SHIFT(zap)); 337 ASSERT3U(zap->zap_f.zap_phys->zap_ptrtbl.zt_blk, ==, 0); 338 339 newblk = zap_allocate_blocks(zap, 1); 340 err = dmu_buf_hold(zap->zap_objset, zap->zap_object, 341 newblk << FZAP_BLOCK_SHIFT(zap), FTAG, &db_new); 342 if (err) 343 return (err); 344 dmu_buf_will_dirty(db_new, tx); 345 zap_ptrtbl_transfer(&ZAP_EMBEDDED_PTRTBL_ENT(zap, 0), 346 db_new->db_data, 1 << ZAP_EMBEDDED_PTRTBL_SHIFT(zap)); 347 dmu_buf_rele(db_new, FTAG); 348 349 zap->zap_f.zap_phys->zap_ptrtbl.zt_blk = newblk; 350 zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks = 1; 351 zap->zap_f.zap_phys->zap_ptrtbl.zt_shift++; 352 353 ASSERT3U(1ULL << zap->zap_f.zap_phys->zap_ptrtbl.zt_shift, ==, 354 zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks << 355 (FZAP_BLOCK_SHIFT(zap)-3)); 356 357 return (0); 358 } else { 359 return (zap_table_grow(zap, &zap->zap_f.zap_phys->zap_ptrtbl, 360 zap_ptrtbl_transfer, tx)); 361 } 362} 363 364static void 365zap_increment_num_entries(zap_t *zap, int delta, dmu_tx_t *tx) 366{ 367 dmu_buf_will_dirty(zap->zap_dbuf, tx); 368 mutex_enter(&zap->zap_f.zap_num_entries_mtx); 369 ASSERT(delta > 0 || zap->zap_f.zap_phys->zap_num_entries >= -delta); 370 zap->zap_f.zap_phys->zap_num_entries += delta; 371 mutex_exit(&zap->zap_f.zap_num_entries_mtx); 372} 373 374static uint64_t 375zap_allocate_blocks(zap_t *zap, int nblocks) 376{ 377 uint64_t newblk; 378 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock)); 379 newblk = zap->zap_f.zap_phys->zap_freeblk; 380 zap->zap_f.zap_phys->zap_freeblk += nblocks; 381 return (newblk); 382} 383 384static zap_leaf_t * 385zap_create_leaf(zap_t *zap, dmu_tx_t *tx) 386{ 387 void *winner; 388 zap_leaf_t *l = kmem_alloc(sizeof (zap_leaf_t), KM_SLEEP); 389 390 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock)); 391 392 rw_init(&l->l_rwlock, NULL, RW_DEFAULT, 0); 393 rw_enter(&l->l_rwlock, RW_WRITER); 394 l->l_blkid = zap_allocate_blocks(zap, 1); 395 l->l_dbuf = NULL; 396 l->l_phys = NULL; 397 398 VERIFY(0 == dmu_buf_hold(zap->zap_objset, zap->zap_object, 399 l->l_blkid << FZAP_BLOCK_SHIFT(zap), NULL, &l->l_dbuf)); 400 winner = dmu_buf_set_user(l->l_dbuf, l, &l->l_phys, zap_leaf_pageout); 401 ASSERT(winner == NULL); 402 dmu_buf_will_dirty(l->l_dbuf, tx); 403 404 zap_leaf_init(l, zap->zap_normflags != 0); 405 406 zap->zap_f.zap_phys->zap_num_leafs++; 407 408 return (l); 409} 410 411int 412fzap_count(zap_t *zap, uint64_t *count) 413{ 414 ASSERT(!zap->zap_ismicro); 415 mutex_enter(&zap->zap_f.zap_num_entries_mtx); /* unnecessary */ 416 *count = zap->zap_f.zap_phys->zap_num_entries; 417 mutex_exit(&zap->zap_f.zap_num_entries_mtx); 418 return (0); 419} 420 421/* 422 * Routines for obtaining zap_leaf_t's 423 */ 424 425void 426zap_put_leaf(zap_leaf_t *l) 427{ 428 rw_exit(&l->l_rwlock); 429 dmu_buf_rele(l->l_dbuf, NULL); 430} 431 432_NOTE(ARGSUSED(0)) 433static void 434zap_leaf_pageout(dmu_buf_t *db, void *vl) 435{ 436 zap_leaf_t *l = vl; 437 438 rw_destroy(&l->l_rwlock); 439 kmem_free(l, sizeof (zap_leaf_t)); 440} 441 442static zap_leaf_t * 443zap_open_leaf(uint64_t blkid, dmu_buf_t *db) 444{ 445 zap_leaf_t *l, *winner; 446 447 ASSERT(blkid != 0); 448 449 l = kmem_alloc(sizeof (zap_leaf_t), KM_SLEEP); 450 rw_init(&l->l_rwlock, NULL, RW_DEFAULT, 0); 451 rw_enter(&l->l_rwlock, RW_WRITER); 452 l->l_blkid = blkid; 453 l->l_bs = highbit(db->db_size)-1; 454 l->l_dbuf = db; 455 l->l_phys = NULL; 456 457 winner = dmu_buf_set_user(db, l, &l->l_phys, zap_leaf_pageout); 458 459 rw_exit(&l->l_rwlock); 460 if (winner != NULL) { 461 /* someone else set it first */ 462 zap_leaf_pageout(NULL, l); 463 l = winner; 464 } 465 466 /* 467 * lhr_pad was previously used for the next leaf in the leaf 468 * chain. There should be no chained leafs (as we have removed 469 * support for them). 470 */ 471 ASSERT3U(l->l_phys->l_hdr.lh_pad1, ==, 0); 472 473 /* 474 * There should be more hash entries than there can be 475 * chunks to put in the hash table 476 */ 477 ASSERT3U(ZAP_LEAF_HASH_NUMENTRIES(l), >, ZAP_LEAF_NUMCHUNKS(l) / 3); 478 479 /* The chunks should begin at the end of the hash table */ 480 ASSERT3P(&ZAP_LEAF_CHUNK(l, 0), ==, 481 &l->l_phys->l_hash[ZAP_LEAF_HASH_NUMENTRIES(l)]); 482 483 /* The chunks should end at the end of the block */ 484 ASSERT3U((uintptr_t)&ZAP_LEAF_CHUNK(l, ZAP_LEAF_NUMCHUNKS(l)) - 485 (uintptr_t)l->l_phys, ==, l->l_dbuf->db_size); 486 487 return (l); 488} 489 490static int 491zap_get_leaf_byblk(zap_t *zap, uint64_t blkid, dmu_tx_t *tx, krw_t lt, 492 zap_leaf_t **lp) 493{ 494 dmu_buf_t *db; 495 zap_leaf_t *l; 496 int bs = FZAP_BLOCK_SHIFT(zap); 497 int err; 498 499 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock)); 500 501 err = dmu_buf_hold(zap->zap_objset, zap->zap_object, 502 blkid << bs, NULL, &db); 503 if (err) 504 return (err); 505 506 ASSERT3U(db->db_object, ==, zap->zap_object); 507 ASSERT3U(db->db_offset, ==, blkid << bs); 508 ASSERT3U(db->db_size, ==, 1 << bs); 509 ASSERT(blkid != 0); 510 511 l = dmu_buf_get_user(db); 512 513 if (l == NULL) 514 l = zap_open_leaf(blkid, db); 515 516 rw_enter(&l->l_rwlock, lt); 517 /* 518 * Must lock before dirtying, otherwise l->l_phys could change, 519 * causing ASSERT below to fail. 520 */ 521 if (lt == RW_WRITER) 522 dmu_buf_will_dirty(db, tx); 523 ASSERT3U(l->l_blkid, ==, blkid); 524 ASSERT3P(l->l_dbuf, ==, db); 525 ASSERT3P(l->l_phys, ==, l->l_dbuf->db_data); 526 ASSERT3U(l->l_phys->l_hdr.lh_block_type, ==, ZBT_LEAF); 527 ASSERT3U(l->l_phys->l_hdr.lh_magic, ==, ZAP_LEAF_MAGIC); 528 529 *lp = l; 530 return (0); 531} 532 533static int 534zap_idx_to_blk(zap_t *zap, uint64_t idx, uint64_t *valp) 535{ 536 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock)); 537 538 if (zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks == 0) { 539 ASSERT3U(idx, <, 540 (1ULL << zap->zap_f.zap_phys->zap_ptrtbl.zt_shift)); 541 *valp = ZAP_EMBEDDED_PTRTBL_ENT(zap, idx); 542 return (0); 543 } else { 544 return (zap_table_load(zap, &zap->zap_f.zap_phys->zap_ptrtbl, 545 idx, valp)); 546 } 547} 548 549static int 550zap_set_idx_to_blk(zap_t *zap, uint64_t idx, uint64_t blk, dmu_tx_t *tx) 551{ 552 ASSERT(tx != NULL); 553 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock)); 554 555 if (zap->zap_f.zap_phys->zap_ptrtbl.zt_blk == 0) { 556 ZAP_EMBEDDED_PTRTBL_ENT(zap, idx) = blk; 557 return (0); 558 } else { 559 return (zap_table_store(zap, &zap->zap_f.zap_phys->zap_ptrtbl, 560 idx, blk, tx)); 561 } 562} 563 564static int 565zap_deref_leaf(zap_t *zap, uint64_t h, dmu_tx_t *tx, krw_t lt, zap_leaf_t **lp) 566{ 567 uint64_t idx, blk; 568 int err; 569 570 ASSERT(zap->zap_dbuf == NULL || 571 zap->zap_f.zap_phys == zap->zap_dbuf->db_data); 572 ASSERT3U(zap->zap_f.zap_phys->zap_magic, ==, ZAP_MAGIC); 573 idx = ZAP_HASH_IDX(h, zap->zap_f.zap_phys->zap_ptrtbl.zt_shift); 574 err = zap_idx_to_blk(zap, idx, &blk); 575 if (err != 0) 576 return (err); 577 err = zap_get_leaf_byblk(zap, blk, tx, lt, lp); 578 579 ASSERT(err || ZAP_HASH_IDX(h, (*lp)->l_phys->l_hdr.lh_prefix_len) == 580 (*lp)->l_phys->l_hdr.lh_prefix); 581 return (err); 582} 583 584static int 585zap_expand_leaf(zap_name_t *zn, zap_leaf_t *l, dmu_tx_t *tx, zap_leaf_t **lp) 586{ 587 zap_t *zap = zn->zn_zap; 588 uint64_t hash = zn->zn_hash; 589 zap_leaf_t *nl; 590 int prefix_diff, i, err; 591 uint64_t sibling; 592 int old_prefix_len = l->l_phys->l_hdr.lh_prefix_len; 593 594 ASSERT3U(old_prefix_len, <=, zap->zap_f.zap_phys->zap_ptrtbl.zt_shift); 595 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock)); 596 597 ASSERT3U(ZAP_HASH_IDX(hash, old_prefix_len), ==, 598 l->l_phys->l_hdr.lh_prefix); 599 600 if (zap_tryupgradedir(zap, tx) == 0 || 601 old_prefix_len == zap->zap_f.zap_phys->zap_ptrtbl.zt_shift) { 602 /* We failed to upgrade, or need to grow the pointer table */ 603 objset_t *os = zap->zap_objset; 604 uint64_t object = zap->zap_object; 605 606 zap_put_leaf(l); 607 zap_unlockdir(zap); 608 err = zap_lockdir(os, object, tx, RW_WRITER, 609 FALSE, FALSE, &zn->zn_zap); 610 zap = zn->zn_zap; 611 if (err) 612 return (err); 613 ASSERT(!zap->zap_ismicro); 614 615 while (old_prefix_len == 616 zap->zap_f.zap_phys->zap_ptrtbl.zt_shift) { 617 err = zap_grow_ptrtbl(zap, tx); 618 if (err) 619 return (err); 620 } 621 622 err = zap_deref_leaf(zap, hash, tx, RW_WRITER, &l); 623 if (err) 624 return (err); 625 626 if (l->l_phys->l_hdr.lh_prefix_len != old_prefix_len) { 627 /* it split while our locks were down */ 628 *lp = l; 629 return (0); 630 } 631 } 632 ASSERT(RW_WRITE_HELD(&zap->zap_rwlock)); 633 ASSERT3U(old_prefix_len, <, zap->zap_f.zap_phys->zap_ptrtbl.zt_shift); 634 ASSERT3U(ZAP_HASH_IDX(hash, old_prefix_len), ==, 635 l->l_phys->l_hdr.lh_prefix); 636 637 prefix_diff = zap->zap_f.zap_phys->zap_ptrtbl.zt_shift - 638 (old_prefix_len + 1); 639 sibling = (ZAP_HASH_IDX(hash, old_prefix_len + 1) | 1) << prefix_diff; 640 641 /* check for i/o errors before doing zap_leaf_split */ 642 for (i = 0; i < (1ULL<<prefix_diff); i++) { 643 uint64_t blk; 644 err = zap_idx_to_blk(zap, sibling+i, &blk); 645 if (err) 646 return (err); 647 ASSERT3U(blk, ==, l->l_blkid); 648 } 649 650 nl = zap_create_leaf(zap, tx); 651 zap_leaf_split(l, nl, zap->zap_normflags != 0); 652 653 /* set sibling pointers */ 654 for (i = 0; i < (1ULL<<prefix_diff); i++) { 655 err = zap_set_idx_to_blk(zap, sibling+i, nl->l_blkid, tx); 656 ASSERT3U(err, ==, 0); /* we checked for i/o errors above */ 657 } 658 659 if (hash & (1ULL << (64 - l->l_phys->l_hdr.lh_prefix_len))) { 660 /* we want the sibling */ 661 zap_put_leaf(l); 662 *lp = nl; 663 } else { 664 zap_put_leaf(nl); 665 *lp = l; 666 } 667 668 return (0); 669} 670 671static void 672zap_put_leaf_maybe_grow_ptrtbl(zap_name_t *zn, zap_leaf_t *l, dmu_tx_t *tx) 673{ 674 zap_t *zap = zn->zn_zap; 675 int shift = zap->zap_f.zap_phys->zap_ptrtbl.zt_shift; 676 int leaffull = (l->l_phys->l_hdr.lh_prefix_len == shift && 677 l->l_phys->l_hdr.lh_nfree < ZAP_LEAF_LOW_WATER); 678 679 zap_put_leaf(l); 680 681 if (leaffull || zap->zap_f.zap_phys->zap_ptrtbl.zt_nextblk) { 682 int err; 683 684 /* 685 * We are in the middle of growing the pointer table, or 686 * this leaf will soon make us grow it. 687 */ 688 if (zap_tryupgradedir(zap, tx) == 0) { 689 objset_t *os = zap->zap_objset; 690 uint64_t zapobj = zap->zap_object; 691 692 zap_unlockdir(zap); 693 err = zap_lockdir(os, zapobj, tx, 694 RW_WRITER, FALSE, FALSE, &zn->zn_zap); 695 zap = zn->zn_zap; 696 if (err) 697 return; 698 } 699 700 /* could have finished growing while our locks were down */ 701 if (zap->zap_f.zap_phys->zap_ptrtbl.zt_shift == shift) 702 (void) zap_grow_ptrtbl(zap, tx); 703 } 704} 705 706 707static int 708fzap_checksize(const char *name, uint64_t integer_size, uint64_t num_integers) 709{ 710 if (name && strlen(name) > ZAP_MAXNAMELEN) 711 return (E2BIG); 712 713 /* Only integer sizes supported by C */ 714 switch (integer_size) { 715 case 1: 716 case 2: 717 case 4: 718 case 8: 719 break; 720 default: 721 return (EINVAL); 722 } 723 724 if (integer_size * num_integers > ZAP_MAXVALUELEN) 725 return (E2BIG); 726 727 return (0); 728} 729 730/* 731 * Routines for manipulating attributes. 732 */ 733int 734fzap_lookup(zap_name_t *zn, 735 uint64_t integer_size, uint64_t num_integers, void *buf, 736 char *realname, int rn_len, boolean_t *ncp) 737{ 738 zap_leaf_t *l; 739 int err; 740 zap_entry_handle_t zeh; 741 742 err = fzap_checksize(zn->zn_name_orij, integer_size, num_integers); 743 if (err != 0) 744 return (err); 745 746 err = zap_deref_leaf(zn->zn_zap, zn->zn_hash, NULL, RW_READER, &l); 747 if (err != 0) 748 return (err); 749 err = zap_leaf_lookup(l, zn, &zeh); 750 if (err == 0) { 751 err = zap_entry_read(&zeh, integer_size, num_integers, buf); 752 (void) zap_entry_read_name(&zeh, rn_len, realname); 753 if (ncp) { 754 *ncp = zap_entry_normalization_conflict(&zeh, 755 zn, NULL, zn->zn_zap); 756 } 757 } 758 759 zap_put_leaf(l); 760 return (err); 761} 762 763int 764fzap_add_cd(zap_name_t *zn, 765 uint64_t integer_size, uint64_t num_integers, 766 const void *val, uint32_t cd, dmu_tx_t *tx) 767{ 768 zap_leaf_t *l; 769 int err; 770 zap_entry_handle_t zeh; 771 zap_t *zap = zn->zn_zap; 772 773 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock)); 774 ASSERT(!zap->zap_ismicro); 775 ASSERT(fzap_checksize(zn->zn_name_orij, 776 integer_size, num_integers) == 0); 777 778 err = zap_deref_leaf(zap, zn->zn_hash, tx, RW_WRITER, &l); 779 if (err != 0) 780 return (err); 781retry: 782 err = zap_leaf_lookup(l, zn, &zeh); 783 if (err == 0) { 784 err = EEXIST; 785 goto out; 786 } 787 if (err != ENOENT) 788 goto out; 789 790 err = zap_entry_create(l, zn->zn_name_orij, zn->zn_hash, cd, 791 integer_size, num_integers, val, &zeh); 792 793 if (err == 0) { 794 zap_increment_num_entries(zap, 1, tx); 795 } else if (err == EAGAIN) { 796 err = zap_expand_leaf(zn, l, tx, &l); 797 zap = zn->zn_zap; /* zap_expand_leaf() may change zap */ 798 if (err == 0) 799 goto retry; 800 } 801 802out: 803 if (zap != NULL) 804 zap_put_leaf_maybe_grow_ptrtbl(zn, l, tx); 805 return (err); 806} 807 808int 809fzap_add(zap_name_t *zn, 810 uint64_t integer_size, uint64_t num_integers, 811 const void *val, dmu_tx_t *tx) 812{ 813 int err = fzap_checksize(zn->zn_name_orij, integer_size, num_integers); 814 if (err != 0) 815 return (err); 816 817 return (fzap_add_cd(zn, integer_size, num_integers, 818 val, ZAP_MAXCD, tx)); 819} 820 821int 822fzap_update(zap_name_t *zn, 823 int integer_size, uint64_t num_integers, const void *val, dmu_tx_t *tx) 824{ 825 zap_leaf_t *l; 826 int err, create; 827 zap_entry_handle_t zeh; 828 zap_t *zap = zn->zn_zap; 829 830 ASSERT(RW_LOCK_HELD(&zap->zap_rwlock)); 831 err = fzap_checksize(zn->zn_name_orij, integer_size, num_integers); 832 if (err != 0) 833 return (err); 834 835 err = zap_deref_leaf(zap, zn->zn_hash, tx, RW_WRITER, &l); 836 if (err != 0) 837 return (err); 838retry: 839 err = zap_leaf_lookup(l, zn, &zeh); 840 create = (err == ENOENT); 841 ASSERT(err == 0 || err == ENOENT); 842 843 if (create) { 844 err = zap_entry_create(l, zn->zn_name_orij, zn->zn_hash, 845 ZAP_MAXCD, integer_size, num_integers, val, &zeh); 846 if (err == 0) 847 zap_increment_num_entries(zap, 1, tx); 848 } else { 849 err = zap_entry_update(&zeh, integer_size, num_integers, val); 850 } 851 852 if (err == EAGAIN) { 853 err = zap_expand_leaf(zn, l, tx, &l); 854 zap = zn->zn_zap; /* zap_expand_leaf() may change zap */ 855 if (err == 0) 856 goto retry; 857 } 858 859 if (zap != NULL) 860 zap_put_leaf_maybe_grow_ptrtbl(zn, l, tx); 861 return (err); 862} 863 864int 865fzap_length(zap_name_t *zn, 866 uint64_t *integer_size, uint64_t *num_integers) 867{ 868 zap_leaf_t *l; 869 int err; 870 zap_entry_handle_t zeh; 871 872 err = zap_deref_leaf(zn->zn_zap, zn->zn_hash, NULL, RW_READER, &l); 873 if (err != 0) 874 return (err); 875 err = zap_leaf_lookup(l, zn, &zeh); 876 if (err != 0) 877 goto out; 878 879 if (integer_size) 880 *integer_size = zeh.zeh_integer_size; 881 if (num_integers) 882 *num_integers = zeh.zeh_num_integers; 883out: 884 zap_put_leaf(l); 885 return (err); 886} 887 888int 889fzap_remove(zap_name_t *zn, dmu_tx_t *tx) 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, tx, RW_WRITER, &l); 896 if (err != 0) 897 return (err); 898 err = zap_leaf_lookup(l, zn, &zeh); 899 if (err == 0) { 900 zap_entry_remove(&zeh); 901 zap_increment_num_entries(zn->zn_zap, -1, tx); 902 } 903 zap_put_leaf(l); 904 return (err); 905} 906 907/* 908 * Helper functions for consumers. 909 */ 910 911int 912zap_value_search(objset_t *os, uint64_t zapobj, uint64_t value, uint64_t mask, 913 char *name) 914{ 915 zap_cursor_t zc; 916 zap_attribute_t *za; 917 int err; 918 919 if (mask == 0) 920 mask = -1ULL; 921 922 za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); 923 for (zap_cursor_init(&zc, os, zapobj); 924 (err = zap_cursor_retrieve(&zc, za)) == 0; 925 zap_cursor_advance(&zc)) { 926 if ((za->za_first_integer & mask) == (value & mask)) { 927 (void) strcpy(name, za->za_name); 928 break; 929 } 930 } 931 zap_cursor_fini(&zc); 932 kmem_free(za, sizeof (zap_attribute_t)); 933 return (err); 934} 935 936int 937zap_join(objset_t *os, uint64_t fromobj, uint64_t intoobj, dmu_tx_t *tx) 938{ 939 zap_cursor_t zc; 940 zap_attribute_t za; 941 int err; 942 943 for (zap_cursor_init(&zc, os, fromobj); 944 zap_cursor_retrieve(&zc, &za) == 0; 945 (void) zap_cursor_advance(&zc)) { 946 if (za.za_integer_length != 8 || za.za_num_integers != 1) 947 return (EINVAL); 948 err = zap_add(os, intoobj, za.za_name, 949 8, 1, &za.za_first_integer, tx); 950 if (err) 951 return (err); 952 } 953 zap_cursor_fini(&zc); 954 return (0); 955} 956 957int 958zap_add_int(objset_t *os, uint64_t obj, uint64_t value, dmu_tx_t *tx) 959{ 960 char name[20]; 961 962 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)value); 963 return (zap_add(os, obj, name, 8, 1, &value, tx)); 964} 965 966int 967zap_remove_int(objset_t *os, uint64_t obj, uint64_t value, dmu_tx_t *tx) 968{ 969 char name[20]; 970 971 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)value); 972 return (zap_remove(os, obj, name, tx)); 973} 974 975int 976zap_lookup_int(objset_t *os, uint64_t obj, uint64_t value) 977{ 978 char name[20]; 979 980 (void) snprintf(name, sizeof (name), "%llx", (longlong_t)value); 981 return (zap_lookup(os, obj, name, 8, 1, &value)); 982} 983 984/* 985 * Routines for iterating over the attributes. 986 */ 987 988int 989fzap_cursor_retrieve(zap_t *zap, zap_cursor_t *zc, zap_attribute_t *za) 990{ 991 int err = ENOENT; 992 zap_entry_handle_t zeh; 993 zap_leaf_t *l; 994 995 /* retrieve the next entry at or after zc_hash/zc_cd */ 996 /* if no entry, return ENOENT */ 997 998 if (zc->zc_leaf && 999 (ZAP_HASH_IDX(zc->zc_hash, 1000 zc->zc_leaf->l_phys->l_hdr.lh_prefix_len) != 1001 zc->zc_leaf->l_phys->l_hdr.lh_prefix)) { 1002 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER); 1003 zap_put_leaf(zc->zc_leaf); 1004 zc->zc_leaf = NULL; 1005 } 1006 1007again: 1008 if (zc->zc_leaf == NULL) { 1009 err = zap_deref_leaf(zap, zc->zc_hash, NULL, RW_READER, 1010 &zc->zc_leaf); 1011 if (err != 0) 1012 return (err); 1013 } else { 1014 rw_enter(&zc->zc_leaf->l_rwlock, RW_READER); 1015 } 1016 l = zc->zc_leaf; 1017 1018 err = zap_leaf_lookup_closest(l, zc->zc_hash, zc->zc_cd, &zeh); 1019 1020 if (err == ENOENT) { 1021 uint64_t nocare = 1022 (1ULL << (64 - l->l_phys->l_hdr.lh_prefix_len)) - 1; 1023 zc->zc_hash = (zc->zc_hash & ~nocare) + nocare + 1; 1024 zc->zc_cd = 0; 1025 if (l->l_phys->l_hdr.lh_prefix_len == 0 || zc->zc_hash == 0) { 1026 zc->zc_hash = -1ULL; 1027 } else { 1028 zap_put_leaf(zc->zc_leaf); 1029 zc->zc_leaf = NULL; 1030 goto again; 1031 } 1032 } 1033 1034 if (err == 0) { 1035 zc->zc_hash = zeh.zeh_hash; 1036 zc->zc_cd = zeh.zeh_cd; 1037 za->za_integer_length = zeh.zeh_integer_size; 1038 za->za_num_integers = zeh.zeh_num_integers; 1039 if (zeh.zeh_num_integers == 0) { 1040 za->za_first_integer = 0; 1041 } else { 1042 err = zap_entry_read(&zeh, 8, 1, &za->za_first_integer); 1043 ASSERT(err == 0 || err == EOVERFLOW); 1044 } 1045 err = zap_entry_read_name(&zeh, 1046 sizeof (za->za_name), za->za_name); 1047 ASSERT(err == 0); 1048 1049 za->za_normalization_conflict = 1050 zap_entry_normalization_conflict(&zeh, 1051 NULL, za->za_name, zap); 1052 } 1053 rw_exit(&zc->zc_leaf->l_rwlock); 1054 return (err); 1055} 1056 1057 1058static void 1059zap_stats_ptrtbl(zap_t *zap, uint64_t *tbl, int len, zap_stats_t *zs) 1060{ 1061 int i, err; 1062 uint64_t lastblk = 0; 1063 1064 /* 1065 * NB: if a leaf has more pointers than an entire ptrtbl block 1066 * can hold, then it'll be accounted for more than once, since 1067 * we won't have lastblk. 1068 */ 1069 for (i = 0; i < len; i++) { 1070 zap_leaf_t *l; 1071 1072 if (tbl[i] == lastblk) 1073 continue; 1074 lastblk = tbl[i]; 1075 1076 err = zap_get_leaf_byblk(zap, tbl[i], NULL, RW_READER, &l); 1077 if (err == 0) { 1078 zap_leaf_stats(zap, l, zs); 1079 zap_put_leaf(l); 1080 } 1081 } 1082} 1083 1084void 1085fzap_get_stats(zap_t *zap, zap_stats_t *zs) 1086{ 1087 int bs = FZAP_BLOCK_SHIFT(zap); 1088 zs->zs_blocksize = 1ULL << bs; 1089 1090 /* 1091 * Set zap_phys_t fields 1092 */ 1093 zs->zs_num_leafs = zap->zap_f.zap_phys->zap_num_leafs; 1094 zs->zs_num_entries = zap->zap_f.zap_phys->zap_num_entries; 1095 zs->zs_num_blocks = zap->zap_f.zap_phys->zap_freeblk; 1096 zs->zs_block_type = zap->zap_f.zap_phys->zap_block_type; 1097 zs->zs_magic = zap->zap_f.zap_phys->zap_magic; 1098 zs->zs_salt = zap->zap_f.zap_phys->zap_salt; 1099 1100 /* 1101 * Set zap_ptrtbl fields 1102 */ 1103 zs->zs_ptrtbl_len = 1ULL << zap->zap_f.zap_phys->zap_ptrtbl.zt_shift; 1104 zs->zs_ptrtbl_nextblk = zap->zap_f.zap_phys->zap_ptrtbl.zt_nextblk; 1105 zs->zs_ptrtbl_blks_copied = 1106 zap->zap_f.zap_phys->zap_ptrtbl.zt_blks_copied; 1107 zs->zs_ptrtbl_zt_blk = zap->zap_f.zap_phys->zap_ptrtbl.zt_blk; 1108 zs->zs_ptrtbl_zt_numblks = zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks; 1109 zs->zs_ptrtbl_zt_shift = zap->zap_f.zap_phys->zap_ptrtbl.zt_shift; 1110 1111 if (zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks == 0) { 1112 /* the ptrtbl is entirely in the header block. */ 1113 zap_stats_ptrtbl(zap, &ZAP_EMBEDDED_PTRTBL_ENT(zap, 0), 1114 1 << ZAP_EMBEDDED_PTRTBL_SHIFT(zap), zs); 1115 } else { 1116 int b; 1117 1118 dmu_prefetch(zap->zap_objset, zap->zap_object, 1119 zap->zap_f.zap_phys->zap_ptrtbl.zt_blk << bs, 1120 zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks << bs); 1121 1122 for (b = 0; b < zap->zap_f.zap_phys->zap_ptrtbl.zt_numblks; 1123 b++) { 1124 dmu_buf_t *db; 1125 int err; 1126 1127 err = dmu_buf_hold(zap->zap_objset, zap->zap_object, 1128 (zap->zap_f.zap_phys->zap_ptrtbl.zt_blk + b) << bs, 1129 FTAG, &db); 1130 if (err == 0) { 1131 zap_stats_ptrtbl(zap, db->db_data, 1132 1<<(bs-3), zs); 1133 dmu_buf_rele(db, FTAG); 1134 } 1135 } 1136 } 1137}
|