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