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, 2017 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.
29 #include <sys/zfs_context.h>
32 #include <sys/dmu_send.h>
33 #include <sys/dmu_impl.h>
35 #include <sys/dmu_objset.h>
36 #include <sys/dsl_dataset.h>
37 #include <sys/dsl_dir.h>
38 #include <sys/dmu_tx.h>
41 #include <sys/dmu_zfetch.h>
43 #include <sys/sa_impl.h>
44 #include <sys/zfeature.h>
45 #include <sys/blkptr.h>
46 #include <sys/range_tree.h>
47 #include <sys/trace_dbuf.h>
48 #include <sys/callb.h>
51 #include <sys/cityhash.h>
55 typedef struct dbuf_stats
{
57 * Various statistics about the size of the dbuf cache.
59 kstat_named_t cache_count
;
60 kstat_named_t cache_size_bytes
;
61 kstat_named_t cache_size_bytes_max
;
63 * Statistics regarding the bounds on the dbuf cache size.
65 kstat_named_t cache_target_bytes
;
66 kstat_named_t cache_lowater_bytes
;
67 kstat_named_t cache_hiwater_bytes
;
69 * Total number of dbuf cache evictions that have occurred.
71 kstat_named_t cache_total_evicts
;
73 * The distribution of dbuf levels in the dbuf cache and
74 * the total size of all dbufs at each level.
76 kstat_named_t cache_levels
[DN_MAX_LEVELS
];
77 kstat_named_t cache_levels_bytes
[DN_MAX_LEVELS
];
79 * Statistics about the dbuf hash table.
81 kstat_named_t hash_hits
;
82 kstat_named_t hash_misses
;
83 kstat_named_t hash_collisions
;
84 kstat_named_t hash_elements
;
85 kstat_named_t hash_elements_max
;
87 * Number of sublists containing more than one dbuf in the dbuf
88 * hash table. Keep track of the longest hash chain.
90 kstat_named_t hash_chains
;
91 kstat_named_t hash_chain_max
;
93 * Number of times a dbuf_create() discovers that a dbuf was
94 * already created and in the dbuf hash table.
96 kstat_named_t hash_insert_race
;
99 dbuf_stats_t dbuf_stats
= {
100 { "cache_count", KSTAT_DATA_UINT64
},
101 { "cache_size_bytes", KSTAT_DATA_UINT64
},
102 { "cache_size_bytes_max", KSTAT_DATA_UINT64
},
103 { "cache_target_bytes", KSTAT_DATA_UINT64
},
104 { "cache_lowater_bytes", KSTAT_DATA_UINT64
},
105 { "cache_hiwater_bytes", KSTAT_DATA_UINT64
},
106 { "cache_total_evicts", KSTAT_DATA_UINT64
},
107 { { "cache_levels_N", KSTAT_DATA_UINT64
} },
108 { { "cache_levels_bytes_N", KSTAT_DATA_UINT64
} },
109 { "hash_hits", KSTAT_DATA_UINT64
},
110 { "hash_misses", KSTAT_DATA_UINT64
},
111 { "hash_collisions", KSTAT_DATA_UINT64
},
112 { "hash_elements", KSTAT_DATA_UINT64
},
113 { "hash_elements_max", KSTAT_DATA_UINT64
},
114 { "hash_chains", KSTAT_DATA_UINT64
},
115 { "hash_chain_max", KSTAT_DATA_UINT64
},
116 { "hash_insert_race", KSTAT_DATA_UINT64
}
119 #define DBUF_STAT_INCR(stat, val) \
120 atomic_add_64(&dbuf_stats.stat.value.ui64, (val));
121 #define DBUF_STAT_DECR(stat, val) \
122 DBUF_STAT_INCR(stat, -(val));
123 #define DBUF_STAT_BUMP(stat) \
124 DBUF_STAT_INCR(stat, 1);
125 #define DBUF_STAT_BUMPDOWN(stat) \
126 DBUF_STAT_INCR(stat, -1);
127 #define DBUF_STAT_MAX(stat, v) { \
129 while ((v) > (_m = dbuf_stats.stat.value.ui64) && \
130 (_m != atomic_cas_64(&dbuf_stats.stat.value.ui64, _m, (v))))\
134 struct dbuf_hold_impl_data
{
135 /* Function arguments */
139 boolean_t dh_fail_sparse
;
140 boolean_t dh_fail_uncached
;
142 dmu_buf_impl_t
**dh_dbp
;
143 /* Local variables */
144 dmu_buf_impl_t
*dh_db
;
145 dmu_buf_impl_t
*dh_parent
;
148 dbuf_dirty_record_t
*dh_dr
;
152 static void __dbuf_hold_impl_init(struct dbuf_hold_impl_data
*dh
,
153 dnode_t
*dn
, uint8_t level
, uint64_t blkid
, boolean_t fail_sparse
,
154 boolean_t fail_uncached
,
155 void *tag
, dmu_buf_impl_t
**dbp
, int depth
);
156 static int __dbuf_hold_impl(struct dbuf_hold_impl_data
*dh
);
158 static boolean_t
dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
);
159 static void dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
);
161 extern inline void dmu_buf_init_user(dmu_buf_user_t
*dbu
,
162 dmu_buf_evict_func_t
*evict_func_sync
,
163 dmu_buf_evict_func_t
*evict_func_async
,
164 dmu_buf_t
**clear_on_evict_dbufp
);
167 * Global data structures and functions for the dbuf cache.
169 static kmem_cache_t
*dbuf_kmem_cache
;
170 static taskq_t
*dbu_evict_taskq
;
172 static kthread_t
*dbuf_cache_evict_thread
;
173 static kmutex_t dbuf_evict_lock
;
174 static kcondvar_t dbuf_evict_cv
;
175 static boolean_t dbuf_evict_thread_exit
;
178 * LRU cache of dbufs. The dbuf cache maintains a list of dbufs that
179 * are not currently held but have been recently released. These dbufs
180 * are not eligible for arc eviction until they are aged out of the cache.
181 * Dbufs are added to the dbuf cache once the last hold is released. If a
182 * dbuf is later accessed and still exists in the dbuf cache, then it will
183 * be removed from the cache and later re-added to the head of the cache.
184 * Dbufs that are aged out of the cache will be immediately destroyed and
185 * become eligible for arc eviction.
187 static multilist_t
*dbuf_cache
;
188 static refcount_t dbuf_cache_size
;
189 unsigned long dbuf_cache_max_bytes
= 0;
191 /* Set the default size of the dbuf cache to log2 fraction of arc size. */
192 int dbuf_cache_shift
= 5;
195 * The dbuf cache uses a three-stage eviction policy:
196 * - A low water marker designates when the dbuf eviction thread
197 * should stop evicting from the dbuf cache.
198 * - When we reach the maximum size (aka mid water mark), we
199 * signal the eviction thread to run.
200 * - The high water mark indicates when the eviction thread
201 * is unable to keep up with the incoming load and eviction must
202 * happen in the context of the calling thread.
206 * low water mid water hi water
207 * +----------------------------------------+----------+----------+
212 * +----------------------------------------+----------+----------+
214 * evicting eviction directly
217 * The high and low water marks indicate the operating range for the eviction
218 * thread. The low water mark is, by default, 90% of the total size of the
219 * cache and the high water mark is at 110% (both of these percentages can be
220 * changed by setting dbuf_cache_lowater_pct and dbuf_cache_hiwater_pct,
221 * respectively). The eviction thread will try to ensure that the cache remains
222 * within this range by waking up every second and checking if the cache is
223 * above the low water mark. The thread can also be woken up by callers adding
224 * elements into the cache if the cache is larger than the mid water (i.e max
225 * cache size). Once the eviction thread is woken up and eviction is required,
226 * it will continue evicting buffers until it's able to reduce the cache size
227 * to the low water mark. If the cache size continues to grow and hits the high
228 * water mark, then callers adding elements to the cache will begin to evict
229 * directly from the cache until the cache is no longer above the high water
234 * The percentage above and below the maximum cache size.
236 uint_t dbuf_cache_hiwater_pct
= 10;
237 uint_t dbuf_cache_lowater_pct
= 10;
241 dbuf_cons(void *vdb
, void *unused
, int kmflag
)
243 dmu_buf_impl_t
*db
= vdb
;
244 bzero(db
, sizeof (dmu_buf_impl_t
));
246 mutex_init(&db
->db_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
247 cv_init(&db
->db_changed
, NULL
, CV_DEFAULT
, NULL
);
248 multilist_link_init(&db
->db_cache_link
);
249 refcount_create(&db
->db_holds
);
256 dbuf_dest(void *vdb
, void *unused
)
258 dmu_buf_impl_t
*db
= vdb
;
259 mutex_destroy(&db
->db_mtx
);
260 cv_destroy(&db
->db_changed
);
261 ASSERT(!multilist_link_active(&db
->db_cache_link
));
262 refcount_destroy(&db
->db_holds
);
266 * dbuf hash table routines
268 static dbuf_hash_table_t dbuf_hash_table
;
270 static uint64_t dbuf_hash_count
;
273 * We use Cityhash for this. It's fast, and has good hash properties without
274 * requiring any large static buffers.
277 dbuf_hash(void *os
, uint64_t obj
, uint8_t lvl
, uint64_t blkid
)
279 return (cityhash4((uintptr_t)os
, obj
, (uint64_t)lvl
, blkid
));
282 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
283 ((dbuf)->db.db_object == (obj) && \
284 (dbuf)->db_objset == (os) && \
285 (dbuf)->db_level == (level) && \
286 (dbuf)->db_blkid == (blkid))
289 dbuf_find(objset_t
*os
, uint64_t obj
, uint8_t level
, uint64_t blkid
)
291 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
296 hv
= dbuf_hash(os
, obj
, level
, blkid
);
297 idx
= hv
& h
->hash_table_mask
;
299 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
300 for (db
= h
->hash_table
[idx
]; db
!= NULL
; db
= db
->db_hash_next
) {
301 if (DBUF_EQUAL(db
, os
, obj
, level
, blkid
)) {
302 mutex_enter(&db
->db_mtx
);
303 if (db
->db_state
!= DB_EVICTING
) {
304 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
307 mutex_exit(&db
->db_mtx
);
310 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
314 static dmu_buf_impl_t
*
315 dbuf_find_bonus(objset_t
*os
, uint64_t object
)
318 dmu_buf_impl_t
*db
= NULL
;
320 if (dnode_hold(os
, object
, FTAG
, &dn
) == 0) {
321 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
322 if (dn
->dn_bonus
!= NULL
) {
324 mutex_enter(&db
->db_mtx
);
326 rw_exit(&dn
->dn_struct_rwlock
);
327 dnode_rele(dn
, FTAG
);
333 * Insert an entry into the hash table. If there is already an element
334 * equal to elem in the hash table, then the already existing element
335 * will be returned and the new element will not be inserted.
336 * Otherwise returns NULL.
338 static dmu_buf_impl_t
*
339 dbuf_hash_insert(dmu_buf_impl_t
*db
)
341 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
342 objset_t
*os
= db
->db_objset
;
343 uint64_t obj
= db
->db
.db_object
;
344 int level
= db
->db_level
;
345 uint64_t blkid
, hv
, idx
;
349 blkid
= db
->db_blkid
;
350 hv
= dbuf_hash(os
, obj
, level
, blkid
);
351 idx
= hv
& h
->hash_table_mask
;
353 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
354 for (dbf
= h
->hash_table
[idx
], i
= 0; dbf
!= NULL
;
355 dbf
= dbf
->db_hash_next
, i
++) {
356 if (DBUF_EQUAL(dbf
, os
, obj
, level
, blkid
)) {
357 mutex_enter(&dbf
->db_mtx
);
358 if (dbf
->db_state
!= DB_EVICTING
) {
359 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
362 mutex_exit(&dbf
->db_mtx
);
367 DBUF_STAT_BUMP(hash_collisions
);
369 DBUF_STAT_BUMP(hash_chains
);
371 DBUF_STAT_MAX(hash_chain_max
, i
);
374 mutex_enter(&db
->db_mtx
);
375 db
->db_hash_next
= h
->hash_table
[idx
];
376 h
->hash_table
[idx
] = db
;
377 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
378 atomic_inc_64(&dbuf_hash_count
);
379 DBUF_STAT_MAX(hash_elements_max
, dbuf_hash_count
);
385 * Remove an entry from the hash table. It must be in the EVICTING state.
388 dbuf_hash_remove(dmu_buf_impl_t
*db
)
390 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
392 dmu_buf_impl_t
*dbf
, **dbp
;
394 hv
= dbuf_hash(db
->db_objset
, db
->db
.db_object
,
395 db
->db_level
, db
->db_blkid
);
396 idx
= hv
& h
->hash_table_mask
;
399 * We mustn't hold db_mtx to maintain lock ordering:
400 * DBUF_HASH_MUTEX > db_mtx.
402 ASSERT(refcount_is_zero(&db
->db_holds
));
403 ASSERT(db
->db_state
== DB_EVICTING
);
404 ASSERT(!MUTEX_HELD(&db
->db_mtx
));
406 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
407 dbp
= &h
->hash_table
[idx
];
408 while ((dbf
= *dbp
) != db
) {
409 dbp
= &dbf
->db_hash_next
;
412 *dbp
= db
->db_hash_next
;
413 db
->db_hash_next
= NULL
;
414 if (h
->hash_table
[idx
] &&
415 h
->hash_table
[idx
]->db_hash_next
== NULL
)
416 DBUF_STAT_BUMPDOWN(hash_chains
);
417 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
418 atomic_dec_64(&dbuf_hash_count
);
424 } dbvu_verify_type_t
;
427 dbuf_verify_user(dmu_buf_impl_t
*db
, dbvu_verify_type_t verify_type
)
432 if (db
->db_user
== NULL
)
435 /* Only data blocks support the attachment of user data. */
436 ASSERT(db
->db_level
== 0);
438 /* Clients must resolve a dbuf before attaching user data. */
439 ASSERT(db
->db
.db_data
!= NULL
);
440 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
442 holds
= refcount_count(&db
->db_holds
);
443 if (verify_type
== DBVU_EVICTING
) {
445 * Immediate eviction occurs when holds == dirtycnt.
446 * For normal eviction buffers, holds is zero on
447 * eviction, except when dbuf_fix_old_data() calls
448 * dbuf_clear_data(). However, the hold count can grow
449 * during eviction even though db_mtx is held (see
450 * dmu_bonus_hold() for an example), so we can only
451 * test the generic invariant that holds >= dirtycnt.
453 ASSERT3U(holds
, >=, db
->db_dirtycnt
);
455 if (db
->db_user_immediate_evict
== TRUE
)
456 ASSERT3U(holds
, >=, db
->db_dirtycnt
);
458 ASSERT3U(holds
, >, 0);
464 dbuf_evict_user(dmu_buf_impl_t
*db
)
466 dmu_buf_user_t
*dbu
= db
->db_user
;
468 ASSERT(MUTEX_HELD(&db
->db_mtx
));
473 dbuf_verify_user(db
, DBVU_EVICTING
);
477 if (dbu
->dbu_clear_on_evict_dbufp
!= NULL
)
478 *dbu
->dbu_clear_on_evict_dbufp
= NULL
;
482 * There are two eviction callbacks - one that we call synchronously
483 * and one that we invoke via a taskq. The async one is useful for
484 * avoiding lock order reversals and limiting stack depth.
486 * Note that if we have a sync callback but no async callback,
487 * it's likely that the sync callback will free the structure
488 * containing the dbu. In that case we need to take care to not
489 * dereference dbu after calling the sync evict func.
491 boolean_t has_async
= (dbu
->dbu_evict_func_async
!= NULL
);
493 if (dbu
->dbu_evict_func_sync
!= NULL
)
494 dbu
->dbu_evict_func_sync(dbu
);
497 taskq_dispatch_ent(dbu_evict_taskq
, dbu
->dbu_evict_func_async
,
498 dbu
, 0, &dbu
->dbu_tqent
);
503 dbuf_is_metadata(dmu_buf_impl_t
*db
)
506 * Consider indirect blocks and spill blocks to be meta data.
508 if (db
->db_level
> 0 || db
->db_blkid
== DMU_SPILL_BLKID
) {
511 boolean_t is_metadata
;
514 is_metadata
= DMU_OT_IS_METADATA(DB_DNODE(db
)->dn_type
);
517 return (is_metadata
);
523 * This function *must* return indices evenly distributed between all
524 * sublists of the multilist. This is needed due to how the dbuf eviction
525 * code is laid out; dbuf_evict_thread() assumes dbufs are evenly
526 * distributed between all sublists and uses this assumption when
527 * deciding which sublist to evict from and how much to evict from it.
530 dbuf_cache_multilist_index_func(multilist_t
*ml
, void *obj
)
532 dmu_buf_impl_t
*db
= obj
;
535 * The assumption here, is the hash value for a given
536 * dmu_buf_impl_t will remain constant throughout it's lifetime
537 * (i.e. it's objset, object, level and blkid fields don't change).
538 * Thus, we don't need to store the dbuf's sublist index
539 * on insertion, as this index can be recalculated on removal.
541 * Also, the low order bits of the hash value are thought to be
542 * distributed evenly. Otherwise, in the case that the multilist
543 * has a power of two number of sublists, each sublists' usage
544 * would not be evenly distributed.
546 return (dbuf_hash(db
->db_objset
, db
->db
.db_object
,
547 db
->db_level
, db
->db_blkid
) %
548 multilist_get_num_sublists(ml
));
551 static inline unsigned long
552 dbuf_cache_target_bytes(void)
554 return MIN(dbuf_cache_max_bytes
,
555 arc_target_bytes() >> dbuf_cache_shift
);
558 static inline uint64_t
559 dbuf_cache_hiwater_bytes(void)
561 uint64_t dbuf_cache_target
= dbuf_cache_target_bytes();
562 return (dbuf_cache_target
+
563 (dbuf_cache_target
* dbuf_cache_hiwater_pct
) / 100);
566 static inline uint64_t
567 dbuf_cache_lowater_bytes(void)
569 uint64_t dbuf_cache_target
= dbuf_cache_target_bytes();
570 return (dbuf_cache_target
-
571 (dbuf_cache_target
* dbuf_cache_lowater_pct
) / 100);
574 static inline boolean_t
575 dbuf_cache_above_hiwater(void)
577 return (refcount_count(&dbuf_cache_size
) > dbuf_cache_hiwater_bytes());
580 static inline boolean_t
581 dbuf_cache_above_lowater(void)
583 return (refcount_count(&dbuf_cache_size
) > dbuf_cache_lowater_bytes());
587 * Evict the oldest eligible dbuf from the dbuf cache.
592 int idx
= multilist_get_random_index(dbuf_cache
);
593 multilist_sublist_t
*mls
= multilist_sublist_lock(dbuf_cache
, idx
);
595 ASSERT(!MUTEX_HELD(&dbuf_evict_lock
));
597 dmu_buf_impl_t
*db
= multilist_sublist_tail(mls
);
598 while (db
!= NULL
&& mutex_tryenter(&db
->db_mtx
) == 0) {
599 db
= multilist_sublist_prev(mls
, db
);
602 DTRACE_PROBE2(dbuf__evict__one
, dmu_buf_impl_t
*, db
,
603 multilist_sublist_t
*, mls
);
606 multilist_sublist_remove(mls
, db
);
607 multilist_sublist_unlock(mls
);
608 (void) refcount_remove_many(&dbuf_cache_size
,
610 DBUF_STAT_BUMPDOWN(cache_levels
[db
->db_level
]);
611 DBUF_STAT_BUMPDOWN(cache_count
);
612 DBUF_STAT_DECR(cache_levels_bytes
[db
->db_level
],
615 DBUF_STAT_MAX(cache_size_bytes_max
,
616 refcount_count(&dbuf_cache_size
));
617 DBUF_STAT_BUMP(cache_total_evicts
);
619 multilist_sublist_unlock(mls
);
624 * The dbuf evict thread is responsible for aging out dbufs from the
625 * cache. Once the cache has reached it's maximum size, dbufs are removed
626 * and destroyed. The eviction thread will continue running until the size
627 * of the dbuf cache is at or below the maximum size. Once the dbuf is aged
628 * out of the cache it is destroyed and becomes eligible for arc eviction.
632 dbuf_evict_thread(void *unused
)
636 CALLB_CPR_INIT(&cpr
, &dbuf_evict_lock
, callb_generic_cpr
, FTAG
);
638 mutex_enter(&dbuf_evict_lock
);
639 while (!dbuf_evict_thread_exit
) {
640 while (!dbuf_cache_above_lowater() && !dbuf_evict_thread_exit
) {
641 CALLB_CPR_SAFE_BEGIN(&cpr
);
642 (void) cv_timedwait_sig_hires(&dbuf_evict_cv
,
643 &dbuf_evict_lock
, SEC2NSEC(1), MSEC2NSEC(1), 0);
644 CALLB_CPR_SAFE_END(&cpr
, &dbuf_evict_lock
);
646 mutex_exit(&dbuf_evict_lock
);
649 * Keep evicting as long as we're above the low water mark
650 * for the cache. We do this without holding the locks to
651 * minimize lock contention.
653 while (dbuf_cache_above_lowater() && !dbuf_evict_thread_exit
) {
657 mutex_enter(&dbuf_evict_lock
);
660 dbuf_evict_thread_exit
= B_FALSE
;
661 cv_broadcast(&dbuf_evict_cv
);
662 CALLB_CPR_EXIT(&cpr
); /* drops dbuf_evict_lock */
667 * Wake up the dbuf eviction thread if the dbuf cache is at its max size.
668 * If the dbuf cache is at its high water mark, then evict a dbuf from the
669 * dbuf cache using the callers context.
672 dbuf_evict_notify(void)
675 * We check if we should evict without holding the dbuf_evict_lock,
676 * because it's OK to occasionally make the wrong decision here,
677 * and grabbing the lock results in massive lock contention.
679 if (refcount_count(&dbuf_cache_size
) > dbuf_cache_target_bytes()) {
680 if (dbuf_cache_above_hiwater())
682 cv_signal(&dbuf_evict_cv
);
687 dbuf_kstat_update(kstat_t
*ksp
, int rw
)
689 dbuf_stats_t
*ds
= ksp
->ks_data
;
691 if (rw
== KSTAT_WRITE
) {
692 return (SET_ERROR(EACCES
));
694 ds
->cache_size_bytes
.value
.ui64
=
695 refcount_count(&dbuf_cache_size
);
696 ds
->cache_target_bytes
.value
.ui64
= dbuf_cache_target_bytes();
697 ds
->cache_hiwater_bytes
.value
.ui64
= dbuf_cache_hiwater_bytes();
698 ds
->cache_lowater_bytes
.value
.ui64
= dbuf_cache_lowater_bytes();
699 ds
->hash_elements
.value
.ui64
= dbuf_hash_count
;
708 uint64_t hsize
= 1ULL << 16;
709 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
713 * The hash table is big enough to fill all of physical memory
714 * with an average block size of zfs_arc_average_blocksize (default 8K).
715 * By default, the table will take up
716 * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers).
718 while (hsize
* zfs_arc_average_blocksize
< physmem
* PAGESIZE
)
722 h
->hash_table_mask
= hsize
- 1;
725 * Large allocations which do not require contiguous pages
726 * should be using vmem_alloc() in the linux kernel
728 h
->hash_table
= vmem_zalloc(hsize
* sizeof (void *), KM_SLEEP
);
730 h
->hash_table
= kmem_zalloc(hsize
* sizeof (void *), KM_NOSLEEP
);
732 if (h
->hash_table
== NULL
) {
733 /* XXX - we should really return an error instead of assert */
734 ASSERT(hsize
> (1ULL << 10));
739 dbuf_kmem_cache
= kmem_cache_create("dmu_buf_impl_t",
740 sizeof (dmu_buf_impl_t
),
741 0, dbuf_cons
, dbuf_dest
, NULL
, NULL
, NULL
, 0);
743 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
744 mutex_init(&h
->hash_mutexes
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
749 * Setup the parameters for the dbuf cache. We set the size of the
750 * dbuf cache to 1/32nd (default) of the target size of the ARC. If
751 * the value has been specified as a module option and it's not
752 * greater than the target size of the ARC, then we honor that value.
754 if (dbuf_cache_max_bytes
== 0 ||
755 dbuf_cache_max_bytes
>= arc_target_bytes()) {
756 dbuf_cache_max_bytes
= arc_target_bytes() >> dbuf_cache_shift
;
760 * All entries are queued via taskq_dispatch_ent(), so min/maxalloc
761 * configuration is not required.
763 dbu_evict_taskq
= taskq_create("dbu_evict", 1, defclsyspri
, 0, 0, 0);
765 dbuf_cache
= multilist_create(sizeof (dmu_buf_impl_t
),
766 offsetof(dmu_buf_impl_t
, db_cache_link
),
767 dbuf_cache_multilist_index_func
);
768 refcount_create(&dbuf_cache_size
);
770 dbuf_evict_thread_exit
= B_FALSE
;
771 mutex_init(&dbuf_evict_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
772 cv_init(&dbuf_evict_cv
, NULL
, CV_DEFAULT
, NULL
);
773 dbuf_cache_evict_thread
= thread_create(NULL
, 0, dbuf_evict_thread
,
774 NULL
, 0, &p0
, TS_RUN
, minclsyspri
);
776 dbuf_ksp
= kstat_create("zfs", 0, "dbufstats", "misc",
777 KSTAT_TYPE_NAMED
, sizeof (dbuf_stats
) / sizeof (kstat_named_t
),
779 if (dbuf_ksp
!= NULL
) {
780 dbuf_ksp
->ks_data
= &dbuf_stats
;
781 dbuf_ksp
->ks_update
= dbuf_kstat_update
;
782 kstat_install(dbuf_ksp
);
784 for (i
= 0; i
< DN_MAX_LEVELS
; i
++) {
785 snprintf(dbuf_stats
.cache_levels
[i
].name
,
786 KSTAT_STRLEN
, "cache_level_%d", i
);
787 dbuf_stats
.cache_levels
[i
].data_type
=
789 snprintf(dbuf_stats
.cache_levels_bytes
[i
].name
,
790 KSTAT_STRLEN
, "cache_level_%d_bytes", i
);
791 dbuf_stats
.cache_levels_bytes
[i
].data_type
=
800 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
803 dbuf_stats_destroy();
805 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
806 mutex_destroy(&h
->hash_mutexes
[i
]);
809 * Large allocations which do not require contiguous pages
810 * should be using vmem_free() in the linux kernel
812 vmem_free(h
->hash_table
, (h
->hash_table_mask
+ 1) * sizeof (void *));
814 kmem_free(h
->hash_table
, (h
->hash_table_mask
+ 1) * sizeof (void *));
816 kmem_cache_destroy(dbuf_kmem_cache
);
817 taskq_destroy(dbu_evict_taskq
);
819 mutex_enter(&dbuf_evict_lock
);
820 dbuf_evict_thread_exit
= B_TRUE
;
821 while (dbuf_evict_thread_exit
) {
822 cv_signal(&dbuf_evict_cv
);
823 cv_wait(&dbuf_evict_cv
, &dbuf_evict_lock
);
825 mutex_exit(&dbuf_evict_lock
);
827 mutex_destroy(&dbuf_evict_lock
);
828 cv_destroy(&dbuf_evict_cv
);
830 refcount_destroy(&dbuf_cache_size
);
831 multilist_destroy(dbuf_cache
);
833 if (dbuf_ksp
!= NULL
) {
834 kstat_delete(dbuf_ksp
);
845 dbuf_verify(dmu_buf_impl_t
*db
)
848 dbuf_dirty_record_t
*dr
;
850 ASSERT(MUTEX_HELD(&db
->db_mtx
));
852 if (!(zfs_flags
& ZFS_DEBUG_DBUF_VERIFY
))
855 ASSERT(db
->db_objset
!= NULL
);
859 ASSERT(db
->db_parent
== NULL
);
860 ASSERT(db
->db_blkptr
== NULL
);
862 ASSERT3U(db
->db
.db_object
, ==, dn
->dn_object
);
863 ASSERT3P(db
->db_objset
, ==, dn
->dn_objset
);
864 ASSERT3U(db
->db_level
, <, dn
->dn_nlevels
);
865 ASSERT(db
->db_blkid
== DMU_BONUS_BLKID
||
866 db
->db_blkid
== DMU_SPILL_BLKID
||
867 !avl_is_empty(&dn
->dn_dbufs
));
869 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
871 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
872 ASSERT3U(db
->db
.db_offset
, ==, DMU_BONUS_BLKID
);
873 } else if (db
->db_blkid
== DMU_SPILL_BLKID
) {
875 ASSERT0(db
->db
.db_offset
);
877 ASSERT3U(db
->db
.db_offset
, ==, db
->db_blkid
* db
->db
.db_size
);
880 for (dr
= db
->db_data_pending
; dr
!= NULL
; dr
= dr
->dr_next
)
881 ASSERT(dr
->dr_dbuf
== db
);
883 for (dr
= db
->db_last_dirty
; dr
!= NULL
; dr
= dr
->dr_next
)
884 ASSERT(dr
->dr_dbuf
== db
);
887 * We can't assert that db_size matches dn_datablksz because it
888 * can be momentarily different when another thread is doing
891 if (db
->db_level
== 0 && db
->db
.db_object
== DMU_META_DNODE_OBJECT
) {
892 dr
= db
->db_data_pending
;
894 * It should only be modified in syncing context, so
895 * make sure we only have one copy of the data.
897 ASSERT(dr
== NULL
|| dr
->dt
.dl
.dr_data
== db
->db_buf
);
900 /* verify db->db_blkptr */
902 if (db
->db_parent
== dn
->dn_dbuf
) {
903 /* db is pointed to by the dnode */
904 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
905 if (DMU_OBJECT_IS_SPECIAL(db
->db
.db_object
))
906 ASSERT(db
->db_parent
== NULL
);
908 ASSERT(db
->db_parent
!= NULL
);
909 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
910 ASSERT3P(db
->db_blkptr
, ==,
911 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
913 /* db is pointed to by an indirect block */
914 ASSERTV(int epb
= db
->db_parent
->db
.db_size
>>
916 ASSERT3U(db
->db_parent
->db_level
, ==, db
->db_level
+1);
917 ASSERT3U(db
->db_parent
->db
.db_object
, ==,
920 * dnode_grow_indblksz() can make this fail if we don't
921 * have the struct_rwlock. XXX indblksz no longer
922 * grows. safe to do this now?
924 if (RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
925 ASSERT3P(db
->db_blkptr
, ==,
926 ((blkptr_t
*)db
->db_parent
->db
.db_data
+
927 db
->db_blkid
% epb
));
931 if ((db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
)) &&
932 (db
->db_buf
== NULL
|| db
->db_buf
->b_data
) &&
933 db
->db
.db_data
&& db
->db_blkid
!= DMU_BONUS_BLKID
&&
934 db
->db_state
!= DB_FILL
&& !dn
->dn_free_txg
) {
936 * If the blkptr isn't set but they have nonzero data,
937 * it had better be dirty, otherwise we'll lose that
938 * data when we evict this buffer.
940 * There is an exception to this rule for indirect blocks; in
941 * this case, if the indirect block is a hole, we fill in a few
942 * fields on each of the child blocks (importantly, birth time)
943 * to prevent hole birth times from being lost when you
944 * partially fill in a hole.
946 if (db
->db_dirtycnt
== 0) {
947 if (db
->db_level
== 0) {
948 uint64_t *buf
= db
->db
.db_data
;
951 for (i
= 0; i
< db
->db
.db_size
>> 3; i
++) {
955 blkptr_t
*bps
= db
->db
.db_data
;
956 ASSERT3U(1 << DB_DNODE(db
)->dn_indblkshift
, ==,
959 * We want to verify that all the blkptrs in the
960 * indirect block are holes, but we may have
961 * automatically set up a few fields for them.
962 * We iterate through each blkptr and verify
963 * they only have those fields set.
966 i
< db
->db
.db_size
/ sizeof (blkptr_t
);
968 blkptr_t
*bp
= &bps
[i
];
969 ASSERT(ZIO_CHECKSUM_IS_ZERO(
972 DVA_IS_EMPTY(&bp
->blk_dva
[0]) &&
973 DVA_IS_EMPTY(&bp
->blk_dva
[1]) &&
974 DVA_IS_EMPTY(&bp
->blk_dva
[2]));
975 ASSERT0(bp
->blk_fill
);
976 ASSERT0(bp
->blk_pad
[0]);
977 ASSERT0(bp
->blk_pad
[1]);
978 ASSERT(!BP_IS_EMBEDDED(bp
));
979 ASSERT(BP_IS_HOLE(bp
));
980 ASSERT0(bp
->blk_phys_birth
);
990 dbuf_clear_data(dmu_buf_impl_t
*db
)
992 ASSERT(MUTEX_HELD(&db
->db_mtx
));
994 ASSERT3P(db
->db_buf
, ==, NULL
);
995 db
->db
.db_data
= NULL
;
996 if (db
->db_state
!= DB_NOFILL
)
997 db
->db_state
= DB_UNCACHED
;
1001 dbuf_set_data(dmu_buf_impl_t
*db
, arc_buf_t
*buf
)
1003 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1004 ASSERT(buf
!= NULL
);
1007 ASSERT(buf
->b_data
!= NULL
);
1008 db
->db
.db_data
= buf
->b_data
;
1012 * Loan out an arc_buf for read. Return the loaned arc_buf.
1015 dbuf_loan_arcbuf(dmu_buf_impl_t
*db
)
1019 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1020 mutex_enter(&db
->db_mtx
);
1021 if (arc_released(db
->db_buf
) || refcount_count(&db
->db_holds
) > 1) {
1022 int blksz
= db
->db
.db_size
;
1023 spa_t
*spa
= db
->db_objset
->os_spa
;
1025 mutex_exit(&db
->db_mtx
);
1026 abuf
= arc_loan_buf(spa
, B_FALSE
, blksz
);
1027 bcopy(db
->db
.db_data
, abuf
->b_data
, blksz
);
1030 arc_loan_inuse_buf(abuf
, db
);
1032 dbuf_clear_data(db
);
1033 mutex_exit(&db
->db_mtx
);
1039 * Calculate which level n block references the data at the level 0 offset
1043 dbuf_whichblock(const dnode_t
*dn
, const int64_t level
, const uint64_t offset
)
1045 if (dn
->dn_datablkshift
!= 0 && dn
->dn_indblkshift
!= 0) {
1047 * The level n blkid is equal to the level 0 blkid divided by
1048 * the number of level 0s in a level n block.
1050 * The level 0 blkid is offset >> datablkshift =
1051 * offset / 2^datablkshift.
1053 * The number of level 0s in a level n is the number of block
1054 * pointers in an indirect block, raised to the power of level.
1055 * This is 2^(indblkshift - SPA_BLKPTRSHIFT)^level =
1056 * 2^(level*(indblkshift - SPA_BLKPTRSHIFT)).
1058 * Thus, the level n blkid is: offset /
1059 * ((2^datablkshift)*(2^(level*(indblkshift - SPA_BLKPTRSHIFT)))
1060 * = offset / 2^(datablkshift + level *
1061 * (indblkshift - SPA_BLKPTRSHIFT))
1062 * = offset >> (datablkshift + level *
1063 * (indblkshift - SPA_BLKPTRSHIFT))
1066 const unsigned exp
= dn
->dn_datablkshift
+
1067 level
* (dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
);
1069 if (exp
>= 8 * sizeof (offset
)) {
1070 /* This only happens on the highest indirection level */
1071 ASSERT3U(level
, ==, dn
->dn_nlevels
- 1);
1075 ASSERT3U(exp
, <, 8 * sizeof (offset
));
1077 return (offset
>> exp
);
1079 ASSERT3U(offset
, <, dn
->dn_datablksz
);
1085 dbuf_read_done(zio_t
*zio
, const zbookmark_phys_t
*zb
, const blkptr_t
*bp
,
1086 arc_buf_t
*buf
, void *vdb
)
1088 dmu_buf_impl_t
*db
= vdb
;
1090 mutex_enter(&db
->db_mtx
);
1091 ASSERT3U(db
->db_state
, ==, DB_READ
);
1093 * All reads are synchronous, so we must have a hold on the dbuf
1095 ASSERT(refcount_count(&db
->db_holds
) > 0);
1096 ASSERT(db
->db_buf
== NULL
);
1097 ASSERT(db
->db
.db_data
== NULL
);
1098 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
1099 /* we were freed in flight; disregard any error */
1101 buf
= arc_alloc_buf(db
->db_objset
->os_spa
,
1102 db
, DBUF_GET_BUFC_TYPE(db
), db
->db
.db_size
);
1104 arc_release(buf
, db
);
1105 bzero(buf
->b_data
, db
->db
.db_size
);
1106 arc_buf_freeze(buf
);
1107 db
->db_freed_in_flight
= FALSE
;
1108 dbuf_set_data(db
, buf
);
1109 db
->db_state
= DB_CACHED
;
1110 } else if (buf
!= NULL
) {
1111 dbuf_set_data(db
, buf
);
1112 db
->db_state
= DB_CACHED
;
1114 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1115 ASSERT3P(db
->db_buf
, ==, NULL
);
1116 db
->db_state
= DB_UNCACHED
;
1118 cv_broadcast(&db
->db_changed
);
1119 dbuf_rele_and_unlock(db
, NULL
, B_FALSE
);
1124 * This function ensures that, when doing a decrypting read of a block,
1125 * we make sure we have decrypted the dnode associated with it. We must do
1126 * this so that we ensure we are fully authenticating the checksum-of-MACs
1127 * tree from the root of the objset down to this block. Indirect blocks are
1128 * always verified against their secure checksum-of-MACs assuming that the
1129 * dnode containing them is correct. Now that we are doing a decrypting read,
1130 * we can be sure that the key is loaded and verify that assumption. This is
1131 * especially important considering that we always read encrypted dnode
1132 * blocks as raw data (without verifying their MACs) to start, and
1133 * decrypt / authenticate them when we need to read an encrypted bonus buffer.
1136 dbuf_read_verify_dnode_crypt(dmu_buf_impl_t
*db
, uint32_t flags
)
1139 objset_t
*os
= db
->db_objset
;
1140 arc_buf_t
*dnode_abuf
;
1142 zbookmark_phys_t zb
;
1144 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1146 if (!os
->os_encrypted
|| os
->os_raw_receive
||
1147 (flags
& DB_RF_NO_DECRYPT
) != 0)
1152 dnode_abuf
= (dn
->dn_dbuf
!= NULL
) ? dn
->dn_dbuf
->db_buf
: NULL
;
1154 if (dnode_abuf
== NULL
|| !arc_is_encrypted(dnode_abuf
)) {
1159 SET_BOOKMARK(&zb
, dmu_objset_id(os
),
1160 DMU_META_DNODE_OBJECT
, 0, dn
->dn_dbuf
->db_blkid
);
1161 err
= arc_untransform(dnode_abuf
, os
->os_spa
, &zb
, B_TRUE
);
1164 * An error code of EACCES tells us that the key is still not
1165 * available. This is ok if we are only reading authenticated
1166 * (and therefore non-encrypted) blocks.
1168 if (err
== EACCES
&& ((db
->db_blkid
!= DMU_BONUS_BLKID
&&
1169 !DMU_OT_IS_ENCRYPTED(dn
->dn_type
)) ||
1170 (db
->db_blkid
== DMU_BONUS_BLKID
&&
1171 !DMU_OT_IS_ENCRYPTED(dn
->dn_bonustype
))))
1181 dbuf_read_impl(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t flags
)
1184 zbookmark_phys_t zb
;
1185 uint32_t aflags
= ARC_FLAG_NOWAIT
;
1186 int err
, zio_flags
= 0;
1190 ASSERT(!refcount_is_zero(&db
->db_holds
));
1191 /* We need the struct_rwlock to prevent db_blkptr from changing. */
1192 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
1193 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1194 ASSERT(db
->db_state
== DB_UNCACHED
);
1195 ASSERT(db
->db_buf
== NULL
);
1197 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1199 * The bonus length stored in the dnode may be less than
1200 * the maximum available space in the bonus buffer.
1202 int bonuslen
= MIN(dn
->dn_bonuslen
, dn
->dn_phys
->dn_bonuslen
);
1203 int max_bonuslen
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
);
1205 /* if the underlying dnode block is encrypted, decrypt it */
1206 err
= dbuf_read_verify_dnode_crypt(db
, flags
);
1209 mutex_exit(&db
->db_mtx
);
1213 ASSERT3U(bonuslen
, <=, db
->db
.db_size
);
1214 db
->db
.db_data
= kmem_alloc(max_bonuslen
, KM_SLEEP
);
1215 arc_space_consume(max_bonuslen
, ARC_SPACE_BONUS
);
1216 if (bonuslen
< max_bonuslen
)
1217 bzero(db
->db
.db_data
, max_bonuslen
);
1219 bcopy(DN_BONUS(dn
->dn_phys
), db
->db
.db_data
, bonuslen
);
1221 db
->db_state
= DB_CACHED
;
1222 mutex_exit(&db
->db_mtx
);
1227 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
1228 * processes the delete record and clears the bp while we are waiting
1229 * for the dn_mtx (resulting in a "no" from block_freed).
1231 if (db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
) ||
1232 (db
->db_level
== 0 && (dnode_block_freed(dn
, db
->db_blkid
) ||
1233 BP_IS_HOLE(db
->db_blkptr
)))) {
1234 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1236 dbuf_set_data(db
, arc_alloc_buf(db
->db_objset
->os_spa
, db
, type
,
1238 bzero(db
->db
.db_data
, db
->db
.db_size
);
1240 if (db
->db_blkptr
!= NULL
&& db
->db_level
> 0 &&
1241 BP_IS_HOLE(db
->db_blkptr
) &&
1242 db
->db_blkptr
->blk_birth
!= 0) {
1243 blkptr_t
*bps
= db
->db
.db_data
;
1244 for (int i
= 0; i
< ((1 <<
1245 DB_DNODE(db
)->dn_indblkshift
) / sizeof (blkptr_t
));
1247 blkptr_t
*bp
= &bps
[i
];
1248 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==,
1249 1 << dn
->dn_indblkshift
);
1251 BP_GET_LEVEL(db
->db_blkptr
) == 1 ?
1253 BP_GET_LSIZE(db
->db_blkptr
));
1254 BP_SET_TYPE(bp
, BP_GET_TYPE(db
->db_blkptr
));
1256 BP_GET_LEVEL(db
->db_blkptr
) - 1);
1257 BP_SET_BIRTH(bp
, db
->db_blkptr
->blk_birth
, 0);
1261 db
->db_state
= DB_CACHED
;
1262 mutex_exit(&db
->db_mtx
);
1267 SET_BOOKMARK(&zb
, dmu_objset_id(db
->db_objset
),
1268 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
1271 * All bps of an encrypted os should have the encryption bit set.
1272 * If this is not true it indicates tampering and we report an error.
1274 if (db
->db_objset
->os_encrypted
&& !BP_USES_CRYPT(db
->db_blkptr
)) {
1275 spa_log_error(db
->db_objset
->os_spa
, &zb
);
1276 zfs_panic_recover("unencrypted block in encrypted "
1277 "object set %llu", dmu_objset_id(db
->db_objset
));
1279 mutex_exit(&db
->db_mtx
);
1280 return (SET_ERROR(EIO
));
1283 err
= dbuf_read_verify_dnode_crypt(db
, flags
);
1286 mutex_exit(&db
->db_mtx
);
1292 db
->db_state
= DB_READ
;
1293 mutex_exit(&db
->db_mtx
);
1295 if (DBUF_IS_L2CACHEABLE(db
))
1296 aflags
|= ARC_FLAG_L2CACHE
;
1298 dbuf_add_ref(db
, NULL
);
1300 zio_flags
= (flags
& DB_RF_CANFAIL
) ?
1301 ZIO_FLAG_CANFAIL
: ZIO_FLAG_MUSTSUCCEED
;
1303 if ((flags
& DB_RF_NO_DECRYPT
) && BP_IS_PROTECTED(db
->db_blkptr
))
1304 zio_flags
|= ZIO_FLAG_RAW
;
1306 err
= arc_read(zio
, db
->db_objset
->os_spa
, db
->db_blkptr
,
1307 dbuf_read_done
, db
, ZIO_PRIORITY_SYNC_READ
, zio_flags
,
1314 * This is our just-in-time copy function. It makes a copy of buffers that
1315 * have been modified in a previous transaction group before we access them in
1316 * the current active group.
1318 * This function is used in three places: when we are dirtying a buffer for the
1319 * first time in a txg, when we are freeing a range in a dnode that includes
1320 * this buffer, and when we are accessing a buffer which was received compressed
1321 * and later referenced in a WRITE_BYREF record.
1323 * Note that when we are called from dbuf_free_range() we do not put a hold on
1324 * the buffer, we just traverse the active dbuf list for the dnode.
1327 dbuf_fix_old_data(dmu_buf_impl_t
*db
, uint64_t txg
)
1329 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
1331 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1332 ASSERT(db
->db
.db_data
!= NULL
);
1333 ASSERT(db
->db_level
== 0);
1334 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
);
1337 (dr
->dt
.dl
.dr_data
!=
1338 ((db
->db_blkid
== DMU_BONUS_BLKID
) ? db
->db
.db_data
: db
->db_buf
)))
1342 * If the last dirty record for this dbuf has not yet synced
1343 * and its referencing the dbuf data, either:
1344 * reset the reference to point to a new copy,
1345 * or (if there a no active holders)
1346 * just null out the current db_data pointer.
1348 ASSERT3U(dr
->dr_txg
, >=, txg
- 2);
1349 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1350 dnode_t
*dn
= DB_DNODE(db
);
1351 int bonuslen
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
);
1352 dr
->dt
.dl
.dr_data
= kmem_alloc(bonuslen
, KM_SLEEP
);
1353 arc_space_consume(bonuslen
, ARC_SPACE_BONUS
);
1354 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
, bonuslen
);
1355 } else if (refcount_count(&db
->db_holds
) > db
->db_dirtycnt
) {
1356 dnode_t
*dn
= DB_DNODE(db
);
1357 int size
= arc_buf_size(db
->db_buf
);
1358 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1359 spa_t
*spa
= db
->db_objset
->os_spa
;
1360 enum zio_compress compress_type
=
1361 arc_get_compression(db
->db_buf
);
1363 if (arc_is_encrypted(db
->db_buf
)) {
1364 boolean_t byteorder
;
1365 uint8_t salt
[ZIO_DATA_SALT_LEN
];
1366 uint8_t iv
[ZIO_DATA_IV_LEN
];
1367 uint8_t mac
[ZIO_DATA_MAC_LEN
];
1369 arc_get_raw_params(db
->db_buf
, &byteorder
, salt
,
1371 dr
->dt
.dl
.dr_data
= arc_alloc_raw_buf(spa
, db
,
1372 dmu_objset_id(dn
->dn_objset
), byteorder
, salt
, iv
,
1373 mac
, dn
->dn_type
, size
, arc_buf_lsize(db
->db_buf
),
1375 } else if (compress_type
!= ZIO_COMPRESS_OFF
) {
1376 ASSERT3U(type
, ==, ARC_BUFC_DATA
);
1377 dr
->dt
.dl
.dr_data
= arc_alloc_compressed_buf(spa
, db
,
1378 size
, arc_buf_lsize(db
->db_buf
), compress_type
);
1380 dr
->dt
.dl
.dr_data
= arc_alloc_buf(spa
, db
, type
, size
);
1382 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
->b_data
, size
);
1385 dbuf_clear_data(db
);
1390 dbuf_read(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t flags
)
1397 * We don't have to hold the mutex to check db_state because it
1398 * can't be freed while we have a hold on the buffer.
1400 ASSERT(!refcount_is_zero(&db
->db_holds
));
1402 if (db
->db_state
== DB_NOFILL
)
1403 return (SET_ERROR(EIO
));
1407 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1408 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
1410 prefetch
= db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1411 (flags
& DB_RF_NOPREFETCH
) == 0 && dn
!= NULL
&&
1412 DBUF_IS_CACHEABLE(db
);
1414 mutex_enter(&db
->db_mtx
);
1415 if (db
->db_state
== DB_CACHED
) {
1416 spa_t
*spa
= dn
->dn_objset
->os_spa
;
1419 * Ensure that this block's dnode has been decrypted if
1420 * the caller has requested decrypted data.
1422 err
= dbuf_read_verify_dnode_crypt(db
, flags
);
1425 * If the arc buf is compressed or encrypted and the caller
1426 * requested uncompressed data, we need to untransform it
1427 * before returning. We also call arc_untransform() on any
1428 * unauthenticated blocks, which will verify their MAC if
1429 * the key is now available.
1431 if (err
== 0 && db
->db_buf
!= NULL
&&
1432 (flags
& DB_RF_NO_DECRYPT
) == 0 &&
1433 (arc_is_encrypted(db
->db_buf
) ||
1434 arc_is_unauthenticated(db
->db_buf
) ||
1435 arc_get_compression(db
->db_buf
) != ZIO_COMPRESS_OFF
)) {
1436 zbookmark_phys_t zb
;
1438 SET_BOOKMARK(&zb
, dmu_objset_id(db
->db_objset
),
1439 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
1440 dbuf_fix_old_data(db
, spa_syncing_txg(spa
));
1441 err
= arc_untransform(db
->db_buf
, spa
, &zb
, B_FALSE
);
1442 dbuf_set_data(db
, db
->db_buf
);
1444 mutex_exit(&db
->db_mtx
);
1445 if (err
== 0 && prefetch
)
1446 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
);
1447 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1448 rw_exit(&dn
->dn_struct_rwlock
);
1450 DBUF_STAT_BUMP(hash_hits
);
1451 } else if (db
->db_state
== DB_UNCACHED
) {
1452 spa_t
*spa
= dn
->dn_objset
->os_spa
;
1453 boolean_t need_wait
= B_FALSE
;
1456 db
->db_blkptr
!= NULL
&& !BP_IS_HOLE(db
->db_blkptr
)) {
1457 zio
= zio_root(spa
, NULL
, NULL
, ZIO_FLAG_CANFAIL
);
1460 err
= dbuf_read_impl(db
, zio
, flags
);
1462 /* dbuf_read_impl has dropped db_mtx for us */
1464 if (!err
&& prefetch
)
1465 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
);
1467 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1468 rw_exit(&dn
->dn_struct_rwlock
);
1470 DBUF_STAT_BUMP(hash_misses
);
1472 if (!err
&& need_wait
)
1473 err
= zio_wait(zio
);
1476 * Another reader came in while the dbuf was in flight
1477 * between UNCACHED and CACHED. Either a writer will finish
1478 * writing the buffer (sending the dbuf to CACHED) or the
1479 * first reader's request will reach the read_done callback
1480 * and send the dbuf to CACHED. Otherwise, a failure
1481 * occurred and the dbuf went to UNCACHED.
1483 mutex_exit(&db
->db_mtx
);
1485 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
);
1486 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1487 rw_exit(&dn
->dn_struct_rwlock
);
1489 DBUF_STAT_BUMP(hash_misses
);
1491 /* Skip the wait per the caller's request. */
1492 mutex_enter(&db
->db_mtx
);
1493 if ((flags
& DB_RF_NEVERWAIT
) == 0) {
1494 while (db
->db_state
== DB_READ
||
1495 db
->db_state
== DB_FILL
) {
1496 ASSERT(db
->db_state
== DB_READ
||
1497 (flags
& DB_RF_HAVESTRUCT
) == 0);
1498 DTRACE_PROBE2(blocked__read
, dmu_buf_impl_t
*,
1500 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1502 if (db
->db_state
== DB_UNCACHED
)
1503 err
= SET_ERROR(EIO
);
1505 mutex_exit(&db
->db_mtx
);
1512 dbuf_noread(dmu_buf_impl_t
*db
)
1514 ASSERT(!refcount_is_zero(&db
->db_holds
));
1515 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1516 mutex_enter(&db
->db_mtx
);
1517 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
1518 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1519 if (db
->db_state
== DB_UNCACHED
) {
1520 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1521 spa_t
*spa
= db
->db_objset
->os_spa
;
1523 ASSERT(db
->db_buf
== NULL
);
1524 ASSERT(db
->db
.db_data
== NULL
);
1525 dbuf_set_data(db
, arc_alloc_buf(spa
, db
, type
, db
->db
.db_size
));
1526 db
->db_state
= DB_FILL
;
1527 } else if (db
->db_state
== DB_NOFILL
) {
1528 dbuf_clear_data(db
);
1530 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
1532 mutex_exit(&db
->db_mtx
);
1536 dbuf_unoverride(dbuf_dirty_record_t
*dr
)
1538 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
1539 blkptr_t
*bp
= &dr
->dt
.dl
.dr_overridden_by
;
1540 uint64_t txg
= dr
->dr_txg
;
1542 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1544 * This assert is valid because dmu_sync() expects to be called by
1545 * a zilog's get_data while holding a range lock. This call only
1546 * comes from dbuf_dirty() callers who must also hold a range lock.
1548 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_IN_DMU_SYNC
);
1549 ASSERT(db
->db_level
== 0);
1551 if (db
->db_blkid
== DMU_BONUS_BLKID
||
1552 dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
)
1555 ASSERT(db
->db_data_pending
!= dr
);
1557 /* free this block */
1558 if (!BP_IS_HOLE(bp
) && !dr
->dt
.dl
.dr_nopwrite
)
1559 zio_free(db
->db_objset
->os_spa
, txg
, bp
);
1561 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
1562 dr
->dt
.dl
.dr_nopwrite
= B_FALSE
;
1563 dr
->dt
.dl
.dr_has_raw_params
= B_FALSE
;
1566 * Release the already-written buffer, so we leave it in
1567 * a consistent dirty state. Note that all callers are
1568 * modifying the buffer, so they will immediately do
1569 * another (redundant) arc_release(). Therefore, leave
1570 * the buf thawed to save the effort of freezing &
1571 * immediately re-thawing it.
1573 arc_release(dr
->dt
.dl
.dr_data
, db
);
1577 * Evict (if its unreferenced) or clear (if its referenced) any level-0
1578 * data blocks in the free range, so that any future readers will find
1582 dbuf_free_range(dnode_t
*dn
, uint64_t start_blkid
, uint64_t end_blkid
,
1585 dmu_buf_impl_t
*db_search
;
1586 dmu_buf_impl_t
*db
, *db_next
;
1587 uint64_t txg
= tx
->tx_txg
;
1590 if (end_blkid
> dn
->dn_maxblkid
&&
1591 !(start_blkid
== DMU_SPILL_BLKID
|| end_blkid
== DMU_SPILL_BLKID
))
1592 end_blkid
= dn
->dn_maxblkid
;
1593 dprintf_dnode(dn
, "start=%llu end=%llu\n", start_blkid
, end_blkid
);
1595 db_search
= kmem_alloc(sizeof (dmu_buf_impl_t
), KM_SLEEP
);
1596 db_search
->db_level
= 0;
1597 db_search
->db_blkid
= start_blkid
;
1598 db_search
->db_state
= DB_SEARCH
;
1600 mutex_enter(&dn
->dn_dbufs_mtx
);
1601 db
= avl_find(&dn
->dn_dbufs
, db_search
, &where
);
1602 ASSERT3P(db
, ==, NULL
);
1604 db
= avl_nearest(&dn
->dn_dbufs
, where
, AVL_AFTER
);
1606 for (; db
!= NULL
; db
= db_next
) {
1607 db_next
= AVL_NEXT(&dn
->dn_dbufs
, db
);
1608 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1610 if (db
->db_level
!= 0 || db
->db_blkid
> end_blkid
) {
1613 ASSERT3U(db
->db_blkid
, >=, start_blkid
);
1615 /* found a level 0 buffer in the range */
1616 mutex_enter(&db
->db_mtx
);
1617 if (dbuf_undirty(db
, tx
)) {
1618 /* mutex has been dropped and dbuf destroyed */
1622 if (db
->db_state
== DB_UNCACHED
||
1623 db
->db_state
== DB_NOFILL
||
1624 db
->db_state
== DB_EVICTING
) {
1625 ASSERT(db
->db
.db_data
== NULL
);
1626 mutex_exit(&db
->db_mtx
);
1629 if (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
) {
1630 /* will be handled in dbuf_read_done or dbuf_rele */
1631 db
->db_freed_in_flight
= TRUE
;
1632 mutex_exit(&db
->db_mtx
);
1635 if (refcount_count(&db
->db_holds
) == 0) {
1640 /* The dbuf is referenced */
1642 if (db
->db_last_dirty
!= NULL
) {
1643 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
1645 if (dr
->dr_txg
== txg
) {
1647 * This buffer is "in-use", re-adjust the file
1648 * size to reflect that this buffer may
1649 * contain new data when we sync.
1651 if (db
->db_blkid
!= DMU_SPILL_BLKID
&&
1652 db
->db_blkid
> dn
->dn_maxblkid
)
1653 dn
->dn_maxblkid
= db
->db_blkid
;
1654 dbuf_unoverride(dr
);
1657 * This dbuf is not dirty in the open context.
1658 * Either uncache it (if its not referenced in
1659 * the open context) or reset its contents to
1662 dbuf_fix_old_data(db
, txg
);
1665 /* clear the contents if its cached */
1666 if (db
->db_state
== DB_CACHED
) {
1667 ASSERT(db
->db
.db_data
!= NULL
);
1668 arc_release(db
->db_buf
, db
);
1669 bzero(db
->db
.db_data
, db
->db
.db_size
);
1670 arc_buf_freeze(db
->db_buf
);
1673 mutex_exit(&db
->db_mtx
);
1676 kmem_free(db_search
, sizeof (dmu_buf_impl_t
));
1677 mutex_exit(&dn
->dn_dbufs_mtx
);
1681 dbuf_new_size(dmu_buf_impl_t
*db
, int size
, dmu_tx_t
*tx
)
1683 arc_buf_t
*buf
, *obuf
;
1684 int osize
= db
->db
.db_size
;
1685 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1688 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1693 /* XXX does *this* func really need the lock? */
1694 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
1697 * This call to dmu_buf_will_dirty() with the dn_struct_rwlock held
1698 * is OK, because there can be no other references to the db
1699 * when we are changing its size, so no concurrent DB_FILL can
1703 * XXX we should be doing a dbuf_read, checking the return
1704 * value and returning that up to our callers
1706 dmu_buf_will_dirty(&db
->db
, tx
);
1708 /* create the data buffer for the new block */
1709 buf
= arc_alloc_buf(dn
->dn_objset
->os_spa
, db
, type
, size
);
1711 /* copy old block data to the new block */
1713 bcopy(obuf
->b_data
, buf
->b_data
, MIN(osize
, size
));
1714 /* zero the remainder */
1716 bzero((uint8_t *)buf
->b_data
+ osize
, size
- osize
);
1718 mutex_enter(&db
->db_mtx
);
1719 dbuf_set_data(db
, buf
);
1720 arc_buf_destroy(obuf
, db
);
1721 db
->db
.db_size
= size
;
1723 if (db
->db_level
== 0) {
1724 ASSERT3U(db
->db_last_dirty
->dr_txg
, ==, tx
->tx_txg
);
1725 db
->db_last_dirty
->dt
.dl
.dr_data
= buf
;
1727 mutex_exit(&db
->db_mtx
);
1729 dmu_objset_willuse_space(dn
->dn_objset
, size
- osize
, tx
);
1734 dbuf_release_bp(dmu_buf_impl_t
*db
)
1736 ASSERTV(objset_t
*os
= db
->db_objset
);
1738 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os
)));
1739 ASSERT(arc_released(os
->os_phys_buf
) ||
1740 list_link_active(&os
->os_dsl_dataset
->ds_synced_link
));
1741 ASSERT(db
->db_parent
== NULL
|| arc_released(db
->db_parent
->db_buf
));
1743 (void) arc_release(db
->db_buf
, db
);
1747 * We already have a dirty record for this TXG, and we are being
1751 dbuf_redirty(dbuf_dirty_record_t
*dr
)
1753 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
1755 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1757 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
) {
1759 * If this buffer has already been written out,
1760 * we now need to reset its state.
1762 dbuf_unoverride(dr
);
1763 if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
&&
1764 db
->db_state
!= DB_NOFILL
) {
1765 /* Already released on initial dirty, so just thaw. */
1766 ASSERT(arc_released(db
->db_buf
));
1767 arc_buf_thaw(db
->db_buf
);
1772 dbuf_dirty_record_t
*
1773 dbuf_dirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1777 dbuf_dirty_record_t
**drp
, *dr
;
1778 int drop_struct_lock
= FALSE
;
1779 int txgoff
= tx
->tx_txg
& TXG_MASK
;
1781 ASSERT(tx
->tx_txg
!= 0);
1782 ASSERT(!refcount_is_zero(&db
->db_holds
));
1783 DMU_TX_DIRTY_BUF(tx
, db
);
1788 * Shouldn't dirty a regular buffer in syncing context. Private
1789 * objects may be dirtied in syncing context, but only if they
1790 * were already pre-dirtied in open context.
1793 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
) {
1794 rrw_enter(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
,
1797 ASSERT(!dmu_tx_is_syncing(tx
) ||
1798 BP_IS_HOLE(dn
->dn_objset
->os_rootbp
) ||
1799 DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
1800 dn
->dn_objset
->os_dsl_dataset
== NULL
);
1801 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
1802 rrw_exit(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
, FTAG
);
1805 * We make this assert for private objects as well, but after we
1806 * check if we're already dirty. They are allowed to re-dirty
1807 * in syncing context.
1809 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
1810 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
1811 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
1813 mutex_enter(&db
->db_mtx
);
1815 * XXX make this true for indirects too? The problem is that
1816 * transactions created with dmu_tx_create_assigned() from
1817 * syncing context don't bother holding ahead.
1819 ASSERT(db
->db_level
!= 0 ||
1820 db
->db_state
== DB_CACHED
|| db
->db_state
== DB_FILL
||
1821 db
->db_state
== DB_NOFILL
);
1823 mutex_enter(&dn
->dn_mtx
);
1825 * Don't set dirtyctx to SYNC if we're just modifying this as we
1826 * initialize the objset.
1828 if (dn
->dn_dirtyctx
== DN_UNDIRTIED
) {
1829 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
) {
1830 rrw_enter(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
,
1833 if (!BP_IS_HOLE(dn
->dn_objset
->os_rootbp
)) {
1834 dn
->dn_dirtyctx
= (dmu_tx_is_syncing(tx
) ?
1835 DN_DIRTY_SYNC
: DN_DIRTY_OPEN
);
1836 ASSERT(dn
->dn_dirtyctx_firstset
== NULL
);
1837 dn
->dn_dirtyctx_firstset
= kmem_alloc(1, KM_SLEEP
);
1839 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
) {
1840 rrw_exit(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
,
1845 if (tx
->tx_txg
> dn
->dn_dirty_txg
)
1846 dn
->dn_dirty_txg
= tx
->tx_txg
;
1847 mutex_exit(&dn
->dn_mtx
);
1849 if (db
->db_blkid
== DMU_SPILL_BLKID
)
1850 dn
->dn_have_spill
= B_TRUE
;
1853 * If this buffer is already dirty, we're done.
1855 drp
= &db
->db_last_dirty
;
1856 ASSERT(*drp
== NULL
|| (*drp
)->dr_txg
<= tx
->tx_txg
||
1857 db
->db
.db_object
== DMU_META_DNODE_OBJECT
);
1858 while ((dr
= *drp
) != NULL
&& dr
->dr_txg
> tx
->tx_txg
)
1860 if (dr
&& dr
->dr_txg
== tx
->tx_txg
) {
1864 mutex_exit(&db
->db_mtx
);
1869 * Only valid if not already dirty.
1871 ASSERT(dn
->dn_object
== 0 ||
1872 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
1873 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
1875 ASSERT3U(dn
->dn_nlevels
, >, db
->db_level
);
1878 * We should only be dirtying in syncing context if it's the
1879 * mos or we're initializing the os or it's a special object.
1880 * However, we are allowed to dirty in syncing context provided
1881 * we already dirtied it in open context. Hence we must make
1882 * this assertion only if we're not already dirty.
1885 VERIFY3U(tx
->tx_txg
, <=, spa_final_dirty_txg(os
->os_spa
));
1887 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
1888 rrw_enter(&os
->os_dsl_dataset
->ds_bp_rwlock
, RW_READER
, FTAG
);
1889 ASSERT(!dmu_tx_is_syncing(tx
) || DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
1890 os
->os_dsl_dataset
== NULL
|| BP_IS_HOLE(os
->os_rootbp
));
1891 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
1892 rrw_exit(&os
->os_dsl_dataset
->ds_bp_rwlock
, FTAG
);
1894 ASSERT(db
->db
.db_size
!= 0);
1896 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
1898 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
1899 dmu_objset_willuse_space(os
, db
->db
.db_size
, tx
);
1903 * If this buffer is dirty in an old transaction group we need
1904 * to make a copy of it so that the changes we make in this
1905 * transaction group won't leak out when we sync the older txg.
1907 dr
= kmem_zalloc(sizeof (dbuf_dirty_record_t
), KM_SLEEP
);
1908 list_link_init(&dr
->dr_dirty_node
);
1909 if (db
->db_level
== 0) {
1910 void *data_old
= db
->db_buf
;
1912 if (db
->db_state
!= DB_NOFILL
) {
1913 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1914 dbuf_fix_old_data(db
, tx
->tx_txg
);
1915 data_old
= db
->db
.db_data
;
1916 } else if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
) {
1918 * Release the data buffer from the cache so
1919 * that we can modify it without impacting
1920 * possible other users of this cached data
1921 * block. Note that indirect blocks and
1922 * private objects are not released until the
1923 * syncing state (since they are only modified
1926 arc_release(db
->db_buf
, db
);
1927 dbuf_fix_old_data(db
, tx
->tx_txg
);
1928 data_old
= db
->db_buf
;
1930 ASSERT(data_old
!= NULL
);
1932 dr
->dt
.dl
.dr_data
= data_old
;
1934 mutex_init(&dr
->dt
.di
.dr_mtx
, NULL
, MUTEX_NOLOCKDEP
, NULL
);
1935 list_create(&dr
->dt
.di
.dr_children
,
1936 sizeof (dbuf_dirty_record_t
),
1937 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
1939 if (db
->db_blkid
!= DMU_BONUS_BLKID
&& os
->os_dsl_dataset
!= NULL
)
1940 dr
->dr_accounted
= db
->db
.db_size
;
1942 dr
->dr_txg
= tx
->tx_txg
;
1947 * We could have been freed_in_flight between the dbuf_noread
1948 * and dbuf_dirty. We win, as though the dbuf_noread() had
1949 * happened after the free.
1951 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1952 db
->db_blkid
!= DMU_SPILL_BLKID
) {
1953 mutex_enter(&dn
->dn_mtx
);
1954 if (dn
->dn_free_ranges
[txgoff
] != NULL
) {
1955 range_tree_clear(dn
->dn_free_ranges
[txgoff
],
1958 mutex_exit(&dn
->dn_mtx
);
1959 db
->db_freed_in_flight
= FALSE
;
1963 * This buffer is now part of this txg
1965 dbuf_add_ref(db
, (void *)(uintptr_t)tx
->tx_txg
);
1966 db
->db_dirtycnt
+= 1;
1967 ASSERT3U(db
->db_dirtycnt
, <=, 3);
1969 mutex_exit(&db
->db_mtx
);
1971 if (db
->db_blkid
== DMU_BONUS_BLKID
||
1972 db
->db_blkid
== DMU_SPILL_BLKID
) {
1973 mutex_enter(&dn
->dn_mtx
);
1974 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1975 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
1976 mutex_exit(&dn
->dn_mtx
);
1977 dnode_setdirty(dn
, tx
);
1983 * The dn_struct_rwlock prevents db_blkptr from changing
1984 * due to a write from syncing context completing
1985 * while we are running, so we want to acquire it before
1986 * looking at db_blkptr.
1988 if (!RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
1989 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
1990 drop_struct_lock
= TRUE
;
1994 * We need to hold the dn_struct_rwlock to make this assertion,
1995 * because it protects dn_phys / dn_next_nlevels from changing.
1997 ASSERT((dn
->dn_phys
->dn_nlevels
== 0 && db
->db_level
== 0) ||
1998 dn
->dn_phys
->dn_nlevels
> db
->db_level
||
1999 dn
->dn_next_nlevels
[txgoff
] > db
->db_level
||
2000 dn
->dn_next_nlevels
[(tx
->tx_txg
-1) & TXG_MASK
] > db
->db_level
||
2001 dn
->dn_next_nlevels
[(tx
->tx_txg
-2) & TXG_MASK
] > db
->db_level
);
2004 * If we are overwriting a dedup BP, then unless it is snapshotted,
2005 * when we get to syncing context we will need to decrement its
2006 * refcount in the DDT. Prefetch the relevant DDT block so that
2007 * syncing context won't have to wait for the i/o.
2009 ddt_prefetch(os
->os_spa
, db
->db_blkptr
);
2011 if (db
->db_level
== 0) {
2012 ASSERT(!db
->db_objset
->os_raw_receive
||
2013 dn
->dn_maxblkid
>= db
->db_blkid
);
2014 dnode_new_blkid(dn
, db
->db_blkid
, tx
, drop_struct_lock
);
2015 ASSERT(dn
->dn_maxblkid
>= db
->db_blkid
);
2018 if (db
->db_level
+1 < dn
->dn_nlevels
) {
2019 dmu_buf_impl_t
*parent
= db
->db_parent
;
2020 dbuf_dirty_record_t
*di
;
2021 int parent_held
= FALSE
;
2023 if (db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
) {
2024 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2026 parent
= dbuf_hold_level(dn
, db
->db_level
+1,
2027 db
->db_blkid
>> epbs
, FTAG
);
2028 ASSERT(parent
!= NULL
);
2031 if (drop_struct_lock
)
2032 rw_exit(&dn
->dn_struct_rwlock
);
2033 ASSERT3U(db
->db_level
+1, ==, parent
->db_level
);
2034 di
= dbuf_dirty(parent
, tx
);
2036 dbuf_rele(parent
, FTAG
);
2038 mutex_enter(&db
->db_mtx
);
2040 * Since we've dropped the mutex, it's possible that
2041 * dbuf_undirty() might have changed this out from under us.
2043 if (db
->db_last_dirty
== dr
||
2044 dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
2045 mutex_enter(&di
->dt
.di
.dr_mtx
);
2046 ASSERT3U(di
->dr_txg
, ==, tx
->tx_txg
);
2047 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
2048 list_insert_tail(&di
->dt
.di
.dr_children
, dr
);
2049 mutex_exit(&di
->dt
.di
.dr_mtx
);
2052 mutex_exit(&db
->db_mtx
);
2054 ASSERT(db
->db_level
+1 == dn
->dn_nlevels
);
2055 ASSERT(db
->db_blkid
< dn
->dn_nblkptr
);
2056 ASSERT(db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
2057 mutex_enter(&dn
->dn_mtx
);
2058 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
2059 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
2060 mutex_exit(&dn
->dn_mtx
);
2061 if (drop_struct_lock
)
2062 rw_exit(&dn
->dn_struct_rwlock
);
2065 dnode_setdirty(dn
, tx
);
2071 * Undirty a buffer in the transaction group referenced by the given
2072 * transaction. Return whether this evicted the dbuf.
2075 dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
2078 uint64_t txg
= tx
->tx_txg
;
2079 dbuf_dirty_record_t
*dr
, **drp
;
2084 * Due to our use of dn_nlevels below, this can only be called
2085 * in open context, unless we are operating on the MOS.
2086 * From syncing context, dn_nlevels may be different from the
2087 * dn_nlevels used when dbuf was dirtied.
2089 ASSERT(db
->db_objset
==
2090 dmu_objset_pool(db
->db_objset
)->dp_meta_objset
||
2091 txg
!= spa_syncing_txg(dmu_objset_spa(db
->db_objset
)));
2092 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2093 ASSERT0(db
->db_level
);
2094 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2097 * If this buffer is not dirty, we're done.
2099 for (drp
= &db
->db_last_dirty
; (dr
= *drp
) != NULL
; drp
= &dr
->dr_next
)
2100 if (dr
->dr_txg
<= txg
)
2102 if (dr
== NULL
|| dr
->dr_txg
< txg
)
2104 ASSERT(dr
->dr_txg
== txg
);
2105 ASSERT(dr
->dr_dbuf
== db
);
2110 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
2112 ASSERT(db
->db
.db_size
!= 0);
2114 dsl_pool_undirty_space(dmu_objset_pool(dn
->dn_objset
),
2115 dr
->dr_accounted
, txg
);
2120 * Note that there are three places in dbuf_dirty()
2121 * where this dirty record may be put on a list.
2122 * Make sure to do a list_remove corresponding to
2123 * every one of those list_insert calls.
2125 if (dr
->dr_parent
) {
2126 mutex_enter(&dr
->dr_parent
->dt
.di
.dr_mtx
);
2127 list_remove(&dr
->dr_parent
->dt
.di
.dr_children
, dr
);
2128 mutex_exit(&dr
->dr_parent
->dt
.di
.dr_mtx
);
2129 } else if (db
->db_blkid
== DMU_SPILL_BLKID
||
2130 db
->db_level
+ 1 == dn
->dn_nlevels
) {
2131 ASSERT(db
->db_blkptr
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
2132 mutex_enter(&dn
->dn_mtx
);
2133 list_remove(&dn
->dn_dirty_records
[txg
& TXG_MASK
], dr
);
2134 mutex_exit(&dn
->dn_mtx
);
2138 if (db
->db_state
!= DB_NOFILL
) {
2139 dbuf_unoverride(dr
);
2141 ASSERT(db
->db_buf
!= NULL
);
2142 ASSERT(dr
->dt
.dl
.dr_data
!= NULL
);
2143 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
2144 arc_buf_destroy(dr
->dt
.dl
.dr_data
, db
);
2147 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
2149 ASSERT(db
->db_dirtycnt
> 0);
2150 db
->db_dirtycnt
-= 1;
2152 if (refcount_remove(&db
->db_holds
, (void *)(uintptr_t)txg
) == 0) {
2153 ASSERT(db
->db_state
== DB_NOFILL
|| arc_released(db
->db_buf
));
2162 dmu_buf_will_dirty_impl(dmu_buf_t
*db_fake
, int flags
, dmu_tx_t
*tx
)
2164 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2166 ASSERT(tx
->tx_txg
!= 0);
2167 ASSERT(!refcount_is_zero(&db
->db_holds
));
2170 * Quick check for dirtyness. For already dirty blocks, this
2171 * reduces runtime of this function by >90%, and overall performance
2172 * by 50% for some workloads (e.g. file deletion with indirect blocks
2175 mutex_enter(&db
->db_mtx
);
2177 dbuf_dirty_record_t
*dr
;
2178 for (dr
= db
->db_last_dirty
;
2179 dr
!= NULL
&& dr
->dr_txg
>= tx
->tx_txg
; dr
= dr
->dr_next
) {
2181 * It's possible that it is already dirty but not cached,
2182 * because there are some calls to dbuf_dirty() that don't
2183 * go through dmu_buf_will_dirty().
2185 if (dr
->dr_txg
== tx
->tx_txg
&& db
->db_state
== DB_CACHED
) {
2186 /* This dbuf is already dirty and cached. */
2188 mutex_exit(&db
->db_mtx
);
2192 mutex_exit(&db
->db_mtx
);
2195 if (RW_WRITE_HELD(&DB_DNODE(db
)->dn_struct_rwlock
))
2196 flags
|= DB_RF_HAVESTRUCT
;
2198 (void) dbuf_read(db
, NULL
, flags
);
2199 (void) dbuf_dirty(db
, tx
);
2203 dmu_buf_will_dirty(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
2205 dmu_buf_will_dirty_impl(db_fake
,
2206 DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
, tx
);
2210 dmu_buf_will_not_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
2212 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2214 db
->db_state
= DB_NOFILL
;
2216 dmu_buf_will_fill(db_fake
, tx
);
2220 dmu_buf_will_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
2222 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2224 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2225 ASSERT(tx
->tx_txg
!= 0);
2226 ASSERT(db
->db_level
== 0);
2227 ASSERT(!refcount_is_zero(&db
->db_holds
));
2229 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
||
2230 dmu_tx_private_ok(tx
));
2233 (void) dbuf_dirty(db
, tx
);
2237 * This function is effectively the same as dmu_buf_will_dirty(), but
2238 * indicates the caller expects raw encrypted data in the db, and provides
2239 * the crypt params (byteorder, salt, iv, mac) which should be stored in the
2240 * blkptr_t when this dbuf is written. This is only used for blocks of
2241 * dnodes, during raw receive.
2244 dmu_buf_set_crypt_params(dmu_buf_t
*db_fake
, boolean_t byteorder
,
2245 const uint8_t *salt
, const uint8_t *iv
, const uint8_t *mac
, dmu_tx_t
*tx
)
2247 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2248 dbuf_dirty_record_t
*dr
;
2251 * dr_has_raw_params is only processed for blocks of dnodes
2252 * (see dbuf_sync_dnode_leaf_crypt()).
2254 ASSERT3U(db
->db
.db_object
, ==, DMU_META_DNODE_OBJECT
);
2255 ASSERT3U(db
->db_level
, ==, 0);
2256 ASSERT(db
->db_objset
->os_raw_receive
);
2258 dmu_buf_will_dirty_impl(db_fake
,
2259 DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
| DB_RF_NO_DECRYPT
, tx
);
2261 dr
= db
->db_last_dirty
;
2262 while (dr
!= NULL
&& dr
->dr_txg
> tx
->tx_txg
)
2265 ASSERT3P(dr
, !=, NULL
);
2266 ASSERT3U(dr
->dr_txg
, ==, tx
->tx_txg
);
2268 dr
->dt
.dl
.dr_has_raw_params
= B_TRUE
;
2269 dr
->dt
.dl
.dr_byteorder
= byteorder
;
2270 bcopy(salt
, dr
->dt
.dl
.dr_salt
, ZIO_DATA_SALT_LEN
);
2271 bcopy(iv
, dr
->dt
.dl
.dr_iv
, ZIO_DATA_IV_LEN
);
2272 bcopy(mac
, dr
->dt
.dl
.dr_mac
, ZIO_DATA_MAC_LEN
);
2275 #pragma weak dmu_buf_fill_done = dbuf_fill_done
2278 dbuf_fill_done(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
2280 mutex_enter(&db
->db_mtx
);
2283 if (db
->db_state
== DB_FILL
) {
2284 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
2285 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2286 /* we were freed while filling */
2287 /* XXX dbuf_undirty? */
2288 bzero(db
->db
.db_data
, db
->db
.db_size
);
2289 db
->db_freed_in_flight
= FALSE
;
2291 db
->db_state
= DB_CACHED
;
2292 cv_broadcast(&db
->db_changed
);
2294 mutex_exit(&db
->db_mtx
);
2298 dmu_buf_write_embedded(dmu_buf_t
*dbuf
, void *data
,
2299 bp_embedded_type_t etype
, enum zio_compress comp
,
2300 int uncompressed_size
, int compressed_size
, int byteorder
,
2303 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbuf
;
2304 struct dirty_leaf
*dl
;
2305 dmu_object_type_t type
;
2307 if (etype
== BP_EMBEDDED_TYPE_DATA
) {
2308 ASSERT(spa_feature_is_active(dmu_objset_spa(db
->db_objset
),
2309 SPA_FEATURE_EMBEDDED_DATA
));
2313 type
= DB_DNODE(db
)->dn_type
;
2316 ASSERT0(db
->db_level
);
2317 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2319 dmu_buf_will_not_fill(dbuf
, tx
);
2321 ASSERT3U(db
->db_last_dirty
->dr_txg
, ==, tx
->tx_txg
);
2322 dl
= &db
->db_last_dirty
->dt
.dl
;
2323 encode_embedded_bp_compressed(&dl
->dr_overridden_by
,
2324 data
, comp
, uncompressed_size
, compressed_size
);
2325 BPE_SET_ETYPE(&dl
->dr_overridden_by
, etype
);
2326 BP_SET_TYPE(&dl
->dr_overridden_by
, type
);
2327 BP_SET_LEVEL(&dl
->dr_overridden_by
, 0);
2328 BP_SET_BYTEORDER(&dl
->dr_overridden_by
, byteorder
);
2330 dl
->dr_override_state
= DR_OVERRIDDEN
;
2331 dl
->dr_overridden_by
.blk_birth
= db
->db_last_dirty
->dr_txg
;
2335 * Directly assign a provided arc buf to a given dbuf if it's not referenced
2336 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
2339 dbuf_assign_arcbuf(dmu_buf_impl_t
*db
, arc_buf_t
*buf
, dmu_tx_t
*tx
)
2341 ASSERT(!refcount_is_zero(&db
->db_holds
));
2342 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2343 ASSERT(db
->db_level
== 0);
2344 ASSERT3U(dbuf_is_metadata(db
), ==, arc_is_metadata(buf
));
2345 ASSERT(buf
!= NULL
);
2346 ASSERT(arc_buf_lsize(buf
) == db
->db
.db_size
);
2347 ASSERT(tx
->tx_txg
!= 0);
2349 arc_return_buf(buf
, db
);
2350 ASSERT(arc_released(buf
));
2352 mutex_enter(&db
->db_mtx
);
2354 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
2355 cv_wait(&db
->db_changed
, &db
->db_mtx
);
2357 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_UNCACHED
);
2359 if (db
->db_state
== DB_CACHED
&&
2360 refcount_count(&db
->db_holds
) - 1 > db
->db_dirtycnt
) {
2362 * In practice, we will never have a case where we have an
2363 * encrypted arc buffer while additional holds exist on the
2364 * dbuf. We don't handle this here so we simply assert that
2367 ASSERT(!arc_is_encrypted(buf
));
2368 mutex_exit(&db
->db_mtx
);
2369 (void) dbuf_dirty(db
, tx
);
2370 bcopy(buf
->b_data
, db
->db
.db_data
, db
->db
.db_size
);
2371 arc_buf_destroy(buf
, db
);
2372 xuio_stat_wbuf_copied();
2376 xuio_stat_wbuf_nocopy();
2377 if (db
->db_state
== DB_CACHED
) {
2378 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
2380 ASSERT(db
->db_buf
!= NULL
);
2381 if (dr
!= NULL
&& dr
->dr_txg
== tx
->tx_txg
) {
2382 ASSERT(dr
->dt
.dl
.dr_data
== db
->db_buf
);
2384 if (!arc_released(db
->db_buf
)) {
2385 ASSERT(dr
->dt
.dl
.dr_override_state
==
2387 arc_release(db
->db_buf
, db
);
2389 dr
->dt
.dl
.dr_data
= buf
;
2390 arc_buf_destroy(db
->db_buf
, db
);
2391 } else if (dr
== NULL
|| dr
->dt
.dl
.dr_data
!= db
->db_buf
) {
2392 arc_release(db
->db_buf
, db
);
2393 arc_buf_destroy(db
->db_buf
, db
);
2397 ASSERT(db
->db_buf
== NULL
);
2398 dbuf_set_data(db
, buf
);
2399 db
->db_state
= DB_FILL
;
2400 mutex_exit(&db
->db_mtx
);
2401 (void) dbuf_dirty(db
, tx
);
2402 dmu_buf_fill_done(&db
->db
, tx
);
2406 dbuf_destroy(dmu_buf_impl_t
*db
)
2409 dmu_buf_impl_t
*parent
= db
->db_parent
;
2410 dmu_buf_impl_t
*dndb
;
2412 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2413 ASSERT(refcount_is_zero(&db
->db_holds
));
2415 if (db
->db_buf
!= NULL
) {
2416 arc_buf_destroy(db
->db_buf
, db
);
2420 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2421 int slots
= DB_DNODE(db
)->dn_num_slots
;
2422 int bonuslen
= DN_SLOTS_TO_BONUSLEN(slots
);
2423 if (db
->db
.db_data
!= NULL
) {
2424 kmem_free(db
->db
.db_data
, bonuslen
);
2425 arc_space_return(bonuslen
, ARC_SPACE_BONUS
);
2426 db
->db_state
= DB_UNCACHED
;
2430 dbuf_clear_data(db
);
2432 if (multilist_link_active(&db
->db_cache_link
)) {
2433 multilist_remove(dbuf_cache
, db
);
2434 (void) refcount_remove_many(&dbuf_cache_size
,
2435 db
->db
.db_size
, db
);
2436 DBUF_STAT_BUMPDOWN(cache_levels
[db
->db_level
]);
2437 DBUF_STAT_BUMPDOWN(cache_count
);
2438 DBUF_STAT_DECR(cache_levels_bytes
[db
->db_level
],
2442 ASSERT(db
->db_state
== DB_UNCACHED
|| db
->db_state
== DB_NOFILL
);
2443 ASSERT(db
->db_data_pending
== NULL
);
2445 db
->db_state
= DB_EVICTING
;
2446 db
->db_blkptr
= NULL
;
2449 * Now that db_state is DB_EVICTING, nobody else can find this via
2450 * the hash table. We can now drop db_mtx, which allows us to
2451 * acquire the dn_dbufs_mtx.
2453 mutex_exit(&db
->db_mtx
);
2458 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
2459 boolean_t needlock
= !MUTEX_HELD(&dn
->dn_dbufs_mtx
);
2461 mutex_enter(&dn
->dn_dbufs_mtx
);
2462 avl_remove(&dn
->dn_dbufs
, db
);
2463 atomic_dec_32(&dn
->dn_dbufs_count
);
2467 mutex_exit(&dn
->dn_dbufs_mtx
);
2469 * Decrementing the dbuf count means that the hold corresponding
2470 * to the removed dbuf is no longer discounted in dnode_move(),
2471 * so the dnode cannot be moved until after we release the hold.
2472 * The membar_producer() ensures visibility of the decremented
2473 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
2476 mutex_enter(&dn
->dn_mtx
);
2477 dnode_rele_and_unlock(dn
, db
, B_TRUE
);
2478 db
->db_dnode_handle
= NULL
;
2480 dbuf_hash_remove(db
);
2485 ASSERT(refcount_is_zero(&db
->db_holds
));
2487 db
->db_parent
= NULL
;
2489 ASSERT(db
->db_buf
== NULL
);
2490 ASSERT(db
->db
.db_data
== NULL
);
2491 ASSERT(db
->db_hash_next
== NULL
);
2492 ASSERT(db
->db_blkptr
== NULL
);
2493 ASSERT(db
->db_data_pending
== NULL
);
2494 ASSERT(!multilist_link_active(&db
->db_cache_link
));
2496 kmem_cache_free(dbuf_kmem_cache
, db
);
2497 arc_space_return(sizeof (dmu_buf_impl_t
), ARC_SPACE_DBUF
);
2500 * If this dbuf is referenced from an indirect dbuf,
2501 * decrement the ref count on the indirect dbuf.
2503 if (parent
&& parent
!= dndb
) {
2504 mutex_enter(&parent
->db_mtx
);
2505 dbuf_rele_and_unlock(parent
, db
, B_TRUE
);
2510 * Note: While bpp will always be updated if the function returns success,
2511 * parentp will not be updated if the dnode does not have dn_dbuf filled in;
2512 * this happens when the dnode is the meta-dnode, or {user|group|project}used
2515 __attribute__((always_inline
))
2517 dbuf_findbp(dnode_t
*dn
, int level
, uint64_t blkid
, int fail_sparse
,
2518 dmu_buf_impl_t
**parentp
, blkptr_t
**bpp
, struct dbuf_hold_impl_data
*dh
)
2523 ASSERT(blkid
!= DMU_BONUS_BLKID
);
2525 if (blkid
== DMU_SPILL_BLKID
) {
2526 mutex_enter(&dn
->dn_mtx
);
2527 if (dn
->dn_have_spill
&&
2528 (dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
))
2529 *bpp
= DN_SPILL_BLKPTR(dn
->dn_phys
);
2532 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
2533 *parentp
= dn
->dn_dbuf
;
2534 mutex_exit(&dn
->dn_mtx
);
2539 (dn
->dn_phys
->dn_nlevels
== 0) ? 1 : dn
->dn_phys
->dn_nlevels
;
2540 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2542 ASSERT3U(level
* epbs
, <, 64);
2543 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2545 * This assertion shouldn't trip as long as the max indirect block size
2546 * is less than 1M. The reason for this is that up to that point,
2547 * the number of levels required to address an entire object with blocks
2548 * of size SPA_MINBLOCKSIZE satisfies nlevels * epbs + 1 <= 64. In
2549 * other words, if N * epbs + 1 > 64, then if (N-1) * epbs + 1 > 55
2550 * (i.e. we can address the entire object), objects will all use at most
2551 * N-1 levels and the assertion won't overflow. However, once epbs is
2552 * 13, 4 * 13 + 1 = 53, but 5 * 13 + 1 = 66. Then, 4 levels will not be
2553 * enough to address an entire object, so objects will have 5 levels,
2554 * but then this assertion will overflow.
2556 * All this is to say that if we ever increase DN_MAX_INDBLKSHIFT, we
2557 * need to redo this logic to handle overflows.
2559 ASSERT(level
>= nlevels
||
2560 ((nlevels
- level
- 1) * epbs
) +
2561 highbit64(dn
->dn_phys
->dn_nblkptr
) <= 64);
2562 if (level
>= nlevels
||
2563 blkid
>= ((uint64_t)dn
->dn_phys
->dn_nblkptr
<<
2564 ((nlevels
- level
- 1) * epbs
)) ||
2566 blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))) {
2567 /* the buffer has no parent yet */
2568 return (SET_ERROR(ENOENT
));
2569 } else if (level
< nlevels
-1) {
2570 /* this block is referenced from an indirect block */
2573 err
= dbuf_hold_impl(dn
, level
+1,
2574 blkid
>> epbs
, fail_sparse
, FALSE
, NULL
, parentp
);
2576 __dbuf_hold_impl_init(dh
+ 1, dn
, dh
->dh_level
+ 1,
2577 blkid
>> epbs
, fail_sparse
, FALSE
, NULL
,
2578 parentp
, dh
->dh_depth
+ 1);
2579 err
= __dbuf_hold_impl(dh
+ 1);
2583 err
= dbuf_read(*parentp
, NULL
,
2584 (DB_RF_HAVESTRUCT
| DB_RF_NOPREFETCH
| DB_RF_CANFAIL
));
2586 dbuf_rele(*parentp
, NULL
);
2590 *bpp
= ((blkptr_t
*)(*parentp
)->db
.db_data
) +
2591 (blkid
& ((1ULL << epbs
) - 1));
2592 if (blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))
2593 ASSERT(BP_IS_HOLE(*bpp
));
2596 /* the block is referenced from the dnode */
2597 ASSERT3U(level
, ==, nlevels
-1);
2598 ASSERT(dn
->dn_phys
->dn_nblkptr
== 0 ||
2599 blkid
< dn
->dn_phys
->dn_nblkptr
);
2601 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
2602 *parentp
= dn
->dn_dbuf
;
2604 *bpp
= &dn
->dn_phys
->dn_blkptr
[blkid
];
2609 static dmu_buf_impl_t
*
2610 dbuf_create(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
2611 dmu_buf_impl_t
*parent
, blkptr_t
*blkptr
)
2613 objset_t
*os
= dn
->dn_objset
;
2614 dmu_buf_impl_t
*db
, *odb
;
2616 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2617 ASSERT(dn
->dn_type
!= DMU_OT_NONE
);
2619 db
= kmem_cache_alloc(dbuf_kmem_cache
, KM_SLEEP
);
2622 db
->db
.db_object
= dn
->dn_object
;
2623 db
->db_level
= level
;
2624 db
->db_blkid
= blkid
;
2625 db
->db_last_dirty
= NULL
;
2626 db
->db_dirtycnt
= 0;
2627 db
->db_dnode_handle
= dn
->dn_handle
;
2628 db
->db_parent
= parent
;
2629 db
->db_blkptr
= blkptr
;
2632 db
->db_user_immediate_evict
= FALSE
;
2633 db
->db_freed_in_flight
= FALSE
;
2634 db
->db_pending_evict
= FALSE
;
2636 if (blkid
== DMU_BONUS_BLKID
) {
2637 ASSERT3P(parent
, ==, dn
->dn_dbuf
);
2638 db
->db
.db_size
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
) -
2639 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
);
2640 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
2641 db
->db
.db_offset
= DMU_BONUS_BLKID
;
2642 db
->db_state
= DB_UNCACHED
;
2643 /* the bonus dbuf is not placed in the hash table */
2644 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_DBUF
);
2646 } else if (blkid
== DMU_SPILL_BLKID
) {
2647 db
->db
.db_size
= (blkptr
!= NULL
) ?
2648 BP_GET_LSIZE(blkptr
) : SPA_MINBLOCKSIZE
;
2649 db
->db
.db_offset
= 0;
2652 db
->db_level
? 1 << dn
->dn_indblkshift
: dn
->dn_datablksz
;
2653 db
->db
.db_size
= blocksize
;
2654 db
->db
.db_offset
= db
->db_blkid
* blocksize
;
2658 * Hold the dn_dbufs_mtx while we get the new dbuf
2659 * in the hash table *and* added to the dbufs list.
2660 * This prevents a possible deadlock with someone
2661 * trying to look up this dbuf before its added to the
2664 mutex_enter(&dn
->dn_dbufs_mtx
);
2665 db
->db_state
= DB_EVICTING
;
2666 if ((odb
= dbuf_hash_insert(db
)) != NULL
) {
2667 /* someone else inserted it first */
2668 kmem_cache_free(dbuf_kmem_cache
, db
);
2669 mutex_exit(&dn
->dn_dbufs_mtx
);
2670 DBUF_STAT_BUMP(hash_insert_race
);
2673 avl_add(&dn
->dn_dbufs
, db
);
2675 db
->db_state
= DB_UNCACHED
;
2676 mutex_exit(&dn
->dn_dbufs_mtx
);
2677 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_DBUF
);
2679 if (parent
&& parent
!= dn
->dn_dbuf
)
2680 dbuf_add_ref(parent
, db
);
2682 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
2683 refcount_count(&dn
->dn_holds
) > 0);
2684 (void) refcount_add(&dn
->dn_holds
, db
);
2685 atomic_inc_32(&dn
->dn_dbufs_count
);
2687 dprintf_dbuf(db
, "db=%p\n", db
);
2692 typedef struct dbuf_prefetch_arg
{
2693 spa_t
*dpa_spa
; /* The spa to issue the prefetch in. */
2694 zbookmark_phys_t dpa_zb
; /* The target block to prefetch. */
2695 int dpa_epbs
; /* Entries (blkptr_t's) Per Block Shift. */
2696 int dpa_curlevel
; /* The current level that we're reading */
2697 dnode_t
*dpa_dnode
; /* The dnode associated with the prefetch */
2698 zio_priority_t dpa_prio
; /* The priority I/Os should be issued at. */
2699 zio_t
*dpa_zio
; /* The parent zio_t for all prefetches. */
2700 arc_flags_t dpa_aflags
; /* Flags to pass to the final prefetch. */
2701 } dbuf_prefetch_arg_t
;
2704 * Actually issue the prefetch read for the block given.
2707 dbuf_issue_final_prefetch(dbuf_prefetch_arg_t
*dpa
, blkptr_t
*bp
)
2709 if (BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
))
2712 int zio_flags
= ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
;
2713 arc_flags_t aflags
=
2714 dpa
->dpa_aflags
| ARC_FLAG_NOWAIT
| ARC_FLAG_PREFETCH
;
2716 /* dnodes are always read as raw and then converted later */
2717 if (BP_GET_TYPE(bp
) == DMU_OT_DNODE
&& BP_IS_PROTECTED(bp
) &&
2718 dpa
->dpa_curlevel
== 0)
2719 zio_flags
|= ZIO_FLAG_RAW
;
2721 ASSERT3U(dpa
->dpa_curlevel
, ==, BP_GET_LEVEL(bp
));
2722 ASSERT3U(dpa
->dpa_curlevel
, ==, dpa
->dpa_zb
.zb_level
);
2723 ASSERT(dpa
->dpa_zio
!= NULL
);
2724 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
, bp
, NULL
, NULL
,
2725 dpa
->dpa_prio
, zio_flags
, &aflags
, &dpa
->dpa_zb
);
2729 * Called when an indirect block above our prefetch target is read in. This
2730 * will either read in the next indirect block down the tree or issue the actual
2731 * prefetch if the next block down is our target.
2734 dbuf_prefetch_indirect_done(zio_t
*zio
, const zbookmark_phys_t
*zb
,
2735 const blkptr_t
*iobp
, arc_buf_t
*abuf
, void *private)
2737 dbuf_prefetch_arg_t
*dpa
= private;
2739 ASSERT3S(dpa
->dpa_zb
.zb_level
, <, dpa
->dpa_curlevel
);
2740 ASSERT3S(dpa
->dpa_curlevel
, >, 0);
2743 * The dpa_dnode is only valid if we are called with a NULL
2744 * zio. This indicates that the arc_read() returned without
2745 * first calling zio_read() to issue a physical read. Once
2746 * a physical read is made the dpa_dnode must be invalidated
2747 * as the locks guarding it may have been dropped. If the
2748 * dpa_dnode is still valid, then we want to add it to the dbuf
2749 * cache. To do so, we must hold the dbuf associated with the block
2750 * we just prefetched, read its contents so that we associate it
2751 * with an arc_buf_t, and then release it.
2754 ASSERT3S(BP_GET_LEVEL(zio
->io_bp
), ==, dpa
->dpa_curlevel
);
2755 if (zio
->io_flags
& ZIO_FLAG_RAW_COMPRESS
) {
2756 ASSERT3U(BP_GET_PSIZE(zio
->io_bp
), ==, zio
->io_size
);
2758 ASSERT3U(BP_GET_LSIZE(zio
->io_bp
), ==, zio
->io_size
);
2760 ASSERT3P(zio
->io_spa
, ==, dpa
->dpa_spa
);
2762 dpa
->dpa_dnode
= NULL
;
2763 } else if (dpa
->dpa_dnode
!= NULL
) {
2764 uint64_t curblkid
= dpa
->dpa_zb
.zb_blkid
>>
2765 (dpa
->dpa_epbs
* (dpa
->dpa_curlevel
-
2766 dpa
->dpa_zb
.zb_level
));
2767 dmu_buf_impl_t
*db
= dbuf_hold_level(dpa
->dpa_dnode
,
2768 dpa
->dpa_curlevel
, curblkid
, FTAG
);
2769 (void) dbuf_read(db
, NULL
,
2770 DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
| DB_RF_HAVESTRUCT
);
2771 dbuf_rele(db
, FTAG
);
2775 kmem_free(dpa
, sizeof (*dpa
));
2779 dpa
->dpa_curlevel
--;
2780 uint64_t nextblkid
= dpa
->dpa_zb
.zb_blkid
>>
2781 (dpa
->dpa_epbs
* (dpa
->dpa_curlevel
- dpa
->dpa_zb
.zb_level
));
2782 blkptr_t
*bp
= ((blkptr_t
*)abuf
->b_data
) +
2783 P2PHASE(nextblkid
, 1ULL << dpa
->dpa_epbs
);
2785 if (BP_IS_HOLE(bp
)) {
2786 kmem_free(dpa
, sizeof (*dpa
));
2787 } else if (dpa
->dpa_curlevel
== dpa
->dpa_zb
.zb_level
) {
2788 ASSERT3U(nextblkid
, ==, dpa
->dpa_zb
.zb_blkid
);
2789 dbuf_issue_final_prefetch(dpa
, bp
);
2790 kmem_free(dpa
, sizeof (*dpa
));
2792 arc_flags_t iter_aflags
= ARC_FLAG_NOWAIT
;
2793 zbookmark_phys_t zb
;
2795 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
2796 if (dpa
->dpa_aflags
& ARC_FLAG_L2CACHE
)
2797 iter_aflags
|= ARC_FLAG_L2CACHE
;
2799 ASSERT3U(dpa
->dpa_curlevel
, ==, BP_GET_LEVEL(bp
));
2801 SET_BOOKMARK(&zb
, dpa
->dpa_zb
.zb_objset
,
2802 dpa
->dpa_zb
.zb_object
, dpa
->dpa_curlevel
, nextblkid
);
2804 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
,
2805 bp
, dbuf_prefetch_indirect_done
, dpa
, dpa
->dpa_prio
,
2806 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
2810 arc_buf_destroy(abuf
, private);
2814 * Issue prefetch reads for the given block on the given level. If the indirect
2815 * blocks above that block are not in memory, we will read them in
2816 * asynchronously. As a result, this call never blocks waiting for a read to
2817 * complete. Note that the prefetch might fail if the dataset is encrypted and
2818 * the encryption key is unmapped before the IO completes.
2821 dbuf_prefetch(dnode_t
*dn
, int64_t level
, uint64_t blkid
, zio_priority_t prio
,
2825 int epbs
, nlevels
, curlevel
;
2828 ASSERT(blkid
!= DMU_BONUS_BLKID
);
2829 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2831 if (blkid
> dn
->dn_maxblkid
)
2834 if (dnode_block_freed(dn
, blkid
))
2838 * This dnode hasn't been written to disk yet, so there's nothing to
2841 nlevels
= dn
->dn_phys
->dn_nlevels
;
2842 if (level
>= nlevels
|| dn
->dn_phys
->dn_nblkptr
== 0)
2845 epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2846 if (dn
->dn_phys
->dn_maxblkid
< blkid
<< (epbs
* level
))
2849 dmu_buf_impl_t
*db
= dbuf_find(dn
->dn_objset
, dn
->dn_object
,
2852 mutex_exit(&db
->db_mtx
);
2854 * This dbuf already exists. It is either CACHED, or
2855 * (we assume) about to be read or filled.
2861 * Find the closest ancestor (indirect block) of the target block
2862 * that is present in the cache. In this indirect block, we will
2863 * find the bp that is at curlevel, curblkid.
2867 while (curlevel
< nlevels
- 1) {
2868 int parent_level
= curlevel
+ 1;
2869 uint64_t parent_blkid
= curblkid
>> epbs
;
2872 if (dbuf_hold_impl(dn
, parent_level
, parent_blkid
,
2873 FALSE
, TRUE
, FTAG
, &db
) == 0) {
2874 blkptr_t
*bpp
= db
->db_buf
->b_data
;
2875 bp
= bpp
[P2PHASE(curblkid
, 1 << epbs
)];
2876 dbuf_rele(db
, FTAG
);
2880 curlevel
= parent_level
;
2881 curblkid
= parent_blkid
;
2884 if (curlevel
== nlevels
- 1) {
2885 /* No cached indirect blocks found. */
2886 ASSERT3U(curblkid
, <, dn
->dn_phys
->dn_nblkptr
);
2887 bp
= dn
->dn_phys
->dn_blkptr
[curblkid
];
2889 if (BP_IS_HOLE(&bp
))
2892 ASSERT3U(curlevel
, ==, BP_GET_LEVEL(&bp
));
2894 zio_t
*pio
= zio_root(dmu_objset_spa(dn
->dn_objset
), NULL
, NULL
,
2897 dbuf_prefetch_arg_t
*dpa
= kmem_zalloc(sizeof (*dpa
), KM_SLEEP
);
2898 dsl_dataset_t
*ds
= dn
->dn_objset
->os_dsl_dataset
;
2899 SET_BOOKMARK(&dpa
->dpa_zb
, ds
!= NULL
? ds
->ds_object
: DMU_META_OBJSET
,
2900 dn
->dn_object
, level
, blkid
);
2901 dpa
->dpa_curlevel
= curlevel
;
2902 dpa
->dpa_prio
= prio
;
2903 dpa
->dpa_aflags
= aflags
;
2904 dpa
->dpa_spa
= dn
->dn_objset
->os_spa
;
2905 dpa
->dpa_dnode
= dn
;
2906 dpa
->dpa_epbs
= epbs
;
2909 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
2910 if (DNODE_LEVEL_IS_L2CACHEABLE(dn
, level
))
2911 dpa
->dpa_aflags
|= ARC_FLAG_L2CACHE
;
2914 * If we have the indirect just above us, no need to do the asynchronous
2915 * prefetch chain; we'll just run the last step ourselves. If we're at
2916 * a higher level, though, we want to issue the prefetches for all the
2917 * indirect blocks asynchronously, so we can go on with whatever we were
2920 if (curlevel
== level
) {
2921 ASSERT3U(curblkid
, ==, blkid
);
2922 dbuf_issue_final_prefetch(dpa
, &bp
);
2923 kmem_free(dpa
, sizeof (*dpa
));
2925 arc_flags_t iter_aflags
= ARC_FLAG_NOWAIT
;
2926 zbookmark_phys_t zb
;
2928 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
2929 if (DNODE_LEVEL_IS_L2CACHEABLE(dn
, level
))
2930 iter_aflags
|= ARC_FLAG_L2CACHE
;
2932 SET_BOOKMARK(&zb
, ds
!= NULL
? ds
->ds_object
: DMU_META_OBJSET
,
2933 dn
->dn_object
, curlevel
, curblkid
);
2934 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
,
2935 &bp
, dbuf_prefetch_indirect_done
, dpa
, prio
,
2936 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
2940 * We use pio here instead of dpa_zio since it's possible that
2941 * dpa may have already been freed.
2946 #define DBUF_HOLD_IMPL_MAX_DEPTH 20
2949 * Helper function for __dbuf_hold_impl() to copy a buffer. Handles
2950 * the case of encrypted, compressed and uncompressed buffers by
2951 * allocating the new buffer, respectively, with arc_alloc_raw_buf(),
2952 * arc_alloc_compressed_buf() or arc_alloc_buf().*
2954 * NOTE: Declared noinline to avoid stack bloat in __dbuf_hold_impl().
2956 noinline
static void
2957 dbuf_hold_copy(struct dbuf_hold_impl_data
*dh
)
2959 dnode_t
*dn
= dh
->dh_dn
;
2960 dmu_buf_impl_t
*db
= dh
->dh_db
;
2961 dbuf_dirty_record_t
*dr
= dh
->dh_dr
;
2962 arc_buf_t
*data
= dr
->dt
.dl
.dr_data
;
2964 enum zio_compress compress_type
= arc_get_compression(data
);
2966 if (arc_is_encrypted(data
)) {
2967 boolean_t byteorder
;
2968 uint8_t salt
[ZIO_DATA_SALT_LEN
];
2969 uint8_t iv
[ZIO_DATA_IV_LEN
];
2970 uint8_t mac
[ZIO_DATA_MAC_LEN
];
2972 arc_get_raw_params(data
, &byteorder
, salt
, iv
, mac
);
2973 dbuf_set_data(db
, arc_alloc_raw_buf(dn
->dn_objset
->os_spa
, db
,
2974 dmu_objset_id(dn
->dn_objset
), byteorder
, salt
, iv
, mac
,
2975 dn
->dn_type
, arc_buf_size(data
), arc_buf_lsize(data
),
2977 } else if (compress_type
!= ZIO_COMPRESS_OFF
) {
2978 dbuf_set_data(db
, arc_alloc_compressed_buf(
2979 dn
->dn_objset
->os_spa
, db
, arc_buf_size(data
),
2980 arc_buf_lsize(data
), compress_type
));
2982 dbuf_set_data(db
, arc_alloc_buf(dn
->dn_objset
->os_spa
, db
,
2983 DBUF_GET_BUFC_TYPE(db
), db
->db
.db_size
));
2986 bcopy(data
->b_data
, db
->db
.db_data
, arc_buf_size(data
));
2990 * Returns with db_holds incremented, and db_mtx not held.
2991 * Note: dn_struct_rwlock must be held.
2994 __dbuf_hold_impl(struct dbuf_hold_impl_data
*dh
)
2996 ASSERT3S(dh
->dh_depth
, <, DBUF_HOLD_IMPL_MAX_DEPTH
);
2997 dh
->dh_parent
= NULL
;
2999 ASSERT(dh
->dh_blkid
!= DMU_BONUS_BLKID
);
3000 ASSERT(RW_LOCK_HELD(&dh
->dh_dn
->dn_struct_rwlock
));
3001 ASSERT3U(dh
->dh_dn
->dn_nlevels
, >, dh
->dh_level
);
3003 *(dh
->dh_dbp
) = NULL
;
3005 /* dbuf_find() returns with db_mtx held */
3006 dh
->dh_db
= dbuf_find(dh
->dh_dn
->dn_objset
, dh
->dh_dn
->dn_object
,
3007 dh
->dh_level
, dh
->dh_blkid
);
3009 if (dh
->dh_db
== NULL
) {
3012 if (dh
->dh_fail_uncached
)
3013 return (SET_ERROR(ENOENT
));
3015 ASSERT3P(dh
->dh_parent
, ==, NULL
);
3016 dh
->dh_err
= dbuf_findbp(dh
->dh_dn
, dh
->dh_level
, dh
->dh_blkid
,
3017 dh
->dh_fail_sparse
, &dh
->dh_parent
, &dh
->dh_bp
, dh
);
3018 if (dh
->dh_fail_sparse
) {
3019 if (dh
->dh_err
== 0 &&
3020 dh
->dh_bp
&& BP_IS_HOLE(dh
->dh_bp
))
3021 dh
->dh_err
= SET_ERROR(ENOENT
);
3024 dbuf_rele(dh
->dh_parent
, NULL
);
3025 return (dh
->dh_err
);
3028 if (dh
->dh_err
&& dh
->dh_err
!= ENOENT
)
3029 return (dh
->dh_err
);
3030 dh
->dh_db
= dbuf_create(dh
->dh_dn
, dh
->dh_level
, dh
->dh_blkid
,
3031 dh
->dh_parent
, dh
->dh_bp
);
3034 if (dh
->dh_fail_uncached
&& dh
->dh_db
->db_state
!= DB_CACHED
) {
3035 mutex_exit(&dh
->dh_db
->db_mtx
);
3036 return (SET_ERROR(ENOENT
));
3039 if (dh
->dh_db
->db_buf
!= NULL
) {
3040 arc_buf_access(dh
->dh_db
->db_buf
);
3041 ASSERT3P(dh
->dh_db
->db
.db_data
, ==, dh
->dh_db
->db_buf
->b_data
);
3044 ASSERT(dh
->dh_db
->db_buf
== NULL
|| arc_referenced(dh
->dh_db
->db_buf
));
3047 * If this buffer is currently syncing out, and we are are
3048 * still referencing it from db_data, we need to make a copy
3049 * of it in case we decide we want to dirty it again in this txg.
3051 if (dh
->dh_db
->db_level
== 0 &&
3052 dh
->dh_db
->db_blkid
!= DMU_BONUS_BLKID
&&
3053 dh
->dh_dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
3054 dh
->dh_db
->db_state
== DB_CACHED
&& dh
->dh_db
->db_data_pending
) {
3055 dh
->dh_dr
= dh
->dh_db
->db_data_pending
;
3056 if (dh
->dh_dr
->dt
.dl
.dr_data
== dh
->dh_db
->db_buf
)
3060 if (multilist_link_active(&dh
->dh_db
->db_cache_link
)) {
3061 ASSERT(refcount_is_zero(&dh
->dh_db
->db_holds
));
3062 multilist_remove(dbuf_cache
, dh
->dh_db
);
3063 (void) refcount_remove_many(&dbuf_cache_size
,
3064 dh
->dh_db
->db
.db_size
, dh
->dh_db
);
3065 DBUF_STAT_BUMPDOWN(cache_levels
[dh
->dh_db
->db_level
]);
3066 DBUF_STAT_BUMPDOWN(cache_count
);
3067 DBUF_STAT_DECR(cache_levels_bytes
[dh
->dh_db
->db_level
],
3068 dh
->dh_db
->db
.db_size
);
3070 (void) refcount_add(&dh
->dh_db
->db_holds
, dh
->dh_tag
);
3071 DBUF_VERIFY(dh
->dh_db
);
3072 mutex_exit(&dh
->dh_db
->db_mtx
);
3074 /* NOTE: we can't rele the parent until after we drop the db_mtx */
3076 dbuf_rele(dh
->dh_parent
, NULL
);
3078 ASSERT3P(DB_DNODE(dh
->dh_db
), ==, dh
->dh_dn
);
3079 ASSERT3U(dh
->dh_db
->db_blkid
, ==, dh
->dh_blkid
);
3080 ASSERT3U(dh
->dh_db
->db_level
, ==, dh
->dh_level
);
3081 *(dh
->dh_dbp
) = dh
->dh_db
;
3087 * The following code preserves the recursive function dbuf_hold_impl()
3088 * but moves the local variables AND function arguments to the heap to
3089 * minimize the stack frame size. Enough space is initially allocated
3090 * on the stack for 20 levels of recursion.
3093 dbuf_hold_impl(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
3094 boolean_t fail_sparse
, boolean_t fail_uncached
,
3095 void *tag
, dmu_buf_impl_t
**dbp
)
3097 struct dbuf_hold_impl_data
*dh
;
3100 dh
= kmem_alloc(sizeof (struct dbuf_hold_impl_data
) *
3101 DBUF_HOLD_IMPL_MAX_DEPTH
, KM_SLEEP
);
3102 __dbuf_hold_impl_init(dh
, dn
, level
, blkid
, fail_sparse
,
3103 fail_uncached
, tag
, dbp
, 0);
3105 error
= __dbuf_hold_impl(dh
);
3107 kmem_free(dh
, sizeof (struct dbuf_hold_impl_data
) *
3108 DBUF_HOLD_IMPL_MAX_DEPTH
);
3114 __dbuf_hold_impl_init(struct dbuf_hold_impl_data
*dh
,
3115 dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
3116 boolean_t fail_sparse
, boolean_t fail_uncached
,
3117 void *tag
, dmu_buf_impl_t
**dbp
, int depth
)
3120 dh
->dh_level
= level
;
3121 dh
->dh_blkid
= blkid
;
3123 dh
->dh_fail_sparse
= fail_sparse
;
3124 dh
->dh_fail_uncached
= fail_uncached
;
3130 dh
->dh_parent
= NULL
;
3135 dh
->dh_depth
= depth
;
3139 dbuf_hold(dnode_t
*dn
, uint64_t blkid
, void *tag
)
3141 return (dbuf_hold_level(dn
, 0, blkid
, tag
));
3145 dbuf_hold_level(dnode_t
*dn
, int level
, uint64_t blkid
, void *tag
)
3148 int err
= dbuf_hold_impl(dn
, level
, blkid
, FALSE
, FALSE
, tag
, &db
);
3149 return (err
? NULL
: db
);
3153 dbuf_create_bonus(dnode_t
*dn
)
3155 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
3157 ASSERT(dn
->dn_bonus
== NULL
);
3158 dn
->dn_bonus
= dbuf_create(dn
, 0, DMU_BONUS_BLKID
, dn
->dn_dbuf
, NULL
);
3162 dbuf_spill_set_blksz(dmu_buf_t
*db_fake
, uint64_t blksz
, dmu_tx_t
*tx
)
3164 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3167 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
3168 return (SET_ERROR(ENOTSUP
));
3170 blksz
= SPA_MINBLOCKSIZE
;
3171 ASSERT3U(blksz
, <=, spa_maxblocksize(dmu_objset_spa(db
->db_objset
)));
3172 blksz
= P2ROUNDUP(blksz
, SPA_MINBLOCKSIZE
);
3176 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
3177 dbuf_new_size(db
, blksz
, tx
);
3178 rw_exit(&dn
->dn_struct_rwlock
);
3185 dbuf_rm_spill(dnode_t
*dn
, dmu_tx_t
*tx
)
3187 dbuf_free_range(dn
, DMU_SPILL_BLKID
, DMU_SPILL_BLKID
, tx
);
3190 #pragma weak dmu_buf_add_ref = dbuf_add_ref
3192 dbuf_add_ref(dmu_buf_impl_t
*db
, void *tag
)
3194 int64_t holds
= refcount_add(&db
->db_holds
, tag
);
3195 VERIFY3S(holds
, >, 1);
3198 #pragma weak dmu_buf_try_add_ref = dbuf_try_add_ref
3200 dbuf_try_add_ref(dmu_buf_t
*db_fake
, objset_t
*os
, uint64_t obj
, uint64_t blkid
,
3203 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3204 dmu_buf_impl_t
*found_db
;
3205 boolean_t result
= B_FALSE
;
3207 if (blkid
== DMU_BONUS_BLKID
)
3208 found_db
= dbuf_find_bonus(os
, obj
);
3210 found_db
= dbuf_find(os
, obj
, 0, blkid
);
3212 if (found_db
!= NULL
) {
3213 if (db
== found_db
&& dbuf_refcount(db
) > db
->db_dirtycnt
) {
3214 (void) refcount_add(&db
->db_holds
, tag
);
3217 mutex_exit(&found_db
->db_mtx
);
3223 * If you call dbuf_rele() you had better not be referencing the dnode handle
3224 * unless you have some other direct or indirect hold on the dnode. (An indirect
3225 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
3226 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
3227 * dnode's parent dbuf evicting its dnode handles.
3230 dbuf_rele(dmu_buf_impl_t
*db
, void *tag
)
3232 mutex_enter(&db
->db_mtx
);
3233 dbuf_rele_and_unlock(db
, tag
, B_FALSE
);
3237 dmu_buf_rele(dmu_buf_t
*db
, void *tag
)
3239 dbuf_rele((dmu_buf_impl_t
*)db
, tag
);
3243 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
3244 * db_dirtycnt and db_holds to be updated atomically. The 'evicting'
3245 * argument should be set if we are already in the dbuf-evicting code
3246 * path, in which case we don't want to recursively evict. This allows us to
3247 * avoid deeply nested stacks that would have a call flow similar to this:
3249 * dbuf_rele()-->dbuf_rele_and_unlock()-->dbuf_evict_notify()
3252 * +-----dbuf_destroy()<--dbuf_evict_one()<--------+
3256 dbuf_rele_and_unlock(dmu_buf_impl_t
*db
, void *tag
, boolean_t evicting
)
3260 ASSERT(MUTEX_HELD(&db
->db_mtx
));
3264 * Remove the reference to the dbuf before removing its hold on the
3265 * dnode so we can guarantee in dnode_move() that a referenced bonus
3266 * buffer has a corresponding dnode hold.
3268 holds
= refcount_remove(&db
->db_holds
, tag
);
3272 * We can't freeze indirects if there is a possibility that they
3273 * may be modified in the current syncing context.
3275 if (db
->db_buf
!= NULL
&&
3276 holds
== (db
->db_level
== 0 ? db
->db_dirtycnt
: 0)) {
3277 arc_buf_freeze(db
->db_buf
);
3280 if (holds
== db
->db_dirtycnt
&&
3281 db
->db_level
== 0 && db
->db_user_immediate_evict
)
3282 dbuf_evict_user(db
);
3285 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
3287 boolean_t evict_dbuf
= db
->db_pending_evict
;
3290 * If the dnode moves here, we cannot cross this
3291 * barrier until the move completes.
3296 atomic_dec_32(&dn
->dn_dbufs_count
);
3299 * Decrementing the dbuf count means that the bonus
3300 * buffer's dnode hold is no longer discounted in
3301 * dnode_move(). The dnode cannot move until after
3302 * the dnode_rele() below.
3307 * Do not reference db after its lock is dropped.
3308 * Another thread may evict it.
3310 mutex_exit(&db
->db_mtx
);
3313 dnode_evict_bonus(dn
);
3316 } else if (db
->db_buf
== NULL
) {
3318 * This is a special case: we never associated this
3319 * dbuf with any data allocated from the ARC.
3321 ASSERT(db
->db_state
== DB_UNCACHED
||
3322 db
->db_state
== DB_NOFILL
);
3324 } else if (arc_released(db
->db_buf
)) {
3326 * This dbuf has anonymous data associated with it.
3330 boolean_t do_arc_evict
= B_FALSE
;
3332 spa_t
*spa
= dmu_objset_spa(db
->db_objset
);
3334 if (!DBUF_IS_CACHEABLE(db
) &&
3335 db
->db_blkptr
!= NULL
&&
3336 !BP_IS_HOLE(db
->db_blkptr
) &&
3337 !BP_IS_EMBEDDED(db
->db_blkptr
)) {
3338 do_arc_evict
= B_TRUE
;
3339 bp
= *db
->db_blkptr
;
3342 if (!DBUF_IS_CACHEABLE(db
) ||
3343 db
->db_pending_evict
) {
3345 } else if (!multilist_link_active(&db
->db_cache_link
)) {
3346 multilist_insert(dbuf_cache
, db
);
3347 (void) refcount_add_many(&dbuf_cache_size
,
3348 db
->db
.db_size
, db
);
3349 DBUF_STAT_BUMP(cache_levels
[db
->db_level
]);
3350 DBUF_STAT_BUMP(cache_count
);
3351 DBUF_STAT_INCR(cache_levels_bytes
[db
->db_level
],
3353 DBUF_STAT_MAX(cache_size_bytes_max
,
3354 refcount_count(&dbuf_cache_size
));
3355 mutex_exit(&db
->db_mtx
);
3358 dbuf_evict_notify();
3362 arc_freed(spa
, &bp
);
3365 mutex_exit(&db
->db_mtx
);
3370 #pragma weak dmu_buf_refcount = dbuf_refcount
3372 dbuf_refcount(dmu_buf_impl_t
*db
)
3374 return (refcount_count(&db
->db_holds
));
3378 dmu_buf_user_refcount(dmu_buf_t
*db_fake
)
3381 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3383 mutex_enter(&db
->db_mtx
);
3384 ASSERT3U(refcount_count(&db
->db_holds
), >=, db
->db_dirtycnt
);
3385 holds
= refcount_count(&db
->db_holds
) - db
->db_dirtycnt
;
3386 mutex_exit(&db
->db_mtx
);
3392 dmu_buf_replace_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*old_user
,
3393 dmu_buf_user_t
*new_user
)
3395 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3397 mutex_enter(&db
->db_mtx
);
3398 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
3399 if (db
->db_user
== old_user
)
3400 db
->db_user
= new_user
;
3402 old_user
= db
->db_user
;
3403 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
3404 mutex_exit(&db
->db_mtx
);
3410 dmu_buf_set_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
3412 return (dmu_buf_replace_user(db_fake
, NULL
, user
));
3416 dmu_buf_set_user_ie(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
3418 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3420 db
->db_user_immediate_evict
= TRUE
;
3421 return (dmu_buf_set_user(db_fake
, user
));
3425 dmu_buf_remove_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
3427 return (dmu_buf_replace_user(db_fake
, user
, NULL
));
3431 dmu_buf_get_user(dmu_buf_t
*db_fake
)
3433 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3435 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
3436 return (db
->db_user
);
3440 dmu_buf_user_evict_wait()
3442 taskq_wait(dbu_evict_taskq
);
3446 dmu_buf_get_blkptr(dmu_buf_t
*db
)
3448 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3449 return (dbi
->db_blkptr
);
3453 dmu_buf_get_objset(dmu_buf_t
*db
)
3455 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3456 return (dbi
->db_objset
);
3460 dmu_buf_dnode_enter(dmu_buf_t
*db
)
3462 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3463 DB_DNODE_ENTER(dbi
);
3464 return (DB_DNODE(dbi
));
3468 dmu_buf_dnode_exit(dmu_buf_t
*db
)
3470 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3475 dbuf_check_blkptr(dnode_t
*dn
, dmu_buf_impl_t
*db
)
3477 /* ASSERT(dmu_tx_is_syncing(tx) */
3478 ASSERT(MUTEX_HELD(&db
->db_mtx
));
3480 if (db
->db_blkptr
!= NULL
)
3483 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3484 db
->db_blkptr
= DN_SPILL_BLKPTR(dn
->dn_phys
);
3485 BP_ZERO(db
->db_blkptr
);
3488 if (db
->db_level
== dn
->dn_phys
->dn_nlevels
-1) {
3490 * This buffer was allocated at a time when there was
3491 * no available blkptrs from the dnode, or it was
3492 * inappropriate to hook it in (i.e., nlevels mis-match).
3494 ASSERT(db
->db_blkid
< dn
->dn_phys
->dn_nblkptr
);
3495 ASSERT(db
->db_parent
== NULL
);
3496 db
->db_parent
= dn
->dn_dbuf
;
3497 db
->db_blkptr
= &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
];
3500 dmu_buf_impl_t
*parent
= db
->db_parent
;
3501 int epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
3503 ASSERT(dn
->dn_phys
->dn_nlevels
> 1);
3504 if (parent
== NULL
) {
3505 mutex_exit(&db
->db_mtx
);
3506 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
3507 parent
= dbuf_hold_level(dn
, db
->db_level
+ 1,
3508 db
->db_blkid
>> epbs
, db
);
3509 rw_exit(&dn
->dn_struct_rwlock
);
3510 mutex_enter(&db
->db_mtx
);
3511 db
->db_parent
= parent
;
3513 db
->db_blkptr
= (blkptr_t
*)parent
->db
.db_data
+
3514 (db
->db_blkid
& ((1ULL << epbs
) - 1));
3520 * When syncing out a blocks of dnodes, adjust the block to deal with
3521 * encryption. Normally, we make sure the block is decrypted before writing
3522 * it. If we have crypt params, then we are writing a raw (encrypted) block,
3523 * from a raw receive. In this case, set the ARC buf's crypt params so
3524 * that the BP will be filled with the correct byteorder, salt, iv, and mac.
3527 dbuf_prepare_encrypted_dnode_leaf(dbuf_dirty_record_t
*dr
)
3530 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3532 ASSERT(MUTEX_HELD(&db
->db_mtx
));
3533 ASSERT3U(db
->db
.db_object
, ==, DMU_META_DNODE_OBJECT
);
3534 ASSERT3U(db
->db_level
, ==, 0);
3536 if (!db
->db_objset
->os_raw_receive
&& arc_is_encrypted(db
->db_buf
)) {
3537 zbookmark_phys_t zb
;
3540 * Unfortunately, there is currently no mechanism for
3541 * syncing context to handle decryption errors. An error
3542 * here is only possible if an attacker maliciously
3543 * changed a dnode block and updated the associated
3544 * checksums going up the block tree.
3546 SET_BOOKMARK(&zb
, dmu_objset_id(db
->db_objset
),
3547 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
3548 err
= arc_untransform(db
->db_buf
, db
->db_objset
->os_spa
,
3551 panic("Invalid dnode block MAC");
3552 } else if (dr
->dt
.dl
.dr_has_raw_params
) {
3553 (void) arc_release(dr
->dt
.dl
.dr_data
, db
);
3554 arc_convert_to_raw(dr
->dt
.dl
.dr_data
,
3555 dmu_objset_id(db
->db_objset
),
3556 dr
->dt
.dl
.dr_byteorder
, DMU_OT_DNODE
,
3557 dr
->dt
.dl
.dr_salt
, dr
->dt
.dl
.dr_iv
, dr
->dt
.dl
.dr_mac
);
3562 * dbuf_sync_indirect() is called recursively from dbuf_sync_list() so it
3563 * is critical the we not allow the compiler to inline this function in to
3564 * dbuf_sync_list() thereby drastically bloating the stack usage.
3566 noinline
static void
3567 dbuf_sync_indirect(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
3569 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3573 ASSERT(dmu_tx_is_syncing(tx
));
3575 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
3577 mutex_enter(&db
->db_mtx
);
3579 ASSERT(db
->db_level
> 0);
3582 /* Read the block if it hasn't been read yet. */
3583 if (db
->db_buf
== NULL
) {
3584 mutex_exit(&db
->db_mtx
);
3585 (void) dbuf_read(db
, NULL
, DB_RF_MUST_SUCCEED
);
3586 mutex_enter(&db
->db_mtx
);
3588 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
3589 ASSERT(db
->db_buf
!= NULL
);
3593 /* Indirect block size must match what the dnode thinks it is. */
3594 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
3595 dbuf_check_blkptr(dn
, db
);
3598 /* Provide the pending dirty record to child dbufs */
3599 db
->db_data_pending
= dr
;
3601 mutex_exit(&db
->db_mtx
);
3603 dbuf_write(dr
, db
->db_buf
, tx
);
3606 mutex_enter(&dr
->dt
.di
.dr_mtx
);
3607 dbuf_sync_list(&dr
->dt
.di
.dr_children
, db
->db_level
- 1, tx
);
3608 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
3609 mutex_exit(&dr
->dt
.di
.dr_mtx
);
3614 * dbuf_sync_leaf() is called recursively from dbuf_sync_list() so it is
3615 * critical the we not allow the compiler to inline this function in to
3616 * dbuf_sync_list() thereby drastically bloating the stack usage.
3618 noinline
static void
3619 dbuf_sync_leaf(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
3621 arc_buf_t
**datap
= &dr
->dt
.dl
.dr_data
;
3622 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3625 uint64_t txg
= tx
->tx_txg
;
3627 ASSERT(dmu_tx_is_syncing(tx
));
3629 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
3631 mutex_enter(&db
->db_mtx
);
3633 * To be synced, we must be dirtied. But we
3634 * might have been freed after the dirty.
3636 if (db
->db_state
== DB_UNCACHED
) {
3637 /* This buffer has been freed since it was dirtied */
3638 ASSERT(db
->db
.db_data
== NULL
);
3639 } else if (db
->db_state
== DB_FILL
) {
3640 /* This buffer was freed and is now being re-filled */
3641 ASSERT(db
->db
.db_data
!= dr
->dt
.dl
.dr_data
);
3643 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_NOFILL
);
3650 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3651 mutex_enter(&dn
->dn_mtx
);
3652 if (!(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
)) {
3654 * In the previous transaction group, the bonus buffer
3655 * was entirely used to store the attributes for the
3656 * dnode which overrode the dn_spill field. However,
3657 * when adding more attributes to the file a spill
3658 * block was required to hold the extra attributes.
3660 * Make sure to clear the garbage left in the dn_spill
3661 * field from the previous attributes in the bonus
3662 * buffer. Otherwise, after writing out the spill
3663 * block to the new allocated dva, it will free
3664 * the old block pointed to by the invalid dn_spill.
3666 db
->db_blkptr
= NULL
;
3668 dn
->dn_phys
->dn_flags
|= DNODE_FLAG_SPILL_BLKPTR
;
3669 mutex_exit(&dn
->dn_mtx
);
3673 * If this is a bonus buffer, simply copy the bonus data into the
3674 * dnode. It will be written out when the dnode is synced (and it
3675 * will be synced, since it must have been dirty for dbuf_sync to
3678 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
3679 dbuf_dirty_record_t
**drp
;
3681 ASSERT(*datap
!= NULL
);
3682 ASSERT0(db
->db_level
);
3683 ASSERT3U(DN_MAX_BONUS_LEN(dn
->dn_phys
), <=,
3684 DN_SLOTS_TO_BONUSLEN(dn
->dn_phys
->dn_extra_slots
+ 1));
3685 bcopy(*datap
, DN_BONUS(dn
->dn_phys
),
3686 DN_MAX_BONUS_LEN(dn
->dn_phys
));
3689 if (*datap
!= db
->db
.db_data
) {
3690 int slots
= DB_DNODE(db
)->dn_num_slots
;
3691 int bonuslen
= DN_SLOTS_TO_BONUSLEN(slots
);
3692 kmem_free(*datap
, bonuslen
);
3693 arc_space_return(bonuslen
, ARC_SPACE_BONUS
);
3695 db
->db_data_pending
= NULL
;
3696 drp
= &db
->db_last_dirty
;
3698 drp
= &(*drp
)->dr_next
;
3699 ASSERT(dr
->dr_next
== NULL
);
3700 ASSERT(dr
->dr_dbuf
== db
);
3702 if (dr
->dr_dbuf
->db_level
!= 0) {
3703 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
3704 list_destroy(&dr
->dt
.di
.dr_children
);
3706 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
3707 ASSERT(db
->db_dirtycnt
> 0);
3708 db
->db_dirtycnt
-= 1;
3709 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)txg
, B_FALSE
);
3716 * This function may have dropped the db_mtx lock allowing a dmu_sync
3717 * operation to sneak in. As a result, we need to ensure that we
3718 * don't check the dr_override_state until we have returned from
3719 * dbuf_check_blkptr.
3721 dbuf_check_blkptr(dn
, db
);
3724 * If this buffer is in the middle of an immediate write,
3725 * wait for the synchronous IO to complete.
3727 while (dr
->dt
.dl
.dr_override_state
== DR_IN_DMU_SYNC
) {
3728 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
3729 cv_wait(&db
->db_changed
, &db
->db_mtx
);
3730 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_NOT_OVERRIDDEN
);
3734 * If this is a dnode block, ensure it is appropriately encrypted
3735 * or decrypted, depending on what we are writing to it this txg.
3737 if (os
->os_encrypted
&& dn
->dn_object
== DMU_META_DNODE_OBJECT
)
3738 dbuf_prepare_encrypted_dnode_leaf(dr
);
3740 if (db
->db_state
!= DB_NOFILL
&&
3741 dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
3742 refcount_count(&db
->db_holds
) > 1 &&
3743 dr
->dt
.dl
.dr_override_state
!= DR_OVERRIDDEN
&&
3744 *datap
== db
->db_buf
) {
3746 * If this buffer is currently "in use" (i.e., there
3747 * are active holds and db_data still references it),
3748 * then make a copy before we start the write so that
3749 * any modifications from the open txg will not leak
3752 * NOTE: this copy does not need to be made for
3753 * objects only modified in the syncing context (e.g.
3754 * DNONE_DNODE blocks).
3756 int psize
= arc_buf_size(*datap
);
3757 int lsize
= arc_buf_lsize(*datap
);
3758 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
3759 enum zio_compress compress_type
= arc_get_compression(*datap
);
3761 if (arc_is_encrypted(*datap
)) {
3762 boolean_t byteorder
;
3763 uint8_t salt
[ZIO_DATA_SALT_LEN
];
3764 uint8_t iv
[ZIO_DATA_IV_LEN
];
3765 uint8_t mac
[ZIO_DATA_MAC_LEN
];
3767 arc_get_raw_params(*datap
, &byteorder
, salt
, iv
, mac
);
3768 *datap
= arc_alloc_raw_buf(os
->os_spa
, db
,
3769 dmu_objset_id(os
), byteorder
, salt
, iv
, mac
,
3770 dn
->dn_type
, psize
, lsize
, compress_type
);
3771 } else if (compress_type
!= ZIO_COMPRESS_OFF
) {
3772 ASSERT3U(type
, ==, ARC_BUFC_DATA
);
3773 *datap
= arc_alloc_compressed_buf(os
->os_spa
, db
,
3774 psize
, lsize
, compress_type
);
3776 *datap
= arc_alloc_buf(os
->os_spa
, db
, type
, psize
);
3778 bcopy(db
->db
.db_data
, (*datap
)->b_data
, psize
);
3780 db
->db_data_pending
= dr
;
3782 mutex_exit(&db
->db_mtx
);
3784 dbuf_write(dr
, *datap
, tx
);
3786 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
3787 if (dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
3788 list_insert_tail(&dn
->dn_dirty_records
[txg
&TXG_MASK
], dr
);
3792 * Although zio_nowait() does not "wait for an IO", it does
3793 * initiate the IO. If this is an empty write it seems plausible
3794 * that the IO could actually be completed before the nowait
3795 * returns. We need to DB_DNODE_EXIT() first in case
3796 * zio_nowait() invalidates the dbuf.
3799 zio_nowait(dr
->dr_zio
);
3804 dbuf_sync_list(list_t
*list
, int level
, dmu_tx_t
*tx
)
3806 dbuf_dirty_record_t
*dr
;
3808 while ((dr
= list_head(list
))) {
3809 if (dr
->dr_zio
!= NULL
) {
3811 * If we find an already initialized zio then we
3812 * are processing the meta-dnode, and we have finished.
3813 * The dbufs for all dnodes are put back on the list
3814 * during processing, so that we can zio_wait()
3815 * these IOs after initiating all child IOs.
3817 ASSERT3U(dr
->dr_dbuf
->db
.db_object
, ==,
3818 DMU_META_DNODE_OBJECT
);
3821 if (dr
->dr_dbuf
->db_blkid
!= DMU_BONUS_BLKID
&&
3822 dr
->dr_dbuf
->db_blkid
!= DMU_SPILL_BLKID
) {
3823 VERIFY3U(dr
->dr_dbuf
->db_level
, ==, level
);
3825 list_remove(list
, dr
);
3826 if (dr
->dr_dbuf
->db_level
> 0)
3827 dbuf_sync_indirect(dr
, tx
);
3829 dbuf_sync_leaf(dr
, tx
);
3835 dbuf_write_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
3837 dmu_buf_impl_t
*db
= vdb
;
3839 blkptr_t
*bp
= zio
->io_bp
;
3840 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
3841 spa_t
*spa
= zio
->io_spa
;
3846 ASSERT3P(db
->db_blkptr
, !=, NULL
);
3847 ASSERT3P(&db
->db_data_pending
->dr_bp_copy
, ==, bp
);
3851 delta
= bp_get_dsize_sync(spa
, bp
) - bp_get_dsize_sync(spa
, bp_orig
);
3852 dnode_diduse_space(dn
, delta
- zio
->io_prev_space_delta
);
3853 zio
->io_prev_space_delta
= delta
;
3855 if (bp
->blk_birth
!= 0) {
3856 ASSERT((db
->db_blkid
!= DMU_SPILL_BLKID
&&
3857 BP_GET_TYPE(bp
) == dn
->dn_type
) ||
3858 (db
->db_blkid
== DMU_SPILL_BLKID
&&
3859 BP_GET_TYPE(bp
) == dn
->dn_bonustype
) ||
3860 BP_IS_EMBEDDED(bp
));
3861 ASSERT(BP_GET_LEVEL(bp
) == db
->db_level
);
3864 mutex_enter(&db
->db_mtx
);
3867 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3868 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
3869 ASSERT(!(BP_IS_HOLE(bp
)) &&
3870 db
->db_blkptr
== DN_SPILL_BLKPTR(dn
->dn_phys
));
3874 if (db
->db_level
== 0) {
3875 mutex_enter(&dn
->dn_mtx
);
3876 if (db
->db_blkid
> dn
->dn_phys
->dn_maxblkid
&&
3877 db
->db_blkid
!= DMU_SPILL_BLKID
) {
3878 ASSERT0(db
->db_objset
->os_raw_receive
);
3879 dn
->dn_phys
->dn_maxblkid
= db
->db_blkid
;
3881 mutex_exit(&dn
->dn_mtx
);
3883 if (dn
->dn_type
== DMU_OT_DNODE
) {
3885 while (i
< db
->db
.db_size
) {
3887 (void *)(((char *)db
->db
.db_data
) + i
);
3889 i
+= DNODE_MIN_SIZE
;
3890 if (dnp
->dn_type
!= DMU_OT_NONE
) {
3892 i
+= dnp
->dn_extra_slots
*
3897 if (BP_IS_HOLE(bp
)) {
3904 blkptr_t
*ibp
= db
->db
.db_data
;
3905 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
3906 for (i
= db
->db
.db_size
>> SPA_BLKPTRSHIFT
; i
> 0; i
--, ibp
++) {
3907 if (BP_IS_HOLE(ibp
))
3909 fill
+= BP_GET_FILL(ibp
);
3914 if (!BP_IS_EMBEDDED(bp
))
3915 BP_SET_FILL(bp
, fill
);
3917 mutex_exit(&db
->db_mtx
);
3919 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
3920 *db
->db_blkptr
= *bp
;
3921 rw_exit(&dn
->dn_struct_rwlock
);
3926 * This function gets called just prior to running through the compression
3927 * stage of the zio pipeline. If we're an indirect block comprised of only
3928 * holes, then we want this indirect to be compressed away to a hole. In
3929 * order to do that we must zero out any information about the holes that
3930 * this indirect points to prior to before we try to compress it.
3933 dbuf_write_children_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
3935 dmu_buf_impl_t
*db
= vdb
;
3938 unsigned int epbs
, i
;
3940 ASSERT3U(db
->db_level
, >, 0);
3943 epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
3944 ASSERT3U(epbs
, <, 31);
3946 /* Determine if all our children are holes */
3947 for (i
= 0, bp
= db
->db
.db_data
; i
< 1ULL << epbs
; i
++, bp
++) {
3948 if (!BP_IS_HOLE(bp
))
3953 * If all the children are holes, then zero them all out so that
3954 * we may get compressed away.
3956 if (i
== 1ULL << epbs
) {
3958 * We only found holes. Grab the rwlock to prevent
3959 * anybody from reading the blocks we're about to
3962 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
3963 bzero(db
->db
.db_data
, db
->db
.db_size
);
3964 rw_exit(&dn
->dn_struct_rwlock
);
3970 * The SPA will call this callback several times for each zio - once
3971 * for every physical child i/o (zio->io_phys_children times). This
3972 * allows the DMU to monitor the progress of each logical i/o. For example,
3973 * there may be 2 copies of an indirect block, or many fragments of a RAID-Z
3974 * block. There may be a long delay before all copies/fragments are completed,
3975 * so this callback allows us to retire dirty space gradually, as the physical
3980 dbuf_write_physdone(zio_t
*zio
, arc_buf_t
*buf
, void *arg
)
3982 dmu_buf_impl_t
*db
= arg
;
3983 objset_t
*os
= db
->db_objset
;
3984 dsl_pool_t
*dp
= dmu_objset_pool(os
);
3985 dbuf_dirty_record_t
*dr
;
3988 dr
= db
->db_data_pending
;
3989 ASSERT3U(dr
->dr_txg
, ==, zio
->io_txg
);
3992 * The callback will be called io_phys_children times. Retire one
3993 * portion of our dirty space each time we are called. Any rounding
3994 * error will be cleaned up by dsl_pool_sync()'s call to
3995 * dsl_pool_undirty_space().
3997 delta
= dr
->dr_accounted
/ zio
->io_phys_children
;
3998 dsl_pool_undirty_space(dp
, delta
, zio
->io_txg
);
4003 dbuf_write_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
4005 dmu_buf_impl_t
*db
= vdb
;
4006 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
4007 blkptr_t
*bp
= db
->db_blkptr
;
4008 objset_t
*os
= db
->db_objset
;
4009 dmu_tx_t
*tx
= os
->os_synctx
;
4010 dbuf_dirty_record_t
**drp
, *dr
;
4012 ASSERT0(zio
->io_error
);
4013 ASSERT(db
->db_blkptr
== bp
);
4016 * For nopwrites and rewrites we ensure that the bp matches our
4017 * original and bypass all the accounting.
4019 if (zio
->io_flags
& (ZIO_FLAG_IO_REWRITE
| ZIO_FLAG_NOPWRITE
)) {
4020 ASSERT(BP_EQUAL(bp
, bp_orig
));
4022 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
4023 (void) dsl_dataset_block_kill(ds
, bp_orig
, tx
, B_TRUE
);
4024 dsl_dataset_block_born(ds
, bp
, tx
);
4027 mutex_enter(&db
->db_mtx
);
4031 drp
= &db
->db_last_dirty
;
4032 while ((dr
= *drp
) != db
->db_data_pending
)
4034 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
4035 ASSERT(dr
->dr_dbuf
== db
);
4036 ASSERT(dr
->dr_next
== NULL
);
4040 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
4045 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
4046 ASSERT(!(BP_IS_HOLE(db
->db_blkptr
)) &&
4047 db
->db_blkptr
== DN_SPILL_BLKPTR(dn
->dn_phys
));
4052 if (db
->db_level
== 0) {
4053 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
4054 ASSERT(dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
);
4055 if (db
->db_state
!= DB_NOFILL
) {
4056 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
4057 arc_buf_destroy(dr
->dt
.dl
.dr_data
, db
);
4064 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
4065 ASSERT3U(db
->db
.db_size
, ==, 1 << dn
->dn_phys
->dn_indblkshift
);
4066 if (!BP_IS_HOLE(db
->db_blkptr
)) {
4067 ASSERTV(int epbs
= dn
->dn_phys
->dn_indblkshift
-
4069 ASSERT3U(db
->db_blkid
, <=,
4070 dn
->dn_phys
->dn_maxblkid
>> (db
->db_level
* epbs
));
4071 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==,
4075 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
4076 list_destroy(&dr
->dt
.di
.dr_children
);
4078 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
4080 cv_broadcast(&db
->db_changed
);
4081 ASSERT(db
->db_dirtycnt
> 0);
4082 db
->db_dirtycnt
-= 1;
4083 db
->db_data_pending
= NULL
;
4084 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)tx
->tx_txg
, B_FALSE
);
4088 dbuf_write_nofill_ready(zio_t
*zio
)
4090 dbuf_write_ready(zio
, NULL
, zio
->io_private
);
4094 dbuf_write_nofill_done(zio_t
*zio
)
4096 dbuf_write_done(zio
, NULL
, zio
->io_private
);
4100 dbuf_write_override_ready(zio_t
*zio
)
4102 dbuf_dirty_record_t
*dr
= zio
->io_private
;
4103 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
4105 dbuf_write_ready(zio
, NULL
, db
);
4109 dbuf_write_override_done(zio_t
*zio
)
4111 dbuf_dirty_record_t
*dr
= zio
->io_private
;
4112 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
4113 blkptr_t
*obp
= &dr
->dt
.dl
.dr_overridden_by
;
4115 mutex_enter(&db
->db_mtx
);
4116 if (!BP_EQUAL(zio
->io_bp
, obp
)) {
4117 if (!BP_IS_HOLE(obp
))
4118 dsl_free(spa_get_dsl(zio
->io_spa
), zio
->io_txg
, obp
);
4119 arc_release(dr
->dt
.dl
.dr_data
, db
);
4121 mutex_exit(&db
->db_mtx
);
4123 dbuf_write_done(zio
, NULL
, db
);
4125 if (zio
->io_abd
!= NULL
)
4126 abd_put(zio
->io_abd
);
4129 typedef struct dbuf_remap_impl_callback_arg
{
4131 uint64_t drica_blk_birth
;
4133 } dbuf_remap_impl_callback_arg_t
;
4136 dbuf_remap_impl_callback(uint64_t vdev
, uint64_t offset
, uint64_t size
,
4139 dbuf_remap_impl_callback_arg_t
*drica
= arg
;
4140 objset_t
*os
= drica
->drica_os
;
4141 spa_t
*spa
= dmu_objset_spa(os
);
4142 dmu_tx_t
*tx
= drica
->drica_tx
;
4144 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa
)));
4146 if (os
== spa_meta_objset(spa
)) {
4147 spa_vdev_indirect_mark_obsolete(spa
, vdev
, offset
, size
, tx
);
4149 dsl_dataset_block_remapped(dmu_objset_ds(os
), vdev
, offset
,
4150 size
, drica
->drica_blk_birth
, tx
);
4155 dbuf_remap_impl(dnode_t
*dn
, blkptr_t
*bp
, dmu_tx_t
*tx
)
4157 blkptr_t bp_copy
= *bp
;
4158 spa_t
*spa
= dmu_objset_spa(dn
->dn_objset
);
4159 dbuf_remap_impl_callback_arg_t drica
;
4161 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa
)));
4163 drica
.drica_os
= dn
->dn_objset
;
4164 drica
.drica_blk_birth
= bp
->blk_birth
;
4165 drica
.drica_tx
= tx
;
4166 if (spa_remap_blkptr(spa
, &bp_copy
, dbuf_remap_impl_callback
,
4169 * The struct_rwlock prevents dbuf_read_impl() from
4170 * dereferencing the BP while we are changing it. To
4171 * avoid lock contention, only grab it when we are actually
4174 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
4176 rw_exit(&dn
->dn_struct_rwlock
);
4181 * Returns true if a dbuf_remap would modify the dbuf. We do this by attempting
4182 * to remap a copy of every bp in the dbuf.
4185 dbuf_can_remap(const dmu_buf_impl_t
*db
)
4187 spa_t
*spa
= dmu_objset_spa(db
->db_objset
);
4188 blkptr_t
*bp
= db
->db
.db_data
;
4189 boolean_t ret
= B_FALSE
;
4191 ASSERT3U(db
->db_level
, >, 0);
4192 ASSERT3S(db
->db_state
, ==, DB_CACHED
);
4194 ASSERT(spa_feature_is_active(spa
, SPA_FEATURE_DEVICE_REMOVAL
));
4196 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
4197 for (int i
= 0; i
< db
->db
.db_size
>> SPA_BLKPTRSHIFT
; i
++) {
4198 blkptr_t bp_copy
= bp
[i
];
4199 if (spa_remap_blkptr(spa
, &bp_copy
, NULL
, NULL
)) {
4204 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
4210 dnode_needs_remap(const dnode_t
*dn
)
4212 spa_t
*spa
= dmu_objset_spa(dn
->dn_objset
);
4213 boolean_t ret
= B_FALSE
;
4215 if (dn
->dn_phys
->dn_nlevels
== 0) {
4219 ASSERT(spa_feature_is_active(spa
, SPA_FEATURE_DEVICE_REMOVAL
));
4221 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
4222 for (int j
= 0; j
< dn
->dn_phys
->dn_nblkptr
; j
++) {
4223 blkptr_t bp_copy
= dn
->dn_phys
->dn_blkptr
[j
];
4224 if (spa_remap_blkptr(spa
, &bp_copy
, NULL
, NULL
)) {
4229 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
4235 * Remap any existing BP's to concrete vdevs, if possible.
4238 dbuf_remap(dnode_t
*dn
, dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
4240 spa_t
*spa
= dmu_objset_spa(db
->db_objset
);
4241 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa
)));
4243 if (!spa_feature_is_active(spa
, SPA_FEATURE_DEVICE_REMOVAL
))
4246 if (db
->db_level
> 0) {
4247 blkptr_t
*bp
= db
->db
.db_data
;
4248 for (int i
= 0; i
< db
->db
.db_size
>> SPA_BLKPTRSHIFT
; i
++) {
4249 dbuf_remap_impl(dn
, &bp
[i
], tx
);
4251 } else if (db
->db
.db_object
== DMU_META_DNODE_OBJECT
) {
4252 dnode_phys_t
*dnp
= db
->db
.db_data
;
4253 ASSERT3U(db
->db_dnode_handle
->dnh_dnode
->dn_type
, ==,
4255 for (int i
= 0; i
< db
->db
.db_size
>> DNODE_SHIFT
;
4256 i
+= dnp
[i
].dn_extra_slots
+ 1) {
4257 for (int j
= 0; j
< dnp
[i
].dn_nblkptr
; j
++) {
4258 dbuf_remap_impl(dn
, &dnp
[i
].dn_blkptr
[j
], tx
);
4265 /* Issue I/O to commit a dirty buffer to disk. */
4267 dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
)
4269 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
4272 dmu_buf_impl_t
*parent
= db
->db_parent
;
4273 uint64_t txg
= tx
->tx_txg
;
4274 zbookmark_phys_t zb
;
4279 ASSERT(dmu_tx_is_syncing(tx
));
4285 if (db
->db_state
!= DB_NOFILL
) {
4286 if (db
->db_level
> 0 || dn
->dn_type
== DMU_OT_DNODE
) {
4288 * Private object buffers are released here rather
4289 * than in dbuf_dirty() since they are only modified
4290 * in the syncing context and we don't want the
4291 * overhead of making multiple copies of the data.
4293 if (BP_IS_HOLE(db
->db_blkptr
)) {
4296 dbuf_release_bp(db
);
4298 dbuf_remap(dn
, db
, tx
);
4302 if (parent
!= dn
->dn_dbuf
) {
4303 /* Our parent is an indirect block. */
4304 /* We have a dirty parent that has been scheduled for write. */
4305 ASSERT(parent
&& parent
->db_data_pending
);
4306 /* Our parent's buffer is one level closer to the dnode. */
4307 ASSERT(db
->db_level
== parent
->db_level
-1);
4309 * We're about to modify our parent's db_data by modifying
4310 * our block pointer, so the parent must be released.
4312 ASSERT(arc_released(parent
->db_buf
));
4313 zio
= parent
->db_data_pending
->dr_zio
;
4315 /* Our parent is the dnode itself. */
4316 ASSERT((db
->db_level
== dn
->dn_phys
->dn_nlevels
-1 &&
4317 db
->db_blkid
!= DMU_SPILL_BLKID
) ||
4318 (db
->db_blkid
== DMU_SPILL_BLKID
&& db
->db_level
== 0));
4319 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
4320 ASSERT3P(db
->db_blkptr
, ==,
4321 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
4325 ASSERT(db
->db_level
== 0 || data
== db
->db_buf
);
4326 ASSERT3U(db
->db_blkptr
->blk_birth
, <=, txg
);
4329 SET_BOOKMARK(&zb
, os
->os_dsl_dataset
?
4330 os
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
4331 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
4333 if (db
->db_blkid
== DMU_SPILL_BLKID
)
4335 wp_flag
|= (db
->db_state
== DB_NOFILL
) ? WP_NOFILL
: 0;
4337 dmu_write_policy(os
, dn
, db
->db_level
, wp_flag
, &zp
);
4341 * We copy the blkptr now (rather than when we instantiate the dirty
4342 * record), because its value can change between open context and
4343 * syncing context. We do not need to hold dn_struct_rwlock to read
4344 * db_blkptr because we are in syncing context.
4346 dr
->dr_bp_copy
= *db
->db_blkptr
;
4348 if (db
->db_level
== 0 &&
4349 dr
->dt
.dl
.dr_override_state
== DR_OVERRIDDEN
) {
4351 * The BP for this block has been provided by open context
4352 * (by dmu_sync() or dmu_buf_write_embedded()).
4354 abd_t
*contents
= (data
!= NULL
) ?
4355 abd_get_from_buf(data
->b_data
, arc_buf_size(data
)) : NULL
;
4357 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
,
4358 &dr
->dr_bp_copy
, contents
, db
->db
.db_size
, db
->db
.db_size
,
4359 &zp
, dbuf_write_override_ready
, NULL
, NULL
,
4360 dbuf_write_override_done
,
4361 dr
, ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
4362 mutex_enter(&db
->db_mtx
);
4363 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
4364 zio_write_override(dr
->dr_zio
, &dr
->dt
.dl
.dr_overridden_by
,
4365 dr
->dt
.dl
.dr_copies
, dr
->dt
.dl
.dr_nopwrite
);
4366 mutex_exit(&db
->db_mtx
);
4367 } else if (db
->db_state
== DB_NOFILL
) {
4368 ASSERT(zp
.zp_checksum
== ZIO_CHECKSUM_OFF
||
4369 zp
.zp_checksum
== ZIO_CHECKSUM_NOPARITY
);
4370 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
,
4371 &dr
->dr_bp_copy
, NULL
, db
->db
.db_size
, db
->db
.db_size
, &zp
,
4372 dbuf_write_nofill_ready
, NULL
, NULL
,
4373 dbuf_write_nofill_done
, db
,
4374 ZIO_PRIORITY_ASYNC_WRITE
,
4375 ZIO_FLAG_MUSTSUCCEED
| ZIO_FLAG_NODATA
, &zb
);
4377 ASSERT(arc_released(data
));
4380 * For indirect blocks, we want to setup the children
4381 * ready callback so that we can properly handle an indirect
4382 * block that only contains holes.
4384 arc_write_done_func_t
*children_ready_cb
= NULL
;
4385 if (db
->db_level
!= 0)
4386 children_ready_cb
= dbuf_write_children_ready
;
4388 dr
->dr_zio
= arc_write(zio
, os
->os_spa
, txg
,
4389 &dr
->dr_bp_copy
, data
, DBUF_IS_L2CACHEABLE(db
),
4390 &zp
, dbuf_write_ready
,
4391 children_ready_cb
, dbuf_write_physdone
,
4392 dbuf_write_done
, db
, ZIO_PRIORITY_ASYNC_WRITE
,
4393 ZIO_FLAG_MUSTSUCCEED
, &zb
);
4397 #if defined(_KERNEL)
4398 EXPORT_SYMBOL(dbuf_find
);
4399 EXPORT_SYMBOL(dbuf_is_metadata
);
4400 EXPORT_SYMBOL(dbuf_destroy
);
4401 EXPORT_SYMBOL(dbuf_loan_arcbuf
);
4402 EXPORT_SYMBOL(dbuf_whichblock
);
4403 EXPORT_SYMBOL(dbuf_read
);
4404 EXPORT_SYMBOL(dbuf_unoverride
);
4405 EXPORT_SYMBOL(dbuf_free_range
);
4406 EXPORT_SYMBOL(dbuf_new_size
);
4407 EXPORT_SYMBOL(dbuf_release_bp
);
4408 EXPORT_SYMBOL(dbuf_dirty
);
4409 EXPORT_SYMBOL(dmu_buf_set_crypt_params
);
4410 EXPORT_SYMBOL(dmu_buf_will_dirty
);
4411 EXPORT_SYMBOL(dmu_buf_will_not_fill
);
4412 EXPORT_SYMBOL(dmu_buf_will_fill
);
4413 EXPORT_SYMBOL(dmu_buf_fill_done
);
4414 EXPORT_SYMBOL(dmu_buf_rele
);
4415 EXPORT_SYMBOL(dbuf_assign_arcbuf
);
4416 EXPORT_SYMBOL(dbuf_prefetch
);
4417 EXPORT_SYMBOL(dbuf_hold_impl
);
4418 EXPORT_SYMBOL(dbuf_hold
);
4419 EXPORT_SYMBOL(dbuf_hold_level
);
4420 EXPORT_SYMBOL(dbuf_create_bonus
);
4421 EXPORT_SYMBOL(dbuf_spill_set_blksz
);
4422 EXPORT_SYMBOL(dbuf_rm_spill
);
4423 EXPORT_SYMBOL(dbuf_add_ref
);
4424 EXPORT_SYMBOL(dbuf_rele
);
4425 EXPORT_SYMBOL(dbuf_rele_and_unlock
);
4426 EXPORT_SYMBOL(dbuf_refcount
);
4427 EXPORT_SYMBOL(dbuf_sync_list
);
4428 EXPORT_SYMBOL(dmu_buf_set_user
);
4429 EXPORT_SYMBOL(dmu_buf_set_user_ie
);
4430 EXPORT_SYMBOL(dmu_buf_get_user
);
4431 EXPORT_SYMBOL(dmu_buf_get_blkptr
);
4434 module_param(dbuf_cache_max_bytes
, ulong
, 0644);
4435 MODULE_PARM_DESC(dbuf_cache_max_bytes
,
4436 "Maximum size in bytes of the dbuf cache.");
4438 module_param(dbuf_cache_hiwater_pct
, uint
, 0644);
4439 MODULE_PARM_DESC(dbuf_cache_hiwater_pct
,
4440 "Percentage over dbuf_cache_max_bytes when dbufs must be evicted "
4443 module_param(dbuf_cache_lowater_pct
, uint
, 0644);
4444 MODULE_PARM_DESC(dbuf_cache_lowater_pct
,
4445 "Percentage below dbuf_cache_max_bytes when the evict thread stops "
4448 module_param(dbuf_cache_shift
, int, 0644);
4449 MODULE_PARM_DESC(dbuf_cache_shift
,
4450 "Set the size of the dbuf cache to a log2 fraction of arc size.");