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.
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.
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]
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) 2014 Spectra Logic Corporation, All rights reserved.
27 * Copyright (c) 2019, Klara Inc.
28 * Copyright (c) 2019, Allan Jude
31 #include <sys/zfs_context.h>
34 #include <sys/dmu_send.h>
35 #include <sys/dmu_impl.h>
37 #include <sys/dmu_objset.h>
38 #include <sys/dsl_dataset.h>
39 #include <sys/dsl_dir.h>
40 #include <sys/dmu_tx.h>
43 #include <sys/dmu_zfetch.h>
45 #include <sys/sa_impl.h>
46 #include <sys/zfeature.h>
47 #include <sys/blkptr.h>
48 #include <sys/range_tree.h>
49 #include <sys/trace_zfs.h>
50 #include <sys/callb.h>
54 #include <sys/spa_impl.h>
58 typedef struct dbuf_stats {
60 * Various statistics about the size of the dbuf cache.
62 kstat_named_t cache_count;
63 kstat_named_t cache_size_bytes;
64 kstat_named_t cache_size_bytes_max;
66 * Statistics regarding the bounds on the dbuf cache size.
68 kstat_named_t cache_target_bytes;
69 kstat_named_t cache_lowater_bytes;
70 kstat_named_t cache_hiwater_bytes;
72 * Total number of dbuf cache evictions that have occurred.
74 kstat_named_t cache_total_evicts;
76 * The distribution of dbuf levels in the dbuf cache and
77 * the total size of all dbufs at each level.
79 kstat_named_t cache_levels[DN_MAX_LEVELS];
80 kstat_named_t cache_levels_bytes[DN_MAX_LEVELS];
82 * Statistics about the dbuf hash table.
84 kstat_named_t hash_hits;
85 kstat_named_t hash_misses;
86 kstat_named_t hash_collisions;
87 kstat_named_t hash_elements;
88 kstat_named_t hash_elements_max;
90 * Number of sublists containing more than one dbuf in the dbuf
91 * hash table. Keep track of the longest hash chain.
93 kstat_named_t hash_chains;
94 kstat_named_t hash_chain_max;
96 * Number of times a dbuf_create() discovers that a dbuf was
97 * already created and in the dbuf hash table.
99 kstat_named_t hash_insert_race;
101 * Statistics about the size of the metadata dbuf cache.
103 kstat_named_t metadata_cache_count;
104 kstat_named_t metadata_cache_size_bytes;
105 kstat_named_t metadata_cache_size_bytes_max;
107 * For diagnostic purposes, this is incremented whenever we can't add
108 * something to the metadata cache because it's full, and instead put
109 * the data in the regular dbuf cache.
111 kstat_named_t metadata_cache_overflow;
114 dbuf_stats_t dbuf_stats = {
115 { "cache_count", KSTAT_DATA_UINT64 },
116 { "cache_size_bytes", KSTAT_DATA_UINT64 },
117 { "cache_size_bytes_max", KSTAT_DATA_UINT64 },
118 { "cache_target_bytes", KSTAT_DATA_UINT64 },
119 { "cache_lowater_bytes", KSTAT_DATA_UINT64 },
120 { "cache_hiwater_bytes", KSTAT_DATA_UINT64 },
121 { "cache_total_evicts", KSTAT_DATA_UINT64 },
122 { { "cache_levels_N", KSTAT_DATA_UINT64 } },
123 { { "cache_levels_bytes_N", KSTAT_DATA_UINT64 } },
124 { "hash_hits", KSTAT_DATA_UINT64 },
125 { "hash_misses", KSTAT_DATA_UINT64 },
126 { "hash_collisions", KSTAT_DATA_UINT64 },
127 { "hash_elements", KSTAT_DATA_UINT64 },
128 { "hash_elements_max", KSTAT_DATA_UINT64 },
129 { "hash_chains", KSTAT_DATA_UINT64 },
130 { "hash_chain_max", KSTAT_DATA_UINT64 },
131 { "hash_insert_race", KSTAT_DATA_UINT64 },
132 { "metadata_cache_count", KSTAT_DATA_UINT64 },
133 { "metadata_cache_size_bytes", KSTAT_DATA_UINT64 },
134 { "metadata_cache_size_bytes_max", KSTAT_DATA_UINT64 },
135 { "metadata_cache_overflow", KSTAT_DATA_UINT64 }
138 #define DBUF_STAT_INCR(stat, val) \
139 atomic_add_64(&dbuf_stats.stat.value.ui64, (val));
140 #define DBUF_STAT_DECR(stat, val) \
141 DBUF_STAT_INCR(stat, -(val));
142 #define DBUF_STAT_BUMP(stat) \
143 DBUF_STAT_INCR(stat, 1);
144 #define DBUF_STAT_BUMPDOWN(stat) \
145 DBUF_STAT_INCR(stat, -1);
146 #define DBUF_STAT_MAX(stat, v) { \
148 while ((v) > (_m = dbuf_stats.stat.value.ui64) && \
149 (_m != atomic_cas_64(&dbuf_stats.stat.value.ui64, _m, (v))))\
153 static boolean_t dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx);
154 static void dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx);
155 static void dbuf_sync_leaf_verify_bonus_dnode(dbuf_dirty_record_t *dr);
156 static int dbuf_read_verify_dnode_crypt(dmu_buf_impl_t *db, uint32_t flags);
158 extern inline void dmu_buf_init_user(dmu_buf_user_t *dbu,
159 dmu_buf_evict_func_t *evict_func_sync,
160 dmu_buf_evict_func_t *evict_func_async,
161 dmu_buf_t **clear_on_evict_dbufp);
164 * Global data structures and functions for the dbuf cache.
166 static kmem_cache_t *dbuf_kmem_cache;
167 static taskq_t *dbu_evict_taskq;
169 static kthread_t *dbuf_cache_evict_thread;
170 static kmutex_t dbuf_evict_lock;
171 static kcondvar_t dbuf_evict_cv;
172 static boolean_t dbuf_evict_thread_exit;
175 * There are two dbuf caches; each dbuf can only be in one of them at a time.
177 * 1. Cache of metadata dbufs, to help make read-heavy administrative commands
178 * from /sbin/zfs run faster. The "metadata cache" specifically stores dbufs
179 * that represent the metadata that describes filesystems/snapshots/
180 * bookmarks/properties/etc. We only evict from this cache when we export a
181 * pool, to short-circuit as much I/O as possible for all administrative
182 * commands that need the metadata. There is no eviction policy for this
183 * cache, because we try to only include types in it which would occupy a
184 * very small amount of space per object but create a large impact on the
185 * performance of these commands. Instead, after it reaches a maximum size
186 * (which should only happen on very small memory systems with a very large
187 * number of filesystem objects), we stop taking new dbufs into the
188 * metadata cache, instead putting them in the normal dbuf cache.
190 * 2. LRU cache of dbufs. The dbuf cache maintains a list of dbufs that
191 * are not currently held but have been recently released. These dbufs
192 * are not eligible for arc eviction until they are aged out of the cache.
193 * Dbufs that are aged out of the cache will be immediately destroyed and
194 * become eligible for arc eviction.
196 * Dbufs are added to these caches once the last hold is released. If a dbuf is
197 * later accessed and still exists in the dbuf cache, then it will be removed
198 * from the cache and later re-added to the head of the cache.
200 * If a given dbuf meets the requirements for the metadata cache, it will go
201 * there, otherwise it will be considered for the generic LRU dbuf cache. The
202 * caches and the refcounts tracking their sizes are stored in an array indexed
203 * by those caches' matching enum values (from dbuf_cached_state_t).
205 typedef struct dbuf_cache {
209 dbuf_cache_t dbuf_caches[DB_CACHE_MAX];
211 /* Size limits for the caches */
212 unsigned long dbuf_cache_max_bytes = ULONG_MAX;
213 unsigned long dbuf_metadata_cache_max_bytes = ULONG_MAX;
215 /* Set the default sizes of the caches to log2 fraction of arc size */
216 int dbuf_cache_shift = 5;
217 int dbuf_metadata_cache_shift = 6;
219 static unsigned long dbuf_cache_target_bytes(void);
220 static unsigned long dbuf_metadata_cache_target_bytes(void);
223 * The LRU dbuf cache uses a three-stage eviction policy:
224 * - A low water marker designates when the dbuf eviction thread
225 * should stop evicting from the dbuf cache.
226 * - When we reach the maximum size (aka mid water mark), we
227 * signal the eviction thread to run.
228 * - The high water mark indicates when the eviction thread
229 * is unable to keep up with the incoming load and eviction must
230 * happen in the context of the calling thread.
234 * low water mid water hi water
235 * +----------------------------------------+----------+----------+
240 * +----------------------------------------+----------+----------+
242 * evicting eviction directly
245 * The high and low water marks indicate the operating range for the eviction
246 * thread. The low water mark is, by default, 90% of the total size of the
247 * cache and the high water mark is at 110% (both of these percentages can be
248 * changed by setting dbuf_cache_lowater_pct and dbuf_cache_hiwater_pct,
249 * respectively). The eviction thread will try to ensure that the cache remains
250 * within this range by waking up every second and checking if the cache is
251 * above the low water mark. The thread can also be woken up by callers adding
252 * elements into the cache if the cache is larger than the mid water (i.e max
253 * cache size). Once the eviction thread is woken up and eviction is required,
254 * it will continue evicting buffers until it's able to reduce the cache size
255 * to the low water mark. If the cache size continues to grow and hits the high
256 * water mark, then callers adding elements to the cache will begin to evict
257 * directly from the cache until the cache is no longer above the high water
262 * The percentage above and below the maximum cache size.
264 uint_t dbuf_cache_hiwater_pct = 10;
265 uint_t dbuf_cache_lowater_pct = 10;
269 dbuf_cons(void *vdb, void *unused, int kmflag)
271 dmu_buf_impl_t *db = vdb;
272 bzero(db, sizeof (dmu_buf_impl_t));
274 mutex_init(&db->db_mtx, NULL, MUTEX_DEFAULT, NULL);
275 rw_init(&db->db_rwlock, NULL, RW_DEFAULT, NULL);
276 cv_init(&db->db_changed, NULL, CV_DEFAULT, NULL);
277 multilist_link_init(&db->db_cache_link);
278 zfs_refcount_create(&db->db_holds);
285 dbuf_dest(void *vdb, void *unused)
287 dmu_buf_impl_t *db = vdb;
288 mutex_destroy(&db->db_mtx);
289 rw_destroy(&db->db_rwlock);
290 cv_destroy(&db->db_changed);
291 ASSERT(!multilist_link_active(&db->db_cache_link));
292 zfs_refcount_destroy(&db->db_holds);
296 * dbuf hash table routines
298 static dbuf_hash_table_t dbuf_hash_table;
300 static uint64_t dbuf_hash_count;
303 * We use Cityhash for this. It's fast, and has good hash properties without
304 * requiring any large static buffers.
307 dbuf_hash(void *os, uint64_t obj, uint8_t lvl, uint64_t blkid)
309 return (cityhash4((uintptr_t)os, obj, (uint64_t)lvl, blkid));
312 #define DTRACE_SET_STATE(db, why) \
313 DTRACE_PROBE2(dbuf__state_change, dmu_buf_impl_t *, db, \
316 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
317 ((dbuf)->db.db_object == (obj) && \
318 (dbuf)->db_objset == (os) && \
319 (dbuf)->db_level == (level) && \
320 (dbuf)->db_blkid == (blkid))
323 dbuf_find(objset_t *os, uint64_t obj, uint8_t level, uint64_t blkid)
325 dbuf_hash_table_t *h = &dbuf_hash_table;
330 hv = dbuf_hash(os, obj, level, blkid);
331 idx = hv & h->hash_table_mask;
333 mutex_enter(DBUF_HASH_MUTEX(h, idx));
334 for (db = h->hash_table[idx]; db != NULL; db = db->db_hash_next) {
335 if (DBUF_EQUAL(db, os, obj, level, blkid)) {
336 mutex_enter(&db->db_mtx);
337 if (db->db_state != DB_EVICTING) {
338 mutex_exit(DBUF_HASH_MUTEX(h, idx));
341 mutex_exit(&db->db_mtx);
344 mutex_exit(DBUF_HASH_MUTEX(h, idx));
348 static dmu_buf_impl_t *
349 dbuf_find_bonus(objset_t *os, uint64_t object)
352 dmu_buf_impl_t *db = NULL;
354 if (dnode_hold(os, object, FTAG, &dn) == 0) {
355 rw_enter(&dn->dn_struct_rwlock, RW_READER);
356 if (dn->dn_bonus != NULL) {
358 mutex_enter(&db->db_mtx);
360 rw_exit(&dn->dn_struct_rwlock);
361 dnode_rele(dn, FTAG);
367 * Insert an entry into the hash table. If there is already an element
368 * equal to elem in the hash table, then the already existing element
369 * will be returned and the new element will not be inserted.
370 * Otherwise returns NULL.
372 static dmu_buf_impl_t *
373 dbuf_hash_insert(dmu_buf_impl_t *db)
375 dbuf_hash_table_t *h = &dbuf_hash_table;
376 objset_t *os = db->db_objset;
377 uint64_t obj = db->db.db_object;
378 int level = db->db_level;
379 uint64_t blkid, hv, idx;
383 blkid = db->db_blkid;
384 hv = dbuf_hash(os, obj, level, blkid);
385 idx = hv & h->hash_table_mask;
387 mutex_enter(DBUF_HASH_MUTEX(h, idx));
388 for (dbf = h->hash_table[idx], i = 0; dbf != NULL;
389 dbf = dbf->db_hash_next, i++) {
390 if (DBUF_EQUAL(dbf, os, obj, level, blkid)) {
391 mutex_enter(&dbf->db_mtx);
392 if (dbf->db_state != DB_EVICTING) {
393 mutex_exit(DBUF_HASH_MUTEX(h, idx));
396 mutex_exit(&dbf->db_mtx);
401 DBUF_STAT_BUMP(hash_collisions);
403 DBUF_STAT_BUMP(hash_chains);
405 DBUF_STAT_MAX(hash_chain_max, i);
408 mutex_enter(&db->db_mtx);
409 db->db_hash_next = h->hash_table[idx];
410 h->hash_table[idx] = db;
411 mutex_exit(DBUF_HASH_MUTEX(h, idx));
412 atomic_inc_64(&dbuf_hash_count);
413 DBUF_STAT_MAX(hash_elements_max, dbuf_hash_count);
419 * This returns whether this dbuf should be stored in the metadata cache, which
420 * is based on whether it's from one of the dnode types that store data related
421 * to traversing dataset hierarchies.
424 dbuf_include_in_metadata_cache(dmu_buf_impl_t *db)
427 dmu_object_type_t type = DB_DNODE(db)->dn_type;
430 /* Check if this dbuf is one of the types we care about */
431 if (DMU_OT_IS_METADATA_CACHED(type)) {
432 /* If we hit this, then we set something up wrong in dmu_ot */
433 ASSERT(DMU_OT_IS_METADATA(type));
436 * Sanity check for small-memory systems: don't allocate too
437 * much memory for this purpose.
439 if (zfs_refcount_count(
440 &dbuf_caches[DB_DBUF_METADATA_CACHE].size) >
441 dbuf_metadata_cache_target_bytes()) {
442 DBUF_STAT_BUMP(metadata_cache_overflow);
453 * Remove an entry from the hash table. It must be in the EVICTING state.
456 dbuf_hash_remove(dmu_buf_impl_t *db)
458 dbuf_hash_table_t *h = &dbuf_hash_table;
460 dmu_buf_impl_t *dbf, **dbp;
462 hv = dbuf_hash(db->db_objset, db->db.db_object,
463 db->db_level, db->db_blkid);
464 idx = hv & h->hash_table_mask;
467 * We mustn't hold db_mtx to maintain lock ordering:
468 * DBUF_HASH_MUTEX > db_mtx.
470 ASSERT(zfs_refcount_is_zero(&db->db_holds));
471 ASSERT(db->db_state == DB_EVICTING);
472 ASSERT(!MUTEX_HELD(&db->db_mtx));
474 mutex_enter(DBUF_HASH_MUTEX(h, idx));
475 dbp = &h->hash_table[idx];
476 while ((dbf = *dbp) != db) {
477 dbp = &dbf->db_hash_next;
480 *dbp = db->db_hash_next;
481 db->db_hash_next = NULL;
482 if (h->hash_table[idx] &&
483 h->hash_table[idx]->db_hash_next == NULL)
484 DBUF_STAT_BUMPDOWN(hash_chains);
485 mutex_exit(DBUF_HASH_MUTEX(h, idx));
486 atomic_dec_64(&dbuf_hash_count);
492 } dbvu_verify_type_t;
495 dbuf_verify_user(dmu_buf_impl_t *db, dbvu_verify_type_t verify_type)
500 if (db->db_user == NULL)
503 /* Only data blocks support the attachment of user data. */
504 ASSERT(db->db_level == 0);
506 /* Clients must resolve a dbuf before attaching user data. */
507 ASSERT(db->db.db_data != NULL);
508 ASSERT3U(db->db_state, ==, DB_CACHED);
510 holds = zfs_refcount_count(&db->db_holds);
511 if (verify_type == DBVU_EVICTING) {
513 * Immediate eviction occurs when holds == dirtycnt.
514 * For normal eviction buffers, holds is zero on
515 * eviction, except when dbuf_fix_old_data() calls
516 * dbuf_clear_data(). However, the hold count can grow
517 * during eviction even though db_mtx is held (see
518 * dmu_bonus_hold() for an example), so we can only
519 * test the generic invariant that holds >= dirtycnt.
521 ASSERT3U(holds, >=, db->db_dirtycnt);
523 if (db->db_user_immediate_evict == TRUE)
524 ASSERT3U(holds, >=, db->db_dirtycnt);
526 ASSERT3U(holds, >, 0);
532 dbuf_evict_user(dmu_buf_impl_t *db)
534 dmu_buf_user_t *dbu = db->db_user;
536 ASSERT(MUTEX_HELD(&db->db_mtx));
541 dbuf_verify_user(db, DBVU_EVICTING);
545 if (dbu->dbu_clear_on_evict_dbufp != NULL)
546 *dbu->dbu_clear_on_evict_dbufp = NULL;
550 * There are two eviction callbacks - one that we call synchronously
551 * and one that we invoke via a taskq. The async one is useful for
552 * avoiding lock order reversals and limiting stack depth.
554 * Note that if we have a sync callback but no async callback,
555 * it's likely that the sync callback will free the structure
556 * containing the dbu. In that case we need to take care to not
557 * dereference dbu after calling the sync evict func.
559 boolean_t has_async = (dbu->dbu_evict_func_async != NULL);
561 if (dbu->dbu_evict_func_sync != NULL)
562 dbu->dbu_evict_func_sync(dbu);
565 taskq_dispatch_ent(dbu_evict_taskq, dbu->dbu_evict_func_async,
566 dbu, 0, &dbu->dbu_tqent);
571 dbuf_is_metadata(dmu_buf_impl_t *db)
574 * Consider indirect blocks and spill blocks to be meta data.
576 if (db->db_level > 0 || db->db_blkid == DMU_SPILL_BLKID) {
579 boolean_t is_metadata;
582 is_metadata = DMU_OT_IS_METADATA(DB_DNODE(db)->dn_type);
585 return (is_metadata);
591 * This function *must* return indices evenly distributed between all
592 * sublists of the multilist. This is needed due to how the dbuf eviction
593 * code is laid out; dbuf_evict_thread() assumes dbufs are evenly
594 * distributed between all sublists and uses this assumption when
595 * deciding which sublist to evict from and how much to evict from it.
598 dbuf_cache_multilist_index_func(multilist_t *ml, void *obj)
600 dmu_buf_impl_t *db = obj;
603 * The assumption here, is the hash value for a given
604 * dmu_buf_impl_t will remain constant throughout it's lifetime
605 * (i.e. it's objset, object, level and blkid fields don't change).
606 * Thus, we don't need to store the dbuf's sublist index
607 * on insertion, as this index can be recalculated on removal.
609 * Also, the low order bits of the hash value are thought to be
610 * distributed evenly. Otherwise, in the case that the multilist
611 * has a power of two number of sublists, each sublists' usage
612 * would not be evenly distributed.
614 return (dbuf_hash(db->db_objset, db->db.db_object,
615 db->db_level, db->db_blkid) %
616 multilist_get_num_sublists(ml));
620 * The target size of the dbuf cache can grow with the ARC target,
621 * unless limited by the tunable dbuf_cache_max_bytes.
623 static inline unsigned long
624 dbuf_cache_target_bytes(void)
626 return (MIN(dbuf_cache_max_bytes,
627 arc_target_bytes() >> dbuf_cache_shift));
631 * The target size of the dbuf metadata cache can grow with the ARC target,
632 * unless limited by the tunable dbuf_metadata_cache_max_bytes.
634 static inline unsigned long
635 dbuf_metadata_cache_target_bytes(void)
637 return (MIN(dbuf_metadata_cache_max_bytes,
638 arc_target_bytes() >> dbuf_metadata_cache_shift));
641 static inline uint64_t
642 dbuf_cache_hiwater_bytes(void)
644 uint64_t dbuf_cache_target = dbuf_cache_target_bytes();
645 return (dbuf_cache_target +
646 (dbuf_cache_target * dbuf_cache_hiwater_pct) / 100);
649 static inline uint64_t
650 dbuf_cache_lowater_bytes(void)
652 uint64_t dbuf_cache_target = dbuf_cache_target_bytes();
653 return (dbuf_cache_target -
654 (dbuf_cache_target * dbuf_cache_lowater_pct) / 100);
657 static inline boolean_t
658 dbuf_cache_above_lowater(void)
660 return (zfs_refcount_count(&dbuf_caches[DB_DBUF_CACHE].size) >
661 dbuf_cache_lowater_bytes());
665 * Evict the oldest eligible dbuf from the dbuf cache.
670 int idx = multilist_get_random_index(dbuf_caches[DB_DBUF_CACHE].cache);
671 multilist_sublist_t *mls = multilist_sublist_lock(
672 dbuf_caches[DB_DBUF_CACHE].cache, idx);
674 ASSERT(!MUTEX_HELD(&dbuf_evict_lock));
676 dmu_buf_impl_t *db = multilist_sublist_tail(mls);
677 while (db != NULL && mutex_tryenter(&db->db_mtx) == 0) {
678 db = multilist_sublist_prev(mls, db);
681 DTRACE_PROBE2(dbuf__evict__one, dmu_buf_impl_t *, db,
682 multilist_sublist_t *, mls);
685 multilist_sublist_remove(mls, db);
686 multilist_sublist_unlock(mls);
687 (void) zfs_refcount_remove_many(
688 &dbuf_caches[DB_DBUF_CACHE].size, db->db.db_size, db);
689 DBUF_STAT_BUMPDOWN(cache_levels[db->db_level]);
690 DBUF_STAT_BUMPDOWN(cache_count);
691 DBUF_STAT_DECR(cache_levels_bytes[db->db_level],
693 ASSERT3U(db->db_caching_status, ==, DB_DBUF_CACHE);
694 db->db_caching_status = DB_NO_CACHE;
696 DBUF_STAT_BUMP(cache_total_evicts);
698 multilist_sublist_unlock(mls);
703 * The dbuf evict thread is responsible for aging out dbufs from the
704 * cache. Once the cache has reached it's maximum size, dbufs are removed
705 * and destroyed. The eviction thread will continue running until the size
706 * of the dbuf cache is at or below the maximum size. Once the dbuf is aged
707 * out of the cache it is destroyed and becomes eligible for arc eviction.
711 dbuf_evict_thread(void *unused)
715 CALLB_CPR_INIT(&cpr, &dbuf_evict_lock, callb_generic_cpr, FTAG);
717 mutex_enter(&dbuf_evict_lock);
718 while (!dbuf_evict_thread_exit) {
719 while (!dbuf_cache_above_lowater() && !dbuf_evict_thread_exit) {
720 CALLB_CPR_SAFE_BEGIN(&cpr);
721 (void) cv_timedwait_idle_hires(&dbuf_evict_cv,
722 &dbuf_evict_lock, SEC2NSEC(1), MSEC2NSEC(1), 0);
723 CALLB_CPR_SAFE_END(&cpr, &dbuf_evict_lock);
725 mutex_exit(&dbuf_evict_lock);
728 * Keep evicting as long as we're above the low water mark
729 * for the cache. We do this without holding the locks to
730 * minimize lock contention.
732 while (dbuf_cache_above_lowater() && !dbuf_evict_thread_exit) {
736 mutex_enter(&dbuf_evict_lock);
739 dbuf_evict_thread_exit = B_FALSE;
740 cv_broadcast(&dbuf_evict_cv);
741 CALLB_CPR_EXIT(&cpr); /* drops dbuf_evict_lock */
746 * Wake up the dbuf eviction thread if the dbuf cache is at its max size.
747 * If the dbuf cache is at its high water mark, then evict a dbuf from the
748 * dbuf cache using the callers context.
751 dbuf_evict_notify(uint64_t size)
754 * We check if we should evict without holding the dbuf_evict_lock,
755 * because it's OK to occasionally make the wrong decision here,
756 * and grabbing the lock results in massive lock contention.
758 if (size > dbuf_cache_target_bytes()) {
759 if (size > dbuf_cache_hiwater_bytes())
761 cv_signal(&dbuf_evict_cv);
766 dbuf_kstat_update(kstat_t *ksp, int rw)
768 dbuf_stats_t *ds = ksp->ks_data;
770 if (rw == KSTAT_WRITE) {
771 return (SET_ERROR(EACCES));
773 ds->metadata_cache_size_bytes.value.ui64 = zfs_refcount_count(
774 &dbuf_caches[DB_DBUF_METADATA_CACHE].size);
775 ds->cache_size_bytes.value.ui64 =
776 zfs_refcount_count(&dbuf_caches[DB_DBUF_CACHE].size);
777 ds->cache_target_bytes.value.ui64 = dbuf_cache_target_bytes();
778 ds->cache_hiwater_bytes.value.ui64 = dbuf_cache_hiwater_bytes();
779 ds->cache_lowater_bytes.value.ui64 = dbuf_cache_lowater_bytes();
780 ds->hash_elements.value.ui64 = dbuf_hash_count;
789 uint64_t hsize = 1ULL << 16;
790 dbuf_hash_table_t *h = &dbuf_hash_table;
794 * The hash table is big enough to fill all of physical memory
795 * with an average block size of zfs_arc_average_blocksize (default 8K).
796 * By default, the table will take up
797 * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers).
799 while (hsize * zfs_arc_average_blocksize < physmem * PAGESIZE)
803 h->hash_table_mask = hsize - 1;
806 * Large allocations which do not require contiguous pages
807 * should be using vmem_alloc() in the linux kernel
809 h->hash_table = vmem_zalloc(hsize * sizeof (void *), KM_SLEEP);
811 h->hash_table = kmem_zalloc(hsize * sizeof (void *), KM_NOSLEEP);
813 if (h->hash_table == NULL) {
814 /* XXX - we should really return an error instead of assert */
815 ASSERT(hsize > (1ULL << 10));
820 dbuf_kmem_cache = kmem_cache_create("dmu_buf_impl_t",
821 sizeof (dmu_buf_impl_t),
822 0, dbuf_cons, dbuf_dest, NULL, NULL, NULL, 0);
824 for (i = 0; i < DBUF_MUTEXES; i++)
825 mutex_init(&h->hash_mutexes[i], NULL, MUTEX_DEFAULT, NULL);
830 * All entries are queued via taskq_dispatch_ent(), so min/maxalloc
831 * configuration is not required.
833 dbu_evict_taskq = taskq_create("dbu_evict", 1, defclsyspri, 0, 0, 0);
835 for (dbuf_cached_state_t dcs = 0; dcs < DB_CACHE_MAX; dcs++) {
836 dbuf_caches[dcs].cache =
837 multilist_create(sizeof (dmu_buf_impl_t),
838 offsetof(dmu_buf_impl_t, db_cache_link),
839 dbuf_cache_multilist_index_func);
840 zfs_refcount_create(&dbuf_caches[dcs].size);
843 dbuf_evict_thread_exit = B_FALSE;
844 mutex_init(&dbuf_evict_lock, NULL, MUTEX_DEFAULT, NULL);
845 cv_init(&dbuf_evict_cv, NULL, CV_DEFAULT, NULL);
846 dbuf_cache_evict_thread = thread_create(NULL, 0, dbuf_evict_thread,
847 NULL, 0, &p0, TS_RUN, minclsyspri);
849 dbuf_ksp = kstat_create("zfs", 0, "dbufstats", "misc",
850 KSTAT_TYPE_NAMED, sizeof (dbuf_stats) / sizeof (kstat_named_t),
852 if (dbuf_ksp != NULL) {
853 for (i = 0; i < DN_MAX_LEVELS; i++) {
854 snprintf(dbuf_stats.cache_levels[i].name,
855 KSTAT_STRLEN, "cache_level_%d", i);
856 dbuf_stats.cache_levels[i].data_type =
858 snprintf(dbuf_stats.cache_levels_bytes[i].name,
859 KSTAT_STRLEN, "cache_level_%d_bytes", i);
860 dbuf_stats.cache_levels_bytes[i].data_type =
863 dbuf_ksp->ks_data = &dbuf_stats;
864 dbuf_ksp->ks_update = dbuf_kstat_update;
865 kstat_install(dbuf_ksp);
872 dbuf_hash_table_t *h = &dbuf_hash_table;
875 dbuf_stats_destroy();
877 for (i = 0; i < DBUF_MUTEXES; i++)
878 mutex_destroy(&h->hash_mutexes[i]);
881 * Large allocations which do not require contiguous pages
882 * should be using vmem_free() in the linux kernel
884 vmem_free(h->hash_table, (h->hash_table_mask + 1) * sizeof (void *));
886 kmem_free(h->hash_table, (h->hash_table_mask + 1) * sizeof (void *));
888 kmem_cache_destroy(dbuf_kmem_cache);
889 taskq_destroy(dbu_evict_taskq);
891 mutex_enter(&dbuf_evict_lock);
892 dbuf_evict_thread_exit = B_TRUE;
893 while (dbuf_evict_thread_exit) {
894 cv_signal(&dbuf_evict_cv);
895 cv_wait(&dbuf_evict_cv, &dbuf_evict_lock);
897 mutex_exit(&dbuf_evict_lock);
899 mutex_destroy(&dbuf_evict_lock);
900 cv_destroy(&dbuf_evict_cv);
902 for (dbuf_cached_state_t dcs = 0; dcs < DB_CACHE_MAX; dcs++) {
903 zfs_refcount_destroy(&dbuf_caches[dcs].size);
904 multilist_destroy(dbuf_caches[dcs].cache);
907 if (dbuf_ksp != NULL) {
908 kstat_delete(dbuf_ksp);
919 dbuf_verify(dmu_buf_impl_t *db)
922 dbuf_dirty_record_t *dr;
925 ASSERT(MUTEX_HELD(&db->db_mtx));
927 if (!(zfs_flags & ZFS_DEBUG_DBUF_VERIFY))
930 ASSERT(db->db_objset != NULL);
934 ASSERT(db->db_parent == NULL);
935 ASSERT(db->db_blkptr == NULL);
937 ASSERT3U(db->db.db_object, ==, dn->dn_object);
938 ASSERT3P(db->db_objset, ==, dn->dn_objset);
939 ASSERT3U(db->db_level, <, dn->dn_nlevels);
940 ASSERT(db->db_blkid == DMU_BONUS_BLKID ||
941 db->db_blkid == DMU_SPILL_BLKID ||
942 !avl_is_empty(&dn->dn_dbufs));
944 if (db->db_blkid == DMU_BONUS_BLKID) {
946 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
947 ASSERT3U(db->db.db_offset, ==, DMU_BONUS_BLKID);
948 } else if (db->db_blkid == DMU_SPILL_BLKID) {
950 ASSERT0(db->db.db_offset);
952 ASSERT3U(db->db.db_offset, ==, db->db_blkid * db->db.db_size);
955 if ((dr = list_head(&db->db_dirty_records)) != NULL) {
956 ASSERT(dr->dr_dbuf == db);
957 txg_prev = dr->dr_txg;
958 for (dr = list_next(&db->db_dirty_records, dr); dr != NULL;
959 dr = list_next(&db->db_dirty_records, dr)) {
960 ASSERT(dr->dr_dbuf == db);
961 ASSERT(txg_prev > dr->dr_txg);
962 txg_prev = dr->dr_txg;
967 * We can't assert that db_size matches dn_datablksz because it
968 * can be momentarily different when another thread is doing
971 if (db->db_level == 0 && db->db.db_object == DMU_META_DNODE_OBJECT) {
972 dr = db->db_data_pending;
974 * It should only be modified in syncing context, so
975 * make sure we only have one copy of the data.
977 ASSERT(dr == NULL || dr->dt.dl.dr_data == db->db_buf);
980 /* verify db->db_blkptr */
982 if (db->db_parent == dn->dn_dbuf) {
983 /* db is pointed to by the dnode */
984 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
985 if (DMU_OBJECT_IS_SPECIAL(db->db.db_object))
986 ASSERT(db->db_parent == NULL);
988 ASSERT(db->db_parent != NULL);
989 if (db->db_blkid != DMU_SPILL_BLKID)
990 ASSERT3P(db->db_blkptr, ==,
991 &dn->dn_phys->dn_blkptr[db->db_blkid]);
993 /* db is pointed to by an indirect block */
994 int epb __maybe_unused = db->db_parent->db.db_size >>
996 ASSERT3U(db->db_parent->db_level, ==, db->db_level+1);
997 ASSERT3U(db->db_parent->db.db_object, ==,
1000 * dnode_grow_indblksz() can make this fail if we don't
1001 * have the parent's rwlock. XXX indblksz no longer
1002 * grows. safe to do this now?
1004 if (RW_LOCK_HELD(&db->db_parent->db_rwlock)) {
1005 ASSERT3P(db->db_blkptr, ==,
1006 ((blkptr_t *)db->db_parent->db.db_data +
1007 db->db_blkid % epb));
1011 if ((db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr)) &&
1012 (db->db_buf == NULL || db->db_buf->b_data) &&
1013 db->db.db_data && db->db_blkid != DMU_BONUS_BLKID &&
1014 db->db_state != DB_FILL && !dn->dn_free_txg) {
1016 * If the blkptr isn't set but they have nonzero data,
1017 * it had better be dirty, otherwise we'll lose that
1018 * data when we evict this buffer.
1020 * There is an exception to this rule for indirect blocks; in
1021 * this case, if the indirect block is a hole, we fill in a few
1022 * fields on each of the child blocks (importantly, birth time)
1023 * to prevent hole birth times from being lost when you
1024 * partially fill in a hole.
1026 if (db->db_dirtycnt == 0) {
1027 if (db->db_level == 0) {
1028 uint64_t *buf = db->db.db_data;
1031 for (i = 0; i < db->db.db_size >> 3; i++) {
1032 ASSERT(buf[i] == 0);
1035 blkptr_t *bps = db->db.db_data;
1036 ASSERT3U(1 << DB_DNODE(db)->dn_indblkshift, ==,
1039 * We want to verify that all the blkptrs in the
1040 * indirect block are holes, but we may have
1041 * automatically set up a few fields for them.
1042 * We iterate through each blkptr and verify
1043 * they only have those fields set.
1046 i < db->db.db_size / sizeof (blkptr_t);
1048 blkptr_t *bp = &bps[i];
1049 ASSERT(ZIO_CHECKSUM_IS_ZERO(
1052 DVA_IS_EMPTY(&bp->blk_dva[0]) &&
1053 DVA_IS_EMPTY(&bp->blk_dva[1]) &&
1054 DVA_IS_EMPTY(&bp->blk_dva[2]));
1055 ASSERT0(bp->blk_fill);
1056 ASSERT0(bp->blk_pad[0]);
1057 ASSERT0(bp->blk_pad[1]);
1058 ASSERT(!BP_IS_EMBEDDED(bp));
1059 ASSERT(BP_IS_HOLE(bp));
1060 ASSERT0(bp->blk_phys_birth);
1070 dbuf_clear_data(dmu_buf_impl_t *db)
1072 ASSERT(MUTEX_HELD(&db->db_mtx));
1073 dbuf_evict_user(db);
1074 ASSERT3P(db->db_buf, ==, NULL);
1075 db->db.db_data = NULL;
1076 if (db->db_state != DB_NOFILL) {
1077 db->db_state = DB_UNCACHED;
1078 DTRACE_SET_STATE(db, "clear data");
1083 dbuf_set_data(dmu_buf_impl_t *db, arc_buf_t *buf)
1085 ASSERT(MUTEX_HELD(&db->db_mtx));
1086 ASSERT(buf != NULL);
1089 ASSERT(buf->b_data != NULL);
1090 db->db.db_data = buf->b_data;
1094 dbuf_alloc_arcbuf_from_arcbuf(dmu_buf_impl_t *db, arc_buf_t *data)
1096 objset_t *os = db->db_objset;
1097 spa_t *spa = os->os_spa;
1098 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1099 enum zio_compress compress_type;
1103 psize = arc_buf_size(data);
1104 lsize = arc_buf_lsize(data);
1105 compress_type = arc_get_compression(data);
1106 complevel = arc_get_complevel(data);
1108 if (arc_is_encrypted(data)) {
1109 boolean_t byteorder;
1110 uint8_t salt[ZIO_DATA_SALT_LEN];
1111 uint8_t iv[ZIO_DATA_IV_LEN];
1112 uint8_t mac[ZIO_DATA_MAC_LEN];
1113 dnode_t *dn = DB_DNODE(db);
1115 arc_get_raw_params(data, &byteorder, salt, iv, mac);
1116 data = arc_alloc_raw_buf(spa, db, dmu_objset_id(os),
1117 byteorder, salt, iv, mac, dn->dn_type, psize, lsize,
1118 compress_type, complevel);
1119 } else if (compress_type != ZIO_COMPRESS_OFF) {
1120 ASSERT3U(type, ==, ARC_BUFC_DATA);
1121 data = arc_alloc_compressed_buf(spa, db,
1122 psize, lsize, compress_type, complevel);
1124 data = arc_alloc_buf(spa, db, type, psize);
1130 dbuf_alloc_arcbuf(dmu_buf_impl_t *db)
1132 spa_t *spa = db->db_objset->os_spa;
1134 return (arc_alloc_buf(spa, db, DBUF_GET_BUFC_TYPE(db), db->db.db_size));
1138 * Loan out an arc_buf for read. Return the loaned arc_buf.
1141 dbuf_loan_arcbuf(dmu_buf_impl_t *db)
1145 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1146 mutex_enter(&db->db_mtx);
1147 if (arc_released(db->db_buf) || zfs_refcount_count(&db->db_holds) > 1) {
1148 int blksz = db->db.db_size;
1149 spa_t *spa = db->db_objset->os_spa;
1151 mutex_exit(&db->db_mtx);
1152 abuf = arc_loan_buf(spa, B_FALSE, blksz);
1153 bcopy(db->db.db_data, abuf->b_data, blksz);
1156 arc_loan_inuse_buf(abuf, db);
1158 dbuf_clear_data(db);
1159 mutex_exit(&db->db_mtx);
1165 * Calculate which level n block references the data at the level 0 offset
1169 dbuf_whichblock(const dnode_t *dn, const int64_t level, const uint64_t offset)
1171 if (dn->dn_datablkshift != 0 && dn->dn_indblkshift != 0) {
1173 * The level n blkid is equal to the level 0 blkid divided by
1174 * the number of level 0s in a level n block.
1176 * The level 0 blkid is offset >> datablkshift =
1177 * offset / 2^datablkshift.
1179 * The number of level 0s in a level n is the number of block
1180 * pointers in an indirect block, raised to the power of level.
1181 * This is 2^(indblkshift - SPA_BLKPTRSHIFT)^level =
1182 * 2^(level*(indblkshift - SPA_BLKPTRSHIFT)).
1184 * Thus, the level n blkid is: offset /
1185 * ((2^datablkshift)*(2^(level*(indblkshift-SPA_BLKPTRSHIFT))))
1186 * = offset / 2^(datablkshift + level *
1187 * (indblkshift - SPA_BLKPTRSHIFT))
1188 * = offset >> (datablkshift + level *
1189 * (indblkshift - SPA_BLKPTRSHIFT))
1192 const unsigned exp = dn->dn_datablkshift +
1193 level * (dn->dn_indblkshift - SPA_BLKPTRSHIFT);
1195 if (exp >= 8 * sizeof (offset)) {
1196 /* This only happens on the highest indirection level */
1197 ASSERT3U(level, ==, dn->dn_nlevels - 1);
1201 ASSERT3U(exp, <, 8 * sizeof (offset));
1203 return (offset >> exp);
1205 ASSERT3U(offset, <, dn->dn_datablksz);
1211 * This function is used to lock the parent of the provided dbuf. This should be
1212 * used when modifying or reading db_blkptr.
1215 dmu_buf_lock_parent(dmu_buf_impl_t *db, krw_t rw, void *tag)
1217 enum db_lock_type ret = DLT_NONE;
1218 if (db->db_parent != NULL) {
1219 rw_enter(&db->db_parent->db_rwlock, rw);
1221 } else if (dmu_objset_ds(db->db_objset) != NULL) {
1222 rrw_enter(&dmu_objset_ds(db->db_objset)->ds_bp_rwlock, rw,
1227 * We only return a DLT_NONE lock when it's the top-most indirect block
1228 * of the meta-dnode of the MOS.
1234 * We need to pass the lock type in because it's possible that the block will
1235 * move from being the topmost indirect block in a dnode (and thus, have no
1236 * parent) to not the top-most via an indirection increase. This would cause a
1237 * panic if we didn't pass the lock type in.
1240 dmu_buf_unlock_parent(dmu_buf_impl_t *db, db_lock_type_t type, void *tag)
1242 if (type == DLT_PARENT)
1243 rw_exit(&db->db_parent->db_rwlock);
1244 else if (type == DLT_OBJSET)
1245 rrw_exit(&dmu_objset_ds(db->db_objset)->ds_bp_rwlock, tag);
1249 dbuf_read_done(zio_t *zio, const zbookmark_phys_t *zb, const blkptr_t *bp,
1250 arc_buf_t *buf, void *vdb)
1252 dmu_buf_impl_t *db = vdb;
1254 mutex_enter(&db->db_mtx);
1255 ASSERT3U(db->db_state, ==, DB_READ);
1257 * All reads are synchronous, so we must have a hold on the dbuf
1259 ASSERT(zfs_refcount_count(&db->db_holds) > 0);
1260 ASSERT(db->db_buf == NULL);
1261 ASSERT(db->db.db_data == NULL);
1264 ASSERT(zio == NULL || zio->io_error != 0);
1265 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1266 ASSERT3P(db->db_buf, ==, NULL);
1267 db->db_state = DB_UNCACHED;
1268 DTRACE_SET_STATE(db, "i/o error");
1269 } else if (db->db_level == 0 && db->db_freed_in_flight) {
1270 /* freed in flight */
1271 ASSERT(zio == NULL || zio->io_error == 0);
1272 arc_release(buf, db);
1273 bzero(buf->b_data, db->db.db_size);
1274 arc_buf_freeze(buf);
1275 db->db_freed_in_flight = FALSE;
1276 dbuf_set_data(db, buf);
1277 db->db_state = DB_CACHED;
1278 DTRACE_SET_STATE(db, "freed in flight");
1281 ASSERT(zio == NULL || zio->io_error == 0);
1282 dbuf_set_data(db, buf);
1283 db->db_state = DB_CACHED;
1284 DTRACE_SET_STATE(db, "successful read");
1286 cv_broadcast(&db->db_changed);
1287 dbuf_rele_and_unlock(db, NULL, B_FALSE);
1291 * Shortcut for performing reads on bonus dbufs. Returns
1292 * an error if we fail to verify the dnode associated with
1293 * a decrypted block. Otherwise success.
1296 dbuf_read_bonus(dmu_buf_impl_t *db, dnode_t *dn, uint32_t flags)
1298 int bonuslen, max_bonuslen, err;
1300 err = dbuf_read_verify_dnode_crypt(db, flags);
1304 bonuslen = MIN(dn->dn_bonuslen, dn->dn_phys->dn_bonuslen);
1305 max_bonuslen = DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots);
1306 ASSERT(MUTEX_HELD(&db->db_mtx));
1307 ASSERT(DB_DNODE_HELD(db));
1308 ASSERT3U(bonuslen, <=, db->db.db_size);
1309 db->db.db_data = kmem_alloc(max_bonuslen, KM_SLEEP);
1310 arc_space_consume(max_bonuslen, ARC_SPACE_BONUS);
1311 if (bonuslen < max_bonuslen)
1312 bzero(db->db.db_data, max_bonuslen);
1314 bcopy(DN_BONUS(dn->dn_phys), db->db.db_data, bonuslen);
1315 db->db_state = DB_CACHED;
1316 DTRACE_SET_STATE(db, "bonus buffer filled");
1321 dbuf_handle_indirect_hole(dmu_buf_impl_t *db, dnode_t *dn)
1323 blkptr_t *bps = db->db.db_data;
1324 uint32_t indbs = 1ULL << dn->dn_indblkshift;
1325 int n_bps = indbs >> SPA_BLKPTRSHIFT;
1327 for (int i = 0; i < n_bps; i++) {
1328 blkptr_t *bp = &bps[i];
1330 ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==, indbs);
1331 BP_SET_LSIZE(bp, BP_GET_LEVEL(db->db_blkptr) == 1 ?
1332 dn->dn_datablksz : BP_GET_LSIZE(db->db_blkptr));
1333 BP_SET_TYPE(bp, BP_GET_TYPE(db->db_blkptr));
1334 BP_SET_LEVEL(bp, BP_GET_LEVEL(db->db_blkptr) - 1);
1335 BP_SET_BIRTH(bp, db->db_blkptr->blk_birth, 0);
1340 * Handle reads on dbufs that are holes, if necessary. This function
1341 * requires that the dbuf's mutex is held. Returns success (0) if action
1342 * was taken, ENOENT if no action was taken.
1345 dbuf_read_hole(dmu_buf_impl_t *db, dnode_t *dn, uint32_t flags)
1347 ASSERT(MUTEX_HELD(&db->db_mtx));
1349 int is_hole = db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr);
1351 * For level 0 blocks only, if the above check fails:
1352 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
1353 * processes the delete record and clears the bp while we are waiting
1354 * for the dn_mtx (resulting in a "no" from block_freed).
1356 if (!is_hole && db->db_level == 0) {
1357 is_hole = dnode_block_freed(dn, db->db_blkid) ||
1358 BP_IS_HOLE(db->db_blkptr);
1362 dbuf_set_data(db, dbuf_alloc_arcbuf(db));
1363 bzero(db->db.db_data, db->db.db_size);
1365 if (db->db_blkptr != NULL && db->db_level > 0 &&
1366 BP_IS_HOLE(db->db_blkptr) &&
1367 db->db_blkptr->blk_birth != 0) {
1368 dbuf_handle_indirect_hole(db, dn);
1370 db->db_state = DB_CACHED;
1371 DTRACE_SET_STATE(db, "hole read satisfied");
1378 * This function ensures that, when doing a decrypting read of a block,
1379 * we make sure we have decrypted the dnode associated with it. We must do
1380 * this so that we ensure we are fully authenticating the checksum-of-MACs
1381 * tree from the root of the objset down to this block. Indirect blocks are
1382 * always verified against their secure checksum-of-MACs assuming that the
1383 * dnode containing them is correct. Now that we are doing a decrypting read,
1384 * we can be sure that the key is loaded and verify that assumption. This is
1385 * especially important considering that we always read encrypted dnode
1386 * blocks as raw data (without verifying their MACs) to start, and
1387 * decrypt / authenticate them when we need to read an encrypted bonus buffer.
1390 dbuf_read_verify_dnode_crypt(dmu_buf_impl_t *db, uint32_t flags)
1393 objset_t *os = db->db_objset;
1394 arc_buf_t *dnode_abuf;
1396 zbookmark_phys_t zb;
1398 ASSERT(MUTEX_HELD(&db->db_mtx));
1400 if (!os->os_encrypted || os->os_raw_receive ||
1401 (flags & DB_RF_NO_DECRYPT) != 0)
1406 dnode_abuf = (dn->dn_dbuf != NULL) ? dn->dn_dbuf->db_buf : NULL;
1408 if (dnode_abuf == NULL || !arc_is_encrypted(dnode_abuf)) {
1413 SET_BOOKMARK(&zb, dmu_objset_id(os),
1414 DMU_META_DNODE_OBJECT, 0, dn->dn_dbuf->db_blkid);
1415 err = arc_untransform(dnode_abuf, os->os_spa, &zb, B_TRUE);
1418 * An error code of EACCES tells us that the key is still not
1419 * available. This is ok if we are only reading authenticated
1420 * (and therefore non-encrypted) blocks.
1422 if (err == EACCES && ((db->db_blkid != DMU_BONUS_BLKID &&
1423 !DMU_OT_IS_ENCRYPTED(dn->dn_type)) ||
1424 (db->db_blkid == DMU_BONUS_BLKID &&
1425 !DMU_OT_IS_ENCRYPTED(dn->dn_bonustype))))
1434 * Drops db_mtx and the parent lock specified by dblt and tag before
1438 dbuf_read_impl(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags,
1439 db_lock_type_t dblt, void *tag)
1442 zbookmark_phys_t zb;
1443 uint32_t aflags = ARC_FLAG_NOWAIT;
1445 boolean_t bonus_read;
1447 err = zio_flags = 0;
1448 bonus_read = B_FALSE;
1451 ASSERT(!zfs_refcount_is_zero(&db->db_holds));
1452 ASSERT(MUTEX_HELD(&db->db_mtx));
1453 ASSERT(db->db_state == DB_UNCACHED);
1454 ASSERT(db->db_buf == NULL);
1455 ASSERT(db->db_parent == NULL ||
1456 RW_LOCK_HELD(&db->db_parent->db_rwlock));
1458 if (db->db_blkid == DMU_BONUS_BLKID) {
1459 err = dbuf_read_bonus(db, dn, flags);
1463 err = dbuf_read_hole(db, dn, flags);
1468 * Any attempt to read a redacted block should result in an error. This
1469 * will never happen under normal conditions, but can be useful for
1470 * debugging purposes.
1472 if (BP_IS_REDACTED(db->db_blkptr)) {
1473 ASSERT(dsl_dataset_feature_is_active(
1474 db->db_objset->os_dsl_dataset,
1475 SPA_FEATURE_REDACTED_DATASETS));
1476 err = SET_ERROR(EIO);
1480 SET_BOOKMARK(&zb, dmu_objset_id(db->db_objset),
1481 db->db.db_object, db->db_level, db->db_blkid);
1484 * All bps of an encrypted os should have the encryption bit set.
1485 * If this is not true it indicates tampering and we report an error.
1487 if (db->db_objset->os_encrypted && !BP_USES_CRYPT(db->db_blkptr)) {
1488 spa_log_error(db->db_objset->os_spa, &zb);
1489 zfs_panic_recover("unencrypted block in encrypted "
1490 "object set %llu", dmu_objset_id(db->db_objset));
1491 err = SET_ERROR(EIO);
1495 err = dbuf_read_verify_dnode_crypt(db, flags);
1501 db->db_state = DB_READ;
1502 DTRACE_SET_STATE(db, "read issued");
1503 mutex_exit(&db->db_mtx);
1505 if (DBUF_IS_L2CACHEABLE(db))
1506 aflags |= ARC_FLAG_L2CACHE;
1508 dbuf_add_ref(db, NULL);
1510 zio_flags = (flags & DB_RF_CANFAIL) ?
1511 ZIO_FLAG_CANFAIL : ZIO_FLAG_MUSTSUCCEED;
1513 if ((flags & DB_RF_NO_DECRYPT) && BP_IS_PROTECTED(db->db_blkptr))
1514 zio_flags |= ZIO_FLAG_RAW;
1516 * The zio layer will copy the provided blkptr later, but we need to
1517 * do this now so that we can release the parent's rwlock. We have to
1518 * do that now so that if dbuf_read_done is called synchronously (on
1519 * an l1 cache hit) we don't acquire the db_mtx while holding the
1520 * parent's rwlock, which would be a lock ordering violation.
1522 blkptr_t bp = *db->db_blkptr;
1523 dmu_buf_unlock_parent(db, dblt, tag);
1524 (void) arc_read(zio, db->db_objset->os_spa, &bp,
1525 dbuf_read_done, db, ZIO_PRIORITY_SYNC_READ, zio_flags,
1530 mutex_exit(&db->db_mtx);
1531 dmu_buf_unlock_parent(db, dblt, tag);
1536 * This is our just-in-time copy function. It makes a copy of buffers that
1537 * have been modified in a previous transaction group before we access them in
1538 * the current active group.
1540 * This function is used in three places: when we are dirtying a buffer for the
1541 * first time in a txg, when we are freeing a range in a dnode that includes
1542 * this buffer, and when we are accessing a buffer which was received compressed
1543 * and later referenced in a WRITE_BYREF record.
1545 * Note that when we are called from dbuf_free_range() we do not put a hold on
1546 * the buffer, we just traverse the active dbuf list for the dnode.
1549 dbuf_fix_old_data(dmu_buf_impl_t *db, uint64_t txg)
1551 dbuf_dirty_record_t *dr = list_head(&db->db_dirty_records);
1553 ASSERT(MUTEX_HELD(&db->db_mtx));
1554 ASSERT(db->db.db_data != NULL);
1555 ASSERT(db->db_level == 0);
1556 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT);
1559 (dr->dt.dl.dr_data !=
1560 ((db->db_blkid == DMU_BONUS_BLKID) ? db->db.db_data : db->db_buf)))
1564 * If the last dirty record for this dbuf has not yet synced
1565 * and its referencing the dbuf data, either:
1566 * reset the reference to point to a new copy,
1567 * or (if there a no active holders)
1568 * just null out the current db_data pointer.
1570 ASSERT3U(dr->dr_txg, >=, txg - 2);
1571 if (db->db_blkid == DMU_BONUS_BLKID) {
1572 dnode_t *dn = DB_DNODE(db);
1573 int bonuslen = DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots);
1574 dr->dt.dl.dr_data = kmem_alloc(bonuslen, KM_SLEEP);
1575 arc_space_consume(bonuslen, ARC_SPACE_BONUS);
1576 bcopy(db->db.db_data, dr->dt.dl.dr_data, bonuslen);
1577 } else if (zfs_refcount_count(&db->db_holds) > db->db_dirtycnt) {
1578 arc_buf_t *buf = dbuf_alloc_arcbuf_from_arcbuf(db, db->db_buf);
1579 dr->dt.dl.dr_data = buf;
1580 bcopy(db->db.db_data, buf->b_data, arc_buf_size(buf));
1583 dbuf_clear_data(db);
1588 dbuf_read(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags)
1595 * We don't have to hold the mutex to check db_state because it
1596 * can't be freed while we have a hold on the buffer.
1598 ASSERT(!zfs_refcount_is_zero(&db->db_holds));
1600 if (db->db_state == DB_NOFILL)
1601 return (SET_ERROR(EIO));
1606 prefetch = db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
1607 (flags & DB_RF_NOPREFETCH) == 0 && dn != NULL &&
1608 DBUF_IS_CACHEABLE(db);
1610 mutex_enter(&db->db_mtx);
1611 if (db->db_state == DB_CACHED) {
1612 spa_t *spa = dn->dn_objset->os_spa;
1615 * Ensure that this block's dnode has been decrypted if
1616 * the caller has requested decrypted data.
1618 err = dbuf_read_verify_dnode_crypt(db, flags);
1621 * If the arc buf is compressed or encrypted and the caller
1622 * requested uncompressed data, we need to untransform it
1623 * before returning. We also call arc_untransform() on any
1624 * unauthenticated blocks, which will verify their MAC if
1625 * the key is now available.
1627 if (err == 0 && db->db_buf != NULL &&
1628 (flags & DB_RF_NO_DECRYPT) == 0 &&
1629 (arc_is_encrypted(db->db_buf) ||
1630 arc_is_unauthenticated(db->db_buf) ||
1631 arc_get_compression(db->db_buf) != ZIO_COMPRESS_OFF)) {
1632 zbookmark_phys_t zb;
1634 SET_BOOKMARK(&zb, dmu_objset_id(db->db_objset),
1635 db->db.db_object, db->db_level, db->db_blkid);
1636 dbuf_fix_old_data(db, spa_syncing_txg(spa));
1637 err = arc_untransform(db->db_buf, spa, &zb, B_FALSE);
1638 dbuf_set_data(db, db->db_buf);
1640 mutex_exit(&db->db_mtx);
1641 if (err == 0 && prefetch) {
1642 dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1, B_TRUE,
1643 flags & DB_RF_HAVESTRUCT);
1646 DBUF_STAT_BUMP(hash_hits);
1647 } else if (db->db_state == DB_UNCACHED) {
1648 spa_t *spa = dn->dn_objset->os_spa;
1649 boolean_t need_wait = B_FALSE;
1651 db_lock_type_t dblt = dmu_buf_lock_parent(db, RW_READER, FTAG);
1654 db->db_blkptr != NULL && !BP_IS_HOLE(db->db_blkptr)) {
1655 zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
1658 err = dbuf_read_impl(db, zio, flags, dblt, FTAG);
1660 * dbuf_read_impl has dropped db_mtx and our parent's rwlock
1663 if (!err && prefetch) {
1664 dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1, B_TRUE,
1665 flags & DB_RF_HAVESTRUCT);
1669 DBUF_STAT_BUMP(hash_misses);
1672 * If we created a zio_root we must execute it to avoid
1673 * leaking it, even if it isn't attached to any work due
1674 * to an error in dbuf_read_impl().
1678 err = zio_wait(zio);
1680 VERIFY0(zio_wait(zio));
1684 * Another reader came in while the dbuf was in flight
1685 * between UNCACHED and CACHED. Either a writer will finish
1686 * writing the buffer (sending the dbuf to CACHED) or the
1687 * first reader's request will reach the read_done callback
1688 * and send the dbuf to CACHED. Otherwise, a failure
1689 * occurred and the dbuf went to UNCACHED.
1691 mutex_exit(&db->db_mtx);
1693 dmu_zfetch(&dn->dn_zfetch, db->db_blkid, 1, B_TRUE,
1694 flags & DB_RF_HAVESTRUCT);
1697 DBUF_STAT_BUMP(hash_misses);
1699 /* Skip the wait per the caller's request. */
1700 if ((flags & DB_RF_NEVERWAIT) == 0) {
1701 mutex_enter(&db->db_mtx);
1702 while (db->db_state == DB_READ ||
1703 db->db_state == DB_FILL) {
1704 ASSERT(db->db_state == DB_READ ||
1705 (flags & DB_RF_HAVESTRUCT) == 0);
1706 DTRACE_PROBE2(blocked__read, dmu_buf_impl_t *,
1708 cv_wait(&db->db_changed, &db->db_mtx);
1710 if (db->db_state == DB_UNCACHED)
1711 err = SET_ERROR(EIO);
1712 mutex_exit(&db->db_mtx);
1720 dbuf_noread(dmu_buf_impl_t *db)
1722 ASSERT(!zfs_refcount_is_zero(&db->db_holds));
1723 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1724 mutex_enter(&db->db_mtx);
1725 while (db->db_state == DB_READ || db->db_state == DB_FILL)
1726 cv_wait(&db->db_changed, &db->db_mtx);
1727 if (db->db_state == DB_UNCACHED) {
1728 ASSERT(db->db_buf == NULL);
1729 ASSERT(db->db.db_data == NULL);
1730 dbuf_set_data(db, dbuf_alloc_arcbuf(db));
1731 db->db_state = DB_FILL;
1732 DTRACE_SET_STATE(db, "assigning filled buffer");
1733 } else if (db->db_state == DB_NOFILL) {
1734 dbuf_clear_data(db);
1736 ASSERT3U(db->db_state, ==, DB_CACHED);
1738 mutex_exit(&db->db_mtx);
1742 dbuf_unoverride(dbuf_dirty_record_t *dr)
1744 dmu_buf_impl_t *db = dr->dr_dbuf;
1745 blkptr_t *bp = &dr->dt.dl.dr_overridden_by;
1746 uint64_t txg = dr->dr_txg;
1748 ASSERT(MUTEX_HELD(&db->db_mtx));
1750 * This assert is valid because dmu_sync() expects to be called by
1751 * a zilog's get_data while holding a range lock. This call only
1752 * comes from dbuf_dirty() callers who must also hold a range lock.
1754 ASSERT(dr->dt.dl.dr_override_state != DR_IN_DMU_SYNC);
1755 ASSERT(db->db_level == 0);
1757 if (db->db_blkid == DMU_BONUS_BLKID ||
1758 dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN)
1761 ASSERT(db->db_data_pending != dr);
1763 /* free this block */
1764 if (!BP_IS_HOLE(bp) && !dr->dt.dl.dr_nopwrite)
1765 zio_free(db->db_objset->os_spa, txg, bp);
1767 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
1768 dr->dt.dl.dr_nopwrite = B_FALSE;
1769 dr->dt.dl.dr_has_raw_params = B_FALSE;
1772 * Release the already-written buffer, so we leave it in
1773 * a consistent dirty state. Note that all callers are
1774 * modifying the buffer, so they will immediately do
1775 * another (redundant) arc_release(). Therefore, leave
1776 * the buf thawed to save the effort of freezing &
1777 * immediately re-thawing it.
1779 arc_release(dr->dt.dl.dr_data, db);
1783 * Evict (if its unreferenced) or clear (if its referenced) any level-0
1784 * data blocks in the free range, so that any future readers will find
1788 dbuf_free_range(dnode_t *dn, uint64_t start_blkid, uint64_t end_blkid,
1791 dmu_buf_impl_t *db_search;
1792 dmu_buf_impl_t *db, *db_next;
1793 uint64_t txg = tx->tx_txg;
1795 dbuf_dirty_record_t *dr;
1797 if (end_blkid > dn->dn_maxblkid &&
1798 !(start_blkid == DMU_SPILL_BLKID || end_blkid == DMU_SPILL_BLKID))
1799 end_blkid = dn->dn_maxblkid;
1800 dprintf_dnode(dn, "start=%llu end=%llu\n", start_blkid, end_blkid);
1802 db_search = kmem_alloc(sizeof (dmu_buf_impl_t), KM_SLEEP);
1803 db_search->db_level = 0;
1804 db_search->db_blkid = start_blkid;
1805 db_search->db_state = DB_SEARCH;
1807 mutex_enter(&dn->dn_dbufs_mtx);
1808 db = avl_find(&dn->dn_dbufs, db_search, &where);
1809 ASSERT3P(db, ==, NULL);
1811 db = avl_nearest(&dn->dn_dbufs, where, AVL_AFTER);
1813 for (; db != NULL; db = db_next) {
1814 db_next = AVL_NEXT(&dn->dn_dbufs, db);
1815 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1817 if (db->db_level != 0 || db->db_blkid > end_blkid) {
1820 ASSERT3U(db->db_blkid, >=, start_blkid);
1822 /* found a level 0 buffer in the range */
1823 mutex_enter(&db->db_mtx);
1824 if (dbuf_undirty(db, tx)) {
1825 /* mutex has been dropped and dbuf destroyed */
1829 if (db->db_state == DB_UNCACHED ||
1830 db->db_state == DB_NOFILL ||
1831 db->db_state == DB_EVICTING) {
1832 ASSERT(db->db.db_data == NULL);
1833 mutex_exit(&db->db_mtx);
1836 if (db->db_state == DB_READ || db->db_state == DB_FILL) {
1837 /* will be handled in dbuf_read_done or dbuf_rele */
1838 db->db_freed_in_flight = TRUE;
1839 mutex_exit(&db->db_mtx);
1842 if (zfs_refcount_count(&db->db_holds) == 0) {
1847 /* The dbuf is referenced */
1849 dr = list_head(&db->db_dirty_records);
1851 if (dr->dr_txg == txg) {
1853 * This buffer is "in-use", re-adjust the file
1854 * size to reflect that this buffer may
1855 * contain new data when we sync.
1857 if (db->db_blkid != DMU_SPILL_BLKID &&
1858 db->db_blkid > dn->dn_maxblkid)
1859 dn->dn_maxblkid = db->db_blkid;
1860 dbuf_unoverride(dr);
1863 * This dbuf is not dirty in the open context.
1864 * Either uncache it (if its not referenced in
1865 * the open context) or reset its contents to
1868 dbuf_fix_old_data(db, txg);
1871 /* clear the contents if its cached */
1872 if (db->db_state == DB_CACHED) {
1873 ASSERT(db->db.db_data != NULL);
1874 arc_release(db->db_buf, db);
1875 rw_enter(&db->db_rwlock, RW_WRITER);
1876 bzero(db->db.db_data, db->db.db_size);
1877 rw_exit(&db->db_rwlock);
1878 arc_buf_freeze(db->db_buf);
1881 mutex_exit(&db->db_mtx);
1884 kmem_free(db_search, sizeof (dmu_buf_impl_t));
1885 mutex_exit(&dn->dn_dbufs_mtx);
1889 dbuf_new_size(dmu_buf_impl_t *db, int size, dmu_tx_t *tx)
1891 arc_buf_t *buf, *old_buf;
1892 dbuf_dirty_record_t *dr;
1893 int osize = db->db.db_size;
1894 arc_buf_contents_t type = DBUF_GET_BUFC_TYPE(db);
1897 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
1903 * XXX we should be doing a dbuf_read, checking the return
1904 * value and returning that up to our callers
1906 dmu_buf_will_dirty(&db->db, tx);
1908 /* create the data buffer for the new block */
1909 buf = arc_alloc_buf(dn->dn_objset->os_spa, db, type, size);
1911 /* copy old block data to the new block */
1912 old_buf = db->db_buf;
1913 bcopy(old_buf->b_data, buf->b_data, MIN(osize, size));
1914 /* zero the remainder */
1916 bzero((uint8_t *)buf->b_data + osize, size - osize);
1918 mutex_enter(&db->db_mtx);
1919 dbuf_set_data(db, buf);
1920 arc_buf_destroy(old_buf, db);
1921 db->db.db_size = size;
1923 dr = list_head(&db->db_dirty_records);
1924 /* dirty record added by dmu_buf_will_dirty() */
1926 if (db->db_level == 0)
1927 dr->dt.dl.dr_data = buf;
1928 ASSERT3U(dr->dr_txg, ==, tx->tx_txg);
1929 ASSERT3U(dr->dr_accounted, ==, osize);
1930 dr->dr_accounted = size;
1931 mutex_exit(&db->db_mtx);
1933 dmu_objset_willuse_space(dn->dn_objset, size - osize, tx);
1938 dbuf_release_bp(dmu_buf_impl_t *db)
1940 objset_t *os __maybe_unused = db->db_objset;
1942 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os)));
1943 ASSERT(arc_released(os->os_phys_buf) ||
1944 list_link_active(&os->os_dsl_dataset->ds_synced_link));
1945 ASSERT(db->db_parent == NULL || arc_released(db->db_parent->db_buf));
1947 (void) arc_release(db->db_buf, db);
1951 * We already have a dirty record for this TXG, and we are being
1955 dbuf_redirty(dbuf_dirty_record_t *dr)
1957 dmu_buf_impl_t *db = dr->dr_dbuf;
1959 ASSERT(MUTEX_HELD(&db->db_mtx));
1961 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID) {
1963 * If this buffer has already been written out,
1964 * we now need to reset its state.
1966 dbuf_unoverride(dr);
1967 if (db->db.db_object != DMU_META_DNODE_OBJECT &&
1968 db->db_state != DB_NOFILL) {
1969 /* Already released on initial dirty, so just thaw. */
1970 ASSERT(arc_released(db->db_buf));
1971 arc_buf_thaw(db->db_buf);
1976 dbuf_dirty_record_t *
1977 dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
1981 dbuf_dirty_record_t *dr, *dr_next, *dr_head;
1982 int txgoff = tx->tx_txg & TXG_MASK;
1983 boolean_t drop_struct_rwlock = B_FALSE;
1985 ASSERT(tx->tx_txg != 0);
1986 ASSERT(!zfs_refcount_is_zero(&db->db_holds));
1987 DMU_TX_DIRTY_BUF(tx, db);
1992 * Shouldn't dirty a regular buffer in syncing context. Private
1993 * objects may be dirtied in syncing context, but only if they
1994 * were already pre-dirtied in open context.
1997 if (dn->dn_objset->os_dsl_dataset != NULL) {
1998 rrw_enter(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock,
2001 ASSERT(!dmu_tx_is_syncing(tx) ||
2002 BP_IS_HOLE(dn->dn_objset->os_rootbp) ||
2003 DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
2004 dn->dn_objset->os_dsl_dataset == NULL);
2005 if (dn->dn_objset->os_dsl_dataset != NULL)
2006 rrw_exit(&dn->dn_objset->os_dsl_dataset->ds_bp_rwlock, FTAG);
2009 * We make this assert for private objects as well, but after we
2010 * check if we're already dirty. They are allowed to re-dirty
2011 * in syncing context.
2013 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
2014 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
2015 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
2017 mutex_enter(&db->db_mtx);
2019 * XXX make this true for indirects too? The problem is that
2020 * transactions created with dmu_tx_create_assigned() from
2021 * syncing context don't bother holding ahead.
2023 ASSERT(db->db_level != 0 ||
2024 db->db_state == DB_CACHED || db->db_state == DB_FILL ||
2025 db->db_state == DB_NOFILL);
2027 mutex_enter(&dn->dn_mtx);
2028 dnode_set_dirtyctx(dn, tx, db);
2029 if (tx->tx_txg > dn->dn_dirty_txg)
2030 dn->dn_dirty_txg = tx->tx_txg;
2031 mutex_exit(&dn->dn_mtx);
2033 if (db->db_blkid == DMU_SPILL_BLKID)
2034 dn->dn_have_spill = B_TRUE;
2037 * If this buffer is already dirty, we're done.
2039 dr_head = list_head(&db->db_dirty_records);
2040 ASSERT(dr_head == NULL || dr_head->dr_txg <= tx->tx_txg ||
2041 db->db.db_object == DMU_META_DNODE_OBJECT);
2042 dr_next = dbuf_find_dirty_lte(db, tx->tx_txg);
2043 if (dr_next && dr_next->dr_txg == tx->tx_txg) {
2046 dbuf_redirty(dr_next);
2047 mutex_exit(&db->db_mtx);
2052 * Only valid if not already dirty.
2054 ASSERT(dn->dn_object == 0 ||
2055 dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
2056 (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
2058 ASSERT3U(dn->dn_nlevels, >, db->db_level);
2061 * We should only be dirtying in syncing context if it's the
2062 * mos or we're initializing the os or it's a special object.
2063 * However, we are allowed to dirty in syncing context provided
2064 * we already dirtied it in open context. Hence we must make
2065 * this assertion only if we're not already dirty.
2068 VERIFY3U(tx->tx_txg, <=, spa_final_dirty_txg(os->os_spa));
2070 if (dn->dn_objset->os_dsl_dataset != NULL)
2071 rrw_enter(&os->os_dsl_dataset->ds_bp_rwlock, RW_READER, FTAG);
2072 ASSERT(!dmu_tx_is_syncing(tx) || DMU_OBJECT_IS_SPECIAL(dn->dn_object) ||
2073 os->os_dsl_dataset == NULL || BP_IS_HOLE(os->os_rootbp));
2074 if (dn->dn_objset->os_dsl_dataset != NULL)
2075 rrw_exit(&os->os_dsl_dataset->ds_bp_rwlock, FTAG);
2077 ASSERT(db->db.db_size != 0);
2079 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
2081 if (db->db_blkid != DMU_BONUS_BLKID) {
2082 dmu_objset_willuse_space(os, db->db.db_size, tx);
2086 * If this buffer is dirty in an old transaction group we need
2087 * to make a copy of it so that the changes we make in this
2088 * transaction group won't leak out when we sync the older txg.
2090 dr = kmem_zalloc(sizeof (dbuf_dirty_record_t), KM_SLEEP);
2091 list_link_init(&dr->dr_dirty_node);
2092 list_link_init(&dr->dr_dbuf_node);
2093 if (db->db_level == 0) {
2094 void *data_old = db->db_buf;
2096 if (db->db_state != DB_NOFILL) {
2097 if (db->db_blkid == DMU_BONUS_BLKID) {
2098 dbuf_fix_old_data(db, tx->tx_txg);
2099 data_old = db->db.db_data;
2100 } else if (db->db.db_object != DMU_META_DNODE_OBJECT) {
2102 * Release the data buffer from the cache so
2103 * that we can modify it without impacting
2104 * possible other users of this cached data
2105 * block. Note that indirect blocks and
2106 * private objects are not released until the
2107 * syncing state (since they are only modified
2110 arc_release(db->db_buf, db);
2111 dbuf_fix_old_data(db, tx->tx_txg);
2112 data_old = db->db_buf;
2114 ASSERT(data_old != NULL);
2116 dr->dt.dl.dr_data = data_old;
2118 mutex_init(&dr->dt.di.dr_mtx, NULL, MUTEX_NOLOCKDEP, NULL);
2119 list_create(&dr->dt.di.dr_children,
2120 sizeof (dbuf_dirty_record_t),
2121 offsetof(dbuf_dirty_record_t, dr_dirty_node));
2123 if (db->db_blkid != DMU_BONUS_BLKID)
2124 dr->dr_accounted = db->db.db_size;
2126 dr->dr_txg = tx->tx_txg;
2127 list_insert_before(&db->db_dirty_records, dr_next, dr);
2130 * We could have been freed_in_flight between the dbuf_noread
2131 * and dbuf_dirty. We win, as though the dbuf_noread() had
2132 * happened after the free.
2134 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
2135 db->db_blkid != DMU_SPILL_BLKID) {
2136 mutex_enter(&dn->dn_mtx);
2137 if (dn->dn_free_ranges[txgoff] != NULL) {
2138 range_tree_clear(dn->dn_free_ranges[txgoff],
2141 mutex_exit(&dn->dn_mtx);
2142 db->db_freed_in_flight = FALSE;
2146 * This buffer is now part of this txg
2148 dbuf_add_ref(db, (void *)(uintptr_t)tx->tx_txg);
2149 db->db_dirtycnt += 1;
2150 ASSERT3U(db->db_dirtycnt, <=, 3);
2152 mutex_exit(&db->db_mtx);
2154 if (db->db_blkid == DMU_BONUS_BLKID ||
2155 db->db_blkid == DMU_SPILL_BLKID) {
2156 mutex_enter(&dn->dn_mtx);
2157 ASSERT(!list_link_active(&dr->dr_dirty_node));
2158 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
2159 mutex_exit(&dn->dn_mtx);
2160 dnode_setdirty(dn, tx);
2165 if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
2166 rw_enter(&dn->dn_struct_rwlock, RW_READER);
2167 drop_struct_rwlock = B_TRUE;
2171 * If we are overwriting a dedup BP, then unless it is snapshotted,
2172 * when we get to syncing context we will need to decrement its
2173 * refcount in the DDT. Prefetch the relevant DDT block so that
2174 * syncing context won't have to wait for the i/o.
2176 if (db->db_blkptr != NULL) {
2177 db_lock_type_t dblt = dmu_buf_lock_parent(db, RW_READER, FTAG);
2178 ddt_prefetch(os->os_spa, db->db_blkptr);
2179 dmu_buf_unlock_parent(db, dblt, FTAG);
2183 * We need to hold the dn_struct_rwlock to make this assertion,
2184 * because it protects dn_phys / dn_next_nlevels from changing.
2186 ASSERT((dn->dn_phys->dn_nlevels == 0 && db->db_level == 0) ||
2187 dn->dn_phys->dn_nlevels > db->db_level ||
2188 dn->dn_next_nlevels[txgoff] > db->db_level ||
2189 dn->dn_next_nlevels[(tx->tx_txg-1) & TXG_MASK] > db->db_level ||
2190 dn->dn_next_nlevels[(tx->tx_txg-2) & TXG_MASK] > db->db_level);
2193 if (db->db_level == 0) {
2194 ASSERT(!db->db_objset->os_raw_receive ||
2195 dn->dn_maxblkid >= db->db_blkid);
2196 dnode_new_blkid(dn, db->db_blkid, tx,
2197 drop_struct_rwlock, B_FALSE);
2198 ASSERT(dn->dn_maxblkid >= db->db_blkid);
2201 if (db->db_level+1 < dn->dn_nlevels) {
2202 dmu_buf_impl_t *parent = db->db_parent;
2203 dbuf_dirty_record_t *di;
2204 int parent_held = FALSE;
2206 if (db->db_parent == NULL || db->db_parent == dn->dn_dbuf) {
2207 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
2208 parent = dbuf_hold_level(dn, db->db_level + 1,
2209 db->db_blkid >> epbs, FTAG);
2210 ASSERT(parent != NULL);
2213 if (drop_struct_rwlock)
2214 rw_exit(&dn->dn_struct_rwlock);
2215 ASSERT3U(db->db_level + 1, ==, parent->db_level);
2216 di = dbuf_dirty(parent, tx);
2218 dbuf_rele(parent, FTAG);
2220 mutex_enter(&db->db_mtx);
2222 * Since we've dropped the mutex, it's possible that
2223 * dbuf_undirty() might have changed this out from under us.
2225 if (list_head(&db->db_dirty_records) == dr ||
2226 dn->dn_object == DMU_META_DNODE_OBJECT) {
2227 mutex_enter(&di->dt.di.dr_mtx);
2228 ASSERT3U(di->dr_txg, ==, tx->tx_txg);
2229 ASSERT(!list_link_active(&dr->dr_dirty_node));
2230 list_insert_tail(&di->dt.di.dr_children, dr);
2231 mutex_exit(&di->dt.di.dr_mtx);
2234 mutex_exit(&db->db_mtx);
2236 ASSERT(db->db_level + 1 == dn->dn_nlevels);
2237 ASSERT(db->db_blkid < dn->dn_nblkptr);
2238 ASSERT(db->db_parent == NULL || db->db_parent == dn->dn_dbuf);
2239 mutex_enter(&dn->dn_mtx);
2240 ASSERT(!list_link_active(&dr->dr_dirty_node));
2241 list_insert_tail(&dn->dn_dirty_records[txgoff], dr);
2242 mutex_exit(&dn->dn_mtx);
2243 if (drop_struct_rwlock)
2244 rw_exit(&dn->dn_struct_rwlock);
2247 dnode_setdirty(dn, tx);
2253 dbuf_undirty_bonus(dbuf_dirty_record_t *dr)
2255 dmu_buf_impl_t *db = dr->dr_dbuf;
2257 if (dr->dt.dl.dr_data != db->db.db_data) {
2258 struct dnode *dn = DB_DNODE(db);
2259 int max_bonuslen = DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots);
2261 kmem_free(dr->dt.dl.dr_data, max_bonuslen);
2262 arc_space_return(max_bonuslen, ARC_SPACE_BONUS);
2264 db->db_data_pending = NULL;
2265 ASSERT(list_next(&db->db_dirty_records, dr) == NULL);
2266 list_remove(&db->db_dirty_records, dr);
2267 if (dr->dr_dbuf->db_level != 0) {
2268 mutex_destroy(&dr->dt.di.dr_mtx);
2269 list_destroy(&dr->dt.di.dr_children);
2271 kmem_free(dr, sizeof (dbuf_dirty_record_t));
2272 ASSERT3U(db->db_dirtycnt, >, 0);
2273 db->db_dirtycnt -= 1;
2277 * Undirty a buffer in the transaction group referenced by the given
2278 * transaction. Return whether this evicted the dbuf.
2281 dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
2284 uint64_t txg = tx->tx_txg;
2285 dbuf_dirty_record_t *dr;
2290 * Due to our use of dn_nlevels below, this can only be called
2291 * in open context, unless we are operating on the MOS.
2292 * From syncing context, dn_nlevels may be different from the
2293 * dn_nlevels used when dbuf was dirtied.
2295 ASSERT(db->db_objset ==
2296 dmu_objset_pool(db->db_objset)->dp_meta_objset ||
2297 txg != spa_syncing_txg(dmu_objset_spa(db->db_objset)));
2298 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
2299 ASSERT0(db->db_level);
2300 ASSERT(MUTEX_HELD(&db->db_mtx));
2303 * If this buffer is not dirty, we're done.
2305 dr = dbuf_find_dirty_eq(db, txg);
2308 ASSERT(dr->dr_dbuf == db);
2313 dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
2315 ASSERT(db->db.db_size != 0);
2317 dsl_pool_undirty_space(dmu_objset_pool(dn->dn_objset),
2318 dr->dr_accounted, txg);
2320 list_remove(&db->db_dirty_records, dr);
2323 * Note that there are three places in dbuf_dirty()
2324 * where this dirty record may be put on a list.
2325 * Make sure to do a list_remove corresponding to
2326 * every one of those list_insert calls.
2328 if (dr->dr_parent) {
2329 mutex_enter(&dr->dr_parent->dt.di.dr_mtx);
2330 list_remove(&dr->dr_parent->dt.di.dr_children, dr);
2331 mutex_exit(&dr->dr_parent->dt.di.dr_mtx);
2332 } else if (db->db_blkid == DMU_SPILL_BLKID ||
2333 db->db_level + 1 == dn->dn_nlevels) {
2334 ASSERT(db->db_blkptr == NULL || db->db_parent == dn->dn_dbuf);
2335 mutex_enter(&dn->dn_mtx);
2336 list_remove(&dn->dn_dirty_records[txg & TXG_MASK], dr);
2337 mutex_exit(&dn->dn_mtx);
2341 if (db->db_state != DB_NOFILL) {
2342 dbuf_unoverride(dr);
2344 ASSERT(db->db_buf != NULL);
2345 ASSERT(dr->dt.dl.dr_data != NULL);
2346 if (dr->dt.dl.dr_data != db->db_buf)
2347 arc_buf_destroy(dr->dt.dl.dr_data, db);
2350 kmem_free(dr, sizeof (dbuf_dirty_record_t));
2352 ASSERT(db->db_dirtycnt > 0);
2353 db->db_dirtycnt -= 1;
2355 if (zfs_refcount_remove(&db->db_holds, (void *)(uintptr_t)txg) == 0) {
2356 ASSERT(db->db_state == DB_NOFILL || arc_released(db->db_buf));
2365 dmu_buf_will_dirty_impl(dmu_buf_t *db_fake, int flags, dmu_tx_t *tx)
2367 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2369 ASSERT(tx->tx_txg != 0);
2370 ASSERT(!zfs_refcount_is_zero(&db->db_holds));
2373 * Quick check for dirtiness. For already dirty blocks, this
2374 * reduces runtime of this function by >90%, and overall performance
2375 * by 50% for some workloads (e.g. file deletion with indirect blocks
2378 mutex_enter(&db->db_mtx);
2380 if (db->db_state == DB_CACHED) {
2381 dbuf_dirty_record_t *dr = dbuf_find_dirty_eq(db, tx->tx_txg);
2383 * It's possible that it is already dirty but not cached,
2384 * because there are some calls to dbuf_dirty() that don't
2385 * go through dmu_buf_will_dirty().
2388 /* This dbuf is already dirty and cached. */
2390 mutex_exit(&db->db_mtx);
2394 mutex_exit(&db->db_mtx);
2397 if (RW_WRITE_HELD(&DB_DNODE(db)->dn_struct_rwlock))
2398 flags |= DB_RF_HAVESTRUCT;
2400 (void) dbuf_read(db, NULL, flags);
2401 (void) dbuf_dirty(db, tx);
2405 dmu_buf_will_dirty(dmu_buf_t *db_fake, dmu_tx_t *tx)
2407 dmu_buf_will_dirty_impl(db_fake,
2408 DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH, tx);
2412 dmu_buf_is_dirty(dmu_buf_t *db_fake, dmu_tx_t *tx)
2414 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2415 dbuf_dirty_record_t *dr;
2417 mutex_enter(&db->db_mtx);
2418 dr = dbuf_find_dirty_eq(db, tx->tx_txg);
2419 mutex_exit(&db->db_mtx);
2420 return (dr != NULL);
2424 dmu_buf_will_not_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
2426 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2428 db->db_state = DB_NOFILL;
2429 DTRACE_SET_STATE(db, "allocating NOFILL buffer");
2430 dmu_buf_will_fill(db_fake, tx);
2434 dmu_buf_will_fill(dmu_buf_t *db_fake, dmu_tx_t *tx)
2436 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2438 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
2439 ASSERT(tx->tx_txg != 0);
2440 ASSERT(db->db_level == 0);
2441 ASSERT(!zfs_refcount_is_zero(&db->db_holds));
2443 ASSERT(db->db.db_object != DMU_META_DNODE_OBJECT ||
2444 dmu_tx_private_ok(tx));
2447 (void) dbuf_dirty(db, tx);
2451 * This function is effectively the same as dmu_buf_will_dirty(), but
2452 * indicates the caller expects raw encrypted data in the db, and provides
2453 * the crypt params (byteorder, salt, iv, mac) which should be stored in the
2454 * blkptr_t when this dbuf is written. This is only used for blocks of
2455 * dnodes, during raw receive.
2458 dmu_buf_set_crypt_params(dmu_buf_t *db_fake, boolean_t byteorder,
2459 const uint8_t *salt, const uint8_t *iv, const uint8_t *mac, dmu_tx_t *tx)
2461 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2462 dbuf_dirty_record_t *dr;
2465 * dr_has_raw_params is only processed for blocks of dnodes
2466 * (see dbuf_sync_dnode_leaf_crypt()).
2468 ASSERT3U(db->db.db_object, ==, DMU_META_DNODE_OBJECT);
2469 ASSERT3U(db->db_level, ==, 0);
2470 ASSERT(db->db_objset->os_raw_receive);
2472 dmu_buf_will_dirty_impl(db_fake,
2473 DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH | DB_RF_NO_DECRYPT, tx);
2475 dr = dbuf_find_dirty_eq(db, tx->tx_txg);
2477 ASSERT3P(dr, !=, NULL);
2479 dr->dt.dl.dr_has_raw_params = B_TRUE;
2480 dr->dt.dl.dr_byteorder = byteorder;
2481 bcopy(salt, dr->dt.dl.dr_salt, ZIO_DATA_SALT_LEN);
2482 bcopy(iv, dr->dt.dl.dr_iv, ZIO_DATA_IV_LEN);
2483 bcopy(mac, dr->dt.dl.dr_mac, ZIO_DATA_MAC_LEN);
2487 dbuf_override_impl(dmu_buf_impl_t *db, const blkptr_t *bp, dmu_tx_t *tx)
2489 struct dirty_leaf *dl;
2490 dbuf_dirty_record_t *dr;
2492 dr = list_head(&db->db_dirty_records);
2493 ASSERT3U(dr->dr_txg, ==, tx->tx_txg);
2495 dl->dr_overridden_by = *bp;
2496 dl->dr_override_state = DR_OVERRIDDEN;
2497 dl->dr_overridden_by.blk_birth = dr->dr_txg;
2502 dmu_buf_fill_done(dmu_buf_t *dbuf, dmu_tx_t *tx)
2504 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf;
2505 dbuf_states_t old_state;
2506 mutex_enter(&db->db_mtx);
2509 old_state = db->db_state;
2510 db->db_state = DB_CACHED;
2511 if (old_state == DB_FILL) {
2512 if (db->db_level == 0 && db->db_freed_in_flight) {
2513 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
2514 /* we were freed while filling */
2515 /* XXX dbuf_undirty? */
2516 bzero(db->db.db_data, db->db.db_size);
2517 db->db_freed_in_flight = FALSE;
2518 DTRACE_SET_STATE(db,
2519 "fill done handling freed in flight");
2521 DTRACE_SET_STATE(db, "fill done");
2523 cv_broadcast(&db->db_changed);
2525 mutex_exit(&db->db_mtx);
2529 dmu_buf_write_embedded(dmu_buf_t *dbuf, void *data,
2530 bp_embedded_type_t etype, enum zio_compress comp,
2531 int uncompressed_size, int compressed_size, int byteorder,
2534 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf;
2535 struct dirty_leaf *dl;
2536 dmu_object_type_t type;
2537 dbuf_dirty_record_t *dr;
2539 if (etype == BP_EMBEDDED_TYPE_DATA) {
2540 ASSERT(spa_feature_is_active(dmu_objset_spa(db->db_objset),
2541 SPA_FEATURE_EMBEDDED_DATA));
2545 type = DB_DNODE(db)->dn_type;
2548 ASSERT0(db->db_level);
2549 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
2551 dmu_buf_will_not_fill(dbuf, tx);
2553 dr = list_head(&db->db_dirty_records);
2554 ASSERT3U(dr->dr_txg, ==, tx->tx_txg);
2556 encode_embedded_bp_compressed(&dl->dr_overridden_by,
2557 data, comp, uncompressed_size, compressed_size);
2558 BPE_SET_ETYPE(&dl->dr_overridden_by, etype);
2559 BP_SET_TYPE(&dl->dr_overridden_by, type);
2560 BP_SET_LEVEL(&dl->dr_overridden_by, 0);
2561 BP_SET_BYTEORDER(&dl->dr_overridden_by, byteorder);
2563 dl->dr_override_state = DR_OVERRIDDEN;
2564 dl->dr_overridden_by.blk_birth = dr->dr_txg;
2568 dmu_buf_redact(dmu_buf_t *dbuf, dmu_tx_t *tx)
2570 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbuf;
2571 dmu_object_type_t type;
2572 ASSERT(dsl_dataset_feature_is_active(db->db_objset->os_dsl_dataset,
2573 SPA_FEATURE_REDACTED_DATASETS));
2576 type = DB_DNODE(db)->dn_type;
2579 ASSERT0(db->db_level);
2580 dmu_buf_will_not_fill(dbuf, tx);
2582 blkptr_t bp = { { { {0} } } };
2583 BP_SET_TYPE(&bp, type);
2584 BP_SET_LEVEL(&bp, 0);
2585 BP_SET_BIRTH(&bp, tx->tx_txg, 0);
2586 BP_SET_REDACTED(&bp);
2587 BPE_SET_LSIZE(&bp, dbuf->db_size);
2589 dbuf_override_impl(db, &bp, tx);
2593 * Directly assign a provided arc buf to a given dbuf if it's not referenced
2594 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
2597 dbuf_assign_arcbuf(dmu_buf_impl_t *db, arc_buf_t *buf, dmu_tx_t *tx)
2599 ASSERT(!zfs_refcount_is_zero(&db->db_holds));
2600 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
2601 ASSERT(db->db_level == 0);
2602 ASSERT3U(dbuf_is_metadata(db), ==, arc_is_metadata(buf));
2603 ASSERT(buf != NULL);
2604 ASSERT3U(arc_buf_lsize(buf), ==, db->db.db_size);
2605 ASSERT(tx->tx_txg != 0);
2607 arc_return_buf(buf, db);
2608 ASSERT(arc_released(buf));
2610 mutex_enter(&db->db_mtx);
2612 while (db->db_state == DB_READ || db->db_state == DB_FILL)
2613 cv_wait(&db->db_changed, &db->db_mtx);
2615 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_UNCACHED);
2617 if (db->db_state == DB_CACHED &&
2618 zfs_refcount_count(&db->db_holds) - 1 > db->db_dirtycnt) {
2620 * In practice, we will never have a case where we have an
2621 * encrypted arc buffer while additional holds exist on the
2622 * dbuf. We don't handle this here so we simply assert that
2625 ASSERT(!arc_is_encrypted(buf));
2626 mutex_exit(&db->db_mtx);
2627 (void) dbuf_dirty(db, tx);
2628 bcopy(buf->b_data, db->db.db_data, db->db.db_size);
2629 arc_buf_destroy(buf, db);
2630 xuio_stat_wbuf_copied();
2634 xuio_stat_wbuf_nocopy();
2635 if (db->db_state == DB_CACHED) {
2636 dbuf_dirty_record_t *dr = list_head(&db->db_dirty_records);
2638 ASSERT(db->db_buf != NULL);
2639 if (dr != NULL && dr->dr_txg == tx->tx_txg) {
2640 ASSERT(dr->dt.dl.dr_data == db->db_buf);
2642 if (!arc_released(db->db_buf)) {
2643 ASSERT(dr->dt.dl.dr_override_state ==
2645 arc_release(db->db_buf, db);
2647 dr->dt.dl.dr_data = buf;
2648 arc_buf_destroy(db->db_buf, db);
2649 } else if (dr == NULL || dr->dt.dl.dr_data != db->db_buf) {
2650 arc_release(db->db_buf, db);
2651 arc_buf_destroy(db->db_buf, db);
2655 ASSERT(db->db_buf == NULL);
2656 dbuf_set_data(db, buf);
2657 db->db_state = DB_FILL;
2658 DTRACE_SET_STATE(db, "filling assigned arcbuf");
2659 mutex_exit(&db->db_mtx);
2660 (void) dbuf_dirty(db, tx);
2661 dmu_buf_fill_done(&db->db, tx);
2665 dbuf_destroy(dmu_buf_impl_t *db)
2668 dmu_buf_impl_t *parent = db->db_parent;
2669 dmu_buf_impl_t *dndb;
2671 ASSERT(MUTEX_HELD(&db->db_mtx));
2672 ASSERT(zfs_refcount_is_zero(&db->db_holds));
2674 if (db->db_buf != NULL) {
2675 arc_buf_destroy(db->db_buf, db);
2679 if (db->db_blkid == DMU_BONUS_BLKID) {
2680 int slots = DB_DNODE(db)->dn_num_slots;
2681 int bonuslen = DN_SLOTS_TO_BONUSLEN(slots);
2682 if (db->db.db_data != NULL) {
2683 kmem_free(db->db.db_data, bonuslen);
2684 arc_space_return(bonuslen, ARC_SPACE_BONUS);
2685 db->db_state = DB_UNCACHED;
2686 DTRACE_SET_STATE(db, "buffer cleared");
2690 dbuf_clear_data(db);
2692 if (multilist_link_active(&db->db_cache_link)) {
2693 ASSERT(db->db_caching_status == DB_DBUF_CACHE ||
2694 db->db_caching_status == DB_DBUF_METADATA_CACHE);
2696 multilist_remove(dbuf_caches[db->db_caching_status].cache, db);
2697 (void) zfs_refcount_remove_many(
2698 &dbuf_caches[db->db_caching_status].size,
2699 db->db.db_size, db);
2701 if (db->db_caching_status == DB_DBUF_METADATA_CACHE) {
2702 DBUF_STAT_BUMPDOWN(metadata_cache_count);
2704 DBUF_STAT_BUMPDOWN(cache_levels[db->db_level]);
2705 DBUF_STAT_BUMPDOWN(cache_count);
2706 DBUF_STAT_DECR(cache_levels_bytes[db->db_level],
2709 db->db_caching_status = DB_NO_CACHE;
2712 ASSERT(db->db_state == DB_UNCACHED || db->db_state == DB_NOFILL);
2713 ASSERT(db->db_data_pending == NULL);
2714 ASSERT(list_is_empty(&db->db_dirty_records));
2716 db->db_state = DB_EVICTING;
2717 DTRACE_SET_STATE(db, "buffer eviction started");
2718 db->db_blkptr = NULL;
2721 * Now that db_state is DB_EVICTING, nobody else can find this via
2722 * the hash table. We can now drop db_mtx, which allows us to
2723 * acquire the dn_dbufs_mtx.
2725 mutex_exit(&db->db_mtx);
2730 if (db->db_blkid != DMU_BONUS_BLKID) {
2731 boolean_t needlock = !MUTEX_HELD(&dn->dn_dbufs_mtx);
2733 mutex_enter_nested(&dn->dn_dbufs_mtx,
2735 avl_remove(&dn->dn_dbufs, db);
2739 mutex_exit(&dn->dn_dbufs_mtx);
2741 * Decrementing the dbuf count means that the hold corresponding
2742 * to the removed dbuf is no longer discounted in dnode_move(),
2743 * so the dnode cannot be moved until after we release the hold.
2744 * The membar_producer() ensures visibility of the decremented
2745 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
2748 mutex_enter(&dn->dn_mtx);
2749 dnode_rele_and_unlock(dn, db, B_TRUE);
2750 db->db_dnode_handle = NULL;
2752 dbuf_hash_remove(db);
2757 ASSERT(zfs_refcount_is_zero(&db->db_holds));
2759 db->db_parent = NULL;
2761 ASSERT(db->db_buf == NULL);
2762 ASSERT(db->db.db_data == NULL);
2763 ASSERT(db->db_hash_next == NULL);
2764 ASSERT(db->db_blkptr == NULL);
2765 ASSERT(db->db_data_pending == NULL);
2766 ASSERT3U(db->db_caching_status, ==, DB_NO_CACHE);
2767 ASSERT(!multilist_link_active(&db->db_cache_link));
2769 kmem_cache_free(dbuf_kmem_cache, db);
2770 arc_space_return(sizeof (dmu_buf_impl_t), ARC_SPACE_DBUF);
2773 * If this dbuf is referenced from an indirect dbuf,
2774 * decrement the ref count on the indirect dbuf.
2776 if (parent && parent != dndb) {
2777 mutex_enter(&parent->db_mtx);
2778 dbuf_rele_and_unlock(parent, db, B_TRUE);
2783 * Note: While bpp will always be updated if the function returns success,
2784 * parentp will not be updated if the dnode does not have dn_dbuf filled in;
2785 * this happens when the dnode is the meta-dnode, or {user|group|project}used
2788 __attribute__((always_inline))
2790 dbuf_findbp(dnode_t *dn, int level, uint64_t blkid, int fail_sparse,
2791 dmu_buf_impl_t **parentp, blkptr_t **bpp)
2796 ASSERT(blkid != DMU_BONUS_BLKID);
2798 if (blkid == DMU_SPILL_BLKID) {
2799 mutex_enter(&dn->dn_mtx);
2800 if (dn->dn_have_spill &&
2801 (dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR))
2802 *bpp = DN_SPILL_BLKPTR(dn->dn_phys);
2805 dbuf_add_ref(dn->dn_dbuf, NULL);
2806 *parentp = dn->dn_dbuf;
2807 mutex_exit(&dn->dn_mtx);
2812 (dn->dn_phys->dn_nlevels == 0) ? 1 : dn->dn_phys->dn_nlevels;
2813 int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
2815 ASSERT3U(level * epbs, <, 64);
2816 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2818 * This assertion shouldn't trip as long as the max indirect block size
2819 * is less than 1M. The reason for this is that up to that point,
2820 * the number of levels required to address an entire object with blocks
2821 * of size SPA_MINBLOCKSIZE satisfies nlevels * epbs + 1 <= 64. In
2822 * other words, if N * epbs + 1 > 64, then if (N-1) * epbs + 1 > 55
2823 * (i.e. we can address the entire object), objects will all use at most
2824 * N-1 levels and the assertion won't overflow. However, once epbs is
2825 * 13, 4 * 13 + 1 = 53, but 5 * 13 + 1 = 66. Then, 4 levels will not be
2826 * enough to address an entire object, so objects will have 5 levels,
2827 * but then this assertion will overflow.
2829 * All this is to say that if we ever increase DN_MAX_INDBLKSHIFT, we
2830 * need to redo this logic to handle overflows.
2832 ASSERT(level >= nlevels ||
2833 ((nlevels - level - 1) * epbs) +
2834 highbit64(dn->dn_phys->dn_nblkptr) <= 64);
2835 if (level >= nlevels ||
2836 blkid >= ((uint64_t)dn->dn_phys->dn_nblkptr <<
2837 ((nlevels - level - 1) * epbs)) ||
2839 blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))) {
2840 /* the buffer has no parent yet */
2841 return (SET_ERROR(ENOENT));
2842 } else if (level < nlevels-1) {
2843 /* this block is referenced from an indirect block */
2846 err = dbuf_hold_impl(dn, level + 1,
2847 blkid >> epbs, fail_sparse, FALSE, NULL, parentp);
2851 err = dbuf_read(*parentp, NULL,
2852 (DB_RF_HAVESTRUCT | DB_RF_NOPREFETCH | DB_RF_CANFAIL));
2854 dbuf_rele(*parentp, NULL);
2858 rw_enter(&(*parentp)->db_rwlock, RW_READER);
2859 *bpp = ((blkptr_t *)(*parentp)->db.db_data) +
2860 (blkid & ((1ULL << epbs) - 1));
2861 if (blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))
2862 ASSERT(BP_IS_HOLE(*bpp));
2863 rw_exit(&(*parentp)->db_rwlock);
2866 /* the block is referenced from the dnode */
2867 ASSERT3U(level, ==, nlevels-1);
2868 ASSERT(dn->dn_phys->dn_nblkptr == 0 ||
2869 blkid < dn->dn_phys->dn_nblkptr);
2871 dbuf_add_ref(dn->dn_dbuf, NULL);
2872 *parentp = dn->dn_dbuf;
2874 *bpp = &dn->dn_phys->dn_blkptr[blkid];
2879 static dmu_buf_impl_t *
2880 dbuf_create(dnode_t *dn, uint8_t level, uint64_t blkid,
2881 dmu_buf_impl_t *parent, blkptr_t *blkptr)
2883 objset_t *os = dn->dn_objset;
2884 dmu_buf_impl_t *db, *odb;
2886 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2887 ASSERT(dn->dn_type != DMU_OT_NONE);
2889 db = kmem_cache_alloc(dbuf_kmem_cache, KM_SLEEP);
2891 list_create(&db->db_dirty_records, sizeof (dbuf_dirty_record_t),
2892 offsetof(dbuf_dirty_record_t, dr_dbuf_node));
2895 db->db.db_object = dn->dn_object;
2896 db->db_level = level;
2897 db->db_blkid = blkid;
2898 db->db_dirtycnt = 0;
2899 db->db_dnode_handle = dn->dn_handle;
2900 db->db_parent = parent;
2901 db->db_blkptr = blkptr;
2904 db->db_user_immediate_evict = FALSE;
2905 db->db_freed_in_flight = FALSE;
2906 db->db_pending_evict = FALSE;
2908 if (blkid == DMU_BONUS_BLKID) {
2909 ASSERT3P(parent, ==, dn->dn_dbuf);
2910 db->db.db_size = DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots) -
2911 (dn->dn_nblkptr-1) * sizeof (blkptr_t);
2912 ASSERT3U(db->db.db_size, >=, dn->dn_bonuslen);
2913 db->db.db_offset = DMU_BONUS_BLKID;
2914 db->db_state = DB_UNCACHED;
2915 DTRACE_SET_STATE(db, "bonus buffer created");
2916 db->db_caching_status = DB_NO_CACHE;
2917 /* the bonus dbuf is not placed in the hash table */
2918 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_DBUF);
2920 } else if (blkid == DMU_SPILL_BLKID) {
2921 db->db.db_size = (blkptr != NULL) ?
2922 BP_GET_LSIZE(blkptr) : SPA_MINBLOCKSIZE;
2923 db->db.db_offset = 0;
2926 db->db_level ? 1 << dn->dn_indblkshift : dn->dn_datablksz;
2927 db->db.db_size = blocksize;
2928 db->db.db_offset = db->db_blkid * blocksize;
2932 * Hold the dn_dbufs_mtx while we get the new dbuf
2933 * in the hash table *and* added to the dbufs list.
2934 * This prevents a possible deadlock with someone
2935 * trying to look up this dbuf before it's added to the
2938 mutex_enter(&dn->dn_dbufs_mtx);
2939 db->db_state = DB_EVICTING; /* not worth logging this state change */
2940 if ((odb = dbuf_hash_insert(db)) != NULL) {
2941 /* someone else inserted it first */
2942 kmem_cache_free(dbuf_kmem_cache, db);
2943 mutex_exit(&dn->dn_dbufs_mtx);
2944 DBUF_STAT_BUMP(hash_insert_race);
2947 avl_add(&dn->dn_dbufs, db);
2949 db->db_state = DB_UNCACHED;
2950 DTRACE_SET_STATE(db, "regular buffer created");
2951 db->db_caching_status = DB_NO_CACHE;
2952 mutex_exit(&dn->dn_dbufs_mtx);
2953 arc_space_consume(sizeof (dmu_buf_impl_t), ARC_SPACE_DBUF);
2955 if (parent && parent != dn->dn_dbuf)
2956 dbuf_add_ref(parent, db);
2958 ASSERT(dn->dn_object == DMU_META_DNODE_OBJECT ||
2959 zfs_refcount_count(&dn->dn_holds) > 0);
2960 (void) zfs_refcount_add(&dn->dn_holds, db);
2962 dprintf_dbuf(db, "db=%p\n", db);
2968 * This function returns a block pointer and information about the object,
2969 * given a dnode and a block. This is a publicly accessible version of
2970 * dbuf_findbp that only returns some information, rather than the
2971 * dbuf. Note that the dnode passed in must be held, and the dn_struct_rwlock
2972 * should be locked as (at least) a reader.
2975 dbuf_dnode_findbp(dnode_t *dn, uint64_t level, uint64_t blkid,
2976 blkptr_t *bp, uint16_t *datablkszsec, uint8_t *indblkshift)
2978 dmu_buf_impl_t *dbp = NULL;
2981 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
2983 err = dbuf_findbp(dn, level, blkid, B_FALSE, &dbp, &bp2);
2987 dbuf_rele(dbp, NULL);
2988 if (datablkszsec != NULL)
2989 *datablkszsec = dn->dn_phys->dn_datablkszsec;
2990 if (indblkshift != NULL)
2991 *indblkshift = dn->dn_phys->dn_indblkshift;
2997 typedef struct dbuf_prefetch_arg {
2998 spa_t *dpa_spa; /* The spa to issue the prefetch in. */
2999 zbookmark_phys_t dpa_zb; /* The target block to prefetch. */
3000 int dpa_epbs; /* Entries (blkptr_t's) Per Block Shift. */
3001 int dpa_curlevel; /* The current level that we're reading */
3002 dnode_t *dpa_dnode; /* The dnode associated with the prefetch */
3003 zio_priority_t dpa_prio; /* The priority I/Os should be issued at. */
3004 zio_t *dpa_zio; /* The parent zio_t for all prefetches. */
3005 arc_flags_t dpa_aflags; /* Flags to pass to the final prefetch. */
3006 } dbuf_prefetch_arg_t;
3009 * Actually issue the prefetch read for the block given.
3012 dbuf_issue_final_prefetch(dbuf_prefetch_arg_t *dpa, blkptr_t *bp)
3014 ASSERT(!BP_IS_REDACTED(bp) ||
3015 dsl_dataset_feature_is_active(
3016 dpa->dpa_dnode->dn_objset->os_dsl_dataset,
3017 SPA_FEATURE_REDACTED_DATASETS));
3019 if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp) || BP_IS_REDACTED(bp))
3022 int zio_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE;
3023 arc_flags_t aflags =
3024 dpa->dpa_aflags | ARC_FLAG_NOWAIT | ARC_FLAG_PREFETCH;
3026 /* dnodes are always read as raw and then converted later */
3027 if (BP_GET_TYPE(bp) == DMU_OT_DNODE && BP_IS_PROTECTED(bp) &&
3028 dpa->dpa_curlevel == 0)
3029 zio_flags |= ZIO_FLAG_RAW;
3031 ASSERT3U(dpa->dpa_curlevel, ==, BP_GET_LEVEL(bp));
3032 ASSERT3U(dpa->dpa_curlevel, ==, dpa->dpa_zb.zb_level);
3033 ASSERT(dpa->dpa_zio != NULL);
3034 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa, bp, NULL, NULL,
3035 dpa->dpa_prio, zio_flags, &aflags, &dpa->dpa_zb);
3039 * Called when an indirect block above our prefetch target is read in. This
3040 * will either read in the next indirect block down the tree or issue the actual
3041 * prefetch if the next block down is our target.
3044 dbuf_prefetch_indirect_done(zio_t *zio, const zbookmark_phys_t *zb,
3045 const blkptr_t *iobp, arc_buf_t *abuf, void *private)
3047 dbuf_prefetch_arg_t *dpa = private;
3049 ASSERT3S(dpa->dpa_zb.zb_level, <, dpa->dpa_curlevel);
3050 ASSERT3S(dpa->dpa_curlevel, >, 0);
3053 ASSERT(zio == NULL || zio->io_error != 0);
3054 kmem_free(dpa, sizeof (*dpa));
3057 ASSERT(zio == NULL || zio->io_error == 0);
3060 * The dpa_dnode is only valid if we are called with a NULL
3061 * zio. This indicates that the arc_read() returned without
3062 * first calling zio_read() to issue a physical read. Once
3063 * a physical read is made the dpa_dnode must be invalidated
3064 * as the locks guarding it may have been dropped. If the
3065 * dpa_dnode is still valid, then we want to add it to the dbuf
3066 * cache. To do so, we must hold the dbuf associated with the block
3067 * we just prefetched, read its contents so that we associate it
3068 * with an arc_buf_t, and then release it.
3071 ASSERT3S(BP_GET_LEVEL(zio->io_bp), ==, dpa->dpa_curlevel);
3072 if (zio->io_flags & ZIO_FLAG_RAW_COMPRESS) {
3073 ASSERT3U(BP_GET_PSIZE(zio->io_bp), ==, zio->io_size);
3075 ASSERT3U(BP_GET_LSIZE(zio->io_bp), ==, zio->io_size);
3077 ASSERT3P(zio->io_spa, ==, dpa->dpa_spa);
3079 dpa->dpa_dnode = NULL;
3080 } else if (dpa->dpa_dnode != NULL) {
3081 uint64_t curblkid = dpa->dpa_zb.zb_blkid >>
3082 (dpa->dpa_epbs * (dpa->dpa_curlevel -
3083 dpa->dpa_zb.zb_level));
3084 dmu_buf_impl_t *db = dbuf_hold_level(dpa->dpa_dnode,
3085 dpa->dpa_curlevel, curblkid, FTAG);
3087 kmem_free(dpa, sizeof (*dpa));
3088 arc_buf_destroy(abuf, private);
3092 (void) dbuf_read(db, NULL,
3093 DB_RF_MUST_SUCCEED | DB_RF_NOPREFETCH | DB_RF_HAVESTRUCT);
3094 dbuf_rele(db, FTAG);
3097 dpa->dpa_curlevel--;
3098 uint64_t nextblkid = dpa->dpa_zb.zb_blkid >>
3099 (dpa->dpa_epbs * (dpa->dpa_curlevel - dpa->dpa_zb.zb_level));
3100 blkptr_t *bp = ((blkptr_t *)abuf->b_data) +
3101 P2PHASE(nextblkid, 1ULL << dpa->dpa_epbs);
3103 ASSERT(!BP_IS_REDACTED(bp) ||
3104 dsl_dataset_feature_is_active(
3105 dpa->dpa_dnode->dn_objset->os_dsl_dataset,
3106 SPA_FEATURE_REDACTED_DATASETS));
3107 if (BP_IS_HOLE(bp) || BP_IS_REDACTED(bp)) {
3108 kmem_free(dpa, sizeof (*dpa));
3109 } else if (dpa->dpa_curlevel == dpa->dpa_zb.zb_level) {
3110 ASSERT3U(nextblkid, ==, dpa->dpa_zb.zb_blkid);
3111 dbuf_issue_final_prefetch(dpa, bp);
3112 kmem_free(dpa, sizeof (*dpa));
3114 arc_flags_t iter_aflags = ARC_FLAG_NOWAIT;
3115 zbookmark_phys_t zb;
3117 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
3118 if (dpa->dpa_aflags & ARC_FLAG_L2CACHE)
3119 iter_aflags |= ARC_FLAG_L2CACHE;
3121 ASSERT3U(dpa->dpa_curlevel, ==, BP_GET_LEVEL(bp));
3123 SET_BOOKMARK(&zb, dpa->dpa_zb.zb_objset,
3124 dpa->dpa_zb.zb_object, dpa->dpa_curlevel, nextblkid);
3126 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa,
3127 bp, dbuf_prefetch_indirect_done, dpa, dpa->dpa_prio,
3128 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
3132 arc_buf_destroy(abuf, private);
3136 * Issue prefetch reads for the given block on the given level. If the indirect
3137 * blocks above that block are not in memory, we will read them in
3138 * asynchronously. As a result, this call never blocks waiting for a read to
3139 * complete. Note that the prefetch might fail if the dataset is encrypted and
3140 * the encryption key is unmapped before the IO completes.
3143 dbuf_prefetch(dnode_t *dn, int64_t level, uint64_t blkid, zio_priority_t prio,
3147 int epbs, nlevels, curlevel;
3150 ASSERT(blkid != DMU_BONUS_BLKID);
3151 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
3153 if (blkid > dn->dn_maxblkid)
3156 if (level == 0 && dnode_block_freed(dn, blkid))
3160 * This dnode hasn't been written to disk yet, so there's nothing to
3163 nlevels = dn->dn_phys->dn_nlevels;
3164 if (level >= nlevels || dn->dn_phys->dn_nblkptr == 0)
3167 epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
3168 if (dn->dn_phys->dn_maxblkid < blkid << (epbs * level))
3171 dmu_buf_impl_t *db = dbuf_find(dn->dn_objset, dn->dn_object,
3174 mutex_exit(&db->db_mtx);
3176 * This dbuf already exists. It is either CACHED, or
3177 * (we assume) about to be read or filled.
3183 * Find the closest ancestor (indirect block) of the target block
3184 * that is present in the cache. In this indirect block, we will
3185 * find the bp that is at curlevel, curblkid.
3189 while (curlevel < nlevels - 1) {
3190 int parent_level = curlevel + 1;
3191 uint64_t parent_blkid = curblkid >> epbs;
3194 if (dbuf_hold_impl(dn, parent_level, parent_blkid,
3195 FALSE, TRUE, FTAG, &db) == 0) {
3196 blkptr_t *bpp = db->db_buf->b_data;
3197 bp = bpp[P2PHASE(curblkid, 1 << epbs)];
3198 dbuf_rele(db, FTAG);
3202 curlevel = parent_level;
3203 curblkid = parent_blkid;
3206 if (curlevel == nlevels - 1) {
3207 /* No cached indirect blocks found. */
3208 ASSERT3U(curblkid, <, dn->dn_phys->dn_nblkptr);
3209 bp = dn->dn_phys->dn_blkptr[curblkid];
3211 ASSERT(!BP_IS_REDACTED(&bp) ||
3212 dsl_dataset_feature_is_active(dn->dn_objset->os_dsl_dataset,
3213 SPA_FEATURE_REDACTED_DATASETS));
3214 if (BP_IS_HOLE(&bp) || BP_IS_REDACTED(&bp))
3217 ASSERT3U(curlevel, ==, BP_GET_LEVEL(&bp));
3219 zio_t *pio = zio_root(dmu_objset_spa(dn->dn_objset), NULL, NULL,
3222 dbuf_prefetch_arg_t *dpa = kmem_zalloc(sizeof (*dpa), KM_SLEEP);
3223 dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset;
3224 SET_BOOKMARK(&dpa->dpa_zb, ds != NULL ? ds->ds_object : DMU_META_OBJSET,
3225 dn->dn_object, level, blkid);
3226 dpa->dpa_curlevel = curlevel;
3227 dpa->dpa_prio = prio;
3228 dpa->dpa_aflags = aflags;
3229 dpa->dpa_spa = dn->dn_objset->os_spa;
3230 dpa->dpa_dnode = dn;
3231 dpa->dpa_epbs = epbs;
3234 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
3235 if (DNODE_LEVEL_IS_L2CACHEABLE(dn, level))
3236 dpa->dpa_aflags |= ARC_FLAG_L2CACHE;
3239 * If we have the indirect just above us, no need to do the asynchronous
3240 * prefetch chain; we'll just run the last step ourselves. If we're at
3241 * a higher level, though, we want to issue the prefetches for all the
3242 * indirect blocks asynchronously, so we can go on with whatever we were
3245 if (curlevel == level) {
3246 ASSERT3U(curblkid, ==, blkid);
3247 dbuf_issue_final_prefetch(dpa, &bp);
3248 kmem_free(dpa, sizeof (*dpa));
3250 arc_flags_t iter_aflags = ARC_FLAG_NOWAIT;
3251 zbookmark_phys_t zb;
3253 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
3254 if (DNODE_LEVEL_IS_L2CACHEABLE(dn, level))
3255 iter_aflags |= ARC_FLAG_L2CACHE;
3257 SET_BOOKMARK(&zb, ds != NULL ? ds->ds_object : DMU_META_OBJSET,
3258 dn->dn_object, curlevel, curblkid);
3259 (void) arc_read(dpa->dpa_zio, dpa->dpa_spa,
3260 &bp, dbuf_prefetch_indirect_done, dpa, prio,
3261 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
3265 * We use pio here instead of dpa_zio since it's possible that
3266 * dpa may have already been freed.
3272 * Helper function for dbuf_hold_impl() to copy a buffer. Handles
3273 * the case of encrypted, compressed and uncompressed buffers by
3274 * allocating the new buffer, respectively, with arc_alloc_raw_buf(),
3275 * arc_alloc_compressed_buf() or arc_alloc_buf().*
3277 * NOTE: Declared noinline to avoid stack bloat in dbuf_hold_impl().
3279 noinline static void
3280 dbuf_hold_copy(dnode_t *dn, dmu_buf_impl_t *db)
3282 dbuf_dirty_record_t *dr = db->db_data_pending;
3283 arc_buf_t *newdata, *data = dr->dt.dl.dr_data;
3285 newdata = dbuf_alloc_arcbuf_from_arcbuf(db, data);
3286 dbuf_set_data(db, newdata);
3287 rw_enter(&db->db_rwlock, RW_WRITER);
3288 bcopy(data->b_data, db->db.db_data, arc_buf_size(data));
3289 rw_exit(&db->db_rwlock);
3293 * Returns with db_holds incremented, and db_mtx not held.
3294 * Note: dn_struct_rwlock must be held.
3297 dbuf_hold_impl(dnode_t *dn, uint8_t level, uint64_t blkid,
3298 boolean_t fail_sparse, boolean_t fail_uncached,
3299 void *tag, dmu_buf_impl_t **dbp)
3301 dmu_buf_impl_t *db, *parent = NULL;
3303 /* If the pool has been created, verify the tx_sync_lock is not held */
3304 spa_t *spa = dn->dn_objset->os_spa;
3305 dsl_pool_t *dp = spa->spa_dsl_pool;
3307 ASSERT(!MUTEX_HELD(&dp->dp_tx.tx_sync_lock));
3310 ASSERT(blkid != DMU_BONUS_BLKID);
3311 ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
3312 ASSERT3U(dn->dn_nlevels, >, level);
3316 /* dbuf_find() returns with db_mtx held */
3317 db = dbuf_find(dn->dn_objset, dn->dn_object, level, blkid);
3320 blkptr_t *bp = NULL;
3324 return (SET_ERROR(ENOENT));
3326 ASSERT3P(parent, ==, NULL);
3327 err = dbuf_findbp(dn, level, blkid, fail_sparse, &parent, &bp);
3329 if (err == 0 && bp && BP_IS_HOLE(bp))
3330 err = SET_ERROR(ENOENT);
3333 dbuf_rele(parent, NULL);
3337 if (err && err != ENOENT)
3339 db = dbuf_create(dn, level, blkid, parent, bp);
3342 if (fail_uncached && db->db_state != DB_CACHED) {
3343 mutex_exit(&db->db_mtx);
3344 return (SET_ERROR(ENOENT));
3347 if (db->db_buf != NULL) {
3348 arc_buf_access(db->db_buf);
3349 ASSERT3P(db->db.db_data, ==, db->db_buf->b_data);
3352 ASSERT(db->db_buf == NULL || arc_referenced(db->db_buf));
3355 * If this buffer is currently syncing out, and we are
3356 * still referencing it from db_data, we need to make a copy
3357 * of it in case we decide we want to dirty it again in this txg.
3359 if (db->db_level == 0 && db->db_blkid != DMU_BONUS_BLKID &&
3360 dn->dn_object != DMU_META_DNODE_OBJECT &&
3361 db->db_state == DB_CACHED && db->db_data_pending) {
3362 dbuf_dirty_record_t *dr = db->db_data_pending;
3363 if (dr->dt.dl.dr_data == db->db_buf)
3364 dbuf_hold_copy(dn, db);
3367 if (multilist_link_active(&db->db_cache_link)) {
3368 ASSERT(zfs_refcount_is_zero(&db->db_holds));
3369 ASSERT(db->db_caching_status == DB_DBUF_CACHE ||
3370 db->db_caching_status == DB_DBUF_METADATA_CACHE);
3372 multilist_remove(dbuf_caches[db->db_caching_status].cache, db);
3373 (void) zfs_refcount_remove_many(
3374 &dbuf_caches[db->db_caching_status].size,
3375 db->db.db_size, db);
3377 if (db->db_caching_status == DB_DBUF_METADATA_CACHE) {
3378 DBUF_STAT_BUMPDOWN(metadata_cache_count);
3380 DBUF_STAT_BUMPDOWN(cache_levels[db->db_level]);
3381 DBUF_STAT_BUMPDOWN(cache_count);
3382 DBUF_STAT_DECR(cache_levels_bytes[db->db_level],
3385 db->db_caching_status = DB_NO_CACHE;
3387 (void) zfs_refcount_add(&db->db_holds, tag);
3389 mutex_exit(&db->db_mtx);
3391 /* NOTE: we can't rele the parent until after we drop the db_mtx */
3393 dbuf_rele(parent, NULL);
3395 ASSERT3P(DB_DNODE(db), ==, dn);
3396 ASSERT3U(db->db_blkid, ==, blkid);
3397 ASSERT3U(db->db_level, ==, level);
3404 dbuf_hold(dnode_t *dn, uint64_t blkid, void *tag)
3406 return (dbuf_hold_level(dn, 0, blkid, tag));
3410 dbuf_hold_level(dnode_t *dn, int level, uint64_t blkid, void *tag)
3413 int err = dbuf_hold_impl(dn, level, blkid, FALSE, FALSE, tag, &db);
3414 return (err ? NULL : db);
3418 dbuf_create_bonus(dnode_t *dn)
3420 ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock));
3422 ASSERT(dn->dn_bonus == NULL);
3423 dn->dn_bonus = dbuf_create(dn, 0, DMU_BONUS_BLKID, dn->dn_dbuf, NULL);
3427 dbuf_spill_set_blksz(dmu_buf_t *db_fake, uint64_t blksz, dmu_tx_t *tx)
3429 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
3431 if (db->db_blkid != DMU_SPILL_BLKID)
3432 return (SET_ERROR(ENOTSUP));
3434 blksz = SPA_MINBLOCKSIZE;
3435 ASSERT3U(blksz, <=, spa_maxblocksize(dmu_objset_spa(db->db_objset)));
3436 blksz = P2ROUNDUP(blksz, SPA_MINBLOCKSIZE);
3438 dbuf_new_size(db, blksz, tx);
3444 dbuf_rm_spill(dnode_t *dn, dmu_tx_t *tx)
3446 dbuf_free_range(dn, DMU_SPILL_BLKID, DMU_SPILL_BLKID, tx);
3449 #pragma weak dmu_buf_add_ref = dbuf_add_ref
3451 dbuf_add_ref(dmu_buf_impl_t *db, void *tag)
3453 int64_t holds = zfs_refcount_add(&db->db_holds, tag);
3454 VERIFY3S(holds, >, 1);
3457 #pragma weak dmu_buf_try_add_ref = dbuf_try_add_ref
3459 dbuf_try_add_ref(dmu_buf_t *db_fake, objset_t *os, uint64_t obj, uint64_t blkid,
3462 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
3463 dmu_buf_impl_t *found_db;
3464 boolean_t result = B_FALSE;
3466 if (blkid == DMU_BONUS_BLKID)
3467 found_db = dbuf_find_bonus(os, obj);
3469 found_db = dbuf_find(os, obj, 0, blkid);
3471 if (found_db != NULL) {
3472 if (db == found_db && dbuf_refcount(db) > db->db_dirtycnt) {
3473 (void) zfs_refcount_add(&db->db_holds, tag);
3476 mutex_exit(&found_db->db_mtx);
3482 * If you call dbuf_rele() you had better not be referencing the dnode handle
3483 * unless you have some other direct or indirect hold on the dnode. (An indirect
3484 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
3485 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
3486 * dnode's parent dbuf evicting its dnode handles.
3489 dbuf_rele(dmu_buf_impl_t *db, void *tag)
3491 mutex_enter(&db->db_mtx);
3492 dbuf_rele_and_unlock(db, tag, B_FALSE);
3496 dmu_buf_rele(dmu_buf_t *db, void *tag)
3498 dbuf_rele((dmu_buf_impl_t *)db, tag);
3502 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
3503 * db_dirtycnt and db_holds to be updated atomically. The 'evicting'
3504 * argument should be set if we are already in the dbuf-evicting code
3505 * path, in which case we don't want to recursively evict. This allows us to
3506 * avoid deeply nested stacks that would have a call flow similar to this:
3508 * dbuf_rele()-->dbuf_rele_and_unlock()-->dbuf_evict_notify()
3511 * +-----dbuf_destroy()<--dbuf_evict_one()<--------+
3515 dbuf_rele_and_unlock(dmu_buf_impl_t *db, void *tag, boolean_t evicting)
3520 ASSERT(MUTEX_HELD(&db->db_mtx));
3524 * Remove the reference to the dbuf before removing its hold on the
3525 * dnode so we can guarantee in dnode_move() that a referenced bonus
3526 * buffer has a corresponding dnode hold.
3528 holds = zfs_refcount_remove(&db->db_holds, tag);
3532 * We can't freeze indirects if there is a possibility that they
3533 * may be modified in the current syncing context.
3535 if (db->db_buf != NULL &&
3536 holds == (db->db_level == 0 ? db->db_dirtycnt : 0)) {
3537 arc_buf_freeze(db->db_buf);
3540 if (holds == db->db_dirtycnt &&
3541 db->db_level == 0 && db->db_user_immediate_evict)
3542 dbuf_evict_user(db);
3545 if (db->db_blkid == DMU_BONUS_BLKID) {
3547 boolean_t evict_dbuf = db->db_pending_evict;
3550 * If the dnode moves here, we cannot cross this
3551 * barrier until the move completes.
3556 atomic_dec_32(&dn->dn_dbufs_count);
3559 * Decrementing the dbuf count means that the bonus
3560 * buffer's dnode hold is no longer discounted in
3561 * dnode_move(). The dnode cannot move until after
3562 * the dnode_rele() below.
3567 * Do not reference db after its lock is dropped.
3568 * Another thread may evict it.
3570 mutex_exit(&db->db_mtx);
3573 dnode_evict_bonus(dn);
3576 } else if (db->db_buf == NULL) {
3578 * This is a special case: we never associated this
3579 * dbuf with any data allocated from the ARC.
3581 ASSERT(db->db_state == DB_UNCACHED ||
3582 db->db_state == DB_NOFILL);
3584 } else if (arc_released(db->db_buf)) {
3586 * This dbuf has anonymous data associated with it.
3590 boolean_t do_arc_evict = B_FALSE;
3592 spa_t *spa = dmu_objset_spa(db->db_objset);
3594 if (!DBUF_IS_CACHEABLE(db) &&
3595 db->db_blkptr != NULL &&
3596 !BP_IS_HOLE(db->db_blkptr) &&
3597 !BP_IS_EMBEDDED(db->db_blkptr)) {
3598 do_arc_evict = B_TRUE;
3599 bp = *db->db_blkptr;
3602 if (!DBUF_IS_CACHEABLE(db) ||
3603 db->db_pending_evict) {
3605 } else if (!multilist_link_active(&db->db_cache_link)) {
3606 ASSERT3U(db->db_caching_status, ==,
3609 dbuf_cached_state_t dcs =
3610 dbuf_include_in_metadata_cache(db) ?
3611 DB_DBUF_METADATA_CACHE : DB_DBUF_CACHE;
3612 db->db_caching_status = dcs;
3614 multilist_insert(dbuf_caches[dcs].cache, db);
3615 size = zfs_refcount_add_many(
3616 &dbuf_caches[dcs].size,
3617 db->db.db_size, db);
3619 if (dcs == DB_DBUF_METADATA_CACHE) {
3620 DBUF_STAT_BUMP(metadata_cache_count);
3622 metadata_cache_size_bytes_max,
3626 cache_levels[db->db_level]);
3627 DBUF_STAT_BUMP(cache_count);
3629 cache_levels_bytes[db->db_level],
3631 DBUF_STAT_MAX(cache_size_bytes_max,
3634 mutex_exit(&db->db_mtx);
3636 if (dcs == DB_DBUF_CACHE && !evicting)
3637 dbuf_evict_notify(size);
3641 arc_freed(spa, &bp);
3644 mutex_exit(&db->db_mtx);
3649 #pragma weak dmu_buf_refcount = dbuf_refcount
3651 dbuf_refcount(dmu_buf_impl_t *db)
3653 return (zfs_refcount_count(&db->db_holds));
3657 dmu_buf_user_refcount(dmu_buf_t *db_fake)
3660 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
3662 mutex_enter(&db->db_mtx);
3663 ASSERT3U(zfs_refcount_count(&db->db_holds), >=, db->db_dirtycnt);
3664 holds = zfs_refcount_count(&db->db_holds) - db->db_dirtycnt;
3665 mutex_exit(&db->db_mtx);
3671 dmu_buf_replace_user(dmu_buf_t *db_fake, dmu_buf_user_t *old_user,
3672 dmu_buf_user_t *new_user)
3674 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
3676 mutex_enter(&db->db_mtx);
3677 dbuf_verify_user(db, DBVU_NOT_EVICTING);
3678 if (db->db_user == old_user)
3679 db->db_user = new_user;
3681 old_user = db->db_user;
3682 dbuf_verify_user(db, DBVU_NOT_EVICTING);
3683 mutex_exit(&db->db_mtx);
3689 dmu_buf_set_user(dmu_buf_t *db_fake, dmu_buf_user_t *user)
3691 return (dmu_buf_replace_user(db_fake, NULL, user));
3695 dmu_buf_set_user_ie(dmu_buf_t *db_fake, dmu_buf_user_t *user)
3697 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
3699 db->db_user_immediate_evict = TRUE;
3700 return (dmu_buf_set_user(db_fake, user));
3704 dmu_buf_remove_user(dmu_buf_t *db_fake, dmu_buf_user_t *user)
3706 return (dmu_buf_replace_user(db_fake, user, NULL));
3710 dmu_buf_get_user(dmu_buf_t *db_fake)
3712 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
3714 dbuf_verify_user(db, DBVU_NOT_EVICTING);
3715 return (db->db_user);
3719 dmu_buf_user_evict_wait()
3721 taskq_wait(dbu_evict_taskq);
3725 dmu_buf_get_blkptr(dmu_buf_t *db)
3727 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
3728 return (dbi->db_blkptr);
3732 dmu_buf_get_objset(dmu_buf_t *db)
3734 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
3735 return (dbi->db_objset);
3739 dmu_buf_dnode_enter(dmu_buf_t *db)
3741 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
3742 DB_DNODE_ENTER(dbi);
3743 return (DB_DNODE(dbi));
3747 dmu_buf_dnode_exit(dmu_buf_t *db)
3749 dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
3754 dbuf_check_blkptr(dnode_t *dn, dmu_buf_impl_t *db)
3756 /* ASSERT(dmu_tx_is_syncing(tx) */
3757 ASSERT(MUTEX_HELD(&db->db_mtx));
3759 if (db->db_blkptr != NULL)
3762 if (db->db_blkid == DMU_SPILL_BLKID) {
3763 db->db_blkptr = DN_SPILL_BLKPTR(dn->dn_phys);
3764 BP_ZERO(db->db_blkptr);
3767 if (db->db_level == dn->dn_phys->dn_nlevels-1) {
3769 * This buffer was allocated at a time when there was
3770 * no available blkptrs from the dnode, or it was
3771 * inappropriate to hook it in (i.e., nlevels mismatch).
3773 ASSERT(db->db_blkid < dn->dn_phys->dn_nblkptr);
3774 ASSERT(db->db_parent == NULL);
3775 db->db_parent = dn->dn_dbuf;
3776 db->db_blkptr = &dn->dn_phys->dn_blkptr[db->db_blkid];
3779 dmu_buf_impl_t *parent = db->db_parent;
3780 int epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
3782 ASSERT(dn->dn_phys->dn_nlevels > 1);
3783 if (parent == NULL) {
3784 mutex_exit(&db->db_mtx);
3785 rw_enter(&dn->dn_struct_rwlock, RW_READER);
3786 parent = dbuf_hold_level(dn, db->db_level + 1,
3787 db->db_blkid >> epbs, db);
3788 rw_exit(&dn->dn_struct_rwlock);
3789 mutex_enter(&db->db_mtx);
3790 db->db_parent = parent;
3792 db->db_blkptr = (blkptr_t *)parent->db.db_data +
3793 (db->db_blkid & ((1ULL << epbs) - 1));
3799 dbuf_sync_bonus(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
3801 dmu_buf_impl_t *db = dr->dr_dbuf;
3802 void *data = dr->dt.dl.dr_data;
3804 ASSERT0(db->db_level);
3805 ASSERT(MUTEX_HELD(&db->db_mtx));
3806 ASSERT(DB_DNODE_HELD(db));
3807 ASSERT(db->db_blkid == DMU_BONUS_BLKID);
3808 ASSERT(data != NULL);
3810 dnode_t *dn = DB_DNODE(db);
3811 ASSERT3U(DN_MAX_BONUS_LEN(dn->dn_phys), <=,
3812 DN_SLOTS_TO_BONUSLEN(dn->dn_phys->dn_extra_slots + 1));
3813 bcopy(data, DN_BONUS(dn->dn_phys), DN_MAX_BONUS_LEN(dn->dn_phys));
3816 dbuf_sync_leaf_verify_bonus_dnode(dr);
3818 dbuf_undirty_bonus(dr);
3819 dbuf_rele_and_unlock(db, (void *)(uintptr_t)tx->tx_txg, B_FALSE);
3823 * When syncing out a blocks of dnodes, adjust the block to deal with
3824 * encryption. Normally, we make sure the block is decrypted before writing
3825 * it. If we have crypt params, then we are writing a raw (encrypted) block,
3826 * from a raw receive. In this case, set the ARC buf's crypt params so
3827 * that the BP will be filled with the correct byteorder, salt, iv, and mac.
3830 dbuf_prepare_encrypted_dnode_leaf(dbuf_dirty_record_t *dr)
3833 dmu_buf_impl_t *db = dr->dr_dbuf;
3835 ASSERT(MUTEX_HELD(&db->db_mtx));
3836 ASSERT3U(db->db.db_object, ==, DMU_META_DNODE_OBJECT);
3837 ASSERT3U(db->db_level, ==, 0);
3839 if (!db->db_objset->os_raw_receive && arc_is_encrypted(db->db_buf)) {
3840 zbookmark_phys_t zb;
3843 * Unfortunately, there is currently no mechanism for
3844 * syncing context to handle decryption errors. An error
3845 * here is only possible if an attacker maliciously
3846 * changed a dnode block and updated the associated
3847 * checksums going up the block tree.
3849 SET_BOOKMARK(&zb, dmu_objset_id(db->db_objset),
3850 db->db.db_object, db->db_level, db->db_blkid);
3851 err = arc_untransform(db->db_buf, db->db_objset->os_spa,
3854 panic("Invalid dnode block MAC");
3855 } else if (dr->dt.dl.dr_has_raw_params) {
3856 (void) arc_release(dr->dt.dl.dr_data, db);
3857 arc_convert_to_raw(dr->dt.dl.dr_data,
3858 dmu_objset_id(db->db_objset),
3859 dr->dt.dl.dr_byteorder, DMU_OT_DNODE,
3860 dr->dt.dl.dr_salt, dr->dt.dl.dr_iv, dr->dt.dl.dr_mac);
3865 * dbuf_sync_indirect() is called recursively from dbuf_sync_list() so it
3866 * is critical the we not allow the compiler to inline this function in to
3867 * dbuf_sync_list() thereby drastically bloating the stack usage.
3869 noinline static void
3870 dbuf_sync_indirect(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
3872 dmu_buf_impl_t *db = dr->dr_dbuf;
3876 ASSERT(dmu_tx_is_syncing(tx));
3878 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
3880 mutex_enter(&db->db_mtx);
3882 ASSERT(db->db_level > 0);
3885 /* Read the block if it hasn't been read yet. */
3886 if (db->db_buf == NULL) {
3887 mutex_exit(&db->db_mtx);
3888 (void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED);
3889 mutex_enter(&db->db_mtx);
3891 ASSERT3U(db->db_state, ==, DB_CACHED);
3892 ASSERT(db->db_buf != NULL);
3896 /* Indirect block size must match what the dnode thinks it is. */
3897 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
3898 dbuf_check_blkptr(dn, db);
3901 /* Provide the pending dirty record to child dbufs */
3902 db->db_data_pending = dr;
3904 mutex_exit(&db->db_mtx);
3906 dbuf_write(dr, db->db_buf, tx);
3909 mutex_enter(&dr->dt.di.dr_mtx);
3910 dbuf_sync_list(&dr->dt.di.dr_children, db->db_level - 1, tx);
3911 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
3912 mutex_exit(&dr->dt.di.dr_mtx);
3917 * Verify that the size of the data in our bonus buffer does not exceed
3918 * its recorded size.
3920 * The purpose of this verification is to catch any cases in development
3921 * where the size of a phys structure (i.e space_map_phys_t) grows and,
3922 * due to incorrect feature management, older pools expect to read more
3923 * data even though they didn't actually write it to begin with.
3925 * For a example, this would catch an error in the feature logic where we
3926 * open an older pool and we expect to write the space map histogram of
3927 * a space map with size SPACE_MAP_SIZE_V0.
3930 dbuf_sync_leaf_verify_bonus_dnode(dbuf_dirty_record_t *dr)
3933 dnode_t *dn = DB_DNODE(dr->dr_dbuf);
3936 * Encrypted bonus buffers can have data past their bonuslen.
3937 * Skip the verification of these blocks.
3939 if (DMU_OT_IS_ENCRYPTED(dn->dn_bonustype))
3942 uint16_t bonuslen = dn->dn_phys->dn_bonuslen;
3943 uint16_t maxbonuslen = DN_SLOTS_TO_BONUSLEN(dn->dn_num_slots);
3944 ASSERT3U(bonuslen, <=, maxbonuslen);
3946 arc_buf_t *datap = dr->dt.dl.dr_data;
3947 char *datap_end = ((char *)datap) + bonuslen;
3948 char *datap_max = ((char *)datap) + maxbonuslen;
3950 /* ensure that everything is zero after our data */
3951 for (; datap_end < datap_max; datap_end++)
3952 ASSERT(*datap_end == 0);
3957 * dbuf_sync_leaf() is called recursively from dbuf_sync_list() so it is
3958 * critical the we not allow the compiler to inline this function in to
3959 * dbuf_sync_list() thereby drastically bloating the stack usage.
3961 noinline static void
3962 dbuf_sync_leaf(dbuf_dirty_record_t *dr, dmu_tx_t *tx)
3964 arc_buf_t **datap = &dr->dt.dl.dr_data;
3965 dmu_buf_impl_t *db = dr->dr_dbuf;
3968 uint64_t txg = tx->tx_txg;
3970 ASSERT(dmu_tx_is_syncing(tx));
3972 dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
3974 mutex_enter(&db->db_mtx);
3976 * To be synced, we must be dirtied. But we
3977 * might have been freed after the dirty.
3979 if (db->db_state == DB_UNCACHED) {
3980 /* This buffer has been freed since it was dirtied */
3981 ASSERT(db->db.db_data == NULL);
3982 } else if (db->db_state == DB_FILL) {
3983 /* This buffer was freed and is now being re-filled */
3984 ASSERT(db->db.db_data != dr->dt.dl.dr_data);
3986 ASSERT(db->db_state == DB_CACHED || db->db_state == DB_NOFILL);
3993 if (db->db_blkid == DMU_SPILL_BLKID) {
3994 mutex_enter(&dn->dn_mtx);
3995 if (!(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR)) {
3997 * In the previous transaction group, the bonus buffer
3998 * was entirely used to store the attributes for the
3999 * dnode which overrode the dn_spill field. However,
4000 * when adding more attributes to the file a spill
4001 * block was required to hold the extra attributes.
4003 * Make sure to clear the garbage left in the dn_spill
4004 * field from the previous attributes in the bonus
4005 * buffer. Otherwise, after writing out the spill
4006 * block to the new allocated dva, it will free
4007 * the old block pointed to by the invalid dn_spill.
4009 db->db_blkptr = NULL;
4011 dn->dn_phys->dn_flags |= DNODE_FLAG_SPILL_BLKPTR;
4012 mutex_exit(&dn->dn_mtx);
4016 * If this is a bonus buffer, simply copy the bonus data into the
4017 * dnode. It will be written out when the dnode is synced (and it
4018 * will be synced, since it must have been dirty for dbuf_sync to
4021 if (db->db_blkid == DMU_BONUS_BLKID) {
4022 ASSERT(dr->dr_dbuf == db);
4023 dbuf_sync_bonus(dr, tx);
4030 * This function may have dropped the db_mtx lock allowing a dmu_sync
4031 * operation to sneak in. As a result, we need to ensure that we
4032 * don't check the dr_override_state until we have returned from
4033 * dbuf_check_blkptr.
4035 dbuf_check_blkptr(dn, db);
4038 * If this buffer is in the middle of an immediate write,
4039 * wait for the synchronous IO to complete.
4041 while (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC) {
4042 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
4043 cv_wait(&db->db_changed, &db->db_mtx);
4044 ASSERT(dr->dt.dl.dr_override_state != DR_NOT_OVERRIDDEN);
4048 * If this is a dnode block, ensure it is appropriately encrypted
4049 * or decrypted, depending on what we are writing to it this txg.
4051 if (os->os_encrypted && dn->dn_object == DMU_META_DNODE_OBJECT)
4052 dbuf_prepare_encrypted_dnode_leaf(dr);
4054 if (db->db_state != DB_NOFILL &&
4055 dn->dn_object != DMU_META_DNODE_OBJECT &&
4056 zfs_refcount_count(&db->db_holds) > 1 &&
4057 dr->dt.dl.dr_override_state != DR_OVERRIDDEN &&
4058 *datap == db->db_buf) {
4060 * If this buffer is currently "in use" (i.e., there
4061 * are active holds and db_data still references it),
4062 * then make a copy before we start the write so that
4063 * any modifications from the open txg will not leak
4066 * NOTE: this copy does not need to be made for
4067 * objects only modified in the syncing context (e.g.
4068 * DNONE_DNODE blocks).
4070 *datap = dbuf_alloc_arcbuf_from_arcbuf(db, db->db_buf);
4071 bcopy(db->db.db_data, (*datap)->b_data, arc_buf_size(*datap));
4073 db->db_data_pending = dr;
4075 mutex_exit(&db->db_mtx);
4077 dbuf_write(dr, *datap, tx);
4079 ASSERT(!list_link_active(&dr->dr_dirty_node));
4080 if (dn->dn_object == DMU_META_DNODE_OBJECT) {
4081 list_insert_tail(&dn->dn_dirty_records[txg & TXG_MASK], dr);
4085 * Although zio_nowait() does not "wait for an IO", it does
4086 * initiate the IO. If this is an empty write it seems plausible
4087 * that the IO could actually be completed before the nowait
4088 * returns. We need to DB_DNODE_EXIT() first in case
4089 * zio_nowait() invalidates the dbuf.
4092 zio_nowait(dr->dr_zio);
4097 dbuf_sync_list(list_t *list, int level, dmu_tx_t *tx)
4099 dbuf_dirty_record_t *dr;
4101 while ((dr = list_head(list))) {
4102 if (dr->dr_zio != NULL) {
4104 * If we find an already initialized zio then we
4105 * are processing the meta-dnode, and we have finished.
4106 * The dbufs for all dnodes are put back on the list
4107 * during processing, so that we can zio_wait()
4108 * these IOs after initiating all child IOs.
4110 ASSERT3U(dr->dr_dbuf->db.db_object, ==,
4111 DMU_META_DNODE_OBJECT);
4114 if (dr->dr_dbuf->db_blkid != DMU_BONUS_BLKID &&
4115 dr->dr_dbuf->db_blkid != DMU_SPILL_BLKID) {
4116 VERIFY3U(dr->dr_dbuf->db_level, ==, level);
4118 list_remove(list, dr);
4119 if (dr->dr_dbuf->db_level > 0)
4120 dbuf_sync_indirect(dr, tx);
4122 dbuf_sync_leaf(dr, tx);
4128 dbuf_write_ready(zio_t *zio, arc_buf_t *buf, void *vdb)
4130 dmu_buf_impl_t *db = vdb;
4132 blkptr_t *bp = zio->io_bp;
4133 blkptr_t *bp_orig = &zio->io_bp_orig;
4134 spa_t *spa = zio->io_spa;
4139 ASSERT3P(db->db_blkptr, !=, NULL);
4140 ASSERT3P(&db->db_data_pending->dr_bp_copy, ==, bp);
4144 delta = bp_get_dsize_sync(spa, bp) - bp_get_dsize_sync(spa, bp_orig);
4145 dnode_diduse_space(dn, delta - zio->io_prev_space_delta);
4146 zio->io_prev_space_delta = delta;
4148 if (bp->blk_birth != 0) {
4149 ASSERT((db->db_blkid != DMU_SPILL_BLKID &&
4150 BP_GET_TYPE(bp) == dn->dn_type) ||
4151 (db->db_blkid == DMU_SPILL_BLKID &&
4152 BP_GET_TYPE(bp) == dn->dn_bonustype) ||
4153 BP_IS_EMBEDDED(bp));
4154 ASSERT(BP_GET_LEVEL(bp) == db->db_level);
4157 mutex_enter(&db->db_mtx);
4160 if (db->db_blkid == DMU_SPILL_BLKID) {
4161 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
4162 ASSERT(!(BP_IS_HOLE(bp)) &&
4163 db->db_blkptr == DN_SPILL_BLKPTR(dn->dn_phys));
4167 if (db->db_level == 0) {
4168 mutex_enter(&dn->dn_mtx);
4169 if (db->db_blkid > dn->dn_phys->dn_maxblkid &&
4170 db->db_blkid != DMU_SPILL_BLKID) {
4171 ASSERT0(db->db_objset->os_raw_receive);
4172 dn->dn_phys->dn_maxblkid = db->db_blkid;
4174 mutex_exit(&dn->dn_mtx);
4176 if (dn->dn_type == DMU_OT_DNODE) {
4178 while (i < db->db.db_size) {
4180 (void *)(((char *)db->db.db_data) + i);
4182 i += DNODE_MIN_SIZE;
4183 if (dnp->dn_type != DMU_OT_NONE) {
4185 i += dnp->dn_extra_slots *
4190 if (BP_IS_HOLE(bp)) {
4197 blkptr_t *ibp = db->db.db_data;
4198 ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
4199 for (i = db->db.db_size >> SPA_BLKPTRSHIFT; i > 0; i--, ibp++) {
4200 if (BP_IS_HOLE(ibp))
4202 fill += BP_GET_FILL(ibp);
4207 if (!BP_IS_EMBEDDED(bp))
4208 BP_SET_FILL(bp, fill);
4210 mutex_exit(&db->db_mtx);
4212 db_lock_type_t dblt = dmu_buf_lock_parent(db, RW_WRITER, FTAG);
4213 *db->db_blkptr = *bp;
4214 dmu_buf_unlock_parent(db, dblt, FTAG);
4219 * This function gets called just prior to running through the compression
4220 * stage of the zio pipeline. If we're an indirect block comprised of only
4221 * holes, then we want this indirect to be compressed away to a hole. In
4222 * order to do that we must zero out any information about the holes that
4223 * this indirect points to prior to before we try to compress it.
4226 dbuf_write_children_ready(zio_t *zio, arc_buf_t *buf, void *vdb)
4228 dmu_buf_impl_t *db = vdb;
4231 unsigned int epbs, i;
4233 ASSERT3U(db->db_level, >, 0);
4236 epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
4237 ASSERT3U(epbs, <, 31);
4239 /* Determine if all our children are holes */
4240 for (i = 0, bp = db->db.db_data; i < 1ULL << epbs; i++, bp++) {
4241 if (!BP_IS_HOLE(bp))
4246 * If all the children are holes, then zero them all out so that
4247 * we may get compressed away.
4249 if (i == 1ULL << epbs) {
4251 * We only found holes. Grab the rwlock to prevent
4252 * anybody from reading the blocks we're about to
4255 rw_enter(&db->db_rwlock, RW_WRITER);
4256 bzero(db->db.db_data, db->db.db_size);
4257 rw_exit(&db->db_rwlock);
4263 * The SPA will call this callback several times for each zio - once
4264 * for every physical child i/o (zio->io_phys_children times). This
4265 * allows the DMU to monitor the progress of each logical i/o. For example,
4266 * there may be 2 copies of an indirect block, or many fragments of a RAID-Z
4267 * block. There may be a long delay before all copies/fragments are completed,
4268 * so this callback allows us to retire dirty space gradually, as the physical
4273 dbuf_write_physdone(zio_t *zio, arc_buf_t *buf, void *arg)
4275 dmu_buf_impl_t *db = arg;
4276 objset_t *os = db->db_objset;
4277 dsl_pool_t *dp = dmu_objset_pool(os);
4278 dbuf_dirty_record_t *dr;
4281 dr = db->db_data_pending;
4282 ASSERT3U(dr->dr_txg, ==, zio->io_txg);
4285 * The callback will be called io_phys_children times. Retire one
4286 * portion of our dirty space each time we are called. Any rounding
4287 * error will be cleaned up by dbuf_write_done().
4289 delta = dr->dr_accounted / zio->io_phys_children;
4290 dsl_pool_undirty_space(dp, delta, zio->io_txg);
4295 dbuf_write_done(zio_t *zio, arc_buf_t *buf, void *vdb)
4297 dmu_buf_impl_t *db = vdb;
4298 blkptr_t *bp_orig = &zio->io_bp_orig;
4299 blkptr_t *bp = db->db_blkptr;
4300 objset_t *os = db->db_objset;
4301 dmu_tx_t *tx = os->os_synctx;
4302 dbuf_dirty_record_t *dr;
4304 ASSERT0(zio->io_error);
4305 ASSERT(db->db_blkptr == bp);
4308 * For nopwrites and rewrites we ensure that the bp matches our
4309 * original and bypass all the accounting.
4311 if (zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)) {
4312 ASSERT(BP_EQUAL(bp, bp_orig));
4314 dsl_dataset_t *ds = os->os_dsl_dataset;
4315 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
4316 dsl_dataset_block_born(ds, bp, tx);
4319 mutex_enter(&db->db_mtx);
4323 dr = db->db_data_pending;
4324 ASSERT(!list_link_active(&dr->dr_dirty_node));
4325 ASSERT(dr->dr_dbuf == db);
4326 ASSERT(list_next(&db->db_dirty_records, dr) == NULL);
4327 list_remove(&db->db_dirty_records, dr);
4330 if (db->db_blkid == DMU_SPILL_BLKID) {
4335 ASSERT(dn->dn_phys->dn_flags & DNODE_FLAG_SPILL_BLKPTR);
4336 ASSERT(!(BP_IS_HOLE(db->db_blkptr)) &&
4337 db->db_blkptr == DN_SPILL_BLKPTR(dn->dn_phys));
4342 if (db->db_level == 0) {
4343 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
4344 ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN);
4345 if (db->db_state != DB_NOFILL) {
4346 if (dr->dt.dl.dr_data != db->db_buf)
4347 arc_buf_destroy(dr->dt.dl.dr_data, db);
4354 ASSERT(list_head(&dr->dt.di.dr_children) == NULL);
4355 ASSERT3U(db->db.db_size, ==, 1 << dn->dn_phys->dn_indblkshift);
4356 if (!BP_IS_HOLE(db->db_blkptr)) {
4357 int epbs __maybe_unused = dn->dn_phys->dn_indblkshift -
4359 ASSERT3U(db->db_blkid, <=,
4360 dn->dn_phys->dn_maxblkid >> (db->db_level * epbs));
4361 ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==,
4365 mutex_destroy(&dr->dt.di.dr_mtx);
4366 list_destroy(&dr->dt.di.dr_children);
4369 cv_broadcast(&db->db_changed);
4370 ASSERT(db->db_dirtycnt > 0);
4371 db->db_dirtycnt -= 1;
4372 db->db_data_pending = NULL;
4373 dbuf_rele_and_unlock(db, (void *)(uintptr_t)tx->tx_txg, B_FALSE);
4376 * If we didn't do a physical write in this ZIO and we
4377 * still ended up here, it means that the space of the
4378 * dbuf that we just released (and undirtied) above hasn't
4379 * been marked as undirtied in the pool's accounting.
4381 * Thus, we undirty that space in the pool's view of the
4382 * world here. For physical writes this type of update
4383 * happens in dbuf_write_physdone().
4385 * If we did a physical write, cleanup any rounding errors
4386 * that came up due to writing multiple copies of a block
4387 * on disk [see dbuf_write_physdone()].
4389 if (zio->io_phys_children == 0) {
4390 dsl_pool_undirty_space(dmu_objset_pool(os),
4391 dr->dr_accounted, zio->io_txg);
4393 dsl_pool_undirty_space(dmu_objset_pool(os),
4394 dr->dr_accounted % zio->io_phys_children, zio->io_txg);
4397 kmem_free(dr, sizeof (dbuf_dirty_record_t));
4401 dbuf_write_nofill_ready(zio_t *zio)
4403 dbuf_write_ready(zio, NULL, zio->io_private);
4407 dbuf_write_nofill_done(zio_t *zio)
4409 dbuf_write_done(zio, NULL, zio->io_private);
4413 dbuf_write_override_ready(zio_t *zio)
4415 dbuf_dirty_record_t *dr = zio->io_private;
4416 dmu_buf_impl_t *db = dr->dr_dbuf;
4418 dbuf_write_ready(zio, NULL, db);
4422 dbuf_write_override_done(zio_t *zio)
4424 dbuf_dirty_record_t *dr = zio->io_private;
4425 dmu_buf_impl_t *db = dr->dr_dbuf;
4426 blkptr_t *obp = &dr->dt.dl.dr_overridden_by;
4428 mutex_enter(&db->db_mtx);
4429 if (!BP_EQUAL(zio->io_bp, obp)) {
4430 if (!BP_IS_HOLE(obp))
4431 dsl_free(spa_get_dsl(zio->io_spa), zio->io_txg, obp);
4432 arc_release(dr->dt.dl.dr_data, db);
4434 mutex_exit(&db->db_mtx);
4436 dbuf_write_done(zio, NULL, db);
4438 if (zio->io_abd != NULL)
4439 abd_put(zio->io_abd);
4442 typedef struct dbuf_remap_impl_callback_arg {
4444 uint64_t drica_blk_birth;
4446 } dbuf_remap_impl_callback_arg_t;
4449 dbuf_remap_impl_callback(uint64_t vdev, uint64_t offset, uint64_t size,
4452 dbuf_remap_impl_callback_arg_t *drica = arg;
4453 objset_t *os = drica->drica_os;
4454 spa_t *spa = dmu_objset_spa(os);
4455 dmu_tx_t *tx = drica->drica_tx;
4457 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa)));
4459 if (os == spa_meta_objset(spa)) {
4460 spa_vdev_indirect_mark_obsolete(spa, vdev, offset, size, tx);
4462 dsl_dataset_block_remapped(dmu_objset_ds(os), vdev, offset,
4463 size, drica->drica_blk_birth, tx);
4468 dbuf_remap_impl(dnode_t *dn, blkptr_t *bp, krwlock_t *rw, dmu_tx_t *tx)
4470 blkptr_t bp_copy = *bp;
4471 spa_t *spa = dmu_objset_spa(dn->dn_objset);
4472 dbuf_remap_impl_callback_arg_t drica;
4474 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa)));
4476 drica.drica_os = dn->dn_objset;
4477 drica.drica_blk_birth = bp->blk_birth;
4478 drica.drica_tx = tx;
4479 if (spa_remap_blkptr(spa, &bp_copy, dbuf_remap_impl_callback,
4482 * If the blkptr being remapped is tracked by a livelist,
4483 * then we need to make sure the livelist reflects the update.
4484 * First, cancel out the old blkptr by appending a 'FREE'
4485 * entry. Next, add an 'ALLOC' to track the new version. This
4486 * way we avoid trying to free an inaccurate blkptr at delete.
4487 * Note that embedded blkptrs are not tracked in livelists.
4489 if (dn->dn_objset != spa_meta_objset(spa)) {
4490 dsl_dataset_t *ds = dmu_objset_ds(dn->dn_objset);
4491 if (dsl_deadlist_is_open(&ds->ds_dir->dd_livelist) &&
4492 bp->blk_birth > ds->ds_dir->dd_origin_txg) {
4493 ASSERT(!BP_IS_EMBEDDED(bp));
4494 ASSERT(dsl_dir_is_clone(ds->ds_dir));
4495 ASSERT(spa_feature_is_enabled(spa,
4496 SPA_FEATURE_LIVELIST));
4497 bplist_append(&ds->ds_dir->dd_pending_frees,
4499 bplist_append(&ds->ds_dir->dd_pending_allocs,
4505 * The db_rwlock prevents dbuf_read_impl() from
4506 * dereferencing the BP while we are changing it. To
4507 * avoid lock contention, only grab it when we are actually
4511 rw_enter(rw, RW_WRITER);
4519 * Remap any existing BP's to concrete vdevs, if possible.
4522 dbuf_remap(dnode_t *dn, dmu_buf_impl_t *db, dmu_tx_t *tx)
4524 spa_t *spa = dmu_objset_spa(db->db_objset);
4525 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa)));
4527 if (!spa_feature_is_active(spa, SPA_FEATURE_DEVICE_REMOVAL))
4530 if (db->db_level > 0) {
4531 blkptr_t *bp = db->db.db_data;
4532 for (int i = 0; i < db->db.db_size >> SPA_BLKPTRSHIFT; i++) {
4533 dbuf_remap_impl(dn, &bp[i], &db->db_rwlock, tx);
4535 } else if (db->db.db_object == DMU_META_DNODE_OBJECT) {
4536 dnode_phys_t *dnp = db->db.db_data;
4537 ASSERT3U(db->db_dnode_handle->dnh_dnode->dn_type, ==,
4539 for (int i = 0; i < db->db.db_size >> DNODE_SHIFT;
4540 i += dnp[i].dn_extra_slots + 1) {
4541 for (int j = 0; j < dnp[i].dn_nblkptr; j++) {
4542 krwlock_t *lock = (dn->dn_dbuf == NULL ? NULL :
4543 &dn->dn_dbuf->db_rwlock);
4544 dbuf_remap_impl(dn, &dnp[i].dn_blkptr[j], lock,
4552 /* Issue I/O to commit a dirty buffer to disk. */
4554 dbuf_write(dbuf_dirty_record_t *dr, arc_buf_t *data, dmu_tx_t *tx)
4556 dmu_buf_impl_t *db = dr->dr_dbuf;
4559 dmu_buf_impl_t *parent = db->db_parent;
4560 uint64_t txg = tx->tx_txg;
4561 zbookmark_phys_t zb;
4563 zio_t *pio; /* parent I/O */
4566 ASSERT(dmu_tx_is_syncing(tx));
4572 if (db->db_state != DB_NOFILL) {
4573 if (db->db_level > 0 || dn->dn_type == DMU_OT_DNODE) {
4575 * Private object buffers are released here rather
4576 * than in dbuf_dirty() since they are only modified
4577 * in the syncing context and we don't want the
4578 * overhead of making multiple copies of the data.
4580 if (BP_IS_HOLE(db->db_blkptr)) {
4583 dbuf_release_bp(db);
4585 dbuf_remap(dn, db, tx);
4589 if (parent != dn->dn_dbuf) {
4590 /* Our parent is an indirect block. */
4591 /* We have a dirty parent that has been scheduled for write. */
4592 ASSERT(parent && parent->db_data_pending);
4593 /* Our parent's buffer is one level closer to the dnode. */
4594 ASSERT(db->db_level == parent->db_level-1);
4596 * We're about to modify our parent's db_data by modifying
4597 * our block pointer, so the parent must be released.
4599 ASSERT(arc_released(parent->db_buf));
4600 pio = parent->db_data_pending->dr_zio;
4602 /* Our parent is the dnode itself. */
4603 ASSERT((db->db_level == dn->dn_phys->dn_nlevels-1 &&
4604 db->db_blkid != DMU_SPILL_BLKID) ||
4605 (db->db_blkid == DMU_SPILL_BLKID && db->db_level == 0));
4606 if (db->db_blkid != DMU_SPILL_BLKID)
4607 ASSERT3P(db->db_blkptr, ==,
4608 &dn->dn_phys->dn_blkptr[db->db_blkid]);
4612 ASSERT(db->db_level == 0 || data == db->db_buf);
4613 ASSERT3U(db->db_blkptr->blk_birth, <=, txg);
4616 SET_BOOKMARK(&zb, os->os_dsl_dataset ?
4617 os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
4618 db->db.db_object, db->db_level, db->db_blkid);
4620 if (db->db_blkid == DMU_SPILL_BLKID)
4622 wp_flag |= (db->db_state == DB_NOFILL) ? WP_NOFILL : 0;
4624 dmu_write_policy(os, dn, db->db_level, wp_flag, &zp);
4628 * We copy the blkptr now (rather than when we instantiate the dirty
4629 * record), because its value can change between open context and
4630 * syncing context. We do not need to hold dn_struct_rwlock to read
4631 * db_blkptr because we are in syncing context.
4633 dr->dr_bp_copy = *db->db_blkptr;
4635 if (db->db_level == 0 &&
4636 dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
4638 * The BP for this block has been provided by open context
4639 * (by dmu_sync() or dmu_buf_write_embedded()).
4641 abd_t *contents = (data != NULL) ?
4642 abd_get_from_buf(data->b_data, arc_buf_size(data)) : NULL;
4644 dr->dr_zio = zio_write(pio, os->os_spa, txg, &dr->dr_bp_copy,
4645 contents, db->db.db_size, db->db.db_size, &zp,
4646 dbuf_write_override_ready, NULL, NULL,
4647 dbuf_write_override_done,
4648 dr, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
4649 mutex_enter(&db->db_mtx);
4650 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
4651 zio_write_override(dr->dr_zio, &dr->dt.dl.dr_overridden_by,
4652 dr->dt.dl.dr_copies, dr->dt.dl.dr_nopwrite);
4653 mutex_exit(&db->db_mtx);
4654 } else if (db->db_state == DB_NOFILL) {
4655 ASSERT(zp.zp_checksum == ZIO_CHECKSUM_OFF ||
4656 zp.zp_checksum == ZIO_CHECKSUM_NOPARITY);
4657 dr->dr_zio = zio_write(pio, os->os_spa, txg,
4658 &dr->dr_bp_copy, NULL, db->db.db_size, db->db.db_size, &zp,
4659 dbuf_write_nofill_ready, NULL, NULL,
4660 dbuf_write_nofill_done, db,
4661 ZIO_PRIORITY_ASYNC_WRITE,
4662 ZIO_FLAG_MUSTSUCCEED | ZIO_FLAG_NODATA, &zb);
4664 ASSERT(arc_released(data));
4667 * For indirect blocks, we want to setup the children
4668 * ready callback so that we can properly handle an indirect
4669 * block that only contains holes.
4671 arc_write_done_func_t *children_ready_cb = NULL;
4672 if (db->db_level != 0)
4673 children_ready_cb = dbuf_write_children_ready;
4675 dr->dr_zio = arc_write(pio, os->os_spa, txg,
4676 &dr->dr_bp_copy, data, DBUF_IS_L2CACHEABLE(db),
4677 &zp, dbuf_write_ready,
4678 children_ready_cb, dbuf_write_physdone,
4679 dbuf_write_done, db, ZIO_PRIORITY_ASYNC_WRITE,
4680 ZIO_FLAG_MUSTSUCCEED, &zb);
4684 EXPORT_SYMBOL(dbuf_find);
4685 EXPORT_SYMBOL(dbuf_is_metadata);
4686 EXPORT_SYMBOL(dbuf_destroy);
4687 EXPORT_SYMBOL(dbuf_loan_arcbuf);
4688 EXPORT_SYMBOL(dbuf_whichblock);
4689 EXPORT_SYMBOL(dbuf_read);
4690 EXPORT_SYMBOL(dbuf_unoverride);
4691 EXPORT_SYMBOL(dbuf_free_range);
4692 EXPORT_SYMBOL(dbuf_new_size);
4693 EXPORT_SYMBOL(dbuf_release_bp);
4694 EXPORT_SYMBOL(dbuf_dirty);
4695 EXPORT_SYMBOL(dmu_buf_set_crypt_params);
4696 EXPORT_SYMBOL(dmu_buf_will_dirty);
4697 EXPORT_SYMBOL(dmu_buf_is_dirty);
4698 EXPORT_SYMBOL(dmu_buf_will_not_fill);
4699 EXPORT_SYMBOL(dmu_buf_will_fill);
4700 EXPORT_SYMBOL(dmu_buf_fill_done);
4701 EXPORT_SYMBOL(dmu_buf_rele);
4702 EXPORT_SYMBOL(dbuf_assign_arcbuf);
4703 EXPORT_SYMBOL(dbuf_prefetch);
4704 EXPORT_SYMBOL(dbuf_hold_impl);
4705 EXPORT_SYMBOL(dbuf_hold);
4706 EXPORT_SYMBOL(dbuf_hold_level);
4707 EXPORT_SYMBOL(dbuf_create_bonus);
4708 EXPORT_SYMBOL(dbuf_spill_set_blksz);
4709 EXPORT_SYMBOL(dbuf_rm_spill);
4710 EXPORT_SYMBOL(dbuf_add_ref);
4711 EXPORT_SYMBOL(dbuf_rele);
4712 EXPORT_SYMBOL(dbuf_rele_and_unlock);
4713 EXPORT_SYMBOL(dbuf_refcount);
4714 EXPORT_SYMBOL(dbuf_sync_list);
4715 EXPORT_SYMBOL(dmu_buf_set_user);
4716 EXPORT_SYMBOL(dmu_buf_set_user_ie);
4717 EXPORT_SYMBOL(dmu_buf_get_user);
4718 EXPORT_SYMBOL(dmu_buf_get_blkptr);
4721 ZFS_MODULE_PARAM(zfs_dbuf_cache, dbuf_cache_, max_bytes, ULONG, ZMOD_RW,
4722 "Maximum size in bytes of the dbuf cache.");
4724 ZFS_MODULE_PARAM(zfs_dbuf_cache, dbuf_cache_, hiwater_pct, UINT, ZMOD_RW,
4725 "Percentage over dbuf_cache_max_bytes when dbufs must be evicted "
4728 ZFS_MODULE_PARAM(zfs_dbuf_cache, dbuf_cache_, lowater_pct, UINT, ZMOD_RW,
4729 "Percentage below dbuf_cache_max_bytes when the evict thread stops "
4732 ZFS_MODULE_PARAM(zfs_dbuf, dbuf_, metadata_cache_max_bytes, ULONG, ZMOD_RW,
4733 "Maximum size in bytes of the dbuf metadata cache.");
4735 ZFS_MODULE_PARAM(zfs_dbuf, dbuf_, cache_shift, INT, ZMOD_RW,
4736 "Set the size of the dbuf cache to a log2 fraction of arc size.");
4738 ZFS_MODULE_PARAM(zfs_dbuf, dbuf_, metadata_cache_shift, INT, ZMOD_RW,
4739 "Set the size of the dbuf metadata cache to a log2 fraction of arc "