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
);
175 dbuf_dest(void *vdb
, void *unused
)
177 dmu_buf_impl_t
*db
= vdb
;
178 mutex_destroy(&db
->db_mtx
);
179 cv_destroy(&db
->db_changed
);
180 ASSERT(!multilist_link_active(&db
->db_cache_link
));
181 refcount_destroy(&db
->db_holds
);
185 * dbuf hash table routines
187 static dbuf_hash_table_t dbuf_hash_table
;
189 static uint64_t dbuf_hash_count
;
192 dbuf_hash(void *os
, uint64_t obj
, uint8_t lvl
, uint64_t blkid
)
194 uintptr_t osv
= (uintptr_t)os
;
195 uint64_t crc
= -1ULL;
197 ASSERT(zfs_crc64_table
[128] == ZFS_CRC64_POLY
);
198 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (lvl
)) & 0xFF];
199 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (osv
>> 6)) & 0xFF];
200 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 0)) & 0xFF];
201 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 8)) & 0xFF];
202 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (blkid
>> 0)) & 0xFF];
203 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (blkid
>> 8)) & 0xFF];
205 crc
^= (osv
>>14) ^ (obj
>>16) ^ (blkid
>>16);
210 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
211 ((dbuf)->db.db_object == (obj) && \
212 (dbuf)->db_objset == (os) && \
213 (dbuf)->db_level == (level) && \
214 (dbuf)->db_blkid == (blkid))
217 dbuf_find(objset_t
*os
, uint64_t obj
, uint8_t level
, uint64_t blkid
)
219 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
224 hv
= dbuf_hash(os
, obj
, level
, blkid
);
225 idx
= hv
& h
->hash_table_mask
;
227 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
228 for (db
= h
->hash_table
[idx
]; db
!= NULL
; db
= db
->db_hash_next
) {
229 if (DBUF_EQUAL(db
, os
, obj
, level
, blkid
)) {
230 mutex_enter(&db
->db_mtx
);
231 if (db
->db_state
!= DB_EVICTING
) {
232 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
235 mutex_exit(&db
->db_mtx
);
238 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
242 static dmu_buf_impl_t
*
243 dbuf_find_bonus(objset_t
*os
, uint64_t object
)
246 dmu_buf_impl_t
*db
= NULL
;
248 if (dnode_hold(os
, object
, FTAG
, &dn
) == 0) {
249 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
250 if (dn
->dn_bonus
!= NULL
) {
252 mutex_enter(&db
->db_mtx
);
254 rw_exit(&dn
->dn_struct_rwlock
);
255 dnode_rele(dn
, FTAG
);
261 * Insert an entry into the hash table. If there is already an element
262 * equal to elem in the hash table, then the already existing element
263 * will be returned and the new element will not be inserted.
264 * Otherwise returns NULL.
266 static dmu_buf_impl_t
*
267 dbuf_hash_insert(dmu_buf_impl_t
*db
)
269 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
270 objset_t
*os
= db
->db_objset
;
271 uint64_t obj
= db
->db
.db_object
;
272 int level
= db
->db_level
;
273 uint64_t blkid
, hv
, idx
;
276 blkid
= db
->db_blkid
;
277 hv
= dbuf_hash(os
, obj
, level
, blkid
);
278 idx
= hv
& h
->hash_table_mask
;
280 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
281 for (dbf
= h
->hash_table
[idx
]; dbf
!= NULL
; dbf
= dbf
->db_hash_next
) {
282 if (DBUF_EQUAL(dbf
, os
, obj
, level
, blkid
)) {
283 mutex_enter(&dbf
->db_mtx
);
284 if (dbf
->db_state
!= DB_EVICTING
) {
285 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
288 mutex_exit(&dbf
->db_mtx
);
292 mutex_enter(&db
->db_mtx
);
293 db
->db_hash_next
= h
->hash_table
[idx
];
294 h
->hash_table
[idx
] = db
;
295 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
296 atomic_inc_64(&dbuf_hash_count
);
302 * Remove an entry from the hash table. It must be in the EVICTING state.
305 dbuf_hash_remove(dmu_buf_impl_t
*db
)
307 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
309 dmu_buf_impl_t
*dbf
, **dbp
;
311 hv
= dbuf_hash(db
->db_objset
, db
->db
.db_object
,
312 db
->db_level
, db
->db_blkid
);
313 idx
= hv
& h
->hash_table_mask
;
316 * We mustn't hold db_mtx to maintain lock ordering:
317 * DBUF_HASH_MUTEX > db_mtx.
319 ASSERT(refcount_is_zero(&db
->db_holds
));
320 ASSERT(db
->db_state
== DB_EVICTING
);
321 ASSERT(!MUTEX_HELD(&db
->db_mtx
));
323 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
324 dbp
= &h
->hash_table
[idx
];
325 while ((dbf
= *dbp
) != db
) {
326 dbp
= &dbf
->db_hash_next
;
329 *dbp
= db
->db_hash_next
;
330 db
->db_hash_next
= NULL
;
331 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
332 atomic_dec_64(&dbuf_hash_count
);
338 } dbvu_verify_type_t
;
341 dbuf_verify_user(dmu_buf_impl_t
*db
, dbvu_verify_type_t verify_type
)
346 if (db
->db_user
== NULL
)
349 /* Only data blocks support the attachment of user data. */
350 ASSERT(db
->db_level
== 0);
352 /* Clients must resolve a dbuf before attaching user data. */
353 ASSERT(db
->db
.db_data
!= NULL
);
354 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
356 holds
= refcount_count(&db
->db_holds
);
357 if (verify_type
== DBVU_EVICTING
) {
359 * Immediate eviction occurs when holds == dirtycnt.
360 * For normal eviction buffers, holds is zero on
361 * eviction, except when dbuf_fix_old_data() calls
362 * dbuf_clear_data(). However, the hold count can grow
363 * during eviction even though db_mtx is held (see
364 * dmu_bonus_hold() for an example), so we can only
365 * test the generic invariant that holds >= dirtycnt.
367 ASSERT3U(holds
, >=, db
->db_dirtycnt
);
369 if (db
->db_user_immediate_evict
== TRUE
)
370 ASSERT3U(holds
, >=, db
->db_dirtycnt
);
372 ASSERT3U(holds
, >, 0);
378 dbuf_evict_user(dmu_buf_impl_t
*db
)
380 dmu_buf_user_t
*dbu
= db
->db_user
;
382 ASSERT(MUTEX_HELD(&db
->db_mtx
));
387 dbuf_verify_user(db
, DBVU_EVICTING
);
391 if (dbu
->dbu_clear_on_evict_dbufp
!= NULL
)
392 *dbu
->dbu_clear_on_evict_dbufp
= NULL
;
396 * There are two eviction callbacks - one that we call synchronously
397 * and one that we invoke via a taskq. The async one is useful for
398 * avoiding lock order reversals and limiting stack depth.
400 * Note that if we have a sync callback but no async callback,
401 * it's likely that the sync callback will free the structure
402 * containing the dbu. In that case we need to take care to not
403 * dereference dbu after calling the sync evict func.
405 boolean_t has_async
= (dbu
->dbu_evict_func_async
!= NULL
);
407 if (dbu
->dbu_evict_func_sync
!= NULL
)
408 dbu
->dbu_evict_func_sync(dbu
);
411 taskq_dispatch_ent(dbu_evict_taskq
, dbu
->dbu_evict_func_async
,
412 dbu
, 0, &dbu
->dbu_tqent
);
417 dbuf_is_metadata(dmu_buf_impl_t
*db
)
420 * Consider indirect blocks and spill blocks to be meta data.
422 if (db
->db_level
> 0 || db
->db_blkid
== DMU_SPILL_BLKID
) {
425 boolean_t is_metadata
;
428 is_metadata
= DMU_OT_IS_METADATA(DB_DNODE(db
)->dn_type
);
431 return (is_metadata
);
437 * This function *must* return indices evenly distributed between all
438 * sublists of the multilist. This is needed due to how the dbuf eviction
439 * code is laid out; dbuf_evict_thread() assumes dbufs are evenly
440 * distributed between all sublists and uses this assumption when
441 * deciding which sublist to evict from and how much to evict from it.
444 dbuf_cache_multilist_index_func(multilist_t
*ml
, void *obj
)
446 dmu_buf_impl_t
*db
= obj
;
449 * The assumption here, is the hash value for a given
450 * dmu_buf_impl_t will remain constant throughout it's lifetime
451 * (i.e. it's objset, object, level and blkid fields don't change).
452 * Thus, we don't need to store the dbuf's sublist index
453 * on insertion, as this index can be recalculated on removal.
455 * Also, the low order bits of the hash value are thought to be
456 * distributed evenly. Otherwise, in the case that the multilist
457 * has a power of two number of sublists, each sublists' usage
458 * would not be evenly distributed.
460 return (dbuf_hash(db
->db_objset
, db
->db
.db_object
,
461 db
->db_level
, db
->db_blkid
) %
462 multilist_get_num_sublists(ml
));
465 static inline unsigned long
466 dbuf_cache_target_bytes(void)
468 return MIN(dbuf_cache_max_bytes
,
469 arc_target_bytes() >> dbuf_cache_max_shift
);
472 static inline boolean_t
473 dbuf_cache_above_hiwater(void)
475 uint64_t dbuf_cache_target
= dbuf_cache_target_bytes();
477 uint64_t dbuf_cache_hiwater_bytes
=
478 (dbuf_cache_target
* dbuf_cache_hiwater_pct
) / 100;
480 return (refcount_count(&dbuf_cache_size
) >
481 dbuf_cache_target
+ dbuf_cache_hiwater_bytes
);
484 static inline boolean_t
485 dbuf_cache_above_lowater(void)
487 uint64_t dbuf_cache_target
= dbuf_cache_target_bytes();
489 uint64_t dbuf_cache_lowater_bytes
=
490 (dbuf_cache_target
* dbuf_cache_lowater_pct
) / 100;
492 return (refcount_count(&dbuf_cache_size
) >
493 dbuf_cache_target
- dbuf_cache_lowater_bytes
);
497 * Evict the oldest eligible dbuf from the dbuf cache.
502 int idx
= multilist_get_random_index(dbuf_cache
);
503 multilist_sublist_t
*mls
= multilist_sublist_lock(dbuf_cache
, idx
);
505 ASSERT(!MUTEX_HELD(&dbuf_evict_lock
));
508 * Set the thread's tsd to indicate that it's processing evictions.
509 * Once a thread stops evicting from the dbuf cache it will
510 * reset its tsd to NULL.
512 ASSERT3P(tsd_get(zfs_dbuf_evict_key
), ==, NULL
);
513 (void) tsd_set(zfs_dbuf_evict_key
, (void *)B_TRUE
);
515 dmu_buf_impl_t
*db
= multilist_sublist_tail(mls
);
516 while (db
!= NULL
&& mutex_tryenter(&db
->db_mtx
) == 0) {
517 db
= multilist_sublist_prev(mls
, db
);
520 DTRACE_PROBE2(dbuf__evict__one
, dmu_buf_impl_t
*, db
,
521 multilist_sublist_t
*, mls
);
524 multilist_sublist_remove(mls
, db
);
525 multilist_sublist_unlock(mls
);
526 (void) refcount_remove_many(&dbuf_cache_size
,
530 multilist_sublist_unlock(mls
);
532 (void) tsd_set(zfs_dbuf_evict_key
, NULL
);
536 * The dbuf evict thread is responsible for aging out dbufs from the
537 * cache. Once the cache has reached it's maximum size, dbufs are removed
538 * and destroyed. The eviction thread will continue running until the size
539 * of the dbuf cache is at or below the maximum size. Once the dbuf is aged
540 * out of the cache it is destroyed and becomes eligible for arc eviction.
544 dbuf_evict_thread(void *unused
)
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
++) {
846 blkptr_t
*bps
= db
->db
.db_data
;
847 ASSERT3U(1 << DB_DNODE(db
)->dn_indblkshift
, ==,
850 * We want to verify that all the blkptrs in the
851 * indirect block are holes, but we may have
852 * automatically set up a few fields for them.
853 * We iterate through each blkptr and verify
854 * they only have those fields set.
857 i
< db
->db
.db_size
/ sizeof (blkptr_t
);
859 blkptr_t
*bp
= &bps
[i
];
860 ASSERT(ZIO_CHECKSUM_IS_ZERO(
863 DVA_IS_EMPTY(&bp
->blk_dva
[0]) &&
864 DVA_IS_EMPTY(&bp
->blk_dva
[1]) &&
865 DVA_IS_EMPTY(&bp
->blk_dva
[2]));
866 ASSERT0(bp
->blk_fill
);
867 ASSERT0(bp
->blk_pad
[0]);
868 ASSERT0(bp
->blk_pad
[1]);
869 ASSERT(!BP_IS_EMBEDDED(bp
));
870 ASSERT(BP_IS_HOLE(bp
));
871 ASSERT0(bp
->blk_phys_birth
);
881 dbuf_clear_data(dmu_buf_impl_t
*db
)
883 ASSERT(MUTEX_HELD(&db
->db_mtx
));
885 ASSERT3P(db
->db_buf
, ==, NULL
);
886 db
->db
.db_data
= NULL
;
887 if (db
->db_state
!= DB_NOFILL
)
888 db
->db_state
= DB_UNCACHED
;
892 dbuf_set_data(dmu_buf_impl_t
*db
, arc_buf_t
*buf
)
894 ASSERT(MUTEX_HELD(&db
->db_mtx
));
898 ASSERT(buf
->b_data
!= NULL
);
899 db
->db
.db_data
= buf
->b_data
;
903 * Loan out an arc_buf for read. Return the loaned arc_buf.
906 dbuf_loan_arcbuf(dmu_buf_impl_t
*db
)
910 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
911 mutex_enter(&db
->db_mtx
);
912 if (arc_released(db
->db_buf
) || refcount_count(&db
->db_holds
) > 1) {
913 int blksz
= db
->db
.db_size
;
914 spa_t
*spa
= db
->db_objset
->os_spa
;
916 mutex_exit(&db
->db_mtx
);
917 abuf
= arc_loan_buf(spa
, B_FALSE
, blksz
);
918 bcopy(db
->db
.db_data
, abuf
->b_data
, blksz
);
921 arc_loan_inuse_buf(abuf
, db
);
924 mutex_exit(&db
->db_mtx
);
930 * Calculate which level n block references the data at the level 0 offset
934 dbuf_whichblock(const dnode_t
*dn
, const int64_t level
, const uint64_t offset
)
936 if (dn
->dn_datablkshift
!= 0 && dn
->dn_indblkshift
!= 0) {
938 * The level n blkid is equal to the level 0 blkid divided by
939 * the number of level 0s in a level n block.
941 * The level 0 blkid is offset >> datablkshift =
942 * offset / 2^datablkshift.
944 * The number of level 0s in a level n is the number of block
945 * pointers in an indirect block, raised to the power of level.
946 * This is 2^(indblkshift - SPA_BLKPTRSHIFT)^level =
947 * 2^(level*(indblkshift - SPA_BLKPTRSHIFT)).
949 * Thus, the level n blkid is: offset /
950 * ((2^datablkshift)*(2^(level*(indblkshift - SPA_BLKPTRSHIFT)))
951 * = offset / 2^(datablkshift + level *
952 * (indblkshift - SPA_BLKPTRSHIFT))
953 * = offset >> (datablkshift + level *
954 * (indblkshift - SPA_BLKPTRSHIFT))
957 const unsigned exp
= dn
->dn_datablkshift
+
958 level
* (dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
);
960 if (exp
>= 8 * sizeof (offset
)) {
961 /* This only happens on the highest indirection level */
962 ASSERT3U(level
, ==, dn
->dn_nlevels
- 1);
966 ASSERT3U(exp
, <, 8 * sizeof (offset
));
968 return (offset
>> exp
);
970 ASSERT3U(offset
, <, dn
->dn_datablksz
);
976 dbuf_read_done(zio_t
*zio
, int err
, arc_buf_t
*buf
, void *vdb
)
978 dmu_buf_impl_t
*db
= vdb
;
980 mutex_enter(&db
->db_mtx
);
981 ASSERT3U(db
->db_state
, ==, DB_READ
);
983 * All reads are synchronous, so we must have a hold on the dbuf
985 ASSERT(refcount_count(&db
->db_holds
) > 0);
986 ASSERT(db
->db_buf
== NULL
);
987 ASSERT(db
->db
.db_data
== NULL
);
988 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
989 /* we were freed in flight; disregard any error */
990 arc_release(buf
, db
);
991 bzero(buf
->b_data
, db
->db
.db_size
);
993 db
->db_freed_in_flight
= FALSE
;
994 dbuf_set_data(db
, buf
);
995 db
->db_state
= DB_CACHED
;
996 } else if (err
== 0) {
997 dbuf_set_data(db
, buf
);
998 db
->db_state
= DB_CACHED
;
1000 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1001 ASSERT3P(db
->db_buf
, ==, NULL
);
1002 arc_buf_destroy(buf
, db
);
1003 db
->db_state
= DB_UNCACHED
;
1005 cv_broadcast(&db
->db_changed
);
1006 dbuf_rele_and_unlock(db
, NULL
);
1010 dbuf_read_impl(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t flags
)
1013 zbookmark_phys_t zb
;
1014 uint32_t aflags
= ARC_FLAG_NOWAIT
;
1015 int err
, zio_flags
= 0;
1019 ASSERT(!refcount_is_zero(&db
->db_holds
));
1020 /* We need the struct_rwlock to prevent db_blkptr from changing. */
1021 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
1022 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1023 ASSERT(db
->db_state
== DB_UNCACHED
);
1024 ASSERT(db
->db_buf
== NULL
);
1026 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1028 * The bonus length stored in the dnode may be less than
1029 * the maximum available space in the bonus buffer.
1031 int bonuslen
= MIN(dn
->dn_bonuslen
, dn
->dn_phys
->dn_bonuslen
);
1032 int max_bonuslen
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
);
1033 arc_buf_t
*dn_buf
= (dn
->dn_dbuf
!= NULL
) ?
1034 dn
->dn_dbuf
->db_buf
: NULL
;
1036 /* if the underlying dnode block is encrypted, decrypt it */
1037 if (dn_buf
!= NULL
&& dn
->dn_objset
->os_encrypted
&&
1038 DMU_OT_IS_ENCRYPTED(dn
->dn_bonustype
) &&
1039 (flags
& DB_RF_NO_DECRYPT
) == 0 &&
1040 arc_is_encrypted(dn_buf
)) {
1041 err
= arc_untransform(dn_buf
, dn
->dn_objset
->os_spa
,
1042 dmu_objset_id(dn
->dn_objset
), B_TRUE
);
1045 mutex_exit(&db
->db_mtx
);
1050 ASSERT3U(bonuslen
, <=, db
->db
.db_size
);
1051 db
->db
.db_data
= kmem_alloc(max_bonuslen
, KM_SLEEP
);
1052 arc_space_consume(max_bonuslen
, ARC_SPACE_BONUS
);
1053 if (bonuslen
< max_bonuslen
)
1054 bzero(db
->db
.db_data
, max_bonuslen
);
1056 bcopy(DN_BONUS(dn
->dn_phys
), db
->db
.db_data
, bonuslen
);
1058 db
->db_state
= DB_CACHED
;
1059 mutex_exit(&db
->db_mtx
);
1064 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
1065 * processes the delete record and clears the bp while we are waiting
1066 * for the dn_mtx (resulting in a "no" from block_freed).
1068 if (db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
) ||
1069 (db
->db_level
== 0 && (dnode_block_freed(dn
, db
->db_blkid
) ||
1070 BP_IS_HOLE(db
->db_blkptr
)))) {
1071 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1073 dbuf_set_data(db
, arc_alloc_buf(db
->db_objset
->os_spa
, db
, type
,
1075 bzero(db
->db
.db_data
, db
->db
.db_size
);
1077 if (db
->db_blkptr
!= NULL
&& db
->db_level
> 0 &&
1078 BP_IS_HOLE(db
->db_blkptr
) &&
1079 db
->db_blkptr
->blk_birth
!= 0) {
1080 blkptr_t
*bps
= db
->db
.db_data
;
1081 for (int i
= 0; i
< ((1 <<
1082 DB_DNODE(db
)->dn_indblkshift
) / sizeof (blkptr_t
));
1084 blkptr_t
*bp
= &bps
[i
];
1085 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==,
1086 1 << dn
->dn_indblkshift
);
1088 BP_GET_LEVEL(db
->db_blkptr
) == 1 ?
1090 BP_GET_LSIZE(db
->db_blkptr
));
1091 BP_SET_TYPE(bp
, BP_GET_TYPE(db
->db_blkptr
));
1093 BP_GET_LEVEL(db
->db_blkptr
) - 1);
1094 BP_SET_BIRTH(bp
, db
->db_blkptr
->blk_birth
, 0);
1098 db
->db_state
= DB_CACHED
;
1099 mutex_exit(&db
->db_mtx
);
1105 db
->db_state
= DB_READ
;
1106 mutex_exit(&db
->db_mtx
);
1108 if (DBUF_IS_L2CACHEABLE(db
))
1109 aflags
|= ARC_FLAG_L2CACHE
;
1111 SET_BOOKMARK(&zb
, db
->db_objset
->os_dsl_dataset
?
1112 db
->db_objset
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
1113 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
1116 * All bps of an encrypted os should have the encryption bit set.
1117 * If this is not true it indicates tampering and we report an error.
1119 if (db
->db_objset
->os_encrypted
&& !BP_USES_CRYPT(db
->db_blkptr
)) {
1120 spa_log_error(db
->db_objset
->os_spa
, &zb
);
1121 zfs_panic_recover("unencrypted block in encrypted "
1122 "object set %llu", dmu_objset_id(db
->db_objset
));
1123 return (SET_ERROR(EIO
));
1126 dbuf_add_ref(db
, NULL
);
1128 zio_flags
= (flags
& DB_RF_CANFAIL
) ?
1129 ZIO_FLAG_CANFAIL
: ZIO_FLAG_MUSTSUCCEED
;
1131 if ((flags
& DB_RF_NO_DECRYPT
) && BP_IS_PROTECTED(db
->db_blkptr
))
1132 zio_flags
|= ZIO_FLAG_RAW
;
1134 err
= arc_read(zio
, db
->db_objset
->os_spa
, db
->db_blkptr
,
1135 dbuf_read_done
, db
, ZIO_PRIORITY_SYNC_READ
, zio_flags
,
1142 * This is our just-in-time copy function. It makes a copy of buffers that
1143 * have been modified in a previous transaction group before we access them in
1144 * the current active group.
1146 * This function is used in three places: when we are dirtying a buffer for the
1147 * first time in a txg, when we are freeing a range in a dnode that includes
1148 * this buffer, and when we are accessing a buffer which was received compressed
1149 * and later referenced in a WRITE_BYREF record.
1151 * Note that when we are called from dbuf_free_range() we do not put a hold on
1152 * the buffer, we just traverse the active dbuf list for the dnode.
1155 dbuf_fix_old_data(dmu_buf_impl_t
*db
, uint64_t txg
)
1157 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
1159 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1160 ASSERT(db
->db
.db_data
!= NULL
);
1161 ASSERT(db
->db_level
== 0);
1162 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
);
1165 (dr
->dt
.dl
.dr_data
!=
1166 ((db
->db_blkid
== DMU_BONUS_BLKID
) ? db
->db
.db_data
: db
->db_buf
)))
1170 * If the last dirty record for this dbuf has not yet synced
1171 * and its referencing the dbuf data, either:
1172 * reset the reference to point to a new copy,
1173 * or (if there a no active holders)
1174 * just null out the current db_data pointer.
1176 ASSERT3U(dr
->dr_txg
, >=, txg
- 2);
1177 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1178 dnode_t
*dn
= DB_DNODE(db
);
1179 int bonuslen
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
);
1180 dr
->dt
.dl
.dr_data
= kmem_alloc(bonuslen
, KM_SLEEP
);
1181 arc_space_consume(bonuslen
, ARC_SPACE_BONUS
);
1182 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
, bonuslen
);
1183 } else if (refcount_count(&db
->db_holds
) > db
->db_dirtycnt
) {
1184 dnode_t
*dn
= DB_DNODE(db
);
1185 int size
= arc_buf_size(db
->db_buf
);
1186 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1187 spa_t
*spa
= db
->db_objset
->os_spa
;
1188 enum zio_compress compress_type
=
1189 arc_get_compression(db
->db_buf
);
1191 if (arc_is_encrypted(db
->db_buf
)) {
1192 boolean_t byteorder
;
1193 uint8_t salt
[ZIO_DATA_SALT_LEN
];
1194 uint8_t iv
[ZIO_DATA_IV_LEN
];
1195 uint8_t mac
[ZIO_DATA_MAC_LEN
];
1197 arc_get_raw_params(db
->db_buf
, &byteorder
, salt
,
1199 dr
->dt
.dl
.dr_data
= arc_alloc_raw_buf(spa
, db
,
1200 dmu_objset_id(dn
->dn_objset
), byteorder
, salt
, iv
,
1201 mac
, dn
->dn_type
, size
, arc_buf_lsize(db
->db_buf
),
1203 } else if (compress_type
!= ZIO_COMPRESS_OFF
) {
1204 ASSERT3U(type
, ==, ARC_BUFC_DATA
);
1205 dr
->dt
.dl
.dr_data
= arc_alloc_compressed_buf(spa
, db
,
1206 size
, arc_buf_lsize(db
->db_buf
), compress_type
);
1208 dr
->dt
.dl
.dr_data
= arc_alloc_buf(spa
, db
, type
, size
);
1210 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
->b_data
, size
);
1213 dbuf_clear_data(db
);
1218 dbuf_read(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t flags
)
1225 * We don't have to hold the mutex to check db_state because it
1226 * can't be freed while we have a hold on the buffer.
1228 ASSERT(!refcount_is_zero(&db
->db_holds
));
1230 if (db
->db_state
== DB_NOFILL
)
1231 return (SET_ERROR(EIO
));
1235 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1236 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
1238 prefetch
= db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1239 (flags
& DB_RF_NOPREFETCH
) == 0 && dn
!= NULL
&&
1240 DBUF_IS_CACHEABLE(db
);
1242 mutex_enter(&db
->db_mtx
);
1243 if (db
->db_state
== DB_CACHED
) {
1244 spa_t
*spa
= dn
->dn_objset
->os_spa
;
1247 * If the arc buf is compressed or encrypted, we need to
1248 * untransform it to read the data. This could happen during
1249 * the "zfs receive" of a stream which is deduplicated and
1250 * either raw or compressed. We do not need to do this if the
1251 * caller wants raw encrypted data.
1253 if (db
->db_buf
!= NULL
&& (flags
& DB_RF_NO_DECRYPT
) == 0 &&
1254 (arc_is_encrypted(db
->db_buf
) ||
1255 arc_get_compression(db
->db_buf
) != ZIO_COMPRESS_OFF
)) {
1256 dbuf_fix_old_data(db
, spa_syncing_txg(spa
));
1257 err
= arc_untransform(db
->db_buf
, spa
,
1258 dmu_objset_id(db
->db_objset
), B_FALSE
);
1259 dbuf_set_data(db
, db
->db_buf
);
1261 mutex_exit(&db
->db_mtx
);
1263 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
);
1264 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1265 rw_exit(&dn
->dn_struct_rwlock
);
1267 } else if (db
->db_state
== DB_UNCACHED
) {
1268 spa_t
*spa
= dn
->dn_objset
->os_spa
;
1269 boolean_t need_wait
= B_FALSE
;
1272 db
->db_blkptr
!= NULL
&& !BP_IS_HOLE(db
->db_blkptr
)) {
1273 zio
= zio_root(spa
, NULL
, NULL
, ZIO_FLAG_CANFAIL
);
1276 err
= dbuf_read_impl(db
, zio
, flags
);
1278 /* dbuf_read_impl has dropped db_mtx for us */
1280 if (!err
&& prefetch
)
1281 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
);
1283 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1284 rw_exit(&dn
->dn_struct_rwlock
);
1287 if (!err
&& need_wait
)
1288 err
= zio_wait(zio
);
1291 * Another reader came in while the dbuf was in flight
1292 * between UNCACHED and CACHED. Either a writer will finish
1293 * writing the buffer (sending the dbuf to CACHED) or the
1294 * first reader's request will reach the read_done callback
1295 * and send the dbuf to CACHED. Otherwise, a failure
1296 * occurred and the dbuf went to UNCACHED.
1298 mutex_exit(&db
->db_mtx
);
1300 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
);
1301 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1302 rw_exit(&dn
->dn_struct_rwlock
);
1305 /* Skip the wait per the caller's request. */
1306 mutex_enter(&db
->db_mtx
);
1307 if ((flags
& DB_RF_NEVERWAIT
) == 0) {
1308 while (db
->db_state
== DB_READ
||
1309 db
->db_state
== DB_FILL
) {
1310 ASSERT(db
->db_state
== DB_READ
||
1311 (flags
& DB_RF_HAVESTRUCT
) == 0);
1312 DTRACE_PROBE2(blocked__read
, dmu_buf_impl_t
*,
1314 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1316 if (db
->db_state
== DB_UNCACHED
)
1317 err
= SET_ERROR(EIO
);
1319 mutex_exit(&db
->db_mtx
);
1326 dbuf_noread(dmu_buf_impl_t
*db
)
1328 ASSERT(!refcount_is_zero(&db
->db_holds
));
1329 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1330 mutex_enter(&db
->db_mtx
);
1331 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
1332 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1333 if (db
->db_state
== DB_UNCACHED
) {
1334 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1335 spa_t
*spa
= db
->db_objset
->os_spa
;
1337 ASSERT(db
->db_buf
== NULL
);
1338 ASSERT(db
->db
.db_data
== NULL
);
1339 dbuf_set_data(db
, arc_alloc_buf(spa
, db
, type
, db
->db
.db_size
));
1340 db
->db_state
= DB_FILL
;
1341 } else if (db
->db_state
== DB_NOFILL
) {
1342 dbuf_clear_data(db
);
1344 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
1346 mutex_exit(&db
->db_mtx
);
1350 dbuf_unoverride(dbuf_dirty_record_t
*dr
)
1352 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
1353 blkptr_t
*bp
= &dr
->dt
.dl
.dr_overridden_by
;
1354 uint64_t txg
= dr
->dr_txg
;
1356 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1358 * This assert is valid because dmu_sync() expects to be called by
1359 * a zilog's get_data while holding a range lock. This call only
1360 * comes from dbuf_dirty() callers who must also hold a range lock.
1362 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_IN_DMU_SYNC
);
1363 ASSERT(db
->db_level
== 0);
1365 if (db
->db_blkid
== DMU_BONUS_BLKID
||
1366 dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
)
1369 ASSERT(db
->db_data_pending
!= dr
);
1371 /* free this block */
1372 if (!BP_IS_HOLE(bp
) && !dr
->dt
.dl
.dr_nopwrite
)
1373 zio_free(db
->db_objset
->os_spa
, txg
, bp
);
1375 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
1376 dr
->dt
.dl
.dr_nopwrite
= B_FALSE
;
1377 dr
->dt
.dl
.dr_raw
= B_FALSE
;
1380 * Release the already-written buffer, so we leave it in
1381 * a consistent dirty state. Note that all callers are
1382 * modifying the buffer, so they will immediately do
1383 * another (redundant) arc_release(). Therefore, leave
1384 * the buf thawed to save the effort of freezing &
1385 * immediately re-thawing it.
1387 arc_release(dr
->dt
.dl
.dr_data
, db
);
1391 * Evict (if its unreferenced) or clear (if its referenced) any level-0
1392 * data blocks in the free range, so that any future readers will find
1396 dbuf_free_range(dnode_t
*dn
, uint64_t start_blkid
, uint64_t end_blkid
,
1399 dmu_buf_impl_t
*db_search
;
1400 dmu_buf_impl_t
*db
, *db_next
;
1401 uint64_t txg
= tx
->tx_txg
;
1404 if (end_blkid
> dn
->dn_maxblkid
&&
1405 !(start_blkid
== DMU_SPILL_BLKID
|| end_blkid
== DMU_SPILL_BLKID
))
1406 end_blkid
= dn
->dn_maxblkid
;
1407 dprintf_dnode(dn
, "start=%llu end=%llu\n", start_blkid
, end_blkid
);
1409 db_search
= kmem_alloc(sizeof (dmu_buf_impl_t
), KM_SLEEP
);
1410 db_search
->db_level
= 0;
1411 db_search
->db_blkid
= start_blkid
;
1412 db_search
->db_state
= DB_SEARCH
;
1414 mutex_enter(&dn
->dn_dbufs_mtx
);
1415 db
= avl_find(&dn
->dn_dbufs
, db_search
, &where
);
1416 ASSERT3P(db
, ==, NULL
);
1418 db
= avl_nearest(&dn
->dn_dbufs
, where
, AVL_AFTER
);
1420 for (; db
!= NULL
; db
= db_next
) {
1421 db_next
= AVL_NEXT(&dn
->dn_dbufs
, db
);
1422 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1424 if (db
->db_level
!= 0 || db
->db_blkid
> end_blkid
) {
1427 ASSERT3U(db
->db_blkid
, >=, start_blkid
);
1429 /* found a level 0 buffer in the range */
1430 mutex_enter(&db
->db_mtx
);
1431 if (dbuf_undirty(db
, tx
)) {
1432 /* mutex has been dropped and dbuf destroyed */
1436 if (db
->db_state
== DB_UNCACHED
||
1437 db
->db_state
== DB_NOFILL
||
1438 db
->db_state
== DB_EVICTING
) {
1439 ASSERT(db
->db
.db_data
== NULL
);
1440 mutex_exit(&db
->db_mtx
);
1443 if (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
) {
1444 /* will be handled in dbuf_read_done or dbuf_rele */
1445 db
->db_freed_in_flight
= TRUE
;
1446 mutex_exit(&db
->db_mtx
);
1449 if (refcount_count(&db
->db_holds
) == 0) {
1454 /* The dbuf is referenced */
1456 if (db
->db_last_dirty
!= NULL
) {
1457 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
1459 if (dr
->dr_txg
== txg
) {
1461 * This buffer is "in-use", re-adjust the file
1462 * size to reflect that this buffer may
1463 * contain new data when we sync.
1465 if (db
->db_blkid
!= DMU_SPILL_BLKID
&&
1466 db
->db_blkid
> dn
->dn_maxblkid
)
1467 dn
->dn_maxblkid
= db
->db_blkid
;
1468 dbuf_unoverride(dr
);
1471 * This dbuf is not dirty in the open context.
1472 * Either uncache it (if its not referenced in
1473 * the open context) or reset its contents to
1476 dbuf_fix_old_data(db
, txg
);
1479 /* clear the contents if its cached */
1480 if (db
->db_state
== DB_CACHED
) {
1481 ASSERT(db
->db
.db_data
!= NULL
);
1482 arc_release(db
->db_buf
, db
);
1483 bzero(db
->db
.db_data
, db
->db
.db_size
);
1484 arc_buf_freeze(db
->db_buf
);
1487 mutex_exit(&db
->db_mtx
);
1490 kmem_free(db_search
, sizeof (dmu_buf_impl_t
));
1491 mutex_exit(&dn
->dn_dbufs_mtx
);
1495 dbuf_new_size(dmu_buf_impl_t
*db
, int size
, dmu_tx_t
*tx
)
1497 arc_buf_t
*buf
, *obuf
;
1498 int osize
= db
->db
.db_size
;
1499 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1502 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1507 /* XXX does *this* func really need the lock? */
1508 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
1511 * This call to dmu_buf_will_dirty() with the dn_struct_rwlock held
1512 * is OK, because there can be no other references to the db
1513 * when we are changing its size, so no concurrent DB_FILL can
1517 * XXX we should be doing a dbuf_read, checking the return
1518 * value and returning that up to our callers
1520 dmu_buf_will_dirty(&db
->db
, tx
);
1522 /* create the data buffer for the new block */
1523 buf
= arc_alloc_buf(dn
->dn_objset
->os_spa
, db
, type
, size
);
1525 /* copy old block data to the new block */
1527 bcopy(obuf
->b_data
, buf
->b_data
, MIN(osize
, size
));
1528 /* zero the remainder */
1530 bzero((uint8_t *)buf
->b_data
+ osize
, size
- osize
);
1532 mutex_enter(&db
->db_mtx
);
1533 dbuf_set_data(db
, buf
);
1534 arc_buf_destroy(obuf
, db
);
1535 db
->db
.db_size
= size
;
1537 if (db
->db_level
== 0) {
1538 ASSERT3U(db
->db_last_dirty
->dr_txg
, ==, tx
->tx_txg
);
1539 db
->db_last_dirty
->dt
.dl
.dr_data
= buf
;
1541 mutex_exit(&db
->db_mtx
);
1543 dmu_objset_willuse_space(dn
->dn_objset
, size
- osize
, tx
);
1548 dbuf_release_bp(dmu_buf_impl_t
*db
)
1550 ASSERTV(objset_t
*os
= db
->db_objset
);
1552 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os
)));
1553 ASSERT(arc_released(os
->os_phys_buf
) ||
1554 list_link_active(&os
->os_dsl_dataset
->ds_synced_link
));
1555 ASSERT(db
->db_parent
== NULL
|| arc_released(db
->db_parent
->db_buf
));
1557 (void) arc_release(db
->db_buf
, db
);
1561 * We already have a dirty record for this TXG, and we are being
1565 dbuf_redirty(dbuf_dirty_record_t
*dr
)
1567 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
1569 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1571 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
) {
1573 * If this buffer has already been written out,
1574 * we now need to reset its state.
1576 dbuf_unoverride(dr
);
1577 if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
&&
1578 db
->db_state
!= DB_NOFILL
) {
1579 /* Already released on initial dirty, so just thaw. */
1580 ASSERT(arc_released(db
->db_buf
));
1581 arc_buf_thaw(db
->db_buf
);
1586 dbuf_dirty_record_t
*
1587 dbuf_dirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1591 dbuf_dirty_record_t
**drp
, *dr
;
1592 int drop_struct_lock
= FALSE
;
1593 int txgoff
= tx
->tx_txg
& TXG_MASK
;
1595 ASSERT(tx
->tx_txg
!= 0);
1596 ASSERT(!refcount_is_zero(&db
->db_holds
));
1597 DMU_TX_DIRTY_BUF(tx
, db
);
1602 * Shouldn't dirty a regular buffer in syncing context. Private
1603 * objects may be dirtied in syncing context, but only if they
1604 * were already pre-dirtied in open context.
1607 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
) {
1608 rrw_enter(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
,
1611 ASSERT(!dmu_tx_is_syncing(tx
) ||
1612 BP_IS_HOLE(dn
->dn_objset
->os_rootbp
) ||
1613 DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
1614 dn
->dn_objset
->os_dsl_dataset
== NULL
);
1615 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
1616 rrw_exit(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
, FTAG
);
1619 * We make this assert for private objects as well, but after we
1620 * check if we're already dirty. They are allowed to re-dirty
1621 * in syncing context.
1623 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
1624 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
1625 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
1627 mutex_enter(&db
->db_mtx
);
1629 * XXX make this true for indirects too? The problem is that
1630 * transactions created with dmu_tx_create_assigned() from
1631 * syncing context don't bother holding ahead.
1633 ASSERT(db
->db_level
!= 0 ||
1634 db
->db_state
== DB_CACHED
|| db
->db_state
== DB_FILL
||
1635 db
->db_state
== DB_NOFILL
);
1637 mutex_enter(&dn
->dn_mtx
);
1639 * Don't set dirtyctx to SYNC if we're just modifying this as we
1640 * initialize the objset.
1642 if (dn
->dn_dirtyctx
== DN_UNDIRTIED
) {
1643 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
) {
1644 rrw_enter(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
,
1647 if (!BP_IS_HOLE(dn
->dn_objset
->os_rootbp
)) {
1648 dn
->dn_dirtyctx
= (dmu_tx_is_syncing(tx
) ?
1649 DN_DIRTY_SYNC
: DN_DIRTY_OPEN
);
1650 ASSERT(dn
->dn_dirtyctx_firstset
== NULL
);
1651 dn
->dn_dirtyctx_firstset
= kmem_alloc(1, KM_SLEEP
);
1653 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
) {
1654 rrw_exit(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
,
1658 mutex_exit(&dn
->dn_mtx
);
1660 if (db
->db_blkid
== DMU_SPILL_BLKID
)
1661 dn
->dn_have_spill
= B_TRUE
;
1664 * If this buffer is already dirty, we're done.
1666 drp
= &db
->db_last_dirty
;
1667 ASSERT(*drp
== NULL
|| (*drp
)->dr_txg
<= tx
->tx_txg
||
1668 db
->db
.db_object
== DMU_META_DNODE_OBJECT
);
1669 while ((dr
= *drp
) != NULL
&& dr
->dr_txg
> tx
->tx_txg
)
1671 if (dr
&& dr
->dr_txg
== tx
->tx_txg
) {
1675 mutex_exit(&db
->db_mtx
);
1680 * Only valid if not already dirty.
1682 ASSERT(dn
->dn_object
== 0 ||
1683 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
1684 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
1686 ASSERT3U(dn
->dn_nlevels
, >, db
->db_level
);
1689 * We should only be dirtying in syncing context if it's the
1690 * mos or we're initializing the os or it's a special object.
1691 * However, we are allowed to dirty in syncing context provided
1692 * we already dirtied it in open context. Hence we must make
1693 * this assertion only if we're not already dirty.
1696 VERIFY3U(tx
->tx_txg
, <=, spa_final_dirty_txg(os
->os_spa
));
1698 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
1699 rrw_enter(&os
->os_dsl_dataset
->ds_bp_rwlock
, RW_READER
, FTAG
);
1700 ASSERT(!dmu_tx_is_syncing(tx
) || DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
1701 os
->os_dsl_dataset
== NULL
|| BP_IS_HOLE(os
->os_rootbp
));
1702 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
1703 rrw_exit(&os
->os_dsl_dataset
->ds_bp_rwlock
, FTAG
);
1705 ASSERT(db
->db
.db_size
!= 0);
1707 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
1709 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
1710 dmu_objset_willuse_space(os
, db
->db
.db_size
, tx
);
1714 * If this buffer is dirty in an old transaction group we need
1715 * to make a copy of it so that the changes we make in this
1716 * transaction group won't leak out when we sync the older txg.
1718 dr
= kmem_zalloc(sizeof (dbuf_dirty_record_t
), KM_SLEEP
);
1719 list_link_init(&dr
->dr_dirty_node
);
1720 if (db
->db_level
== 0) {
1721 void *data_old
= db
->db_buf
;
1723 if (db
->db_state
!= DB_NOFILL
) {
1724 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1725 dbuf_fix_old_data(db
, tx
->tx_txg
);
1726 data_old
= db
->db
.db_data
;
1727 } else if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
) {
1729 * Release the data buffer from the cache so
1730 * that we can modify it without impacting
1731 * possible other users of this cached data
1732 * block. Note that indirect blocks and
1733 * private objects are not released until the
1734 * syncing state (since they are only modified
1737 arc_release(db
->db_buf
, db
);
1738 dbuf_fix_old_data(db
, tx
->tx_txg
);
1739 data_old
= db
->db_buf
;
1741 ASSERT(data_old
!= NULL
);
1743 dr
->dt
.dl
.dr_data
= data_old
;
1745 mutex_init(&dr
->dt
.di
.dr_mtx
, NULL
, MUTEX_NOLOCKDEP
, NULL
);
1746 list_create(&dr
->dt
.di
.dr_children
,
1747 sizeof (dbuf_dirty_record_t
),
1748 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
1750 if (db
->db_blkid
!= DMU_BONUS_BLKID
&& os
->os_dsl_dataset
!= NULL
)
1751 dr
->dr_accounted
= db
->db
.db_size
;
1753 dr
->dr_txg
= tx
->tx_txg
;
1758 * We could have been freed_in_flight between the dbuf_noread
1759 * and dbuf_dirty. We win, as though the dbuf_noread() had
1760 * happened after the free.
1762 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1763 db
->db_blkid
!= DMU_SPILL_BLKID
) {
1764 mutex_enter(&dn
->dn_mtx
);
1765 if (dn
->dn_free_ranges
[txgoff
] != NULL
) {
1766 range_tree_clear(dn
->dn_free_ranges
[txgoff
],
1769 mutex_exit(&dn
->dn_mtx
);
1770 db
->db_freed_in_flight
= FALSE
;
1774 * This buffer is now part of this txg
1776 dbuf_add_ref(db
, (void *)(uintptr_t)tx
->tx_txg
);
1777 db
->db_dirtycnt
+= 1;
1778 ASSERT3U(db
->db_dirtycnt
, <=, 3);
1780 mutex_exit(&db
->db_mtx
);
1782 if (db
->db_blkid
== DMU_BONUS_BLKID
||
1783 db
->db_blkid
== DMU_SPILL_BLKID
) {
1784 mutex_enter(&dn
->dn_mtx
);
1785 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1786 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
1787 mutex_exit(&dn
->dn_mtx
);
1788 dnode_setdirty(dn
, tx
);
1794 * The dn_struct_rwlock prevents db_blkptr from changing
1795 * due to a write from syncing context completing
1796 * while we are running, so we want to acquire it before
1797 * looking at db_blkptr.
1799 if (!RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
1800 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
1801 drop_struct_lock
= TRUE
;
1805 * We need to hold the dn_struct_rwlock to make this assertion,
1806 * because it protects dn_phys / dn_next_nlevels from changing.
1808 ASSERT((dn
->dn_phys
->dn_nlevels
== 0 && db
->db_level
== 0) ||
1809 dn
->dn_phys
->dn_nlevels
> db
->db_level
||
1810 dn
->dn_next_nlevels
[txgoff
] > db
->db_level
||
1811 dn
->dn_next_nlevels
[(tx
->tx_txg
-1) & TXG_MASK
] > db
->db_level
||
1812 dn
->dn_next_nlevels
[(tx
->tx_txg
-2) & TXG_MASK
] > db
->db_level
);
1815 * If we are overwriting a dedup BP, then unless it is snapshotted,
1816 * when we get to syncing context we will need to decrement its
1817 * refcount in the DDT. Prefetch the relevant DDT block so that
1818 * syncing context won't have to wait for the i/o.
1820 ddt_prefetch(os
->os_spa
, db
->db_blkptr
);
1822 if (db
->db_level
== 0) {
1823 dnode_new_blkid(dn
, db
->db_blkid
, tx
, drop_struct_lock
);
1824 ASSERT(dn
->dn_maxblkid
>= db
->db_blkid
);
1827 if (db
->db_level
+1 < dn
->dn_nlevels
) {
1828 dmu_buf_impl_t
*parent
= db
->db_parent
;
1829 dbuf_dirty_record_t
*di
;
1830 int parent_held
= FALSE
;
1832 if (db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
) {
1833 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1835 parent
= dbuf_hold_level(dn
, db
->db_level
+1,
1836 db
->db_blkid
>> epbs
, FTAG
);
1837 ASSERT(parent
!= NULL
);
1840 if (drop_struct_lock
)
1841 rw_exit(&dn
->dn_struct_rwlock
);
1842 ASSERT3U(db
->db_level
+1, ==, parent
->db_level
);
1843 di
= dbuf_dirty(parent
, tx
);
1845 dbuf_rele(parent
, FTAG
);
1847 mutex_enter(&db
->db_mtx
);
1849 * Since we've dropped the mutex, it's possible that
1850 * dbuf_undirty() might have changed this out from under us.
1852 if (db
->db_last_dirty
== dr
||
1853 dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
1854 mutex_enter(&di
->dt
.di
.dr_mtx
);
1855 ASSERT3U(di
->dr_txg
, ==, tx
->tx_txg
);
1856 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1857 list_insert_tail(&di
->dt
.di
.dr_children
, dr
);
1858 mutex_exit(&di
->dt
.di
.dr_mtx
);
1861 mutex_exit(&db
->db_mtx
);
1863 ASSERT(db
->db_level
+1 == dn
->dn_nlevels
);
1864 ASSERT(db
->db_blkid
< dn
->dn_nblkptr
);
1865 ASSERT(db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
1866 mutex_enter(&dn
->dn_mtx
);
1867 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1868 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
1869 mutex_exit(&dn
->dn_mtx
);
1870 if (drop_struct_lock
)
1871 rw_exit(&dn
->dn_struct_rwlock
);
1874 dnode_setdirty(dn
, tx
);
1880 * Undirty a buffer in the transaction group referenced by the given
1881 * transaction. Return whether this evicted the dbuf.
1884 dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1887 uint64_t txg
= tx
->tx_txg
;
1888 dbuf_dirty_record_t
*dr
, **drp
;
1893 * Due to our use of dn_nlevels below, this can only be called
1894 * in open context, unless we are operating on the MOS.
1895 * From syncing context, dn_nlevels may be different from the
1896 * dn_nlevels used when dbuf was dirtied.
1898 ASSERT(db
->db_objset
==
1899 dmu_objset_pool(db
->db_objset
)->dp_meta_objset
||
1900 txg
!= spa_syncing_txg(dmu_objset_spa(db
->db_objset
)));
1901 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1902 ASSERT0(db
->db_level
);
1903 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1906 * If this buffer is not dirty, we're done.
1908 for (drp
= &db
->db_last_dirty
; (dr
= *drp
) != NULL
; drp
= &dr
->dr_next
)
1909 if (dr
->dr_txg
<= txg
)
1911 if (dr
== NULL
|| dr
->dr_txg
< txg
)
1913 ASSERT(dr
->dr_txg
== txg
);
1914 ASSERT(dr
->dr_dbuf
== db
);
1919 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
1921 ASSERT(db
->db
.db_size
!= 0);
1923 dsl_pool_undirty_space(dmu_objset_pool(dn
->dn_objset
),
1924 dr
->dr_accounted
, txg
);
1929 * Note that there are three places in dbuf_dirty()
1930 * where this dirty record may be put on a list.
1931 * Make sure to do a list_remove corresponding to
1932 * every one of those list_insert calls.
1934 if (dr
->dr_parent
) {
1935 mutex_enter(&dr
->dr_parent
->dt
.di
.dr_mtx
);
1936 list_remove(&dr
->dr_parent
->dt
.di
.dr_children
, dr
);
1937 mutex_exit(&dr
->dr_parent
->dt
.di
.dr_mtx
);
1938 } else if (db
->db_blkid
== DMU_SPILL_BLKID
||
1939 db
->db_level
+ 1 == dn
->dn_nlevels
) {
1940 ASSERT(db
->db_blkptr
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
1941 mutex_enter(&dn
->dn_mtx
);
1942 list_remove(&dn
->dn_dirty_records
[txg
& TXG_MASK
], dr
);
1943 mutex_exit(&dn
->dn_mtx
);
1947 if (db
->db_state
!= DB_NOFILL
) {
1948 dbuf_unoverride(dr
);
1950 ASSERT(db
->db_buf
!= NULL
);
1951 ASSERT(dr
->dt
.dl
.dr_data
!= NULL
);
1952 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
1953 arc_buf_destroy(dr
->dt
.dl
.dr_data
, db
);
1956 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
1958 ASSERT(db
->db_dirtycnt
> 0);
1959 db
->db_dirtycnt
-= 1;
1961 if (refcount_remove(&db
->db_holds
, (void *)(uintptr_t)txg
) == 0) {
1962 ASSERT(db
->db_state
== DB_NOFILL
|| arc_released(db
->db_buf
));
1971 dmu_buf_will_dirty_impl(dmu_buf_t
*db_fake
, int flags
, dmu_tx_t
*tx
)
1973 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1975 ASSERT(tx
->tx_txg
!= 0);
1976 ASSERT(!refcount_is_zero(&db
->db_holds
));
1979 * Quick check for dirtyness. For already dirty blocks, this
1980 * reduces runtime of this function by >90%, and overall performance
1981 * by 50% for some workloads (e.g. file deletion with indirect blocks
1984 mutex_enter(&db
->db_mtx
);
1986 dbuf_dirty_record_t
*dr
;
1987 for (dr
= db
->db_last_dirty
;
1988 dr
!= NULL
&& dr
->dr_txg
>= tx
->tx_txg
; dr
= dr
->dr_next
) {
1990 * It's possible that it is already dirty but not cached,
1991 * because there are some calls to dbuf_dirty() that don't
1992 * go through dmu_buf_will_dirty().
1994 if (dr
->dr_txg
== tx
->tx_txg
&& db
->db_state
== DB_CACHED
) {
1995 /* This dbuf is already dirty and cached. */
1997 mutex_exit(&db
->db_mtx
);
2001 mutex_exit(&db
->db_mtx
);
2004 if (RW_WRITE_HELD(&DB_DNODE(db
)->dn_struct_rwlock
))
2005 flags
|= DB_RF_HAVESTRUCT
;
2007 (void) dbuf_read(db
, NULL
, flags
);
2008 (void) dbuf_dirty(db
, tx
);
2012 dmu_buf_will_dirty(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
2014 dmu_buf_will_dirty_impl(db_fake
,
2015 DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
, tx
);
2019 dmu_buf_will_not_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
2021 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2023 db
->db_state
= DB_NOFILL
;
2025 dmu_buf_will_fill(db_fake
, tx
);
2029 dmu_buf_will_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
2031 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2033 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2034 ASSERT(tx
->tx_txg
!= 0);
2035 ASSERT(db
->db_level
== 0);
2036 ASSERT(!refcount_is_zero(&db
->db_holds
));
2038 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
||
2039 dmu_tx_private_ok(tx
));
2042 (void) dbuf_dirty(db
, tx
);
2046 * This function is effectively the same as dmu_buf_will_dirty(), but
2047 * indicates the caller expects raw encrypted data in the db. It will
2048 * also set the raw flag on the created dirty record.
2051 dmu_buf_will_change_crypt_params(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
2053 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2054 dbuf_dirty_record_t
*dr
;
2056 dmu_buf_will_dirty_impl(db_fake
,
2057 DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
| DB_RF_NO_DECRYPT
, tx
);
2059 dr
= db
->db_last_dirty
;
2060 while (dr
!= NULL
&& dr
->dr_txg
> tx
->tx_txg
)
2063 ASSERT3P(dr
, !=, NULL
);
2064 ASSERT3U(dr
->dr_txg
, ==, tx
->tx_txg
);
2065 dr
->dt
.dl
.dr_raw
= B_TRUE
;
2068 #pragma weak dmu_buf_fill_done = dbuf_fill_done
2071 dbuf_fill_done(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
2073 mutex_enter(&db
->db_mtx
);
2076 if (db
->db_state
== DB_FILL
) {
2077 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
2078 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2079 /* we were freed while filling */
2080 /* XXX dbuf_undirty? */
2081 bzero(db
->db
.db_data
, db
->db
.db_size
);
2082 db
->db_freed_in_flight
= FALSE
;
2084 db
->db_state
= DB_CACHED
;
2085 cv_broadcast(&db
->db_changed
);
2087 mutex_exit(&db
->db_mtx
);
2091 dmu_buf_write_embedded(dmu_buf_t
*dbuf
, void *data
,
2092 bp_embedded_type_t etype
, enum zio_compress comp
,
2093 int uncompressed_size
, int compressed_size
, int byteorder
,
2096 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbuf
;
2097 struct dirty_leaf
*dl
;
2098 dmu_object_type_t type
;
2100 if (etype
== BP_EMBEDDED_TYPE_DATA
) {
2101 ASSERT(spa_feature_is_active(dmu_objset_spa(db
->db_objset
),
2102 SPA_FEATURE_EMBEDDED_DATA
));
2106 type
= DB_DNODE(db
)->dn_type
;
2109 ASSERT0(db
->db_level
);
2110 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2112 dmu_buf_will_not_fill(dbuf
, tx
);
2114 ASSERT3U(db
->db_last_dirty
->dr_txg
, ==, tx
->tx_txg
);
2115 dl
= &db
->db_last_dirty
->dt
.dl
;
2116 encode_embedded_bp_compressed(&dl
->dr_overridden_by
,
2117 data
, comp
, uncompressed_size
, compressed_size
);
2118 BPE_SET_ETYPE(&dl
->dr_overridden_by
, etype
);
2119 BP_SET_TYPE(&dl
->dr_overridden_by
, type
);
2120 BP_SET_LEVEL(&dl
->dr_overridden_by
, 0);
2121 BP_SET_BYTEORDER(&dl
->dr_overridden_by
, byteorder
);
2123 dl
->dr_override_state
= DR_OVERRIDDEN
;
2124 dl
->dr_overridden_by
.blk_birth
= db
->db_last_dirty
->dr_txg
;
2128 * Directly assign a provided arc buf to a given dbuf if it's not referenced
2129 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
2132 dbuf_assign_arcbuf(dmu_buf_impl_t
*db
, arc_buf_t
*buf
, dmu_tx_t
*tx
)
2134 ASSERT(!refcount_is_zero(&db
->db_holds
));
2135 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2136 ASSERT(db
->db_level
== 0);
2137 ASSERT3U(dbuf_is_metadata(db
), ==, arc_is_metadata(buf
));
2138 ASSERT(buf
!= NULL
);
2139 ASSERT(arc_buf_lsize(buf
) == db
->db
.db_size
);
2140 ASSERT(tx
->tx_txg
!= 0);
2142 arc_return_buf(buf
, db
);
2143 ASSERT(arc_released(buf
));
2145 mutex_enter(&db
->db_mtx
);
2147 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
2148 cv_wait(&db
->db_changed
, &db
->db_mtx
);
2150 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_UNCACHED
);
2152 if (db
->db_state
== DB_CACHED
&&
2153 refcount_count(&db
->db_holds
) - 1 > db
->db_dirtycnt
) {
2155 * In practice, we will never have a case where we have an
2156 * encrypted arc buffer while additional holds exist on the
2157 * dbuf. We don't handle this here so we simply assert that
2160 ASSERT(!arc_is_encrypted(buf
));
2161 mutex_exit(&db
->db_mtx
);
2162 (void) dbuf_dirty(db
, tx
);
2163 bcopy(buf
->b_data
, db
->db
.db_data
, db
->db
.db_size
);
2164 arc_buf_destroy(buf
, db
);
2165 xuio_stat_wbuf_copied();
2169 xuio_stat_wbuf_nocopy();
2170 if (db
->db_state
== DB_CACHED
) {
2171 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
2173 ASSERT(db
->db_buf
!= NULL
);
2174 if (dr
!= NULL
&& dr
->dr_txg
== tx
->tx_txg
) {
2175 ASSERT(dr
->dt
.dl
.dr_data
== db
->db_buf
);
2176 IMPLY(arc_is_encrypted(buf
), dr
->dt
.dl
.dr_raw
);
2178 if (!arc_released(db
->db_buf
)) {
2179 ASSERT(dr
->dt
.dl
.dr_override_state
==
2181 arc_release(db
->db_buf
, db
);
2183 dr
->dt
.dl
.dr_data
= buf
;
2184 arc_buf_destroy(db
->db_buf
, db
);
2185 } else if (dr
== NULL
|| dr
->dt
.dl
.dr_data
!= db
->db_buf
) {
2186 arc_release(db
->db_buf
, db
);
2187 arc_buf_destroy(db
->db_buf
, db
);
2191 ASSERT(db
->db_buf
== NULL
);
2192 dbuf_set_data(db
, buf
);
2193 db
->db_state
= DB_FILL
;
2194 mutex_exit(&db
->db_mtx
);
2195 (void) dbuf_dirty(db
, tx
);
2196 dmu_buf_fill_done(&db
->db
, tx
);
2200 dbuf_destroy(dmu_buf_impl_t
*db
)
2203 dmu_buf_impl_t
*parent
= db
->db_parent
;
2204 dmu_buf_impl_t
*dndb
;
2206 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2207 ASSERT(refcount_is_zero(&db
->db_holds
));
2209 if (db
->db_buf
!= NULL
) {
2210 arc_buf_destroy(db
->db_buf
, db
);
2214 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2215 int slots
= DB_DNODE(db
)->dn_num_slots
;
2216 int bonuslen
= DN_SLOTS_TO_BONUSLEN(slots
);
2217 if (db
->db
.db_data
!= NULL
) {
2218 kmem_free(db
->db
.db_data
, bonuslen
);
2219 arc_space_return(bonuslen
, ARC_SPACE_BONUS
);
2220 db
->db_state
= DB_UNCACHED
;
2224 dbuf_clear_data(db
);
2226 if (multilist_link_active(&db
->db_cache_link
)) {
2227 multilist_remove(dbuf_cache
, db
);
2228 (void) refcount_remove_many(&dbuf_cache_size
,
2229 db
->db
.db_size
, db
);
2232 ASSERT(db
->db_state
== DB_UNCACHED
|| db
->db_state
== DB_NOFILL
);
2233 ASSERT(db
->db_data_pending
== NULL
);
2235 db
->db_state
= DB_EVICTING
;
2236 db
->db_blkptr
= NULL
;
2239 * Now that db_state is DB_EVICTING, nobody else can find this via
2240 * the hash table. We can now drop db_mtx, which allows us to
2241 * acquire the dn_dbufs_mtx.
2243 mutex_exit(&db
->db_mtx
);
2248 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
2249 boolean_t needlock
= !MUTEX_HELD(&dn
->dn_dbufs_mtx
);
2251 mutex_enter(&dn
->dn_dbufs_mtx
);
2252 avl_remove(&dn
->dn_dbufs
, db
);
2253 atomic_dec_32(&dn
->dn_dbufs_count
);
2257 mutex_exit(&dn
->dn_dbufs_mtx
);
2259 * Decrementing the dbuf count means that the hold corresponding
2260 * to the removed dbuf is no longer discounted in dnode_move(),
2261 * so the dnode cannot be moved until after we release the hold.
2262 * The membar_producer() ensures visibility of the decremented
2263 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
2267 db
->db_dnode_handle
= NULL
;
2269 dbuf_hash_remove(db
);
2274 ASSERT(refcount_is_zero(&db
->db_holds
));
2276 db
->db_parent
= NULL
;
2278 ASSERT(db
->db_buf
== NULL
);
2279 ASSERT(db
->db
.db_data
== NULL
);
2280 ASSERT(db
->db_hash_next
== NULL
);
2281 ASSERT(db
->db_blkptr
== NULL
);
2282 ASSERT(db
->db_data_pending
== NULL
);
2283 ASSERT(!multilist_link_active(&db
->db_cache_link
));
2285 kmem_cache_free(dbuf_kmem_cache
, db
);
2286 arc_space_return(sizeof (dmu_buf_impl_t
), ARC_SPACE_DBUF
);
2289 * If this dbuf is referenced from an indirect dbuf,
2290 * decrement the ref count on the indirect dbuf.
2292 if (parent
&& parent
!= dndb
)
2293 dbuf_rele(parent
, db
);
2297 * Note: While bpp will always be updated if the function returns success,
2298 * parentp will not be updated if the dnode does not have dn_dbuf filled in;
2299 * this happens when the dnode is the meta-dnode, or a userused or groupused
2302 __attribute__((always_inline
))
2304 dbuf_findbp(dnode_t
*dn
, int level
, uint64_t blkid
, int fail_sparse
,
2305 dmu_buf_impl_t
**parentp
, blkptr_t
**bpp
, struct dbuf_hold_impl_data
*dh
)
2310 ASSERT(blkid
!= DMU_BONUS_BLKID
);
2312 if (blkid
== DMU_SPILL_BLKID
) {
2313 mutex_enter(&dn
->dn_mtx
);
2314 if (dn
->dn_have_spill
&&
2315 (dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
))
2316 *bpp
= DN_SPILL_BLKPTR(dn
->dn_phys
);
2319 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
2320 *parentp
= dn
->dn_dbuf
;
2321 mutex_exit(&dn
->dn_mtx
);
2326 (dn
->dn_phys
->dn_nlevels
== 0) ? 1 : dn
->dn_phys
->dn_nlevels
;
2327 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2329 ASSERT3U(level
* epbs
, <, 64);
2330 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2332 * This assertion shouldn't trip as long as the max indirect block size
2333 * is less than 1M. The reason for this is that up to that point,
2334 * the number of levels required to address an entire object with blocks
2335 * of size SPA_MINBLOCKSIZE satisfies nlevels * epbs + 1 <= 64. In
2336 * other words, if N * epbs + 1 > 64, then if (N-1) * epbs + 1 > 55
2337 * (i.e. we can address the entire object), objects will all use at most
2338 * N-1 levels and the assertion won't overflow. However, once epbs is
2339 * 13, 4 * 13 + 1 = 53, but 5 * 13 + 1 = 66. Then, 4 levels will not be
2340 * enough to address an entire object, so objects will have 5 levels,
2341 * but then this assertion will overflow.
2343 * All this is to say that if we ever increase DN_MAX_INDBLKSHIFT, we
2344 * need to redo this logic to handle overflows.
2346 ASSERT(level
>= nlevels
||
2347 ((nlevels
- level
- 1) * epbs
) +
2348 highbit64(dn
->dn_phys
->dn_nblkptr
) <= 64);
2349 if (level
>= nlevels
||
2350 blkid
>= ((uint64_t)dn
->dn_phys
->dn_nblkptr
<<
2351 ((nlevels
- level
- 1) * epbs
)) ||
2353 blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))) {
2354 /* the buffer has no parent yet */
2355 return (SET_ERROR(ENOENT
));
2356 } else if (level
< nlevels
-1) {
2357 /* this block is referenced from an indirect block */
2360 err
= dbuf_hold_impl(dn
, level
+1,
2361 blkid
>> epbs
, fail_sparse
, FALSE
, NULL
, parentp
);
2363 __dbuf_hold_impl_init(dh
+ 1, dn
, dh
->dh_level
+ 1,
2364 blkid
>> epbs
, fail_sparse
, FALSE
, NULL
,
2365 parentp
, dh
->dh_depth
+ 1);
2366 err
= __dbuf_hold_impl(dh
+ 1);
2370 err
= dbuf_read(*parentp
, NULL
,
2371 (DB_RF_HAVESTRUCT
| DB_RF_NOPREFETCH
| DB_RF_CANFAIL
));
2373 dbuf_rele(*parentp
, NULL
);
2377 *bpp
= ((blkptr_t
*)(*parentp
)->db
.db_data
) +
2378 (blkid
& ((1ULL << epbs
) - 1));
2379 if (blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))
2380 ASSERT(BP_IS_HOLE(*bpp
));
2383 /* the block is referenced from the dnode */
2384 ASSERT3U(level
, ==, nlevels
-1);
2385 ASSERT(dn
->dn_phys
->dn_nblkptr
== 0 ||
2386 blkid
< dn
->dn_phys
->dn_nblkptr
);
2388 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
2389 *parentp
= dn
->dn_dbuf
;
2391 *bpp
= &dn
->dn_phys
->dn_blkptr
[blkid
];
2396 static dmu_buf_impl_t
*
2397 dbuf_create(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
2398 dmu_buf_impl_t
*parent
, blkptr_t
*blkptr
)
2400 objset_t
*os
= dn
->dn_objset
;
2401 dmu_buf_impl_t
*db
, *odb
;
2403 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2404 ASSERT(dn
->dn_type
!= DMU_OT_NONE
);
2406 db
= kmem_cache_alloc(dbuf_kmem_cache
, KM_SLEEP
);
2409 db
->db
.db_object
= dn
->dn_object
;
2410 db
->db_level
= level
;
2411 db
->db_blkid
= blkid
;
2412 db
->db_last_dirty
= NULL
;
2413 db
->db_dirtycnt
= 0;
2414 db
->db_dnode_handle
= dn
->dn_handle
;
2415 db
->db_parent
= parent
;
2416 db
->db_blkptr
= blkptr
;
2419 db
->db_user_immediate_evict
= FALSE
;
2420 db
->db_freed_in_flight
= FALSE
;
2421 db
->db_pending_evict
= FALSE
;
2423 if (blkid
== DMU_BONUS_BLKID
) {
2424 ASSERT3P(parent
, ==, dn
->dn_dbuf
);
2425 db
->db
.db_size
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
) -
2426 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
);
2427 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
2428 db
->db
.db_offset
= DMU_BONUS_BLKID
;
2429 db
->db_state
= DB_UNCACHED
;
2430 /* the bonus dbuf is not placed in the hash table */
2431 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_DBUF
);
2433 } else if (blkid
== DMU_SPILL_BLKID
) {
2434 db
->db
.db_size
= (blkptr
!= NULL
) ?
2435 BP_GET_LSIZE(blkptr
) : SPA_MINBLOCKSIZE
;
2436 db
->db
.db_offset
= 0;
2439 db
->db_level
? 1 << dn
->dn_indblkshift
: dn
->dn_datablksz
;
2440 db
->db
.db_size
= blocksize
;
2441 db
->db
.db_offset
= db
->db_blkid
* blocksize
;
2445 * Hold the dn_dbufs_mtx while we get the new dbuf
2446 * in the hash table *and* added to the dbufs list.
2447 * This prevents a possible deadlock with someone
2448 * trying to look up this dbuf before its added to the
2451 mutex_enter(&dn
->dn_dbufs_mtx
);
2452 db
->db_state
= DB_EVICTING
;
2453 if ((odb
= dbuf_hash_insert(db
)) != NULL
) {
2454 /* someone else inserted it first */
2455 kmem_cache_free(dbuf_kmem_cache
, db
);
2456 mutex_exit(&dn
->dn_dbufs_mtx
);
2459 avl_add(&dn
->dn_dbufs
, db
);
2461 db
->db_state
= DB_UNCACHED
;
2462 mutex_exit(&dn
->dn_dbufs_mtx
);
2463 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_DBUF
);
2465 if (parent
&& parent
!= dn
->dn_dbuf
)
2466 dbuf_add_ref(parent
, db
);
2468 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
2469 refcount_count(&dn
->dn_holds
) > 0);
2470 (void) refcount_add(&dn
->dn_holds
, db
);
2471 atomic_inc_32(&dn
->dn_dbufs_count
);
2473 dprintf_dbuf(db
, "db=%p\n", db
);
2478 typedef struct dbuf_prefetch_arg
{
2479 spa_t
*dpa_spa
; /* The spa to issue the prefetch in. */
2480 zbookmark_phys_t dpa_zb
; /* The target block to prefetch. */
2481 int dpa_epbs
; /* Entries (blkptr_t's) Per Block Shift. */
2482 int dpa_curlevel
; /* The current level that we're reading */
2483 dnode_t
*dpa_dnode
; /* The dnode associated with the prefetch */
2484 zio_priority_t dpa_prio
; /* The priority I/Os should be issued at. */
2485 zio_t
*dpa_zio
; /* The parent zio_t for all prefetches. */
2486 arc_flags_t dpa_aflags
; /* Flags to pass to the final prefetch. */
2487 } dbuf_prefetch_arg_t
;
2490 * Actually issue the prefetch read for the block given.
2493 dbuf_issue_final_prefetch(dbuf_prefetch_arg_t
*dpa
, blkptr_t
*bp
)
2495 if (BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
))
2498 arc_flags_t aflags
=
2499 dpa
->dpa_aflags
| ARC_FLAG_NOWAIT
| ARC_FLAG_PREFETCH
;
2501 ASSERT3U(dpa
->dpa_curlevel
, ==, BP_GET_LEVEL(bp
));
2502 ASSERT3U(dpa
->dpa_curlevel
, ==, dpa
->dpa_zb
.zb_level
);
2503 ASSERT(dpa
->dpa_zio
!= NULL
);
2504 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
, bp
, NULL
, NULL
,
2505 dpa
->dpa_prio
, ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
2506 &aflags
, &dpa
->dpa_zb
);
2510 * Called when an indirect block above our prefetch target is read in. This
2511 * will either read in the next indirect block down the tree or issue the actual
2512 * prefetch if the next block down is our target.
2515 dbuf_prefetch_indirect_done(zio_t
*zio
, int err
, arc_buf_t
*abuf
, void *private)
2517 dbuf_prefetch_arg_t
*dpa
= private;
2519 ASSERT3S(dpa
->dpa_zb
.zb_level
, <, dpa
->dpa_curlevel
);
2520 ASSERT3S(dpa
->dpa_curlevel
, >, 0);
2523 * The dpa_dnode is only valid if we are called with a NULL
2524 * zio. This indicates that the arc_read() returned without
2525 * first calling zio_read() to issue a physical read. Once
2526 * a physical read is made the dpa_dnode must be invalidated
2527 * as the locks guarding it may have been dropped. If the
2528 * dpa_dnode is still valid, then we want to add it to the dbuf
2529 * cache. To do so, we must hold the dbuf associated with the block
2530 * we just prefetched, read its contents so that we associate it
2531 * with an arc_buf_t, and then release it.
2534 ASSERT3S(BP_GET_LEVEL(zio
->io_bp
), ==, dpa
->dpa_curlevel
);
2535 if (zio
->io_flags
& ZIO_FLAG_RAW_COMPRESS
) {
2536 ASSERT3U(BP_GET_PSIZE(zio
->io_bp
), ==, zio
->io_size
);
2538 ASSERT3U(BP_GET_LSIZE(zio
->io_bp
), ==, zio
->io_size
);
2540 ASSERT3P(zio
->io_spa
, ==, dpa
->dpa_spa
);
2542 dpa
->dpa_dnode
= NULL
;
2543 } else if (dpa
->dpa_dnode
!= NULL
) {
2544 uint64_t curblkid
= dpa
->dpa_zb
.zb_blkid
>>
2545 (dpa
->dpa_epbs
* (dpa
->dpa_curlevel
-
2546 dpa
->dpa_zb
.zb_level
));
2547 dmu_buf_impl_t
*db
= dbuf_hold_level(dpa
->dpa_dnode
,
2548 dpa
->dpa_curlevel
, curblkid
, FTAG
);
2549 (void) dbuf_read(db
, NULL
,
2550 DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
| DB_RF_HAVESTRUCT
);
2551 dbuf_rele(db
, FTAG
);
2554 dpa
->dpa_curlevel
--;
2556 uint64_t nextblkid
= dpa
->dpa_zb
.zb_blkid
>>
2557 (dpa
->dpa_epbs
* (dpa
->dpa_curlevel
- dpa
->dpa_zb
.zb_level
));
2558 blkptr_t
*bp
= ((blkptr_t
*)abuf
->b_data
) +
2559 P2PHASE(nextblkid
, 1ULL << dpa
->dpa_epbs
);
2560 if (BP_IS_HOLE(bp
) || err
!= 0) {
2561 kmem_free(dpa
, sizeof (*dpa
));
2562 } else if (dpa
->dpa_curlevel
== dpa
->dpa_zb
.zb_level
) {
2563 ASSERT3U(nextblkid
, ==, dpa
->dpa_zb
.zb_blkid
);
2564 dbuf_issue_final_prefetch(dpa
, bp
);
2565 kmem_free(dpa
, sizeof (*dpa
));
2567 arc_flags_t iter_aflags
= ARC_FLAG_NOWAIT
;
2568 zbookmark_phys_t zb
;
2570 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
2571 if (dpa
->dpa_aflags
& ARC_FLAG_L2CACHE
)
2572 iter_aflags
|= ARC_FLAG_L2CACHE
;
2574 ASSERT3U(dpa
->dpa_curlevel
, ==, BP_GET_LEVEL(bp
));
2576 SET_BOOKMARK(&zb
, dpa
->dpa_zb
.zb_objset
,
2577 dpa
->dpa_zb
.zb_object
, dpa
->dpa_curlevel
, nextblkid
);
2579 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
,
2580 bp
, dbuf_prefetch_indirect_done
, dpa
, dpa
->dpa_prio
,
2581 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
2585 arc_buf_destroy(abuf
, private);
2589 * Issue prefetch reads for the given block on the given level. If the indirect
2590 * blocks above that block are not in memory, we will read them in
2591 * asynchronously. As a result, this call never blocks waiting for a read to
2592 * complete. Note that the prefetch might fail if the dataset is encrypted and
2593 * the encryption key is unmapped before the IO completes.
2596 dbuf_prefetch(dnode_t
*dn
, int64_t level
, uint64_t blkid
, zio_priority_t prio
,
2600 int epbs
, nlevels
, curlevel
;
2603 ASSERT(blkid
!= DMU_BONUS_BLKID
);
2604 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2606 if (blkid
> dn
->dn_maxblkid
)
2609 if (dnode_block_freed(dn
, blkid
))
2613 * This dnode hasn't been written to disk yet, so there's nothing to
2616 nlevels
= dn
->dn_phys
->dn_nlevels
;
2617 if (level
>= nlevels
|| dn
->dn_phys
->dn_nblkptr
== 0)
2620 epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2621 if (dn
->dn_phys
->dn_maxblkid
< blkid
<< (epbs
* level
))
2624 dmu_buf_impl_t
*db
= dbuf_find(dn
->dn_objset
, dn
->dn_object
,
2627 mutex_exit(&db
->db_mtx
);
2629 * This dbuf already exists. It is either CACHED, or
2630 * (we assume) about to be read or filled.
2636 * Find the closest ancestor (indirect block) of the target block
2637 * that is present in the cache. In this indirect block, we will
2638 * find the bp that is at curlevel, curblkid.
2642 while (curlevel
< nlevels
- 1) {
2643 int parent_level
= curlevel
+ 1;
2644 uint64_t parent_blkid
= curblkid
>> epbs
;
2647 if (dbuf_hold_impl(dn
, parent_level
, parent_blkid
,
2648 FALSE
, TRUE
, FTAG
, &db
) == 0) {
2649 blkptr_t
*bpp
= db
->db_buf
->b_data
;
2650 bp
= bpp
[P2PHASE(curblkid
, 1 << epbs
)];
2651 dbuf_rele(db
, FTAG
);
2655 curlevel
= parent_level
;
2656 curblkid
= parent_blkid
;
2659 if (curlevel
== nlevels
- 1) {
2660 /* No cached indirect blocks found. */
2661 ASSERT3U(curblkid
, <, dn
->dn_phys
->dn_nblkptr
);
2662 bp
= dn
->dn_phys
->dn_blkptr
[curblkid
];
2664 if (BP_IS_HOLE(&bp
))
2667 ASSERT3U(curlevel
, ==, BP_GET_LEVEL(&bp
));
2669 zio_t
*pio
= zio_root(dmu_objset_spa(dn
->dn_objset
), NULL
, NULL
,
2672 dbuf_prefetch_arg_t
*dpa
= kmem_zalloc(sizeof (*dpa
), KM_SLEEP
);
2673 dsl_dataset_t
*ds
= dn
->dn_objset
->os_dsl_dataset
;
2674 SET_BOOKMARK(&dpa
->dpa_zb
, ds
!= NULL
? ds
->ds_object
: DMU_META_OBJSET
,
2675 dn
->dn_object
, level
, blkid
);
2676 dpa
->dpa_curlevel
= curlevel
;
2677 dpa
->dpa_prio
= prio
;
2678 dpa
->dpa_aflags
= aflags
;
2679 dpa
->dpa_spa
= dn
->dn_objset
->os_spa
;
2680 dpa
->dpa_dnode
= dn
;
2681 dpa
->dpa_epbs
= epbs
;
2684 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
2685 if (DNODE_LEVEL_IS_L2CACHEABLE(dn
, level
))
2686 dpa
->dpa_aflags
|= ARC_FLAG_L2CACHE
;
2689 * If we have the indirect just above us, no need to do the asynchronous
2690 * prefetch chain; we'll just run the last step ourselves. If we're at
2691 * a higher level, though, we want to issue the prefetches for all the
2692 * indirect blocks asynchronously, so we can go on with whatever we were
2695 if (curlevel
== level
) {
2696 ASSERT3U(curblkid
, ==, blkid
);
2697 dbuf_issue_final_prefetch(dpa
, &bp
);
2698 kmem_free(dpa
, sizeof (*dpa
));
2700 arc_flags_t iter_aflags
= ARC_FLAG_NOWAIT
;
2701 zbookmark_phys_t zb
;
2703 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
2704 if (DNODE_LEVEL_IS_L2CACHEABLE(dn
, level
))
2705 iter_aflags
|= ARC_FLAG_L2CACHE
;
2707 SET_BOOKMARK(&zb
, ds
!= NULL
? ds
->ds_object
: DMU_META_OBJSET
,
2708 dn
->dn_object
, curlevel
, curblkid
);
2709 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
,
2710 &bp
, dbuf_prefetch_indirect_done
, dpa
, prio
,
2711 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
2715 * We use pio here instead of dpa_zio since it's possible that
2716 * dpa may have already been freed.
2721 #define DBUF_HOLD_IMPL_MAX_DEPTH 20
2724 * Helper function for __dbuf_hold_impl() to copy a buffer. Handles
2725 * the case of encrypted, compressed and uncompressed buffers by
2726 * allocating the new buffer, respectively, with arc_alloc_raw_buf(),
2727 * arc_alloc_compressed_buf() or arc_alloc_buf().*
2729 * NOTE: Declared noinline to avoid stack bloat in __dbuf_hold_impl().
2731 noinline
static void
2732 dbuf_hold_copy(struct dbuf_hold_impl_data
*dh
)
2734 dnode_t
*dn
= dh
->dh_dn
;
2735 dmu_buf_impl_t
*db
= dh
->dh_db
;
2736 dbuf_dirty_record_t
*dr
= dh
->dh_dr
;
2737 arc_buf_t
*data
= dr
->dt
.dl
.dr_data
;
2739 enum zio_compress compress_type
= arc_get_compression(data
);
2741 if (arc_is_encrypted(data
)) {
2742 boolean_t byteorder
;
2743 uint8_t salt
[ZIO_DATA_SALT_LEN
];
2744 uint8_t iv
[ZIO_DATA_IV_LEN
];
2745 uint8_t mac
[ZIO_DATA_MAC_LEN
];
2747 arc_get_raw_params(data
, &byteorder
, salt
, iv
, mac
);
2748 dbuf_set_data(db
, arc_alloc_raw_buf(dn
->dn_objset
->os_spa
, db
,
2749 dmu_objset_id(dn
->dn_objset
), byteorder
, salt
, iv
, mac
,
2750 dn
->dn_type
, arc_buf_size(data
), arc_buf_lsize(data
),
2752 } else if (compress_type
!= ZIO_COMPRESS_OFF
) {
2753 dbuf_set_data(db
, arc_alloc_compressed_buf(
2754 dn
->dn_objset
->os_spa
, db
, arc_buf_size(data
),
2755 arc_buf_lsize(data
), compress_type
));
2757 dbuf_set_data(db
, arc_alloc_buf(dn
->dn_objset
->os_spa
, db
,
2758 DBUF_GET_BUFC_TYPE(db
), db
->db
.db_size
));
2761 bcopy(data
->b_data
, db
->db
.db_data
, arc_buf_size(data
));
2765 * Returns with db_holds incremented, and db_mtx not held.
2766 * Note: dn_struct_rwlock must be held.
2769 __dbuf_hold_impl(struct dbuf_hold_impl_data
*dh
)
2771 ASSERT3S(dh
->dh_depth
, <, DBUF_HOLD_IMPL_MAX_DEPTH
);
2772 dh
->dh_parent
= NULL
;
2774 ASSERT(dh
->dh_blkid
!= DMU_BONUS_BLKID
);
2775 ASSERT(RW_LOCK_HELD(&dh
->dh_dn
->dn_struct_rwlock
));
2776 ASSERT3U(dh
->dh_dn
->dn_nlevels
, >, dh
->dh_level
);
2778 *(dh
->dh_dbp
) = NULL
;
2780 /* dbuf_find() returns with db_mtx held */
2781 dh
->dh_db
= dbuf_find(dh
->dh_dn
->dn_objset
, dh
->dh_dn
->dn_object
,
2782 dh
->dh_level
, dh
->dh_blkid
);
2784 if (dh
->dh_db
== NULL
) {
2787 if (dh
->dh_fail_uncached
)
2788 return (SET_ERROR(ENOENT
));
2790 ASSERT3P(dh
->dh_parent
, ==, NULL
);
2791 dh
->dh_err
= dbuf_findbp(dh
->dh_dn
, dh
->dh_level
, dh
->dh_blkid
,
2792 dh
->dh_fail_sparse
, &dh
->dh_parent
, &dh
->dh_bp
, dh
);
2793 if (dh
->dh_fail_sparse
) {
2794 if (dh
->dh_err
== 0 &&
2795 dh
->dh_bp
&& BP_IS_HOLE(dh
->dh_bp
))
2796 dh
->dh_err
= SET_ERROR(ENOENT
);
2799 dbuf_rele(dh
->dh_parent
, NULL
);
2800 return (dh
->dh_err
);
2803 if (dh
->dh_err
&& dh
->dh_err
!= ENOENT
)
2804 return (dh
->dh_err
);
2805 dh
->dh_db
= dbuf_create(dh
->dh_dn
, dh
->dh_level
, dh
->dh_blkid
,
2806 dh
->dh_parent
, dh
->dh_bp
);
2809 if (dh
->dh_fail_uncached
&& dh
->dh_db
->db_state
!= DB_CACHED
) {
2810 mutex_exit(&dh
->dh_db
->db_mtx
);
2811 return (SET_ERROR(ENOENT
));
2814 if (dh
->dh_db
->db_buf
!= NULL
)
2815 ASSERT3P(dh
->dh_db
->db
.db_data
, ==, dh
->dh_db
->db_buf
->b_data
);
2817 ASSERT(dh
->dh_db
->db_buf
== NULL
|| arc_referenced(dh
->dh_db
->db_buf
));
2820 * If this buffer is currently syncing out, and we are are
2821 * still referencing it from db_data, we need to make a copy
2822 * of it in case we decide we want to dirty it again in this txg.
2824 if (dh
->dh_db
->db_level
== 0 &&
2825 dh
->dh_db
->db_blkid
!= DMU_BONUS_BLKID
&&
2826 dh
->dh_dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
2827 dh
->dh_db
->db_state
== DB_CACHED
&& dh
->dh_db
->db_data_pending
) {
2828 dh
->dh_dr
= dh
->dh_db
->db_data_pending
;
2829 if (dh
->dh_dr
->dt
.dl
.dr_data
== dh
->dh_db
->db_buf
)
2833 if (multilist_link_active(&dh
->dh_db
->db_cache_link
)) {
2834 ASSERT(refcount_is_zero(&dh
->dh_db
->db_holds
));
2835 multilist_remove(dbuf_cache
, dh
->dh_db
);
2836 (void) refcount_remove_many(&dbuf_cache_size
,
2837 dh
->dh_db
->db
.db_size
, dh
->dh_db
);
2839 (void) refcount_add(&dh
->dh_db
->db_holds
, dh
->dh_tag
);
2840 DBUF_VERIFY(dh
->dh_db
);
2841 mutex_exit(&dh
->dh_db
->db_mtx
);
2843 /* NOTE: we can't rele the parent until after we drop the db_mtx */
2845 dbuf_rele(dh
->dh_parent
, NULL
);
2847 ASSERT3P(DB_DNODE(dh
->dh_db
), ==, dh
->dh_dn
);
2848 ASSERT3U(dh
->dh_db
->db_blkid
, ==, dh
->dh_blkid
);
2849 ASSERT3U(dh
->dh_db
->db_level
, ==, dh
->dh_level
);
2850 *(dh
->dh_dbp
) = dh
->dh_db
;
2856 * The following code preserves the recursive function dbuf_hold_impl()
2857 * but moves the local variables AND function arguments to the heap to
2858 * minimize the stack frame size. Enough space is initially allocated
2859 * on the stack for 20 levels of recursion.
2862 dbuf_hold_impl(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
2863 boolean_t fail_sparse
, boolean_t fail_uncached
,
2864 void *tag
, dmu_buf_impl_t
**dbp
)
2866 struct dbuf_hold_impl_data
*dh
;
2869 dh
= kmem_alloc(sizeof (struct dbuf_hold_impl_data
) *
2870 DBUF_HOLD_IMPL_MAX_DEPTH
, KM_SLEEP
);
2871 __dbuf_hold_impl_init(dh
, dn
, level
, blkid
, fail_sparse
,
2872 fail_uncached
, tag
, dbp
, 0);
2874 error
= __dbuf_hold_impl(dh
);
2876 kmem_free(dh
, sizeof (struct dbuf_hold_impl_data
) *
2877 DBUF_HOLD_IMPL_MAX_DEPTH
);
2883 __dbuf_hold_impl_init(struct dbuf_hold_impl_data
*dh
,
2884 dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
2885 boolean_t fail_sparse
, boolean_t fail_uncached
,
2886 void *tag
, dmu_buf_impl_t
**dbp
, int depth
)
2889 dh
->dh_level
= level
;
2890 dh
->dh_blkid
= blkid
;
2892 dh
->dh_fail_sparse
= fail_sparse
;
2893 dh
->dh_fail_uncached
= fail_uncached
;
2899 dh
->dh_parent
= NULL
;
2904 dh
->dh_depth
= depth
;
2908 dbuf_hold(dnode_t
*dn
, uint64_t blkid
, void *tag
)
2910 return (dbuf_hold_level(dn
, 0, blkid
, tag
));
2914 dbuf_hold_level(dnode_t
*dn
, int level
, uint64_t blkid
, void *tag
)
2917 int err
= dbuf_hold_impl(dn
, level
, blkid
, FALSE
, FALSE
, tag
, &db
);
2918 return (err
? NULL
: db
);
2922 dbuf_create_bonus(dnode_t
*dn
)
2924 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
2926 ASSERT(dn
->dn_bonus
== NULL
);
2927 dn
->dn_bonus
= dbuf_create(dn
, 0, DMU_BONUS_BLKID
, dn
->dn_dbuf
, NULL
);
2931 dbuf_spill_set_blksz(dmu_buf_t
*db_fake
, uint64_t blksz
, dmu_tx_t
*tx
)
2933 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2936 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
2937 return (SET_ERROR(ENOTSUP
));
2939 blksz
= SPA_MINBLOCKSIZE
;
2940 ASSERT3U(blksz
, <=, spa_maxblocksize(dmu_objset_spa(db
->db_objset
)));
2941 blksz
= P2ROUNDUP(blksz
, SPA_MINBLOCKSIZE
);
2945 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
2946 dbuf_new_size(db
, blksz
, tx
);
2947 rw_exit(&dn
->dn_struct_rwlock
);
2954 dbuf_rm_spill(dnode_t
*dn
, dmu_tx_t
*tx
)
2956 dbuf_free_range(dn
, DMU_SPILL_BLKID
, DMU_SPILL_BLKID
, tx
);
2959 #pragma weak dmu_buf_add_ref = dbuf_add_ref
2961 dbuf_add_ref(dmu_buf_impl_t
*db
, void *tag
)
2963 int64_t holds
= refcount_add(&db
->db_holds
, tag
);
2964 VERIFY3S(holds
, >, 1);
2967 #pragma weak dmu_buf_try_add_ref = dbuf_try_add_ref
2969 dbuf_try_add_ref(dmu_buf_t
*db_fake
, objset_t
*os
, uint64_t obj
, uint64_t blkid
,
2972 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2973 dmu_buf_impl_t
*found_db
;
2974 boolean_t result
= B_FALSE
;
2976 if (blkid
== DMU_BONUS_BLKID
)
2977 found_db
= dbuf_find_bonus(os
, obj
);
2979 found_db
= dbuf_find(os
, obj
, 0, blkid
);
2981 if (found_db
!= NULL
) {
2982 if (db
== found_db
&& dbuf_refcount(db
) > db
->db_dirtycnt
) {
2983 (void) refcount_add(&db
->db_holds
, tag
);
2986 mutex_exit(&found_db
->db_mtx
);
2992 * If you call dbuf_rele() you had better not be referencing the dnode handle
2993 * unless you have some other direct or indirect hold on the dnode. (An indirect
2994 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
2995 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
2996 * dnode's parent dbuf evicting its dnode handles.
2999 dbuf_rele(dmu_buf_impl_t
*db
, void *tag
)
3001 mutex_enter(&db
->db_mtx
);
3002 dbuf_rele_and_unlock(db
, tag
);
3006 dmu_buf_rele(dmu_buf_t
*db
, void *tag
)
3008 dbuf_rele((dmu_buf_impl_t
*)db
, tag
);
3012 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
3013 * db_dirtycnt and db_holds to be updated atomically.
3016 dbuf_rele_and_unlock(dmu_buf_impl_t
*db
, void *tag
)
3020 ASSERT(MUTEX_HELD(&db
->db_mtx
));
3024 * Remove the reference to the dbuf before removing its hold on the
3025 * dnode so we can guarantee in dnode_move() that a referenced bonus
3026 * buffer has a corresponding dnode hold.
3028 holds
= refcount_remove(&db
->db_holds
, tag
);
3032 * We can't freeze indirects if there is a possibility that they
3033 * may be modified in the current syncing context.
3035 if (db
->db_buf
!= NULL
&&
3036 holds
== (db
->db_level
== 0 ? db
->db_dirtycnt
: 0)) {
3037 arc_buf_freeze(db
->db_buf
);
3040 if (holds
== db
->db_dirtycnt
&&
3041 db
->db_level
== 0 && db
->db_user_immediate_evict
)
3042 dbuf_evict_user(db
);
3045 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
3047 boolean_t evict_dbuf
= db
->db_pending_evict
;
3050 * If the dnode moves here, we cannot cross this
3051 * barrier until the move completes.
3056 atomic_dec_32(&dn
->dn_dbufs_count
);
3059 * Decrementing the dbuf count means that the bonus
3060 * buffer's dnode hold is no longer discounted in
3061 * dnode_move(). The dnode cannot move until after
3062 * the dnode_rele() below.
3067 * Do not reference db after its lock is dropped.
3068 * Another thread may evict it.
3070 mutex_exit(&db
->db_mtx
);
3073 dnode_evict_bonus(dn
);
3076 } else if (db
->db_buf
== NULL
) {
3078 * This is a special case: we never associated this
3079 * dbuf with any data allocated from the ARC.
3081 ASSERT(db
->db_state
== DB_UNCACHED
||
3082 db
->db_state
== DB_NOFILL
);
3084 } else if (arc_released(db
->db_buf
)) {
3086 * This dbuf has anonymous data associated with it.
3090 boolean_t do_arc_evict
= B_FALSE
;
3092 spa_t
*spa
= dmu_objset_spa(db
->db_objset
);
3094 if (!DBUF_IS_CACHEABLE(db
) &&
3095 db
->db_blkptr
!= NULL
&&
3096 !BP_IS_HOLE(db
->db_blkptr
) &&
3097 !BP_IS_EMBEDDED(db
->db_blkptr
)) {
3098 do_arc_evict
= B_TRUE
;
3099 bp
= *db
->db_blkptr
;
3102 if (!DBUF_IS_CACHEABLE(db
) ||
3103 db
->db_pending_evict
) {
3105 } else if (!multilist_link_active(&db
->db_cache_link
)) {
3106 multilist_insert(dbuf_cache
, db
);
3107 (void) refcount_add_many(&dbuf_cache_size
,
3108 db
->db
.db_size
, db
);
3109 mutex_exit(&db
->db_mtx
);
3111 dbuf_evict_notify();
3115 arc_freed(spa
, &bp
);
3118 mutex_exit(&db
->db_mtx
);
3123 #pragma weak dmu_buf_refcount = dbuf_refcount
3125 dbuf_refcount(dmu_buf_impl_t
*db
)
3127 return (refcount_count(&db
->db_holds
));
3131 dmu_buf_replace_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*old_user
,
3132 dmu_buf_user_t
*new_user
)
3134 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3136 mutex_enter(&db
->db_mtx
);
3137 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
3138 if (db
->db_user
== old_user
)
3139 db
->db_user
= new_user
;
3141 old_user
= db
->db_user
;
3142 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
3143 mutex_exit(&db
->db_mtx
);
3149 dmu_buf_set_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
3151 return (dmu_buf_replace_user(db_fake
, NULL
, user
));
3155 dmu_buf_set_user_ie(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
3157 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3159 db
->db_user_immediate_evict
= TRUE
;
3160 return (dmu_buf_set_user(db_fake
, user
));
3164 dmu_buf_remove_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
3166 return (dmu_buf_replace_user(db_fake
, user
, NULL
));
3170 dmu_buf_get_user(dmu_buf_t
*db_fake
)
3172 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3174 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
3175 return (db
->db_user
);
3179 dmu_buf_user_evict_wait()
3181 taskq_wait(dbu_evict_taskq
);
3185 dmu_buf_get_blkptr(dmu_buf_t
*db
)
3187 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3188 return (dbi
->db_blkptr
);
3192 dmu_buf_get_objset(dmu_buf_t
*db
)
3194 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3195 return (dbi
->db_objset
);
3199 dmu_buf_dnode_enter(dmu_buf_t
*db
)
3201 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3202 DB_DNODE_ENTER(dbi
);
3203 return (DB_DNODE(dbi
));
3207 dmu_buf_dnode_exit(dmu_buf_t
*db
)
3209 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3214 dbuf_check_blkptr(dnode_t
*dn
, dmu_buf_impl_t
*db
)
3216 /* ASSERT(dmu_tx_is_syncing(tx) */
3217 ASSERT(MUTEX_HELD(&db
->db_mtx
));
3219 if (db
->db_blkptr
!= NULL
)
3222 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3223 db
->db_blkptr
= DN_SPILL_BLKPTR(dn
->dn_phys
);
3224 BP_ZERO(db
->db_blkptr
);
3227 if (db
->db_level
== dn
->dn_phys
->dn_nlevels
-1) {
3229 * This buffer was allocated at a time when there was
3230 * no available blkptrs from the dnode, or it was
3231 * inappropriate to hook it in (i.e., nlevels mis-match).
3233 ASSERT(db
->db_blkid
< dn
->dn_phys
->dn_nblkptr
);
3234 ASSERT(db
->db_parent
== NULL
);
3235 db
->db_parent
= dn
->dn_dbuf
;
3236 db
->db_blkptr
= &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
];
3239 dmu_buf_impl_t
*parent
= db
->db_parent
;
3240 int epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
3242 ASSERT(dn
->dn_phys
->dn_nlevels
> 1);
3243 if (parent
== NULL
) {
3244 mutex_exit(&db
->db_mtx
);
3245 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
3246 parent
= dbuf_hold_level(dn
, db
->db_level
+ 1,
3247 db
->db_blkid
>> epbs
, db
);
3248 rw_exit(&dn
->dn_struct_rwlock
);
3249 mutex_enter(&db
->db_mtx
);
3250 db
->db_parent
= parent
;
3252 db
->db_blkptr
= (blkptr_t
*)parent
->db
.db_data
+
3253 (db
->db_blkid
& ((1ULL << epbs
) - 1));
3259 * Ensure the dbuf's data is untransformed if the associated dirty
3260 * record requires it. This is used by dbuf_sync_leaf() to ensure
3261 * that a dnode block is decrypted before we write new data to it.
3262 * For raw writes we assert that the buffer is already encrypted.
3265 dbuf_check_crypt(dbuf_dirty_record_t
*dr
)
3268 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3270 ASSERT(MUTEX_HELD(&db
->db_mtx
));
3272 if (!dr
->dt
.dl
.dr_raw
&& arc_is_encrypted(db
->db_buf
)) {
3274 * Unfortunately, there is currently no mechanism for
3275 * syncing context to handle decryption errors. An error
3276 * here is only possible if an attacker maliciously
3277 * changed a dnode block and updated the associated
3278 * checksums going up the block tree.
3280 err
= arc_untransform(db
->db_buf
, db
->db_objset
->os_spa
,
3281 dmu_objset_id(db
->db_objset
), B_TRUE
);
3283 panic("Invalid dnode block MAC");
3284 } else if (dr
->dt
.dl
.dr_raw
) {
3286 * Writing raw encrypted data requires the db's arc buffer
3287 * to be converted to raw by the caller.
3289 ASSERT(arc_is_encrypted(db
->db_buf
));
3294 * dbuf_sync_indirect() is called recursively from dbuf_sync_list() so it
3295 * is critical the we not allow the compiler to inline this function in to
3296 * dbuf_sync_list() thereby drastically bloating the stack usage.
3298 noinline
static void
3299 dbuf_sync_indirect(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
3301 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3305 ASSERT(dmu_tx_is_syncing(tx
));
3307 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
3309 mutex_enter(&db
->db_mtx
);
3311 ASSERT(db
->db_level
> 0);
3314 /* Read the block if it hasn't been read yet. */
3315 if (db
->db_buf
== NULL
) {
3316 mutex_exit(&db
->db_mtx
);
3317 (void) dbuf_read(db
, NULL
, DB_RF_MUST_SUCCEED
);
3318 mutex_enter(&db
->db_mtx
);
3320 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
3321 ASSERT(db
->db_buf
!= NULL
);
3325 /* Indirect block size must match what the dnode thinks it is. */
3326 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
3327 dbuf_check_blkptr(dn
, db
);
3330 /* Provide the pending dirty record to child dbufs */
3331 db
->db_data_pending
= dr
;
3333 mutex_exit(&db
->db_mtx
);
3334 dbuf_write(dr
, db
->db_buf
, tx
);
3337 mutex_enter(&dr
->dt
.di
.dr_mtx
);
3338 dbuf_sync_list(&dr
->dt
.di
.dr_children
, db
->db_level
- 1, tx
);
3339 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
3340 mutex_exit(&dr
->dt
.di
.dr_mtx
);
3345 * dbuf_sync_leaf() is called recursively from dbuf_sync_list() so it is
3346 * critical the we not allow the compiler to inline this function in to
3347 * dbuf_sync_list() thereby drastically bloating the stack usage.
3349 noinline
static void
3350 dbuf_sync_leaf(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
3352 arc_buf_t
**datap
= &dr
->dt
.dl
.dr_data
;
3353 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3356 uint64_t txg
= tx
->tx_txg
;
3358 ASSERT(dmu_tx_is_syncing(tx
));
3360 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
3362 mutex_enter(&db
->db_mtx
);
3364 * To be synced, we must be dirtied. But we
3365 * might have been freed after the dirty.
3367 if (db
->db_state
== DB_UNCACHED
) {
3368 /* This buffer has been freed since it was dirtied */
3369 ASSERT(db
->db
.db_data
== NULL
);
3370 } else if (db
->db_state
== DB_FILL
) {
3371 /* This buffer was freed and is now being re-filled */
3372 ASSERT(db
->db
.db_data
!= dr
->dt
.dl
.dr_data
);
3374 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_NOFILL
);
3381 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3382 mutex_enter(&dn
->dn_mtx
);
3383 if (!(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
)) {
3385 * In the previous transaction group, the bonus buffer
3386 * was entirely used to store the attributes for the
3387 * dnode which overrode the dn_spill field. However,
3388 * when adding more attributes to the file a spill
3389 * block was required to hold the extra attributes.
3391 * Make sure to clear the garbage left in the dn_spill
3392 * field from the previous attributes in the bonus
3393 * buffer. Otherwise, after writing out the spill
3394 * block to the new allocated dva, it will free
3395 * the old block pointed to by the invalid dn_spill.
3397 db
->db_blkptr
= NULL
;
3399 dn
->dn_phys
->dn_flags
|= DNODE_FLAG_SPILL_BLKPTR
;
3400 mutex_exit(&dn
->dn_mtx
);
3404 * If this is a bonus buffer, simply copy the bonus data into the
3405 * dnode. It will be written out when the dnode is synced (and it
3406 * will be synced, since it must have been dirty for dbuf_sync to
3409 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
3410 dbuf_dirty_record_t
**drp
;
3412 ASSERT(*datap
!= NULL
);
3413 ASSERT0(db
->db_level
);
3414 ASSERT3U(DN_MAX_BONUS_LEN(dn
->dn_phys
), <=,
3415 DN_SLOTS_TO_BONUSLEN(dn
->dn_phys
->dn_extra_slots
+ 1));
3416 bcopy(*datap
, DN_BONUS(dn
->dn_phys
),
3417 DN_MAX_BONUS_LEN(dn
->dn_phys
));
3420 if (*datap
!= db
->db
.db_data
) {
3421 int slots
= DB_DNODE(db
)->dn_num_slots
;
3422 int bonuslen
= DN_SLOTS_TO_BONUSLEN(slots
);
3423 kmem_free(*datap
, bonuslen
);
3424 arc_space_return(bonuslen
, ARC_SPACE_BONUS
);
3426 db
->db_data_pending
= NULL
;
3427 drp
= &db
->db_last_dirty
;
3429 drp
= &(*drp
)->dr_next
;
3430 ASSERT(dr
->dr_next
== NULL
);
3431 ASSERT(dr
->dr_dbuf
== db
);
3433 if (dr
->dr_dbuf
->db_level
!= 0) {
3434 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
3435 list_destroy(&dr
->dt
.di
.dr_children
);
3437 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
3438 ASSERT(db
->db_dirtycnt
> 0);
3439 db
->db_dirtycnt
-= 1;
3440 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)txg
);
3447 * This function may have dropped the db_mtx lock allowing a dmu_sync
3448 * operation to sneak in. As a result, we need to ensure that we
3449 * don't check the dr_override_state until we have returned from
3450 * dbuf_check_blkptr.
3452 dbuf_check_blkptr(dn
, db
);
3455 * If this buffer is in the middle of an immediate write,
3456 * wait for the synchronous IO to complete.
3458 while (dr
->dt
.dl
.dr_override_state
== DR_IN_DMU_SYNC
) {
3459 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
3460 cv_wait(&db
->db_changed
, &db
->db_mtx
);
3461 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_NOT_OVERRIDDEN
);
3465 * If this is a dnode block, ensure it is appropriately encrypted
3466 * or decrypted, depending on what we are writing to it this txg.
3468 if (os
->os_encrypted
&& dn
->dn_object
== DMU_META_DNODE_OBJECT
)
3469 dbuf_check_crypt(dr
);
3471 if (db
->db_state
!= DB_NOFILL
&&
3472 dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
3473 refcount_count(&db
->db_holds
) > 1 &&
3474 dr
->dt
.dl
.dr_override_state
!= DR_OVERRIDDEN
&&
3475 *datap
== db
->db_buf
) {
3477 * If this buffer is currently "in use" (i.e., there
3478 * are active holds and db_data still references it),
3479 * then make a copy before we start the write so that
3480 * any modifications from the open txg will not leak
3483 * NOTE: this copy does not need to be made for
3484 * objects only modified in the syncing context (e.g.
3485 * DNONE_DNODE blocks).
3487 int psize
= arc_buf_size(*datap
);
3488 int lsize
= arc_buf_lsize(*datap
);
3489 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
3490 enum zio_compress compress_type
= arc_get_compression(*datap
);
3492 if (arc_is_encrypted(*datap
)) {
3493 boolean_t byteorder
;
3494 uint8_t salt
[ZIO_DATA_SALT_LEN
];
3495 uint8_t iv
[ZIO_DATA_IV_LEN
];
3496 uint8_t mac
[ZIO_DATA_MAC_LEN
];
3498 arc_get_raw_params(*datap
, &byteorder
, salt
, iv
, mac
);
3499 *datap
= arc_alloc_raw_buf(os
->os_spa
, db
,
3500 dmu_objset_id(os
), byteorder
, salt
, iv
, mac
,
3501 dn
->dn_type
, psize
, lsize
, compress_type
);
3502 } else if (compress_type
!= ZIO_COMPRESS_OFF
) {
3503 ASSERT3U(type
, ==, ARC_BUFC_DATA
);
3504 *datap
= arc_alloc_compressed_buf(os
->os_spa
, db
,
3505 psize
, lsize
, compress_type
);
3507 *datap
= arc_alloc_buf(os
->os_spa
, db
, type
, psize
);
3509 bcopy(db
->db
.db_data
, (*datap
)->b_data
, psize
);
3511 db
->db_data_pending
= dr
;
3513 mutex_exit(&db
->db_mtx
);
3515 dbuf_write(dr
, *datap
, tx
);
3517 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
3518 if (dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
3519 list_insert_tail(&dn
->dn_dirty_records
[txg
&TXG_MASK
], dr
);
3523 * Although zio_nowait() does not "wait for an IO", it does
3524 * initiate the IO. If this is an empty write it seems plausible
3525 * that the IO could actually be completed before the nowait
3526 * returns. We need to DB_DNODE_EXIT() first in case
3527 * zio_nowait() invalidates the dbuf.
3530 zio_nowait(dr
->dr_zio
);
3535 dbuf_sync_list(list_t
*list
, int level
, dmu_tx_t
*tx
)
3537 dbuf_dirty_record_t
*dr
;
3539 while ((dr
= list_head(list
))) {
3540 if (dr
->dr_zio
!= NULL
) {
3542 * If we find an already initialized zio then we
3543 * are processing the meta-dnode, and we have finished.
3544 * The dbufs for all dnodes are put back on the list
3545 * during processing, so that we can zio_wait()
3546 * these IOs after initiating all child IOs.
3548 ASSERT3U(dr
->dr_dbuf
->db
.db_object
, ==,
3549 DMU_META_DNODE_OBJECT
);
3552 if (dr
->dr_dbuf
->db_blkid
!= DMU_BONUS_BLKID
&&
3553 dr
->dr_dbuf
->db_blkid
!= DMU_SPILL_BLKID
) {
3554 VERIFY3U(dr
->dr_dbuf
->db_level
, ==, level
);
3556 list_remove(list
, dr
);
3557 if (dr
->dr_dbuf
->db_level
> 0)
3558 dbuf_sync_indirect(dr
, tx
);
3560 dbuf_sync_leaf(dr
, tx
);
3566 dbuf_write_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
3568 dmu_buf_impl_t
*db
= vdb
;
3570 blkptr_t
*bp
= zio
->io_bp
;
3571 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
3572 spa_t
*spa
= zio
->io_spa
;
3577 ASSERT3P(db
->db_blkptr
, !=, NULL
);
3578 ASSERT3P(&db
->db_data_pending
->dr_bp_copy
, ==, bp
);
3582 delta
= bp_get_dsize_sync(spa
, bp
) - bp_get_dsize_sync(spa
, bp_orig
);
3583 dnode_diduse_space(dn
, delta
- zio
->io_prev_space_delta
);
3584 zio
->io_prev_space_delta
= delta
;
3586 if (bp
->blk_birth
!= 0) {
3587 ASSERT((db
->db_blkid
!= DMU_SPILL_BLKID
&&
3588 BP_GET_TYPE(bp
) == dn
->dn_type
) ||
3589 (db
->db_blkid
== DMU_SPILL_BLKID
&&
3590 BP_GET_TYPE(bp
) == dn
->dn_bonustype
) ||
3591 BP_IS_EMBEDDED(bp
));
3592 ASSERT(BP_GET_LEVEL(bp
) == db
->db_level
);
3595 mutex_enter(&db
->db_mtx
);
3598 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3599 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
3600 ASSERT(!(BP_IS_HOLE(bp
)) &&
3601 db
->db_blkptr
== DN_SPILL_BLKPTR(dn
->dn_phys
));
3605 if (db
->db_level
== 0) {
3606 mutex_enter(&dn
->dn_mtx
);
3607 if (db
->db_blkid
> dn
->dn_phys
->dn_maxblkid
&&
3608 db
->db_blkid
!= DMU_SPILL_BLKID
)
3609 dn
->dn_phys
->dn_maxblkid
= db
->db_blkid
;
3610 mutex_exit(&dn
->dn_mtx
);
3612 if (dn
->dn_type
== DMU_OT_DNODE
) {
3614 while (i
< db
->db
.db_size
) {
3616 (void *)(((char *)db
->db
.db_data
) + i
);
3618 i
+= DNODE_MIN_SIZE
;
3619 if (dnp
->dn_type
!= DMU_OT_NONE
) {
3621 i
+= dnp
->dn_extra_slots
*
3626 if (BP_IS_HOLE(bp
)) {
3633 blkptr_t
*ibp
= db
->db
.db_data
;
3634 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
3635 for (i
= db
->db
.db_size
>> SPA_BLKPTRSHIFT
; i
> 0; i
--, ibp
++) {
3636 if (BP_IS_HOLE(ibp
))
3638 fill
+= BP_GET_FILL(ibp
);
3643 if (!BP_IS_EMBEDDED(bp
))
3644 BP_SET_FILL(bp
, fill
);
3646 mutex_exit(&db
->db_mtx
);
3648 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
3649 *db
->db_blkptr
= *bp
;
3650 rw_exit(&dn
->dn_struct_rwlock
);
3655 * This function gets called just prior to running through the compression
3656 * stage of the zio pipeline. If we're an indirect block comprised of only
3657 * holes, then we want this indirect to be compressed away to a hole. In
3658 * order to do that we must zero out any information about the holes that
3659 * this indirect points to prior to before we try to compress it.
3662 dbuf_write_children_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
3664 dmu_buf_impl_t
*db
= vdb
;
3667 unsigned int epbs
, i
;
3669 ASSERT3U(db
->db_level
, >, 0);
3672 epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
3673 ASSERT3U(epbs
, <, 31);
3675 /* Determine if all our children are holes */
3676 for (i
= 0, bp
= db
->db
.db_data
; i
< 1ULL << epbs
; i
++, bp
++) {
3677 if (!BP_IS_HOLE(bp
))
3682 * If all the children are holes, then zero them all out so that
3683 * we may get compressed away.
3685 if (i
== 1ULL << epbs
) {
3687 * We only found holes. Grab the rwlock to prevent
3688 * anybody from reading the blocks we're about to
3691 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
3692 bzero(db
->db
.db_data
, db
->db
.db_size
);
3693 rw_exit(&dn
->dn_struct_rwlock
);
3699 * The SPA will call this callback several times for each zio - once
3700 * for every physical child i/o (zio->io_phys_children times). This
3701 * allows the DMU to monitor the progress of each logical i/o. For example,
3702 * there may be 2 copies of an indirect block, or many fragments of a RAID-Z
3703 * block. There may be a long delay before all copies/fragments are completed,
3704 * so this callback allows us to retire dirty space gradually, as the physical
3709 dbuf_write_physdone(zio_t
*zio
, arc_buf_t
*buf
, void *arg
)
3711 dmu_buf_impl_t
*db
= arg
;
3712 objset_t
*os
= db
->db_objset
;
3713 dsl_pool_t
*dp
= dmu_objset_pool(os
);
3714 dbuf_dirty_record_t
*dr
;
3717 dr
= db
->db_data_pending
;
3718 ASSERT3U(dr
->dr_txg
, ==, zio
->io_txg
);
3721 * The callback will be called io_phys_children times. Retire one
3722 * portion of our dirty space each time we are called. Any rounding
3723 * error will be cleaned up by dsl_pool_sync()'s call to
3724 * dsl_pool_undirty_space().
3726 delta
= dr
->dr_accounted
/ zio
->io_phys_children
;
3727 dsl_pool_undirty_space(dp
, delta
, zio
->io_txg
);
3732 dbuf_write_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
3734 dmu_buf_impl_t
*db
= vdb
;
3735 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
3736 blkptr_t
*bp
= db
->db_blkptr
;
3737 objset_t
*os
= db
->db_objset
;
3738 dmu_tx_t
*tx
= os
->os_synctx
;
3739 dbuf_dirty_record_t
**drp
, *dr
;
3741 ASSERT0(zio
->io_error
);
3742 ASSERT(db
->db_blkptr
== bp
);
3745 * For nopwrites and rewrites we ensure that the bp matches our
3746 * original and bypass all the accounting.
3748 if (zio
->io_flags
& (ZIO_FLAG_IO_REWRITE
| ZIO_FLAG_NOPWRITE
)) {
3749 ASSERT(BP_EQUAL(bp
, bp_orig
));
3751 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
3752 (void) dsl_dataset_block_kill(ds
, bp_orig
, tx
, B_TRUE
);
3753 dsl_dataset_block_born(ds
, bp
, tx
);
3756 mutex_enter(&db
->db_mtx
);
3760 drp
= &db
->db_last_dirty
;
3761 while ((dr
= *drp
) != db
->db_data_pending
)
3763 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
3764 ASSERT(dr
->dr_dbuf
== db
);
3765 ASSERT(dr
->dr_next
== NULL
);
3769 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3774 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
3775 ASSERT(!(BP_IS_HOLE(db
->db_blkptr
)) &&
3776 db
->db_blkptr
== DN_SPILL_BLKPTR(dn
->dn_phys
));
3781 if (db
->db_level
== 0) {
3782 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
3783 ASSERT(dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
);
3784 if (db
->db_state
!= DB_NOFILL
) {
3785 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
3786 arc_buf_destroy(dr
->dt
.dl
.dr_data
, db
);
3793 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
3794 ASSERT3U(db
->db
.db_size
, ==, 1 << dn
->dn_phys
->dn_indblkshift
);
3795 if (!BP_IS_HOLE(db
->db_blkptr
)) {
3796 ASSERTV(int epbs
= dn
->dn_phys
->dn_indblkshift
-
3798 ASSERT3U(db
->db_blkid
, <=,
3799 dn
->dn_phys
->dn_maxblkid
>> (db
->db_level
* epbs
));
3800 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==,
3804 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
3805 list_destroy(&dr
->dt
.di
.dr_children
);
3807 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
3809 cv_broadcast(&db
->db_changed
);
3810 ASSERT(db
->db_dirtycnt
> 0);
3811 db
->db_dirtycnt
-= 1;
3812 db
->db_data_pending
= NULL
;
3813 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)tx
->tx_txg
);
3817 dbuf_write_nofill_ready(zio_t
*zio
)
3819 dbuf_write_ready(zio
, NULL
, zio
->io_private
);
3823 dbuf_write_nofill_done(zio_t
*zio
)
3825 dbuf_write_done(zio
, NULL
, zio
->io_private
);
3829 dbuf_write_override_ready(zio_t
*zio
)
3831 dbuf_dirty_record_t
*dr
= zio
->io_private
;
3832 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3834 dbuf_write_ready(zio
, NULL
, db
);
3838 dbuf_write_override_done(zio_t
*zio
)
3840 dbuf_dirty_record_t
*dr
= zio
->io_private
;
3841 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3842 blkptr_t
*obp
= &dr
->dt
.dl
.dr_overridden_by
;
3844 mutex_enter(&db
->db_mtx
);
3845 if (!BP_EQUAL(zio
->io_bp
, obp
)) {
3846 if (!BP_IS_HOLE(obp
))
3847 dsl_free(spa_get_dsl(zio
->io_spa
), zio
->io_txg
, obp
);
3848 arc_release(dr
->dt
.dl
.dr_data
, db
);
3850 mutex_exit(&db
->db_mtx
);
3852 dbuf_write_done(zio
, NULL
, db
);
3854 if (zio
->io_abd
!= NULL
)
3855 abd_put(zio
->io_abd
);
3858 /* Issue I/O to commit a dirty buffer to disk. */
3860 dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
)
3862 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3865 dmu_buf_impl_t
*parent
= db
->db_parent
;
3866 uint64_t txg
= tx
->tx_txg
;
3867 zbookmark_phys_t zb
;
3872 ASSERT(dmu_tx_is_syncing(tx
));
3878 if (db
->db_state
!= DB_NOFILL
) {
3879 if (db
->db_level
> 0 || dn
->dn_type
== DMU_OT_DNODE
) {
3881 * Private object buffers are released here rather
3882 * than in dbuf_dirty() since they are only modified
3883 * in the syncing context and we don't want the
3884 * overhead of making multiple copies of the data.
3886 if (BP_IS_HOLE(db
->db_blkptr
)) {
3889 dbuf_release_bp(db
);
3894 if (parent
!= dn
->dn_dbuf
) {
3895 /* Our parent is an indirect block. */
3896 /* We have a dirty parent that has been scheduled for write. */
3897 ASSERT(parent
&& parent
->db_data_pending
);
3898 /* Our parent's buffer is one level closer to the dnode. */
3899 ASSERT(db
->db_level
== parent
->db_level
-1);
3901 * We're about to modify our parent's db_data by modifying
3902 * our block pointer, so the parent must be released.
3904 ASSERT(arc_released(parent
->db_buf
));
3905 zio
= parent
->db_data_pending
->dr_zio
;
3907 /* Our parent is the dnode itself. */
3908 ASSERT((db
->db_level
== dn
->dn_phys
->dn_nlevels
-1 &&
3909 db
->db_blkid
!= DMU_SPILL_BLKID
) ||
3910 (db
->db_blkid
== DMU_SPILL_BLKID
&& db
->db_level
== 0));
3911 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
3912 ASSERT3P(db
->db_blkptr
, ==,
3913 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
3917 ASSERT(db
->db_level
== 0 || data
== db
->db_buf
);
3918 ASSERT3U(db
->db_blkptr
->blk_birth
, <=, txg
);
3921 SET_BOOKMARK(&zb
, os
->os_dsl_dataset
?
3922 os
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
3923 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
3925 if (db
->db_blkid
== DMU_SPILL_BLKID
)
3927 wp_flag
|= (db
->db_state
== DB_NOFILL
) ? WP_NOFILL
: 0;
3929 dmu_write_policy(os
, dn
, db
->db_level
, wp_flag
, &zp
);
3933 * We copy the blkptr now (rather than when we instantiate the dirty
3934 * record), because its value can change between open context and
3935 * syncing context. We do not need to hold dn_struct_rwlock to read
3936 * db_blkptr because we are in syncing context.
3938 dr
->dr_bp_copy
= *db
->db_blkptr
;
3940 if (db
->db_level
== 0 &&
3941 dr
->dt
.dl
.dr_override_state
== DR_OVERRIDDEN
) {
3943 * The BP for this block has been provided by open context
3944 * (by dmu_sync() or dmu_buf_write_embedded()).
3946 abd_t
*contents
= (data
!= NULL
) ?
3947 abd_get_from_buf(data
->b_data
, arc_buf_size(data
)) : NULL
;
3949 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
,
3950 &dr
->dr_bp_copy
, contents
, db
->db
.db_size
, db
->db
.db_size
,
3951 &zp
, dbuf_write_override_ready
, NULL
, NULL
,
3952 dbuf_write_override_done
,
3953 dr
, ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
3954 mutex_enter(&db
->db_mtx
);
3955 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
3956 zio_write_override(dr
->dr_zio
, &dr
->dt
.dl
.dr_overridden_by
,
3957 dr
->dt
.dl
.dr_copies
, dr
->dt
.dl
.dr_nopwrite
);
3958 mutex_exit(&db
->db_mtx
);
3959 } else if (db
->db_state
== DB_NOFILL
) {
3960 ASSERT(zp
.zp_checksum
== ZIO_CHECKSUM_OFF
||
3961 zp
.zp_checksum
== ZIO_CHECKSUM_NOPARITY
);
3962 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
,
3963 &dr
->dr_bp_copy
, NULL
, db
->db
.db_size
, db
->db
.db_size
, &zp
,
3964 dbuf_write_nofill_ready
, NULL
, NULL
,
3965 dbuf_write_nofill_done
, db
,
3966 ZIO_PRIORITY_ASYNC_WRITE
,
3967 ZIO_FLAG_MUSTSUCCEED
| ZIO_FLAG_NODATA
, &zb
);
3969 ASSERT(arc_released(data
));
3972 * For indirect blocks, we want to setup the children
3973 * ready callback so that we can properly handle an indirect
3974 * block that only contains holes.
3976 arc_write_done_func_t
*children_ready_cb
= NULL
;
3977 if (db
->db_level
!= 0)
3978 children_ready_cb
= dbuf_write_children_ready
;
3980 dr
->dr_zio
= arc_write(zio
, os
->os_spa
, txg
,
3981 &dr
->dr_bp_copy
, data
, DBUF_IS_L2CACHEABLE(db
),
3982 &zp
, dbuf_write_ready
,
3983 children_ready_cb
, dbuf_write_physdone
,
3984 dbuf_write_done
, db
, ZIO_PRIORITY_ASYNC_WRITE
,
3985 ZIO_FLAG_MUSTSUCCEED
, &zb
);
3989 #if defined(_KERNEL) && defined(HAVE_SPL)
3990 EXPORT_SYMBOL(dbuf_find
);
3991 EXPORT_SYMBOL(dbuf_is_metadata
);
3992 EXPORT_SYMBOL(dbuf_destroy
);
3993 EXPORT_SYMBOL(dbuf_loan_arcbuf
);
3994 EXPORT_SYMBOL(dbuf_whichblock
);
3995 EXPORT_SYMBOL(dbuf_read
);
3996 EXPORT_SYMBOL(dbuf_unoverride
);
3997 EXPORT_SYMBOL(dbuf_free_range
);
3998 EXPORT_SYMBOL(dbuf_new_size
);
3999 EXPORT_SYMBOL(dbuf_release_bp
);
4000 EXPORT_SYMBOL(dbuf_dirty
);
4001 EXPORT_SYMBOL(dmu_buf_will_change_crypt_params
);
4002 EXPORT_SYMBOL(dmu_buf_will_dirty
);
4003 EXPORT_SYMBOL(dmu_buf_will_not_fill
);
4004 EXPORT_SYMBOL(dmu_buf_will_fill
);
4005 EXPORT_SYMBOL(dmu_buf_fill_done
);
4006 EXPORT_SYMBOL(dmu_buf_rele
);
4007 EXPORT_SYMBOL(dbuf_assign_arcbuf
);
4008 EXPORT_SYMBOL(dbuf_prefetch
);
4009 EXPORT_SYMBOL(dbuf_hold_impl
);
4010 EXPORT_SYMBOL(dbuf_hold
);
4011 EXPORT_SYMBOL(dbuf_hold_level
);
4012 EXPORT_SYMBOL(dbuf_create_bonus
);
4013 EXPORT_SYMBOL(dbuf_spill_set_blksz
);
4014 EXPORT_SYMBOL(dbuf_rm_spill
);
4015 EXPORT_SYMBOL(dbuf_add_ref
);
4016 EXPORT_SYMBOL(dbuf_rele
);
4017 EXPORT_SYMBOL(dbuf_rele_and_unlock
);
4018 EXPORT_SYMBOL(dbuf_refcount
);
4019 EXPORT_SYMBOL(dbuf_sync_list
);
4020 EXPORT_SYMBOL(dmu_buf_set_user
);
4021 EXPORT_SYMBOL(dmu_buf_set_user_ie
);
4022 EXPORT_SYMBOL(dmu_buf_get_user
);
4023 EXPORT_SYMBOL(dmu_buf_get_blkptr
);
4026 module_param(dbuf_cache_max_bytes
, ulong
, 0644);
4027 MODULE_PARM_DESC(dbuf_cache_max_bytes
,
4028 "Maximum size in bytes of the dbuf cache.");
4030 module_param(dbuf_cache_hiwater_pct
, uint
, 0644);
4031 MODULE_PARM_DESC(dbuf_cache_hiwater_pct
,
4032 "Percentage over dbuf_cache_max_bytes when dbufs must be evicted "
4035 module_param(dbuf_cache_lowater_pct
, uint
, 0644);
4036 MODULE_PARM_DESC(dbuf_cache_lowater_pct
,
4037 "Percentage below dbuf_cache_max_bytes when the evict thread stops "
4040 module_param(dbuf_cache_max_shift
, int, 0644);
4041 MODULE_PARM_DESC(dbuf_cache_max_shift
,
4042 "Cap the size of the dbuf cache to a log2 fraction of arc size.");