4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
25 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
26 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
29 #include <sys/zfs_context.h>
32 #include <sys/dmu_send.h>
33 #include <sys/dmu_impl.h>
35 #include <sys/dmu_objset.h>
36 #include <sys/dsl_dataset.h>
37 #include <sys/dsl_dir.h>
38 #include <sys/dmu_tx.h>
41 #include <sys/dmu_zfetch.h>
43 #include <sys/sa_impl.h>
44 #include <sys/zfeature.h>
45 #include <sys/blkptr.h>
46 #include <sys/range_tree.h>
47 #include <sys/trace_dbuf.h>
48 #include <sys/callb.h>
51 struct dbuf_hold_impl_data
{
52 /* Function arguments */
56 boolean_t dh_fail_sparse
;
57 boolean_t dh_fail_uncached
;
59 dmu_buf_impl_t
**dh_dbp
;
61 dmu_buf_impl_t
*dh_db
;
62 dmu_buf_impl_t
*dh_parent
;
65 dbuf_dirty_record_t
*dh_dr
;
69 static void __dbuf_hold_impl_init(struct dbuf_hold_impl_data
*dh
,
70 dnode_t
*dn
, uint8_t level
, uint64_t blkid
, boolean_t fail_sparse
,
71 boolean_t fail_uncached
,
72 void *tag
, dmu_buf_impl_t
**dbp
, int depth
);
73 static int __dbuf_hold_impl(struct dbuf_hold_impl_data
*dh
);
75 uint_t zfs_dbuf_evict_key
;
77 static boolean_t
dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
);
78 static void dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
);
80 extern inline void dmu_buf_init_user(dmu_buf_user_t
*dbu
,
81 dmu_buf_evict_func_t
*evict_func_sync
,
82 dmu_buf_evict_func_t
*evict_func_async
,
83 dmu_buf_t
**clear_on_evict_dbufp
);
86 * Global data structures and functions for the dbuf cache.
88 static kmem_cache_t
*dbuf_kmem_cache
;
89 static taskq_t
*dbu_evict_taskq
;
91 static kthread_t
*dbuf_cache_evict_thread
;
92 static kmutex_t dbuf_evict_lock
;
93 static kcondvar_t dbuf_evict_cv
;
94 static boolean_t dbuf_evict_thread_exit
;
97 * LRU cache of dbufs. The dbuf cache maintains a list of dbufs that
98 * are not currently held but have been recently released. These dbufs
99 * are not eligible for arc eviction until they are aged out of the cache.
100 * Dbufs are added to the dbuf cache once the last hold is released. If a
101 * dbuf is later accessed and still exists in the dbuf cache, then it will
102 * be removed from the cache and later re-added to the head of the cache.
103 * Dbufs that are aged out of the cache will be immediately destroyed and
104 * become eligible for arc eviction.
106 static multilist_t
*dbuf_cache
;
107 static refcount_t dbuf_cache_size
;
108 unsigned long dbuf_cache_max_bytes
= 100 * 1024 * 1024;
110 /* Cap the size of the dbuf cache to log2 fraction of arc size. */
111 int dbuf_cache_max_shift
= 5;
114 * The dbuf cache uses a three-stage eviction policy:
115 * - A low water marker designates when the dbuf eviction thread
116 * should stop evicting from the dbuf cache.
117 * - When we reach the maximum size (aka mid water mark), we
118 * signal the eviction thread to run.
119 * - The high water mark indicates when the eviction thread
120 * is unable to keep up with the incoming load and eviction must
121 * happen in the context of the calling thread.
125 * low water mid water hi water
126 * +----------------------------------------+----------+----------+
131 * +----------------------------------------+----------+----------+
133 * evicting eviction directly
136 * The high and low water marks indicate the operating range for the eviction
137 * thread. The low water mark is, by default, 90% of the total size of the
138 * cache and the high water mark is at 110% (both of these percentages can be
139 * changed by setting dbuf_cache_lowater_pct and dbuf_cache_hiwater_pct,
140 * respectively). The eviction thread will try to ensure that the cache remains
141 * within this range by waking up every second and checking if the cache is
142 * above the low water mark. The thread can also be woken up by callers adding
143 * elements into the cache if the cache is larger than the mid water (i.e max
144 * cache size). Once the eviction thread is woken up and eviction is required,
145 * it will continue evicting buffers until it's able to reduce the cache size
146 * to the low water mark. If the cache size continues to grow and hits the high
147 * water mark, then callers adding elements to the cache will begin to evict
148 * directly from the cache until the cache is no longer above the high water
153 * The percentage above and below the maximum cache size.
155 uint_t dbuf_cache_hiwater_pct
= 10;
156 uint_t dbuf_cache_lowater_pct
= 10;
160 dbuf_cons(void *vdb
, void *unused
, int kmflag
)
162 dmu_buf_impl_t
*db
= vdb
;
163 bzero(db
, sizeof (dmu_buf_impl_t
));
165 mutex_init(&db
->db_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
166 cv_init(&db
->db_changed
, NULL
, CV_DEFAULT
, NULL
);
167 multilist_link_init(&db
->db_cache_link
);
168 refcount_create(&db
->db_holds
);
169 multilist_link_init(&db
->db_cache_link
);
176 dbuf_dest(void *vdb
, void *unused
)
178 dmu_buf_impl_t
*db
= vdb
;
179 mutex_destroy(&db
->db_mtx
);
180 cv_destroy(&db
->db_changed
);
181 ASSERT(!multilist_link_active(&db
->db_cache_link
));
182 refcount_destroy(&db
->db_holds
);
186 * dbuf hash table routines
188 static dbuf_hash_table_t dbuf_hash_table
;
190 static uint64_t dbuf_hash_count
;
193 dbuf_hash(void *os
, uint64_t obj
, uint8_t lvl
, uint64_t blkid
)
195 uintptr_t osv
= (uintptr_t)os
;
196 uint64_t crc
= -1ULL;
198 ASSERT(zfs_crc64_table
[128] == ZFS_CRC64_POLY
);
199 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (lvl
)) & 0xFF];
200 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (osv
>> 6)) & 0xFF];
201 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 0)) & 0xFF];
202 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 8)) & 0xFF];
203 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (blkid
>> 0)) & 0xFF];
204 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (blkid
>> 8)) & 0xFF];
206 crc
^= (osv
>>14) ^ (obj
>>16) ^ (blkid
>>16);
211 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
212 ((dbuf)->db.db_object == (obj) && \
213 (dbuf)->db_objset == (os) && \
214 (dbuf)->db_level == (level) && \
215 (dbuf)->db_blkid == (blkid))
218 dbuf_find(objset_t
*os
, uint64_t obj
, uint8_t level
, uint64_t blkid
)
220 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
225 hv
= dbuf_hash(os
, obj
, level
, blkid
);
226 idx
= hv
& h
->hash_table_mask
;
228 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
229 for (db
= h
->hash_table
[idx
]; db
!= NULL
; db
= db
->db_hash_next
) {
230 if (DBUF_EQUAL(db
, os
, obj
, level
, blkid
)) {
231 mutex_enter(&db
->db_mtx
);
232 if (db
->db_state
!= DB_EVICTING
) {
233 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
236 mutex_exit(&db
->db_mtx
);
239 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
243 static dmu_buf_impl_t
*
244 dbuf_find_bonus(objset_t
*os
, uint64_t object
)
247 dmu_buf_impl_t
*db
= NULL
;
249 if (dnode_hold(os
, object
, FTAG
, &dn
) == 0) {
250 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
251 if (dn
->dn_bonus
!= NULL
) {
253 mutex_enter(&db
->db_mtx
);
255 rw_exit(&dn
->dn_struct_rwlock
);
256 dnode_rele(dn
, FTAG
);
262 * Insert an entry into the hash table. If there is already an element
263 * equal to elem in the hash table, then the already existing element
264 * will be returned and the new element will not be inserted.
265 * Otherwise returns NULL.
267 static dmu_buf_impl_t
*
268 dbuf_hash_insert(dmu_buf_impl_t
*db
)
270 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
271 objset_t
*os
= db
->db_objset
;
272 uint64_t obj
= db
->db
.db_object
;
273 int level
= db
->db_level
;
274 uint64_t blkid
, hv
, idx
;
277 blkid
= db
->db_blkid
;
278 hv
= dbuf_hash(os
, obj
, level
, blkid
);
279 idx
= hv
& h
->hash_table_mask
;
281 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
282 for (dbf
= h
->hash_table
[idx
]; dbf
!= NULL
; dbf
= dbf
->db_hash_next
) {
283 if (DBUF_EQUAL(dbf
, os
, obj
, level
, blkid
)) {
284 mutex_enter(&dbf
->db_mtx
);
285 if (dbf
->db_state
!= DB_EVICTING
) {
286 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
289 mutex_exit(&dbf
->db_mtx
);
293 mutex_enter(&db
->db_mtx
);
294 db
->db_hash_next
= h
->hash_table
[idx
];
295 h
->hash_table
[idx
] = db
;
296 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
297 atomic_inc_64(&dbuf_hash_count
);
303 * Remove an entry from the hash table. It must be in the EVICTING state.
306 dbuf_hash_remove(dmu_buf_impl_t
*db
)
308 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
310 dmu_buf_impl_t
*dbf
, **dbp
;
312 hv
= dbuf_hash(db
->db_objset
, db
->db
.db_object
,
313 db
->db_level
, db
->db_blkid
);
314 idx
= hv
& h
->hash_table_mask
;
317 * We mustn't hold db_mtx to maintain lock ordering:
318 * DBUF_HASH_MUTEX > db_mtx.
320 ASSERT(refcount_is_zero(&db
->db_holds
));
321 ASSERT(db
->db_state
== DB_EVICTING
);
322 ASSERT(!MUTEX_HELD(&db
->db_mtx
));
324 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
325 dbp
= &h
->hash_table
[idx
];
326 while ((dbf
= *dbp
) != db
) {
327 dbp
= &dbf
->db_hash_next
;
330 *dbp
= db
->db_hash_next
;
331 db
->db_hash_next
= NULL
;
332 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
333 atomic_dec_64(&dbuf_hash_count
);
339 } dbvu_verify_type_t
;
342 dbuf_verify_user(dmu_buf_impl_t
*db
, dbvu_verify_type_t verify_type
)
347 if (db
->db_user
== NULL
)
350 /* Only data blocks support the attachment of user data. */
351 ASSERT(db
->db_level
== 0);
353 /* Clients must resolve a dbuf before attaching user data. */
354 ASSERT(db
->db
.db_data
!= NULL
);
355 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
357 holds
= refcount_count(&db
->db_holds
);
358 if (verify_type
== DBVU_EVICTING
) {
360 * Immediate eviction occurs when holds == dirtycnt.
361 * For normal eviction buffers, holds is zero on
362 * eviction, except when dbuf_fix_old_data() calls
363 * dbuf_clear_data(). However, the hold count can grow
364 * during eviction even though db_mtx is held (see
365 * dmu_bonus_hold() for an example), so we can only
366 * test the generic invariant that holds >= dirtycnt.
368 ASSERT3U(holds
, >=, db
->db_dirtycnt
);
370 if (db
->db_user_immediate_evict
== TRUE
)
371 ASSERT3U(holds
, >=, db
->db_dirtycnt
);
373 ASSERT3U(holds
, >, 0);
379 dbuf_evict_user(dmu_buf_impl_t
*db
)
381 dmu_buf_user_t
*dbu
= db
->db_user
;
383 ASSERT(MUTEX_HELD(&db
->db_mtx
));
388 dbuf_verify_user(db
, DBVU_EVICTING
);
392 if (dbu
->dbu_clear_on_evict_dbufp
!= NULL
)
393 *dbu
->dbu_clear_on_evict_dbufp
= NULL
;
397 * There are two eviction callbacks - one that we call synchronously
398 * and one that we invoke via a taskq. The async one is useful for
399 * avoiding lock order reversals and limiting stack depth.
401 * Note that if we have a sync callback but no async callback,
402 * it's likely that the sync callback will free the structure
403 * containing the dbu. In that case we need to take care to not
404 * dereference dbu after calling the sync evict func.
406 boolean_t has_async
= (dbu
->dbu_evict_func_async
!= NULL
);
408 if (dbu
->dbu_evict_func_sync
!= NULL
)
409 dbu
->dbu_evict_func_sync(dbu
);
412 taskq_dispatch_ent(dbu_evict_taskq
, dbu
->dbu_evict_func_async
,
413 dbu
, 0, &dbu
->dbu_tqent
);
418 dbuf_is_metadata(dmu_buf_impl_t
*db
)
421 * Consider indirect blocks and spill blocks to be meta data.
423 if (db
->db_level
> 0 || db
->db_blkid
== DMU_SPILL_BLKID
) {
426 boolean_t is_metadata
;
429 is_metadata
= DMU_OT_IS_METADATA(DB_DNODE(db
)->dn_type
);
432 return (is_metadata
);
438 * This function *must* return indices evenly distributed between all
439 * sublists of the multilist. This is needed due to how the dbuf eviction
440 * code is laid out; dbuf_evict_thread() assumes dbufs are evenly
441 * distributed between all sublists and uses this assumption when
442 * deciding which sublist to evict from and how much to evict from it.
445 dbuf_cache_multilist_index_func(multilist_t
*ml
, void *obj
)
447 dmu_buf_impl_t
*db
= obj
;
450 * The assumption here, is the hash value for a given
451 * dmu_buf_impl_t will remain constant throughout it's lifetime
452 * (i.e. it's objset, object, level and blkid fields don't change).
453 * Thus, we don't need to store the dbuf's sublist index
454 * on insertion, as this index can be recalculated on removal.
456 * Also, the low order bits of the hash value are thought to be
457 * distributed evenly. Otherwise, in the case that the multilist
458 * has a power of two number of sublists, each sublists' usage
459 * would not be evenly distributed.
461 return (dbuf_hash(db
->db_objset
, db
->db
.db_object
,
462 db
->db_level
, db
->db_blkid
) %
463 multilist_get_num_sublists(ml
));
466 static inline unsigned long
467 dbuf_cache_target_bytes(void)
469 return MIN(dbuf_cache_max_bytes
,
470 arc_target_bytes() >> dbuf_cache_max_shift
);
473 static inline boolean_t
474 dbuf_cache_above_hiwater(void)
476 uint64_t dbuf_cache_target
= dbuf_cache_target_bytes();
478 uint64_t dbuf_cache_hiwater_bytes
=
479 (dbuf_cache_target
* dbuf_cache_hiwater_pct
) / 100;
481 return (refcount_count(&dbuf_cache_size
) >
482 dbuf_cache_target
+ dbuf_cache_hiwater_bytes
);
485 static inline boolean_t
486 dbuf_cache_above_lowater(void)
488 uint64_t dbuf_cache_target
= dbuf_cache_target_bytes();
490 uint64_t dbuf_cache_lowater_bytes
=
491 (dbuf_cache_target
* dbuf_cache_lowater_pct
) / 100;
493 return (refcount_count(&dbuf_cache_size
) >
494 dbuf_cache_target
- dbuf_cache_lowater_bytes
);
498 * Evict the oldest eligible dbuf from the dbuf cache.
503 int idx
= multilist_get_random_index(dbuf_cache
);
504 multilist_sublist_t
*mls
= multilist_sublist_lock(dbuf_cache
, idx
);
506 ASSERT(!MUTEX_HELD(&dbuf_evict_lock
));
509 * Set the thread's tsd to indicate that it's processing evictions.
510 * Once a thread stops evicting from the dbuf cache it will
511 * reset its tsd to NULL.
513 ASSERT3P(tsd_get(zfs_dbuf_evict_key
), ==, NULL
);
514 (void) tsd_set(zfs_dbuf_evict_key
, (void *)B_TRUE
);
516 db
= multilist_sublist_tail(mls
);
517 while (db
!= NULL
&& mutex_tryenter(&db
->db_mtx
) == 0) {
518 db
= multilist_sublist_prev(mls
, db
);
521 DTRACE_PROBE2(dbuf__evict__one
, dmu_buf_impl_t
*, db
,
522 multilist_sublist_t
*, mls
);
525 multilist_sublist_remove(mls
, db
);
526 multilist_sublist_unlock(mls
);
527 (void) refcount_remove_many(&dbuf_cache_size
,
531 multilist_sublist_unlock(mls
);
533 (void) tsd_set(zfs_dbuf_evict_key
, NULL
);
537 * The dbuf evict thread is responsible for aging out dbufs from the
538 * cache. Once the cache has reached it's maximum size, dbufs are removed
539 * and destroyed. The eviction thread will continue running until the size
540 * of the dbuf cache is at or below the maximum size. Once the dbuf is aged
541 * out of the cache it is destroyed and becomes eligible for arc eviction.
544 dbuf_evict_thread(void)
548 CALLB_CPR_INIT(&cpr
, &dbuf_evict_lock
, callb_generic_cpr
, FTAG
);
550 mutex_enter(&dbuf_evict_lock
);
551 while (!dbuf_evict_thread_exit
) {
552 while (!dbuf_cache_above_lowater() && !dbuf_evict_thread_exit
) {
553 CALLB_CPR_SAFE_BEGIN(&cpr
);
554 (void) cv_timedwait_sig_hires(&dbuf_evict_cv
,
555 &dbuf_evict_lock
, SEC2NSEC(1), MSEC2NSEC(1), 0);
556 CALLB_CPR_SAFE_END(&cpr
, &dbuf_evict_lock
);
558 mutex_exit(&dbuf_evict_lock
);
561 * Keep evicting as long as we're above the low water mark
562 * for the cache. We do this without holding the locks to
563 * minimize lock contention.
565 while (dbuf_cache_above_lowater() && !dbuf_evict_thread_exit
) {
569 mutex_enter(&dbuf_evict_lock
);
572 dbuf_evict_thread_exit
= B_FALSE
;
573 cv_broadcast(&dbuf_evict_cv
);
574 CALLB_CPR_EXIT(&cpr
); /* drops dbuf_evict_lock */
579 * Wake up the dbuf eviction thread if the dbuf cache is at its max size.
580 * If the dbuf cache is at its high water mark, then evict a dbuf from the
581 * dbuf cache using the callers context.
584 dbuf_evict_notify(void)
588 * We use thread specific data to track when a thread has
589 * started processing evictions. This allows us to avoid deeply
590 * nested stacks that would have a call flow similar to this:
592 * dbuf_rele()-->dbuf_rele_and_unlock()-->dbuf_evict_notify()
595 * +-----dbuf_destroy()<--dbuf_evict_one()<--------+
597 * The dbuf_eviction_thread will always have its tsd set until
598 * that thread exits. All other threads will only set their tsd
599 * if they are participating in the eviction process. This only
600 * happens if the eviction thread is unable to process evictions
601 * fast enough. To keep the dbuf cache size in check, other threads
602 * can evict from the dbuf cache directly. Those threads will set
603 * their tsd values so that we ensure that they only evict one dbuf
604 * from the dbuf cache.
606 if (tsd_get(zfs_dbuf_evict_key
) != NULL
)
610 * We check if we should evict without holding the dbuf_evict_lock,
611 * because it's OK to occasionally make the wrong decision here,
612 * and grabbing the lock results in massive lock contention.
614 if (refcount_count(&dbuf_cache_size
) > dbuf_cache_target_bytes()) {
615 if (dbuf_cache_above_hiwater())
617 cv_signal(&dbuf_evict_cv
);
626 uint64_t hsize
= 1ULL << 16;
627 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
631 * The hash table is big enough to fill all of physical memory
632 * with an average block size of zfs_arc_average_blocksize (default 8K).
633 * By default, the table will take up
634 * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers).
636 while (hsize
* zfs_arc_average_blocksize
< physmem
* PAGESIZE
)
640 h
->hash_table_mask
= hsize
- 1;
641 #if defined(_KERNEL) && defined(HAVE_SPL)
643 * Large allocations which do not require contiguous pages
644 * should be using vmem_alloc() in the linux kernel
646 h
->hash_table
= vmem_zalloc(hsize
* sizeof (void *), KM_SLEEP
);
648 h
->hash_table
= kmem_zalloc(hsize
* sizeof (void *), KM_NOSLEEP
);
650 if (h
->hash_table
== NULL
) {
651 /* XXX - we should really return an error instead of assert */
652 ASSERT(hsize
> (1ULL << 10));
657 dbuf_kmem_cache
= kmem_cache_create("dmu_buf_impl_t",
658 sizeof (dmu_buf_impl_t
),
659 0, dbuf_cons
, dbuf_dest
, NULL
, NULL
, NULL
, 0);
661 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
662 mutex_init(&h
->hash_mutexes
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
667 * Setup the parameters for the dbuf cache. We cap the size of the
668 * dbuf cache to 1/32nd (default) of the size of the ARC.
670 dbuf_cache_max_bytes
= MIN(dbuf_cache_max_bytes
,
671 arc_target_bytes() >> dbuf_cache_max_shift
);
674 * All entries are queued via taskq_dispatch_ent(), so min/maxalloc
675 * configuration is not required.
677 dbu_evict_taskq
= taskq_create("dbu_evict", 1, defclsyspri
, 0, 0, 0);
679 dbuf_cache
= multilist_create(sizeof (dmu_buf_impl_t
),
680 offsetof(dmu_buf_impl_t
, db_cache_link
),
681 dbuf_cache_multilist_index_func
);
682 refcount_create(&dbuf_cache_size
);
684 tsd_create(&zfs_dbuf_evict_key
, NULL
);
685 dbuf_evict_thread_exit
= B_FALSE
;
686 mutex_init(&dbuf_evict_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
687 cv_init(&dbuf_evict_cv
, NULL
, CV_DEFAULT
, NULL
);
688 dbuf_cache_evict_thread
= thread_create(NULL
, 0, dbuf_evict_thread
,
689 NULL
, 0, &p0
, TS_RUN
, minclsyspri
);
695 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
698 dbuf_stats_destroy();
700 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
701 mutex_destroy(&h
->hash_mutexes
[i
]);
702 #if defined(_KERNEL) && defined(HAVE_SPL)
704 * Large allocations which do not require contiguous pages
705 * should be using vmem_free() in the linux kernel
707 vmem_free(h
->hash_table
, (h
->hash_table_mask
+ 1) * sizeof (void *));
709 kmem_free(h
->hash_table
, (h
->hash_table_mask
+ 1) * sizeof (void *));
711 kmem_cache_destroy(dbuf_kmem_cache
);
712 taskq_destroy(dbu_evict_taskq
);
714 mutex_enter(&dbuf_evict_lock
);
715 dbuf_evict_thread_exit
= B_TRUE
;
716 while (dbuf_evict_thread_exit
) {
717 cv_signal(&dbuf_evict_cv
);
718 cv_wait(&dbuf_evict_cv
, &dbuf_evict_lock
);
720 mutex_exit(&dbuf_evict_lock
);
721 tsd_destroy(&zfs_dbuf_evict_key
);
723 mutex_destroy(&dbuf_evict_lock
);
724 cv_destroy(&dbuf_evict_cv
);
726 refcount_destroy(&dbuf_cache_size
);
727 multilist_destroy(dbuf_cache
);
736 dbuf_verify(dmu_buf_impl_t
*db
)
739 dbuf_dirty_record_t
*dr
;
741 ASSERT(MUTEX_HELD(&db
->db_mtx
));
743 if (!(zfs_flags
& ZFS_DEBUG_DBUF_VERIFY
))
746 ASSERT(db
->db_objset
!= NULL
);
750 ASSERT(db
->db_parent
== NULL
);
751 ASSERT(db
->db_blkptr
== NULL
);
753 ASSERT3U(db
->db
.db_object
, ==, dn
->dn_object
);
754 ASSERT3P(db
->db_objset
, ==, dn
->dn_objset
);
755 ASSERT3U(db
->db_level
, <, dn
->dn_nlevels
);
756 ASSERT(db
->db_blkid
== DMU_BONUS_BLKID
||
757 db
->db_blkid
== DMU_SPILL_BLKID
||
758 !avl_is_empty(&dn
->dn_dbufs
));
760 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
762 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
763 ASSERT3U(db
->db
.db_offset
, ==, DMU_BONUS_BLKID
);
764 } else if (db
->db_blkid
== DMU_SPILL_BLKID
) {
766 ASSERT0(db
->db
.db_offset
);
768 ASSERT3U(db
->db
.db_offset
, ==, db
->db_blkid
* db
->db
.db_size
);
771 for (dr
= db
->db_data_pending
; dr
!= NULL
; dr
= dr
->dr_next
)
772 ASSERT(dr
->dr_dbuf
== db
);
774 for (dr
= db
->db_last_dirty
; dr
!= NULL
; dr
= dr
->dr_next
)
775 ASSERT(dr
->dr_dbuf
== db
);
778 * We can't assert that db_size matches dn_datablksz because it
779 * can be momentarily different when another thread is doing
782 if (db
->db_level
== 0 && db
->db
.db_object
== DMU_META_DNODE_OBJECT
) {
783 dr
= db
->db_data_pending
;
785 * It should only be modified in syncing context, so
786 * make sure we only have one copy of the data.
788 ASSERT(dr
== NULL
|| dr
->dt
.dl
.dr_data
== db
->db_buf
);
791 /* verify db->db_blkptr */
793 if (db
->db_parent
== dn
->dn_dbuf
) {
794 /* db is pointed to by the dnode */
795 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
796 if (DMU_OBJECT_IS_SPECIAL(db
->db
.db_object
))
797 ASSERT(db
->db_parent
== NULL
);
799 ASSERT(db
->db_parent
!= NULL
);
800 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
801 ASSERT3P(db
->db_blkptr
, ==,
802 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
804 /* db is pointed to by an indirect block */
805 ASSERTV(int epb
= db
->db_parent
->db
.db_size
>>
807 ASSERT3U(db
->db_parent
->db_level
, ==, db
->db_level
+1);
808 ASSERT3U(db
->db_parent
->db
.db_object
, ==,
811 * dnode_grow_indblksz() can make this fail if we don't
812 * have the struct_rwlock. XXX indblksz no longer
813 * grows. safe to do this now?
815 if (RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
816 ASSERT3P(db
->db_blkptr
, ==,
817 ((blkptr_t
*)db
->db_parent
->db
.db_data
+
818 db
->db_blkid
% epb
));
822 if ((db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
)) &&
823 (db
->db_buf
== NULL
|| db
->db_buf
->b_data
) &&
824 db
->db
.db_data
&& db
->db_blkid
!= DMU_BONUS_BLKID
&&
825 db
->db_state
!= DB_FILL
&& !dn
->dn_free_txg
) {
827 * If the blkptr isn't set but they have nonzero data,
828 * it had better be dirty, otherwise we'll lose that
829 * data when we evict this buffer.
831 * There is an exception to this rule for indirect blocks; in
832 * this case, if the indirect block is a hole, we fill in a few
833 * fields on each of the child blocks (importantly, birth time)
834 * to prevent hole birth times from being lost when you
835 * partially fill in a hole.
837 if (db
->db_dirtycnt
== 0) {
838 if (db
->db_level
== 0) {
839 uint64_t *buf
= db
->db
.db_data
;
842 for (i
= 0; i
< db
->db
.db_size
>> 3; i
++) {
847 blkptr_t
*bps
= db
->db
.db_data
;
848 ASSERT3U(1 << DB_DNODE(db
)->dn_indblkshift
, ==,
851 * We want to verify that all the blkptrs in the
852 * indirect block are holes, but we may have
853 * automatically set up a few fields for them.
854 * We iterate through each blkptr and verify
855 * they only have those fields set.
858 i
< db
->db
.db_size
/ sizeof (blkptr_t
);
860 blkptr_t
*bp
= &bps
[i
];
861 ASSERT(ZIO_CHECKSUM_IS_ZERO(
864 DVA_IS_EMPTY(&bp
->blk_dva
[0]) &&
865 DVA_IS_EMPTY(&bp
->blk_dva
[1]) &&
866 DVA_IS_EMPTY(&bp
->blk_dva
[2]));
867 ASSERT0(bp
->blk_fill
);
868 ASSERT0(bp
->blk_pad
[0]);
869 ASSERT0(bp
->blk_pad
[1]);
870 ASSERT(!BP_IS_EMBEDDED(bp
));
871 ASSERT(BP_IS_HOLE(bp
));
872 ASSERT0(bp
->blk_phys_birth
);
882 dbuf_clear_data(dmu_buf_impl_t
*db
)
884 ASSERT(MUTEX_HELD(&db
->db_mtx
));
886 ASSERT3P(db
->db_buf
, ==, NULL
);
887 db
->db
.db_data
= NULL
;
888 if (db
->db_state
!= DB_NOFILL
)
889 db
->db_state
= DB_UNCACHED
;
893 dbuf_set_data(dmu_buf_impl_t
*db
, arc_buf_t
*buf
)
895 ASSERT(MUTEX_HELD(&db
->db_mtx
));
899 ASSERT(buf
->b_data
!= NULL
);
900 db
->db
.db_data
= buf
->b_data
;
904 * Loan out an arc_buf for read. Return the loaned arc_buf.
907 dbuf_loan_arcbuf(dmu_buf_impl_t
*db
)
911 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
912 mutex_enter(&db
->db_mtx
);
913 if (arc_released(db
->db_buf
) || refcount_count(&db
->db_holds
) > 1) {
914 int blksz
= db
->db
.db_size
;
915 spa_t
*spa
= db
->db_objset
->os_spa
;
917 mutex_exit(&db
->db_mtx
);
918 abuf
= arc_loan_buf(spa
, B_FALSE
, blksz
);
919 bcopy(db
->db
.db_data
, abuf
->b_data
, blksz
);
922 arc_loan_inuse_buf(abuf
, db
);
925 mutex_exit(&db
->db_mtx
);
931 * Calculate which level n block references the data at the level 0 offset
935 dbuf_whichblock(const dnode_t
*dn
, const int64_t level
, const uint64_t offset
)
937 if (dn
->dn_datablkshift
!= 0 && dn
->dn_indblkshift
!= 0) {
939 * The level n blkid is equal to the level 0 blkid divided by
940 * the number of level 0s in a level n block.
942 * The level 0 blkid is offset >> datablkshift =
943 * offset / 2^datablkshift.
945 * The number of level 0s in a level n is the number of block
946 * pointers in an indirect block, raised to the power of level.
947 * This is 2^(indblkshift - SPA_BLKPTRSHIFT)^level =
948 * 2^(level*(indblkshift - SPA_BLKPTRSHIFT)).
950 * Thus, the level n blkid is: offset /
951 * ((2^datablkshift)*(2^(level*(indblkshift - SPA_BLKPTRSHIFT)))
952 * = offset / 2^(datablkshift + level *
953 * (indblkshift - SPA_BLKPTRSHIFT))
954 * = offset >> (datablkshift + level *
955 * (indblkshift - SPA_BLKPTRSHIFT))
958 const unsigned exp
= dn
->dn_datablkshift
+
959 level
* (dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
);
961 if (exp
>= 8 * sizeof (offset
)) {
962 /* This only happens on the highest indirection level */
963 ASSERT3U(level
, ==, dn
->dn_nlevels
- 1);
967 ASSERT3U(exp
, <, 8 * sizeof (offset
));
969 return (offset
>> exp
);
971 ASSERT3U(offset
, <, dn
->dn_datablksz
);
977 dbuf_read_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
979 dmu_buf_impl_t
*db
= vdb
;
981 mutex_enter(&db
->db_mtx
);
982 ASSERT3U(db
->db_state
, ==, DB_READ
);
984 * All reads are synchronous, so we must have a hold on the dbuf
986 ASSERT(refcount_count(&db
->db_holds
) > 0);
987 ASSERT(db
->db_buf
== NULL
);
988 ASSERT(db
->db
.db_data
== NULL
);
989 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
990 /* we were freed in flight; disregard any error */
991 arc_release(buf
, db
);
992 bzero(buf
->b_data
, db
->db
.db_size
);
994 db
->db_freed_in_flight
= FALSE
;
995 dbuf_set_data(db
, buf
);
996 db
->db_state
= DB_CACHED
;
997 } else if (zio
== NULL
|| zio
->io_error
== 0) {
998 dbuf_set_data(db
, buf
);
999 db
->db_state
= DB_CACHED
;
1001 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1002 ASSERT3P(db
->db_buf
, ==, NULL
);
1003 arc_buf_destroy(buf
, db
);
1004 db
->db_state
= DB_UNCACHED
;
1006 cv_broadcast(&db
->db_changed
);
1007 dbuf_rele_and_unlock(db
, NULL
);
1011 dbuf_read_impl(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t flags
)
1014 zbookmark_phys_t zb
;
1015 uint32_t aflags
= ARC_FLAG_NOWAIT
;
1020 ASSERT(!refcount_is_zero(&db
->db_holds
));
1021 /* We need the struct_rwlock to prevent db_blkptr from changing. */
1022 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
1023 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1024 ASSERT(db
->db_state
== DB_UNCACHED
);
1025 ASSERT(db
->db_buf
== NULL
);
1027 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1029 * The bonus length stored in the dnode may be less than
1030 * the maximum available space in the bonus buffer.
1032 int bonuslen
= MIN(dn
->dn_bonuslen
, dn
->dn_phys
->dn_bonuslen
);
1033 int max_bonuslen
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
);
1035 ASSERT3U(bonuslen
, <=, db
->db
.db_size
);
1036 db
->db
.db_data
= kmem_alloc(max_bonuslen
, KM_SLEEP
);
1037 arc_space_consume(max_bonuslen
, ARC_SPACE_BONUS
);
1038 if (bonuslen
< max_bonuslen
)
1039 bzero(db
->db
.db_data
, max_bonuslen
);
1041 bcopy(DN_BONUS(dn
->dn_phys
), db
->db
.db_data
, bonuslen
);
1043 db
->db_state
= DB_CACHED
;
1044 mutex_exit(&db
->db_mtx
);
1049 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
1050 * processes the delete record and clears the bp while we are waiting
1051 * for the dn_mtx (resulting in a "no" from block_freed).
1053 if (db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
) ||
1054 (db
->db_level
== 0 && (dnode_block_freed(dn
, db
->db_blkid
) ||
1055 BP_IS_HOLE(db
->db_blkptr
)))) {
1056 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1058 dbuf_set_data(db
, arc_alloc_buf(db
->db_objset
->os_spa
, db
, type
,
1060 bzero(db
->db
.db_data
, db
->db
.db_size
);
1062 if (db
->db_blkptr
!= NULL
&& db
->db_level
> 0 &&
1063 BP_IS_HOLE(db
->db_blkptr
) &&
1064 db
->db_blkptr
->blk_birth
!= 0) {
1065 blkptr_t
*bps
= db
->db
.db_data
;
1067 for (i
= 0; i
< ((1 <<
1068 DB_DNODE(db
)->dn_indblkshift
) / sizeof (blkptr_t
));
1070 blkptr_t
*bp
= &bps
[i
];
1071 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==,
1072 1 << dn
->dn_indblkshift
);
1074 BP_GET_LEVEL(db
->db_blkptr
) == 1 ?
1076 BP_GET_LSIZE(db
->db_blkptr
));
1077 BP_SET_TYPE(bp
, BP_GET_TYPE(db
->db_blkptr
));
1079 BP_GET_LEVEL(db
->db_blkptr
) - 1);
1080 BP_SET_BIRTH(bp
, db
->db_blkptr
->blk_birth
, 0);
1084 db
->db_state
= DB_CACHED
;
1085 mutex_exit(&db
->db_mtx
);
1091 db
->db_state
= DB_READ
;
1092 mutex_exit(&db
->db_mtx
);
1094 if (DBUF_IS_L2CACHEABLE(db
))
1095 aflags
|= ARC_FLAG_L2CACHE
;
1097 SET_BOOKMARK(&zb
, db
->db_objset
->os_dsl_dataset
?
1098 db
->db_objset
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
1099 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
1101 dbuf_add_ref(db
, NULL
);
1103 err
= arc_read(zio
, db
->db_objset
->os_spa
, db
->db_blkptr
,
1104 dbuf_read_done
, db
, ZIO_PRIORITY_SYNC_READ
,
1105 (flags
& DB_RF_CANFAIL
) ? ZIO_FLAG_CANFAIL
: ZIO_FLAG_MUSTSUCCEED
,
1112 * This is our just-in-time copy function. It makes a copy of buffers that
1113 * have been modified in a previous transaction group before we access them in
1114 * the current active group.
1116 * This function is used in three places: when we are dirtying a buffer for the
1117 * first time in a txg, when we are freeing a range in a dnode that includes
1118 * this buffer, and when we are accessing a buffer which was received compressed
1119 * and later referenced in a WRITE_BYREF record.
1121 * Note that when we are called from dbuf_free_range() we do not put a hold on
1122 * the buffer, we just traverse the active dbuf list for the dnode.
1125 dbuf_fix_old_data(dmu_buf_impl_t
*db
, uint64_t txg
)
1127 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
1129 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1130 ASSERT(db
->db
.db_data
!= NULL
);
1131 ASSERT(db
->db_level
== 0);
1132 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
);
1135 (dr
->dt
.dl
.dr_data
!=
1136 ((db
->db_blkid
== DMU_BONUS_BLKID
) ? db
->db
.db_data
: db
->db_buf
)))
1140 * If the last dirty record for this dbuf has not yet synced
1141 * and its referencing the dbuf data, either:
1142 * reset the reference to point to a new copy,
1143 * or (if there a no active holders)
1144 * just null out the current db_data pointer.
1146 ASSERT(dr
->dr_txg
>= txg
- 2);
1147 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1148 dnode_t
*dn
= DB_DNODE(db
);
1149 int bonuslen
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
);
1150 dr
->dt
.dl
.dr_data
= kmem_alloc(bonuslen
, KM_SLEEP
);
1151 arc_space_consume(bonuslen
, ARC_SPACE_BONUS
);
1152 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
, bonuslen
);
1153 } else if (refcount_count(&db
->db_holds
) > db
->db_dirtycnt
) {
1154 int size
= arc_buf_size(db
->db_buf
);
1155 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1156 spa_t
*spa
= db
->db_objset
->os_spa
;
1157 enum zio_compress compress_type
=
1158 arc_get_compression(db
->db_buf
);
1160 if (compress_type
== ZIO_COMPRESS_OFF
) {
1161 dr
->dt
.dl
.dr_data
= arc_alloc_buf(spa
, db
, type
, size
);
1163 ASSERT3U(type
, ==, ARC_BUFC_DATA
);
1164 dr
->dt
.dl
.dr_data
= arc_alloc_compressed_buf(spa
, db
,
1165 size
, arc_buf_lsize(db
->db_buf
), compress_type
);
1167 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
->b_data
, size
);
1170 dbuf_clear_data(db
);
1175 dbuf_read(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t flags
)
1182 * We don't have to hold the mutex to check db_state because it
1183 * can't be freed while we have a hold on the buffer.
1185 ASSERT(!refcount_is_zero(&db
->db_holds
));
1187 if (db
->db_state
== DB_NOFILL
)
1188 return (SET_ERROR(EIO
));
1192 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1193 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
1195 prefetch
= db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1196 (flags
& DB_RF_NOPREFETCH
) == 0 && dn
!= NULL
&&
1197 DBUF_IS_CACHEABLE(db
);
1199 mutex_enter(&db
->db_mtx
);
1200 if (db
->db_state
== DB_CACHED
) {
1202 * If the arc buf is compressed, we need to decompress it to
1203 * read the data. This could happen during the "zfs receive" of
1204 * a stream which is compressed and deduplicated.
1206 if (db
->db_buf
!= NULL
&&
1207 arc_get_compression(db
->db_buf
) != ZIO_COMPRESS_OFF
) {
1208 dbuf_fix_old_data(db
,
1209 spa_syncing_txg(dmu_objset_spa(db
->db_objset
)));
1210 err
= arc_decompress(db
->db_buf
);
1211 dbuf_set_data(db
, db
->db_buf
);
1213 mutex_exit(&db
->db_mtx
);
1215 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
);
1216 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1217 rw_exit(&dn
->dn_struct_rwlock
);
1219 } else if (db
->db_state
== DB_UNCACHED
) {
1220 spa_t
*spa
= dn
->dn_objset
->os_spa
;
1221 boolean_t need_wait
= B_FALSE
;
1224 db
->db_blkptr
!= NULL
&& !BP_IS_HOLE(db
->db_blkptr
)) {
1225 zio
= zio_root(spa
, NULL
, NULL
, ZIO_FLAG_CANFAIL
);
1228 err
= dbuf_read_impl(db
, zio
, flags
);
1230 /* dbuf_read_impl has dropped db_mtx for us */
1232 if (!err
&& prefetch
)
1233 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
);
1235 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1236 rw_exit(&dn
->dn_struct_rwlock
);
1239 if (!err
&& need_wait
)
1240 err
= zio_wait(zio
);
1243 * Another reader came in while the dbuf was in flight
1244 * between UNCACHED and CACHED. Either a writer will finish
1245 * writing the buffer (sending the dbuf to CACHED) or the
1246 * first reader's request will reach the read_done callback
1247 * and send the dbuf to CACHED. Otherwise, a failure
1248 * occurred and the dbuf went to UNCACHED.
1250 mutex_exit(&db
->db_mtx
);
1252 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
);
1253 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1254 rw_exit(&dn
->dn_struct_rwlock
);
1257 /* Skip the wait per the caller's request. */
1258 mutex_enter(&db
->db_mtx
);
1259 if ((flags
& DB_RF_NEVERWAIT
) == 0) {
1260 while (db
->db_state
== DB_READ
||
1261 db
->db_state
== DB_FILL
) {
1262 ASSERT(db
->db_state
== DB_READ
||
1263 (flags
& DB_RF_HAVESTRUCT
) == 0);
1264 DTRACE_PROBE2(blocked__read
, dmu_buf_impl_t
*,
1266 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1268 if (db
->db_state
== DB_UNCACHED
)
1269 err
= SET_ERROR(EIO
);
1271 mutex_exit(&db
->db_mtx
);
1278 dbuf_noread(dmu_buf_impl_t
*db
)
1280 ASSERT(!refcount_is_zero(&db
->db_holds
));
1281 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1282 mutex_enter(&db
->db_mtx
);
1283 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
1284 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1285 if (db
->db_state
== DB_UNCACHED
) {
1286 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1287 spa_t
*spa
= db
->db_objset
->os_spa
;
1289 ASSERT(db
->db_buf
== NULL
);
1290 ASSERT(db
->db
.db_data
== NULL
);
1291 dbuf_set_data(db
, arc_alloc_buf(spa
, db
, type
, db
->db
.db_size
));
1292 db
->db_state
= DB_FILL
;
1293 } else if (db
->db_state
== DB_NOFILL
) {
1294 dbuf_clear_data(db
);
1296 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
1298 mutex_exit(&db
->db_mtx
);
1302 dbuf_unoverride(dbuf_dirty_record_t
*dr
)
1304 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
1305 blkptr_t
*bp
= &dr
->dt
.dl
.dr_overridden_by
;
1306 uint64_t txg
= dr
->dr_txg
;
1308 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1310 * This assert is valid because dmu_sync() expects to be called by
1311 * a zilog's get_data while holding a range lock. This call only
1312 * comes from dbuf_dirty() callers who must also hold a range lock.
1314 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_IN_DMU_SYNC
);
1315 ASSERT(db
->db_level
== 0);
1317 if (db
->db_blkid
== DMU_BONUS_BLKID
||
1318 dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
)
1321 ASSERT(db
->db_data_pending
!= dr
);
1323 /* free this block */
1324 if (!BP_IS_HOLE(bp
) && !dr
->dt
.dl
.dr_nopwrite
)
1325 zio_free(db
->db_objset
->os_spa
, txg
, bp
);
1327 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
1328 dr
->dt
.dl
.dr_nopwrite
= B_FALSE
;
1331 * Release the already-written buffer, so we leave it in
1332 * a consistent dirty state. Note that all callers are
1333 * modifying the buffer, so they will immediately do
1334 * another (redundant) arc_release(). Therefore, leave
1335 * the buf thawed to save the effort of freezing &
1336 * immediately re-thawing it.
1338 arc_release(dr
->dt
.dl
.dr_data
, db
);
1342 * Evict (if its unreferenced) or clear (if its referenced) any level-0
1343 * data blocks in the free range, so that any future readers will find
1347 dbuf_free_range(dnode_t
*dn
, uint64_t start_blkid
, uint64_t end_blkid
,
1350 dmu_buf_impl_t
*db_search
;
1351 dmu_buf_impl_t
*db
, *db_next
;
1352 uint64_t txg
= tx
->tx_txg
;
1355 if (end_blkid
> dn
->dn_maxblkid
&&
1356 !(start_blkid
== DMU_SPILL_BLKID
|| end_blkid
== DMU_SPILL_BLKID
))
1357 end_blkid
= dn
->dn_maxblkid
;
1358 dprintf_dnode(dn
, "start=%llu end=%llu\n", start_blkid
, end_blkid
);
1360 db_search
= kmem_alloc(sizeof (dmu_buf_impl_t
), KM_SLEEP
);
1361 db_search
->db_level
= 0;
1362 db_search
->db_blkid
= start_blkid
;
1363 db_search
->db_state
= DB_SEARCH
;
1365 mutex_enter(&dn
->dn_dbufs_mtx
);
1366 db
= avl_find(&dn
->dn_dbufs
, db_search
, &where
);
1367 ASSERT3P(db
, ==, NULL
);
1369 db
= avl_nearest(&dn
->dn_dbufs
, where
, AVL_AFTER
);
1371 for (; db
!= NULL
; db
= db_next
) {
1372 db_next
= AVL_NEXT(&dn
->dn_dbufs
, db
);
1373 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1375 if (db
->db_level
!= 0 || db
->db_blkid
> end_blkid
) {
1378 ASSERT3U(db
->db_blkid
, >=, start_blkid
);
1380 /* found a level 0 buffer in the range */
1381 mutex_enter(&db
->db_mtx
);
1382 if (dbuf_undirty(db
, tx
)) {
1383 /* mutex has been dropped and dbuf destroyed */
1387 if (db
->db_state
== DB_UNCACHED
||
1388 db
->db_state
== DB_NOFILL
||
1389 db
->db_state
== DB_EVICTING
) {
1390 ASSERT(db
->db
.db_data
== NULL
);
1391 mutex_exit(&db
->db_mtx
);
1394 if (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
) {
1395 /* will be handled in dbuf_read_done or dbuf_rele */
1396 db
->db_freed_in_flight
= TRUE
;
1397 mutex_exit(&db
->db_mtx
);
1400 if (refcount_count(&db
->db_holds
) == 0) {
1405 /* The dbuf is referenced */
1407 if (db
->db_last_dirty
!= NULL
) {
1408 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
1410 if (dr
->dr_txg
== txg
) {
1412 * This buffer is "in-use", re-adjust the file
1413 * size to reflect that this buffer may
1414 * contain new data when we sync.
1416 if (db
->db_blkid
!= DMU_SPILL_BLKID
&&
1417 db
->db_blkid
> dn
->dn_maxblkid
)
1418 dn
->dn_maxblkid
= db
->db_blkid
;
1419 dbuf_unoverride(dr
);
1422 * This dbuf is not dirty in the open context.
1423 * Either uncache it (if its not referenced in
1424 * the open context) or reset its contents to
1427 dbuf_fix_old_data(db
, txg
);
1430 /* clear the contents if its cached */
1431 if (db
->db_state
== DB_CACHED
) {
1432 ASSERT(db
->db
.db_data
!= NULL
);
1433 arc_release(db
->db_buf
, db
);
1434 bzero(db
->db
.db_data
, db
->db
.db_size
);
1435 arc_buf_freeze(db
->db_buf
);
1438 mutex_exit(&db
->db_mtx
);
1441 kmem_free(db_search
, sizeof (dmu_buf_impl_t
));
1442 mutex_exit(&dn
->dn_dbufs_mtx
);
1446 dbuf_new_size(dmu_buf_impl_t
*db
, int size
, dmu_tx_t
*tx
)
1448 arc_buf_t
*buf
, *obuf
;
1449 int osize
= db
->db
.db_size
;
1450 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1453 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1458 /* XXX does *this* func really need the lock? */
1459 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
1462 * This call to dmu_buf_will_dirty() with the dn_struct_rwlock held
1463 * is OK, because there can be no other references to the db
1464 * when we are changing its size, so no concurrent DB_FILL can
1468 * XXX we should be doing a dbuf_read, checking the return
1469 * value and returning that up to our callers
1471 dmu_buf_will_dirty(&db
->db
, tx
);
1473 /* create the data buffer for the new block */
1474 buf
= arc_alloc_buf(dn
->dn_objset
->os_spa
, db
, type
, size
);
1476 /* copy old block data to the new block */
1478 bcopy(obuf
->b_data
, buf
->b_data
, MIN(osize
, size
));
1479 /* zero the remainder */
1481 bzero((uint8_t *)buf
->b_data
+ osize
, size
- osize
);
1483 mutex_enter(&db
->db_mtx
);
1484 dbuf_set_data(db
, buf
);
1485 arc_buf_destroy(obuf
, db
);
1486 db
->db
.db_size
= size
;
1488 if (db
->db_level
== 0) {
1489 ASSERT3U(db
->db_last_dirty
->dr_txg
, ==, tx
->tx_txg
);
1490 db
->db_last_dirty
->dt
.dl
.dr_data
= buf
;
1492 mutex_exit(&db
->db_mtx
);
1494 dmu_objset_willuse_space(dn
->dn_objset
, size
- osize
, tx
);
1499 dbuf_release_bp(dmu_buf_impl_t
*db
)
1501 ASSERTV(objset_t
*os
= db
->db_objset
);
1503 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os
)));
1504 ASSERT(arc_released(os
->os_phys_buf
) ||
1505 list_link_active(&os
->os_dsl_dataset
->ds_synced_link
));
1506 ASSERT(db
->db_parent
== NULL
|| arc_released(db
->db_parent
->db_buf
));
1508 (void) arc_release(db
->db_buf
, db
);
1512 * We already have a dirty record for this TXG, and we are being
1516 dbuf_redirty(dbuf_dirty_record_t
*dr
)
1518 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
1520 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1522 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
) {
1524 * If this buffer has already been written out,
1525 * we now need to reset its state.
1527 dbuf_unoverride(dr
);
1528 if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
&&
1529 db
->db_state
!= DB_NOFILL
) {
1530 /* Already released on initial dirty, so just thaw. */
1531 ASSERT(arc_released(db
->db_buf
));
1532 arc_buf_thaw(db
->db_buf
);
1537 dbuf_dirty_record_t
*
1538 dbuf_dirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1542 dbuf_dirty_record_t
**drp
, *dr
;
1543 int drop_struct_lock
= FALSE
;
1544 int txgoff
= tx
->tx_txg
& TXG_MASK
;
1546 ASSERT(tx
->tx_txg
!= 0);
1547 ASSERT(!refcount_is_zero(&db
->db_holds
));
1548 DMU_TX_DIRTY_BUF(tx
, db
);
1553 * Shouldn't dirty a regular buffer in syncing context. Private
1554 * objects may be dirtied in syncing context, but only if they
1555 * were already pre-dirtied in open context.
1558 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
) {
1559 rrw_enter(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
,
1562 ASSERT(!dmu_tx_is_syncing(tx
) ||
1563 BP_IS_HOLE(dn
->dn_objset
->os_rootbp
) ||
1564 DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
1565 dn
->dn_objset
->os_dsl_dataset
== NULL
);
1566 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
1567 rrw_exit(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
, FTAG
);
1570 * We make this assert for private objects as well, but after we
1571 * check if we're already dirty. They are allowed to re-dirty
1572 * in syncing context.
1574 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
1575 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
1576 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
1578 mutex_enter(&db
->db_mtx
);
1580 * XXX make this true for indirects too? The problem is that
1581 * transactions created with dmu_tx_create_assigned() from
1582 * syncing context don't bother holding ahead.
1584 ASSERT(db
->db_level
!= 0 ||
1585 db
->db_state
== DB_CACHED
|| db
->db_state
== DB_FILL
||
1586 db
->db_state
== DB_NOFILL
);
1588 mutex_enter(&dn
->dn_mtx
);
1590 * Don't set dirtyctx to SYNC if we're just modifying this as we
1591 * initialize the objset.
1593 if (dn
->dn_dirtyctx
== DN_UNDIRTIED
) {
1594 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
) {
1595 rrw_enter(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
,
1598 if (!BP_IS_HOLE(dn
->dn_objset
->os_rootbp
)) {
1599 dn
->dn_dirtyctx
= (dmu_tx_is_syncing(tx
) ?
1600 DN_DIRTY_SYNC
: DN_DIRTY_OPEN
);
1601 ASSERT(dn
->dn_dirtyctx_firstset
== NULL
);
1602 dn
->dn_dirtyctx_firstset
= kmem_alloc(1, KM_SLEEP
);
1604 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
) {
1605 rrw_exit(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
,
1610 if (tx
->tx_txg
> dn
->dn_dirty_txg
)
1611 dn
->dn_dirty_txg
= tx
->tx_txg
;
1612 mutex_exit(&dn
->dn_mtx
);
1614 if (db
->db_blkid
== DMU_SPILL_BLKID
)
1615 dn
->dn_have_spill
= B_TRUE
;
1618 * If this buffer is already dirty, we're done.
1620 drp
= &db
->db_last_dirty
;
1621 ASSERT(*drp
== NULL
|| (*drp
)->dr_txg
<= tx
->tx_txg
||
1622 db
->db
.db_object
== DMU_META_DNODE_OBJECT
);
1623 while ((dr
= *drp
) != NULL
&& dr
->dr_txg
> tx
->tx_txg
)
1625 if (dr
&& dr
->dr_txg
== tx
->tx_txg
) {
1629 mutex_exit(&db
->db_mtx
);
1634 * Only valid if not already dirty.
1636 ASSERT(dn
->dn_object
== 0 ||
1637 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
1638 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
1640 ASSERT3U(dn
->dn_nlevels
, >, db
->db_level
);
1643 * We should only be dirtying in syncing context if it's the
1644 * mos or we're initializing the os or it's a special object.
1645 * However, we are allowed to dirty in syncing context provided
1646 * we already dirtied it in open context. Hence we must make
1647 * this assertion only if we're not already dirty.
1650 VERIFY3U(tx
->tx_txg
, <=, spa_final_dirty_txg(os
->os_spa
));
1652 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
1653 rrw_enter(&os
->os_dsl_dataset
->ds_bp_rwlock
, RW_READER
, FTAG
);
1654 ASSERT(!dmu_tx_is_syncing(tx
) || DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
1655 os
->os_dsl_dataset
== NULL
|| BP_IS_HOLE(os
->os_rootbp
));
1656 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
1657 rrw_exit(&os
->os_dsl_dataset
->ds_bp_rwlock
, FTAG
);
1659 ASSERT(db
->db
.db_size
!= 0);
1661 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
1663 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
1664 dmu_objset_willuse_space(os
, db
->db
.db_size
, tx
);
1668 * If this buffer is dirty in an old transaction group we need
1669 * to make a copy of it so that the changes we make in this
1670 * transaction group won't leak out when we sync the older txg.
1672 dr
= kmem_zalloc(sizeof (dbuf_dirty_record_t
), KM_SLEEP
);
1673 list_link_init(&dr
->dr_dirty_node
);
1674 if (db
->db_level
== 0) {
1675 void *data_old
= db
->db_buf
;
1677 if (db
->db_state
!= DB_NOFILL
) {
1678 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1679 dbuf_fix_old_data(db
, tx
->tx_txg
);
1680 data_old
= db
->db
.db_data
;
1681 } else if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
) {
1683 * Release the data buffer from the cache so
1684 * that we can modify it without impacting
1685 * possible other users of this cached data
1686 * block. Note that indirect blocks and
1687 * private objects are not released until the
1688 * syncing state (since they are only modified
1691 arc_release(db
->db_buf
, db
);
1692 dbuf_fix_old_data(db
, tx
->tx_txg
);
1693 data_old
= db
->db_buf
;
1695 ASSERT(data_old
!= NULL
);
1697 dr
->dt
.dl
.dr_data
= data_old
;
1699 mutex_init(&dr
->dt
.di
.dr_mtx
, NULL
, MUTEX_NOLOCKDEP
, NULL
);
1700 list_create(&dr
->dt
.di
.dr_children
,
1701 sizeof (dbuf_dirty_record_t
),
1702 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
1704 if (db
->db_blkid
!= DMU_BONUS_BLKID
&& os
->os_dsl_dataset
!= NULL
)
1705 dr
->dr_accounted
= db
->db
.db_size
;
1707 dr
->dr_txg
= tx
->tx_txg
;
1712 * We could have been freed_in_flight between the dbuf_noread
1713 * and dbuf_dirty. We win, as though the dbuf_noread() had
1714 * happened after the free.
1716 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1717 db
->db_blkid
!= DMU_SPILL_BLKID
) {
1718 mutex_enter(&dn
->dn_mtx
);
1719 if (dn
->dn_free_ranges
[txgoff
] != NULL
) {
1720 range_tree_clear(dn
->dn_free_ranges
[txgoff
],
1723 mutex_exit(&dn
->dn_mtx
);
1724 db
->db_freed_in_flight
= FALSE
;
1728 * This buffer is now part of this txg
1730 dbuf_add_ref(db
, (void *)(uintptr_t)tx
->tx_txg
);
1731 db
->db_dirtycnt
+= 1;
1732 ASSERT3U(db
->db_dirtycnt
, <=, 3);
1734 mutex_exit(&db
->db_mtx
);
1736 if (db
->db_blkid
== DMU_BONUS_BLKID
||
1737 db
->db_blkid
== DMU_SPILL_BLKID
) {
1738 mutex_enter(&dn
->dn_mtx
);
1739 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1740 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
1741 mutex_exit(&dn
->dn_mtx
);
1742 dnode_setdirty(dn
, tx
);
1748 * The dn_struct_rwlock prevents db_blkptr from changing
1749 * due to a write from syncing context completing
1750 * while we are running, so we want to acquire it before
1751 * looking at db_blkptr.
1753 if (!RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
1754 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
1755 drop_struct_lock
= TRUE
;
1759 * We need to hold the dn_struct_rwlock to make this assertion,
1760 * because it protects dn_phys / dn_next_nlevels from changing.
1762 ASSERT((dn
->dn_phys
->dn_nlevels
== 0 && db
->db_level
== 0) ||
1763 dn
->dn_phys
->dn_nlevels
> db
->db_level
||
1764 dn
->dn_next_nlevels
[txgoff
] > db
->db_level
||
1765 dn
->dn_next_nlevels
[(tx
->tx_txg
-1) & TXG_MASK
] > db
->db_level
||
1766 dn
->dn_next_nlevels
[(tx
->tx_txg
-2) & TXG_MASK
] > db
->db_level
);
1769 * If we are overwriting a dedup BP, then unless it is snapshotted,
1770 * when we get to syncing context we will need to decrement its
1771 * refcount in the DDT. Prefetch the relevant DDT block so that
1772 * syncing context won't have to wait for the i/o.
1774 ddt_prefetch(os
->os_spa
, db
->db_blkptr
);
1776 if (db
->db_level
== 0) {
1777 dnode_new_blkid(dn
, db
->db_blkid
, tx
, drop_struct_lock
);
1778 ASSERT(dn
->dn_maxblkid
>= db
->db_blkid
);
1781 if (db
->db_level
+1 < dn
->dn_nlevels
) {
1782 dmu_buf_impl_t
*parent
= db
->db_parent
;
1783 dbuf_dirty_record_t
*di
;
1784 int parent_held
= FALSE
;
1786 if (db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
) {
1787 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1789 parent
= dbuf_hold_level(dn
, db
->db_level
+1,
1790 db
->db_blkid
>> epbs
, FTAG
);
1791 ASSERT(parent
!= NULL
);
1794 if (drop_struct_lock
)
1795 rw_exit(&dn
->dn_struct_rwlock
);
1796 ASSERT3U(db
->db_level
+1, ==, parent
->db_level
);
1797 di
= dbuf_dirty(parent
, tx
);
1799 dbuf_rele(parent
, FTAG
);
1801 mutex_enter(&db
->db_mtx
);
1803 * Since we've dropped the mutex, it's possible that
1804 * dbuf_undirty() might have changed this out from under us.
1806 if (db
->db_last_dirty
== dr
||
1807 dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
1808 mutex_enter(&di
->dt
.di
.dr_mtx
);
1809 ASSERT3U(di
->dr_txg
, ==, tx
->tx_txg
);
1810 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1811 list_insert_tail(&di
->dt
.di
.dr_children
, dr
);
1812 mutex_exit(&di
->dt
.di
.dr_mtx
);
1815 mutex_exit(&db
->db_mtx
);
1817 ASSERT(db
->db_level
+1 == dn
->dn_nlevels
);
1818 ASSERT(db
->db_blkid
< dn
->dn_nblkptr
);
1819 ASSERT(db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
1820 mutex_enter(&dn
->dn_mtx
);
1821 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1822 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
1823 mutex_exit(&dn
->dn_mtx
);
1824 if (drop_struct_lock
)
1825 rw_exit(&dn
->dn_struct_rwlock
);
1828 dnode_setdirty(dn
, tx
);
1834 * Undirty a buffer in the transaction group referenced by the given
1835 * transaction. Return whether this evicted the dbuf.
1838 dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1841 uint64_t txg
= tx
->tx_txg
;
1842 dbuf_dirty_record_t
*dr
, **drp
;
1847 * Due to our use of dn_nlevels below, this can only be called
1848 * in open context, unless we are operating on the MOS.
1849 * From syncing context, dn_nlevels may be different from the
1850 * dn_nlevels used when dbuf was dirtied.
1852 ASSERT(db
->db_objset
==
1853 dmu_objset_pool(db
->db_objset
)->dp_meta_objset
||
1854 txg
!= spa_syncing_txg(dmu_objset_spa(db
->db_objset
)));
1855 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1856 ASSERT0(db
->db_level
);
1857 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1860 * If this buffer is not dirty, we're done.
1862 for (drp
= &db
->db_last_dirty
; (dr
= *drp
) != NULL
; drp
= &dr
->dr_next
)
1863 if (dr
->dr_txg
<= txg
)
1865 if (dr
== NULL
|| dr
->dr_txg
< txg
)
1867 ASSERT(dr
->dr_txg
== txg
);
1868 ASSERT(dr
->dr_dbuf
== db
);
1873 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
1875 ASSERT(db
->db
.db_size
!= 0);
1877 dsl_pool_undirty_space(dmu_objset_pool(dn
->dn_objset
),
1878 dr
->dr_accounted
, txg
);
1883 * Note that there are three places in dbuf_dirty()
1884 * where this dirty record may be put on a list.
1885 * Make sure to do a list_remove corresponding to
1886 * every one of those list_insert calls.
1888 if (dr
->dr_parent
) {
1889 mutex_enter(&dr
->dr_parent
->dt
.di
.dr_mtx
);
1890 list_remove(&dr
->dr_parent
->dt
.di
.dr_children
, dr
);
1891 mutex_exit(&dr
->dr_parent
->dt
.di
.dr_mtx
);
1892 } else if (db
->db_blkid
== DMU_SPILL_BLKID
||
1893 db
->db_level
+ 1 == dn
->dn_nlevels
) {
1894 ASSERT(db
->db_blkptr
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
1895 mutex_enter(&dn
->dn_mtx
);
1896 list_remove(&dn
->dn_dirty_records
[txg
& TXG_MASK
], dr
);
1897 mutex_exit(&dn
->dn_mtx
);
1901 if (db
->db_state
!= DB_NOFILL
) {
1902 dbuf_unoverride(dr
);
1904 ASSERT(db
->db_buf
!= NULL
);
1905 ASSERT(dr
->dt
.dl
.dr_data
!= NULL
);
1906 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
1907 arc_buf_destroy(dr
->dt
.dl
.dr_data
, db
);
1910 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
1912 ASSERT(db
->db_dirtycnt
> 0);
1913 db
->db_dirtycnt
-= 1;
1915 if (refcount_remove(&db
->db_holds
, (void *)(uintptr_t)txg
) == 0) {
1916 ASSERT(db
->db_state
== DB_NOFILL
|| arc_released(db
->db_buf
));
1925 dmu_buf_will_dirty(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1927 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1928 int rf
= DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
;
1929 dbuf_dirty_record_t
*dr
;
1931 ASSERT(tx
->tx_txg
!= 0);
1932 ASSERT(!refcount_is_zero(&db
->db_holds
));
1935 * Quick check for dirtyness. For already dirty blocks, this
1936 * reduces runtime of this function by >90%, and overall performance
1937 * by 50% for some workloads (e.g. file deletion with indirect blocks
1940 mutex_enter(&db
->db_mtx
);
1942 for (dr
= db
->db_last_dirty
;
1943 dr
!= NULL
&& dr
->dr_txg
>= tx
->tx_txg
; dr
= dr
->dr_next
) {
1945 * It's possible that it is already dirty but not cached,
1946 * because there are some calls to dbuf_dirty() that don't
1947 * go through dmu_buf_will_dirty().
1949 if (dr
->dr_txg
== tx
->tx_txg
&& db
->db_state
== DB_CACHED
) {
1950 /* This dbuf is already dirty and cached. */
1952 mutex_exit(&db
->db_mtx
);
1956 mutex_exit(&db
->db_mtx
);
1959 if (RW_WRITE_HELD(&DB_DNODE(db
)->dn_struct_rwlock
))
1960 rf
|= DB_RF_HAVESTRUCT
;
1962 (void) dbuf_read(db
, NULL
, rf
);
1963 (void) dbuf_dirty(db
, tx
);
1967 dmu_buf_will_not_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1969 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1971 db
->db_state
= DB_NOFILL
;
1973 dmu_buf_will_fill(db_fake
, tx
);
1977 dmu_buf_will_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1979 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1981 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1982 ASSERT(tx
->tx_txg
!= 0);
1983 ASSERT(db
->db_level
== 0);
1984 ASSERT(!refcount_is_zero(&db
->db_holds
));
1986 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
||
1987 dmu_tx_private_ok(tx
));
1990 (void) dbuf_dirty(db
, tx
);
1993 #pragma weak dmu_buf_fill_done = dbuf_fill_done
1996 dbuf_fill_done(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1998 mutex_enter(&db
->db_mtx
);
2001 if (db
->db_state
== DB_FILL
) {
2002 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
2003 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2004 /* we were freed while filling */
2005 /* XXX dbuf_undirty? */
2006 bzero(db
->db
.db_data
, db
->db
.db_size
);
2007 db
->db_freed_in_flight
= FALSE
;
2009 db
->db_state
= DB_CACHED
;
2010 cv_broadcast(&db
->db_changed
);
2012 mutex_exit(&db
->db_mtx
);
2016 dmu_buf_write_embedded(dmu_buf_t
*dbuf
, void *data
,
2017 bp_embedded_type_t etype
, enum zio_compress comp
,
2018 int uncompressed_size
, int compressed_size
, int byteorder
,
2021 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbuf
;
2022 struct dirty_leaf
*dl
;
2023 dmu_object_type_t type
;
2025 if (etype
== BP_EMBEDDED_TYPE_DATA
) {
2026 ASSERT(spa_feature_is_active(dmu_objset_spa(db
->db_objset
),
2027 SPA_FEATURE_EMBEDDED_DATA
));
2031 type
= DB_DNODE(db
)->dn_type
;
2034 ASSERT0(db
->db_level
);
2035 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2037 dmu_buf_will_not_fill(dbuf
, tx
);
2039 ASSERT3U(db
->db_last_dirty
->dr_txg
, ==, tx
->tx_txg
);
2040 dl
= &db
->db_last_dirty
->dt
.dl
;
2041 encode_embedded_bp_compressed(&dl
->dr_overridden_by
,
2042 data
, comp
, uncompressed_size
, compressed_size
);
2043 BPE_SET_ETYPE(&dl
->dr_overridden_by
, etype
);
2044 BP_SET_TYPE(&dl
->dr_overridden_by
, type
);
2045 BP_SET_LEVEL(&dl
->dr_overridden_by
, 0);
2046 BP_SET_BYTEORDER(&dl
->dr_overridden_by
, byteorder
);
2048 dl
->dr_override_state
= DR_OVERRIDDEN
;
2049 dl
->dr_overridden_by
.blk_birth
= db
->db_last_dirty
->dr_txg
;
2053 * Directly assign a provided arc buf to a given dbuf if it's not referenced
2054 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
2057 dbuf_assign_arcbuf(dmu_buf_impl_t
*db
, arc_buf_t
*buf
, dmu_tx_t
*tx
)
2059 ASSERT(!refcount_is_zero(&db
->db_holds
));
2060 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2061 ASSERT(db
->db_level
== 0);
2062 ASSERT3U(dbuf_is_metadata(db
), ==, arc_is_metadata(buf
));
2063 ASSERT(buf
!= NULL
);
2064 ASSERT(arc_buf_lsize(buf
) == db
->db
.db_size
);
2065 ASSERT(tx
->tx_txg
!= 0);
2067 arc_return_buf(buf
, db
);
2068 ASSERT(arc_released(buf
));
2070 mutex_enter(&db
->db_mtx
);
2072 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
2073 cv_wait(&db
->db_changed
, &db
->db_mtx
);
2075 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_UNCACHED
);
2077 if (db
->db_state
== DB_CACHED
&&
2078 refcount_count(&db
->db_holds
) - 1 > db
->db_dirtycnt
) {
2079 mutex_exit(&db
->db_mtx
);
2080 (void) dbuf_dirty(db
, tx
);
2081 bcopy(buf
->b_data
, db
->db
.db_data
, db
->db
.db_size
);
2082 arc_buf_destroy(buf
, db
);
2083 xuio_stat_wbuf_copied();
2087 xuio_stat_wbuf_nocopy();
2088 if (db
->db_state
== DB_CACHED
) {
2089 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
2091 ASSERT(db
->db_buf
!= NULL
);
2092 if (dr
!= NULL
&& dr
->dr_txg
== tx
->tx_txg
) {
2093 ASSERT(dr
->dt
.dl
.dr_data
== db
->db_buf
);
2094 if (!arc_released(db
->db_buf
)) {
2095 ASSERT(dr
->dt
.dl
.dr_override_state
==
2097 arc_release(db
->db_buf
, db
);
2099 dr
->dt
.dl
.dr_data
= buf
;
2100 arc_buf_destroy(db
->db_buf
, db
);
2101 } else if (dr
== NULL
|| dr
->dt
.dl
.dr_data
!= db
->db_buf
) {
2102 arc_release(db
->db_buf
, db
);
2103 arc_buf_destroy(db
->db_buf
, db
);
2107 ASSERT(db
->db_buf
== NULL
);
2108 dbuf_set_data(db
, buf
);
2109 db
->db_state
= DB_FILL
;
2110 mutex_exit(&db
->db_mtx
);
2111 (void) dbuf_dirty(db
, tx
);
2112 dmu_buf_fill_done(&db
->db
, tx
);
2116 dbuf_destroy(dmu_buf_impl_t
*db
)
2119 dmu_buf_impl_t
*parent
= db
->db_parent
;
2120 dmu_buf_impl_t
*dndb
;
2122 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2123 ASSERT(refcount_is_zero(&db
->db_holds
));
2125 if (db
->db_buf
!= NULL
) {
2126 arc_buf_destroy(db
->db_buf
, db
);
2130 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2131 int slots
= DB_DNODE(db
)->dn_num_slots
;
2132 int bonuslen
= DN_SLOTS_TO_BONUSLEN(slots
);
2133 ASSERT(db
->db
.db_data
!= NULL
);
2134 kmem_free(db
->db
.db_data
, bonuslen
);
2135 arc_space_return(bonuslen
, ARC_SPACE_BONUS
);
2136 db
->db_state
= DB_UNCACHED
;
2139 dbuf_clear_data(db
);
2141 if (multilist_link_active(&db
->db_cache_link
)) {
2142 multilist_remove(dbuf_cache
, db
);
2143 (void) refcount_remove_many(&dbuf_cache_size
,
2144 db
->db
.db_size
, db
);
2147 ASSERT(db
->db_state
== DB_UNCACHED
|| db
->db_state
== DB_NOFILL
);
2148 ASSERT(db
->db_data_pending
== NULL
);
2150 db
->db_state
= DB_EVICTING
;
2151 db
->db_blkptr
= NULL
;
2154 * Now that db_state is DB_EVICTING, nobody else can find this via
2155 * the hash table. We can now drop db_mtx, which allows us to
2156 * acquire the dn_dbufs_mtx.
2158 mutex_exit(&db
->db_mtx
);
2163 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
2164 boolean_t needlock
= !MUTEX_HELD(&dn
->dn_dbufs_mtx
);
2166 mutex_enter(&dn
->dn_dbufs_mtx
);
2167 avl_remove(&dn
->dn_dbufs
, db
);
2168 atomic_dec_32(&dn
->dn_dbufs_count
);
2172 mutex_exit(&dn
->dn_dbufs_mtx
);
2174 * Decrementing the dbuf count means that the hold corresponding
2175 * to the removed dbuf is no longer discounted in dnode_move(),
2176 * so the dnode cannot be moved until after we release the hold.
2177 * The membar_producer() ensures visibility of the decremented
2178 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
2182 db
->db_dnode_handle
= NULL
;
2184 dbuf_hash_remove(db
);
2189 ASSERT(refcount_is_zero(&db
->db_holds
));
2191 db
->db_parent
= NULL
;
2193 ASSERT(db
->db_buf
== NULL
);
2194 ASSERT(db
->db
.db_data
== NULL
);
2195 ASSERT(db
->db_hash_next
== NULL
);
2196 ASSERT(db
->db_blkptr
== NULL
);
2197 ASSERT(db
->db_data_pending
== NULL
);
2198 ASSERT(!multilist_link_active(&db
->db_cache_link
));
2200 kmem_cache_free(dbuf_kmem_cache
, db
);
2201 arc_space_return(sizeof (dmu_buf_impl_t
), ARC_SPACE_DBUF
);
2204 * If this dbuf is referenced from an indirect dbuf,
2205 * decrement the ref count on the indirect dbuf.
2207 if (parent
&& parent
!= dndb
)
2208 dbuf_rele(parent
, db
);
2212 * Note: While bpp will always be updated if the function returns success,
2213 * parentp will not be updated if the dnode does not have dn_dbuf filled in;
2214 * this happens when the dnode is the meta-dnode, or a userused or groupused
2217 __attribute__((always_inline
))
2219 dbuf_findbp(dnode_t
*dn
, int level
, uint64_t blkid
, int fail_sparse
,
2220 dmu_buf_impl_t
**parentp
, blkptr_t
**bpp
, struct dbuf_hold_impl_data
*dh
)
2227 ASSERT(blkid
!= DMU_BONUS_BLKID
);
2229 if (blkid
== DMU_SPILL_BLKID
) {
2230 mutex_enter(&dn
->dn_mtx
);
2231 if (dn
->dn_have_spill
&&
2232 (dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
))
2233 *bpp
= DN_SPILL_BLKPTR(dn
->dn_phys
);
2236 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
2237 *parentp
= dn
->dn_dbuf
;
2238 mutex_exit(&dn
->dn_mtx
);
2243 (dn
->dn_phys
->dn_nlevels
== 0) ? 1 : dn
->dn_phys
->dn_nlevels
;
2244 epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2246 ASSERT3U(level
* epbs
, <, 64);
2247 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2249 * This assertion shouldn't trip as long as the max indirect block size
2250 * is less than 1M. The reason for this is that up to that point,
2251 * the number of levels required to address an entire object with blocks
2252 * of size SPA_MINBLOCKSIZE satisfies nlevels * epbs + 1 <= 64. In
2253 * other words, if N * epbs + 1 > 64, then if (N-1) * epbs + 1 > 55
2254 * (i.e. we can address the entire object), objects will all use at most
2255 * N-1 levels and the assertion won't overflow. However, once epbs is
2256 * 13, 4 * 13 + 1 = 53, but 5 * 13 + 1 = 66. Then, 4 levels will not be
2257 * enough to address an entire object, so objects will have 5 levels,
2258 * but then this assertion will overflow.
2260 * All this is to say that if we ever increase DN_MAX_INDBLKSHIFT, we
2261 * need to redo this logic to handle overflows.
2263 ASSERT(level
>= nlevels
||
2264 ((nlevels
- level
- 1) * epbs
) +
2265 highbit64(dn
->dn_phys
->dn_nblkptr
) <= 64);
2266 if (level
>= nlevels
||
2267 blkid
>= ((uint64_t)dn
->dn_phys
->dn_nblkptr
<<
2268 ((nlevels
- level
- 1) * epbs
)) ||
2270 blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))) {
2271 /* the buffer has no parent yet */
2272 return (SET_ERROR(ENOENT
));
2273 } else if (level
< nlevels
-1) {
2274 /* this block is referenced from an indirect block */
2277 err
= dbuf_hold_impl(dn
, level
+1,
2278 blkid
>> epbs
, fail_sparse
, FALSE
, NULL
, parentp
);
2280 __dbuf_hold_impl_init(dh
+ 1, dn
, dh
->dh_level
+ 1,
2281 blkid
>> epbs
, fail_sparse
, FALSE
, NULL
,
2282 parentp
, dh
->dh_depth
+ 1);
2283 err
= __dbuf_hold_impl(dh
+ 1);
2287 err
= dbuf_read(*parentp
, NULL
,
2288 (DB_RF_HAVESTRUCT
| DB_RF_NOPREFETCH
| DB_RF_CANFAIL
));
2290 dbuf_rele(*parentp
, NULL
);
2294 *bpp
= ((blkptr_t
*)(*parentp
)->db
.db_data
) +
2295 (blkid
& ((1ULL << epbs
) - 1));
2296 if (blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))
2297 ASSERT(BP_IS_HOLE(*bpp
));
2300 /* the block is referenced from the dnode */
2301 ASSERT3U(level
, ==, nlevels
-1);
2302 ASSERT(dn
->dn_phys
->dn_nblkptr
== 0 ||
2303 blkid
< dn
->dn_phys
->dn_nblkptr
);
2305 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
2306 *parentp
= dn
->dn_dbuf
;
2308 *bpp
= &dn
->dn_phys
->dn_blkptr
[blkid
];
2313 static dmu_buf_impl_t
*
2314 dbuf_create(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
2315 dmu_buf_impl_t
*parent
, blkptr_t
*blkptr
)
2317 objset_t
*os
= dn
->dn_objset
;
2318 dmu_buf_impl_t
*db
, *odb
;
2320 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2321 ASSERT(dn
->dn_type
!= DMU_OT_NONE
);
2323 db
= kmem_cache_alloc(dbuf_kmem_cache
, KM_SLEEP
);
2326 db
->db
.db_object
= dn
->dn_object
;
2327 db
->db_level
= level
;
2328 db
->db_blkid
= blkid
;
2329 db
->db_last_dirty
= NULL
;
2330 db
->db_dirtycnt
= 0;
2331 db
->db_dnode_handle
= dn
->dn_handle
;
2332 db
->db_parent
= parent
;
2333 db
->db_blkptr
= blkptr
;
2336 db
->db_user_immediate_evict
= FALSE
;
2337 db
->db_freed_in_flight
= FALSE
;
2338 db
->db_pending_evict
= FALSE
;
2340 if (blkid
== DMU_BONUS_BLKID
) {
2341 ASSERT3P(parent
, ==, dn
->dn_dbuf
);
2342 db
->db
.db_size
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
) -
2343 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
);
2344 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
2345 db
->db
.db_offset
= DMU_BONUS_BLKID
;
2346 db
->db_state
= DB_UNCACHED
;
2347 /* the bonus dbuf is not placed in the hash table */
2348 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_DBUF
);
2350 } else if (blkid
== DMU_SPILL_BLKID
) {
2351 db
->db
.db_size
= (blkptr
!= NULL
) ?
2352 BP_GET_LSIZE(blkptr
) : SPA_MINBLOCKSIZE
;
2353 db
->db
.db_offset
= 0;
2356 db
->db_level
? 1 << dn
->dn_indblkshift
: dn
->dn_datablksz
;
2357 db
->db
.db_size
= blocksize
;
2358 db
->db
.db_offset
= db
->db_blkid
* blocksize
;
2362 * Hold the dn_dbufs_mtx while we get the new dbuf
2363 * in the hash table *and* added to the dbufs list.
2364 * This prevents a possible deadlock with someone
2365 * trying to look up this dbuf before its added to the
2368 mutex_enter(&dn
->dn_dbufs_mtx
);
2369 db
->db_state
= DB_EVICTING
;
2370 if ((odb
= dbuf_hash_insert(db
)) != NULL
) {
2371 /* someone else inserted it first */
2372 kmem_cache_free(dbuf_kmem_cache
, db
);
2373 mutex_exit(&dn
->dn_dbufs_mtx
);
2376 avl_add(&dn
->dn_dbufs
, db
);
2378 db
->db_state
= DB_UNCACHED
;
2379 mutex_exit(&dn
->dn_dbufs_mtx
);
2380 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_DBUF
);
2382 if (parent
&& parent
!= dn
->dn_dbuf
)
2383 dbuf_add_ref(parent
, db
);
2385 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
2386 refcount_count(&dn
->dn_holds
) > 0);
2387 (void) refcount_add(&dn
->dn_holds
, db
);
2388 atomic_inc_32(&dn
->dn_dbufs_count
);
2390 dprintf_dbuf(db
, "db=%p\n", db
);
2395 typedef struct dbuf_prefetch_arg
{
2396 spa_t
*dpa_spa
; /* The spa to issue the prefetch in. */
2397 zbookmark_phys_t dpa_zb
; /* The target block to prefetch. */
2398 int dpa_epbs
; /* Entries (blkptr_t's) Per Block Shift. */
2399 int dpa_curlevel
; /* The current level that we're reading */
2400 dnode_t
*dpa_dnode
; /* The dnode associated with the prefetch */
2401 zio_priority_t dpa_prio
; /* The priority I/Os should be issued at. */
2402 zio_t
*dpa_zio
; /* The parent zio_t for all prefetches. */
2403 arc_flags_t dpa_aflags
; /* Flags to pass to the final prefetch. */
2404 } dbuf_prefetch_arg_t
;
2407 * Actually issue the prefetch read for the block given.
2410 dbuf_issue_final_prefetch(dbuf_prefetch_arg_t
*dpa
, blkptr_t
*bp
)
2413 if (BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
))
2416 aflags
= dpa
->dpa_aflags
| ARC_FLAG_NOWAIT
| ARC_FLAG_PREFETCH
;
2418 ASSERT3U(dpa
->dpa_curlevel
, ==, BP_GET_LEVEL(bp
));
2419 ASSERT3U(dpa
->dpa_curlevel
, ==, dpa
->dpa_zb
.zb_level
);
2420 ASSERT(dpa
->dpa_zio
!= NULL
);
2421 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
, bp
, NULL
, NULL
,
2422 dpa
->dpa_prio
, ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
2423 &aflags
, &dpa
->dpa_zb
);
2427 * Called when an indirect block above our prefetch target is read in. This
2428 * will either read in the next indirect block down the tree or issue the actual
2429 * prefetch if the next block down is our target.
2432 dbuf_prefetch_indirect_done(zio_t
*zio
, arc_buf_t
*abuf
, void *private)
2434 dbuf_prefetch_arg_t
*dpa
= private;
2438 ASSERT3S(dpa
->dpa_zb
.zb_level
, <, dpa
->dpa_curlevel
);
2439 ASSERT3S(dpa
->dpa_curlevel
, >, 0);
2442 * The dpa_dnode is only valid if we are called with a NULL
2443 * zio. This indicates that the arc_read() returned without
2444 * first calling zio_read() to issue a physical read. Once
2445 * a physical read is made the dpa_dnode must be invalidated
2446 * as the locks guarding it may have been dropped. If the
2447 * dpa_dnode is still valid, then we want to add it to the dbuf
2448 * cache. To do so, we must hold the dbuf associated with the block
2449 * we just prefetched, read its contents so that we associate it
2450 * with an arc_buf_t, and then release it.
2453 ASSERT3S(BP_GET_LEVEL(zio
->io_bp
), ==, dpa
->dpa_curlevel
);
2454 if (zio
->io_flags
& ZIO_FLAG_RAW
) {
2455 ASSERT3U(BP_GET_PSIZE(zio
->io_bp
), ==, zio
->io_size
);
2457 ASSERT3U(BP_GET_LSIZE(zio
->io_bp
), ==, zio
->io_size
);
2459 ASSERT3P(zio
->io_spa
, ==, dpa
->dpa_spa
);
2461 dpa
->dpa_dnode
= NULL
;
2462 } else if (dpa
->dpa_dnode
!= NULL
) {
2463 uint64_t curblkid
= dpa
->dpa_zb
.zb_blkid
>>
2464 (dpa
->dpa_epbs
* (dpa
->dpa_curlevel
-
2465 dpa
->dpa_zb
.zb_level
));
2466 dmu_buf_impl_t
*db
= dbuf_hold_level(dpa
->dpa_dnode
,
2467 dpa
->dpa_curlevel
, curblkid
, FTAG
);
2468 (void) dbuf_read(db
, NULL
,
2469 DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
| DB_RF_HAVESTRUCT
);
2470 dbuf_rele(db
, FTAG
);
2473 dpa
->dpa_curlevel
--;
2475 nextblkid
= dpa
->dpa_zb
.zb_blkid
>>
2476 (dpa
->dpa_epbs
* (dpa
->dpa_curlevel
- dpa
->dpa_zb
.zb_level
));
2477 bp
= ((blkptr_t
*)abuf
->b_data
) +
2478 P2PHASE(nextblkid
, 1ULL << dpa
->dpa_epbs
);
2479 if (BP_IS_HOLE(bp
) || (zio
!= NULL
&& zio
->io_error
!= 0)) {
2480 kmem_free(dpa
, sizeof (*dpa
));
2481 } else if (dpa
->dpa_curlevel
== dpa
->dpa_zb
.zb_level
) {
2482 ASSERT3U(nextblkid
, ==, dpa
->dpa_zb
.zb_blkid
);
2483 dbuf_issue_final_prefetch(dpa
, bp
);
2484 kmem_free(dpa
, sizeof (*dpa
));
2486 arc_flags_t iter_aflags
= ARC_FLAG_NOWAIT
;
2487 zbookmark_phys_t zb
;
2489 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
2490 if (dpa
->dpa_aflags
& ARC_FLAG_L2CACHE
)
2491 iter_aflags
|= ARC_FLAG_L2CACHE
;
2493 ASSERT3U(dpa
->dpa_curlevel
, ==, BP_GET_LEVEL(bp
));
2495 SET_BOOKMARK(&zb
, dpa
->dpa_zb
.zb_objset
,
2496 dpa
->dpa_zb
.zb_object
, dpa
->dpa_curlevel
, nextblkid
);
2498 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
,
2499 bp
, dbuf_prefetch_indirect_done
, dpa
, dpa
->dpa_prio
,
2500 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
2504 arc_buf_destroy(abuf
, private);
2508 * Issue prefetch reads for the given block on the given level. If the indirect
2509 * blocks above that block are not in memory, we will read them in
2510 * asynchronously. As a result, this call never blocks waiting for a read to
2514 dbuf_prefetch(dnode_t
*dn
, int64_t level
, uint64_t blkid
, zio_priority_t prio
,
2518 int epbs
, nlevels
, curlevel
;
2522 dbuf_prefetch_arg_t
*dpa
;
2525 ASSERT(blkid
!= DMU_BONUS_BLKID
);
2526 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2528 if (blkid
> dn
->dn_maxblkid
)
2531 if (dnode_block_freed(dn
, blkid
))
2535 * This dnode hasn't been written to disk yet, so there's nothing to
2538 nlevels
= dn
->dn_phys
->dn_nlevels
;
2539 if (level
>= nlevels
|| dn
->dn_phys
->dn_nblkptr
== 0)
2542 epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2543 if (dn
->dn_phys
->dn_maxblkid
< blkid
<< (epbs
* level
))
2546 db
= dbuf_find(dn
->dn_objset
, dn
->dn_object
,
2549 mutex_exit(&db
->db_mtx
);
2551 * This dbuf already exists. It is either CACHED, or
2552 * (we assume) about to be read or filled.
2558 * Find the closest ancestor (indirect block) of the target block
2559 * that is present in the cache. In this indirect block, we will
2560 * find the bp that is at curlevel, curblkid.
2564 while (curlevel
< nlevels
- 1) {
2565 int parent_level
= curlevel
+ 1;
2566 uint64_t parent_blkid
= curblkid
>> epbs
;
2569 if (dbuf_hold_impl(dn
, parent_level
, parent_blkid
,
2570 FALSE
, TRUE
, FTAG
, &db
) == 0) {
2571 blkptr_t
*bpp
= db
->db_buf
->b_data
;
2572 bp
= bpp
[P2PHASE(curblkid
, 1 << epbs
)];
2573 dbuf_rele(db
, FTAG
);
2577 curlevel
= parent_level
;
2578 curblkid
= parent_blkid
;
2581 if (curlevel
== nlevels
- 1) {
2582 /* No cached indirect blocks found. */
2583 ASSERT3U(curblkid
, <, dn
->dn_phys
->dn_nblkptr
);
2584 bp
= dn
->dn_phys
->dn_blkptr
[curblkid
];
2586 if (BP_IS_HOLE(&bp
))
2589 ASSERT3U(curlevel
, ==, BP_GET_LEVEL(&bp
));
2591 pio
= zio_root(dmu_objset_spa(dn
->dn_objset
), NULL
, NULL
,
2594 dpa
= kmem_zalloc(sizeof (*dpa
), KM_SLEEP
);
2595 ds
= dn
->dn_objset
->os_dsl_dataset
;
2596 SET_BOOKMARK(&dpa
->dpa_zb
, ds
!= NULL
? ds
->ds_object
: DMU_META_OBJSET
,
2597 dn
->dn_object
, level
, blkid
);
2598 dpa
->dpa_curlevel
= curlevel
;
2599 dpa
->dpa_prio
= prio
;
2600 dpa
->dpa_aflags
= aflags
;
2601 dpa
->dpa_spa
= dn
->dn_objset
->os_spa
;
2602 dpa
->dpa_dnode
= dn
;
2603 dpa
->dpa_epbs
= epbs
;
2606 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
2607 if (DNODE_LEVEL_IS_L2CACHEABLE(dn
, level
))
2608 dpa
->dpa_aflags
|= ARC_FLAG_L2CACHE
;
2611 * If we have the indirect just above us, no need to do the asynchronous
2612 * prefetch chain; we'll just run the last step ourselves. If we're at
2613 * a higher level, though, we want to issue the prefetches for all the
2614 * indirect blocks asynchronously, so we can go on with whatever we were
2617 if (curlevel
== level
) {
2618 ASSERT3U(curblkid
, ==, blkid
);
2619 dbuf_issue_final_prefetch(dpa
, &bp
);
2620 kmem_free(dpa
, sizeof (*dpa
));
2622 arc_flags_t iter_aflags
= ARC_FLAG_NOWAIT
;
2623 zbookmark_phys_t zb
;
2625 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
2626 if (DNODE_LEVEL_IS_L2CACHEABLE(dn
, level
))
2627 iter_aflags
|= ARC_FLAG_L2CACHE
;
2629 SET_BOOKMARK(&zb
, ds
!= NULL
? ds
->ds_object
: DMU_META_OBJSET
,
2630 dn
->dn_object
, curlevel
, curblkid
);
2631 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
,
2632 &bp
, dbuf_prefetch_indirect_done
, dpa
, prio
,
2633 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
2637 * We use pio here instead of dpa_zio since it's possible that
2638 * dpa may have already been freed.
2643 #define DBUF_HOLD_IMPL_MAX_DEPTH 20
2646 * Helper function for __dbuf_hold_impl() to copy a buffer. Handles
2647 * the case of compressed and uncompressed buffers by allocating the
2648 * new buffer, respectively, with arc_alloc_raw_buf(),
2649 * arc_alloc_compressed_buf() or arc_alloc_buf().*
2651 * NOTE: Declared noinline to avoid stack bloat in __dbuf_hold_impl().
2653 noinline
static void
2654 dbuf_hold_copy(struct dbuf_hold_impl_data
*dh
)
2656 dnode_t
*dn
= dh
->dh_dn
;
2657 dmu_buf_impl_t
*db
= dh
->dh_db
;
2658 dbuf_dirty_record_t
*dr
= dh
->dh_dr
;
2659 arc_buf_t
*data
= dr
->dt
.dl
.dr_data
;
2661 enum zio_compress compress_type
= arc_get_compression(data
);
2663 if (compress_type
!= ZIO_COMPRESS_OFF
) {
2664 dbuf_set_data(db
, arc_alloc_compressed_buf(
2665 dn
->dn_objset
->os_spa
, db
, arc_buf_size(data
),
2666 arc_buf_lsize(data
), compress_type
));
2668 dbuf_set_data(db
, arc_alloc_buf(dn
->dn_objset
->os_spa
, db
,
2669 DBUF_GET_BUFC_TYPE(db
), db
->db
.db_size
));
2672 bcopy(data
->b_data
, db
->db
.db_data
, arc_buf_size(data
));
2676 * Returns with db_holds incremented, and db_mtx not held.
2677 * Note: dn_struct_rwlock must be held.
2680 __dbuf_hold_impl(struct dbuf_hold_impl_data
*dh
)
2682 ASSERT3S(dh
->dh_depth
, <, DBUF_HOLD_IMPL_MAX_DEPTH
);
2683 dh
->dh_parent
= NULL
;
2685 ASSERT(dh
->dh_blkid
!= DMU_BONUS_BLKID
);
2686 ASSERT(RW_LOCK_HELD(&dh
->dh_dn
->dn_struct_rwlock
));
2687 ASSERT3U(dh
->dh_dn
->dn_nlevels
, >, dh
->dh_level
);
2689 *(dh
->dh_dbp
) = NULL
;
2691 /* dbuf_find() returns with db_mtx held */
2692 dh
->dh_db
= dbuf_find(dh
->dh_dn
->dn_objset
, dh
->dh_dn
->dn_object
,
2693 dh
->dh_level
, dh
->dh_blkid
);
2695 if (dh
->dh_db
== NULL
) {
2698 if (dh
->dh_fail_uncached
)
2699 return (SET_ERROR(ENOENT
));
2701 ASSERT3P(dh
->dh_parent
, ==, NULL
);
2702 dh
->dh_err
= dbuf_findbp(dh
->dh_dn
, dh
->dh_level
, dh
->dh_blkid
,
2703 dh
->dh_fail_sparse
, &dh
->dh_parent
, &dh
->dh_bp
, dh
);
2704 if (dh
->dh_fail_sparse
) {
2705 if (dh
->dh_err
== 0 &&
2706 dh
->dh_bp
&& BP_IS_HOLE(dh
->dh_bp
))
2707 dh
->dh_err
= SET_ERROR(ENOENT
);
2710 dbuf_rele(dh
->dh_parent
, NULL
);
2711 return (dh
->dh_err
);
2714 if (dh
->dh_err
&& dh
->dh_err
!= ENOENT
)
2715 return (dh
->dh_err
);
2716 dh
->dh_db
= dbuf_create(dh
->dh_dn
, dh
->dh_level
, dh
->dh_blkid
,
2717 dh
->dh_parent
, dh
->dh_bp
);
2720 if (dh
->dh_fail_uncached
&& dh
->dh_db
->db_state
!= DB_CACHED
) {
2721 mutex_exit(&dh
->dh_db
->db_mtx
);
2722 return (SET_ERROR(ENOENT
));
2725 if (dh
->dh_db
->db_buf
!= NULL
) {
2726 arc_buf_access(dh
->dh_db
->db_buf
);
2727 ASSERT3P(dh
->dh_db
->db
.db_data
, ==, dh
->dh_db
->db_buf
->b_data
);
2730 ASSERT(dh
->dh_db
->db_buf
== NULL
|| arc_referenced(dh
->dh_db
->db_buf
));
2733 * If this buffer is currently syncing out, and we are are
2734 * still referencing it from db_data, we need to make a copy
2735 * of it in case we decide we want to dirty it again in this txg.
2737 if (dh
->dh_db
->db_level
== 0 &&
2738 dh
->dh_db
->db_blkid
!= DMU_BONUS_BLKID
&&
2739 dh
->dh_dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
2740 dh
->dh_db
->db_state
== DB_CACHED
&& dh
->dh_db
->db_data_pending
) {
2741 dh
->dh_dr
= dh
->dh_db
->db_data_pending
;
2742 if (dh
->dh_dr
->dt
.dl
.dr_data
== dh
->dh_db
->db_buf
)
2746 if (multilist_link_active(&dh
->dh_db
->db_cache_link
)) {
2747 ASSERT(refcount_is_zero(&dh
->dh_db
->db_holds
));
2748 multilist_remove(dbuf_cache
, dh
->dh_db
);
2749 (void) refcount_remove_many(&dbuf_cache_size
,
2750 dh
->dh_db
->db
.db_size
, dh
->dh_db
);
2752 (void) refcount_add(&dh
->dh_db
->db_holds
, dh
->dh_tag
);
2753 DBUF_VERIFY(dh
->dh_db
);
2754 mutex_exit(&dh
->dh_db
->db_mtx
);
2756 /* NOTE: we can't rele the parent until after we drop the db_mtx */
2758 dbuf_rele(dh
->dh_parent
, NULL
);
2760 ASSERT3P(DB_DNODE(dh
->dh_db
), ==, dh
->dh_dn
);
2761 ASSERT3U(dh
->dh_db
->db_blkid
, ==, dh
->dh_blkid
);
2762 ASSERT3U(dh
->dh_db
->db_level
, ==, dh
->dh_level
);
2763 *(dh
->dh_dbp
) = dh
->dh_db
;
2769 * The following code preserves the recursive function dbuf_hold_impl()
2770 * but moves the local variables AND function arguments to the heap to
2771 * minimize the stack frame size. Enough space is initially allocated
2772 * on the stack for 20 levels of recursion.
2775 dbuf_hold_impl(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
2776 boolean_t fail_sparse
, boolean_t fail_uncached
,
2777 void *tag
, dmu_buf_impl_t
**dbp
)
2779 struct dbuf_hold_impl_data
*dh
;
2782 dh
= kmem_alloc(sizeof (struct dbuf_hold_impl_data
) *
2783 DBUF_HOLD_IMPL_MAX_DEPTH
, KM_SLEEP
);
2784 __dbuf_hold_impl_init(dh
, dn
, level
, blkid
, fail_sparse
,
2785 fail_uncached
, tag
, dbp
, 0);
2787 error
= __dbuf_hold_impl(dh
);
2789 kmem_free(dh
, sizeof (struct dbuf_hold_impl_data
) *
2790 DBUF_HOLD_IMPL_MAX_DEPTH
);
2796 __dbuf_hold_impl_init(struct dbuf_hold_impl_data
*dh
,
2797 dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
2798 boolean_t fail_sparse
, boolean_t fail_uncached
,
2799 void *tag
, dmu_buf_impl_t
**dbp
, int depth
)
2802 dh
->dh_level
= level
;
2803 dh
->dh_blkid
= blkid
;
2805 dh
->dh_fail_sparse
= fail_sparse
;
2806 dh
->dh_fail_uncached
= fail_uncached
;
2812 dh
->dh_parent
= NULL
;
2817 dh
->dh_depth
= depth
;
2821 dbuf_hold(dnode_t
*dn
, uint64_t blkid
, void *tag
)
2823 return (dbuf_hold_level(dn
, 0, blkid
, tag
));
2827 dbuf_hold_level(dnode_t
*dn
, int level
, uint64_t blkid
, void *tag
)
2830 int err
= dbuf_hold_impl(dn
, level
, blkid
, FALSE
, FALSE
, tag
, &db
);
2831 return (err
? NULL
: db
);
2835 dbuf_create_bonus(dnode_t
*dn
)
2837 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
2839 ASSERT(dn
->dn_bonus
== NULL
);
2840 dn
->dn_bonus
= dbuf_create(dn
, 0, DMU_BONUS_BLKID
, dn
->dn_dbuf
, NULL
);
2844 dbuf_spill_set_blksz(dmu_buf_t
*db_fake
, uint64_t blksz
, dmu_tx_t
*tx
)
2846 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2849 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
2850 return (SET_ERROR(ENOTSUP
));
2852 blksz
= SPA_MINBLOCKSIZE
;
2853 ASSERT3U(blksz
, <=, spa_maxblocksize(dmu_objset_spa(db
->db_objset
)));
2854 blksz
= P2ROUNDUP(blksz
, SPA_MINBLOCKSIZE
);
2858 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
2859 dbuf_new_size(db
, blksz
, tx
);
2860 rw_exit(&dn
->dn_struct_rwlock
);
2867 dbuf_rm_spill(dnode_t
*dn
, dmu_tx_t
*tx
)
2869 dbuf_free_range(dn
, DMU_SPILL_BLKID
, DMU_SPILL_BLKID
, tx
);
2872 #pragma weak dmu_buf_add_ref = dbuf_add_ref
2874 dbuf_add_ref(dmu_buf_impl_t
*db
, void *tag
)
2876 int64_t holds
= refcount_add(&db
->db_holds
, tag
);
2877 VERIFY3S(holds
, >, 1);
2880 #pragma weak dmu_buf_try_add_ref = dbuf_try_add_ref
2882 dbuf_try_add_ref(dmu_buf_t
*db_fake
, objset_t
*os
, uint64_t obj
, uint64_t blkid
,
2885 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2886 dmu_buf_impl_t
*found_db
;
2887 boolean_t result
= B_FALSE
;
2889 if (blkid
== DMU_BONUS_BLKID
)
2890 found_db
= dbuf_find_bonus(os
, obj
);
2892 found_db
= dbuf_find(os
, obj
, 0, blkid
);
2894 if (found_db
!= NULL
) {
2895 if (db
== found_db
&& dbuf_refcount(db
) > db
->db_dirtycnt
) {
2896 (void) refcount_add(&db
->db_holds
, tag
);
2899 mutex_exit(&found_db
->db_mtx
);
2905 * If you call dbuf_rele() you had better not be referencing the dnode handle
2906 * unless you have some other direct or indirect hold on the dnode. (An indirect
2907 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
2908 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
2909 * dnode's parent dbuf evicting its dnode handles.
2912 dbuf_rele(dmu_buf_impl_t
*db
, void *tag
)
2914 mutex_enter(&db
->db_mtx
);
2915 dbuf_rele_and_unlock(db
, tag
);
2919 dmu_buf_rele(dmu_buf_t
*db
, void *tag
)
2921 dbuf_rele((dmu_buf_impl_t
*)db
, tag
);
2925 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
2926 * db_dirtycnt and db_holds to be updated atomically.
2929 dbuf_rele_and_unlock(dmu_buf_impl_t
*db
, void *tag
)
2933 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2937 * Remove the reference to the dbuf before removing its hold on the
2938 * dnode so we can guarantee in dnode_move() that a referenced bonus
2939 * buffer has a corresponding dnode hold.
2941 holds
= refcount_remove(&db
->db_holds
, tag
);
2945 * We can't freeze indirects if there is a possibility that they
2946 * may be modified in the current syncing context.
2948 if (db
->db_buf
!= NULL
&&
2949 holds
== (db
->db_level
== 0 ? db
->db_dirtycnt
: 0)) {
2950 arc_buf_freeze(db
->db_buf
);
2953 if (holds
== db
->db_dirtycnt
&&
2954 db
->db_level
== 0 && db
->db_user_immediate_evict
)
2955 dbuf_evict_user(db
);
2958 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2960 boolean_t evict_dbuf
= db
->db_pending_evict
;
2963 * If the dnode moves here, we cannot cross this
2964 * barrier until the move completes.
2969 atomic_dec_32(&dn
->dn_dbufs_count
);
2972 * Decrementing the dbuf count means that the bonus
2973 * buffer's dnode hold is no longer discounted in
2974 * dnode_move(). The dnode cannot move until after
2975 * the dnode_rele() below.
2980 * Do not reference db after its lock is dropped.
2981 * Another thread may evict it.
2983 mutex_exit(&db
->db_mtx
);
2986 dnode_evict_bonus(dn
);
2989 } else if (db
->db_buf
== NULL
) {
2991 * This is a special case: we never associated this
2992 * dbuf with any data allocated from the ARC.
2994 ASSERT(db
->db_state
== DB_UNCACHED
||
2995 db
->db_state
== DB_NOFILL
);
2997 } else if (arc_released(db
->db_buf
)) {
2999 * This dbuf has anonymous data associated with it.
3003 boolean_t do_arc_evict
= B_FALSE
;
3005 spa_t
*spa
= dmu_objset_spa(db
->db_objset
);
3007 if (!DBUF_IS_CACHEABLE(db
) &&
3008 db
->db_blkptr
!= NULL
&&
3009 !BP_IS_HOLE(db
->db_blkptr
) &&
3010 !BP_IS_EMBEDDED(db
->db_blkptr
)) {
3011 do_arc_evict
= B_TRUE
;
3012 bp
= *db
->db_blkptr
;
3015 if (!DBUF_IS_CACHEABLE(db
) ||
3016 db
->db_pending_evict
) {
3018 } else if (!multilist_link_active(&db
->db_cache_link
)) {
3019 multilist_insert(dbuf_cache
, db
);
3020 (void) refcount_add_many(&dbuf_cache_size
,
3021 db
->db
.db_size
, db
);
3022 mutex_exit(&db
->db_mtx
);
3024 dbuf_evict_notify();
3028 arc_freed(spa
, &bp
);
3031 mutex_exit(&db
->db_mtx
);
3036 #pragma weak dmu_buf_refcount = dbuf_refcount
3038 dbuf_refcount(dmu_buf_impl_t
*db
)
3040 return (refcount_count(&db
->db_holds
));
3044 dmu_buf_replace_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*old_user
,
3045 dmu_buf_user_t
*new_user
)
3047 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3049 mutex_enter(&db
->db_mtx
);
3050 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
3051 if (db
->db_user
== old_user
)
3052 db
->db_user
= new_user
;
3054 old_user
= db
->db_user
;
3055 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
3056 mutex_exit(&db
->db_mtx
);
3062 dmu_buf_set_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
3064 return (dmu_buf_replace_user(db_fake
, NULL
, user
));
3068 dmu_buf_set_user_ie(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
3070 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3072 db
->db_user_immediate_evict
= TRUE
;
3073 return (dmu_buf_set_user(db_fake
, user
));
3077 dmu_buf_remove_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
3079 return (dmu_buf_replace_user(db_fake
, user
, NULL
));
3083 dmu_buf_get_user(dmu_buf_t
*db_fake
)
3085 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3087 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
3088 return (db
->db_user
);
3092 dmu_buf_user_evict_wait()
3094 taskq_wait(dbu_evict_taskq
);
3098 dmu_buf_get_blkptr(dmu_buf_t
*db
)
3100 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3101 return (dbi
->db_blkptr
);
3105 dmu_buf_get_objset(dmu_buf_t
*db
)
3107 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3108 return (dbi
->db_objset
);
3112 dmu_buf_dnode_enter(dmu_buf_t
*db
)
3114 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3115 DB_DNODE_ENTER(dbi
);
3116 return (DB_DNODE(dbi
));
3120 dmu_buf_dnode_exit(dmu_buf_t
*db
)
3122 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3127 dbuf_check_blkptr(dnode_t
*dn
, dmu_buf_impl_t
*db
)
3129 /* ASSERT(dmu_tx_is_syncing(tx) */
3130 ASSERT(MUTEX_HELD(&db
->db_mtx
));
3132 if (db
->db_blkptr
!= NULL
)
3135 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3136 db
->db_blkptr
= DN_SPILL_BLKPTR(dn
->dn_phys
);
3137 BP_ZERO(db
->db_blkptr
);
3140 if (db
->db_level
== dn
->dn_phys
->dn_nlevels
-1) {
3142 * This buffer was allocated at a time when there was
3143 * no available blkptrs from the dnode, or it was
3144 * inappropriate to hook it in (i.e., nlevels mis-match).
3146 ASSERT(db
->db_blkid
< dn
->dn_phys
->dn_nblkptr
);
3147 ASSERT(db
->db_parent
== NULL
);
3148 db
->db_parent
= dn
->dn_dbuf
;
3149 db
->db_blkptr
= &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
];
3152 dmu_buf_impl_t
*parent
= db
->db_parent
;
3153 int epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
3155 ASSERT(dn
->dn_phys
->dn_nlevels
> 1);
3156 if (parent
== NULL
) {
3157 mutex_exit(&db
->db_mtx
);
3158 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
3159 parent
= dbuf_hold_level(dn
, db
->db_level
+ 1,
3160 db
->db_blkid
>> epbs
, db
);
3161 rw_exit(&dn
->dn_struct_rwlock
);
3162 mutex_enter(&db
->db_mtx
);
3163 db
->db_parent
= parent
;
3165 db
->db_blkptr
= (blkptr_t
*)parent
->db
.db_data
+
3166 (db
->db_blkid
& ((1ULL << epbs
) - 1));
3172 * dbuf_sync_indirect() is called recursively from dbuf_sync_list() so it
3173 * is critical the we not allow the compiler to inline this function in to
3174 * dbuf_sync_list() thereby drastically bloating the stack usage.
3176 noinline
static void
3177 dbuf_sync_indirect(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
3179 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3183 ASSERT(dmu_tx_is_syncing(tx
));
3185 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
3187 mutex_enter(&db
->db_mtx
);
3189 ASSERT(db
->db_level
> 0);
3192 /* Read the block if it hasn't been read yet. */
3193 if (db
->db_buf
== NULL
) {
3194 mutex_exit(&db
->db_mtx
);
3195 (void) dbuf_read(db
, NULL
, DB_RF_MUST_SUCCEED
);
3196 mutex_enter(&db
->db_mtx
);
3198 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
3199 ASSERT(db
->db_buf
!= NULL
);
3203 /* Indirect block size must match what the dnode thinks it is. */
3204 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
3205 dbuf_check_blkptr(dn
, db
);
3208 /* Provide the pending dirty record to child dbufs */
3209 db
->db_data_pending
= dr
;
3211 mutex_exit(&db
->db_mtx
);
3212 dbuf_write(dr
, db
->db_buf
, tx
);
3215 mutex_enter(&dr
->dt
.di
.dr_mtx
);
3216 dbuf_sync_list(&dr
->dt
.di
.dr_children
, db
->db_level
- 1, tx
);
3217 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
3218 mutex_exit(&dr
->dt
.di
.dr_mtx
);
3223 * dbuf_sync_leaf() is called recursively from dbuf_sync_list() so it is
3224 * critical the we not allow the compiler to inline this function in to
3225 * dbuf_sync_list() thereby drastically bloating the stack usage.
3227 noinline
static void
3228 dbuf_sync_leaf(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
3230 arc_buf_t
**datap
= &dr
->dt
.dl
.dr_data
;
3231 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3234 uint64_t txg
= tx
->tx_txg
;
3236 ASSERT(dmu_tx_is_syncing(tx
));
3238 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
3240 mutex_enter(&db
->db_mtx
);
3242 * To be synced, we must be dirtied. But we
3243 * might have been freed after the dirty.
3245 if (db
->db_state
== DB_UNCACHED
) {
3246 /* This buffer has been freed since it was dirtied */
3247 ASSERT(db
->db
.db_data
== NULL
);
3248 } else if (db
->db_state
== DB_FILL
) {
3249 /* This buffer was freed and is now being re-filled */
3250 ASSERT(db
->db
.db_data
!= dr
->dt
.dl
.dr_data
);
3252 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_NOFILL
);
3259 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3260 mutex_enter(&dn
->dn_mtx
);
3261 if (!(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
)) {
3263 * In the previous transaction group, the bonus buffer
3264 * was entirely used to store the attributes for the
3265 * dnode which overrode the dn_spill field. However,
3266 * when adding more attributes to the file a spill
3267 * block was required to hold the extra attributes.
3269 * Make sure to clear the garbage left in the dn_spill
3270 * field from the previous attributes in the bonus
3271 * buffer. Otherwise, after writing out the spill
3272 * block to the new allocated dva, it will free
3273 * the old block pointed to by the invalid dn_spill.
3275 db
->db_blkptr
= NULL
;
3277 dn
->dn_phys
->dn_flags
|= DNODE_FLAG_SPILL_BLKPTR
;
3278 mutex_exit(&dn
->dn_mtx
);
3282 * If this is a bonus buffer, simply copy the bonus data into the
3283 * dnode. It will be written out when the dnode is synced (and it
3284 * will be synced, since it must have been dirty for dbuf_sync to
3287 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
3288 dbuf_dirty_record_t
**drp
;
3290 ASSERT(*datap
!= NULL
);
3291 ASSERT0(db
->db_level
);
3292 ASSERT3U(dn
->dn_phys
->dn_bonuslen
, <=,
3293 DN_SLOTS_TO_BONUSLEN(dn
->dn_phys
->dn_extra_slots
+ 1));
3294 bcopy(*datap
, DN_BONUS(dn
->dn_phys
), dn
->dn_phys
->dn_bonuslen
);
3297 if (*datap
!= db
->db
.db_data
) {
3298 int slots
= DB_DNODE(db
)->dn_num_slots
;
3299 int bonuslen
= DN_SLOTS_TO_BONUSLEN(slots
);
3300 kmem_free(*datap
, bonuslen
);
3301 arc_space_return(bonuslen
, ARC_SPACE_BONUS
);
3303 db
->db_data_pending
= NULL
;
3304 drp
= &db
->db_last_dirty
;
3306 drp
= &(*drp
)->dr_next
;
3307 ASSERT(dr
->dr_next
== NULL
);
3308 ASSERT(dr
->dr_dbuf
== db
);
3310 if (dr
->dr_dbuf
->db_level
!= 0) {
3311 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
3312 list_destroy(&dr
->dt
.di
.dr_children
);
3314 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
3315 ASSERT(db
->db_dirtycnt
> 0);
3316 db
->db_dirtycnt
-= 1;
3317 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)txg
);
3324 * This function may have dropped the db_mtx lock allowing a dmu_sync
3325 * operation to sneak in. As a result, we need to ensure that we
3326 * don't check the dr_override_state until we have returned from
3327 * dbuf_check_blkptr.
3329 dbuf_check_blkptr(dn
, db
);
3332 * If this buffer is in the middle of an immediate write,
3333 * wait for the synchronous IO to complete.
3335 while (dr
->dt
.dl
.dr_override_state
== DR_IN_DMU_SYNC
) {
3336 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
3337 cv_wait(&db
->db_changed
, &db
->db_mtx
);
3338 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_NOT_OVERRIDDEN
);
3341 if (db
->db_state
!= DB_NOFILL
&&
3342 dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
3343 refcount_count(&db
->db_holds
) > 1 &&
3344 dr
->dt
.dl
.dr_override_state
!= DR_OVERRIDDEN
&&
3345 *datap
== db
->db_buf
) {
3347 * If this buffer is currently "in use" (i.e., there
3348 * are active holds and db_data still references it),
3349 * then make a copy before we start the write so that
3350 * any modifications from the open txg will not leak
3353 * NOTE: this copy does not need to be made for
3354 * objects only modified in the syncing context (e.g.
3355 * DNONE_DNODE blocks).
3357 int psize
= arc_buf_size(*datap
);
3358 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
3359 enum zio_compress compress_type
= arc_get_compression(*datap
);
3361 if (compress_type
== ZIO_COMPRESS_OFF
) {
3362 *datap
= arc_alloc_buf(os
->os_spa
, db
, type
, psize
);
3364 ASSERT3U(type
, ==, ARC_BUFC_DATA
);
3365 int lsize
= arc_buf_lsize(*datap
);
3366 *datap
= arc_alloc_compressed_buf(os
->os_spa
, db
,
3367 psize
, lsize
, compress_type
);
3369 bcopy(db
->db
.db_data
, (*datap
)->b_data
, psize
);
3371 db
->db_data_pending
= dr
;
3373 mutex_exit(&db
->db_mtx
);
3375 dbuf_write(dr
, *datap
, tx
);
3377 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
3378 if (dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
3379 list_insert_tail(&dn
->dn_dirty_records
[txg
&TXG_MASK
], dr
);
3383 * Although zio_nowait() does not "wait for an IO", it does
3384 * initiate the IO. If this is an empty write it seems plausible
3385 * that the IO could actually be completed before the nowait
3386 * returns. We need to DB_DNODE_EXIT() first in case
3387 * zio_nowait() invalidates the dbuf.
3390 zio_nowait(dr
->dr_zio
);
3395 dbuf_sync_list(list_t
*list
, int level
, dmu_tx_t
*tx
)
3397 dbuf_dirty_record_t
*dr
;
3399 while ((dr
= list_head(list
))) {
3400 if (dr
->dr_zio
!= NULL
) {
3402 * If we find an already initialized zio then we
3403 * are processing the meta-dnode, and we have finished.
3404 * The dbufs for all dnodes are put back on the list
3405 * during processing, so that we can zio_wait()
3406 * these IOs after initiating all child IOs.
3408 ASSERT3U(dr
->dr_dbuf
->db
.db_object
, ==,
3409 DMU_META_DNODE_OBJECT
);
3412 if (dr
->dr_dbuf
->db_blkid
!= DMU_BONUS_BLKID
&&
3413 dr
->dr_dbuf
->db_blkid
!= DMU_SPILL_BLKID
) {
3414 VERIFY3U(dr
->dr_dbuf
->db_level
, ==, level
);
3416 list_remove(list
, dr
);
3417 if (dr
->dr_dbuf
->db_level
> 0)
3418 dbuf_sync_indirect(dr
, tx
);
3420 dbuf_sync_leaf(dr
, tx
);
3426 dbuf_write_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
3428 dmu_buf_impl_t
*db
= vdb
;
3430 blkptr_t
*bp
= zio
->io_bp
;
3431 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
3432 spa_t
*spa
= zio
->io_spa
;
3437 ASSERT3P(db
->db_blkptr
, !=, NULL
);
3438 ASSERT3P(&db
->db_data_pending
->dr_bp_copy
, ==, bp
);
3442 delta
= bp_get_dsize_sync(spa
, bp
) - bp_get_dsize_sync(spa
, bp_orig
);
3443 dnode_diduse_space(dn
, delta
- zio
->io_prev_space_delta
);
3444 zio
->io_prev_space_delta
= delta
;
3446 if (bp
->blk_birth
!= 0) {
3447 ASSERT((db
->db_blkid
!= DMU_SPILL_BLKID
&&
3448 BP_GET_TYPE(bp
) == dn
->dn_type
) ||
3449 (db
->db_blkid
== DMU_SPILL_BLKID
&&
3450 BP_GET_TYPE(bp
) == dn
->dn_bonustype
) ||
3451 BP_IS_EMBEDDED(bp
));
3452 ASSERT(BP_GET_LEVEL(bp
) == db
->db_level
);
3455 mutex_enter(&db
->db_mtx
);
3458 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3459 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
3460 ASSERT(!(BP_IS_HOLE(bp
)) &&
3461 db
->db_blkptr
== DN_SPILL_BLKPTR(dn
->dn_phys
));
3465 if (db
->db_level
== 0) {
3466 mutex_enter(&dn
->dn_mtx
);
3467 if (db
->db_blkid
> dn
->dn_phys
->dn_maxblkid
&&
3468 db
->db_blkid
!= DMU_SPILL_BLKID
)
3469 dn
->dn_phys
->dn_maxblkid
= db
->db_blkid
;
3470 mutex_exit(&dn
->dn_mtx
);
3472 if (dn
->dn_type
== DMU_OT_DNODE
) {
3474 while (i
< db
->db
.db_size
) {
3476 (void *)(((char *)db
->db
.db_data
) + i
);
3478 i
+= DNODE_MIN_SIZE
;
3479 if (dnp
->dn_type
!= DMU_OT_NONE
) {
3481 i
+= dnp
->dn_extra_slots
*
3486 if (BP_IS_HOLE(bp
)) {
3493 blkptr_t
*ibp
= db
->db
.db_data
;
3494 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
3495 for (i
= db
->db
.db_size
>> SPA_BLKPTRSHIFT
; i
> 0; i
--, ibp
++) {
3496 if (BP_IS_HOLE(ibp
))
3498 fill
+= BP_GET_FILL(ibp
);
3503 if (!BP_IS_EMBEDDED(bp
))
3504 bp
->blk_fill
= fill
;
3506 mutex_exit(&db
->db_mtx
);
3508 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
3509 *db
->db_blkptr
= *bp
;
3510 rw_exit(&dn
->dn_struct_rwlock
);
3515 * This function gets called just prior to running through the compression
3516 * stage of the zio pipeline. If we're an indirect block comprised of only
3517 * holes, then we want this indirect to be compressed away to a hole. In
3518 * order to do that we must zero out any information about the holes that
3519 * this indirect points to prior to before we try to compress it.
3522 dbuf_write_children_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
3524 dmu_buf_impl_t
*db
= vdb
;
3527 unsigned int epbs
, i
;
3529 ASSERT3U(db
->db_level
, >, 0);
3532 epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
3533 ASSERT3U(epbs
, <, 31);
3535 /* Determine if all our children are holes */
3536 for (i
= 0, bp
= db
->db
.db_data
; i
< 1ULL << epbs
; i
++, bp
++) {
3537 if (!BP_IS_HOLE(bp
))
3542 * If all the children are holes, then zero them all out so that
3543 * we may get compressed away.
3545 if (i
== 1ULL << epbs
) {
3547 * We only found holes. Grab the rwlock to prevent
3548 * anybody from reading the blocks we're about to
3551 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
3552 bzero(db
->db
.db_data
, db
->db
.db_size
);
3553 rw_exit(&dn
->dn_struct_rwlock
);
3559 * The SPA will call this callback several times for each zio - once
3560 * for every physical child i/o (zio->io_phys_children times). This
3561 * allows the DMU to monitor the progress of each logical i/o. For example,
3562 * there may be 2 copies of an indirect block, or many fragments of a RAID-Z
3563 * block. There may be a long delay before all copies/fragments are completed,
3564 * so this callback allows us to retire dirty space gradually, as the physical
3569 dbuf_write_physdone(zio_t
*zio
, arc_buf_t
*buf
, void *arg
)
3571 dmu_buf_impl_t
*db
= arg
;
3572 objset_t
*os
= db
->db_objset
;
3573 dsl_pool_t
*dp
= dmu_objset_pool(os
);
3574 dbuf_dirty_record_t
*dr
;
3577 dr
= db
->db_data_pending
;
3578 ASSERT3U(dr
->dr_txg
, ==, zio
->io_txg
);
3581 * The callback will be called io_phys_children times. Retire one
3582 * portion of our dirty space each time we are called. Any rounding
3583 * error will be cleaned up by dsl_pool_sync()'s call to
3584 * dsl_pool_undirty_space().
3586 delta
= dr
->dr_accounted
/ zio
->io_phys_children
;
3587 dsl_pool_undirty_space(dp
, delta
, zio
->io_txg
);
3592 dbuf_write_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
3594 dmu_buf_impl_t
*db
= vdb
;
3595 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
3596 blkptr_t
*bp
= db
->db_blkptr
;
3597 objset_t
*os
= db
->db_objset
;
3598 dmu_tx_t
*tx
= os
->os_synctx
;
3599 dbuf_dirty_record_t
**drp
, *dr
;
3601 ASSERT0(zio
->io_error
);
3602 ASSERT(db
->db_blkptr
== bp
);
3605 * For nopwrites and rewrites we ensure that the bp matches our
3606 * original and bypass all the accounting.
3608 if (zio
->io_flags
& (ZIO_FLAG_IO_REWRITE
| ZIO_FLAG_NOPWRITE
)) {
3609 ASSERT(BP_EQUAL(bp
, bp_orig
));
3611 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
3612 (void) dsl_dataset_block_kill(ds
, bp_orig
, tx
, B_TRUE
);
3613 dsl_dataset_block_born(ds
, bp
, tx
);
3616 mutex_enter(&db
->db_mtx
);
3620 drp
= &db
->db_last_dirty
;
3621 while ((dr
= *drp
) != db
->db_data_pending
)
3623 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
3624 ASSERT(dr
->dr_dbuf
== db
);
3625 ASSERT(dr
->dr_next
== NULL
);
3629 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3634 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
3635 ASSERT(!(BP_IS_HOLE(db
->db_blkptr
)) &&
3636 db
->db_blkptr
== DN_SPILL_BLKPTR(dn
->dn_phys
));
3641 if (db
->db_level
== 0) {
3642 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
3643 ASSERT(dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
);
3644 if (db
->db_state
!= DB_NOFILL
) {
3645 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
3646 arc_buf_destroy(dr
->dt
.dl
.dr_data
, db
);
3653 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
3654 ASSERT3U(db
->db
.db_size
, ==, 1 << dn
->dn_phys
->dn_indblkshift
);
3655 if (!BP_IS_HOLE(db
->db_blkptr
)) {
3656 ASSERTV(int epbs
= dn
->dn_phys
->dn_indblkshift
-
3658 ASSERT3U(db
->db_blkid
, <=,
3659 dn
->dn_phys
->dn_maxblkid
>> (db
->db_level
* epbs
));
3660 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==,
3664 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
3665 list_destroy(&dr
->dt
.di
.dr_children
);
3667 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
3669 cv_broadcast(&db
->db_changed
);
3670 ASSERT(db
->db_dirtycnt
> 0);
3671 db
->db_dirtycnt
-= 1;
3672 db
->db_data_pending
= NULL
;
3673 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)tx
->tx_txg
);
3677 dbuf_write_nofill_ready(zio_t
*zio
)
3679 dbuf_write_ready(zio
, NULL
, zio
->io_private
);
3683 dbuf_write_nofill_done(zio_t
*zio
)
3685 dbuf_write_done(zio
, NULL
, zio
->io_private
);
3689 dbuf_write_override_ready(zio_t
*zio
)
3691 dbuf_dirty_record_t
*dr
= zio
->io_private
;
3692 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3694 dbuf_write_ready(zio
, NULL
, db
);
3698 dbuf_write_override_done(zio_t
*zio
)
3700 dbuf_dirty_record_t
*dr
= zio
->io_private
;
3701 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3702 blkptr_t
*obp
= &dr
->dt
.dl
.dr_overridden_by
;
3704 mutex_enter(&db
->db_mtx
);
3705 if (!BP_EQUAL(zio
->io_bp
, obp
)) {
3706 if (!BP_IS_HOLE(obp
))
3707 dsl_free(spa_get_dsl(zio
->io_spa
), zio
->io_txg
, obp
);
3708 arc_release(dr
->dt
.dl
.dr_data
, db
);
3710 mutex_exit(&db
->db_mtx
);
3712 dbuf_write_done(zio
, NULL
, db
);
3714 if (zio
->io_abd
!= NULL
)
3715 abd_put(zio
->io_abd
);
3718 /* Issue I/O to commit a dirty buffer to disk. */
3720 dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
)
3722 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3725 dmu_buf_impl_t
*parent
= db
->db_parent
;
3726 uint64_t txg
= tx
->tx_txg
;
3727 zbookmark_phys_t zb
;
3732 ASSERT(dmu_tx_is_syncing(tx
));
3738 if (db
->db_state
!= DB_NOFILL
) {
3739 if (db
->db_level
> 0 || dn
->dn_type
== DMU_OT_DNODE
) {
3741 * Private object buffers are released here rather
3742 * than in dbuf_dirty() since they are only modified
3743 * in the syncing context and we don't want the
3744 * overhead of making multiple copies of the data.
3746 if (BP_IS_HOLE(db
->db_blkptr
)) {
3749 dbuf_release_bp(db
);
3754 if (parent
!= dn
->dn_dbuf
) {
3755 /* Our parent is an indirect block. */
3756 /* We have a dirty parent that has been scheduled for write. */
3757 ASSERT(parent
&& parent
->db_data_pending
);
3758 /* Our parent's buffer is one level closer to the dnode. */
3759 ASSERT(db
->db_level
== parent
->db_level
-1);
3761 * We're about to modify our parent's db_data by modifying
3762 * our block pointer, so the parent must be released.
3764 ASSERT(arc_released(parent
->db_buf
));
3765 zio
= parent
->db_data_pending
->dr_zio
;
3767 /* Our parent is the dnode itself. */
3768 ASSERT((db
->db_level
== dn
->dn_phys
->dn_nlevels
-1 &&
3769 db
->db_blkid
!= DMU_SPILL_BLKID
) ||
3770 (db
->db_blkid
== DMU_SPILL_BLKID
&& db
->db_level
== 0));
3771 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
3772 ASSERT3P(db
->db_blkptr
, ==,
3773 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
3777 ASSERT(db
->db_level
== 0 || data
== db
->db_buf
);
3778 ASSERT3U(db
->db_blkptr
->blk_birth
, <=, txg
);
3781 SET_BOOKMARK(&zb
, os
->os_dsl_dataset
?
3782 os
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
3783 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
3785 if (db
->db_blkid
== DMU_SPILL_BLKID
)
3787 wp_flag
|= (db
->db_state
== DB_NOFILL
) ? WP_NOFILL
: 0;
3789 dmu_write_policy(os
, dn
, db
->db_level
, wp_flag
, &zp
);
3793 * We copy the blkptr now (rather than when we instantiate the dirty
3794 * record), because its value can change between open context and
3795 * syncing context. We do not need to hold dn_struct_rwlock to read
3796 * db_blkptr because we are in syncing context.
3798 dr
->dr_bp_copy
= *db
->db_blkptr
;
3800 if (db
->db_level
== 0 &&
3801 dr
->dt
.dl
.dr_override_state
== DR_OVERRIDDEN
) {
3803 * The BP for this block has been provided by open context
3804 * (by dmu_sync() or dmu_buf_write_embedded()).
3806 abd_t
*contents
= (data
!= NULL
) ?
3807 abd_get_from_buf(data
->b_data
, arc_buf_size(data
)) : NULL
;
3809 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
,
3810 &dr
->dr_bp_copy
, contents
, db
->db
.db_size
, db
->db
.db_size
,
3811 &zp
, dbuf_write_override_ready
, NULL
, NULL
,
3812 dbuf_write_override_done
,
3813 dr
, ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
3814 mutex_enter(&db
->db_mtx
);
3815 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
3816 zio_write_override(dr
->dr_zio
, &dr
->dt
.dl
.dr_overridden_by
,
3817 dr
->dt
.dl
.dr_copies
, dr
->dt
.dl
.dr_nopwrite
);
3818 mutex_exit(&db
->db_mtx
);
3819 } else if (db
->db_state
== DB_NOFILL
) {
3820 ASSERT(zp
.zp_checksum
== ZIO_CHECKSUM_OFF
||
3821 zp
.zp_checksum
== ZIO_CHECKSUM_NOPARITY
);
3822 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
,
3823 &dr
->dr_bp_copy
, NULL
, db
->db
.db_size
, db
->db
.db_size
, &zp
,
3824 dbuf_write_nofill_ready
, NULL
, NULL
,
3825 dbuf_write_nofill_done
, db
,
3826 ZIO_PRIORITY_ASYNC_WRITE
,
3827 ZIO_FLAG_MUSTSUCCEED
| ZIO_FLAG_NODATA
, &zb
);
3829 arc_done_func_t
*children_ready_cb
= NULL
;
3830 ASSERT(arc_released(data
));
3833 * For indirect blocks, we want to setup the children
3834 * ready callback so that we can properly handle an indirect
3835 * block that only contains holes.
3837 if (db
->db_level
!= 0)
3838 children_ready_cb
= dbuf_write_children_ready
;
3840 dr
->dr_zio
= arc_write(zio
, os
->os_spa
, txg
,
3841 &dr
->dr_bp_copy
, data
, DBUF_IS_L2CACHEABLE(db
),
3842 &zp
, dbuf_write_ready
,
3843 children_ready_cb
, dbuf_write_physdone
,
3844 dbuf_write_done
, db
, ZIO_PRIORITY_ASYNC_WRITE
,
3845 ZIO_FLAG_MUSTSUCCEED
, &zb
);
3849 #if defined(_KERNEL) && defined(HAVE_SPL)
3850 EXPORT_SYMBOL(dbuf_find
);
3851 EXPORT_SYMBOL(dbuf_is_metadata
);
3852 EXPORT_SYMBOL(dbuf_destroy
);
3853 EXPORT_SYMBOL(dbuf_loan_arcbuf
);
3854 EXPORT_SYMBOL(dbuf_whichblock
);
3855 EXPORT_SYMBOL(dbuf_read
);
3856 EXPORT_SYMBOL(dbuf_unoverride
);
3857 EXPORT_SYMBOL(dbuf_free_range
);
3858 EXPORT_SYMBOL(dbuf_new_size
);
3859 EXPORT_SYMBOL(dbuf_release_bp
);
3860 EXPORT_SYMBOL(dbuf_dirty
);
3861 EXPORT_SYMBOL(dmu_buf_will_dirty
);
3862 EXPORT_SYMBOL(dmu_buf_will_not_fill
);
3863 EXPORT_SYMBOL(dmu_buf_will_fill
);
3864 EXPORT_SYMBOL(dmu_buf_fill_done
);
3865 EXPORT_SYMBOL(dmu_buf_rele
);
3866 EXPORT_SYMBOL(dbuf_assign_arcbuf
);
3867 EXPORT_SYMBOL(dbuf_prefetch
);
3868 EXPORT_SYMBOL(dbuf_hold_impl
);
3869 EXPORT_SYMBOL(dbuf_hold
);
3870 EXPORT_SYMBOL(dbuf_hold_level
);
3871 EXPORT_SYMBOL(dbuf_create_bonus
);
3872 EXPORT_SYMBOL(dbuf_spill_set_blksz
);
3873 EXPORT_SYMBOL(dbuf_rm_spill
);
3874 EXPORT_SYMBOL(dbuf_add_ref
);
3875 EXPORT_SYMBOL(dbuf_rele
);
3876 EXPORT_SYMBOL(dbuf_rele_and_unlock
);
3877 EXPORT_SYMBOL(dbuf_refcount
);
3878 EXPORT_SYMBOL(dbuf_sync_list
);
3879 EXPORT_SYMBOL(dmu_buf_set_user
);
3880 EXPORT_SYMBOL(dmu_buf_set_user_ie
);
3881 EXPORT_SYMBOL(dmu_buf_get_user
);
3882 EXPORT_SYMBOL(dmu_buf_get_blkptr
);
3885 module_param(dbuf_cache_max_bytes
, ulong
, 0644);
3886 MODULE_PARM_DESC(dbuf_cache_max_bytes
,
3887 "Maximum size in bytes of the dbuf cache.");
3889 module_param(dbuf_cache_hiwater_pct
, uint
, 0644);
3890 MODULE_PARM_DESC(dbuf_cache_hiwater_pct
,
3891 "Percentage over dbuf_cache_max_bytes when dbufs must be evicted "
3894 module_param(dbuf_cache_lowater_pct
, uint
, 0644);
3895 MODULE_PARM_DESC(dbuf_cache_lowater_pct
,
3896 "Percentage below dbuf_cache_max_bytes when the evict thread stops "
3899 module_param(dbuf_cache_max_shift
, int, 0644);
3900 MODULE_PARM_DESC(dbuf_cache_max_shift
,
3901 "Cap the size of the dbuf cache to a log2 fraction of arc size.");