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 2011 Nexenta Systems, Inc. All rights reserved. 24 * Copyright (c) 2012, 2018 by Delphix. All rights reserved. 25 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved. 26 * Copyright (c) 2013, Joyent, Inc. All rights reserved. 27 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. 28 * Copyright (c) 2014 Integros [integros.com] 29 */ 30 31 #include <sys/zfs_context.h> 32 #include <sys/dmu.h> 33 #include <sys/dmu_send.h> 34 #include <sys/dmu_impl.h> 35 #include <sys/dbuf.h> 36 #include <sys/dmu_objset.h> 37 #include <sys/dsl_dataset.h> 38 #include <sys/dsl_dir.h> 39 #include <sys/dmu_tx.h> 40 #include <sys/spa.h> 41 #include <sys/zio.h> 42 #include <sys/dmu_zfetch.h> 43 #include <sys/sa.h> 44 #include <sys/sa_impl.h> 45 #include <sys/zfeature.h> 46 #include <sys/blkptr.h> 47 #include <sys/range_tree.h> 48 #include <sys/callb.h> 49 #include <sys/abd.h> 50 #include <sys/vdev.h> 51 #include <sys/cityhash.h> 52 #include <sys/spa_impl.h> 53 54 static boolean_t dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx); 55 static void dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx); 56 57 #ifndef __lint 58 extern inline void dmu_buf_init_user(dmu_buf_user_t *dbu, 59 dmu_buf_evict_func_t *evict_func_sync, 60 dmu_buf_evict_func_t *evict_func_async, 61 dmu_buf_t **clear_on_evict_dbufp); 62 #endif /* ! __lint */ 63 64 /* 65 * Global data structures and functions for the dbuf cache. 66 */ 67 static kmem_cache_t *dbuf_kmem_cache; 68 static taskq_t *dbu_evict_taskq; 69 70 static kthread_t *dbuf_cache_evict_thread; 71 static kmutex_t dbuf_evict_lock; 72 static kcondvar_t dbuf_evict_cv; 73 static boolean_t dbuf_evict_thread_exit; 74 75 /* 76 * There are two dbuf caches; each dbuf can only be in one of them at a time. 77 * 78 * 1. Cache of metadata dbufs, to help make read-heavy administrative commands 79 * from /sbin/zfs run faster. The "metadata cache" specifically stores dbufs 80 * that represent the metadata that describes filesystems/snapshots/ 81 * bookmarks/properties/etc. We only evict from this cache when we export a 82 * pool, to short-circuit as much I/O as possible for all administrative 83 * commands that need the metadata. There is no eviction policy for this 84 * cache, because we try to only include types in it which would occupy a 85 * very small amount of space per object but create a large impact on the 86 * performance of these commands. Instead, after it reaches a maximum size 87 * (which should only happen on very small memory systems with a very large 88 * number of filesystem objects), we stop taking new dbufs into the 89 * metadata cache, instead putting them in the normal dbuf cache. 90 * 91 * 2. LRU cache of dbufs. The "dbuf cache" maintains a list of dbufs that 92 * are not currently held but have been recently released. These dbufs 93 * are not eligible for arc eviction until they are aged out of the cache. 94 * Dbufs that are aged out of the cache will be immediately destroyed and 95 * become eligible for arc eviction. 96 * 97 * Dbufs are added to these caches once the last hold is released. If a dbuf is 98 * later accessed and still exists in the dbuf cache, then it will be removed 99 * from the cache and later re-added to the head of the cache. 100 * 101 * If a given dbuf meets the requirements for the metadata cache, it will go 102 * there, otherwise it will be considered for the generic LRU dbuf cache. The 103 * caches and the refcounts tracking their sizes are stored in an array indexed 104 * by those caches' matching enum values (from dbuf_cached_state_t). 105 */ 106 typedef struct dbuf_cache { 107 multilist_t *cache; 108 refcount_t size; 109 } dbuf_cache_t; 110 dbuf_cache_t dbuf_caches[DB_CACHE_MAX]; 111 112 /* Size limits for the caches */ 113 uint64_t dbuf_cache_max_bytes = 0; 114 uint64_t dbuf_metadata_cache_max_bytes = 0; 115 /* Set the default sizes of the caches to log2 fraction of arc size */ 116 int dbuf_cache_shift = 5; 117 int dbuf_metadata_cache_shift = 6; 118 119 /* 120 * For diagnostic purposes, this is incremented whenever we can't add 121 * something to the metadata cache because it's full, and instead put 122 * the data in the regular dbuf cache. 123 */ 124 uint64_t dbuf_metadata_cache_overflow; 125 126 /* 127 * The LRU dbuf cache uses a three-stage eviction policy: 128 * - A low water marker designates when the dbuf eviction thread 129 * should stop evicting from the dbuf cache. 130 * - When we reach the maximum size (aka mid water mark), we 131 * signal the eviction thread to run. 132 * - The high water mark indicates when the eviction thread 133 * is unable to keep up with the incoming load and eviction must 134 * happen in the context of the calling thread. 135 * 136 * The dbuf cache: 137 * (max size) 138 * low water mid water hi water 139 * +----------------------------------------+----------+----------+ 140 * | | | | 141 * | | | | 142 * | | | | 143 * | | | | 144 * +----------------------------------------+----------+----------+ 145 * stop signal evict 146 * evicting eviction directly 147 * thread 148 * 149 * The high and low water marks indicate the operating range for the eviction 150 * thread. The low water mark is, by default, 90% of the total size of the 151 * cache and the high water mark is at 110% (both of these percentages can be 152 * changed by setting dbuf_cache_lowater_pct and dbuf_cache_hiwater_pct, 153 * respectively). The eviction thread will try to ensure that the cache remains 154 * within this range by waking up every second and checking if the cache is 155 * above the low water mark. The thread can also be woken up by callers adding 156 * elements into the cache if the cache is larger than the mid water (i.e max 157 * cache size). Once the eviction thread is woken up and eviction is required, 158 * it will continue evicting buffers until it's able to reduce the cache size 159 * to the low water mark. If the cache size continues to grow and hits the high 160 * water mark, then callers adding elments to the cache will begin to evict 161 * directly from the cache until the cache is no longer above the high water 162 * mark. 163 */ 164 165 /* 166 * The percentage above and below the maximum cache size. 167 */ 168 uint_t dbuf_cache_hiwater_pct = 10; 169 uint_t dbuf_cache_lowater_pct = 10; 170 171 /* ARGSUSED */ 172 static int 173 dbuf_cons(void *vdb, void *unused, int kmflag) 174 { 175 dmu_buf_impl_t *db = vdb; 176 bzero(db, sizeof (dmu_buf_impl_t)); 177 178 mutex_init(&db->db_mtx, NULL, MUTEX_DEFAULT, NULL); 179 cv_init(&db->db_changed, NULL, CV_DEFAULT, NULL); 180 multilist_link_init(&db->db_cache_link); 181 refcount_create(&db->db_holds); 182 183 return (0); 184 } 185 186 /* ARGSUSED */ 187 static void 188 dbuf_dest(void *vdb, void *unused) 189 { 190 dmu_buf_impl_t *db = vdb; 191 mutex_destroy(&db->db_mtx); 192 cv_destroy(&db->db_changed); 193 ASSERT(!multilist_link_active(&db->db_cache_link)); 194 refcount_destroy(&db->db_holds); 195 } 196 197 /* 198 * dbuf hash table routines 199 */ 200 static dbuf_hash_table_t dbuf_hash_table; 201 202 static uint64_t dbuf_hash_count; 203 204 /* 205 * We use Cityhash for this. It's fast, and has good hash properties without 206 * requiring any large static buffers. 207 */ 208 static uint64_t 209 dbuf_hash(void *os, uint64_t obj, uint8_t lvl, uint64_t blkid) 210 { 211 return (cityhash4((uintptr_t)os, obj, (uint64_t)lvl, blkid)); 212 } 213 214 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \ 215 ((dbuf)->db.db_object == (obj) && \ 216 (dbuf)->db_objset == (os) && \ 217 (dbuf)->db_level == (level) && \ 218 (dbuf)->db_blkid == (blkid)) 219 220 dmu_buf_impl_t * 221 dbuf_find(objset_t *os, uint64_t obj, uint8_t level, uint64_t blkid) 222 { 223 dbuf_hash_table_t *h = &dbuf_hash_table; 224 uint64_t hv = dbuf_hash(os, obj, level, blkid); 225 uint64_t idx = hv & h->hash_table_mask; 226 dmu_buf_impl_t *db; 227 228 mutex_enter(DBUF_HASH_MUTEX(h, idx)); 229 for (db = h->hash_table[idx]; db != NULL; db = db->db_hash_next) { 230 if (DBUF_EQUAL(db, os, obj, level, blkid)) { 231 mutex_enter(&db->db_mtx); 232 if (db->db_state != DB_EVICTING) { 233 mutex_exit(DBUF_HASH_MUTEX(h, idx)); 234 return (db); 235 } 236 mutex_exit(&db->db_mtx); 237 } 238 } 239 mutex_exit(DBUF_HASH_MUTEX(h, idx)); 240 return (NULL); 241 } 242 243 static dmu_buf_impl_t * 244 dbuf_find_bonus(objset_t *os, uint64_t object) 245 { 246 dnode_t *dn; 247 dmu_buf_impl_t *db = NULL; 248 249 if (dnode_hold(os, object, FTAG, &dn) == 0) { 250 rw_enter(&dn->dn_struct_rwlock, RW_READER); 251 if (dn->dn_bonus != NULL) { 252 db = dn->dn_bonus; 253 mutex_enter(&db->db_mtx); 254 } 255 rw_exit(&dn->dn_struct_rwlock); 256 dnode_rele(dn, FTAG); 257 } 258 return (db); 259 } 260 261 /* 262 * Insert an entry into the hash table. If there is already an element 263 * equal to elem in the hash table, then the already existing element 264 * will be returned and the new element will not be inserted. 265 * Otherwise returns NULL. 266 */ 267 static dmu_buf_impl_t * 268 dbuf_hash_insert(dmu_buf_impl_t *db) 269 { 270 dbuf_hash_table_t *h = &dbuf_hash_table; 271 objset_t *os = db->db_objset; 272 uint64_t obj = db->db.db_object; 273 int level = db->db_level; 274 uint64_t blkid = db->db_blkid; 275 uint64_t hv = dbuf_hash(os, obj, level, blkid); 276 uint64_t idx = hv & h->hash_table_mask; 277 dmu_buf_impl_t *dbf; 278 279 mutex_enter(DBUF_HASH_MUTEX(h, idx)); 280 for (dbf = h->hash_table[idx]; dbf != NULL; dbf = dbf->db_hash_next) { 281 if (DBUF_EQUAL(dbf, os, obj, level, blkid)) { 282 mutex_enter(&dbf->db_mtx); 283 if (dbf->db_state != DB_EVICTING) { 284 mutex_exit(DBUF_HASH_MUTEX(h, idx)); 285 return (dbf); 286 } 287 mutex_exit(&dbf->db_mtx); 288 } 289 } 290 291 mutex_enter(&db->db_mtx); 292 db->db_hash_next = h->hash_table[idx]; 293 h->hash_table[idx] = db; 294 mutex_exit(DBUF_HASH_MUTEX(h, idx)); 295 atomic_inc_64(&dbuf_hash_count); 296 297 return (NULL); 298 } 299 300 /* 301 * Remove an entry from the hash table. It must be in the EVICTING state. 302 */ 303 static void 304 dbuf_hash_remove(dmu_buf_impl_t *db) 305 { 306 dbuf_hash_table_t *h = &dbuf_hash_table; 307 uint64_t hv = dbuf_hash(db->db_objset, db->db.db_object, 308 db->db_level, db->db_blkid); 309 uint64_t idx = hv & h->hash_table_mask; 310 dmu_buf_impl_t *dbf, **dbp; 311 312 /* 313 * We musn't hold db_mtx to maintain lock ordering: 314 * DBUF_HASH_MUTEX > db_mtx. 315 */ 316 ASSERT(refcount_is_zero(&db->db_holds)); 317 ASSERT(db->db_state == DB_EVICTING); 318 ASSERT(!MUTEX_HELD(&db->db_mtx)); 319 320 mutex_enter(DBUF_HASH_MUTEX(h, idx)); 321 dbp = &h->hash_table[idx]; 322 while ((dbf = *dbp) != db) { 323 dbp = &dbf->db_hash_next; 324 ASSERT(dbf != NULL); 325 } 326 *dbp = db->db_hash_next; 327 db->db_hash_next = NULL; 328 mutex_exit(DBUF_HASH_MUTEX(h, idx)); 329 atomic_dec_64(&dbuf_hash_count); 330 } 331 332 typedef enum { 333 DBVU_EVICTING, 334 DBVU_NOT_EVICTING 335 } dbvu_verify_type_t; 336 337 static void 338 dbuf_verify_user(dmu_buf_impl_t *db, dbvu_verify_type_t verify_type) 339 { 340 #ifdef ZFS_DEBUG 341 int64_t holds; 342 343 if (db->db_user == NULL) 344 return; 345 346 /* Only data blocks support the attachment of user data. */ 347 ASSERT(db->db_level == 0); 348 349 /* Clients must resolve a dbuf before attaching user data. */ 350 ASSERT(db->db.db_data != NULL); 351 ASSERT3U(db->db_state, ==, DB_CACHED); 352 353 holds = refcount_count(&db->db_holds); 354 if (verify_type == DBVU_EVICTING) { 355 /* 356 * Immediate eviction occurs when holds == dirtycnt. 357 * For normal eviction buffers, holds is zero on 358 * eviction, except when dbuf_fix_old_data() calls 359 * dbuf_clear_data(). However, the hold count can grow 360 * during eviction even though db_mtx is held (see 361 * dmu_bonus_hold() for an example), so we can only 362 * test the generic invariant that holds >= dirtycnt. 363 */ 364 ASSERT3U(holds, >=, db->db_dirtycnt); 365 } else { 366 if (db->db_user_immediate_evict == TRUE) 367 ASSERT3U(holds, >=, db->db_dirtycnt); 368 else 369 ASSERT3U(holds, >, 0); 370 } 371 #endif 372 } 373 374 static void 375 dbuf_evict_user(dmu_buf_impl_t *db) 376 { 377 dmu_buf_user_t *dbu = db->db_user; 378 379 ASSERT(MUTEX_HELD(&db->db_mtx)); 380 381 if (dbu == NULL) 382 return; 383 384 dbuf_verify_user(db, DBVU_EVICTING); 385 db->db_user = NULL; 386 387 #ifdef ZFS_DEBUG 388 if (dbu->dbu_clear_on_evict_dbufp != NULL) 389 *dbu->dbu_clear_on_evict_dbufp = NULL; 390 #endif 391 392 /* 393 * There are two eviction callbacks - one that we call synchronously 394 * and one that we invoke via a taskq. The async one is useful for 395 * avoiding lock order reversals and limiting stack depth. 396 * 397 * Note that if we have a sync callback but no async callback, 398 * it's likely that the sync callback will free the structure 399 * containing the dbu. In that case we need to take care to not 400 * dereference dbu after calling the sync evict func. 401 */ 402 boolean_t has_async = (dbu->dbu_evict_func_async != NULL); 403 404 if (dbu->dbu_evict_func_sync != NULL) 405 dbu->dbu_evict_func_sync(dbu); 406 407 if (has_async) { 408 taskq_dispatch_ent(dbu_evict_taskq, dbu->dbu_evict_func_async, 409 dbu, 0, &dbu->dbu_tqent); 410 } 411 } 412 413 boolean_t 414 dbuf_is_metadata(dmu_buf_impl_t *db) 415 { 416 if (db->db_level > 0) { 417 return (B_TRUE); 418 } else { 419 boolean_t is_metadata; 420 421 DB_DNODE_ENTER(db); 422 is_metadata = DMU_OT_IS_METADATA(DB_DNODE(db)->dn_type); 423 DB_DNODE_EXIT(db); 424 425 return (is_metadata); 426 } 427 } 428 429 /* 430 * This returns whether this dbuf should be stored in the metadata cache, which 431 * is based on whether it's from one of the dnode types that store data related 432 * to traversing dataset hierarchies. 433 */ 434 static boolean_t 435 dbuf_include_in_metadata_cache(dmu_buf_impl_t *db) 436 { 437 DB_DNODE_ENTER(db); 438 dmu_object_type_t type = DB_DNODE(db)->dn_type; 439 DB_DNODE_EXIT(db); 440 441 /* Check if this dbuf is one of the types we care about */ 442 if (DMU_OT_IS_METADATA_CACHED(type)) { 443 /* If we hit this, then we set something up wrong in dmu_ot */ 444 ASSERT(DMU_OT_IS_METADATA(type)); 445 446 /* 447 * Sanity check for small-memory systems: don't allocate too 448 * much memory for this purpose. 449 */ 450 if (refcount_count(&dbuf_caches[DB_DBUF_METADATA_CACHE].size) > 451 dbuf_metadata_cache_max_bytes) { 452 dbuf_metadata_cache_overflow++; 453 DTRACE_PROBE1(dbuf__metadata__cache__overflow, 454 dmu_buf_impl_t *, db); 455 return (B_FALSE); 456 } 457 458 return (B_TRUE); 459 } 460 461 return (B_FALSE); 462 } 463 464 /* 465 * This function *must* return indices evenly distributed between all 466 * sublists of the multilist. This is needed due to how the dbuf eviction 467 * code is laid out; dbuf_evict_thread() assumes dbufs are evenly 468 * distributed between all sublists and uses this assumption when 469 * deciding which sublist to evict from and how much to evict from it. 470 */ 471 unsigned int 472 dbuf_cache_multilist_index_func(multilist_t *ml, void *obj) 473 { 474 dmu_buf_impl_t *db = obj; 475 476 /* 477 * The assumption here, is the hash value for a given 478 * dmu_buf_impl_t will remain constant throughout it's lifetime 479 * (i.e. it's objset, object, level and blkid fields don't change). 480 * Thus, we don't need to store the dbuf's sublist index 481 * on insertion, as this index can be recalculated on removal. 482 * 483 * Also, the low order bits of the hash value are thought to be 484 * distributed evenly. Otherwise, in the case that the multilist 485 * has a power of two number of sublists, each sublists' usage 486 * would not be evenly distributed. 487 */ 488 return (dbuf_hash(db->db_objset, db->db.db_object, 489 db->db_level, db->db_blkid) % 490 multilist_get_num_sublists(ml)); 491 } 492 493 static inline boolean_t 494 dbuf_cache_above_hiwater(void) 495 { 496 uint64_t dbuf_cache_hiwater_bytes = 497 (dbuf_cache_max_bytes * dbuf_cache_hiwater_pct) / 100; 498 499 return (refcount_count(&dbuf_caches[DB_DBUF_CACHE].size) > 500 dbuf_cache_max_bytes + dbuf_cache_hiwater_bytes); 501 } 502 503 static inline boolean_t 504 dbuf_cache_above_lowater(void) 505 { 506 uint64_t dbuf_cache_lowater_bytes = 507 (dbuf_cache_max_bytes * dbuf_cache_lowater_pct) / 100; 508 509 return (refcount_count(&dbuf_caches[DB_DBUF_CACHE].size) > 510 dbuf_cache_max_bytes - dbuf_cache_lowater_bytes); 511 } 512 513 /* 514 * Evict the oldest eligible dbuf from the dbuf cache. 515 */ 516 static void 517 dbuf_evict_one(void) 518 { 519 int idx = multilist_get_random_index(dbuf_caches[DB_DBUF_CACHE].cache); 520 multilist_sublist_t *mls = multilist_sublist_lock( 521 dbuf_caches[DB_DBUF_CACHE].cache, idx); 522 523 ASSERT(!MUTEX_HELD(&dbuf_evict_lock)); 524 525 dmu_buf_impl_t *db = multilist_sublist_tail(mls); 526 while (db != NULL && mutex_tryenter(&db->db_mtx) == 0) { 527 db = multilist_sublist_prev(mls, db); 528 } 529 530 DTRACE_PROBE2(dbuf__evict__one, dmu_buf_impl_t *, db, 531 multilist_sublist_t *, mls); 532 533 if (db != NULL) { 534 multilist_sublist_remove(mls, db); 535 multilist_sublist_unlock(mls); 536 (void) refcount_remove_many(&dbuf_caches[DB_DBUF_CACHE].size, 537 db->db.db_size, db); 538 ASSERT3U(db->db_caching_status, ==, DB_DBUF_CACHE); 539 db->db_caching_status = DB_NO_CACHE; 540 dbuf_destroy(db); 541 } else { 542 multilist_sublist_unlock(mls); 543 } 544 } 545 546 /* 547 * The dbuf evict thread is responsible for aging out dbufs from the 548 * cache. Once the cache has reached it's maximum size, dbufs are removed 549 * and destroyed. The eviction thread will continue running until the size 550 * of the dbuf cache is at or below the maximum size. Once the dbuf is aged 551 * out of the cache it is destroyed and becomes eligible for arc eviction. 552 */ 553 /* ARGSUSED */ 554 static void 555 dbuf_evict_thread(void *unused) 556 { 557 callb_cpr_t cpr; 558 559 CALLB_CPR_INIT(&cpr, &dbuf_evict_lock, callb_generic_cpr, FTAG); 560 561 mutex_enter(&dbuf_evict_lock); 562 while (!dbuf_evict_thread_exit) { 563 while (!dbuf_cache_above_lowater() && !dbuf_evict_thread_exit) { 564 CALLB_CPR_SAFE_BEGIN(&cpr); 565 (void) cv_timedwait_hires(&dbuf_evict_cv, 566 &dbuf_evict_lock, SEC2NSEC(1), MSEC2NSEC(1), 0); 567 CALLB_CPR_SAFE_END(&cpr, &dbuf_evict_lock); 568 } 569 mutex_exit(&dbuf_evict_lock); 570 571 /* 572 * Keep evicting as long as we're above the low water mark 573 * for the cache. We do this without holding the locks to 574 * minimize lock contention. 575 */ 576 while (dbuf_cache_above_lowater() && !dbuf_evict_thread_exit) { 577 dbuf_evict_one(); 578 } 579 580 mutex_enter(&dbuf_evict_lock); 581 } 582 583 dbuf_evict_thread_exit = B_FALSE; 584 cv_broadcast(&dbuf_evict_cv); 585 CALLB_CPR_EXIT(&cpr); /* drops dbuf_evict_lock */ 586 thread_exit(); 587 } 588 589 /* 590 * Wake up the dbuf eviction thread if the dbuf cache is at its max size. 591 * If the dbuf cache is at its high water mark, then evict a dbuf from the 592 * dbuf cache using the callers context. 593 */ 594 static void 595 dbuf_evict_notify(void) 596 { 597 /* 598 * We check if we should evict without holding the dbuf_evict_lock, 599 * because it's OK to occasionally make the wrong decision here, 600 * and grabbing the lock results in massive lock contention. 601 */ 602 if (refcount_count(&dbuf_caches[DB_DBUF_CACHE].size) > 603 dbuf_cache_max_bytes) { 604 if (dbuf_cache_above_hiwater()) 605 dbuf_evict_one(); 606 cv_signal(&dbuf_evict_cv); 607 } 608 } 609 610 void 611 dbuf_init(void) 612 { 613 uint64_t hsize = 1ULL << 16; 614 dbuf_hash_table_t *h = &dbuf_hash_table; 615 int i; 616 617 /* 618 * The hash table is big enough to fill all of physical memory 619 * with an average 4K block size. The table will take up 620 * totalmem*sizeof(void*)/4K (i.e. 2MB/GB with 8-byte pointers). 621 */ 622 while (hsize * 4096 < physmem * PAGESIZE) 623 hsize <<= 1; 624 625 retry: 626 h->hash_table_mask = hsize - 1; 627 h->hash_table = kmem_zalloc(hsize * sizeof (void *), KM_NOSLEEP); 628 if (h->hash_table == NULL) { 629 /* XXX - we should really return an error instead of assert */ 630 ASSERT(hsize > (1ULL << 10)); 631 hsize >>= 1; 632 goto retry; 633 } 634 635 dbuf_kmem_cache = kmem_cache_create("dmu_buf_impl_t", 636 sizeof (dmu_buf_impl_t), 637 0, dbuf_cons, dbuf_dest, NULL, NULL, NULL, 0); 638 639 for (i = 0; i < DBUF_MUTEXES; i++) 640 mutex_init(&h->hash_mutexes[i], NULL, MUTEX_DEFAULT, NULL); 641 642 /* 643 * Setup the parameters for the dbuf caches. We set the sizes of the 644 * dbuf cache and the metadata cache to 1/32nd and 1/16th (default) 645 * of the size of the ARC, respectively. If the values are set in 646 * /etc/system and they're not greater than the size of the ARC, then 647 * we honor that value. 648 */ 649 if (dbuf_cache_max_bytes == 0 || 650 dbuf_cache_max_bytes >= arc_max_bytes()) { 651 dbuf_cache_max_bytes = arc_max_bytes() >> dbuf_cache_shift; 652 } 653 if (dbuf_metadata_cache_max_bytes == 0 || 654 dbuf_metadata_cache_max_bytes >= arc_max_bytes()) { 655 dbuf_metadata_cache_max_bytes = 656 arc_max_bytes() >> dbuf_metadata_cache_shift; 657 } 658 659 /* 660 * All entries are queued via taskq_dispatch_ent(), so min/maxalloc 661 * configuration is not required. 662 */ 663 dbu_evict_taskq = taskq_create("dbu_evict", 1, minclsyspri, 0, 0, 0); 664 665 for (dbuf_cached_state_t dcs = 0; dcs < DB_CACHE_MAX; dcs++) { 666 dbuf_caches[dcs].cache = 667 multilist_create(sizeof (dmu_buf_impl_t), 668 offsetof(dmu_buf_impl_t, db_cache_link), 669 dbuf_cache_multilist_index_func); 670 refcount_create(&dbuf_caches[dcs].size); 671 } 672 673 dbuf_evict_thread_exit = B_FALSE; 674 mutex_init(&dbuf_evict_lock, NULL, MUTEX_DEFAULT, NULL); 675 cv_init(&dbuf_evict_cv, NULL, CV_DEFAULT, NULL); 676 dbuf_cache_evict_thread = thread_create(NULL, 0, dbuf_evict_thread, 677 NULL, 0, &p0, TS_RUN, minclsyspri); 678 } 679 680 void 681 dbuf_fini(void) 682 { 683 dbuf_hash_table_t *h = &dbuf_hash_table; 684 int i; 685 686 for (i = 0; i < DBUF_MUTEXES; i++) 687 mutex_destroy(&h->hash_mutexes[i]); 688 kmem_free(h->hash_table, (h->hash_table_mask + 1) * sizeof (void *)); 689 kmem_cache_destroy(dbuf_kmem_cache); 690 taskq_destroy(dbu_evict_taskq); 691 692 mutex_enter(&dbuf_evict_lock); 693 dbuf_evict_thread_exit = B_TRUE; 694 while (dbuf_evict_thread_exit) { 695 cv_signal(&dbuf_evict_cv); 696 cv_wait(&dbuf_evict_cv, &dbuf_evict_lock); 697 } 698 mutex_exit(&dbuf_evict_lock); 699 700 mutex_destroy(&dbuf_evict_lock); 701 cv_destroy(&dbuf_evict_cv); 702 703 for (dbuf_cached_state_t dcs = 0; dcs < DB_CACHE_MAX; dcs++) { 704 refcount_destroy(&dbuf_caches[dcs].size); 705 multilist_destroy(dbuf_caches[dcs].cache); 706 } 707 } 708 709 /* 710 * Other stuff. 711 */ 712 713 #ifdef ZFS_DEBUG 714 static void 715 dbuf_verify(dmu_buf_impl_t *db) 716 { 717 dnode_t *dn; 718 dbuf_dirty_record_t *dr; 719 720 ASSERT(MUTEX_HELD(&db->db_mtx)); 721 722 if (!(zfs_flags & ZFS_DEBUG_DBUF_VERIFY)) 723 return; 724 725 ASSERT(db->db_objset != NULL); 726 DB_DNODE_ENTER(db); 727 dn = DB_DNODE(db); 728 if (dn == NULL) { 729 ASSERT(db->db_parent == NULL); 730 ASSERT(db->db_blkptr == NULL); 731 } else { 732 ASSERT3U(db->db.db_object, ==, dn->dn_object); 733 ASSERT3P(db->db_objset, ==, dn->dn_objset); 734 ASSERT3U(db->db_level, <, dn->dn_nlevels); 735 ASSERT(db->db_blkid == DMU_BONUS_BLKID || 736 db->db_blkid == DMU_SPILL_BLKID || 737 !avl_is_empty(&dn->dn_dbufs)); 738 } 739 if (db->db_blkid == DMU_BONUS_BLKID) { 740 ASSERT(dn != NULL); 741 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen); 742 ASSERT3U(db->db.db_offset, ==, DMU_BONUS_BLKID); 743 } else if (db->db_blkid == DMU_SPILL_BLKID) { 744 ASSERT(dn != NULL); 745 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen); 746 ASSERT0(db->db.db_offset); 747 } else { 748 ASSERT3U(db->db.db_offset, ==, db->db_blkid * db->db.db_size); 749 } 750 751 for (dr = db->db_data_pending; dr != NULL; dr = dr->dr_next) 752 ASSERT(dr->dr_dbuf == db); 753 754 for (dr = db->db_last_dirty; dr != NULL; dr = dr->dr_next) 755 ASSERT(dr->dr_dbuf == db); 756 757 /* 758 * We can't assert that db_size matches dn_datablksz because it 759 * can be momentarily different when another thread is doing 760 * dnode_set_blksz(). 761 */ 762 if (db->db_level == 0 && db->db.db_object == DMU_META_DNODE_OBJECT) { 763 dr = db->db_data_pending; 764 /* 765 * It should only be modified in syncing context, so 766 * make sure we only have one copy of the data. 767 */ 768 ASSERT(dr == NULL || dr->dt.dl.dr_data == db->db_buf); 769 } 770 771 /* verify db->db_blkptr */ 772 if (db->db_blkptr) { 773 if (db->db_parent == dn->dn_dbuf) { 774 /* db is pointed to by the dnode */ 775 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */ 776 if (DMU_OBJECT_IS_SPECIAL(db->db.db_object)) 777 ASSERT(db->db_parent == NULL); 778 else 779 ASSERT(db->db_parent != NULL); 780 if (db->db_blkid != DMU_SPILL_BLKID) 781 ASSERT3P(db->db_blkptr, ==, 782 &dn->dn_phys->dn_blkptr[db->db_blkid]); 783 } else { 784 /* db is pointed to by an indirect block */ 785 int epb = db->db_parent->db.db_size >> SPA_BLKPTRSHIFT; 786 ASSERT3U(db->db_parent->db_level, ==, db->db_level+1); 787 ASSERT3U(db->db_parent->db.db_object, ==, 788 db->db.db_object); 789 /* 790 * dnode_grow_indblksz() can make this fail if we don't 791 * have the struct_rwlock. XXX indblksz no longer 792 * grows. safe to do this now? 793 */ 794 if (RW_WRITE_HELD(&dn->dn_struct_rwlock)) { 795 ASSERT3P(db->db_blkptr, ==, 796 ((blkptr_t *)db->db_parent->db.db_data + 797 db->db_blkid % epb)); 798 } 799 } 800 } 801 if ((db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr)) && 802 (db->db_buf == NULL || db->db_buf->b_data) && 803 db->db.db_data && db->db_blkid != DMU_BONUS_BLKID && 804 db->db_state != DB_FILL && !dn->dn_free_txg) { 805 /* 806 * If the blkptr isn't set but they have nonzero data, 807 * it had better be dirty, otherwise we'll lose that 808 * data when we evict this buffer. 809 * 810 * There is an exception to this rule for indirect blocks; in 811 * this case, if the indirect block is a hole, we fill in a few 812 * fields on each of the child blocks (importantly, birth time) 813 * to prevent hole birth times from being lost when you 814 * partially fill in a hole. 815 */ 816 if (db->db_dirtycnt == 0) { 817 if (db->db_level == 0) { 818 uint64_t *buf = db->db.db_data; 819 int i; 820 821 for (i = 0; i < db->db.db_size >> 3; i++) { 822 ASSERT(buf[i] == 0); 823 } 824 } else { 825 blkptr_t *bps = db->db.db_data; 826 ASSERT3U(1 << DB_DNODE(db)->dn_indblkshift, ==, 827 db->db.db_size); 828 /* 829 * We want to verify that all the blkptrs in the 830 * indirect block are holes, but we may have 831 * automatically set up a few fields for them. 832 * We iterate through each blkptr and verify 833 * they only have those fields set. 834 */ 835 for (int i = 0; 836 i < db->db.db_size / sizeof (blkptr_t); 837 i++) { 838 blkptr_t *bp = &bps[i]; 839 ASSERT(ZIO_CHECKSUM_IS_ZERO( 840 &bp->blk_cksum)); 841 ASSERT( 842 DVA_IS_EMPTY(&bp->blk_dva[0]) && 843 DVA_IS_EMPTY(&bp->blk_dva[1]) && 844 DVA_IS_EMPTY(&bp->blk_dva[2])); 845 ASSERT0(bp->blk_fill); 846 ASSERT0(bp->blk_pad[0]); 847 ASSERT0(bp->blk_pad[1]); 848 ASSERT(!BP_IS_EMBEDDED(bp)); 849 ASSERT(BP_IS_HOLE(bp)); 850 ASSERT0(bp->blk_phys_birth); 851 } 852 } 853 } 854 } 855 DB_DNODE_EXIT(db); 856 } 857 #endif 858 859 static void 860 dbuf_clear_data(dmu_buf_impl_t *db) 861 { 862 ASSERT(MUTEX_HELD(&db->db_mtx)); 863 dbuf_evict_user(db); 864 ASSERT3P(db->db_buf, ==, NULL); 865 db->db.db_data = NULL; 866 if (db->db_state != DB_NOFILL) 867 db->db_state = DB_UNCACHED; 868 } 869 870 static void 871 dbuf_set_data(dmu_buf_impl_t *db, arc_buf_t *buf) 872 { 873 ASSERT(MUTEX_HELD(&db->db_mtx)); 874 ASSERT(buf != NULL); 875 876 db->db_buf = buf; 877 ASSERT(buf->b_data != NULL); 878 db->db.db_data = buf->b_data; 879 } 880 881 /* 882 * Loan out an arc_buf for read. Return the loaned arc_buf. 883 */ 884 arc_buf_t * 885 dbuf_loan_arcbuf(dmu_buf_impl_t *db) 886 { 887 arc_buf_t *abuf; 888 889 ASSERT(db->db_blkid != DMU_BONUS_BLKID); 890 mutex_enter(&db->db_mtx); 891 if (arc_released(db->db_buf) || refcount_count(&db->db_holds) > 1) { 892 int blksz = db->db.db_size; 893 spa_t *spa = db->db_objset->os_spa; 894 895 mutex_exit(&db->db_mtx); 896 abuf = arc_loan_buf(spa, B_FALSE, blksz); 897 bcopy(db->db.db_data, abuf->b_data, blksz); 898 } else { 899 abuf = db->db_buf; 900 arc_loan_inuse_buf(abuf, db); 901 db->db_buf = NULL; 902 dbuf_clear_data(db); 903 mutex_exit(&db->db_mtx); 904 } 905 return (abuf); 906 } 907 908 /* 909 * Calculate which level n block references the data at the level 0 offset 910 * provided. 911 */ 912 uint64_t 913 dbuf_whichblock(dnode_t *dn, int64_t level, uint64_t offset) 914 { 915 if (dn->dn_datablkshift != 0 && dn->dn_indblkshift != 0) { 916 /* 917 * The level n blkid is equal to the level 0 blkid divided by 918 * the number of level 0s in a level n block. 919 * 920 * The level 0 blkid is offset >> datablkshift = 921 * offset / 2^datablkshift. 922 * 923 * The number of level 0s in a level n is the number of block 924 * pointers in an indirect block, raised to the power of level. 925 * This is 2^(indblkshift - SPA_BLKPTRSHIFT)^level = 926 * 2^(level*(indblkshift - SPA_BLKPTRSHIFT)). 927 * 928 * Thus, the level n blkid is: offset / 929 * ((2^datablkshift)*(2^(level*(indblkshift - SPA_BLKPTRSHIFT))) 930 * = offset / 2^(datablkshift + level * 931 * (indblkshift - SPA_BLKPTRSHIFT)) 932 * = offset >> (datablkshift + level * 933 * (indblkshift - SPA_BLKPTRSHIFT)) 934 */ 935 return (offset >> (dn->dn_datablkshift + level * 936 (dn->dn_indblkshift - SPA_BLKPTRSHIFT))); 937 } else { 938 ASSERT3U(offset, <, dn->dn_datablksz); 939 return (0); 940 } 941 } 942 943 static void 944 dbuf_read_done(zio_t *zio, arc_buf_t *buf, void *vdb) 945 { 946 dmu_buf_impl_t *db = vdb; 947 948 mutex_enter(&db->db_mtx); 949 ASSERT3U(db->db_state, ==, DB_READ); 950 /* 951 * All reads are synchronous, so we must have a hold on the dbuf 952 */ 953 ASSERT(refcount_count(&db->db_holds) > 0); 954 ASSERT(db->db_buf == NULL); 955 ASSERT(db->db.db_data == NULL); 956 if (buf == NULL) { 957 /* i/o error */ 958 ASSERT(zio == NULL || zio->io_error != 0); 959 ASSERT(db->db_blkid != DMU_BONUS_BLKID); 960 ASSERT3P(db->db_buf, ==, NULL); 961 db->db_state = DB_UNCACHED; 962 } else if (db->db_level == 0 && db->db_freed_in_flight) { 963 /* freed in flight */ 964 ASSERT(zio == NULL || zio->io_error == 0); 965 arc_release(buf, db); 966 bzero(buf->b_data, db->db.db_size); 967 arc_buf_freeze(buf); 968 db->db_freed_in_flight = FALSE; 969 dbuf_set_data(db, buf); 970 db->db_state = DB_CACHED; 971 } else { 972 /* success */ 973 ASSERT(zio == NULL || zio->io_error == 0); 974 dbuf_set_data(db, buf); 975 db->db_state = DB_CACHED; 976 } 977 cv_broadcast(&db->db_changed); 978 dbuf_rele_and_unlock(db, NULL, B_FALSE); 979 } 980 981 static void 982 dbuf_read_impl(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags) 983 { 984 dnode_t *dn; 985 zbookmark_phys_t zb; 986 arc_flags_t aflags = ARC_FLAG_NOWAIT; 987 988 DB_DNODE_ENTER(db); 989 dn = DB_DNODE(db); 990 ASSERT(!refcount_is_zero(&db->db_holds)); 991 /* We need the struct_rwlock to prevent db_blkptr from changing. */ 992 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock)); 993 ASSERT(MUTEX_HELD(&db->db_mtx)); 994 ASSERT(db->db_state == DB_UNCACHED); 995 ASSERT(db->db_buf == NULL); 996 997 if (db->db_blkid == DMU_BONUS_BLKID) { 998 int bonuslen = MIN(dn->dn_bonuslen, dn->dn_phys->dn_bonuslen); 999 1000 ASSERT3U(bonuslen, <=, db->db.db_size); 1001 db->db.db_data = zio_buf_alloc(DN_MAX_BONUSLEN); 1002 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER); 1003 if (bonuslen < DN_MAX_BONUSLEN) 1004 bzero(db->db.db_data, DN_MAX_BONUSLEN); 1005 if (bonuslen) 1006 bcopy(DN_BONUS(dn->dn_phys), db->db.db_data, bonuslen); 1007 DB_DNODE_EXIT(db); 1008 db->db_state = DB_CACHED; 1009 mutex_exit(&db->db_mtx); 1010 return; 1011 } 1012 1013 /* 1014 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync() 1015 * processes the delete record and clears the bp while we are waiting 1016 * for the dn_mtx (resulting in a "no" from block_freed). 1017 */ 1018 if (db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr) || 1019 (db->db_level == 0 && (dnode_block_freed(dn, db->db_blkid) || 1020 BP_IS_HOLE(db->db_blkptr)))) { 1021 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db); 1022 1023 dbuf_set_data(db, arc_alloc_buf(db->db_objset->os_spa, db, type, 1024 db->db.db_size)); 1025 bzero(db->db.db_data, db->db.db_size); 1026 1027 if (db->db_blkptr != NULL && db->db_level > 0 && 1028 BP_IS_HOLE(db->db_blkptr) && 1029 db->db_blkptr->blk_birth != 0) { 1030 blkptr_t *bps = db->db.db_data; 1031 for (int i = 0; i < ((1 << 1032 DB_DNODE(db)->dn_indblkshift) / sizeof (blkptr_t)); 1033 i++) { 1034 blkptr_t *bp = &bps[i]; 1035 ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==, 1036 1 << dn->dn_indblkshift); 1037 BP_SET_LSIZE(bp, 1038 BP_GET_LEVEL(db->db_blkptr) == 1 ? 1039 dn->dn_datablksz : 1040 BP_GET_LSIZE(db->db_blkptr)); 1041 BP_SET_TYPE(bp, BP_GET_TYPE(db->db_blkptr)); 1042 BP_SET_LEVEL(bp, 1043 BP_GET_LEVEL(db->db_blkptr) - 1); 1044 BP_SET_BIRTH(bp, db->db_blkptr->blk_birth, 0); 1045 } 1046 } 1047 DB_DNODE_EXIT(db); 1048 db->db_state = DB_CACHED; 1049 mutex_exit(&db->db_mtx); 1050 return; 1051 } 1052 1053 DB_DNODE_EXIT(db); 1054 1055 db->db_state = DB_READ; 1056 mutex_exit(&db->db_mtx); 1057 1058 if (DBUF_IS_L2CACHEABLE(db)) 1059 aflags |= ARC_FLAG_L2CACHE; 1060 1061 SET_BOOKMARK(&zb, db->db_objset->os_dsl_dataset ? 1062 db->db_objset->os_dsl_dataset->ds_object : DMU_META_OBJSET, 1063 db->db.db_object, db->db_level, db->db_blkid); 1064 1065 dbuf_add_ref(db, NULL); 1066 1067 (void) arc_read(zio, db->db_objset->os_spa, db->db_blkptr, 1068 dbuf_read_done, db, ZIO_PRIORITY_SYNC_READ, 1069 (flags & DB_RF_CANFAIL) ? ZIO_FLAG_CANFAIL : ZIO_FLAG_MUSTSUCCEED, 1070 &aflags, &zb); 1071 } 1072 1073 /* 1074 * This is our just-in-time copy function. It makes a copy of buffers that 1075 * have been modified in a previous transaction group before we access them in 1076 * the current active group. 1077 * 1078 * This function is used in three places: when we are dirtying a buffer for the 1079 * first time in a txg, when we are freeing a range in a dnode that includes 1080 * this buffer, and when we are accessing a buffer which was received compressed 1081 * and later referenced in a WRITE_BYREF record. 1082 * 1083 * Note that when we are called from dbuf_free_range() we do not put a hold on 1084 * the buffer, we just traverse the active dbuf list for the dnode. 1085 */ 1086 static void 1087 dbuf_fix_old_data(dmu_buf_impl_t *db, uint64_t txg) 1088 { 1089 dbuf_dirty_record_t *dr = db->db_last_dirty; 1090 1091 ASSERT(MUTEX_HELD(&db->db_mtx)); 1092 ASSERT(db->db.db_data != NULL); 1093 ASSERT(db->db_level == 0); 1094 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT); 1095 1096 if (dr == NULL || 1097 (dr->dt.dl.dr_data != 1098 ((db->db_blkid == DMU_BONUS_BLKID) ? db->db.db_data : db->db_buf))) 1099 return; 1100 1101 /* 1102 * If the last dirty record for this dbuf has not yet synced 1103 * and its referencing the dbuf data, either: 1104 * reset the reference to point to a new copy, 1105 * or (if there a no active holders) 1106 * just null out the current db_data pointer. 1107 */ 1108 ASSERT(dr->dr_txg >= txg - 2); 1109 if (db->db_blkid == DMU_BONUS_BLKID) { 1110 /* Note that the data bufs here are zio_bufs */ 1111 dr->dt.dl.dr_data = zio_buf_alloc(DN_MAX_BONUSLEN); 1112 arc_space_consume(DN_MAX_BONUSLEN, ARC_SPACE_OTHER); 1113 bcopy(db->db.db_data, dr->dt.dl.dr_data, DN_MAX_BONUSLEN); 1114 } else if (refcount_count(&db->db_holds) > db->db_dirtycnt) { 1115 int size = arc_buf_size(db->db_buf); 1116 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db); 1117 spa_t *spa = db->db_objset->os_spa; 1118 enum zio_compress compress_type = 1119 arc_get_compression(db->db_buf); 1120 1121 if (compress_type == ZIO_COMPRESS_OFF) { 1122 dr->dt.dl.dr_data = arc_alloc_buf(spa, db, type, size); 1123 } else { 1124 ASSERT3U(type, ==, ARC_BUFC_DATA); 1125 dr->dt.dl.dr_data = arc_alloc_compressed_buf(spa, db, 1126 size, arc_buf_lsize(db->db_buf), compress_type); 1127 } 1128 bcopy(db->db.db_data, dr->dt.dl.dr_data->b_data, size); 1129 } else { 1130 db->db_buf = NULL; 1131 dbuf_clear_data(db); 1132 } 1133 } 1134 1135 int 1136 dbuf_read(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags) 1137 { 1138 int err = 0; 1139 boolean_t prefetch; 1140 dnode_t *dn; 1141 1142 /* 1143 * We don't have to hold the mutex to check db_state because it 1144 * can't be freed while we have a hold on the buffer. 1145 */ 1146 ASSERT(!refcount_is_zero(&db->db_holds)); 1147 1148 if (db->db_state == DB_NOFILL) 1149 return (SET_ERROR(EIO)); 1150 1151 DB_DNODE_ENTER(db); 1152 dn = DB_DNODE(db); 1153 if ((flags & DB_RF_HAVESTRUCT) == 0) 1154 rw_enter(&dn->dn_struct_rwlock, RW_READER); 1155 1156 prefetch = db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID && 1157 (flags & DB_RF_NOPREFETCH) == 0 && dn != NULL && 1158 DBUF_IS_CACHEABLE(db); 1159 1160 mutex_enter(&db->db_mtx); 1161 if (db->db_state == DB_CACHED) { 1162 /* 1163 * If the arc buf is compressed, we need to decompress it to 1164 * read the data. This could happen during the "zfs receive" of 1165 * a stream which is compressed and deduplicated. 1166 */ 1167 if (db->db_buf != NULL && 1168 arc_get_compression(db->db_buf) != ZIO_COMPRESS_OFF) { 1169 dbuf_fix_old_data(db, 1170 spa_syncing_txg(dmu_objset_spa(db->db_objset))); 1171 err = arc_decompress(db->db_buf); 1172 dbuf_set_data(db, db->db_buf); 1173 } 1174 mutex_exit(&db->db_mtx); 1175 if (prefetch) 1176 dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1, B_TRUE); 1177 if ((flags & DB_RF_HAVESTRUCT) == 0) 1178 rw_exit(&dn->dn_struct_rwlock); 1179 DB_DNODE_EXIT(db); 1180 } else if (db->db_state == DB_UNCACHED) { 1181 spa_t *spa = dn->dn_objset->os_spa; 1182 boolean_t need_wait = B_FALSE; 1183 1184 if (zio == NULL && 1185 db->db_blkptr != NULL && !BP_IS_HOLE(db->db_blkptr)) { 1186 zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL); 1187 need_wait = B_TRUE; 1188 } 1189 dbuf_read_impl(db, zio, flags); 1190 1191 /* dbuf_read_impl has dropped db_mtx for us */ 1192 1193 if (prefetch) 1194 dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1, B_TRUE); 1195 1196 if ((flags & DB_RF_HAVESTRUCT) == 0) 1197 rw_exit(&dn->dn_struct_rwlock); 1198 DB_DNODE_EXIT(db); 1199 1200 if (need_wait) 1201 err = zio_wait(zio); 1202 } else { 1203 /* 1204 * Another reader came in while the dbuf was in flight 1205 * between UNCACHED and CACHED. Either a writer will finish 1206 * writing the buffer (sending the dbuf to CACHED) or the 1207 * first reader's request will reach the read_done callback 1208 * and send the dbuf to CACHED. Otherwise, a failure 1209 * occurred and the dbuf went to UNCACHED. 1210 */ 1211 mutex_exit(&db->db_mtx); 1212 if (prefetch) 1213 dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1, B_TRUE); 1214 if ((flags & DB_RF_HAVESTRUCT) == 0) 1215 rw_exit(&dn->dn_struct_rwlock); 1216 DB_DNODE_EXIT(db); 1217 1218 /* Skip the wait per the caller's request. */ 1219 mutex_enter(&db->db_mtx); 1220 if ((flags & DB_RF_NEVERWAIT) == 0) { 1221 while (db->db_state == DB_READ || 1222 db->db_state == DB_FILL) { 1223 ASSERT(db->db_state == DB_READ || 1224 (flags & DB_RF_HAVESTRUCT) == 0); 1225 DTRACE_PROBE2(blocked__read, dmu_buf_impl_t *, 1226 db, zio_t *, zio); 1227 cv_wait(&db->db_changed, &db->db_mtx); 1228 } 1229 if (db->db_state == DB_UNCACHED) 1230 err = SET_ERROR(EIO); 1231 } 1232 mutex_exit(&db->db_mtx); 1233 } 1234 1235 return (err); 1236 } 1237 1238 static void 1239 dbuf_noread(dmu_buf_impl_t *db) 1240 { 1241 ASSERT(!refcount_is_zero(&db->db_holds)); 1242 ASSERT(db->db_blkid != DMU_BONUS_BLKID); 1243 mutex_enter(&db->db_mtx); 1244 while (db->db_state == DB_READ || db->db_state == DB_FILL) 1245 cv_wait(&db->db_changed, &db->db_mtx); 1246 if (db->db_state == DB_UNCACHED) { 1247 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db); 1248 spa_t *spa = db->db_objset->os_spa; 1249 1250 ASSERT(db->db_buf == NULL); 1251 ASSERT(db->db.db_data == NULL); 1252 dbuf_set_data(db, arc_alloc_buf(spa, db, type, db->db.db_size)); 1253 db->db_state = DB_FILL; 1254 } else if (db->db_state == DB_NOFILL) { 1255 dbuf_clear_data(db); 1256 } else { 1257 ASSERT3U(db->db_state, ==, DB_CACHED); 1258 } 1259 mutex_exit(&db->db_mtx); 1260 } 1261 1262 void 1263 dbuf_unoverride(dbuf_dirty_record_t *dr) 1264 { 1265 dmu_buf_impl_t *db = dr->dr_dbuf; 1266 blkptr_t *bp = &dr->dt.dl.dr_overridden_by; 1267 uint64_t txg = dr->dr_txg; 1268 1269 ASSERT(MUTEX_HELD(&db->db_mtx)); 1270 /* 1271 * This assert is valid because dmu_sync() expects to be called by 1272 * a zilog's get_data while holding a range lock. This call only 1273 * comes from dbuf_dirty() callers who must also hold a range lock. 1274 */ 1275 ASSERT(dr->dt.dl.dr_override_state != DR_IN_DMU_SYNC); 1276 ASSERT(db->db_level == 0); 1277 1278 if (db->db_blkid == DMU_BONUS_BLKID || 1279 dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN) 1280 return; 1281 1282 ASSERT(db->db_data_pending != dr); 1283 1284 /* free this block */ 1285 if (!BP_IS_HOLE(bp) && !dr->dt.dl.dr_nopwrite) 1286 zio_free(db->db_objset->os_spa, txg, bp); 1287 1288 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN; 1289 dr->dt.dl.dr_nopwrite = B_FALSE; 1290 1291 /* 1292 * Release the already-written buffer, so we leave it in 1293 * a consistent dirty state. Note that all callers are 1294 * modifying the buffer, so they will immediately do 1295 * another (redundant) arc_release(). Therefore, leave 1296 * the buf thawed to save the effort of freezing & 1297 * immediately re-thawing it. 1298 */ 1299 arc_release(dr->dt.dl.dr_data, db); 1300 } 1301 1302 /* 1303 * Evict (if its unreferenced) or clear (if its referenced) any level-0 1304 * data blocks in the free range, so that any future readers will find 1305 * empty blocks. 1306 */ 1307 void 1308 dbuf_free_range(dnode_t *dn, uint64_t start_blkid, uint64_t end_blkid, 1309 dmu_tx_t *tx) 1310 { 1311 dmu_buf_impl_t db_search; 1312 dmu_buf_impl_t *db, *db_next; 1313 uint64_t txg = tx->tx_txg; 1314 avl_index_t where; 1315 1316 if (end_blkid > dn->dn_maxblkid && 1317 !(start_blkid == DMU_SPILL_BLKID || end_blkid == DMU_SPILL_BLKID)) 1318 end_blkid = dn->dn_maxblkid; 1319 dprintf_dnode(dn, "start=%llu end=%llu\n", start_blkid, end_blkid); 1320 1321 db_search.db_level = 0; 1322 db_search.db_blkid = start_blkid; 1323 db_search.db_state = DB_SEARCH; 1324 1325 mutex_enter(&dn->dn_dbufs_mtx); 1326 db = avl_find(&dn->dn_dbufs, &db_search, &where); 1327 ASSERT3P(db, ==, NULL); 1328 1329 db = avl_nearest(&dn->dn_dbufs, where, AVL_AFTER); 1330 1331 for (; db != NULL; db = db_next) { 1332 db_next = AVL_NEXT(&dn->dn_dbufs, db); 1333 ASSERT(db->db_blkid != DMU_BONUS_BLKID); 1334 1335 if (db->db_level != 0 || db->db_blkid > end_blkid) { 1336 break; 1337 } 1338 ASSERT3U(db->db_blkid, >=, start_blkid); 1339 1340 /* found a level 0 buffer in the range */ 1341 mutex_enter(&db->db_mtx); 1342 if (dbuf_undirty(db, tx)) { 1343 /* mutex has been dropped and dbuf destroyed */ 1344 continue; 1345 } 1346 1347 if (db->db_state == DB_UNCACHED || 1348 db->db_state == DB_NOFILL || 1349 db->db_state == DB_EVICTING) { 1350 ASSERT(db->db.db_data == NULL); 1351 mutex_exit(&db->db_mtx); 1352 continue; 1353 } 1354 if (db->db_state == DB_READ || db->db_state == DB_FILL) { 1355 /* will be handled in dbuf_read_done or dbuf_rele */ 1356 db->db_freed_in_flight = TRUE; 1357 mutex_exit(&db->db_mtx); 1358 continue; 1359 } 1360 if (refcount_count(&db->db_holds) == 0) { 1361 ASSERT(db->db_buf); 1362 dbuf_destroy(db); 1363 continue; 1364 } 1365 /* The dbuf is referenced */ 1366 1367 if (db->db_last_dirty != NULL) { 1368 dbuf_dirty_record_t *dr = db->db_last_dirty; 1369 1370 if (dr->dr_txg == txg) { 1371 /* 1372 * This buffer is "in-use", re-adjust the file 1373 * size to reflect that this buffer may 1374 * contain new data when we sync. 1375 */ 1376 if (db->db_blkid != DMU_SPILL_BLKID && 1377 db->db_blkid > dn->dn_maxblkid) 1378 dn->dn_maxblkid = db->db_blkid; 1379 dbuf_unoverride(dr); 1380 } else { 1381 /* 1382 * This dbuf is not dirty in the open context. 1383 * Either uncache it (if its not referenced in 1384 * the open context) or reset its contents to 1385 * empty. 1386 */ 1387 dbuf_fix_old_data(db, txg); 1388 } 1389 } 1390 /* clear the contents if its cached */ 1391 if (db->db_state == DB_CACHED) { 1392 ASSERT(db->db.db_data != NULL); 1393 arc_release(db->db_buf, db); 1394 bzero(db->db.db_data, db->db.db_size); 1395 arc_buf_freeze(db->db_buf); 1396 } 1397 1398 mutex_exit(&db->db_mtx); 1399 } 1400 mutex_exit(&dn->dn_dbufs_mtx); 1401 } 1402 1403 void 1404 dbuf_new_size(dmu_buf_impl_t *db, int size, dmu_tx_t *tx) 1405 { 1406 arc_buf_t *buf, *obuf; 1407 int osize = db->db.db_size; 1408 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db); 1409 dnode_t *dn; 1410 1411 ASSERT(db->db_blkid != DMU_BONUS_BLKID); 1412 1413 DB_DNODE_ENTER(db); 1414 dn = DB_DNODE(db); 1415 1416 /* XXX does *this* func really need the lock? */ 1417 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock)); 1418 1419 /* 1420 * This call to dmu_buf_will_dirty() with the dn_struct_rwlock held 1421 * is OK, because there can be no other references to the db 1422 * when we are changing its size, so no concurrent DB_FILL can 1423 * be happening. 1424 */ 1425 /* 1426 * XXX we should be doing a dbuf_read, checking the return 1427 * value and returning that up to our callers 1428 */ 1429 dmu_buf_will_dirty(&db->db, tx); 1430 1431 /* create the data buffer for the new block */ 1432 buf = arc_alloc_buf(dn->dn_objset->os_spa, db, type, size); 1433 1434 /* copy old block data to the new block */ 1435 obuf = db->db_buf; 1436 bcopy(obuf->b_data, buf->b_data, MIN(osize, size)); 1437 /* zero the remainder */ 1438 if (size > osize) 1439 bzero((uint8_t *)buf->b_data + osize, size - osize); 1440 1441 mutex_enter(&db->db_mtx); 1442 dbuf_set_data(db, buf); 1443 arc_buf_destroy(obuf, db); 1444 db->db.db_size = size; 1445 1446 if (db->db_level == 0) { 1447 ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg); 1448 db->db_last_dirty->dt.dl.dr_data = buf; 1449 } 1450 mutex_exit(&db->db_mtx); 1451 1452 dmu_objset_willuse_space(dn->dn_objset, size - osize, tx); 1453 DB_DNODE_EXIT(db); 1454 } 1455 1456 void 1457 dbuf_release_bp(dmu_buf_impl_t *db) 1458 { 1459 objset_t *os = db->db_objset; 1460 1461 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os))); 1462 ASSERT(arc_released(os->os_phys_buf) || 1463 list_link_active(&os->os_dsl_dataset->ds_synced_link)); 1464 ASSERT(db->db_parent == NULL || arc_released(db->db_parent->db_buf)); 1465 1466 (void) arc_release(db->db_buf, db); 1467 } 1468 1469 /* 1470 * We already have a dirty record for this TXG, and we are being 1471 * dirtied again. 1472 */ 1473 static void 1474 dbuf_redirty(dbuf_dirty_record_t *dr) 1475 { 1476 dmu_buf_impl_t *db = dr->dr_dbuf; 1477 1478 ASSERT(MUTEX_HELD(&db->db_mtx)); 1479 1480 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID) { 1481 /* 1482 * If this buffer has already been written out, 1483 * we now need to reset its state. 1484 */ 1485 dbuf_unoverride(dr); 1486 if (db->db.db_object != DMU_META_DNODE_OBJECT && 1487 db->db_state != DB_NOFILL) { 1488 /* Already released on initial dirty, so just thaw. */ 1489 ASSERT(arc_released(db->db_buf)); 1490 arc_buf_thaw(db->db_buf); 1491 } 1492 } 1493 } 1494 1495 dbuf_dirty_record_t * 1496 dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx) 1497 { 1498 dnode_t *dn; 1499 objset_t *os; 1500 dbuf_dirty_record_t **drp, *dr; 1501 int drop_struct_lock = FALSE; 1502 int txgoff = tx->tx_txg & TXG_MASK; 1503 1504 ASSERT(tx->tx_txg != 0); 1505 ASSERT(!refcount_is_zero(&db->db_holds)); 1506 DMU_TX_DIRTY_BUF(tx, db); 1507 1508 DB_DNODE_ENTER(db); 1509 dn = DB_DNODE(db); 1510 /* 1511 * Shouldn't dirty a regular buffer in syncing context. Private 1512 * objects may be dirtied in syncing context, but only if they 1513 * were already pre-dirtied in open context. 1514 */ 1515 #ifdef DEBUG 1516 if (dn->dn_objset->os_dsl_dataset != NULL) { 1517 rrw_enter(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock, 1518 RW_READER, FTAG); 1519 } 1520 ASSERT(!dmu_tx_is_syncing(tx) || 1521 BP_IS_HOLE(dn->dn_objset->os_rootbp) || 1522 DMU_OBJECT_IS_SPECIAL(dn->dn_object) || 1523 dn->dn_objset->os_dsl_dataset == NULL); 1524 if (dn->dn_objset->os_dsl_dataset != NULL) 1525 rrw_exit(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock, FTAG); 1526 #endif 1527 /* 1528 * We make this assert for private objects as well, but after we 1529 * check if we're already dirty. They are allowed to re-dirty 1530 * in syncing context. 1531 */ 1532 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT || 1533 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx == 1534 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN)); 1535 1536 mutex_enter(&db->db_mtx); 1537 /* 1538 * XXX make this true for indirects too? The problem is that 1539 * transactions created with dmu_tx_create_assigned() from 1540 * syncing context don't bother holding ahead. 1541 */ 1542 ASSERT(db->db_level != 0 || 1543 db->db_state == DB_CACHED || db->db_state == DB_FILL || 1544 db->db_state == DB_NOFILL); 1545 1546 mutex_enter(&dn->dn_mtx); 1547 /* 1548 * Don't set dirtyctx to SYNC if we're just modifying this as we 1549 * initialize the objset. 1550 */ 1551 if (dn->dn_dirtyctx == DN_UNDIRTIED) { 1552 if (dn->dn_objset->os_dsl_dataset != NULL) { 1553 rrw_enter(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock, 1554 RW_READER, FTAG); 1555 } 1556 if (!BP_IS_HOLE(dn->dn_objset->os_rootbp)) { 1557 dn->dn_dirtyctx = (dmu_tx_is_syncing(tx) ? 1558 DN_DIRTY_SYNC : DN_DIRTY_OPEN); 1559 ASSERT(dn->dn_dirtyctx_firstset == NULL); 1560 dn->dn_dirtyctx_firstset = kmem_alloc(1, KM_SLEEP); 1561 } 1562 if (dn->dn_objset->os_dsl_dataset != NULL) { 1563 rrw_exit(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock, 1564 FTAG); 1565 } 1566 } 1567 mutex_exit(&dn->dn_mtx); 1568 1569 if (db->db_blkid == DMU_SPILL_BLKID) 1570 dn->dn_have_spill = B_TRUE; 1571 1572 /* 1573 * If this buffer is already dirty, we're done. 1574 */ 1575 drp = &db->db_last_dirty; 1576 ASSERT(*drp == NULL || (*drp)->dr_txg <= tx->tx_txg || 1577 db->db.db_object == DMU_META_DNODE_OBJECT); 1578 while ((dr = *drp) != NULL && dr->dr_txg > tx->tx_txg) 1579 drp = &dr->dr_next; 1580 if (dr && dr->dr_txg == tx->tx_txg) { 1581 DB_DNODE_EXIT(db); 1582 1583 dbuf_redirty(dr); 1584 mutex_exit(&db->db_mtx); 1585 return (dr); 1586 } 1587 1588 /* 1589 * Only valid if not already dirty. 1590 */ 1591 ASSERT(dn->dn_object == 0 || 1592 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx == 1593 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN)); 1594 1595 ASSERT3U(dn->dn_nlevels, >, db->db_level); 1596 1597 /* 1598 * We should only be dirtying in syncing context if it's the 1599 * mos or we're initializing the os or it's a special object. 1600 * However, we are allowed to dirty in syncing context provided 1601 * we already dirtied it in open context. Hence we must make 1602 * this assertion only if we're not already dirty. 1603 */ 1604 os = dn->dn_objset; 1605 VERIFY3U(tx->tx_txg, <=, spa_final_dirty_txg(os->os_spa)); 1606 #ifdef DEBUG 1607 if (dn->dn_objset->os_dsl_dataset != NULL) 1608 rrw_enter(&os->os_dsl_dataset->ds_bp_rwlock, RW_READER, FTAG); 1609 ASSERT(!dmu_tx_is_syncing(tx) || DMU_OBJECT_IS_SPECIAL(dn->dn_object) || 1610 os->os_dsl_dataset == NULL || BP_IS_HOLE(os->os_rootbp)); 1611 if (dn->dn_objset->os_dsl_dataset != NULL) 1612 rrw_exit(&os->os_dsl_dataset->ds_bp_rwlock, FTAG); 1613 #endif 1614 ASSERT(db->db.db_size != 0); 1615 1616 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size); 1617 1618 if (db->db_blkid != DMU_BONUS_BLKID) { 1619 dmu_objset_willuse_space(os, db->db.db_size, tx); 1620 } 1621 1622 /* 1623 * If this buffer is dirty in an old transaction group we need 1624 * to make a copy of it so that the changes we make in this 1625 * transaction group won't leak out when we sync the older txg. 1626 */ 1627 dr = kmem_zalloc(sizeof (dbuf_dirty_record_t), KM_SLEEP); 1628 if (db->db_level == 0) { 1629 void *data_old = db->db_buf; 1630 1631 if (db->db_state != DB_NOFILL) { 1632 if (db->db_blkid == DMU_BONUS_BLKID) { 1633 dbuf_fix_old_data(db, tx->tx_txg); 1634 data_old = db->db.db_data; 1635 } else if (db->db.db_object != DMU_META_DNODE_OBJECT) { 1636 /* 1637 * Release the data buffer from the cache so 1638 * that we can modify it without impacting 1639 * possible other users of this cached data 1640 * block. Note that indirect blocks and 1641 * private objects are not released until the 1642 * syncing state (since they are only modified 1643 * then). 1644 */ 1645 arc_release(db->db_buf, db); 1646 dbuf_fix_old_data(db, tx->tx_txg); 1647 data_old = db->db_buf; 1648 } 1649 ASSERT(data_old != NULL); 1650 } 1651 dr->dt.dl.dr_data = data_old; 1652 } else { 1653 mutex_init(&dr->dt.di.dr_mtx, NULL, MUTEX_DEFAULT, NULL); 1654 list_create(&dr->dt.di.dr_children, 1655 sizeof (dbuf_dirty_record_t), 1656 offsetof(dbuf_dirty_record_t, dr_dirty_node)); 1657 } 1658 if (db->db_blkid != DMU_BONUS_BLKID && os->os_dsl_dataset != NULL) 1659 dr->dr_accounted = db->db.db_size; 1660 dr->dr_dbuf = db; 1661 dr->dr_txg = tx->tx_txg; 1662 dr->dr_next = *drp; 1663 *drp = dr; 1664 1665 /* 1666 * We could have been freed_in_flight between the dbuf_noread 1667 * and dbuf_dirty. We win, as though the dbuf_noread() had 1668 * happened after the free. 1669 */ 1670 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID && 1671 db->db_blkid != DMU_SPILL_BLKID) { 1672 mutex_enter(&dn->dn_mtx); 1673 if (dn->dn_free_ranges[txgoff] != NULL) { 1674 range_tree_clear(dn->dn_free_ranges[txgoff], 1675 db->db_blkid, 1); 1676 } 1677 mutex_exit(&dn->dn_mtx); 1678 db->db_freed_in_flight = FALSE; 1679 } 1680 1681 /* 1682 * This buffer is now part of this txg 1683 */ 1684 dbuf_add_ref(db, (void *)(uintptr_t)tx->tx_txg); 1685 db->db_dirtycnt += 1; 1686 ASSERT3U(db->db_dirtycnt, <=, 3); 1687 1688 mutex_exit(&db->db_mtx); 1689 1690 if (db->db_blkid == DMU_BONUS_BLKID || 1691 db->db_blkid == DMU_SPILL_BLKID) { 1692 mutex_enter(&dn->dn_mtx); 1693 ASSERT(!list_link_active(&dr->dr_dirty_node)); 1694 list_insert_tail(&dn->dn_dirty_records[txgoff], dr); 1695 mutex_exit(&dn->dn_mtx); 1696 dnode_setdirty(dn, tx); 1697 DB_DNODE_EXIT(db); 1698 return (dr); 1699 } 1700 1701 /* 1702 * The dn_struct_rwlock prevents db_blkptr from changing 1703 * due to a write from syncing context completing 1704 * while we are running, so we want to acquire it before 1705 * looking at db_blkptr. 1706 */ 1707 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) { 1708 rw_enter(&dn->dn_struct_rwlock, RW_READER); 1709 drop_struct_lock = TRUE; 1710 } 1711 1712 /* 1713 * We need to hold the dn_struct_rwlock to make this assertion, 1714 * because it protects dn_phys / dn_next_nlevels from changing. 1715 */ 1716 ASSERT((dn->dn_phys->dn_nlevels == 0 && db->db_level == 0) || 1717 dn->dn_phys->dn_nlevels > db->db_level || 1718 dn->dn_next_nlevels[txgoff] > db->db_level || 1719 dn->dn_next_nlevels[(tx->tx_txg-1) & TXG_MASK] > db->db_level || 1720 dn->dn_next_nlevels[(tx->tx_txg-2) & TXG_MASK] > db->db_level); 1721 1722 /* 1723 * If we are overwriting a dedup BP, then unless it is snapshotted, 1724 * when we get to syncing context we will need to decrement its 1725 * refcount in the DDT. Prefetch the relevant DDT block so that 1726 * syncing context won't have to wait for the i/o. 1727 */ 1728 ddt_prefetch(os->os_spa, db->db_blkptr); 1729 1730 if (db->db_level == 0) { 1731 dnode_new_blkid(dn, db->db_blkid, tx, drop_struct_lock); 1732 ASSERT(dn->dn_maxblkid >= db->db_blkid); 1733 } 1734 1735 if (db->db_level+1 < dn->dn_nlevels) { 1736 dmu_buf_impl_t *parent = db->db_parent; 1737 dbuf_dirty_record_t *di; 1738 int parent_held = FALSE; 1739 1740 if (db->db_parent == NULL || db->db_parent == dn->dn_dbuf) { 1741 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 1742 1743 parent = dbuf_hold_level(dn, db->db_level+1, 1744 db->db_blkid >> epbs, FTAG); 1745 ASSERT(parent != NULL); 1746 parent_held = TRUE; 1747 } 1748 if (drop_struct_lock) 1749 rw_exit(&dn->dn_struct_rwlock); 1750 ASSERT3U(db->db_level+1, ==, parent->db_level); 1751 di = dbuf_dirty(parent, tx); 1752 if (parent_held) 1753 dbuf_rele(parent, FTAG); 1754 1755 mutex_enter(&db->db_mtx); 1756 /* 1757 * Since we've dropped the mutex, it's possible that 1758 * dbuf_undirty() might have changed this out from under us. 1759 */ 1760 if (db->db_last_dirty == dr || 1761 dn->dn_object == DMU_META_DNODE_OBJECT) { 1762 mutex_enter(&di->dt.di.dr_mtx); 1763 ASSERT3U(di->dr_txg, ==, tx->tx_txg); 1764 ASSERT(!list_link_active(&dr->dr_dirty_node)); 1765 list_insert_tail(&di->dt.di.dr_children, dr); 1766 mutex_exit(&di->dt.di.dr_mtx); 1767 dr->dr_parent = di; 1768 } 1769 mutex_exit(&db->db_mtx); 1770 } else { 1771 ASSERT(db->db_level+1 == dn->dn_nlevels); 1772 ASSERT(db->db_blkid < dn->dn_nblkptr); 1773 ASSERT(db->db_parent == NULL || db->db_parent == dn->dn_dbuf); 1774 mutex_enter(&dn->dn_mtx); 1775 ASSERT(!list_link_active(&dr->dr_dirty_node)); 1776 list_insert_tail(&dn->dn_dirty_records[txgoff], dr); 1777 mutex_exit(&dn->dn_mtx); 1778 if (drop_struct_lock) 1779 rw_exit(&dn->dn_struct_rwlock); 1780 } 1781 1782 dnode_setdirty(dn, tx); 1783 DB_DNODE_EXIT(db); 1784 return (dr); 1785 } 1786 1787 /* 1788 * Undirty a buffer in the transaction group referenced by the given 1789 * transaction. Return whether this evicted the dbuf. 1790 */ 1791 static boolean_t 1792 dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx) 1793 { 1794 dnode_t *dn; 1795 uint64_t txg = tx->tx_txg; 1796 dbuf_dirty_record_t *dr, **drp; 1797 1798 ASSERT(txg != 0); 1799 1800 /* 1801 * Due to our use of dn_nlevels below, this can only be called 1802 * in open context, unless we are operating on the MOS. 1803 * From syncing context, dn_nlevels may be different from the 1804 * dn_nlevels used when dbuf was dirtied. 1805 */ 1806 ASSERT(db->db_objset == 1807 dmu_objset_pool(db->db_objset)->dp_meta_objset || 1808 txg != spa_syncing_txg(dmu_objset_spa(db->db_objset))); 1809 ASSERT(db->db_blkid != DMU_BONUS_BLKID); 1810 ASSERT0(db->db_level); 1811 ASSERT(MUTEX_HELD(&db->db_mtx)); 1812 1813 /* 1814 * If this buffer is not dirty, we're done. 1815 */ 1816 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next) 1817 if (dr->dr_txg <= txg) 1818 break; 1819 if (dr == NULL || dr->dr_txg < txg) 1820 return (B_FALSE); 1821 ASSERT(dr->dr_txg == txg); 1822 ASSERT(dr->dr_dbuf == db); 1823 1824 DB_DNODE_ENTER(db); 1825 dn = DB_DNODE(db); 1826 1827 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size); 1828 1829 ASSERT(db->db.db_size != 0); 1830 1831 dsl_pool_undirty_space(dmu_objset_pool(dn->dn_objset), 1832 dr->dr_accounted, txg); 1833 1834 *drp = dr->dr_next; 1835 1836 /* 1837 * Note that there are three places in dbuf_dirty() 1838 * where this dirty record may be put on a list. 1839 * Make sure to do a list_remove corresponding to 1840 * every one of those list_insert calls. 1841 */ 1842 if (dr->dr_parent) { 1843 mutex_enter(&dr->dr_parent->dt.di.dr_mtx); 1844 list_remove(&dr->dr_parent->dt.di.dr_children, dr); 1845 mutex_exit(&dr->dr_parent->dt.di.dr_mtx); 1846 } else if (db->db_blkid == DMU_SPILL_BLKID || 1847 db->db_level + 1 == dn->dn_nlevels) { 1848 ASSERT(db->db_blkptr == NULL || db->db_parent == dn->dn_dbuf); 1849 mutex_enter(&dn->dn_mtx); 1850 list_remove(&dn->dn_dirty_records[txg & TXG_MASK], dr); 1851 mutex_exit(&dn->dn_mtx); 1852 } 1853 DB_DNODE_EXIT(db); 1854 1855 if (db->db_state != DB_NOFILL) { 1856 dbuf_unoverride(dr); 1857 1858 ASSERT(db->db_buf != NULL); 1859 ASSERT(dr->dt.dl.dr_data != NULL); 1860 if (dr->dt.dl.dr_data != db->db_buf) 1861 arc_buf_destroy(dr->dt.dl.dr_data, db); 1862 } 1863 1864 kmem_free(dr, sizeof (dbuf_dirty_record_t)); 1865 1866 ASSERT(db->db_dirtycnt > 0); 1867 db->db_dirtycnt -= 1; 1868 1869 if (refcount_remove(&db->db_holds, (void *)(uintptr_t)txg) == 0) { 1870 ASSERT(db->db_state == DB_NOFILL || arc_released(db->db_buf)); 1871 dbuf_destroy(db); 1872 return (B_TRUE); 1873 } 1874 1875 return (B_FALSE); 1876 } 1877 1878 void 1879 dmu_buf_will_dirty(dmu_buf_t *db_fake, dmu_tx_t *tx) 1880 { 1881 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 1882 int rf = DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH; 1883 1884 ASSERT(tx->tx_txg != 0); 1885 ASSERT(!refcount_is_zero(&db->db_holds)); 1886 1887 /* 1888 * Quick check for dirtyness. For already dirty blocks, this 1889 * reduces runtime of this function by >90%, and overall performance 1890 * by 50% for some workloads (e.g. file deletion with indirect blocks 1891 * cached). 1892 */ 1893 mutex_enter(&db->db_mtx); 1894 dbuf_dirty_record_t *dr; 1895 for (dr = db->db_last_dirty; 1896 dr != NULL && dr->dr_txg >= tx->tx_txg; dr = dr->dr_next) { 1897 /* 1898 * It's possible that it is already dirty but not cached, 1899 * because there are some calls to dbuf_dirty() that don't 1900 * go through dmu_buf_will_dirty(). 1901 */ 1902 if (dr->dr_txg == tx->tx_txg && db->db_state == DB_CACHED) { 1903 /* This dbuf is already dirty and cached. */ 1904 dbuf_redirty(dr); 1905 mutex_exit(&db->db_mtx); 1906 return; 1907 } 1908 } 1909 mutex_exit(&db->db_mtx); 1910 1911 DB_DNODE_ENTER(db); 1912 if (RW_WRITE_HELD(&DB_DNODE(db)->dn_struct_rwlock)) 1913 rf |= DB_RF_HAVESTRUCT; 1914 DB_DNODE_EXIT(db); 1915 (void) dbuf_read(db, NULL, rf); 1916 (void) dbuf_dirty(db, tx); 1917 } 1918 1919 void 1920 dmu_buf_will_not_fill(dmu_buf_t *db_fake, dmu_tx_t *tx) 1921 { 1922 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 1923 1924 db->db_state = DB_NOFILL; 1925 1926 dmu_buf_will_fill(db_fake, tx); 1927 } 1928 1929 void 1930 dmu_buf_will_fill(dmu_buf_t *db_fake, dmu_tx_t *tx) 1931 { 1932 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 1933 1934 ASSERT(db->db_blkid != DMU_BONUS_BLKID); 1935 ASSERT(tx->tx_txg != 0); 1936 ASSERT(db->db_level == 0); 1937 ASSERT(!refcount_is_zero(&db->db_holds)); 1938 1939 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT || 1940 dmu_tx_private_ok(tx)); 1941 1942 dbuf_noread(db); 1943 (void) dbuf_dirty(db, tx); 1944 } 1945 1946 #pragma weak dmu_buf_fill_done = dbuf_fill_done 1947 /* ARGSUSED */ 1948 void 1949 dbuf_fill_done(dmu_buf_impl_t *db, dmu_tx_t *tx) 1950 { 1951 mutex_enter(&db->db_mtx); 1952 DBUF_VERIFY(db); 1953 1954 if (db->db_state == DB_FILL) { 1955 if (db->db_level == 0 && db->db_freed_in_flight) { 1956 ASSERT(db->db_blkid != DMU_BONUS_BLKID); 1957 /* we were freed while filling */ 1958 /* XXX dbuf_undirty? */ 1959 bzero(db->db.db_data, db->db.db_size); 1960 db->db_freed_in_flight = FALSE; 1961 } 1962 db->db_state = DB_CACHED; 1963 cv_broadcast(&db->db_changed); 1964 } 1965 mutex_exit(&db->db_mtx); 1966 } 1967 1968 void 1969 dmu_buf_write_embedded(dmu_buf_t *dbuf, void *data, 1970 bp_embedded_type_t etype, enum zio_compress comp, 1971 int uncompressed_size, int compressed_size, int byteorder, 1972 dmu_tx_t *tx) 1973 { 1974 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf; 1975 struct dirty_leaf *dl; 1976 dmu_object_type_t type; 1977 1978 if (etype == BP_EMBEDDED_TYPE_DATA) { 1979 ASSERT(spa_feature_is_active(dmu_objset_spa(db->db_objset), 1980 SPA_FEATURE_EMBEDDED_DATA)); 1981 } 1982 1983 DB_DNODE_ENTER(db); 1984 type = DB_DNODE(db)->dn_type; 1985 DB_DNODE_EXIT(db); 1986 1987 ASSERT0(db->db_level); 1988 ASSERT(db->db_blkid != DMU_BONUS_BLKID); 1989 1990 dmu_buf_will_not_fill(dbuf, tx); 1991 1992 ASSERT3U(db->db_last_dirty->dr_txg, ==, tx->tx_txg); 1993 dl = &db->db_last_dirty->dt.dl; 1994 encode_embedded_bp_compressed(&dl->dr_overridden_by, 1995 data, comp, uncompressed_size, compressed_size); 1996 BPE_SET_ETYPE(&dl->dr_overridden_by, etype); 1997 BP_SET_TYPE(&dl->dr_overridden_by, type); 1998 BP_SET_LEVEL(&dl->dr_overridden_by, 0); 1999 BP_SET_BYTEORDER(&dl->dr_overridden_by, byteorder); 2000 2001 dl->dr_override_state = DR_OVERRIDDEN; 2002 dl->dr_overridden_by.blk_birth = db->db_last_dirty->dr_txg; 2003 } 2004 2005 /* 2006 * Directly assign a provided arc buf to a given dbuf if it's not referenced 2007 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf. 2008 */ 2009 void 2010 dbuf_assign_arcbuf(dmu_buf_impl_t *db, arc_buf_t *buf, dmu_tx_t *tx) 2011 { 2012 ASSERT(!refcount_is_zero(&db->db_holds)); 2013 ASSERT(db->db_blkid != DMU_BONUS_BLKID); 2014 ASSERT(db->db_level == 0); 2015 ASSERT3U(dbuf_is_metadata(db), ==, arc_is_metadata(buf)); 2016 ASSERT(buf != NULL); 2017 ASSERT(arc_buf_lsize(buf) == db->db.db_size); 2018 ASSERT(tx->tx_txg != 0); 2019 2020 arc_return_buf(buf, db); 2021 ASSERT(arc_released(buf)); 2022 2023 mutex_enter(&db->db_mtx); 2024 2025 while (db->db_state == DB_READ || db->db_state == DB_FILL) 2026 cv_wait(&db->db_changed, &db->db_mtx); 2027 2028 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_UNCACHED); 2029 2030 if (db->db_state == DB_CACHED && 2031 refcount_count(&db->db_holds) - 1 > db->db_dirtycnt) { 2032 mutex_exit(&db->db_mtx); 2033 (void) dbuf_dirty(db, tx); 2034 bcopy(buf->b_data, db->db.db_data, db->db.db_size); 2035 arc_buf_destroy(buf, db); 2036 xuio_stat_wbuf_copied(); 2037 return; 2038 } 2039 2040 xuio_stat_wbuf_nocopy(); 2041 if (db->db_state == DB_CACHED) { 2042 dbuf_dirty_record_t *dr = db->db_last_dirty; 2043 2044 ASSERT(db->db_buf != NULL); 2045 if (dr != NULL && dr->dr_txg == tx->tx_txg) { 2046 ASSERT(dr->dt.dl.dr_data == db->db_buf); 2047 if (!arc_released(db->db_buf)) { 2048 ASSERT(dr->dt.dl.dr_override_state == 2049 DR_OVERRIDDEN); 2050 arc_release(db->db_buf, db); 2051 } 2052 dr->dt.dl.dr_data = buf; 2053 arc_buf_destroy(db->db_buf, db); 2054 } else if (dr == NULL || dr->dt.dl.dr_data != db->db_buf) { 2055 arc_release(db->db_buf, db); 2056 arc_buf_destroy(db->db_buf, db); 2057 } 2058 db->db_buf = NULL; 2059 } 2060 ASSERT(db->db_buf == NULL); 2061 dbuf_set_data(db, buf); 2062 db->db_state = DB_FILL; 2063 mutex_exit(&db->db_mtx); 2064 (void) dbuf_dirty(db, tx); 2065 dmu_buf_fill_done(&db->db, tx); 2066 } 2067 2068 void 2069 dbuf_destroy(dmu_buf_impl_t *db) 2070 { 2071 dnode_t *dn; 2072 dmu_buf_impl_t *parent = db->db_parent; 2073 dmu_buf_impl_t *dndb; 2074 2075 ASSERT(MUTEX_HELD(&db->db_mtx)); 2076 ASSERT(refcount_is_zero(&db->db_holds)); 2077 2078 if (db->db_buf != NULL) { 2079 arc_buf_destroy(db->db_buf, db); 2080 db->db_buf = NULL; 2081 } 2082 2083 if (db->db_blkid == DMU_BONUS_BLKID) { 2084 ASSERT(db->db.db_data != NULL); 2085 zio_buf_free(db->db.db_data, DN_MAX_BONUSLEN); 2086 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER); 2087 db->db_state = DB_UNCACHED; 2088 } 2089 2090 dbuf_clear_data(db); 2091 2092 if (multilist_link_active(&db->db_cache_link)) { 2093 ASSERT(db->db_caching_status == DB_DBUF_CACHE || 2094 db->db_caching_status == DB_DBUF_METADATA_CACHE); 2095 2096 multilist_remove(dbuf_caches[db->db_caching_status].cache, db); 2097 (void) refcount_remove_many( 2098 &dbuf_caches[db->db_caching_status].size, 2099 db->db.db_size, db); 2100 2101 db->db_caching_status = DB_NO_CACHE; 2102 } 2103 2104 ASSERT(db->db_state == DB_UNCACHED || db->db_state == DB_NOFILL); 2105 ASSERT(db->db_data_pending == NULL); 2106 2107 db->db_state = DB_EVICTING; 2108 db->db_blkptr = NULL; 2109 2110 /* 2111 * Now that db_state is DB_EVICTING, nobody else can find this via 2112 * the hash table. We can now drop db_mtx, which allows us to 2113 * acquire the dn_dbufs_mtx. 2114 */ 2115 mutex_exit(&db->db_mtx); 2116 2117 DB_DNODE_ENTER(db); 2118 dn = DB_DNODE(db); 2119 dndb = dn->dn_dbuf; 2120 if (db->db_blkid != DMU_BONUS_BLKID) { 2121 boolean_t needlock = !MUTEX_HELD(&dn->dn_dbufs_mtx); 2122 if (needlock) 2123 mutex_enter(&dn->dn_dbufs_mtx); 2124 avl_remove(&dn->dn_dbufs, db); 2125 atomic_dec_32(&dn->dn_dbufs_count); 2126 membar_producer(); 2127 DB_DNODE_EXIT(db); 2128 if (needlock) 2129 mutex_exit(&dn->dn_dbufs_mtx); 2130 /* 2131 * Decrementing the dbuf count means that the hold corresponding 2132 * to the removed dbuf is no longer discounted in dnode_move(), 2133 * so the dnode cannot be moved until after we release the hold. 2134 * The membar_producer() ensures visibility of the decremented 2135 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually 2136 * release any lock. 2137 */ 2138 mutex_enter(&dn->dn_mtx); 2139 dnode_rele_and_unlock(dn, db, B_TRUE); 2140 db->db_dnode_handle = NULL; 2141 2142 dbuf_hash_remove(db); 2143 } else { 2144 DB_DNODE_EXIT(db); 2145 } 2146 2147 ASSERT(refcount_is_zero(&db->db_holds)); 2148 2149 db->db_parent = NULL; 2150 2151 ASSERT(db->db_buf == NULL); 2152 ASSERT(db->db.db_data == NULL); 2153 ASSERT(db->db_hash_next == NULL); 2154 ASSERT(db->db_blkptr == NULL); 2155 ASSERT(db->db_data_pending == NULL); 2156 ASSERT3U(db->db_caching_status, ==, DB_NO_CACHE); 2157 ASSERT(!multilist_link_active(&db->db_cache_link)); 2158 2159 kmem_cache_free(dbuf_kmem_cache, db); 2160 arc_space_return(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER); 2161 2162 /* 2163 * If this dbuf is referenced from an indirect dbuf, 2164 * decrement the ref count on the indirect dbuf. 2165 */ 2166 if (parent && parent != dndb) { 2167 mutex_enter(&parent->db_mtx); 2168 dbuf_rele_and_unlock(parent, db, B_TRUE); 2169 } 2170 } 2171 2172 /* 2173 * Note: While bpp will always be updated if the function returns success, 2174 * parentp will not be updated if the dnode does not have dn_dbuf filled in; 2175 * this happens when the dnode is the meta-dnode, or a userused or groupused 2176 * object. 2177 */ 2178 static int 2179 dbuf_findbp(dnode_t *dn, int level, uint64_t blkid, int fail_sparse, 2180 dmu_buf_impl_t **parentp, blkptr_t **bpp) 2181 { 2182 *parentp = NULL; 2183 *bpp = NULL; 2184 2185 ASSERT(blkid != DMU_BONUS_BLKID); 2186 2187 if (blkid == DMU_SPILL_BLKID) { 2188 mutex_enter(&dn->dn_mtx); 2189 if (dn->dn_have_spill && 2190 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR)) 2191 *bpp = &dn->dn_phys->dn_spill; 2192 else 2193 *bpp = NULL; 2194 dbuf_add_ref(dn->dn_dbuf, NULL); 2195 *parentp = dn->dn_dbuf; 2196 mutex_exit(&dn->dn_mtx); 2197 return (0); 2198 } 2199 2200 int nlevels = 2201 (dn->dn_phys->dn_nlevels == 0) ? 1 : dn->dn_phys->dn_nlevels; 2202 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 2203 2204 ASSERT3U(level * epbs, <, 64); 2205 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock)); 2206 /* 2207 * This assertion shouldn't trip as long as the max indirect block size 2208 * is less than 1M. The reason for this is that up to that point, 2209 * the number of levels required to address an entire object with blocks 2210 * of size SPA_MINBLOCKSIZE satisfies nlevels * epbs + 1 <= 64. In 2211 * other words, if N * epbs + 1 > 64, then if (N-1) * epbs + 1 > 55 2212 * (i.e. we can address the entire object), objects will all use at most 2213 * N-1 levels and the assertion won't overflow. However, once epbs is 2214 * 13, 4 * 13 + 1 = 53, but 5 * 13 + 1 = 66. Then, 4 levels will not be 2215 * enough to address an entire object, so objects will have 5 levels, 2216 * but then this assertion will overflow. 2217 * 2218 * All this is to say that if we ever increase DN_MAX_INDBLKSHIFT, we 2219 * need to redo this logic to handle overflows. 2220 */ 2221 ASSERT(level >= nlevels || 2222 ((nlevels - level - 1) * epbs) + 2223 highbit64(dn->dn_phys->dn_nblkptr) <= 64); 2224 if (level >= nlevels || 2225 blkid >= ((uint64_t)dn->dn_phys->dn_nblkptr << 2226 ((nlevels - level - 1) * epbs)) || 2227 (fail_sparse && 2228 blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))) { 2229 /* the buffer has no parent yet */ 2230 return (SET_ERROR(ENOENT)); 2231 } else if (level < nlevels-1) { 2232 /* this block is referenced from an indirect block */ 2233 int err = dbuf_hold_impl(dn, level+1, 2234 blkid >> epbs, fail_sparse, FALSE, NULL, parentp); 2235 if (err) 2236 return (err); 2237 err = dbuf_read(*parentp, NULL, 2238 (DB_RF_HAVESTRUCT | DB_RF_NOPREFETCH | DB_RF_CANFAIL)); 2239 if (err) { 2240 dbuf_rele(*parentp, NULL); 2241 *parentp = NULL; 2242 return (err); 2243 } 2244 *bpp = ((blkptr_t *)(*parentp)->db.db_data) + 2245 (blkid & ((1ULL << epbs) - 1)); 2246 if (blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs))) 2247 ASSERT(BP_IS_HOLE(*bpp)); 2248 return (0); 2249 } else { 2250 /* the block is referenced from the dnode */ 2251 ASSERT3U(level, ==, nlevels-1); 2252 ASSERT(dn->dn_phys->dn_nblkptr == 0 || 2253 blkid < dn->dn_phys->dn_nblkptr); 2254 if (dn->dn_dbuf) { 2255 dbuf_add_ref(dn->dn_dbuf, NULL); 2256 *parentp = dn->dn_dbuf; 2257 } 2258 *bpp = &dn->dn_phys->dn_blkptr[blkid]; 2259 return (0); 2260 } 2261 } 2262 2263 static dmu_buf_impl_t * 2264 dbuf_create(dnode_t *dn, uint8_t level, uint64_t blkid, 2265 dmu_buf_impl_t *parent, blkptr_t *blkptr) 2266 { 2267 objset_t *os = dn->dn_objset; 2268 dmu_buf_impl_t *db, *odb; 2269 2270 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock)); 2271 ASSERT(dn->dn_type != DMU_OT_NONE); 2272 2273 db = kmem_cache_alloc(dbuf_kmem_cache, KM_SLEEP); 2274 2275 db->db_objset = os; 2276 db->db.db_object = dn->dn_object; 2277 db->db_level = level; 2278 db->db_blkid = blkid; 2279 db->db_last_dirty = NULL; 2280 db->db_dirtycnt = 0; 2281 db->db_dnode_handle = dn->dn_handle; 2282 db->db_parent = parent; 2283 db->db_blkptr = blkptr; 2284 2285 db->db_user = NULL; 2286 db->db_user_immediate_evict = FALSE; 2287 db->db_freed_in_flight = FALSE; 2288 db->db_pending_evict = FALSE; 2289 2290 if (blkid == DMU_BONUS_BLKID) { 2291 ASSERT3P(parent, ==, dn->dn_dbuf); 2292 db->db.db_size = DN_MAX_BONUSLEN - 2293 (dn->dn_nblkptr-1) * sizeof (blkptr_t); 2294 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen); 2295 db->db.db_offset = DMU_BONUS_BLKID; 2296 db->db_state = DB_UNCACHED; 2297 db->db_caching_status = DB_NO_CACHE; 2298 /* the bonus dbuf is not placed in the hash table */ 2299 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER); 2300 return (db); 2301 } else if (blkid == DMU_SPILL_BLKID) { 2302 db->db.db_size = (blkptr != NULL) ? 2303 BP_GET_LSIZE(blkptr) : SPA_MINBLOCKSIZE; 2304 db->db.db_offset = 0; 2305 } else { 2306 int blocksize = 2307 db->db_level ? 1 << dn->dn_indblkshift : dn->dn_datablksz; 2308 db->db.db_size = blocksize; 2309 db->db.db_offset = db->db_blkid * blocksize; 2310 } 2311 2312 /* 2313 * Hold the dn_dbufs_mtx while we get the new dbuf 2314 * in the hash table *and* added to the dbufs list. 2315 * This prevents a possible deadlock with someone 2316 * trying to look up this dbuf before its added to the 2317 * dn_dbufs list. 2318 */ 2319 mutex_enter(&dn->dn_dbufs_mtx); 2320 db->db_state = DB_EVICTING; 2321 if ((odb = dbuf_hash_insert(db)) != NULL) { 2322 /* someone else inserted it first */ 2323 kmem_cache_free(dbuf_kmem_cache, db); 2324 mutex_exit(&dn->dn_dbufs_mtx); 2325 return (odb); 2326 } 2327 avl_add(&dn->dn_dbufs, db); 2328 2329 db->db_state = DB_UNCACHED; 2330 db->db_caching_status = DB_NO_CACHE; 2331 mutex_exit(&dn->dn_dbufs_mtx); 2332 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_OTHER); 2333 2334 if (parent && parent != dn->dn_dbuf) 2335 dbuf_add_ref(parent, db); 2336 2337 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT || 2338 refcount_count(&dn->dn_holds) > 0); 2339 (void) refcount_add(&dn->dn_holds, db); 2340 atomic_inc_32(&dn->dn_dbufs_count); 2341 2342 dprintf_dbuf(db, "db=%p\n", db); 2343 2344 return (db); 2345 } 2346 2347 typedef struct dbuf_prefetch_arg { 2348 spa_t *dpa_spa; /* The spa to issue the prefetch in. */ 2349 zbookmark_phys_t dpa_zb; /* The target block to prefetch. */ 2350 int dpa_epbs; /* Entries (blkptr_t's) Per Block Shift. */ 2351 int dpa_curlevel; /* The current level that we're reading */ 2352 dnode_t *dpa_dnode; /* The dnode associated with the prefetch */ 2353 zio_priority_t dpa_prio; /* The priority I/Os should be issued at. */ 2354 zio_t *dpa_zio; /* The parent zio_t for all prefetches. */ 2355 arc_flags_t dpa_aflags; /* Flags to pass to the final prefetch. */ 2356 } dbuf_prefetch_arg_t; 2357 2358 /* 2359 * Actually issue the prefetch read for the block given. 2360 */ 2361 static void 2362 dbuf_issue_final_prefetch(dbuf_prefetch_arg_t *dpa, blkptr_t *bp) 2363 { 2364 if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp)) 2365 return; 2366 2367 arc_flags_t aflags = 2368 dpa->dpa_aflags | ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH; 2369 2370 ASSERT3U(dpa->dpa_curlevel, ==, BP_GET_LEVEL(bp)); 2371 ASSERT3U(dpa->dpa_curlevel, ==, dpa->dpa_zb.zb_level); 2372 ASSERT(dpa->dpa_zio != NULL); 2373 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa, bp, NULL, NULL, 2374 dpa->dpa_prio, ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, 2375 &aflags, &dpa->dpa_zb); 2376 } 2377 2378 /* 2379 * Called when an indirect block above our prefetch target is read in. This 2380 * will either read in the next indirect block down the tree or issue the actual 2381 * prefetch if the next block down is our target. 2382 */ 2383 static void 2384 dbuf_prefetch_indirect_done(zio_t *zio, arc_buf_t *abuf, void *private) 2385 { 2386 dbuf_prefetch_arg_t *dpa = private; 2387 2388 ASSERT3S(dpa->dpa_zb.zb_level, <, dpa->dpa_curlevel); 2389 ASSERT3S(dpa->dpa_curlevel, >, 0); 2390 2391 if (abuf == NULL) { 2392 ASSERT(zio == NULL || zio->io_error != 0); 2393 kmem_free(dpa, sizeof (*dpa)); 2394 return; 2395 } 2396 ASSERT(zio == NULL || zio->io_error == 0); 2397 2398 /* 2399 * The dpa_dnode is only valid if we are called with a NULL 2400 * zio. This indicates that the arc_read() returned without 2401 * first calling zio_read() to issue a physical read. Once 2402 * a physical read is made the dpa_dnode must be invalidated 2403 * as the locks guarding it may have been dropped. If the 2404 * dpa_dnode is still valid, then we want to add it to the dbuf 2405 * cache. To do so, we must hold the dbuf associated with the block 2406 * we just prefetched, read its contents so that we associate it 2407 * with an arc_buf_t, and then release it. 2408 */ 2409 if (zio != NULL) { 2410 ASSERT3S(BP_GET_LEVEL(zio->io_bp), ==, dpa->dpa_curlevel); 2411 if (zio->io_flags & ZIO_FLAG_RAW) { 2412 ASSERT3U(BP_GET_PSIZE(zio->io_bp), ==, zio->io_size); 2413 } else { 2414 ASSERT3U(BP_GET_LSIZE(zio->io_bp), ==, zio->io_size); 2415 } 2416 ASSERT3P(zio->io_spa, ==, dpa->dpa_spa); 2417 2418 dpa->dpa_dnode = NULL; 2419 } else if (dpa->dpa_dnode != NULL) { 2420 uint64_t curblkid = dpa->dpa_zb.zb_blkid >> 2421 (dpa->dpa_epbs * (dpa->dpa_curlevel - 2422 dpa->dpa_zb.zb_level)); 2423 dmu_buf_impl_t *db = dbuf_hold_level(dpa->dpa_dnode, 2424 dpa->dpa_curlevel, curblkid, FTAG); 2425 (void) dbuf_read(db, NULL, 2426 DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH | DB_RF_HAVESTRUCT); 2427 dbuf_rele(db, FTAG); 2428 } 2429 2430 dpa->dpa_curlevel--; 2431 2432 uint64_t nextblkid = dpa->dpa_zb.zb_blkid >> 2433 (dpa->dpa_epbs * (dpa->dpa_curlevel - dpa->dpa_zb.zb_level)); 2434 blkptr_t *bp = ((blkptr_t *)abuf->b_data) + 2435 P2PHASE(nextblkid, 1ULL << dpa->dpa_epbs); 2436 if (BP_IS_HOLE(bp)) { 2437 kmem_free(dpa, sizeof (*dpa)); 2438 } else if (dpa->dpa_curlevel == dpa->dpa_zb.zb_level) { 2439 ASSERT3U(nextblkid, ==, dpa->dpa_zb.zb_blkid); 2440 dbuf_issue_final_prefetch(dpa, bp); 2441 kmem_free(dpa, sizeof (*dpa)); 2442 } else { 2443 arc_flags_t iter_aflags = ARC_FLAG_NOWAIT; 2444 zbookmark_phys_t zb; 2445 2446 /* flag if L2ARC eligible, l2arc_noprefetch then decides */ 2447 if (dpa->dpa_aflags & ARC_FLAG_L2CACHE) 2448 iter_aflags |= ARC_FLAG_L2CACHE; 2449 2450 ASSERT3U(dpa->dpa_curlevel, ==, BP_GET_LEVEL(bp)); 2451 2452 SET_BOOKMARK(&zb, dpa->dpa_zb.zb_objset, 2453 dpa->dpa_zb.zb_object, dpa->dpa_curlevel, nextblkid); 2454 2455 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa, 2456 bp, dbuf_prefetch_indirect_done, dpa, dpa->dpa_prio, 2457 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, 2458 &iter_aflags, &zb); 2459 } 2460 2461 arc_buf_destroy(abuf, private); 2462 } 2463 2464 /* 2465 * Issue prefetch reads for the given block on the given level. If the indirect 2466 * blocks above that block are not in memory, we will read them in 2467 * asynchronously. As a result, this call never blocks waiting for a read to 2468 * complete. 2469 */ 2470 void 2471 dbuf_prefetch(dnode_t *dn, int64_t level, uint64_t blkid, zio_priority_t prio, 2472 arc_flags_t aflags) 2473 { 2474 blkptr_t bp; 2475 int epbs, nlevels, curlevel; 2476 uint64_t curblkid; 2477 2478 ASSERT(blkid != DMU_BONUS_BLKID); 2479 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock)); 2480 2481 if (blkid > dn->dn_maxblkid) 2482 return; 2483 2484 if (dnode_block_freed(dn, blkid)) 2485 return; 2486 2487 /* 2488 * This dnode hasn't been written to disk yet, so there's nothing to 2489 * prefetch. 2490 */ 2491 nlevels = dn->dn_phys->dn_nlevels; 2492 if (level >= nlevels || dn->dn_phys->dn_nblkptr == 0) 2493 return; 2494 2495 epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; 2496 if (dn->dn_phys->dn_maxblkid < blkid << (epbs * level)) 2497 return; 2498 2499 dmu_buf_impl_t *db = dbuf_find(dn->dn_objset, dn->dn_object, 2500 level, blkid); 2501 if (db != NULL) { 2502 mutex_exit(&db->db_mtx); 2503 /* 2504 * This dbuf already exists. It is either CACHED, or 2505 * (we assume) about to be read or filled. 2506 */ 2507 return; 2508 } 2509 2510 /* 2511 * Find the closest ancestor (indirect block) of the target block 2512 * that is present in the cache. In this indirect block, we will 2513 * find the bp that is at curlevel, curblkid. 2514 */ 2515 curlevel = level; 2516 curblkid = blkid; 2517 while (curlevel < nlevels - 1) { 2518 int parent_level = curlevel + 1; 2519 uint64_t parent_blkid = curblkid >> epbs; 2520 dmu_buf_impl_t *db; 2521 2522 if (dbuf_hold_impl(dn, parent_level, parent_blkid, 2523 FALSE, TRUE, FTAG, &db) == 0) { 2524 blkptr_t *bpp = db->db_buf->b_data; 2525 bp = bpp[P2PHASE(curblkid, 1 << epbs)]; 2526 dbuf_rele(db, FTAG); 2527 break; 2528 } 2529 2530 curlevel = parent_level; 2531 curblkid = parent_blkid; 2532 } 2533 2534 if (curlevel == nlevels - 1) { 2535 /* No cached indirect blocks found. */ 2536 ASSERT3U(curblkid, <, dn->dn_phys->dn_nblkptr); 2537 bp = dn->dn_phys->dn_blkptr[curblkid]; 2538 } 2539 if (BP_IS_HOLE(&bp)) 2540 return; 2541 2542 ASSERT3U(curlevel, ==, BP_GET_LEVEL(&bp)); 2543 2544 zio_t *pio = zio_root(dmu_objset_spa(dn->dn_objset), NULL, NULL, 2545 ZIO_FLAG_CANFAIL); 2546 2547 dbuf_prefetch_arg_t *dpa = kmem_zalloc(sizeof (*dpa), KM_SLEEP); 2548 dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset; 2549 SET_BOOKMARK(&dpa->dpa_zb, ds != NULL ? ds->ds_object : DMU_META_OBJSET, 2550 dn->dn_object, level, blkid); 2551 dpa->dpa_curlevel = curlevel; 2552 dpa->dpa_prio = prio; 2553 dpa->dpa_aflags = aflags; 2554 dpa->dpa_spa = dn->dn_objset->os_spa; 2555 dpa->dpa_dnode = dn; 2556 dpa->dpa_epbs = epbs; 2557 dpa->dpa_zio = pio; 2558 2559 /* flag if L2ARC eligible, l2arc_noprefetch then decides */ 2560 if (DNODE_LEVEL_IS_L2CACHEABLE(dn, level)) 2561 dpa->dpa_aflags |= ARC_FLAG_L2CACHE; 2562 2563 /* 2564 * If we have the indirect just above us, no need to do the asynchronous 2565 * prefetch chain; we'll just run the last step ourselves. If we're at 2566 * a higher level, though, we want to issue the prefetches for all the 2567 * indirect blocks asynchronously, so we can go on with whatever we were 2568 * doing. 2569 */ 2570 if (curlevel == level) { 2571 ASSERT3U(curblkid, ==, blkid); 2572 dbuf_issue_final_prefetch(dpa, &bp); 2573 kmem_free(dpa, sizeof (*dpa)); 2574 } else { 2575 arc_flags_t iter_aflags = ARC_FLAG_NOWAIT; 2576 zbookmark_phys_t zb; 2577 2578 /* flag if L2ARC eligible, l2arc_noprefetch then decides */ 2579 if (DNODE_LEVEL_IS_L2CACHEABLE(dn, level)) 2580 iter_aflags |= ARC_FLAG_L2CACHE; 2581 2582 SET_BOOKMARK(&zb, ds != NULL ? ds->ds_object : DMU_META_OBJSET, 2583 dn->dn_object, curlevel, curblkid); 2584 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa, 2585 &bp, dbuf_prefetch_indirect_done, dpa, prio, 2586 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, 2587 &iter_aflags, &zb); 2588 } 2589 /* 2590 * We use pio here instead of dpa_zio since it's possible that 2591 * dpa may have already been freed. 2592 */ 2593 zio_nowait(pio); 2594 } 2595 2596 /* 2597 * Returns with db_holds incremented, and db_mtx not held. 2598 * Note: dn_struct_rwlock must be held. 2599 */ 2600 int 2601 dbuf_hold_impl(dnode_t *dn, uint8_t level, uint64_t blkid, 2602 boolean_t fail_sparse, boolean_t fail_uncached, 2603 void *tag, dmu_buf_impl_t **dbp) 2604 { 2605 dmu_buf_impl_t *db, *parent = NULL; 2606 2607 ASSERT(blkid != DMU_BONUS_BLKID); 2608 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock)); 2609 ASSERT3U(dn->dn_nlevels, >, level); 2610 2611 *dbp = NULL; 2612 top: 2613 /* dbuf_find() returns with db_mtx held */ 2614 db = dbuf_find(dn->dn_objset, dn->dn_object, level, blkid); 2615 2616 if (db == NULL) { 2617 blkptr_t *bp = NULL; 2618 int err; 2619 2620 if (fail_uncached) 2621 return (SET_ERROR(ENOENT)); 2622 2623 ASSERT3P(parent, ==, NULL); 2624 err = dbuf_findbp(dn, level, blkid, fail_sparse, &parent, &bp); 2625 if (fail_sparse) { 2626 if (err == 0 && bp && BP_IS_HOLE(bp)) 2627 err = SET_ERROR(ENOENT); 2628 if (err) { 2629 if (parent) 2630 dbuf_rele(parent, NULL); 2631 return (err); 2632 } 2633 } 2634 if (err && err != ENOENT) 2635 return (err); 2636 db = dbuf_create(dn, level, blkid, parent, bp); 2637 } 2638 2639 if (fail_uncached && db->db_state != DB_CACHED) { 2640 mutex_exit(&db->db_mtx); 2641 return (SET_ERROR(ENOENT)); 2642 } 2643 2644 if (db->db_buf != NULL) 2645 ASSERT3P(db->db.db_data, ==, db->db_buf->b_data); 2646 2647 ASSERT(db->db_buf == NULL || arc_referenced(db->db_buf)); 2648 2649 /* 2650 * If this buffer is currently syncing out, and we are are 2651 * still referencing it from db_data, we need to make a copy 2652 * of it in case we decide we want to dirty it again in this txg. 2653 */ 2654 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID && 2655 dn->dn_object != DMU_META_DNODE_OBJECT && 2656 db->db_state == DB_CACHED && db->db_data_pending) { 2657 dbuf_dirty_record_t *dr = db->db_data_pending; 2658 2659 if (dr->dt.dl.dr_data == db->db_buf) { 2660 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db); 2661 2662 dbuf_set_data(db, 2663 arc_alloc_buf(dn->dn_objset->os_spa, db, type, 2664 db->db.db_size)); 2665 bcopy(dr->dt.dl.dr_data->b_data, db->db.db_data, 2666 db->db.db_size); 2667 } 2668 } 2669 2670 if (multilist_link_active(&db->db_cache_link)) { 2671 ASSERT(refcount_is_zero(&db->db_holds)); 2672 ASSERT(db->db_caching_status == DB_DBUF_CACHE || 2673 db->db_caching_status == DB_DBUF_METADATA_CACHE); 2674 2675 multilist_remove(dbuf_caches[db->db_caching_status].cache, db); 2676 (void) refcount_remove_many( 2677 &dbuf_caches[db->db_caching_status].size, 2678 db->db.db_size, db); 2679 2680 db->db_caching_status = DB_NO_CACHE; 2681 } 2682 (void) refcount_add(&db->db_holds, tag); 2683 DBUF_VERIFY(db); 2684 mutex_exit(&db->db_mtx); 2685 2686 /* NOTE: we can't rele the parent until after we drop the db_mtx */ 2687 if (parent) 2688 dbuf_rele(parent, NULL); 2689 2690 ASSERT3P(DB_DNODE(db), ==, dn); 2691 ASSERT3U(db->db_blkid, ==, blkid); 2692 ASSERT3U(db->db_level, ==, level); 2693 *dbp = db; 2694 2695 return (0); 2696 } 2697 2698 dmu_buf_impl_t * 2699 dbuf_hold(dnode_t *dn, uint64_t blkid, void *tag) 2700 { 2701 return (dbuf_hold_level(dn, 0, blkid, tag)); 2702 } 2703 2704 dmu_buf_impl_t * 2705 dbuf_hold_level(dnode_t *dn, int level, uint64_t blkid, void *tag) 2706 { 2707 dmu_buf_impl_t *db; 2708 int err = dbuf_hold_impl(dn, level, blkid, FALSE, FALSE, tag, &db); 2709 return (err ? NULL : db); 2710 } 2711 2712 void 2713 dbuf_create_bonus(dnode_t *dn) 2714 { 2715 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock)); 2716 2717 ASSERT(dn->dn_bonus == NULL); 2718 dn->dn_bonus = dbuf_create(dn, 0, DMU_BONUS_BLKID, dn->dn_dbuf, NULL); 2719 } 2720 2721 int 2722 dbuf_spill_set_blksz(dmu_buf_t *db_fake, uint64_t blksz, dmu_tx_t *tx) 2723 { 2724 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 2725 dnode_t *dn; 2726 2727 if (db->db_blkid != DMU_SPILL_BLKID) 2728 return (SET_ERROR(ENOTSUP)); 2729 if (blksz == 0) 2730 blksz = SPA_MINBLOCKSIZE; 2731 ASSERT3U(blksz, <=, spa_maxblocksize(dmu_objset_spa(db->db_objset))); 2732 blksz = P2ROUNDUP(blksz, SPA_MINBLOCKSIZE); 2733 2734 DB_DNODE_ENTER(db); 2735 dn = DB_DNODE(db); 2736 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 2737 dbuf_new_size(db, blksz, tx); 2738 rw_exit(&dn->dn_struct_rwlock); 2739 DB_DNODE_EXIT(db); 2740 2741 return (0); 2742 } 2743 2744 void 2745 dbuf_rm_spill(dnode_t *dn, dmu_tx_t *tx) 2746 { 2747 dbuf_free_range(dn, DMU_SPILL_BLKID, DMU_SPILL_BLKID, tx); 2748 } 2749 2750 #pragma weak dmu_buf_add_ref = dbuf_add_ref 2751 void 2752 dbuf_add_ref(dmu_buf_impl_t *db, void *tag) 2753 { 2754 int64_t holds = refcount_add(&db->db_holds, tag); 2755 ASSERT3S(holds, >, 1); 2756 } 2757 2758 #pragma weak dmu_buf_try_add_ref = dbuf_try_add_ref 2759 boolean_t 2760 dbuf_try_add_ref(dmu_buf_t *db_fake, objset_t *os, uint64_t obj, uint64_t blkid, 2761 void *tag) 2762 { 2763 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 2764 dmu_buf_impl_t *found_db; 2765 boolean_t result = B_FALSE; 2766 2767 if (db->db_blkid == DMU_BONUS_BLKID) 2768 found_db = dbuf_find_bonus(os, obj); 2769 else 2770 found_db = dbuf_find(os, obj, 0, blkid); 2771 2772 if (found_db != NULL) { 2773 if (db == found_db && dbuf_refcount(db) > db->db_dirtycnt) { 2774 (void) refcount_add(&db->db_holds, tag); 2775 result = B_TRUE; 2776 } 2777 mutex_exit(&db->db_mtx); 2778 } 2779 return (result); 2780 } 2781 2782 /* 2783 * If you call dbuf_rele() you had better not be referencing the dnode handle 2784 * unless you have some other direct or indirect hold on the dnode. (An indirect 2785 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.) 2786 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the 2787 * dnode's parent dbuf evicting its dnode handles. 2788 */ 2789 void 2790 dbuf_rele(dmu_buf_impl_t *db, void *tag) 2791 { 2792 mutex_enter(&db->db_mtx); 2793 dbuf_rele_and_unlock(db, tag, B_FALSE); 2794 } 2795 2796 void 2797 dmu_buf_rele(dmu_buf_t *db, void *tag) 2798 { 2799 dbuf_rele((dmu_buf_impl_t *)db, tag); 2800 } 2801 2802 /* 2803 * dbuf_rele() for an already-locked dbuf. This is necessary to allow 2804 * db_dirtycnt and db_holds to be updated atomically. The 'evicting' 2805 * argument should be set if we are already in the dbuf-evicting code 2806 * path, in which case we don't want to recursively evict. This allows us to 2807 * avoid deeply nested stacks that would have a call flow similar to this: 2808 * 2809 * dbuf_rele()-->dbuf_rele_and_unlock()-->dbuf_evict_notify() 2810 * ^ | 2811 * | | 2812 * +-----dbuf_destroy()<--dbuf_evict_one()<--------+ 2813 * 2814 */ 2815 void 2816 dbuf_rele_and_unlock(dmu_buf_impl_t *db, void *tag, boolean_t evicting) 2817 { 2818 int64_t holds; 2819 2820 ASSERT(MUTEX_HELD(&db->db_mtx)); 2821 DBUF_VERIFY(db); 2822 2823 /* 2824 * Remove the reference to the dbuf before removing its hold on the 2825 * dnode so we can guarantee in dnode_move() that a referenced bonus 2826 * buffer has a corresponding dnode hold. 2827 */ 2828 holds = refcount_remove(&db->db_holds, tag); 2829 ASSERT(holds >= 0); 2830 2831 /* 2832 * We can't freeze indirects if there is a possibility that they 2833 * may be modified in the current syncing context. 2834 */ 2835 if (db->db_buf != NULL && 2836 holds == (db->db_level == 0 ? db->db_dirtycnt : 0)) { 2837 arc_buf_freeze(db->db_buf); 2838 } 2839 2840 if (holds == db->db_dirtycnt && 2841 db->db_level == 0 && db->db_user_immediate_evict) 2842 dbuf_evict_user(db); 2843 2844 if (holds == 0) { 2845 if (db->db_blkid == DMU_BONUS_BLKID) { 2846 dnode_t *dn; 2847 boolean_t evict_dbuf = db->db_pending_evict; 2848 2849 /* 2850 * If the dnode moves here, we cannot cross this 2851 * barrier until the move completes. 2852 */ 2853 DB_DNODE_ENTER(db); 2854 2855 dn = DB_DNODE(db); 2856 atomic_dec_32(&dn->dn_dbufs_count); 2857 2858 /* 2859 * Decrementing the dbuf count means that the bonus 2860 * buffer's dnode hold is no longer discounted in 2861 * dnode_move(). The dnode cannot move until after 2862 * the dnode_rele() below. 2863 */ 2864 DB_DNODE_EXIT(db); 2865 2866 /* 2867 * Do not reference db after its lock is dropped. 2868 * Another thread may evict it. 2869 */ 2870 mutex_exit(&db->db_mtx); 2871 2872 if (evict_dbuf) 2873 dnode_evict_bonus(dn); 2874 2875 dnode_rele(dn, db); 2876 } else if (db->db_buf == NULL) { 2877 /* 2878 * This is a special case: we never associated this 2879 * dbuf with any data allocated from the ARC. 2880 */ 2881 ASSERT(db->db_state == DB_UNCACHED || 2882 db->db_state == DB_NOFILL); 2883 dbuf_destroy(db); 2884 } else if (arc_released(db->db_buf)) { 2885 /* 2886 * This dbuf has anonymous data associated with it. 2887 */ 2888 dbuf_destroy(db); 2889 } else { 2890 boolean_t do_arc_evict = B_FALSE; 2891 blkptr_t bp; 2892 spa_t *spa = dmu_objset_spa(db->db_objset); 2893 2894 if (!DBUF_IS_CACHEABLE(db) && 2895 db->db_blkptr != NULL && 2896 !BP_IS_HOLE(db->db_blkptr) && 2897 !BP_IS_EMBEDDED(db->db_blkptr)) { 2898 do_arc_evict = B_TRUE; 2899 bp = *db->db_blkptr; 2900 } 2901 2902 if (!DBUF_IS_CACHEABLE(db) || 2903 db->db_pending_evict) { 2904 dbuf_destroy(db); 2905 } else if (!multilist_link_active(&db->db_cache_link)) { 2906 ASSERT3U(db->db_caching_status, ==, 2907 DB_NO_CACHE); 2908 2909 dbuf_cached_state_t dcs = 2910 dbuf_include_in_metadata_cache(db) ? 2911 DB_DBUF_METADATA_CACHE : DB_DBUF_CACHE; 2912 db->db_caching_status = dcs; 2913 2914 multilist_insert(dbuf_caches[dcs].cache, db); 2915 (void) refcount_add_many(&dbuf_caches[dcs].size, 2916 db->db.db_size, db); 2917 mutex_exit(&db->db_mtx); 2918 2919 if (db->db_caching_status == DB_DBUF_CACHE && 2920 !evicting) { 2921 dbuf_evict_notify(); 2922 } 2923 } 2924 2925 if (do_arc_evict) 2926 arc_freed(spa, &bp); 2927 } 2928 } else { 2929 mutex_exit(&db->db_mtx); 2930 } 2931 2932 } 2933 2934 #pragma weak dmu_buf_refcount = dbuf_refcount 2935 uint64_t 2936 dbuf_refcount(dmu_buf_impl_t *db) 2937 { 2938 return (refcount_count(&db->db_holds)); 2939 } 2940 2941 void * 2942 dmu_buf_replace_user(dmu_buf_t *db_fake, dmu_buf_user_t *old_user, 2943 dmu_buf_user_t *new_user) 2944 { 2945 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 2946 2947 mutex_enter(&db->db_mtx); 2948 dbuf_verify_user(db, DBVU_NOT_EVICTING); 2949 if (db->db_user == old_user) 2950 db->db_user = new_user; 2951 else 2952 old_user = db->db_user; 2953 dbuf_verify_user(db, DBVU_NOT_EVICTING); 2954 mutex_exit(&db->db_mtx); 2955 2956 return (old_user); 2957 } 2958 2959 void * 2960 dmu_buf_set_user(dmu_buf_t *db_fake, dmu_buf_user_t *user) 2961 { 2962 return (dmu_buf_replace_user(db_fake, NULL, user)); 2963 } 2964 2965 void * 2966 dmu_buf_set_user_ie(dmu_buf_t *db_fake, dmu_buf_user_t *user) 2967 { 2968 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 2969 2970 db->db_user_immediate_evict = TRUE; 2971 return (dmu_buf_set_user(db_fake, user)); 2972 } 2973 2974 void * 2975 dmu_buf_remove_user(dmu_buf_t *db_fake, dmu_buf_user_t *user) 2976 { 2977 return (dmu_buf_replace_user(db_fake, user, NULL)); 2978 } 2979 2980 void * 2981 dmu_buf_get_user(dmu_buf_t *db_fake) 2982 { 2983 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 2984 2985 dbuf_verify_user(db, DBVU_NOT_EVICTING); 2986 return (db->db_user); 2987 } 2988 2989 void 2990 dmu_buf_user_evict_wait() 2991 { 2992 taskq_wait(dbu_evict_taskq); 2993 } 2994 2995 blkptr_t * 2996 dmu_buf_get_blkptr(dmu_buf_t *db) 2997 { 2998 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db; 2999 return (dbi->db_blkptr); 3000 } 3001 3002 objset_t * 3003 dmu_buf_get_objset(dmu_buf_t *db) 3004 { 3005 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db; 3006 return (dbi->db_objset); 3007 } 3008 3009 dnode_t * 3010 dmu_buf_dnode_enter(dmu_buf_t *db) 3011 { 3012 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db; 3013 DB_DNODE_ENTER(dbi); 3014 return (DB_DNODE(dbi)); 3015 } 3016 3017 void 3018 dmu_buf_dnode_exit(dmu_buf_t *db) 3019 { 3020 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db; 3021 DB_DNODE_EXIT(dbi); 3022 } 3023 3024 static void 3025 dbuf_check_blkptr(dnode_t *dn, dmu_buf_impl_t *db) 3026 { 3027 /* ASSERT(dmu_tx_is_syncing(tx) */ 3028 ASSERT(MUTEX_HELD(&db->db_mtx)); 3029 3030 if (db->db_blkptr != NULL) 3031 return; 3032 3033 if (db->db_blkid == DMU_SPILL_BLKID) { 3034 db->db_blkptr = &dn->dn_phys->dn_spill; 3035 BP_ZERO(db->db_blkptr); 3036 return; 3037 } 3038 if (db->db_level == dn->dn_phys->dn_nlevels-1) { 3039 /* 3040 * This buffer was allocated at a time when there was 3041 * no available blkptrs from the dnode, or it was 3042 * inappropriate to hook it in (i.e., nlevels mis-match). 3043 */ 3044 ASSERT(db->db_blkid < dn->dn_phys->dn_nblkptr); 3045 ASSERT(db->db_parent == NULL); 3046 db->db_parent = dn->dn_dbuf; 3047 db->db_blkptr = &dn->dn_phys->dn_blkptr[db->db_blkid]; 3048 DBUF_VERIFY(db); 3049 } else { 3050 dmu_buf_impl_t *parent = db->db_parent; 3051 int epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; 3052 3053 ASSERT(dn->dn_phys->dn_nlevels > 1); 3054 if (parent == NULL) { 3055 mutex_exit(&db->db_mtx); 3056 rw_enter(&dn->dn_struct_rwlock, RW_READER); 3057 parent = dbuf_hold_level(dn, db->db_level + 1, 3058 db->db_blkid >> epbs, db); 3059 rw_exit(&dn->dn_struct_rwlock); 3060 mutex_enter(&db->db_mtx); 3061 db->db_parent = parent; 3062 } 3063 db->db_blkptr = (blkptr_t *)parent->db.db_data + 3064 (db->db_blkid & ((1ULL << epbs) - 1)); 3065 DBUF_VERIFY(db); 3066 } 3067 } 3068 3069 static void 3070 dbuf_sync_indirect(dbuf_dirty_record_t *dr, dmu_tx_t *tx) 3071 { 3072 dmu_buf_impl_t *db = dr->dr_dbuf; 3073 dnode_t *dn; 3074 zio_t *zio; 3075 3076 ASSERT(dmu_tx_is_syncing(tx)); 3077 3078 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr); 3079 3080 mutex_enter(&db->db_mtx); 3081 3082 ASSERT(db->db_level > 0); 3083 DBUF_VERIFY(db); 3084 3085 /* Read the block if it hasn't been read yet. */ 3086 if (db->db_buf == NULL) { 3087 mutex_exit(&db->db_mtx); 3088 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED); 3089 mutex_enter(&db->db_mtx); 3090 } 3091 ASSERT3U(db->db_state, ==, DB_CACHED); 3092 ASSERT(db->db_buf != NULL); 3093 3094 DB_DNODE_ENTER(db); 3095 dn = DB_DNODE(db); 3096 /* Indirect block size must match what the dnode thinks it is. */ 3097 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift); 3098 dbuf_check_blkptr(dn, db); 3099 DB_DNODE_EXIT(db); 3100 3101 /* Provide the pending dirty record to child dbufs */ 3102 db->db_data_pending = dr; 3103 3104 mutex_exit(&db->db_mtx); 3105 3106 dbuf_write(dr, db->db_buf, tx); 3107 3108 zio = dr->dr_zio; 3109 mutex_enter(&dr->dt.di.dr_mtx); 3110 dbuf_sync_list(&dr->dt.di.dr_children, db->db_level - 1, tx); 3111 ASSERT(list_head(&dr->dt.di.dr_children) == NULL); 3112 mutex_exit(&dr->dt.di.dr_mtx); 3113 zio_nowait(zio); 3114 } 3115 3116 static void 3117 dbuf_sync_leaf(dbuf_dirty_record_t *dr, dmu_tx_t *tx) 3118 { 3119 arc_buf_t **datap = &dr->dt.dl.dr_data; 3120 dmu_buf_impl_t *db = dr->dr_dbuf; 3121 dnode_t *dn; 3122 objset_t *os; 3123 uint64_t txg = tx->tx_txg; 3124 3125 ASSERT(dmu_tx_is_syncing(tx)); 3126 3127 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr); 3128 3129 mutex_enter(&db->db_mtx); 3130 /* 3131 * To be synced, we must be dirtied. But we 3132 * might have been freed after the dirty. 3133 */ 3134 if (db->db_state == DB_UNCACHED) { 3135 /* This buffer has been freed since it was dirtied */ 3136 ASSERT(db->db.db_data == NULL); 3137 } else if (db->db_state == DB_FILL) { 3138 /* This buffer was freed and is now being re-filled */ 3139 ASSERT(db->db.db_data != dr->dt.dl.dr_data); 3140 } else { 3141 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_NOFILL); 3142 } 3143 DBUF_VERIFY(db); 3144 3145 DB_DNODE_ENTER(db); 3146 dn = DB_DNODE(db); 3147 3148 if (db->db_blkid == DMU_SPILL_BLKID) { 3149 mutex_enter(&dn->dn_mtx); 3150 dn->dn_phys->dn_flags |= DNODE_FLAG_SPILL_BLKPTR; 3151 mutex_exit(&dn->dn_mtx); 3152 } 3153 3154 /* 3155 * If this is a bonus buffer, simply copy the bonus data into the 3156 * dnode. It will be written out when the dnode is synced (and it 3157 * will be synced, since it must have been dirty for dbuf_sync to 3158 * be called). 3159 */ 3160 if (db->db_blkid == DMU_BONUS_BLKID) { 3161 dbuf_dirty_record_t **drp; 3162 3163 ASSERT(*datap != NULL); 3164 ASSERT0(db->db_level); 3165 ASSERT3U(dn->dn_phys->dn_bonuslen, <=, DN_MAX_BONUSLEN); 3166 bcopy(*datap, DN_BONUS(dn->dn_phys), dn->dn_phys->dn_bonuslen); 3167 DB_DNODE_EXIT(db); 3168 3169 if (*datap != db->db.db_data) { 3170 zio_buf_free(*datap, DN_MAX_BONUSLEN); 3171 arc_space_return(DN_MAX_BONUSLEN, ARC_SPACE_OTHER); 3172 } 3173 db->db_data_pending = NULL; 3174 drp = &db->db_last_dirty; 3175 while (*drp != dr) 3176 drp = &(*drp)->dr_next; 3177 ASSERT(dr->dr_next == NULL); 3178 ASSERT(dr->dr_dbuf == db); 3179 *drp = dr->dr_next; 3180 kmem_free(dr, sizeof (dbuf_dirty_record_t)); 3181 ASSERT(db->db_dirtycnt > 0); 3182 db->db_dirtycnt -= 1; 3183 dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg, B_FALSE); 3184 return; 3185 } 3186 3187 os = dn->dn_objset; 3188 3189 /* 3190 * This function may have dropped the db_mtx lock allowing a dmu_sync 3191 * operation to sneak in. As a result, we need to ensure that we 3192 * don't check the dr_override_state until we have returned from 3193 * dbuf_check_blkptr. 3194 */ 3195 dbuf_check_blkptr(dn, db); 3196 3197 /* 3198 * If this buffer is in the middle of an immediate write, 3199 * wait for the synchronous IO to complete. 3200 */ 3201 while (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) { 3202 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT); 3203 cv_wait(&db->db_changed, &db->db_mtx); 3204 ASSERT(dr->dt.dl.dr_override_state != DR_NOT_OVERRIDDEN); 3205 } 3206 3207 if (db->db_state != DB_NOFILL && 3208 dn->dn_object != DMU_META_DNODE_OBJECT && 3209 refcount_count(&db->db_holds) > 1 && 3210 dr->dt.dl.dr_override_state != DR_OVERRIDDEN && 3211 *datap == db->db_buf) { 3212 /* 3213 * If this buffer is currently "in use" (i.e., there 3214 * are active holds and db_data still references it), 3215 * then make a copy before we start the write so that 3216 * any modifications from the open txg will not leak 3217 * into this write. 3218 * 3219 * NOTE: this copy does not need to be made for 3220 * objects only modified in the syncing context (e.g. 3221 * DNONE_DNODE blocks). 3222 */ 3223 int psize = arc_buf_size(*datap); 3224 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db); 3225 enum zio_compress compress_type = arc_get_compression(*datap); 3226 3227 if (compress_type == ZIO_COMPRESS_OFF) { 3228 *datap = arc_alloc_buf(os->os_spa, db, type, psize); 3229 } else { 3230 ASSERT3U(type, ==, ARC_BUFC_DATA); 3231 int lsize = arc_buf_lsize(*datap); 3232 *datap = arc_alloc_compressed_buf(os->os_spa, db, 3233 psize, lsize, compress_type); 3234 } 3235 bcopy(db->db.db_data, (*datap)->b_data, psize); 3236 } 3237 db->db_data_pending = dr; 3238 3239 mutex_exit(&db->db_mtx); 3240 3241 dbuf_write(dr, *datap, tx); 3242 3243 ASSERT(!list_link_active(&dr->dr_dirty_node)); 3244 if (dn->dn_object == DMU_META_DNODE_OBJECT) { 3245 list_insert_tail(&dn->dn_dirty_records[txg&TXG_MASK], dr); 3246 DB_DNODE_EXIT(db); 3247 } else { 3248 /* 3249 * Although zio_nowait() does not "wait for an IO", it does 3250 * initiate the IO. If this is an empty write it seems plausible 3251 * that the IO could actually be completed before the nowait 3252 * returns. We need to DB_DNODE_EXIT() first in case 3253 * zio_nowait() invalidates the dbuf. 3254 */ 3255 DB_DNODE_EXIT(db); 3256 zio_nowait(dr->dr_zio); 3257 } 3258 } 3259 3260 void 3261 dbuf_sync_list(list_t *list, int level, dmu_tx_t *tx) 3262 { 3263 dbuf_dirty_record_t *dr; 3264 3265 while (dr = list_head(list)) { 3266 if (dr->dr_zio != NULL) { 3267 /* 3268 * If we find an already initialized zio then we 3269 * are processing the meta-dnode, and we have finished. 3270 * The dbufs for all dnodes are put back on the list 3271 * during processing, so that we can zio_wait() 3272 * these IOs after initiating all child IOs. 3273 */ 3274 ASSERT3U(dr->dr_dbuf->db.db_object, ==, 3275 DMU_META_DNODE_OBJECT); 3276 break; 3277 } 3278 if (dr->dr_dbuf->db_blkid != DMU_BONUS_BLKID && 3279 dr->dr_dbuf->db_blkid != DMU_SPILL_BLKID) { 3280 VERIFY3U(dr->dr_dbuf->db_level, ==, level); 3281 } 3282 list_remove(list, dr); 3283 if (dr->dr_dbuf->db_level > 0) 3284 dbuf_sync_indirect(dr, tx); 3285 else 3286 dbuf_sync_leaf(dr, tx); 3287 } 3288 } 3289 3290 /* ARGSUSED */ 3291 static void 3292 dbuf_write_ready(zio_t *zio, arc_buf_t *buf, void *vdb) 3293 { 3294 dmu_buf_impl_t *db = vdb; 3295 dnode_t *dn; 3296 blkptr_t *bp = zio->io_bp; 3297 blkptr_t *bp_orig = &zio->io_bp_orig; 3298 spa_t *spa = zio->io_spa; 3299 int64_t delta; 3300 uint64_t fill = 0; 3301 int i; 3302 3303 ASSERT3P(db->db_blkptr, !=, NULL); 3304 ASSERT3P(&db->db_data_pending->dr_bp_copy, ==, bp); 3305 3306 DB_DNODE_ENTER(db); 3307 dn = DB_DNODE(db); 3308 delta = bp_get_dsize_sync(spa, bp) - bp_get_dsize_sync(spa, bp_orig); 3309 dnode_diduse_space(dn, delta - zio->io_prev_space_delta); 3310 zio->io_prev_space_delta = delta; 3311 3312 if (bp->blk_birth != 0) { 3313 ASSERT((db->db_blkid != DMU_SPILL_BLKID && 3314 BP_GET_TYPE(bp) == dn->dn_type) || 3315 (db->db_blkid == DMU_SPILL_BLKID && 3316 BP_GET_TYPE(bp) == dn->dn_bonustype) || 3317 BP_IS_EMBEDDED(bp)); 3318 ASSERT(BP_GET_LEVEL(bp) == db->db_level); 3319 } 3320 3321 mutex_enter(&db->db_mtx); 3322 3323 #ifdef ZFS_DEBUG 3324 if (db->db_blkid == DMU_SPILL_BLKID) { 3325 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR); 3326 ASSERT(!(BP_IS_HOLE(bp)) && 3327 db->db_blkptr == &dn->dn_phys->dn_spill); 3328 } 3329 #endif 3330 3331 if (db->db_level == 0) { 3332 mutex_enter(&dn->dn_mtx); 3333 if (db->db_blkid > dn->dn_phys->dn_maxblkid && 3334 db->db_blkid != DMU_SPILL_BLKID) 3335 dn->dn_phys->dn_maxblkid = db->db_blkid; 3336 mutex_exit(&dn->dn_mtx); 3337 3338 if (dn->dn_type == DMU_OT_DNODE) { 3339 dnode_phys_t *dnp = db->db.db_data; 3340 for (i = db->db.db_size >> DNODE_SHIFT; i > 0; 3341 i--, dnp++) { 3342 if (dnp->dn_type != DMU_OT_NONE) 3343 fill++; 3344 } 3345 } else { 3346 if (BP_IS_HOLE(bp)) { 3347 fill = 0; 3348 } else { 3349 fill = 1; 3350 } 3351 } 3352 } else { 3353 blkptr_t *ibp = db->db.db_data; 3354 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift); 3355 for (i = db->db.db_size >> SPA_BLKPTRSHIFT; i > 0; i--, ibp++) { 3356 if (BP_IS_HOLE(ibp)) 3357 continue; 3358 fill += BP_GET_FILL(ibp); 3359 } 3360 } 3361 DB_DNODE_EXIT(db); 3362 3363 if (!BP_IS_EMBEDDED(bp)) 3364 bp->blk_fill = fill; 3365 3366 mutex_exit(&db->db_mtx); 3367 3368 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 3369 *db->db_blkptr = *bp; 3370 rw_exit(&dn->dn_struct_rwlock); 3371 } 3372 3373 /* ARGSUSED */ 3374 /* 3375 * This function gets called just prior to running through the compression 3376 * stage of the zio pipeline. If we're an indirect block comprised of only 3377 * holes, then we want this indirect to be compressed away to a hole. In 3378 * order to do that we must zero out any information about the holes that 3379 * this indirect points to prior to before we try to compress it. 3380 */ 3381 static void 3382 dbuf_write_children_ready(zio_t *zio, arc_buf_t *buf, void *vdb) 3383 { 3384 dmu_buf_impl_t *db = vdb; 3385 dnode_t *dn; 3386 blkptr_t *bp; 3387 unsigned int epbs, i; 3388 3389 ASSERT3U(db->db_level, >, 0); 3390 DB_DNODE_ENTER(db); 3391 dn = DB_DNODE(db); 3392 epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; 3393 ASSERT3U(epbs, <, 31); 3394 3395 /* Determine if all our children are holes */ 3396 for (i = 0, bp = db->db.db_data; i < 1 << epbs; i++, bp++) { 3397 if (!BP_IS_HOLE(bp)) 3398 break; 3399 } 3400 3401 /* 3402 * If all the children are holes, then zero them all out so that 3403 * we may get compressed away. 3404 */ 3405 if (i == 1 << epbs) { 3406 /* 3407 * We only found holes. Grab the rwlock to prevent 3408 * anybody from reading the blocks we're about to 3409 * zero out. 3410 */ 3411 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 3412 bzero(db->db.db_data, db->db.db_size); 3413 rw_exit(&dn->dn_struct_rwlock); 3414 } 3415 DB_DNODE_EXIT(db); 3416 } 3417 3418 /* 3419 * The SPA will call this callback several times for each zio - once 3420 * for every physical child i/o (zio->io_phys_children times). This 3421 * allows the DMU to monitor the progress of each logical i/o. For example, 3422 * there may be 2 copies of an indirect block, or many fragments of a RAID-Z 3423 * block. There may be a long delay before all copies/fragments are completed, 3424 * so this callback allows us to retire dirty space gradually, as the physical 3425 * i/os complete. 3426 */ 3427 /* ARGSUSED */ 3428 static void 3429 dbuf_write_physdone(zio_t *zio, arc_buf_t *buf, void *arg) 3430 { 3431 dmu_buf_impl_t *db = arg; 3432 objset_t *os = db->db_objset; 3433 dsl_pool_t *dp = dmu_objset_pool(os); 3434 dbuf_dirty_record_t *dr; 3435 int delta = 0; 3436 3437 dr = db->db_data_pending; 3438 ASSERT3U(dr->dr_txg, ==, zio->io_txg); 3439 3440 /* 3441 * The callback will be called io_phys_children times. Retire one 3442 * portion of our dirty space each time we are called. Any rounding 3443 * error will be cleaned up by dsl_pool_sync()'s call to 3444 * dsl_pool_undirty_space(). 3445 */ 3446 delta = dr->dr_accounted / zio->io_phys_children; 3447 dsl_pool_undirty_space(dp, delta, zio->io_txg); 3448 } 3449 3450 /* ARGSUSED */ 3451 static void 3452 dbuf_write_done(zio_t *zio, arc_buf_t *buf, void *vdb) 3453 { 3454 dmu_buf_impl_t *db = vdb; 3455 blkptr_t *bp_orig = &zio->io_bp_orig; 3456 blkptr_t *bp = db->db_blkptr; 3457 objset_t *os = db->db_objset; 3458 dmu_tx_t *tx = os->os_synctx; 3459 dbuf_dirty_record_t **drp, *dr; 3460 3461 ASSERT0(zio->io_error); 3462 ASSERT(db->db_blkptr == bp); 3463 3464 /* 3465 * For nopwrites and rewrites we ensure that the bp matches our 3466 * original and bypass all the accounting. 3467 */ 3468 if (zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)) { 3469 ASSERT(BP_EQUAL(bp, bp_orig)); 3470 } else { 3471 dsl_dataset_t *ds = os->os_dsl_dataset; 3472 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE); 3473 dsl_dataset_block_born(ds, bp, tx); 3474 } 3475 3476 mutex_enter(&db->db_mtx); 3477 3478 DBUF_VERIFY(db); 3479 3480 drp = &db->db_last_dirty; 3481 while ((dr = *drp) != db->db_data_pending) 3482 drp = &dr->dr_next; 3483 ASSERT(!list_link_active(&dr->dr_dirty_node)); 3484 ASSERT(dr->dr_dbuf == db); 3485 ASSERT(dr->dr_next == NULL); 3486 *drp = dr->dr_next; 3487 3488 #ifdef ZFS_DEBUG 3489 if (db->db_blkid == DMU_SPILL_BLKID) { 3490 dnode_t *dn; 3491 3492 DB_DNODE_ENTER(db); 3493 dn = DB_DNODE(db); 3494 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR); 3495 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) && 3496 db->db_blkptr == &dn->dn_phys->dn_spill); 3497 DB_DNODE_EXIT(db); 3498 } 3499 #endif 3500 3501 if (db->db_level == 0) { 3502 ASSERT(db->db_blkid != DMU_BONUS_BLKID); 3503 ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN); 3504 if (db->db_state != DB_NOFILL) { 3505 if (dr->dt.dl.dr_data != db->db_buf) 3506 arc_buf_destroy(dr->dt.dl.dr_data, db); 3507 } 3508 } else { 3509 dnode_t *dn; 3510 3511 DB_DNODE_ENTER(db); 3512 dn = DB_DNODE(db); 3513 ASSERT(list_head(&dr->dt.di.dr_children) == NULL); 3514 ASSERT3U(db->db.db_size, ==, 1 << dn->dn_phys->dn_indblkshift); 3515 if (!BP_IS_HOLE(db->db_blkptr)) { 3516 int epbs = 3517 dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; 3518 ASSERT3U(db->db_blkid, <=, 3519 dn->dn_phys->dn_maxblkid >> (db->db_level * epbs)); 3520 ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==, 3521 db->db.db_size); 3522 } 3523 DB_DNODE_EXIT(db); 3524 mutex_destroy(&dr->dt.di.dr_mtx); 3525 list_destroy(&dr->dt.di.dr_children); 3526 } 3527 kmem_free(dr, sizeof (dbuf_dirty_record_t)); 3528 3529 cv_broadcast(&db->db_changed); 3530 ASSERT(db->db_dirtycnt > 0); 3531 db->db_dirtycnt -= 1; 3532 db->db_data_pending = NULL; 3533 dbuf_rele_and_unlock(db, (void *)(uintptr_t)tx->tx_txg, B_FALSE); 3534 } 3535 3536 static void 3537 dbuf_write_nofill_ready(zio_t *zio) 3538 { 3539 dbuf_write_ready(zio, NULL, zio->io_private); 3540 } 3541 3542 static void 3543 dbuf_write_nofill_done(zio_t *zio) 3544 { 3545 dbuf_write_done(zio, NULL, zio->io_private); 3546 } 3547 3548 static void 3549 dbuf_write_override_ready(zio_t *zio) 3550 { 3551 dbuf_dirty_record_t *dr = zio->io_private; 3552 dmu_buf_impl_t *db = dr->dr_dbuf; 3553 3554 dbuf_write_ready(zio, NULL, db); 3555 } 3556 3557 static void 3558 dbuf_write_override_done(zio_t *zio) 3559 { 3560 dbuf_dirty_record_t *dr = zio->io_private; 3561 dmu_buf_impl_t *db = dr->dr_dbuf; 3562 blkptr_t *obp = &dr->dt.dl.dr_overridden_by; 3563 3564 mutex_enter(&db->db_mtx); 3565 if (!BP_EQUAL(zio->io_bp, obp)) { 3566 if (!BP_IS_HOLE(obp)) 3567 dsl_free(spa_get_dsl(zio->io_spa), zio->io_txg, obp); 3568 arc_release(dr->dt.dl.dr_data, db); 3569 } 3570 mutex_exit(&db->db_mtx); 3571 dbuf_write_done(zio, NULL, db); 3572 3573 if (zio->io_abd != NULL) 3574 abd_put(zio->io_abd); 3575 } 3576 3577 typedef struct dbuf_remap_impl_callback_arg { 3578 objset_t *drica_os; 3579 uint64_t drica_blk_birth; 3580 dmu_tx_t *drica_tx; 3581 } dbuf_remap_impl_callback_arg_t; 3582 3583 static void 3584 dbuf_remap_impl_callback(uint64_t vdev, uint64_t offset, uint64_t size, 3585 void *arg) 3586 { 3587 dbuf_remap_impl_callback_arg_t *drica = arg; 3588 objset_t *os = drica->drica_os; 3589 spa_t *spa = dmu_objset_spa(os); 3590 dmu_tx_t *tx = drica->drica_tx; 3591 3592 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa))); 3593 3594 if (os == spa_meta_objset(spa)) { 3595 spa_vdev_indirect_mark_obsolete(spa, vdev, offset, size, tx); 3596 } else { 3597 dsl_dataset_block_remapped(dmu_objset_ds(os), vdev, offset, 3598 size, drica->drica_blk_birth, tx); 3599 } 3600 } 3601 3602 static void 3603 dbuf_remap_impl(dnode_t *dn, blkptr_t *bp, dmu_tx_t *tx) 3604 { 3605 blkptr_t bp_copy = *bp; 3606 spa_t *spa = dmu_objset_spa(dn->dn_objset); 3607 dbuf_remap_impl_callback_arg_t drica; 3608 3609 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa))); 3610 3611 drica.drica_os = dn->dn_objset; 3612 drica.drica_blk_birth = bp->blk_birth; 3613 drica.drica_tx = tx; 3614 if (spa_remap_blkptr(spa, &bp_copy, dbuf_remap_impl_callback, 3615 &drica)) { 3616 /* 3617 * The struct_rwlock prevents dbuf_read_impl() from 3618 * dereferencing the BP while we are changing it. To 3619 * avoid lock contention, only grab it when we are actually 3620 * changing the BP. 3621 */ 3622 rw_enter(&dn->dn_struct_rwlock, RW_WRITER); 3623 *bp = bp_copy; 3624 rw_exit(&dn->dn_struct_rwlock); 3625 } 3626 } 3627 3628 /* 3629 * Returns true if a dbuf_remap would modify the dbuf. We do this by attempting 3630 * to remap a copy of every bp in the dbuf. 3631 */ 3632 boolean_t 3633 dbuf_can_remap(const dmu_buf_impl_t *db) 3634 { 3635 spa_t *spa = dmu_objset_spa(db->db_objset); 3636 blkptr_t *bp = db->db.db_data; 3637 boolean_t ret = B_FALSE; 3638 3639 ASSERT3U(db->db_level, >, 0); 3640 ASSERT3S(db->db_state, ==, DB_CACHED); 3641 3642 ASSERT(spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL)); 3643 3644 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 3645 for (int i = 0; i < db->db.db_size >> SPA_BLKPTRSHIFT; i++) { 3646 blkptr_t bp_copy = bp[i]; 3647 if (spa_remap_blkptr(spa, &bp_copy, NULL, NULL)) { 3648 ret = B_TRUE; 3649 break; 3650 } 3651 } 3652 spa_config_exit(spa, SCL_VDEV, FTAG); 3653 3654 return (ret); 3655 } 3656 3657 boolean_t 3658 dnode_needs_remap(const dnode_t *dn) 3659 { 3660 spa_t *spa = dmu_objset_spa(dn->dn_objset); 3661 boolean_t ret = B_FALSE; 3662 3663 if (dn->dn_phys->dn_nlevels == 0) { 3664 return (B_FALSE); 3665 } 3666 3667 ASSERT(spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL)); 3668 3669 spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER); 3670 for (int j = 0; j < dn->dn_phys->dn_nblkptr; j++) { 3671 blkptr_t bp_copy = dn->dn_phys->dn_blkptr[j]; 3672 if (spa_remap_blkptr(spa, &bp_copy, NULL, NULL)) { 3673 ret = B_TRUE; 3674 break; 3675 } 3676 } 3677 spa_config_exit(spa, SCL_VDEV, FTAG); 3678 3679 return (ret); 3680 } 3681 3682 /* 3683 * Remap any existing BP's to concrete vdevs, if possible. 3684 */ 3685 static void 3686 dbuf_remap(dnode_t *dn, dmu_buf_impl_t *db, dmu_tx_t *tx) 3687 { 3688 spa_t *spa = dmu_objset_spa(db->db_objset); 3689 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa))); 3690 3691 if (!spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL)) 3692 return; 3693 3694 if (db->db_level > 0) { 3695 blkptr_t *bp = db->db.db_data; 3696 for (int i = 0; i < db->db.db_size >> SPA_BLKPTRSHIFT; i++) { 3697 dbuf_remap_impl(dn, &bp[i], tx); 3698 } 3699 } else if (db->db.db_object == DMU_META_DNODE_OBJECT) { 3700 dnode_phys_t *dnp = db->db.db_data; 3701 ASSERT3U(db->db_dnode_handle->dnh_dnode->dn_type, ==, 3702 DMU_OT_DNODE); 3703 for (int i = 0; i < db->db.db_size >> DNODE_SHIFT; i++) { 3704 for (int j = 0; j < dnp[i].dn_nblkptr; j++) { 3705 dbuf_remap_impl(dn, &dnp[i].dn_blkptr[j], tx); 3706 } 3707 } 3708 } 3709 } 3710 3711 3712 /* Issue I/O to commit a dirty buffer to disk. */ 3713 static void 3714 dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx) 3715 { 3716 dmu_buf_impl_t *db = dr->dr_dbuf; 3717 dnode_t *dn; 3718 objset_t *os; 3719 dmu_buf_impl_t *parent = db->db_parent; 3720 uint64_t txg = tx->tx_txg; 3721 zbookmark_phys_t zb; 3722 zio_prop_t zp; 3723 zio_t *zio; 3724 int wp_flag = 0; 3725 3726 ASSERT(dmu_tx_is_syncing(tx)); 3727 3728 DB_DNODE_ENTER(db); 3729 dn = DB_DNODE(db); 3730 os = dn->dn_objset; 3731 3732 if (db->db_state != DB_NOFILL) { 3733 if (db->db_level > 0 || dn->dn_type == DMU_OT_DNODE) { 3734 /* 3735 * Private object buffers are released here rather 3736 * than in dbuf_dirty() since they are only modified 3737 * in the syncing context and we don't want the 3738 * overhead of making multiple copies of the data. 3739 */ 3740 if (BP_IS_HOLE(db->db_blkptr)) { 3741 arc_buf_thaw(data); 3742 } else { 3743 dbuf_release_bp(db); 3744 } 3745 dbuf_remap(dn, db, tx); 3746 } 3747 } 3748 3749 if (parent != dn->dn_dbuf) { 3750 /* Our parent is an indirect block. */ 3751 /* We have a dirty parent that has been scheduled for write. */ 3752 ASSERT(parent && parent->db_data_pending); 3753 /* Our parent's buffer is one level closer to the dnode. */ 3754 ASSERT(db->db_level == parent->db_level-1); 3755 /* 3756 * We're about to modify our parent's db_data by modifying 3757 * our block pointer, so the parent must be released. 3758 */ 3759 ASSERT(arc_released(parent->db_buf)); 3760 zio = parent->db_data_pending->dr_zio; 3761 } else { 3762 /* Our parent is the dnode itself. */ 3763 ASSERT((db->db_level == dn->dn_phys->dn_nlevels-1 && 3764 db->db_blkid != DMU_SPILL_BLKID) || 3765 (db->db_blkid == DMU_SPILL_BLKID && db->db_level == 0)); 3766 if (db->db_blkid != DMU_SPILL_BLKID) 3767 ASSERT3P(db->db_blkptr, ==, 3768 &dn->dn_phys->dn_blkptr[db->db_blkid]); 3769 zio = dn->dn_zio; 3770 } 3771 3772 ASSERT(db->db_level == 0 || data == db->db_buf); 3773 ASSERT3U(db->db_blkptr->blk_birth, <=, txg); 3774 ASSERT(zio); 3775 3776 SET_BOOKMARK(&zb, os->os_dsl_dataset ? 3777 os->os_dsl_dataset->ds_object : DMU_META_OBJSET, 3778 db->db.db_object, db->db_level, db->db_blkid); 3779 3780 if (db->db_blkid == DMU_SPILL_BLKID) 3781 wp_flag = WP_SPILL; 3782 wp_flag |= (db->db_state == DB_NOFILL) ? WP_NOFILL : 0; 3783 3784 dmu_write_policy(os, dn, db->db_level, wp_flag, &zp); 3785 DB_DNODE_EXIT(db); 3786 3787 /* 3788 * We copy the blkptr now (rather than when we instantiate the dirty 3789 * record), because its value can change between open context and 3790 * syncing context. We do not need to hold dn_struct_rwlock to read 3791 * db_blkptr because we are in syncing context. 3792 */ 3793 dr->dr_bp_copy = *db->db_blkptr; 3794 3795 if (db->db_level == 0 && 3796 dr->dt.dl.dr_override_state == DR_OVERRIDDEN) { 3797 /* 3798 * The BP for this block has been provided by open context 3799 * (by dmu_sync() or dmu_buf_write_embedded()). 3800 */ 3801 abd_t *contents = (data != NULL) ? 3802 abd_get_from_buf(data->b_data, arc_buf_size(data)) : NULL; 3803 3804 dr->dr_zio = zio_write(zio, os->os_spa, txg, &dr->dr_bp_copy, 3805 contents, db->db.db_size, db->db.db_size, &zp, 3806 dbuf_write_override_ready, NULL, NULL, 3807 dbuf_write_override_done, 3808 dr, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb); 3809 mutex_enter(&db->db_mtx); 3810 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN; 3811 zio_write_override(dr->dr_zio, &dr->dt.dl.dr_overridden_by, 3812 dr->dt.dl.dr_copies, dr->dt.dl.dr_nopwrite); 3813 mutex_exit(&db->db_mtx); 3814 } else if (db->db_state == DB_NOFILL) { 3815 ASSERT(zp.zp_checksum == ZIO_CHECKSUM_OFF || 3816 zp.zp_checksum == ZIO_CHECKSUM_NOPARITY); 3817 dr->dr_zio = zio_write(zio, os->os_spa, txg, 3818 &dr->dr_bp_copy, NULL, db->db.db_size, db->db.db_size, &zp, 3819 dbuf_write_nofill_ready, NULL, NULL, 3820 dbuf_write_nofill_done, db, 3821 ZIO_PRIORITY_ASYNC_WRITE, 3822 ZIO_FLAG_MUSTSUCCEED | ZIO_FLAG_NODATA, &zb); 3823 } else { 3824 ASSERT(arc_released(data)); 3825 3826 /* 3827 * For indirect blocks, we want to setup the children 3828 * ready callback so that we can properly handle an indirect 3829 * block that only contains holes. 3830 */ 3831 arc_done_func_t *children_ready_cb = NULL; 3832 if (db->db_level != 0) 3833 children_ready_cb = dbuf_write_children_ready; 3834 3835 dr->dr_zio = arc_write(zio, os->os_spa, txg, 3836 &dr->dr_bp_copy, data, DBUF_IS_L2CACHEABLE(db), 3837 &zp, dbuf_write_ready, children_ready_cb, 3838 dbuf_write_physdone, dbuf_write_done, db, 3839 ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb); 3840 } 3841 } 3842