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 2006 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 #include <sys/zfs_context.h> 29 #include <sys/dbuf.h> 30 #include <sys/dnode.h> 31 #include <sys/dmu.h> 32 #include <sys/dmu_impl.h> 33 #include <sys/dmu_tx.h> 34 #include <sys/dmu_objset.h> 35 #include <sys/dsl_dir.h> 36 #include <sys/dsl_dataset.h> 37 #include <sys/spa.h> 38 #include <sys/zio.h> 39 #include <sys/dmu_zfetch.h> 40 41 static int free_range_compar(const void *node1, const void *node2); 42 43 static kmem_cache_t *dnode_cache; 44 45 static dnode_phys_t dnode_phys_zero; 46 47 int zfs_default_bs = SPA_MINBLOCKSHIFT; 48 int zfs_default_ibs = DN_MAX_INDBLKSHIFT; 49 50 /* ARGSUSED */ 51 static int 52 dnode_cons(void *arg, void *unused, int kmflag) 53 { 54 int i; 55 dnode_t *dn = arg; 56 bzero(dn, sizeof (dnode_t)); 57 58 rw_init(&dn->dn_struct_rwlock, NULL, RW_DEFAULT, NULL); 59 mutex_init(&dn->dn_mtx, NULL, MUTEX_DEFAULT, NULL); 60 mutex_init(&dn->dn_dbufs_mtx, NULL, MUTEX_DEFAULT, NULL); 61 refcount_create(&dn->dn_holds); 62 refcount_create(&dn->dn_tx_holds); 63 64 for (i = 0; i < TXG_SIZE; i++) { 65 avl_create(&dn->dn_ranges[i], free_range_compar, 66 sizeof (free_range_t), 67 offsetof(struct free_range, fr_node)); 68 list_create(&dn->dn_dirty_dbufs[i], 69 sizeof (dmu_buf_impl_t), 70 offsetof(dmu_buf_impl_t, db_dirty_node[i])); 71 } 72 73 list_create(&dn->dn_dbufs, sizeof (dmu_buf_impl_t), 74 offsetof(dmu_buf_impl_t, db_link)); 75 76 return (0); 77 } 78 79 /* ARGSUSED */ 80 static void 81 dnode_dest(void *arg, void *unused) 82 { 83 int i; 84 dnode_t *dn = arg; 85 86 rw_destroy(&dn->dn_struct_rwlock); 87 mutex_destroy(&dn->dn_mtx); 88 mutex_destroy(&dn->dn_dbufs_mtx); 89 refcount_destroy(&dn->dn_holds); 90 refcount_destroy(&dn->dn_tx_holds); 91 92 for (i = 0; i < TXG_SIZE; i++) { 93 avl_destroy(&dn->dn_ranges[i]); 94 list_destroy(&dn->dn_dirty_dbufs[i]); 95 } 96 97 list_destroy(&dn->dn_dbufs); 98 } 99 100 void 101 dnode_init(void) 102 { 103 dnode_cache = kmem_cache_create("dnode_t", 104 sizeof (dnode_t), 105 0, dnode_cons, dnode_dest, NULL, NULL, NULL, 0); 106 } 107 108 void 109 dnode_fini(void) 110 { 111 kmem_cache_destroy(dnode_cache); 112 } 113 114 115 #ifdef ZFS_DEBUG 116 void 117 dnode_verify(dnode_t *dn) 118 { 119 int drop_struct_lock = FALSE; 120 121 ASSERT(dn->dn_phys); 122 ASSERT(dn->dn_objset); 123 124 ASSERT(dn->dn_phys->dn_type < DMU_OT_NUMTYPES); 125 126 if (!(zfs_flags & ZFS_DEBUG_DNODE_VERIFY)) 127 return; 128 129 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) { 130 rw_enter(&dn->dn_struct_rwlock, RW_READER); 131 drop_struct_lock = TRUE; 132 } 133 if (dn->dn_phys->dn_type != DMU_OT_NONE || dn->dn_allocated_txg != 0) { 134 int i; 135 ASSERT3U(dn->dn_indblkshift, >=, 0); 136 ASSERT3U(dn->dn_indblkshift, <=, SPA_MAXBLOCKSHIFT); 137 if (dn->dn_datablkshift) { 138 ASSERT3U(dn->dn_datablkshift, >=, SPA_MINBLOCKSHIFT); 139 ASSERT3U(dn->dn_datablkshift, <=, SPA_MAXBLOCKSHIFT); 140 ASSERT3U(1<<dn->dn_datablkshift, ==, dn->dn_datablksz); 141 } 142 ASSERT3U(dn->dn_nlevels, <=, 30); 143 ASSERT3U(dn->dn_type, <=, DMU_OT_NUMTYPES); 144 ASSERT3U(dn->dn_nblkptr, >=, 1); 145 ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR); 146 ASSERT3U(dn->dn_bonuslen, <=, DN_MAX_BONUSLEN); 147 ASSERT3U(dn->dn_datablksz, ==, 148 dn->dn_datablkszsec << SPA_MINBLOCKSHIFT); 149 ASSERT3U(ISP2(dn->dn_datablksz), ==, dn->dn_datablkshift != 0); 150 ASSERT3U((dn->dn_nblkptr - 1) * sizeof (blkptr_t) + 151 dn->dn_bonuslen, <=, DN_MAX_BONUSLEN); 152 for (i = 0; i < TXG_SIZE; i++) { 153 ASSERT3U(dn->dn_next_nlevels[i], <=, dn->dn_nlevels); 154 } 155 } 156 if (dn->dn_phys->dn_type != DMU_OT_NONE) 157 ASSERT3U(dn->dn_phys->dn_nlevels, <=, dn->dn_nlevels); 158 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT || dn->dn_dbuf != NULL); 159 if (dn->dn_dbuf != NULL) { 160 ASSERT3P(dn->dn_phys, ==, 161 (dnode_phys_t *)dn->dn_dbuf->db.db_data + 162 (dn->dn_object % (dn->dn_dbuf->db.db_size >> DNODE_SHIFT))); 163 } 164 if (drop_struct_lock) 165 rw_exit(&dn->dn_struct_rwlock); 166 } 167 #endif 168 169 void 170 dnode_byteswap(dnode_phys_t *dnp) 171 { 172 uint64_t *buf64 = (void*)&dnp->dn_blkptr; 173 int i; 174 175 if (dnp->dn_type == DMU_OT_NONE) { 176 bzero(dnp, sizeof (dnode_phys_t)); 177 return; 178 } 179 180 dnp->dn_type = BSWAP_8(dnp->dn_type); 181 dnp->dn_indblkshift = BSWAP_8(dnp->dn_indblkshift); 182 dnp->dn_nlevels = BSWAP_8(dnp->dn_nlevels); 183 dnp->dn_nblkptr = BSWAP_8(dnp->dn_nblkptr); 184 dnp->dn_bonustype = BSWAP_8(dnp->dn_bonustype); 185 dnp->dn_checksum = BSWAP_8(dnp->dn_checksum); 186 dnp->dn_compress = BSWAP_8(dnp->dn_compress); 187 dnp->dn_datablkszsec = BSWAP_16(dnp->dn_datablkszsec); 188 dnp->dn_bonuslen = BSWAP_16(dnp->dn_bonuslen); 189 dnp->dn_maxblkid = BSWAP_64(dnp->dn_maxblkid); 190 dnp->dn_secphys = BSWAP_64(dnp->dn_secphys); 191 192 /* 193 * dn_nblkptr is only one byte, so it's OK to read it in either 194 * byte order. We can't read dn_bouslen. 195 */ 196 ASSERT(dnp->dn_indblkshift <= SPA_MAXBLOCKSHIFT); 197 ASSERT(dnp->dn_nblkptr <= DN_MAX_NBLKPTR); 198 for (i = 0; i < dnp->dn_nblkptr * sizeof (blkptr_t)/8; i++) 199 buf64[i] = BSWAP_64(buf64[i]); 200 201 /* 202 * OK to check dn_bonuslen for zero, because it won't matter if 203 * we have the wrong byte order. This is necessary because the 204 * dnode dnode is smaller than a regular dnode. 205 */ 206 if (dnp->dn_bonuslen != 0) { 207 /* 208 * Note that the bonus length calculated here may be 209 * longer than the actual bonus buffer. This is because 210 * we always put the bonus buffer after the last block 211 * pointer (instead of packing it against the end of the 212 * dnode buffer). 213 */ 214 int off = (dnp->dn_nblkptr-1) * sizeof (blkptr_t); 215 size_t len = DN_MAX_BONUSLEN - off; 216 dmu_ot[dnp->dn_bonustype].ot_byteswap(dnp->dn_bonus + off, len); 217 } 218 } 219 220 void 221 dnode_buf_byteswap(void *vbuf, size_t size) 222 { 223 dnode_phys_t *buf = vbuf; 224 int i; 225 226 ASSERT3U(sizeof (dnode_phys_t), ==, (1<<DNODE_SHIFT)); 227 ASSERT((size & (sizeof (dnode_phys_t)-1)) == 0); 228 229 size >>= DNODE_SHIFT; 230 for (i = 0; i < size; i++) { 231 dnode_byteswap(buf); 232 buf++; 233 } 234 } 235 236 static int 237 free_range_compar(const void *node1, const void *node2) 238 { 239 const free_range_t *rp1 = node1; 240 const free_range_t *rp2 = node2; 241 242 if (rp1->fr_blkid < rp2->fr_blkid) 243 return (-1); 244 else if (rp1->fr_blkid > rp2->fr_blkid) 245 return (1); 246 else return (0); 247 } 248 249 static void 250 dnode_setdblksz(dnode_t *dn, int size) 251 { 252 ASSERT3U(P2PHASE(size, SPA_MINBLOCKSIZE), ==, 0); 253 ASSERT3U(size, <=, SPA_MAXBLOCKSIZE); 254 ASSERT3U(size, >=, SPA_MINBLOCKSIZE); 255 ASSERT3U(size >> SPA_MINBLOCKSHIFT, <, 256 1<<(sizeof (dn->dn_phys->dn_datablkszsec) * 8)); 257 dn->dn_datablksz = size; 258 dn->dn_datablkszsec = size >> SPA_MINBLOCKSHIFT; 259 dn->dn_datablkshift = ISP2(size) ? highbit(size - 1) : 0; 260 } 261 262 static dnode_t * 263 dnode_create(objset_impl_t *os, dnode_phys_t *dnp, dmu_buf_impl_t *db, 264 uint64_t object) 265 { 266 dnode_t *dn = kmem_cache_alloc(dnode_cache, KM_SLEEP); 267 (void) dnode_cons(dn, NULL, 0); /* XXX */ 268 269 dn->dn_objset = os; 270 dn->dn_object = object; 271 dn->dn_dbuf = db; 272 dn->dn_phys = dnp; 273 274 if (dnp->dn_datablkszsec) 275 dnode_setdblksz(dn, dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT); 276 dn->dn_indblkshift = dnp->dn_indblkshift; 277 dn->dn_nlevels = dnp->dn_nlevels; 278 dn->dn_type = dnp->dn_type; 279 dn->dn_nblkptr = dnp->dn_nblkptr; 280 dn->dn_checksum = dnp->dn_checksum; 281 dn->dn_compress = dnp->dn_compress; 282 dn->dn_bonustype = dnp->dn_bonustype; 283 dn->dn_bonuslen = dnp->dn_bonuslen; 284 dn->dn_maxblkid = dnp->dn_maxblkid; 285 286 dmu_zfetch_init(&dn->dn_zfetch, dn); 287 288 ASSERT(dn->dn_phys->dn_type < DMU_OT_NUMTYPES); 289 mutex_enter(&os->os_lock); 290 list_insert_head(&os->os_dnodes, dn); 291 mutex_exit(&os->os_lock); 292 293 return (dn); 294 } 295 296 static void 297 dnode_destroy(dnode_t *dn) 298 { 299 objset_impl_t *os = dn->dn_objset; 300 301 mutex_enter(&os->os_lock); 302 list_remove(&os->os_dnodes, dn); 303 mutex_exit(&os->os_lock); 304 305 if (dn->dn_dirtyctx_firstset) { 306 kmem_free(dn->dn_dirtyctx_firstset, 1); 307 dn->dn_dirtyctx_firstset = NULL; 308 } 309 dmu_zfetch_rele(&dn->dn_zfetch); 310 if (dn->dn_bonus) { 311 mutex_enter(&dn->dn_bonus->db_mtx); 312 dbuf_evict(dn->dn_bonus); 313 dn->dn_bonus = NULL; 314 } 315 kmem_cache_free(dnode_cache, dn); 316 } 317 318 void 319 dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs, 320 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 321 { 322 int i; 323 324 if (blocksize == 0) 325 blocksize = 1 << zfs_default_bs; 326 else if (blocksize > SPA_MAXBLOCKSIZE) 327 blocksize = SPA_MAXBLOCKSIZE; 328 else 329 blocksize = P2ROUNDUP(blocksize, SPA_MINBLOCKSIZE); 330 331 if (ibs == 0) 332 ibs = zfs_default_ibs; 333 334 ibs = MIN(MAX(ibs, DN_MIN_INDBLKSHIFT), DN_MAX_INDBLKSHIFT); 335 336 dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d\n", dn->dn_objset, 337 dn->dn_object, tx->tx_txg, blocksize, ibs); 338 339 ASSERT(dn->dn_type == DMU_OT_NONE); 340 ASSERT(bcmp(dn->dn_phys, &dnode_phys_zero, sizeof (dnode_phys_t)) == 0); 341 ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE); 342 ASSERT(ot != DMU_OT_NONE); 343 ASSERT3U(ot, <, DMU_OT_NUMTYPES); 344 ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) || 345 (bonustype != DMU_OT_NONE && bonuslen != 0)); 346 ASSERT3U(bonustype, <, DMU_OT_NUMTYPES); 347 ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN); 348 ASSERT(dn->dn_type == DMU_OT_NONE); 349 ASSERT3U(dn->dn_maxblkid, ==, 0); 350 ASSERT3U(dn->dn_allocated_txg, ==, 0); 351 ASSERT3U(dn->dn_assigned_txg, ==, 0); 352 ASSERT(refcount_is_zero(&dn->dn_tx_holds)); 353 ASSERT3U(refcount_count(&dn->dn_holds), <=, 1); 354 ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL); 355 356 for (i = 0; i < TXG_SIZE; i++) { 357 ASSERT3U(dn->dn_next_nlevels[i], ==, 0); 358 ASSERT3U(dn->dn_next_indblkshift[i], ==, 0); 359 ASSERT3U(dn->dn_next_blksz[i], ==, 0); 360 ASSERT(!list_link_active(&dn->dn_dirty_link[i])); 361 ASSERT3P(list_head(&dn->dn_dirty_dbufs[i]), ==, NULL); 362 ASSERT3U(avl_numnodes(&dn->dn_ranges[i]), ==, 0); 363 } 364 365 dn->dn_type = ot; 366 dnode_setdblksz(dn, blocksize); 367 dn->dn_indblkshift = ibs; 368 dn->dn_nlevels = 1; 369 dn->dn_nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT); 370 dn->dn_bonustype = bonustype; 371 dn->dn_bonuslen = bonuslen; 372 dn->dn_checksum = ZIO_CHECKSUM_INHERIT; 373 dn->dn_compress = ZIO_COMPRESS_INHERIT; 374 dn->dn_dirtyctx = 0; 375 376 dn->dn_free_txg = 0; 377 if (dn->dn_dirtyctx_firstset) { 378 kmem_free(dn->dn_dirtyctx_firstset, 1); 379 dn->dn_dirtyctx_firstset = NULL; 380 } 381 382 dn->dn_allocated_txg = tx->tx_txg; 383 dnode_setdirty(dn, tx); 384 } 385 386 void 387 dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, 388 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 389 { 390 int i; 391 392 ASSERT3U(blocksize, >=, SPA_MINBLOCKSIZE); 393 ASSERT3U(blocksize, <=, SPA_MAXBLOCKSIZE); 394 ASSERT3U(blocksize % SPA_MINBLOCKSIZE, ==, 0); 395 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx)); 396 ASSERT(tx->tx_txg != 0); 397 ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) || 398 (bonustype != DMU_OT_NONE && bonuslen != 0)); 399 ASSERT3U(bonustype, <, DMU_OT_NUMTYPES); 400 ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN); 401 402 for (i = 0; i < TXG_SIZE; i++) 403 ASSERT(!list_link_active(&dn->dn_dirty_link[i])); 404 405 /* clean up any unreferenced dbufs */ 406 dnode_evict_dbufs(dn); 407 ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL); 408 409 /* 410 * XXX I should really have a generation number to tell if we 411 * need to do this... 412 */ 413 if (blocksize != dn->dn_datablksz || 414 dn->dn_bonustype != bonustype || dn->dn_bonuslen != bonuslen) { 415 /* free all old data */ 416 dnode_free_range(dn, 0, -1ULL, tx); 417 } 418 419 /* change blocksize */ 420 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 421 dnode_setdblksz(dn, blocksize); 422 dnode_setdirty(dn, tx); 423 dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = blocksize; 424 rw_exit(&dn->dn_struct_rwlock); 425 426 /* change type */ 427 dn->dn_type = ot; 428 429 if (dn->dn_bonuslen != bonuslen) { 430 dmu_buf_impl_t *db = NULL; 431 432 /* change bonus size */ 433 if (bonuslen == 0) 434 bonuslen = 1; /* XXX */ 435 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 436 if (dn->dn_bonus == NULL) 437 dn->dn_bonus = dbuf_create_bonus(dn); 438 db = dn->dn_bonus; 439 rw_exit(&dn->dn_struct_rwlock); 440 if (refcount_add(&db->db_holds, FTAG) == 1) 441 dnode_add_ref(dn, db); 442 mutex_enter(&db->db_mtx); 443 ASSERT3U(db->db.db_size, ==, dn->dn_bonuslen); 444 ASSERT(db->db.db_data != NULL); 445 db->db.db_size = bonuslen; 446 mutex_exit(&db->db_mtx); 447 dbuf_dirty(db, tx); 448 dbuf_rele(db, FTAG); 449 } 450 451 /* change bonus size and type */ 452 mutex_enter(&dn->dn_mtx); 453 dn->dn_bonustype = bonustype; 454 dn->dn_bonuslen = bonuslen; 455 dn->dn_nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT); 456 dn->dn_checksum = ZIO_CHECKSUM_INHERIT; 457 dn->dn_compress = ZIO_COMPRESS_INHERIT; 458 ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR); 459 460 dn->dn_allocated_txg = tx->tx_txg; 461 mutex_exit(&dn->dn_mtx); 462 } 463 464 void 465 dnode_special_close(dnode_t *dn) 466 { 467 /* 468 * Wait for final references to the dnode to clear. This can 469 * only happen if the arc is asyncronously evicting state that 470 * has a hold on this dnode while we are trying to evict this 471 * dnode. 472 */ 473 while (refcount_count(&dn->dn_holds) > 0) 474 delay(1); 475 dnode_destroy(dn); 476 } 477 478 dnode_t * 479 dnode_special_open(objset_impl_t *os, dnode_phys_t *dnp, uint64_t object) 480 { 481 dnode_t *dn = dnode_create(os, dnp, NULL, object); 482 DNODE_VERIFY(dn); 483 return (dn); 484 } 485 486 static void 487 dnode_buf_pageout(dmu_buf_t *db, void *arg) 488 { 489 dnode_t **children_dnodes = arg; 490 int i; 491 int epb = db->db_size >> DNODE_SHIFT; 492 493 for (i = 0; i < epb; i++) { 494 dnode_t *dn = children_dnodes[i]; 495 int n; 496 497 if (dn == NULL) 498 continue; 499 #ifdef ZFS_DEBUG 500 /* 501 * If there are holds on this dnode, then there should 502 * be holds on the dnode's containing dbuf as well; thus 503 * it wouldn't be eligable for eviction and this function 504 * would not have been called. 505 */ 506 ASSERT(refcount_is_zero(&dn->dn_holds)); 507 ASSERT(list_head(&dn->dn_dbufs) == NULL); 508 ASSERT(refcount_is_zero(&dn->dn_tx_holds)); 509 510 for (n = 0; n < TXG_SIZE; n++) 511 ASSERT(!list_link_active(&dn->dn_dirty_link[n])); 512 #endif 513 children_dnodes[i] = NULL; 514 dnode_destroy(dn); 515 } 516 kmem_free(children_dnodes, epb * sizeof (dnode_t *)); 517 } 518 519 /* 520 * errors: 521 * EINVAL - invalid object number. 522 * EIO - i/o error. 523 * succeeds even for free dnodes. 524 */ 525 int 526 dnode_hold_impl(objset_impl_t *os, uint64_t object, int flag, 527 void *tag, dnode_t **dnp) 528 { 529 int epb, idx, err; 530 int drop_struct_lock = FALSE; 531 int type; 532 uint64_t blk; 533 dnode_t *mdn, *dn; 534 dmu_buf_impl_t *db; 535 dnode_t **children_dnodes; 536 537 if (object == 0 || object >= DN_MAX_OBJECT) 538 return (EINVAL); 539 540 mdn = os->os_meta_dnode; 541 542 DNODE_VERIFY(mdn); 543 544 if (!RW_WRITE_HELD(&mdn->dn_struct_rwlock)) { 545 rw_enter(&mdn->dn_struct_rwlock, RW_READER); 546 drop_struct_lock = TRUE; 547 } 548 549 blk = dbuf_whichblock(mdn, object * sizeof (dnode_phys_t)); 550 551 db = dbuf_hold(mdn, blk, FTAG); 552 if (drop_struct_lock) 553 rw_exit(&mdn->dn_struct_rwlock); 554 if (db == NULL) 555 return (EIO); 556 err = dbuf_read(db, NULL, DB_RF_CANFAIL); 557 if (err) { 558 dbuf_rele(db, FTAG); 559 return (err); 560 } 561 562 ASSERT3U(db->db.db_size, >=, 1<<DNODE_SHIFT); 563 epb = db->db.db_size >> DNODE_SHIFT; 564 565 idx = object & (epb-1); 566 567 children_dnodes = dmu_buf_get_user(&db->db); 568 if (children_dnodes == NULL) { 569 dnode_t **winner; 570 children_dnodes = kmem_zalloc(epb * sizeof (dnode_t *), 571 KM_SLEEP); 572 if (winner = dmu_buf_set_user(&db->db, children_dnodes, NULL, 573 dnode_buf_pageout)) { 574 kmem_free(children_dnodes, epb * sizeof (dnode_t *)); 575 children_dnodes = winner; 576 } 577 } 578 579 if ((dn = children_dnodes[idx]) == NULL) { 580 dnode_t *winner; 581 dn = dnode_create(os, (dnode_phys_t *)db->db.db_data+idx, 582 db, object); 583 winner = atomic_cas_ptr(&children_dnodes[idx], NULL, dn); 584 if (winner != NULL) { 585 dnode_destroy(dn); 586 dn = winner; 587 } 588 } 589 590 mutex_enter(&dn->dn_mtx); 591 type = dn->dn_type; 592 if (dn->dn_free_txg || 593 ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE) || 594 ((flag & DNODE_MUST_BE_FREE) && type != DMU_OT_NONE)) { 595 mutex_exit(&dn->dn_mtx); 596 dbuf_rele(db, FTAG); 597 return (type == DMU_OT_NONE ? ENOENT : EEXIST); 598 } 599 mutex_exit(&dn->dn_mtx); 600 601 if (refcount_add(&dn->dn_holds, tag) == 1) 602 dbuf_add_ref(db, dn); 603 604 DNODE_VERIFY(dn); 605 ASSERT3P(dn->dn_dbuf, ==, db); 606 ASSERT3U(dn->dn_object, ==, object); 607 dbuf_rele(db, FTAG); 608 609 *dnp = dn; 610 return (0); 611 } 612 613 /* 614 * Return held dnode if the object is allocated, NULL if not. 615 */ 616 int 617 dnode_hold(objset_impl_t *os, uint64_t object, void *tag, dnode_t **dnp) 618 { 619 return (dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, tag, dnp)); 620 } 621 622 void 623 dnode_add_ref(dnode_t *dn, void *tag) 624 { 625 ASSERT(refcount_count(&dn->dn_holds) > 0); 626 (void) refcount_add(&dn->dn_holds, tag); 627 } 628 629 void 630 dnode_rele(dnode_t *dn, void *tag) 631 { 632 uint64_t refs; 633 634 refs = refcount_remove(&dn->dn_holds, tag); 635 /* NOTE: the DNODE_DNODE does not have a dn_dbuf */ 636 if (refs == 0 && dn->dn_dbuf) 637 dbuf_rele(dn->dn_dbuf, dn); 638 } 639 640 void 641 dnode_setdirty(dnode_t *dn, dmu_tx_t *tx) 642 { 643 objset_impl_t *os = dn->dn_objset; 644 uint64_t txg = tx->tx_txg; 645 646 if (dn->dn_object == DMU_META_DNODE_OBJECT) 647 return; 648 649 DNODE_VERIFY(dn); 650 651 #ifdef ZFS_DEBUG 652 mutex_enter(&dn->dn_mtx); 653 ASSERT(dn->dn_phys->dn_type || dn->dn_allocated_txg); 654 /* ASSERT(dn->dn_free_txg == 0 || dn->dn_free_txg >= txg); */ 655 mutex_exit(&dn->dn_mtx); 656 #endif 657 658 mutex_enter(&os->os_lock); 659 660 /* 661 * If we are already marked dirty, we're done. 662 */ 663 if (list_link_active(&dn->dn_dirty_link[txg & TXG_MASK])) { 664 mutex_exit(&os->os_lock); 665 return; 666 } 667 668 ASSERT(!refcount_is_zero(&dn->dn_holds) || list_head(&dn->dn_dbufs)); 669 ASSERT(dn->dn_datablksz != 0); 670 671 dprintf_ds(os->os_dsl_dataset, "obj=%llu txg=%llu\n", 672 dn->dn_object, txg); 673 674 if (dn->dn_free_txg > 0 && dn->dn_free_txg <= txg) { 675 list_insert_tail(&os->os_free_dnodes[txg&TXG_MASK], dn); 676 } else { 677 list_insert_tail(&os->os_dirty_dnodes[txg&TXG_MASK], dn); 678 } 679 680 mutex_exit(&os->os_lock); 681 682 /* 683 * The dnode maintains a hold on its containing dbuf as 684 * long as there are holds on it. Each instantiated child 685 * dbuf maintaines a hold on the dnode. When the last child 686 * drops its hold, the dnode will drop its hold on the 687 * containing dbuf. We add a "dirty hold" here so that the 688 * dnode will hang around after we finish processing its 689 * children. 690 */ 691 dnode_add_ref(dn, (void *)(uintptr_t)tx->tx_txg); 692 693 dbuf_dirty(dn->dn_dbuf, tx); 694 695 dsl_dataset_dirty(os->os_dsl_dataset, tx); 696 } 697 698 void 699 dnode_free(dnode_t *dn, dmu_tx_t *tx) 700 { 701 int txgoff = tx->tx_txg & TXG_MASK; 702 703 dprintf("dn=%p txg=%llu\n", dn, tx->tx_txg); 704 705 /* we should be the only holder... hopefully */ 706 /* ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); */ 707 708 mutex_enter(&dn->dn_mtx); 709 if (dn->dn_type == DMU_OT_NONE || dn->dn_free_txg) { 710 mutex_exit(&dn->dn_mtx); 711 return; 712 } 713 dn->dn_free_txg = tx->tx_txg; 714 mutex_exit(&dn->dn_mtx); 715 716 /* 717 * If the dnode is already dirty, it needs to be moved from 718 * the dirty list to the free list. 719 */ 720 mutex_enter(&dn->dn_objset->os_lock); 721 if (list_link_active(&dn->dn_dirty_link[txgoff])) { 722 list_remove(&dn->dn_objset->os_dirty_dnodes[txgoff], dn); 723 list_insert_tail(&dn->dn_objset->os_free_dnodes[txgoff], dn); 724 mutex_exit(&dn->dn_objset->os_lock); 725 } else { 726 mutex_exit(&dn->dn_objset->os_lock); 727 dnode_setdirty(dn, tx); 728 } 729 } 730 731 /* 732 * Try to change the block size for the indicated dnode. This can only 733 * succeed if there are no blocks allocated or dirty beyond first block 734 */ 735 int 736 dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx) 737 { 738 dmu_buf_impl_t *db, *db_next; 739 int have_db0 = FALSE; 740 int err = ENOTSUP; 741 742 if (size == 0) 743 size = SPA_MINBLOCKSIZE; 744 if (size > SPA_MAXBLOCKSIZE) 745 size = SPA_MAXBLOCKSIZE; 746 else 747 size = P2ROUNDUP(size, SPA_MINBLOCKSIZE); 748 749 if (ibs == 0) 750 ibs = dn->dn_indblkshift; 751 752 if (size >> SPA_MINBLOCKSHIFT == dn->dn_datablkszsec && 753 ibs == dn->dn_indblkshift) 754 return (0); 755 756 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 757 758 /* Check for any allocated blocks beyond the first */ 759 if (dn->dn_phys->dn_maxblkid != 0) 760 goto end; 761 762 mutex_enter(&dn->dn_dbufs_mtx); 763 for (db = list_head(&dn->dn_dbufs); db; db = db_next) { 764 db_next = list_next(&dn->dn_dbufs, db); 765 766 if (db->db_blkid == 0) { 767 have_db0 = TRUE; 768 } else if (db->db_blkid != DB_BONUS_BLKID) { 769 mutex_exit(&dn->dn_dbufs_mtx); 770 goto end; 771 } 772 } 773 mutex_exit(&dn->dn_dbufs_mtx); 774 775 db = NULL; 776 if (!BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0]) || have_db0) { 777 /* obtain the old block */ 778 db = dbuf_hold(dn, 0, FTAG); 779 dbuf_new_size(db, size, tx); 780 } 781 782 dnode_setdblksz(dn, size); 783 dn->dn_indblkshift = ibs; 784 dnode_setdirty(dn, tx); 785 dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = size; 786 dn->dn_next_indblkshift[tx->tx_txg&TXG_MASK] = ibs; 787 788 if (db) 789 dbuf_rele(db, FTAG); 790 791 err = 0; 792 end: 793 rw_exit(&dn->dn_struct_rwlock); 794 return (err); 795 } 796 797 uint64_t 798 dnode_max_nonzero_offset(dnode_t *dn) 799 { 800 if (dn->dn_phys->dn_maxblkid == 0 && 801 BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0])) 802 return (0); 803 else 804 return ((dn->dn_phys->dn_maxblkid+1) * dn->dn_datablksz); 805 } 806 807 void 808 dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx) 809 { 810 uint64_t txgoff = tx->tx_txg & TXG_MASK; 811 int drop_struct_lock = FALSE; 812 int epbs, new_nlevels; 813 uint64_t sz; 814 815 ASSERT(blkid != DB_BONUS_BLKID); 816 817 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) { 818 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 819 drop_struct_lock = TRUE; 820 } 821 822 if (blkid <= dn->dn_maxblkid) 823 goto out; 824 825 dn->dn_maxblkid = blkid; 826 827 /* 828 * Compute the number of levels necessary to support the new maxblkid. 829 */ 830 new_nlevels = 1; 831 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 832 for (sz = dn->dn_nblkptr; 833 sz <= blkid && sz >= dn->dn_nblkptr; sz <<= epbs) 834 new_nlevels++; 835 836 if (new_nlevels > dn->dn_nlevels) { 837 int old_nlevels = dn->dn_nlevels; 838 dmu_buf_impl_t *db; 839 840 dn->dn_nlevels = new_nlevels; 841 842 ASSERT3U(new_nlevels, >, dn->dn_next_nlevels[txgoff]); 843 dn->dn_next_nlevels[txgoff] = new_nlevels; 844 845 /* Dirty the left indirects. */ 846 db = dbuf_hold_level(dn, old_nlevels, 0, FTAG); 847 dbuf_dirty(db, tx); 848 dbuf_rele(db, FTAG); 849 850 } 851 852 out: 853 if (drop_struct_lock) 854 rw_exit(&dn->dn_struct_rwlock); 855 } 856 857 void 858 dnode_clear_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx) 859 { 860 avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK]; 861 avl_index_t where; 862 free_range_t *rp; 863 free_range_t rp_tofind; 864 uint64_t endblk = blkid + nblks; 865 866 ASSERT(MUTEX_HELD(&dn->dn_mtx)); 867 ASSERT(nblks <= UINT64_MAX - blkid); /* no overflow */ 868 869 dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n", 870 blkid, nblks, tx->tx_txg); 871 rp_tofind.fr_blkid = blkid; 872 rp = avl_find(tree, &rp_tofind, &where); 873 if (rp == NULL) 874 rp = avl_nearest(tree, where, AVL_BEFORE); 875 if (rp == NULL) 876 rp = avl_nearest(tree, where, AVL_AFTER); 877 878 while (rp && (rp->fr_blkid <= blkid + nblks)) { 879 uint64_t fr_endblk = rp->fr_blkid + rp->fr_nblks; 880 free_range_t *nrp = AVL_NEXT(tree, rp); 881 882 if (blkid <= rp->fr_blkid && endblk >= fr_endblk) { 883 /* clear this entire range */ 884 avl_remove(tree, rp); 885 kmem_free(rp, sizeof (free_range_t)); 886 } else if (blkid <= rp->fr_blkid && 887 endblk > rp->fr_blkid && endblk < fr_endblk) { 888 /* clear the beginning of this range */ 889 rp->fr_blkid = endblk; 890 rp->fr_nblks = fr_endblk - endblk; 891 } else if (blkid > rp->fr_blkid && blkid < fr_endblk && 892 endblk >= fr_endblk) { 893 /* clear the end of this range */ 894 rp->fr_nblks = blkid - rp->fr_blkid; 895 } else if (blkid > rp->fr_blkid && endblk < fr_endblk) { 896 /* clear a chunk out of this range */ 897 free_range_t *new_rp = 898 kmem_alloc(sizeof (free_range_t), KM_SLEEP); 899 900 new_rp->fr_blkid = endblk; 901 new_rp->fr_nblks = fr_endblk - endblk; 902 avl_insert_here(tree, new_rp, rp, AVL_AFTER); 903 rp->fr_nblks = blkid - rp->fr_blkid; 904 } 905 /* there may be no overlap */ 906 rp = nrp; 907 } 908 } 909 910 void 911 dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx) 912 { 913 dmu_buf_impl_t *db; 914 uint64_t start, objsize, blkid, nblks; 915 int blkshift, blksz, tail, head, epbs; 916 int trunc = FALSE; 917 918 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 919 blksz = dn->dn_datablksz; 920 blkshift = dn->dn_datablkshift; 921 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 922 923 /* If the range is past the end of the file, this is a no-op */ 924 objsize = blksz * (dn->dn_maxblkid+1); 925 if (off >= objsize) 926 goto out; 927 if (len == -1ULL) { 928 len = UINT64_MAX - off; 929 trunc = TRUE; 930 } 931 932 /* 933 * First, block align the region to free: 934 */ 935 if (dn->dn_maxblkid == 0) { 936 if (off == 0) { 937 head = 0; 938 } else { 939 head = blksz - off; 940 ASSERT3U(head, >, 0); 941 } 942 start = off; 943 } else { 944 ASSERT(ISP2(blksz)); 945 head = P2NPHASE(off, blksz); 946 start = P2PHASE(off, blksz); 947 } 948 /* zero out any partial block data at the start of the range */ 949 if (head) { 950 ASSERT3U(start + head, ==, blksz); 951 if (len < head) 952 head = len; 953 if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off), TRUE, 954 FTAG, &db) == 0) { 955 caddr_t data; 956 957 /* don't dirty if it isn't on disk and isn't dirty */ 958 if (db->db_dirtied || 959 (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) { 960 rw_exit(&dn->dn_struct_rwlock); 961 dbuf_will_dirty(db, tx); 962 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 963 data = db->db.db_data; 964 bzero(data + start, head); 965 } 966 dbuf_rele(db, FTAG); 967 } 968 off += head; 969 len -= head; 970 } 971 /* If the range was less than one block, we are done */ 972 if (len == 0) 973 goto out; 974 975 /* If the remaining range is past the end of the file, we are done */ 976 if (off > dn->dn_maxblkid << blkshift) 977 goto out; 978 979 if (off + len == UINT64_MAX) 980 tail = 0; 981 else 982 tail = P2PHASE(len, blksz); 983 984 ASSERT3U(P2PHASE(off, blksz), ==, 0); 985 /* zero out any partial block data at the end of the range */ 986 if (tail) { 987 if (len < tail) 988 tail = len; 989 if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off+len), 990 TRUE, FTAG, &db) == 0) { 991 /* don't dirty if it isn't on disk and isn't dirty */ 992 if (db->db_dirtied || 993 (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) { 994 rw_exit(&dn->dn_struct_rwlock); 995 dbuf_will_dirty(db, tx); 996 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 997 bzero(db->db.db_data, tail); 998 } 999 dbuf_rele(db, FTAG); 1000 } 1001 len -= tail; 1002 } 1003 /* If the range did not include a full block, we are done */ 1004 if (len == 0) 1005 goto out; 1006 1007 /* dirty the left indirects */ 1008 if (dn->dn_nlevels > 1 && off != 0) { 1009 db = dbuf_hold_level(dn, 1, 1010 (off - head) >> (blkshift + epbs), FTAG); 1011 dbuf_will_dirty(db, tx); 1012 dbuf_rele(db, FTAG); 1013 } 1014 1015 /* dirty the right indirects */ 1016 if (dn->dn_nlevels > 1 && !trunc) { 1017 db = dbuf_hold_level(dn, 1, 1018 (off + len + tail - 1) >> (blkshift + epbs), FTAG); 1019 dbuf_will_dirty(db, tx); 1020 dbuf_rele(db, FTAG); 1021 } 1022 1023 /* 1024 * Finally, add this range to the dnode range list, we 1025 * will finish up this free operation in the syncing phase. 1026 */ 1027 ASSERT(IS_P2ALIGNED(off, 1<<blkshift)); 1028 ASSERT(off + len == UINT64_MAX || IS_P2ALIGNED(len, 1<<blkshift)); 1029 blkid = off >> blkshift; 1030 nblks = len >> blkshift; 1031 1032 if (trunc) 1033 dn->dn_maxblkid = (blkid ? blkid - 1 : 0); 1034 1035 mutex_enter(&dn->dn_mtx); 1036 dnode_clear_range(dn, blkid, nblks, tx); 1037 { 1038 free_range_t *rp, *found; 1039 avl_index_t where; 1040 avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK]; 1041 1042 /* Add new range to dn_ranges */ 1043 rp = kmem_alloc(sizeof (free_range_t), KM_SLEEP); 1044 rp->fr_blkid = blkid; 1045 rp->fr_nblks = nblks; 1046 found = avl_find(tree, rp, &where); 1047 ASSERT(found == NULL); 1048 avl_insert(tree, rp, where); 1049 dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n", 1050 blkid, nblks, tx->tx_txg); 1051 } 1052 mutex_exit(&dn->dn_mtx); 1053 1054 dbuf_free_range(dn, blkid, nblks, tx); 1055 dnode_setdirty(dn, tx); 1056 out: 1057 rw_exit(&dn->dn_struct_rwlock); 1058 } 1059 1060 /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */ 1061 uint64_t 1062 dnode_block_freed(dnode_t *dn, uint64_t blkid) 1063 { 1064 free_range_t range_tofind; 1065 void *dp = spa_get_dsl(dn->dn_objset->os_spa); 1066 int i; 1067 1068 if (blkid == DB_BONUS_BLKID) 1069 return (FALSE); 1070 1071 /* 1072 * If we're in the process of opening the pool, dp will not be 1073 * set yet, but there shouldn't be anything dirty. 1074 */ 1075 if (dp == NULL) 1076 return (FALSE); 1077 1078 if (dn->dn_free_txg) 1079 return (TRUE); 1080 1081 /* 1082 * If dn_datablkshift is not set, then there's only a single 1083 * block, in which case there will never be a free range so it 1084 * won't matter. 1085 */ 1086 range_tofind.fr_blkid = blkid; 1087 mutex_enter(&dn->dn_mtx); 1088 for (i = 0; i < TXG_SIZE; i++) { 1089 free_range_t *range_found; 1090 avl_index_t idx; 1091 1092 range_found = avl_find(&dn->dn_ranges[i], &range_tofind, &idx); 1093 if (range_found) { 1094 ASSERT(range_found->fr_nblks > 0); 1095 break; 1096 } 1097 range_found = avl_nearest(&dn->dn_ranges[i], idx, AVL_BEFORE); 1098 if (range_found && 1099 range_found->fr_blkid + range_found->fr_nblks > blkid) 1100 break; 1101 } 1102 mutex_exit(&dn->dn_mtx); 1103 return (i < TXG_SIZE); 1104 } 1105 1106 /* call from syncing context when we actually write/free space for this dnode */ 1107 void 1108 dnode_diduse_space(dnode_t *dn, int64_t space) 1109 { 1110 uint64_t sectors; 1111 1112 dprintf_dnode(dn, "dn=%p dnp=%p secphys=%llu space=%lld\n", 1113 dn, dn->dn_phys, 1114 (u_longlong_t)dn->dn_phys->dn_secphys, 1115 (longlong_t)space); 1116 1117 ASSERT(P2PHASE(space, 1<<DEV_BSHIFT) == 0); 1118 1119 mutex_enter(&dn->dn_mtx); 1120 if (space > 0) { 1121 sectors = space >> DEV_BSHIFT; 1122 ASSERT3U(dn->dn_phys->dn_secphys + sectors, >=, 1123 dn->dn_phys->dn_secphys); 1124 dn->dn_phys->dn_secphys += sectors; 1125 } else { 1126 sectors = -space >> DEV_BSHIFT; 1127 ASSERT3U(dn->dn_phys->dn_secphys, >=, sectors); 1128 dn->dn_phys->dn_secphys -= sectors; 1129 } 1130 mutex_exit(&dn->dn_mtx); 1131 } 1132 1133 /* 1134 * Call when we think we're going to write/free space in open context. 1135 * Be conservative (ie. OK to write less than this or free more than 1136 * this, but don't write more or free less). 1137 */ 1138 void 1139 dnode_willuse_space(dnode_t *dn, int64_t space, dmu_tx_t *tx) 1140 { 1141 objset_impl_t *os = dn->dn_objset; 1142 dsl_dataset_t *ds = os->os_dsl_dataset; 1143 1144 if (space > 0) 1145 space = spa_get_asize(os->os_spa, space); 1146 1147 if (ds) 1148 dsl_dir_willuse_space(ds->ds_dir, space, tx); 1149 1150 dmu_tx_willuse_space(tx, space); 1151 } 1152 1153 static int 1154 dnode_next_offset_level(dnode_t *dn, boolean_t hole, uint64_t *offset, 1155 int lvl, uint64_t blkfill) 1156 { 1157 dmu_buf_impl_t *db = NULL; 1158 void *data = NULL; 1159 uint64_t epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; 1160 uint64_t epb = 1ULL << epbs; 1161 uint64_t minfill, maxfill; 1162 int i, error, span; 1163 1164 dprintf("probing object %llu offset %llx level %d of %u\n", 1165 dn->dn_object, *offset, lvl, dn->dn_phys->dn_nlevels); 1166 1167 if (lvl == dn->dn_phys->dn_nlevels) { 1168 error = 0; 1169 epb = dn->dn_phys->dn_nblkptr; 1170 data = dn->dn_phys->dn_blkptr; 1171 } else { 1172 uint64_t blkid = dbuf_whichblock(dn, *offset) >> (epbs * lvl); 1173 error = dbuf_hold_impl(dn, lvl, blkid, TRUE, FTAG, &db); 1174 if (error) { 1175 if (error == ENOENT) 1176 return (hole ? 0 : ESRCH); 1177 return (error); 1178 } 1179 (void) dbuf_read(db, NULL, 1180 DB_RF_MUST_SUCCEED | DB_RF_HAVESTRUCT); 1181 data = db->db.db_data; 1182 } 1183 1184 if (lvl == 0) { 1185 dnode_phys_t *dnp = data; 1186 span = DNODE_SHIFT; 1187 ASSERT(dn->dn_type == DMU_OT_DNODE); 1188 1189 for (i = (*offset >> span) & (blkfill - 1); i < blkfill; i++) { 1190 if (!dnp[i].dn_type == hole) 1191 break; 1192 *offset += 1ULL << span; 1193 } 1194 if (i == blkfill) 1195 error = ESRCH; 1196 } else { 1197 blkptr_t *bp = data; 1198 span = (lvl - 1) * epbs + dn->dn_datablkshift; 1199 minfill = 0; 1200 maxfill = blkfill << ((lvl - 1) * epbs); 1201 1202 if (hole) 1203 maxfill--; 1204 else 1205 minfill++; 1206 1207 for (i = (*offset >> span) & ((1ULL << epbs) - 1); 1208 i < epb; i++) { 1209 if (bp[i].blk_fill >= minfill && 1210 bp[i].blk_fill <= maxfill) 1211 break; 1212 *offset += 1ULL << span; 1213 } 1214 if (i >= epb) 1215 error = ESRCH; 1216 } 1217 1218 if (db) 1219 dbuf_rele(db, FTAG); 1220 1221 return (error); 1222 } 1223 1224 /* 1225 * Find the next hole, data, or sparse region at or after *offset. 1226 * The value 'blkfill' tells us how many items we expect to find 1227 * in an L0 data block; this value is 1 for normal objects, 1228 * DNODES_PER_BLOCK for the meta dnode, and some fraction of 1229 * DNODES_PER_BLOCK when searching for sparse regions thereof. 1230 * Examples: 1231 * 1232 * dnode_next_offset(dn, hole, offset, 1, 1); 1233 * Finds the next hole/data in a file. 1234 * Used in dmu_offset_next(). 1235 * 1236 * dnode_next_offset(mdn, hole, offset, 0, DNODES_PER_BLOCK); 1237 * Finds the next free/allocated dnode an objset's meta-dnode. 1238 * Used in dmu_object_next(). 1239 * 1240 * dnode_next_offset(mdn, TRUE, offset, 2, DNODES_PER_BLOCK >> 2); 1241 * Finds the next L2 meta-dnode bp that's at most 1/4 full. 1242 * Used in dmu_object_alloc(). 1243 */ 1244 int 1245 dnode_next_offset(dnode_t *dn, boolean_t hole, uint64_t *offset, 1246 int minlvl, uint64_t blkfill) 1247 { 1248 int lvl, maxlvl; 1249 int error = 0; 1250 uint64_t initial_offset = *offset; 1251 1252 rw_enter(&dn->dn_struct_rwlock, RW_READER); 1253 1254 if (dn->dn_phys->dn_nlevels == 0) { 1255 rw_exit(&dn->dn_struct_rwlock); 1256 return (ESRCH); 1257 } 1258 1259 if (dn->dn_datablkshift == 0) { 1260 if (*offset < dn->dn_datablksz) { 1261 if (hole) 1262 *offset = dn->dn_datablksz; 1263 } else { 1264 error = ESRCH; 1265 } 1266 rw_exit(&dn->dn_struct_rwlock); 1267 return (error); 1268 } 1269 1270 maxlvl = dn->dn_phys->dn_nlevels; 1271 1272 for (lvl = minlvl; lvl <= maxlvl; lvl++) { 1273 error = dnode_next_offset_level(dn, hole, offset, lvl, blkfill); 1274 if (error == 0) 1275 break; 1276 } 1277 1278 while (--lvl >= minlvl && error == 0) 1279 error = dnode_next_offset_level(dn, hole, offset, lvl, blkfill); 1280 1281 rw_exit(&dn->dn_struct_rwlock); 1282 1283 if (initial_offset > *offset) 1284 return (ESRCH); 1285 1286 return (error); 1287 } 1288