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 https://opensource.org/licenses/CDDL-1.0. 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, 2020 by Delphix. All rights reserved. 25 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved. 26 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. 27 * Copyright (c) 2019, Klara Inc. 28 * Copyright (c) 2019, Allan Jude 29 * Copyright (c) 2021, 2022 by Pawel Jakub Dawidek 30 */ 31 32 #include <sys/zfs_context.h> 33 #include <sys/arc.h> 34 #include <sys/dmu.h> 35 #include <sys/dmu_send.h> 36 #include <sys/dmu_impl.h> 37 #include <sys/dbuf.h> 38 #include <sys/dmu_objset.h> 39 #include <sys/dsl_dataset.h> 40 #include <sys/dsl_dir.h> 41 #include <sys/dmu_tx.h> 42 #include <sys/spa.h> 43 #include <sys/zio.h> 44 #include <sys/dmu_zfetch.h> 45 #include <sys/sa.h> 46 #include <sys/sa_impl.h> 47 #include <sys/zfeature.h> 48 #include <sys/blkptr.h> 49 #include <sys/range_tree.h> 50 #include <sys/trace_zfs.h> 51 #include <sys/callb.h> 52 #include <sys/abd.h> 53 #include <sys/brt.h> 54 #include <sys/vdev.h> 55 #include <cityhash.h> 56 #include <sys/spa_impl.h> 57 #include <sys/wmsum.h> 58 #include <sys/vdev_impl.h> 59 60 static kstat_t *dbuf_ksp; 61 62 typedef struct dbuf_stats { 63 /* 64 * Various statistics about the size of the dbuf cache. 65 */ 66 kstat_named_t cache_count; 67 kstat_named_t cache_size_bytes; 68 kstat_named_t cache_size_bytes_max; 69 /* 70 * Statistics regarding the bounds on the dbuf cache size. 71 */ 72 kstat_named_t cache_target_bytes; 73 kstat_named_t cache_lowater_bytes; 74 kstat_named_t cache_hiwater_bytes; 75 /* 76 * Total number of dbuf cache evictions that have occurred. 77 */ 78 kstat_named_t cache_total_evicts; 79 /* 80 * The distribution of dbuf levels in the dbuf cache and 81 * the total size of all dbufs at each level. 82 */ 83 kstat_named_t cache_levels[DN_MAX_LEVELS]; 84 kstat_named_t cache_levels_bytes[DN_MAX_LEVELS]; 85 /* 86 * Statistics about the dbuf hash table. 87 */ 88 kstat_named_t hash_hits; 89 kstat_named_t hash_misses; 90 kstat_named_t hash_collisions; 91 kstat_named_t hash_elements; 92 kstat_named_t hash_elements_max; 93 /* 94 * Number of sublists containing more than one dbuf in the dbuf 95 * hash table. Keep track of the longest hash chain. 96 */ 97 kstat_named_t hash_chains; 98 kstat_named_t hash_chain_max; 99 /* 100 * Number of times a dbuf_create() discovers that a dbuf was 101 * already created and in the dbuf hash table. 102 */ 103 kstat_named_t hash_insert_race; 104 /* 105 * Number of entries in the hash table dbuf and mutex arrays. 106 */ 107 kstat_named_t hash_table_count; 108 kstat_named_t hash_mutex_count; 109 /* 110 * Statistics about the size of the metadata dbuf cache. 111 */ 112 kstat_named_t metadata_cache_count; 113 kstat_named_t metadata_cache_size_bytes; 114 kstat_named_t metadata_cache_size_bytes_max; 115 /* 116 * For diagnostic purposes, this is incremented whenever we can't add 117 * something to the metadata cache because it's full, and instead put 118 * the data in the regular dbuf cache. 119 */ 120 kstat_named_t metadata_cache_overflow; 121 } dbuf_stats_t; 122 123 dbuf_stats_t dbuf_stats = { 124 { "cache_count", KSTAT_DATA_UINT64 }, 125 { "cache_size_bytes", KSTAT_DATA_UINT64 }, 126 { "cache_size_bytes_max", KSTAT_DATA_UINT64 }, 127 { "cache_target_bytes", KSTAT_DATA_UINT64 }, 128 { "cache_lowater_bytes", KSTAT_DATA_UINT64 }, 129 { "cache_hiwater_bytes", KSTAT_DATA_UINT64 }, 130 { "cache_total_evicts", KSTAT_DATA_UINT64 }, 131 { { "cache_levels_N", KSTAT_DATA_UINT64 } }, 132 { { "cache_levels_bytes_N", KSTAT_DATA_UINT64 } }, 133 { "hash_hits", KSTAT_DATA_UINT64 }, 134 { "hash_misses", KSTAT_DATA_UINT64 }, 135 { "hash_collisions", KSTAT_DATA_UINT64 }, 136 { "hash_elements", KSTAT_DATA_UINT64 }, 137 { "hash_elements_max", KSTAT_DATA_UINT64 }, 138 { "hash_chains", KSTAT_DATA_UINT64 }, 139 { "hash_chain_max", KSTAT_DATA_UINT64 }, 140 { "hash_insert_race", KSTAT_DATA_UINT64 }, 141 { "hash_table_count", KSTAT_DATA_UINT64 }, 142 { "hash_mutex_count", KSTAT_DATA_UINT64 }, 143 { "metadata_cache_count", KSTAT_DATA_UINT64 }, 144 { "metadata_cache_size_bytes", KSTAT_DATA_UINT64 }, 145 { "metadata_cache_size_bytes_max", KSTAT_DATA_UINT64 }, 146 { "metadata_cache_overflow", KSTAT_DATA_UINT64 } 147 }; 148 149 struct { 150 wmsum_t cache_count; 151 wmsum_t cache_total_evicts; 152 wmsum_t cache_levels[DN_MAX_LEVELS]; 153 wmsum_t cache_levels_bytes[DN_MAX_LEVELS]; 154 wmsum_t hash_hits; 155 wmsum_t hash_misses; 156 wmsum_t hash_collisions; 157 wmsum_t hash_chains; 158 wmsum_t hash_insert_race; 159 wmsum_t metadata_cache_count; 160 wmsum_t metadata_cache_overflow; 161 } dbuf_sums; 162 163 #define DBUF_STAT_INCR(stat, val) \ 164 wmsum_add(&dbuf_sums.stat, val); 165 #define DBUF_STAT_DECR(stat, val) \ 166 DBUF_STAT_INCR(stat, -(val)); 167 #define DBUF_STAT_BUMP(stat) \ 168 DBUF_STAT_INCR(stat, 1); 169 #define DBUF_STAT_BUMPDOWN(stat) \ 170 DBUF_STAT_INCR(stat, -1); 171 #define DBUF_STAT_MAX(stat, v) { \ 172 uint64_t _m; \ 173 while ((v) > (_m = dbuf_stats.stat.value.ui64) && \ 174 (_m != atomic_cas_64(&dbuf_stats.stat.value.ui64, _m, (v))))\ 175 continue; \ 176 } 177 178 static void dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx); 179 static void dbuf_sync_leaf_verify_bonus_dnode(dbuf_dirty_record_t *dr); 180 static int dbuf_read_verify_dnode_crypt(dmu_buf_impl_t *db, uint32_t flags); 181 182 /* 183 * Global data structures and functions for the dbuf cache. 184 */ 185 static kmem_cache_t *dbuf_kmem_cache; 186 static taskq_t *dbu_evict_taskq; 187 188 static kthread_t *dbuf_cache_evict_thread; 189 static kmutex_t dbuf_evict_lock; 190 static kcondvar_t dbuf_evict_cv; 191 static boolean_t dbuf_evict_thread_exit; 192 193 /* 194 * There are two dbuf caches; each dbuf can only be in one of them at a time. 195 * 196 * 1. Cache of metadata dbufs, to help make read-heavy administrative commands 197 * from /sbin/zfs run faster. The "metadata cache" specifically stores dbufs 198 * that represent the metadata that describes filesystems/snapshots/ 199 * bookmarks/properties/etc. We only evict from this cache when we export a 200 * pool, to short-circuit as much I/O as possible for all administrative 201 * commands that need the metadata. There is no eviction policy for this 202 * cache, because we try to only include types in it which would occupy a 203 * very small amount of space per object but create a large impact on the 204 * performance of these commands. Instead, after it reaches a maximum size 205 * (which should only happen on very small memory systems with a very large 206 * number of filesystem objects), we stop taking new dbufs into the 207 * metadata cache, instead putting them in the normal dbuf cache. 208 * 209 * 2. LRU cache of dbufs. The dbuf cache maintains a list of dbufs that 210 * are not currently held but have been recently released. These dbufs 211 * are not eligible for arc eviction until they are aged out of the cache. 212 * Dbufs that are aged out of the cache will be immediately destroyed and 213 * become eligible for arc eviction. 214 * 215 * Dbufs are added to these caches once the last hold is released. If a dbuf is 216 * later accessed and still exists in the dbuf cache, then it will be removed 217 * from the cache and later re-added to the head of the cache. 218 * 219 * If a given dbuf meets the requirements for the metadata cache, it will go 220 * there, otherwise it will be considered for the generic LRU dbuf cache. The 221 * caches and the refcounts tracking their sizes are stored in an array indexed 222 * by those caches' matching enum values (from dbuf_cached_state_t). 223 */ 224 typedef struct dbuf_cache { 225 multilist_t cache; 226 zfs_refcount_t size ____cacheline_aligned; 227 } dbuf_cache_t; 228 dbuf_cache_t dbuf_caches[DB_CACHE_MAX]; 229 230 /* Size limits for the caches */ 231 static uint64_t dbuf_cache_max_bytes = UINT64_MAX; 232 static uint64_t dbuf_metadata_cache_max_bytes = UINT64_MAX; 233 234 /* Set the default sizes of the caches to log2 fraction of arc size */ 235 static uint_t dbuf_cache_shift = 5; 236 static uint_t dbuf_metadata_cache_shift = 6; 237 238 /* Set the dbuf hash mutex count as log2 shift (dynamic by default) */ 239 static uint_t dbuf_mutex_cache_shift = 0; 240 241 static unsigned long dbuf_cache_target_bytes(void); 242 static unsigned long dbuf_metadata_cache_target_bytes(void); 243 244 /* 245 * The LRU dbuf cache uses a three-stage eviction policy: 246 * - A low water marker designates when the dbuf eviction thread 247 * should stop evicting from the dbuf cache. 248 * - When we reach the maximum size (aka mid water mark), we 249 * signal the eviction thread to run. 250 * - The high water mark indicates when the eviction thread 251 * is unable to keep up with the incoming load and eviction must 252 * happen in the context of the calling thread. 253 * 254 * The dbuf cache: 255 * (max size) 256 * low water mid water hi water 257 * +----------------------------------------+----------+----------+ 258 * | | | | 259 * | | | | 260 * | | | | 261 * | | | | 262 * +----------------------------------------+----------+----------+ 263 * stop signal evict 264 * evicting eviction directly 265 * thread 266 * 267 * The high and low water marks indicate the operating range for the eviction 268 * thread. The low water mark is, by default, 90% of the total size of the 269 * cache and the high water mark is at 110% (both of these percentages can be 270 * changed by setting dbuf_cache_lowater_pct and dbuf_cache_hiwater_pct, 271 * respectively). The eviction thread will try to ensure that the cache remains 272 * within this range by waking up every second and checking if the cache is 273 * above the low water mark. The thread can also be woken up by callers adding 274 * elements into the cache if the cache is larger than the mid water (i.e max 275 * cache size). Once the eviction thread is woken up and eviction is required, 276 * it will continue evicting buffers until it's able to reduce the cache size 277 * to the low water mark. If the cache size continues to grow and hits the high 278 * water mark, then callers adding elements to the cache will begin to evict 279 * directly from the cache until the cache is no longer above the high water 280 * mark. 281 */ 282 283 /* 284 * The percentage above and below the maximum cache size. 285 */ 286 static uint_t dbuf_cache_hiwater_pct = 10; 287 static uint_t dbuf_cache_lowater_pct = 10; 288 289 static int 290 dbuf_cons(void *vdb, void *unused, int kmflag) 291 { 292 (void) unused, (void) kmflag; 293 dmu_buf_impl_t *db = vdb; 294 memset(db, 0, sizeof (dmu_buf_impl_t)); 295 296 mutex_init(&db->db_mtx, NULL, MUTEX_DEFAULT, NULL); 297 rw_init(&db->db_rwlock, NULL, RW_DEFAULT, NULL); 298 cv_init(&db->db_changed, NULL, CV_DEFAULT, NULL); 299 multilist_link_init(&db->db_cache_link); 300 zfs_refcount_create(&db->db_holds); 301 302 return (0); 303 } 304 305 static void 306 dbuf_dest(void *vdb, void *unused) 307 { 308 (void) unused; 309 dmu_buf_impl_t *db = vdb; 310 mutex_destroy(&db->db_mtx); 311 rw_destroy(&db->db_rwlock); 312 cv_destroy(&db->db_changed); 313 ASSERT(!multilist_link_active(&db->db_cache_link)); 314 zfs_refcount_destroy(&db->db_holds); 315 } 316 317 /* 318 * dbuf hash table routines 319 */ 320 static dbuf_hash_table_t dbuf_hash_table; 321 322 /* 323 * We use Cityhash for this. It's fast, and has good hash properties without 324 * requiring any large static buffers. 325 */ 326 static uint64_t 327 dbuf_hash(void *os, uint64_t obj, uint8_t lvl, uint64_t blkid) 328 { 329 return (cityhash4((uintptr_t)os, obj, (uint64_t)lvl, blkid)); 330 } 331 332 #define DTRACE_SET_STATE(db, why) \ 333 DTRACE_PROBE2(dbuf__state_change, dmu_buf_impl_t *, db, \ 334 const char *, why) 335 336 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \ 337 ((dbuf)->db.db_object == (obj) && \ 338 (dbuf)->db_objset == (os) && \ 339 (dbuf)->db_level == (level) && \ 340 (dbuf)->db_blkid == (blkid)) 341 342 dmu_buf_impl_t * 343 dbuf_find(objset_t *os, uint64_t obj, uint8_t level, uint64_t blkid, 344 uint64_t *hash_out) 345 { 346 dbuf_hash_table_t *h = &dbuf_hash_table; 347 uint64_t hv; 348 uint64_t idx; 349 dmu_buf_impl_t *db; 350 351 hv = dbuf_hash(os, obj, level, blkid); 352 idx = hv & h->hash_table_mask; 353 354 mutex_enter(DBUF_HASH_MUTEX(h, idx)); 355 for (db = h->hash_table[idx]; db != NULL; db = db->db_hash_next) { 356 if (DBUF_EQUAL(db, os, obj, level, blkid)) { 357 mutex_enter(&db->db_mtx); 358 if (db->db_state != DB_EVICTING) { 359 mutex_exit(DBUF_HASH_MUTEX(h, idx)); 360 return (db); 361 } 362 mutex_exit(&db->db_mtx); 363 } 364 } 365 mutex_exit(DBUF_HASH_MUTEX(h, idx)); 366 if (hash_out != NULL) 367 *hash_out = hv; 368 return (NULL); 369 } 370 371 static dmu_buf_impl_t * 372 dbuf_find_bonus(objset_t *os, uint64_t object) 373 { 374 dnode_t *dn; 375 dmu_buf_impl_t *db = NULL; 376 377 if (dnode_hold(os, object, FTAG, &dn) == 0) { 378 rw_enter(&dn->dn_struct_rwlock, RW_READER); 379 if (dn->dn_bonus != NULL) { 380 db = dn->dn_bonus; 381 mutex_enter(&db->db_mtx); 382 } 383 rw_exit(&dn->dn_struct_rwlock); 384 dnode_rele(dn, FTAG); 385 } 386 return (db); 387 } 388 389 /* 390 * Insert an entry into the hash table. If there is already an element 391 * equal to elem in the hash table, then the already existing element 392 * will be returned and the new element will not be inserted. 393 * Otherwise returns NULL. 394 */ 395 static dmu_buf_impl_t * 396 dbuf_hash_insert(dmu_buf_impl_t *db) 397 { 398 dbuf_hash_table_t *h = &dbuf_hash_table; 399 objset_t *os = db->db_objset; 400 uint64_t obj = db->db.db_object; 401 int level = db->db_level; 402 uint64_t blkid, idx; 403 dmu_buf_impl_t *dbf; 404 uint32_t i; 405 406 blkid = db->db_blkid; 407 ASSERT3U(dbuf_hash(os, obj, level, blkid), ==, db->db_hash); 408 idx = db->db_hash & h->hash_table_mask; 409 410 mutex_enter(DBUF_HASH_MUTEX(h, idx)); 411 for (dbf = h->hash_table[idx], i = 0; dbf != NULL; 412 dbf = dbf->db_hash_next, i++) { 413 if (DBUF_EQUAL(dbf, os, obj, level, blkid)) { 414 mutex_enter(&dbf->db_mtx); 415 if (dbf->db_state != DB_EVICTING) { 416 mutex_exit(DBUF_HASH_MUTEX(h, idx)); 417 return (dbf); 418 } 419 mutex_exit(&dbf->db_mtx); 420 } 421 } 422 423 if (i > 0) { 424 DBUF_STAT_BUMP(hash_collisions); 425 if (i == 1) 426 DBUF_STAT_BUMP(hash_chains); 427 428 DBUF_STAT_MAX(hash_chain_max, i); 429 } 430 431 mutex_enter(&db->db_mtx); 432 db->db_hash_next = h->hash_table[idx]; 433 h->hash_table[idx] = db; 434 mutex_exit(DBUF_HASH_MUTEX(h, idx)); 435 uint64_t he = atomic_inc_64_nv(&dbuf_stats.hash_elements.value.ui64); 436 DBUF_STAT_MAX(hash_elements_max, he); 437 438 return (NULL); 439 } 440 441 /* 442 * This returns whether this dbuf should be stored in the metadata cache, which 443 * is based on whether it's from one of the dnode types that store data related 444 * to traversing dataset hierarchies. 445 */ 446 static boolean_t 447 dbuf_include_in_metadata_cache(dmu_buf_impl_t *db) 448 { 449 DB_DNODE_ENTER(db); 450 dmu_object_type_t type = DB_DNODE(db)->dn_type; 451 DB_DNODE_EXIT(db); 452 453 /* Check if this dbuf is one of the types we care about */ 454 if (DMU_OT_IS_METADATA_CACHED(type)) { 455 /* If we hit this, then we set something up wrong in dmu_ot */ 456 ASSERT(DMU_OT_IS_METADATA(type)); 457 458 /* 459 * Sanity check for small-memory systems: don't allocate too 460 * much memory for this purpose. 461 */ 462 if (zfs_refcount_count( 463 &dbuf_caches[DB_DBUF_METADATA_CACHE].size) > 464 dbuf_metadata_cache_target_bytes()) { 465 DBUF_STAT_BUMP(metadata_cache_overflow); 466 return (B_FALSE); 467 } 468 469 return (B_TRUE); 470 } 471 472 return (B_FALSE); 473 } 474 475 /* 476 * Remove an entry from the hash table. It must be in the EVICTING state. 477 */ 478 static void 479 dbuf_hash_remove(dmu_buf_impl_t *db) 480 { 481 dbuf_hash_table_t *h = &dbuf_hash_table; 482 uint64_t idx; 483 dmu_buf_impl_t *dbf, **dbp; 484 485 ASSERT3U(dbuf_hash(db->db_objset, db->db.db_object, db->db_level, 486 db->db_blkid), ==, db->db_hash); 487 idx = db->db_hash & h->hash_table_mask; 488 489 /* 490 * We mustn't hold db_mtx to maintain lock ordering: 491 * DBUF_HASH_MUTEX > db_mtx. 492 */ 493 ASSERT(zfs_refcount_is_zero(&db->db_holds)); 494 ASSERT(db->db_state == DB_EVICTING); 495 ASSERT(!MUTEX_HELD(&db->db_mtx)); 496 497 mutex_enter(DBUF_HASH_MUTEX(h, idx)); 498 dbp = &h->hash_table[idx]; 499 while ((dbf = *dbp) != db) { 500 dbp = &dbf->db_hash_next; 501 ASSERT(dbf != NULL); 502 } 503 *dbp = db->db_hash_next; 504 db->db_hash_next = NULL; 505 if (h->hash_table[idx] && 506 h->hash_table[idx]->db_hash_next == NULL) 507 DBUF_STAT_BUMPDOWN(hash_chains); 508 mutex_exit(DBUF_HASH_MUTEX(h, idx)); 509 atomic_dec_64(&dbuf_stats.hash_elements.value.ui64); 510 } 511 512 typedef enum { 513 DBVU_EVICTING, 514 DBVU_NOT_EVICTING 515 } dbvu_verify_type_t; 516 517 static void 518 dbuf_verify_user(dmu_buf_impl_t *db, dbvu_verify_type_t verify_type) 519 { 520 #ifdef ZFS_DEBUG 521 int64_t holds; 522 523 if (db->db_user == NULL) 524 return; 525 526 /* Only data blocks support the attachment of user data. */ 527 ASSERT(db->db_level == 0); 528 529 /* Clients must resolve a dbuf before attaching user data. */ 530 ASSERT(db->db.db_data != NULL); 531 ASSERT3U(db->db_state, ==, DB_CACHED); 532 533 holds = zfs_refcount_count(&db->db_holds); 534 if (verify_type == DBVU_EVICTING) { 535 /* 536 * Immediate eviction occurs when holds == dirtycnt. 537 * For normal eviction buffers, holds is zero on 538 * eviction, except when dbuf_fix_old_data() calls 539 * dbuf_clear_data(). However, the hold count can grow 540 * during eviction even though db_mtx is held (see 541 * dmu_bonus_hold() for an example), so we can only 542 * test the generic invariant that holds >= dirtycnt. 543 */ 544 ASSERT3U(holds, >=, db->db_dirtycnt); 545 } else { 546 if (db->db_user_immediate_evict == TRUE) 547 ASSERT3U(holds, >=, db->db_dirtycnt); 548 else 549 ASSERT3U(holds, >, 0); 550 } 551 #endif 552 } 553 554 static void 555 dbuf_evict_user(dmu_buf_impl_t *db) 556 { 557 dmu_buf_user_t *dbu = db->db_user; 558 559 ASSERT(MUTEX_HELD(&db->db_mtx)); 560 561 if (dbu == NULL) 562 return; 563 564 dbuf_verify_user(db, DBVU_EVICTING); 565 db->db_user = NULL; 566 567 #ifdef ZFS_DEBUG 568 if (dbu->dbu_clear_on_evict_dbufp != NULL) 569 *dbu->dbu_clear_on_evict_dbufp = NULL; 570 #endif 571 572 if (db->db_caching_status != DB_NO_CACHE) { 573 /* 574 * This is a cached dbuf, so the size of the user data is 575 * included in its cached amount. We adjust it here because the 576 * user data has already been detached from the dbuf, and the 577 * sync functions are not supposed to touch it (the dbuf might 578 * not exist anymore by the time the sync functions run. 579 */ 580 uint64_t size = dbu->dbu_size; 581 (void) zfs_refcount_remove_many( 582 &dbuf_caches[db->db_caching_status].size, size, db); 583 if (db->db_caching_status == DB_DBUF_CACHE) 584 DBUF_STAT_DECR(cache_levels_bytes[db->db_level], size); 585 } 586 587 /* 588 * There are two eviction callbacks - one that we call synchronously 589 * and one that we invoke via a taskq. The async one is useful for 590 * avoiding lock order reversals and limiting stack depth. 591 * 592 * Note that if we have a sync callback but no async callback, 593 * it's likely that the sync callback will free the structure 594 * containing the dbu. In that case we need to take care to not 595 * dereference dbu after calling the sync evict func. 596 */ 597 boolean_t has_async = (dbu->dbu_evict_func_async != NULL); 598 599 if (dbu->dbu_evict_func_sync != NULL) 600 dbu->dbu_evict_func_sync(dbu); 601 602 if (has_async) { 603 taskq_dispatch_ent(dbu_evict_taskq, dbu->dbu_evict_func_async, 604 dbu, 0, &dbu->dbu_tqent); 605 } 606 } 607 608 boolean_t 609 dbuf_is_metadata(dmu_buf_impl_t *db) 610 { 611 /* 612 * Consider indirect blocks and spill blocks to be meta data. 613 */ 614 if (db->db_level > 0 || db->db_blkid == DMU_SPILL_BLKID) { 615 return (B_TRUE); 616 } else { 617 boolean_t is_metadata; 618 619 DB_DNODE_ENTER(db); 620 is_metadata = DMU_OT_IS_METADATA(DB_DNODE(db)->dn_type); 621 DB_DNODE_EXIT(db); 622 623 return (is_metadata); 624 } 625 } 626 627 /* 628 * We want to exclude buffers that are on a special allocation class from 629 * L2ARC. 630 */ 631 boolean_t 632 dbuf_is_l2cacheable(dmu_buf_impl_t *db) 633 { 634 if (db->db_objset->os_secondary_cache == ZFS_CACHE_ALL || 635 (db->db_objset->os_secondary_cache == 636 ZFS_CACHE_METADATA && dbuf_is_metadata(db))) { 637 if (l2arc_exclude_special == 0) 638 return (B_TRUE); 639 640 blkptr_t *bp = db->db_blkptr; 641 if (bp == NULL || BP_IS_HOLE(bp)) 642 return (B_FALSE); 643 uint64_t vdev = DVA_GET_VDEV(bp->blk_dva); 644 vdev_t *rvd = db->db_objset->os_spa->spa_root_vdev; 645 vdev_t *vd = NULL; 646 647 if (vdev < rvd->vdev_children) 648 vd = rvd->vdev_child[vdev]; 649 650 if (vd == NULL) 651 return (B_TRUE); 652 653 if (vd->vdev_alloc_bias != VDEV_BIAS_SPECIAL && 654 vd->vdev_alloc_bias != VDEV_BIAS_DEDUP) 655 return (B_TRUE); 656 } 657 return (B_FALSE); 658 } 659 660 static inline boolean_t 661 dnode_level_is_l2cacheable(blkptr_t *bp, dnode_t *dn, int64_t level) 662 { 663 if (dn->dn_objset->os_secondary_cache == ZFS_CACHE_ALL || 664 (dn->dn_objset->os_secondary_cache == ZFS_CACHE_METADATA && 665 (level > 0 || 666 DMU_OT_IS_METADATA(dn->dn_handle->dnh_dnode->dn_type)))) { 667 if (l2arc_exclude_special == 0) 668 return (B_TRUE); 669 670 if (bp == NULL || BP_IS_HOLE(bp)) 671 return (B_FALSE); 672 uint64_t vdev = DVA_GET_VDEV(bp->blk_dva); 673 vdev_t *rvd = dn->dn_objset->os_spa->spa_root_vdev; 674 vdev_t *vd = NULL; 675 676 if (vdev < rvd->vdev_children) 677 vd = rvd->vdev_child[vdev]; 678 679 if (vd == NULL) 680 return (B_TRUE); 681 682 if (vd->vdev_alloc_bias != VDEV_BIAS_SPECIAL && 683 vd->vdev_alloc_bias != VDEV_BIAS_DEDUP) 684 return (B_TRUE); 685 } 686 return (B_FALSE); 687 } 688 689 690 /* 691 * This function *must* return indices evenly distributed between all 692 * sublists of the multilist. This is needed due to how the dbuf eviction 693 * code is laid out; dbuf_evict_thread() assumes dbufs are evenly 694 * distributed between all sublists and uses this assumption when 695 * deciding which sublist to evict from and how much to evict from it. 696 */ 697 static unsigned int 698 dbuf_cache_multilist_index_func(multilist_t *ml, void *obj) 699 { 700 dmu_buf_impl_t *db = obj; 701 702 /* 703 * The assumption here, is the hash value for a given 704 * dmu_buf_impl_t will remain constant throughout it's lifetime 705 * (i.e. it's objset, object, level and blkid fields don't change). 706 * Thus, we don't need to store the dbuf's sublist index 707 * on insertion, as this index can be recalculated on removal. 708 * 709 * Also, the low order bits of the hash value are thought to be 710 * distributed evenly. Otherwise, in the case that the multilist 711 * has a power of two number of sublists, each sublists' usage 712 * would not be evenly distributed. In this context full 64bit 713 * division would be a waste of time, so limit it to 32 bits. 714 */ 715 return ((unsigned int)dbuf_hash(db->db_objset, db->db.db_object, 716 db->db_level, db->db_blkid) % 717 multilist_get_num_sublists(ml)); 718 } 719 720 /* 721 * The target size of the dbuf cache can grow with the ARC target, 722 * unless limited by the tunable dbuf_cache_max_bytes. 723 */ 724 static inline unsigned long 725 dbuf_cache_target_bytes(void) 726 { 727 return (MIN(dbuf_cache_max_bytes, 728 arc_target_bytes() >> dbuf_cache_shift)); 729 } 730 731 /* 732 * The target size of the dbuf metadata cache can grow with the ARC target, 733 * unless limited by the tunable dbuf_metadata_cache_max_bytes. 734 */ 735 static inline unsigned long 736 dbuf_metadata_cache_target_bytes(void) 737 { 738 return (MIN(dbuf_metadata_cache_max_bytes, 739 arc_target_bytes() >> dbuf_metadata_cache_shift)); 740 } 741 742 static inline uint64_t 743 dbuf_cache_hiwater_bytes(void) 744 { 745 uint64_t dbuf_cache_target = dbuf_cache_target_bytes(); 746 return (dbuf_cache_target + 747 (dbuf_cache_target * dbuf_cache_hiwater_pct) / 100); 748 } 749 750 static inline uint64_t 751 dbuf_cache_lowater_bytes(void) 752 { 753 uint64_t dbuf_cache_target = dbuf_cache_target_bytes(); 754 return (dbuf_cache_target - 755 (dbuf_cache_target * dbuf_cache_lowater_pct) / 100); 756 } 757 758 static inline boolean_t 759 dbuf_cache_above_lowater(void) 760 { 761 return (zfs_refcount_count(&dbuf_caches[DB_DBUF_CACHE].size) > 762 dbuf_cache_lowater_bytes()); 763 } 764 765 /* 766 * Evict the oldest eligible dbuf from the dbuf cache. 767 */ 768 static void 769 dbuf_evict_one(void) 770 { 771 int idx = multilist_get_random_index(&dbuf_caches[DB_DBUF_CACHE].cache); 772 multilist_sublist_t *mls = multilist_sublist_lock( 773 &dbuf_caches[DB_DBUF_CACHE].cache, idx); 774 775 ASSERT(!MUTEX_HELD(&dbuf_evict_lock)); 776 777 dmu_buf_impl_t *db = multilist_sublist_tail(mls); 778 while (db != NULL && mutex_tryenter(&db->db_mtx) == 0) { 779 db = multilist_sublist_prev(mls, db); 780 } 781 782 DTRACE_PROBE2(dbuf__evict__one, dmu_buf_impl_t *, db, 783 multilist_sublist_t *, mls); 784 785 if (db != NULL) { 786 multilist_sublist_remove(mls, db); 787 multilist_sublist_unlock(mls); 788 uint64_t size = db->db.db_size + dmu_buf_user_size(&db->db); 789 (void) zfs_refcount_remove_many( 790 &dbuf_caches[DB_DBUF_CACHE].size, size, db); 791 DBUF_STAT_BUMPDOWN(cache_levels[db->db_level]); 792 DBUF_STAT_BUMPDOWN(cache_count); 793 DBUF_STAT_DECR(cache_levels_bytes[db->db_level], size); 794 ASSERT3U(db->db_caching_status, ==, DB_DBUF_CACHE); 795 db->db_caching_status = DB_NO_CACHE; 796 dbuf_destroy(db); 797 DBUF_STAT_BUMP(cache_total_evicts); 798 } else { 799 multilist_sublist_unlock(mls); 800 } 801 } 802 803 /* 804 * The dbuf evict thread is responsible for aging out dbufs from the 805 * cache. Once the cache has reached it's maximum size, dbufs are removed 806 * and destroyed. The eviction thread will continue running until the size 807 * of the dbuf cache is at or below the maximum size. Once the dbuf is aged 808 * out of the cache it is destroyed and becomes eligible for arc eviction. 809 */ 810 static __attribute__((noreturn)) void 811 dbuf_evict_thread(void *unused) 812 { 813 (void) unused; 814 callb_cpr_t cpr; 815 816 CALLB_CPR_INIT(&cpr, &dbuf_evict_lock, callb_generic_cpr, FTAG); 817 818 mutex_enter(&dbuf_evict_lock); 819 while (!dbuf_evict_thread_exit) { 820 while (!dbuf_cache_above_lowater() && !dbuf_evict_thread_exit) { 821 CALLB_CPR_SAFE_BEGIN(&cpr); 822 (void) cv_timedwait_idle_hires(&dbuf_evict_cv, 823 &dbuf_evict_lock, SEC2NSEC(1), MSEC2NSEC(1), 0); 824 CALLB_CPR_SAFE_END(&cpr, &dbuf_evict_lock); 825 } 826 mutex_exit(&dbuf_evict_lock); 827 828 /* 829 * Keep evicting as long as we're above the low water mark 830 * for the cache. We do this without holding the locks to 831 * minimize lock contention. 832 */ 833 while (dbuf_cache_above_lowater() && !dbuf_evict_thread_exit) { 834 dbuf_evict_one(); 835 } 836 837 mutex_enter(&dbuf_evict_lock); 838 } 839 840 dbuf_evict_thread_exit = B_FALSE; 841 cv_broadcast(&dbuf_evict_cv); 842 CALLB_CPR_EXIT(&cpr); /* drops dbuf_evict_lock */ 843 thread_exit(); 844 } 845 846 /* 847 * Wake up the dbuf eviction thread if the dbuf cache is at its max size. 848 * If the dbuf cache is at its high water mark, then evict a dbuf from the 849 * dbuf cache using the caller's context. 850 */ 851 static void 852 dbuf_evict_notify(uint64_t size) 853 { 854 /* 855 * We check if we should evict without holding the dbuf_evict_lock, 856 * because it's OK to occasionally make the wrong decision here, 857 * and grabbing the lock results in massive lock contention. 858 */ 859 if (size > dbuf_cache_target_bytes()) { 860 if (size > dbuf_cache_hiwater_bytes()) 861 dbuf_evict_one(); 862 cv_signal(&dbuf_evict_cv); 863 } 864 } 865 866 static int 867 dbuf_kstat_update(kstat_t *ksp, int rw) 868 { 869 dbuf_stats_t *ds = ksp->ks_data; 870 dbuf_hash_table_t *h = &dbuf_hash_table; 871 872 if (rw == KSTAT_WRITE) 873 return (SET_ERROR(EACCES)); 874 875 ds->cache_count.value.ui64 = 876 wmsum_value(&dbuf_sums.cache_count); 877 ds->cache_size_bytes.value.ui64 = 878 zfs_refcount_count(&dbuf_caches[DB_DBUF_CACHE].size); 879 ds->cache_target_bytes.value.ui64 = dbuf_cache_target_bytes(); 880 ds->cache_hiwater_bytes.value.ui64 = dbuf_cache_hiwater_bytes(); 881 ds->cache_lowater_bytes.value.ui64 = dbuf_cache_lowater_bytes(); 882 ds->cache_total_evicts.value.ui64 = 883 wmsum_value(&dbuf_sums.cache_total_evicts); 884 for (int i = 0; i < DN_MAX_LEVELS; i++) { 885 ds->cache_levels[i].value.ui64 = 886 wmsum_value(&dbuf_sums.cache_levels[i]); 887 ds->cache_levels_bytes[i].value.ui64 = 888 wmsum_value(&dbuf_sums.cache_levels_bytes[i]); 889 } 890 ds->hash_hits.value.ui64 = 891 wmsum_value(&dbuf_sums.hash_hits); 892 ds->hash_misses.value.ui64 = 893 wmsum_value(&dbuf_sums.hash_misses); 894 ds->hash_collisions.value.ui64 = 895 wmsum_value(&dbuf_sums.hash_collisions); 896 ds->hash_chains.value.ui64 = 897 wmsum_value(&dbuf_sums.hash_chains); 898 ds->hash_insert_race.value.ui64 = 899 wmsum_value(&dbuf_sums.hash_insert_race); 900 ds->hash_table_count.value.ui64 = h->hash_table_mask + 1; 901 ds->hash_mutex_count.value.ui64 = h->hash_mutex_mask + 1; 902 ds->metadata_cache_count.value.ui64 = 903 wmsum_value(&dbuf_sums.metadata_cache_count); 904 ds->metadata_cache_size_bytes.value.ui64 = zfs_refcount_count( 905 &dbuf_caches[DB_DBUF_METADATA_CACHE].size); 906 ds->metadata_cache_overflow.value.ui64 = 907 wmsum_value(&dbuf_sums.metadata_cache_overflow); 908 return (0); 909 } 910 911 void 912 dbuf_init(void) 913 { 914 uint64_t hmsize, hsize = 1ULL << 16; 915 dbuf_hash_table_t *h = &dbuf_hash_table; 916 917 /* 918 * The hash table is big enough to fill one eighth of physical memory 919 * with an average block size of zfs_arc_average_blocksize (default 8K). 920 * By default, the table will take up 921 * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers). 922 */ 923 while (hsize * zfs_arc_average_blocksize < arc_all_memory() / 8) 924 hsize <<= 1; 925 926 h->hash_table = NULL; 927 while (h->hash_table == NULL) { 928 h->hash_table_mask = hsize - 1; 929 930 h->hash_table = vmem_zalloc(hsize * sizeof (void *), KM_SLEEP); 931 if (h->hash_table == NULL) 932 hsize >>= 1; 933 934 ASSERT3U(hsize, >=, 1ULL << 10); 935 } 936 937 /* 938 * The hash table buckets are protected by an array of mutexes where 939 * each mutex is reponsible for protecting 128 buckets. A minimum 940 * array size of 8192 is targeted to avoid contention. 941 */ 942 if (dbuf_mutex_cache_shift == 0) 943 hmsize = MAX(hsize >> 7, 1ULL << 13); 944 else 945 hmsize = 1ULL << MIN(dbuf_mutex_cache_shift, 24); 946 947 h->hash_mutexes = NULL; 948 while (h->hash_mutexes == NULL) { 949 h->hash_mutex_mask = hmsize - 1; 950 951 h->hash_mutexes = vmem_zalloc(hmsize * sizeof (kmutex_t), 952 KM_SLEEP); 953 if (h->hash_mutexes == NULL) 954 hmsize >>= 1; 955 } 956 957 dbuf_kmem_cache = kmem_cache_create("dmu_buf_impl_t", 958 sizeof (dmu_buf_impl_t), 959 0, dbuf_cons, dbuf_dest, NULL, NULL, NULL, 0); 960 961 for (int i = 0; i < hmsize; i++) 962 mutex_init(&h->hash_mutexes[i], NULL, MUTEX_DEFAULT, NULL); 963 964 dbuf_stats_init(h); 965 966 /* 967 * All entries are queued via taskq_dispatch_ent(), so min/maxalloc 968 * configuration is not required. 969 */ 970 dbu_evict_taskq = taskq_create("dbu_evict", 1, defclsyspri, 0, 0, 0); 971 972 for (dbuf_cached_state_t dcs = 0; dcs < DB_CACHE_MAX; dcs++) { 973 multilist_create(&dbuf_caches[dcs].cache, 974 sizeof (dmu_buf_impl_t), 975 offsetof(dmu_buf_impl_t, db_cache_link), 976 dbuf_cache_multilist_index_func); 977 zfs_refcount_create(&dbuf_caches[dcs].size); 978 } 979 980 dbuf_evict_thread_exit = B_FALSE; 981 mutex_init(&dbuf_evict_lock, NULL, MUTEX_DEFAULT, NULL); 982 cv_init(&dbuf_evict_cv, NULL, CV_DEFAULT, NULL); 983 dbuf_cache_evict_thread = thread_create(NULL, 0, dbuf_evict_thread, 984 NULL, 0, &p0, TS_RUN, minclsyspri); 985 986 wmsum_init(&dbuf_sums.cache_count, 0); 987 wmsum_init(&dbuf_sums.cache_total_evicts, 0); 988 for (int i = 0; i < DN_MAX_LEVELS; i++) { 989 wmsum_init(&dbuf_sums.cache_levels[i], 0); 990 wmsum_init(&dbuf_sums.cache_levels_bytes[i], 0); 991 } 992 wmsum_init(&dbuf_sums.hash_hits, 0); 993 wmsum_init(&dbuf_sums.hash_misses, 0); 994 wmsum_init(&dbuf_sums.hash_collisions, 0); 995 wmsum_init(&dbuf_sums.hash_chains, 0); 996 wmsum_init(&dbuf_sums.hash_insert_race, 0); 997 wmsum_init(&dbuf_sums.metadata_cache_count, 0); 998 wmsum_init(&dbuf_sums.metadata_cache_overflow, 0); 999 1000 dbuf_ksp = kstat_create("zfs", 0, "dbufstats", "misc", 1001 KSTAT_TYPE_NAMED, sizeof (dbuf_stats) / sizeof (kstat_named_t), 1002 KSTAT_FLAG_VIRTUAL); 1003 if (dbuf_ksp != NULL) { 1004 for (int i = 0; i < DN_MAX_LEVELS; i++) { 1005 snprintf(dbuf_stats.cache_levels[i].name, 1006 KSTAT_STRLEN, "cache_level_%d", i); 1007 dbuf_stats.cache_levels[i].data_type = 1008 KSTAT_DATA_UINT64; 1009 snprintf(dbuf_stats.cache_levels_bytes[i].name, 1010 KSTAT_STRLEN, "cache_level_%d_bytes", i); 1011 dbuf_stats.cache_levels_bytes[i].data_type = 1012 KSTAT_DATA_UINT64; 1013 } 1014 dbuf_ksp->ks_data = &dbuf_stats; 1015 dbuf_ksp->ks_update = dbuf_kstat_update; 1016 kstat_install(dbuf_ksp); 1017 } 1018 } 1019 1020 void 1021 dbuf_fini(void) 1022 { 1023 dbuf_hash_table_t *h = &dbuf_hash_table; 1024 1025 dbuf_stats_destroy(); 1026 1027 for (int i = 0; i < (h->hash_mutex_mask + 1); i++) 1028 mutex_destroy(&h->hash_mutexes[i]); 1029 1030 vmem_free(h->hash_table, (h->hash_table_mask + 1) * sizeof (void *)); 1031 vmem_free(h->hash_mutexes, (h->hash_mutex_mask + 1) * 1032 sizeof (kmutex_t)); 1033 1034 kmem_cache_destroy(dbuf_kmem_cache); 1035 taskq_destroy(dbu_evict_taskq); 1036 1037 mutex_enter(&dbuf_evict_lock); 1038 dbuf_evict_thread_exit = B_TRUE; 1039 while (dbuf_evict_thread_exit) { 1040 cv_signal(&dbuf_evict_cv); 1041 cv_wait(&dbuf_evict_cv, &dbuf_evict_lock); 1042 } 1043 mutex_exit(&dbuf_evict_lock); 1044 1045 mutex_destroy(&dbuf_evict_lock); 1046 cv_destroy(&dbuf_evict_cv); 1047 1048 for (dbuf_cached_state_t dcs = 0; dcs < DB_CACHE_MAX; dcs++) { 1049 zfs_refcount_destroy(&dbuf_caches[dcs].size); 1050 multilist_destroy(&dbuf_caches[dcs].cache); 1051 } 1052 1053 if (dbuf_ksp != NULL) { 1054 kstat_delete(dbuf_ksp); 1055 dbuf_ksp = NULL; 1056 } 1057 1058 wmsum_fini(&dbuf_sums.cache_count); 1059 wmsum_fini(&dbuf_sums.cache_total_evicts); 1060 for (int i = 0; i < DN_MAX_LEVELS; i++) { 1061 wmsum_fini(&dbuf_sums.cache_levels[i]); 1062 wmsum_fini(&dbuf_sums.cache_levels_bytes[i]); 1063 } 1064 wmsum_fini(&dbuf_sums.hash_hits); 1065 wmsum_fini(&dbuf_sums.hash_misses); 1066 wmsum_fini(&dbuf_sums.hash_collisions); 1067 wmsum_fini(&dbuf_sums.hash_chains); 1068 wmsum_fini(&dbuf_sums.hash_insert_race); 1069 wmsum_fini(&dbuf_sums.metadata_cache_count); 1070 wmsum_fini(&dbuf_sums.metadata_cache_overflow); 1071 } 1072 1073 /* 1074 * Other stuff. 1075 */ 1076 1077 #ifdef ZFS_DEBUG 1078 static void 1079 dbuf_verify(dmu_buf_impl_t *db) 1080 { 1081 dnode_t *dn; 1082 dbuf_dirty_record_t *dr; 1083 uint32_t txg_prev; 1084 1085 ASSERT(MUTEX_HELD(&db->db_mtx)); 1086 1087 if (!(zfs_flags & ZFS_DEBUG_DBUF_VERIFY)) 1088 return; 1089 1090 ASSERT(db->db_objset != NULL); 1091 DB_DNODE_ENTER(db); 1092 dn = DB_DNODE(db); 1093 if (dn == NULL) { 1094 ASSERT(db->db_parent == NULL); 1095 ASSERT(db->db_blkptr == NULL); 1096 } else { 1097 ASSERT3U(db->db.db_object, ==, dn->dn_object); 1098 ASSERT3P(db->db_objset, ==, dn->dn_objset); 1099 ASSERT3U(db->db_level, <, dn->dn_nlevels); 1100 ASSERT(db->db_blkid == DMU_BONUS_BLKID || 1101 db->db_blkid == DMU_SPILL_BLKID || 1102 !avl_is_empty(&dn->dn_dbufs)); 1103 } 1104 if (db->db_blkid == DMU_BONUS_BLKID) { 1105 ASSERT(dn != NULL); 1106 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen); 1107 ASSERT3U(db->db.db_offset, ==, DMU_BONUS_BLKID); 1108 } else if (db->db_blkid == DMU_SPILL_BLKID) { 1109 ASSERT(dn != NULL); 1110 ASSERT0(db->db.db_offset); 1111 } else { 1112 ASSERT3U(db->db.db_offset, ==, db->db_blkid * db->db.db_size); 1113 } 1114 1115 if ((dr = list_head(&db->db_dirty_records)) != NULL) { 1116 ASSERT(dr->dr_dbuf == db); 1117 txg_prev = dr->dr_txg; 1118 for (dr = list_next(&db->db_dirty_records, dr); dr != NULL; 1119 dr = list_next(&db->db_dirty_records, dr)) { 1120 ASSERT(dr->dr_dbuf == db); 1121 ASSERT(txg_prev > dr->dr_txg); 1122 txg_prev = dr->dr_txg; 1123 } 1124 } 1125 1126 /* 1127 * We can't assert that db_size matches dn_datablksz because it 1128 * can be momentarily different when another thread is doing 1129 * dnode_set_blksz(). 1130 */ 1131 if (db->db_level == 0 && db->db.db_object == DMU_META_DNODE_OBJECT) { 1132 dr = db->db_data_pending; 1133 /* 1134 * It should only be modified in syncing context, so 1135 * make sure we only have one copy of the data. 1136 */ 1137 ASSERT(dr == NULL || dr->dt.dl.dr_data == db->db_buf); 1138 } 1139 1140 /* verify db->db_blkptr */ 1141 if (db->db_blkptr) { 1142 if (db->db_parent == dn->dn_dbuf) { 1143 /* db is pointed to by the dnode */ 1144 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */ 1145 if (DMU_OBJECT_IS_SPECIAL(db->db.db_object)) 1146 ASSERT(db->db_parent == NULL); 1147 else 1148 ASSERT(db->db_parent != NULL); 1149 if (db->db_blkid != DMU_SPILL_BLKID) 1150 ASSERT3P(db->db_blkptr, ==, 1151 &dn->dn_phys->dn_blkptr[db->db_blkid]); 1152 } else { 1153 /* db is pointed to by an indirect block */ 1154 int epb __maybe_unused = db->db_parent->db.db_size >> 1155 SPA_BLKPTRSHIFT; 1156 ASSERT3U(db->db_parent->db_level, ==, db->db_level+1); 1157 ASSERT3U(db->db_parent->db.db_object, ==, 1158 db->db.db_object); 1159 /* 1160 * dnode_grow_indblksz() can make this fail if we don't 1161 * have the parent's rwlock. XXX indblksz no longer 1162 * grows. safe to do this now? 1163 */ 1164 if (RW_LOCK_HELD(&db->db_parent->db_rwlock)) { 1165 ASSERT3P(db->db_blkptr, ==, 1166 ((blkptr_t *)db->db_parent->db.db_data + 1167 db->db_blkid % epb)); 1168 } 1169 } 1170 } 1171 if ((db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr)) && 1172 (db->db_buf == NULL || db->db_buf->b_data) && 1173 db->db.db_data && db->db_blkid != DMU_BONUS_BLKID && 1174 db->db_state != DB_FILL && (dn == NULL || !dn->dn_free_txg)) { 1175 /* 1176 * If the blkptr isn't set but they have nonzero data, 1177 * it had better be dirty, otherwise we'll lose that 1178 * data when we evict this buffer. 1179 * 1180 * There is an exception to this rule for indirect blocks; in 1181 * this case, if the indirect block is a hole, we fill in a few 1182 * fields on each of the child blocks (importantly, birth time) 1183 * to prevent hole birth times from being lost when you 1184 * partially fill in a hole. 1185 */ 1186 if (db->db_dirtycnt == 0) { 1187 if (db->db_level == 0) { 1188 uint64_t *buf = db->db.db_data; 1189 int i; 1190 1191 for (i = 0; i < db->db.db_size >> 3; i++) { 1192 ASSERT(buf[i] == 0); 1193 } 1194 } else { 1195 blkptr_t *bps = db->db.db_data; 1196 ASSERT3U(1 << DB_DNODE(db)->dn_indblkshift, ==, 1197 db->db.db_size); 1198 /* 1199 * We want to verify that all the blkptrs in the 1200 * indirect block are holes, but we may have 1201 * automatically set up a few fields for them. 1202 * We iterate through each blkptr and verify 1203 * they only have those fields set. 1204 */ 1205 for (int i = 0; 1206 i < db->db.db_size / sizeof (blkptr_t); 1207 i++) { 1208 blkptr_t *bp = &bps[i]; 1209 ASSERT(ZIO_CHECKSUM_IS_ZERO( 1210 &bp->blk_cksum)); 1211 ASSERT( 1212 DVA_IS_EMPTY(&bp->blk_dva[0]) && 1213 DVA_IS_EMPTY(&bp->blk_dva[1]) && 1214 DVA_IS_EMPTY(&bp->blk_dva[2])); 1215 ASSERT0(bp->blk_fill); 1216 ASSERT0(bp->blk_pad[0]); 1217 ASSERT0(bp->blk_pad[1]); 1218 ASSERT(!BP_IS_EMBEDDED(bp)); 1219 ASSERT(BP_IS_HOLE(bp)); 1220 ASSERT0(bp->blk_phys_birth); 1221 } 1222 } 1223 } 1224 } 1225 DB_DNODE_EXIT(db); 1226 } 1227 #endif 1228 1229 static void 1230 dbuf_clear_data(dmu_buf_impl_t *db) 1231 { 1232 ASSERT(MUTEX_HELD(&db->db_mtx)); 1233 dbuf_evict_user(db); 1234 ASSERT3P(db->db_buf, ==, NULL); 1235 db->db.db_data = NULL; 1236 if (db->db_state != DB_NOFILL) { 1237 db->db_state = DB_UNCACHED; 1238 DTRACE_SET_STATE(db, "clear data"); 1239 } 1240 } 1241 1242 static void 1243 dbuf_set_data(dmu_buf_impl_t *db, arc_buf_t *buf) 1244 { 1245 ASSERT(MUTEX_HELD(&db->db_mtx)); 1246 ASSERT(buf != NULL); 1247 1248 db->db_buf = buf; 1249 ASSERT(buf->b_data != NULL); 1250 db->db.db_data = buf->b_data; 1251 } 1252 1253 static arc_buf_t * 1254 dbuf_alloc_arcbuf(dmu_buf_impl_t *db) 1255 { 1256 spa_t *spa = db->db_objset->os_spa; 1257 1258 return (arc_alloc_buf(spa, db, DBUF_GET_BUFC_TYPE(db), db->db.db_size)); 1259 } 1260 1261 /* 1262 * Loan out an arc_buf for read. Return the loaned arc_buf. 1263 */ 1264 arc_buf_t * 1265 dbuf_loan_arcbuf(dmu_buf_impl_t *db) 1266 { 1267 arc_buf_t *abuf; 1268 1269 ASSERT(db->db_blkid != DMU_BONUS_BLKID); 1270 mutex_enter(&db->db_mtx); 1271 if (arc_released(db->db_buf) || zfs_refcount_count(&db->db_holds) > 1) { 1272 int blksz = db->db.db_size; 1273 spa_t *spa = db->db_objset->os_spa; 1274 1275 mutex_exit(&db->db_mtx); 1276 abuf = arc_loan_buf(spa, B_FALSE, blksz); 1277 memcpy(abuf->b_data, db->db.db_data, blksz); 1278 } else { 1279 abuf = db->db_buf; 1280 arc_loan_inuse_buf(abuf, db); 1281 db->db_buf = NULL; 1282 dbuf_clear_data(db); 1283 mutex_exit(&db->db_mtx); 1284 } 1285 return (abuf); 1286 } 1287 1288 /* 1289 * Calculate which level n block references the data at the level 0 offset 1290 * provided. 1291 */ 1292 uint64_t 1293 dbuf_whichblock(const dnode_t *dn, const int64_t level, const uint64_t offset) 1294 { 1295 if (dn->dn_datablkshift != 0 && dn->dn_indblkshift != 0) { 1296 /* 1297 * The level n blkid is equal to the level 0 blkid divided by 1298 * the number of level 0s in a level n block. 1299 * 1300 * The level 0 blkid is offset >> datablkshift = 1301 * offset / 2^datablkshift. 1302 * 1303 * The number of level 0s in a level n is the number of block 1304 * pointers in an indirect block, raised to the power of level. 1305 * This is 2^(indblkshift - SPA_BLKPTRSHIFT)^level = 1306 * 2^(level*(indblkshift - SPA_BLKPTRSHIFT)). 1307 * 1308 * Thus, the level n blkid is: offset / 1309 * ((2^datablkshift)*(2^(level*(indblkshift-SPA_BLKPTRSHIFT)))) 1310 * = offset / 2^(datablkshift + level * 1311 * (indblkshift - SPA_BLKPTRSHIFT)) 1312 * = offset >> (datablkshift + level * 1313 * (indblkshift - SPA_BLKPTRSHIFT)) 1314 */ 1315 1316 const unsigned exp = dn->dn_datablkshift + 1317 level * (dn->dn_indblkshift - SPA_BLKPTRSHIFT); 1318 1319 if (exp >= 8 * sizeof (offset)) { 1320 /* This only happens on the highest indirection level */ 1321 ASSERT3U(level, ==, dn->dn_nlevels - 1); 1322 return (0); 1323 } 1324 1325 ASSERT3U(exp, <, 8 * sizeof (offset)); 1326 1327 return (offset >> exp); 1328 } else { 1329 ASSERT3U(offset, <, dn->dn_datablksz); 1330 return (0); 1331 } 1332 } 1333 1334 /* 1335 * This function is used to lock the parent of the provided dbuf. This should be 1336 * used when modifying or reading db_blkptr. 1337 */ 1338 db_lock_type_t 1339 dmu_buf_lock_parent(dmu_buf_impl_t *db, krw_t rw, const void *tag) 1340 { 1341 enum db_lock_type ret = DLT_NONE; 1342 if (db->db_parent != NULL) { 1343 rw_enter(&db->db_parent->db_rwlock, rw); 1344 ret = DLT_PARENT; 1345 } else if (dmu_objset_ds(db->db_objset) != NULL) { 1346 rrw_enter(&dmu_objset_ds(db->db_objset)->ds_bp_rwlock, rw, 1347 tag); 1348 ret = DLT_OBJSET; 1349 } 1350 /* 1351 * We only return a DLT_NONE lock when it's the top-most indirect block 1352 * of the meta-dnode of the MOS. 1353 */ 1354 return (ret); 1355 } 1356 1357 /* 1358 * We need to pass the lock type in because it's possible that the block will 1359 * move from being the topmost indirect block in a dnode (and thus, have no 1360 * parent) to not the top-most via an indirection increase. This would cause a 1361 * panic if we didn't pass the lock type in. 1362 */ 1363 void 1364 dmu_buf_unlock_parent(dmu_buf_impl_t *db, db_lock_type_t type, const void *tag) 1365 { 1366 if (type == DLT_PARENT) 1367 rw_exit(&db->db_parent->db_rwlock); 1368 else if (type == DLT_OBJSET) 1369 rrw_exit(&dmu_objset_ds(db->db_objset)->ds_bp_rwlock, tag); 1370 } 1371 1372 static void 1373 dbuf_read_done(zio_t *zio, const zbookmark_phys_t *zb, const blkptr_t *bp, 1374 arc_buf_t *buf, void *vdb) 1375 { 1376 (void) zb, (void) bp; 1377 dmu_buf_impl_t *db = vdb; 1378 1379 mutex_enter(&db->db_mtx); 1380 ASSERT3U(db->db_state, ==, DB_READ); 1381 /* 1382 * All reads are synchronous, so we must have a hold on the dbuf 1383 */ 1384 ASSERT(zfs_refcount_count(&db->db_holds) > 0); 1385 ASSERT(db->db_buf == NULL); 1386 ASSERT(db->db.db_data == NULL); 1387 if (buf == NULL) { 1388 /* i/o error */ 1389 ASSERT(zio == NULL || zio->io_error != 0); 1390 ASSERT(db->db_blkid != DMU_BONUS_BLKID); 1391 ASSERT3P(db->db_buf, ==, NULL); 1392 db->db_state = DB_UNCACHED; 1393 DTRACE_SET_STATE(db, "i/o error"); 1394 } else if (db->db_level == 0 && db->db_freed_in_flight) { 1395 /* freed in flight */ 1396 ASSERT(zio == NULL || zio->io_error == 0); 1397 arc_release(buf, db); 1398 memset(buf->b_data, 0, db->db.db_size); 1399 arc_buf_freeze(buf); 1400 db->db_freed_in_flight = FALSE; 1401 dbuf_set_data(db, buf); 1402 db->db_state = DB_CACHED; 1403 DTRACE_SET_STATE(db, "freed in flight"); 1404 } else { 1405 /* success */ 1406 ASSERT(zio == NULL || zio->io_error == 0); 1407 dbuf_set_data(db, buf); 1408 db->db_state = DB_CACHED; 1409 DTRACE_SET_STATE(db, "successful read"); 1410 } 1411 cv_broadcast(&db->db_changed); 1412 dbuf_rele_and_unlock(db, NULL, B_FALSE); 1413 } 1414 1415 /* 1416 * Shortcut for performing reads on bonus dbufs. Returns 1417 * an error if we fail to verify the dnode associated with 1418 * a decrypted block. Otherwise success. 1419 */ 1420 static int 1421 dbuf_read_bonus(dmu_buf_impl_t *db, dnode_t *dn, uint32_t flags) 1422 { 1423 int bonuslen, max_bonuslen, err; 1424 1425 err = dbuf_read_verify_dnode_crypt(db, flags); 1426 if (err) 1427 return (err); 1428 1429 bonuslen = MIN(dn->dn_bonuslen, dn->dn_phys->dn_bonuslen); 1430 max_bonuslen = DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots); 1431 ASSERT(MUTEX_HELD(&db->db_mtx)); 1432 ASSERT(DB_DNODE_HELD(db)); 1433 ASSERT3U(bonuslen, <=, db->db.db_size); 1434 db->db.db_data = kmem_alloc(max_bonuslen, KM_SLEEP); 1435 arc_space_consume(max_bonuslen, ARC_SPACE_BONUS); 1436 if (bonuslen < max_bonuslen) 1437 memset(db->db.db_data, 0, max_bonuslen); 1438 if (bonuslen) 1439 memcpy(db->db.db_data, DN_BONUS(dn->dn_phys), bonuslen); 1440 db->db_state = DB_CACHED; 1441 DTRACE_SET_STATE(db, "bonus buffer filled"); 1442 return (0); 1443 } 1444 1445 static void 1446 dbuf_handle_indirect_hole(dmu_buf_impl_t *db, dnode_t *dn, blkptr_t *dbbp) 1447 { 1448 blkptr_t *bps = db->db.db_data; 1449 uint32_t indbs = 1ULL << dn->dn_indblkshift; 1450 int n_bps = indbs >> SPA_BLKPTRSHIFT; 1451 1452 for (int i = 0; i < n_bps; i++) { 1453 blkptr_t *bp = &bps[i]; 1454 1455 ASSERT3U(BP_GET_LSIZE(dbbp), ==, indbs); 1456 BP_SET_LSIZE(bp, BP_GET_LEVEL(dbbp) == 1 ? 1457 dn->dn_datablksz : BP_GET_LSIZE(dbbp)); 1458 BP_SET_TYPE(bp, BP_GET_TYPE(dbbp)); 1459 BP_SET_LEVEL(bp, BP_GET_LEVEL(dbbp) - 1); 1460 BP_SET_BIRTH(bp, dbbp->blk_birth, 0); 1461 } 1462 } 1463 1464 /* 1465 * Handle reads on dbufs that are holes, if necessary. This function 1466 * requires that the dbuf's mutex is held. Returns success (0) if action 1467 * was taken, ENOENT if no action was taken. 1468 */ 1469 static int 1470 dbuf_read_hole(dmu_buf_impl_t *db, dnode_t *dn, blkptr_t *bp) 1471 { 1472 ASSERT(MUTEX_HELD(&db->db_mtx)); 1473 1474 int is_hole = bp == NULL || BP_IS_HOLE(bp); 1475 /* 1476 * For level 0 blocks only, if the above check fails: 1477 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync() 1478 * processes the delete record and clears the bp while we are waiting 1479 * for the dn_mtx (resulting in a "no" from block_freed). 1480 */ 1481 if (!is_hole && db->db_level == 0) 1482 is_hole = dnode_block_freed(dn, db->db_blkid) || BP_IS_HOLE(bp); 1483 1484 if (is_hole) { 1485 dbuf_set_data(db, dbuf_alloc_arcbuf(db)); 1486 memset(db->db.db_data, 0, db->db.db_size); 1487 1488 if (bp != NULL && db->db_level > 0 && BP_IS_HOLE(bp) && 1489 bp->blk_birth != 0) { 1490 dbuf_handle_indirect_hole(db, dn, bp); 1491 } 1492 db->db_state = DB_CACHED; 1493 DTRACE_SET_STATE(db, "hole read satisfied"); 1494 return (0); 1495 } 1496 return (ENOENT); 1497 } 1498 1499 /* 1500 * This function ensures that, when doing a decrypting read of a block, 1501 * we make sure we have decrypted the dnode associated with it. We must do 1502 * this so that we ensure we are fully authenticating the checksum-of-MACs 1503 * tree from the root of the objset down to this block. Indirect blocks are 1504 * always verified against their secure checksum-of-MACs assuming that the 1505 * dnode containing them is correct. Now that we are doing a decrypting read, 1506 * we can be sure that the key is loaded and verify that assumption. This is 1507 * especially important considering that we always read encrypted dnode 1508 * blocks as raw data (without verifying their MACs) to start, and 1509 * decrypt / authenticate them when we need to read an encrypted bonus buffer. 1510 */ 1511 static int 1512 dbuf_read_verify_dnode_crypt(dmu_buf_impl_t *db, uint32_t flags) 1513 { 1514 int err = 0; 1515 objset_t *os = db->db_objset; 1516 arc_buf_t *dnode_abuf; 1517 dnode_t *dn; 1518 zbookmark_phys_t zb; 1519 1520 ASSERT(MUTEX_HELD(&db->db_mtx)); 1521 1522 if ((flags & DB_RF_NO_DECRYPT) != 0 || 1523 !os->os_encrypted || os->os_raw_receive) 1524 return (0); 1525 1526 DB_DNODE_ENTER(db); 1527 dn = DB_DNODE(db); 1528 dnode_abuf = (dn->dn_dbuf != NULL) ? dn->dn_dbuf->db_buf : NULL; 1529 1530 if (dnode_abuf == NULL || !arc_is_encrypted(dnode_abuf)) { 1531 DB_DNODE_EXIT(db); 1532 return (0); 1533 } 1534 1535 SET_BOOKMARK(&zb, dmu_objset_id(os), 1536 DMU_META_DNODE_OBJECT, 0, dn->dn_dbuf->db_blkid); 1537 err = arc_untransform(dnode_abuf, os->os_spa, &zb, B_TRUE); 1538 1539 /* 1540 * An error code of EACCES tells us that the key is still not 1541 * available. This is ok if we are only reading authenticated 1542 * (and therefore non-encrypted) blocks. 1543 */ 1544 if (err == EACCES && ((db->db_blkid != DMU_BONUS_BLKID && 1545 !DMU_OT_IS_ENCRYPTED(dn->dn_type)) || 1546 (db->db_blkid == DMU_BONUS_BLKID && 1547 !DMU_OT_IS_ENCRYPTED(dn->dn_bonustype)))) 1548 err = 0; 1549 1550 DB_DNODE_EXIT(db); 1551 1552 return (err); 1553 } 1554 1555 /* 1556 * Drops db_mtx and the parent lock specified by dblt and tag before 1557 * returning. 1558 */ 1559 static int 1560 dbuf_read_impl(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags, 1561 db_lock_type_t dblt, const void *tag) 1562 { 1563 dnode_t *dn; 1564 zbookmark_phys_t zb; 1565 uint32_t aflags = ARC_FLAG_NOWAIT; 1566 int err, zio_flags; 1567 blkptr_t bp, *bpp; 1568 1569 DB_DNODE_ENTER(db); 1570 dn = DB_DNODE(db); 1571 ASSERT(!zfs_refcount_is_zero(&db->db_holds)); 1572 ASSERT(MUTEX_HELD(&db->db_mtx)); 1573 ASSERT(db->db_state == DB_UNCACHED || db->db_state == DB_NOFILL); 1574 ASSERT(db->db_buf == NULL); 1575 ASSERT(db->db_parent == NULL || 1576 RW_LOCK_HELD(&db->db_parent->db_rwlock)); 1577 1578 if (db->db_blkid == DMU_BONUS_BLKID) { 1579 err = dbuf_read_bonus(db, dn, flags); 1580 goto early_unlock; 1581 } 1582 1583 if (db->db_state == DB_UNCACHED) { 1584 if (db->db_blkptr == NULL) { 1585 bpp = NULL; 1586 } else { 1587 bp = *db->db_blkptr; 1588 bpp = &bp; 1589 } 1590 } else { 1591 dbuf_dirty_record_t *dr; 1592 1593 ASSERT3S(db->db_state, ==, DB_NOFILL); 1594 1595 /* 1596 * Block cloning: If we have a pending block clone, 1597 * we don't want to read the underlying block, but the content 1598 * of the block being cloned, so we have the most recent data. 1599 */ 1600 dr = list_head(&db->db_dirty_records); 1601 if (dr == NULL || !dr->dt.dl.dr_brtwrite) { 1602 err = EIO; 1603 goto early_unlock; 1604 } 1605 bp = dr->dt.dl.dr_overridden_by; 1606 bpp = &bp; 1607 } 1608 1609 err = dbuf_read_hole(db, dn, bpp); 1610 if (err == 0) 1611 goto early_unlock; 1612 1613 ASSERT(bpp != NULL); 1614 1615 /* 1616 * Any attempt to read a redacted block should result in an error. This 1617 * will never happen under normal conditions, but can be useful for 1618 * debugging purposes. 1619 */ 1620 if (BP_IS_REDACTED(bpp)) { 1621 ASSERT(dsl_dataset_feature_is_active( 1622 db->db_objset->os_dsl_dataset, 1623 SPA_FEATURE_REDACTED_DATASETS)); 1624 err = SET_ERROR(EIO); 1625 goto early_unlock; 1626 } 1627 1628 SET_BOOKMARK(&zb, dmu_objset_id(db->db_objset), 1629 db->db.db_object, db->db_level, db->db_blkid); 1630 1631 /* 1632 * All bps of an encrypted os should have the encryption bit set. 1633 * If this is not true it indicates tampering and we report an error. 1634 */ 1635 if (db->db_objset->os_encrypted && !BP_USES_CRYPT(bpp)) { 1636 spa_log_error(db->db_objset->os_spa, &zb, &bpp->blk_birth); 1637 zfs_panic_recover("unencrypted block in encrypted " 1638 "object set %llu", dmu_objset_id(db->db_objset)); 1639 err = SET_ERROR(EIO); 1640 goto early_unlock; 1641 } 1642 1643 err = dbuf_read_verify_dnode_crypt(db, flags); 1644 if (err != 0) 1645 goto early_unlock; 1646 1647 DB_DNODE_EXIT(db); 1648 1649 db->db_state = DB_READ; 1650 DTRACE_SET_STATE(db, "read issued"); 1651 mutex_exit(&db->db_mtx); 1652 1653 if (!DBUF_IS_CACHEABLE(db)) 1654 aflags |= ARC_FLAG_UNCACHED; 1655 else if (dbuf_is_l2cacheable(db)) 1656 aflags |= ARC_FLAG_L2CACHE; 1657 1658 dbuf_add_ref(db, NULL); 1659 1660 zio_flags = (flags & DB_RF_CANFAIL) ? 1661 ZIO_FLAG_CANFAIL : ZIO_FLAG_MUSTSUCCEED; 1662 1663 if ((flags & DB_RF_NO_DECRYPT) && BP_IS_PROTECTED(db->db_blkptr)) 1664 zio_flags |= ZIO_FLAG_RAW; 1665 /* 1666 * The zio layer will copy the provided blkptr later, but we have our 1667 * own copy so that we can release the parent's rwlock. We have to 1668 * do that so that if dbuf_read_done is called synchronously (on 1669 * an l1 cache hit) we don't acquire the db_mtx while holding the 1670 * parent's rwlock, which would be a lock ordering violation. 1671 */ 1672 dmu_buf_unlock_parent(db, dblt, tag); 1673 (void) arc_read(zio, db->db_objset->os_spa, bpp, 1674 dbuf_read_done, db, ZIO_PRIORITY_SYNC_READ, zio_flags, 1675 &aflags, &zb); 1676 return (err); 1677 early_unlock: 1678 DB_DNODE_EXIT(db); 1679 mutex_exit(&db->db_mtx); 1680 dmu_buf_unlock_parent(db, dblt, tag); 1681 return (err); 1682 } 1683 1684 /* 1685 * This is our just-in-time copy function. It makes a copy of buffers that 1686 * have been modified in a previous transaction group before we access them in 1687 * the current active group. 1688 * 1689 * This function is used in three places: when we are dirtying a buffer for the 1690 * first time in a txg, when we are freeing a range in a dnode that includes 1691 * this buffer, and when we are accessing a buffer which was received compressed 1692 * and later referenced in a WRITE_BYREF record. 1693 * 1694 * Note that when we are called from dbuf_free_range() we do not put a hold on 1695 * the buffer, we just traverse the active dbuf list for the dnode. 1696 */ 1697 static void 1698 dbuf_fix_old_data(dmu_buf_impl_t *db, uint64_t txg) 1699 { 1700 dbuf_dirty_record_t *dr = list_head(&db->db_dirty_records); 1701 1702 ASSERT(MUTEX_HELD(&db->db_mtx)); 1703 ASSERT(db->db.db_data != NULL); 1704 ASSERT(db->db_level == 0); 1705 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT); 1706 1707 if (dr == NULL || 1708 (dr->dt.dl.dr_data != 1709 ((db->db_blkid == DMU_BONUS_BLKID) ? db->db.db_data : db->db_buf))) 1710 return; 1711 1712 /* 1713 * If the last dirty record for this dbuf has not yet synced 1714 * and its referencing the dbuf data, either: 1715 * reset the reference to point to a new copy, 1716 * or (if there a no active holders) 1717 * just null out the current db_data pointer. 1718 */ 1719 ASSERT3U(dr->dr_txg, >=, txg - 2); 1720 if (db->db_blkid == DMU_BONUS_BLKID) { 1721 dnode_t *dn = DB_DNODE(db); 1722 int bonuslen = DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots); 1723 dr->dt.dl.dr_data = kmem_alloc(bonuslen, KM_SLEEP); 1724 arc_space_consume(bonuslen, ARC_SPACE_BONUS); 1725 memcpy(dr->dt.dl.dr_data, db->db.db_data, bonuslen); 1726 } else if (zfs_refcount_count(&db->db_holds) > db->db_dirtycnt) { 1727 dnode_t *dn = DB_DNODE(db); 1728 int size = arc_buf_size(db->db_buf); 1729 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db); 1730 spa_t *spa = db->db_objset->os_spa; 1731 enum zio_compress compress_type = 1732 arc_get_compression(db->db_buf); 1733 uint8_t complevel = arc_get_complevel(db->db_buf); 1734 1735 if (arc_is_encrypted(db->db_buf)) { 1736 boolean_t byteorder; 1737 uint8_t salt[ZIO_DATA_SALT_LEN]; 1738 uint8_t iv[ZIO_DATA_IV_LEN]; 1739 uint8_t mac[ZIO_DATA_MAC_LEN]; 1740 1741 arc_get_raw_params(db->db_buf, &byteorder, salt, 1742 iv, mac); 1743 dr->dt.dl.dr_data = arc_alloc_raw_buf(spa, db, 1744 dmu_objset_id(dn->dn_objset), byteorder, salt, iv, 1745 mac, dn->dn_type, size, arc_buf_lsize(db->db_buf), 1746 compress_type, complevel); 1747 } else if (compress_type != ZIO_COMPRESS_OFF) { 1748 ASSERT3U(type, ==, ARC_BUFC_DATA); 1749 dr->dt.dl.dr_data = arc_alloc_compressed_buf(spa, db, 1750 size, arc_buf_lsize(db->db_buf), compress_type, 1751 complevel); 1752 } else { 1753 dr->dt.dl.dr_data = arc_alloc_buf(spa, db, type, size); 1754 } 1755 memcpy(dr->dt.dl.dr_data->b_data, db->db.db_data, size); 1756 } else { 1757 db->db_buf = NULL; 1758 dbuf_clear_data(db); 1759 } 1760 } 1761 1762 int 1763 dbuf_read(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags) 1764 { 1765 int err = 0; 1766 boolean_t prefetch; 1767 dnode_t *dn; 1768 1769 /* 1770 * We don't have to hold the mutex to check db_state because it 1771 * can't be freed while we have a hold on the buffer. 1772 */ 1773 ASSERT(!zfs_refcount_is_zero(&db->db_holds)); 1774 1775 DB_DNODE_ENTER(db); 1776 dn = DB_DNODE(db); 1777 1778 prefetch = db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID && 1779 (flags & DB_RF_NOPREFETCH) == 0 && dn != NULL; 1780 1781 mutex_enter(&db->db_mtx); 1782 if (flags & DB_RF_PARTIAL_FIRST) 1783 db->db_partial_read = B_TRUE; 1784 else if (!(flags & DB_RF_PARTIAL_MORE)) 1785 db->db_partial_read = B_FALSE; 1786 if (db->db_state == DB_CACHED) { 1787 /* 1788 * Ensure that this block's dnode has been decrypted if 1789 * the caller has requested decrypted data. 1790 */ 1791 err = dbuf_read_verify_dnode_crypt(db, flags); 1792 1793 /* 1794 * If the arc buf is compressed or encrypted and the caller 1795 * requested uncompressed data, we need to untransform it 1796 * before returning. We also call arc_untransform() on any 1797 * unauthenticated blocks, which will verify their MAC if 1798 * the key is now available. 1799 */ 1800 if (err == 0 && db->db_buf != NULL && 1801 (flags & DB_RF_NO_DECRYPT) == 0 && 1802 (arc_is_encrypted(db->db_buf) || 1803 arc_is_unauthenticated(db->db_buf) || 1804 arc_get_compression(db->db_buf) != ZIO_COMPRESS_OFF)) { 1805 spa_t *spa = dn->dn_objset->os_spa; 1806 zbookmark_phys_t zb; 1807 1808 SET_BOOKMARK(&zb, dmu_objset_id(db->db_objset), 1809 db->db.db_object, db->db_level, db->db_blkid); 1810 dbuf_fix_old_data(db, spa_syncing_txg(spa)); 1811 err = arc_untransform(db->db_buf, spa, &zb, B_FALSE); 1812 dbuf_set_data(db, db->db_buf); 1813 } 1814 mutex_exit(&db->db_mtx); 1815 if (err == 0 && prefetch) { 1816 dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1, B_TRUE, 1817 B_FALSE, flags & DB_RF_HAVESTRUCT); 1818 } 1819 DB_DNODE_EXIT(db); 1820 DBUF_STAT_BUMP(hash_hits); 1821 } else if (db->db_state == DB_UNCACHED || db->db_state == DB_NOFILL) { 1822 boolean_t need_wait = B_FALSE; 1823 1824 db_lock_type_t dblt = dmu_buf_lock_parent(db, RW_READER, FTAG); 1825 1826 if (zio == NULL && (db->db_state == DB_NOFILL || 1827 (db->db_blkptr != NULL && !BP_IS_HOLE(db->db_blkptr)))) { 1828 spa_t *spa = dn->dn_objset->os_spa; 1829 zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL); 1830 need_wait = B_TRUE; 1831 } 1832 err = dbuf_read_impl(db, zio, flags, dblt, FTAG); 1833 /* 1834 * dbuf_read_impl has dropped db_mtx and our parent's rwlock 1835 * for us 1836 */ 1837 if (!err && prefetch) { 1838 dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1, B_TRUE, 1839 db->db_state != DB_CACHED, 1840 flags & DB_RF_HAVESTRUCT); 1841 } 1842 1843 DB_DNODE_EXIT(db); 1844 DBUF_STAT_BUMP(hash_misses); 1845 1846 /* 1847 * If we created a zio_root we must execute it to avoid 1848 * leaking it, even if it isn't attached to any work due 1849 * to an error in dbuf_read_impl(). 1850 */ 1851 if (need_wait) { 1852 if (err == 0) 1853 err = zio_wait(zio); 1854 else 1855 VERIFY0(zio_wait(zio)); 1856 } 1857 } else { 1858 /* 1859 * Another reader came in while the dbuf was in flight 1860 * between UNCACHED and CACHED. Either a writer will finish 1861 * writing the buffer (sending the dbuf to CACHED) or the 1862 * first reader's request will reach the read_done callback 1863 * and send the dbuf to CACHED. Otherwise, a failure 1864 * occurred and the dbuf went to UNCACHED. 1865 */ 1866 mutex_exit(&db->db_mtx); 1867 if (prefetch) { 1868 dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1, B_TRUE, 1869 B_TRUE, flags & DB_RF_HAVESTRUCT); 1870 } 1871 DB_DNODE_EXIT(db); 1872 DBUF_STAT_BUMP(hash_misses); 1873 1874 /* Skip the wait per the caller's request. */ 1875 if ((flags & DB_RF_NEVERWAIT) == 0) { 1876 mutex_enter(&db->db_mtx); 1877 while (db->db_state == DB_READ || 1878 db->db_state == DB_FILL) { 1879 ASSERT(db->db_state == DB_READ || 1880 (flags & DB_RF_HAVESTRUCT) == 0); 1881 DTRACE_PROBE2(blocked__read, dmu_buf_impl_t *, 1882 db, zio_t *, zio); 1883 cv_wait(&db->db_changed, &db->db_mtx); 1884 } 1885 if (db->db_state == DB_UNCACHED) 1886 err = SET_ERROR(EIO); 1887 mutex_exit(&db->db_mtx); 1888 } 1889 } 1890 1891 return (err); 1892 } 1893 1894 static void 1895 dbuf_noread(dmu_buf_impl_t *db) 1896 { 1897 ASSERT(!zfs_refcount_is_zero(&db->db_holds)); 1898 ASSERT(db->db_blkid != DMU_BONUS_BLKID); 1899 mutex_enter(&db->db_mtx); 1900 while (db->db_state == DB_READ || db->db_state == DB_FILL) 1901 cv_wait(&db->db_changed, &db->db_mtx); 1902 if (db->db_state == DB_UNCACHED) { 1903 ASSERT(db->db_buf == NULL); 1904 ASSERT(db->db.db_data == NULL); 1905 dbuf_set_data(db, dbuf_alloc_arcbuf(db)); 1906 db->db_state = DB_FILL; 1907 DTRACE_SET_STATE(db, "assigning filled buffer"); 1908 } else if (db->db_state == DB_NOFILL) { 1909 dbuf_clear_data(db); 1910 } else { 1911 ASSERT3U(db->db_state, ==, DB_CACHED); 1912 } 1913 mutex_exit(&db->db_mtx); 1914 } 1915 1916 void 1917 dbuf_unoverride(dbuf_dirty_record_t *dr) 1918 { 1919 dmu_buf_impl_t *db = dr->dr_dbuf; 1920 blkptr_t *bp = &dr->dt.dl.dr_overridden_by; 1921 uint64_t txg = dr->dr_txg; 1922 1923 ASSERT(MUTEX_HELD(&db->db_mtx)); 1924 /* 1925 * This assert is valid because dmu_sync() expects to be called by 1926 * a zilog's get_data while holding a range lock. This call only 1927 * comes from dbuf_dirty() callers who must also hold a range lock. 1928 */ 1929 ASSERT(dr->dt.dl.dr_override_state != DR_IN_DMU_SYNC); 1930 ASSERT(db->db_level == 0); 1931 1932 if (db->db_blkid == DMU_BONUS_BLKID || 1933 dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN) 1934 return; 1935 1936 ASSERT(db->db_data_pending != dr); 1937 1938 /* free this block */ 1939 if (!BP_IS_HOLE(bp) && !dr->dt.dl.dr_nopwrite) 1940 zio_free(db->db_objset->os_spa, txg, bp); 1941 1942 if (dr->dt.dl.dr_brtwrite) { 1943 ASSERT0P(dr->dt.dl.dr_data); 1944 dr->dt.dl.dr_data = db->db_buf; 1945 } 1946 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN; 1947 dr->dt.dl.dr_nopwrite = B_FALSE; 1948 dr->dt.dl.dr_brtwrite = B_FALSE; 1949 dr->dt.dl.dr_has_raw_params = B_FALSE; 1950 1951 /* 1952 * Release the already-written buffer, so we leave it in 1953 * a consistent dirty state. Note that all callers are 1954 * modifying the buffer, so they will immediately do 1955 * another (redundant) arc_release(). Therefore, leave 1956 * the buf thawed to save the effort of freezing & 1957 * immediately re-thawing it. 1958 */ 1959 if (dr->dt.dl.dr_data) 1960 arc_release(dr->dt.dl.dr_data, db); 1961 } 1962 1963 /* 1964 * Evict (if its unreferenced) or clear (if its referenced) any level-0 1965 * data blocks in the free range, so that any future readers will find 1966 * empty blocks. 1967 */ 1968 void 1969 dbuf_free_range(dnode_t *dn, uint64_t start_blkid, uint64_t end_blkid, 1970 dmu_tx_t *tx) 1971 { 1972 dmu_buf_impl_t *db_search; 1973 dmu_buf_impl_t *db, *db_next; 1974 uint64_t txg = tx->tx_txg; 1975 avl_index_t where; 1976 dbuf_dirty_record_t *dr; 1977 1978 if (end_blkid > dn->dn_maxblkid && 1979 !(start_blkid == DMU_SPILL_BLKID || end_blkid == DMU_SPILL_BLKID)) 1980 end_blkid = dn->dn_maxblkid; 1981 dprintf_dnode(dn, "start=%llu end=%llu\n", (u_longlong_t)start_blkid, 1982 (u_longlong_t)end_blkid); 1983 1984 db_search = kmem_alloc(sizeof (dmu_buf_impl_t), KM_SLEEP); 1985 db_search->db_level = 0; 1986 db_search->db_blkid = start_blkid; 1987 db_search->db_state = DB_SEARCH; 1988 1989 mutex_enter(&dn->dn_dbufs_mtx); 1990 db = avl_find(&dn->dn_dbufs, db_search, &where); 1991 ASSERT3P(db, ==, NULL); 1992 1993 db = avl_nearest(&dn->dn_dbufs, where, AVL_AFTER); 1994 1995 for (; db != NULL; db = db_next) { 1996 db_next = AVL_NEXT(&dn->dn_dbufs, db); 1997 ASSERT(db->db_blkid != DMU_BONUS_BLKID); 1998 1999 if (db->db_level != 0 || db->db_blkid > end_blkid) { 2000 break; 2001 } 2002 ASSERT3U(db->db_blkid, >=, start_blkid); 2003 2004 /* found a level 0 buffer in the range */ 2005 mutex_enter(&db->db_mtx); 2006 if (dbuf_undirty(db, tx)) { 2007 /* mutex has been dropped and dbuf destroyed */ 2008 continue; 2009 } 2010 2011 if (db->db_state == DB_UNCACHED || 2012 db->db_state == DB_NOFILL || 2013 db->db_state == DB_EVICTING) { 2014 ASSERT(db->db.db_data == NULL); 2015 mutex_exit(&db->db_mtx); 2016 continue; 2017 } 2018 if (db->db_state == DB_READ || db->db_state == DB_FILL) { 2019 /* will be handled in dbuf_read_done or dbuf_rele */ 2020 db->db_freed_in_flight = TRUE; 2021 mutex_exit(&db->db_mtx); 2022 continue; 2023 } 2024 if (zfs_refcount_count(&db->db_holds) == 0) { 2025 ASSERT(db->db_buf); 2026 dbuf_destroy(db); 2027 continue; 2028 } 2029 /* The dbuf is referenced */ 2030 2031 dr = list_head(&db->db_dirty_records); 2032 if (dr != NULL) { 2033 if (dr->dr_txg == txg) { 2034 /* 2035 * This buffer is "in-use", re-adjust the file 2036 * size to reflect that this buffer may 2037 * contain new data when we sync. 2038 */ 2039 if (db->db_blkid != DMU_SPILL_BLKID && 2040 db->db_blkid > dn->dn_maxblkid) 2041 dn->dn_maxblkid = db->db_blkid; 2042 dbuf_unoverride(dr); 2043 } else { 2044 /* 2045 * This dbuf is not dirty in the open context. 2046 * Either uncache it (if its not referenced in 2047 * the open context) or reset its contents to 2048 * empty. 2049 */ 2050 dbuf_fix_old_data(db, txg); 2051 } 2052 } 2053 /* clear the contents if its cached */ 2054 if (db->db_state == DB_CACHED) { 2055 ASSERT(db->db.db_data != NULL); 2056 arc_release(db->db_buf, db); 2057 rw_enter(&db->db_rwlock, RW_WRITER); 2058 memset(db->db.db_data, 0, db->db.db_size); 2059 rw_exit(&db->db_rwlock); 2060 arc_buf_freeze(db->db_buf); 2061 } 2062 2063 mutex_exit(&db->db_mtx); 2064 } 2065 2066 mutex_exit(&dn->dn_dbufs_mtx); 2067 kmem_free(db_search, sizeof (dmu_buf_impl_t)); 2068 } 2069 2070 void 2071 dbuf_new_size(dmu_buf_impl_t *db, int size, dmu_tx_t *tx) 2072 { 2073 arc_buf_t *buf, *old_buf; 2074 dbuf_dirty_record_t *dr; 2075 int osize = db->db.db_size; 2076 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db); 2077 dnode_t *dn; 2078 2079 ASSERT(db->db_blkid != DMU_BONUS_BLKID); 2080 2081 DB_DNODE_ENTER(db); 2082 dn = DB_DNODE(db); 2083 2084 /* 2085 * XXX we should be doing a dbuf_read, checking the return 2086 * value and returning that up to our callers 2087 */ 2088 dmu_buf_will_dirty(&db->db, tx); 2089 2090 /* create the data buffer for the new block */ 2091 buf = arc_alloc_buf(dn->dn_objset->os_spa, db, type, size); 2092 2093 /* copy old block data to the new block */ 2094 old_buf = db->db_buf; 2095 memcpy(buf->b_data, old_buf->b_data, MIN(osize, size)); 2096 /* zero the remainder */ 2097 if (size > osize) 2098 memset((uint8_t *)buf->b_data + osize, 0, size - osize); 2099 2100 mutex_enter(&db->db_mtx); 2101 dbuf_set_data(db, buf); 2102 arc_buf_destroy(old_buf, db); 2103 db->db.db_size = size; 2104 2105 dr = list_head(&db->db_dirty_records); 2106 /* dirty record added by dmu_buf_will_dirty() */ 2107 VERIFY(dr != NULL); 2108 if (db->db_level == 0) 2109 dr->dt.dl.dr_data = buf; 2110 ASSERT3U(dr->dr_txg, ==, tx->tx_txg); 2111 ASSERT3U(dr->dr_accounted, ==, osize); 2112 dr->dr_accounted = size; 2113 mutex_exit(&db->db_mtx); 2114 2115 dmu_objset_willuse_space(dn->dn_objset, size - osize, tx); 2116 DB_DNODE_EXIT(db); 2117 } 2118 2119 void 2120 dbuf_release_bp(dmu_buf_impl_t *db) 2121 { 2122 objset_t *os __maybe_unused = db->db_objset; 2123 2124 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os))); 2125 ASSERT(arc_released(os->os_phys_buf) || 2126 list_link_active(&os->os_dsl_dataset->ds_synced_link)); 2127 ASSERT(db->db_parent == NULL || arc_released(db->db_parent->db_buf)); 2128 2129 (void) arc_release(db->db_buf, db); 2130 } 2131 2132 /* 2133 * We already have a dirty record for this TXG, and we are being 2134 * dirtied again. 2135 */ 2136 static void 2137 dbuf_redirty(dbuf_dirty_record_t *dr) 2138 { 2139 dmu_buf_impl_t *db = dr->dr_dbuf; 2140 2141 ASSERT(MUTEX_HELD(&db->db_mtx)); 2142 2143 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID) { 2144 /* 2145 * If this buffer has already been written out, 2146 * we now need to reset its state. 2147 */ 2148 dbuf_unoverride(dr); 2149 if (db->db.db_object != DMU_META_DNODE_OBJECT && 2150 db->db_state != DB_NOFILL) { 2151 /* Already released on initial dirty, so just thaw. */ 2152 ASSERT(arc_released(db->db_buf)); 2153 arc_buf_thaw(db->db_buf); 2154 } 2155 } 2156 } 2157 2158 dbuf_dirty_record_t * 2159 dbuf_dirty_lightweight(dnode_t *dn, uint64_t blkid, dmu_tx_t *tx) 2160 { 2161 rw_enter(&dn->dn_struct_rwlock, RW_READER); 2162 IMPLY(dn->dn_objset->os_raw_receive, dn->dn_maxblkid >= blkid); 2163 dnode_new_blkid(dn, blkid, tx, B_TRUE, B_FALSE); 2164 ASSERT(dn->dn_maxblkid >= blkid); 2165 2166 dbuf_dirty_record_t *dr = kmem_zalloc(sizeof (*dr), KM_SLEEP); 2167 list_link_init(&dr->dr_dirty_node); 2168 list_link_init(&dr->dr_dbuf_node); 2169 dr->dr_dnode = dn; 2170 dr->dr_txg = tx->tx_txg; 2171 dr->dt.dll.dr_blkid = blkid; 2172 dr->dr_accounted = dn->dn_datablksz; 2173 2174 /* 2175 * There should not be any dbuf for the block that we're dirtying. 2176 * Otherwise the buffer contents could be inconsistent between the 2177 * dbuf and the lightweight dirty record. 2178 */ 2179 ASSERT3P(NULL, ==, dbuf_find(dn->dn_objset, dn->dn_object, 0, blkid, 2180 NULL)); 2181 2182 mutex_enter(&dn->dn_mtx); 2183 int txgoff = tx->tx_txg & TXG_MASK; 2184 if (dn->dn_free_ranges[txgoff] != NULL) { 2185 range_tree_clear(dn->dn_free_ranges[txgoff], blkid, 1); 2186 } 2187 2188 if (dn->dn_nlevels == 1) { 2189 ASSERT3U(blkid, <, dn->dn_nblkptr); 2190 list_insert_tail(&dn->dn_dirty_records[txgoff], dr); 2191 mutex_exit(&dn->dn_mtx); 2192 rw_exit(&dn->dn_struct_rwlock); 2193 dnode_setdirty(dn, tx); 2194 } else { 2195 mutex_exit(&dn->dn_mtx); 2196 2197 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 2198 dmu_buf_impl_t *parent_db = dbuf_hold_level(dn, 2199 1, blkid >> epbs, FTAG); 2200 rw_exit(&dn->dn_struct_rwlock); 2201 if (parent_db == NULL) { 2202 kmem_free(dr, sizeof (*dr)); 2203 return (NULL); 2204 } 2205 int err = dbuf_read(parent_db, NULL, 2206 (DB_RF_NOPREFETCH | DB_RF_CANFAIL)); 2207 if (err != 0) { 2208 dbuf_rele(parent_db, FTAG); 2209 kmem_free(dr, sizeof (*dr)); 2210 return (NULL); 2211 } 2212 2213 dbuf_dirty_record_t *parent_dr = dbuf_dirty(parent_db, tx); 2214 dbuf_rele(parent_db, FTAG); 2215 mutex_enter(&parent_dr->dt.di.dr_mtx); 2216 ASSERT3U(parent_dr->dr_txg, ==, tx->tx_txg); 2217 list_insert_tail(&parent_dr->dt.di.dr_children, dr); 2218 mutex_exit(&parent_dr->dt.di.dr_mtx); 2219 dr->dr_parent = parent_dr; 2220 } 2221 2222 dmu_objset_willuse_space(dn->dn_objset, dr->dr_accounted, tx); 2223 2224 return (dr); 2225 } 2226 2227 dbuf_dirty_record_t * 2228 dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx) 2229 { 2230 dnode_t *dn; 2231 objset_t *os; 2232 dbuf_dirty_record_t *dr, *dr_next, *dr_head; 2233 int txgoff = tx->tx_txg & TXG_MASK; 2234 boolean_t drop_struct_rwlock = B_FALSE; 2235 2236 ASSERT(tx->tx_txg != 0); 2237 ASSERT(!zfs_refcount_is_zero(&db->db_holds)); 2238 DMU_TX_DIRTY_BUF(tx, db); 2239 2240 DB_DNODE_ENTER(db); 2241 dn = DB_DNODE(db); 2242 /* 2243 * Shouldn't dirty a regular buffer in syncing context. Private 2244 * objects may be dirtied in syncing context, but only if they 2245 * were already pre-dirtied in open context. 2246 */ 2247 #ifdef ZFS_DEBUG 2248 if (dn->dn_objset->os_dsl_dataset != NULL) { 2249 rrw_enter(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock, 2250 RW_READER, FTAG); 2251 } 2252 ASSERT(!dmu_tx_is_syncing(tx) || 2253 BP_IS_HOLE(dn->dn_objset->os_rootbp) || 2254 DMU_OBJECT_IS_SPECIAL(dn->dn_object) || 2255 dn->dn_objset->os_dsl_dataset == NULL); 2256 if (dn->dn_objset->os_dsl_dataset != NULL) 2257 rrw_exit(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock, FTAG); 2258 #endif 2259 /* 2260 * We make this assert for private objects as well, but after we 2261 * check if we're already dirty. They are allowed to re-dirty 2262 * in syncing context. 2263 */ 2264 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT || 2265 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx == 2266 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN)); 2267 2268 mutex_enter(&db->db_mtx); 2269 /* 2270 * XXX make this true for indirects too? The problem is that 2271 * transactions created with dmu_tx_create_assigned() from 2272 * syncing context don't bother holding ahead. 2273 */ 2274 ASSERT(db->db_level != 0 || 2275 db->db_state == DB_CACHED || db->db_state == DB_FILL || 2276 db->db_state == DB_NOFILL); 2277 2278 mutex_enter(&dn->dn_mtx); 2279 dnode_set_dirtyctx(dn, tx, db); 2280 if (tx->tx_txg > dn->dn_dirty_txg) 2281 dn->dn_dirty_txg = tx->tx_txg; 2282 mutex_exit(&dn->dn_mtx); 2283 2284 if (db->db_blkid == DMU_SPILL_BLKID) 2285 dn->dn_have_spill = B_TRUE; 2286 2287 /* 2288 * If this buffer is already dirty, we're done. 2289 */ 2290 dr_head = list_head(&db->db_dirty_records); 2291 ASSERT(dr_head == NULL || dr_head->dr_txg <= tx->tx_txg || 2292 db->db.db_object == DMU_META_DNODE_OBJECT); 2293 dr_next = dbuf_find_dirty_lte(db, tx->tx_txg); 2294 if (dr_next && dr_next->dr_txg == tx->tx_txg) { 2295 DB_DNODE_EXIT(db); 2296 2297 dbuf_redirty(dr_next); 2298 mutex_exit(&db->db_mtx); 2299 return (dr_next); 2300 } 2301 2302 /* 2303 * Only valid if not already dirty. 2304 */ 2305 ASSERT(dn->dn_object == 0 || 2306 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx == 2307 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN)); 2308 2309 ASSERT3U(dn->dn_nlevels, >, db->db_level); 2310 2311 /* 2312 * We should only be dirtying in syncing context if it's the 2313 * mos or we're initializing the os or it's a special object. 2314 * However, we are allowed to dirty in syncing context provided 2315 * we already dirtied it in open context. Hence we must make 2316 * this assertion only if we're not already dirty. 2317 */ 2318 os = dn->dn_objset; 2319 VERIFY3U(tx->tx_txg, <=, spa_final_dirty_txg(os->os_spa)); 2320 #ifdef ZFS_DEBUG 2321 if (dn->dn_objset->os_dsl_dataset != NULL) 2322 rrw_enter(&os->os_dsl_dataset->ds_bp_rwlock, RW_READER, FTAG); 2323 ASSERT(!dmu_tx_is_syncing(tx) || DMU_OBJECT_IS_SPECIAL(dn->dn_object) || 2324 os->os_dsl_dataset == NULL || BP_IS_HOLE(os->os_rootbp)); 2325 if (dn->dn_objset->os_dsl_dataset != NULL) 2326 rrw_exit(&os->os_dsl_dataset->ds_bp_rwlock, FTAG); 2327 #endif 2328 ASSERT(db->db.db_size != 0); 2329 2330 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size); 2331 2332 if (db->db_blkid != DMU_BONUS_BLKID && db->db_state != DB_NOFILL) { 2333 dmu_objset_willuse_space(os, db->db.db_size, tx); 2334 } 2335 2336 /* 2337 * If this buffer is dirty in an old transaction group we need 2338 * to make a copy of it so that the changes we make in this 2339 * transaction group won't leak out when we sync the older txg. 2340 */ 2341 dr = kmem_zalloc(sizeof (dbuf_dirty_record_t), KM_SLEEP); 2342 list_link_init(&dr->dr_dirty_node); 2343 list_link_init(&dr->dr_dbuf_node); 2344 dr->dr_dnode = dn; 2345 if (db->db_level == 0) { 2346 void *data_old = db->db_buf; 2347 2348 if (db->db_state != DB_NOFILL) { 2349 if (db->db_blkid == DMU_BONUS_BLKID) { 2350 dbuf_fix_old_data(db, tx->tx_txg); 2351 data_old = db->db.db_data; 2352 } else if (db->db.db_object != DMU_META_DNODE_OBJECT) { 2353 /* 2354 * Release the data buffer from the cache so 2355 * that we can modify it without impacting 2356 * possible other users of this cached data 2357 * block. Note that indirect blocks and 2358 * private objects are not released until the 2359 * syncing state (since they are only modified 2360 * then). 2361 */ 2362 arc_release(db->db_buf, db); 2363 dbuf_fix_old_data(db, tx->tx_txg); 2364 data_old = db->db_buf; 2365 } 2366 ASSERT(data_old != NULL); 2367 } 2368 dr->dt.dl.dr_data = data_old; 2369 } else { 2370 mutex_init(&dr->dt.di.dr_mtx, NULL, MUTEX_NOLOCKDEP, NULL); 2371 list_create(&dr->dt.di.dr_children, 2372 sizeof (dbuf_dirty_record_t), 2373 offsetof(dbuf_dirty_record_t, dr_dirty_node)); 2374 } 2375 if (db->db_blkid != DMU_BONUS_BLKID && db->db_state != DB_NOFILL) { 2376 dr->dr_accounted = db->db.db_size; 2377 } 2378 dr->dr_dbuf = db; 2379 dr->dr_txg = tx->tx_txg; 2380 list_insert_before(&db->db_dirty_records, dr_next, dr); 2381 2382 /* 2383 * We could have been freed_in_flight between the dbuf_noread 2384 * and dbuf_dirty. We win, as though the dbuf_noread() had 2385 * happened after the free. 2386 */ 2387 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID && 2388 db->db_blkid != DMU_SPILL_BLKID) { 2389 mutex_enter(&dn->dn_mtx); 2390 if (dn->dn_free_ranges[txgoff] != NULL) { 2391 range_tree_clear(dn->dn_free_ranges[txgoff], 2392 db->db_blkid, 1); 2393 } 2394 mutex_exit(&dn->dn_mtx); 2395 db->db_freed_in_flight = FALSE; 2396 } 2397 2398 /* 2399 * This buffer is now part of this txg 2400 */ 2401 dbuf_add_ref(db, (void *)(uintptr_t)tx->tx_txg); 2402 db->db_dirtycnt += 1; 2403 ASSERT3U(db->db_dirtycnt, <=, 3); 2404 2405 mutex_exit(&db->db_mtx); 2406 2407 if (db->db_blkid == DMU_BONUS_BLKID || 2408 db->db_blkid == DMU_SPILL_BLKID) { 2409 mutex_enter(&dn->dn_mtx); 2410 ASSERT(!list_link_active(&dr->dr_dirty_node)); 2411 list_insert_tail(&dn->dn_dirty_records[txgoff], dr); 2412 mutex_exit(&dn->dn_mtx); 2413 dnode_setdirty(dn, tx); 2414 DB_DNODE_EXIT(db); 2415 return (dr); 2416 } 2417 2418 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) { 2419 rw_enter(&dn->dn_struct_rwlock, RW_READER); 2420 drop_struct_rwlock = B_TRUE; 2421 } 2422 2423 /* 2424 * If we are overwriting a dedup BP, then unless it is snapshotted, 2425 * when we get to syncing context we will need to decrement its 2426 * refcount in the DDT. Prefetch the relevant DDT block so that 2427 * syncing context won't have to wait for the i/o. 2428 */ 2429 if (db->db_blkptr != NULL) { 2430 db_lock_type_t dblt = dmu_buf_lock_parent(db, RW_READER, FTAG); 2431 ddt_prefetch(os->os_spa, db->db_blkptr); 2432 dmu_buf_unlock_parent(db, dblt, FTAG); 2433 } 2434 2435 /* 2436 * We need to hold the dn_struct_rwlock to make this assertion, 2437 * because it protects dn_phys / dn_next_nlevels from changing. 2438 */ 2439 ASSERT((dn->dn_phys->dn_nlevels == 0 && db->db_level == 0) || 2440 dn->dn_phys->dn_nlevels > db->db_level || 2441 dn->dn_next_nlevels[txgoff] > db->db_level || 2442 dn->dn_next_nlevels[(tx->tx_txg-1) & TXG_MASK] > db->db_level || 2443 dn->dn_next_nlevels[(tx->tx_txg-2) & TXG_MASK] > db->db_level); 2444 2445 2446 if (db->db_level == 0) { 2447 ASSERT(!db->db_objset->os_raw_receive || 2448 dn->dn_maxblkid >= db->db_blkid); 2449 dnode_new_blkid(dn, db->db_blkid, tx, 2450 drop_struct_rwlock, B_FALSE); 2451 ASSERT(dn->dn_maxblkid >= db->db_blkid); 2452 } 2453 2454 if (db->db_level+1 < dn->dn_nlevels) { 2455 dmu_buf_impl_t *parent = db->db_parent; 2456 dbuf_dirty_record_t *di; 2457 int parent_held = FALSE; 2458 2459 if (db->db_parent == NULL || db->db_parent == dn->dn_dbuf) { 2460 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 2461 parent = dbuf_hold_level(dn, db->db_level + 1, 2462 db->db_blkid >> epbs, FTAG); 2463 ASSERT(parent != NULL); 2464 parent_held = TRUE; 2465 } 2466 if (drop_struct_rwlock) 2467 rw_exit(&dn->dn_struct_rwlock); 2468 ASSERT3U(db->db_level + 1, ==, parent->db_level); 2469 di = dbuf_dirty(parent, tx); 2470 if (parent_held) 2471 dbuf_rele(parent, FTAG); 2472 2473 mutex_enter(&db->db_mtx); 2474 /* 2475 * Since we've dropped the mutex, it's possible that 2476 * dbuf_undirty() might have changed this out from under us. 2477 */ 2478 if (list_head(&db->db_dirty_records) == dr || 2479 dn->dn_object == DMU_META_DNODE_OBJECT) { 2480 mutex_enter(&di->dt.di.dr_mtx); 2481 ASSERT3U(di->dr_txg, ==, tx->tx_txg); 2482 ASSERT(!list_link_active(&dr->dr_dirty_node)); 2483 list_insert_tail(&di->dt.di.dr_children, dr); 2484 mutex_exit(&di->dt.di.dr_mtx); 2485 dr->dr_parent = di; 2486 } 2487 mutex_exit(&db->db_mtx); 2488 } else { 2489 ASSERT(db->db_level + 1 == dn->dn_nlevels); 2490 ASSERT(db->db_blkid < dn->dn_nblkptr); 2491 ASSERT(db->db_parent == NULL || db->db_parent == dn->dn_dbuf); 2492 mutex_enter(&dn->dn_mtx); 2493 ASSERT(!list_link_active(&dr->dr_dirty_node)); 2494 list_insert_tail(&dn->dn_dirty_records[txgoff], dr); 2495 mutex_exit(&dn->dn_mtx); 2496 if (drop_struct_rwlock) 2497 rw_exit(&dn->dn_struct_rwlock); 2498 } 2499 2500 dnode_setdirty(dn, tx); 2501 DB_DNODE_EXIT(db); 2502 return (dr); 2503 } 2504 2505 static void 2506 dbuf_undirty_bonus(dbuf_dirty_record_t *dr) 2507 { 2508 dmu_buf_impl_t *db = dr->dr_dbuf; 2509 2510 if (dr->dt.dl.dr_data != db->db.db_data) { 2511 struct dnode *dn = dr->dr_dnode; 2512 int max_bonuslen = DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots); 2513 2514 kmem_free(dr->dt.dl.dr_data, max_bonuslen); 2515 arc_space_return(max_bonuslen, ARC_SPACE_BONUS); 2516 } 2517 db->db_data_pending = NULL; 2518 ASSERT(list_next(&db->db_dirty_records, dr) == NULL); 2519 list_remove(&db->db_dirty_records, dr); 2520 if (dr->dr_dbuf->db_level != 0) { 2521 mutex_destroy(&dr->dt.di.dr_mtx); 2522 list_destroy(&dr->dt.di.dr_children); 2523 } 2524 kmem_free(dr, sizeof (dbuf_dirty_record_t)); 2525 ASSERT3U(db->db_dirtycnt, >, 0); 2526 db->db_dirtycnt -= 1; 2527 } 2528 2529 /* 2530 * Undirty a buffer in the transaction group referenced by the given 2531 * transaction. Return whether this evicted the dbuf. 2532 */ 2533 boolean_t 2534 dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx) 2535 { 2536 uint64_t txg = tx->tx_txg; 2537 boolean_t brtwrite; 2538 2539 ASSERT(txg != 0); 2540 2541 /* 2542 * Due to our use of dn_nlevels below, this can only be called 2543 * in open context, unless we are operating on the MOS. 2544 * From syncing context, dn_nlevels may be different from the 2545 * dn_nlevels used when dbuf was dirtied. 2546 */ 2547 ASSERT(db->db_objset == 2548 dmu_objset_pool(db->db_objset)->dp_meta_objset || 2549 txg != spa_syncing_txg(dmu_objset_spa(db->db_objset))); 2550 ASSERT(db->db_blkid != DMU_BONUS_BLKID); 2551 ASSERT0(db->db_level); 2552 ASSERT(MUTEX_HELD(&db->db_mtx)); 2553 2554 /* 2555 * If this buffer is not dirty, we're done. 2556 */ 2557 dbuf_dirty_record_t *dr = dbuf_find_dirty_eq(db, txg); 2558 if (dr == NULL) 2559 return (B_FALSE); 2560 ASSERT(dr->dr_dbuf == db); 2561 2562 brtwrite = dr->dt.dl.dr_brtwrite; 2563 if (brtwrite) { 2564 /* 2565 * We are freeing a block that we cloned in the same 2566 * transaction group. 2567 */ 2568 brt_pending_remove(dmu_objset_spa(db->db_objset), 2569 &dr->dt.dl.dr_overridden_by, tx); 2570 } 2571 2572 dnode_t *dn = dr->dr_dnode; 2573 2574 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size); 2575 2576 ASSERT(db->db.db_size != 0); 2577 2578 dsl_pool_undirty_space(dmu_objset_pool(dn->dn_objset), 2579 dr->dr_accounted, txg); 2580 2581 list_remove(&db->db_dirty_records, dr); 2582 2583 /* 2584 * Note that there are three places in dbuf_dirty() 2585 * where this dirty record may be put on a list. 2586 * Make sure to do a list_remove corresponding to 2587 * every one of those list_insert calls. 2588 */ 2589 if (dr->dr_parent) { 2590 mutex_enter(&dr->dr_parent->dt.di.dr_mtx); 2591 list_remove(&dr->dr_parent->dt.di.dr_children, dr); 2592 mutex_exit(&dr->dr_parent->dt.di.dr_mtx); 2593 } else if (db->db_blkid == DMU_SPILL_BLKID || 2594 db->db_level + 1 == dn->dn_nlevels) { 2595 ASSERT(db->db_blkptr == NULL || db->db_parent == dn->dn_dbuf); 2596 mutex_enter(&dn->dn_mtx); 2597 list_remove(&dn->dn_dirty_records[txg & TXG_MASK], dr); 2598 mutex_exit(&dn->dn_mtx); 2599 } 2600 2601 if (db->db_state != DB_NOFILL && !brtwrite) { 2602 dbuf_unoverride(dr); 2603 2604 ASSERT(db->db_buf != NULL); 2605 ASSERT(dr->dt.dl.dr_data != NULL); 2606 if (dr->dt.dl.dr_data != db->db_buf) 2607 arc_buf_destroy(dr->dt.dl.dr_data, db); 2608 } 2609 2610 kmem_free(dr, sizeof (dbuf_dirty_record_t)); 2611 2612 ASSERT(db->db_dirtycnt > 0); 2613 db->db_dirtycnt -= 1; 2614 2615 if (zfs_refcount_remove(&db->db_holds, (void *)(uintptr_t)txg) == 0) { 2616 ASSERT(db->db_state == DB_NOFILL || brtwrite || 2617 arc_released(db->db_buf)); 2618 dbuf_destroy(db); 2619 return (B_TRUE); 2620 } 2621 2622 return (B_FALSE); 2623 } 2624 2625 static void 2626 dmu_buf_will_dirty_impl(dmu_buf_t *db_fake, int flags, dmu_tx_t *tx) 2627 { 2628 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 2629 boolean_t undirty = B_FALSE; 2630 2631 ASSERT(tx->tx_txg != 0); 2632 ASSERT(!zfs_refcount_is_zero(&db->db_holds)); 2633 2634 /* 2635 * Quick check for dirtiness. For already dirty blocks, this 2636 * reduces runtime of this function by >90%, and overall performance 2637 * by 50% for some workloads (e.g. file deletion with indirect blocks 2638 * cached). 2639 */ 2640 mutex_enter(&db->db_mtx); 2641 2642 if (db->db_state == DB_CACHED || db->db_state == DB_NOFILL) { 2643 dbuf_dirty_record_t *dr = dbuf_find_dirty_eq(db, tx->tx_txg); 2644 /* 2645 * It's possible that it is already dirty but not cached, 2646 * because there are some calls to dbuf_dirty() that don't 2647 * go through dmu_buf_will_dirty(). 2648 */ 2649 if (dr != NULL) { 2650 if (dr->dt.dl.dr_brtwrite) { 2651 /* 2652 * Block cloning: If we are dirtying a cloned 2653 * block, we cannot simply redirty it, because 2654 * this dr has no data associated with it. 2655 * We will go through a full undirtying below, 2656 * before dirtying it again. 2657 */ 2658 undirty = B_TRUE; 2659 } else { 2660 /* This dbuf is already dirty and cached. */ 2661 dbuf_redirty(dr); 2662 mutex_exit(&db->db_mtx); 2663 return; 2664 } 2665 } 2666 } 2667 mutex_exit(&db->db_mtx); 2668 2669 DB_DNODE_ENTER(db); 2670 if (RW_WRITE_HELD(&DB_DNODE(db)->dn_struct_rwlock)) 2671 flags |= DB_RF_HAVESTRUCT; 2672 DB_DNODE_EXIT(db); 2673 2674 /* 2675 * Block cloning: Do the dbuf_read() before undirtying the dbuf, as we 2676 * want to make sure dbuf_read() will read the pending cloned block and 2677 * not the uderlying block that is being replaced. dbuf_undirty() will 2678 * do dbuf_unoverride(), so we will end up with cloned block content, 2679 * without overridden BP. 2680 */ 2681 (void) dbuf_read(db, NULL, flags); 2682 if (undirty) { 2683 mutex_enter(&db->db_mtx); 2684 VERIFY(!dbuf_undirty(db, tx)); 2685 mutex_exit(&db->db_mtx); 2686 } 2687 (void) dbuf_dirty(db, tx); 2688 } 2689 2690 void 2691 dmu_buf_will_dirty(dmu_buf_t *db_fake, dmu_tx_t *tx) 2692 { 2693 dmu_buf_will_dirty_impl(db_fake, 2694 DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH, tx); 2695 } 2696 2697 boolean_t 2698 dmu_buf_is_dirty(dmu_buf_t *db_fake, dmu_tx_t *tx) 2699 { 2700 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 2701 dbuf_dirty_record_t *dr; 2702 2703 mutex_enter(&db->db_mtx); 2704 dr = dbuf_find_dirty_eq(db, tx->tx_txg); 2705 mutex_exit(&db->db_mtx); 2706 return (dr != NULL); 2707 } 2708 2709 void 2710 dmu_buf_will_clone(dmu_buf_t *db_fake, dmu_tx_t *tx) 2711 { 2712 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 2713 2714 /* 2715 * Block cloning: We are going to clone into this block, so undirty 2716 * modifications done to this block so far in this txg. This includes 2717 * writes and clones into this block. 2718 */ 2719 mutex_enter(&db->db_mtx); 2720 DBUF_VERIFY(db); 2721 VERIFY(!dbuf_undirty(db, tx)); 2722 ASSERT0P(dbuf_find_dirty_eq(db, tx->tx_txg)); 2723 if (db->db_buf != NULL) { 2724 arc_buf_destroy(db->db_buf, db); 2725 db->db_buf = NULL; 2726 dbuf_clear_data(db); 2727 } 2728 2729 db->db_state = DB_NOFILL; 2730 DTRACE_SET_STATE(db, "allocating NOFILL buffer for clone"); 2731 2732 DBUF_VERIFY(db); 2733 mutex_exit(&db->db_mtx); 2734 2735 dbuf_noread(db); 2736 (void) dbuf_dirty(db, tx); 2737 } 2738 2739 void 2740 dmu_buf_will_not_fill(dmu_buf_t *db_fake, dmu_tx_t *tx) 2741 { 2742 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 2743 2744 mutex_enter(&db->db_mtx); 2745 db->db_state = DB_NOFILL; 2746 DTRACE_SET_STATE(db, "allocating NOFILL buffer"); 2747 mutex_exit(&db->db_mtx); 2748 2749 dbuf_noread(db); 2750 (void) dbuf_dirty(db, tx); 2751 } 2752 2753 void 2754 dmu_buf_will_fill(dmu_buf_t *db_fake, dmu_tx_t *tx, boolean_t canfail) 2755 { 2756 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 2757 2758 ASSERT(db->db_blkid != DMU_BONUS_BLKID); 2759 ASSERT(tx->tx_txg != 0); 2760 ASSERT(db->db_level == 0); 2761 ASSERT(!zfs_refcount_is_zero(&db->db_holds)); 2762 2763 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT || 2764 dmu_tx_private_ok(tx)); 2765 2766 mutex_enter(&db->db_mtx); 2767 if (db->db_state == DB_NOFILL) { 2768 /* 2769 * Block cloning: We will be completely overwriting a block 2770 * cloned in this transaction group, so let's undirty the 2771 * pending clone and mark the block as uncached. This will be 2772 * as if the clone was never done. But if the fill can fail 2773 * we should have a way to return back to the cloned data. 2774 */ 2775 if (canfail && dbuf_find_dirty_eq(db, tx->tx_txg) != NULL) { 2776 mutex_exit(&db->db_mtx); 2777 dmu_buf_will_dirty(db_fake, tx); 2778 return; 2779 } 2780 VERIFY(!dbuf_undirty(db, tx)); 2781 db->db_state = DB_UNCACHED; 2782 } 2783 mutex_exit(&db->db_mtx); 2784 2785 dbuf_noread(db); 2786 (void) dbuf_dirty(db, tx); 2787 } 2788 2789 /* 2790 * This function is effectively the same as dmu_buf_will_dirty(), but 2791 * indicates the caller expects raw encrypted data in the db, and provides 2792 * the crypt params (byteorder, salt, iv, mac) which should be stored in the 2793 * blkptr_t when this dbuf is written. This is only used for blocks of 2794 * dnodes, during raw receive. 2795 */ 2796 void 2797 dmu_buf_set_crypt_params(dmu_buf_t *db_fake, boolean_t byteorder, 2798 const uint8_t *salt, const uint8_t *iv, const uint8_t *mac, dmu_tx_t *tx) 2799 { 2800 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 2801 dbuf_dirty_record_t *dr; 2802 2803 /* 2804 * dr_has_raw_params is only processed for blocks of dnodes 2805 * (see dbuf_sync_dnode_leaf_crypt()). 2806 */ 2807 ASSERT3U(db->db.db_object, ==, DMU_META_DNODE_OBJECT); 2808 ASSERT3U(db->db_level, ==, 0); 2809 ASSERT(db->db_objset->os_raw_receive); 2810 2811 dmu_buf_will_dirty_impl(db_fake, 2812 DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH | DB_RF_NO_DECRYPT, tx); 2813 2814 dr = dbuf_find_dirty_eq(db, tx->tx_txg); 2815 2816 ASSERT3P(dr, !=, NULL); 2817 2818 dr->dt.dl.dr_has_raw_params = B_TRUE; 2819 dr->dt.dl.dr_byteorder = byteorder; 2820 memcpy(dr->dt.dl.dr_salt, salt, ZIO_DATA_SALT_LEN); 2821 memcpy(dr->dt.dl.dr_iv, iv, ZIO_DATA_IV_LEN); 2822 memcpy(dr->dt.dl.dr_mac, mac, ZIO_DATA_MAC_LEN); 2823 } 2824 2825 static void 2826 dbuf_override_impl(dmu_buf_impl_t *db, const blkptr_t *bp, dmu_tx_t *tx) 2827 { 2828 struct dirty_leaf *dl; 2829 dbuf_dirty_record_t *dr; 2830 2831 dr = list_head(&db->db_dirty_records); 2832 ASSERT3P(dr, !=, NULL); 2833 ASSERT3U(dr->dr_txg, ==, tx->tx_txg); 2834 dl = &dr->dt.dl; 2835 dl->dr_overridden_by = *bp; 2836 dl->dr_override_state = DR_OVERRIDDEN; 2837 dl->dr_overridden_by.blk_birth = dr->dr_txg; 2838 } 2839 2840 boolean_t 2841 dmu_buf_fill_done(dmu_buf_t *dbuf, dmu_tx_t *tx, boolean_t failed) 2842 { 2843 (void) tx; 2844 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf; 2845 mutex_enter(&db->db_mtx); 2846 DBUF_VERIFY(db); 2847 2848 if (db->db_state == DB_FILL) { 2849 if (db->db_level == 0 && db->db_freed_in_flight) { 2850 ASSERT(db->db_blkid != DMU_BONUS_BLKID); 2851 /* we were freed while filling */ 2852 /* XXX dbuf_undirty? */ 2853 memset(db->db.db_data, 0, db->db.db_size); 2854 db->db_freed_in_flight = FALSE; 2855 db->db_state = DB_CACHED; 2856 DTRACE_SET_STATE(db, 2857 "fill done handling freed in flight"); 2858 failed = B_FALSE; 2859 } else if (failed) { 2860 VERIFY(!dbuf_undirty(db, tx)); 2861 db->db_buf = NULL; 2862 dbuf_clear_data(db); 2863 DTRACE_SET_STATE(db, "fill failed"); 2864 } else { 2865 db->db_state = DB_CACHED; 2866 DTRACE_SET_STATE(db, "fill done"); 2867 } 2868 cv_broadcast(&db->db_changed); 2869 } else { 2870 db->db_state = DB_CACHED; 2871 failed = B_FALSE; 2872 } 2873 mutex_exit(&db->db_mtx); 2874 return (failed); 2875 } 2876 2877 void 2878 dmu_buf_write_embedded(dmu_buf_t *dbuf, void *data, 2879 bp_embedded_type_t etype, enum zio_compress comp, 2880 int uncompressed_size, int compressed_size, int byteorder, 2881 dmu_tx_t *tx) 2882 { 2883 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf; 2884 struct dirty_leaf *dl; 2885 dmu_object_type_t type; 2886 dbuf_dirty_record_t *dr; 2887 2888 if (etype == BP_EMBEDDED_TYPE_DATA) { 2889 ASSERT(spa_feature_is_active(dmu_objset_spa(db->db_objset), 2890 SPA_FEATURE_EMBEDDED_DATA)); 2891 } 2892 2893 DB_DNODE_ENTER(db); 2894 type = DB_DNODE(db)->dn_type; 2895 DB_DNODE_EXIT(db); 2896 2897 ASSERT0(db->db_level); 2898 ASSERT(db->db_blkid != DMU_BONUS_BLKID); 2899 2900 dmu_buf_will_not_fill(dbuf, tx); 2901 2902 dr = list_head(&db->db_dirty_records); 2903 ASSERT3P(dr, !=, NULL); 2904 ASSERT3U(dr->dr_txg, ==, tx->tx_txg); 2905 dl = &dr->dt.dl; 2906 encode_embedded_bp_compressed(&dl->dr_overridden_by, 2907 data, comp, uncompressed_size, compressed_size); 2908 BPE_SET_ETYPE(&dl->dr_overridden_by, etype); 2909 BP_SET_TYPE(&dl->dr_overridden_by, type); 2910 BP_SET_LEVEL(&dl->dr_overridden_by, 0); 2911 BP_SET_BYTEORDER(&dl->dr_overridden_by, byteorder); 2912 2913 dl->dr_override_state = DR_OVERRIDDEN; 2914 dl->dr_overridden_by.blk_birth = dr->dr_txg; 2915 } 2916 2917 void 2918 dmu_buf_redact(dmu_buf_t *dbuf, dmu_tx_t *tx) 2919 { 2920 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf; 2921 dmu_object_type_t type; 2922 ASSERT(dsl_dataset_feature_is_active(db->db_objset->os_dsl_dataset, 2923 SPA_FEATURE_REDACTED_DATASETS)); 2924 2925 DB_DNODE_ENTER(db); 2926 type = DB_DNODE(db)->dn_type; 2927 DB_DNODE_EXIT(db); 2928 2929 ASSERT0(db->db_level); 2930 dmu_buf_will_not_fill(dbuf, tx); 2931 2932 blkptr_t bp = { { { {0} } } }; 2933 BP_SET_TYPE(&bp, type); 2934 BP_SET_LEVEL(&bp, 0); 2935 BP_SET_BIRTH(&bp, tx->tx_txg, 0); 2936 BP_SET_REDACTED(&bp); 2937 BPE_SET_LSIZE(&bp, dbuf->db_size); 2938 2939 dbuf_override_impl(db, &bp, tx); 2940 } 2941 2942 /* 2943 * Directly assign a provided arc buf to a given dbuf if it's not referenced 2944 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf. 2945 */ 2946 void 2947 dbuf_assign_arcbuf(dmu_buf_impl_t *db, arc_buf_t *buf, dmu_tx_t *tx) 2948 { 2949 ASSERT(!zfs_refcount_is_zero(&db->db_holds)); 2950 ASSERT(db->db_blkid != DMU_BONUS_BLKID); 2951 ASSERT(db->db_level == 0); 2952 ASSERT3U(dbuf_is_metadata(db), ==, arc_is_metadata(buf)); 2953 ASSERT(buf != NULL); 2954 ASSERT3U(arc_buf_lsize(buf), ==, db->db.db_size); 2955 ASSERT(tx->tx_txg != 0); 2956 2957 arc_return_buf(buf, db); 2958 ASSERT(arc_released(buf)); 2959 2960 mutex_enter(&db->db_mtx); 2961 2962 while (db->db_state == DB_READ || db->db_state == DB_FILL) 2963 cv_wait(&db->db_changed, &db->db_mtx); 2964 2965 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_UNCACHED || 2966 db->db_state == DB_NOFILL); 2967 2968 if (db->db_state == DB_CACHED && 2969 zfs_refcount_count(&db->db_holds) - 1 > db->db_dirtycnt) { 2970 /* 2971 * In practice, we will never have a case where we have an 2972 * encrypted arc buffer while additional holds exist on the 2973 * dbuf. We don't handle this here so we simply assert that 2974 * fact instead. 2975 */ 2976 ASSERT(!arc_is_encrypted(buf)); 2977 mutex_exit(&db->db_mtx); 2978 (void) dbuf_dirty(db, tx); 2979 memcpy(db->db.db_data, buf->b_data, db->db.db_size); 2980 arc_buf_destroy(buf, db); 2981 return; 2982 } 2983 2984 if (db->db_state == DB_CACHED) { 2985 dbuf_dirty_record_t *dr = list_head(&db->db_dirty_records); 2986 2987 ASSERT(db->db_buf != NULL); 2988 if (dr != NULL && dr->dr_txg == tx->tx_txg) { 2989 ASSERT(dr->dt.dl.dr_data == db->db_buf); 2990 2991 if (!arc_released(db->db_buf)) { 2992 ASSERT(dr->dt.dl.dr_override_state == 2993 DR_OVERRIDDEN); 2994 arc_release(db->db_buf, db); 2995 } 2996 dr->dt.dl.dr_data = buf; 2997 arc_buf_destroy(db->db_buf, db); 2998 } else if (dr == NULL || dr->dt.dl.dr_data != db->db_buf) { 2999 arc_release(db->db_buf, db); 3000 arc_buf_destroy(db->db_buf, db); 3001 } 3002 db->db_buf = NULL; 3003 } else if (db->db_state == DB_NOFILL) { 3004 /* 3005 * We will be completely replacing the cloned block. In case 3006 * it was cloned in this transaction group, let's undirty the 3007 * pending clone and mark the block as uncached. This will be 3008 * as if the clone was never done. 3009 */ 3010 VERIFY(!dbuf_undirty(db, tx)); 3011 db->db_state = DB_UNCACHED; 3012 } 3013 ASSERT(db->db_buf == NULL); 3014 dbuf_set_data(db, buf); 3015 db->db_state = DB_FILL; 3016 DTRACE_SET_STATE(db, "filling assigned arcbuf"); 3017 mutex_exit(&db->db_mtx); 3018 (void) dbuf_dirty(db, tx); 3019 dmu_buf_fill_done(&db->db, tx, B_FALSE); 3020 } 3021 3022 void 3023 dbuf_destroy(dmu_buf_impl_t *db) 3024 { 3025 dnode_t *dn; 3026 dmu_buf_impl_t *parent = db->db_parent; 3027 dmu_buf_impl_t *dndb; 3028 3029 ASSERT(MUTEX_HELD(&db->db_mtx)); 3030 ASSERT(zfs_refcount_is_zero(&db->db_holds)); 3031 3032 if (db->db_buf != NULL) { 3033 arc_buf_destroy(db->db_buf, db); 3034 db->db_buf = NULL; 3035 } 3036 3037 if (db->db_blkid == DMU_BONUS_BLKID) { 3038 int slots = DB_DNODE(db)->dn_num_slots; 3039 int bonuslen = DN_SLOTS_TO_BONUSLEN(slots); 3040 if (db->db.db_data != NULL) { 3041 kmem_free(db->db.db_data, bonuslen); 3042 arc_space_return(bonuslen, ARC_SPACE_BONUS); 3043 db->db_state = DB_UNCACHED; 3044 DTRACE_SET_STATE(db, "buffer cleared"); 3045 } 3046 } 3047 3048 dbuf_clear_data(db); 3049 3050 if (multilist_link_active(&db->db_cache_link)) { 3051 ASSERT(db->db_caching_status == DB_DBUF_CACHE || 3052 db->db_caching_status == DB_DBUF_METADATA_CACHE); 3053 3054 multilist_remove(&dbuf_caches[db->db_caching_status].cache, db); 3055 3056 ASSERT0(dmu_buf_user_size(&db->db)); 3057 (void) zfs_refcount_remove_many( 3058 &dbuf_caches[db->db_caching_status].size, 3059 db->db.db_size, db); 3060 3061 if (db->db_caching_status == DB_DBUF_METADATA_CACHE) { 3062 DBUF_STAT_BUMPDOWN(metadata_cache_count); 3063 } else { 3064 DBUF_STAT_BUMPDOWN(cache_levels[db->db_level]); 3065 DBUF_STAT_BUMPDOWN(cache_count); 3066 DBUF_STAT_DECR(cache_levels_bytes[db->db_level], 3067 db->db.db_size); 3068 } 3069 db->db_caching_status = DB_NO_CACHE; 3070 } 3071 3072 ASSERT(db->db_state == DB_UNCACHED || db->db_state == DB_NOFILL); 3073 ASSERT(db->db_data_pending == NULL); 3074 ASSERT(list_is_empty(&db->db_dirty_records)); 3075 3076 db->db_state = DB_EVICTING; 3077 DTRACE_SET_STATE(db, "buffer eviction started"); 3078 db->db_blkptr = NULL; 3079 3080 /* 3081 * Now that db_state is DB_EVICTING, nobody else can find this via 3082 * the hash table. We can now drop db_mtx, which allows us to 3083 * acquire the dn_dbufs_mtx. 3084 */ 3085 mutex_exit(&db->db_mtx); 3086 3087 DB_DNODE_ENTER(db); 3088 dn = DB_DNODE(db); 3089 dndb = dn->dn_dbuf; 3090 if (db->db_blkid != DMU_BONUS_BLKID) { 3091 boolean_t needlock = !MUTEX_HELD(&dn->dn_dbufs_mtx); 3092 if (needlock) 3093 mutex_enter_nested(&dn->dn_dbufs_mtx, 3094 NESTED_SINGLE); 3095 avl_remove(&dn->dn_dbufs, db); 3096 membar_producer(); 3097 DB_DNODE_EXIT(db); 3098 if (needlock) 3099 mutex_exit(&dn->dn_dbufs_mtx); 3100 /* 3101 * Decrementing the dbuf count means that the hold corresponding 3102 * to the removed dbuf is no longer discounted in dnode_move(), 3103 * so the dnode cannot be moved until after we release the hold. 3104 * The membar_producer() ensures visibility of the decremented 3105 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually 3106 * release any lock. 3107 */ 3108 mutex_enter(&dn->dn_mtx); 3109 dnode_rele_and_unlock(dn, db, B_TRUE); 3110 db->db_dnode_handle = NULL; 3111 3112 dbuf_hash_remove(db); 3113 } else { 3114 DB_DNODE_EXIT(db); 3115 } 3116 3117 ASSERT(zfs_refcount_is_zero(&db->db_holds)); 3118 3119 db->db_parent = NULL; 3120 3121 ASSERT(db->db_buf == NULL); 3122 ASSERT(db->db.db_data == NULL); 3123 ASSERT(db->db_hash_next == NULL); 3124 ASSERT(db->db_blkptr == NULL); 3125 ASSERT(db->db_data_pending == NULL); 3126 ASSERT3U(db->db_caching_status, ==, DB_NO_CACHE); 3127 ASSERT(!multilist_link_active(&db->db_cache_link)); 3128 3129 /* 3130 * If this dbuf is referenced from an indirect dbuf, 3131 * decrement the ref count on the indirect dbuf. 3132 */ 3133 if (parent && parent != dndb) { 3134 mutex_enter(&parent->db_mtx); 3135 dbuf_rele_and_unlock(parent, db, B_TRUE); 3136 } 3137 3138 kmem_cache_free(dbuf_kmem_cache, db); 3139 arc_space_return(sizeof (dmu_buf_impl_t), ARC_SPACE_DBUF); 3140 } 3141 3142 /* 3143 * Note: While bpp will always be updated if the function returns success, 3144 * parentp will not be updated if the dnode does not have dn_dbuf filled in; 3145 * this happens when the dnode is the meta-dnode, or {user|group|project}used 3146 * object. 3147 */ 3148 __attribute__((always_inline)) 3149 static inline int 3150 dbuf_findbp(dnode_t *dn, int level, uint64_t blkid, int fail_sparse, 3151 dmu_buf_impl_t **parentp, blkptr_t **bpp) 3152 { 3153 *parentp = NULL; 3154 *bpp = NULL; 3155 3156 ASSERT(blkid != DMU_BONUS_BLKID); 3157 3158 if (blkid == DMU_SPILL_BLKID) { 3159 mutex_enter(&dn->dn_mtx); 3160 if (dn->dn_have_spill && 3161 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR)) 3162 *bpp = DN_SPILL_BLKPTR(dn->dn_phys); 3163 else 3164 *bpp = NULL; 3165 dbuf_add_ref(dn->dn_dbuf, NULL); 3166 *parentp = dn->dn_dbuf; 3167 mutex_exit(&dn->dn_mtx); 3168 return (0); 3169 } 3170 3171 int nlevels = 3172 (dn->dn_phys->dn_nlevels == 0) ? 1 : dn->dn_phys->dn_nlevels; 3173 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 3174 3175 ASSERT3U(level * epbs, <, 64); 3176 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock)); 3177 /* 3178 * This assertion shouldn't trip as long as the max indirect block size 3179 * is less than 1M. The reason for this is that up to that point, 3180 * the number of levels required to address an entire object with blocks 3181 * of size SPA_MINBLOCKSIZE satisfies nlevels * epbs + 1 <= 64. In 3182 * other words, if N * epbs + 1 > 64, then if (N-1) * epbs + 1 > 55 3183 * (i.e. we can address the entire object), objects will all use at most 3184 * N-1 levels and the assertion won't overflow. However, once epbs is 3185 * 13, 4 * 13 + 1 = 53, but 5 * 13 + 1 = 66. Then, 4 levels will not be 3186 * enough to address an entire object, so objects will have 5 levels, 3187 * but then this assertion will overflow. 3188 * 3189 * All this is to say that if we ever increase DN_MAX_INDBLKSHIFT, we 3190 * need to redo this logic to handle overflows. 3191 */ 3192 ASSERT(level >= nlevels || 3193 ((nlevels - level - 1) * epbs) + 3194 highbit64(dn->dn_phys->dn_nblkptr) <= 64); 3195 if (level >= nlevels || 3196 blkid >= ((uint64_t)dn->dn_phys->dn_nblkptr << 3197 ((nlevels - level - 1) * epbs)) || 3198 (fail_sparse && 3199 blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))) { 3200 /* the buffer has no parent yet */ 3201 return (SET_ERROR(ENOENT)); 3202 } else if (level < nlevels-1) { 3203 /* this block is referenced from an indirect block */ 3204 int err; 3205 3206 err = dbuf_hold_impl(dn, level + 1, 3207 blkid >> epbs, fail_sparse, FALSE, NULL, parentp); 3208 3209 if (err) 3210 return (err); 3211 err = dbuf_read(*parentp, NULL, 3212 (DB_RF_HAVESTRUCT | DB_RF_NOPREFETCH | DB_RF_CANFAIL)); 3213 if (err) { 3214 dbuf_rele(*parentp, NULL); 3215 *parentp = NULL; 3216 return (err); 3217 } 3218 rw_enter(&(*parentp)->db_rwlock, RW_READER); 3219 *bpp = ((blkptr_t *)(*parentp)->db.db_data) + 3220 (blkid & ((1ULL << epbs) - 1)); 3221 if (blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs))) 3222 ASSERT(BP_IS_HOLE(*bpp)); 3223 rw_exit(&(*parentp)->db_rwlock); 3224 return (0); 3225 } else { 3226 /* the block is referenced from the dnode */ 3227 ASSERT3U(level, ==, nlevels-1); 3228 ASSERT(dn->dn_phys->dn_nblkptr == 0 || 3229 blkid < dn->dn_phys->dn_nblkptr); 3230 if (dn->dn_dbuf) { 3231 dbuf_add_ref(dn->dn_dbuf, NULL); 3232 *parentp = dn->dn_dbuf; 3233 } 3234 *bpp = &dn->dn_phys->dn_blkptr[blkid]; 3235 return (0); 3236 } 3237 } 3238 3239 static dmu_buf_impl_t * 3240 dbuf_create(dnode_t *dn, uint8_t level, uint64_t blkid, 3241 dmu_buf_impl_t *parent, blkptr_t *blkptr, uint64_t hash) 3242 { 3243 objset_t *os = dn->dn_objset; 3244 dmu_buf_impl_t *db, *odb; 3245 3246 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock)); 3247 ASSERT(dn->dn_type != DMU_OT_NONE); 3248 3249 db = kmem_cache_alloc(dbuf_kmem_cache, KM_SLEEP); 3250 3251 list_create(&db->db_dirty_records, sizeof (dbuf_dirty_record_t), 3252 offsetof(dbuf_dirty_record_t, dr_dbuf_node)); 3253 3254 db->db_objset = os; 3255 db->db.db_object = dn->dn_object; 3256 db->db_level = level; 3257 db->db_blkid = blkid; 3258 db->db_dirtycnt = 0; 3259 db->db_dnode_handle = dn->dn_handle; 3260 db->db_parent = parent; 3261 db->db_blkptr = blkptr; 3262 db->db_hash = hash; 3263 3264 db->db_user = NULL; 3265 db->db_user_immediate_evict = FALSE; 3266 db->db_freed_in_flight = FALSE; 3267 db->db_pending_evict = FALSE; 3268 3269 if (blkid == DMU_BONUS_BLKID) { 3270 ASSERT3P(parent, ==, dn->dn_dbuf); 3271 db->db.db_size = DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots) - 3272 (dn->dn_nblkptr-1) * sizeof (blkptr_t); 3273 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen); 3274 db->db.db_offset = DMU_BONUS_BLKID; 3275 db->db_state = DB_UNCACHED; 3276 DTRACE_SET_STATE(db, "bonus buffer created"); 3277 db->db_caching_status = DB_NO_CACHE; 3278 /* the bonus dbuf is not placed in the hash table */ 3279 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_DBUF); 3280 return (db); 3281 } else if (blkid == DMU_SPILL_BLKID) { 3282 db->db.db_size = (blkptr != NULL) ? 3283 BP_GET_LSIZE(blkptr) : SPA_MINBLOCKSIZE; 3284 db->db.db_offset = 0; 3285 } else { 3286 int blocksize = 3287 db->db_level ? 1 << dn->dn_indblkshift : dn->dn_datablksz; 3288 db->db.db_size = blocksize; 3289 db->db.db_offset = db->db_blkid * blocksize; 3290 } 3291 3292 /* 3293 * Hold the dn_dbufs_mtx while we get the new dbuf 3294 * in the hash table *and* added to the dbufs list. 3295 * This prevents a possible deadlock with someone 3296 * trying to look up this dbuf before it's added to the 3297 * dn_dbufs list. 3298 */ 3299 mutex_enter(&dn->dn_dbufs_mtx); 3300 db->db_state = DB_EVICTING; /* not worth logging this state change */ 3301 if ((odb = dbuf_hash_insert(db)) != NULL) { 3302 /* someone else inserted it first */ 3303 mutex_exit(&dn->dn_dbufs_mtx); 3304 kmem_cache_free(dbuf_kmem_cache, db); 3305 DBUF_STAT_BUMP(hash_insert_race); 3306 return (odb); 3307 } 3308 avl_add(&dn->dn_dbufs, db); 3309 3310 db->db_state = DB_UNCACHED; 3311 DTRACE_SET_STATE(db, "regular buffer created"); 3312 db->db_caching_status = DB_NO_CACHE; 3313 mutex_exit(&dn->dn_dbufs_mtx); 3314 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_DBUF); 3315 3316 if (parent && parent != dn->dn_dbuf) 3317 dbuf_add_ref(parent, db); 3318 3319 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT || 3320 zfs_refcount_count(&dn->dn_holds) > 0); 3321 (void) zfs_refcount_add(&dn->dn_holds, db); 3322 3323 dprintf_dbuf(db, "db=%p\n", db); 3324 3325 return (db); 3326 } 3327 3328 /* 3329 * This function returns a block pointer and information about the object, 3330 * given a dnode and a block. This is a publicly accessible version of 3331 * dbuf_findbp that only returns some information, rather than the 3332 * dbuf. Note that the dnode passed in must be held, and the dn_struct_rwlock 3333 * should be locked as (at least) a reader. 3334 */ 3335 int 3336 dbuf_dnode_findbp(dnode_t *dn, uint64_t level, uint64_t blkid, 3337 blkptr_t *bp, uint16_t *datablkszsec, uint8_t *indblkshift) 3338 { 3339 dmu_buf_impl_t *dbp = NULL; 3340 blkptr_t *bp2; 3341 int err = 0; 3342 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock)); 3343 3344 err = dbuf_findbp(dn, level, blkid, B_FALSE, &dbp, &bp2); 3345 if (err == 0) { 3346 ASSERT3P(bp2, !=, NULL); 3347 *bp = *bp2; 3348 if (dbp != NULL) 3349 dbuf_rele(dbp, NULL); 3350 if (datablkszsec != NULL) 3351 *datablkszsec = dn->dn_phys->dn_datablkszsec; 3352 if (indblkshift != NULL) 3353 *indblkshift = dn->dn_phys->dn_indblkshift; 3354 } 3355 3356 return (err); 3357 } 3358 3359 typedef struct dbuf_prefetch_arg { 3360 spa_t *dpa_spa; /* The spa to issue the prefetch in. */ 3361 zbookmark_phys_t dpa_zb; /* The target block to prefetch. */ 3362 int dpa_epbs; /* Entries (blkptr_t's) Per Block Shift. */ 3363 int dpa_curlevel; /* The current level that we're reading */ 3364 dnode_t *dpa_dnode; /* The dnode associated with the prefetch */ 3365 zio_priority_t dpa_prio; /* The priority I/Os should be issued at. */ 3366 zio_t *dpa_zio; /* The parent zio_t for all prefetches. */ 3367 arc_flags_t dpa_aflags; /* Flags to pass to the final prefetch. */ 3368 dbuf_prefetch_fn dpa_cb; /* prefetch completion callback */ 3369 void *dpa_arg; /* prefetch completion arg */ 3370 } dbuf_prefetch_arg_t; 3371 3372 static void 3373 dbuf_prefetch_fini(dbuf_prefetch_arg_t *dpa, boolean_t io_done) 3374 { 3375 if (dpa->dpa_cb != NULL) { 3376 dpa->dpa_cb(dpa->dpa_arg, dpa->dpa_zb.zb_level, 3377 dpa->dpa_zb.zb_blkid, io_done); 3378 } 3379 kmem_free(dpa, sizeof (*dpa)); 3380 } 3381 3382 static void 3383 dbuf_issue_final_prefetch_done(zio_t *zio, const zbookmark_phys_t *zb, 3384 const blkptr_t *iobp, arc_buf_t *abuf, void *private) 3385 { 3386 (void) zio, (void) zb, (void) iobp; 3387 dbuf_prefetch_arg_t *dpa = private; 3388 3389 if (abuf != NULL) 3390 arc_buf_destroy(abuf, private); 3391 3392 dbuf_prefetch_fini(dpa, B_TRUE); 3393 } 3394 3395 /* 3396 * Actually issue the prefetch read for the block given. 3397 */ 3398 static void 3399 dbuf_issue_final_prefetch(dbuf_prefetch_arg_t *dpa, blkptr_t *bp) 3400 { 3401 ASSERT(!BP_IS_REDACTED(bp) || 3402 dsl_dataset_feature_is_active( 3403 dpa->dpa_dnode->dn_objset->os_dsl_dataset, 3404 SPA_FEATURE_REDACTED_DATASETS)); 3405 3406 if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp) || BP_IS_REDACTED(bp)) 3407 return (dbuf_prefetch_fini(dpa, B_FALSE)); 3408 3409 int zio_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE; 3410 arc_flags_t aflags = 3411 dpa->dpa_aflags | ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH | 3412 ARC_FLAG_NO_BUF; 3413 3414 /* dnodes are always read as raw and then converted later */ 3415 if (BP_GET_TYPE(bp) == DMU_OT_DNODE && BP_IS_PROTECTED(bp) && 3416 dpa->dpa_curlevel == 0) 3417 zio_flags |= ZIO_FLAG_RAW; 3418 3419 ASSERT3U(dpa->dpa_curlevel, ==, BP_GET_LEVEL(bp)); 3420 ASSERT3U(dpa->dpa_curlevel, ==, dpa->dpa_zb.zb_level); 3421 ASSERT(dpa->dpa_zio != NULL); 3422 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa, bp, 3423 dbuf_issue_final_prefetch_done, dpa, 3424 dpa->dpa_prio, zio_flags, &aflags, &dpa->dpa_zb); 3425 } 3426 3427 /* 3428 * Called when an indirect block above our prefetch target is read in. This 3429 * will either read in the next indirect block down the tree or issue the actual 3430 * prefetch if the next block down is our target. 3431 */ 3432 static void 3433 dbuf_prefetch_indirect_done(zio_t *zio, const zbookmark_phys_t *zb, 3434 const blkptr_t *iobp, arc_buf_t *abuf, void *private) 3435 { 3436 (void) zb, (void) iobp; 3437 dbuf_prefetch_arg_t *dpa = private; 3438 3439 ASSERT3S(dpa->dpa_zb.zb_level, <, dpa->dpa_curlevel); 3440 ASSERT3S(dpa->dpa_curlevel, >, 0); 3441 3442 if (abuf == NULL) { 3443 ASSERT(zio == NULL || zio->io_error != 0); 3444 dbuf_prefetch_fini(dpa, B_TRUE); 3445 return; 3446 } 3447 ASSERT(zio == NULL || zio->io_error == 0); 3448 3449 /* 3450 * The dpa_dnode is only valid if we are called with a NULL 3451 * zio. This indicates that the arc_read() returned without 3452 * first calling zio_read() to issue a physical read. Once 3453 * a physical read is made the dpa_dnode must be invalidated 3454 * as the locks guarding it may have been dropped. If the 3455 * dpa_dnode is still valid, then we want to add it to the dbuf 3456 * cache. To do so, we must hold the dbuf associated with the block 3457 * we just prefetched, read its contents so that we associate it 3458 * with an arc_buf_t, and then release it. 3459 */ 3460 if (zio != NULL) { 3461 ASSERT3S(BP_GET_LEVEL(zio->io_bp), ==, dpa->dpa_curlevel); 3462 if (zio->io_flags & ZIO_FLAG_RAW_COMPRESS) { 3463 ASSERT3U(BP_GET_PSIZE(zio->io_bp), ==, zio->io_size); 3464 } else { 3465 ASSERT3U(BP_GET_LSIZE(zio->io_bp), ==, zio->io_size); 3466 } 3467 ASSERT3P(zio->io_spa, ==, dpa->dpa_spa); 3468 3469 dpa->dpa_dnode = NULL; 3470 } else if (dpa->dpa_dnode != NULL) { 3471 uint64_t curblkid = dpa->dpa_zb.zb_blkid >> 3472 (dpa->dpa_epbs * (dpa->dpa_curlevel - 3473 dpa->dpa_zb.zb_level)); 3474 dmu_buf_impl_t *db = dbuf_hold_level(dpa->dpa_dnode, 3475 dpa->dpa_curlevel, curblkid, FTAG); 3476 if (db == NULL) { 3477 arc_buf_destroy(abuf, private); 3478 dbuf_prefetch_fini(dpa, B_TRUE); 3479 return; 3480 } 3481 (void) dbuf_read(db, NULL, 3482 DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH | DB_RF_HAVESTRUCT); 3483 dbuf_rele(db, FTAG); 3484 } 3485 3486 dpa->dpa_curlevel--; 3487 uint64_t nextblkid = dpa->dpa_zb.zb_blkid >> 3488 (dpa->dpa_epbs * (dpa->dpa_curlevel - dpa->dpa_zb.zb_level)); 3489 blkptr_t *bp = ((blkptr_t *)abuf->b_data) + 3490 P2PHASE(nextblkid, 1ULL << dpa->dpa_epbs); 3491 3492 ASSERT(!BP_IS_REDACTED(bp) || (dpa->dpa_dnode && 3493 dsl_dataset_feature_is_active( 3494 dpa->dpa_dnode->dn_objset->os_dsl_dataset, 3495 SPA_FEATURE_REDACTED_DATASETS))); 3496 if (BP_IS_HOLE(bp) || BP_IS_REDACTED(bp)) { 3497 arc_buf_destroy(abuf, private); 3498 dbuf_prefetch_fini(dpa, B_TRUE); 3499 return; 3500 } else if (dpa->dpa_curlevel == dpa->dpa_zb.zb_level) { 3501 ASSERT3U(nextblkid, ==, dpa->dpa_zb.zb_blkid); 3502 dbuf_issue_final_prefetch(dpa, bp); 3503 } else { 3504 arc_flags_t iter_aflags = ARC_FLAG_NOWAIT; 3505 zbookmark_phys_t zb; 3506 3507 /* flag if L2ARC eligible, l2arc_noprefetch then decides */ 3508 if (dpa->dpa_aflags & ARC_FLAG_L2CACHE) 3509 iter_aflags |= ARC_FLAG_L2CACHE; 3510 3511 ASSERT3U(dpa->dpa_curlevel, ==, BP_GET_LEVEL(bp)); 3512 3513 SET_BOOKMARK(&zb, dpa->dpa_zb.zb_objset, 3514 dpa->dpa_zb.zb_object, dpa->dpa_curlevel, nextblkid); 3515 3516 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa, 3517 bp, dbuf_prefetch_indirect_done, dpa, 3518 ZIO_PRIORITY_SYNC_READ, 3519 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, 3520 &iter_aflags, &zb); 3521 } 3522 3523 arc_buf_destroy(abuf, private); 3524 } 3525 3526 /* 3527 * Issue prefetch reads for the given block on the given level. If the indirect 3528 * blocks above that block are not in memory, we will read them in 3529 * asynchronously. As a result, this call never blocks waiting for a read to 3530 * complete. Note that the prefetch might fail if the dataset is encrypted and 3531 * the encryption key is unmapped before the IO completes. 3532 */ 3533 int 3534 dbuf_prefetch_impl(dnode_t *dn, int64_t level, uint64_t blkid, 3535 zio_priority_t prio, arc_flags_t aflags, dbuf_prefetch_fn cb, 3536 void *arg) 3537 { 3538 blkptr_t bp; 3539 int epbs, nlevels, curlevel; 3540 uint64_t curblkid; 3541 3542 ASSERT(blkid != DMU_BONUS_BLKID); 3543 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock)); 3544 3545 if (blkid > dn->dn_maxblkid) 3546 goto no_issue; 3547 3548 if (level == 0 && dnode_block_freed(dn, blkid)) 3549 goto no_issue; 3550 3551 /* 3552 * This dnode hasn't been written to disk yet, so there's nothing to 3553 * prefetch. 3554 */ 3555 nlevels = dn->dn_phys->dn_nlevels; 3556 if (level >= nlevels || dn->dn_phys->dn_nblkptr == 0) 3557 goto no_issue; 3558 3559 epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; 3560 if (dn->dn_phys->dn_maxblkid < blkid << (epbs * level)) 3561 goto no_issue; 3562 3563 dmu_buf_impl_t *db = dbuf_find(dn->dn_objset, dn->dn_object, 3564 level, blkid, NULL); 3565 if (db != NULL) { 3566 mutex_exit(&db->db_mtx); 3567 /* 3568 * This dbuf already exists. It is either CACHED, or 3569 * (we assume) about to be read or filled. 3570 */ 3571 goto no_issue; 3572 } 3573 3574 /* 3575 * Find the closest ancestor (indirect block) of the target block 3576 * that is present in the cache. In this indirect block, we will 3577 * find the bp that is at curlevel, curblkid. 3578 */ 3579 curlevel = level; 3580 curblkid = blkid; 3581 while (curlevel < nlevels - 1) { 3582 int parent_level = curlevel + 1; 3583 uint64_t parent_blkid = curblkid >> epbs; 3584 dmu_buf_impl_t *db; 3585 3586 if (dbuf_hold_impl(dn, parent_level, parent_blkid, 3587 FALSE, TRUE, FTAG, &db) == 0) { 3588 blkptr_t *bpp = db->db_buf->b_data; 3589 bp = bpp[P2PHASE(curblkid, 1 << epbs)]; 3590 dbuf_rele(db, FTAG); 3591 break; 3592 } 3593 3594 curlevel = parent_level; 3595 curblkid = parent_blkid; 3596 } 3597 3598 if (curlevel == nlevels - 1) { 3599 /* No cached indirect blocks found. */ 3600 ASSERT3U(curblkid, <, dn->dn_phys->dn_nblkptr); 3601 bp = dn->dn_phys->dn_blkptr[curblkid]; 3602 } 3603 ASSERT(!BP_IS_REDACTED(&bp) || 3604 dsl_dataset_feature_is_active(dn->dn_objset->os_dsl_dataset, 3605 SPA_FEATURE_REDACTED_DATASETS)); 3606 if (BP_IS_HOLE(&bp) || BP_IS_REDACTED(&bp)) 3607 goto no_issue; 3608 3609 ASSERT3U(curlevel, ==, BP_GET_LEVEL(&bp)); 3610 3611 zio_t *pio = zio_root(dmu_objset_spa(dn->dn_objset), NULL, NULL, 3612 ZIO_FLAG_CANFAIL); 3613 3614 dbuf_prefetch_arg_t *dpa = kmem_zalloc(sizeof (*dpa), KM_SLEEP); 3615 dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset; 3616 SET_BOOKMARK(&dpa->dpa_zb, ds != NULL ? ds->ds_object : DMU_META_OBJSET, 3617 dn->dn_object, level, blkid); 3618 dpa->dpa_curlevel = curlevel; 3619 dpa->dpa_prio = prio; 3620 dpa->dpa_aflags = aflags; 3621 dpa->dpa_spa = dn->dn_objset->os_spa; 3622 dpa->dpa_dnode = dn; 3623 dpa->dpa_epbs = epbs; 3624 dpa->dpa_zio = pio; 3625 dpa->dpa_cb = cb; 3626 dpa->dpa_arg = arg; 3627 3628 if (!DNODE_LEVEL_IS_CACHEABLE(dn, level)) 3629 dpa->dpa_aflags |= ARC_FLAG_UNCACHED; 3630 else if (dnode_level_is_l2cacheable(&bp, dn, level)) 3631 dpa->dpa_aflags |= ARC_FLAG_L2CACHE; 3632 3633 /* 3634 * If we have the indirect just above us, no need to do the asynchronous 3635 * prefetch chain; we'll just run the last step ourselves. If we're at 3636 * a higher level, though, we want to issue the prefetches for all the 3637 * indirect blocks asynchronously, so we can go on with whatever we were 3638 * doing. 3639 */ 3640 if (curlevel == level) { 3641 ASSERT3U(curblkid, ==, blkid); 3642 dbuf_issue_final_prefetch(dpa, &bp); 3643 } else { 3644 arc_flags_t iter_aflags = ARC_FLAG_NOWAIT; 3645 zbookmark_phys_t zb; 3646 3647 /* flag if L2ARC eligible, l2arc_noprefetch then decides */ 3648 if (dnode_level_is_l2cacheable(&bp, dn, level)) 3649 iter_aflags |= ARC_FLAG_L2CACHE; 3650 3651 SET_BOOKMARK(&zb, ds != NULL ? ds->ds_object : DMU_META_OBJSET, 3652 dn->dn_object, curlevel, curblkid); 3653 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa, 3654 &bp, dbuf_prefetch_indirect_done, dpa, 3655 ZIO_PRIORITY_SYNC_READ, 3656 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE, 3657 &iter_aflags, &zb); 3658 } 3659 /* 3660 * We use pio here instead of dpa_zio since it's possible that 3661 * dpa may have already been freed. 3662 */ 3663 zio_nowait(pio); 3664 return (1); 3665 no_issue: 3666 if (cb != NULL) 3667 cb(arg, level, blkid, B_FALSE); 3668 return (0); 3669 } 3670 3671 int 3672 dbuf_prefetch(dnode_t *dn, int64_t level, uint64_t blkid, zio_priority_t prio, 3673 arc_flags_t aflags) 3674 { 3675 3676 return (dbuf_prefetch_impl(dn, level, blkid, prio, aflags, NULL, NULL)); 3677 } 3678 3679 /* 3680 * Helper function for dbuf_hold_impl() to copy a buffer. Handles 3681 * the case of encrypted, compressed and uncompressed buffers by 3682 * allocating the new buffer, respectively, with arc_alloc_raw_buf(), 3683 * arc_alloc_compressed_buf() or arc_alloc_buf().* 3684 * 3685 * NOTE: Declared noinline to avoid stack bloat in dbuf_hold_impl(). 3686 */ 3687 noinline static void 3688 dbuf_hold_copy(dnode_t *dn, dmu_buf_impl_t *db) 3689 { 3690 dbuf_dirty_record_t *dr = db->db_data_pending; 3691 arc_buf_t *data = dr->dt.dl.dr_data; 3692 enum zio_compress compress_type = arc_get_compression(data); 3693 uint8_t complevel = arc_get_complevel(data); 3694 3695 if (arc_is_encrypted(data)) { 3696 boolean_t byteorder; 3697 uint8_t salt[ZIO_DATA_SALT_LEN]; 3698 uint8_t iv[ZIO_DATA_IV_LEN]; 3699 uint8_t mac[ZIO_DATA_MAC_LEN]; 3700 3701 arc_get_raw_params(data, &byteorder, salt, iv, mac); 3702 dbuf_set_data(db, arc_alloc_raw_buf(dn->dn_objset->os_spa, db, 3703 dmu_objset_id(dn->dn_objset), byteorder, salt, iv, mac, 3704 dn->dn_type, arc_buf_size(data), arc_buf_lsize(data), 3705 compress_type, complevel)); 3706 } else if (compress_type != ZIO_COMPRESS_OFF) { 3707 dbuf_set_data(db, arc_alloc_compressed_buf( 3708 dn->dn_objset->os_spa, db, arc_buf_size(data), 3709 arc_buf_lsize(data), compress_type, complevel)); 3710 } else { 3711 dbuf_set_data(db, arc_alloc_buf(dn->dn_objset->os_spa, db, 3712 DBUF_GET_BUFC_TYPE(db), db->db.db_size)); 3713 } 3714 3715 rw_enter(&db->db_rwlock, RW_WRITER); 3716 memcpy(db->db.db_data, data->b_data, arc_buf_size(data)); 3717 rw_exit(&db->db_rwlock); 3718 } 3719 3720 /* 3721 * Returns with db_holds incremented, and db_mtx not held. 3722 * Note: dn_struct_rwlock must be held. 3723 */ 3724 int 3725 dbuf_hold_impl(dnode_t *dn, uint8_t level, uint64_t blkid, 3726 boolean_t fail_sparse, boolean_t fail_uncached, 3727 const void *tag, dmu_buf_impl_t **dbp) 3728 { 3729 dmu_buf_impl_t *db, *parent = NULL; 3730 uint64_t hv; 3731 3732 /* If the pool has been created, verify the tx_sync_lock is not held */ 3733 spa_t *spa = dn->dn_objset->os_spa; 3734 dsl_pool_t *dp = spa->spa_dsl_pool; 3735 if (dp != NULL) { 3736 ASSERT(!MUTEX_HELD(&dp->dp_tx.tx_sync_lock)); 3737 } 3738 3739 ASSERT(blkid != DMU_BONUS_BLKID); 3740 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock)); 3741 ASSERT3U(dn->dn_nlevels, >, level); 3742 3743 *dbp = NULL; 3744 3745 /* dbuf_find() returns with db_mtx held */ 3746 db = dbuf_find(dn->dn_objset, dn->dn_object, level, blkid, &hv); 3747 3748 if (db == NULL) { 3749 blkptr_t *bp = NULL; 3750 int err; 3751 3752 if (fail_uncached) 3753 return (SET_ERROR(ENOENT)); 3754 3755 ASSERT3P(parent, ==, NULL); 3756 err = dbuf_findbp(dn, level, blkid, fail_sparse, &parent, &bp); 3757 if (fail_sparse) { 3758 if (err == 0 && bp && BP_IS_HOLE(bp)) 3759 err = SET_ERROR(ENOENT); 3760 if (err) { 3761 if (parent) 3762 dbuf_rele(parent, NULL); 3763 return (err); 3764 } 3765 } 3766 if (err && err != ENOENT) 3767 return (err); 3768 db = dbuf_create(dn, level, blkid, parent, bp, hv); 3769 } 3770 3771 if (fail_uncached && db->db_state != DB_CACHED) { 3772 mutex_exit(&db->db_mtx); 3773 return (SET_ERROR(ENOENT)); 3774 } 3775 3776 if (db->db_buf != NULL) { 3777 arc_buf_access(db->db_buf); 3778 ASSERT3P(db->db.db_data, ==, db->db_buf->b_data); 3779 } 3780 3781 ASSERT(db->db_buf == NULL || arc_referenced(db->db_buf)); 3782 3783 /* 3784 * If this buffer is currently syncing out, and we are 3785 * still referencing it from db_data, we need to make a copy 3786 * of it in case we decide we want to dirty it again in this txg. 3787 */ 3788 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID && 3789 dn->dn_object != DMU_META_DNODE_OBJECT && 3790 db->db_state == DB_CACHED && db->db_data_pending) { 3791 dbuf_dirty_record_t *dr = db->db_data_pending; 3792 if (dr->dt.dl.dr_data == db->db_buf) { 3793 ASSERT3P(db->db_buf, !=, NULL); 3794 dbuf_hold_copy(dn, db); 3795 } 3796 } 3797 3798 if (multilist_link_active(&db->db_cache_link)) { 3799 ASSERT(zfs_refcount_is_zero(&db->db_holds)); 3800 ASSERT(db->db_caching_status == DB_DBUF_CACHE || 3801 db->db_caching_status == DB_DBUF_METADATA_CACHE); 3802 3803 multilist_remove(&dbuf_caches[db->db_caching_status].cache, db); 3804 3805 uint64_t size = db->db.db_size + dmu_buf_user_size(&db->db); 3806 (void) zfs_refcount_remove_many( 3807 &dbuf_caches[db->db_caching_status].size, size, db); 3808 3809 if (db->db_caching_status == DB_DBUF_METADATA_CACHE) { 3810 DBUF_STAT_BUMPDOWN(metadata_cache_count); 3811 } else { 3812 DBUF_STAT_BUMPDOWN(cache_levels[db->db_level]); 3813 DBUF_STAT_BUMPDOWN(cache_count); 3814 DBUF_STAT_DECR(cache_levels_bytes[db->db_level], size); 3815 } 3816 db->db_caching_status = DB_NO_CACHE; 3817 } 3818 (void) zfs_refcount_add(&db->db_holds, tag); 3819 DBUF_VERIFY(db); 3820 mutex_exit(&db->db_mtx); 3821 3822 /* NOTE: we can't rele the parent until after we drop the db_mtx */ 3823 if (parent) 3824 dbuf_rele(parent, NULL); 3825 3826 ASSERT3P(DB_DNODE(db), ==, dn); 3827 ASSERT3U(db->db_blkid, ==, blkid); 3828 ASSERT3U(db->db_level, ==, level); 3829 *dbp = db; 3830 3831 return (0); 3832 } 3833 3834 dmu_buf_impl_t * 3835 dbuf_hold(dnode_t *dn, uint64_t blkid, const void *tag) 3836 { 3837 return (dbuf_hold_level(dn, 0, blkid, tag)); 3838 } 3839 3840 dmu_buf_impl_t * 3841 dbuf_hold_level(dnode_t *dn, int level, uint64_t blkid, const void *tag) 3842 { 3843 dmu_buf_impl_t *db; 3844 int err = dbuf_hold_impl(dn, level, blkid, FALSE, FALSE, tag, &db); 3845 return (err ? NULL : db); 3846 } 3847 3848 void 3849 dbuf_create_bonus(dnode_t *dn) 3850 { 3851 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock)); 3852 3853 ASSERT(dn->dn_bonus == NULL); 3854 dn->dn_bonus = dbuf_create(dn, 0, DMU_BONUS_BLKID, dn->dn_dbuf, NULL, 3855 dbuf_hash(dn->dn_objset, dn->dn_object, 0, DMU_BONUS_BLKID)); 3856 } 3857 3858 int 3859 dbuf_spill_set_blksz(dmu_buf_t *db_fake, uint64_t blksz, dmu_tx_t *tx) 3860 { 3861 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 3862 3863 if (db->db_blkid != DMU_SPILL_BLKID) 3864 return (SET_ERROR(ENOTSUP)); 3865 if (blksz == 0) 3866 blksz = SPA_MINBLOCKSIZE; 3867 ASSERT3U(blksz, <=, spa_maxblocksize(dmu_objset_spa(db->db_objset))); 3868 blksz = P2ROUNDUP(blksz, SPA_MINBLOCKSIZE); 3869 3870 dbuf_new_size(db, blksz, tx); 3871 3872 return (0); 3873 } 3874 3875 void 3876 dbuf_rm_spill(dnode_t *dn, dmu_tx_t *tx) 3877 { 3878 dbuf_free_range(dn, DMU_SPILL_BLKID, DMU_SPILL_BLKID, tx); 3879 } 3880 3881 #pragma weak dmu_buf_add_ref = dbuf_add_ref 3882 void 3883 dbuf_add_ref(dmu_buf_impl_t *db, const void *tag) 3884 { 3885 int64_t holds = zfs_refcount_add(&db->db_holds, tag); 3886 VERIFY3S(holds, >, 1); 3887 } 3888 3889 #pragma weak dmu_buf_try_add_ref = dbuf_try_add_ref 3890 boolean_t 3891 dbuf_try_add_ref(dmu_buf_t *db_fake, objset_t *os, uint64_t obj, uint64_t blkid, 3892 const void *tag) 3893 { 3894 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 3895 dmu_buf_impl_t *found_db; 3896 boolean_t result = B_FALSE; 3897 3898 if (blkid == DMU_BONUS_BLKID) 3899 found_db = dbuf_find_bonus(os, obj); 3900 else 3901 found_db = dbuf_find(os, obj, 0, blkid, NULL); 3902 3903 if (found_db != NULL) { 3904 if (db == found_db && dbuf_refcount(db) > db->db_dirtycnt) { 3905 (void) zfs_refcount_add(&db->db_holds, tag); 3906 result = B_TRUE; 3907 } 3908 mutex_exit(&found_db->db_mtx); 3909 } 3910 return (result); 3911 } 3912 3913 /* 3914 * If you call dbuf_rele() you had better not be referencing the dnode handle 3915 * unless you have some other direct or indirect hold on the dnode. (An indirect 3916 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.) 3917 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the 3918 * dnode's parent dbuf evicting its dnode handles. 3919 */ 3920 void 3921 dbuf_rele(dmu_buf_impl_t *db, const void *tag) 3922 { 3923 mutex_enter(&db->db_mtx); 3924 dbuf_rele_and_unlock(db, tag, B_FALSE); 3925 } 3926 3927 void 3928 dmu_buf_rele(dmu_buf_t *db, const void *tag) 3929 { 3930 dbuf_rele((dmu_buf_impl_t *)db, tag); 3931 } 3932 3933 /* 3934 * dbuf_rele() for an already-locked dbuf. This is necessary to allow 3935 * db_dirtycnt and db_holds to be updated atomically. The 'evicting' 3936 * argument should be set if we are already in the dbuf-evicting code 3937 * path, in which case we don't want to recursively evict. This allows us to 3938 * avoid deeply nested stacks that would have a call flow similar to this: 3939 * 3940 * dbuf_rele()-->dbuf_rele_and_unlock()-->dbuf_evict_notify() 3941 * ^ | 3942 * | | 3943 * +-----dbuf_destroy()<--dbuf_evict_one()<--------+ 3944 * 3945 */ 3946 void 3947 dbuf_rele_and_unlock(dmu_buf_impl_t *db, const void *tag, boolean_t evicting) 3948 { 3949 int64_t holds; 3950 uint64_t size; 3951 3952 ASSERT(MUTEX_HELD(&db->db_mtx)); 3953 DBUF_VERIFY(db); 3954 3955 /* 3956 * Remove the reference to the dbuf before removing its hold on the 3957 * dnode so we can guarantee in dnode_move() that a referenced bonus 3958 * buffer has a corresponding dnode hold. 3959 */ 3960 holds = zfs_refcount_remove(&db->db_holds, tag); 3961 ASSERT(holds >= 0); 3962 3963 /* 3964 * We can't freeze indirects if there is a possibility that they 3965 * may be modified in the current syncing context. 3966 */ 3967 if (db->db_buf != NULL && 3968 holds == (db->db_level == 0 ? db->db_dirtycnt : 0)) { 3969 arc_buf_freeze(db->db_buf); 3970 } 3971 3972 if (holds == db->db_dirtycnt && 3973 db->db_level == 0 && db->db_user_immediate_evict) 3974 dbuf_evict_user(db); 3975 3976 if (holds == 0) { 3977 if (db->db_blkid == DMU_BONUS_BLKID) { 3978 dnode_t *dn; 3979 boolean_t evict_dbuf = db->db_pending_evict; 3980 3981 /* 3982 * If the dnode moves here, we cannot cross this 3983 * barrier until the move completes. 3984 */ 3985 DB_DNODE_ENTER(db); 3986 3987 dn = DB_DNODE(db); 3988 atomic_dec_32(&dn->dn_dbufs_count); 3989 3990 /* 3991 * Decrementing the dbuf count means that the bonus 3992 * buffer's dnode hold is no longer discounted in 3993 * dnode_move(). The dnode cannot move until after 3994 * the dnode_rele() below. 3995 */ 3996 DB_DNODE_EXIT(db); 3997 3998 /* 3999 * Do not reference db after its lock is dropped. 4000 * Another thread may evict it. 4001 */ 4002 mutex_exit(&db->db_mtx); 4003 4004 if (evict_dbuf) 4005 dnode_evict_bonus(dn); 4006 4007 dnode_rele(dn, db); 4008 } else if (db->db_buf == NULL) { 4009 /* 4010 * This is a special case: we never associated this 4011 * dbuf with any data allocated from the ARC. 4012 */ 4013 ASSERT(db->db_state == DB_UNCACHED || 4014 db->db_state == DB_NOFILL); 4015 dbuf_destroy(db); 4016 } else if (arc_released(db->db_buf)) { 4017 /* 4018 * This dbuf has anonymous data associated with it. 4019 */ 4020 dbuf_destroy(db); 4021 } else if (!(DBUF_IS_CACHEABLE(db) || db->db_partial_read) || 4022 db->db_pending_evict) { 4023 dbuf_destroy(db); 4024 } else if (!multilist_link_active(&db->db_cache_link)) { 4025 ASSERT3U(db->db_caching_status, ==, DB_NO_CACHE); 4026 4027 dbuf_cached_state_t dcs = 4028 dbuf_include_in_metadata_cache(db) ? 4029 DB_DBUF_METADATA_CACHE : DB_DBUF_CACHE; 4030 db->db_caching_status = dcs; 4031 4032 multilist_insert(&dbuf_caches[dcs].cache, db); 4033 uint64_t db_size = db->db.db_size + 4034 dmu_buf_user_size(&db->db); 4035 size = zfs_refcount_add_many( 4036 &dbuf_caches[dcs].size, db_size, db); 4037 uint8_t db_level = db->db_level; 4038 mutex_exit(&db->db_mtx); 4039 4040 if (dcs == DB_DBUF_METADATA_CACHE) { 4041 DBUF_STAT_BUMP(metadata_cache_count); 4042 DBUF_STAT_MAX(metadata_cache_size_bytes_max, 4043 size); 4044 } else { 4045 DBUF_STAT_BUMP(cache_count); 4046 DBUF_STAT_MAX(cache_size_bytes_max, size); 4047 DBUF_STAT_BUMP(cache_levels[db_level]); 4048 DBUF_STAT_INCR(cache_levels_bytes[db_level], 4049 db_size); 4050 } 4051 4052 if (dcs == DB_DBUF_CACHE && !evicting) 4053 dbuf_evict_notify(size); 4054 } 4055 } else { 4056 mutex_exit(&db->db_mtx); 4057 } 4058 4059 } 4060 4061 #pragma weak dmu_buf_refcount = dbuf_refcount 4062 uint64_t 4063 dbuf_refcount(dmu_buf_impl_t *db) 4064 { 4065 return (zfs_refcount_count(&db->db_holds)); 4066 } 4067 4068 uint64_t 4069 dmu_buf_user_refcount(dmu_buf_t *db_fake) 4070 { 4071 uint64_t holds; 4072 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 4073 4074 mutex_enter(&db->db_mtx); 4075 ASSERT3U(zfs_refcount_count(&db->db_holds), >=, db->db_dirtycnt); 4076 holds = zfs_refcount_count(&db->db_holds) - db->db_dirtycnt; 4077 mutex_exit(&db->db_mtx); 4078 4079 return (holds); 4080 } 4081 4082 void * 4083 dmu_buf_replace_user(dmu_buf_t *db_fake, dmu_buf_user_t *old_user, 4084 dmu_buf_user_t *new_user) 4085 { 4086 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 4087 4088 mutex_enter(&db->db_mtx); 4089 dbuf_verify_user(db, DBVU_NOT_EVICTING); 4090 if (db->db_user == old_user) 4091 db->db_user = new_user; 4092 else 4093 old_user = db->db_user; 4094 dbuf_verify_user(db, DBVU_NOT_EVICTING); 4095 mutex_exit(&db->db_mtx); 4096 4097 return (old_user); 4098 } 4099 4100 void * 4101 dmu_buf_set_user(dmu_buf_t *db_fake, dmu_buf_user_t *user) 4102 { 4103 return (dmu_buf_replace_user(db_fake, NULL, user)); 4104 } 4105 4106 void * 4107 dmu_buf_set_user_ie(dmu_buf_t *db_fake, dmu_buf_user_t *user) 4108 { 4109 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 4110 4111 db->db_user_immediate_evict = TRUE; 4112 return (dmu_buf_set_user(db_fake, user)); 4113 } 4114 4115 void * 4116 dmu_buf_remove_user(dmu_buf_t *db_fake, dmu_buf_user_t *user) 4117 { 4118 return (dmu_buf_replace_user(db_fake, user, NULL)); 4119 } 4120 4121 void * 4122 dmu_buf_get_user(dmu_buf_t *db_fake) 4123 { 4124 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 4125 4126 dbuf_verify_user(db, DBVU_NOT_EVICTING); 4127 return (db->db_user); 4128 } 4129 4130 uint64_t 4131 dmu_buf_user_size(dmu_buf_t *db_fake) 4132 { 4133 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 4134 if (db->db_user == NULL) 4135 return (0); 4136 return (atomic_load_64(&db->db_user->dbu_size)); 4137 } 4138 4139 void 4140 dmu_buf_add_user_size(dmu_buf_t *db_fake, uint64_t nadd) 4141 { 4142 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 4143 ASSERT3U(db->db_caching_status, ==, DB_NO_CACHE); 4144 ASSERT3P(db->db_user, !=, NULL); 4145 ASSERT3U(atomic_load_64(&db->db_user->dbu_size), <, UINT64_MAX - nadd); 4146 atomic_add_64(&db->db_user->dbu_size, nadd); 4147 } 4148 4149 void 4150 dmu_buf_sub_user_size(dmu_buf_t *db_fake, uint64_t nsub) 4151 { 4152 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake; 4153 ASSERT3U(db->db_caching_status, ==, DB_NO_CACHE); 4154 ASSERT3P(db->db_user, !=, NULL); 4155 ASSERT3U(atomic_load_64(&db->db_user->dbu_size), >=, nsub); 4156 atomic_sub_64(&db->db_user->dbu_size, nsub); 4157 } 4158 4159 void 4160 dmu_buf_user_evict_wait(void) 4161 { 4162 taskq_wait(dbu_evict_taskq); 4163 } 4164 4165 blkptr_t * 4166 dmu_buf_get_blkptr(dmu_buf_t *db) 4167 { 4168 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db; 4169 return (dbi->db_blkptr); 4170 } 4171 4172 objset_t * 4173 dmu_buf_get_objset(dmu_buf_t *db) 4174 { 4175 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db; 4176 return (dbi->db_objset); 4177 } 4178 4179 dnode_t * 4180 dmu_buf_dnode_enter(dmu_buf_t *db) 4181 { 4182 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db; 4183 DB_DNODE_ENTER(dbi); 4184 return (DB_DNODE(dbi)); 4185 } 4186 4187 void 4188 dmu_buf_dnode_exit(dmu_buf_t *db) 4189 { 4190 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db; 4191 DB_DNODE_EXIT(dbi); 4192 } 4193 4194 static void 4195 dbuf_check_blkptr(dnode_t *dn, dmu_buf_impl_t *db) 4196 { 4197 /* ASSERT(dmu_tx_is_syncing(tx) */ 4198 ASSERT(MUTEX_HELD(&db->db_mtx)); 4199 4200 if (db->db_blkptr != NULL) 4201 return; 4202 4203 if (db->db_blkid == DMU_SPILL_BLKID) { 4204 db->db_blkptr = DN_SPILL_BLKPTR(dn->dn_phys); 4205 BP_ZERO(db->db_blkptr); 4206 return; 4207 } 4208 if (db->db_level == dn->dn_phys->dn_nlevels-1) { 4209 /* 4210 * This buffer was allocated at a time when there was 4211 * no available blkptrs from the dnode, or it was 4212 * inappropriate to hook it in (i.e., nlevels mismatch). 4213 */ 4214 ASSERT(db->db_blkid < dn->dn_phys->dn_nblkptr); 4215 ASSERT(db->db_parent == NULL); 4216 db->db_parent = dn->dn_dbuf; 4217 db->db_blkptr = &dn->dn_phys->dn_blkptr[db->db_blkid]; 4218 DBUF_VERIFY(db); 4219 } else { 4220 dmu_buf_impl_t *parent = db->db_parent; 4221 int epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; 4222 4223 ASSERT(dn->dn_phys->dn_nlevels > 1); 4224 if (parent == NULL) { 4225 mutex_exit(&db->db_mtx); 4226 rw_enter(&dn->dn_struct_rwlock, RW_READER); 4227 parent = dbuf_hold_level(dn, db->db_level + 1, 4228 db->db_blkid >> epbs, db); 4229 rw_exit(&dn->dn_struct_rwlock); 4230 mutex_enter(&db->db_mtx); 4231 db->db_parent = parent; 4232 } 4233 db->db_blkptr = (blkptr_t *)parent->db.db_data + 4234 (db->db_blkid & ((1ULL << epbs) - 1)); 4235 DBUF_VERIFY(db); 4236 } 4237 } 4238 4239 static void 4240 dbuf_sync_bonus(dbuf_dirty_record_t *dr, dmu_tx_t *tx) 4241 { 4242 dmu_buf_impl_t *db = dr->dr_dbuf; 4243 void *data = dr->dt.dl.dr_data; 4244 4245 ASSERT0(db->db_level); 4246 ASSERT(MUTEX_HELD(&db->db_mtx)); 4247 ASSERT(db->db_blkid == DMU_BONUS_BLKID); 4248 ASSERT(data != NULL); 4249 4250 dnode_t *dn = dr->dr_dnode; 4251 ASSERT3U(DN_MAX_BONUS_LEN(dn->dn_phys), <=, 4252 DN_SLOTS_TO_BONUSLEN(dn->dn_phys->dn_extra_slots + 1)); 4253 memcpy(DN_BONUS(dn->dn_phys), data, DN_MAX_BONUS_LEN(dn->dn_phys)); 4254 4255 dbuf_sync_leaf_verify_bonus_dnode(dr); 4256 4257 dbuf_undirty_bonus(dr); 4258 dbuf_rele_and_unlock(db, (void *)(uintptr_t)tx->tx_txg, B_FALSE); 4259 } 4260 4261 /* 4262 * When syncing out a blocks of dnodes, adjust the block to deal with 4263 * encryption. Normally, we make sure the block is decrypted before writing 4264 * it. If we have crypt params, then we are writing a raw (encrypted) block, 4265 * from a raw receive. In this case, set the ARC buf's crypt params so 4266 * that the BP will be filled with the correct byteorder, salt, iv, and mac. 4267 */ 4268 static void 4269 dbuf_prepare_encrypted_dnode_leaf(dbuf_dirty_record_t *dr) 4270 { 4271 int err; 4272 dmu_buf_impl_t *db = dr->dr_dbuf; 4273 4274 ASSERT(MUTEX_HELD(&db->db_mtx)); 4275 ASSERT3U(db->db.db_object, ==, DMU_META_DNODE_OBJECT); 4276 ASSERT3U(db->db_level, ==, 0); 4277 4278 if (!db->db_objset->os_raw_receive && arc_is_encrypted(db->db_buf)) { 4279 zbookmark_phys_t zb; 4280 4281 /* 4282 * Unfortunately, there is currently no mechanism for 4283 * syncing context to handle decryption errors. An error 4284 * here is only possible if an attacker maliciously 4285 * changed a dnode block and updated the associated 4286 * checksums going up the block tree. 4287 */ 4288 SET_BOOKMARK(&zb, dmu_objset_id(db->db_objset), 4289 db->db.db_object, db->db_level, db->db_blkid); 4290 err = arc_untransform(db->db_buf, db->db_objset->os_spa, 4291 &zb, B_TRUE); 4292 if (err) 4293 panic("Invalid dnode block MAC"); 4294 } else if (dr->dt.dl.dr_has_raw_params) { 4295 (void) arc_release(dr->dt.dl.dr_data, db); 4296 arc_convert_to_raw(dr->dt.dl.dr_data, 4297 dmu_objset_id(db->db_objset), 4298 dr->dt.dl.dr_byteorder, DMU_OT_DNODE, 4299 dr->dt.dl.dr_salt, dr->dt.dl.dr_iv, dr->dt.dl.dr_mac); 4300 } 4301 } 4302 4303 /* 4304 * dbuf_sync_indirect() is called recursively from dbuf_sync_list() so it 4305 * is critical the we not allow the compiler to inline this function in to 4306 * dbuf_sync_list() thereby drastically bloating the stack usage. 4307 */ 4308 noinline static void 4309 dbuf_sync_indirect(dbuf_dirty_record_t *dr, dmu_tx_t *tx) 4310 { 4311 dmu_buf_impl_t *db = dr->dr_dbuf; 4312 dnode_t *dn = dr->dr_dnode; 4313 4314 ASSERT(dmu_tx_is_syncing(tx)); 4315 4316 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr); 4317 4318 mutex_enter(&db->db_mtx); 4319 4320 ASSERT(db->db_level > 0); 4321 DBUF_VERIFY(db); 4322 4323 /* Read the block if it hasn't been read yet. */ 4324 if (db->db_buf == NULL) { 4325 mutex_exit(&db->db_mtx); 4326 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED); 4327 mutex_enter(&db->db_mtx); 4328 } 4329 ASSERT3U(db->db_state, ==, DB_CACHED); 4330 ASSERT(db->db_buf != NULL); 4331 4332 /* Indirect block size must match what the dnode thinks it is. */ 4333 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift); 4334 dbuf_check_blkptr(dn, db); 4335 4336 /* Provide the pending dirty record to child dbufs */ 4337 db->db_data_pending = dr; 4338 4339 mutex_exit(&db->db_mtx); 4340 4341 dbuf_write(dr, db->db_buf, tx); 4342 4343 zio_t *zio = dr->dr_zio; 4344 mutex_enter(&dr->dt.di.dr_mtx); 4345 dbuf_sync_list(&dr->dt.di.dr_children, db->db_level - 1, tx); 4346 ASSERT(list_head(&dr->dt.di.dr_children) == NULL); 4347 mutex_exit(&dr->dt.di.dr_mtx); 4348 zio_nowait(zio); 4349 } 4350 4351 /* 4352 * Verify that the size of the data in our bonus buffer does not exceed 4353 * its recorded size. 4354 * 4355 * The purpose of this verification is to catch any cases in development 4356 * where the size of a phys structure (i.e space_map_phys_t) grows and, 4357 * due to incorrect feature management, older pools expect to read more 4358 * data even though they didn't actually write it to begin with. 4359 * 4360 * For a example, this would catch an error in the feature logic where we 4361 * open an older pool and we expect to write the space map histogram of 4362 * a space map with size SPACE_MAP_SIZE_V0. 4363 */ 4364 static void 4365 dbuf_sync_leaf_verify_bonus_dnode(dbuf_dirty_record_t *dr) 4366 { 4367 #ifdef ZFS_DEBUG 4368 dnode_t *dn = dr->dr_dnode; 4369 4370 /* 4371 * Encrypted bonus buffers can have data past their bonuslen. 4372 * Skip the verification of these blocks. 4373 */ 4374 if (DMU_OT_IS_ENCRYPTED(dn->dn_bonustype)) 4375 return; 4376 4377 uint16_t bonuslen = dn->dn_phys->dn_bonuslen; 4378 uint16_t maxbonuslen = DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots); 4379 ASSERT3U(bonuslen, <=, maxbonuslen); 4380 4381 arc_buf_t *datap = dr->dt.dl.dr_data; 4382 char *datap_end = ((char *)datap) + bonuslen; 4383 char *datap_max = ((char *)datap) + maxbonuslen; 4384 4385 /* ensure that everything is zero after our data */ 4386 for (; datap_end < datap_max; datap_end++) 4387 ASSERT(*datap_end == 0); 4388 #endif 4389 } 4390 4391 static blkptr_t * 4392 dbuf_lightweight_bp(dbuf_dirty_record_t *dr) 4393 { 4394 /* This must be a lightweight dirty record. */ 4395 ASSERT3P(dr->dr_dbuf, ==, NULL); 4396 dnode_t *dn = dr->dr_dnode; 4397 4398 if (dn->dn_phys->dn_nlevels == 1) { 4399 VERIFY3U(dr->dt.dll.dr_blkid, <, dn->dn_phys->dn_nblkptr); 4400 return (&dn->dn_phys->dn_blkptr[dr->dt.dll.dr_blkid]); 4401 } else { 4402 dmu_buf_impl_t *parent_db = dr->dr_parent->dr_dbuf; 4403 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT; 4404 VERIFY3U(parent_db->db_level, ==, 1); 4405 VERIFY3P(parent_db->db_dnode_handle->dnh_dnode, ==, dn); 4406 VERIFY3U(dr->dt.dll.dr_blkid >> epbs, ==, parent_db->db_blkid); 4407 blkptr_t *bp = parent_db->db.db_data; 4408 return (&bp[dr->dt.dll.dr_blkid & ((1 << epbs) - 1)]); 4409 } 4410 } 4411 4412 static void 4413 dbuf_lightweight_ready(zio_t *zio) 4414 { 4415 dbuf_dirty_record_t *dr = zio->io_private; 4416 blkptr_t *bp = zio->io_bp; 4417 4418 if (zio->io_error != 0) 4419 return; 4420 4421 dnode_t *dn = dr->dr_dnode; 4422 4423 blkptr_t *bp_orig = dbuf_lightweight_bp(dr); 4424 spa_t *spa = dmu_objset_spa(dn->dn_objset); 4425 int64_t delta = bp_get_dsize_sync(spa, bp) - 4426 bp_get_dsize_sync(spa, bp_orig); 4427 dnode_diduse_space(dn, delta); 4428 4429 uint64_t blkid = dr->dt.dll.dr_blkid; 4430 mutex_enter(&dn->dn_mtx); 4431 if (blkid > dn->dn_phys->dn_maxblkid) { 4432 ASSERT0(dn->dn_objset->os_raw_receive); 4433 dn->dn_phys->dn_maxblkid = blkid; 4434 } 4435 mutex_exit(&dn->dn_mtx); 4436 4437 if (!BP_IS_EMBEDDED(bp)) { 4438 uint64_t fill = BP_IS_HOLE(bp) ? 0 : 1; 4439 BP_SET_FILL(bp, fill); 4440 } 4441 4442 dmu_buf_impl_t *parent_db; 4443 EQUIV(dr->dr_parent == NULL, dn->dn_phys->dn_nlevels == 1); 4444 if (dr->dr_parent == NULL) { 4445 parent_db = dn->dn_dbuf; 4446 } else { 4447 parent_db = dr->dr_parent->dr_dbuf; 4448 } 4449 rw_enter(&parent_db->db_rwlock, RW_WRITER); 4450 *bp_orig = *bp; 4451 rw_exit(&parent_db->db_rwlock); 4452 } 4453 4454 static void 4455 dbuf_lightweight_done(zio_t *zio) 4456 { 4457 dbuf_dirty_record_t *dr = zio->io_private; 4458 4459 VERIFY0(zio->io_error); 4460 4461 objset_t *os = dr->dr_dnode->dn_objset; 4462 dmu_tx_t *tx = os->os_synctx; 4463 4464 if (zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)) { 4465 ASSERT(BP_EQUAL(zio->io_bp, &zio->io_bp_orig)); 4466 } else { 4467 dsl_dataset_t *ds = os->os_dsl_dataset; 4468 (void) dsl_dataset_block_kill(ds, &zio->io_bp_orig, tx, B_TRUE); 4469 dsl_dataset_block_born(ds, zio->io_bp, tx); 4470 } 4471 4472 dsl_pool_undirty_space(dmu_objset_pool(os), dr->dr_accounted, 4473 zio->io_txg); 4474 4475 abd_free(dr->dt.dll.dr_abd); 4476 kmem_free(dr, sizeof (*dr)); 4477 } 4478 4479 noinline static void 4480 dbuf_sync_lightweight(dbuf_dirty_record_t *dr, dmu_tx_t *tx) 4481 { 4482 dnode_t *dn = dr->dr_dnode; 4483 zio_t *pio; 4484 if (dn->dn_phys->dn_nlevels == 1) { 4485 pio = dn->dn_zio; 4486 } else { 4487 pio = dr->dr_parent->dr_zio; 4488 } 4489 4490 zbookmark_phys_t zb = { 4491 .zb_objset = dmu_objset_id(dn->dn_objset), 4492 .zb_object = dn->dn_object, 4493 .zb_level = 0, 4494 .zb_blkid = dr->dt.dll.dr_blkid, 4495 }; 4496 4497 /* 4498 * See comment in dbuf_write(). This is so that zio->io_bp_orig 4499 * will have the old BP in dbuf_lightweight_done(). 4500 */ 4501 dr->dr_bp_copy = *dbuf_lightweight_bp(dr); 4502 4503 dr->dr_zio = zio_write(pio, dmu_objset_spa(dn->dn_objset), 4504 dmu_tx_get_txg(tx), &dr->dr_bp_copy, dr->dt.dll.dr_abd, 4505 dn->dn_datablksz, abd_get_size(dr->dt.dll.dr_abd), 4506 &dr->dt.dll.dr_props, dbuf_lightweight_ready, NULL, 4507 dbuf_lightweight_done, dr, ZIO_PRIORITY_ASYNC_WRITE, 4508 ZIO_FLAG_MUSTSUCCEED | dr->dt.dll.dr_flags, &zb); 4509 4510 zio_nowait(dr->dr_zio); 4511 } 4512 4513 /* 4514 * dbuf_sync_leaf() is called recursively from dbuf_sync_list() so it is 4515 * critical the we not allow the compiler to inline this function in to 4516 * dbuf_sync_list() thereby drastically bloating the stack usage. 4517 */ 4518 noinline static void 4519 dbuf_sync_leaf(dbuf_dirty_record_t *dr, dmu_tx_t *tx) 4520 { 4521 arc_buf_t **datap = &dr->dt.dl.dr_data; 4522 dmu_buf_impl_t *db = dr->dr_dbuf; 4523 dnode_t *dn = dr->dr_dnode; 4524 objset_t *os; 4525 uint64_t txg = tx->tx_txg; 4526 4527 ASSERT(dmu_tx_is_syncing(tx)); 4528 4529 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr); 4530 4531 mutex_enter(&db->db_mtx); 4532 /* 4533 * To be synced, we must be dirtied. But we 4534 * might have been freed after the dirty. 4535 */ 4536 if (db->db_state == DB_UNCACHED) { 4537 /* This buffer has been freed since it was dirtied */ 4538 ASSERT(db->db.db_data == NULL); 4539 } else if (db->db_state == DB_FILL) { 4540 /* This buffer was freed and is now being re-filled */ 4541 ASSERT(db->db.db_data != dr->dt.dl.dr_data); 4542 } else if (db->db_state == DB_READ) { 4543 /* 4544 * This buffer has a clone we need to write, and an in-flight 4545 * read on the BP we're about to clone. Its safe to issue the 4546 * write here because the read has already been issued and the 4547 * contents won't change. 4548 */ 4549 ASSERT(dr->dt.dl.dr_brtwrite && 4550 dr->dt.dl.dr_override_state == DR_OVERRIDDEN); 4551 } else { 4552 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_NOFILL); 4553 } 4554 DBUF_VERIFY(db); 4555 4556 if (db->db_blkid == DMU_SPILL_BLKID) { 4557 mutex_enter(&dn->dn_mtx); 4558 if (!(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR)) { 4559 /* 4560 * In the previous transaction group, the bonus buffer 4561 * was entirely used to store the attributes for the 4562 * dnode which overrode the dn_spill field. However, 4563 * when adding more attributes to the file a spill 4564 * block was required to hold the extra attributes. 4565 * 4566 * Make sure to clear the garbage left in the dn_spill 4567 * field from the previous attributes in the bonus 4568 * buffer. Otherwise, after writing out the spill 4569 * block to the new allocated dva, it will free 4570 * the old block pointed to by the invalid dn_spill. 4571 */ 4572 db->db_blkptr = NULL; 4573 } 4574 dn->dn_phys->dn_flags |= DNODE_FLAG_SPILL_BLKPTR; 4575 mutex_exit(&dn->dn_mtx); 4576 } 4577 4578 /* 4579 * If this is a bonus buffer, simply copy the bonus data into the 4580 * dnode. It will be written out when the dnode is synced (and it 4581 * will be synced, since it must have been dirty for dbuf_sync to 4582 * be called). 4583 */ 4584 if (db->db_blkid == DMU_BONUS_BLKID) { 4585 ASSERT(dr->dr_dbuf == db); 4586 dbuf_sync_bonus(dr, tx); 4587 return; 4588 } 4589 4590 os = dn->dn_objset; 4591 4592 /* 4593 * This function may have dropped the db_mtx lock allowing a dmu_sync 4594 * operation to sneak in. As a result, we need to ensure that we 4595 * don't check the dr_override_state until we have returned from 4596 * dbuf_check_blkptr. 4597 */ 4598 dbuf_check_blkptr(dn, db); 4599 4600 /* 4601 * If this buffer is in the middle of an immediate write, 4602 * wait for the synchronous IO to complete. 4603 */ 4604 while (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) { 4605 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT); 4606 cv_wait(&db->db_changed, &db->db_mtx); 4607 } 4608 4609 /* 4610 * If this is a dnode block, ensure it is appropriately encrypted 4611 * or decrypted, depending on what we are writing to it this txg. 4612 */ 4613 if (os->os_encrypted && dn->dn_object == DMU_META_DNODE_OBJECT) 4614 dbuf_prepare_encrypted_dnode_leaf(dr); 4615 4616 if (db->db_state != DB_NOFILL && 4617 dn->dn_object != DMU_META_DNODE_OBJECT && 4618 zfs_refcount_count(&db->db_holds) > 1 && 4619 dr->dt.dl.dr_override_state != DR_OVERRIDDEN && 4620 *datap == db->db_buf) { 4621 /* 4622 * If this buffer is currently "in use" (i.e., there 4623 * are active holds and db_data still references it), 4624 * then make a copy before we start the write so that 4625 * any modifications from the open txg will not leak 4626 * into this write. 4627 * 4628 * NOTE: this copy does not need to be made for 4629 * objects only modified in the syncing context (e.g. 4630 * DNONE_DNODE blocks). 4631 */ 4632 int psize = arc_buf_size(*datap); 4633 int lsize = arc_buf_lsize(*datap); 4634 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db); 4635 enum zio_compress compress_type = arc_get_compression(*datap); 4636 uint8_t complevel = arc_get_complevel(*datap); 4637 4638 if (arc_is_encrypted(*datap)) { 4639 boolean_t byteorder; 4640 uint8_t salt[ZIO_DATA_SALT_LEN]; 4641 uint8_t iv[ZIO_DATA_IV_LEN]; 4642 uint8_t mac[ZIO_DATA_MAC_LEN]; 4643 4644 arc_get_raw_params(*datap, &byteorder, salt, iv, mac); 4645 *datap = arc_alloc_raw_buf(os->os_spa, db, 4646 dmu_objset_id(os), byteorder, salt, iv, mac, 4647 dn->dn_type, psize, lsize, compress_type, 4648 complevel); 4649 } else if (compress_type != ZIO_COMPRESS_OFF) { 4650 ASSERT3U(type, ==, ARC_BUFC_DATA); 4651 *datap = arc_alloc_compressed_buf(os->os_spa, db, 4652 psize, lsize, compress_type, complevel); 4653 } else { 4654 *datap = arc_alloc_buf(os->os_spa, db, type, psize); 4655 } 4656 memcpy((*datap)->b_data, db->db.db_data, psize); 4657 } 4658 db->db_data_pending = dr; 4659 4660 mutex_exit(&db->db_mtx); 4661 4662 dbuf_write(dr, *datap, tx); 4663 4664 ASSERT(!list_link_active(&dr->dr_dirty_node)); 4665 if (dn->dn_object == DMU_META_DNODE_OBJECT) { 4666 list_insert_tail(&dn->dn_dirty_records[txg & TXG_MASK], dr); 4667 } else { 4668 zio_nowait(dr->dr_zio); 4669 } 4670 } 4671 4672 /* 4673 * Syncs out a range of dirty records for indirect or leaf dbufs. May be 4674 * called recursively from dbuf_sync_indirect(). 4675 */ 4676 void 4677 dbuf_sync_list(list_t *list, int level, dmu_tx_t *tx) 4678 { 4679 dbuf_dirty_record_t *dr; 4680 4681 while ((dr = list_head(list))) { 4682 if (dr->dr_zio != NULL) { 4683 /* 4684 * If we find an already initialized zio then we 4685 * are processing the meta-dnode, and we have finished. 4686 * The dbufs for all dnodes are put back on the list 4687 * during processing, so that we can zio_wait() 4688 * these IOs after initiating all child IOs. 4689 */ 4690 ASSERT3U(dr->dr_dbuf->db.db_object, ==, 4691 DMU_META_DNODE_OBJECT); 4692 break; 4693 } 4694 list_remove(list, dr); 4695 if (dr->dr_dbuf == NULL) { 4696 dbuf_sync_lightweight(dr, tx); 4697 } else { 4698 if (dr->dr_dbuf->db_blkid != DMU_BONUS_BLKID && 4699 dr->dr_dbuf->db_blkid != DMU_SPILL_BLKID) { 4700 VERIFY3U(dr->dr_dbuf->db_level, ==, level); 4701 } 4702 if (dr->dr_dbuf->db_level > 0) 4703 dbuf_sync_indirect(dr, tx); 4704 else 4705 dbuf_sync_leaf(dr, tx); 4706 } 4707 } 4708 } 4709 4710 static void 4711 dbuf_write_ready(zio_t *zio, arc_buf_t *buf, void *vdb) 4712 { 4713 (void) buf; 4714 dmu_buf_impl_t *db = vdb; 4715 dnode_t *dn; 4716 blkptr_t *bp = zio->io_bp; 4717 blkptr_t *bp_orig = &zio->io_bp_orig; 4718 spa_t *spa = zio->io_spa; 4719 int64_t delta; 4720 uint64_t fill = 0; 4721 int i; 4722 4723 ASSERT3P(db->db_blkptr, !=, NULL); 4724 ASSERT3P(&db->db_data_pending->dr_bp_copy, ==, bp); 4725 4726 DB_DNODE_ENTER(db); 4727 dn = DB_DNODE(db); 4728 delta = bp_get_dsize_sync(spa, bp) - bp_get_dsize_sync(spa, bp_orig); 4729 dnode_diduse_space(dn, delta - zio->io_prev_space_delta); 4730 zio->io_prev_space_delta = delta; 4731 4732 if (bp->blk_birth != 0) { 4733 ASSERT((db->db_blkid != DMU_SPILL_BLKID && 4734 BP_GET_TYPE(bp) == dn->dn_type) || 4735 (db->db_blkid == DMU_SPILL_BLKID && 4736 BP_GET_TYPE(bp) == dn->dn_bonustype) || 4737 BP_IS_EMBEDDED(bp)); 4738 ASSERT(BP_GET_LEVEL(bp) == db->db_level); 4739 } 4740 4741 mutex_enter(&db->db_mtx); 4742 4743 #ifdef ZFS_DEBUG 4744 if (db->db_blkid == DMU_SPILL_BLKID) { 4745 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR); 4746 ASSERT(!(BP_IS_HOLE(bp)) && 4747 db->db_blkptr == DN_SPILL_BLKPTR(dn->dn_phys)); 4748 } 4749 #endif 4750 4751 if (db->db_level == 0) { 4752 mutex_enter(&dn->dn_mtx); 4753 if (db->db_blkid > dn->dn_phys->dn_maxblkid && 4754 db->db_blkid != DMU_SPILL_BLKID) { 4755 ASSERT0(db->db_objset->os_raw_receive); 4756 dn->dn_phys->dn_maxblkid = db->db_blkid; 4757 } 4758 mutex_exit(&dn->dn_mtx); 4759 4760 if (dn->dn_type == DMU_OT_DNODE) { 4761 i = 0; 4762 while (i < db->db.db_size) { 4763 dnode_phys_t *dnp = 4764 (void *)(((char *)db->db.db_data) + i); 4765 4766 i += DNODE_MIN_SIZE; 4767 if (dnp->dn_type != DMU_OT_NONE) { 4768 fill++; 4769 for (int j = 0; j < dnp->dn_nblkptr; 4770 j++) { 4771 (void) zfs_blkptr_verify(spa, 4772 &dnp->dn_blkptr[j], 4773 BLK_CONFIG_SKIP, 4774 BLK_VERIFY_HALT); 4775 } 4776 if (dnp->dn_flags & 4777 DNODE_FLAG_SPILL_BLKPTR) { 4778 (void) zfs_blkptr_verify(spa, 4779 DN_SPILL_BLKPTR(dnp), 4780 BLK_CONFIG_SKIP, 4781 BLK_VERIFY_HALT); 4782 } 4783 i += dnp->dn_extra_slots * 4784 DNODE_MIN_SIZE; 4785 } 4786 } 4787 } else { 4788 if (BP_IS_HOLE(bp)) { 4789 fill = 0; 4790 } else { 4791 fill = 1; 4792 } 4793 } 4794 } else { 4795 blkptr_t *ibp = db->db.db_data; 4796 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift); 4797 for (i = db->db.db_size >> SPA_BLKPTRSHIFT; i > 0; i--, ibp++) { 4798 if (BP_IS_HOLE(ibp)) 4799 continue; 4800 (void) zfs_blkptr_verify(spa, ibp, 4801 BLK_CONFIG_SKIP, BLK_VERIFY_HALT); 4802 fill += BP_GET_FILL(ibp); 4803 } 4804 } 4805 DB_DNODE_EXIT(db); 4806 4807 if (!BP_IS_EMBEDDED(bp)) 4808 BP_SET_FILL(bp, fill); 4809 4810 mutex_exit(&db->db_mtx); 4811 4812 db_lock_type_t dblt = dmu_buf_lock_parent(db, RW_WRITER, FTAG); 4813 *db->db_blkptr = *bp; 4814 dmu_buf_unlock_parent(db, dblt, FTAG); 4815 } 4816 4817 /* 4818 * This function gets called just prior to running through the compression 4819 * stage of the zio pipeline. If we're an indirect block comprised of only 4820 * holes, then we want this indirect to be compressed away to a hole. In 4821 * order to do that we must zero out any information about the holes that 4822 * this indirect points to prior to before we try to compress it. 4823 */ 4824 static void 4825 dbuf_write_children_ready(zio_t *zio, arc_buf_t *buf, void *vdb) 4826 { 4827 (void) zio, (void) buf; 4828 dmu_buf_impl_t *db = vdb; 4829 dnode_t *dn; 4830 blkptr_t *bp; 4831 unsigned int epbs, i; 4832 4833 ASSERT3U(db->db_level, >, 0); 4834 DB_DNODE_ENTER(db); 4835 dn = DB_DNODE(db); 4836 epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; 4837 ASSERT3U(epbs, <, 31); 4838 4839 /* Determine if all our children are holes */ 4840 for (i = 0, bp = db->db.db_data; i < 1ULL << epbs; i++, bp++) { 4841 if (!BP_IS_HOLE(bp)) 4842 break; 4843 } 4844 4845 /* 4846 * If all the children are holes, then zero them all out so that 4847 * we may get compressed away. 4848 */ 4849 if (i == 1ULL << epbs) { 4850 /* 4851 * We only found holes. Grab the rwlock to prevent 4852 * anybody from reading the blocks we're about to 4853 * zero out. 4854 */ 4855 rw_enter(&db->db_rwlock, RW_WRITER); 4856 memset(db->db.db_data, 0, db->db.db_size); 4857 rw_exit(&db->db_rwlock); 4858 } 4859 DB_DNODE_EXIT(db); 4860 } 4861 4862 static void 4863 dbuf_write_done(zio_t *zio, arc_buf_t *buf, void *vdb) 4864 { 4865 (void) buf; 4866 dmu_buf_impl_t *db = vdb; 4867 blkptr_t *bp_orig = &zio->io_bp_orig; 4868 blkptr_t *bp = db->db_blkptr; 4869 objset_t *os = db->db_objset; 4870 dmu_tx_t *tx = os->os_synctx; 4871 4872 ASSERT0(zio->io_error); 4873 ASSERT(db->db_blkptr == bp); 4874 4875 /* 4876 * For nopwrites and rewrites we ensure that the bp matches our 4877 * original and bypass all the accounting. 4878 */ 4879 if (zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)) { 4880 ASSERT(BP_EQUAL(bp, bp_orig)); 4881 } else { 4882 dsl_dataset_t *ds = os->os_dsl_dataset; 4883 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE); 4884 dsl_dataset_block_born(ds, bp, tx); 4885 } 4886 4887 mutex_enter(&db->db_mtx); 4888 4889 DBUF_VERIFY(db); 4890 4891 dbuf_dirty_record_t *dr = db->db_data_pending; 4892 dnode_t *dn = dr->dr_dnode; 4893 ASSERT(!list_link_active(&dr->dr_dirty_node)); 4894 ASSERT(dr->dr_dbuf == db); 4895 ASSERT(list_next(&db->db_dirty_records, dr) == NULL); 4896 list_remove(&db->db_dirty_records, dr); 4897 4898 #ifdef ZFS_DEBUG 4899 if (db->db_blkid == DMU_SPILL_BLKID) { 4900 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR); 4901 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) && 4902 db->db_blkptr == DN_SPILL_BLKPTR(dn->dn_phys)); 4903 } 4904 #endif 4905 4906 if (db->db_level == 0) { 4907 ASSERT(db->db_blkid != DMU_BONUS_BLKID); 4908 ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN); 4909 if (db->db_state != DB_NOFILL) { 4910 if (dr->dt.dl.dr_data != NULL && 4911 dr->dt.dl.dr_data != db->db_buf) { 4912 arc_buf_destroy(dr->dt.dl.dr_data, db); 4913 } 4914 } 4915 } else { 4916 ASSERT(list_head(&dr->dt.di.dr_children) == NULL); 4917 ASSERT3U(db->db.db_size, ==, 1 << dn->dn_phys->dn_indblkshift); 4918 if (!BP_IS_HOLE(db->db_blkptr)) { 4919 int epbs __maybe_unused = dn->dn_phys->dn_indblkshift - 4920 SPA_BLKPTRSHIFT; 4921 ASSERT3U(db->db_blkid, <=, 4922 dn->dn_phys->dn_maxblkid >> (db->db_level * epbs)); 4923 ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==, 4924 db->db.db_size); 4925 } 4926 mutex_destroy(&dr->dt.di.dr_mtx); 4927 list_destroy(&dr->dt.di.dr_children); 4928 } 4929 4930 cv_broadcast(&db->db_changed); 4931 ASSERT(db->db_dirtycnt > 0); 4932 db->db_dirtycnt -= 1; 4933 db->db_data_pending = NULL; 4934 dbuf_rele_and_unlock(db, (void *)(uintptr_t)tx->tx_txg, B_FALSE); 4935 4936 dsl_pool_undirty_space(dmu_objset_pool(os), dr->dr_accounted, 4937 zio->io_txg); 4938 4939 kmem_free(dr, sizeof (dbuf_dirty_record_t)); 4940 } 4941 4942 static void 4943 dbuf_write_nofill_ready(zio_t *zio) 4944 { 4945 dbuf_write_ready(zio, NULL, zio->io_private); 4946 } 4947 4948 static void 4949 dbuf_write_nofill_done(zio_t *zio) 4950 { 4951 dbuf_write_done(zio, NULL, zio->io_private); 4952 } 4953 4954 static void 4955 dbuf_write_override_ready(zio_t *zio) 4956 { 4957 dbuf_dirty_record_t *dr = zio->io_private; 4958 dmu_buf_impl_t *db = dr->dr_dbuf; 4959 4960 dbuf_write_ready(zio, NULL, db); 4961 } 4962 4963 static void 4964 dbuf_write_override_done(zio_t *zio) 4965 { 4966 dbuf_dirty_record_t *dr = zio->io_private; 4967 dmu_buf_impl_t *db = dr->dr_dbuf; 4968 blkptr_t *obp = &dr->dt.dl.dr_overridden_by; 4969 4970 mutex_enter(&db->db_mtx); 4971 if (!BP_EQUAL(zio->io_bp, obp)) { 4972 if (!BP_IS_HOLE(obp)) 4973 dsl_free(spa_get_dsl(zio->io_spa), zio->io_txg, obp); 4974 arc_release(dr->dt.dl.dr_data, db); 4975 } 4976 mutex_exit(&db->db_mtx); 4977 4978 dbuf_write_done(zio, NULL, db); 4979 4980 if (zio->io_abd != NULL) 4981 abd_free(zio->io_abd); 4982 } 4983 4984 typedef struct dbuf_remap_impl_callback_arg { 4985 objset_t *drica_os; 4986 uint64_t drica_blk_birth; 4987 dmu_tx_t *drica_tx; 4988 } dbuf_remap_impl_callback_arg_t; 4989 4990 static void 4991 dbuf_remap_impl_callback(uint64_t vdev, uint64_t offset, uint64_t size, 4992 void *arg) 4993 { 4994 dbuf_remap_impl_callback_arg_t *drica = arg; 4995 objset_t *os = drica->drica_os; 4996 spa_t *spa = dmu_objset_spa(os); 4997 dmu_tx_t *tx = drica->drica_tx; 4998 4999 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa))); 5000 5001 if (os == spa_meta_objset(spa)) { 5002 spa_vdev_indirect_mark_obsolete(spa, vdev, offset, size, tx); 5003 } else { 5004 dsl_dataset_block_remapped(dmu_objset_ds(os), vdev, offset, 5005 size, drica->drica_blk_birth, tx); 5006 } 5007 } 5008 5009 static void 5010 dbuf_remap_impl(dnode_t *dn, blkptr_t *bp, krwlock_t *rw, dmu_tx_t *tx) 5011 { 5012 blkptr_t bp_copy = *bp; 5013 spa_t *spa = dmu_objset_spa(dn->dn_objset); 5014 dbuf_remap_impl_callback_arg_t drica; 5015 5016 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa))); 5017 5018 drica.drica_os = dn->dn_objset; 5019 drica.drica_blk_birth = bp->blk_birth; 5020 drica.drica_tx = tx; 5021 if (spa_remap_blkptr(spa, &bp_copy, dbuf_remap_impl_callback, 5022 &drica)) { 5023 /* 5024 * If the blkptr being remapped is tracked by a livelist, 5025 * then we need to make sure the livelist reflects the update. 5026 * First, cancel out the old blkptr by appending a 'FREE' 5027 * entry. Next, add an 'ALLOC' to track the new version. This 5028 * way we avoid trying to free an inaccurate blkptr at delete. 5029 * Note that embedded blkptrs are not tracked in livelists. 5030 */ 5031 if (dn->dn_objset != spa_meta_objset(spa)) { 5032 dsl_dataset_t *ds = dmu_objset_ds(dn->dn_objset); 5033 if (dsl_deadlist_is_open(&ds->ds_dir->dd_livelist) && 5034 bp->blk_birth > ds->ds_dir->dd_origin_txg) { 5035 ASSERT(!BP_IS_EMBEDDED(bp)); 5036 ASSERT(dsl_dir_is_clone(ds->ds_dir)); 5037 ASSERT(spa_feature_is_enabled(spa, 5038 SPA_FEATURE_LIVELIST)); 5039 bplist_append(&ds->ds_dir->dd_pending_frees, 5040 bp); 5041 bplist_append(&ds->ds_dir->dd_pending_allocs, 5042 &bp_copy); 5043 } 5044 } 5045 5046 /* 5047 * The db_rwlock prevents dbuf_read_impl() from 5048 * dereferencing the BP while we are changing it. To 5049 * avoid lock contention, only grab it when we are actually 5050 * changing the BP. 5051 */ 5052 if (rw != NULL) 5053 rw_enter(rw, RW_WRITER); 5054 *bp = bp_copy; 5055 if (rw != NULL) 5056 rw_exit(rw); 5057 } 5058 } 5059 5060 /* 5061 * Remap any existing BP's to concrete vdevs, if possible. 5062 */ 5063 static void 5064 dbuf_remap(dnode_t *dn, dmu_buf_impl_t *db, dmu_tx_t *tx) 5065 { 5066 spa_t *spa = dmu_objset_spa(db->db_objset); 5067 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa))); 5068 5069 if (!spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL)) 5070 return; 5071 5072 if (db->db_level > 0) { 5073 blkptr_t *bp = db->db.db_data; 5074 for (int i = 0; i < db->db.db_size >> SPA_BLKPTRSHIFT; i++) { 5075 dbuf_remap_impl(dn, &bp[i], &db->db_rwlock, tx); 5076 } 5077 } else if (db->db.db_object == DMU_META_DNODE_OBJECT) { 5078 dnode_phys_t *dnp = db->db.db_data; 5079 ASSERT3U(db->db_dnode_handle->dnh_dnode->dn_type, ==, 5080 DMU_OT_DNODE); 5081 for (int i = 0; i < db->db.db_size >> DNODE_SHIFT; 5082 i += dnp[i].dn_extra_slots + 1) { 5083 for (int j = 0; j < dnp[i].dn_nblkptr; j++) { 5084 krwlock_t *lock = (dn->dn_dbuf == NULL ? NULL : 5085 &dn->dn_dbuf->db_rwlock); 5086 dbuf_remap_impl(dn, &dnp[i].dn_blkptr[j], lock, 5087 tx); 5088 } 5089 } 5090 } 5091 } 5092 5093 5094 /* 5095 * Populate dr->dr_zio with a zio to commit a dirty buffer to disk. 5096 * Caller is responsible for issuing the zio_[no]wait(dr->dr_zio). 5097 */ 5098 static void 5099 dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx) 5100 { 5101 dmu_buf_impl_t *db = dr->dr_dbuf; 5102 dnode_t *dn = dr->dr_dnode; 5103 objset_t *os; 5104 dmu_buf_impl_t *parent = db->db_parent; 5105 uint64_t txg = tx->tx_txg; 5106 zbookmark_phys_t zb; 5107 zio_prop_t zp; 5108 zio_t *pio; /* parent I/O */ 5109 int wp_flag = 0; 5110 5111 ASSERT(dmu_tx_is_syncing(tx)); 5112 5113 os = dn->dn_objset; 5114 5115 if (db->db_state != DB_NOFILL) { 5116 if (db->db_level > 0 || dn->dn_type == DMU_OT_DNODE) { 5117 /* 5118 * Private object buffers are released here rather 5119 * than in dbuf_dirty() since they are only modified 5120 * in the syncing context and we don't want the 5121 * overhead of making multiple copies of the data. 5122 */ 5123 if (BP_IS_HOLE(db->db_blkptr)) { 5124 arc_buf_thaw(data); 5125 } else { 5126 dbuf_release_bp(db); 5127 } 5128 dbuf_remap(dn, db, tx); 5129 } 5130 } 5131 5132 if (parent != dn->dn_dbuf) { 5133 /* Our parent is an indirect block. */ 5134 /* We have a dirty parent that has been scheduled for write. */ 5135 ASSERT(parent && parent->db_data_pending); 5136 /* Our parent's buffer is one level closer to the dnode. */ 5137 ASSERT(db->db_level == parent->db_level-1); 5138 /* 5139 * We're about to modify our parent's db_data by modifying 5140 * our block pointer, so the parent must be released. 5141 */ 5142 ASSERT(arc_released(parent->db_buf)); 5143 pio = parent->db_data_pending->dr_zio; 5144 } else { 5145 /* Our parent is the dnode itself. */ 5146 ASSERT((db->db_level == dn->dn_phys->dn_nlevels-1 && 5147 db->db_blkid != DMU_SPILL_BLKID) || 5148 (db->db_blkid == DMU_SPILL_BLKID && db->db_level == 0)); 5149 if (db->db_blkid != DMU_SPILL_BLKID) 5150 ASSERT3P(db->db_blkptr, ==, 5151 &dn->dn_phys->dn_blkptr[db->db_blkid]); 5152 pio = dn->dn_zio; 5153 } 5154 5155 ASSERT(db->db_level == 0 || data == db->db_buf); 5156 ASSERT3U(db->db_blkptr->blk_birth, <=, txg); 5157 ASSERT(pio); 5158 5159 SET_BOOKMARK(&zb, os->os_dsl_dataset ? 5160 os->os_dsl_dataset->ds_object : DMU_META_OBJSET, 5161 db->db.db_object, db->db_level, db->db_blkid); 5162 5163 if (db->db_blkid == DMU_SPILL_BLKID) 5164 wp_flag = WP_SPILL; 5165 wp_flag |= (db->db_state == DB_NOFILL) ? WP_NOFILL : 0; 5166 5167 dmu_write_policy(os, dn, db->db_level, wp_flag, &zp); 5168 5169 /* 5170 * We copy the blkptr now (rather than when we instantiate the dirty 5171 * record), because its value can change between open context and 5172 * syncing context. We do not need to hold dn_struct_rwlock to read 5173 * db_blkptr because we are in syncing context. 5174 */ 5175 dr->dr_bp_copy = *db->db_blkptr; 5176 5177 if (db->db_level == 0 && 5178 dr->dt.dl.dr_override_state == DR_OVERRIDDEN) { 5179 /* 5180 * The BP for this block has been provided by open context 5181 * (by dmu_sync() or dmu_buf_write_embedded()). 5182 */ 5183 abd_t *contents = (data != NULL) ? 5184 abd_get_from_buf(data->b_data, arc_buf_size(data)) : NULL; 5185 5186 dr->dr_zio = zio_write(pio, os->os_spa, txg, &dr->dr_bp_copy, 5187 contents, db->db.db_size, db->db.db_size, &zp, 5188 dbuf_write_override_ready, NULL, 5189 dbuf_write_override_done, 5190 dr, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb); 5191 mutex_enter(&db->db_mtx); 5192 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN; 5193 zio_write_override(dr->dr_zio, &dr->dt.dl.dr_overridden_by, 5194 dr->dt.dl.dr_copies, dr->dt.dl.dr_nopwrite, 5195 dr->dt.dl.dr_brtwrite); 5196 mutex_exit(&db->db_mtx); 5197 } else if (db->db_state == DB_NOFILL) { 5198 ASSERT(zp.zp_checksum == ZIO_CHECKSUM_OFF || 5199 zp.zp_checksum == ZIO_CHECKSUM_NOPARITY); 5200 dr->dr_zio = zio_write(pio, os->os_spa, txg, 5201 &dr->dr_bp_copy, NULL, db->db.db_size, db->db.db_size, &zp, 5202 dbuf_write_nofill_ready, NULL, 5203 dbuf_write_nofill_done, db, 5204 ZIO_PRIORITY_ASYNC_WRITE, 5205 ZIO_FLAG_MUSTSUCCEED | ZIO_FLAG_NODATA, &zb); 5206 } else { 5207 ASSERT(arc_released(data)); 5208 5209 /* 5210 * For indirect blocks, we want to setup the children 5211 * ready callback so that we can properly handle an indirect 5212 * block that only contains holes. 5213 */ 5214 arc_write_done_func_t *children_ready_cb = NULL; 5215 if (db->db_level != 0) 5216 children_ready_cb = dbuf_write_children_ready; 5217 5218 dr->dr_zio = arc_write(pio, os->os_spa, txg, 5219 &dr->dr_bp_copy, data, !DBUF_IS_CACHEABLE(db), 5220 dbuf_is_l2cacheable(db), &zp, dbuf_write_ready, 5221 children_ready_cb, dbuf_write_done, db, 5222 ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb); 5223 } 5224 } 5225 5226 EXPORT_SYMBOL(dbuf_find); 5227 EXPORT_SYMBOL(dbuf_is_metadata); 5228 EXPORT_SYMBOL(dbuf_destroy); 5229 EXPORT_SYMBOL(dbuf_loan_arcbuf); 5230 EXPORT_SYMBOL(dbuf_whichblock); 5231 EXPORT_SYMBOL(dbuf_read); 5232 EXPORT_SYMBOL(dbuf_unoverride); 5233 EXPORT_SYMBOL(dbuf_free_range); 5234 EXPORT_SYMBOL(dbuf_new_size); 5235 EXPORT_SYMBOL(dbuf_release_bp); 5236 EXPORT_SYMBOL(dbuf_dirty); 5237 EXPORT_SYMBOL(dmu_buf_set_crypt_params); 5238 EXPORT_SYMBOL(dmu_buf_will_dirty); 5239 EXPORT_SYMBOL(dmu_buf_is_dirty); 5240 EXPORT_SYMBOL(dmu_buf_will_clone); 5241 EXPORT_SYMBOL(dmu_buf_will_not_fill); 5242 EXPORT_SYMBOL(dmu_buf_will_fill); 5243 EXPORT_SYMBOL(dmu_buf_fill_done); 5244 EXPORT_SYMBOL(dmu_buf_rele); 5245 EXPORT_SYMBOL(dbuf_assign_arcbuf); 5246 EXPORT_SYMBOL(dbuf_prefetch); 5247 EXPORT_SYMBOL(dbuf_hold_impl); 5248 EXPORT_SYMBOL(dbuf_hold); 5249 EXPORT_SYMBOL(dbuf_hold_level); 5250 EXPORT_SYMBOL(dbuf_create_bonus); 5251 EXPORT_SYMBOL(dbuf_spill_set_blksz); 5252 EXPORT_SYMBOL(dbuf_rm_spill); 5253 EXPORT_SYMBOL(dbuf_add_ref); 5254 EXPORT_SYMBOL(dbuf_rele); 5255 EXPORT_SYMBOL(dbuf_rele_and_unlock); 5256 EXPORT_SYMBOL(dbuf_refcount); 5257 EXPORT_SYMBOL(dbuf_sync_list); 5258 EXPORT_SYMBOL(dmu_buf_set_user); 5259 EXPORT_SYMBOL(dmu_buf_set_user_ie); 5260 EXPORT_SYMBOL(dmu_buf_get_user); 5261 EXPORT_SYMBOL(dmu_buf_get_blkptr); 5262 5263 ZFS_MODULE_PARAM(zfs_dbuf_cache, dbuf_cache_, max_bytes, U64, ZMOD_RW, 5264 "Maximum size in bytes of the dbuf cache."); 5265 5266 ZFS_MODULE_PARAM(zfs_dbuf_cache, dbuf_cache_, hiwater_pct, UINT, ZMOD_RW, 5267 "Percentage over dbuf_cache_max_bytes for direct dbuf eviction."); 5268 5269 ZFS_MODULE_PARAM(zfs_dbuf_cache, dbuf_cache_, lowater_pct, UINT, ZMOD_RW, 5270 "Percentage below dbuf_cache_max_bytes when dbuf eviction stops."); 5271 5272 ZFS_MODULE_PARAM(zfs_dbuf, dbuf_, metadata_cache_max_bytes, U64, ZMOD_RW, 5273 "Maximum size in bytes of dbuf metadata cache."); 5274 5275 ZFS_MODULE_PARAM(zfs_dbuf, dbuf_, cache_shift, UINT, ZMOD_RW, 5276 "Set size of dbuf cache to log2 fraction of arc size."); 5277 5278 ZFS_MODULE_PARAM(zfs_dbuf, dbuf_, metadata_cache_shift, UINT, ZMOD_RW, 5279 "Set size of dbuf metadata cache to log2 fraction of arc size."); 5280 5281 ZFS_MODULE_PARAM(zfs_dbuf, dbuf_, mutex_cache_shift, UINT, ZMOD_RD, 5282 "Set size of dbuf cache mutex array as log2 shift."); 5283