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