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(IS_DNODE_DNODE(dn->dn_object) || dn->dn_dbuf); 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 kmem_cache_free(dnode_cache, dn); 311 } 312 313 void 314 dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs, 315 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 316 { 317 int i; 318 319 if (blocksize == 0) 320 blocksize = 1 << zfs_default_bs; 321 else if (blocksize > SPA_MAXBLOCKSIZE) 322 blocksize = SPA_MAXBLOCKSIZE; 323 else 324 blocksize = P2ROUNDUP(blocksize, SPA_MINBLOCKSIZE); 325 326 if (ibs == 0) 327 ibs = zfs_default_ibs; 328 329 ibs = MIN(MAX(ibs, DN_MIN_INDBLKSHIFT), DN_MAX_INDBLKSHIFT); 330 331 dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d\n", dn->dn_objset, 332 dn->dn_object, tx->tx_txg, blocksize, ibs); 333 334 ASSERT(dn->dn_type == DMU_OT_NONE); 335 ASSERT(bcmp(dn->dn_phys, &dnode_phys_zero, sizeof (dnode_phys_t)) == 0); 336 ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE); 337 ASSERT(ot != DMU_OT_NONE); 338 ASSERT3U(ot, <, DMU_OT_NUMTYPES); 339 ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) || 340 (bonustype != DMU_OT_NONE && bonuslen != 0)); 341 ASSERT3U(bonustype, <, DMU_OT_NUMTYPES); 342 ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN); 343 ASSERT(dn->dn_type == DMU_OT_NONE); 344 ASSERT3U(dn->dn_maxblkid, ==, 0); 345 ASSERT3U(dn->dn_allocated_txg, ==, 0); 346 ASSERT3U(dn->dn_assigned_txg, ==, 0); 347 ASSERT(refcount_is_zero(&dn->dn_tx_holds)); 348 ASSERT3U(refcount_count(&dn->dn_holds), <=, 1); 349 ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL); 350 351 for (i = 0; i < TXG_SIZE; i++) { 352 ASSERT3U(dn->dn_next_nlevels[i], ==, 0); 353 ASSERT3U(dn->dn_next_indblkshift[i], ==, 0); 354 ASSERT3U(dn->dn_dirtyblksz[i], ==, 0); 355 ASSERT3P(list_head(&dn->dn_dirty_dbufs[i]), ==, NULL); 356 ASSERT3U(avl_numnodes(&dn->dn_ranges[i]), ==, 0); 357 } 358 359 dn->dn_type = ot; 360 dnode_setdblksz(dn, blocksize); 361 dn->dn_indblkshift = ibs; 362 dn->dn_nlevels = 1; 363 dn->dn_nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT); 364 dn->dn_bonustype = bonustype; 365 dn->dn_bonuslen = bonuslen; 366 dn->dn_checksum = ZIO_CHECKSUM_INHERIT; 367 dn->dn_compress = ZIO_COMPRESS_INHERIT; 368 dn->dn_dirtyctx = 0; 369 370 dn->dn_free_txg = 0; 371 if (dn->dn_dirtyctx_firstset) { 372 kmem_free(dn->dn_dirtyctx_firstset, 1); 373 dn->dn_dirtyctx_firstset = NULL; 374 } 375 376 dn->dn_allocated_txg = tx->tx_txg; 377 dnode_setdirty(dn, tx); 378 } 379 380 void 381 dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, 382 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 383 { 384 dmu_buf_impl_t *db = NULL; 385 386 ASSERT3U(blocksize, >=, SPA_MINBLOCKSIZE); 387 ASSERT3U(blocksize, <=, SPA_MAXBLOCKSIZE); 388 ASSERT3U(blocksize % SPA_MINBLOCKSIZE, ==, 0); 389 ASSERT3P(list_head(&dn->dn_dbufs), ==, NULL); 390 ASSERT(!(dn->dn_object & DMU_PRIVATE_OBJECT) || dmu_tx_private_ok(tx)); 391 ASSERT(tx->tx_txg != 0); 392 ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) || 393 (bonustype != DMU_OT_NONE && bonuslen != 0)); 394 ASSERT3U(bonustype, <, DMU_OT_NUMTYPES); 395 ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN); 396 ASSERT(dn->dn_dirtyblksz[0] == 0); 397 ASSERT(dn->dn_dirtyblksz[1] == 0); 398 ASSERT(dn->dn_dirtyblksz[2] == 0); 399 ASSERT(dn->dn_dirtyblksz[3] == 0); 400 401 /* 402 * XXX I should really have a generation number to tell if we 403 * need to do this... 404 */ 405 if (blocksize != dn->dn_datablksz || 406 dn->dn_bonustype != bonustype || dn->dn_bonuslen != bonuslen) { 407 /* free all old data */ 408 dnode_free_range(dn, 0, -1ULL, tx); 409 } 410 411 /* change blocksize */ 412 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 413 dnode_setdblksz(dn, blocksize); 414 dnode_setdirty(dn, tx); 415 /* don't need dd_dirty_mtx, dnode is already dirty */ 416 ASSERT(dn->dn_dirtyblksz[tx->tx_txg&TXG_MASK] != 0); 417 dn->dn_dirtyblksz[tx->tx_txg&TXG_MASK] = blocksize; 418 rw_exit(&dn->dn_struct_rwlock); 419 420 /* change type */ 421 dn->dn_type = ot; 422 423 if (dn->dn_bonuslen != bonuslen) { 424 /* change bonus size */ 425 if (bonuslen == 0) 426 bonuslen = 1; /* XXX */ 427 db = dbuf_hold_bonus(dn, FTAG); 428 dbuf_read(db); 429 mutex_enter(&db->db_mtx); 430 ASSERT3U(db->db.db_size, ==, dn->dn_bonuslen); 431 ASSERT(db->db.db_data != NULL); 432 db->db.db_size = bonuslen; 433 mutex_exit(&db->db_mtx); 434 dbuf_dirty(db, tx); 435 } 436 437 /* change bonus size and type */ 438 mutex_enter(&dn->dn_mtx); 439 dn->dn_bonustype = bonustype; 440 dn->dn_bonuslen = bonuslen; 441 dn->dn_nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT); 442 dn->dn_checksum = ZIO_CHECKSUM_INHERIT; 443 dn->dn_compress = ZIO_COMPRESS_INHERIT; 444 ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR); 445 446 dn->dn_allocated_txg = tx->tx_txg; 447 mutex_exit(&dn->dn_mtx); 448 449 if (db) 450 dbuf_remove_ref(db, FTAG); 451 } 452 453 void 454 dnode_special_close(dnode_t *dn) 455 { 456 dnode_destroy(dn); 457 } 458 459 dnode_t * 460 dnode_special_open(objset_impl_t *os, dnode_phys_t *dnp, uint64_t object) 461 { 462 dnode_t *dn = dnode_create(os, dnp, NULL, object); 463 DNODE_VERIFY(dn); 464 return (dn); 465 } 466 467 static void 468 dnode_buf_pageout(dmu_buf_t *db, void *arg) 469 { 470 dnode_t **children_dnodes = arg; 471 int i; 472 int epb = db->db_size >> DNODE_SHIFT; 473 474 for (i = 0; i < epb; i++) { 475 dnode_t *dn = children_dnodes[i]; 476 int n; 477 478 if (dn == NULL) 479 continue; 480 #ifdef ZFS_DEBUG 481 /* 482 * If there are holds on this dnode, then there should 483 * be holds on the dnode's containing dbuf as well; thus 484 * it wouldn't be eligable for eviction and this function 485 * would not have been called. 486 */ 487 ASSERT(refcount_is_zero(&dn->dn_holds)); 488 ASSERT(list_head(&dn->dn_dbufs) == NULL); 489 ASSERT(refcount_is_zero(&dn->dn_tx_holds)); 490 491 for (n = 0; n < TXG_SIZE; n++) 492 ASSERT(dn->dn_dirtyblksz[n] == 0); 493 #endif 494 children_dnodes[i] = NULL; 495 dnode_destroy(dn); 496 } 497 kmem_free(children_dnodes, epb * sizeof (dnode_t *)); 498 } 499 500 /* 501 * Returns held dnode if the object number is valid, NULL if not. 502 * Note that this will succeed even for free dnodes. 503 */ 504 dnode_t * 505 dnode_hold_impl(objset_impl_t *os, uint64_t object, int flag, void *ref) 506 { 507 int epb, idx; 508 int drop_struct_lock = FALSE; 509 uint64_t blk; 510 dnode_t *mdn, *dn; 511 dmu_buf_impl_t *db; 512 dnode_t **children_dnodes; 513 514 if (object == 0 || object >= DN_MAX_OBJECT) 515 return (NULL); 516 517 mdn = os->os_meta_dnode; 518 519 DNODE_VERIFY(mdn); 520 521 if (!RW_WRITE_HELD(&mdn->dn_struct_rwlock)) { 522 rw_enter(&mdn->dn_struct_rwlock, RW_READER); 523 drop_struct_lock = TRUE; 524 } 525 526 blk = dbuf_whichblock(mdn, object * sizeof (dnode_phys_t)); 527 528 db = dbuf_hold(mdn, blk); 529 if (drop_struct_lock) 530 rw_exit(&mdn->dn_struct_rwlock); 531 dbuf_read(db); 532 533 ASSERT3U(db->db.db_size, >=, 1<<DNODE_SHIFT); 534 epb = db->db.db_size >> DNODE_SHIFT; 535 536 idx = object & (epb-1); 537 538 children_dnodes = dmu_buf_get_user(&db->db); 539 if (children_dnodes == NULL) { 540 dnode_t **winner; 541 children_dnodes = kmem_zalloc(epb * sizeof (dnode_t *), 542 KM_SLEEP); 543 if (winner = dmu_buf_set_user(&db->db, children_dnodes, NULL, 544 dnode_buf_pageout)) { 545 kmem_free(children_dnodes, epb * sizeof (dnode_t *)); 546 children_dnodes = winner; 547 } 548 } 549 550 if ((dn = children_dnodes[idx]) == NULL) { 551 dnode_t *winner; 552 dn = dnode_create(os, (dnode_phys_t *)db->db.db_data+idx, 553 db, object); 554 winner = atomic_cas_ptr(&children_dnodes[idx], NULL, dn); 555 if (winner != NULL) { 556 dnode_destroy(dn); 557 dn = winner; 558 } 559 } 560 561 mutex_enter(&dn->dn_mtx); 562 if (dn->dn_free_txg || 563 ((flag & DNODE_MUST_BE_ALLOCATED) && dn->dn_type == DMU_OT_NONE) || 564 ((flag & DNODE_MUST_BE_FREE) && dn->dn_type != DMU_OT_NONE)) { 565 mutex_exit(&dn->dn_mtx); 566 dbuf_rele(db); 567 return (NULL); 568 } 569 mutex_exit(&dn->dn_mtx); 570 571 if (refcount_add(&dn->dn_holds, ref) == 1) 572 dbuf_add_ref(db, dn); 573 574 DNODE_VERIFY(dn); 575 ASSERT3P(dn->dn_dbuf, ==, db); 576 ASSERT3U(dn->dn_object, ==, object); 577 dbuf_rele(db); 578 579 return (dn); 580 } 581 582 /* 583 * Return held dnode if the object is allocated, NULL if not. 584 */ 585 dnode_t * 586 dnode_hold(objset_impl_t *os, uint64_t object, void *ref) 587 { 588 return (dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, ref)); 589 } 590 591 void 592 dnode_add_ref(dnode_t *dn, void *ref) 593 { 594 ASSERT(refcount_count(&dn->dn_holds) > 0); 595 (void) refcount_add(&dn->dn_holds, ref); 596 } 597 598 void 599 dnode_rele(dnode_t *dn, void *ref) 600 { 601 uint64_t refs; 602 603 refs = refcount_remove(&dn->dn_holds, ref); 604 /* NOTE: the DNODE_DNODE does not have a dn_dbuf */ 605 if (refs == 0 && dn->dn_dbuf) 606 dbuf_remove_ref(dn->dn_dbuf, dn); 607 } 608 609 void 610 dnode_setdirty(dnode_t *dn, dmu_tx_t *tx) 611 { 612 objset_impl_t *os = dn->dn_objset; 613 uint64_t txg = tx->tx_txg; 614 615 if (IS_DNODE_DNODE(dn->dn_object)) 616 return; 617 618 DNODE_VERIFY(dn); 619 620 #ifdef ZFS_DEBUG 621 mutex_enter(&dn->dn_mtx); 622 ASSERT(dn->dn_phys->dn_type || dn->dn_allocated_txg); 623 /* ASSERT(dn->dn_free_txg == 0 || dn->dn_free_txg >= txg); */ 624 mutex_exit(&dn->dn_mtx); 625 #endif 626 627 mutex_enter(&os->os_lock); 628 629 /* 630 * If we are already marked dirty, we're done. 631 */ 632 if (dn->dn_dirtyblksz[txg&TXG_MASK] > 0) { 633 mutex_exit(&os->os_lock); 634 return; 635 } 636 637 ASSERT(!refcount_is_zero(&dn->dn_holds) || list_head(&dn->dn_dbufs)); 638 ASSERT(dn->dn_datablksz != 0); 639 dn->dn_dirtyblksz[txg&TXG_MASK] = dn->dn_datablksz; 640 641 dprintf_ds(os->os_dsl_dataset, "obj=%llu txg=%llu\n", 642 dn->dn_object, txg); 643 644 if (dn->dn_free_txg > 0 && dn->dn_free_txg <= txg) { 645 list_insert_tail(&os->os_free_dnodes[txg&TXG_MASK], dn); 646 } else { 647 list_insert_tail(&os->os_dirty_dnodes[txg&TXG_MASK], dn); 648 } 649 650 mutex_exit(&os->os_lock); 651 652 /* 653 * The dnode maintains a hold on its containing dbuf as 654 * long as there are holds on it. Each instantiated child 655 * dbuf maintaines a hold on the dnode. When the last child 656 * drops its hold, the dnode will drop its hold on the 657 * containing dbuf. We add a "dirty hold" here so that the 658 * dnode will hang around after we finish processing its 659 * children. 660 */ 661 (void) refcount_add(&dn->dn_holds, (void *)(uintptr_t)tx->tx_txg); 662 663 dbuf_dirty(dn->dn_dbuf, tx); 664 665 dsl_dataset_dirty(os->os_dsl_dataset, tx); 666 } 667 668 void 669 dnode_free(dnode_t *dn, dmu_tx_t *tx) 670 { 671 dprintf("dn=%p txg=%llu\n", dn, tx->tx_txg); 672 673 /* we should be the only holder... hopefully */ 674 /* ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); */ 675 676 mutex_enter(&dn->dn_mtx); 677 if (dn->dn_type == DMU_OT_NONE || dn->dn_free_txg) { 678 mutex_exit(&dn->dn_mtx); 679 return; 680 } 681 dn->dn_free_txg = tx->tx_txg; 682 mutex_exit(&dn->dn_mtx); 683 684 /* 685 * If the dnode is already dirty, it needs to be moved from 686 * the dirty list to the free list. 687 */ 688 mutex_enter(&dn->dn_objset->os_lock); 689 if (dn->dn_dirtyblksz[tx->tx_txg&TXG_MASK] > 0) { 690 list_remove( 691 &dn->dn_objset->os_dirty_dnodes[tx->tx_txg&TXG_MASK], dn); 692 list_insert_tail( 693 &dn->dn_objset->os_free_dnodes[tx->tx_txg&TXG_MASK], dn); 694 mutex_exit(&dn->dn_objset->os_lock); 695 } else { 696 mutex_exit(&dn->dn_objset->os_lock); 697 dnode_setdirty(dn, tx); 698 } 699 } 700 701 /* 702 * Try to change the block size for the indicated dnode. This can only 703 * succeed if there are no blocks allocated or dirty beyond first block 704 */ 705 int 706 dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx) 707 { 708 dmu_buf_impl_t *db, *db_next; 709 int have_db0 = FALSE; 710 int err = ENOTSUP; 711 712 if (size == 0) 713 size = SPA_MINBLOCKSIZE; 714 if (size > SPA_MAXBLOCKSIZE) 715 size = SPA_MAXBLOCKSIZE; 716 else 717 size = P2ROUNDUP(size, SPA_MINBLOCKSIZE); 718 719 if (ibs == 0) 720 ibs = dn->dn_indblkshift; 721 722 if (size >> SPA_MINBLOCKSHIFT == dn->dn_datablkszsec && 723 ibs == dn->dn_indblkshift) 724 return (0); 725 726 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 727 728 /* Check for any allocated blocks beyond the first */ 729 if (dn->dn_phys->dn_maxblkid != 0) 730 goto end; 731 732 /* 733 * Any buffers allocated for blocks beyond the first 734 * must be evictable/evicted, because they're the wrong size. 735 */ 736 mutex_enter(&dn->dn_dbufs_mtx); 737 /* 738 * Since we have the dn_dbufs_mtx, nothing can be 739 * removed from dn_dbufs. Since we have dn_struct_rwlock/w, 740 * nothing can be added to dn_dbufs. 741 */ 742 for (db = list_head(&dn->dn_dbufs); db; db = db_next) { 743 db_next = list_next(&dn->dn_dbufs, db); 744 745 if (db->db_blkid == 0) { 746 have_db0 = TRUE; 747 } else if (db->db_blkid != DB_BONUS_BLKID) { 748 mutex_exit(&dn->dn_dbufs_mtx); 749 goto end; 750 } 751 } 752 mutex_exit(&dn->dn_dbufs_mtx); 753 754 /* Fast-track if there is no data in the file */ 755 if (BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0]) && !have_db0) { 756 dnode_setdblksz(dn, size); 757 dn->dn_indblkshift = ibs; 758 dnode_setdirty(dn, tx); 759 /* don't need dd_dirty_mtx, dnode is already dirty */ 760 dn->dn_dirtyblksz[tx->tx_txg&TXG_MASK] = size; 761 dn->dn_next_indblkshift[tx->tx_txg&TXG_MASK] = ibs; 762 rw_exit(&dn->dn_struct_rwlock); 763 return (0); 764 } 765 766 /* obtain the old block */ 767 db = dbuf_hold(dn, 0); 768 769 dbuf_new_size(db, size, tx); 770 771 dnode_setdblksz(dn, size); 772 dn->dn_indblkshift = ibs; 773 /* don't need dd_dirty_mtx, dnode is already dirty */ 774 dn->dn_dirtyblksz[tx->tx_txg&TXG_MASK] = size; 775 dn->dn_next_indblkshift[tx->tx_txg&TXG_MASK] = ibs; 776 dbuf_rele(db); 777 778 err = 0; 779 end: 780 rw_exit(&dn->dn_struct_rwlock); 781 return (err); 782 } 783 784 uint64_t 785 dnode_max_nonzero_offset(dnode_t *dn) 786 { 787 if (dn->dn_phys->dn_maxblkid == 0 && 788 BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0])) 789 return (0); 790 else 791 return ((dn->dn_phys->dn_maxblkid+1) * dn->dn_datablksz); 792 } 793 794 void 795 dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx) 796 { 797 uint64_t txgoff = tx->tx_txg & TXG_MASK; 798 int drop_struct_lock = FALSE; 799 int epbs, old_nlevels, new_nlevels; 800 uint64_t sz; 801 802 if (blkid == DB_BONUS_BLKID) 803 return; 804 805 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) { 806 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 807 drop_struct_lock = TRUE; 808 } 809 810 if (blkid > dn->dn_maxblkid) 811 dn->dn_maxblkid = blkid; 812 813 /* 814 * Compute the number of levels necessary to support the 815 * new blkid. 816 */ 817 new_nlevels = 1; 818 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 819 820 for (sz = dn->dn_nblkptr; sz <= blkid && sz >= dn->dn_nblkptr; 821 sz <<= epbs) 822 new_nlevels++; 823 old_nlevels = dn->dn_nlevels; 824 825 if (new_nlevels > dn->dn_next_nlevels[txgoff]) 826 dn->dn_next_nlevels[txgoff] = new_nlevels; 827 828 if (new_nlevels > old_nlevels) { 829 dprintf("dn %p increasing nlevels from %u to %u\n", 830 dn, dn->dn_nlevels, new_nlevels); 831 dn->dn_nlevels = new_nlevels; 832 } 833 834 /* 835 * Dirty the left indirects. 836 * Note: the caller should have just dnode_use_space()'d one 837 * data block's worth, so we could subtract that out of 838 * dn_inflight_data to determine if there is any dirty data 839 * besides this block. 840 * We don't strictly need to dirty them unless there's 841 * *something* in the object (eg. on disk or dirty)... 842 */ 843 if (new_nlevels > old_nlevels) { 844 dmu_buf_impl_t *db = dbuf_hold_level(dn, old_nlevels, 0, FTAG); 845 dprintf("dn %p dirtying left indirects\n", dn); 846 dbuf_dirty(db, tx); 847 dbuf_remove_ref(db, FTAG); 848 } 849 #ifdef ZFS_DEBUG 850 else if (old_nlevels > 1 && new_nlevels > old_nlevels) { 851 dmu_buf_impl_t *db; 852 int i; 853 854 for (i = 0; i < dn->dn_nblkptr; i++) { 855 db = dbuf_hold_level(dn, old_nlevels-1, i, FTAG); 856 ASSERT(! 857 list_link_active(&db->db_dirty_node[txgoff])); 858 dbuf_remove_ref(db, FTAG); 859 } 860 } 861 #endif 862 863 dprintf("dn %p done\n", dn); 864 865 out: 866 if (drop_struct_lock) 867 rw_exit(&dn->dn_struct_rwlock); 868 } 869 870 void 871 dnode_clear_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx) 872 { 873 avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK]; 874 avl_index_t where; 875 free_range_t *rp; 876 free_range_t rp_tofind; 877 uint64_t endblk = blkid + nblks; 878 879 ASSERT(MUTEX_HELD(&dn->dn_mtx)); 880 ASSERT(nblks <= UINT64_MAX - blkid); /* no overflow */ 881 882 dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n", 883 blkid, nblks, tx->tx_txg); 884 rp_tofind.fr_blkid = blkid; 885 rp = avl_find(tree, &rp_tofind, &where); 886 if (rp == NULL) 887 rp = avl_nearest(tree, where, AVL_BEFORE); 888 if (rp == NULL) 889 rp = avl_nearest(tree, where, AVL_AFTER); 890 891 while (rp && (rp->fr_blkid <= blkid + nblks)) { 892 uint64_t fr_endblk = rp->fr_blkid + rp->fr_nblks; 893 free_range_t *nrp = AVL_NEXT(tree, rp); 894 895 if (blkid <= rp->fr_blkid && endblk >= fr_endblk) { 896 /* clear this entire range */ 897 avl_remove(tree, rp); 898 kmem_free(rp, sizeof (free_range_t)); 899 } else if (blkid <= rp->fr_blkid && 900 endblk > rp->fr_blkid && endblk < fr_endblk) { 901 /* clear the beginning of this range */ 902 rp->fr_blkid = endblk; 903 rp->fr_nblks = fr_endblk - endblk; 904 } else if (blkid > rp->fr_blkid && blkid < fr_endblk && 905 endblk >= fr_endblk) { 906 /* clear the end of this range */ 907 rp->fr_nblks = blkid - rp->fr_blkid; 908 } else if (blkid > rp->fr_blkid && endblk < fr_endblk) { 909 /* clear a chunk out of this range */ 910 free_range_t *new_rp = 911 kmem_alloc(sizeof (free_range_t), KM_SLEEP); 912 913 new_rp->fr_blkid = endblk; 914 new_rp->fr_nblks = fr_endblk - endblk; 915 avl_insert_here(tree, new_rp, rp, AVL_AFTER); 916 rp->fr_nblks = blkid - rp->fr_blkid; 917 } 918 /* there may be no overlap */ 919 rp = nrp; 920 } 921 } 922 923 void 924 dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx) 925 { 926 dmu_buf_impl_t *db; 927 uint64_t start, objsize, blkid, nblks; 928 int blkshift, blksz, tail, head, epbs; 929 int trunc = FALSE; 930 931 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 932 blksz = dn->dn_datablksz; 933 blkshift = dn->dn_datablkshift; 934 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 935 936 /* If the range is past the end of the file, this is a no-op */ 937 objsize = blksz * (dn->dn_maxblkid+1); 938 if (off >= objsize) 939 goto out; 940 if (len == -1ULL) { 941 len = UINT64_MAX - off; 942 trunc = TRUE; 943 } 944 945 /* 946 * First, block align the region to free: 947 */ 948 if (dn->dn_maxblkid == 0) { 949 if (off == 0) { 950 head = 0; 951 } else { 952 head = blksz - off; 953 ASSERT3U(head, >, 0); 954 } 955 start = off; 956 } else { 957 ASSERT(ISP2(blksz)); 958 head = P2NPHASE(off, blksz); 959 start = P2PHASE(off, blksz); 960 } 961 /* zero out any partial block data at the start of the range */ 962 if (head) { 963 ASSERT3U(start + head, ==, blksz); 964 if (len < head) 965 head = len; 966 if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off), TRUE, 967 FTAG, &db) == 0) { 968 caddr_t data; 969 970 /* don't dirty if it isn't on disk and isn't dirty */ 971 if (db->db_dirtied || 972 (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) { 973 rw_exit(&dn->dn_struct_rwlock); 974 dbuf_will_dirty(db, tx); 975 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 976 data = db->db.db_data; 977 bzero(data + start, head); 978 } 979 dbuf_remove_ref(db, FTAG); 980 } 981 off += head; 982 len -= head; 983 } 984 /* If the range was less than one block, we are done */ 985 if (len == 0) 986 goto out; 987 988 /* If the remaining range is past the end of the file, we are done */ 989 if (off > dn->dn_maxblkid << blkshift) 990 goto out; 991 992 if (off + len == UINT64_MAX) 993 tail = 0; 994 else 995 tail = P2PHASE(len, blksz); 996 997 ASSERT3U(P2PHASE(off, blksz), ==, 0); 998 /* zero out any partial block data at the end of the range */ 999 if (tail) { 1000 if (len < tail) 1001 tail = len; 1002 if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off+len), 1003 TRUE, FTAG, &db) == 0) { 1004 /* don't dirty if it isn't on disk and isn't dirty */ 1005 if (db->db_dirtied || 1006 (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) { 1007 rw_exit(&dn->dn_struct_rwlock); 1008 dbuf_will_dirty(db, tx); 1009 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 1010 bzero(db->db.db_data, tail); 1011 } 1012 dbuf_remove_ref(db, FTAG); 1013 } 1014 len -= tail; 1015 } 1016 /* If the range did not include a full block, we are done */ 1017 if (len == 0) 1018 goto out; 1019 1020 /* dirty the left indirects */ 1021 if (dn->dn_nlevels > 1 && off != 0) { 1022 db = dbuf_hold_level(dn, 1, 1023 (off - head) >> (blkshift + epbs), FTAG); 1024 dbuf_will_dirty(db, tx); 1025 dbuf_remove_ref(db, FTAG); 1026 } 1027 1028 /* dirty the right indirects */ 1029 if (dn->dn_nlevels > 1 && !trunc) { 1030 db = dbuf_hold_level(dn, 1, 1031 (off + len + tail - 1) >> (blkshift + epbs), FTAG); 1032 dbuf_will_dirty(db, tx); 1033 dbuf_remove_ref(db, FTAG); 1034 } 1035 1036 /* 1037 * Finally, add this range to the dnode range list, we 1038 * will finish up this free operation in the syncing phase. 1039 */ 1040 ASSERT(IS_P2ALIGNED(off, 1<<blkshift)); 1041 ASSERT(off + len == UINT64_MAX || IS_P2ALIGNED(len, 1<<blkshift)); 1042 blkid = off >> blkshift; 1043 nblks = len >> blkshift; 1044 1045 if (trunc) 1046 dn->dn_maxblkid = (blkid ? blkid - 1 : 0); 1047 1048 mutex_enter(&dn->dn_mtx); 1049 dnode_clear_range(dn, blkid, nblks, tx); 1050 { 1051 free_range_t *rp, *found; 1052 avl_index_t where; 1053 avl_tree_t *tree = &dn->dn_ranges[tx->tx_txg&TXG_MASK]; 1054 1055 /* Add new range to dn_ranges */ 1056 rp = kmem_alloc(sizeof (free_range_t), KM_SLEEP); 1057 rp->fr_blkid = blkid; 1058 rp->fr_nblks = nblks; 1059 found = avl_find(tree, rp, &where); 1060 ASSERT(found == NULL); 1061 avl_insert(tree, rp, where); 1062 dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n", 1063 blkid, nblks, tx->tx_txg); 1064 } 1065 mutex_exit(&dn->dn_mtx); 1066 1067 dbuf_free_range(dn, blkid, nblks, tx); 1068 dnode_setdirty(dn, tx); 1069 out: 1070 rw_exit(&dn->dn_struct_rwlock); 1071 } 1072 1073 /* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */ 1074 uint64_t 1075 dnode_block_freed(dnode_t *dn, uint64_t blkid) 1076 { 1077 free_range_t range_tofind; 1078 void *dp = spa_get_dsl(dn->dn_objset->os_spa); 1079 int i; 1080 1081 if (blkid == DB_BONUS_BLKID) 1082 return (FALSE); 1083 1084 /* 1085 * If we're in the process of opening the pool, dp will not be 1086 * set yet, but there shouldn't be anything dirty. 1087 */ 1088 if (dp == NULL) 1089 return (FALSE); 1090 1091 if (dn->dn_free_txg) 1092 return (TRUE); 1093 1094 /* 1095 * If dn_datablkshift is not set, then there's only a single 1096 * block, in which case there will never be a free range so it 1097 * won't matter. 1098 */ 1099 range_tofind.fr_blkid = blkid; 1100 mutex_enter(&dn->dn_mtx); 1101 for (i = 0; i < TXG_SIZE; i++) { 1102 free_range_t *range_found; 1103 avl_index_t idx; 1104 1105 range_found = avl_find(&dn->dn_ranges[i], &range_tofind, &idx); 1106 if (range_found) { 1107 ASSERT(range_found->fr_nblks > 0); 1108 break; 1109 } 1110 range_found = avl_nearest(&dn->dn_ranges[i], idx, AVL_BEFORE); 1111 if (range_found && 1112 range_found->fr_blkid + range_found->fr_nblks > blkid) 1113 break; 1114 } 1115 mutex_exit(&dn->dn_mtx); 1116 return (i < TXG_SIZE); 1117 } 1118 1119 /* call from syncing context when we actually write/free space for this dnode */ 1120 void 1121 dnode_diduse_space(dnode_t *dn, int64_t space) 1122 { 1123 uint64_t sectors; 1124 1125 dprintf_dnode(dn, "dn=%p dnp=%p secphys=%llu space=%lld\n", 1126 dn, dn->dn_phys, 1127 (u_longlong_t)dn->dn_phys->dn_secphys, 1128 (longlong_t)space); 1129 1130 ASSERT(P2PHASE(space, 1<<DEV_BSHIFT) == 0); 1131 1132 mutex_enter(&dn->dn_mtx); 1133 if (space > 0) { 1134 sectors = space >> DEV_BSHIFT; 1135 ASSERT3U(dn->dn_phys->dn_secphys + sectors, >=, 1136 dn->dn_phys->dn_secphys); 1137 dn->dn_phys->dn_secphys += sectors; 1138 } else { 1139 sectors = -space >> DEV_BSHIFT; 1140 ASSERT3U(dn->dn_phys->dn_secphys, >=, sectors); 1141 dn->dn_phys->dn_secphys -= sectors; 1142 } 1143 mutex_exit(&dn->dn_mtx); 1144 } 1145 1146 /* 1147 * Call when we think we're going to write/free space in open context. 1148 * Be conservative (ie. OK to write less than this or free more than 1149 * this, but don't write more or free less). 1150 */ 1151 void 1152 dnode_willuse_space(dnode_t *dn, int64_t space, dmu_tx_t *tx) 1153 { 1154 objset_impl_t *os = dn->dn_objset; 1155 dsl_dataset_t *ds = os->os_dsl_dataset; 1156 1157 if (space > 0) 1158 space = spa_get_asize(os->os_spa, space); 1159 1160 if (ds) 1161 dsl_dir_willuse_space(ds->ds_dir, space, tx); 1162 1163 dmu_tx_willuse_space(tx, space); 1164 } 1165 1166 static int 1167 dnode_next_offset_level(dnode_t *dn, boolean_t hole, uint64_t *offset, 1168 int lvl, uint64_t blkfill) 1169 { 1170 dmu_buf_impl_t *db = NULL; 1171 void *data = NULL; 1172 uint64_t epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; 1173 uint64_t epb = 1ULL << epbs; 1174 uint64_t minfill, maxfill; 1175 int i, error, span; 1176 1177 dprintf("probing object %llu offset %llx level %d of %u\n", 1178 dn->dn_object, *offset, lvl, dn->dn_phys->dn_nlevels); 1179 1180 if (lvl == dn->dn_phys->dn_nlevels) { 1181 error = 0; 1182 epb = dn->dn_phys->dn_nblkptr; 1183 data = dn->dn_phys->dn_blkptr; 1184 } else { 1185 uint64_t blkid = dbuf_whichblock(dn, *offset) >> (epbs * lvl); 1186 error = dbuf_hold_impl(dn, lvl, blkid, TRUE, FTAG, &db); 1187 if (error) { 1188 if (error == ENOENT) 1189 return (hole ? 0 : ESRCH); 1190 return (error); 1191 } 1192 dbuf_read_havestruct(db); 1193 data = db->db.db_data; 1194 } 1195 1196 if (lvl == 0) { 1197 dnode_phys_t *dnp = data; 1198 span = DNODE_SHIFT; 1199 ASSERT(dn->dn_type == DMU_OT_DNODE); 1200 1201 for (i = (*offset >> span) & (blkfill - 1); i < blkfill; i++) { 1202 if (!dnp[i].dn_type == hole) 1203 break; 1204 *offset += 1ULL << span; 1205 } 1206 if (i == blkfill) 1207 error = ESRCH; 1208 } else { 1209 blkptr_t *bp = data; 1210 span = (lvl - 1) * epbs + dn->dn_datablkshift; 1211 minfill = 0; 1212 maxfill = blkfill << ((lvl - 1) * epbs); 1213 1214 if (hole) 1215 maxfill--; 1216 else 1217 minfill++; 1218 1219 for (i = (*offset >> span) & ((1ULL << epbs) - 1); 1220 i < epb; i++) { 1221 if (bp[i].blk_fill >= minfill && 1222 bp[i].blk_fill <= maxfill) 1223 break; 1224 *offset += 1ULL << span; 1225 } 1226 if (i >= epb) 1227 error = ESRCH; 1228 } 1229 1230 if (db) 1231 dbuf_remove_ref(db, FTAG); 1232 1233 return (error); 1234 } 1235 1236 /* 1237 * Find the next hole, data, or sparse region at or after *offset. 1238 * The value 'blkfill' tells us how many items we expect to find 1239 * in an L0 data block; this value is 1 for normal objects, 1240 * DNODES_PER_BLOCK for the meta dnode, and some fraction of 1241 * DNODES_PER_BLOCK when searching for sparse regions thereof. 1242 * Examples: 1243 * 1244 * dnode_next_offset(dn, hole, offset, 1, 1); 1245 * Finds the next hole/data in a file. 1246 * Used in dmu_offset_next(). 1247 * 1248 * dnode_next_offset(mdn, hole, offset, 0, DNODES_PER_BLOCK); 1249 * Finds the next free/allocated dnode an objset's meta-dnode. 1250 * Used in dmu_object_next(). 1251 * 1252 * dnode_next_offset(mdn, TRUE, offset, 2, DNODES_PER_BLOCK >> 2); 1253 * Finds the next L2 meta-dnode bp that's at most 1/4 full. 1254 * Used in dmu_object_alloc(). 1255 */ 1256 int 1257 dnode_next_offset(dnode_t *dn, boolean_t hole, uint64_t *offset, 1258 int minlvl, uint64_t blkfill) 1259 { 1260 int lvl, maxlvl; 1261 int error = 0; 1262 uint64_t initial_offset = *offset; 1263 1264 rw_enter(&dn->dn_struct_rwlock, RW_READER); 1265 1266 if (dn->dn_phys->dn_nlevels == 0) { 1267 rw_exit(&dn->dn_struct_rwlock); 1268 return (ESRCH); 1269 } 1270 1271 if (dn->dn_datablkshift == 0) { 1272 if (*offset < dn->dn_datablksz) { 1273 if (hole) 1274 *offset = dn->dn_datablksz; 1275 } else { 1276 error = ESRCH; 1277 } 1278 rw_exit(&dn->dn_struct_rwlock); 1279 return (error); 1280 } 1281 1282 maxlvl = dn->dn_phys->dn_nlevels; 1283 1284 for (lvl = minlvl; lvl <= maxlvl; lvl++) { 1285 error = dnode_next_offset_level(dn, hole, offset, lvl, blkfill); 1286 if (error == 0) 1287 break; 1288 } 1289 1290 while (--lvl >= minlvl && error == 0) 1291 error = dnode_next_offset_level(dn, hole, offset, lvl, blkfill); 1292 1293 rw_exit(&dn->dn_struct_rwlock); 1294 1295 if (initial_offset > *offset) 1296 return (ESRCH); 1297 1298 return (error); 1299 } 1300