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
;
66 arc_buf_contents_t dh_type
;
70 static void __dbuf_hold_impl_init(struct dbuf_hold_impl_data
*dh
,
71 dnode_t
*dn
, uint8_t level
, uint64_t blkid
, boolean_t fail_sparse
,
72 boolean_t fail_uncached
,
73 void *tag
, dmu_buf_impl_t
**dbp
, int depth
);
74 static int __dbuf_hold_impl(struct dbuf_hold_impl_data
*dh
);
76 uint_t zfs_dbuf_evict_key
;
78 static boolean_t
dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
);
79 static void dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
);
81 extern inline void dmu_buf_init_user(dmu_buf_user_t
*dbu
,
82 dmu_buf_evict_func_t
*evict_func_sync
,
83 dmu_buf_evict_func_t
*evict_func_async
,
84 dmu_buf_t
**clear_on_evict_dbufp
);
87 * Global data structures and functions for the dbuf cache.
89 static kmem_cache_t
*dbuf_kmem_cache
;
90 static taskq_t
*dbu_evict_taskq
;
92 static kthread_t
*dbuf_cache_evict_thread
;
93 static kmutex_t dbuf_evict_lock
;
94 static kcondvar_t dbuf_evict_cv
;
95 static boolean_t dbuf_evict_thread_exit
;
98 * LRU cache of dbufs. The dbuf cache maintains a list of dbufs that
99 * are not currently held but have been recently released. These dbufs
100 * are not eligible for arc eviction until they are aged out of the cache.
101 * Dbufs are added to the dbuf cache once the last hold is released. If a
102 * dbuf is later accessed and still exists in the dbuf cache, then it will
103 * be removed from the cache and later re-added to the head of the cache.
104 * Dbufs that are aged out of the cache will be immediately destroyed and
105 * become eligible for arc eviction.
107 static multilist_t
*dbuf_cache
;
108 static refcount_t dbuf_cache_size
;
109 unsigned long dbuf_cache_max_bytes
= 100 * 1024 * 1024;
111 /* Cap the size of the dbuf cache to log2 fraction of arc size. */
112 int dbuf_cache_max_shift
= 5;
115 * The dbuf cache uses a three-stage eviction policy:
116 * - A low water marker designates when the dbuf eviction thread
117 * should stop evicting from the dbuf cache.
118 * - When we reach the maximum size (aka mid water mark), we
119 * signal the eviction thread to run.
120 * - The high water mark indicates when the eviction thread
121 * is unable to keep up with the incoming load and eviction must
122 * happen in the context of the calling thread.
126 * low water mid water hi water
127 * +----------------------------------------+----------+----------+
132 * +----------------------------------------+----------+----------+
134 * evicting eviction directly
137 * The high and low water marks indicate the operating range for the eviction
138 * thread. The low water mark is, by default, 90% of the total size of the
139 * cache and the high water mark is at 110% (both of these percentages can be
140 * changed by setting dbuf_cache_lowater_pct and dbuf_cache_hiwater_pct,
141 * respectively). The eviction thread will try to ensure that the cache remains
142 * within this range by waking up every second and checking if the cache is
143 * above the low water mark. The thread can also be woken up by callers adding
144 * elements into the cache if the cache is larger than the mid water (i.e max
145 * cache size). Once the eviction thread is woken up and eviction is required,
146 * it will continue evicting buffers until it's able to reduce the cache size
147 * to the low water mark. If the cache size continues to grow and hits the high
148 * water mark, then callers adding elements to the cache will begin to evict
149 * directly from the cache until the cache is no longer above the high water
154 * The percentage above and below the maximum cache size.
156 uint_t dbuf_cache_hiwater_pct
= 10;
157 uint_t dbuf_cache_lowater_pct
= 10;
161 dbuf_cons(void *vdb
, void *unused
, int kmflag
)
163 dmu_buf_impl_t
*db
= vdb
;
164 bzero(db
, sizeof (dmu_buf_impl_t
));
166 mutex_init(&db
->db_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
167 cv_init(&db
->db_changed
, NULL
, CV_DEFAULT
, NULL
);
168 multilist_link_init(&db
->db_cache_link
);
169 refcount_create(&db
->db_holds
);
176 dbuf_dest(void *vdb
, void *unused
)
178 dmu_buf_impl_t
*db
= vdb
;
179 mutex_destroy(&db
->db_mtx
);
180 cv_destroy(&db
->db_changed
);
181 ASSERT(!multilist_link_active(&db
->db_cache_link
));
182 refcount_destroy(&db
->db_holds
);
186 * dbuf hash table routines
188 static dbuf_hash_table_t dbuf_hash_table
;
190 static uint64_t dbuf_hash_count
;
193 dbuf_hash(void *os
, uint64_t obj
, uint8_t lvl
, uint64_t blkid
)
195 uintptr_t osv
= (uintptr_t)os
;
196 uint64_t crc
= -1ULL;
198 ASSERT(zfs_crc64_table
[128] == ZFS_CRC64_POLY
);
199 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (lvl
)) & 0xFF];
200 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (osv
>> 6)) & 0xFF];
201 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 0)) & 0xFF];
202 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 8)) & 0xFF];
203 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (blkid
>> 0)) & 0xFF];
204 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (blkid
>> 8)) & 0xFF];
206 crc
^= (osv
>>14) ^ (obj
>>16) ^ (blkid
>>16);
211 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
212 ((dbuf)->db.db_object == (obj) && \
213 (dbuf)->db_objset == (os) && \
214 (dbuf)->db_level == (level) && \
215 (dbuf)->db_blkid == (blkid))
218 dbuf_find(objset_t
*os
, uint64_t obj
, uint8_t level
, uint64_t blkid
)
220 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
225 hv
= dbuf_hash(os
, obj
, level
, blkid
);
226 idx
= hv
& h
->hash_table_mask
;
228 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
229 for (db
= h
->hash_table
[idx
]; db
!= NULL
; db
= db
->db_hash_next
) {
230 if (DBUF_EQUAL(db
, os
, obj
, level
, blkid
)) {
231 mutex_enter(&db
->db_mtx
);
232 if (db
->db_state
!= DB_EVICTING
) {
233 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
236 mutex_exit(&db
->db_mtx
);
239 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
243 static dmu_buf_impl_t
*
244 dbuf_find_bonus(objset_t
*os
, uint64_t object
)
247 dmu_buf_impl_t
*db
= NULL
;
249 if (dnode_hold(os
, object
, FTAG
, &dn
) == 0) {
250 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
251 if (dn
->dn_bonus
!= NULL
) {
253 mutex_enter(&db
->db_mtx
);
255 rw_exit(&dn
->dn_struct_rwlock
);
256 dnode_rele(dn
, FTAG
);
262 * Insert an entry into the hash table. If there is already an element
263 * equal to elem in the hash table, then the already existing element
264 * will be returned and the new element will not be inserted.
265 * Otherwise returns NULL.
267 static dmu_buf_impl_t
*
268 dbuf_hash_insert(dmu_buf_impl_t
*db
)
270 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
271 objset_t
*os
= db
->db_objset
;
272 uint64_t obj
= db
->db
.db_object
;
273 int level
= db
->db_level
;
274 uint64_t blkid
, hv
, idx
;
277 blkid
= db
->db_blkid
;
278 hv
= dbuf_hash(os
, obj
, level
, blkid
);
279 idx
= hv
& h
->hash_table_mask
;
281 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
282 for (dbf
= h
->hash_table
[idx
]; dbf
!= NULL
; dbf
= dbf
->db_hash_next
) {
283 if (DBUF_EQUAL(dbf
, os
, obj
, level
, blkid
)) {
284 mutex_enter(&dbf
->db_mtx
);
285 if (dbf
->db_state
!= DB_EVICTING
) {
286 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
289 mutex_exit(&dbf
->db_mtx
);
293 mutex_enter(&db
->db_mtx
);
294 db
->db_hash_next
= h
->hash_table
[idx
];
295 h
->hash_table
[idx
] = db
;
296 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
297 atomic_inc_64(&dbuf_hash_count
);
303 * Remove an entry from the hash table. It must be in the EVICTING state.
306 dbuf_hash_remove(dmu_buf_impl_t
*db
)
308 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
310 dmu_buf_impl_t
*dbf
, **dbp
;
312 hv
= dbuf_hash(db
->db_objset
, db
->db
.db_object
,
313 db
->db_level
, db
->db_blkid
);
314 idx
= hv
& h
->hash_table_mask
;
317 * We mustn't hold db_mtx to maintain lock ordering:
318 * DBUF_HASH_MUTEX > db_mtx.
320 ASSERT(refcount_is_zero(&db
->db_holds
));
321 ASSERT(db
->db_state
== DB_EVICTING
);
322 ASSERT(!MUTEX_HELD(&db
->db_mtx
));
324 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
325 dbp
= &h
->hash_table
[idx
];
326 while ((dbf
= *dbp
) != db
) {
327 dbp
= &dbf
->db_hash_next
;
330 *dbp
= db
->db_hash_next
;
331 db
->db_hash_next
= NULL
;
332 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
333 atomic_dec_64(&dbuf_hash_count
);
339 } dbvu_verify_type_t
;
342 dbuf_verify_user(dmu_buf_impl_t
*db
, dbvu_verify_type_t verify_type
)
347 if (db
->db_user
== NULL
)
350 /* Only data blocks support the attachment of user data. */
351 ASSERT(db
->db_level
== 0);
353 /* Clients must resolve a dbuf before attaching user data. */
354 ASSERT(db
->db
.db_data
!= NULL
);
355 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
357 holds
= refcount_count(&db
->db_holds
);
358 if (verify_type
== DBVU_EVICTING
) {
360 * Immediate eviction occurs when holds == dirtycnt.
361 * For normal eviction buffers, holds is zero on
362 * eviction, except when dbuf_fix_old_data() calls
363 * dbuf_clear_data(). However, the hold count can grow
364 * during eviction even though db_mtx is held (see
365 * dmu_bonus_hold() for an example), so we can only
366 * test the generic invariant that holds >= dirtycnt.
368 ASSERT3U(holds
, >=, db
->db_dirtycnt
);
370 if (db
->db_user_immediate_evict
== TRUE
)
371 ASSERT3U(holds
, >=, db
->db_dirtycnt
);
373 ASSERT3U(holds
, >, 0);
379 dbuf_evict_user(dmu_buf_impl_t
*db
)
381 dmu_buf_user_t
*dbu
= db
->db_user
;
383 ASSERT(MUTEX_HELD(&db
->db_mtx
));
388 dbuf_verify_user(db
, DBVU_EVICTING
);
392 if (dbu
->dbu_clear_on_evict_dbufp
!= NULL
)
393 *dbu
->dbu_clear_on_evict_dbufp
= NULL
;
397 * There are two eviction callbacks - one that we call synchronously
398 * and one that we invoke via a taskq. The async one is useful for
399 * avoiding lock order reversals and limiting stack depth.
401 * Note that if we have a sync callback but no async callback,
402 * it's likely that the sync callback will free the structure
403 * containing the dbu. In that case we need to take care to not
404 * dereference dbu after calling the sync evict func.
406 boolean_t has_async
= (dbu
->dbu_evict_func_async
!= NULL
);
408 if (dbu
->dbu_evict_func_sync
!= NULL
)
409 dbu
->dbu_evict_func_sync(dbu
);
412 taskq_dispatch_ent(dbu_evict_taskq
, dbu
->dbu_evict_func_async
,
413 dbu
, 0, &dbu
->dbu_tqent
);
418 dbuf_is_metadata(dmu_buf_impl_t
*db
)
421 * Consider indirect blocks and spill blocks to be meta data.
423 if (db
->db_level
> 0 || db
->db_blkid
== DMU_SPILL_BLKID
) {
426 boolean_t is_metadata
;
429 is_metadata
= DMU_OT_IS_METADATA(DB_DNODE(db
)->dn_type
);
432 return (is_metadata
);
438 * This function *must* return indices evenly distributed between all
439 * sublists of the multilist. This is needed due to how the dbuf eviction
440 * code is laid out; dbuf_evict_thread() assumes dbufs are evenly
441 * distributed between all sublists and uses this assumption when
442 * deciding which sublist to evict from and how much to evict from it.
445 dbuf_cache_multilist_index_func(multilist_t
*ml
, void *obj
)
447 dmu_buf_impl_t
*db
= obj
;
450 * The assumption here, is the hash value for a given
451 * dmu_buf_impl_t will remain constant throughout it's lifetime
452 * (i.e. it's objset, object, level and blkid fields don't change).
453 * Thus, we don't need to store the dbuf's sublist index
454 * on insertion, as this index can be recalculated on removal.
456 * Also, the low order bits of the hash value are thought to be
457 * distributed evenly. Otherwise, in the case that the multilist
458 * has a power of two number of sublists, each sublists' usage
459 * would not be evenly distributed.
461 return (dbuf_hash(db
->db_objset
, db
->db
.db_object
,
462 db
->db_level
, db
->db_blkid
) %
463 multilist_get_num_sublists(ml
));
466 static inline unsigned long
467 dbuf_cache_target_bytes(void)
469 return MIN(dbuf_cache_max_bytes
,
470 arc_target_bytes() >> dbuf_cache_max_shift
);
473 static inline boolean_t
474 dbuf_cache_above_hiwater(void)
476 uint64_t dbuf_cache_target
= dbuf_cache_target_bytes();
478 uint64_t dbuf_cache_hiwater_bytes
=
479 (dbuf_cache_target
* dbuf_cache_hiwater_pct
) / 100;
481 return (refcount_count(&dbuf_cache_size
) >
482 dbuf_cache_target
+ dbuf_cache_hiwater_bytes
);
485 static inline boolean_t
486 dbuf_cache_above_lowater(void)
488 uint64_t dbuf_cache_target
= dbuf_cache_target_bytes();
490 uint64_t dbuf_cache_lowater_bytes
=
491 (dbuf_cache_target
* dbuf_cache_lowater_pct
) / 100;
493 return (refcount_count(&dbuf_cache_size
) >
494 dbuf_cache_target
- dbuf_cache_lowater_bytes
);
498 * Evict the oldest eligible dbuf from the dbuf cache.
503 int idx
= multilist_get_random_index(dbuf_cache
);
504 multilist_sublist_t
*mls
= multilist_sublist_lock(dbuf_cache
, idx
);
506 ASSERT(!MUTEX_HELD(&dbuf_evict_lock
));
509 * Set the thread's tsd to indicate that it's processing evictions.
510 * Once a thread stops evicting from the dbuf cache it will
511 * reset its tsd to NULL.
513 ASSERT3P(tsd_get(zfs_dbuf_evict_key
), ==, NULL
);
514 (void) tsd_set(zfs_dbuf_evict_key
, (void *)B_TRUE
);
516 dmu_buf_impl_t
*db
= multilist_sublist_tail(mls
);
517 while (db
!= NULL
&& mutex_tryenter(&db
->db_mtx
) == 0) {
518 db
= multilist_sublist_prev(mls
, db
);
521 DTRACE_PROBE2(dbuf__evict__one
, dmu_buf_impl_t
*, db
,
522 multilist_sublist_t
*, mls
);
525 multilist_sublist_remove(mls
, db
);
526 multilist_sublist_unlock(mls
);
527 (void) refcount_remove_many(&dbuf_cache_size
,
531 multilist_sublist_unlock(mls
);
533 (void) tsd_set(zfs_dbuf_evict_key
, NULL
);
537 * The dbuf evict thread is responsible for aging out dbufs from the
538 * cache. Once the cache has reached it's maximum size, dbufs are removed
539 * and destroyed. The eviction thread will continue running until the size
540 * of the dbuf cache is at or below the maximum size. Once the dbuf is aged
541 * out of the cache it is destroyed and becomes eligible for arc eviction.
545 dbuf_evict_thread(void *unused
)
549 CALLB_CPR_INIT(&cpr
, &dbuf_evict_lock
, callb_generic_cpr
, FTAG
);
551 mutex_enter(&dbuf_evict_lock
);
552 while (!dbuf_evict_thread_exit
) {
553 while (!dbuf_cache_above_lowater() && !dbuf_evict_thread_exit
) {
554 CALLB_CPR_SAFE_BEGIN(&cpr
);
555 (void) cv_timedwait_sig_hires(&dbuf_evict_cv
,
556 &dbuf_evict_lock
, SEC2NSEC(1), MSEC2NSEC(1), 0);
557 CALLB_CPR_SAFE_END(&cpr
, &dbuf_evict_lock
);
559 mutex_exit(&dbuf_evict_lock
);
562 * Keep evicting as long as we're above the low water mark
563 * for the cache. We do this without holding the locks to
564 * minimize lock contention.
566 while (dbuf_cache_above_lowater() && !dbuf_evict_thread_exit
) {
570 mutex_enter(&dbuf_evict_lock
);
573 dbuf_evict_thread_exit
= B_FALSE
;
574 cv_broadcast(&dbuf_evict_cv
);
575 CALLB_CPR_EXIT(&cpr
); /* drops dbuf_evict_lock */
580 * Wake up the dbuf eviction thread if the dbuf cache is at its max size.
581 * If the dbuf cache is at its high water mark, then evict a dbuf from the
582 * dbuf cache using the callers context.
585 dbuf_evict_notify(void)
589 * We use thread specific data to track when a thread has
590 * started processing evictions. This allows us to avoid deeply
591 * nested stacks that would have a call flow similar to this:
593 * dbuf_rele()-->dbuf_rele_and_unlock()-->dbuf_evict_notify()
596 * +-----dbuf_destroy()<--dbuf_evict_one()<--------+
598 * The dbuf_eviction_thread will always have its tsd set until
599 * that thread exits. All other threads will only set their tsd
600 * if they are participating in the eviction process. This only
601 * happens if the eviction thread is unable to process evictions
602 * fast enough. To keep the dbuf cache size in check, other threads
603 * can evict from the dbuf cache directly. Those threads will set
604 * their tsd values so that we ensure that they only evict one dbuf
605 * from the dbuf cache.
607 if (tsd_get(zfs_dbuf_evict_key
) != NULL
)
611 * We check if we should evict without holding the dbuf_evict_lock,
612 * because it's OK to occasionally make the wrong decision here,
613 * and grabbing the lock results in massive lock contention.
615 if (refcount_count(&dbuf_cache_size
) > dbuf_cache_target_bytes()) {
616 if (dbuf_cache_above_hiwater())
618 cv_signal(&dbuf_evict_cv
);
627 uint64_t hsize
= 1ULL << 16;
628 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
632 * The hash table is big enough to fill all of physical memory
633 * with an average block size of zfs_arc_average_blocksize (default 8K).
634 * By default, the table will take up
635 * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers).
637 while (hsize
* zfs_arc_average_blocksize
< physmem
* PAGESIZE
)
641 h
->hash_table_mask
= hsize
- 1;
642 #if defined(_KERNEL) && defined(HAVE_SPL)
644 * Large allocations which do not require contiguous pages
645 * should be using vmem_alloc() in the linux kernel
647 h
->hash_table
= vmem_zalloc(hsize
* sizeof (void *), KM_SLEEP
);
649 h
->hash_table
= kmem_zalloc(hsize
* sizeof (void *), KM_NOSLEEP
);
651 if (h
->hash_table
== NULL
) {
652 /* XXX - we should really return an error instead of assert */
653 ASSERT(hsize
> (1ULL << 10));
658 dbuf_kmem_cache
= kmem_cache_create("dmu_buf_impl_t",
659 sizeof (dmu_buf_impl_t
),
660 0, dbuf_cons
, dbuf_dest
, NULL
, NULL
, NULL
, 0);
662 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
663 mutex_init(&h
->hash_mutexes
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
668 * Setup the parameters for the dbuf cache. We cap the size of the
669 * dbuf cache to 1/32nd (default) of the size of the ARC.
671 dbuf_cache_max_bytes
= MIN(dbuf_cache_max_bytes
,
672 arc_target_bytes() >> dbuf_cache_max_shift
);
675 * All entries are queued via taskq_dispatch_ent(), so min/maxalloc
676 * configuration is not required.
678 dbu_evict_taskq
= taskq_create("dbu_evict", 1, defclsyspri
, 0, 0, 0);
680 dbuf_cache
= multilist_create(sizeof (dmu_buf_impl_t
),
681 offsetof(dmu_buf_impl_t
, db_cache_link
),
682 dbuf_cache_multilist_index_func
);
683 refcount_create(&dbuf_cache_size
);
685 tsd_create(&zfs_dbuf_evict_key
, NULL
);
686 dbuf_evict_thread_exit
= B_FALSE
;
687 mutex_init(&dbuf_evict_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
688 cv_init(&dbuf_evict_cv
, NULL
, CV_DEFAULT
, NULL
);
689 dbuf_cache_evict_thread
= thread_create(NULL
, 0, dbuf_evict_thread
,
690 NULL
, 0, &p0
, TS_RUN
, minclsyspri
);
696 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
699 dbuf_stats_destroy();
701 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
702 mutex_destroy(&h
->hash_mutexes
[i
]);
703 #if defined(_KERNEL) && defined(HAVE_SPL)
705 * Large allocations which do not require contiguous pages
706 * should be using vmem_free() in the linux kernel
708 vmem_free(h
->hash_table
, (h
->hash_table_mask
+ 1) * sizeof (void *));
710 kmem_free(h
->hash_table
, (h
->hash_table_mask
+ 1) * sizeof (void *));
712 kmem_cache_destroy(dbuf_kmem_cache
);
713 taskq_destroy(dbu_evict_taskq
);
715 mutex_enter(&dbuf_evict_lock
);
716 dbuf_evict_thread_exit
= B_TRUE
;
717 while (dbuf_evict_thread_exit
) {
718 cv_signal(&dbuf_evict_cv
);
719 cv_wait(&dbuf_evict_cv
, &dbuf_evict_lock
);
721 mutex_exit(&dbuf_evict_lock
);
722 tsd_destroy(&zfs_dbuf_evict_key
);
724 mutex_destroy(&dbuf_evict_lock
);
725 cv_destroy(&dbuf_evict_cv
);
727 refcount_destroy(&dbuf_cache_size
);
728 multilist_destroy(dbuf_cache
);
737 dbuf_verify(dmu_buf_impl_t
*db
)
740 dbuf_dirty_record_t
*dr
;
742 ASSERT(MUTEX_HELD(&db
->db_mtx
));
744 if (!(zfs_flags
& ZFS_DEBUG_DBUF_VERIFY
))
747 ASSERT(db
->db_objset
!= NULL
);
751 ASSERT(db
->db_parent
== NULL
);
752 ASSERT(db
->db_blkptr
== NULL
);
754 ASSERT3U(db
->db
.db_object
, ==, dn
->dn_object
);
755 ASSERT3P(db
->db_objset
, ==, dn
->dn_objset
);
756 ASSERT3U(db
->db_level
, <, dn
->dn_nlevels
);
757 ASSERT(db
->db_blkid
== DMU_BONUS_BLKID
||
758 db
->db_blkid
== DMU_SPILL_BLKID
||
759 !avl_is_empty(&dn
->dn_dbufs
));
761 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
763 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
764 ASSERT3U(db
->db
.db_offset
, ==, DMU_BONUS_BLKID
);
765 } else if (db
->db_blkid
== DMU_SPILL_BLKID
) {
767 ASSERT0(db
->db
.db_offset
);
769 ASSERT3U(db
->db
.db_offset
, ==, db
->db_blkid
* db
->db
.db_size
);
772 for (dr
= db
->db_data_pending
; dr
!= NULL
; dr
= dr
->dr_next
)
773 ASSERT(dr
->dr_dbuf
== db
);
775 for (dr
= db
->db_last_dirty
; dr
!= NULL
; dr
= dr
->dr_next
)
776 ASSERT(dr
->dr_dbuf
== db
);
779 * We can't assert that db_size matches dn_datablksz because it
780 * can be momentarily different when another thread is doing
783 if (db
->db_level
== 0 && db
->db
.db_object
== DMU_META_DNODE_OBJECT
) {
784 dr
= db
->db_data_pending
;
786 * It should only be modified in syncing context, so
787 * make sure we only have one copy of the data.
789 ASSERT(dr
== NULL
|| dr
->dt
.dl
.dr_data
== db
->db_buf
);
792 /* verify db->db_blkptr */
794 if (db
->db_parent
== dn
->dn_dbuf
) {
795 /* db is pointed to by the dnode */
796 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
797 if (DMU_OBJECT_IS_SPECIAL(db
->db
.db_object
))
798 ASSERT(db
->db_parent
== NULL
);
800 ASSERT(db
->db_parent
!= NULL
);
801 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
802 ASSERT3P(db
->db_blkptr
, ==,
803 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
805 /* db is pointed to by an indirect block */
806 ASSERTV(int epb
= db
->db_parent
->db
.db_size
>>
808 ASSERT3U(db
->db_parent
->db_level
, ==, db
->db_level
+1);
809 ASSERT3U(db
->db_parent
->db
.db_object
, ==,
812 * dnode_grow_indblksz() can make this fail if we don't
813 * have the struct_rwlock. XXX indblksz no longer
814 * grows. safe to do this now?
816 if (RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
817 ASSERT3P(db
->db_blkptr
, ==,
818 ((blkptr_t
*)db
->db_parent
->db
.db_data
+
819 db
->db_blkid
% epb
));
823 if ((db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
)) &&
824 (db
->db_buf
== NULL
|| db
->db_buf
->b_data
) &&
825 db
->db
.db_data
&& db
->db_blkid
!= DMU_BONUS_BLKID
&&
826 db
->db_state
!= DB_FILL
&& !dn
->dn_free_txg
) {
828 * If the blkptr isn't set but they have nonzero data,
829 * it had better be dirty, otherwise we'll lose that
830 * data when we evict this buffer.
832 * There is an exception to this rule for indirect blocks; in
833 * this case, if the indirect block is a hole, we fill in a few
834 * fields on each of the child blocks (importantly, birth time)
835 * to prevent hole birth times from being lost when you
836 * partially fill in a hole.
838 if (db
->db_dirtycnt
== 0) {
839 if (db
->db_level
== 0) {
840 uint64_t *buf
= db
->db
.db_data
;
843 for (i
= 0; i
< db
->db
.db_size
>> 3; i
++) {
847 blkptr_t
*bps
= db
->db
.db_data
;
848 ASSERT3U(1 << DB_DNODE(db
)->dn_indblkshift
, ==,
851 * We want to verify that all the blkptrs in the
852 * indirect block are holes, but we may have
853 * automatically set up a few fields for them.
854 * We iterate through each blkptr and verify
855 * they only have those fields set.
858 i
< db
->db
.db_size
/ sizeof (blkptr_t
);
860 blkptr_t
*bp
= &bps
[i
];
861 ASSERT(ZIO_CHECKSUM_IS_ZERO(
864 DVA_IS_EMPTY(&bp
->blk_dva
[0]) &&
865 DVA_IS_EMPTY(&bp
->blk_dva
[1]) &&
866 DVA_IS_EMPTY(&bp
->blk_dva
[2]));
867 ASSERT0(bp
->blk_fill
);
868 ASSERT0(bp
->blk_pad
[0]);
869 ASSERT0(bp
->blk_pad
[1]);
870 ASSERT(!BP_IS_EMBEDDED(bp
));
871 ASSERT(BP_IS_HOLE(bp
));
872 ASSERT0(bp
->blk_phys_birth
);
882 dbuf_clear_data(dmu_buf_impl_t
*db
)
884 ASSERT(MUTEX_HELD(&db
->db_mtx
));
886 ASSERT3P(db
->db_buf
, ==, NULL
);
887 db
->db
.db_data
= NULL
;
888 if (db
->db_state
!= DB_NOFILL
)
889 db
->db_state
= DB_UNCACHED
;
893 dbuf_set_data(dmu_buf_impl_t
*db
, arc_buf_t
*buf
)
895 ASSERT(MUTEX_HELD(&db
->db_mtx
));
899 ASSERT(buf
->b_data
!= NULL
);
900 db
->db
.db_data
= buf
->b_data
;
904 * Loan out an arc_buf for read. Return the loaned arc_buf.
907 dbuf_loan_arcbuf(dmu_buf_impl_t
*db
)
911 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
912 mutex_enter(&db
->db_mtx
);
913 if (arc_released(db
->db_buf
) || refcount_count(&db
->db_holds
) > 1) {
914 int blksz
= db
->db
.db_size
;
915 spa_t
*spa
= db
->db_objset
->os_spa
;
917 mutex_exit(&db
->db_mtx
);
918 abuf
= arc_loan_buf(spa
, B_FALSE
, blksz
);
919 bcopy(db
->db
.db_data
, abuf
->b_data
, blksz
);
922 arc_loan_inuse_buf(abuf
, db
);
925 mutex_exit(&db
->db_mtx
);
931 * Calculate which level n block references the data at the level 0 offset
935 dbuf_whichblock(const dnode_t
*dn
, const int64_t level
, const uint64_t offset
)
937 if (dn
->dn_datablkshift
!= 0 && dn
->dn_indblkshift
!= 0) {
939 * The level n blkid is equal to the level 0 blkid divided by
940 * the number of level 0s in a level n block.
942 * The level 0 blkid is offset >> datablkshift =
943 * offset / 2^datablkshift.
945 * The number of level 0s in a level n is the number of block
946 * pointers in an indirect block, raised to the power of level.
947 * This is 2^(indblkshift - SPA_BLKPTRSHIFT)^level =
948 * 2^(level*(indblkshift - SPA_BLKPTRSHIFT)).
950 * Thus, the level n blkid is: offset /
951 * ((2^datablkshift)*(2^(level*(indblkshift - SPA_BLKPTRSHIFT)))
952 * = offset / 2^(datablkshift + level *
953 * (indblkshift - SPA_BLKPTRSHIFT))
954 * = offset >> (datablkshift + level *
955 * (indblkshift - SPA_BLKPTRSHIFT))
958 const unsigned exp
= dn
->dn_datablkshift
+
959 level
* (dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
);
961 if (exp
>= 8 * sizeof (offset
)) {
962 /* This only happens on the highest indirection level */
963 ASSERT3U(level
, ==, dn
->dn_nlevels
- 1);
967 ASSERT3U(exp
, <, 8 * sizeof (offset
));
969 return (offset
>> exp
);
971 ASSERT3U(offset
, <, dn
->dn_datablksz
);
977 dbuf_read_done(zio_t
*zio
, int err
, arc_buf_t
*buf
, void *vdb
)
979 dmu_buf_impl_t
*db
= vdb
;
981 mutex_enter(&db
->db_mtx
);
982 ASSERT3U(db
->db_state
, ==, DB_READ
);
984 * All reads are synchronous, so we must have a hold on the dbuf
986 ASSERT(refcount_count(&db
->db_holds
) > 0);
987 ASSERT(db
->db_buf
== NULL
);
988 ASSERT(db
->db
.db_data
== NULL
);
989 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
990 /* we were freed in flight; disregard any error */
991 arc_release(buf
, db
);
992 bzero(buf
->b_data
, db
->db
.db_size
);
994 db
->db_freed_in_flight
= FALSE
;
995 dbuf_set_data(db
, buf
);
996 db
->db_state
= DB_CACHED
;
997 } else if (err
== 0) {
998 dbuf_set_data(db
, buf
);
999 db
->db_state
= DB_CACHED
;
1001 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1002 ASSERT3P(db
->db_buf
, ==, NULL
);
1003 arc_buf_destroy(buf
, db
);
1004 db
->db_state
= DB_UNCACHED
;
1006 cv_broadcast(&db
->db_changed
);
1007 dbuf_rele_and_unlock(db
, NULL
);
1011 dbuf_read_impl(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t flags
)
1014 zbookmark_phys_t zb
;
1015 uint32_t aflags
= ARC_FLAG_NOWAIT
;
1016 int err
, zio_flags
= 0;
1020 ASSERT(!refcount_is_zero(&db
->db_holds
));
1021 /* We need the struct_rwlock to prevent db_blkptr from changing. */
1022 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
1023 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1024 ASSERT(db
->db_state
== DB_UNCACHED
);
1025 ASSERT(db
->db_buf
== NULL
);
1027 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1029 * The bonus length stored in the dnode may be less than
1030 * the maximum available space in the bonus buffer.
1032 int bonuslen
= MIN(dn
->dn_bonuslen
, dn
->dn_phys
->dn_bonuslen
);
1033 int max_bonuslen
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
);
1034 arc_buf_t
*dn_buf
= (dn
->dn_dbuf
!= NULL
) ?
1035 dn
->dn_dbuf
->db_buf
: NULL
;
1037 /* if the underlying dnode block is encrypted, decrypt it */
1038 if (dn_buf
!= NULL
&& dn
->dn_objset
->os_encrypted
&&
1039 DMU_OT_IS_ENCRYPTED(dn
->dn_bonustype
) &&
1040 (flags
& DB_RF_NO_DECRYPT
) == 0 &&
1041 arc_is_encrypted(dn_buf
)) {
1042 err
= arc_untransform(dn_buf
, dn
->dn_objset
->os_spa
,
1043 dmu_objset_id(dn
->dn_objset
), B_TRUE
);
1046 mutex_exit(&db
->db_mtx
);
1051 ASSERT3U(bonuslen
, <=, db
->db
.db_size
);
1052 db
->db
.db_data
= kmem_alloc(max_bonuslen
, KM_SLEEP
);
1053 arc_space_consume(max_bonuslen
, ARC_SPACE_BONUS
);
1054 if (bonuslen
< max_bonuslen
)
1055 bzero(db
->db
.db_data
, max_bonuslen
);
1057 bcopy(DN_BONUS(dn
->dn_phys
), db
->db
.db_data
, bonuslen
);
1059 db
->db_state
= DB_CACHED
;
1060 mutex_exit(&db
->db_mtx
);
1065 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
1066 * processes the delete record and clears the bp while we are waiting
1067 * for the dn_mtx (resulting in a "no" from block_freed).
1069 if (db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
) ||
1070 (db
->db_level
== 0 && (dnode_block_freed(dn
, db
->db_blkid
) ||
1071 BP_IS_HOLE(db
->db_blkptr
)))) {
1072 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1074 dbuf_set_data(db
, arc_alloc_buf(db
->db_objset
->os_spa
, db
, type
,
1076 bzero(db
->db
.db_data
, db
->db
.db_size
);
1078 if (db
->db_blkptr
!= NULL
&& db
->db_level
> 0 &&
1079 BP_IS_HOLE(db
->db_blkptr
) &&
1080 db
->db_blkptr
->blk_birth
!= 0) {
1081 blkptr_t
*bps
= db
->db
.db_data
;
1082 for (int i
= 0; i
< ((1 <<
1083 DB_DNODE(db
)->dn_indblkshift
) / sizeof (blkptr_t
));
1085 blkptr_t
*bp
= &bps
[i
];
1086 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==,
1087 1 << dn
->dn_indblkshift
);
1089 BP_GET_LEVEL(db
->db_blkptr
) == 1 ?
1091 BP_GET_LSIZE(db
->db_blkptr
));
1092 BP_SET_TYPE(bp
, BP_GET_TYPE(db
->db_blkptr
));
1094 BP_GET_LEVEL(db
->db_blkptr
) - 1);
1095 BP_SET_BIRTH(bp
, db
->db_blkptr
->blk_birth
, 0);
1099 db
->db_state
= DB_CACHED
;
1100 mutex_exit(&db
->db_mtx
);
1106 db
->db_state
= DB_READ
;
1107 mutex_exit(&db
->db_mtx
);
1109 if (DBUF_IS_L2CACHEABLE(db
))
1110 aflags
|= ARC_FLAG_L2CACHE
;
1112 SET_BOOKMARK(&zb
, db
->db_objset
->os_dsl_dataset
?
1113 db
->db_objset
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
1114 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
1117 * All bps of an encrypted os should have the encryption bit set.
1118 * If this is not true it indicates tampering and we report an error.
1120 if (db
->db_objset
->os_encrypted
&& !BP_USES_CRYPT(db
->db_blkptr
)) {
1121 spa_log_error(db
->db_objset
->os_spa
, &zb
);
1122 zfs_panic_recover("unencrypted block in encrypted "
1123 "object set %llu", dmu_objset_id(db
->db_objset
));
1124 return (SET_ERROR(EIO
));
1127 dbuf_add_ref(db
, NULL
);
1129 zio_flags
= (flags
& DB_RF_CANFAIL
) ?
1130 ZIO_FLAG_CANFAIL
: ZIO_FLAG_MUSTSUCCEED
;
1132 if ((flags
& DB_RF_NO_DECRYPT
) && BP_IS_PROTECTED(db
->db_blkptr
))
1133 zio_flags
|= ZIO_FLAG_RAW
;
1135 err
= arc_read(zio
, db
->db_objset
->os_spa
, db
->db_blkptr
,
1136 dbuf_read_done
, db
, ZIO_PRIORITY_SYNC_READ
, zio_flags
,
1143 * This is our just-in-time copy function. It makes a copy of buffers that
1144 * have been modified in a previous transaction group before we access them in
1145 * the current active group.
1147 * This function is used in three places: when we are dirtying a buffer for the
1148 * first time in a txg, when we are freeing a range in a dnode that includes
1149 * this buffer, and when we are accessing a buffer which was received compressed
1150 * and later referenced in a WRITE_BYREF record.
1152 * Note that when we are called from dbuf_free_range() we do not put a hold on
1153 * the buffer, we just traverse the active dbuf list for the dnode.
1156 dbuf_fix_old_data(dmu_buf_impl_t
*db
, uint64_t txg
)
1158 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
1160 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1161 ASSERT(db
->db
.db_data
!= NULL
);
1162 ASSERT(db
->db_level
== 0);
1163 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
);
1166 (dr
->dt
.dl
.dr_data
!=
1167 ((db
->db_blkid
== DMU_BONUS_BLKID
) ? db
->db
.db_data
: db
->db_buf
)))
1171 * If the last dirty record for this dbuf has not yet synced
1172 * and its referencing the dbuf data, either:
1173 * reset the reference to point to a new copy,
1174 * or (if there a no active holders)
1175 * just null out the current db_data pointer.
1177 ASSERT3U(dr
->dr_txg
, >=, txg
- 2);
1178 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1179 dnode_t
*dn
= DB_DNODE(db
);
1180 int bonuslen
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
);
1181 dr
->dt
.dl
.dr_data
= kmem_alloc(bonuslen
, KM_SLEEP
);
1182 arc_space_consume(bonuslen
, ARC_SPACE_BONUS
);
1183 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
, bonuslen
);
1184 } else if (refcount_count(&db
->db_holds
) > db
->db_dirtycnt
) {
1185 dnode_t
*dn
= DB_DNODE(db
);
1186 int size
= arc_buf_size(db
->db_buf
);
1187 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1188 spa_t
*spa
= db
->db_objset
->os_spa
;
1189 enum zio_compress compress_type
=
1190 arc_get_compression(db
->db_buf
);
1192 if (arc_is_encrypted(db
->db_buf
)) {
1193 boolean_t byteorder
;
1194 uint8_t salt
[ZIO_DATA_SALT_LEN
];
1195 uint8_t iv
[ZIO_DATA_IV_LEN
];
1196 uint8_t mac
[ZIO_DATA_MAC_LEN
];
1198 arc_get_raw_params(db
->db_buf
, &byteorder
, salt
,
1200 dr
->dt
.dl
.dr_data
= arc_alloc_raw_buf(spa
, db
,
1201 dmu_objset_id(dn
->dn_objset
), byteorder
, salt
, iv
,
1202 mac
, dn
->dn_type
, size
, arc_buf_lsize(db
->db_buf
),
1204 } else if (compress_type
!= ZIO_COMPRESS_OFF
) {
1205 ASSERT3U(type
, ==, ARC_BUFC_DATA
);
1206 dr
->dt
.dl
.dr_data
= arc_alloc_compressed_buf(spa
, db
,
1207 size
, arc_buf_lsize(db
->db_buf
), compress_type
);
1209 dr
->dt
.dl
.dr_data
= arc_alloc_buf(spa
, db
, type
, size
);
1211 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
->b_data
, size
);
1214 dbuf_clear_data(db
);
1219 dbuf_read(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t flags
)
1226 * We don't have to hold the mutex to check db_state because it
1227 * can't be freed while we have a hold on the buffer.
1229 ASSERT(!refcount_is_zero(&db
->db_holds
));
1231 if (db
->db_state
== DB_NOFILL
)
1232 return (SET_ERROR(EIO
));
1236 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1237 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
1239 prefetch
= db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1240 (flags
& DB_RF_NOPREFETCH
) == 0 && dn
!= NULL
&&
1241 DBUF_IS_CACHEABLE(db
);
1243 mutex_enter(&db
->db_mtx
);
1244 if (db
->db_state
== DB_CACHED
) {
1245 spa_t
*spa
= dn
->dn_objset
->os_spa
;
1248 * If the arc buf is compressed or encrypted, we need to
1249 * untransform it to read the data. This could happen during
1250 * the "zfs receive" of a stream which is deduplicated and
1251 * either raw or compressed. We do not need to do this if the
1252 * caller wants raw encrypted data.
1254 if (db
->db_buf
!= NULL
&& (flags
& DB_RF_NO_DECRYPT
) == 0 &&
1255 (arc_is_encrypted(db
->db_buf
) ||
1256 arc_get_compression(db
->db_buf
) != ZIO_COMPRESS_OFF
)) {
1257 dbuf_fix_old_data(db
, spa_syncing_txg(spa
));
1258 err
= arc_untransform(db
->db_buf
, spa
,
1259 dmu_objset_id(db
->db_objset
), B_FALSE
);
1260 dbuf_set_data(db
, db
->db_buf
);
1262 mutex_exit(&db
->db_mtx
);
1264 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
);
1265 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1266 rw_exit(&dn
->dn_struct_rwlock
);
1268 } else if (db
->db_state
== DB_UNCACHED
) {
1269 spa_t
*spa
= dn
->dn_objset
->os_spa
;
1270 boolean_t need_wait
= B_FALSE
;
1273 db
->db_blkptr
!= NULL
&& !BP_IS_HOLE(db
->db_blkptr
)) {
1274 zio
= zio_root(spa
, NULL
, NULL
, ZIO_FLAG_CANFAIL
);
1277 err
= dbuf_read_impl(db
, zio
, flags
);
1279 /* dbuf_read_impl has dropped db_mtx for us */
1281 if (!err
&& prefetch
)
1282 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
);
1284 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1285 rw_exit(&dn
->dn_struct_rwlock
);
1288 if (!err
&& need_wait
)
1289 err
= zio_wait(zio
);
1292 * Another reader came in while the dbuf was in flight
1293 * between UNCACHED and CACHED. Either a writer will finish
1294 * writing the buffer (sending the dbuf to CACHED) or the
1295 * first reader's request will reach the read_done callback
1296 * and send the dbuf to CACHED. Otherwise, a failure
1297 * occurred and the dbuf went to UNCACHED.
1299 mutex_exit(&db
->db_mtx
);
1301 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
);
1302 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1303 rw_exit(&dn
->dn_struct_rwlock
);
1306 /* Skip the wait per the caller's request. */
1307 mutex_enter(&db
->db_mtx
);
1308 if ((flags
& DB_RF_NEVERWAIT
) == 0) {
1309 while (db
->db_state
== DB_READ
||
1310 db
->db_state
== DB_FILL
) {
1311 ASSERT(db
->db_state
== DB_READ
||
1312 (flags
& DB_RF_HAVESTRUCT
) == 0);
1313 DTRACE_PROBE2(blocked__read
, dmu_buf_impl_t
*,
1315 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1317 if (db
->db_state
== DB_UNCACHED
)
1318 err
= SET_ERROR(EIO
);
1320 mutex_exit(&db
->db_mtx
);
1327 dbuf_noread(dmu_buf_impl_t
*db
)
1329 ASSERT(!refcount_is_zero(&db
->db_holds
));
1330 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1331 mutex_enter(&db
->db_mtx
);
1332 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
1333 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1334 if (db
->db_state
== DB_UNCACHED
) {
1335 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1336 spa_t
*spa
= db
->db_objset
->os_spa
;
1338 ASSERT(db
->db_buf
== NULL
);
1339 ASSERT(db
->db
.db_data
== NULL
);
1340 dbuf_set_data(db
, arc_alloc_buf(spa
, db
, type
, db
->db
.db_size
));
1341 db
->db_state
= DB_FILL
;
1342 } else if (db
->db_state
== DB_NOFILL
) {
1343 dbuf_clear_data(db
);
1345 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
1347 mutex_exit(&db
->db_mtx
);
1351 dbuf_unoverride(dbuf_dirty_record_t
*dr
)
1353 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
1354 blkptr_t
*bp
= &dr
->dt
.dl
.dr_overridden_by
;
1355 uint64_t txg
= dr
->dr_txg
;
1357 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1359 * This assert is valid because dmu_sync() expects to be called by
1360 * a zilog's get_data while holding a range lock. This call only
1361 * comes from dbuf_dirty() callers who must also hold a range lock.
1363 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_IN_DMU_SYNC
);
1364 ASSERT(db
->db_level
== 0);
1366 if (db
->db_blkid
== DMU_BONUS_BLKID
||
1367 dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
)
1370 ASSERT(db
->db_data_pending
!= dr
);
1372 /* free this block */
1373 if (!BP_IS_HOLE(bp
) && !dr
->dt
.dl
.dr_nopwrite
)
1374 zio_free(db
->db_objset
->os_spa
, txg
, bp
);
1376 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
1377 dr
->dt
.dl
.dr_nopwrite
= B_FALSE
;
1378 dr
->dt
.dl
.dr_raw
= B_FALSE
;
1381 * Release the already-written buffer, so we leave it in
1382 * a consistent dirty state. Note that all callers are
1383 * modifying the buffer, so they will immediately do
1384 * another (redundant) arc_release(). Therefore, leave
1385 * the buf thawed to save the effort of freezing &
1386 * immediately re-thawing it.
1388 arc_release(dr
->dt
.dl
.dr_data
, db
);
1392 * Evict (if its unreferenced) or clear (if its referenced) any level-0
1393 * data blocks in the free range, so that any future readers will find
1397 dbuf_free_range(dnode_t
*dn
, uint64_t start_blkid
, uint64_t end_blkid
,
1400 dmu_buf_impl_t
*db_search
;
1401 dmu_buf_impl_t
*db
, *db_next
;
1402 uint64_t txg
= tx
->tx_txg
;
1405 if (end_blkid
> dn
->dn_maxblkid
&&
1406 !(start_blkid
== DMU_SPILL_BLKID
|| end_blkid
== DMU_SPILL_BLKID
))
1407 end_blkid
= dn
->dn_maxblkid
;
1408 dprintf_dnode(dn
, "start=%llu end=%llu\n", start_blkid
, end_blkid
);
1410 db_search
= kmem_alloc(sizeof (dmu_buf_impl_t
), KM_SLEEP
);
1411 db_search
->db_level
= 0;
1412 db_search
->db_blkid
= start_blkid
;
1413 db_search
->db_state
= DB_SEARCH
;
1415 mutex_enter(&dn
->dn_dbufs_mtx
);
1416 db
= avl_find(&dn
->dn_dbufs
, db_search
, &where
);
1417 ASSERT3P(db
, ==, NULL
);
1419 db
= avl_nearest(&dn
->dn_dbufs
, where
, AVL_AFTER
);
1421 for (; db
!= NULL
; db
= db_next
) {
1422 db_next
= AVL_NEXT(&dn
->dn_dbufs
, db
);
1423 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1425 if (db
->db_level
!= 0 || db
->db_blkid
> end_blkid
) {
1428 ASSERT3U(db
->db_blkid
, >=, start_blkid
);
1430 /* found a level 0 buffer in the range */
1431 mutex_enter(&db
->db_mtx
);
1432 if (dbuf_undirty(db
, tx
)) {
1433 /* mutex has been dropped and dbuf destroyed */
1437 if (db
->db_state
== DB_UNCACHED
||
1438 db
->db_state
== DB_NOFILL
||
1439 db
->db_state
== DB_EVICTING
) {
1440 ASSERT(db
->db
.db_data
== NULL
);
1441 mutex_exit(&db
->db_mtx
);
1444 if (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
) {
1445 /* will be handled in dbuf_read_done or dbuf_rele */
1446 db
->db_freed_in_flight
= TRUE
;
1447 mutex_exit(&db
->db_mtx
);
1450 if (refcount_count(&db
->db_holds
) == 0) {
1455 /* The dbuf is referenced */
1457 if (db
->db_last_dirty
!= NULL
) {
1458 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
1460 if (dr
->dr_txg
== txg
) {
1462 * This buffer is "in-use", re-adjust the file
1463 * size to reflect that this buffer may
1464 * contain new data when we sync.
1466 if (db
->db_blkid
!= DMU_SPILL_BLKID
&&
1467 db
->db_blkid
> dn
->dn_maxblkid
)
1468 dn
->dn_maxblkid
= db
->db_blkid
;
1469 dbuf_unoverride(dr
);
1472 * This dbuf is not dirty in the open context.
1473 * Either uncache it (if its not referenced in
1474 * the open context) or reset its contents to
1477 dbuf_fix_old_data(db
, txg
);
1480 /* clear the contents if its cached */
1481 if (db
->db_state
== DB_CACHED
) {
1482 ASSERT(db
->db
.db_data
!= NULL
);
1483 arc_release(db
->db_buf
, db
);
1484 bzero(db
->db
.db_data
, db
->db
.db_size
);
1485 arc_buf_freeze(db
->db_buf
);
1488 mutex_exit(&db
->db_mtx
);
1491 kmem_free(db_search
, sizeof (dmu_buf_impl_t
));
1492 mutex_exit(&dn
->dn_dbufs_mtx
);
1496 dbuf_new_size(dmu_buf_impl_t
*db
, int size
, dmu_tx_t
*tx
)
1498 arc_buf_t
*buf
, *obuf
;
1499 int osize
= db
->db
.db_size
;
1500 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1503 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1508 /* XXX does *this* func really need the lock? */
1509 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
1512 * This call to dmu_buf_will_dirty() with the dn_struct_rwlock held
1513 * is OK, because there can be no other references to the db
1514 * when we are changing its size, so no concurrent DB_FILL can
1518 * XXX we should be doing a dbuf_read, checking the return
1519 * value and returning that up to our callers
1521 dmu_buf_will_dirty(&db
->db
, tx
);
1523 /* create the data buffer for the new block */
1524 buf
= arc_alloc_buf(dn
->dn_objset
->os_spa
, db
, type
, size
);
1526 /* copy old block data to the new block */
1528 bcopy(obuf
->b_data
, buf
->b_data
, MIN(osize
, size
));
1529 /* zero the remainder */
1531 bzero((uint8_t *)buf
->b_data
+ osize
, size
- osize
);
1533 mutex_enter(&db
->db_mtx
);
1534 dbuf_set_data(db
, buf
);
1535 arc_buf_destroy(obuf
, db
);
1536 db
->db
.db_size
= size
;
1538 if (db
->db_level
== 0) {
1539 ASSERT3U(db
->db_last_dirty
->dr_txg
, ==, tx
->tx_txg
);
1540 db
->db_last_dirty
->dt
.dl
.dr_data
= buf
;
1542 mutex_exit(&db
->db_mtx
);
1544 dmu_objset_willuse_space(dn
->dn_objset
, size
- osize
, tx
);
1549 dbuf_release_bp(dmu_buf_impl_t
*db
)
1551 ASSERTV(objset_t
*os
= db
->db_objset
);
1553 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os
)));
1554 ASSERT(arc_released(os
->os_phys_buf
) ||
1555 list_link_active(&os
->os_dsl_dataset
->ds_synced_link
));
1556 ASSERT(db
->db_parent
== NULL
|| arc_released(db
->db_parent
->db_buf
));
1558 (void) arc_release(db
->db_buf
, db
);
1562 * We already have a dirty record for this TXG, and we are being
1566 dbuf_redirty(dbuf_dirty_record_t
*dr
)
1568 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
1570 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1572 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
) {
1574 * If this buffer has already been written out,
1575 * we now need to reset its state.
1577 dbuf_unoverride(dr
);
1578 if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
&&
1579 db
->db_state
!= DB_NOFILL
) {
1580 /* Already released on initial dirty, so just thaw. */
1581 ASSERT(arc_released(db
->db_buf
));
1582 arc_buf_thaw(db
->db_buf
);
1587 dbuf_dirty_record_t
*
1588 dbuf_dirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1592 dbuf_dirty_record_t
**drp
, *dr
;
1593 int drop_struct_lock
= FALSE
;
1594 int txgoff
= tx
->tx_txg
& TXG_MASK
;
1596 ASSERT(tx
->tx_txg
!= 0);
1597 ASSERT(!refcount_is_zero(&db
->db_holds
));
1598 DMU_TX_DIRTY_BUF(tx
, db
);
1603 * Shouldn't dirty a regular buffer in syncing context. Private
1604 * objects may be dirtied in syncing context, but only if they
1605 * were already pre-dirtied in open context.
1608 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
) {
1609 rrw_enter(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
,
1612 ASSERT(!dmu_tx_is_syncing(tx
) ||
1613 BP_IS_HOLE(dn
->dn_objset
->os_rootbp
) ||
1614 DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
1615 dn
->dn_objset
->os_dsl_dataset
== NULL
);
1616 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
1617 rrw_exit(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
, FTAG
);
1620 * We make this assert for private objects as well, but after we
1621 * check if we're already dirty. They are allowed to re-dirty
1622 * in syncing context.
1624 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
1625 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
1626 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
1628 mutex_enter(&db
->db_mtx
);
1630 * XXX make this true for indirects too? The problem is that
1631 * transactions created with dmu_tx_create_assigned() from
1632 * syncing context don't bother holding ahead.
1634 ASSERT(db
->db_level
!= 0 ||
1635 db
->db_state
== DB_CACHED
|| db
->db_state
== DB_FILL
||
1636 db
->db_state
== DB_NOFILL
);
1638 mutex_enter(&dn
->dn_mtx
);
1640 * Don't set dirtyctx to SYNC if we're just modifying this as we
1641 * initialize the objset.
1643 if (dn
->dn_dirtyctx
== DN_UNDIRTIED
) {
1644 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
) {
1645 rrw_enter(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
,
1648 if (!BP_IS_HOLE(dn
->dn_objset
->os_rootbp
)) {
1649 dn
->dn_dirtyctx
= (dmu_tx_is_syncing(tx
) ?
1650 DN_DIRTY_SYNC
: DN_DIRTY_OPEN
);
1651 ASSERT(dn
->dn_dirtyctx_firstset
== NULL
);
1652 dn
->dn_dirtyctx_firstset
= kmem_alloc(1, KM_SLEEP
);
1654 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
) {
1655 rrw_exit(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
,
1659 mutex_exit(&dn
->dn_mtx
);
1661 if (db
->db_blkid
== DMU_SPILL_BLKID
)
1662 dn
->dn_have_spill
= B_TRUE
;
1665 * If this buffer is already dirty, we're done.
1667 drp
= &db
->db_last_dirty
;
1668 ASSERT(*drp
== NULL
|| (*drp
)->dr_txg
<= tx
->tx_txg
||
1669 db
->db
.db_object
== DMU_META_DNODE_OBJECT
);
1670 while ((dr
= *drp
) != NULL
&& dr
->dr_txg
> tx
->tx_txg
)
1672 if (dr
&& dr
->dr_txg
== tx
->tx_txg
) {
1676 mutex_exit(&db
->db_mtx
);
1681 * Only valid if not already dirty.
1683 ASSERT(dn
->dn_object
== 0 ||
1684 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
1685 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
1687 ASSERT3U(dn
->dn_nlevels
, >, db
->db_level
);
1690 * We should only be dirtying in syncing context if it's the
1691 * mos or we're initializing the os or it's a special object.
1692 * However, we are allowed to dirty in syncing context provided
1693 * we already dirtied it in open context. Hence we must make
1694 * this assertion only if we're not already dirty.
1697 VERIFY3U(tx
->tx_txg
, <=, spa_final_dirty_txg(os
->os_spa
));
1699 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
1700 rrw_enter(&os
->os_dsl_dataset
->ds_bp_rwlock
, RW_READER
, FTAG
);
1701 ASSERT(!dmu_tx_is_syncing(tx
) || DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
1702 os
->os_dsl_dataset
== NULL
|| BP_IS_HOLE(os
->os_rootbp
));
1703 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
1704 rrw_exit(&os
->os_dsl_dataset
->ds_bp_rwlock
, FTAG
);
1706 ASSERT(db
->db
.db_size
!= 0);
1708 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
1710 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
1711 dmu_objset_willuse_space(os
, db
->db
.db_size
, tx
);
1715 * If this buffer is dirty in an old transaction group we need
1716 * to make a copy of it so that the changes we make in this
1717 * transaction group won't leak out when we sync the older txg.
1719 dr
= kmem_zalloc(sizeof (dbuf_dirty_record_t
), KM_SLEEP
);
1720 list_link_init(&dr
->dr_dirty_node
);
1721 if (db
->db_level
== 0) {
1722 void *data_old
= db
->db_buf
;
1724 if (db
->db_state
!= DB_NOFILL
) {
1725 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1726 dbuf_fix_old_data(db
, tx
->tx_txg
);
1727 data_old
= db
->db
.db_data
;
1728 } else if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
) {
1730 * Release the data buffer from the cache so
1731 * that we can modify it without impacting
1732 * possible other users of this cached data
1733 * block. Note that indirect blocks and
1734 * private objects are not released until the
1735 * syncing state (since they are only modified
1738 arc_release(db
->db_buf
, db
);
1739 dbuf_fix_old_data(db
, tx
->tx_txg
);
1740 data_old
= db
->db_buf
;
1742 ASSERT(data_old
!= NULL
);
1744 dr
->dt
.dl
.dr_data
= data_old
;
1746 mutex_init(&dr
->dt
.di
.dr_mtx
, NULL
, MUTEX_NOLOCKDEP
, NULL
);
1747 list_create(&dr
->dt
.di
.dr_children
,
1748 sizeof (dbuf_dirty_record_t
),
1749 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
1751 if (db
->db_blkid
!= DMU_BONUS_BLKID
&& os
->os_dsl_dataset
!= NULL
)
1752 dr
->dr_accounted
= db
->db
.db_size
;
1754 dr
->dr_txg
= tx
->tx_txg
;
1759 * We could have been freed_in_flight between the dbuf_noread
1760 * and dbuf_dirty. We win, as though the dbuf_noread() had
1761 * happened after the free.
1763 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1764 db
->db_blkid
!= DMU_SPILL_BLKID
) {
1765 mutex_enter(&dn
->dn_mtx
);
1766 if (dn
->dn_free_ranges
[txgoff
] != NULL
) {
1767 range_tree_clear(dn
->dn_free_ranges
[txgoff
],
1770 mutex_exit(&dn
->dn_mtx
);
1771 db
->db_freed_in_flight
= FALSE
;
1775 * This buffer is now part of this txg
1777 dbuf_add_ref(db
, (void *)(uintptr_t)tx
->tx_txg
);
1778 db
->db_dirtycnt
+= 1;
1779 ASSERT3U(db
->db_dirtycnt
, <=, 3);
1781 mutex_exit(&db
->db_mtx
);
1783 if (db
->db_blkid
== DMU_BONUS_BLKID
||
1784 db
->db_blkid
== DMU_SPILL_BLKID
) {
1785 mutex_enter(&dn
->dn_mtx
);
1786 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1787 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
1788 mutex_exit(&dn
->dn_mtx
);
1789 dnode_setdirty(dn
, tx
);
1795 * The dn_struct_rwlock prevents db_blkptr from changing
1796 * due to a write from syncing context completing
1797 * while we are running, so we want to acquire it before
1798 * looking at db_blkptr.
1800 if (!RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
1801 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
1802 drop_struct_lock
= TRUE
;
1806 * We need to hold the dn_struct_rwlock to make this assertion,
1807 * because it protects dn_phys / dn_next_nlevels from changing.
1809 ASSERT((dn
->dn_phys
->dn_nlevels
== 0 && db
->db_level
== 0) ||
1810 dn
->dn_phys
->dn_nlevels
> db
->db_level
||
1811 dn
->dn_next_nlevels
[txgoff
] > db
->db_level
||
1812 dn
->dn_next_nlevels
[(tx
->tx_txg
-1) & TXG_MASK
] > db
->db_level
||
1813 dn
->dn_next_nlevels
[(tx
->tx_txg
-2) & TXG_MASK
] > db
->db_level
);
1816 * If we are overwriting a dedup BP, then unless it is snapshotted,
1817 * when we get to syncing context we will need to decrement its
1818 * refcount in the DDT. Prefetch the relevant DDT block so that
1819 * syncing context won't have to wait for the i/o.
1821 ddt_prefetch(os
->os_spa
, db
->db_blkptr
);
1823 if (db
->db_level
== 0) {
1824 dnode_new_blkid(dn
, db
->db_blkid
, tx
, drop_struct_lock
);
1825 ASSERT(dn
->dn_maxblkid
>= db
->db_blkid
);
1828 if (db
->db_level
+1 < dn
->dn_nlevels
) {
1829 dmu_buf_impl_t
*parent
= db
->db_parent
;
1830 dbuf_dirty_record_t
*di
;
1831 int parent_held
= FALSE
;
1833 if (db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
) {
1834 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1836 parent
= dbuf_hold_level(dn
, db
->db_level
+1,
1837 db
->db_blkid
>> epbs
, FTAG
);
1838 ASSERT(parent
!= NULL
);
1841 if (drop_struct_lock
)
1842 rw_exit(&dn
->dn_struct_rwlock
);
1843 ASSERT3U(db
->db_level
+1, ==, parent
->db_level
);
1844 di
= dbuf_dirty(parent
, tx
);
1846 dbuf_rele(parent
, FTAG
);
1848 mutex_enter(&db
->db_mtx
);
1850 * Since we've dropped the mutex, it's possible that
1851 * dbuf_undirty() might have changed this out from under us.
1853 if (db
->db_last_dirty
== dr
||
1854 dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
1855 mutex_enter(&di
->dt
.di
.dr_mtx
);
1856 ASSERT3U(di
->dr_txg
, ==, tx
->tx_txg
);
1857 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1858 list_insert_tail(&di
->dt
.di
.dr_children
, dr
);
1859 mutex_exit(&di
->dt
.di
.dr_mtx
);
1862 mutex_exit(&db
->db_mtx
);
1864 ASSERT(db
->db_level
+1 == dn
->dn_nlevels
);
1865 ASSERT(db
->db_blkid
< dn
->dn_nblkptr
);
1866 ASSERT(db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
1867 mutex_enter(&dn
->dn_mtx
);
1868 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1869 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
1870 mutex_exit(&dn
->dn_mtx
);
1871 if (drop_struct_lock
)
1872 rw_exit(&dn
->dn_struct_rwlock
);
1875 dnode_setdirty(dn
, tx
);
1881 * Undirty a buffer in the transaction group referenced by the given
1882 * transaction. Return whether this evicted the dbuf.
1885 dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1888 uint64_t txg
= tx
->tx_txg
;
1889 dbuf_dirty_record_t
*dr
, **drp
;
1894 * Due to our use of dn_nlevels below, this can only be called
1895 * in open context, unless we are operating on the MOS.
1896 * From syncing context, dn_nlevels may be different from the
1897 * dn_nlevels used when dbuf was dirtied.
1899 ASSERT(db
->db_objset
==
1900 dmu_objset_pool(db
->db_objset
)->dp_meta_objset
||
1901 txg
!= spa_syncing_txg(dmu_objset_spa(db
->db_objset
)));
1902 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1903 ASSERT0(db
->db_level
);
1904 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1907 * If this buffer is not dirty, we're done.
1909 for (drp
= &db
->db_last_dirty
; (dr
= *drp
) != NULL
; drp
= &dr
->dr_next
)
1910 if (dr
->dr_txg
<= txg
)
1912 if (dr
== NULL
|| dr
->dr_txg
< txg
)
1914 ASSERT(dr
->dr_txg
== txg
);
1915 ASSERT(dr
->dr_dbuf
== db
);
1920 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
1922 ASSERT(db
->db
.db_size
!= 0);
1924 dsl_pool_undirty_space(dmu_objset_pool(dn
->dn_objset
),
1925 dr
->dr_accounted
, txg
);
1930 * Note that there are three places in dbuf_dirty()
1931 * where this dirty record may be put on a list.
1932 * Make sure to do a list_remove corresponding to
1933 * every one of those list_insert calls.
1935 if (dr
->dr_parent
) {
1936 mutex_enter(&dr
->dr_parent
->dt
.di
.dr_mtx
);
1937 list_remove(&dr
->dr_parent
->dt
.di
.dr_children
, dr
);
1938 mutex_exit(&dr
->dr_parent
->dt
.di
.dr_mtx
);
1939 } else if (db
->db_blkid
== DMU_SPILL_BLKID
||
1940 db
->db_level
+ 1 == dn
->dn_nlevels
) {
1941 ASSERT(db
->db_blkptr
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
1942 mutex_enter(&dn
->dn_mtx
);
1943 list_remove(&dn
->dn_dirty_records
[txg
& TXG_MASK
], dr
);
1944 mutex_exit(&dn
->dn_mtx
);
1948 if (db
->db_state
!= DB_NOFILL
) {
1949 dbuf_unoverride(dr
);
1951 ASSERT(db
->db_buf
!= NULL
);
1952 ASSERT(dr
->dt
.dl
.dr_data
!= NULL
);
1953 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
1954 arc_buf_destroy(dr
->dt
.dl
.dr_data
, db
);
1957 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
1959 ASSERT(db
->db_dirtycnt
> 0);
1960 db
->db_dirtycnt
-= 1;
1962 if (refcount_remove(&db
->db_holds
, (void *)(uintptr_t)txg
) == 0) {
1963 ASSERT(db
->db_state
== DB_NOFILL
|| arc_released(db
->db_buf
));
1972 dmu_buf_will_dirty_impl(dmu_buf_t
*db_fake
, int flags
, dmu_tx_t
*tx
)
1974 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1976 ASSERT(tx
->tx_txg
!= 0);
1977 ASSERT(!refcount_is_zero(&db
->db_holds
));
1980 * Quick check for dirtyness. For already dirty blocks, this
1981 * reduces runtime of this function by >90%, and overall performance
1982 * by 50% for some workloads (e.g. file deletion with indirect blocks
1985 mutex_enter(&db
->db_mtx
);
1987 dbuf_dirty_record_t
*dr
;
1988 for (dr
= db
->db_last_dirty
;
1989 dr
!= NULL
&& dr
->dr_txg
>= tx
->tx_txg
; dr
= dr
->dr_next
) {
1991 * It's possible that it is already dirty but not cached,
1992 * because there are some calls to dbuf_dirty() that don't
1993 * go through dmu_buf_will_dirty().
1995 if (dr
->dr_txg
== tx
->tx_txg
&& db
->db_state
== DB_CACHED
) {
1996 /* This dbuf is already dirty and cached. */
1998 mutex_exit(&db
->db_mtx
);
2002 mutex_exit(&db
->db_mtx
);
2005 if (RW_WRITE_HELD(&DB_DNODE(db
)->dn_struct_rwlock
))
2006 flags
|= DB_RF_HAVESTRUCT
;
2008 (void) dbuf_read(db
, NULL
, flags
);
2009 (void) dbuf_dirty(db
, tx
);
2013 dmu_buf_will_dirty(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
2015 dmu_buf_will_dirty_impl(db_fake
,
2016 DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
, tx
);
2020 dmu_buf_will_not_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
2022 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2024 db
->db_state
= DB_NOFILL
;
2026 dmu_buf_will_fill(db_fake
, tx
);
2030 dmu_buf_will_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
2032 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2034 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2035 ASSERT(tx
->tx_txg
!= 0);
2036 ASSERT(db
->db_level
== 0);
2037 ASSERT(!refcount_is_zero(&db
->db_holds
));
2039 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
||
2040 dmu_tx_private_ok(tx
));
2043 (void) dbuf_dirty(db
, tx
);
2047 * This function is effectively the same as dmu_buf_will_dirty(), but
2048 * indicates the caller expects raw encrypted data in the db. It will
2049 * also set the raw flag on the created dirty record.
2052 dmu_buf_will_change_crypt_params(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
2054 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2055 dbuf_dirty_record_t
*dr
;
2057 dmu_buf_will_dirty_impl(db_fake
,
2058 DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
| DB_RF_NO_DECRYPT
, tx
);
2060 dr
= db
->db_last_dirty
;
2061 while (dr
!= NULL
&& dr
->dr_txg
> tx
->tx_txg
)
2064 ASSERT3P(dr
, !=, NULL
);
2065 ASSERT3U(dr
->dr_txg
, ==, tx
->tx_txg
);
2066 dr
->dt
.dl
.dr_raw
= B_TRUE
;
2069 #pragma weak dmu_buf_fill_done = dbuf_fill_done
2072 dbuf_fill_done(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
2074 mutex_enter(&db
->db_mtx
);
2077 if (db
->db_state
== DB_FILL
) {
2078 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
2079 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2080 /* we were freed while filling */
2081 /* XXX dbuf_undirty? */
2082 bzero(db
->db
.db_data
, db
->db
.db_size
);
2083 db
->db_freed_in_flight
= FALSE
;
2085 db
->db_state
= DB_CACHED
;
2086 cv_broadcast(&db
->db_changed
);
2088 mutex_exit(&db
->db_mtx
);
2092 dmu_buf_write_embedded(dmu_buf_t
*dbuf
, void *data
,
2093 bp_embedded_type_t etype
, enum zio_compress comp
,
2094 int uncompressed_size
, int compressed_size
, int byteorder
,
2097 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbuf
;
2098 struct dirty_leaf
*dl
;
2099 dmu_object_type_t type
;
2101 if (etype
== BP_EMBEDDED_TYPE_DATA
) {
2102 ASSERT(spa_feature_is_active(dmu_objset_spa(db
->db_objset
),
2103 SPA_FEATURE_EMBEDDED_DATA
));
2107 type
= DB_DNODE(db
)->dn_type
;
2110 ASSERT0(db
->db_level
);
2111 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2113 dmu_buf_will_not_fill(dbuf
, tx
);
2115 ASSERT3U(db
->db_last_dirty
->dr_txg
, ==, tx
->tx_txg
);
2116 dl
= &db
->db_last_dirty
->dt
.dl
;
2117 encode_embedded_bp_compressed(&dl
->dr_overridden_by
,
2118 data
, comp
, uncompressed_size
, compressed_size
);
2119 BPE_SET_ETYPE(&dl
->dr_overridden_by
, etype
);
2120 BP_SET_TYPE(&dl
->dr_overridden_by
, type
);
2121 BP_SET_LEVEL(&dl
->dr_overridden_by
, 0);
2122 BP_SET_BYTEORDER(&dl
->dr_overridden_by
, byteorder
);
2124 dl
->dr_override_state
= DR_OVERRIDDEN
;
2125 dl
->dr_overridden_by
.blk_birth
= db
->db_last_dirty
->dr_txg
;
2129 * Directly assign a provided arc buf to a given dbuf if it's not referenced
2130 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
2133 dbuf_assign_arcbuf(dmu_buf_impl_t
*db
, arc_buf_t
*buf
, dmu_tx_t
*tx
)
2135 ASSERT(!refcount_is_zero(&db
->db_holds
));
2136 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2137 ASSERT(db
->db_level
== 0);
2138 ASSERT3U(dbuf_is_metadata(db
), ==, arc_is_metadata(buf
));
2139 ASSERT(buf
!= NULL
);
2140 ASSERT(arc_buf_lsize(buf
) == db
->db
.db_size
);
2141 ASSERT(tx
->tx_txg
!= 0);
2143 arc_return_buf(buf
, db
);
2144 ASSERT(arc_released(buf
));
2146 mutex_enter(&db
->db_mtx
);
2148 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
2149 cv_wait(&db
->db_changed
, &db
->db_mtx
);
2151 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_UNCACHED
);
2153 if (db
->db_state
== DB_CACHED
&&
2154 refcount_count(&db
->db_holds
) - 1 > db
->db_dirtycnt
) {
2156 * In practice, we will never have a case where we have an
2157 * encrypted arc buffer while additional holds exist on the
2158 * dbuf. We don't handle this here so we simply assert that
2161 ASSERT(!arc_is_encrypted(buf
));
2162 mutex_exit(&db
->db_mtx
);
2163 (void) dbuf_dirty(db
, tx
);
2164 bcopy(buf
->b_data
, db
->db
.db_data
, db
->db
.db_size
);
2165 arc_buf_destroy(buf
, db
);
2166 xuio_stat_wbuf_copied();
2170 xuio_stat_wbuf_nocopy();
2171 if (db
->db_state
== DB_CACHED
) {
2172 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
2174 ASSERT(db
->db_buf
!= NULL
);
2175 if (dr
!= NULL
&& dr
->dr_txg
== tx
->tx_txg
) {
2176 ASSERT(dr
->dt
.dl
.dr_data
== db
->db_buf
);
2177 IMPLY(arc_is_encrypted(buf
), dr
->dt
.dl
.dr_raw
);
2179 if (!arc_released(db
->db_buf
)) {
2180 ASSERT(dr
->dt
.dl
.dr_override_state
==
2182 arc_release(db
->db_buf
, db
);
2184 dr
->dt
.dl
.dr_data
= buf
;
2185 arc_buf_destroy(db
->db_buf
, db
);
2186 } else if (dr
== NULL
|| dr
->dt
.dl
.dr_data
!= db
->db_buf
) {
2187 arc_release(db
->db_buf
, db
);
2188 arc_buf_destroy(db
->db_buf
, db
);
2192 ASSERT(db
->db_buf
== NULL
);
2193 dbuf_set_data(db
, buf
);
2194 db
->db_state
= DB_FILL
;
2195 mutex_exit(&db
->db_mtx
);
2196 (void) dbuf_dirty(db
, tx
);
2197 dmu_buf_fill_done(&db
->db
, tx
);
2201 dbuf_destroy(dmu_buf_impl_t
*db
)
2204 dmu_buf_impl_t
*parent
= db
->db_parent
;
2205 dmu_buf_impl_t
*dndb
;
2207 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2208 ASSERT(refcount_is_zero(&db
->db_holds
));
2210 if (db
->db_buf
!= NULL
) {
2211 arc_buf_destroy(db
->db_buf
, db
);
2215 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2216 int slots
= DB_DNODE(db
)->dn_num_slots
;
2217 int bonuslen
= DN_SLOTS_TO_BONUSLEN(slots
);
2218 if (db
->db
.db_data
!= NULL
) {
2219 kmem_free(db
->db
.db_data
, bonuslen
);
2220 arc_space_return(bonuslen
, ARC_SPACE_BONUS
);
2221 db
->db_state
= DB_UNCACHED
;
2225 dbuf_clear_data(db
);
2227 if (multilist_link_active(&db
->db_cache_link
)) {
2228 multilist_remove(dbuf_cache
, db
);
2229 (void) refcount_remove_many(&dbuf_cache_size
,
2230 db
->db
.db_size
, db
);
2233 ASSERT(db
->db_state
== DB_UNCACHED
|| db
->db_state
== DB_NOFILL
);
2234 ASSERT(db
->db_data_pending
== NULL
);
2236 db
->db_state
= DB_EVICTING
;
2237 db
->db_blkptr
= NULL
;
2240 * Now that db_state is DB_EVICTING, nobody else can find this via
2241 * the hash table. We can now drop db_mtx, which allows us to
2242 * acquire the dn_dbufs_mtx.
2244 mutex_exit(&db
->db_mtx
);
2249 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
2250 boolean_t needlock
= !MUTEX_HELD(&dn
->dn_dbufs_mtx
);
2252 mutex_enter(&dn
->dn_dbufs_mtx
);
2253 avl_remove(&dn
->dn_dbufs
, db
);
2254 atomic_dec_32(&dn
->dn_dbufs_count
);
2258 mutex_exit(&dn
->dn_dbufs_mtx
);
2260 * Decrementing the dbuf count means that the hold corresponding
2261 * to the removed dbuf is no longer discounted in dnode_move(),
2262 * so the dnode cannot be moved until after we release the hold.
2263 * The membar_producer() ensures visibility of the decremented
2264 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
2268 db
->db_dnode_handle
= NULL
;
2270 dbuf_hash_remove(db
);
2275 ASSERT(refcount_is_zero(&db
->db_holds
));
2277 db
->db_parent
= NULL
;
2279 ASSERT(db
->db_buf
== NULL
);
2280 ASSERT(db
->db
.db_data
== NULL
);
2281 ASSERT(db
->db_hash_next
== NULL
);
2282 ASSERT(db
->db_blkptr
== NULL
);
2283 ASSERT(db
->db_data_pending
== NULL
);
2284 ASSERT(!multilist_link_active(&db
->db_cache_link
));
2286 kmem_cache_free(dbuf_kmem_cache
, db
);
2287 arc_space_return(sizeof (dmu_buf_impl_t
), ARC_SPACE_DBUF
);
2290 * If this dbuf is referenced from an indirect dbuf,
2291 * decrement the ref count on the indirect dbuf.
2293 if (parent
&& parent
!= dndb
)
2294 dbuf_rele(parent
, db
);
2298 * Note: While bpp will always be updated if the function returns success,
2299 * parentp will not be updated if the dnode does not have dn_dbuf filled in;
2300 * this happens when the dnode is the meta-dnode, or a userused or groupused
2303 __attribute__((always_inline
))
2305 dbuf_findbp(dnode_t
*dn
, int level
, uint64_t blkid
, int fail_sparse
,
2306 dmu_buf_impl_t
**parentp
, blkptr_t
**bpp
, struct dbuf_hold_impl_data
*dh
)
2311 ASSERT(blkid
!= DMU_BONUS_BLKID
);
2313 if (blkid
== DMU_SPILL_BLKID
) {
2314 mutex_enter(&dn
->dn_mtx
);
2315 if (dn
->dn_have_spill
&&
2316 (dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
))
2317 *bpp
= DN_SPILL_BLKPTR(dn
->dn_phys
);
2320 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
2321 *parentp
= dn
->dn_dbuf
;
2322 mutex_exit(&dn
->dn_mtx
);
2327 (dn
->dn_phys
->dn_nlevels
== 0) ? 1 : dn
->dn_phys
->dn_nlevels
;
2328 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2330 ASSERT3U(level
* epbs
, <, 64);
2331 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2333 * This assertion shouldn't trip as long as the max indirect block size
2334 * is less than 1M. The reason for this is that up to that point,
2335 * the number of levels required to address an entire object with blocks
2336 * of size SPA_MINBLOCKSIZE satisfies nlevels * epbs + 1 <= 64. In
2337 * other words, if N * epbs + 1 > 64, then if (N-1) * epbs + 1 > 55
2338 * (i.e. we can address the entire object), objects will all use at most
2339 * N-1 levels and the assertion won't overflow. However, once epbs is
2340 * 13, 4 * 13 + 1 = 53, but 5 * 13 + 1 = 66. Then, 4 levels will not be
2341 * enough to address an entire object, so objects will have 5 levels,
2342 * but then this assertion will overflow.
2344 * All this is to say that if we ever increase DN_MAX_INDBLKSHIFT, we
2345 * need to redo this logic to handle overflows.
2347 ASSERT(level
>= nlevels
||
2348 ((nlevels
- level
- 1) * epbs
) +
2349 highbit64(dn
->dn_phys
->dn_nblkptr
) <= 64);
2350 if (level
>= nlevels
||
2351 blkid
>= ((uint64_t)dn
->dn_phys
->dn_nblkptr
<<
2352 ((nlevels
- level
- 1) * epbs
)) ||
2354 blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))) {
2355 /* the buffer has no parent yet */
2356 return (SET_ERROR(ENOENT
));
2357 } else if (level
< nlevels
-1) {
2358 /* this block is referenced from an indirect block */
2361 err
= dbuf_hold_impl(dn
, level
+1,
2362 blkid
>> epbs
, fail_sparse
, FALSE
, NULL
, parentp
);
2364 __dbuf_hold_impl_init(dh
+ 1, dn
, dh
->dh_level
+ 1,
2365 blkid
>> epbs
, fail_sparse
, FALSE
, NULL
,
2366 parentp
, dh
->dh_depth
+ 1);
2367 err
= __dbuf_hold_impl(dh
+ 1);
2371 err
= dbuf_read(*parentp
, NULL
,
2372 (DB_RF_HAVESTRUCT
| DB_RF_NOPREFETCH
| DB_RF_CANFAIL
));
2374 dbuf_rele(*parentp
, NULL
);
2378 *bpp
= ((blkptr_t
*)(*parentp
)->db
.db_data
) +
2379 (blkid
& ((1ULL << epbs
) - 1));
2380 if (blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))
2381 ASSERT(BP_IS_HOLE(*bpp
));
2384 /* the block is referenced from the dnode */
2385 ASSERT3U(level
, ==, nlevels
-1);
2386 ASSERT(dn
->dn_phys
->dn_nblkptr
== 0 ||
2387 blkid
< dn
->dn_phys
->dn_nblkptr
);
2389 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
2390 *parentp
= dn
->dn_dbuf
;
2392 *bpp
= &dn
->dn_phys
->dn_blkptr
[blkid
];
2397 static dmu_buf_impl_t
*
2398 dbuf_create(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
2399 dmu_buf_impl_t
*parent
, blkptr_t
*blkptr
)
2401 objset_t
*os
= dn
->dn_objset
;
2402 dmu_buf_impl_t
*db
, *odb
;
2404 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2405 ASSERT(dn
->dn_type
!= DMU_OT_NONE
);
2407 db
= kmem_cache_alloc(dbuf_kmem_cache
, KM_SLEEP
);
2410 db
->db
.db_object
= dn
->dn_object
;
2411 db
->db_level
= level
;
2412 db
->db_blkid
= blkid
;
2413 db
->db_last_dirty
= NULL
;
2414 db
->db_dirtycnt
= 0;
2415 db
->db_dnode_handle
= dn
->dn_handle
;
2416 db
->db_parent
= parent
;
2417 db
->db_blkptr
= blkptr
;
2420 db
->db_user_immediate_evict
= FALSE
;
2421 db
->db_freed_in_flight
= FALSE
;
2422 db
->db_pending_evict
= FALSE
;
2424 if (blkid
== DMU_BONUS_BLKID
) {
2425 ASSERT3P(parent
, ==, dn
->dn_dbuf
);
2426 db
->db
.db_size
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
) -
2427 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
);
2428 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
2429 db
->db
.db_offset
= DMU_BONUS_BLKID
;
2430 db
->db_state
= DB_UNCACHED
;
2431 /* the bonus dbuf is not placed in the hash table */
2432 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_DBUF
);
2434 } else if (blkid
== DMU_SPILL_BLKID
) {
2435 db
->db
.db_size
= (blkptr
!= NULL
) ?
2436 BP_GET_LSIZE(blkptr
) : SPA_MINBLOCKSIZE
;
2437 db
->db
.db_offset
= 0;
2440 db
->db_level
? 1 << dn
->dn_indblkshift
: dn
->dn_datablksz
;
2441 db
->db
.db_size
= blocksize
;
2442 db
->db
.db_offset
= db
->db_blkid
* blocksize
;
2446 * Hold the dn_dbufs_mtx while we get the new dbuf
2447 * in the hash table *and* added to the dbufs list.
2448 * This prevents a possible deadlock with someone
2449 * trying to look up this dbuf before its added to the
2452 mutex_enter(&dn
->dn_dbufs_mtx
);
2453 db
->db_state
= DB_EVICTING
;
2454 if ((odb
= dbuf_hash_insert(db
)) != NULL
) {
2455 /* someone else inserted it first */
2456 kmem_cache_free(dbuf_kmem_cache
, db
);
2457 mutex_exit(&dn
->dn_dbufs_mtx
);
2460 avl_add(&dn
->dn_dbufs
, db
);
2462 db
->db_state
= DB_UNCACHED
;
2463 mutex_exit(&dn
->dn_dbufs_mtx
);
2464 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_DBUF
);
2466 if (parent
&& parent
!= dn
->dn_dbuf
)
2467 dbuf_add_ref(parent
, db
);
2469 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
2470 refcount_count(&dn
->dn_holds
) > 0);
2471 (void) refcount_add(&dn
->dn_holds
, db
);
2472 atomic_inc_32(&dn
->dn_dbufs_count
);
2474 dprintf_dbuf(db
, "db=%p\n", db
);
2479 typedef struct dbuf_prefetch_arg
{
2480 spa_t
*dpa_spa
; /* The spa to issue the prefetch in. */
2481 zbookmark_phys_t dpa_zb
; /* The target block to prefetch. */
2482 int dpa_epbs
; /* Entries (blkptr_t's) Per Block Shift. */
2483 int dpa_curlevel
; /* The current level that we're reading */
2484 dnode_t
*dpa_dnode
; /* The dnode associated with the prefetch */
2485 zio_priority_t dpa_prio
; /* The priority I/Os should be issued at. */
2486 zio_t
*dpa_zio
; /* The parent zio_t for all prefetches. */
2487 arc_flags_t dpa_aflags
; /* Flags to pass to the final prefetch. */
2488 } dbuf_prefetch_arg_t
;
2491 * Actually issue the prefetch read for the block given.
2494 dbuf_issue_final_prefetch(dbuf_prefetch_arg_t
*dpa
, blkptr_t
*bp
)
2496 if (BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
))
2499 arc_flags_t aflags
=
2500 dpa
->dpa_aflags
| ARC_FLAG_NOWAIT
| ARC_FLAG_PREFETCH
;
2502 ASSERT3U(dpa
->dpa_curlevel
, ==, BP_GET_LEVEL(bp
));
2503 ASSERT3U(dpa
->dpa_curlevel
, ==, dpa
->dpa_zb
.zb_level
);
2504 ASSERT(dpa
->dpa_zio
!= NULL
);
2505 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
, bp
, NULL
, NULL
,
2506 dpa
->dpa_prio
, ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
2507 &aflags
, &dpa
->dpa_zb
);
2511 * Called when an indirect block above our prefetch target is read in. This
2512 * will either read in the next indirect block down the tree or issue the actual
2513 * prefetch if the next block down is our target.
2516 dbuf_prefetch_indirect_done(zio_t
*zio
, int err
, arc_buf_t
*abuf
, void *private)
2518 dbuf_prefetch_arg_t
*dpa
= private;
2520 ASSERT3S(dpa
->dpa_zb
.zb_level
, <, dpa
->dpa_curlevel
);
2521 ASSERT3S(dpa
->dpa_curlevel
, >, 0);
2524 * The dpa_dnode is only valid if we are called with a NULL
2525 * zio. This indicates that the arc_read() returned without
2526 * first calling zio_read() to issue a physical read. Once
2527 * a physical read is made the dpa_dnode must be invalidated
2528 * as the locks guarding it may have been dropped. If the
2529 * dpa_dnode is still valid, then we want to add it to the dbuf
2530 * cache. To do so, we must hold the dbuf associated with the block
2531 * we just prefetched, read its contents so that we associate it
2532 * with an arc_buf_t, and then release it.
2535 ASSERT3S(BP_GET_LEVEL(zio
->io_bp
), ==, dpa
->dpa_curlevel
);
2536 if (zio
->io_flags
& ZIO_FLAG_RAW_COMPRESS
) {
2537 ASSERT3U(BP_GET_PSIZE(zio
->io_bp
), ==, zio
->io_size
);
2539 ASSERT3U(BP_GET_LSIZE(zio
->io_bp
), ==, zio
->io_size
);
2541 ASSERT3P(zio
->io_spa
, ==, dpa
->dpa_spa
);
2543 dpa
->dpa_dnode
= NULL
;
2544 } else if (dpa
->dpa_dnode
!= NULL
) {
2545 uint64_t curblkid
= dpa
->dpa_zb
.zb_blkid
>>
2546 (dpa
->dpa_epbs
* (dpa
->dpa_curlevel
-
2547 dpa
->dpa_zb
.zb_level
));
2548 dmu_buf_impl_t
*db
= dbuf_hold_level(dpa
->dpa_dnode
,
2549 dpa
->dpa_curlevel
, curblkid
, FTAG
);
2550 (void) dbuf_read(db
, NULL
,
2551 DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
| DB_RF_HAVESTRUCT
);
2552 dbuf_rele(db
, FTAG
);
2555 dpa
->dpa_curlevel
--;
2557 uint64_t nextblkid
= dpa
->dpa_zb
.zb_blkid
>>
2558 (dpa
->dpa_epbs
* (dpa
->dpa_curlevel
- dpa
->dpa_zb
.zb_level
));
2559 blkptr_t
*bp
= ((blkptr_t
*)abuf
->b_data
) +
2560 P2PHASE(nextblkid
, 1ULL << dpa
->dpa_epbs
);
2561 if (BP_IS_HOLE(bp
) || err
!= 0) {
2562 kmem_free(dpa
, sizeof (*dpa
));
2563 } else if (dpa
->dpa_curlevel
== dpa
->dpa_zb
.zb_level
) {
2564 ASSERT3U(nextblkid
, ==, dpa
->dpa_zb
.zb_blkid
);
2565 dbuf_issue_final_prefetch(dpa
, bp
);
2566 kmem_free(dpa
, sizeof (*dpa
));
2568 arc_flags_t iter_aflags
= ARC_FLAG_NOWAIT
;
2569 zbookmark_phys_t zb
;
2571 ASSERT3U(dpa
->dpa_curlevel
, ==, BP_GET_LEVEL(bp
));
2573 SET_BOOKMARK(&zb
, dpa
->dpa_zb
.zb_objset
,
2574 dpa
->dpa_zb
.zb_object
, dpa
->dpa_curlevel
, nextblkid
);
2576 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
,
2577 bp
, dbuf_prefetch_indirect_done
, dpa
, dpa
->dpa_prio
,
2578 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
2582 arc_buf_destroy(abuf
, private);
2586 * Issue prefetch reads for the given block on the given level. If the indirect
2587 * blocks above that block are not in memory, we will read them in
2588 * asynchronously. As a result, this call never blocks waiting for a read to
2589 * complete. Note that the prefetch might fail if the dataset is encrypted and
2590 * the encryption key is unmapped before the IO completes.
2593 dbuf_prefetch(dnode_t
*dn
, int64_t level
, uint64_t blkid
, zio_priority_t prio
,
2597 int epbs
, nlevels
, curlevel
;
2600 ASSERT(blkid
!= DMU_BONUS_BLKID
);
2601 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2603 if (blkid
> dn
->dn_maxblkid
)
2606 if (dnode_block_freed(dn
, blkid
))
2610 * This dnode hasn't been written to disk yet, so there's nothing to
2613 nlevels
= dn
->dn_phys
->dn_nlevels
;
2614 if (level
>= nlevels
|| dn
->dn_phys
->dn_nblkptr
== 0)
2617 epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2618 if (dn
->dn_phys
->dn_maxblkid
< blkid
<< (epbs
* level
))
2621 dmu_buf_impl_t
*db
= dbuf_find(dn
->dn_objset
, dn
->dn_object
,
2624 mutex_exit(&db
->db_mtx
);
2626 * This dbuf already exists. It is either CACHED, or
2627 * (we assume) about to be read or filled.
2633 * Find the closest ancestor (indirect block) of the target block
2634 * that is present in the cache. In this indirect block, we will
2635 * find the bp that is at curlevel, curblkid.
2639 while (curlevel
< nlevels
- 1) {
2640 int parent_level
= curlevel
+ 1;
2641 uint64_t parent_blkid
= curblkid
>> epbs
;
2644 if (dbuf_hold_impl(dn
, parent_level
, parent_blkid
,
2645 FALSE
, TRUE
, FTAG
, &db
) == 0) {
2646 blkptr_t
*bpp
= db
->db_buf
->b_data
;
2647 bp
= bpp
[P2PHASE(curblkid
, 1 << epbs
)];
2648 dbuf_rele(db
, FTAG
);
2652 curlevel
= parent_level
;
2653 curblkid
= parent_blkid
;
2656 if (curlevel
== nlevels
- 1) {
2657 /* No cached indirect blocks found. */
2658 ASSERT3U(curblkid
, <, dn
->dn_phys
->dn_nblkptr
);
2659 bp
= dn
->dn_phys
->dn_blkptr
[curblkid
];
2661 if (BP_IS_HOLE(&bp
))
2664 ASSERT3U(curlevel
, ==, BP_GET_LEVEL(&bp
));
2666 zio_t
*pio
= zio_root(dmu_objset_spa(dn
->dn_objset
), NULL
, NULL
,
2669 dbuf_prefetch_arg_t
*dpa
= kmem_zalloc(sizeof (*dpa
), KM_SLEEP
);
2670 dsl_dataset_t
*ds
= dn
->dn_objset
->os_dsl_dataset
;
2671 SET_BOOKMARK(&dpa
->dpa_zb
, ds
!= NULL
? ds
->ds_object
: DMU_META_OBJSET
,
2672 dn
->dn_object
, level
, blkid
);
2673 dpa
->dpa_curlevel
= curlevel
;
2674 dpa
->dpa_prio
= prio
;
2675 dpa
->dpa_aflags
= aflags
;
2676 dpa
->dpa_spa
= dn
->dn_objset
->os_spa
;
2677 dpa
->dpa_dnode
= dn
;
2678 dpa
->dpa_epbs
= epbs
;
2682 * If we have the indirect just above us, no need to do the asynchronous
2683 * prefetch chain; we'll just run the last step ourselves. If we're at
2684 * a higher level, though, we want to issue the prefetches for all the
2685 * indirect blocks asynchronously, so we can go on with whatever we were
2688 if (curlevel
== level
) {
2689 ASSERT3U(curblkid
, ==, blkid
);
2690 dbuf_issue_final_prefetch(dpa
, &bp
);
2691 kmem_free(dpa
, sizeof (*dpa
));
2693 arc_flags_t iter_aflags
= ARC_FLAG_NOWAIT
;
2694 zbookmark_phys_t zb
;
2696 SET_BOOKMARK(&zb
, ds
!= NULL
? ds
->ds_object
: DMU_META_OBJSET
,
2697 dn
->dn_object
, curlevel
, curblkid
);
2698 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
,
2699 &bp
, dbuf_prefetch_indirect_done
, dpa
, prio
,
2700 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
2704 * We use pio here instead of dpa_zio since it's possible that
2705 * dpa may have already been freed.
2710 #define DBUF_HOLD_IMPL_MAX_DEPTH 20
2713 * Returns with db_holds incremented, and db_mtx not held.
2714 * Note: dn_struct_rwlock must be held.
2717 __dbuf_hold_impl(struct dbuf_hold_impl_data
*dh
)
2719 ASSERT3S(dh
->dh_depth
, <, DBUF_HOLD_IMPL_MAX_DEPTH
);
2720 dh
->dh_parent
= NULL
;
2722 ASSERT(dh
->dh_blkid
!= DMU_BONUS_BLKID
);
2723 ASSERT(RW_LOCK_HELD(&dh
->dh_dn
->dn_struct_rwlock
));
2724 ASSERT3U(dh
->dh_dn
->dn_nlevels
, >, dh
->dh_level
);
2726 *(dh
->dh_dbp
) = NULL
;
2728 /* dbuf_find() returns with db_mtx held */
2729 dh
->dh_db
= dbuf_find(dh
->dh_dn
->dn_objset
, dh
->dh_dn
->dn_object
,
2730 dh
->dh_level
, dh
->dh_blkid
);
2732 if (dh
->dh_db
== NULL
) {
2735 if (dh
->dh_fail_uncached
)
2736 return (SET_ERROR(ENOENT
));
2738 ASSERT3P(dh
->dh_parent
, ==, NULL
);
2739 dh
->dh_err
= dbuf_findbp(dh
->dh_dn
, dh
->dh_level
, dh
->dh_blkid
,
2740 dh
->dh_fail_sparse
, &dh
->dh_parent
, &dh
->dh_bp
, dh
);
2741 if (dh
->dh_fail_sparse
) {
2742 if (dh
->dh_err
== 0 &&
2743 dh
->dh_bp
&& BP_IS_HOLE(dh
->dh_bp
))
2744 dh
->dh_err
= SET_ERROR(ENOENT
);
2747 dbuf_rele(dh
->dh_parent
, NULL
);
2748 return (dh
->dh_err
);
2751 if (dh
->dh_err
&& dh
->dh_err
!= ENOENT
)
2752 return (dh
->dh_err
);
2753 dh
->dh_db
= dbuf_create(dh
->dh_dn
, dh
->dh_level
, dh
->dh_blkid
,
2754 dh
->dh_parent
, dh
->dh_bp
);
2757 if (dh
->dh_fail_uncached
&& dh
->dh_db
->db_state
!= DB_CACHED
) {
2758 mutex_exit(&dh
->dh_db
->db_mtx
);
2759 return (SET_ERROR(ENOENT
));
2762 if (dh
->dh_db
->db_buf
!= NULL
)
2763 ASSERT3P(dh
->dh_db
->db
.db_data
, ==, dh
->dh_db
->db_buf
->b_data
);
2765 ASSERT(dh
->dh_db
->db_buf
== NULL
|| arc_referenced(dh
->dh_db
->db_buf
));
2768 * If this buffer is currently syncing out, and we are are
2769 * still referencing it from db_data, we need to make a copy
2770 * of it in case we decide we want to dirty it again in this txg.
2772 if (dh
->dh_db
->db_level
== 0 &&
2773 dh
->dh_db
->db_blkid
!= DMU_BONUS_BLKID
&&
2774 dh
->dh_dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
2775 dh
->dh_db
->db_state
== DB_CACHED
&& dh
->dh_db
->db_data_pending
) {
2776 dh
->dh_dr
= dh
->dh_db
->db_data_pending
;
2778 if (dh
->dh_dr
->dt
.dl
.dr_data
== dh
->dh_db
->db_buf
) {
2779 dh
->dh_type
= DBUF_GET_BUFC_TYPE(dh
->dh_db
);
2781 dbuf_set_data(dh
->dh_db
,
2782 arc_alloc_buf(dh
->dh_dn
->dn_objset
->os_spa
,
2783 dh
->dh_db
, dh
->dh_type
, dh
->dh_db
->db
.db_size
));
2784 bcopy(dh
->dh_dr
->dt
.dl
.dr_data
->b_data
,
2785 dh
->dh_db
->db
.db_data
, dh
->dh_db
->db
.db_size
);
2789 if (multilist_link_active(&dh
->dh_db
->db_cache_link
)) {
2790 ASSERT(refcount_is_zero(&dh
->dh_db
->db_holds
));
2791 multilist_remove(dbuf_cache
, dh
->dh_db
);
2792 (void) refcount_remove_many(&dbuf_cache_size
,
2793 dh
->dh_db
->db
.db_size
, dh
->dh_db
);
2795 (void) refcount_add(&dh
->dh_db
->db_holds
, dh
->dh_tag
);
2796 DBUF_VERIFY(dh
->dh_db
);
2797 mutex_exit(&dh
->dh_db
->db_mtx
);
2799 /* NOTE: we can't rele the parent until after we drop the db_mtx */
2801 dbuf_rele(dh
->dh_parent
, NULL
);
2803 ASSERT3P(DB_DNODE(dh
->dh_db
), ==, dh
->dh_dn
);
2804 ASSERT3U(dh
->dh_db
->db_blkid
, ==, dh
->dh_blkid
);
2805 ASSERT3U(dh
->dh_db
->db_level
, ==, dh
->dh_level
);
2806 *(dh
->dh_dbp
) = dh
->dh_db
;
2812 * The following code preserves the recursive function dbuf_hold_impl()
2813 * but moves the local variables AND function arguments to the heap to
2814 * minimize the stack frame size. Enough space is initially allocated
2815 * on the stack for 20 levels of recursion.
2818 dbuf_hold_impl(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
2819 boolean_t fail_sparse
, boolean_t fail_uncached
,
2820 void *tag
, dmu_buf_impl_t
**dbp
)
2822 struct dbuf_hold_impl_data
*dh
;
2825 dh
= kmem_alloc(sizeof (struct dbuf_hold_impl_data
) *
2826 DBUF_HOLD_IMPL_MAX_DEPTH
, KM_SLEEP
);
2827 __dbuf_hold_impl_init(dh
, dn
, level
, blkid
, fail_sparse
,
2828 fail_uncached
, tag
, dbp
, 0);
2830 error
= __dbuf_hold_impl(dh
);
2832 kmem_free(dh
, sizeof (struct dbuf_hold_impl_data
) *
2833 DBUF_HOLD_IMPL_MAX_DEPTH
);
2839 __dbuf_hold_impl_init(struct dbuf_hold_impl_data
*dh
,
2840 dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
2841 boolean_t fail_sparse
, boolean_t fail_uncached
,
2842 void *tag
, dmu_buf_impl_t
**dbp
, int depth
)
2845 dh
->dh_level
= level
;
2846 dh
->dh_blkid
= blkid
;
2848 dh
->dh_fail_sparse
= fail_sparse
;
2849 dh
->dh_fail_uncached
= fail_uncached
;
2855 dh
->dh_parent
= NULL
;
2861 dh
->dh_depth
= depth
;
2865 dbuf_hold(dnode_t
*dn
, uint64_t blkid
, void *tag
)
2867 return (dbuf_hold_level(dn
, 0, blkid
, tag
));
2871 dbuf_hold_level(dnode_t
*dn
, int level
, uint64_t blkid
, void *tag
)
2874 int err
= dbuf_hold_impl(dn
, level
, blkid
, FALSE
, FALSE
, tag
, &db
);
2875 return (err
? NULL
: db
);
2879 dbuf_create_bonus(dnode_t
*dn
)
2881 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
2883 ASSERT(dn
->dn_bonus
== NULL
);
2884 dn
->dn_bonus
= dbuf_create(dn
, 0, DMU_BONUS_BLKID
, dn
->dn_dbuf
, NULL
);
2888 dbuf_spill_set_blksz(dmu_buf_t
*db_fake
, uint64_t blksz
, dmu_tx_t
*tx
)
2890 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2893 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
2894 return (SET_ERROR(ENOTSUP
));
2896 blksz
= SPA_MINBLOCKSIZE
;
2897 ASSERT3U(blksz
, <=, spa_maxblocksize(dmu_objset_spa(db
->db_objset
)));
2898 blksz
= P2ROUNDUP(blksz
, SPA_MINBLOCKSIZE
);
2902 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
2903 dbuf_new_size(db
, blksz
, tx
);
2904 rw_exit(&dn
->dn_struct_rwlock
);
2911 dbuf_rm_spill(dnode_t
*dn
, dmu_tx_t
*tx
)
2913 dbuf_free_range(dn
, DMU_SPILL_BLKID
, DMU_SPILL_BLKID
, tx
);
2916 #pragma weak dmu_buf_add_ref = dbuf_add_ref
2918 dbuf_add_ref(dmu_buf_impl_t
*db
, void *tag
)
2920 int64_t holds
= refcount_add(&db
->db_holds
, tag
);
2921 VERIFY3S(holds
, >, 1);
2924 #pragma weak dmu_buf_try_add_ref = dbuf_try_add_ref
2926 dbuf_try_add_ref(dmu_buf_t
*db_fake
, objset_t
*os
, uint64_t obj
, uint64_t blkid
,
2929 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2930 dmu_buf_impl_t
*found_db
;
2931 boolean_t result
= B_FALSE
;
2933 if (blkid
== DMU_BONUS_BLKID
)
2934 found_db
= dbuf_find_bonus(os
, obj
);
2936 found_db
= dbuf_find(os
, obj
, 0, blkid
);
2938 if (found_db
!= NULL
) {
2939 if (db
== found_db
&& dbuf_refcount(db
) > db
->db_dirtycnt
) {
2940 (void) refcount_add(&db
->db_holds
, tag
);
2943 mutex_exit(&found_db
->db_mtx
);
2949 * If you call dbuf_rele() you had better not be referencing the dnode handle
2950 * unless you have some other direct or indirect hold on the dnode. (An indirect
2951 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
2952 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
2953 * dnode's parent dbuf evicting its dnode handles.
2956 dbuf_rele(dmu_buf_impl_t
*db
, void *tag
)
2958 mutex_enter(&db
->db_mtx
);
2959 dbuf_rele_and_unlock(db
, tag
);
2963 dmu_buf_rele(dmu_buf_t
*db
, void *tag
)
2965 dbuf_rele((dmu_buf_impl_t
*)db
, tag
);
2969 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
2970 * db_dirtycnt and db_holds to be updated atomically.
2973 dbuf_rele_and_unlock(dmu_buf_impl_t
*db
, void *tag
)
2977 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2981 * Remove the reference to the dbuf before removing its hold on the
2982 * dnode so we can guarantee in dnode_move() that a referenced bonus
2983 * buffer has a corresponding dnode hold.
2985 holds
= refcount_remove(&db
->db_holds
, tag
);
2989 * We can't freeze indirects if there is a possibility that they
2990 * may be modified in the current syncing context.
2992 if (db
->db_buf
!= NULL
&&
2993 holds
== (db
->db_level
== 0 ? db
->db_dirtycnt
: 0)) {
2994 arc_buf_freeze(db
->db_buf
);
2997 if (holds
== db
->db_dirtycnt
&&
2998 db
->db_level
== 0 && db
->db_user_immediate_evict
)
2999 dbuf_evict_user(db
);
3002 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
3004 boolean_t evict_dbuf
= db
->db_pending_evict
;
3007 * If the dnode moves here, we cannot cross this
3008 * barrier until the move completes.
3013 atomic_dec_32(&dn
->dn_dbufs_count
);
3016 * Decrementing the dbuf count means that the bonus
3017 * buffer's dnode hold is no longer discounted in
3018 * dnode_move(). The dnode cannot move until after
3019 * the dnode_rele() below.
3024 * Do not reference db after its lock is dropped.
3025 * Another thread may evict it.
3027 mutex_exit(&db
->db_mtx
);
3030 dnode_evict_bonus(dn
);
3033 } else if (db
->db_buf
== NULL
) {
3035 * This is a special case: we never associated this
3036 * dbuf with any data allocated from the ARC.
3038 ASSERT(db
->db_state
== DB_UNCACHED
||
3039 db
->db_state
== DB_NOFILL
);
3041 } else if (arc_released(db
->db_buf
)) {
3043 * This dbuf has anonymous data associated with it.
3047 boolean_t do_arc_evict
= B_FALSE
;
3049 spa_t
*spa
= dmu_objset_spa(db
->db_objset
);
3051 if (!DBUF_IS_CACHEABLE(db
) &&
3052 db
->db_blkptr
!= NULL
&&
3053 !BP_IS_HOLE(db
->db_blkptr
) &&
3054 !BP_IS_EMBEDDED(db
->db_blkptr
)) {
3055 do_arc_evict
= B_TRUE
;
3056 bp
= *db
->db_blkptr
;
3059 if (!DBUF_IS_CACHEABLE(db
) ||
3060 db
->db_pending_evict
) {
3062 } else if (!multilist_link_active(&db
->db_cache_link
)) {
3063 multilist_insert(dbuf_cache
, db
);
3064 (void) refcount_add_many(&dbuf_cache_size
,
3065 db
->db
.db_size
, db
);
3066 mutex_exit(&db
->db_mtx
);
3068 dbuf_evict_notify();
3072 arc_freed(spa
, &bp
);
3075 mutex_exit(&db
->db_mtx
);
3080 #pragma weak dmu_buf_refcount = dbuf_refcount
3082 dbuf_refcount(dmu_buf_impl_t
*db
)
3084 return (refcount_count(&db
->db_holds
));
3088 dmu_buf_replace_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*old_user
,
3089 dmu_buf_user_t
*new_user
)
3091 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3093 mutex_enter(&db
->db_mtx
);
3094 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
3095 if (db
->db_user
== old_user
)
3096 db
->db_user
= new_user
;
3098 old_user
= db
->db_user
;
3099 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
3100 mutex_exit(&db
->db_mtx
);
3106 dmu_buf_set_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
3108 return (dmu_buf_replace_user(db_fake
, NULL
, user
));
3112 dmu_buf_set_user_ie(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
3114 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3116 db
->db_user_immediate_evict
= TRUE
;
3117 return (dmu_buf_set_user(db_fake
, user
));
3121 dmu_buf_remove_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
3123 return (dmu_buf_replace_user(db_fake
, user
, NULL
));
3127 dmu_buf_get_user(dmu_buf_t
*db_fake
)
3129 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3131 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
3132 return (db
->db_user
);
3136 dmu_buf_user_evict_wait()
3138 taskq_wait(dbu_evict_taskq
);
3142 dmu_buf_get_blkptr(dmu_buf_t
*db
)
3144 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3145 return (dbi
->db_blkptr
);
3149 dmu_buf_get_objset(dmu_buf_t
*db
)
3151 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3152 return (dbi
->db_objset
);
3156 dmu_buf_dnode_enter(dmu_buf_t
*db
)
3158 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3159 DB_DNODE_ENTER(dbi
);
3160 return (DB_DNODE(dbi
));
3164 dmu_buf_dnode_exit(dmu_buf_t
*db
)
3166 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3171 dbuf_check_blkptr(dnode_t
*dn
, dmu_buf_impl_t
*db
)
3173 /* ASSERT(dmu_tx_is_syncing(tx) */
3174 ASSERT(MUTEX_HELD(&db
->db_mtx
));
3176 if (db
->db_blkptr
!= NULL
)
3179 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3180 db
->db_blkptr
= DN_SPILL_BLKPTR(dn
->dn_phys
);
3181 BP_ZERO(db
->db_blkptr
);
3184 if (db
->db_level
== dn
->dn_phys
->dn_nlevels
-1) {
3186 * This buffer was allocated at a time when there was
3187 * no available blkptrs from the dnode, or it was
3188 * inappropriate to hook it in (i.e., nlevels mis-match).
3190 ASSERT(db
->db_blkid
< dn
->dn_phys
->dn_nblkptr
);
3191 ASSERT(db
->db_parent
== NULL
);
3192 db
->db_parent
= dn
->dn_dbuf
;
3193 db
->db_blkptr
= &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
];
3196 dmu_buf_impl_t
*parent
= db
->db_parent
;
3197 int epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
3199 ASSERT(dn
->dn_phys
->dn_nlevels
> 1);
3200 if (parent
== NULL
) {
3201 mutex_exit(&db
->db_mtx
);
3202 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
3203 parent
= dbuf_hold_level(dn
, db
->db_level
+ 1,
3204 db
->db_blkid
>> epbs
, db
);
3205 rw_exit(&dn
->dn_struct_rwlock
);
3206 mutex_enter(&db
->db_mtx
);
3207 db
->db_parent
= parent
;
3209 db
->db_blkptr
= (blkptr_t
*)parent
->db
.db_data
+
3210 (db
->db_blkid
& ((1ULL << epbs
) - 1));
3216 * Ensure the dbuf's data is untransformed if the associated dirty
3217 * record requires it. This is used by dbuf_sync_leaf() to ensure
3218 * that a dnode block is decrypted before we write new data to it.
3219 * For raw writes we assert that the buffer is already encrypted.
3222 dbuf_check_crypt(dbuf_dirty_record_t
*dr
)
3225 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3227 ASSERT(MUTEX_HELD(&db
->db_mtx
));
3229 if (!dr
->dt
.dl
.dr_raw
&& arc_is_encrypted(db
->db_buf
)) {
3231 * Unfortunately, there is currently no mechanism for
3232 * syncing context to handle decryption errors. An error
3233 * here is only possible if an attacker maliciously
3234 * changed a dnode block and updated the associated
3235 * checksums going up the block tree.
3237 err
= arc_untransform(db
->db_buf
, db
->db_objset
->os_spa
,
3238 dmu_objset_id(db
->db_objset
), B_TRUE
);
3240 panic("Invalid dnode block MAC");
3241 } else if (dr
->dt
.dl
.dr_raw
) {
3243 * Writing raw encrypted data requires the db's arc buffer
3244 * to be converted to raw by the caller.
3246 ASSERT(arc_is_encrypted(db
->db_buf
));
3251 * dbuf_sync_indirect() is called recursively from dbuf_sync_list() so it
3252 * is critical the we not allow the compiler to inline this function in to
3253 * dbuf_sync_list() thereby drastically bloating the stack usage.
3255 noinline
static void
3256 dbuf_sync_indirect(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
3258 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3262 ASSERT(dmu_tx_is_syncing(tx
));
3264 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
3266 mutex_enter(&db
->db_mtx
);
3268 ASSERT(db
->db_level
> 0);
3271 /* Read the block if it hasn't been read yet. */
3272 if (db
->db_buf
== NULL
) {
3273 mutex_exit(&db
->db_mtx
);
3274 (void) dbuf_read(db
, NULL
, DB_RF_MUST_SUCCEED
);
3275 mutex_enter(&db
->db_mtx
);
3277 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
3278 ASSERT(db
->db_buf
!= NULL
);
3282 /* Indirect block size must match what the dnode thinks it is. */
3283 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
3284 dbuf_check_blkptr(dn
, db
);
3287 /* Provide the pending dirty record to child dbufs */
3288 db
->db_data_pending
= dr
;
3290 mutex_exit(&db
->db_mtx
);
3291 dbuf_write(dr
, db
->db_buf
, tx
);
3294 mutex_enter(&dr
->dt
.di
.dr_mtx
);
3295 dbuf_sync_list(&dr
->dt
.di
.dr_children
, db
->db_level
- 1, tx
);
3296 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
3297 mutex_exit(&dr
->dt
.di
.dr_mtx
);
3302 * dbuf_sync_leaf() is called recursively from dbuf_sync_list() so it is
3303 * critical the we not allow the compiler to inline this function in to
3304 * dbuf_sync_list() thereby drastically bloating the stack usage.
3306 noinline
static void
3307 dbuf_sync_leaf(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
3309 arc_buf_t
**datap
= &dr
->dt
.dl
.dr_data
;
3310 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3313 uint64_t txg
= tx
->tx_txg
;
3315 ASSERT(dmu_tx_is_syncing(tx
));
3317 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
3319 mutex_enter(&db
->db_mtx
);
3321 * To be synced, we must be dirtied. But we
3322 * might have been freed after the dirty.
3324 if (db
->db_state
== DB_UNCACHED
) {
3325 /* This buffer has been freed since it was dirtied */
3326 ASSERT(db
->db
.db_data
== NULL
);
3327 } else if (db
->db_state
== DB_FILL
) {
3328 /* This buffer was freed and is now being re-filled */
3329 ASSERT(db
->db
.db_data
!= dr
->dt
.dl
.dr_data
);
3331 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_NOFILL
);
3338 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3339 mutex_enter(&dn
->dn_mtx
);
3340 if (!(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
)) {
3342 * In the previous transaction group, the bonus buffer
3343 * was entirely used to store the attributes for the
3344 * dnode which overrode the dn_spill field. However,
3345 * when adding more attributes to the file a spill
3346 * block was required to hold the extra attributes.
3348 * Make sure to clear the garbage left in the dn_spill
3349 * field from the previous attributes in the bonus
3350 * buffer. Otherwise, after writing out the spill
3351 * block to the new allocated dva, it will free
3352 * the old block pointed to by the invalid dn_spill.
3354 db
->db_blkptr
= NULL
;
3356 dn
->dn_phys
->dn_flags
|= DNODE_FLAG_SPILL_BLKPTR
;
3357 mutex_exit(&dn
->dn_mtx
);
3361 * If this is a bonus buffer, simply copy the bonus data into the
3362 * dnode. It will be written out when the dnode is synced (and it
3363 * will be synced, since it must have been dirty for dbuf_sync to
3366 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
3367 dbuf_dirty_record_t
**drp
;
3369 ASSERT(*datap
!= NULL
);
3370 ASSERT0(db
->db_level
);
3371 ASSERT3U(DN_MAX_BONUS_LEN(dn
->dn_phys
), <=,
3372 DN_SLOTS_TO_BONUSLEN(dn
->dn_phys
->dn_extra_slots
+ 1));
3373 bcopy(*datap
, DN_BONUS(dn
->dn_phys
),
3374 DN_MAX_BONUS_LEN(dn
->dn_phys
));
3377 if (*datap
!= db
->db
.db_data
) {
3378 int slots
= DB_DNODE(db
)->dn_num_slots
;
3379 int bonuslen
= DN_SLOTS_TO_BONUSLEN(slots
);
3380 kmem_free(*datap
, bonuslen
);
3381 arc_space_return(bonuslen
, ARC_SPACE_BONUS
);
3383 db
->db_data_pending
= NULL
;
3384 drp
= &db
->db_last_dirty
;
3386 drp
= &(*drp
)->dr_next
;
3387 ASSERT(dr
->dr_next
== NULL
);
3388 ASSERT(dr
->dr_dbuf
== db
);
3390 if (dr
->dr_dbuf
->db_level
!= 0) {
3391 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
3392 list_destroy(&dr
->dt
.di
.dr_children
);
3394 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
3395 ASSERT(db
->db_dirtycnt
> 0);
3396 db
->db_dirtycnt
-= 1;
3397 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)txg
);
3404 * This function may have dropped the db_mtx lock allowing a dmu_sync
3405 * operation to sneak in. As a result, we need to ensure that we
3406 * don't check the dr_override_state until we have returned from
3407 * dbuf_check_blkptr.
3409 dbuf_check_blkptr(dn
, db
);
3412 * If this buffer is in the middle of an immediate write,
3413 * wait for the synchronous IO to complete.
3415 while (dr
->dt
.dl
.dr_override_state
== DR_IN_DMU_SYNC
) {
3416 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
3417 cv_wait(&db
->db_changed
, &db
->db_mtx
);
3418 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_NOT_OVERRIDDEN
);
3422 * If this is a dnode block, ensure it is appropriately encrypted
3423 * or decrypted, depending on what we are writing to it this txg.
3425 if (os
->os_encrypted
&& dn
->dn_object
== DMU_META_DNODE_OBJECT
)
3426 dbuf_check_crypt(dr
);
3428 if (db
->db_state
!= DB_NOFILL
&&
3429 dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
3430 refcount_count(&db
->db_holds
) > 1 &&
3431 dr
->dt
.dl
.dr_override_state
!= DR_OVERRIDDEN
&&
3432 *datap
== db
->db_buf
) {
3434 * If this buffer is currently "in use" (i.e., there
3435 * are active holds and db_data still references it),
3436 * then make a copy before we start the write so that
3437 * any modifications from the open txg will not leak
3440 * NOTE: this copy does not need to be made for
3441 * objects only modified in the syncing context (e.g.
3442 * DNONE_DNODE blocks).
3444 int psize
= arc_buf_size(*datap
);
3445 int lsize
= arc_buf_lsize(*datap
);
3446 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
3447 enum zio_compress compress_type
= arc_get_compression(*datap
);
3449 if (arc_is_encrypted(*datap
)) {
3450 boolean_t byteorder
;
3451 uint8_t salt
[ZIO_DATA_SALT_LEN
];
3452 uint8_t iv
[ZIO_DATA_IV_LEN
];
3453 uint8_t mac
[ZIO_DATA_MAC_LEN
];
3455 arc_get_raw_params(*datap
, &byteorder
, salt
, iv
, mac
);
3456 *datap
= arc_alloc_raw_buf(os
->os_spa
, db
,
3457 dmu_objset_id(os
), byteorder
, salt
, iv
, mac
,
3458 dn
->dn_type
, psize
, lsize
, compress_type
);
3459 } else if (compress_type
!= ZIO_COMPRESS_OFF
) {
3460 ASSERT3U(type
, ==, ARC_BUFC_DATA
);
3461 *datap
= arc_alloc_compressed_buf(os
->os_spa
, db
,
3462 psize
, lsize
, compress_type
);
3464 *datap
= arc_alloc_buf(os
->os_spa
, db
, type
, psize
);
3466 bcopy(db
->db
.db_data
, (*datap
)->b_data
, psize
);
3468 db
->db_data_pending
= dr
;
3470 mutex_exit(&db
->db_mtx
);
3472 dbuf_write(dr
, *datap
, tx
);
3474 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
3475 if (dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
3476 list_insert_tail(&dn
->dn_dirty_records
[txg
&TXG_MASK
], dr
);
3480 * Although zio_nowait() does not "wait for an IO", it does
3481 * initiate the IO. If this is an empty write it seems plausible
3482 * that the IO could actually be completed before the nowait
3483 * returns. We need to DB_DNODE_EXIT() first in case
3484 * zio_nowait() invalidates the dbuf.
3487 zio_nowait(dr
->dr_zio
);
3492 dbuf_sync_list(list_t
*list
, int level
, dmu_tx_t
*tx
)
3494 dbuf_dirty_record_t
*dr
;
3496 while ((dr
= list_head(list
))) {
3497 if (dr
->dr_zio
!= NULL
) {
3499 * If we find an already initialized zio then we
3500 * are processing the meta-dnode, and we have finished.
3501 * The dbufs for all dnodes are put back on the list
3502 * during processing, so that we can zio_wait()
3503 * these IOs after initiating all child IOs.
3505 ASSERT3U(dr
->dr_dbuf
->db
.db_object
, ==,
3506 DMU_META_DNODE_OBJECT
);
3509 if (dr
->dr_dbuf
->db_blkid
!= DMU_BONUS_BLKID
&&
3510 dr
->dr_dbuf
->db_blkid
!= DMU_SPILL_BLKID
) {
3511 VERIFY3U(dr
->dr_dbuf
->db_level
, ==, level
);
3513 list_remove(list
, dr
);
3514 if (dr
->dr_dbuf
->db_level
> 0)
3515 dbuf_sync_indirect(dr
, tx
);
3517 dbuf_sync_leaf(dr
, tx
);
3523 dbuf_write_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
3525 dmu_buf_impl_t
*db
= vdb
;
3527 blkptr_t
*bp
= zio
->io_bp
;
3528 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
3529 spa_t
*spa
= zio
->io_spa
;
3534 ASSERT3P(db
->db_blkptr
, !=, NULL
);
3535 ASSERT3P(&db
->db_data_pending
->dr_bp_copy
, ==, bp
);
3539 delta
= bp_get_dsize_sync(spa
, bp
) - bp_get_dsize_sync(spa
, bp_orig
);
3540 dnode_diduse_space(dn
, delta
- zio
->io_prev_space_delta
);
3541 zio
->io_prev_space_delta
= delta
;
3543 if (bp
->blk_birth
!= 0) {
3544 ASSERT((db
->db_blkid
!= DMU_SPILL_BLKID
&&
3545 BP_GET_TYPE(bp
) == dn
->dn_type
) ||
3546 (db
->db_blkid
== DMU_SPILL_BLKID
&&
3547 BP_GET_TYPE(bp
) == dn
->dn_bonustype
) ||
3548 BP_IS_EMBEDDED(bp
));
3549 ASSERT(BP_GET_LEVEL(bp
) == db
->db_level
);
3552 mutex_enter(&db
->db_mtx
);
3555 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3556 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
3557 ASSERT(!(BP_IS_HOLE(bp
)) &&
3558 db
->db_blkptr
== DN_SPILL_BLKPTR(dn
->dn_phys
));
3562 if (db
->db_level
== 0) {
3563 mutex_enter(&dn
->dn_mtx
);
3564 if (db
->db_blkid
> dn
->dn_phys
->dn_maxblkid
&&
3565 db
->db_blkid
!= DMU_SPILL_BLKID
)
3566 dn
->dn_phys
->dn_maxblkid
= db
->db_blkid
;
3567 mutex_exit(&dn
->dn_mtx
);
3569 if (dn
->dn_type
== DMU_OT_DNODE
) {
3571 while (i
< db
->db
.db_size
) {
3573 (void *)(((char *)db
->db
.db_data
) + i
);
3575 i
+= DNODE_MIN_SIZE
;
3576 if (dnp
->dn_type
!= DMU_OT_NONE
) {
3578 i
+= dnp
->dn_extra_slots
*
3583 if (BP_IS_HOLE(bp
)) {
3590 blkptr_t
*ibp
= db
->db
.db_data
;
3591 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
3592 for (i
= db
->db
.db_size
>> SPA_BLKPTRSHIFT
; i
> 0; i
--, ibp
++) {
3593 if (BP_IS_HOLE(ibp
))
3595 fill
+= BP_GET_FILL(ibp
);
3600 if (!BP_IS_EMBEDDED(bp
))
3601 BP_SET_FILL(bp
, fill
);
3603 mutex_exit(&db
->db_mtx
);
3605 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
3606 *db
->db_blkptr
= *bp
;
3607 rw_exit(&dn
->dn_struct_rwlock
);
3612 * This function gets called just prior to running through the compression
3613 * stage of the zio pipeline. If we're an indirect block comprised of only
3614 * holes, then we want this indirect to be compressed away to a hole. In
3615 * order to do that we must zero out any information about the holes that
3616 * this indirect points to prior to before we try to compress it.
3619 dbuf_write_children_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
3621 dmu_buf_impl_t
*db
= vdb
;
3624 unsigned int epbs
, i
;
3626 ASSERT3U(db
->db_level
, >, 0);
3629 epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
3630 ASSERT3U(epbs
, <, 31);
3632 /* Determine if all our children are holes */
3633 for (i
= 0, bp
= db
->db
.db_data
; i
< 1ULL << epbs
; i
++, bp
++) {
3634 if (!BP_IS_HOLE(bp
))
3639 * If all the children are holes, then zero them all out so that
3640 * we may get compressed away.
3642 if (i
== 1ULL << epbs
) {
3644 * We only found holes. Grab the rwlock to prevent
3645 * anybody from reading the blocks we're about to
3648 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
3649 bzero(db
->db
.db_data
, db
->db
.db_size
);
3650 rw_exit(&dn
->dn_struct_rwlock
);
3656 * The SPA will call this callback several times for each zio - once
3657 * for every physical child i/o (zio->io_phys_children times). This
3658 * allows the DMU to monitor the progress of each logical i/o. For example,
3659 * there may be 2 copies of an indirect block, or many fragments of a RAID-Z
3660 * block. There may be a long delay before all copies/fragments are completed,
3661 * so this callback allows us to retire dirty space gradually, as the physical
3666 dbuf_write_physdone(zio_t
*zio
, arc_buf_t
*buf
, void *arg
)
3668 dmu_buf_impl_t
*db
= arg
;
3669 objset_t
*os
= db
->db_objset
;
3670 dsl_pool_t
*dp
= dmu_objset_pool(os
);
3671 dbuf_dirty_record_t
*dr
;
3674 dr
= db
->db_data_pending
;
3675 ASSERT3U(dr
->dr_txg
, ==, zio
->io_txg
);
3678 * The callback will be called io_phys_children times. Retire one
3679 * portion of our dirty space each time we are called. Any rounding
3680 * error will be cleaned up by dsl_pool_sync()'s call to
3681 * dsl_pool_undirty_space().
3683 delta
= dr
->dr_accounted
/ zio
->io_phys_children
;
3684 dsl_pool_undirty_space(dp
, delta
, zio
->io_txg
);
3689 dbuf_write_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
3691 dmu_buf_impl_t
*db
= vdb
;
3692 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
3693 blkptr_t
*bp
= db
->db_blkptr
;
3694 objset_t
*os
= db
->db_objset
;
3695 dmu_tx_t
*tx
= os
->os_synctx
;
3696 dbuf_dirty_record_t
**drp
, *dr
;
3698 ASSERT0(zio
->io_error
);
3699 ASSERT(db
->db_blkptr
== bp
);
3702 * For nopwrites and rewrites we ensure that the bp matches our
3703 * original and bypass all the accounting.
3705 if (zio
->io_flags
& (ZIO_FLAG_IO_REWRITE
| ZIO_FLAG_NOPWRITE
)) {
3706 ASSERT(BP_EQUAL(bp
, bp_orig
));
3708 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
3709 (void) dsl_dataset_block_kill(ds
, bp_orig
, tx
, B_TRUE
);
3710 dsl_dataset_block_born(ds
, bp
, tx
);
3713 mutex_enter(&db
->db_mtx
);
3717 drp
= &db
->db_last_dirty
;
3718 while ((dr
= *drp
) != db
->db_data_pending
)
3720 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
3721 ASSERT(dr
->dr_dbuf
== db
);
3722 ASSERT(dr
->dr_next
== NULL
);
3726 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3731 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
3732 ASSERT(!(BP_IS_HOLE(db
->db_blkptr
)) &&
3733 db
->db_blkptr
== DN_SPILL_BLKPTR(dn
->dn_phys
));
3738 if (db
->db_level
== 0) {
3739 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
3740 ASSERT(dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
);
3741 if (db
->db_state
!= DB_NOFILL
) {
3742 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
3743 arc_buf_destroy(dr
->dt
.dl
.dr_data
, db
);
3750 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
3751 ASSERT3U(db
->db
.db_size
, ==, 1 << dn
->dn_phys
->dn_indblkshift
);
3752 if (!BP_IS_HOLE(db
->db_blkptr
)) {
3753 ASSERTV(int epbs
= dn
->dn_phys
->dn_indblkshift
-
3755 ASSERT3U(db
->db_blkid
, <=,
3756 dn
->dn_phys
->dn_maxblkid
>> (db
->db_level
* epbs
));
3757 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==,
3761 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
3762 list_destroy(&dr
->dt
.di
.dr_children
);
3764 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
3766 cv_broadcast(&db
->db_changed
);
3767 ASSERT(db
->db_dirtycnt
> 0);
3768 db
->db_dirtycnt
-= 1;
3769 db
->db_data_pending
= NULL
;
3770 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)tx
->tx_txg
);
3774 dbuf_write_nofill_ready(zio_t
*zio
)
3776 dbuf_write_ready(zio
, NULL
, zio
->io_private
);
3780 dbuf_write_nofill_done(zio_t
*zio
)
3782 dbuf_write_done(zio
, NULL
, zio
->io_private
);
3786 dbuf_write_override_ready(zio_t
*zio
)
3788 dbuf_dirty_record_t
*dr
= zio
->io_private
;
3789 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3791 dbuf_write_ready(zio
, NULL
, db
);
3795 dbuf_write_override_done(zio_t
*zio
)
3797 dbuf_dirty_record_t
*dr
= zio
->io_private
;
3798 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3799 blkptr_t
*obp
= &dr
->dt
.dl
.dr_overridden_by
;
3801 mutex_enter(&db
->db_mtx
);
3802 if (!BP_EQUAL(zio
->io_bp
, obp
)) {
3803 if (!BP_IS_HOLE(obp
))
3804 dsl_free(spa_get_dsl(zio
->io_spa
), zio
->io_txg
, obp
);
3805 arc_release(dr
->dt
.dl
.dr_data
, db
);
3807 mutex_exit(&db
->db_mtx
);
3809 dbuf_write_done(zio
, NULL
, db
);
3811 if (zio
->io_abd
!= NULL
)
3812 abd_put(zio
->io_abd
);
3815 /* Issue I/O to commit a dirty buffer to disk. */
3817 dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
)
3819 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3822 dmu_buf_impl_t
*parent
= db
->db_parent
;
3823 uint64_t txg
= tx
->tx_txg
;
3824 zbookmark_phys_t zb
;
3829 ASSERT(dmu_tx_is_syncing(tx
));
3835 if (db
->db_state
!= DB_NOFILL
) {
3836 if (db
->db_level
> 0 || dn
->dn_type
== DMU_OT_DNODE
) {
3838 * Private object buffers are released here rather
3839 * than in dbuf_dirty() since they are only modified
3840 * in the syncing context and we don't want the
3841 * overhead of making multiple copies of the data.
3843 if (BP_IS_HOLE(db
->db_blkptr
)) {
3846 dbuf_release_bp(db
);
3851 if (parent
!= dn
->dn_dbuf
) {
3852 /* Our parent is an indirect block. */
3853 /* We have a dirty parent that has been scheduled for write. */
3854 ASSERT(parent
&& parent
->db_data_pending
);
3855 /* Our parent's buffer is one level closer to the dnode. */
3856 ASSERT(db
->db_level
== parent
->db_level
-1);
3858 * We're about to modify our parent's db_data by modifying
3859 * our block pointer, so the parent must be released.
3861 ASSERT(arc_released(parent
->db_buf
));
3862 zio
= parent
->db_data_pending
->dr_zio
;
3864 /* Our parent is the dnode itself. */
3865 ASSERT((db
->db_level
== dn
->dn_phys
->dn_nlevels
-1 &&
3866 db
->db_blkid
!= DMU_SPILL_BLKID
) ||
3867 (db
->db_blkid
== DMU_SPILL_BLKID
&& db
->db_level
== 0));
3868 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
3869 ASSERT3P(db
->db_blkptr
, ==,
3870 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
3874 ASSERT(db
->db_level
== 0 || data
== db
->db_buf
);
3875 ASSERT3U(db
->db_blkptr
->blk_birth
, <=, txg
);
3878 SET_BOOKMARK(&zb
, os
->os_dsl_dataset
?
3879 os
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
3880 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
3882 if (db
->db_blkid
== DMU_SPILL_BLKID
)
3884 wp_flag
|= (db
->db_state
== DB_NOFILL
) ? WP_NOFILL
: 0;
3886 dmu_write_policy(os
, dn
, db
->db_level
, wp_flag
, &zp
);
3890 * We copy the blkptr now (rather than when we instantiate the dirty
3891 * record), because its value can change between open context and
3892 * syncing context. We do not need to hold dn_struct_rwlock to read
3893 * db_blkptr because we are in syncing context.
3895 dr
->dr_bp_copy
= *db
->db_blkptr
;
3897 if (db
->db_level
== 0 &&
3898 dr
->dt
.dl
.dr_override_state
== DR_OVERRIDDEN
) {
3900 * The BP for this block has been provided by open context
3901 * (by dmu_sync() or dmu_buf_write_embedded()).
3903 abd_t
*contents
= (data
!= NULL
) ?
3904 abd_get_from_buf(data
->b_data
, arc_buf_size(data
)) : NULL
;
3906 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
,
3907 &dr
->dr_bp_copy
, contents
, db
->db
.db_size
, db
->db
.db_size
,
3908 &zp
, dbuf_write_override_ready
, NULL
, NULL
,
3909 dbuf_write_override_done
,
3910 dr
, ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
3911 mutex_enter(&db
->db_mtx
);
3912 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
3913 zio_write_override(dr
->dr_zio
, &dr
->dt
.dl
.dr_overridden_by
,
3914 dr
->dt
.dl
.dr_copies
, dr
->dt
.dl
.dr_nopwrite
);
3915 mutex_exit(&db
->db_mtx
);
3916 } else if (db
->db_state
== DB_NOFILL
) {
3917 ASSERT(zp
.zp_checksum
== ZIO_CHECKSUM_OFF
||
3918 zp
.zp_checksum
== ZIO_CHECKSUM_NOPARITY
);
3919 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
,
3920 &dr
->dr_bp_copy
, NULL
, db
->db
.db_size
, db
->db
.db_size
, &zp
,
3921 dbuf_write_nofill_ready
, NULL
, NULL
,
3922 dbuf_write_nofill_done
, db
,
3923 ZIO_PRIORITY_ASYNC_WRITE
,
3924 ZIO_FLAG_MUSTSUCCEED
| ZIO_FLAG_NODATA
, &zb
);
3926 ASSERT(arc_released(data
));
3929 * For indirect blocks, we want to setup the children
3930 * ready callback so that we can properly handle an indirect
3931 * block that only contains holes.
3933 arc_write_done_func_t
*children_ready_cb
= NULL
;
3934 if (db
->db_level
!= 0)
3935 children_ready_cb
= dbuf_write_children_ready
;
3937 dr
->dr_zio
= arc_write(zio
, os
->os_spa
, txg
,
3938 &dr
->dr_bp_copy
, data
, DBUF_IS_L2CACHEABLE(db
),
3939 &zp
, dbuf_write_ready
,
3940 children_ready_cb
, dbuf_write_physdone
,
3941 dbuf_write_done
, db
, ZIO_PRIORITY_ASYNC_WRITE
,
3942 ZIO_FLAG_MUSTSUCCEED
, &zb
);
3946 #if defined(_KERNEL) && defined(HAVE_SPL)
3947 EXPORT_SYMBOL(dbuf_find
);
3948 EXPORT_SYMBOL(dbuf_is_metadata
);
3949 EXPORT_SYMBOL(dbuf_destroy
);
3950 EXPORT_SYMBOL(dbuf_loan_arcbuf
);
3951 EXPORT_SYMBOL(dbuf_whichblock
);
3952 EXPORT_SYMBOL(dbuf_read
);
3953 EXPORT_SYMBOL(dbuf_unoverride
);
3954 EXPORT_SYMBOL(dbuf_free_range
);
3955 EXPORT_SYMBOL(dbuf_new_size
);
3956 EXPORT_SYMBOL(dbuf_release_bp
);
3957 EXPORT_SYMBOL(dbuf_dirty
);
3958 EXPORT_SYMBOL(dmu_buf_will_change_crypt_params
);
3959 EXPORT_SYMBOL(dmu_buf_will_dirty
);
3960 EXPORT_SYMBOL(dmu_buf_will_not_fill
);
3961 EXPORT_SYMBOL(dmu_buf_will_fill
);
3962 EXPORT_SYMBOL(dmu_buf_fill_done
);
3963 EXPORT_SYMBOL(dmu_buf_rele
);
3964 EXPORT_SYMBOL(dbuf_assign_arcbuf
);
3965 EXPORT_SYMBOL(dbuf_prefetch
);
3966 EXPORT_SYMBOL(dbuf_hold_impl
);
3967 EXPORT_SYMBOL(dbuf_hold
);
3968 EXPORT_SYMBOL(dbuf_hold_level
);
3969 EXPORT_SYMBOL(dbuf_create_bonus
);
3970 EXPORT_SYMBOL(dbuf_spill_set_blksz
);
3971 EXPORT_SYMBOL(dbuf_rm_spill
);
3972 EXPORT_SYMBOL(dbuf_add_ref
);
3973 EXPORT_SYMBOL(dbuf_rele
);
3974 EXPORT_SYMBOL(dbuf_rele_and_unlock
);
3975 EXPORT_SYMBOL(dbuf_refcount
);
3976 EXPORT_SYMBOL(dbuf_sync_list
);
3977 EXPORT_SYMBOL(dmu_buf_set_user
);
3978 EXPORT_SYMBOL(dmu_buf_set_user_ie
);
3979 EXPORT_SYMBOL(dmu_buf_get_user
);
3980 EXPORT_SYMBOL(dmu_buf_get_blkptr
);
3983 module_param(dbuf_cache_max_bytes
, ulong
, 0644);
3984 MODULE_PARM_DESC(dbuf_cache_max_bytes
,
3985 "Maximum size in bytes of the dbuf cache.");
3987 module_param(dbuf_cache_hiwater_pct
, uint
, 0644);
3988 MODULE_PARM_DESC(dbuf_cache_hiwater_pct
,
3989 "Percentage over dbuf_cache_max_bytes when dbufs must be evicted "
3992 module_param(dbuf_cache_lowater_pct
, uint
, 0644);
3993 MODULE_PARM_DESC(dbuf_cache_lowater_pct
,
3994 "Percentage below dbuf_cache_max_bytes when the evict thread stops "
3997 module_param(dbuf_cache_max_shift
, int, 0644);
3998 MODULE_PARM_DESC(dbuf_cache_max_shift
,
3999 "Cap the size of the dbuf cache to a log2 fraction of arc size.");