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) 2013, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
28 * Copyright (c) 2014 Integros [integros.com]
31 #include <sys/zfs_context.h>
33 #include <sys/dmu_send.h>
34 #include <sys/dmu_impl.h>
36 #include <sys/dmu_objset.h>
37 #include <sys/dsl_dataset.h>
38 #include <sys/dsl_dir.h>
39 #include <sys/dmu_tx.h>
42 #include <sys/dmu_zfetch.h>
44 #include <sys/sa_impl.h>
45 #include <sys/zfeature.h>
46 #include <sys/blkptr.h>
47 #include <sys/range_tree.h>
48 #include <sys/callb.h>
51 #include <sys/cityhash.h>
53 uint_t zfs_dbuf_evict_key
;
55 static boolean_t
dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
);
56 static void dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
);
59 extern inline void dmu_buf_init_user(dmu_buf_user_t
*dbu
,
60 dmu_buf_evict_func_t
*evict_func_sync
,
61 dmu_buf_evict_func_t
*evict_func_async
,
62 dmu_buf_t
**clear_on_evict_dbufp
);
66 * Global data structures and functions for the dbuf cache.
68 static kmem_cache_t
*dbuf_kmem_cache
;
69 static taskq_t
*dbu_evict_taskq
;
71 static kthread_t
*dbuf_cache_evict_thread
;
72 static kmutex_t dbuf_evict_lock
;
73 static kcondvar_t dbuf_evict_cv
;
74 static boolean_t dbuf_evict_thread_exit
;
77 * LRU cache of dbufs. The dbuf cache maintains a list of dbufs that
78 * are not currently held but have been recently released. These dbufs
79 * are not eligible for arc eviction until they are aged out of the cache.
80 * Dbufs are added to the dbuf cache once the last hold is released. If a
81 * dbuf is later accessed and still exists in the dbuf cache, then it will
82 * be removed from the cache and later re-added to the head of the cache.
83 * Dbufs that are aged out of the cache will be immediately destroyed and
84 * become eligible for arc eviction.
86 static multilist_t
*dbuf_cache
;
87 static refcount_t dbuf_cache_size
;
88 uint64_t dbuf_cache_max_bytes
= 0;
90 /* Set the default size of the dbuf cache to log2 fraction of arc size. */
91 int dbuf_cache_shift
= 5;
94 * The dbuf cache uses a three-stage eviction policy:
95 * - A low water marker designates when the dbuf eviction thread
96 * should stop evicting from the dbuf cache.
97 * - When we reach the maximum size (aka mid water mark), we
98 * signal the eviction thread to run.
99 * - The high water mark indicates when the eviction thread
100 * is unable to keep up with the incoming load and eviction must
101 * happen in the context of the calling thread.
105 * low water mid water hi water
106 * +----------------------------------------+----------+----------+
111 * +----------------------------------------+----------+----------+
113 * evicting eviction directly
116 * The high and low water marks indicate the operating range for the eviction
117 * thread. The low water mark is, by default, 90% of the total size of the
118 * cache and the high water mark is at 110% (both of these percentages can be
119 * changed by setting dbuf_cache_lowater_pct and dbuf_cache_hiwater_pct,
120 * respectively). The eviction thread will try to ensure that the cache remains
121 * within this range by waking up every second and checking if the cache is
122 * above the low water mark. The thread can also be woken up by callers adding
123 * elements into the cache if the cache is larger than the mid water (i.e max
124 * cache size). Once the eviction thread is woken up and eviction is required,
125 * it will continue evicting buffers until it's able to reduce the cache size
126 * to the low water mark. If the cache size continues to grow and hits the high
127 * water mark, then callers adding elments to the cache will begin to evict
128 * directly from the cache until the cache is no longer above the high water
133 * The percentage above and below the maximum cache size.
135 uint_t dbuf_cache_hiwater_pct
= 10;
136 uint_t dbuf_cache_lowater_pct
= 10;
140 dbuf_cons(void *vdb
, void *unused
, int kmflag
)
142 dmu_buf_impl_t
*db
= vdb
;
143 bzero(db
, sizeof (dmu_buf_impl_t
));
145 mutex_init(&db
->db_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
146 cv_init(&db
->db_changed
, NULL
, CV_DEFAULT
, NULL
);
147 multilist_link_init(&db
->db_cache_link
);
148 refcount_create(&db
->db_holds
);
155 dbuf_dest(void *vdb
, void *unused
)
157 dmu_buf_impl_t
*db
= vdb
;
158 mutex_destroy(&db
->db_mtx
);
159 cv_destroy(&db
->db_changed
);
160 ASSERT(!multilist_link_active(&db
->db_cache_link
));
161 refcount_destroy(&db
->db_holds
);
165 * dbuf hash table routines
167 static dbuf_hash_table_t dbuf_hash_table
;
169 static uint64_t dbuf_hash_count
;
172 * We use Cityhash for this. It's fast, and has good hash properties without
173 * requiring any large static buffers.
176 dbuf_hash(void *os
, uint64_t obj
, uint8_t lvl
, uint64_t blkid
)
178 return (cityhash4((uintptr_t)os
, obj
, (uint64_t)lvl
, blkid
));
181 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
182 ((dbuf)->db.db_object == (obj) && \
183 (dbuf)->db_objset == (os) && \
184 (dbuf)->db_level == (level) && \
185 (dbuf)->db_blkid == (blkid))
188 dbuf_find(objset_t
*os
, uint64_t obj
, uint8_t level
, uint64_t blkid
)
190 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
191 uint64_t hv
= dbuf_hash(os
, obj
, level
, blkid
);
192 uint64_t idx
= hv
& h
->hash_table_mask
;
195 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
196 for (db
= h
->hash_table
[idx
]; db
!= NULL
; db
= db
->db_hash_next
) {
197 if (DBUF_EQUAL(db
, os
, obj
, level
, blkid
)) {
198 mutex_enter(&db
->db_mtx
);
199 if (db
->db_state
!= DB_EVICTING
) {
200 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
203 mutex_exit(&db
->db_mtx
);
206 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
210 static dmu_buf_impl_t
*
211 dbuf_find_bonus(objset_t
*os
, uint64_t object
)
214 dmu_buf_impl_t
*db
= NULL
;
216 if (dnode_hold(os
, object
, FTAG
, &dn
) == 0) {
217 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
218 if (dn
->dn_bonus
!= NULL
) {
220 mutex_enter(&db
->db_mtx
);
222 rw_exit(&dn
->dn_struct_rwlock
);
223 dnode_rele(dn
, FTAG
);
229 * Insert an entry into the hash table. If there is already an element
230 * equal to elem in the hash table, then the already existing element
231 * will be returned and the new element will not be inserted.
232 * Otherwise returns NULL.
234 static dmu_buf_impl_t
*
235 dbuf_hash_insert(dmu_buf_impl_t
*db
)
237 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
238 objset_t
*os
= db
->db_objset
;
239 uint64_t obj
= db
->db
.db_object
;
240 int level
= db
->db_level
;
241 uint64_t blkid
= db
->db_blkid
;
242 uint64_t hv
= dbuf_hash(os
, obj
, level
, blkid
);
243 uint64_t idx
= hv
& h
->hash_table_mask
;
246 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
247 for (dbf
= h
->hash_table
[idx
]; dbf
!= NULL
; dbf
= dbf
->db_hash_next
) {
248 if (DBUF_EQUAL(dbf
, os
, obj
, level
, blkid
)) {
249 mutex_enter(&dbf
->db_mtx
);
250 if (dbf
->db_state
!= DB_EVICTING
) {
251 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
254 mutex_exit(&dbf
->db_mtx
);
258 mutex_enter(&db
->db_mtx
);
259 db
->db_hash_next
= h
->hash_table
[idx
];
260 h
->hash_table
[idx
] = db
;
261 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
262 atomic_inc_64(&dbuf_hash_count
);
268 * Remove an entry from the hash table. It must be in the EVICTING state.
271 dbuf_hash_remove(dmu_buf_impl_t
*db
)
273 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
274 uint64_t hv
= dbuf_hash(db
->db_objset
, db
->db
.db_object
,
275 db
->db_level
, db
->db_blkid
);
276 uint64_t idx
= hv
& h
->hash_table_mask
;
277 dmu_buf_impl_t
*dbf
, **dbp
;
280 * We musn't hold db_mtx to maintain lock ordering:
281 * DBUF_HASH_MUTEX > db_mtx.
283 ASSERT(refcount_is_zero(&db
->db_holds
));
284 ASSERT(db
->db_state
== DB_EVICTING
);
285 ASSERT(!MUTEX_HELD(&db
->db_mtx
));
287 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
288 dbp
= &h
->hash_table
[idx
];
289 while ((dbf
= *dbp
) != db
) {
290 dbp
= &dbf
->db_hash_next
;
293 *dbp
= db
->db_hash_next
;
294 db
->db_hash_next
= NULL
;
295 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
296 atomic_dec_64(&dbuf_hash_count
);
302 } dbvu_verify_type_t
;
305 dbuf_verify_user(dmu_buf_impl_t
*db
, dbvu_verify_type_t verify_type
)
310 if (db
->db_user
== NULL
)
313 /* Only data blocks support the attachment of user data. */
314 ASSERT(db
->db_level
== 0);
316 /* Clients must resolve a dbuf before attaching user data. */
317 ASSERT(db
->db
.db_data
!= NULL
);
318 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
320 holds
= refcount_count(&db
->db_holds
);
321 if (verify_type
== DBVU_EVICTING
) {
323 * Immediate eviction occurs when holds == dirtycnt.
324 * For normal eviction buffers, holds is zero on
325 * eviction, except when dbuf_fix_old_data() calls
326 * dbuf_clear_data(). However, the hold count can grow
327 * during eviction even though db_mtx is held (see
328 * dmu_bonus_hold() for an example), so we can only
329 * test the generic invariant that holds >= dirtycnt.
331 ASSERT3U(holds
, >=, db
->db_dirtycnt
);
333 if (db
->db_user_immediate_evict
== TRUE
)
334 ASSERT3U(holds
, >=, db
->db_dirtycnt
);
336 ASSERT3U(holds
, >, 0);
342 dbuf_evict_user(dmu_buf_impl_t
*db
)
344 dmu_buf_user_t
*dbu
= db
->db_user
;
346 ASSERT(MUTEX_HELD(&db
->db_mtx
));
351 dbuf_verify_user(db
, DBVU_EVICTING
);
355 if (dbu
->dbu_clear_on_evict_dbufp
!= NULL
)
356 *dbu
->dbu_clear_on_evict_dbufp
= NULL
;
360 * There are two eviction callbacks - one that we call synchronously
361 * and one that we invoke via a taskq. The async one is useful for
362 * avoiding lock order reversals and limiting stack depth.
364 * Note that if we have a sync callback but no async callback,
365 * it's likely that the sync callback will free the structure
366 * containing the dbu. In that case we need to take care to not
367 * dereference dbu after calling the sync evict func.
369 boolean_t has_async
= (dbu
->dbu_evict_func_async
!= NULL
);
371 if (dbu
->dbu_evict_func_sync
!= NULL
)
372 dbu
->dbu_evict_func_sync(dbu
);
375 taskq_dispatch_ent(dbu_evict_taskq
, dbu
->dbu_evict_func_async
,
376 dbu
, 0, &dbu
->dbu_tqent
);
381 dbuf_is_metadata(dmu_buf_impl_t
*db
)
383 if (db
->db_level
> 0) {
386 boolean_t is_metadata
;
389 is_metadata
= DMU_OT_IS_METADATA(DB_DNODE(db
)->dn_type
);
392 return (is_metadata
);
397 * This function *must* return indices evenly distributed between all
398 * sublists of the multilist. This is needed due to how the dbuf eviction
399 * code is laid out; dbuf_evict_thread() assumes dbufs are evenly
400 * distributed between all sublists and uses this assumption when
401 * deciding which sublist to evict from and how much to evict from it.
404 dbuf_cache_multilist_index_func(multilist_t
*ml
, void *obj
)
406 dmu_buf_impl_t
*db
= obj
;
409 * The assumption here, is the hash value for a given
410 * dmu_buf_impl_t will remain constant throughout it's lifetime
411 * (i.e. it's objset, object, level and blkid fields don't change).
412 * Thus, we don't need to store the dbuf's sublist index
413 * on insertion, as this index can be recalculated on removal.
415 * Also, the low order bits of the hash value are thought to be
416 * distributed evenly. Otherwise, in the case that the multilist
417 * has a power of two number of sublists, each sublists' usage
418 * would not be evenly distributed.
420 return (dbuf_hash(db
->db_objset
, db
->db
.db_object
,
421 db
->db_level
, db
->db_blkid
) %
422 multilist_get_num_sublists(ml
));
425 static inline boolean_t
426 dbuf_cache_above_hiwater(void)
428 uint64_t dbuf_cache_hiwater_bytes
=
429 (dbuf_cache_max_bytes
* dbuf_cache_hiwater_pct
) / 100;
431 return (refcount_count(&dbuf_cache_size
) >
432 dbuf_cache_max_bytes
+ dbuf_cache_hiwater_bytes
);
435 static inline boolean_t
436 dbuf_cache_above_lowater(void)
438 uint64_t dbuf_cache_lowater_bytes
=
439 (dbuf_cache_max_bytes
* dbuf_cache_lowater_pct
) / 100;
441 return (refcount_count(&dbuf_cache_size
) >
442 dbuf_cache_max_bytes
- dbuf_cache_lowater_bytes
);
446 * Evict the oldest eligible dbuf from the dbuf cache.
451 int idx
= multilist_get_random_index(dbuf_cache
);
452 multilist_sublist_t
*mls
= multilist_sublist_lock(dbuf_cache
, idx
);
454 ASSERT(!MUTEX_HELD(&dbuf_evict_lock
));
457 * Set the thread's tsd to indicate that it's processing evictions.
458 * Once a thread stops evicting from the dbuf cache it will
459 * reset its tsd to NULL.
461 ASSERT3P(tsd_get(zfs_dbuf_evict_key
), ==, NULL
);
462 (void) tsd_set(zfs_dbuf_evict_key
, (void *)B_TRUE
);
464 dmu_buf_impl_t
*db
= multilist_sublist_tail(mls
);
465 while (db
!= NULL
&& mutex_tryenter(&db
->db_mtx
) == 0) {
466 db
= multilist_sublist_prev(mls
, db
);
469 DTRACE_PROBE2(dbuf__evict__one
, dmu_buf_impl_t
*, db
,
470 multilist_sublist_t
*, mls
);
473 multilist_sublist_remove(mls
, db
);
474 multilist_sublist_unlock(mls
);
475 (void) refcount_remove_many(&dbuf_cache_size
,
479 multilist_sublist_unlock(mls
);
481 (void) tsd_set(zfs_dbuf_evict_key
, NULL
);
485 * The dbuf evict thread is responsible for aging out dbufs from the
486 * cache. Once the cache has reached it's maximum size, dbufs are removed
487 * and destroyed. The eviction thread will continue running until the size
488 * of the dbuf cache is at or below the maximum size. Once the dbuf is aged
489 * out of the cache it is destroyed and becomes eligible for arc eviction.
493 dbuf_evict_thread(void *unused
)
497 CALLB_CPR_INIT(&cpr
, &dbuf_evict_lock
, callb_generic_cpr
, FTAG
);
499 mutex_enter(&dbuf_evict_lock
);
500 while (!dbuf_evict_thread_exit
) {
501 while (!dbuf_cache_above_lowater() && !dbuf_evict_thread_exit
) {
502 CALLB_CPR_SAFE_BEGIN(&cpr
);
503 (void) cv_timedwait_hires(&dbuf_evict_cv
,
504 &dbuf_evict_lock
, SEC2NSEC(1), MSEC2NSEC(1), 0);
505 CALLB_CPR_SAFE_END(&cpr
, &dbuf_evict_lock
);
507 mutex_exit(&dbuf_evict_lock
);
510 * Keep evicting as long as we're above the low water mark
511 * for the cache. We do this without holding the locks to
512 * minimize lock contention.
514 while (dbuf_cache_above_lowater() && !dbuf_evict_thread_exit
) {
518 mutex_enter(&dbuf_evict_lock
);
521 dbuf_evict_thread_exit
= B_FALSE
;
522 cv_broadcast(&dbuf_evict_cv
);
523 CALLB_CPR_EXIT(&cpr
); /* drops dbuf_evict_lock */
528 * Wake up the dbuf eviction thread if the dbuf cache is at its max size.
529 * If the dbuf cache is at its high water mark, then evict a dbuf from the
530 * dbuf cache using the callers context.
533 dbuf_evict_notify(void)
537 * We use thread specific data to track when a thread has
538 * started processing evictions. This allows us to avoid deeply
539 * nested stacks that would have a call flow similar to this:
541 * dbuf_rele()-->dbuf_rele_and_unlock()-->dbuf_evict_notify()
544 * +-----dbuf_destroy()<--dbuf_evict_one()<--------+
546 * The dbuf_eviction_thread will always have its tsd set until
547 * that thread exits. All other threads will only set their tsd
548 * if they are participating in the eviction process. This only
549 * happens if the eviction thread is unable to process evictions
550 * fast enough. To keep the dbuf cache size in check, other threads
551 * can evict from the dbuf cache directly. Those threads will set
552 * their tsd values so that we ensure that they only evict one dbuf
553 * from the dbuf cache.
555 if (tsd_get(zfs_dbuf_evict_key
) != NULL
)
559 * We check if we should evict without holding the dbuf_evict_lock,
560 * because it's OK to occasionally make the wrong decision here,
561 * and grabbing the lock results in massive lock contention.
563 if (refcount_count(&dbuf_cache_size
) > dbuf_cache_max_bytes
) {
564 if (dbuf_cache_above_hiwater())
566 cv_signal(&dbuf_evict_cv
);
573 uint64_t hsize
= 1ULL << 16;
574 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
578 * The hash table is big enough to fill all of physical memory
579 * with an average 4K block size. The table will take up
580 * totalmem*sizeof(void*)/4K (i.e. 2MB/GB with 8-byte pointers).
582 while (hsize
* 4096 < physmem
* PAGESIZE
)
586 h
->hash_table_mask
= hsize
- 1;
587 h
->hash_table
= kmem_zalloc(hsize
* sizeof (void *), KM_NOSLEEP
);
588 if (h
->hash_table
== NULL
) {
589 /* XXX - we should really return an error instead of assert */
590 ASSERT(hsize
> (1ULL << 10));
595 dbuf_kmem_cache
= kmem_cache_create("dmu_buf_impl_t",
596 sizeof (dmu_buf_impl_t
),
597 0, dbuf_cons
, dbuf_dest
, NULL
, NULL
, NULL
, 0);
599 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
600 mutex_init(&h
->hash_mutexes
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
603 * Setup the parameters for the dbuf cache. We set the size of the
604 * dbuf cache to 1/32nd (default) of the size of the ARC. If the value
605 * has been set in /etc/system and it's not greater than the size of
606 * the ARC, then we honor that value.
608 if (dbuf_cache_max_bytes
== 0 ||
609 dbuf_cache_max_bytes
>= arc_max_bytes()) {
610 dbuf_cache_max_bytes
= arc_max_bytes() >> dbuf_cache_shift
;
614 * All entries are queued via taskq_dispatch_ent(), so min/maxalloc
615 * configuration is not required.
617 dbu_evict_taskq
= taskq_create("dbu_evict", 1, minclsyspri
, 0, 0, 0);
619 dbuf_cache
= multilist_create(sizeof (dmu_buf_impl_t
),
620 offsetof(dmu_buf_impl_t
, db_cache_link
),
621 dbuf_cache_multilist_index_func
);
622 refcount_create(&dbuf_cache_size
);
624 tsd_create(&zfs_dbuf_evict_key
, NULL
);
625 dbuf_evict_thread_exit
= B_FALSE
;
626 mutex_init(&dbuf_evict_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
627 cv_init(&dbuf_evict_cv
, NULL
, CV_DEFAULT
, NULL
);
628 dbuf_cache_evict_thread
= thread_create(NULL
, 0, dbuf_evict_thread
,
629 NULL
, 0, &p0
, TS_RUN
, minclsyspri
);
635 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
638 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
639 mutex_destroy(&h
->hash_mutexes
[i
]);
640 kmem_free(h
->hash_table
, (h
->hash_table_mask
+ 1) * sizeof (void *));
641 kmem_cache_destroy(dbuf_kmem_cache
);
642 taskq_destroy(dbu_evict_taskq
);
644 mutex_enter(&dbuf_evict_lock
);
645 dbuf_evict_thread_exit
= B_TRUE
;
646 while (dbuf_evict_thread_exit
) {
647 cv_signal(&dbuf_evict_cv
);
648 cv_wait(&dbuf_evict_cv
, &dbuf_evict_lock
);
650 mutex_exit(&dbuf_evict_lock
);
651 tsd_destroy(&zfs_dbuf_evict_key
);
653 mutex_destroy(&dbuf_evict_lock
);
654 cv_destroy(&dbuf_evict_cv
);
656 refcount_destroy(&dbuf_cache_size
);
657 multilist_destroy(dbuf_cache
);
666 dbuf_verify(dmu_buf_impl_t
*db
)
669 dbuf_dirty_record_t
*dr
;
671 ASSERT(MUTEX_HELD(&db
->db_mtx
));
673 if (!(zfs_flags
& ZFS_DEBUG_DBUF_VERIFY
))
676 ASSERT(db
->db_objset
!= NULL
);
680 ASSERT(db
->db_parent
== NULL
);
681 ASSERT(db
->db_blkptr
== NULL
);
683 ASSERT3U(db
->db
.db_object
, ==, dn
->dn_object
);
684 ASSERT3P(db
->db_objset
, ==, dn
->dn_objset
);
685 ASSERT3U(db
->db_level
, <, dn
->dn_nlevels
);
686 ASSERT(db
->db_blkid
== DMU_BONUS_BLKID
||
687 db
->db_blkid
== DMU_SPILL_BLKID
||
688 !avl_is_empty(&dn
->dn_dbufs
));
690 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
692 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
693 ASSERT3U(db
->db
.db_offset
, ==, DMU_BONUS_BLKID
);
694 } else if (db
->db_blkid
== DMU_SPILL_BLKID
) {
696 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
697 ASSERT0(db
->db
.db_offset
);
699 ASSERT3U(db
->db
.db_offset
, ==, db
->db_blkid
* db
->db
.db_size
);
702 for (dr
= db
->db_data_pending
; dr
!= NULL
; dr
= dr
->dr_next
)
703 ASSERT(dr
->dr_dbuf
== db
);
705 for (dr
= db
->db_last_dirty
; dr
!= NULL
; dr
= dr
->dr_next
)
706 ASSERT(dr
->dr_dbuf
== db
);
709 * We can't assert that db_size matches dn_datablksz because it
710 * can be momentarily different when another thread is doing
713 if (db
->db_level
== 0 && db
->db
.db_object
== DMU_META_DNODE_OBJECT
) {
714 dr
= db
->db_data_pending
;
716 * It should only be modified in syncing context, so
717 * make sure we only have one copy of the data.
719 ASSERT(dr
== NULL
|| dr
->dt
.dl
.dr_data
== db
->db_buf
);
722 /* verify db->db_blkptr */
724 if (db
->db_parent
== dn
->dn_dbuf
) {
725 /* db is pointed to by the dnode */
726 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
727 if (DMU_OBJECT_IS_SPECIAL(db
->db
.db_object
))
728 ASSERT(db
->db_parent
== NULL
);
730 ASSERT(db
->db_parent
!= NULL
);
731 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
732 ASSERT3P(db
->db_blkptr
, ==,
733 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
735 /* db is pointed to by an indirect block */
736 int epb
= db
->db_parent
->db
.db_size
>> SPA_BLKPTRSHIFT
;
737 ASSERT3U(db
->db_parent
->db_level
, ==, db
->db_level
+1);
738 ASSERT3U(db
->db_parent
->db
.db_object
, ==,
741 * dnode_grow_indblksz() can make this fail if we don't
742 * have the struct_rwlock. XXX indblksz no longer
743 * grows. safe to do this now?
745 if (RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
746 ASSERT3P(db
->db_blkptr
, ==,
747 ((blkptr_t
*)db
->db_parent
->db
.db_data
+
748 db
->db_blkid
% epb
));
752 if ((db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
)) &&
753 (db
->db_buf
== NULL
|| db
->db_buf
->b_data
) &&
754 db
->db
.db_data
&& db
->db_blkid
!= DMU_BONUS_BLKID
&&
755 db
->db_state
!= DB_FILL
&& !dn
->dn_free_txg
) {
757 * If the blkptr isn't set but they have nonzero data,
758 * it had better be dirty, otherwise we'll lose that
759 * data when we evict this buffer.
761 * There is an exception to this rule for indirect blocks; in
762 * this case, if the indirect block is a hole, we fill in a few
763 * fields on each of the child blocks (importantly, birth time)
764 * to prevent hole birth times from being lost when you
765 * partially fill in a hole.
767 if (db
->db_dirtycnt
== 0) {
768 if (db
->db_level
== 0) {
769 uint64_t *buf
= db
->db
.db_data
;
772 for (i
= 0; i
< db
->db
.db_size
>> 3; i
++) {
776 blkptr_t
*bps
= db
->db
.db_data
;
777 ASSERT3U(1 << DB_DNODE(db
)->dn_indblkshift
, ==,
780 * We want to verify that all the blkptrs in the
781 * indirect block are holes, but we may have
782 * automatically set up a few fields for them.
783 * We iterate through each blkptr and verify
784 * they only have those fields set.
787 i
< db
->db
.db_size
/ sizeof (blkptr_t
);
789 blkptr_t
*bp
= &bps
[i
];
790 ASSERT(ZIO_CHECKSUM_IS_ZERO(
793 DVA_IS_EMPTY(&bp
->blk_dva
[0]) &&
794 DVA_IS_EMPTY(&bp
->blk_dva
[1]) &&
795 DVA_IS_EMPTY(&bp
->blk_dva
[2]));
796 ASSERT0(bp
->blk_fill
);
797 ASSERT0(bp
->blk_pad
[0]);
798 ASSERT0(bp
->blk_pad
[1]);
799 ASSERT(!BP_IS_EMBEDDED(bp
));
800 ASSERT(BP_IS_HOLE(bp
));
801 ASSERT0(bp
->blk_phys_birth
);
811 dbuf_clear_data(dmu_buf_impl_t
*db
)
813 ASSERT(MUTEX_HELD(&db
->db_mtx
));
815 ASSERT3P(db
->db_buf
, ==, NULL
);
816 db
->db
.db_data
= NULL
;
817 if (db
->db_state
!= DB_NOFILL
)
818 db
->db_state
= DB_UNCACHED
;
822 dbuf_set_data(dmu_buf_impl_t
*db
, arc_buf_t
*buf
)
824 ASSERT(MUTEX_HELD(&db
->db_mtx
));
828 ASSERT(buf
->b_data
!= NULL
);
829 db
->db
.db_data
= buf
->b_data
;
833 * Loan out an arc_buf for read. Return the loaned arc_buf.
836 dbuf_loan_arcbuf(dmu_buf_impl_t
*db
)
840 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
841 mutex_enter(&db
->db_mtx
);
842 if (arc_released(db
->db_buf
) || refcount_count(&db
->db_holds
) > 1) {
843 int blksz
= db
->db
.db_size
;
844 spa_t
*spa
= db
->db_objset
->os_spa
;
846 mutex_exit(&db
->db_mtx
);
847 abuf
= arc_loan_buf(spa
, B_FALSE
, blksz
);
848 bcopy(db
->db
.db_data
, abuf
->b_data
, blksz
);
851 arc_loan_inuse_buf(abuf
, db
);
854 mutex_exit(&db
->db_mtx
);
860 * Calculate which level n block references the data at the level 0 offset
864 dbuf_whichblock(dnode_t
*dn
, int64_t level
, uint64_t offset
)
866 if (dn
->dn_datablkshift
!= 0 && dn
->dn_indblkshift
!= 0) {
868 * The level n blkid is equal to the level 0 blkid divided by
869 * the number of level 0s in a level n block.
871 * The level 0 blkid is offset >> datablkshift =
872 * offset / 2^datablkshift.
874 * The number of level 0s in a level n is the number of block
875 * pointers in an indirect block, raised to the power of level.
876 * This is 2^(indblkshift - SPA_BLKPTRSHIFT)^level =
877 * 2^(level*(indblkshift - SPA_BLKPTRSHIFT)).
879 * Thus, the level n blkid is: offset /
880 * ((2^datablkshift)*(2^(level*(indblkshift - SPA_BLKPTRSHIFT)))
881 * = offset / 2^(datablkshift + level *
882 * (indblkshift - SPA_BLKPTRSHIFT))
883 * = offset >> (datablkshift + level *
884 * (indblkshift - SPA_BLKPTRSHIFT))
886 return (offset
>> (dn
->dn_datablkshift
+ level
*
887 (dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
)));
889 ASSERT3U(offset
, <, dn
->dn_datablksz
);
895 dbuf_read_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
897 dmu_buf_impl_t
*db
= vdb
;
899 mutex_enter(&db
->db_mtx
);
900 ASSERT3U(db
->db_state
, ==, DB_READ
);
902 * All reads are synchronous, so we must have a hold on the dbuf
904 ASSERT(refcount_count(&db
->db_holds
) > 0);
905 ASSERT(db
->db_buf
== NULL
);
906 ASSERT(db
->db
.db_data
== NULL
);
907 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
908 /* we were freed in flight; disregard any error */
909 arc_release(buf
, db
);
910 bzero(buf
->b_data
, db
->db
.db_size
);
912 db
->db_freed_in_flight
= FALSE
;
913 dbuf_set_data(db
, buf
);
914 db
->db_state
= DB_CACHED
;
915 } else if (zio
== NULL
|| zio
->io_error
== 0) {
916 dbuf_set_data(db
, buf
);
917 db
->db_state
= DB_CACHED
;
919 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
920 ASSERT3P(db
->db_buf
, ==, NULL
);
921 arc_buf_destroy(buf
, db
);
922 db
->db_state
= DB_UNCACHED
;
924 cv_broadcast(&db
->db_changed
);
925 dbuf_rele_and_unlock(db
, NULL
);
929 dbuf_read_impl(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t flags
)
933 arc_flags_t aflags
= ARC_FLAG_NOWAIT
;
937 ASSERT(!refcount_is_zero(&db
->db_holds
));
938 /* We need the struct_rwlock to prevent db_blkptr from changing. */
939 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
940 ASSERT(MUTEX_HELD(&db
->db_mtx
));
941 ASSERT(db
->db_state
== DB_UNCACHED
);
942 ASSERT(db
->db_buf
== NULL
);
944 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
945 int bonuslen
= MIN(dn
->dn_bonuslen
, dn
->dn_phys
->dn_bonuslen
);
947 ASSERT3U(bonuslen
, <=, db
->db
.db_size
);
948 db
->db
.db_data
= zio_buf_alloc(DN_MAX_BONUSLEN
);
949 arc_space_consume(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
950 if (bonuslen
< DN_MAX_BONUSLEN
)
951 bzero(db
->db
.db_data
, DN_MAX_BONUSLEN
);
953 bcopy(DN_BONUS(dn
->dn_phys
), db
->db
.db_data
, bonuslen
);
955 db
->db_state
= DB_CACHED
;
956 mutex_exit(&db
->db_mtx
);
961 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
962 * processes the delete record and clears the bp while we are waiting
963 * for the dn_mtx (resulting in a "no" from block_freed).
965 if (db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
) ||
966 (db
->db_level
== 0 && (dnode_block_freed(dn
, db
->db_blkid
) ||
967 BP_IS_HOLE(db
->db_blkptr
)))) {
968 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
970 dbuf_set_data(db
, arc_alloc_buf(db
->db_objset
->os_spa
, db
, type
,
972 bzero(db
->db
.db_data
, db
->db
.db_size
);
974 if (db
->db_blkptr
!= NULL
&& db
->db_level
> 0 &&
975 BP_IS_HOLE(db
->db_blkptr
) &&
976 db
->db_blkptr
->blk_birth
!= 0) {
977 blkptr_t
*bps
= db
->db
.db_data
;
978 for (int i
= 0; i
< ((1 <<
979 DB_DNODE(db
)->dn_indblkshift
) / sizeof (blkptr_t
));
981 blkptr_t
*bp
= &bps
[i
];
982 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==,
983 1 << dn
->dn_indblkshift
);
985 BP_GET_LEVEL(db
->db_blkptr
) == 1 ?
987 BP_GET_LSIZE(db
->db_blkptr
));
988 BP_SET_TYPE(bp
, BP_GET_TYPE(db
->db_blkptr
));
990 BP_GET_LEVEL(db
->db_blkptr
) - 1);
991 BP_SET_BIRTH(bp
, db
->db_blkptr
->blk_birth
, 0);
995 db
->db_state
= DB_CACHED
;
996 mutex_exit(&db
->db_mtx
);
1002 db
->db_state
= DB_READ
;
1003 mutex_exit(&db
->db_mtx
);
1005 if (DBUF_IS_L2CACHEABLE(db
))
1006 aflags
|= ARC_FLAG_L2CACHE
;
1008 SET_BOOKMARK(&zb
, db
->db_objset
->os_dsl_dataset
?
1009 db
->db_objset
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
1010 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
1012 dbuf_add_ref(db
, NULL
);
1014 (void) arc_read(zio
, db
->db_objset
->os_spa
, db
->db_blkptr
,
1015 dbuf_read_done
, db
, ZIO_PRIORITY_SYNC_READ
,
1016 (flags
& DB_RF_CANFAIL
) ? ZIO_FLAG_CANFAIL
: ZIO_FLAG_MUSTSUCCEED
,
1021 * This is our just-in-time copy function. It makes a copy of buffers that
1022 * have been modified in a previous transaction group before we access them in
1023 * the current active group.
1025 * This function is used in three places: when we are dirtying a buffer for the
1026 * first time in a txg, when we are freeing a range in a dnode that includes
1027 * this buffer, and when we are accessing a buffer which was received compressed
1028 * and later referenced in a WRITE_BYREF record.
1030 * Note that when we are called from dbuf_free_range() we do not put a hold on
1031 * the buffer, we just traverse the active dbuf list for the dnode.
1034 dbuf_fix_old_data(dmu_buf_impl_t
*db
, uint64_t txg
)
1036 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
1038 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1039 ASSERT(db
->db
.db_data
!= NULL
);
1040 ASSERT(db
->db_level
== 0);
1041 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
);
1044 (dr
->dt
.dl
.dr_data
!=
1045 ((db
->db_blkid
== DMU_BONUS_BLKID
) ? db
->db
.db_data
: db
->db_buf
)))
1049 * If the last dirty record for this dbuf has not yet synced
1050 * and its referencing the dbuf data, either:
1051 * reset the reference to point to a new copy,
1052 * or (if there a no active holders)
1053 * just null out the current db_data pointer.
1055 ASSERT(dr
->dr_txg
>= txg
- 2);
1056 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1057 /* Note that the data bufs here are zio_bufs */
1058 dr
->dt
.dl
.dr_data
= zio_buf_alloc(DN_MAX_BONUSLEN
);
1059 arc_space_consume(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
1060 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
, DN_MAX_BONUSLEN
);
1061 } else if (refcount_count(&db
->db_holds
) > db
->db_dirtycnt
) {
1062 int size
= arc_buf_size(db
->db_buf
);
1063 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1064 spa_t
*spa
= db
->db_objset
->os_spa
;
1065 enum zio_compress compress_type
=
1066 arc_get_compression(db
->db_buf
);
1068 if (compress_type
== ZIO_COMPRESS_OFF
) {
1069 dr
->dt
.dl
.dr_data
= arc_alloc_buf(spa
, db
, type
, size
);
1071 ASSERT3U(type
, ==, ARC_BUFC_DATA
);
1072 dr
->dt
.dl
.dr_data
= arc_alloc_compressed_buf(spa
, db
,
1073 size
, arc_buf_lsize(db
->db_buf
), compress_type
);
1075 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
->b_data
, size
);
1078 dbuf_clear_data(db
);
1083 dbuf_read(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t flags
)
1090 * We don't have to hold the mutex to check db_state because it
1091 * can't be freed while we have a hold on the buffer.
1093 ASSERT(!refcount_is_zero(&db
->db_holds
));
1095 if (db
->db_state
== DB_NOFILL
)
1096 return (SET_ERROR(EIO
));
1100 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1101 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
1103 prefetch
= db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1104 (flags
& DB_RF_NOPREFETCH
) == 0 && dn
!= NULL
&&
1105 DBUF_IS_CACHEABLE(db
);
1107 mutex_enter(&db
->db_mtx
);
1108 if (db
->db_state
== DB_CACHED
) {
1110 * If the arc buf is compressed, we need to decompress it to
1111 * read the data. This could happen during the "zfs receive" of
1112 * a stream which is compressed and deduplicated.
1114 if (db
->db_buf
!= NULL
&&
1115 arc_get_compression(db
->db_buf
) != ZIO_COMPRESS_OFF
) {
1116 dbuf_fix_old_data(db
,
1117 spa_syncing_txg(dmu_objset_spa(db
->db_objset
)));
1118 err
= arc_decompress(db
->db_buf
);
1119 dbuf_set_data(db
, db
->db_buf
);
1121 mutex_exit(&db
->db_mtx
);
1123 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
);
1124 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1125 rw_exit(&dn
->dn_struct_rwlock
);
1127 } else if (db
->db_state
== DB_UNCACHED
) {
1128 spa_t
*spa
= dn
->dn_objset
->os_spa
;
1129 boolean_t need_wait
= B_FALSE
;
1132 db
->db_blkptr
!= NULL
&& !BP_IS_HOLE(db
->db_blkptr
)) {
1133 zio
= zio_root(spa
, NULL
, NULL
, ZIO_FLAG_CANFAIL
);
1136 dbuf_read_impl(db
, zio
, flags
);
1138 /* dbuf_read_impl has dropped db_mtx for us */
1141 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
);
1143 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1144 rw_exit(&dn
->dn_struct_rwlock
);
1148 err
= zio_wait(zio
);
1151 * Another reader came in while the dbuf was in flight
1152 * between UNCACHED and CACHED. Either a writer will finish
1153 * writing the buffer (sending the dbuf to CACHED) or the
1154 * first reader's request will reach the read_done callback
1155 * and send the dbuf to CACHED. Otherwise, a failure
1156 * occurred and the dbuf went to UNCACHED.
1158 mutex_exit(&db
->db_mtx
);
1160 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
);
1161 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1162 rw_exit(&dn
->dn_struct_rwlock
);
1165 /* Skip the wait per the caller's request. */
1166 mutex_enter(&db
->db_mtx
);
1167 if ((flags
& DB_RF_NEVERWAIT
) == 0) {
1168 while (db
->db_state
== DB_READ
||
1169 db
->db_state
== DB_FILL
) {
1170 ASSERT(db
->db_state
== DB_READ
||
1171 (flags
& DB_RF_HAVESTRUCT
) == 0);
1172 DTRACE_PROBE2(blocked__read
, dmu_buf_impl_t
*,
1174 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1176 if (db
->db_state
== DB_UNCACHED
)
1177 err
= SET_ERROR(EIO
);
1179 mutex_exit(&db
->db_mtx
);
1186 dbuf_noread(dmu_buf_impl_t
*db
)
1188 ASSERT(!refcount_is_zero(&db
->db_holds
));
1189 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1190 mutex_enter(&db
->db_mtx
);
1191 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
1192 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1193 if (db
->db_state
== DB_UNCACHED
) {
1194 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1195 spa_t
*spa
= db
->db_objset
->os_spa
;
1197 ASSERT(db
->db_buf
== NULL
);
1198 ASSERT(db
->db
.db_data
== NULL
);
1199 dbuf_set_data(db
, arc_alloc_buf(spa
, db
, type
, db
->db
.db_size
));
1200 db
->db_state
= DB_FILL
;
1201 } else if (db
->db_state
== DB_NOFILL
) {
1202 dbuf_clear_data(db
);
1204 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
1206 mutex_exit(&db
->db_mtx
);
1210 dbuf_unoverride(dbuf_dirty_record_t
*dr
)
1212 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
1213 blkptr_t
*bp
= &dr
->dt
.dl
.dr_overridden_by
;
1214 uint64_t txg
= dr
->dr_txg
;
1216 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1218 * This assert is valid because dmu_sync() expects to be called by
1219 * a zilog's get_data while holding a range lock. This call only
1220 * comes from dbuf_dirty() callers who must also hold a range lock.
1222 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_IN_DMU_SYNC
);
1223 ASSERT(db
->db_level
== 0);
1225 if (db
->db_blkid
== DMU_BONUS_BLKID
||
1226 dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
)
1229 ASSERT(db
->db_data_pending
!= dr
);
1231 /* free this block */
1232 if (!BP_IS_HOLE(bp
) && !dr
->dt
.dl
.dr_nopwrite
)
1233 zio_free(db
->db_objset
->os_spa
, txg
, bp
);
1235 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
1236 dr
->dt
.dl
.dr_nopwrite
= B_FALSE
;
1239 * Release the already-written buffer, so we leave it in
1240 * a consistent dirty state. Note that all callers are
1241 * modifying the buffer, so they will immediately do
1242 * another (redundant) arc_release(). Therefore, leave
1243 * the buf thawed to save the effort of freezing &
1244 * immediately re-thawing it.
1246 arc_release(dr
->dt
.dl
.dr_data
, db
);
1250 * Evict (if its unreferenced) or clear (if its referenced) any level-0
1251 * data blocks in the free range, so that any future readers will find
1255 dbuf_free_range(dnode_t
*dn
, uint64_t start_blkid
, uint64_t end_blkid
,
1258 dmu_buf_impl_t db_search
;
1259 dmu_buf_impl_t
*db
, *db_next
;
1260 uint64_t txg
= tx
->tx_txg
;
1263 if (end_blkid
> dn
->dn_maxblkid
&&
1264 !(start_blkid
== DMU_SPILL_BLKID
|| end_blkid
== DMU_SPILL_BLKID
))
1265 end_blkid
= dn
->dn_maxblkid
;
1266 dprintf_dnode(dn
, "start=%llu end=%llu\n", start_blkid
, end_blkid
);
1268 db_search
.db_level
= 0;
1269 db_search
.db_blkid
= start_blkid
;
1270 db_search
.db_state
= DB_SEARCH
;
1272 mutex_enter(&dn
->dn_dbufs_mtx
);
1273 db
= avl_find(&dn
->dn_dbufs
, &db_search
, &where
);
1274 ASSERT3P(db
, ==, NULL
);
1276 db
= avl_nearest(&dn
->dn_dbufs
, where
, AVL_AFTER
);
1278 for (; db
!= NULL
; db
= db_next
) {
1279 db_next
= AVL_NEXT(&dn
->dn_dbufs
, db
);
1280 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1282 if (db
->db_level
!= 0 || db
->db_blkid
> end_blkid
) {
1285 ASSERT3U(db
->db_blkid
, >=, start_blkid
);
1287 /* found a level 0 buffer in the range */
1288 mutex_enter(&db
->db_mtx
);
1289 if (dbuf_undirty(db
, tx
)) {
1290 /* mutex has been dropped and dbuf destroyed */
1294 if (db
->db_state
== DB_UNCACHED
||
1295 db
->db_state
== DB_NOFILL
||
1296 db
->db_state
== DB_EVICTING
) {
1297 ASSERT(db
->db
.db_data
== NULL
);
1298 mutex_exit(&db
->db_mtx
);
1301 if (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
) {
1302 /* will be handled in dbuf_read_done or dbuf_rele */
1303 db
->db_freed_in_flight
= TRUE
;
1304 mutex_exit(&db
->db_mtx
);
1307 if (refcount_count(&db
->db_holds
) == 0) {
1312 /* The dbuf is referenced */
1314 if (db
->db_last_dirty
!= NULL
) {
1315 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
1317 if (dr
->dr_txg
== txg
) {
1319 * This buffer is "in-use", re-adjust the file
1320 * size to reflect that this buffer may
1321 * contain new data when we sync.
1323 if (db
->db_blkid
!= DMU_SPILL_BLKID
&&
1324 db
->db_blkid
> dn
->dn_maxblkid
)
1325 dn
->dn_maxblkid
= db
->db_blkid
;
1326 dbuf_unoverride(dr
);
1329 * This dbuf is not dirty in the open context.
1330 * Either uncache it (if its not referenced in
1331 * the open context) or reset its contents to
1334 dbuf_fix_old_data(db
, txg
);
1337 /* clear the contents if its cached */
1338 if (db
->db_state
== DB_CACHED
) {
1339 ASSERT(db
->db
.db_data
!= NULL
);
1340 arc_release(db
->db_buf
, db
);
1341 bzero(db
->db
.db_data
, db
->db
.db_size
);
1342 arc_buf_freeze(db
->db_buf
);
1345 mutex_exit(&db
->db_mtx
);
1347 mutex_exit(&dn
->dn_dbufs_mtx
);
1351 dbuf_new_size(dmu_buf_impl_t
*db
, int size
, dmu_tx_t
*tx
)
1353 arc_buf_t
*buf
, *obuf
;
1354 int osize
= db
->db
.db_size
;
1355 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1358 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1363 /* XXX does *this* func really need the lock? */
1364 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
1367 * This call to dmu_buf_will_dirty() with the dn_struct_rwlock held
1368 * is OK, because there can be no other references to the db
1369 * when we are changing its size, so no concurrent DB_FILL can
1373 * XXX we should be doing a dbuf_read, checking the return
1374 * value and returning that up to our callers
1376 dmu_buf_will_dirty(&db
->db
, tx
);
1378 /* create the data buffer for the new block */
1379 buf
= arc_alloc_buf(dn
->dn_objset
->os_spa
, db
, type
, size
);
1381 /* copy old block data to the new block */
1383 bcopy(obuf
->b_data
, buf
->b_data
, MIN(osize
, size
));
1384 /* zero the remainder */
1386 bzero((uint8_t *)buf
->b_data
+ osize
, size
- osize
);
1388 mutex_enter(&db
->db_mtx
);
1389 dbuf_set_data(db
, buf
);
1390 arc_buf_destroy(obuf
, db
);
1391 db
->db
.db_size
= size
;
1393 if (db
->db_level
== 0) {
1394 ASSERT3U(db
->db_last_dirty
->dr_txg
, ==, tx
->tx_txg
);
1395 db
->db_last_dirty
->dt
.dl
.dr_data
= buf
;
1397 mutex_exit(&db
->db_mtx
);
1399 dmu_objset_willuse_space(dn
->dn_objset
, size
- osize
, tx
);
1404 dbuf_release_bp(dmu_buf_impl_t
*db
)
1406 objset_t
*os
= db
->db_objset
;
1408 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os
)));
1409 ASSERT(arc_released(os
->os_phys_buf
) ||
1410 list_link_active(&os
->os_dsl_dataset
->ds_synced_link
));
1411 ASSERT(db
->db_parent
== NULL
|| arc_released(db
->db_parent
->db_buf
));
1413 (void) arc_release(db
->db_buf
, db
);
1417 * We already have a dirty record for this TXG, and we are being
1421 dbuf_redirty(dbuf_dirty_record_t
*dr
)
1423 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
1425 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1427 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
) {
1429 * If this buffer has already been written out,
1430 * we now need to reset its state.
1432 dbuf_unoverride(dr
);
1433 if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
&&
1434 db
->db_state
!= DB_NOFILL
) {
1435 /* Already released on initial dirty, so just thaw. */
1436 ASSERT(arc_released(db
->db_buf
));
1437 arc_buf_thaw(db
->db_buf
);
1442 dbuf_dirty_record_t
*
1443 dbuf_dirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1447 dbuf_dirty_record_t
**drp
, *dr
;
1448 int drop_struct_lock
= FALSE
;
1449 int txgoff
= tx
->tx_txg
& TXG_MASK
;
1451 ASSERT(tx
->tx_txg
!= 0);
1452 ASSERT(!refcount_is_zero(&db
->db_holds
));
1453 DMU_TX_DIRTY_BUF(tx
, db
);
1458 * Shouldn't dirty a regular buffer in syncing context. Private
1459 * objects may be dirtied in syncing context, but only if they
1460 * were already pre-dirtied in open context.
1463 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
) {
1464 rrw_enter(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
,
1467 ASSERT(!dmu_tx_is_syncing(tx
) ||
1468 BP_IS_HOLE(dn
->dn_objset
->os_rootbp
) ||
1469 DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
1470 dn
->dn_objset
->os_dsl_dataset
== NULL
);
1471 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
1472 rrw_exit(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
, FTAG
);
1475 * We make this assert for private objects as well, but after we
1476 * check if we're already dirty. They are allowed to re-dirty
1477 * in syncing context.
1479 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
1480 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
1481 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
1483 mutex_enter(&db
->db_mtx
);
1485 * XXX make this true for indirects too? The problem is that
1486 * transactions created with dmu_tx_create_assigned() from
1487 * syncing context don't bother holding ahead.
1489 ASSERT(db
->db_level
!= 0 ||
1490 db
->db_state
== DB_CACHED
|| db
->db_state
== DB_FILL
||
1491 db
->db_state
== DB_NOFILL
);
1493 mutex_enter(&dn
->dn_mtx
);
1495 * Don't set dirtyctx to SYNC if we're just modifying this as we
1496 * initialize the objset.
1498 if (dn
->dn_dirtyctx
== DN_UNDIRTIED
) {
1499 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
) {
1500 rrw_enter(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
,
1503 if (!BP_IS_HOLE(dn
->dn_objset
->os_rootbp
)) {
1504 dn
->dn_dirtyctx
= (dmu_tx_is_syncing(tx
) ?
1505 DN_DIRTY_SYNC
: DN_DIRTY_OPEN
);
1506 ASSERT(dn
->dn_dirtyctx_firstset
== NULL
);
1507 dn
->dn_dirtyctx_firstset
= kmem_alloc(1, KM_SLEEP
);
1509 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
) {
1510 rrw_exit(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
,
1514 mutex_exit(&dn
->dn_mtx
);
1516 if (db
->db_blkid
== DMU_SPILL_BLKID
)
1517 dn
->dn_have_spill
= B_TRUE
;
1520 * If this buffer is already dirty, we're done.
1522 drp
= &db
->db_last_dirty
;
1523 ASSERT(*drp
== NULL
|| (*drp
)->dr_txg
<= tx
->tx_txg
||
1524 db
->db
.db_object
== DMU_META_DNODE_OBJECT
);
1525 while ((dr
= *drp
) != NULL
&& dr
->dr_txg
> tx
->tx_txg
)
1527 if (dr
&& dr
->dr_txg
== tx
->tx_txg
) {
1531 mutex_exit(&db
->db_mtx
);
1536 * Only valid if not already dirty.
1538 ASSERT(dn
->dn_object
== 0 ||
1539 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
1540 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
1542 ASSERT3U(dn
->dn_nlevels
, >, db
->db_level
);
1545 * We should only be dirtying in syncing context if it's the
1546 * mos or we're initializing the os or it's a special object.
1547 * However, we are allowed to dirty in syncing context provided
1548 * we already dirtied it in open context. Hence we must make
1549 * this assertion only if we're not already dirty.
1552 VERIFY3U(tx
->tx_txg
, <=, spa_final_dirty_txg(os
->os_spa
));
1554 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
1555 rrw_enter(&os
->os_dsl_dataset
->ds_bp_rwlock
, RW_READER
, FTAG
);
1556 ASSERT(!dmu_tx_is_syncing(tx
) || DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
1557 os
->os_dsl_dataset
== NULL
|| BP_IS_HOLE(os
->os_rootbp
));
1558 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
1559 rrw_exit(&os
->os_dsl_dataset
->ds_bp_rwlock
, FTAG
);
1561 ASSERT(db
->db
.db_size
!= 0);
1563 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
1565 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
1566 dmu_objset_willuse_space(os
, db
->db
.db_size
, tx
);
1570 * If this buffer is dirty in an old transaction group we need
1571 * to make a copy of it so that the changes we make in this
1572 * transaction group won't leak out when we sync the older txg.
1574 dr
= kmem_zalloc(sizeof (dbuf_dirty_record_t
), KM_SLEEP
);
1575 if (db
->db_level
== 0) {
1576 void *data_old
= db
->db_buf
;
1578 if (db
->db_state
!= DB_NOFILL
) {
1579 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1580 dbuf_fix_old_data(db
, tx
->tx_txg
);
1581 data_old
= db
->db
.db_data
;
1582 } else if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
) {
1584 * Release the data buffer from the cache so
1585 * that we can modify it without impacting
1586 * possible other users of this cached data
1587 * block. Note that indirect blocks and
1588 * private objects are not released until the
1589 * syncing state (since they are only modified
1592 arc_release(db
->db_buf
, db
);
1593 dbuf_fix_old_data(db
, tx
->tx_txg
);
1594 data_old
= db
->db_buf
;
1596 ASSERT(data_old
!= NULL
);
1598 dr
->dt
.dl
.dr_data
= data_old
;
1600 mutex_init(&dr
->dt
.di
.dr_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
1601 list_create(&dr
->dt
.di
.dr_children
,
1602 sizeof (dbuf_dirty_record_t
),
1603 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
1605 if (db
->db_blkid
!= DMU_BONUS_BLKID
&& os
->os_dsl_dataset
!= NULL
)
1606 dr
->dr_accounted
= db
->db
.db_size
;
1608 dr
->dr_txg
= tx
->tx_txg
;
1613 * We could have been freed_in_flight between the dbuf_noread
1614 * and dbuf_dirty. We win, as though the dbuf_noread() had
1615 * happened after the free.
1617 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1618 db
->db_blkid
!= DMU_SPILL_BLKID
) {
1619 mutex_enter(&dn
->dn_mtx
);
1620 if (dn
->dn_free_ranges
[txgoff
] != NULL
) {
1621 range_tree_clear(dn
->dn_free_ranges
[txgoff
],
1624 mutex_exit(&dn
->dn_mtx
);
1625 db
->db_freed_in_flight
= FALSE
;
1629 * This buffer is now part of this txg
1631 dbuf_add_ref(db
, (void *)(uintptr_t)tx
->tx_txg
);
1632 db
->db_dirtycnt
+= 1;
1633 ASSERT3U(db
->db_dirtycnt
, <=, 3);
1635 mutex_exit(&db
->db_mtx
);
1637 if (db
->db_blkid
== DMU_BONUS_BLKID
||
1638 db
->db_blkid
== DMU_SPILL_BLKID
) {
1639 mutex_enter(&dn
->dn_mtx
);
1640 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1641 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
1642 mutex_exit(&dn
->dn_mtx
);
1643 dnode_setdirty(dn
, tx
);
1649 * The dn_struct_rwlock prevents db_blkptr from changing
1650 * due to a write from syncing context completing
1651 * while we are running, so we want to acquire it before
1652 * looking at db_blkptr.
1654 if (!RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
1655 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
1656 drop_struct_lock
= TRUE
;
1660 * We need to hold the dn_struct_rwlock to make this assertion,
1661 * because it protects dn_phys / dn_next_nlevels from changing.
1663 ASSERT((dn
->dn_phys
->dn_nlevels
== 0 && db
->db_level
== 0) ||
1664 dn
->dn_phys
->dn_nlevels
> db
->db_level
||
1665 dn
->dn_next_nlevels
[txgoff
] > db
->db_level
||
1666 dn
->dn_next_nlevels
[(tx
->tx_txg
-1) & TXG_MASK
] > db
->db_level
||
1667 dn
->dn_next_nlevels
[(tx
->tx_txg
-2) & TXG_MASK
] > db
->db_level
);
1670 * If we are overwriting a dedup BP, then unless it is snapshotted,
1671 * when we get to syncing context we will need to decrement its
1672 * refcount in the DDT. Prefetch the relevant DDT block so that
1673 * syncing context won't have to wait for the i/o.
1675 ddt_prefetch(os
->os_spa
, db
->db_blkptr
);
1677 if (db
->db_level
== 0) {
1678 dnode_new_blkid(dn
, db
->db_blkid
, tx
, drop_struct_lock
);
1679 ASSERT(dn
->dn_maxblkid
>= db
->db_blkid
);
1682 if (db
->db_level
+1 < dn
->dn_nlevels
) {
1683 dmu_buf_impl_t
*parent
= db
->db_parent
;
1684 dbuf_dirty_record_t
*di
;
1685 int parent_held
= FALSE
;
1687 if (db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
) {
1688 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1690 parent
= dbuf_hold_level(dn
, db
->db_level
+1,
1691 db
->db_blkid
>> epbs
, FTAG
);
1692 ASSERT(parent
!= NULL
);
1695 if (drop_struct_lock
)
1696 rw_exit(&dn
->dn_struct_rwlock
);
1697 ASSERT3U(db
->db_level
+1, ==, parent
->db_level
);
1698 di
= dbuf_dirty(parent
, tx
);
1700 dbuf_rele(parent
, FTAG
);
1702 mutex_enter(&db
->db_mtx
);
1704 * Since we've dropped the mutex, it's possible that
1705 * dbuf_undirty() might have changed this out from under us.
1707 if (db
->db_last_dirty
== dr
||
1708 dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
1709 mutex_enter(&di
->dt
.di
.dr_mtx
);
1710 ASSERT3U(di
->dr_txg
, ==, tx
->tx_txg
);
1711 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1712 list_insert_tail(&di
->dt
.di
.dr_children
, dr
);
1713 mutex_exit(&di
->dt
.di
.dr_mtx
);
1716 mutex_exit(&db
->db_mtx
);
1718 ASSERT(db
->db_level
+1 == dn
->dn_nlevels
);
1719 ASSERT(db
->db_blkid
< dn
->dn_nblkptr
);
1720 ASSERT(db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
1721 mutex_enter(&dn
->dn_mtx
);
1722 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1723 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
1724 mutex_exit(&dn
->dn_mtx
);
1725 if (drop_struct_lock
)
1726 rw_exit(&dn
->dn_struct_rwlock
);
1729 dnode_setdirty(dn
, tx
);
1735 * Undirty a buffer in the transaction group referenced by the given
1736 * transaction. Return whether this evicted the dbuf.
1739 dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1742 uint64_t txg
= tx
->tx_txg
;
1743 dbuf_dirty_record_t
*dr
, **drp
;
1748 * Due to our use of dn_nlevels below, this can only be called
1749 * in open context, unless we are operating on the MOS.
1750 * From syncing context, dn_nlevels may be different from the
1751 * dn_nlevels used when dbuf was dirtied.
1753 ASSERT(db
->db_objset
==
1754 dmu_objset_pool(db
->db_objset
)->dp_meta_objset
||
1755 txg
!= spa_syncing_txg(dmu_objset_spa(db
->db_objset
)));
1756 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1757 ASSERT0(db
->db_level
);
1758 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1761 * If this buffer is not dirty, we're done.
1763 for (drp
= &db
->db_last_dirty
; (dr
= *drp
) != NULL
; drp
= &dr
->dr_next
)
1764 if (dr
->dr_txg
<= txg
)
1766 if (dr
== NULL
|| dr
->dr_txg
< txg
)
1768 ASSERT(dr
->dr_txg
== txg
);
1769 ASSERT(dr
->dr_dbuf
== db
);
1774 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
1776 ASSERT(db
->db
.db_size
!= 0);
1778 dsl_pool_undirty_space(dmu_objset_pool(dn
->dn_objset
),
1779 dr
->dr_accounted
, txg
);
1784 * Note that there are three places in dbuf_dirty()
1785 * where this dirty record may be put on a list.
1786 * Make sure to do a list_remove corresponding to
1787 * every one of those list_insert calls.
1789 if (dr
->dr_parent
) {
1790 mutex_enter(&dr
->dr_parent
->dt
.di
.dr_mtx
);
1791 list_remove(&dr
->dr_parent
->dt
.di
.dr_children
, dr
);
1792 mutex_exit(&dr
->dr_parent
->dt
.di
.dr_mtx
);
1793 } else if (db
->db_blkid
== DMU_SPILL_BLKID
||
1794 db
->db_level
+ 1 == dn
->dn_nlevels
) {
1795 ASSERT(db
->db_blkptr
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
1796 mutex_enter(&dn
->dn_mtx
);
1797 list_remove(&dn
->dn_dirty_records
[txg
& TXG_MASK
], dr
);
1798 mutex_exit(&dn
->dn_mtx
);
1802 if (db
->db_state
!= DB_NOFILL
) {
1803 dbuf_unoverride(dr
);
1805 ASSERT(db
->db_buf
!= NULL
);
1806 ASSERT(dr
->dt
.dl
.dr_data
!= NULL
);
1807 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
1808 arc_buf_destroy(dr
->dt
.dl
.dr_data
, db
);
1811 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
1813 ASSERT(db
->db_dirtycnt
> 0);
1814 db
->db_dirtycnt
-= 1;
1816 if (refcount_remove(&db
->db_holds
, (void *)(uintptr_t)txg
) == 0) {
1817 ASSERT(db
->db_state
== DB_NOFILL
|| arc_released(db
->db_buf
));
1826 dmu_buf_will_dirty(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1828 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1829 int rf
= DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
;
1831 ASSERT(tx
->tx_txg
!= 0);
1832 ASSERT(!refcount_is_zero(&db
->db_holds
));
1835 * Quick check for dirtyness. For already dirty blocks, this
1836 * reduces runtime of this function by >90%, and overall performance
1837 * by 50% for some workloads (e.g. file deletion with indirect blocks
1840 mutex_enter(&db
->db_mtx
);
1841 dbuf_dirty_record_t
*dr
;
1842 for (dr
= db
->db_last_dirty
;
1843 dr
!= NULL
&& dr
->dr_txg
>= tx
->tx_txg
; dr
= dr
->dr_next
) {
1845 * It's possible that it is already dirty but not cached,
1846 * because there are some calls to dbuf_dirty() that don't
1847 * go through dmu_buf_will_dirty().
1849 if (dr
->dr_txg
== tx
->tx_txg
&& db
->db_state
== DB_CACHED
) {
1850 /* This dbuf is already dirty and cached. */
1852 mutex_exit(&db
->db_mtx
);
1856 mutex_exit(&db
->db_mtx
);
1859 if (RW_WRITE_HELD(&DB_DNODE(db
)->dn_struct_rwlock
))
1860 rf
|= DB_RF_HAVESTRUCT
;
1862 (void) dbuf_read(db
, NULL
, rf
);
1863 (void) dbuf_dirty(db
, tx
);
1867 dmu_buf_will_not_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1869 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1871 db
->db_state
= DB_NOFILL
;
1873 dmu_buf_will_fill(db_fake
, tx
);
1877 dmu_buf_will_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1879 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1881 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1882 ASSERT(tx
->tx_txg
!= 0);
1883 ASSERT(db
->db_level
== 0);
1884 ASSERT(!refcount_is_zero(&db
->db_holds
));
1886 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
||
1887 dmu_tx_private_ok(tx
));
1890 (void) dbuf_dirty(db
, tx
);
1893 #pragma weak dmu_buf_fill_done = dbuf_fill_done
1896 dbuf_fill_done(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1898 mutex_enter(&db
->db_mtx
);
1901 if (db
->db_state
== DB_FILL
) {
1902 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
1903 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1904 /* we were freed while filling */
1905 /* XXX dbuf_undirty? */
1906 bzero(db
->db
.db_data
, db
->db
.db_size
);
1907 db
->db_freed_in_flight
= FALSE
;
1909 db
->db_state
= DB_CACHED
;
1910 cv_broadcast(&db
->db_changed
);
1912 mutex_exit(&db
->db_mtx
);
1916 dmu_buf_write_embedded(dmu_buf_t
*dbuf
, void *data
,
1917 bp_embedded_type_t etype
, enum zio_compress comp
,
1918 int uncompressed_size
, int compressed_size
, int byteorder
,
1921 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbuf
;
1922 struct dirty_leaf
*dl
;
1923 dmu_object_type_t type
;
1925 if (etype
== BP_EMBEDDED_TYPE_DATA
) {
1926 ASSERT(spa_feature_is_active(dmu_objset_spa(db
->db_objset
),
1927 SPA_FEATURE_EMBEDDED_DATA
));
1931 type
= DB_DNODE(db
)->dn_type
;
1934 ASSERT0(db
->db_level
);
1935 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1937 dmu_buf_will_not_fill(dbuf
, tx
);
1939 ASSERT3U(db
->db_last_dirty
->dr_txg
, ==, tx
->tx_txg
);
1940 dl
= &db
->db_last_dirty
->dt
.dl
;
1941 encode_embedded_bp_compressed(&dl
->dr_overridden_by
,
1942 data
, comp
, uncompressed_size
, compressed_size
);
1943 BPE_SET_ETYPE(&dl
->dr_overridden_by
, etype
);
1944 BP_SET_TYPE(&dl
->dr_overridden_by
, type
);
1945 BP_SET_LEVEL(&dl
->dr_overridden_by
, 0);
1946 BP_SET_BYTEORDER(&dl
->dr_overridden_by
, byteorder
);
1948 dl
->dr_override_state
= DR_OVERRIDDEN
;
1949 dl
->dr_overridden_by
.blk_birth
= db
->db_last_dirty
->dr_txg
;
1953 * Directly assign a provided arc buf to a given dbuf if it's not referenced
1954 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
1957 dbuf_assign_arcbuf(dmu_buf_impl_t
*db
, arc_buf_t
*buf
, dmu_tx_t
*tx
)
1959 ASSERT(!refcount_is_zero(&db
->db_holds
));
1960 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1961 ASSERT(db
->db_level
== 0);
1962 ASSERT3U(dbuf_is_metadata(db
), ==, arc_is_metadata(buf
));
1963 ASSERT(buf
!= NULL
);
1964 ASSERT(arc_buf_lsize(buf
) == db
->db
.db_size
);
1965 ASSERT(tx
->tx_txg
!= 0);
1967 arc_return_buf(buf
, db
);
1968 ASSERT(arc_released(buf
));
1970 mutex_enter(&db
->db_mtx
);
1972 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
1973 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1975 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_UNCACHED
);
1977 if (db
->db_state
== DB_CACHED
&&
1978 refcount_count(&db
->db_holds
) - 1 > db
->db_dirtycnt
) {
1979 mutex_exit(&db
->db_mtx
);
1980 (void) dbuf_dirty(db
, tx
);
1981 bcopy(buf
->b_data
, db
->db
.db_data
, db
->db
.db_size
);
1982 arc_buf_destroy(buf
, db
);
1983 xuio_stat_wbuf_copied();
1987 xuio_stat_wbuf_nocopy();
1988 if (db
->db_state
== DB_CACHED
) {
1989 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
1991 ASSERT(db
->db_buf
!= NULL
);
1992 if (dr
!= NULL
&& dr
->dr_txg
== tx
->tx_txg
) {
1993 ASSERT(dr
->dt
.dl
.dr_data
== db
->db_buf
);
1994 if (!arc_released(db
->db_buf
)) {
1995 ASSERT(dr
->dt
.dl
.dr_override_state
==
1997 arc_release(db
->db_buf
, db
);
1999 dr
->dt
.dl
.dr_data
= buf
;
2000 arc_buf_destroy(db
->db_buf
, db
);
2001 } else if (dr
== NULL
|| dr
->dt
.dl
.dr_data
!= db
->db_buf
) {
2002 arc_release(db
->db_buf
, db
);
2003 arc_buf_destroy(db
->db_buf
, db
);
2007 ASSERT(db
->db_buf
== NULL
);
2008 dbuf_set_data(db
, buf
);
2009 db
->db_state
= DB_FILL
;
2010 mutex_exit(&db
->db_mtx
);
2011 (void) dbuf_dirty(db
, tx
);
2012 dmu_buf_fill_done(&db
->db
, tx
);
2016 dbuf_destroy(dmu_buf_impl_t
*db
)
2019 dmu_buf_impl_t
*parent
= db
->db_parent
;
2020 dmu_buf_impl_t
*dndb
;
2022 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2023 ASSERT(refcount_is_zero(&db
->db_holds
));
2025 if (db
->db_buf
!= NULL
) {
2026 arc_buf_destroy(db
->db_buf
, db
);
2030 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2031 ASSERT(db
->db
.db_data
!= NULL
);
2032 zio_buf_free(db
->db
.db_data
, DN_MAX_BONUSLEN
);
2033 arc_space_return(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
2034 db
->db_state
= DB_UNCACHED
;
2037 dbuf_clear_data(db
);
2039 if (multilist_link_active(&db
->db_cache_link
)) {
2040 multilist_remove(dbuf_cache
, db
);
2041 (void) refcount_remove_many(&dbuf_cache_size
,
2042 db
->db
.db_size
, db
);
2045 ASSERT(db
->db_state
== DB_UNCACHED
|| db
->db_state
== DB_NOFILL
);
2046 ASSERT(db
->db_data_pending
== NULL
);
2048 db
->db_state
= DB_EVICTING
;
2049 db
->db_blkptr
= NULL
;
2052 * Now that db_state is DB_EVICTING, nobody else can find this via
2053 * the hash table. We can now drop db_mtx, which allows us to
2054 * acquire the dn_dbufs_mtx.
2056 mutex_exit(&db
->db_mtx
);
2061 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
2062 boolean_t needlock
= !MUTEX_HELD(&dn
->dn_dbufs_mtx
);
2064 mutex_enter(&dn
->dn_dbufs_mtx
);
2065 avl_remove(&dn
->dn_dbufs
, db
);
2066 atomic_dec_32(&dn
->dn_dbufs_count
);
2070 mutex_exit(&dn
->dn_dbufs_mtx
);
2072 * Decrementing the dbuf count means that the hold corresponding
2073 * to the removed dbuf is no longer discounted in dnode_move(),
2074 * so the dnode cannot be moved until after we release the hold.
2075 * The membar_producer() ensures visibility of the decremented
2076 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
2080 db
->db_dnode_handle
= NULL
;
2082 dbuf_hash_remove(db
);
2087 ASSERT(refcount_is_zero(&db
->db_holds
));
2089 db
->db_parent
= NULL
;
2091 ASSERT(db
->db_buf
== NULL
);
2092 ASSERT(db
->db
.db_data
== NULL
);
2093 ASSERT(db
->db_hash_next
== NULL
);
2094 ASSERT(db
->db_blkptr
== NULL
);
2095 ASSERT(db
->db_data_pending
== NULL
);
2096 ASSERT(!multilist_link_active(&db
->db_cache_link
));
2098 kmem_cache_free(dbuf_kmem_cache
, db
);
2099 arc_space_return(sizeof (dmu_buf_impl_t
), ARC_SPACE_OTHER
);
2102 * If this dbuf is referenced from an indirect dbuf,
2103 * decrement the ref count on the indirect dbuf.
2105 if (parent
&& parent
!= dndb
)
2106 dbuf_rele(parent
, db
);
2110 * Note: While bpp will always be updated if the function returns success,
2111 * parentp will not be updated if the dnode does not have dn_dbuf filled in;
2112 * this happens when the dnode is the meta-dnode, or a userused or groupused
2116 dbuf_findbp(dnode_t
*dn
, int level
, uint64_t blkid
, int fail_sparse
,
2117 dmu_buf_impl_t
**parentp
, blkptr_t
**bpp
)
2122 ASSERT(blkid
!= DMU_BONUS_BLKID
);
2124 if (blkid
== DMU_SPILL_BLKID
) {
2125 mutex_enter(&dn
->dn_mtx
);
2126 if (dn
->dn_have_spill
&&
2127 (dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
))
2128 *bpp
= &dn
->dn_phys
->dn_spill
;
2131 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
2132 *parentp
= dn
->dn_dbuf
;
2133 mutex_exit(&dn
->dn_mtx
);
2138 (dn
->dn_phys
->dn_nlevels
== 0) ? 1 : dn
->dn_phys
->dn_nlevels
;
2139 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2141 ASSERT3U(level
* epbs
, <, 64);
2142 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2144 * This assertion shouldn't trip as long as the max indirect block size
2145 * is less than 1M. The reason for this is that up to that point,
2146 * the number of levels required to address an entire object with blocks
2147 * of size SPA_MINBLOCKSIZE satisfies nlevels * epbs + 1 <= 64. In
2148 * other words, if N * epbs + 1 > 64, then if (N-1) * epbs + 1 > 55
2149 * (i.e. we can address the entire object), objects will all use at most
2150 * N-1 levels and the assertion won't overflow. However, once epbs is
2151 * 13, 4 * 13 + 1 = 53, but 5 * 13 + 1 = 66. Then, 4 levels will not be
2152 * enough to address an entire object, so objects will have 5 levels,
2153 * but then this assertion will overflow.
2155 * All this is to say that if we ever increase DN_MAX_INDBLKSHIFT, we
2156 * need to redo this logic to handle overflows.
2158 ASSERT(level
>= nlevels
||
2159 ((nlevels
- level
- 1) * epbs
) +
2160 highbit64(dn
->dn_phys
->dn_nblkptr
) <= 64);
2161 if (level
>= nlevels
||
2162 blkid
>= ((uint64_t)dn
->dn_phys
->dn_nblkptr
<<
2163 ((nlevels
- level
- 1) * epbs
)) ||
2165 blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))) {
2166 /* the buffer has no parent yet */
2167 return (SET_ERROR(ENOENT
));
2168 } else if (level
< nlevels
-1) {
2169 /* this block is referenced from an indirect block */
2170 int err
= dbuf_hold_impl(dn
, level
+1,
2171 blkid
>> epbs
, fail_sparse
, FALSE
, NULL
, parentp
);
2174 err
= dbuf_read(*parentp
, NULL
,
2175 (DB_RF_HAVESTRUCT
| DB_RF_NOPREFETCH
| DB_RF_CANFAIL
));
2177 dbuf_rele(*parentp
, NULL
);
2181 *bpp
= ((blkptr_t
*)(*parentp
)->db
.db_data
) +
2182 (blkid
& ((1ULL << epbs
) - 1));
2183 if (blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))
2184 ASSERT(BP_IS_HOLE(*bpp
));
2187 /* the block is referenced from the dnode */
2188 ASSERT3U(level
, ==, nlevels
-1);
2189 ASSERT(dn
->dn_phys
->dn_nblkptr
== 0 ||
2190 blkid
< dn
->dn_phys
->dn_nblkptr
);
2192 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
2193 *parentp
= dn
->dn_dbuf
;
2195 *bpp
= &dn
->dn_phys
->dn_blkptr
[blkid
];
2200 static dmu_buf_impl_t
*
2201 dbuf_create(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
2202 dmu_buf_impl_t
*parent
, blkptr_t
*blkptr
)
2204 objset_t
*os
= dn
->dn_objset
;
2205 dmu_buf_impl_t
*db
, *odb
;
2207 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2208 ASSERT(dn
->dn_type
!= DMU_OT_NONE
);
2210 db
= kmem_cache_alloc(dbuf_kmem_cache
, KM_SLEEP
);
2213 db
->db
.db_object
= dn
->dn_object
;
2214 db
->db_level
= level
;
2215 db
->db_blkid
= blkid
;
2216 db
->db_last_dirty
= NULL
;
2217 db
->db_dirtycnt
= 0;
2218 db
->db_dnode_handle
= dn
->dn_handle
;
2219 db
->db_parent
= parent
;
2220 db
->db_blkptr
= blkptr
;
2223 db
->db_user_immediate_evict
= FALSE
;
2224 db
->db_freed_in_flight
= FALSE
;
2225 db
->db_pending_evict
= FALSE
;
2227 if (blkid
== DMU_BONUS_BLKID
) {
2228 ASSERT3P(parent
, ==, dn
->dn_dbuf
);
2229 db
->db
.db_size
= DN_MAX_BONUSLEN
-
2230 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
);
2231 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
2232 db
->db
.db_offset
= DMU_BONUS_BLKID
;
2233 db
->db_state
= DB_UNCACHED
;
2234 /* the bonus dbuf is not placed in the hash table */
2235 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_OTHER
);
2237 } else if (blkid
== DMU_SPILL_BLKID
) {
2238 db
->db
.db_size
= (blkptr
!= NULL
) ?
2239 BP_GET_LSIZE(blkptr
) : SPA_MINBLOCKSIZE
;
2240 db
->db
.db_offset
= 0;
2243 db
->db_level
? 1 << dn
->dn_indblkshift
: dn
->dn_datablksz
;
2244 db
->db
.db_size
= blocksize
;
2245 db
->db
.db_offset
= db
->db_blkid
* blocksize
;
2249 * Hold the dn_dbufs_mtx while we get the new dbuf
2250 * in the hash table *and* added to the dbufs list.
2251 * This prevents a possible deadlock with someone
2252 * trying to look up this dbuf before its added to the
2255 mutex_enter(&dn
->dn_dbufs_mtx
);
2256 db
->db_state
= DB_EVICTING
;
2257 if ((odb
= dbuf_hash_insert(db
)) != NULL
) {
2258 /* someone else inserted it first */
2259 kmem_cache_free(dbuf_kmem_cache
, db
);
2260 mutex_exit(&dn
->dn_dbufs_mtx
);
2263 avl_add(&dn
->dn_dbufs
, db
);
2265 db
->db_state
= DB_UNCACHED
;
2266 mutex_exit(&dn
->dn_dbufs_mtx
);
2267 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_OTHER
);
2269 if (parent
&& parent
!= dn
->dn_dbuf
)
2270 dbuf_add_ref(parent
, db
);
2272 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
2273 refcount_count(&dn
->dn_holds
) > 0);
2274 (void) refcount_add(&dn
->dn_holds
, db
);
2275 atomic_inc_32(&dn
->dn_dbufs_count
);
2277 dprintf_dbuf(db
, "db=%p\n", db
);
2282 typedef struct dbuf_prefetch_arg
{
2283 spa_t
*dpa_spa
; /* The spa to issue the prefetch in. */
2284 zbookmark_phys_t dpa_zb
; /* The target block to prefetch. */
2285 int dpa_epbs
; /* Entries (blkptr_t's) Per Block Shift. */
2286 int dpa_curlevel
; /* The current level that we're reading */
2287 dnode_t
*dpa_dnode
; /* The dnode associated with the prefetch */
2288 zio_priority_t dpa_prio
; /* The priority I/Os should be issued at. */
2289 zio_t
*dpa_zio
; /* The parent zio_t for all prefetches. */
2290 arc_flags_t dpa_aflags
; /* Flags to pass to the final prefetch. */
2291 } dbuf_prefetch_arg_t
;
2294 * Actually issue the prefetch read for the block given.
2297 dbuf_issue_final_prefetch(dbuf_prefetch_arg_t
*dpa
, blkptr_t
*bp
)
2299 if (BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
))
2302 arc_flags_t aflags
=
2303 dpa
->dpa_aflags
| ARC_FLAG_NOWAIT
| ARC_FLAG_PREFETCH
;
2305 ASSERT3U(dpa
->dpa_curlevel
, ==, BP_GET_LEVEL(bp
));
2306 ASSERT3U(dpa
->dpa_curlevel
, ==, dpa
->dpa_zb
.zb_level
);
2307 ASSERT(dpa
->dpa_zio
!= NULL
);
2308 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
, bp
, NULL
, NULL
,
2309 dpa
->dpa_prio
, ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
2310 &aflags
, &dpa
->dpa_zb
);
2314 * Called when an indirect block above our prefetch target is read in. This
2315 * will either read in the next indirect block down the tree or issue the actual
2316 * prefetch if the next block down is our target.
2319 dbuf_prefetch_indirect_done(zio_t
*zio
, arc_buf_t
*abuf
, void *private)
2321 dbuf_prefetch_arg_t
*dpa
= private;
2323 ASSERT3S(dpa
->dpa_zb
.zb_level
, <, dpa
->dpa_curlevel
);
2324 ASSERT3S(dpa
->dpa_curlevel
, >, 0);
2327 * The dpa_dnode is only valid if we are called with a NULL
2328 * zio. This indicates that the arc_read() returned without
2329 * first calling zio_read() to issue a physical read. Once
2330 * a physical read is made the dpa_dnode must be invalidated
2331 * as the locks guarding it may have been dropped. If the
2332 * dpa_dnode is still valid, then we want to add it to the dbuf
2333 * cache. To do so, we must hold the dbuf associated with the block
2334 * we just prefetched, read its contents so that we associate it
2335 * with an arc_buf_t, and then release it.
2338 ASSERT3S(BP_GET_LEVEL(zio
->io_bp
), ==, dpa
->dpa_curlevel
);
2339 if (zio
->io_flags
& ZIO_FLAG_RAW
) {
2340 ASSERT3U(BP_GET_PSIZE(zio
->io_bp
), ==, zio
->io_size
);
2342 ASSERT3U(BP_GET_LSIZE(zio
->io_bp
), ==, zio
->io_size
);
2344 ASSERT3P(zio
->io_spa
, ==, dpa
->dpa_spa
);
2346 dpa
->dpa_dnode
= NULL
;
2347 } else if (dpa
->dpa_dnode
!= NULL
) {
2348 uint64_t curblkid
= dpa
->dpa_zb
.zb_blkid
>>
2349 (dpa
->dpa_epbs
* (dpa
->dpa_curlevel
-
2350 dpa
->dpa_zb
.zb_level
));
2351 dmu_buf_impl_t
*db
= dbuf_hold_level(dpa
->dpa_dnode
,
2352 dpa
->dpa_curlevel
, curblkid
, FTAG
);
2353 (void) dbuf_read(db
, NULL
,
2354 DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
| DB_RF_HAVESTRUCT
);
2355 dbuf_rele(db
, FTAG
);
2358 dpa
->dpa_curlevel
--;
2360 uint64_t nextblkid
= dpa
->dpa_zb
.zb_blkid
>>
2361 (dpa
->dpa_epbs
* (dpa
->dpa_curlevel
- dpa
->dpa_zb
.zb_level
));
2362 blkptr_t
*bp
= ((blkptr_t
*)abuf
->b_data
) +
2363 P2PHASE(nextblkid
, 1ULL << dpa
->dpa_epbs
);
2364 if (BP_IS_HOLE(bp
) || (zio
!= NULL
&& zio
->io_error
!= 0)) {
2365 kmem_free(dpa
, sizeof (*dpa
));
2366 } else if (dpa
->dpa_curlevel
== dpa
->dpa_zb
.zb_level
) {
2367 ASSERT3U(nextblkid
, ==, dpa
->dpa_zb
.zb_blkid
);
2368 dbuf_issue_final_prefetch(dpa
, bp
);
2369 kmem_free(dpa
, sizeof (*dpa
));
2371 arc_flags_t iter_aflags
= ARC_FLAG_NOWAIT
;
2372 zbookmark_phys_t zb
;
2374 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
2375 if (dpa
->dpa_aflags
& ARC_FLAG_L2CACHE
)
2376 iter_aflags
|= ARC_FLAG_L2CACHE
;
2378 ASSERT3U(dpa
->dpa_curlevel
, ==, BP_GET_LEVEL(bp
));
2380 SET_BOOKMARK(&zb
, dpa
->dpa_zb
.zb_objset
,
2381 dpa
->dpa_zb
.zb_object
, dpa
->dpa_curlevel
, nextblkid
);
2383 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
,
2384 bp
, dbuf_prefetch_indirect_done
, dpa
, dpa
->dpa_prio
,
2385 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
2389 arc_buf_destroy(abuf
, private);
2393 * Issue prefetch reads for the given block on the given level. If the indirect
2394 * blocks above that block are not in memory, we will read them in
2395 * asynchronously. As a result, this call never blocks waiting for a read to
2399 dbuf_prefetch(dnode_t
*dn
, int64_t level
, uint64_t blkid
, zio_priority_t prio
,
2403 int epbs
, nlevels
, curlevel
;
2406 ASSERT(blkid
!= DMU_BONUS_BLKID
);
2407 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2409 if (blkid
> dn
->dn_maxblkid
)
2412 if (dnode_block_freed(dn
, blkid
))
2416 * This dnode hasn't been written to disk yet, so there's nothing to
2419 nlevels
= dn
->dn_phys
->dn_nlevels
;
2420 if (level
>= nlevels
|| dn
->dn_phys
->dn_nblkptr
== 0)
2423 epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2424 if (dn
->dn_phys
->dn_maxblkid
< blkid
<< (epbs
* level
))
2427 dmu_buf_impl_t
*db
= dbuf_find(dn
->dn_objset
, dn
->dn_object
,
2430 mutex_exit(&db
->db_mtx
);
2432 * This dbuf already exists. It is either CACHED, or
2433 * (we assume) about to be read or filled.
2439 * Find the closest ancestor (indirect block) of the target block
2440 * that is present in the cache. In this indirect block, we will
2441 * find the bp that is at curlevel, curblkid.
2445 while (curlevel
< nlevels
- 1) {
2446 int parent_level
= curlevel
+ 1;
2447 uint64_t parent_blkid
= curblkid
>> epbs
;
2450 if (dbuf_hold_impl(dn
, parent_level
, parent_blkid
,
2451 FALSE
, TRUE
, FTAG
, &db
) == 0) {
2452 blkptr_t
*bpp
= db
->db_buf
->b_data
;
2453 bp
= bpp
[P2PHASE(curblkid
, 1 << epbs
)];
2454 dbuf_rele(db
, FTAG
);
2458 curlevel
= parent_level
;
2459 curblkid
= parent_blkid
;
2462 if (curlevel
== nlevels
- 1) {
2463 /* No cached indirect blocks found. */
2464 ASSERT3U(curblkid
, <, dn
->dn_phys
->dn_nblkptr
);
2465 bp
= dn
->dn_phys
->dn_blkptr
[curblkid
];
2467 if (BP_IS_HOLE(&bp
))
2470 ASSERT3U(curlevel
, ==, BP_GET_LEVEL(&bp
));
2472 zio_t
*pio
= zio_root(dmu_objset_spa(dn
->dn_objset
), NULL
, NULL
,
2475 dbuf_prefetch_arg_t
*dpa
= kmem_zalloc(sizeof (*dpa
), KM_SLEEP
);
2476 dsl_dataset_t
*ds
= dn
->dn_objset
->os_dsl_dataset
;
2477 SET_BOOKMARK(&dpa
->dpa_zb
, ds
!= NULL
? ds
->ds_object
: DMU_META_OBJSET
,
2478 dn
->dn_object
, level
, blkid
);
2479 dpa
->dpa_curlevel
= curlevel
;
2480 dpa
->dpa_prio
= prio
;
2481 dpa
->dpa_aflags
= aflags
;
2482 dpa
->dpa_spa
= dn
->dn_objset
->os_spa
;
2483 dpa
->dpa_dnode
= dn
;
2484 dpa
->dpa_epbs
= epbs
;
2487 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
2488 if (DNODE_LEVEL_IS_L2CACHEABLE(dn
, level
))
2489 dpa
->dpa_aflags
|= ARC_FLAG_L2CACHE
;
2492 * If we have the indirect just above us, no need to do the asynchronous
2493 * prefetch chain; we'll just run the last step ourselves. If we're at
2494 * a higher level, though, we want to issue the prefetches for all the
2495 * indirect blocks asynchronously, so we can go on with whatever we were
2498 if (curlevel
== level
) {
2499 ASSERT3U(curblkid
, ==, blkid
);
2500 dbuf_issue_final_prefetch(dpa
, &bp
);
2501 kmem_free(dpa
, sizeof (*dpa
));
2503 arc_flags_t iter_aflags
= ARC_FLAG_NOWAIT
;
2504 zbookmark_phys_t zb
;
2506 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
2507 if (DNODE_LEVEL_IS_L2CACHEABLE(dn
, level
))
2508 iter_aflags
|= ARC_FLAG_L2CACHE
;
2510 SET_BOOKMARK(&zb
, ds
!= NULL
? ds
->ds_object
: DMU_META_OBJSET
,
2511 dn
->dn_object
, curlevel
, curblkid
);
2512 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
,
2513 &bp
, dbuf_prefetch_indirect_done
, dpa
, prio
,
2514 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
2518 * We use pio here instead of dpa_zio since it's possible that
2519 * dpa may have already been freed.
2525 * Returns with db_holds incremented, and db_mtx not held.
2526 * Note: dn_struct_rwlock must be held.
2529 dbuf_hold_impl(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
2530 boolean_t fail_sparse
, boolean_t fail_uncached
,
2531 void *tag
, dmu_buf_impl_t
**dbp
)
2533 dmu_buf_impl_t
*db
, *parent
= NULL
;
2535 ASSERT(blkid
!= DMU_BONUS_BLKID
);
2536 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2537 ASSERT3U(dn
->dn_nlevels
, >, level
);
2541 /* dbuf_find() returns with db_mtx held */
2542 db
= dbuf_find(dn
->dn_objset
, dn
->dn_object
, level
, blkid
);
2545 blkptr_t
*bp
= NULL
;
2549 return (SET_ERROR(ENOENT
));
2551 ASSERT3P(parent
, ==, NULL
);
2552 err
= dbuf_findbp(dn
, level
, blkid
, fail_sparse
, &parent
, &bp
);
2554 if (err
== 0 && bp
&& BP_IS_HOLE(bp
))
2555 err
= SET_ERROR(ENOENT
);
2558 dbuf_rele(parent
, NULL
);
2562 if (err
&& err
!= ENOENT
)
2564 db
= dbuf_create(dn
, level
, blkid
, parent
, bp
);
2567 if (fail_uncached
&& db
->db_state
!= DB_CACHED
) {
2568 mutex_exit(&db
->db_mtx
);
2569 return (SET_ERROR(ENOENT
));
2572 if (db
->db_buf
!= NULL
)
2573 ASSERT3P(db
->db
.db_data
, ==, db
->db_buf
->b_data
);
2575 ASSERT(db
->db_buf
== NULL
|| arc_referenced(db
->db_buf
));
2578 * If this buffer is currently syncing out, and we are are
2579 * still referencing it from db_data, we need to make a copy
2580 * of it in case we decide we want to dirty it again in this txg.
2582 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
2583 dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
2584 db
->db_state
== DB_CACHED
&& db
->db_data_pending
) {
2585 dbuf_dirty_record_t
*dr
= db
->db_data_pending
;
2587 if (dr
->dt
.dl
.dr_data
== db
->db_buf
) {
2588 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
2591 arc_alloc_buf(dn
->dn_objset
->os_spa
, db
, type
,
2593 bcopy(dr
->dt
.dl
.dr_data
->b_data
, db
->db
.db_data
,
2598 if (multilist_link_active(&db
->db_cache_link
)) {
2599 ASSERT(refcount_is_zero(&db
->db_holds
));
2600 multilist_remove(dbuf_cache
, db
);
2601 (void) refcount_remove_many(&dbuf_cache_size
,
2602 db
->db
.db_size
, db
);
2604 (void) refcount_add(&db
->db_holds
, tag
);
2606 mutex_exit(&db
->db_mtx
);
2608 /* NOTE: we can't rele the parent until after we drop the db_mtx */
2610 dbuf_rele(parent
, NULL
);
2612 ASSERT3P(DB_DNODE(db
), ==, dn
);
2613 ASSERT3U(db
->db_blkid
, ==, blkid
);
2614 ASSERT3U(db
->db_level
, ==, level
);
2621 dbuf_hold(dnode_t
*dn
, uint64_t blkid
, void *tag
)
2623 return (dbuf_hold_level(dn
, 0, blkid
, tag
));
2627 dbuf_hold_level(dnode_t
*dn
, int level
, uint64_t blkid
, void *tag
)
2630 int err
= dbuf_hold_impl(dn
, level
, blkid
, FALSE
, FALSE
, tag
, &db
);
2631 return (err
? NULL
: db
);
2635 dbuf_create_bonus(dnode_t
*dn
)
2637 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
2639 ASSERT(dn
->dn_bonus
== NULL
);
2640 dn
->dn_bonus
= dbuf_create(dn
, 0, DMU_BONUS_BLKID
, dn
->dn_dbuf
, NULL
);
2644 dbuf_spill_set_blksz(dmu_buf_t
*db_fake
, uint64_t blksz
, dmu_tx_t
*tx
)
2646 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2649 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
2650 return (SET_ERROR(ENOTSUP
));
2652 blksz
= SPA_MINBLOCKSIZE
;
2653 ASSERT3U(blksz
, <=, spa_maxblocksize(dmu_objset_spa(db
->db_objset
)));
2654 blksz
= P2ROUNDUP(blksz
, SPA_MINBLOCKSIZE
);
2658 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
2659 dbuf_new_size(db
, blksz
, tx
);
2660 rw_exit(&dn
->dn_struct_rwlock
);
2667 dbuf_rm_spill(dnode_t
*dn
, dmu_tx_t
*tx
)
2669 dbuf_free_range(dn
, DMU_SPILL_BLKID
, DMU_SPILL_BLKID
, tx
);
2672 #pragma weak dmu_buf_add_ref = dbuf_add_ref
2674 dbuf_add_ref(dmu_buf_impl_t
*db
, void *tag
)
2676 int64_t holds
= refcount_add(&db
->db_holds
, tag
);
2677 ASSERT3S(holds
, >, 1);
2680 #pragma weak dmu_buf_try_add_ref = dbuf_try_add_ref
2682 dbuf_try_add_ref(dmu_buf_t
*db_fake
, objset_t
*os
, uint64_t obj
, uint64_t blkid
,
2685 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2686 dmu_buf_impl_t
*found_db
;
2687 boolean_t result
= B_FALSE
;
2689 if (db
->db_blkid
== DMU_BONUS_BLKID
)
2690 found_db
= dbuf_find_bonus(os
, obj
);
2692 found_db
= dbuf_find(os
, obj
, 0, blkid
);
2694 if (found_db
!= NULL
) {
2695 if (db
== found_db
&& dbuf_refcount(db
) > db
->db_dirtycnt
) {
2696 (void) refcount_add(&db
->db_holds
, tag
);
2699 mutex_exit(&db
->db_mtx
);
2705 * If you call dbuf_rele() you had better not be referencing the dnode handle
2706 * unless you have some other direct or indirect hold on the dnode. (An indirect
2707 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
2708 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
2709 * dnode's parent dbuf evicting its dnode handles.
2712 dbuf_rele(dmu_buf_impl_t
*db
, void *tag
)
2714 mutex_enter(&db
->db_mtx
);
2715 dbuf_rele_and_unlock(db
, tag
);
2719 dmu_buf_rele(dmu_buf_t
*db
, void *tag
)
2721 dbuf_rele((dmu_buf_impl_t
*)db
, tag
);
2725 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
2726 * db_dirtycnt and db_holds to be updated atomically.
2729 dbuf_rele_and_unlock(dmu_buf_impl_t
*db
, void *tag
)
2733 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2737 * Remove the reference to the dbuf before removing its hold on the
2738 * dnode so we can guarantee in dnode_move() that a referenced bonus
2739 * buffer has a corresponding dnode hold.
2741 holds
= refcount_remove(&db
->db_holds
, tag
);
2745 * We can't freeze indirects if there is a possibility that they
2746 * may be modified in the current syncing context.
2748 if (db
->db_buf
!= NULL
&&
2749 holds
== (db
->db_level
== 0 ? db
->db_dirtycnt
: 0)) {
2750 arc_buf_freeze(db
->db_buf
);
2753 if (holds
== db
->db_dirtycnt
&&
2754 db
->db_level
== 0 && db
->db_user_immediate_evict
)
2755 dbuf_evict_user(db
);
2758 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2760 boolean_t evict_dbuf
= db
->db_pending_evict
;
2763 * If the dnode moves here, we cannot cross this
2764 * barrier until the move completes.
2769 atomic_dec_32(&dn
->dn_dbufs_count
);
2772 * Decrementing the dbuf count means that the bonus
2773 * buffer's dnode hold is no longer discounted in
2774 * dnode_move(). The dnode cannot move until after
2775 * the dnode_rele() below.
2780 * Do not reference db after its lock is dropped.
2781 * Another thread may evict it.
2783 mutex_exit(&db
->db_mtx
);
2786 dnode_evict_bonus(dn
);
2789 } else if (db
->db_buf
== NULL
) {
2791 * This is a special case: we never associated this
2792 * dbuf with any data allocated from the ARC.
2794 ASSERT(db
->db_state
== DB_UNCACHED
||
2795 db
->db_state
== DB_NOFILL
);
2797 } else if (arc_released(db
->db_buf
)) {
2799 * This dbuf has anonymous data associated with it.
2803 boolean_t do_arc_evict
= B_FALSE
;
2805 spa_t
*spa
= dmu_objset_spa(db
->db_objset
);
2807 if (!DBUF_IS_CACHEABLE(db
) &&
2808 db
->db_blkptr
!= NULL
&&
2809 !BP_IS_HOLE(db
->db_blkptr
) &&
2810 !BP_IS_EMBEDDED(db
->db_blkptr
)) {
2811 do_arc_evict
= B_TRUE
;
2812 bp
= *db
->db_blkptr
;
2815 if (!DBUF_IS_CACHEABLE(db
) ||
2816 db
->db_pending_evict
) {
2818 } else if (!multilist_link_active(&db
->db_cache_link
)) {
2819 multilist_insert(dbuf_cache
, db
);
2820 (void) refcount_add_many(&dbuf_cache_size
,
2821 db
->db
.db_size
, db
);
2822 mutex_exit(&db
->db_mtx
);
2824 dbuf_evict_notify();
2828 arc_freed(spa
, &bp
);
2831 mutex_exit(&db
->db_mtx
);
2836 #pragma weak dmu_buf_refcount = dbuf_refcount
2838 dbuf_refcount(dmu_buf_impl_t
*db
)
2840 return (refcount_count(&db
->db_holds
));
2844 dmu_buf_replace_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*old_user
,
2845 dmu_buf_user_t
*new_user
)
2847 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2849 mutex_enter(&db
->db_mtx
);
2850 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
2851 if (db
->db_user
== old_user
)
2852 db
->db_user
= new_user
;
2854 old_user
= db
->db_user
;
2855 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
2856 mutex_exit(&db
->db_mtx
);
2862 dmu_buf_set_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
2864 return (dmu_buf_replace_user(db_fake
, NULL
, user
));
2868 dmu_buf_set_user_ie(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
2870 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2872 db
->db_user_immediate_evict
= TRUE
;
2873 return (dmu_buf_set_user(db_fake
, user
));
2877 dmu_buf_remove_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
2879 return (dmu_buf_replace_user(db_fake
, user
, NULL
));
2883 dmu_buf_get_user(dmu_buf_t
*db_fake
)
2885 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2887 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
2888 return (db
->db_user
);
2892 dmu_buf_user_evict_wait()
2894 taskq_wait(dbu_evict_taskq
);
2898 dmu_buf_get_blkptr(dmu_buf_t
*db
)
2900 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
2901 return (dbi
->db_blkptr
);
2905 dmu_buf_get_objset(dmu_buf_t
*db
)
2907 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
2908 return (dbi
->db_objset
);
2912 dmu_buf_dnode_enter(dmu_buf_t
*db
)
2914 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
2915 DB_DNODE_ENTER(dbi
);
2916 return (DB_DNODE(dbi
));
2920 dmu_buf_dnode_exit(dmu_buf_t
*db
)
2922 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
2927 dbuf_check_blkptr(dnode_t
*dn
, dmu_buf_impl_t
*db
)
2929 /* ASSERT(dmu_tx_is_syncing(tx) */
2930 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2932 if (db
->db_blkptr
!= NULL
)
2935 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
2936 db
->db_blkptr
= &dn
->dn_phys
->dn_spill
;
2937 BP_ZERO(db
->db_blkptr
);
2940 if (db
->db_level
== dn
->dn_phys
->dn_nlevels
-1) {
2942 * This buffer was allocated at a time when there was
2943 * no available blkptrs from the dnode, or it was
2944 * inappropriate to hook it in (i.e., nlevels mis-match).
2946 ASSERT(db
->db_blkid
< dn
->dn_phys
->dn_nblkptr
);
2947 ASSERT(db
->db_parent
== NULL
);
2948 db
->db_parent
= dn
->dn_dbuf
;
2949 db
->db_blkptr
= &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
];
2952 dmu_buf_impl_t
*parent
= db
->db_parent
;
2953 int epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2955 ASSERT(dn
->dn_phys
->dn_nlevels
> 1);
2956 if (parent
== NULL
) {
2957 mutex_exit(&db
->db_mtx
);
2958 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
2959 parent
= dbuf_hold_level(dn
, db
->db_level
+ 1,
2960 db
->db_blkid
>> epbs
, db
);
2961 rw_exit(&dn
->dn_struct_rwlock
);
2962 mutex_enter(&db
->db_mtx
);
2963 db
->db_parent
= parent
;
2965 db
->db_blkptr
= (blkptr_t
*)parent
->db
.db_data
+
2966 (db
->db_blkid
& ((1ULL << epbs
) - 1));
2972 dbuf_sync_indirect(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
2974 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2978 ASSERT(dmu_tx_is_syncing(tx
));
2980 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
2982 mutex_enter(&db
->db_mtx
);
2984 ASSERT(db
->db_level
> 0);
2987 /* Read the block if it hasn't been read yet. */
2988 if (db
->db_buf
== NULL
) {
2989 mutex_exit(&db
->db_mtx
);
2990 (void) dbuf_read(db
, NULL
, DB_RF_MUST_SUCCEED
);
2991 mutex_enter(&db
->db_mtx
);
2993 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
2994 ASSERT(db
->db_buf
!= NULL
);
2998 /* Indirect block size must match what the dnode thinks it is. */
2999 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
3000 dbuf_check_blkptr(dn
, db
);
3003 /* Provide the pending dirty record to child dbufs */
3004 db
->db_data_pending
= dr
;
3006 mutex_exit(&db
->db_mtx
);
3008 dbuf_write(dr
, db
->db_buf
, tx
);
3011 mutex_enter(&dr
->dt
.di
.dr_mtx
);
3012 dbuf_sync_list(&dr
->dt
.di
.dr_children
, db
->db_level
- 1, tx
);
3013 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
3014 mutex_exit(&dr
->dt
.di
.dr_mtx
);
3019 dbuf_sync_leaf(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
3021 arc_buf_t
**datap
= &dr
->dt
.dl
.dr_data
;
3022 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3025 uint64_t txg
= tx
->tx_txg
;
3027 ASSERT(dmu_tx_is_syncing(tx
));
3029 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
3031 mutex_enter(&db
->db_mtx
);
3033 * To be synced, we must be dirtied. But we
3034 * might have been freed after the dirty.
3036 if (db
->db_state
== DB_UNCACHED
) {
3037 /* This buffer has been freed since it was dirtied */
3038 ASSERT(db
->db
.db_data
== NULL
);
3039 } else if (db
->db_state
== DB_FILL
) {
3040 /* This buffer was freed and is now being re-filled */
3041 ASSERT(db
->db
.db_data
!= dr
->dt
.dl
.dr_data
);
3043 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_NOFILL
);
3050 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3051 mutex_enter(&dn
->dn_mtx
);
3052 dn
->dn_phys
->dn_flags
|= DNODE_FLAG_SPILL_BLKPTR
;
3053 mutex_exit(&dn
->dn_mtx
);
3057 * If this is a bonus buffer, simply copy the bonus data into the
3058 * dnode. It will be written out when the dnode is synced (and it
3059 * will be synced, since it must have been dirty for dbuf_sync to
3062 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
3063 dbuf_dirty_record_t
**drp
;
3065 ASSERT(*datap
!= NULL
);
3066 ASSERT0(db
->db_level
);
3067 ASSERT3U(dn
->dn_phys
->dn_bonuslen
, <=, DN_MAX_BONUSLEN
);
3068 bcopy(*datap
, DN_BONUS(dn
->dn_phys
), dn
->dn_phys
->dn_bonuslen
);
3071 if (*datap
!= db
->db
.db_data
) {
3072 zio_buf_free(*datap
, DN_MAX_BONUSLEN
);
3073 arc_space_return(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
3075 db
->db_data_pending
= NULL
;
3076 drp
= &db
->db_last_dirty
;
3078 drp
= &(*drp
)->dr_next
;
3079 ASSERT(dr
->dr_next
== NULL
);
3080 ASSERT(dr
->dr_dbuf
== db
);
3082 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
3083 ASSERT(db
->db_dirtycnt
> 0);
3084 db
->db_dirtycnt
-= 1;
3085 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)txg
);
3092 * This function may have dropped the db_mtx lock allowing a dmu_sync
3093 * operation to sneak in. As a result, we need to ensure that we
3094 * don't check the dr_override_state until we have returned from
3095 * dbuf_check_blkptr.
3097 dbuf_check_blkptr(dn
, db
);
3100 * If this buffer is in the middle of an immediate write,
3101 * wait for the synchronous IO to complete.
3103 while (dr
->dt
.dl
.dr_override_state
== DR_IN_DMU_SYNC
) {
3104 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
3105 cv_wait(&db
->db_changed
, &db
->db_mtx
);
3106 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_NOT_OVERRIDDEN
);
3109 if (db
->db_state
!= DB_NOFILL
&&
3110 dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
3111 refcount_count(&db
->db_holds
) > 1 &&
3112 dr
->dt
.dl
.dr_override_state
!= DR_OVERRIDDEN
&&
3113 *datap
== db
->db_buf
) {
3115 * If this buffer is currently "in use" (i.e., there
3116 * are active holds and db_data still references it),
3117 * then make a copy before we start the write so that
3118 * any modifications from the open txg will not leak
3121 * NOTE: this copy does not need to be made for
3122 * objects only modified in the syncing context (e.g.
3123 * DNONE_DNODE blocks).
3125 int psize
= arc_buf_size(*datap
);
3126 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
3127 enum zio_compress compress_type
= arc_get_compression(*datap
);
3129 if (compress_type
== ZIO_COMPRESS_OFF
) {
3130 *datap
= arc_alloc_buf(os
->os_spa
, db
, type
, psize
);
3132 ASSERT3U(type
, ==, ARC_BUFC_DATA
);
3133 int lsize
= arc_buf_lsize(*datap
);
3134 *datap
= arc_alloc_compressed_buf(os
->os_spa
, db
,
3135 psize
, lsize
, compress_type
);
3137 bcopy(db
->db
.db_data
, (*datap
)->b_data
, psize
);
3139 db
->db_data_pending
= dr
;
3141 mutex_exit(&db
->db_mtx
);
3143 dbuf_write(dr
, *datap
, tx
);
3145 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
3146 if (dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
3147 list_insert_tail(&dn
->dn_dirty_records
[txg
&TXG_MASK
], dr
);
3151 * Although zio_nowait() does not "wait for an IO", it does
3152 * initiate the IO. If this is an empty write it seems plausible
3153 * that the IO could actually be completed before the nowait
3154 * returns. We need to DB_DNODE_EXIT() first in case
3155 * zio_nowait() invalidates the dbuf.
3158 zio_nowait(dr
->dr_zio
);
3163 dbuf_sync_list(list_t
*list
, int level
, dmu_tx_t
*tx
)
3165 dbuf_dirty_record_t
*dr
;
3167 while (dr
= list_head(list
)) {
3168 if (dr
->dr_zio
!= NULL
) {
3170 * If we find an already initialized zio then we
3171 * are processing the meta-dnode, and we have finished.
3172 * The dbufs for all dnodes are put back on the list
3173 * during processing, so that we can zio_wait()
3174 * these IOs after initiating all child IOs.
3176 ASSERT3U(dr
->dr_dbuf
->db
.db_object
, ==,
3177 DMU_META_DNODE_OBJECT
);
3180 if (dr
->dr_dbuf
->db_blkid
!= DMU_BONUS_BLKID
&&
3181 dr
->dr_dbuf
->db_blkid
!= DMU_SPILL_BLKID
) {
3182 VERIFY3U(dr
->dr_dbuf
->db_level
, ==, level
);
3184 list_remove(list
, dr
);
3185 if (dr
->dr_dbuf
->db_level
> 0)
3186 dbuf_sync_indirect(dr
, tx
);
3188 dbuf_sync_leaf(dr
, tx
);
3194 dbuf_write_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
3196 dmu_buf_impl_t
*db
= vdb
;
3198 blkptr_t
*bp
= zio
->io_bp
;
3199 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
3200 spa_t
*spa
= zio
->io_spa
;
3205 ASSERT3P(db
->db_blkptr
, !=, NULL
);
3206 ASSERT3P(&db
->db_data_pending
->dr_bp_copy
, ==, bp
);
3210 delta
= bp_get_dsize_sync(spa
, bp
) - bp_get_dsize_sync(spa
, bp_orig
);
3211 dnode_diduse_space(dn
, delta
- zio
->io_prev_space_delta
);
3212 zio
->io_prev_space_delta
= delta
;
3214 if (bp
->blk_birth
!= 0) {
3215 ASSERT((db
->db_blkid
!= DMU_SPILL_BLKID
&&
3216 BP_GET_TYPE(bp
) == dn
->dn_type
) ||
3217 (db
->db_blkid
== DMU_SPILL_BLKID
&&
3218 BP_GET_TYPE(bp
) == dn
->dn_bonustype
) ||
3219 BP_IS_EMBEDDED(bp
));
3220 ASSERT(BP_GET_LEVEL(bp
) == db
->db_level
);
3223 mutex_enter(&db
->db_mtx
);
3226 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3227 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
3228 ASSERT(!(BP_IS_HOLE(bp
)) &&
3229 db
->db_blkptr
== &dn
->dn_phys
->dn_spill
);
3233 if (db
->db_level
== 0) {
3234 mutex_enter(&dn
->dn_mtx
);
3235 if (db
->db_blkid
> dn
->dn_phys
->dn_maxblkid
&&
3236 db
->db_blkid
!= DMU_SPILL_BLKID
)
3237 dn
->dn_phys
->dn_maxblkid
= db
->db_blkid
;
3238 mutex_exit(&dn
->dn_mtx
);
3240 if (dn
->dn_type
== DMU_OT_DNODE
) {
3241 dnode_phys_t
*dnp
= db
->db
.db_data
;
3242 for (i
= db
->db
.db_size
>> DNODE_SHIFT
; i
> 0;
3244 if (dnp
->dn_type
!= DMU_OT_NONE
)
3248 if (BP_IS_HOLE(bp
)) {
3255 blkptr_t
*ibp
= db
->db
.db_data
;
3256 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
3257 for (i
= db
->db
.db_size
>> SPA_BLKPTRSHIFT
; i
> 0; i
--, ibp
++) {
3258 if (BP_IS_HOLE(ibp
))
3260 fill
+= BP_GET_FILL(ibp
);
3265 if (!BP_IS_EMBEDDED(bp
))
3266 bp
->blk_fill
= fill
;
3268 mutex_exit(&db
->db_mtx
);
3270 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
3271 *db
->db_blkptr
= *bp
;
3272 rw_exit(&dn
->dn_struct_rwlock
);
3277 * This function gets called just prior to running through the compression
3278 * stage of the zio pipeline. If we're an indirect block comprised of only
3279 * holes, then we want this indirect to be compressed away to a hole. In
3280 * order to do that we must zero out any information about the holes that
3281 * this indirect points to prior to before we try to compress it.
3284 dbuf_write_children_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
3286 dmu_buf_impl_t
*db
= vdb
;
3289 unsigned int epbs
, i
;
3291 ASSERT3U(db
->db_level
, >, 0);
3294 epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
3295 ASSERT3U(epbs
, <, 31);
3297 /* Determine if all our children are holes */
3298 for (i
= 0, bp
= db
->db
.db_data
; i
< 1 << epbs
; i
++, bp
++) {
3299 if (!BP_IS_HOLE(bp
))
3304 * If all the children are holes, then zero them all out so that
3305 * we may get compressed away.
3307 if (i
== 1 << epbs
) {
3309 * We only found holes. Grab the rwlock to prevent
3310 * anybody from reading the blocks we're about to
3313 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
3314 bzero(db
->db
.db_data
, db
->db
.db_size
);
3315 rw_exit(&dn
->dn_struct_rwlock
);
3321 * The SPA will call this callback several times for each zio - once
3322 * for every physical child i/o (zio->io_phys_children times). This
3323 * allows the DMU to monitor the progress of each logical i/o. For example,
3324 * there may be 2 copies of an indirect block, or many fragments of a RAID-Z
3325 * block. There may be a long delay before all copies/fragments are completed,
3326 * so this callback allows us to retire dirty space gradually, as the physical
3331 dbuf_write_physdone(zio_t
*zio
, arc_buf_t
*buf
, void *arg
)
3333 dmu_buf_impl_t
*db
= arg
;
3334 objset_t
*os
= db
->db_objset
;
3335 dsl_pool_t
*dp
= dmu_objset_pool(os
);
3336 dbuf_dirty_record_t
*dr
;
3339 dr
= db
->db_data_pending
;
3340 ASSERT3U(dr
->dr_txg
, ==, zio
->io_txg
);
3343 * The callback will be called io_phys_children times. Retire one
3344 * portion of our dirty space each time we are called. Any rounding
3345 * error will be cleaned up by dsl_pool_sync()'s call to
3346 * dsl_pool_undirty_space().
3348 delta
= dr
->dr_accounted
/ zio
->io_phys_children
;
3349 dsl_pool_undirty_space(dp
, delta
, zio
->io_txg
);
3354 dbuf_write_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
3356 dmu_buf_impl_t
*db
= vdb
;
3357 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
3358 blkptr_t
*bp
= db
->db_blkptr
;
3359 objset_t
*os
= db
->db_objset
;
3360 dmu_tx_t
*tx
= os
->os_synctx
;
3361 dbuf_dirty_record_t
**drp
, *dr
;
3363 ASSERT0(zio
->io_error
);
3364 ASSERT(db
->db_blkptr
== bp
);
3367 * For nopwrites and rewrites we ensure that the bp matches our
3368 * original and bypass all the accounting.
3370 if (zio
->io_flags
& (ZIO_FLAG_IO_REWRITE
| ZIO_FLAG_NOPWRITE
)) {
3371 ASSERT(BP_EQUAL(bp
, bp_orig
));
3373 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
3374 (void) dsl_dataset_block_kill(ds
, bp_orig
, tx
, B_TRUE
);
3375 dsl_dataset_block_born(ds
, bp
, tx
);
3378 mutex_enter(&db
->db_mtx
);
3382 drp
= &db
->db_last_dirty
;
3383 while ((dr
= *drp
) != db
->db_data_pending
)
3385 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
3386 ASSERT(dr
->dr_dbuf
== db
);
3387 ASSERT(dr
->dr_next
== NULL
);
3391 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3396 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
3397 ASSERT(!(BP_IS_HOLE(db
->db_blkptr
)) &&
3398 db
->db_blkptr
== &dn
->dn_phys
->dn_spill
);
3403 if (db
->db_level
== 0) {
3404 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
3405 ASSERT(dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
);
3406 if (db
->db_state
!= DB_NOFILL
) {
3407 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
3408 arc_buf_destroy(dr
->dt
.dl
.dr_data
, db
);
3415 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
3416 ASSERT3U(db
->db
.db_size
, ==, 1 << dn
->dn_phys
->dn_indblkshift
);
3417 if (!BP_IS_HOLE(db
->db_blkptr
)) {
3419 dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
3420 ASSERT3U(db
->db_blkid
, <=,
3421 dn
->dn_phys
->dn_maxblkid
>> (db
->db_level
* epbs
));
3422 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==,
3426 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
3427 list_destroy(&dr
->dt
.di
.dr_children
);
3429 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
3431 cv_broadcast(&db
->db_changed
);
3432 ASSERT(db
->db_dirtycnt
> 0);
3433 db
->db_dirtycnt
-= 1;
3434 db
->db_data_pending
= NULL
;
3435 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)tx
->tx_txg
);
3439 dbuf_write_nofill_ready(zio_t
*zio
)
3441 dbuf_write_ready(zio
, NULL
, zio
->io_private
);
3445 dbuf_write_nofill_done(zio_t
*zio
)
3447 dbuf_write_done(zio
, NULL
, zio
->io_private
);
3451 dbuf_write_override_ready(zio_t
*zio
)
3453 dbuf_dirty_record_t
*dr
= zio
->io_private
;
3454 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3456 dbuf_write_ready(zio
, NULL
, db
);
3460 dbuf_write_override_done(zio_t
*zio
)
3462 dbuf_dirty_record_t
*dr
= zio
->io_private
;
3463 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3464 blkptr_t
*obp
= &dr
->dt
.dl
.dr_overridden_by
;
3466 mutex_enter(&db
->db_mtx
);
3467 if (!BP_EQUAL(zio
->io_bp
, obp
)) {
3468 if (!BP_IS_HOLE(obp
))
3469 dsl_free(spa_get_dsl(zio
->io_spa
), zio
->io_txg
, obp
);
3470 arc_release(dr
->dt
.dl
.dr_data
, db
);
3472 mutex_exit(&db
->db_mtx
);
3473 dbuf_write_done(zio
, NULL
, db
);
3475 if (zio
->io_abd
!= NULL
)
3476 abd_put(zio
->io_abd
);
3479 typedef struct dbuf_remap_impl_callback_arg
{
3481 uint64_t drica_blk_birth
;
3483 } dbuf_remap_impl_callback_arg_t
;
3486 dbuf_remap_impl_callback(uint64_t vdev
, uint64_t offset
, uint64_t size
,
3489 dbuf_remap_impl_callback_arg_t
*drica
= arg
;
3490 objset_t
*os
= drica
->drica_os
;
3491 spa_t
*spa
= dmu_objset_spa(os
);
3492 dmu_tx_t
*tx
= drica
->drica_tx
;
3494 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa
)));
3496 if (os
== spa_meta_objset(spa
)) {
3497 spa_vdev_indirect_mark_obsolete(spa
, vdev
, offset
, size
, tx
);
3499 dsl_dataset_block_remapped(dmu_objset_ds(os
), vdev
, offset
,
3500 size
, drica
->drica_blk_birth
, tx
);
3505 dbuf_remap_impl(dnode_t
*dn
, blkptr_t
*bp
, dmu_tx_t
*tx
)
3507 blkptr_t bp_copy
= *bp
;
3508 spa_t
*spa
= dmu_objset_spa(dn
->dn_objset
);
3509 dbuf_remap_impl_callback_arg_t drica
;
3511 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa
)));
3513 drica
.drica_os
= dn
->dn_objset
;
3514 drica
.drica_blk_birth
= bp
->blk_birth
;
3515 drica
.drica_tx
= tx
;
3516 if (spa_remap_blkptr(spa
, &bp_copy
, dbuf_remap_impl_callback
,
3519 * The struct_rwlock prevents dbuf_read_impl() from
3520 * dereferencing the BP while we are changing it. To
3521 * avoid lock contention, only grab it when we are actually
3524 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
3526 rw_exit(&dn
->dn_struct_rwlock
);
3531 * Returns true if a dbuf_remap would modify the dbuf. We do this by attempting
3532 * to remap a copy of every bp in the dbuf.
3535 dbuf_can_remap(const dmu_buf_impl_t
*db
)
3537 spa_t
*spa
= dmu_objset_spa(db
->db_objset
);
3538 blkptr_t
*bp
= db
->db
.db_data
;
3539 boolean_t ret
= B_FALSE
;
3541 ASSERT3U(db
->db_level
, >, 0);
3542 ASSERT3S(db
->db_state
, ==, DB_CACHED
);
3544 ASSERT(spa_feature_is_active(spa
, SPA_FEATURE_DEVICE_REMOVAL
));
3546 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
3547 for (int i
= 0; i
< db
->db
.db_size
>> SPA_BLKPTRSHIFT
; i
++) {
3548 blkptr_t bp_copy
= bp
[i
];
3549 if (spa_remap_blkptr(spa
, &bp_copy
, NULL
, NULL
)) {
3554 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3560 dnode_needs_remap(const dnode_t
*dn
)
3562 spa_t
*spa
= dmu_objset_spa(dn
->dn_objset
);
3563 boolean_t ret
= B_FALSE
;
3565 if (dn
->dn_phys
->dn_nlevels
== 0) {
3569 ASSERT(spa_feature_is_active(spa
, SPA_FEATURE_DEVICE_REMOVAL
));
3571 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
3572 for (int j
= 0; j
< dn
->dn_phys
->dn_nblkptr
; j
++) {
3573 blkptr_t bp_copy
= dn
->dn_phys
->dn_blkptr
[j
];
3574 if (spa_remap_blkptr(spa
, &bp_copy
, NULL
, NULL
)) {
3579 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
3585 * Remap any existing BP's to concrete vdevs, if possible.
3588 dbuf_remap(dnode_t
*dn
, dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
3590 spa_t
*spa
= dmu_objset_spa(db
->db_objset
);
3591 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa
)));
3593 if (!spa_feature_is_active(spa
, SPA_FEATURE_DEVICE_REMOVAL
))
3596 if (db
->db_level
> 0) {
3597 blkptr_t
*bp
= db
->db
.db_data
;
3598 for (int i
= 0; i
< db
->db
.db_size
>> SPA_BLKPTRSHIFT
; i
++) {
3599 dbuf_remap_impl(dn
, &bp
[i
], tx
);
3601 } else if (db
->db
.db_object
== DMU_META_DNODE_OBJECT
) {
3602 dnode_phys_t
*dnp
= db
->db
.db_data
;
3603 ASSERT3U(db
->db_dnode_handle
->dnh_dnode
->dn_type
, ==,
3605 for (int i
= 0; i
< db
->db
.db_size
>> DNODE_SHIFT
; i
++) {
3606 for (int j
= 0; j
< dnp
[i
].dn_nblkptr
; j
++) {
3607 dbuf_remap_impl(dn
, &dnp
[i
].dn_blkptr
[j
], tx
);
3614 /* Issue I/O to commit a dirty buffer to disk. */
3616 dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
)
3618 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3621 dmu_buf_impl_t
*parent
= db
->db_parent
;
3622 uint64_t txg
= tx
->tx_txg
;
3623 zbookmark_phys_t zb
;
3628 ASSERT(dmu_tx_is_syncing(tx
));
3634 if (db
->db_state
!= DB_NOFILL
) {
3635 if (db
->db_level
> 0 || dn
->dn_type
== DMU_OT_DNODE
) {
3637 * Private object buffers are released here rather
3638 * than in dbuf_dirty() since they are only modified
3639 * in the syncing context and we don't want the
3640 * overhead of making multiple copies of the data.
3642 if (BP_IS_HOLE(db
->db_blkptr
)) {
3645 dbuf_release_bp(db
);
3647 dbuf_remap(dn
, db
, tx
);
3651 if (parent
!= dn
->dn_dbuf
) {
3652 /* Our parent is an indirect block. */
3653 /* We have a dirty parent that has been scheduled for write. */
3654 ASSERT(parent
&& parent
->db_data_pending
);
3655 /* Our parent's buffer is one level closer to the dnode. */
3656 ASSERT(db
->db_level
== parent
->db_level
-1);
3658 * We're about to modify our parent's db_data by modifying
3659 * our block pointer, so the parent must be released.
3661 ASSERT(arc_released(parent
->db_buf
));
3662 zio
= parent
->db_data_pending
->dr_zio
;
3664 /* Our parent is the dnode itself. */
3665 ASSERT((db
->db_level
== dn
->dn_phys
->dn_nlevels
-1 &&
3666 db
->db_blkid
!= DMU_SPILL_BLKID
) ||
3667 (db
->db_blkid
== DMU_SPILL_BLKID
&& db
->db_level
== 0));
3668 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
3669 ASSERT3P(db
->db_blkptr
, ==,
3670 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
3674 ASSERT(db
->db_level
== 0 || data
== db
->db_buf
);
3675 ASSERT3U(db
->db_blkptr
->blk_birth
, <=, txg
);
3678 SET_BOOKMARK(&zb
, os
->os_dsl_dataset
?
3679 os
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
3680 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
3682 if (db
->db_blkid
== DMU_SPILL_BLKID
)
3684 wp_flag
|= (db
->db_state
== DB_NOFILL
) ? WP_NOFILL
: 0;
3686 dmu_write_policy(os
, dn
, db
->db_level
, wp_flag
, &zp
);
3690 * We copy the blkptr now (rather than when we instantiate the dirty
3691 * record), because its value can change between open context and
3692 * syncing context. We do not need to hold dn_struct_rwlock to read
3693 * db_blkptr because we are in syncing context.
3695 dr
->dr_bp_copy
= *db
->db_blkptr
;
3697 if (db
->db_level
== 0 &&
3698 dr
->dt
.dl
.dr_override_state
== DR_OVERRIDDEN
) {
3700 * The BP for this block has been provided by open context
3701 * (by dmu_sync() or dmu_buf_write_embedded()).
3703 abd_t
*contents
= (data
!= NULL
) ?
3704 abd_get_from_buf(data
->b_data
, arc_buf_size(data
)) : NULL
;
3706 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
, &dr
->dr_bp_copy
,
3707 contents
, db
->db
.db_size
, db
->db
.db_size
, &zp
,
3708 dbuf_write_override_ready
, NULL
, NULL
,
3709 dbuf_write_override_done
,
3710 dr
, ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
3711 mutex_enter(&db
->db_mtx
);
3712 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
3713 zio_write_override(dr
->dr_zio
, &dr
->dt
.dl
.dr_overridden_by
,
3714 dr
->dt
.dl
.dr_copies
, dr
->dt
.dl
.dr_nopwrite
);
3715 mutex_exit(&db
->db_mtx
);
3716 } else if (db
->db_state
== DB_NOFILL
) {
3717 ASSERT(zp
.zp_checksum
== ZIO_CHECKSUM_OFF
||
3718 zp
.zp_checksum
== ZIO_CHECKSUM_NOPARITY
);
3719 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
,
3720 &dr
->dr_bp_copy
, NULL
, db
->db
.db_size
, db
->db
.db_size
, &zp
,
3721 dbuf_write_nofill_ready
, NULL
, NULL
,
3722 dbuf_write_nofill_done
, db
,
3723 ZIO_PRIORITY_ASYNC_WRITE
,
3724 ZIO_FLAG_MUSTSUCCEED
| ZIO_FLAG_NODATA
, &zb
);
3726 ASSERT(arc_released(data
));
3729 * For indirect blocks, we want to setup the children
3730 * ready callback so that we can properly handle an indirect
3731 * block that only contains holes.
3733 arc_done_func_t
*children_ready_cb
= NULL
;
3734 if (db
->db_level
!= 0)
3735 children_ready_cb
= dbuf_write_children_ready
;
3737 dr
->dr_zio
= arc_write(zio
, os
->os_spa
, txg
,
3738 &dr
->dr_bp_copy
, data
, DBUF_IS_L2CACHEABLE(db
),
3739 &zp
, dbuf_write_ready
, children_ready_cb
,
3740 dbuf_write_physdone
, dbuf_write_done
, db
,
3741 ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);