dnode.c revision 272134
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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright (c) 2012, 2014 by Delphix. All rights reserved. 24 */ 25 26#include <sys/zfs_context.h> 27#include <sys/dbuf.h> 28#include <sys/dnode.h> 29#include <sys/dmu.h> 30#include <sys/dmu_impl.h> 31#include <sys/dmu_tx.h> 32#include <sys/dmu_objset.h> 33#include <sys/dsl_dir.h> 34#include <sys/dsl_dataset.h> 35#include <sys/spa.h> 36#include <sys/zio.h> 37#include <sys/dmu_zfetch.h> 38#include <sys/range_tree.h> 39 40static kmem_cache_t *dnode_cache; 41/* 42 * Define DNODE_STATS to turn on statistic gathering. By default, it is only 43 * turned on when DEBUG is also defined. 44 */ 45#ifdef DEBUG 46#define DNODE_STATS 47#endif /* DEBUG */ 48 49#ifdef DNODE_STATS 50#define DNODE_STAT_ADD(stat) ((stat)++) 51#else 52#define DNODE_STAT_ADD(stat) /* nothing */ 53#endif /* DNODE_STATS */ 54 55static dnode_phys_t dnode_phys_zero; 56 57int zfs_default_bs = SPA_MINBLOCKSHIFT; 58int zfs_default_ibs = DN_MAX_INDBLKSHIFT; 59 60#ifdef sun 61static kmem_cbrc_t dnode_move(void *, void *, size_t, void *); 62#endif 63 64static int 65dbuf_compare(const void *x1, const void *x2) 66{ 67 const dmu_buf_impl_t *d1 = x1; 68 const dmu_buf_impl_t *d2 = x2; 69 70 if (d1->db_level < d2->db_level) { 71 return (-1); 72 } 73 if (d1->db_level > d2->db_level) { 74 return (1); 75 } 76 77 if (d1->db_blkid < d2->db_blkid) { 78 return (-1); 79 } 80 if (d1->db_blkid > d2->db_blkid) { 81 return (1); 82 } 83 84 if (d1->db_state < d2->db_state) { 85 return (-1); 86 } 87 if (d1->db_state > d2->db_state) { 88 return (1); 89 } 90 91 ASSERT3S(d1->db_state, !=, DB_SEARCH); 92 ASSERT3S(d2->db_state, !=, DB_SEARCH); 93 94 if ((uintptr_t)d1 < (uintptr_t)d2) { 95 return (-1); 96 } 97 if ((uintptr_t)d1 > (uintptr_t)d2) { 98 return (1); 99 } 100 return (0); 101} 102 103/* ARGSUSED */ 104static int 105dnode_cons(void *arg, void *unused, int kmflag) 106{ 107 dnode_t *dn = arg; 108 int i; 109 110 rw_init(&dn->dn_struct_rwlock, NULL, RW_DEFAULT, NULL); 111 mutex_init(&dn->dn_mtx, NULL, MUTEX_DEFAULT, NULL); 112 mutex_init(&dn->dn_dbufs_mtx, NULL, MUTEX_DEFAULT, NULL); 113 cv_init(&dn->dn_notxholds, NULL, CV_DEFAULT, NULL); 114 115 /* 116 * Every dbuf has a reference, and dropping a tracked reference is 117 * O(number of references), so don't track dn_holds. 118 */ 119 refcount_create_untracked(&dn->dn_holds); 120 refcount_create(&dn->dn_tx_holds); 121 list_link_init(&dn->dn_link); 122 123 bzero(&dn->dn_next_nblkptr[0], sizeof (dn->dn_next_nblkptr)); 124 bzero(&dn->dn_next_nlevels[0], sizeof (dn->dn_next_nlevels)); 125 bzero(&dn->dn_next_indblkshift[0], sizeof (dn->dn_next_indblkshift)); 126 bzero(&dn->dn_next_bonustype[0], sizeof (dn->dn_next_bonustype)); 127 bzero(&dn->dn_rm_spillblk[0], sizeof (dn->dn_rm_spillblk)); 128 bzero(&dn->dn_next_bonuslen[0], sizeof (dn->dn_next_bonuslen)); 129 bzero(&dn->dn_next_blksz[0], sizeof (dn->dn_next_blksz)); 130 131 for (i = 0; i < TXG_SIZE; i++) { 132 list_link_init(&dn->dn_dirty_link[i]); 133 dn->dn_free_ranges[i] = NULL; 134 list_create(&dn->dn_dirty_records[i], 135 sizeof (dbuf_dirty_record_t), 136 offsetof(dbuf_dirty_record_t, dr_dirty_node)); 137 } 138 139 dn->dn_allocated_txg = 0; 140 dn->dn_free_txg = 0; 141 dn->dn_assigned_txg = 0; 142 dn->dn_dirtyctx = 0; 143 dn->dn_dirtyctx_firstset = NULL; 144 dn->dn_bonus = NULL; 145 dn->dn_have_spill = B_FALSE; 146 dn->dn_zio = NULL; 147 dn->dn_oldused = 0; 148 dn->dn_oldflags = 0; 149 dn->dn_olduid = 0; 150 dn->dn_oldgid = 0; 151 dn->dn_newuid = 0; 152 dn->dn_newgid = 0; 153 dn->dn_id_flags = 0; 154 155 dn->dn_dbufs_count = 0; 156 dn->dn_unlisted_l0_blkid = 0; 157 avl_create(&dn->dn_dbufs, dbuf_compare, sizeof (dmu_buf_impl_t), 158 offsetof(dmu_buf_impl_t, db_link)); 159 160 dn->dn_moved = 0; 161 POINTER_INVALIDATE(&dn->dn_objset); 162 return (0); 163} 164 165/* ARGSUSED */ 166static void 167dnode_dest(void *arg, void *unused) 168{ 169 int i; 170 dnode_t *dn = arg; 171 172 rw_destroy(&dn->dn_struct_rwlock); 173 mutex_destroy(&dn->dn_mtx); 174 mutex_destroy(&dn->dn_dbufs_mtx); 175 cv_destroy(&dn->dn_notxholds); 176 refcount_destroy(&dn->dn_holds); 177 refcount_destroy(&dn->dn_tx_holds); 178 ASSERT(!list_link_active(&dn->dn_link)); 179 180 for (i = 0; i < TXG_SIZE; i++) { 181 ASSERT(!list_link_active(&dn->dn_dirty_link[i])); 182 ASSERT3P(dn->dn_free_ranges[i], ==, NULL); 183 list_destroy(&dn->dn_dirty_records[i]); 184 ASSERT0(dn->dn_next_nblkptr[i]); 185 ASSERT0(dn->dn_next_nlevels[i]); 186 ASSERT0(dn->dn_next_indblkshift[i]); 187 ASSERT0(dn->dn_next_bonustype[i]); 188 ASSERT0(dn->dn_rm_spillblk[i]); 189 ASSERT0(dn->dn_next_bonuslen[i]); 190 ASSERT0(dn->dn_next_blksz[i]); 191 } 192 193 ASSERT0(dn->dn_allocated_txg); 194 ASSERT0(dn->dn_free_txg); 195 ASSERT0(dn->dn_assigned_txg); 196 ASSERT0(dn->dn_dirtyctx); 197 ASSERT3P(dn->dn_dirtyctx_firstset, ==, NULL); 198 ASSERT3P(dn->dn_bonus, ==, NULL); 199 ASSERT(!dn->dn_have_spill); 200 ASSERT3P(dn->dn_zio, ==, NULL); 201 ASSERT0(dn->dn_oldused); 202 ASSERT0(dn->dn_oldflags); 203 ASSERT0(dn->dn_olduid); 204 ASSERT0(dn->dn_oldgid); 205 ASSERT0(dn->dn_newuid); 206 ASSERT0(dn->dn_newgid); 207 ASSERT0(dn->dn_id_flags); 208 209 ASSERT0(dn->dn_dbufs_count); 210 ASSERT0(dn->dn_unlisted_l0_blkid); 211 avl_destroy(&dn->dn_dbufs); 212} 213 214void 215dnode_init(void) 216{ 217 ASSERT(dnode_cache == NULL); 218 dnode_cache = kmem_cache_create("dnode_t", 219 sizeof (dnode_t), 220 0, dnode_cons, dnode_dest, NULL, NULL, NULL, 0); 221 kmem_cache_set_move(dnode_cache, dnode_move); 222} 223 224void 225dnode_fini(void) 226{ 227 kmem_cache_destroy(dnode_cache); 228 dnode_cache = NULL; 229} 230 231 232#ifdef ZFS_DEBUG 233void 234dnode_verify(dnode_t *dn) 235{ 236 int drop_struct_lock = FALSE; 237 238 ASSERT(dn->dn_phys); 239 ASSERT(dn->dn_objset); 240 ASSERT(dn->dn_handle->dnh_dnode == dn); 241 242 ASSERT(DMU_OT_IS_VALID(dn->dn_phys->dn_type)); 243 244 if (!(zfs_flags & ZFS_DEBUG_DNODE_VERIFY)) 245 return; 246 247 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) { 248 rw_enter(&dn->dn_struct_rwlock, RW_READER); 249 drop_struct_lock = TRUE; 250 } 251 if (dn->dn_phys->dn_type != DMU_OT_NONE || dn->dn_allocated_txg != 0) { 252 int i; 253 ASSERT3U(dn->dn_indblkshift, >=, 0); 254 ASSERT3U(dn->dn_indblkshift, <=, SPA_MAXBLOCKSHIFT); 255 if (dn->dn_datablkshift) { 256 ASSERT3U(dn->dn_datablkshift, >=, SPA_MINBLOCKSHIFT); 257 ASSERT3U(dn->dn_datablkshift, <=, SPA_MAXBLOCKSHIFT); 258 ASSERT3U(1<<dn->dn_datablkshift, ==, dn->dn_datablksz); 259 } 260 ASSERT3U(dn->dn_nlevels, <=, 30); 261 ASSERT(DMU_OT_IS_VALID(dn->dn_type)); 262 ASSERT3U(dn->dn_nblkptr, >=, 1); 263 ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR); 264 ASSERT3U(dn->dn_bonuslen, <=, DN_MAX_BONUSLEN); 265 ASSERT3U(dn->dn_datablksz, ==, 266 dn->dn_datablkszsec << SPA_MINBLOCKSHIFT); 267 ASSERT3U(ISP2(dn->dn_datablksz), ==, dn->dn_datablkshift != 0); 268 ASSERT3U((dn->dn_nblkptr - 1) * sizeof (blkptr_t) + 269 dn->dn_bonuslen, <=, DN_MAX_BONUSLEN); 270 for (i = 0; i < TXG_SIZE; i++) { 271 ASSERT3U(dn->dn_next_nlevels[i], <=, dn->dn_nlevels); 272 } 273 } 274 if (dn->dn_phys->dn_type != DMU_OT_NONE) 275 ASSERT3U(dn->dn_phys->dn_nlevels, <=, dn->dn_nlevels); 276 ASSERT(DMU_OBJECT_IS_SPECIAL(dn->dn_object) || dn->dn_dbuf != NULL); 277 if (dn->dn_dbuf != NULL) { 278 ASSERT3P(dn->dn_phys, ==, 279 (dnode_phys_t *)dn->dn_dbuf->db.db_data + 280 (dn->dn_object % (dn->dn_dbuf->db.db_size >> DNODE_SHIFT))); 281 } 282 if (drop_struct_lock) 283 rw_exit(&dn->dn_struct_rwlock); 284} 285#endif 286 287void 288dnode_byteswap(dnode_phys_t *dnp) 289{ 290 uint64_t *buf64 = (void*)&dnp->dn_blkptr; 291 int i; 292 293 if (dnp->dn_type == DMU_OT_NONE) { 294 bzero(dnp, sizeof (dnode_phys_t)); 295 return; 296 } 297 298 dnp->dn_datablkszsec = BSWAP_16(dnp->dn_datablkszsec); 299 dnp->dn_bonuslen = BSWAP_16(dnp->dn_bonuslen); 300 dnp->dn_maxblkid = BSWAP_64(dnp->dn_maxblkid); 301 dnp->dn_used = BSWAP_64(dnp->dn_used); 302 303 /* 304 * dn_nblkptr is only one byte, so it's OK to read it in either 305 * byte order. We can't read dn_bouslen. 306 */ 307 ASSERT(dnp->dn_indblkshift <= SPA_MAXBLOCKSHIFT); 308 ASSERT(dnp->dn_nblkptr <= DN_MAX_NBLKPTR); 309 for (i = 0; i < dnp->dn_nblkptr * sizeof (blkptr_t)/8; i++) 310 buf64[i] = BSWAP_64(buf64[i]); 311 312 /* 313 * OK to check dn_bonuslen for zero, because it won't matter if 314 * we have the wrong byte order. This is necessary because the 315 * dnode dnode is smaller than a regular dnode. 316 */ 317 if (dnp->dn_bonuslen != 0) { 318 /* 319 * Note that the bonus length calculated here may be 320 * longer than the actual bonus buffer. This is because 321 * we always put the bonus buffer after the last block 322 * pointer (instead of packing it against the end of the 323 * dnode buffer). 324 */ 325 int off = (dnp->dn_nblkptr-1) * sizeof (blkptr_t); 326 size_t len = DN_MAX_BONUSLEN - off; 327 ASSERT(DMU_OT_IS_VALID(dnp->dn_bonustype)); 328 dmu_object_byteswap_t byteswap = 329 DMU_OT_BYTESWAP(dnp->dn_bonustype); 330 dmu_ot_byteswap[byteswap].ob_func(dnp->dn_bonus + off, len); 331 } 332 333 /* Swap SPILL block if we have one */ 334 if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) 335 byteswap_uint64_array(&dnp->dn_spill, sizeof (blkptr_t)); 336 337} 338 339void 340dnode_buf_byteswap(void *vbuf, size_t size) 341{ 342 dnode_phys_t *buf = vbuf; 343 int i; 344 345 ASSERT3U(sizeof (dnode_phys_t), ==, (1<<DNODE_SHIFT)); 346 ASSERT((size & (sizeof (dnode_phys_t)-1)) == 0); 347 348 size >>= DNODE_SHIFT; 349 for (i = 0; i < size; i++) { 350 dnode_byteswap(buf); 351 buf++; 352 } 353} 354 355void 356dnode_setbonuslen(dnode_t *dn, int newsize, dmu_tx_t *tx) 357{ 358 ASSERT3U(refcount_count(&dn->dn_holds), >=, 1); 359 360 dnode_setdirty(dn, tx); 361 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 362 ASSERT3U(newsize, <=, DN_MAX_BONUSLEN - 363 (dn->dn_nblkptr-1) * sizeof (blkptr_t)); 364 dn->dn_bonuslen = newsize; 365 if (newsize == 0) 366 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = DN_ZERO_BONUSLEN; 367 else 368 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen; 369 rw_exit(&dn->dn_struct_rwlock); 370} 371 372void 373dnode_setbonus_type(dnode_t *dn, dmu_object_type_t newtype, dmu_tx_t *tx) 374{ 375 ASSERT3U(refcount_count(&dn->dn_holds), >=, 1); 376 dnode_setdirty(dn, tx); 377 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 378 dn->dn_bonustype = newtype; 379 dn->dn_next_bonustype[tx->tx_txg & TXG_MASK] = dn->dn_bonustype; 380 rw_exit(&dn->dn_struct_rwlock); 381} 382 383void 384dnode_rm_spill(dnode_t *dn, dmu_tx_t *tx) 385{ 386 ASSERT3U(refcount_count(&dn->dn_holds), >=, 1); 387 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock)); 388 dnode_setdirty(dn, tx); 389 dn->dn_rm_spillblk[tx->tx_txg&TXG_MASK] = DN_KILL_SPILLBLK; 390 dn->dn_have_spill = B_FALSE; 391} 392 393static void 394dnode_setdblksz(dnode_t *dn, int size) 395{ 396 ASSERT0(P2PHASE(size, SPA_MINBLOCKSIZE)); 397 ASSERT3U(size, <=, SPA_MAXBLOCKSIZE); 398 ASSERT3U(size, >=, SPA_MINBLOCKSIZE); 399 ASSERT3U(size >> SPA_MINBLOCKSHIFT, <, 400 1<<(sizeof (dn->dn_phys->dn_datablkszsec) * 8)); 401 dn->dn_datablksz = size; 402 dn->dn_datablkszsec = size >> SPA_MINBLOCKSHIFT; 403 dn->dn_datablkshift = ISP2(size) ? highbit64(size - 1) : 0; 404} 405 406static dnode_t * 407dnode_create(objset_t *os, dnode_phys_t *dnp, dmu_buf_impl_t *db, 408 uint64_t object, dnode_handle_t *dnh) 409{ 410 dnode_t *dn = kmem_cache_alloc(dnode_cache, KM_SLEEP); 411 412 ASSERT(!POINTER_IS_VALID(dn->dn_objset)); 413 dn->dn_moved = 0; 414 415 /* 416 * Defer setting dn_objset until the dnode is ready to be a candidate 417 * for the dnode_move() callback. 418 */ 419 dn->dn_object = object; 420 dn->dn_dbuf = db; 421 dn->dn_handle = dnh; 422 dn->dn_phys = dnp; 423 424 if (dnp->dn_datablkszsec) { 425 dnode_setdblksz(dn, dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT); 426 } else { 427 dn->dn_datablksz = 0; 428 dn->dn_datablkszsec = 0; 429 dn->dn_datablkshift = 0; 430 } 431 dn->dn_indblkshift = dnp->dn_indblkshift; 432 dn->dn_nlevels = dnp->dn_nlevels; 433 dn->dn_type = dnp->dn_type; 434 dn->dn_nblkptr = dnp->dn_nblkptr; 435 dn->dn_checksum = dnp->dn_checksum; 436 dn->dn_compress = dnp->dn_compress; 437 dn->dn_bonustype = dnp->dn_bonustype; 438 dn->dn_bonuslen = dnp->dn_bonuslen; 439 dn->dn_maxblkid = dnp->dn_maxblkid; 440 dn->dn_have_spill = ((dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) != 0); 441 dn->dn_id_flags = 0; 442 443 dmu_zfetch_init(&dn->dn_zfetch, dn); 444 445 ASSERT(DMU_OT_IS_VALID(dn->dn_phys->dn_type)); 446 447 mutex_enter(&os->os_lock); 448 list_insert_head(&os->os_dnodes, dn); 449 membar_producer(); 450 /* 451 * Everything else must be valid before assigning dn_objset makes the 452 * dnode eligible for dnode_move(). 453 */ 454 dn->dn_objset = os; 455 mutex_exit(&os->os_lock); 456 457 arc_space_consume(sizeof (dnode_t), ARC_SPACE_OTHER); 458 return (dn); 459} 460 461/* 462 * Caller must be holding the dnode handle, which is released upon return. 463 */ 464static void 465dnode_destroy(dnode_t *dn) 466{ 467 objset_t *os = dn->dn_objset; 468 469 ASSERT((dn->dn_id_flags & DN_ID_NEW_EXIST) == 0); 470 471 mutex_enter(&os->os_lock); 472 POINTER_INVALIDATE(&dn->dn_objset); 473 list_remove(&os->os_dnodes, dn); 474 mutex_exit(&os->os_lock); 475 476 /* the dnode can no longer move, so we can release the handle */ 477 zrl_remove(&dn->dn_handle->dnh_zrlock); 478 479 dn->dn_allocated_txg = 0; 480 dn->dn_free_txg = 0; 481 dn->dn_assigned_txg = 0; 482 483 dn->dn_dirtyctx = 0; 484 if (dn->dn_dirtyctx_firstset != NULL) { 485 kmem_free(dn->dn_dirtyctx_firstset, 1); 486 dn->dn_dirtyctx_firstset = NULL; 487 } 488 if (dn->dn_bonus != NULL) { 489 mutex_enter(&dn->dn_bonus->db_mtx); 490 dbuf_evict(dn->dn_bonus); 491 dn->dn_bonus = NULL; 492 } 493 dn->dn_zio = NULL; 494 495 dn->dn_have_spill = B_FALSE; 496 dn->dn_oldused = 0; 497 dn->dn_oldflags = 0; 498 dn->dn_olduid = 0; 499 dn->dn_oldgid = 0; 500 dn->dn_newuid = 0; 501 dn->dn_newgid = 0; 502 dn->dn_id_flags = 0; 503 dn->dn_unlisted_l0_blkid = 0; 504 505 dmu_zfetch_rele(&dn->dn_zfetch); 506 kmem_cache_free(dnode_cache, dn); 507 arc_space_return(sizeof (dnode_t), ARC_SPACE_OTHER); 508} 509 510void 511dnode_allocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, int ibs, 512 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 513{ 514 int i; 515 516 if (blocksize == 0) 517 blocksize = 1 << zfs_default_bs; 518 else if (blocksize > SPA_MAXBLOCKSIZE) 519 blocksize = SPA_MAXBLOCKSIZE; 520 else 521 blocksize = P2ROUNDUP(blocksize, SPA_MINBLOCKSIZE); 522 523 if (ibs == 0) 524 ibs = zfs_default_ibs; 525 526 ibs = MIN(MAX(ibs, DN_MIN_INDBLKSHIFT), DN_MAX_INDBLKSHIFT); 527 528 dprintf("os=%p obj=%llu txg=%llu blocksize=%d ibs=%d\n", dn->dn_objset, 529 dn->dn_object, tx->tx_txg, blocksize, ibs); 530 531 ASSERT(dn->dn_type == DMU_OT_NONE); 532 ASSERT(bcmp(dn->dn_phys, &dnode_phys_zero, sizeof (dnode_phys_t)) == 0); 533 ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE); 534 ASSERT(ot != DMU_OT_NONE); 535 ASSERT(DMU_OT_IS_VALID(ot)); 536 ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) || 537 (bonustype == DMU_OT_SA && bonuslen == 0) || 538 (bonustype != DMU_OT_NONE && bonuslen != 0)); 539 ASSERT(DMU_OT_IS_VALID(bonustype)); 540 ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN); 541 ASSERT(dn->dn_type == DMU_OT_NONE); 542 ASSERT0(dn->dn_maxblkid); 543 ASSERT0(dn->dn_allocated_txg); 544 ASSERT0(dn->dn_assigned_txg); 545 ASSERT(refcount_is_zero(&dn->dn_tx_holds)); 546 ASSERT3U(refcount_count(&dn->dn_holds), <=, 1); 547 ASSERT(avl_is_empty(&dn->dn_dbufs)); 548 549 for (i = 0; i < TXG_SIZE; i++) { 550 ASSERT0(dn->dn_next_nblkptr[i]); 551 ASSERT0(dn->dn_next_nlevels[i]); 552 ASSERT0(dn->dn_next_indblkshift[i]); 553 ASSERT0(dn->dn_next_bonuslen[i]); 554 ASSERT0(dn->dn_next_bonustype[i]); 555 ASSERT0(dn->dn_rm_spillblk[i]); 556 ASSERT0(dn->dn_next_blksz[i]); 557 ASSERT(!list_link_active(&dn->dn_dirty_link[i])); 558 ASSERT3P(list_head(&dn->dn_dirty_records[i]), ==, NULL); 559 ASSERT3P(dn->dn_free_ranges[i], ==, NULL); 560 } 561 562 dn->dn_type = ot; 563 dnode_setdblksz(dn, blocksize); 564 dn->dn_indblkshift = ibs; 565 dn->dn_nlevels = 1; 566 if (bonustype == DMU_OT_SA) /* Maximize bonus space for SA */ 567 dn->dn_nblkptr = 1; 568 else 569 dn->dn_nblkptr = 1 + 570 ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT); 571 dn->dn_bonustype = bonustype; 572 dn->dn_bonuslen = bonuslen; 573 dn->dn_checksum = ZIO_CHECKSUM_INHERIT; 574 dn->dn_compress = ZIO_COMPRESS_INHERIT; 575 dn->dn_dirtyctx = 0; 576 577 dn->dn_free_txg = 0; 578 if (dn->dn_dirtyctx_firstset) { 579 kmem_free(dn->dn_dirtyctx_firstset, 1); 580 dn->dn_dirtyctx_firstset = NULL; 581 } 582 583 dn->dn_allocated_txg = tx->tx_txg; 584 dn->dn_id_flags = 0; 585 586 dnode_setdirty(dn, tx); 587 dn->dn_next_indblkshift[tx->tx_txg & TXG_MASK] = ibs; 588 dn->dn_next_bonuslen[tx->tx_txg & TXG_MASK] = dn->dn_bonuslen; 589 dn->dn_next_bonustype[tx->tx_txg & TXG_MASK] = dn->dn_bonustype; 590 dn->dn_next_blksz[tx->tx_txg & TXG_MASK] = dn->dn_datablksz; 591} 592 593void 594dnode_reallocate(dnode_t *dn, dmu_object_type_t ot, int blocksize, 595 dmu_object_type_t bonustype, int bonuslen, dmu_tx_t *tx) 596{ 597 int nblkptr; 598 599 ASSERT3U(blocksize, >=, SPA_MINBLOCKSIZE); 600 ASSERT3U(blocksize, <=, SPA_MAXBLOCKSIZE); 601 ASSERT0(blocksize % SPA_MINBLOCKSIZE); 602 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT || dmu_tx_private_ok(tx)); 603 ASSERT(tx->tx_txg != 0); 604 ASSERT((bonustype == DMU_OT_NONE && bonuslen == 0) || 605 (bonustype != DMU_OT_NONE && bonuslen != 0) || 606 (bonustype == DMU_OT_SA && bonuslen == 0)); 607 ASSERT(DMU_OT_IS_VALID(bonustype)); 608 ASSERT3U(bonuslen, <=, DN_MAX_BONUSLEN); 609 610 /* clean up any unreferenced dbufs */ 611 dnode_evict_dbufs(dn); 612 613 dn->dn_id_flags = 0; 614 615 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 616 dnode_setdirty(dn, tx); 617 if (dn->dn_datablksz != blocksize) { 618 /* change blocksize */ 619 ASSERT(dn->dn_maxblkid == 0 && 620 (BP_IS_HOLE(&dn->dn_phys->dn_blkptr[0]) || 621 dnode_block_freed(dn, 0))); 622 dnode_setdblksz(dn, blocksize); 623 dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = blocksize; 624 } 625 if (dn->dn_bonuslen != bonuslen) 626 dn->dn_next_bonuslen[tx->tx_txg&TXG_MASK] = bonuslen; 627 628 if (bonustype == DMU_OT_SA) /* Maximize bonus space for SA */ 629 nblkptr = 1; 630 else 631 nblkptr = 1 + ((DN_MAX_BONUSLEN - bonuslen) >> SPA_BLKPTRSHIFT); 632 if (dn->dn_bonustype != bonustype) 633 dn->dn_next_bonustype[tx->tx_txg&TXG_MASK] = bonustype; 634 if (dn->dn_nblkptr != nblkptr) 635 dn->dn_next_nblkptr[tx->tx_txg&TXG_MASK] = nblkptr; 636 if (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR) { 637 dbuf_rm_spill(dn, tx); 638 dnode_rm_spill(dn, tx); 639 } 640 rw_exit(&dn->dn_struct_rwlock); 641 642 /* change type */ 643 dn->dn_type = ot; 644 645 /* change bonus size and type */ 646 mutex_enter(&dn->dn_mtx); 647 dn->dn_bonustype = bonustype; 648 dn->dn_bonuslen = bonuslen; 649 dn->dn_nblkptr = nblkptr; 650 dn->dn_checksum = ZIO_CHECKSUM_INHERIT; 651 dn->dn_compress = ZIO_COMPRESS_INHERIT; 652 ASSERT3U(dn->dn_nblkptr, <=, DN_MAX_NBLKPTR); 653 654 /* fix up the bonus db_size */ 655 if (dn->dn_bonus) { 656 dn->dn_bonus->db.db_size = 657 DN_MAX_BONUSLEN - (dn->dn_nblkptr-1) * sizeof (blkptr_t); 658 ASSERT(dn->dn_bonuslen <= dn->dn_bonus->db.db_size); 659 } 660 661 dn->dn_allocated_txg = tx->tx_txg; 662 mutex_exit(&dn->dn_mtx); 663} 664 665#ifdef DNODE_STATS 666static struct { 667 uint64_t dms_dnode_invalid; 668 uint64_t dms_dnode_recheck1; 669 uint64_t dms_dnode_recheck2; 670 uint64_t dms_dnode_special; 671 uint64_t dms_dnode_handle; 672 uint64_t dms_dnode_rwlock; 673 uint64_t dms_dnode_active; 674} dnode_move_stats; 675#endif /* DNODE_STATS */ 676 677static void 678dnode_move_impl(dnode_t *odn, dnode_t *ndn) 679{ 680 int i; 681 682 ASSERT(!RW_LOCK_HELD(&odn->dn_struct_rwlock)); 683 ASSERT(MUTEX_NOT_HELD(&odn->dn_mtx)); 684 ASSERT(MUTEX_NOT_HELD(&odn->dn_dbufs_mtx)); 685 ASSERT(!RW_LOCK_HELD(&odn->dn_zfetch.zf_rwlock)); 686 687 /* Copy fields. */ 688 ndn->dn_objset = odn->dn_objset; 689 ndn->dn_object = odn->dn_object; 690 ndn->dn_dbuf = odn->dn_dbuf; 691 ndn->dn_handle = odn->dn_handle; 692 ndn->dn_phys = odn->dn_phys; 693 ndn->dn_type = odn->dn_type; 694 ndn->dn_bonuslen = odn->dn_bonuslen; 695 ndn->dn_bonustype = odn->dn_bonustype; 696 ndn->dn_nblkptr = odn->dn_nblkptr; 697 ndn->dn_checksum = odn->dn_checksum; 698 ndn->dn_compress = odn->dn_compress; 699 ndn->dn_nlevels = odn->dn_nlevels; 700 ndn->dn_indblkshift = odn->dn_indblkshift; 701 ndn->dn_datablkshift = odn->dn_datablkshift; 702 ndn->dn_datablkszsec = odn->dn_datablkszsec; 703 ndn->dn_datablksz = odn->dn_datablksz; 704 ndn->dn_maxblkid = odn->dn_maxblkid; 705 bcopy(&odn->dn_next_nblkptr[0], &ndn->dn_next_nblkptr[0], 706 sizeof (odn->dn_next_nblkptr)); 707 bcopy(&odn->dn_next_nlevels[0], &ndn->dn_next_nlevels[0], 708 sizeof (odn->dn_next_nlevels)); 709 bcopy(&odn->dn_next_indblkshift[0], &ndn->dn_next_indblkshift[0], 710 sizeof (odn->dn_next_indblkshift)); 711 bcopy(&odn->dn_next_bonustype[0], &ndn->dn_next_bonustype[0], 712 sizeof (odn->dn_next_bonustype)); 713 bcopy(&odn->dn_rm_spillblk[0], &ndn->dn_rm_spillblk[0], 714 sizeof (odn->dn_rm_spillblk)); 715 bcopy(&odn->dn_next_bonuslen[0], &ndn->dn_next_bonuslen[0], 716 sizeof (odn->dn_next_bonuslen)); 717 bcopy(&odn->dn_next_blksz[0], &ndn->dn_next_blksz[0], 718 sizeof (odn->dn_next_blksz)); 719 for (i = 0; i < TXG_SIZE; i++) { 720 list_move_tail(&ndn->dn_dirty_records[i], 721 &odn->dn_dirty_records[i]); 722 } 723 bcopy(&odn->dn_free_ranges[0], &ndn->dn_free_ranges[0], 724 sizeof (odn->dn_free_ranges)); 725 ndn->dn_allocated_txg = odn->dn_allocated_txg; 726 ndn->dn_free_txg = odn->dn_free_txg; 727 ndn->dn_assigned_txg = odn->dn_assigned_txg; 728 ndn->dn_dirtyctx = odn->dn_dirtyctx; 729 ndn->dn_dirtyctx_firstset = odn->dn_dirtyctx_firstset; 730 ASSERT(refcount_count(&odn->dn_tx_holds) == 0); 731 refcount_transfer(&ndn->dn_holds, &odn->dn_holds); 732 ASSERT(avl_is_empty(&ndn->dn_dbufs)); 733 avl_swap(&ndn->dn_dbufs, &odn->dn_dbufs); 734 ndn->dn_dbufs_count = odn->dn_dbufs_count; 735 ndn->dn_unlisted_l0_blkid = odn->dn_unlisted_l0_blkid; 736 ndn->dn_bonus = odn->dn_bonus; 737 ndn->dn_have_spill = odn->dn_have_spill; 738 ndn->dn_zio = odn->dn_zio; 739 ndn->dn_oldused = odn->dn_oldused; 740 ndn->dn_oldflags = odn->dn_oldflags; 741 ndn->dn_olduid = odn->dn_olduid; 742 ndn->dn_oldgid = odn->dn_oldgid; 743 ndn->dn_newuid = odn->dn_newuid; 744 ndn->dn_newgid = odn->dn_newgid; 745 ndn->dn_id_flags = odn->dn_id_flags; 746 dmu_zfetch_init(&ndn->dn_zfetch, NULL); 747 list_move_tail(&ndn->dn_zfetch.zf_stream, &odn->dn_zfetch.zf_stream); 748 ndn->dn_zfetch.zf_dnode = odn->dn_zfetch.zf_dnode; 749 ndn->dn_zfetch.zf_stream_cnt = odn->dn_zfetch.zf_stream_cnt; 750 ndn->dn_zfetch.zf_alloc_fail = odn->dn_zfetch.zf_alloc_fail; 751 752 /* 753 * Update back pointers. Updating the handle fixes the back pointer of 754 * every descendant dbuf as well as the bonus dbuf. 755 */ 756 ASSERT(ndn->dn_handle->dnh_dnode == odn); 757 ndn->dn_handle->dnh_dnode = ndn; 758 if (ndn->dn_zfetch.zf_dnode == odn) { 759 ndn->dn_zfetch.zf_dnode = ndn; 760 } 761 762 /* 763 * Invalidate the original dnode by clearing all of its back pointers. 764 */ 765 odn->dn_dbuf = NULL; 766 odn->dn_handle = NULL; 767 avl_create(&odn->dn_dbufs, dbuf_compare, sizeof (dmu_buf_impl_t), 768 offsetof(dmu_buf_impl_t, db_link)); 769 odn->dn_dbufs_count = 0; 770 odn->dn_unlisted_l0_blkid = 0; 771 odn->dn_bonus = NULL; 772 odn->dn_zfetch.zf_dnode = NULL; 773 774 /* 775 * Set the low bit of the objset pointer to ensure that dnode_move() 776 * recognizes the dnode as invalid in any subsequent callback. 777 */ 778 POINTER_INVALIDATE(&odn->dn_objset); 779 780 /* 781 * Satisfy the destructor. 782 */ 783 for (i = 0; i < TXG_SIZE; i++) { 784 list_create(&odn->dn_dirty_records[i], 785 sizeof (dbuf_dirty_record_t), 786 offsetof(dbuf_dirty_record_t, dr_dirty_node)); 787 odn->dn_free_ranges[i] = NULL; 788 odn->dn_next_nlevels[i] = 0; 789 odn->dn_next_indblkshift[i] = 0; 790 odn->dn_next_bonustype[i] = 0; 791 odn->dn_rm_spillblk[i] = 0; 792 odn->dn_next_bonuslen[i] = 0; 793 odn->dn_next_blksz[i] = 0; 794 } 795 odn->dn_allocated_txg = 0; 796 odn->dn_free_txg = 0; 797 odn->dn_assigned_txg = 0; 798 odn->dn_dirtyctx = 0; 799 odn->dn_dirtyctx_firstset = NULL; 800 odn->dn_have_spill = B_FALSE; 801 odn->dn_zio = NULL; 802 odn->dn_oldused = 0; 803 odn->dn_oldflags = 0; 804 odn->dn_olduid = 0; 805 odn->dn_oldgid = 0; 806 odn->dn_newuid = 0; 807 odn->dn_newgid = 0; 808 odn->dn_id_flags = 0; 809 810 /* 811 * Mark the dnode. 812 */ 813 ndn->dn_moved = 1; 814 odn->dn_moved = (uint8_t)-1; 815} 816 817#ifdef sun 818#ifdef _KERNEL 819/*ARGSUSED*/ 820static kmem_cbrc_t 821dnode_move(void *buf, void *newbuf, size_t size, void *arg) 822{ 823 dnode_t *odn = buf, *ndn = newbuf; 824 objset_t *os; 825 int64_t refcount; 826 uint32_t dbufs; 827 828 /* 829 * The dnode is on the objset's list of known dnodes if the objset 830 * pointer is valid. We set the low bit of the objset pointer when 831 * freeing the dnode to invalidate it, and the memory patterns written 832 * by kmem (baddcafe and deadbeef) set at least one of the two low bits. 833 * A newly created dnode sets the objset pointer last of all to indicate 834 * that the dnode is known and in a valid state to be moved by this 835 * function. 836 */ 837 os = odn->dn_objset; 838 if (!POINTER_IS_VALID(os)) { 839 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_invalid); 840 return (KMEM_CBRC_DONT_KNOW); 841 } 842 843 /* 844 * Ensure that the objset does not go away during the move. 845 */ 846 rw_enter(&os_lock, RW_WRITER); 847 if (os != odn->dn_objset) { 848 rw_exit(&os_lock); 849 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_recheck1); 850 return (KMEM_CBRC_DONT_KNOW); 851 } 852 853 /* 854 * If the dnode is still valid, then so is the objset. We know that no 855 * valid objset can be freed while we hold os_lock, so we can safely 856 * ensure that the objset remains in use. 857 */ 858 mutex_enter(&os->os_lock); 859 860 /* 861 * Recheck the objset pointer in case the dnode was removed just before 862 * acquiring the lock. 863 */ 864 if (os != odn->dn_objset) { 865 mutex_exit(&os->os_lock); 866 rw_exit(&os_lock); 867 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_recheck2); 868 return (KMEM_CBRC_DONT_KNOW); 869 } 870 871 /* 872 * At this point we know that as long as we hold os->os_lock, the dnode 873 * cannot be freed and fields within the dnode can be safely accessed. 874 * The objset listing this dnode cannot go away as long as this dnode is 875 * on its list. 876 */ 877 rw_exit(&os_lock); 878 if (DMU_OBJECT_IS_SPECIAL(odn->dn_object)) { 879 mutex_exit(&os->os_lock); 880 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_special); 881 return (KMEM_CBRC_NO); 882 } 883 ASSERT(odn->dn_dbuf != NULL); /* only "special" dnodes have no parent */ 884 885 /* 886 * Lock the dnode handle to prevent the dnode from obtaining any new 887 * holds. This also prevents the descendant dbufs and the bonus dbuf 888 * from accessing the dnode, so that we can discount their holds. The 889 * handle is safe to access because we know that while the dnode cannot 890 * go away, neither can its handle. Once we hold dnh_zrlock, we can 891 * safely move any dnode referenced only by dbufs. 892 */ 893 if (!zrl_tryenter(&odn->dn_handle->dnh_zrlock)) { 894 mutex_exit(&os->os_lock); 895 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_handle); 896 return (KMEM_CBRC_LATER); 897 } 898 899 /* 900 * Ensure a consistent view of the dnode's holds and the dnode's dbufs. 901 * We need to guarantee that there is a hold for every dbuf in order to 902 * determine whether the dnode is actively referenced. Falsely matching 903 * a dbuf to an active hold would lead to an unsafe move. It's possible 904 * that a thread already having an active dnode hold is about to add a 905 * dbuf, and we can't compare hold and dbuf counts while the add is in 906 * progress. 907 */ 908 if (!rw_tryenter(&odn->dn_struct_rwlock, RW_WRITER)) { 909 zrl_exit(&odn->dn_handle->dnh_zrlock); 910 mutex_exit(&os->os_lock); 911 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_rwlock); 912 return (KMEM_CBRC_LATER); 913 } 914 915 /* 916 * A dbuf may be removed (evicted) without an active dnode hold. In that 917 * case, the dbuf count is decremented under the handle lock before the 918 * dbuf's hold is released. This order ensures that if we count the hold 919 * after the dbuf is removed but before its hold is released, we will 920 * treat the unmatched hold as active and exit safely. If we count the 921 * hold before the dbuf is removed, the hold is discounted, and the 922 * removal is blocked until the move completes. 923 */ 924 refcount = refcount_count(&odn->dn_holds); 925 ASSERT(refcount >= 0); 926 dbufs = odn->dn_dbufs_count; 927 928 /* We can't have more dbufs than dnode holds. */ 929 ASSERT3U(dbufs, <=, refcount); 930 DTRACE_PROBE3(dnode__move, dnode_t *, odn, int64_t, refcount, 931 uint32_t, dbufs); 932 933 if (refcount > dbufs) { 934 rw_exit(&odn->dn_struct_rwlock); 935 zrl_exit(&odn->dn_handle->dnh_zrlock); 936 mutex_exit(&os->os_lock); 937 DNODE_STAT_ADD(dnode_move_stats.dms_dnode_active); 938 return (KMEM_CBRC_LATER); 939 } 940 941 rw_exit(&odn->dn_struct_rwlock); 942 943 /* 944 * At this point we know that anyone with a hold on the dnode is not 945 * actively referencing it. The dnode is known and in a valid state to 946 * move. We're holding the locks needed to execute the critical section. 947 */ 948 dnode_move_impl(odn, ndn); 949 950 list_link_replace(&odn->dn_link, &ndn->dn_link); 951 /* If the dnode was safe to move, the refcount cannot have changed. */ 952 ASSERT(refcount == refcount_count(&ndn->dn_holds)); 953 ASSERT(dbufs == ndn->dn_dbufs_count); 954 zrl_exit(&ndn->dn_handle->dnh_zrlock); /* handle has moved */ 955 mutex_exit(&os->os_lock); 956 957 return (KMEM_CBRC_YES); 958} 959#endif /* _KERNEL */ 960#endif /* sun */ 961 962void 963dnode_special_close(dnode_handle_t *dnh) 964{ 965 dnode_t *dn = dnh->dnh_dnode; 966 967 /* 968 * Wait for final references to the dnode to clear. This can 969 * only happen if the arc is asyncronously evicting state that 970 * has a hold on this dnode while we are trying to evict this 971 * dnode. 972 */ 973 while (refcount_count(&dn->dn_holds) > 0) 974 delay(1); 975 zrl_add(&dnh->dnh_zrlock); 976 dnode_destroy(dn); /* implicit zrl_remove() */ 977 zrl_destroy(&dnh->dnh_zrlock); 978 dnh->dnh_dnode = NULL; 979} 980 981dnode_t * 982dnode_special_open(objset_t *os, dnode_phys_t *dnp, uint64_t object, 983 dnode_handle_t *dnh) 984{ 985 dnode_t *dn = dnode_create(os, dnp, NULL, object, dnh); 986 dnh->dnh_dnode = dn; 987 zrl_init(&dnh->dnh_zrlock); 988 DNODE_VERIFY(dn); 989 return (dn); 990} 991 992static void 993dnode_buf_pageout(dmu_buf_t *db, void *arg) 994{ 995 dnode_children_t *children_dnodes = arg; 996 int i; 997 int epb = db->db_size >> DNODE_SHIFT; 998 999 ASSERT(epb == children_dnodes->dnc_count); 1000 1001 for (i = 0; i < epb; i++) { 1002 dnode_handle_t *dnh = &children_dnodes->dnc_children[i]; 1003 dnode_t *dn; 1004 1005 /* 1006 * The dnode handle lock guards against the dnode moving to 1007 * another valid address, so there is no need here to guard 1008 * against changes to or from NULL. 1009 */ 1010 if (dnh->dnh_dnode == NULL) { 1011 zrl_destroy(&dnh->dnh_zrlock); 1012 continue; 1013 } 1014 1015 zrl_add(&dnh->dnh_zrlock); 1016 dn = dnh->dnh_dnode; 1017 /* 1018 * If there are holds on this dnode, then there should 1019 * be holds on the dnode's containing dbuf as well; thus 1020 * it wouldn't be eligible for eviction and this function 1021 * would not have been called. 1022 */ 1023 ASSERT(refcount_is_zero(&dn->dn_holds)); 1024 ASSERT(refcount_is_zero(&dn->dn_tx_holds)); 1025 1026 dnode_destroy(dn); /* implicit zrl_remove() */ 1027 zrl_destroy(&dnh->dnh_zrlock); 1028 dnh->dnh_dnode = NULL; 1029 } 1030 kmem_free(children_dnodes, sizeof (dnode_children_t) + 1031 epb * sizeof (dnode_handle_t)); 1032} 1033 1034/* 1035 * errors: 1036 * EINVAL - invalid object number. 1037 * EIO - i/o error. 1038 * succeeds even for free dnodes. 1039 */ 1040int 1041dnode_hold_impl(objset_t *os, uint64_t object, int flag, 1042 void *tag, dnode_t **dnp) 1043{ 1044 int epb, idx, err; 1045 int drop_struct_lock = FALSE; 1046 int type; 1047 uint64_t blk; 1048 dnode_t *mdn, *dn; 1049 dmu_buf_impl_t *db; 1050 dnode_children_t *children_dnodes; 1051 dnode_handle_t *dnh; 1052 1053 /* 1054 * If you are holding the spa config lock as writer, you shouldn't 1055 * be asking the DMU to do *anything* unless it's the root pool 1056 * which may require us to read from the root filesystem while 1057 * holding some (not all) of the locks as writer. 1058 */ 1059 ASSERT(spa_config_held(os->os_spa, SCL_ALL, RW_WRITER) == 0 || 1060 (spa_is_root(os->os_spa) && 1061 spa_config_held(os->os_spa, SCL_STATE, RW_WRITER))); 1062 1063 if (object == DMU_USERUSED_OBJECT || object == DMU_GROUPUSED_OBJECT) { 1064 dn = (object == DMU_USERUSED_OBJECT) ? 1065 DMU_USERUSED_DNODE(os) : DMU_GROUPUSED_DNODE(os); 1066 if (dn == NULL) 1067 return (SET_ERROR(ENOENT)); 1068 type = dn->dn_type; 1069 if ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE) 1070 return (SET_ERROR(ENOENT)); 1071 if ((flag & DNODE_MUST_BE_FREE) && type != DMU_OT_NONE) 1072 return (SET_ERROR(EEXIST)); 1073 DNODE_VERIFY(dn); 1074 (void) refcount_add(&dn->dn_holds, tag); 1075 *dnp = dn; 1076 return (0); 1077 } 1078 1079 if (object == 0 || object >= DN_MAX_OBJECT) 1080 return (SET_ERROR(EINVAL)); 1081 1082 mdn = DMU_META_DNODE(os); 1083 ASSERT(mdn->dn_object == DMU_META_DNODE_OBJECT); 1084 1085 DNODE_VERIFY(mdn); 1086 1087 if (!RW_WRITE_HELD(&mdn->dn_struct_rwlock)) { 1088 rw_enter(&mdn->dn_struct_rwlock, RW_READER); 1089 drop_struct_lock = TRUE; 1090 } 1091 1092 blk = dbuf_whichblock(mdn, object * sizeof (dnode_phys_t)); 1093 1094 db = dbuf_hold(mdn, blk, FTAG); 1095 if (drop_struct_lock) 1096 rw_exit(&mdn->dn_struct_rwlock); 1097 if (db == NULL) 1098 return (SET_ERROR(EIO)); 1099 err = dbuf_read(db, NULL, DB_RF_CANFAIL); 1100 if (err) { 1101 dbuf_rele(db, FTAG); 1102 return (err); 1103 } 1104 1105 ASSERT3U(db->db.db_size, >=, 1<<DNODE_SHIFT); 1106 epb = db->db.db_size >> DNODE_SHIFT; 1107 1108 idx = object & (epb-1); 1109 1110 ASSERT(DB_DNODE(db)->dn_type == DMU_OT_DNODE); 1111 children_dnodes = dmu_buf_get_user(&db->db); 1112 if (children_dnodes == NULL) { 1113 int i; 1114 dnode_children_t *winner; 1115 children_dnodes = kmem_zalloc(sizeof (dnode_children_t) + 1116 epb * sizeof (dnode_handle_t), KM_SLEEP); 1117 children_dnodes->dnc_count = epb; 1118 dnh = &children_dnodes->dnc_children[0]; 1119 for (i = 0; i < epb; i++) { 1120 zrl_init(&dnh[i].dnh_zrlock); 1121 dnh[i].dnh_dnode = NULL; 1122 } 1123 if (winner = dmu_buf_set_user(&db->db, children_dnodes, NULL, 1124 dnode_buf_pageout)) { 1125 1126 for (i = 0; i < epb; i++) { 1127 zrl_destroy(&dnh[i].dnh_zrlock); 1128 } 1129 1130 kmem_free(children_dnodes, sizeof (dnode_children_t) + 1131 epb * sizeof (dnode_handle_t)); 1132 children_dnodes = winner; 1133 } 1134 } 1135 ASSERT(children_dnodes->dnc_count == epb); 1136 1137 dnh = &children_dnodes->dnc_children[idx]; 1138 zrl_add(&dnh->dnh_zrlock); 1139 if ((dn = dnh->dnh_dnode) == NULL) { 1140 dnode_phys_t *phys = (dnode_phys_t *)db->db.db_data+idx; 1141 dnode_t *winner; 1142 1143 dn = dnode_create(os, phys, db, object, dnh); 1144 winner = atomic_cas_ptr(&dnh->dnh_dnode, NULL, dn); 1145 if (winner != NULL) { 1146 zrl_add(&dnh->dnh_zrlock); 1147 dnode_destroy(dn); /* implicit zrl_remove() */ 1148 dn = winner; 1149 } 1150 } 1151 1152 mutex_enter(&dn->dn_mtx); 1153 type = dn->dn_type; 1154 if (dn->dn_free_txg || 1155 ((flag & DNODE_MUST_BE_ALLOCATED) && type == DMU_OT_NONE) || 1156 ((flag & DNODE_MUST_BE_FREE) && 1157 (type != DMU_OT_NONE || !refcount_is_zero(&dn->dn_holds)))) { 1158 mutex_exit(&dn->dn_mtx); 1159 zrl_remove(&dnh->dnh_zrlock); 1160 dbuf_rele(db, FTAG); 1161 return (type == DMU_OT_NONE ? ENOENT : EEXIST); 1162 } 1163 mutex_exit(&dn->dn_mtx); 1164 1165 if (refcount_add(&dn->dn_holds, tag) == 1) 1166 dbuf_add_ref(db, dnh); 1167 /* Now we can rely on the hold to prevent the dnode from moving. */ 1168 zrl_remove(&dnh->dnh_zrlock); 1169 1170 DNODE_VERIFY(dn); 1171 ASSERT3P(dn->dn_dbuf, ==, db); 1172 ASSERT3U(dn->dn_object, ==, object); 1173 dbuf_rele(db, FTAG); 1174 1175 *dnp = dn; 1176 return (0); 1177} 1178 1179/* 1180 * Return held dnode if the object is allocated, NULL if not. 1181 */ 1182int 1183dnode_hold(objset_t *os, uint64_t object, void *tag, dnode_t **dnp) 1184{ 1185 return (dnode_hold_impl(os, object, DNODE_MUST_BE_ALLOCATED, tag, dnp)); 1186} 1187 1188/* 1189 * Can only add a reference if there is already at least one 1190 * reference on the dnode. Returns FALSE if unable to add a 1191 * new reference. 1192 */ 1193boolean_t 1194dnode_add_ref(dnode_t *dn, void *tag) 1195{ 1196 mutex_enter(&dn->dn_mtx); 1197 if (refcount_is_zero(&dn->dn_holds)) { 1198 mutex_exit(&dn->dn_mtx); 1199 return (FALSE); 1200 } 1201 VERIFY(1 < refcount_add(&dn->dn_holds, tag)); 1202 mutex_exit(&dn->dn_mtx); 1203 return (TRUE); 1204} 1205 1206void 1207dnode_rele(dnode_t *dn, void *tag) 1208{ 1209 uint64_t refs; 1210 /* Get while the hold prevents the dnode from moving. */ 1211 dmu_buf_impl_t *db = dn->dn_dbuf; 1212 dnode_handle_t *dnh = dn->dn_handle; 1213 1214 mutex_enter(&dn->dn_mtx); 1215 refs = refcount_remove(&dn->dn_holds, tag); 1216 mutex_exit(&dn->dn_mtx); 1217 1218 /* 1219 * It's unsafe to release the last hold on a dnode by dnode_rele() or 1220 * indirectly by dbuf_rele() while relying on the dnode handle to 1221 * prevent the dnode from moving, since releasing the last hold could 1222 * result in the dnode's parent dbuf evicting its dnode handles. For 1223 * that reason anyone calling dnode_rele() or dbuf_rele() without some 1224 * other direct or indirect hold on the dnode must first drop the dnode 1225 * handle. 1226 */ 1227 ASSERT(refs > 0 || dnh->dnh_zrlock.zr_owner != curthread); 1228 1229 /* NOTE: the DNODE_DNODE does not have a dn_dbuf */ 1230 if (refs == 0 && db != NULL) { 1231 /* 1232 * Another thread could add a hold to the dnode handle in 1233 * dnode_hold_impl() while holding the parent dbuf. Since the 1234 * hold on the parent dbuf prevents the handle from being 1235 * destroyed, the hold on the handle is OK. We can't yet assert 1236 * that the handle has zero references, but that will be 1237 * asserted anyway when the handle gets destroyed. 1238 */ 1239 dbuf_rele(db, dnh); 1240 } 1241} 1242 1243void 1244dnode_setdirty(dnode_t *dn, dmu_tx_t *tx) 1245{ 1246 objset_t *os = dn->dn_objset; 1247 uint64_t txg = tx->tx_txg; 1248 1249 if (DMU_OBJECT_IS_SPECIAL(dn->dn_object)) { 1250 dsl_dataset_dirty(os->os_dsl_dataset, tx); 1251 return; 1252 } 1253 1254 DNODE_VERIFY(dn); 1255 1256#ifdef ZFS_DEBUG 1257 mutex_enter(&dn->dn_mtx); 1258 ASSERT(dn->dn_phys->dn_type || dn->dn_allocated_txg); 1259 ASSERT(dn->dn_free_txg == 0 || dn->dn_free_txg >= txg); 1260 mutex_exit(&dn->dn_mtx); 1261#endif 1262 1263 /* 1264 * Determine old uid/gid when necessary 1265 */ 1266 dmu_objset_userquota_get_ids(dn, B_TRUE, tx); 1267 1268 mutex_enter(&os->os_lock); 1269 1270 /* 1271 * If we are already marked dirty, we're done. 1272 */ 1273 if (list_link_active(&dn->dn_dirty_link[txg & TXG_MASK])) { 1274 mutex_exit(&os->os_lock); 1275 return; 1276 } 1277 1278 ASSERT(!refcount_is_zero(&dn->dn_holds) || 1279 !avl_is_empty(&dn->dn_dbufs)); 1280 ASSERT(dn->dn_datablksz != 0); 1281 ASSERT0(dn->dn_next_bonuslen[txg&TXG_MASK]); 1282 ASSERT0(dn->dn_next_blksz[txg&TXG_MASK]); 1283 ASSERT0(dn->dn_next_bonustype[txg&TXG_MASK]); 1284 1285 dprintf_ds(os->os_dsl_dataset, "obj=%llu txg=%llu\n", 1286 dn->dn_object, txg); 1287 1288 if (dn->dn_free_txg > 0 && dn->dn_free_txg <= txg) { 1289 list_insert_tail(&os->os_free_dnodes[txg&TXG_MASK], dn); 1290 } else { 1291 list_insert_tail(&os->os_dirty_dnodes[txg&TXG_MASK], dn); 1292 } 1293 1294 mutex_exit(&os->os_lock); 1295 1296 /* 1297 * The dnode maintains a hold on its containing dbuf as 1298 * long as there are holds on it. Each instantiated child 1299 * dbuf maintains a hold on the dnode. When the last child 1300 * drops its hold, the dnode will drop its hold on the 1301 * containing dbuf. We add a "dirty hold" here so that the 1302 * dnode will hang around after we finish processing its 1303 * children. 1304 */ 1305 VERIFY(dnode_add_ref(dn, (void *)(uintptr_t)tx->tx_txg)); 1306 1307 (void) dbuf_dirty(dn->dn_dbuf, tx); 1308 1309 dsl_dataset_dirty(os->os_dsl_dataset, tx); 1310} 1311 1312void 1313dnode_free(dnode_t *dn, dmu_tx_t *tx) 1314{ 1315 int txgoff = tx->tx_txg & TXG_MASK; 1316 1317 dprintf("dn=%p txg=%llu\n", dn, tx->tx_txg); 1318 1319 /* we should be the only holder... hopefully */ 1320 /* ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); */ 1321 1322 mutex_enter(&dn->dn_mtx); 1323 if (dn->dn_type == DMU_OT_NONE || dn->dn_free_txg) { 1324 mutex_exit(&dn->dn_mtx); 1325 return; 1326 } 1327 dn->dn_free_txg = tx->tx_txg; 1328 mutex_exit(&dn->dn_mtx); 1329 1330 /* 1331 * If the dnode is already dirty, it needs to be moved from 1332 * the dirty list to the free list. 1333 */ 1334 mutex_enter(&dn->dn_objset->os_lock); 1335 if (list_link_active(&dn->dn_dirty_link[txgoff])) { 1336 list_remove(&dn->dn_objset->os_dirty_dnodes[txgoff], dn); 1337 list_insert_tail(&dn->dn_objset->os_free_dnodes[txgoff], dn); 1338 mutex_exit(&dn->dn_objset->os_lock); 1339 } else { 1340 mutex_exit(&dn->dn_objset->os_lock); 1341 dnode_setdirty(dn, tx); 1342 } 1343} 1344 1345/* 1346 * Try to change the block size for the indicated dnode. This can only 1347 * succeed if there are no blocks allocated or dirty beyond first block 1348 */ 1349int 1350dnode_set_blksz(dnode_t *dn, uint64_t size, int ibs, dmu_tx_t *tx) 1351{ 1352 dmu_buf_impl_t *db; 1353 int err; 1354 1355 if (size == 0) 1356 size = SPA_MINBLOCKSIZE; 1357 if (size > SPA_MAXBLOCKSIZE) 1358 size = SPA_MAXBLOCKSIZE; 1359 else 1360 size = P2ROUNDUP(size, SPA_MINBLOCKSIZE); 1361 1362 if (ibs == dn->dn_indblkshift) 1363 ibs = 0; 1364 1365 if (size >> SPA_MINBLOCKSHIFT == dn->dn_datablkszsec && ibs == 0) 1366 return (0); 1367 1368 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 1369 1370 /* Check for any allocated blocks beyond the first */ 1371 if (dn->dn_maxblkid != 0) 1372 goto fail; 1373 1374 mutex_enter(&dn->dn_dbufs_mtx); 1375 for (db = avl_first(&dn->dn_dbufs); db != NULL; 1376 db = AVL_NEXT(&dn->dn_dbufs, db)) { 1377 if (db->db_blkid != 0 && db->db_blkid != DMU_BONUS_BLKID && 1378 db->db_blkid != DMU_SPILL_BLKID) { 1379 mutex_exit(&dn->dn_dbufs_mtx); 1380 goto fail; 1381 } 1382 } 1383 mutex_exit(&dn->dn_dbufs_mtx); 1384 1385 if (ibs && dn->dn_nlevels != 1) 1386 goto fail; 1387 1388 /* resize the old block */ 1389 err = dbuf_hold_impl(dn, 0, 0, TRUE, FTAG, &db); 1390 if (err == 0) 1391 dbuf_new_size(db, size, tx); 1392 else if (err != ENOENT) 1393 goto fail; 1394 1395 dnode_setdblksz(dn, size); 1396 dnode_setdirty(dn, tx); 1397 dn->dn_next_blksz[tx->tx_txg&TXG_MASK] = size; 1398 if (ibs) { 1399 dn->dn_indblkshift = ibs; 1400 dn->dn_next_indblkshift[tx->tx_txg&TXG_MASK] = ibs; 1401 } 1402 /* rele after we have fixed the blocksize in the dnode */ 1403 if (db) 1404 dbuf_rele(db, FTAG); 1405 1406 rw_exit(&dn->dn_struct_rwlock); 1407 return (0); 1408 1409fail: 1410 rw_exit(&dn->dn_struct_rwlock); 1411 return (SET_ERROR(ENOTSUP)); 1412} 1413 1414/* read-holding callers must not rely on the lock being continuously held */ 1415void 1416dnode_new_blkid(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx, boolean_t have_read) 1417{ 1418 uint64_t txgoff = tx->tx_txg & TXG_MASK; 1419 int epbs, new_nlevels; 1420 uint64_t sz; 1421 1422 ASSERT(blkid != DMU_BONUS_BLKID); 1423 1424 ASSERT(have_read ? 1425 RW_READ_HELD(&dn->dn_struct_rwlock) : 1426 RW_WRITE_HELD(&dn->dn_struct_rwlock)); 1427 1428 /* 1429 * if we have a read-lock, check to see if we need to do any work 1430 * before upgrading to a write-lock. 1431 */ 1432 if (have_read) { 1433 if (blkid <= dn->dn_maxblkid) 1434 return; 1435 1436 if (!rw_tryupgrade(&dn->dn_struct_rwlock)) { 1437 rw_exit(&dn->dn_struct_rwlock); 1438 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 1439 } 1440 } 1441 1442 if (blkid <= dn->dn_maxblkid) 1443 goto out; 1444 1445 dn->dn_maxblkid = blkid; 1446 1447 /* 1448 * Compute the number of levels necessary to support the new maxblkid. 1449 */ 1450 new_nlevels = 1; 1451 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 1452 for (sz = dn->dn_nblkptr; 1453 sz <= blkid && sz >= dn->dn_nblkptr; sz <<= epbs) 1454 new_nlevels++; 1455 1456 if (new_nlevels > dn->dn_nlevels) { 1457 int old_nlevels = dn->dn_nlevels; 1458 dmu_buf_impl_t *db; 1459 list_t *list; 1460 dbuf_dirty_record_t *new, *dr, *dr_next; 1461 1462 dn->dn_nlevels = new_nlevels; 1463 1464 ASSERT3U(new_nlevels, >, dn->dn_next_nlevels[txgoff]); 1465 dn->dn_next_nlevels[txgoff] = new_nlevels; 1466 1467 /* dirty the left indirects */ 1468 db = dbuf_hold_level(dn, old_nlevels, 0, FTAG); 1469 ASSERT(db != NULL); 1470 new = dbuf_dirty(db, tx); 1471 dbuf_rele(db, FTAG); 1472 1473 /* transfer the dirty records to the new indirect */ 1474 mutex_enter(&dn->dn_mtx); 1475 mutex_enter(&new->dt.di.dr_mtx); 1476 list = &dn->dn_dirty_records[txgoff]; 1477 for (dr = list_head(list); dr; dr = dr_next) { 1478 dr_next = list_next(&dn->dn_dirty_records[txgoff], dr); 1479 if (dr->dr_dbuf->db_level != new_nlevels-1 && 1480 dr->dr_dbuf->db_blkid != DMU_BONUS_BLKID && 1481 dr->dr_dbuf->db_blkid != DMU_SPILL_BLKID) { 1482 ASSERT(dr->dr_dbuf->db_level == old_nlevels-1); 1483 list_remove(&dn->dn_dirty_records[txgoff], dr); 1484 list_insert_tail(&new->dt.di.dr_children, dr); 1485 dr->dr_parent = new; 1486 } 1487 } 1488 mutex_exit(&new->dt.di.dr_mtx); 1489 mutex_exit(&dn->dn_mtx); 1490 } 1491 1492out: 1493 if (have_read) 1494 rw_downgrade(&dn->dn_struct_rwlock); 1495} 1496 1497void 1498dnode_free_range(dnode_t *dn, uint64_t off, uint64_t len, dmu_tx_t *tx) 1499{ 1500 dmu_buf_impl_t *db; 1501 uint64_t blkoff, blkid, nblks; 1502 int blksz, blkshift, head, tail; 1503 int trunc = FALSE; 1504 int epbs; 1505 1506 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 1507 blksz = dn->dn_datablksz; 1508 blkshift = dn->dn_datablkshift; 1509 epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 1510 1511 if (len == DMU_OBJECT_END) { 1512 len = UINT64_MAX - off; 1513 trunc = TRUE; 1514 } 1515 1516 /* 1517 * First, block align the region to free: 1518 */ 1519 if (ISP2(blksz)) { 1520 head = P2NPHASE(off, blksz); 1521 blkoff = P2PHASE(off, blksz); 1522 if ((off >> blkshift) > dn->dn_maxblkid) 1523 goto out; 1524 } else { 1525 ASSERT(dn->dn_maxblkid == 0); 1526 if (off == 0 && len >= blksz) { 1527 /* 1528 * Freeing the whole block; fast-track this request. 1529 * Note that we won't dirty any indirect blocks, 1530 * which is fine because we will be freeing the entire 1531 * file and thus all indirect blocks will be freed 1532 * by free_children(). 1533 */ 1534 blkid = 0; 1535 nblks = 1; 1536 goto done; 1537 } else if (off >= blksz) { 1538 /* Freeing past end-of-data */ 1539 goto out; 1540 } else { 1541 /* Freeing part of the block. */ 1542 head = blksz - off; 1543 ASSERT3U(head, >, 0); 1544 } 1545 blkoff = off; 1546 } 1547 /* zero out any partial block data at the start of the range */ 1548 if (head) { 1549 ASSERT3U(blkoff + head, ==, blksz); 1550 if (len < head) 1551 head = len; 1552 if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off), TRUE, 1553 FTAG, &db) == 0) { 1554 caddr_t data; 1555 1556 /* don't dirty if it isn't on disk and isn't dirty */ 1557 if (db->db_last_dirty || 1558 (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) { 1559 rw_exit(&dn->dn_struct_rwlock); 1560 dmu_buf_will_dirty(&db->db, tx); 1561 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 1562 data = db->db.db_data; 1563 bzero(data + blkoff, head); 1564 } 1565 dbuf_rele(db, FTAG); 1566 } 1567 off += head; 1568 len -= head; 1569 } 1570 1571 /* If the range was less than one block, we're done */ 1572 if (len == 0) 1573 goto out; 1574 1575 /* If the remaining range is past end of file, we're done */ 1576 if ((off >> blkshift) > dn->dn_maxblkid) 1577 goto out; 1578 1579 ASSERT(ISP2(blksz)); 1580 if (trunc) 1581 tail = 0; 1582 else 1583 tail = P2PHASE(len, blksz); 1584 1585 ASSERT0(P2PHASE(off, blksz)); 1586 /* zero out any partial block data at the end of the range */ 1587 if (tail) { 1588 if (len < tail) 1589 tail = len; 1590 if (dbuf_hold_impl(dn, 0, dbuf_whichblock(dn, off+len), 1591 TRUE, FTAG, &db) == 0) { 1592 /* don't dirty if not on disk and not dirty */ 1593 if (db->db_last_dirty || 1594 (db->db_blkptr && !BP_IS_HOLE(db->db_blkptr))) { 1595 rw_exit(&dn->dn_struct_rwlock); 1596 dmu_buf_will_dirty(&db->db, tx); 1597 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 1598 bzero(db->db.db_data, tail); 1599 } 1600 dbuf_rele(db, FTAG); 1601 } 1602 len -= tail; 1603 } 1604 1605 /* If the range did not include a full block, we are done */ 1606 if (len == 0) 1607 goto out; 1608 1609 ASSERT(IS_P2ALIGNED(off, blksz)); 1610 ASSERT(trunc || IS_P2ALIGNED(len, blksz)); 1611 blkid = off >> blkshift; 1612 nblks = len >> blkshift; 1613 if (trunc) 1614 nblks += 1; 1615 1616 /* 1617 * Dirty the first and last indirect blocks, as they (and/or their 1618 * parents) will need to be written out if they were only 1619 * partially freed. Interior indirect blocks will be themselves freed, 1620 * by free_children(), so they need not be dirtied. Note that these 1621 * interior blocks have already been prefetched by dmu_tx_hold_free(). 1622 */ 1623 if (dn->dn_nlevels > 1) { 1624 uint64_t first, last; 1625 1626 first = blkid >> epbs; 1627 if (db = dbuf_hold_level(dn, 1, first, FTAG)) { 1628 dmu_buf_will_dirty(&db->db, tx); 1629 dbuf_rele(db, FTAG); 1630 } 1631 if (trunc) 1632 last = dn->dn_maxblkid >> epbs; 1633 else 1634 last = (blkid + nblks - 1) >> epbs; 1635 if (last > first && (db = dbuf_hold_level(dn, 1, last, FTAG))) { 1636 dmu_buf_will_dirty(&db->db, tx); 1637 dbuf_rele(db, FTAG); 1638 } 1639 } 1640 1641done: 1642 /* 1643 * Add this range to the dnode range list. 1644 * We will finish up this free operation in the syncing phase. 1645 */ 1646 mutex_enter(&dn->dn_mtx); 1647 int txgoff = tx->tx_txg & TXG_MASK; 1648 if (dn->dn_free_ranges[txgoff] == NULL) { 1649 dn->dn_free_ranges[txgoff] = 1650 range_tree_create(NULL, NULL, &dn->dn_mtx); 1651 } 1652 range_tree_clear(dn->dn_free_ranges[txgoff], blkid, nblks); 1653 range_tree_add(dn->dn_free_ranges[txgoff], blkid, nblks); 1654 dprintf_dnode(dn, "blkid=%llu nblks=%llu txg=%llu\n", 1655 blkid, nblks, tx->tx_txg); 1656 mutex_exit(&dn->dn_mtx); 1657 1658 dbuf_free_range(dn, blkid, blkid + nblks - 1, tx); 1659 dnode_setdirty(dn, tx); 1660out: 1661 1662 rw_exit(&dn->dn_struct_rwlock); 1663} 1664 1665static boolean_t 1666dnode_spill_freed(dnode_t *dn) 1667{ 1668 int i; 1669 1670 mutex_enter(&dn->dn_mtx); 1671 for (i = 0; i < TXG_SIZE; i++) { 1672 if (dn->dn_rm_spillblk[i] == DN_KILL_SPILLBLK) 1673 break; 1674 } 1675 mutex_exit(&dn->dn_mtx); 1676 return (i < TXG_SIZE); 1677} 1678 1679/* return TRUE if this blkid was freed in a recent txg, or FALSE if it wasn't */ 1680uint64_t 1681dnode_block_freed(dnode_t *dn, uint64_t blkid) 1682{ 1683 void *dp = spa_get_dsl(dn->dn_objset->os_spa); 1684 int i; 1685 1686 if (blkid == DMU_BONUS_BLKID) 1687 return (FALSE); 1688 1689 /* 1690 * If we're in the process of opening the pool, dp will not be 1691 * set yet, but there shouldn't be anything dirty. 1692 */ 1693 if (dp == NULL) 1694 return (FALSE); 1695 1696 if (dn->dn_free_txg) 1697 return (TRUE); 1698 1699 if (blkid == DMU_SPILL_BLKID) 1700 return (dnode_spill_freed(dn)); 1701 1702 mutex_enter(&dn->dn_mtx); 1703 for (i = 0; i < TXG_SIZE; i++) { 1704 if (dn->dn_free_ranges[i] != NULL && 1705 range_tree_contains(dn->dn_free_ranges[i], blkid, 1)) 1706 break; 1707 } 1708 mutex_exit(&dn->dn_mtx); 1709 return (i < TXG_SIZE); 1710} 1711 1712/* call from syncing context when we actually write/free space for this dnode */ 1713void 1714dnode_diduse_space(dnode_t *dn, int64_t delta) 1715{ 1716 uint64_t space; 1717 dprintf_dnode(dn, "dn=%p dnp=%p used=%llu delta=%lld\n", 1718 dn, dn->dn_phys, 1719 (u_longlong_t)dn->dn_phys->dn_used, 1720 (longlong_t)delta); 1721 1722 mutex_enter(&dn->dn_mtx); 1723 space = DN_USED_BYTES(dn->dn_phys); 1724 if (delta > 0) { 1725 ASSERT3U(space + delta, >=, space); /* no overflow */ 1726 } else { 1727 ASSERT3U(space, >=, -delta); /* no underflow */ 1728 } 1729 space += delta; 1730 if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_DNODE_BYTES) { 1731 ASSERT((dn->dn_phys->dn_flags & DNODE_FLAG_USED_BYTES) == 0); 1732 ASSERT0(P2PHASE(space, 1<<DEV_BSHIFT)); 1733 dn->dn_phys->dn_used = space >> DEV_BSHIFT; 1734 } else { 1735 dn->dn_phys->dn_used = space; 1736 dn->dn_phys->dn_flags |= DNODE_FLAG_USED_BYTES; 1737 } 1738 mutex_exit(&dn->dn_mtx); 1739} 1740 1741/* 1742 * Call when we think we're going to write/free space in open context to track 1743 * the amount of memory in use by the currently open txg. 1744 */ 1745void 1746dnode_willuse_space(dnode_t *dn, int64_t space, dmu_tx_t *tx) 1747{ 1748 objset_t *os = dn->dn_objset; 1749 dsl_dataset_t *ds = os->os_dsl_dataset; 1750 int64_t aspace = spa_get_asize(os->os_spa, space); 1751 1752 if (ds != NULL) { 1753 dsl_dir_willuse_space(ds->ds_dir, aspace, tx); 1754 dsl_pool_dirty_space(dmu_tx_pool(tx), space, tx); 1755 } 1756 1757 dmu_tx_willuse_space(tx, aspace); 1758} 1759 1760/* 1761 * Scans a block at the indicated "level" looking for a hole or data, 1762 * depending on 'flags'. 1763 * 1764 * If level > 0, then we are scanning an indirect block looking at its 1765 * pointers. If level == 0, then we are looking at a block of dnodes. 1766 * 1767 * If we don't find what we are looking for in the block, we return ESRCH. 1768 * Otherwise, return with *offset pointing to the beginning (if searching 1769 * forwards) or end (if searching backwards) of the range covered by the 1770 * block pointer we matched on (or dnode). 1771 * 1772 * The basic search algorithm used below by dnode_next_offset() is to 1773 * use this function to search up the block tree (widen the search) until 1774 * we find something (i.e., we don't return ESRCH) and then search back 1775 * down the tree (narrow the search) until we reach our original search 1776 * level. 1777 */ 1778static int 1779dnode_next_offset_level(dnode_t *dn, int flags, uint64_t *offset, 1780 int lvl, uint64_t blkfill, uint64_t txg) 1781{ 1782 dmu_buf_impl_t *db = NULL; 1783 void *data = NULL; 1784 uint64_t epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; 1785 uint64_t epb = 1ULL << epbs; 1786 uint64_t minfill, maxfill; 1787 boolean_t hole; 1788 int i, inc, error, span; 1789 1790 dprintf("probing object %llu offset %llx level %d of %u\n", 1791 dn->dn_object, *offset, lvl, dn->dn_phys->dn_nlevels); 1792 1793 hole = ((flags & DNODE_FIND_HOLE) != 0); 1794 inc = (flags & DNODE_FIND_BACKWARDS) ? -1 : 1; 1795 ASSERT(txg == 0 || !hole); 1796 1797 if (lvl == dn->dn_phys->dn_nlevels) { 1798 error = 0; 1799 epb = dn->dn_phys->dn_nblkptr; 1800 data = dn->dn_phys->dn_blkptr; 1801 } else { 1802 uint64_t blkid = dbuf_whichblock(dn, *offset) >> (epbs * lvl); 1803 error = dbuf_hold_impl(dn, lvl, blkid, TRUE, FTAG, &db); 1804 if (error) { 1805 if (error != ENOENT) 1806 return (error); 1807 if (hole) 1808 return (0); 1809 /* 1810 * This can only happen when we are searching up 1811 * the block tree for data. We don't really need to 1812 * adjust the offset, as we will just end up looking 1813 * at the pointer to this block in its parent, and its 1814 * going to be unallocated, so we will skip over it. 1815 */ 1816 return (SET_ERROR(ESRCH)); 1817 } 1818 error = dbuf_read(db, NULL, DB_RF_CANFAIL | DB_RF_HAVESTRUCT); 1819 if (error) { 1820 dbuf_rele(db, FTAG); 1821 return (error); 1822 } 1823 data = db->db.db_data; 1824 } 1825 1826 1827 if (db != NULL && txg != 0 && (db->db_blkptr == NULL || 1828 db->db_blkptr->blk_birth <= txg || 1829 BP_IS_HOLE(db->db_blkptr))) { 1830 /* 1831 * This can only happen when we are searching up the tree 1832 * and these conditions mean that we need to keep climbing. 1833 */ 1834 error = SET_ERROR(ESRCH); 1835 } else if (lvl == 0) { 1836 dnode_phys_t *dnp = data; 1837 span = DNODE_SHIFT; 1838 ASSERT(dn->dn_type == DMU_OT_DNODE); 1839 1840 for (i = (*offset >> span) & (blkfill - 1); 1841 i >= 0 && i < blkfill; i += inc) { 1842 if ((dnp[i].dn_type == DMU_OT_NONE) == hole) 1843 break; 1844 *offset += (1ULL << span) * inc; 1845 } 1846 if (i < 0 || i == blkfill) 1847 error = SET_ERROR(ESRCH); 1848 } else { 1849 blkptr_t *bp = data; 1850 uint64_t start = *offset; 1851 span = (lvl - 1) * epbs + dn->dn_datablkshift; 1852 minfill = 0; 1853 maxfill = blkfill << ((lvl - 1) * epbs); 1854 1855 if (hole) 1856 maxfill--; 1857 else 1858 minfill++; 1859 1860 *offset = *offset >> span; 1861 for (i = BF64_GET(*offset, 0, epbs); 1862 i >= 0 && i < epb; i += inc) { 1863 if (BP_GET_FILL(&bp[i]) >= minfill && 1864 BP_GET_FILL(&bp[i]) <= maxfill && 1865 (hole || bp[i].blk_birth > txg)) 1866 break; 1867 if (inc > 0 || *offset > 0) 1868 *offset += inc; 1869 } 1870 *offset = *offset << span; 1871 if (inc < 0) { 1872 /* traversing backwards; position offset at the end */ 1873 ASSERT3U(*offset, <=, start); 1874 *offset = MIN(*offset + (1ULL << span) - 1, start); 1875 } else if (*offset < start) { 1876 *offset = start; 1877 } 1878 if (i < 0 || i >= epb) 1879 error = SET_ERROR(ESRCH); 1880 } 1881 1882 if (db) 1883 dbuf_rele(db, FTAG); 1884 1885 return (error); 1886} 1887 1888/* 1889 * Find the next hole, data, or sparse region at or after *offset. 1890 * The value 'blkfill' tells us how many items we expect to find 1891 * in an L0 data block; this value is 1 for normal objects, 1892 * DNODES_PER_BLOCK for the meta dnode, and some fraction of 1893 * DNODES_PER_BLOCK when searching for sparse regions thereof. 1894 * 1895 * Examples: 1896 * 1897 * dnode_next_offset(dn, flags, offset, 1, 1, 0); 1898 * Finds the next/previous hole/data in a file. 1899 * Used in dmu_offset_next(). 1900 * 1901 * dnode_next_offset(mdn, flags, offset, 0, DNODES_PER_BLOCK, txg); 1902 * Finds the next free/allocated dnode an objset's meta-dnode. 1903 * Only finds objects that have new contents since txg (ie. 1904 * bonus buffer changes and content removal are ignored). 1905 * Used in dmu_object_next(). 1906 * 1907 * dnode_next_offset(mdn, DNODE_FIND_HOLE, offset, 2, DNODES_PER_BLOCK >> 2, 0); 1908 * Finds the next L2 meta-dnode bp that's at most 1/4 full. 1909 * Used in dmu_object_alloc(). 1910 */ 1911int 1912dnode_next_offset(dnode_t *dn, int flags, uint64_t *offset, 1913 int minlvl, uint64_t blkfill, uint64_t txg) 1914{ 1915 uint64_t initial_offset = *offset; 1916 int lvl, maxlvl; 1917 int error = 0; 1918 1919 if (!(flags & DNODE_FIND_HAVELOCK)) 1920 rw_enter(&dn->dn_struct_rwlock, RW_READER); 1921 1922 if (dn->dn_phys->dn_nlevels == 0) { 1923 error = SET_ERROR(ESRCH); 1924 goto out; 1925 } 1926 1927 if (dn->dn_datablkshift == 0) { 1928 if (*offset < dn->dn_datablksz) { 1929 if (flags & DNODE_FIND_HOLE) 1930 *offset = dn->dn_datablksz; 1931 } else { 1932 error = SET_ERROR(ESRCH); 1933 } 1934 goto out; 1935 } 1936 1937 maxlvl = dn->dn_phys->dn_nlevels; 1938 1939 for (lvl = minlvl; lvl <= maxlvl; lvl++) { 1940 error = dnode_next_offset_level(dn, 1941 flags, offset, lvl, blkfill, txg); 1942 if (error != ESRCH) 1943 break; 1944 } 1945 1946 while (error == 0 && --lvl >= minlvl) { 1947 error = dnode_next_offset_level(dn, 1948 flags, offset, lvl, blkfill, txg); 1949 } 1950 1951 /* 1952 * There's always a "virtual hole" at the end of the object, even 1953 * if all BP's which physically exist are non-holes. 1954 */ 1955 if ((flags & DNODE_FIND_HOLE) && error == ESRCH && txg == 0 && 1956 minlvl == 1 && blkfill == 1 && !(flags & DNODE_FIND_BACKWARDS)) { 1957 error = 0; 1958 } 1959 1960 if (error == 0 && (flags & DNODE_FIND_BACKWARDS ? 1961 initial_offset < *offset : initial_offset > *offset)) 1962 error = SET_ERROR(ESRCH); 1963out: 1964 if (!(flags & DNODE_FIND_HAVELOCK)) 1965 rw_exit(&dn->dn_struct_rwlock); 1966 1967 return (error); 1968} 1969