4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
25 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
26 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
28 * Copyright (c) 2014 Integros [integros.com]
31 #include <sys/zfs_context.h>
33 #include <sys/dmu_send.h>
34 #include <sys/dmu_impl.h>
36 #include <sys/dmu_objset.h>
37 #include <sys/dsl_dataset.h>
38 #include <sys/dsl_dir.h>
39 #include <sys/dmu_tx.h>
42 #include <sys/dmu_zfetch.h>
44 #include <sys/sa_impl.h>
45 #include <sys/zfeature.h>
46 #include <sys/blkptr.h>
47 #include <sys/range_tree.h>
48 #include <sys/callb.h>
51 uint_t zfs_dbuf_evict_key
;
53 static boolean_t
dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
);
54 static void dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
);
57 extern inline void dmu_buf_init_user(dmu_buf_user_t
*dbu
,
58 dmu_buf_evict_func_t
*evict_func_sync
,
59 dmu_buf_evict_func_t
*evict_func_async
,
60 dmu_buf_t
**clear_on_evict_dbufp
);
64 * Global data structures and functions for the dbuf cache.
66 static kmem_cache_t
*dbuf_kmem_cache
;
67 static taskq_t
*dbu_evict_taskq
;
69 static kthread_t
*dbuf_cache_evict_thread
;
70 static kmutex_t dbuf_evict_lock
;
71 static kcondvar_t dbuf_evict_cv
;
72 static boolean_t dbuf_evict_thread_exit
;
75 * LRU cache of dbufs. The dbuf cache maintains a list of dbufs that
76 * are not currently held but have been recently released. These dbufs
77 * are not eligible for arc eviction until they are aged out of the cache.
78 * Dbufs are added to the dbuf cache once the last hold is released. If a
79 * dbuf is later accessed and still exists in the dbuf cache, then it will
80 * be removed from the cache and later re-added to the head of the cache.
81 * Dbufs that are aged out of the cache will be immediately destroyed and
82 * become eligible for arc eviction.
84 static multilist_t
*dbuf_cache
;
85 static refcount_t dbuf_cache_size
;
86 uint64_t dbuf_cache_max_bytes
= 100 * 1024 * 1024;
88 /* Cap the size of the dbuf cache to log2 fraction of arc size. */
89 int dbuf_cache_max_shift
= 5;
92 * The dbuf cache uses a three-stage eviction policy:
93 * - A low water marker designates when the dbuf eviction thread
94 * should stop evicting from the dbuf cache.
95 * - When we reach the maximum size (aka mid water mark), we
96 * signal the eviction thread to run.
97 * - The high water mark indicates when the eviction thread
98 * is unable to keep up with the incoming load and eviction must
99 * happen in the context of the calling thread.
103 * low water mid water hi water
104 * +----------------------------------------+----------+----------+
109 * +----------------------------------------+----------+----------+
111 * evicting eviction directly
114 * The high and low water marks indicate the operating range for the eviction
115 * thread. The low water mark is, by default, 90% of the total size of the
116 * cache and the high water mark is at 110% (both of these percentages can be
117 * changed by setting dbuf_cache_lowater_pct and dbuf_cache_hiwater_pct,
118 * respectively). The eviction thread will try to ensure that the cache remains
119 * within this range by waking up every second and checking if the cache is
120 * above the low water mark. The thread can also be woken up by callers adding
121 * elements into the cache if the cache is larger than the mid water (i.e max
122 * cache size). Once the eviction thread is woken up and eviction is required,
123 * it will continue evicting buffers until it's able to reduce the cache size
124 * to the low water mark. If the cache size continues to grow and hits the high
125 * water mark, then callers adding elments to the cache will begin to evict
126 * directly from the cache until the cache is no longer above the high water
131 * The percentage above and below the maximum cache size.
133 uint_t dbuf_cache_hiwater_pct
= 10;
134 uint_t dbuf_cache_lowater_pct
= 10;
138 dbuf_cons(void *vdb
, void *unused
, int kmflag
)
140 dmu_buf_impl_t
*db
= vdb
;
141 bzero(db
, sizeof (dmu_buf_impl_t
));
143 mutex_init(&db
->db_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
144 cv_init(&db
->db_changed
, NULL
, CV_DEFAULT
, NULL
);
145 multilist_link_init(&db
->db_cache_link
);
146 refcount_create(&db
->db_holds
);
153 dbuf_dest(void *vdb
, void *unused
)
155 dmu_buf_impl_t
*db
= vdb
;
156 mutex_destroy(&db
->db_mtx
);
157 cv_destroy(&db
->db_changed
);
158 ASSERT(!multilist_link_active(&db
->db_cache_link
));
159 refcount_destroy(&db
->db_holds
);
163 * dbuf hash table routines
165 static dbuf_hash_table_t dbuf_hash_table
;
167 static uint64_t dbuf_hash_count
;
170 dbuf_hash(void *os
, uint64_t obj
, uint8_t lvl
, uint64_t blkid
)
172 uintptr_t osv
= (uintptr_t)os
;
173 uint64_t crc
= -1ULL;
175 ASSERT(zfs_crc64_table
[128] == ZFS_CRC64_POLY
);
176 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (lvl
)) & 0xFF];
177 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (osv
>> 6)) & 0xFF];
178 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 0)) & 0xFF];
179 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 8)) & 0xFF];
180 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (blkid
>> 0)) & 0xFF];
181 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (blkid
>> 8)) & 0xFF];
183 crc
^= (osv
>>14) ^ (obj
>>16) ^ (blkid
>>16);
188 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
189 ((dbuf)->db.db_object == (obj) && \
190 (dbuf)->db_objset == (os) && \
191 (dbuf)->db_level == (level) && \
192 (dbuf)->db_blkid == (blkid))
195 dbuf_find(objset_t
*os
, uint64_t obj
, uint8_t level
, uint64_t blkid
)
197 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
198 uint64_t hv
= dbuf_hash(os
, obj
, level
, blkid
);
199 uint64_t idx
= hv
& h
->hash_table_mask
;
202 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
203 for (db
= h
->hash_table
[idx
]; db
!= NULL
; db
= db
->db_hash_next
) {
204 if (DBUF_EQUAL(db
, os
, obj
, level
, blkid
)) {
205 mutex_enter(&db
->db_mtx
);
206 if (db
->db_state
!= DB_EVICTING
) {
207 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
210 mutex_exit(&db
->db_mtx
);
213 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
217 static dmu_buf_impl_t
*
218 dbuf_find_bonus(objset_t
*os
, uint64_t object
)
221 dmu_buf_impl_t
*db
= NULL
;
223 if (dnode_hold(os
, object
, FTAG
, &dn
) == 0) {
224 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
225 if (dn
->dn_bonus
!= NULL
) {
227 mutex_enter(&db
->db_mtx
);
229 rw_exit(&dn
->dn_struct_rwlock
);
230 dnode_rele(dn
, FTAG
);
236 * Insert an entry into the hash table. If there is already an element
237 * equal to elem in the hash table, then the already existing element
238 * will be returned and the new element will not be inserted.
239 * Otherwise returns NULL.
241 static dmu_buf_impl_t
*
242 dbuf_hash_insert(dmu_buf_impl_t
*db
)
244 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
245 objset_t
*os
= db
->db_objset
;
246 uint64_t obj
= db
->db
.db_object
;
247 int level
= db
->db_level
;
248 uint64_t blkid
= db
->db_blkid
;
249 uint64_t hv
= dbuf_hash(os
, obj
, level
, blkid
);
250 uint64_t idx
= hv
& h
->hash_table_mask
;
253 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
254 for (dbf
= h
->hash_table
[idx
]; dbf
!= NULL
; dbf
= dbf
->db_hash_next
) {
255 if (DBUF_EQUAL(dbf
, os
, obj
, level
, blkid
)) {
256 mutex_enter(&dbf
->db_mtx
);
257 if (dbf
->db_state
!= DB_EVICTING
) {
258 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
261 mutex_exit(&dbf
->db_mtx
);
265 mutex_enter(&db
->db_mtx
);
266 db
->db_hash_next
= h
->hash_table
[idx
];
267 h
->hash_table
[idx
] = db
;
268 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
269 atomic_inc_64(&dbuf_hash_count
);
275 * Remove an entry from the hash table. It must be in the EVICTING state.
278 dbuf_hash_remove(dmu_buf_impl_t
*db
)
280 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
281 uint64_t hv
= dbuf_hash(db
->db_objset
, db
->db
.db_object
,
282 db
->db_level
, db
->db_blkid
);
283 uint64_t idx
= hv
& h
->hash_table_mask
;
284 dmu_buf_impl_t
*dbf
, **dbp
;
287 * We musn't hold db_mtx to maintain lock ordering:
288 * DBUF_HASH_MUTEX > db_mtx.
290 ASSERT(refcount_is_zero(&db
->db_holds
));
291 ASSERT(db
->db_state
== DB_EVICTING
);
292 ASSERT(!MUTEX_HELD(&db
->db_mtx
));
294 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
295 dbp
= &h
->hash_table
[idx
];
296 while ((dbf
= *dbp
) != db
) {
297 dbp
= &dbf
->db_hash_next
;
300 *dbp
= db
->db_hash_next
;
301 db
->db_hash_next
= NULL
;
302 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
303 atomic_dec_64(&dbuf_hash_count
);
309 } dbvu_verify_type_t
;
312 dbuf_verify_user(dmu_buf_impl_t
*db
, dbvu_verify_type_t verify_type
)
317 if (db
->db_user
== NULL
)
320 /* Only data blocks support the attachment of user data. */
321 ASSERT(db
->db_level
== 0);
323 /* Clients must resolve a dbuf before attaching user data. */
324 ASSERT(db
->db
.db_data
!= NULL
);
325 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
327 holds
= refcount_count(&db
->db_holds
);
328 if (verify_type
== DBVU_EVICTING
) {
330 * Immediate eviction occurs when holds == dirtycnt.
331 * For normal eviction buffers, holds is zero on
332 * eviction, except when dbuf_fix_old_data() calls
333 * dbuf_clear_data(). However, the hold count can grow
334 * during eviction even though db_mtx is held (see
335 * dmu_bonus_hold() for an example), so we can only
336 * test the generic invariant that holds >= dirtycnt.
338 ASSERT3U(holds
, >=, db
->db_dirtycnt
);
340 if (db
->db_user_immediate_evict
== TRUE
)
341 ASSERT3U(holds
, >=, db
->db_dirtycnt
);
343 ASSERT3U(holds
, >, 0);
349 dbuf_evict_user(dmu_buf_impl_t
*db
)
351 dmu_buf_user_t
*dbu
= db
->db_user
;
353 ASSERT(MUTEX_HELD(&db
->db_mtx
));
358 dbuf_verify_user(db
, DBVU_EVICTING
);
362 if (dbu
->dbu_clear_on_evict_dbufp
!= NULL
)
363 *dbu
->dbu_clear_on_evict_dbufp
= NULL
;
367 * There are two eviction callbacks - one that we call synchronously
368 * and one that we invoke via a taskq. The async one is useful for
369 * avoiding lock order reversals and limiting stack depth.
371 * Note that if we have a sync callback but no async callback,
372 * it's likely that the sync callback will free the structure
373 * containing the dbu. In that case we need to take care to not
374 * dereference dbu after calling the sync evict func.
376 boolean_t has_async
= (dbu
->dbu_evict_func_async
!= NULL
);
378 if (dbu
->dbu_evict_func_sync
!= NULL
)
379 dbu
->dbu_evict_func_sync(dbu
);
382 taskq_dispatch_ent(dbu_evict_taskq
, dbu
->dbu_evict_func_async
,
383 dbu
, 0, &dbu
->dbu_tqent
);
388 dbuf_is_metadata(dmu_buf_impl_t
*db
)
390 if (db
->db_level
> 0) {
393 boolean_t is_metadata
;
396 is_metadata
= DMU_OT_IS_METADATA(DB_DNODE(db
)->dn_type
);
399 return (is_metadata
);
404 * This function *must* return indices evenly distributed between all
405 * sublists of the multilist. This is needed due to how the dbuf eviction
406 * code is laid out; dbuf_evict_thread() assumes dbufs are evenly
407 * distributed between all sublists and uses this assumption when
408 * deciding which sublist to evict from and how much to evict from it.
411 dbuf_cache_multilist_index_func(multilist_t
*ml
, void *obj
)
413 dmu_buf_impl_t
*db
= obj
;
416 * The assumption here, is the hash value for a given
417 * dmu_buf_impl_t will remain constant throughout it's lifetime
418 * (i.e. it's objset, object, level and blkid fields don't change).
419 * Thus, we don't need to store the dbuf's sublist index
420 * on insertion, as this index can be recalculated on removal.
422 * Also, the low order bits of the hash value are thought to be
423 * distributed evenly. Otherwise, in the case that the multilist
424 * has a power of two number of sublists, each sublists' usage
425 * would not be evenly distributed.
427 return (dbuf_hash(db
->db_objset
, db
->db
.db_object
,
428 db
->db_level
, db
->db_blkid
) %
429 multilist_get_num_sublists(ml
));
432 static inline boolean_t
433 dbuf_cache_above_hiwater(void)
435 uint64_t dbuf_cache_hiwater_bytes
=
436 (dbuf_cache_max_bytes
* dbuf_cache_hiwater_pct
) / 100;
438 return (refcount_count(&dbuf_cache_size
) >
439 dbuf_cache_max_bytes
+ dbuf_cache_hiwater_bytes
);
442 static inline boolean_t
443 dbuf_cache_above_lowater(void)
445 uint64_t dbuf_cache_lowater_bytes
=
446 (dbuf_cache_max_bytes
* dbuf_cache_lowater_pct
) / 100;
448 return (refcount_count(&dbuf_cache_size
) >
449 dbuf_cache_max_bytes
- dbuf_cache_lowater_bytes
);
453 * Evict the oldest eligible dbuf from the dbuf cache.
458 int idx
= multilist_get_random_index(dbuf_cache
);
459 multilist_sublist_t
*mls
= multilist_sublist_lock(dbuf_cache
, idx
);
461 ASSERT(!MUTEX_HELD(&dbuf_evict_lock
));
464 * Set the thread's tsd to indicate that it's processing evictions.
465 * Once a thread stops evicting from the dbuf cache it will
466 * reset its tsd to NULL.
468 ASSERT3P(tsd_get(zfs_dbuf_evict_key
), ==, NULL
);
469 (void) tsd_set(zfs_dbuf_evict_key
, (void *)B_TRUE
);
471 dmu_buf_impl_t
*db
= multilist_sublist_tail(mls
);
472 while (db
!= NULL
&& mutex_tryenter(&db
->db_mtx
) == 0) {
473 db
= multilist_sublist_prev(mls
, db
);
476 DTRACE_PROBE2(dbuf__evict__one
, dmu_buf_impl_t
*, db
,
477 multilist_sublist_t
*, mls
);
480 multilist_sublist_remove(mls
, db
);
481 multilist_sublist_unlock(mls
);
482 (void) refcount_remove_many(&dbuf_cache_size
,
486 multilist_sublist_unlock(mls
);
488 (void) tsd_set(zfs_dbuf_evict_key
, NULL
);
492 * The dbuf evict thread is responsible for aging out dbufs from the
493 * cache. Once the cache has reached it's maximum size, dbufs are removed
494 * and destroyed. The eviction thread will continue running until the size
495 * of the dbuf cache is at or below the maximum size. Once the dbuf is aged
496 * out of the cache it is destroyed and becomes eligible for arc eviction.
499 dbuf_evict_thread(void)
503 CALLB_CPR_INIT(&cpr
, &dbuf_evict_lock
, callb_generic_cpr
, FTAG
);
505 mutex_enter(&dbuf_evict_lock
);
506 while (!dbuf_evict_thread_exit
) {
507 while (!dbuf_cache_above_lowater() && !dbuf_evict_thread_exit
) {
508 CALLB_CPR_SAFE_BEGIN(&cpr
);
509 (void) cv_timedwait_hires(&dbuf_evict_cv
,
510 &dbuf_evict_lock
, SEC2NSEC(1), MSEC2NSEC(1), 0);
511 CALLB_CPR_SAFE_END(&cpr
, &dbuf_evict_lock
);
513 mutex_exit(&dbuf_evict_lock
);
516 * Keep evicting as long as we're above the low water mark
517 * for the cache. We do this without holding the locks to
518 * minimize lock contention.
520 while (dbuf_cache_above_lowater() && !dbuf_evict_thread_exit
) {
524 mutex_enter(&dbuf_evict_lock
);
527 dbuf_evict_thread_exit
= B_FALSE
;
528 cv_broadcast(&dbuf_evict_cv
);
529 CALLB_CPR_EXIT(&cpr
); /* drops dbuf_evict_lock */
534 * Wake up the dbuf eviction thread if the dbuf cache is at its max size.
535 * If the dbuf cache is at its high water mark, then evict a dbuf from the
536 * dbuf cache using the callers context.
539 dbuf_evict_notify(void)
543 * We use thread specific data to track when a thread has
544 * started processing evictions. This allows us to avoid deeply
545 * nested stacks that would have a call flow similar to this:
547 * dbuf_rele()-->dbuf_rele_and_unlock()-->dbuf_evict_notify()
550 * +-----dbuf_destroy()<--dbuf_evict_one()<--------+
552 * The dbuf_eviction_thread will always have its tsd set until
553 * that thread exits. All other threads will only set their tsd
554 * if they are participating in the eviction process. This only
555 * happens if the eviction thread is unable to process evictions
556 * fast enough. To keep the dbuf cache size in check, other threads
557 * can evict from the dbuf cache directly. Those threads will set
558 * their tsd values so that we ensure that they only evict one dbuf
559 * from the dbuf cache.
561 if (tsd_get(zfs_dbuf_evict_key
) != NULL
)
564 if (refcount_count(&dbuf_cache_size
) > dbuf_cache_max_bytes
) {
565 boolean_t evict_now
= B_FALSE
;
567 mutex_enter(&dbuf_evict_lock
);
568 if (refcount_count(&dbuf_cache_size
) > dbuf_cache_max_bytes
) {
569 evict_now
= dbuf_cache_above_hiwater();
570 cv_signal(&dbuf_evict_cv
);
572 mutex_exit(&dbuf_evict_lock
);
583 uint64_t hsize
= 1ULL << 16;
584 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
588 * The hash table is big enough to fill all of physical memory
589 * with an average 4K block size. The table will take up
590 * totalmem*sizeof(void*)/4K (i.e. 2MB/GB with 8-byte pointers).
592 while (hsize
* 4096 < physmem
* PAGESIZE
)
596 h
->hash_table_mask
= hsize
- 1;
597 h
->hash_table
= kmem_zalloc(hsize
* sizeof (void *), KM_NOSLEEP
);
598 if (h
->hash_table
== NULL
) {
599 /* XXX - we should really return an error instead of assert */
600 ASSERT(hsize
> (1ULL << 10));
605 dbuf_kmem_cache
= kmem_cache_create("dmu_buf_impl_t",
606 sizeof (dmu_buf_impl_t
),
607 0, dbuf_cons
, dbuf_dest
, NULL
, NULL
, NULL
, 0);
609 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
610 mutex_init(&h
->hash_mutexes
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
613 * Setup the parameters for the dbuf cache. We cap the size of the
614 * dbuf cache to 1/32nd (default) of the size of the ARC.
616 dbuf_cache_max_bytes
= MIN(dbuf_cache_max_bytes
,
617 arc_max_bytes() >> dbuf_cache_max_shift
);
620 * All entries are queued via taskq_dispatch_ent(), so min/maxalloc
621 * configuration is not required.
623 dbu_evict_taskq
= taskq_create("dbu_evict", 1, minclsyspri
, 0, 0, 0);
625 dbuf_cache
= multilist_create(sizeof (dmu_buf_impl_t
),
626 offsetof(dmu_buf_impl_t
, db_cache_link
),
627 dbuf_cache_multilist_index_func
);
628 refcount_create(&dbuf_cache_size
);
630 tsd_create(&zfs_dbuf_evict_key
, NULL
);
631 dbuf_evict_thread_exit
= B_FALSE
;
632 mutex_init(&dbuf_evict_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
633 cv_init(&dbuf_evict_cv
, NULL
, CV_DEFAULT
, NULL
);
634 dbuf_cache_evict_thread
= thread_create(NULL
, 0, dbuf_evict_thread
,
635 NULL
, 0, &p0
, TS_RUN
, minclsyspri
);
641 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
644 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
645 mutex_destroy(&h
->hash_mutexes
[i
]);
646 kmem_free(h
->hash_table
, (h
->hash_table_mask
+ 1) * sizeof (void *));
647 kmem_cache_destroy(dbuf_kmem_cache
);
648 taskq_destroy(dbu_evict_taskq
);
650 mutex_enter(&dbuf_evict_lock
);
651 dbuf_evict_thread_exit
= B_TRUE
;
652 while (dbuf_evict_thread_exit
) {
653 cv_signal(&dbuf_evict_cv
);
654 cv_wait(&dbuf_evict_cv
, &dbuf_evict_lock
);
656 mutex_exit(&dbuf_evict_lock
);
657 tsd_destroy(&zfs_dbuf_evict_key
);
659 mutex_destroy(&dbuf_evict_lock
);
660 cv_destroy(&dbuf_evict_cv
);
662 refcount_destroy(&dbuf_cache_size
);
663 multilist_destroy(dbuf_cache
);
672 dbuf_verify(dmu_buf_impl_t
*db
)
675 dbuf_dirty_record_t
*dr
;
677 ASSERT(MUTEX_HELD(&db
->db_mtx
));
679 if (!(zfs_flags
& ZFS_DEBUG_DBUF_VERIFY
))
682 ASSERT(db
->db_objset
!= NULL
);
686 ASSERT(db
->db_parent
== NULL
);
687 ASSERT(db
->db_blkptr
== NULL
);
689 ASSERT3U(db
->db
.db_object
, ==, dn
->dn_object
);
690 ASSERT3P(db
->db_objset
, ==, dn
->dn_objset
);
691 ASSERT3U(db
->db_level
, <, dn
->dn_nlevels
);
692 ASSERT(db
->db_blkid
== DMU_BONUS_BLKID
||
693 db
->db_blkid
== DMU_SPILL_BLKID
||
694 !avl_is_empty(&dn
->dn_dbufs
));
696 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
698 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
699 ASSERT3U(db
->db
.db_offset
, ==, DMU_BONUS_BLKID
);
700 } else if (db
->db_blkid
== DMU_SPILL_BLKID
) {
702 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
703 ASSERT0(db
->db
.db_offset
);
705 ASSERT3U(db
->db
.db_offset
, ==, db
->db_blkid
* db
->db
.db_size
);
708 for (dr
= db
->db_data_pending
; dr
!= NULL
; dr
= dr
->dr_next
)
709 ASSERT(dr
->dr_dbuf
== db
);
711 for (dr
= db
->db_last_dirty
; dr
!= NULL
; dr
= dr
->dr_next
)
712 ASSERT(dr
->dr_dbuf
== db
);
715 * We can't assert that db_size matches dn_datablksz because it
716 * can be momentarily different when another thread is doing
719 if (db
->db_level
== 0 && db
->db
.db_object
== DMU_META_DNODE_OBJECT
) {
720 dr
= db
->db_data_pending
;
722 * It should only be modified in syncing context, so
723 * make sure we only have one copy of the data.
725 ASSERT(dr
== NULL
|| dr
->dt
.dl
.dr_data
== db
->db_buf
);
728 /* verify db->db_blkptr */
730 if (db
->db_parent
== dn
->dn_dbuf
) {
731 /* db is pointed to by the dnode */
732 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
733 if (DMU_OBJECT_IS_SPECIAL(db
->db
.db_object
))
734 ASSERT(db
->db_parent
== NULL
);
736 ASSERT(db
->db_parent
!= NULL
);
737 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
738 ASSERT3P(db
->db_blkptr
, ==,
739 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
741 /* db is pointed to by an indirect block */
742 int epb
= db
->db_parent
->db
.db_size
>> SPA_BLKPTRSHIFT
;
743 ASSERT3U(db
->db_parent
->db_level
, ==, db
->db_level
+1);
744 ASSERT3U(db
->db_parent
->db
.db_object
, ==,
747 * dnode_grow_indblksz() can make this fail if we don't
748 * have the struct_rwlock. XXX indblksz no longer
749 * grows. safe to do this now?
751 if (RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
752 ASSERT3P(db
->db_blkptr
, ==,
753 ((blkptr_t
*)db
->db_parent
->db
.db_data
+
754 db
->db_blkid
% epb
));
758 if ((db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
)) &&
759 (db
->db_buf
== NULL
|| db
->db_buf
->b_data
) &&
760 db
->db
.db_data
&& db
->db_blkid
!= DMU_BONUS_BLKID
&&
761 db
->db_state
!= DB_FILL
&& !dn
->dn_free_txg
) {
763 * If the blkptr isn't set but they have nonzero data,
764 * it had better be dirty, otherwise we'll lose that
765 * data when we evict this buffer.
767 * There is an exception to this rule for indirect blocks; in
768 * this case, if the indirect block is a hole, we fill in a few
769 * fields on each of the child blocks (importantly, birth time)
770 * to prevent hole birth times from being lost when you
771 * partially fill in a hole.
773 if (db
->db_dirtycnt
== 0) {
774 if (db
->db_level
== 0) {
775 uint64_t *buf
= db
->db
.db_data
;
778 for (i
= 0; i
< db
->db
.db_size
>> 3; i
++) {
782 blkptr_t
*bps
= db
->db
.db_data
;
783 ASSERT3U(1 << DB_DNODE(db
)->dn_indblkshift
, ==,
786 * We want to verify that all the blkptrs in the
787 * indirect block are holes, but we may have
788 * automatically set up a few fields for them.
789 * We iterate through each blkptr and verify
790 * they only have those fields set.
793 i
< db
->db
.db_size
/ sizeof (blkptr_t
);
795 blkptr_t
*bp
= &bps
[i
];
796 ASSERT(ZIO_CHECKSUM_IS_ZERO(
799 DVA_IS_EMPTY(&bp
->blk_dva
[0]) &&
800 DVA_IS_EMPTY(&bp
->blk_dva
[1]) &&
801 DVA_IS_EMPTY(&bp
->blk_dva
[2]));
802 ASSERT0(bp
->blk_fill
);
803 ASSERT0(bp
->blk_pad
[0]);
804 ASSERT0(bp
->blk_pad
[1]);
805 ASSERT(!BP_IS_EMBEDDED(bp
));
806 ASSERT(BP_IS_HOLE(bp
));
807 ASSERT0(bp
->blk_phys_birth
);
817 dbuf_clear_data(dmu_buf_impl_t
*db
)
819 ASSERT(MUTEX_HELD(&db
->db_mtx
));
821 ASSERT3P(db
->db_buf
, ==, NULL
);
822 db
->db
.db_data
= NULL
;
823 if (db
->db_state
!= DB_NOFILL
)
824 db
->db_state
= DB_UNCACHED
;
828 dbuf_set_data(dmu_buf_impl_t
*db
, arc_buf_t
*buf
)
830 ASSERT(MUTEX_HELD(&db
->db_mtx
));
834 ASSERT(buf
->b_data
!= NULL
);
835 db
->db
.db_data
= buf
->b_data
;
839 * Loan out an arc_buf for read. Return the loaned arc_buf.
842 dbuf_loan_arcbuf(dmu_buf_impl_t
*db
)
846 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
847 mutex_enter(&db
->db_mtx
);
848 if (arc_released(db
->db_buf
) || refcount_count(&db
->db_holds
) > 1) {
849 int blksz
= db
->db
.db_size
;
850 spa_t
*spa
= db
->db_objset
->os_spa
;
852 mutex_exit(&db
->db_mtx
);
853 abuf
= arc_loan_buf(spa
, B_FALSE
, blksz
);
854 bcopy(db
->db
.db_data
, abuf
->b_data
, blksz
);
857 arc_loan_inuse_buf(abuf
, db
);
860 mutex_exit(&db
->db_mtx
);
866 * Calculate which level n block references the data at the level 0 offset
870 dbuf_whichblock(dnode_t
*dn
, int64_t level
, uint64_t offset
)
872 if (dn
->dn_datablkshift
!= 0 && dn
->dn_indblkshift
!= 0) {
874 * The level n blkid is equal to the level 0 blkid divided by
875 * the number of level 0s in a level n block.
877 * The level 0 blkid is offset >> datablkshift =
878 * offset / 2^datablkshift.
880 * The number of level 0s in a level n is the number of block
881 * pointers in an indirect block, raised to the power of level.
882 * This is 2^(indblkshift - SPA_BLKPTRSHIFT)^level =
883 * 2^(level*(indblkshift - SPA_BLKPTRSHIFT)).
885 * Thus, the level n blkid is: offset /
886 * ((2^datablkshift)*(2^(level*(indblkshift - SPA_BLKPTRSHIFT)))
887 * = offset / 2^(datablkshift + level *
888 * (indblkshift - SPA_BLKPTRSHIFT))
889 * = offset >> (datablkshift + level *
890 * (indblkshift - SPA_BLKPTRSHIFT))
892 return (offset
>> (dn
->dn_datablkshift
+ level
*
893 (dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
)));
895 ASSERT3U(offset
, <, dn
->dn_datablksz
);
901 dbuf_read_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
903 dmu_buf_impl_t
*db
= vdb
;
905 mutex_enter(&db
->db_mtx
);
906 ASSERT3U(db
->db_state
, ==, DB_READ
);
908 * All reads are synchronous, so we must have a hold on the dbuf
910 ASSERT(refcount_count(&db
->db_holds
) > 0);
911 ASSERT(db
->db_buf
== NULL
);
912 ASSERT(db
->db
.db_data
== NULL
);
913 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
914 /* we were freed in flight; disregard any error */
915 arc_release(buf
, db
);
916 bzero(buf
->b_data
, db
->db
.db_size
);
918 db
->db_freed_in_flight
= FALSE
;
919 dbuf_set_data(db
, buf
);
920 db
->db_state
= DB_CACHED
;
921 } else if (zio
== NULL
|| zio
->io_error
== 0) {
922 dbuf_set_data(db
, buf
);
923 db
->db_state
= DB_CACHED
;
925 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
926 ASSERT3P(db
->db_buf
, ==, NULL
);
927 arc_buf_destroy(buf
, db
);
928 db
->db_state
= DB_UNCACHED
;
930 cv_broadcast(&db
->db_changed
);
931 dbuf_rele_and_unlock(db
, NULL
);
935 dbuf_read_impl(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t flags
)
939 arc_flags_t aflags
= ARC_FLAG_NOWAIT
;
943 ASSERT(!refcount_is_zero(&db
->db_holds
));
944 /* We need the struct_rwlock to prevent db_blkptr from changing. */
945 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
946 ASSERT(MUTEX_HELD(&db
->db_mtx
));
947 ASSERT(db
->db_state
== DB_UNCACHED
);
948 ASSERT(db
->db_buf
== NULL
);
950 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
951 int bonuslen
= MIN(dn
->dn_bonuslen
, dn
->dn_phys
->dn_bonuslen
);
953 ASSERT3U(bonuslen
, <=, db
->db
.db_size
);
954 db
->db
.db_data
= zio_buf_alloc(DN_MAX_BONUSLEN
);
955 arc_space_consume(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
956 if (bonuslen
< DN_MAX_BONUSLEN
)
957 bzero(db
->db
.db_data
, DN_MAX_BONUSLEN
);
959 bcopy(DN_BONUS(dn
->dn_phys
), db
->db
.db_data
, bonuslen
);
961 db
->db_state
= DB_CACHED
;
962 mutex_exit(&db
->db_mtx
);
967 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
968 * processes the delete record and clears the bp while we are waiting
969 * for the dn_mtx (resulting in a "no" from block_freed).
971 if (db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
) ||
972 (db
->db_level
== 0 && (dnode_block_freed(dn
, db
->db_blkid
) ||
973 BP_IS_HOLE(db
->db_blkptr
)))) {
974 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
976 dbuf_set_data(db
, arc_alloc_buf(db
->db_objset
->os_spa
, db
, type
,
978 bzero(db
->db
.db_data
, db
->db
.db_size
);
980 if (db
->db_blkptr
!= NULL
&& db
->db_level
> 0 &&
981 BP_IS_HOLE(db
->db_blkptr
) &&
982 db
->db_blkptr
->blk_birth
!= 0) {
983 blkptr_t
*bps
= db
->db
.db_data
;
984 for (int i
= 0; i
< ((1 <<
985 DB_DNODE(db
)->dn_indblkshift
) / sizeof (blkptr_t
));
987 blkptr_t
*bp
= &bps
[i
];
988 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==,
989 1 << dn
->dn_indblkshift
);
991 BP_GET_LEVEL(db
->db_blkptr
) == 1 ?
993 BP_GET_LSIZE(db
->db_blkptr
));
994 BP_SET_TYPE(bp
, BP_GET_TYPE(db
->db_blkptr
));
996 BP_GET_LEVEL(db
->db_blkptr
) - 1);
997 BP_SET_BIRTH(bp
, db
->db_blkptr
->blk_birth
, 0);
1001 db
->db_state
= DB_CACHED
;
1002 mutex_exit(&db
->db_mtx
);
1008 db
->db_state
= DB_READ
;
1009 mutex_exit(&db
->db_mtx
);
1011 if (DBUF_IS_L2CACHEABLE(db
))
1012 aflags
|= ARC_FLAG_L2CACHE
;
1014 SET_BOOKMARK(&zb
, db
->db_objset
->os_dsl_dataset
?
1015 db
->db_objset
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
1016 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
1018 dbuf_add_ref(db
, NULL
);
1020 (void) arc_read(zio
, db
->db_objset
->os_spa
, db
->db_blkptr
,
1021 dbuf_read_done
, db
, ZIO_PRIORITY_SYNC_READ
,
1022 (flags
& DB_RF_CANFAIL
) ? ZIO_FLAG_CANFAIL
: ZIO_FLAG_MUSTSUCCEED
,
1027 * This is our just-in-time copy function. It makes a copy of buffers that
1028 * have been modified in a previous transaction group before we access them in
1029 * the current active group.
1031 * This function is used in three places: when we are dirtying a buffer for the
1032 * first time in a txg, when we are freeing a range in a dnode that includes
1033 * this buffer, and when we are accessing a buffer which was received compressed
1034 * and later referenced in a WRITE_BYREF record.
1036 * Note that when we are called from dbuf_free_range() we do not put a hold on
1037 * the buffer, we just traverse the active dbuf list for the dnode.
1040 dbuf_fix_old_data(dmu_buf_impl_t
*db
, uint64_t txg
)
1042 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
1044 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1045 ASSERT(db
->db
.db_data
!= NULL
);
1046 ASSERT(db
->db_level
== 0);
1047 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
);
1050 (dr
->dt
.dl
.dr_data
!=
1051 ((db
->db_blkid
== DMU_BONUS_BLKID
) ? db
->db
.db_data
: db
->db_buf
)))
1055 * If the last dirty record for this dbuf has not yet synced
1056 * and its referencing the dbuf data, either:
1057 * reset the reference to point to a new copy,
1058 * or (if there a no active holders)
1059 * just null out the current db_data pointer.
1061 ASSERT(dr
->dr_txg
>= txg
- 2);
1062 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1063 /* Note that the data bufs here are zio_bufs */
1064 dr
->dt
.dl
.dr_data
= zio_buf_alloc(DN_MAX_BONUSLEN
);
1065 arc_space_consume(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
1066 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
, DN_MAX_BONUSLEN
);
1067 } else if (refcount_count(&db
->db_holds
) > db
->db_dirtycnt
) {
1068 int size
= arc_buf_size(db
->db_buf
);
1069 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1070 spa_t
*spa
= db
->db_objset
->os_spa
;
1071 enum zio_compress compress_type
=
1072 arc_get_compression(db
->db_buf
);
1074 if (compress_type
== ZIO_COMPRESS_OFF
) {
1075 dr
->dt
.dl
.dr_data
= arc_alloc_buf(spa
, db
, type
, size
);
1077 ASSERT3U(type
, ==, ARC_BUFC_DATA
);
1078 dr
->dt
.dl
.dr_data
= arc_alloc_compressed_buf(spa
, db
,
1079 size
, arc_buf_lsize(db
->db_buf
), compress_type
);
1081 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
->b_data
, size
);
1084 dbuf_clear_data(db
);
1089 dbuf_read(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t flags
)
1096 * We don't have to hold the mutex to check db_state because it
1097 * can't be freed while we have a hold on the buffer.
1099 ASSERT(!refcount_is_zero(&db
->db_holds
));
1101 if (db
->db_state
== DB_NOFILL
)
1102 return (SET_ERROR(EIO
));
1106 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1107 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
1109 prefetch
= db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1110 (flags
& DB_RF_NOPREFETCH
) == 0 && dn
!= NULL
&&
1111 DBUF_IS_CACHEABLE(db
);
1113 mutex_enter(&db
->db_mtx
);
1114 if (db
->db_state
== DB_CACHED
) {
1116 * If the arc buf is compressed, we need to decompress it to
1117 * read the data. This could happen during the "zfs receive" of
1118 * a stream which is compressed and deduplicated.
1120 if (db
->db_buf
!= NULL
&&
1121 arc_get_compression(db
->db_buf
) != ZIO_COMPRESS_OFF
) {
1122 dbuf_fix_old_data(db
,
1123 spa_syncing_txg(dmu_objset_spa(db
->db_objset
)));
1124 err
= arc_decompress(db
->db_buf
);
1125 dbuf_set_data(db
, db
->db_buf
);
1127 mutex_exit(&db
->db_mtx
);
1129 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
);
1130 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1131 rw_exit(&dn
->dn_struct_rwlock
);
1133 } else if (db
->db_state
== DB_UNCACHED
) {
1134 spa_t
*spa
= dn
->dn_objset
->os_spa
;
1135 boolean_t need_wait
= B_FALSE
;
1138 db
->db_blkptr
!= NULL
&& !BP_IS_HOLE(db
->db_blkptr
)) {
1139 zio
= zio_root(spa
, NULL
, NULL
, ZIO_FLAG_CANFAIL
);
1142 dbuf_read_impl(db
, zio
, flags
);
1144 /* dbuf_read_impl has dropped db_mtx for us */
1147 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
);
1149 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1150 rw_exit(&dn
->dn_struct_rwlock
);
1154 err
= zio_wait(zio
);
1157 * Another reader came in while the dbuf was in flight
1158 * between UNCACHED and CACHED. Either a writer will finish
1159 * writing the buffer (sending the dbuf to CACHED) or the
1160 * first reader's request will reach the read_done callback
1161 * and send the dbuf to CACHED. Otherwise, a failure
1162 * occurred and the dbuf went to UNCACHED.
1164 mutex_exit(&db
->db_mtx
);
1166 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
);
1167 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1168 rw_exit(&dn
->dn_struct_rwlock
);
1171 /* Skip the wait per the caller's request. */
1172 mutex_enter(&db
->db_mtx
);
1173 if ((flags
& DB_RF_NEVERWAIT
) == 0) {
1174 while (db
->db_state
== DB_READ
||
1175 db
->db_state
== DB_FILL
) {
1176 ASSERT(db
->db_state
== DB_READ
||
1177 (flags
& DB_RF_HAVESTRUCT
) == 0);
1178 DTRACE_PROBE2(blocked__read
, dmu_buf_impl_t
*,
1180 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1182 if (db
->db_state
== DB_UNCACHED
)
1183 err
= SET_ERROR(EIO
);
1185 mutex_exit(&db
->db_mtx
);
1192 dbuf_noread(dmu_buf_impl_t
*db
)
1194 ASSERT(!refcount_is_zero(&db
->db_holds
));
1195 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1196 mutex_enter(&db
->db_mtx
);
1197 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
1198 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1199 if (db
->db_state
== DB_UNCACHED
) {
1200 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1201 spa_t
*spa
= db
->db_objset
->os_spa
;
1203 ASSERT(db
->db_buf
== NULL
);
1204 ASSERT(db
->db
.db_data
== NULL
);
1205 dbuf_set_data(db
, arc_alloc_buf(spa
, db
, type
, db
->db
.db_size
));
1206 db
->db_state
= DB_FILL
;
1207 } else if (db
->db_state
== DB_NOFILL
) {
1208 dbuf_clear_data(db
);
1210 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
1212 mutex_exit(&db
->db_mtx
);
1216 dbuf_unoverride(dbuf_dirty_record_t
*dr
)
1218 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
1219 blkptr_t
*bp
= &dr
->dt
.dl
.dr_overridden_by
;
1220 uint64_t txg
= dr
->dr_txg
;
1222 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1224 * This assert is valid because dmu_sync() expects to be called by
1225 * a zilog's get_data while holding a range lock. This call only
1226 * comes from dbuf_dirty() callers who must also hold a range lock.
1228 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_IN_DMU_SYNC
);
1229 ASSERT(db
->db_level
== 0);
1231 if (db
->db_blkid
== DMU_BONUS_BLKID
||
1232 dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
)
1235 ASSERT(db
->db_data_pending
!= dr
);
1237 /* free this block */
1238 if (!BP_IS_HOLE(bp
) && !dr
->dt
.dl
.dr_nopwrite
)
1239 zio_free(db
->db_objset
->os_spa
, txg
, bp
);
1241 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
1242 dr
->dt
.dl
.dr_nopwrite
= B_FALSE
;
1245 * Release the already-written buffer, so we leave it in
1246 * a consistent dirty state. Note that all callers are
1247 * modifying the buffer, so they will immediately do
1248 * another (redundant) arc_release(). Therefore, leave
1249 * the buf thawed to save the effort of freezing &
1250 * immediately re-thawing it.
1252 arc_release(dr
->dt
.dl
.dr_data
, db
);
1256 * Evict (if its unreferenced) or clear (if its referenced) any level-0
1257 * data blocks in the free range, so that any future readers will find
1261 dbuf_free_range(dnode_t
*dn
, uint64_t start_blkid
, uint64_t end_blkid
,
1264 dmu_buf_impl_t db_search
;
1265 dmu_buf_impl_t
*db
, *db_next
;
1266 uint64_t txg
= tx
->tx_txg
;
1269 if (end_blkid
> dn
->dn_maxblkid
&&
1270 !(start_blkid
== DMU_SPILL_BLKID
|| end_blkid
== DMU_SPILL_BLKID
))
1271 end_blkid
= dn
->dn_maxblkid
;
1272 dprintf_dnode(dn
, "start=%llu end=%llu\n", start_blkid
, end_blkid
);
1274 db_search
.db_level
= 0;
1275 db_search
.db_blkid
= start_blkid
;
1276 db_search
.db_state
= DB_SEARCH
;
1278 mutex_enter(&dn
->dn_dbufs_mtx
);
1279 db
= avl_find(&dn
->dn_dbufs
, &db_search
, &where
);
1280 ASSERT3P(db
, ==, NULL
);
1282 db
= avl_nearest(&dn
->dn_dbufs
, where
, AVL_AFTER
);
1284 for (; db
!= NULL
; db
= db_next
) {
1285 db_next
= AVL_NEXT(&dn
->dn_dbufs
, db
);
1286 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1288 if (db
->db_level
!= 0 || db
->db_blkid
> end_blkid
) {
1291 ASSERT3U(db
->db_blkid
, >=, start_blkid
);
1293 /* found a level 0 buffer in the range */
1294 mutex_enter(&db
->db_mtx
);
1295 if (dbuf_undirty(db
, tx
)) {
1296 /* mutex has been dropped and dbuf destroyed */
1300 if (db
->db_state
== DB_UNCACHED
||
1301 db
->db_state
== DB_NOFILL
||
1302 db
->db_state
== DB_EVICTING
) {
1303 ASSERT(db
->db
.db_data
== NULL
);
1304 mutex_exit(&db
->db_mtx
);
1307 if (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
) {
1308 /* will be handled in dbuf_read_done or dbuf_rele */
1309 db
->db_freed_in_flight
= TRUE
;
1310 mutex_exit(&db
->db_mtx
);
1313 if (refcount_count(&db
->db_holds
) == 0) {
1318 /* The dbuf is referenced */
1320 if (db
->db_last_dirty
!= NULL
) {
1321 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
1323 if (dr
->dr_txg
== txg
) {
1325 * This buffer is "in-use", re-adjust the file
1326 * size to reflect that this buffer may
1327 * contain new data when we sync.
1329 if (db
->db_blkid
!= DMU_SPILL_BLKID
&&
1330 db
->db_blkid
> dn
->dn_maxblkid
)
1331 dn
->dn_maxblkid
= db
->db_blkid
;
1332 dbuf_unoverride(dr
);
1335 * This dbuf is not dirty in the open context.
1336 * Either uncache it (if its not referenced in
1337 * the open context) or reset its contents to
1340 dbuf_fix_old_data(db
, txg
);
1343 /* clear the contents if its cached */
1344 if (db
->db_state
== DB_CACHED
) {
1345 ASSERT(db
->db
.db_data
!= NULL
);
1346 arc_release(db
->db_buf
, db
);
1347 bzero(db
->db
.db_data
, db
->db
.db_size
);
1348 arc_buf_freeze(db
->db_buf
);
1351 mutex_exit(&db
->db_mtx
);
1353 mutex_exit(&dn
->dn_dbufs_mtx
);
1357 dbuf_new_size(dmu_buf_impl_t
*db
, int size
, dmu_tx_t
*tx
)
1359 arc_buf_t
*buf
, *obuf
;
1360 int osize
= db
->db
.db_size
;
1361 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1364 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1369 /* XXX does *this* func really need the lock? */
1370 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
1373 * This call to dmu_buf_will_dirty() with the dn_struct_rwlock held
1374 * is OK, because there can be no other references to the db
1375 * when we are changing its size, so no concurrent DB_FILL can
1379 * XXX we should be doing a dbuf_read, checking the return
1380 * value and returning that up to our callers
1382 dmu_buf_will_dirty(&db
->db
, tx
);
1384 /* create the data buffer for the new block */
1385 buf
= arc_alloc_buf(dn
->dn_objset
->os_spa
, db
, type
, size
);
1387 /* copy old block data to the new block */
1389 bcopy(obuf
->b_data
, buf
->b_data
, MIN(osize
, size
));
1390 /* zero the remainder */
1392 bzero((uint8_t *)buf
->b_data
+ osize
, size
- osize
);
1394 mutex_enter(&db
->db_mtx
);
1395 dbuf_set_data(db
, buf
);
1396 arc_buf_destroy(obuf
, db
);
1397 db
->db
.db_size
= size
;
1399 if (db
->db_level
== 0) {
1400 ASSERT3U(db
->db_last_dirty
->dr_txg
, ==, tx
->tx_txg
);
1401 db
->db_last_dirty
->dt
.dl
.dr_data
= buf
;
1403 mutex_exit(&db
->db_mtx
);
1405 dmu_objset_willuse_space(dn
->dn_objset
, size
- osize
, tx
);
1410 dbuf_release_bp(dmu_buf_impl_t
*db
)
1412 objset_t
*os
= db
->db_objset
;
1414 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os
)));
1415 ASSERT(arc_released(os
->os_phys_buf
) ||
1416 list_link_active(&os
->os_dsl_dataset
->ds_synced_link
));
1417 ASSERT(db
->db_parent
== NULL
|| arc_released(db
->db_parent
->db_buf
));
1419 (void) arc_release(db
->db_buf
, db
);
1423 * We already have a dirty record for this TXG, and we are being
1427 dbuf_redirty(dbuf_dirty_record_t
*dr
)
1429 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
1431 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1433 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
) {
1435 * If this buffer has already been written out,
1436 * we now need to reset its state.
1438 dbuf_unoverride(dr
);
1439 if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
&&
1440 db
->db_state
!= DB_NOFILL
) {
1441 /* Already released on initial dirty, so just thaw. */
1442 ASSERT(arc_released(db
->db_buf
));
1443 arc_buf_thaw(db
->db_buf
);
1448 dbuf_dirty_record_t
*
1449 dbuf_dirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1453 dbuf_dirty_record_t
**drp
, *dr
;
1454 int drop_struct_lock
= FALSE
;
1455 int txgoff
= tx
->tx_txg
& TXG_MASK
;
1457 ASSERT(tx
->tx_txg
!= 0);
1458 ASSERT(!refcount_is_zero(&db
->db_holds
));
1459 DMU_TX_DIRTY_BUF(tx
, db
);
1464 * Shouldn't dirty a regular buffer in syncing context. Private
1465 * objects may be dirtied in syncing context, but only if they
1466 * were already pre-dirtied in open context.
1469 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
) {
1470 rrw_enter(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
,
1473 ASSERT(!dmu_tx_is_syncing(tx
) ||
1474 BP_IS_HOLE(dn
->dn_objset
->os_rootbp
) ||
1475 DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
1476 dn
->dn_objset
->os_dsl_dataset
== NULL
);
1477 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
1478 rrw_exit(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
, FTAG
);
1481 * We make this assert for private objects as well, but after we
1482 * check if we're already dirty. They are allowed to re-dirty
1483 * in syncing context.
1485 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
1486 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
1487 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
1489 mutex_enter(&db
->db_mtx
);
1491 * XXX make this true for indirects too? The problem is that
1492 * transactions created with dmu_tx_create_assigned() from
1493 * syncing context don't bother holding ahead.
1495 ASSERT(db
->db_level
!= 0 ||
1496 db
->db_state
== DB_CACHED
|| db
->db_state
== DB_FILL
||
1497 db
->db_state
== DB_NOFILL
);
1499 mutex_enter(&dn
->dn_mtx
);
1501 * Don't set dirtyctx to SYNC if we're just modifying this as we
1502 * initialize the objset.
1504 if (dn
->dn_dirtyctx
== DN_UNDIRTIED
) {
1505 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
) {
1506 rrw_enter(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
,
1509 if (!BP_IS_HOLE(dn
->dn_objset
->os_rootbp
)) {
1510 dn
->dn_dirtyctx
= (dmu_tx_is_syncing(tx
) ?
1511 DN_DIRTY_SYNC
: DN_DIRTY_OPEN
);
1512 ASSERT(dn
->dn_dirtyctx_firstset
== NULL
);
1513 dn
->dn_dirtyctx_firstset
= kmem_alloc(1, KM_SLEEP
);
1515 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
) {
1516 rrw_exit(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
,
1520 mutex_exit(&dn
->dn_mtx
);
1522 if (db
->db_blkid
== DMU_SPILL_BLKID
)
1523 dn
->dn_have_spill
= B_TRUE
;
1526 * If this buffer is already dirty, we're done.
1528 drp
= &db
->db_last_dirty
;
1529 ASSERT(*drp
== NULL
|| (*drp
)->dr_txg
<= tx
->tx_txg
||
1530 db
->db
.db_object
== DMU_META_DNODE_OBJECT
);
1531 while ((dr
= *drp
) != NULL
&& dr
->dr_txg
> tx
->tx_txg
)
1533 if (dr
&& dr
->dr_txg
== tx
->tx_txg
) {
1537 mutex_exit(&db
->db_mtx
);
1542 * Only valid if not already dirty.
1544 ASSERT(dn
->dn_object
== 0 ||
1545 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
1546 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
1548 ASSERT3U(dn
->dn_nlevels
, >, db
->db_level
);
1549 ASSERT((dn
->dn_phys
->dn_nlevels
== 0 && db
->db_level
== 0) ||
1550 dn
->dn_phys
->dn_nlevels
> db
->db_level
||
1551 dn
->dn_next_nlevels
[txgoff
] > db
->db_level
||
1552 dn
->dn_next_nlevels
[(tx
->tx_txg
-1) & TXG_MASK
] > db
->db_level
||
1553 dn
->dn_next_nlevels
[(tx
->tx_txg
-2) & TXG_MASK
] > db
->db_level
);
1556 * We should only be dirtying in syncing context if it's the
1557 * mos or we're initializing the os or it's a special object.
1558 * However, we are allowed to dirty in syncing context provided
1559 * we already dirtied it in open context. Hence we must make
1560 * this assertion only if we're not already dirty.
1563 VERIFY3U(tx
->tx_txg
, <=, spa_final_dirty_txg(os
->os_spa
));
1565 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
1566 rrw_enter(&os
->os_dsl_dataset
->ds_bp_rwlock
, RW_READER
, FTAG
);
1567 ASSERT(!dmu_tx_is_syncing(tx
) || DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
1568 os
->os_dsl_dataset
== NULL
|| BP_IS_HOLE(os
->os_rootbp
));
1569 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
1570 rrw_exit(&os
->os_dsl_dataset
->ds_bp_rwlock
, FTAG
);
1572 ASSERT(db
->db
.db_size
!= 0);
1574 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
1576 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
1577 dmu_objset_willuse_space(os
, db
->db
.db_size
, tx
);
1581 * If this buffer is dirty in an old transaction group we need
1582 * to make a copy of it so that the changes we make in this
1583 * transaction group won't leak out when we sync the older txg.
1585 dr
= kmem_zalloc(sizeof (dbuf_dirty_record_t
), KM_SLEEP
);
1586 if (db
->db_level
== 0) {
1587 void *data_old
= db
->db_buf
;
1589 if (db
->db_state
!= DB_NOFILL
) {
1590 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1591 dbuf_fix_old_data(db
, tx
->tx_txg
);
1592 data_old
= db
->db
.db_data
;
1593 } else if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
) {
1595 * Release the data buffer from the cache so
1596 * that we can modify it without impacting
1597 * possible other users of this cached data
1598 * block. Note that indirect blocks and
1599 * private objects are not released until the
1600 * syncing state (since they are only modified
1603 arc_release(db
->db_buf
, db
);
1604 dbuf_fix_old_data(db
, tx
->tx_txg
);
1605 data_old
= db
->db_buf
;
1607 ASSERT(data_old
!= NULL
);
1609 dr
->dt
.dl
.dr_data
= data_old
;
1611 mutex_init(&dr
->dt
.di
.dr_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
1612 list_create(&dr
->dt
.di
.dr_children
,
1613 sizeof (dbuf_dirty_record_t
),
1614 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
1616 if (db
->db_blkid
!= DMU_BONUS_BLKID
&& os
->os_dsl_dataset
!= NULL
)
1617 dr
->dr_accounted
= db
->db
.db_size
;
1619 dr
->dr_txg
= tx
->tx_txg
;
1624 * We could have been freed_in_flight between the dbuf_noread
1625 * and dbuf_dirty. We win, as though the dbuf_noread() had
1626 * happened after the free.
1628 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1629 db
->db_blkid
!= DMU_SPILL_BLKID
) {
1630 mutex_enter(&dn
->dn_mtx
);
1631 if (dn
->dn_free_ranges
[txgoff
] != NULL
) {
1632 range_tree_clear(dn
->dn_free_ranges
[txgoff
],
1635 mutex_exit(&dn
->dn_mtx
);
1636 db
->db_freed_in_flight
= FALSE
;
1640 * This buffer is now part of this txg
1642 dbuf_add_ref(db
, (void *)(uintptr_t)tx
->tx_txg
);
1643 db
->db_dirtycnt
+= 1;
1644 ASSERT3U(db
->db_dirtycnt
, <=, 3);
1646 mutex_exit(&db
->db_mtx
);
1648 if (db
->db_blkid
== DMU_BONUS_BLKID
||
1649 db
->db_blkid
== DMU_SPILL_BLKID
) {
1650 mutex_enter(&dn
->dn_mtx
);
1651 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1652 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
1653 mutex_exit(&dn
->dn_mtx
);
1654 dnode_setdirty(dn
, tx
);
1660 * The dn_struct_rwlock prevents db_blkptr from changing
1661 * due to a write from syncing context completing
1662 * while we are running, so we want to acquire it before
1663 * looking at db_blkptr.
1665 if (!RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
1666 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
1667 drop_struct_lock
= TRUE
;
1671 * If we are overwriting a dedup BP, then unless it is snapshotted,
1672 * when we get to syncing context we will need to decrement its
1673 * refcount in the DDT. Prefetch the relevant DDT block so that
1674 * syncing context won't have to wait for the i/o.
1676 ddt_prefetch(os
->os_spa
, db
->db_blkptr
);
1678 if (db
->db_level
== 0) {
1679 dnode_new_blkid(dn
, db
->db_blkid
, tx
, drop_struct_lock
);
1680 ASSERT(dn
->dn_maxblkid
>= db
->db_blkid
);
1683 if (db
->db_level
+1 < dn
->dn_nlevels
) {
1684 dmu_buf_impl_t
*parent
= db
->db_parent
;
1685 dbuf_dirty_record_t
*di
;
1686 int parent_held
= FALSE
;
1688 if (db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
) {
1689 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1691 parent
= dbuf_hold_level(dn
, db
->db_level
+1,
1692 db
->db_blkid
>> epbs
, FTAG
);
1693 ASSERT(parent
!= NULL
);
1696 if (drop_struct_lock
)
1697 rw_exit(&dn
->dn_struct_rwlock
);
1698 ASSERT3U(db
->db_level
+1, ==, parent
->db_level
);
1699 di
= dbuf_dirty(parent
, tx
);
1701 dbuf_rele(parent
, FTAG
);
1703 mutex_enter(&db
->db_mtx
);
1705 * Since we've dropped the mutex, it's possible that
1706 * dbuf_undirty() might have changed this out from under us.
1708 if (db
->db_last_dirty
== dr
||
1709 dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
1710 mutex_enter(&di
->dt
.di
.dr_mtx
);
1711 ASSERT3U(di
->dr_txg
, ==, tx
->tx_txg
);
1712 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1713 list_insert_tail(&di
->dt
.di
.dr_children
, dr
);
1714 mutex_exit(&di
->dt
.di
.dr_mtx
);
1717 mutex_exit(&db
->db_mtx
);
1719 ASSERT(db
->db_level
+1 == dn
->dn_nlevels
);
1720 ASSERT(db
->db_blkid
< dn
->dn_nblkptr
);
1721 ASSERT(db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
1722 mutex_enter(&dn
->dn_mtx
);
1723 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1724 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
1725 mutex_exit(&dn
->dn_mtx
);
1726 if (drop_struct_lock
)
1727 rw_exit(&dn
->dn_struct_rwlock
);
1730 dnode_setdirty(dn
, tx
);
1736 * Undirty a buffer in the transaction group referenced by the given
1737 * transaction. Return whether this evicted the dbuf.
1740 dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1743 uint64_t txg
= tx
->tx_txg
;
1744 dbuf_dirty_record_t
*dr
, **drp
;
1749 * Due to our use of dn_nlevels below, this can only be called
1750 * in open context, unless we are operating on the MOS.
1751 * From syncing context, dn_nlevels may be different from the
1752 * dn_nlevels used when dbuf was dirtied.
1754 ASSERT(db
->db_objset
==
1755 dmu_objset_pool(db
->db_objset
)->dp_meta_objset
||
1756 txg
!= spa_syncing_txg(dmu_objset_spa(db
->db_objset
)));
1757 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1758 ASSERT0(db
->db_level
);
1759 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1762 * If this buffer is not dirty, we're done.
1764 for (drp
= &db
->db_last_dirty
; (dr
= *drp
) != NULL
; drp
= &dr
->dr_next
)
1765 if (dr
->dr_txg
<= txg
)
1767 if (dr
== NULL
|| dr
->dr_txg
< txg
)
1769 ASSERT(dr
->dr_txg
== txg
);
1770 ASSERT(dr
->dr_dbuf
== db
);
1775 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
1777 ASSERT(db
->db
.db_size
!= 0);
1779 dsl_pool_undirty_space(dmu_objset_pool(dn
->dn_objset
),
1780 dr
->dr_accounted
, txg
);
1785 * Note that there are three places in dbuf_dirty()
1786 * where this dirty record may be put on a list.
1787 * Make sure to do a list_remove corresponding to
1788 * every one of those list_insert calls.
1790 if (dr
->dr_parent
) {
1791 mutex_enter(&dr
->dr_parent
->dt
.di
.dr_mtx
);
1792 list_remove(&dr
->dr_parent
->dt
.di
.dr_children
, dr
);
1793 mutex_exit(&dr
->dr_parent
->dt
.di
.dr_mtx
);
1794 } else if (db
->db_blkid
== DMU_SPILL_BLKID
||
1795 db
->db_level
+ 1 == dn
->dn_nlevels
) {
1796 ASSERT(db
->db_blkptr
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
1797 mutex_enter(&dn
->dn_mtx
);
1798 list_remove(&dn
->dn_dirty_records
[txg
& TXG_MASK
], dr
);
1799 mutex_exit(&dn
->dn_mtx
);
1803 if (db
->db_state
!= DB_NOFILL
) {
1804 dbuf_unoverride(dr
);
1806 ASSERT(db
->db_buf
!= NULL
);
1807 ASSERT(dr
->dt
.dl
.dr_data
!= NULL
);
1808 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
1809 arc_buf_destroy(dr
->dt
.dl
.dr_data
, db
);
1812 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
1814 ASSERT(db
->db_dirtycnt
> 0);
1815 db
->db_dirtycnt
-= 1;
1817 if (refcount_remove(&db
->db_holds
, (void *)(uintptr_t)txg
) == 0) {
1818 ASSERT(db
->db_state
== DB_NOFILL
|| arc_released(db
->db_buf
));
1827 dmu_buf_will_dirty(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1829 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1830 int rf
= DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
;
1832 ASSERT(tx
->tx_txg
!= 0);
1833 ASSERT(!refcount_is_zero(&db
->db_holds
));
1836 * Quick check for dirtyness. For already dirty blocks, this
1837 * reduces runtime of this function by >90%, and overall performance
1838 * by 50% for some workloads (e.g. file deletion with indirect blocks
1841 mutex_enter(&db
->db_mtx
);
1842 dbuf_dirty_record_t
*dr
;
1843 for (dr
= db
->db_last_dirty
;
1844 dr
!= NULL
&& dr
->dr_txg
>= tx
->tx_txg
; dr
= dr
->dr_next
) {
1846 * It's possible that it is already dirty but not cached,
1847 * because there are some calls to dbuf_dirty() that don't
1848 * go through dmu_buf_will_dirty().
1850 if (dr
->dr_txg
== tx
->tx_txg
&& db
->db_state
== DB_CACHED
) {
1851 /* This dbuf is already dirty and cached. */
1853 mutex_exit(&db
->db_mtx
);
1857 mutex_exit(&db
->db_mtx
);
1860 if (RW_WRITE_HELD(&DB_DNODE(db
)->dn_struct_rwlock
))
1861 rf
|= DB_RF_HAVESTRUCT
;
1863 (void) dbuf_read(db
, NULL
, rf
);
1864 (void) dbuf_dirty(db
, tx
);
1868 dmu_buf_will_not_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1870 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1872 db
->db_state
= DB_NOFILL
;
1874 dmu_buf_will_fill(db_fake
, tx
);
1878 dmu_buf_will_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1880 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1882 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1883 ASSERT(tx
->tx_txg
!= 0);
1884 ASSERT(db
->db_level
== 0);
1885 ASSERT(!refcount_is_zero(&db
->db_holds
));
1887 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
||
1888 dmu_tx_private_ok(tx
));
1891 (void) dbuf_dirty(db
, tx
);
1894 #pragma weak dmu_buf_fill_done = dbuf_fill_done
1897 dbuf_fill_done(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1899 mutex_enter(&db
->db_mtx
);
1902 if (db
->db_state
== DB_FILL
) {
1903 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
1904 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1905 /* we were freed while filling */
1906 /* XXX dbuf_undirty? */
1907 bzero(db
->db
.db_data
, db
->db
.db_size
);
1908 db
->db_freed_in_flight
= FALSE
;
1910 db
->db_state
= DB_CACHED
;
1911 cv_broadcast(&db
->db_changed
);
1913 mutex_exit(&db
->db_mtx
);
1917 dmu_buf_write_embedded(dmu_buf_t
*dbuf
, void *data
,
1918 bp_embedded_type_t etype
, enum zio_compress comp
,
1919 int uncompressed_size
, int compressed_size
, int byteorder
,
1922 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbuf
;
1923 struct dirty_leaf
*dl
;
1924 dmu_object_type_t type
;
1926 if (etype
== BP_EMBEDDED_TYPE_DATA
) {
1927 ASSERT(spa_feature_is_active(dmu_objset_spa(db
->db_objset
),
1928 SPA_FEATURE_EMBEDDED_DATA
));
1932 type
= DB_DNODE(db
)->dn_type
;
1935 ASSERT0(db
->db_level
);
1936 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1938 dmu_buf_will_not_fill(dbuf
, tx
);
1940 ASSERT3U(db
->db_last_dirty
->dr_txg
, ==, tx
->tx_txg
);
1941 dl
= &db
->db_last_dirty
->dt
.dl
;
1942 encode_embedded_bp_compressed(&dl
->dr_overridden_by
,
1943 data
, comp
, uncompressed_size
, compressed_size
);
1944 BPE_SET_ETYPE(&dl
->dr_overridden_by
, etype
);
1945 BP_SET_TYPE(&dl
->dr_overridden_by
, type
);
1946 BP_SET_LEVEL(&dl
->dr_overridden_by
, 0);
1947 BP_SET_BYTEORDER(&dl
->dr_overridden_by
, byteorder
);
1949 dl
->dr_override_state
= DR_OVERRIDDEN
;
1950 dl
->dr_overridden_by
.blk_birth
= db
->db_last_dirty
->dr_txg
;
1954 * Directly assign a provided arc buf to a given dbuf if it's not referenced
1955 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
1958 dbuf_assign_arcbuf(dmu_buf_impl_t
*db
, arc_buf_t
*buf
, dmu_tx_t
*tx
)
1960 ASSERT(!refcount_is_zero(&db
->db_holds
));
1961 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1962 ASSERT(db
->db_level
== 0);
1963 ASSERT3U(dbuf_is_metadata(db
), ==, arc_is_metadata(buf
));
1964 ASSERT(buf
!= NULL
);
1965 ASSERT(arc_buf_lsize(buf
) == db
->db
.db_size
);
1966 ASSERT(tx
->tx_txg
!= 0);
1968 arc_return_buf(buf
, db
);
1969 ASSERT(arc_released(buf
));
1971 mutex_enter(&db
->db_mtx
);
1973 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
1974 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1976 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_UNCACHED
);
1978 if (db
->db_state
== DB_CACHED
&&
1979 refcount_count(&db
->db_holds
) - 1 > db
->db_dirtycnt
) {
1980 mutex_exit(&db
->db_mtx
);
1981 (void) dbuf_dirty(db
, tx
);
1982 bcopy(buf
->b_data
, db
->db
.db_data
, db
->db
.db_size
);
1983 arc_buf_destroy(buf
, db
);
1984 xuio_stat_wbuf_copied();
1988 xuio_stat_wbuf_nocopy();
1989 if (db
->db_state
== DB_CACHED
) {
1990 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
1992 ASSERT(db
->db_buf
!= NULL
);
1993 if (dr
!= NULL
&& dr
->dr_txg
== tx
->tx_txg
) {
1994 ASSERT(dr
->dt
.dl
.dr_data
== db
->db_buf
);
1995 if (!arc_released(db
->db_buf
)) {
1996 ASSERT(dr
->dt
.dl
.dr_override_state
==
1998 arc_release(db
->db_buf
, db
);
2000 dr
->dt
.dl
.dr_data
= buf
;
2001 arc_buf_destroy(db
->db_buf
, db
);
2002 } else if (dr
== NULL
|| dr
->dt
.dl
.dr_data
!= db
->db_buf
) {
2003 arc_release(db
->db_buf
, db
);
2004 arc_buf_destroy(db
->db_buf
, db
);
2008 ASSERT(db
->db_buf
== NULL
);
2009 dbuf_set_data(db
, buf
);
2010 db
->db_state
= DB_FILL
;
2011 mutex_exit(&db
->db_mtx
);
2012 (void) dbuf_dirty(db
, tx
);
2013 dmu_buf_fill_done(&db
->db
, tx
);
2017 dbuf_destroy(dmu_buf_impl_t
*db
)
2020 dmu_buf_impl_t
*parent
= db
->db_parent
;
2021 dmu_buf_impl_t
*dndb
;
2023 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2024 ASSERT(refcount_is_zero(&db
->db_holds
));
2026 if (db
->db_buf
!= NULL
) {
2027 arc_buf_destroy(db
->db_buf
, db
);
2031 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2032 ASSERT(db
->db
.db_data
!= NULL
);
2033 zio_buf_free(db
->db
.db_data
, DN_MAX_BONUSLEN
);
2034 arc_space_return(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
2035 db
->db_state
= DB_UNCACHED
;
2038 dbuf_clear_data(db
);
2040 if (multilist_link_active(&db
->db_cache_link
)) {
2041 multilist_remove(dbuf_cache
, db
);
2042 (void) refcount_remove_many(&dbuf_cache_size
,
2043 db
->db
.db_size
, db
);
2046 ASSERT(db
->db_state
== DB_UNCACHED
|| db
->db_state
== DB_NOFILL
);
2047 ASSERT(db
->db_data_pending
== NULL
);
2049 db
->db_state
= DB_EVICTING
;
2050 db
->db_blkptr
= NULL
;
2053 * Now that db_state is DB_EVICTING, nobody else can find this via
2054 * the hash table. We can now drop db_mtx, which allows us to
2055 * acquire the dn_dbufs_mtx.
2057 mutex_exit(&db
->db_mtx
);
2062 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
2063 boolean_t needlock
= !MUTEX_HELD(&dn
->dn_dbufs_mtx
);
2065 mutex_enter(&dn
->dn_dbufs_mtx
);
2066 avl_remove(&dn
->dn_dbufs
, db
);
2067 atomic_dec_32(&dn
->dn_dbufs_count
);
2071 mutex_exit(&dn
->dn_dbufs_mtx
);
2073 * Decrementing the dbuf count means that the hold corresponding
2074 * to the removed dbuf is no longer discounted in dnode_move(),
2075 * so the dnode cannot be moved until after we release the hold.
2076 * The membar_producer() ensures visibility of the decremented
2077 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
2081 db
->db_dnode_handle
= NULL
;
2083 dbuf_hash_remove(db
);
2088 ASSERT(refcount_is_zero(&db
->db_holds
));
2090 db
->db_parent
= NULL
;
2092 ASSERT(db
->db_buf
== NULL
);
2093 ASSERT(db
->db
.db_data
== NULL
);
2094 ASSERT(db
->db_hash_next
== NULL
);
2095 ASSERT(db
->db_blkptr
== NULL
);
2096 ASSERT(db
->db_data_pending
== NULL
);
2097 ASSERT(!multilist_link_active(&db
->db_cache_link
));
2099 kmem_cache_free(dbuf_kmem_cache
, db
);
2100 arc_space_return(sizeof (dmu_buf_impl_t
), ARC_SPACE_OTHER
);
2103 * If this dbuf is referenced from an indirect dbuf,
2104 * decrement the ref count on the indirect dbuf.
2106 if (parent
&& parent
!= dndb
)
2107 dbuf_rele(parent
, db
);
2111 * Note: While bpp will always be updated if the function returns success,
2112 * parentp will not be updated if the dnode does not have dn_dbuf filled in;
2113 * this happens when the dnode is the meta-dnode, or a userused or groupused
2117 dbuf_findbp(dnode_t
*dn
, int level
, uint64_t blkid
, int fail_sparse
,
2118 dmu_buf_impl_t
**parentp
, blkptr_t
**bpp
)
2123 ASSERT(blkid
!= DMU_BONUS_BLKID
);
2125 if (blkid
== DMU_SPILL_BLKID
) {
2126 mutex_enter(&dn
->dn_mtx
);
2127 if (dn
->dn_have_spill
&&
2128 (dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
))
2129 *bpp
= &dn
->dn_phys
->dn_spill
;
2132 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
2133 *parentp
= dn
->dn_dbuf
;
2134 mutex_exit(&dn
->dn_mtx
);
2139 (dn
->dn_phys
->dn_nlevels
== 0) ? 1 : dn
->dn_phys
->dn_nlevels
;
2140 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2142 ASSERT3U(level
* epbs
, <, 64);
2143 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2145 * This assertion shouldn't trip as long as the max indirect block size
2146 * is less than 1M. The reason for this is that up to that point,
2147 * the number of levels required to address an entire object with blocks
2148 * of size SPA_MINBLOCKSIZE satisfies nlevels * epbs + 1 <= 64. In
2149 * other words, if N * epbs + 1 > 64, then if (N-1) * epbs + 1 > 55
2150 * (i.e. we can address the entire object), objects will all use at most
2151 * N-1 levels and the assertion won't overflow. However, once epbs is
2152 * 13, 4 * 13 + 1 = 53, but 5 * 13 + 1 = 66. Then, 4 levels will not be
2153 * enough to address an entire object, so objects will have 5 levels,
2154 * but then this assertion will overflow.
2156 * All this is to say that if we ever increase DN_MAX_INDBLKSHIFT, we
2157 * need to redo this logic to handle overflows.
2159 ASSERT(level
>= nlevels
||
2160 ((nlevels
- level
- 1) * epbs
) +
2161 highbit64(dn
->dn_phys
->dn_nblkptr
) <= 64);
2162 if (level
>= nlevels
||
2163 blkid
>= ((uint64_t)dn
->dn_phys
->dn_nblkptr
<<
2164 ((nlevels
- level
- 1) * epbs
)) ||
2166 blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))) {
2167 /* the buffer has no parent yet */
2168 return (SET_ERROR(ENOENT
));
2169 } else if (level
< nlevels
-1) {
2170 /* this block is referenced from an indirect block */
2171 int err
= dbuf_hold_impl(dn
, level
+1,
2172 blkid
>> epbs
, fail_sparse
, FALSE
, NULL
, parentp
);
2175 err
= dbuf_read(*parentp
, NULL
,
2176 (DB_RF_HAVESTRUCT
| DB_RF_NOPREFETCH
| DB_RF_CANFAIL
));
2178 dbuf_rele(*parentp
, NULL
);
2182 *bpp
= ((blkptr_t
*)(*parentp
)->db
.db_data
) +
2183 (blkid
& ((1ULL << epbs
) - 1));
2184 if (blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))
2185 ASSERT(BP_IS_HOLE(*bpp
));
2188 /* the block is referenced from the dnode */
2189 ASSERT3U(level
, ==, nlevels
-1);
2190 ASSERT(dn
->dn_phys
->dn_nblkptr
== 0 ||
2191 blkid
< dn
->dn_phys
->dn_nblkptr
);
2193 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
2194 *parentp
= dn
->dn_dbuf
;
2196 *bpp
= &dn
->dn_phys
->dn_blkptr
[blkid
];
2201 static dmu_buf_impl_t
*
2202 dbuf_create(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
2203 dmu_buf_impl_t
*parent
, blkptr_t
*blkptr
)
2205 objset_t
*os
= dn
->dn_objset
;
2206 dmu_buf_impl_t
*db
, *odb
;
2208 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2209 ASSERT(dn
->dn_type
!= DMU_OT_NONE
);
2211 db
= kmem_cache_alloc(dbuf_kmem_cache
, KM_SLEEP
);
2214 db
->db
.db_object
= dn
->dn_object
;
2215 db
->db_level
= level
;
2216 db
->db_blkid
= blkid
;
2217 db
->db_last_dirty
= NULL
;
2218 db
->db_dirtycnt
= 0;
2219 db
->db_dnode_handle
= dn
->dn_handle
;
2220 db
->db_parent
= parent
;
2221 db
->db_blkptr
= blkptr
;
2224 db
->db_user_immediate_evict
= FALSE
;
2225 db
->db_freed_in_flight
= FALSE
;
2226 db
->db_pending_evict
= FALSE
;
2228 if (blkid
== DMU_BONUS_BLKID
) {
2229 ASSERT3P(parent
, ==, dn
->dn_dbuf
);
2230 db
->db
.db_size
= DN_MAX_BONUSLEN
-
2231 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
);
2232 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
2233 db
->db
.db_offset
= DMU_BONUS_BLKID
;
2234 db
->db_state
= DB_UNCACHED
;
2235 /* the bonus dbuf is not placed in the hash table */
2236 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_OTHER
);
2238 } else if (blkid
== DMU_SPILL_BLKID
) {
2239 db
->db
.db_size
= (blkptr
!= NULL
) ?
2240 BP_GET_LSIZE(blkptr
) : SPA_MINBLOCKSIZE
;
2241 db
->db
.db_offset
= 0;
2244 db
->db_level
? 1 << dn
->dn_indblkshift
: dn
->dn_datablksz
;
2245 db
->db
.db_size
= blocksize
;
2246 db
->db
.db_offset
= db
->db_blkid
* blocksize
;
2250 * Hold the dn_dbufs_mtx while we get the new dbuf
2251 * in the hash table *and* added to the dbufs list.
2252 * This prevents a possible deadlock with someone
2253 * trying to look up this dbuf before its added to the
2256 mutex_enter(&dn
->dn_dbufs_mtx
);
2257 db
->db_state
= DB_EVICTING
;
2258 if ((odb
= dbuf_hash_insert(db
)) != NULL
) {
2259 /* someone else inserted it first */
2260 kmem_cache_free(dbuf_kmem_cache
, db
);
2261 mutex_exit(&dn
->dn_dbufs_mtx
);
2264 avl_add(&dn
->dn_dbufs
, db
);
2266 db
->db_state
= DB_UNCACHED
;
2267 mutex_exit(&dn
->dn_dbufs_mtx
);
2268 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_OTHER
);
2270 if (parent
&& parent
!= dn
->dn_dbuf
)
2271 dbuf_add_ref(parent
, db
);
2273 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
2274 refcount_count(&dn
->dn_holds
) > 0);
2275 (void) refcount_add(&dn
->dn_holds
, db
);
2276 atomic_inc_32(&dn
->dn_dbufs_count
);
2278 dprintf_dbuf(db
, "db=%p\n", db
);
2283 typedef struct dbuf_prefetch_arg
{
2284 spa_t
*dpa_spa
; /* The spa to issue the prefetch in. */
2285 zbookmark_phys_t dpa_zb
; /* The target block to prefetch. */
2286 int dpa_epbs
; /* Entries (blkptr_t's) Per Block Shift. */
2287 int dpa_curlevel
; /* The current level that we're reading */
2288 dnode_t
*dpa_dnode
; /* The dnode associated with the prefetch */
2289 zio_priority_t dpa_prio
; /* The priority I/Os should be issued at. */
2290 zio_t
*dpa_zio
; /* The parent zio_t for all prefetches. */
2291 arc_flags_t dpa_aflags
; /* Flags to pass to the final prefetch. */
2292 } dbuf_prefetch_arg_t
;
2295 * Actually issue the prefetch read for the block given.
2298 dbuf_issue_final_prefetch(dbuf_prefetch_arg_t
*dpa
, blkptr_t
*bp
)
2300 if (BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
))
2303 arc_flags_t aflags
=
2304 dpa
->dpa_aflags
| ARC_FLAG_NOWAIT
| ARC_FLAG_PREFETCH
;
2306 ASSERT3U(dpa
->dpa_curlevel
, ==, BP_GET_LEVEL(bp
));
2307 ASSERT3U(dpa
->dpa_curlevel
, ==, dpa
->dpa_zb
.zb_level
);
2308 ASSERT(dpa
->dpa_zio
!= NULL
);
2309 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
, bp
, NULL
, NULL
,
2310 dpa
->dpa_prio
, ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
2311 &aflags
, &dpa
->dpa_zb
);
2315 * Called when an indirect block above our prefetch target is read in. This
2316 * will either read in the next indirect block down the tree or issue the actual
2317 * prefetch if the next block down is our target.
2320 dbuf_prefetch_indirect_done(zio_t
*zio
, arc_buf_t
*abuf
, void *private)
2322 dbuf_prefetch_arg_t
*dpa
= private;
2324 ASSERT3S(dpa
->dpa_zb
.zb_level
, <, dpa
->dpa_curlevel
);
2325 ASSERT3S(dpa
->dpa_curlevel
, >, 0);
2328 * The dpa_dnode is only valid if we are called with a NULL
2329 * zio. This indicates that the arc_read() returned without
2330 * first calling zio_read() to issue a physical read. Once
2331 * a physical read is made the dpa_dnode must be invalidated
2332 * as the locks guarding it may have been dropped. If the
2333 * dpa_dnode is still valid, then we want to add it to the dbuf
2334 * cache. To do so, we must hold the dbuf associated with the block
2335 * we just prefetched, read its contents so that we associate it
2336 * with an arc_buf_t, and then release it.
2339 ASSERT3S(BP_GET_LEVEL(zio
->io_bp
), ==, dpa
->dpa_curlevel
);
2340 if (zio
->io_flags
& ZIO_FLAG_RAW
) {
2341 ASSERT3U(BP_GET_PSIZE(zio
->io_bp
), ==, zio
->io_size
);
2343 ASSERT3U(BP_GET_LSIZE(zio
->io_bp
), ==, zio
->io_size
);
2345 ASSERT3P(zio
->io_spa
, ==, dpa
->dpa_spa
);
2347 dpa
->dpa_dnode
= NULL
;
2348 } else if (dpa
->dpa_dnode
!= NULL
) {
2349 uint64_t curblkid
= dpa
->dpa_zb
.zb_blkid
>>
2350 (dpa
->dpa_epbs
* (dpa
->dpa_curlevel
-
2351 dpa
->dpa_zb
.zb_level
));
2352 dmu_buf_impl_t
*db
= dbuf_hold_level(dpa
->dpa_dnode
,
2353 dpa
->dpa_curlevel
, curblkid
, FTAG
);
2354 (void) dbuf_read(db
, NULL
,
2355 DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
| DB_RF_HAVESTRUCT
);
2356 dbuf_rele(db
, FTAG
);
2359 dpa
->dpa_curlevel
--;
2361 uint64_t nextblkid
= dpa
->dpa_zb
.zb_blkid
>>
2362 (dpa
->dpa_epbs
* (dpa
->dpa_curlevel
- dpa
->dpa_zb
.zb_level
));
2363 blkptr_t
*bp
= ((blkptr_t
*)abuf
->b_data
) +
2364 P2PHASE(nextblkid
, 1ULL << dpa
->dpa_epbs
);
2365 if (BP_IS_HOLE(bp
) || (zio
!= NULL
&& zio
->io_error
!= 0)) {
2366 kmem_free(dpa
, sizeof (*dpa
));
2367 } else if (dpa
->dpa_curlevel
== dpa
->dpa_zb
.zb_level
) {
2368 ASSERT3U(nextblkid
, ==, dpa
->dpa_zb
.zb_blkid
);
2369 dbuf_issue_final_prefetch(dpa
, bp
);
2370 kmem_free(dpa
, sizeof (*dpa
));
2372 arc_flags_t iter_aflags
= ARC_FLAG_NOWAIT
;
2373 zbookmark_phys_t zb
;
2375 ASSERT3U(dpa
->dpa_curlevel
, ==, BP_GET_LEVEL(bp
));
2377 SET_BOOKMARK(&zb
, dpa
->dpa_zb
.zb_objset
,
2378 dpa
->dpa_zb
.zb_object
, dpa
->dpa_curlevel
, nextblkid
);
2380 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
,
2381 bp
, dbuf_prefetch_indirect_done
, dpa
, dpa
->dpa_prio
,
2382 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
2386 arc_buf_destroy(abuf
, private);
2390 * Issue prefetch reads for the given block on the given level. If the indirect
2391 * blocks above that block are not in memory, we will read them in
2392 * asynchronously. As a result, this call never blocks waiting for a read to
2396 dbuf_prefetch(dnode_t
*dn
, int64_t level
, uint64_t blkid
, zio_priority_t prio
,
2400 int epbs
, nlevels
, curlevel
;
2403 ASSERT(blkid
!= DMU_BONUS_BLKID
);
2404 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2406 if (blkid
> dn
->dn_maxblkid
)
2409 if (dnode_block_freed(dn
, blkid
))
2413 * This dnode hasn't been written to disk yet, so there's nothing to
2416 nlevels
= dn
->dn_phys
->dn_nlevels
;
2417 if (level
>= nlevels
|| dn
->dn_phys
->dn_nblkptr
== 0)
2420 epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2421 if (dn
->dn_phys
->dn_maxblkid
< blkid
<< (epbs
* level
))
2424 dmu_buf_impl_t
*db
= dbuf_find(dn
->dn_objset
, dn
->dn_object
,
2427 mutex_exit(&db
->db_mtx
);
2429 * This dbuf already exists. It is either CACHED, or
2430 * (we assume) about to be read or filled.
2436 * Find the closest ancestor (indirect block) of the target block
2437 * that is present in the cache. In this indirect block, we will
2438 * find the bp that is at curlevel, curblkid.
2442 while (curlevel
< nlevels
- 1) {
2443 int parent_level
= curlevel
+ 1;
2444 uint64_t parent_blkid
= curblkid
>> epbs
;
2447 if (dbuf_hold_impl(dn
, parent_level
, parent_blkid
,
2448 FALSE
, TRUE
, FTAG
, &db
) == 0) {
2449 blkptr_t
*bpp
= db
->db_buf
->b_data
;
2450 bp
= bpp
[P2PHASE(curblkid
, 1 << epbs
)];
2451 dbuf_rele(db
, FTAG
);
2455 curlevel
= parent_level
;
2456 curblkid
= parent_blkid
;
2459 if (curlevel
== nlevels
- 1) {
2460 /* No cached indirect blocks found. */
2461 ASSERT3U(curblkid
, <, dn
->dn_phys
->dn_nblkptr
);
2462 bp
= dn
->dn_phys
->dn_blkptr
[curblkid
];
2464 if (BP_IS_HOLE(&bp
))
2467 ASSERT3U(curlevel
, ==, BP_GET_LEVEL(&bp
));
2469 zio_t
*pio
= zio_root(dmu_objset_spa(dn
->dn_objset
), NULL
, NULL
,
2472 dbuf_prefetch_arg_t
*dpa
= kmem_zalloc(sizeof (*dpa
), KM_SLEEP
);
2473 dsl_dataset_t
*ds
= dn
->dn_objset
->os_dsl_dataset
;
2474 SET_BOOKMARK(&dpa
->dpa_zb
, ds
!= NULL
? ds
->ds_object
: DMU_META_OBJSET
,
2475 dn
->dn_object
, level
, blkid
);
2476 dpa
->dpa_curlevel
= curlevel
;
2477 dpa
->dpa_prio
= prio
;
2478 dpa
->dpa_aflags
= aflags
;
2479 dpa
->dpa_spa
= dn
->dn_objset
->os_spa
;
2480 dpa
->dpa_dnode
= dn
;
2481 dpa
->dpa_epbs
= epbs
;
2485 * If we have the indirect just above us, no need to do the asynchronous
2486 * prefetch chain; we'll just run the last step ourselves. If we're at
2487 * a higher level, though, we want to issue the prefetches for all the
2488 * indirect blocks asynchronously, so we can go on with whatever we were
2491 if (curlevel
== level
) {
2492 ASSERT3U(curblkid
, ==, blkid
);
2493 dbuf_issue_final_prefetch(dpa
, &bp
);
2494 kmem_free(dpa
, sizeof (*dpa
));
2496 arc_flags_t iter_aflags
= ARC_FLAG_NOWAIT
;
2497 zbookmark_phys_t zb
;
2499 SET_BOOKMARK(&zb
, ds
!= NULL
? ds
->ds_object
: DMU_META_OBJSET
,
2500 dn
->dn_object
, curlevel
, curblkid
);
2501 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
,
2502 &bp
, dbuf_prefetch_indirect_done
, dpa
, prio
,
2503 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
2507 * We use pio here instead of dpa_zio since it's possible that
2508 * dpa may have already been freed.
2514 * Returns with db_holds incremented, and db_mtx not held.
2515 * Note: dn_struct_rwlock must be held.
2518 dbuf_hold_impl(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
2519 boolean_t fail_sparse
, boolean_t fail_uncached
,
2520 void *tag
, dmu_buf_impl_t
**dbp
)
2522 dmu_buf_impl_t
*db
, *parent
= NULL
;
2524 ASSERT(blkid
!= DMU_BONUS_BLKID
);
2525 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2526 ASSERT3U(dn
->dn_nlevels
, >, level
);
2530 /* dbuf_find() returns with db_mtx held */
2531 db
= dbuf_find(dn
->dn_objset
, dn
->dn_object
, level
, blkid
);
2534 blkptr_t
*bp
= NULL
;
2538 return (SET_ERROR(ENOENT
));
2540 ASSERT3P(parent
, ==, NULL
);
2541 err
= dbuf_findbp(dn
, level
, blkid
, fail_sparse
, &parent
, &bp
);
2543 if (err
== 0 && bp
&& BP_IS_HOLE(bp
))
2544 err
= SET_ERROR(ENOENT
);
2547 dbuf_rele(parent
, NULL
);
2551 if (err
&& err
!= ENOENT
)
2553 db
= dbuf_create(dn
, level
, blkid
, parent
, bp
);
2556 if (fail_uncached
&& db
->db_state
!= DB_CACHED
) {
2557 mutex_exit(&db
->db_mtx
);
2558 return (SET_ERROR(ENOENT
));
2561 if (db
->db_buf
!= NULL
)
2562 ASSERT3P(db
->db
.db_data
, ==, db
->db_buf
->b_data
);
2564 ASSERT(db
->db_buf
== NULL
|| arc_referenced(db
->db_buf
));
2567 * If this buffer is currently syncing out, and we are are
2568 * still referencing it from db_data, we need to make a copy
2569 * of it in case we decide we want to dirty it again in this txg.
2571 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
2572 dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
2573 db
->db_state
== DB_CACHED
&& db
->db_data_pending
) {
2574 dbuf_dirty_record_t
*dr
= db
->db_data_pending
;
2576 if (dr
->dt
.dl
.dr_data
== db
->db_buf
) {
2577 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
2580 arc_alloc_buf(dn
->dn_objset
->os_spa
, db
, type
,
2582 bcopy(dr
->dt
.dl
.dr_data
->b_data
, db
->db
.db_data
,
2587 if (multilist_link_active(&db
->db_cache_link
)) {
2588 ASSERT(refcount_is_zero(&db
->db_holds
));
2589 multilist_remove(dbuf_cache
, db
);
2590 (void) refcount_remove_many(&dbuf_cache_size
,
2591 db
->db
.db_size
, db
);
2593 (void) refcount_add(&db
->db_holds
, tag
);
2595 mutex_exit(&db
->db_mtx
);
2597 /* NOTE: we can't rele the parent until after we drop the db_mtx */
2599 dbuf_rele(parent
, NULL
);
2601 ASSERT3P(DB_DNODE(db
), ==, dn
);
2602 ASSERT3U(db
->db_blkid
, ==, blkid
);
2603 ASSERT3U(db
->db_level
, ==, level
);
2610 dbuf_hold(dnode_t
*dn
, uint64_t blkid
, void *tag
)
2612 return (dbuf_hold_level(dn
, 0, blkid
, tag
));
2616 dbuf_hold_level(dnode_t
*dn
, int level
, uint64_t blkid
, void *tag
)
2619 int err
= dbuf_hold_impl(dn
, level
, blkid
, FALSE
, FALSE
, tag
, &db
);
2620 return (err
? NULL
: db
);
2624 dbuf_create_bonus(dnode_t
*dn
)
2626 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
2628 ASSERT(dn
->dn_bonus
== NULL
);
2629 dn
->dn_bonus
= dbuf_create(dn
, 0, DMU_BONUS_BLKID
, dn
->dn_dbuf
, NULL
);
2633 dbuf_spill_set_blksz(dmu_buf_t
*db_fake
, uint64_t blksz
, dmu_tx_t
*tx
)
2635 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2638 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
2639 return (SET_ERROR(ENOTSUP
));
2641 blksz
= SPA_MINBLOCKSIZE
;
2642 ASSERT3U(blksz
, <=, spa_maxblocksize(dmu_objset_spa(db
->db_objset
)));
2643 blksz
= P2ROUNDUP(blksz
, SPA_MINBLOCKSIZE
);
2647 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
2648 dbuf_new_size(db
, blksz
, tx
);
2649 rw_exit(&dn
->dn_struct_rwlock
);
2656 dbuf_rm_spill(dnode_t
*dn
, dmu_tx_t
*tx
)
2658 dbuf_free_range(dn
, DMU_SPILL_BLKID
, DMU_SPILL_BLKID
, tx
);
2661 #pragma weak dmu_buf_add_ref = dbuf_add_ref
2663 dbuf_add_ref(dmu_buf_impl_t
*db
, void *tag
)
2665 int64_t holds
= refcount_add(&db
->db_holds
, tag
);
2666 ASSERT3S(holds
, >, 1);
2669 #pragma weak dmu_buf_try_add_ref = dbuf_try_add_ref
2671 dbuf_try_add_ref(dmu_buf_t
*db_fake
, objset_t
*os
, uint64_t obj
, uint64_t blkid
,
2674 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2675 dmu_buf_impl_t
*found_db
;
2676 boolean_t result
= B_FALSE
;
2678 if (db
->db_blkid
== DMU_BONUS_BLKID
)
2679 found_db
= dbuf_find_bonus(os
, obj
);
2681 found_db
= dbuf_find(os
, obj
, 0, blkid
);
2683 if (found_db
!= NULL
) {
2684 if (db
== found_db
&& dbuf_refcount(db
) > db
->db_dirtycnt
) {
2685 (void) refcount_add(&db
->db_holds
, tag
);
2688 mutex_exit(&db
->db_mtx
);
2694 * If you call dbuf_rele() you had better not be referencing the dnode handle
2695 * unless you have some other direct or indirect hold on the dnode. (An indirect
2696 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
2697 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
2698 * dnode's parent dbuf evicting its dnode handles.
2701 dbuf_rele(dmu_buf_impl_t
*db
, void *tag
)
2703 mutex_enter(&db
->db_mtx
);
2704 dbuf_rele_and_unlock(db
, tag
);
2708 dmu_buf_rele(dmu_buf_t
*db
, void *tag
)
2710 dbuf_rele((dmu_buf_impl_t
*)db
, tag
);
2714 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
2715 * db_dirtycnt and db_holds to be updated atomically.
2718 dbuf_rele_and_unlock(dmu_buf_impl_t
*db
, void *tag
)
2722 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2726 * Remove the reference to the dbuf before removing its hold on the
2727 * dnode so we can guarantee in dnode_move() that a referenced bonus
2728 * buffer has a corresponding dnode hold.
2730 holds
= refcount_remove(&db
->db_holds
, tag
);
2734 * We can't freeze indirects if there is a possibility that they
2735 * may be modified in the current syncing context.
2737 if (db
->db_buf
!= NULL
&&
2738 holds
== (db
->db_level
== 0 ? db
->db_dirtycnt
: 0)) {
2739 arc_buf_freeze(db
->db_buf
);
2742 if (holds
== db
->db_dirtycnt
&&
2743 db
->db_level
== 0 && db
->db_user_immediate_evict
)
2744 dbuf_evict_user(db
);
2747 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2749 boolean_t evict_dbuf
= db
->db_pending_evict
;
2752 * If the dnode moves here, we cannot cross this
2753 * barrier until the move completes.
2758 atomic_dec_32(&dn
->dn_dbufs_count
);
2761 * Decrementing the dbuf count means that the bonus
2762 * buffer's dnode hold is no longer discounted in
2763 * dnode_move(). The dnode cannot move until after
2764 * the dnode_rele() below.
2769 * Do not reference db after its lock is dropped.
2770 * Another thread may evict it.
2772 mutex_exit(&db
->db_mtx
);
2775 dnode_evict_bonus(dn
);
2778 } else if (db
->db_buf
== NULL
) {
2780 * This is a special case: we never associated this
2781 * dbuf with any data allocated from the ARC.
2783 ASSERT(db
->db_state
== DB_UNCACHED
||
2784 db
->db_state
== DB_NOFILL
);
2786 } else if (arc_released(db
->db_buf
)) {
2788 * This dbuf has anonymous data associated with it.
2792 boolean_t do_arc_evict
= B_FALSE
;
2794 spa_t
*spa
= dmu_objset_spa(db
->db_objset
);
2796 if (!DBUF_IS_CACHEABLE(db
) &&
2797 db
->db_blkptr
!= NULL
&&
2798 !BP_IS_HOLE(db
->db_blkptr
) &&
2799 !BP_IS_EMBEDDED(db
->db_blkptr
)) {
2800 do_arc_evict
= B_TRUE
;
2801 bp
= *db
->db_blkptr
;
2804 if (!DBUF_IS_CACHEABLE(db
) ||
2805 db
->db_pending_evict
) {
2807 } else if (!multilist_link_active(&db
->db_cache_link
)) {
2808 multilist_insert(dbuf_cache
, db
);
2809 (void) refcount_add_many(&dbuf_cache_size
,
2810 db
->db
.db_size
, db
);
2811 mutex_exit(&db
->db_mtx
);
2813 dbuf_evict_notify();
2817 arc_freed(spa
, &bp
);
2820 mutex_exit(&db
->db_mtx
);
2825 #pragma weak dmu_buf_refcount = dbuf_refcount
2827 dbuf_refcount(dmu_buf_impl_t
*db
)
2829 return (refcount_count(&db
->db_holds
));
2833 dmu_buf_replace_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*old_user
,
2834 dmu_buf_user_t
*new_user
)
2836 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2838 mutex_enter(&db
->db_mtx
);
2839 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
2840 if (db
->db_user
== old_user
)
2841 db
->db_user
= new_user
;
2843 old_user
= db
->db_user
;
2844 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
2845 mutex_exit(&db
->db_mtx
);
2851 dmu_buf_set_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
2853 return (dmu_buf_replace_user(db_fake
, NULL
, user
));
2857 dmu_buf_set_user_ie(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
2859 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2861 db
->db_user_immediate_evict
= TRUE
;
2862 return (dmu_buf_set_user(db_fake
, user
));
2866 dmu_buf_remove_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
2868 return (dmu_buf_replace_user(db_fake
, user
, NULL
));
2872 dmu_buf_get_user(dmu_buf_t
*db_fake
)
2874 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2876 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
2877 return (db
->db_user
);
2881 dmu_buf_user_evict_wait()
2883 taskq_wait(dbu_evict_taskq
);
2887 dmu_buf_get_blkptr(dmu_buf_t
*db
)
2889 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
2890 return (dbi
->db_blkptr
);
2894 dmu_buf_get_objset(dmu_buf_t
*db
)
2896 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
2897 return (dbi
->db_objset
);
2901 dmu_buf_dnode_enter(dmu_buf_t
*db
)
2903 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
2904 DB_DNODE_ENTER(dbi
);
2905 return (DB_DNODE(dbi
));
2909 dmu_buf_dnode_exit(dmu_buf_t
*db
)
2911 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
2916 dbuf_check_blkptr(dnode_t
*dn
, dmu_buf_impl_t
*db
)
2918 /* ASSERT(dmu_tx_is_syncing(tx) */
2919 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2921 if (db
->db_blkptr
!= NULL
)
2924 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
2925 db
->db_blkptr
= &dn
->dn_phys
->dn_spill
;
2926 BP_ZERO(db
->db_blkptr
);
2929 if (db
->db_level
== dn
->dn_phys
->dn_nlevels
-1) {
2931 * This buffer was allocated at a time when there was
2932 * no available blkptrs from the dnode, or it was
2933 * inappropriate to hook it in (i.e., nlevels mis-match).
2935 ASSERT(db
->db_blkid
< dn
->dn_phys
->dn_nblkptr
);
2936 ASSERT(db
->db_parent
== NULL
);
2937 db
->db_parent
= dn
->dn_dbuf
;
2938 db
->db_blkptr
= &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
];
2941 dmu_buf_impl_t
*parent
= db
->db_parent
;
2942 int epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2944 ASSERT(dn
->dn_phys
->dn_nlevels
> 1);
2945 if (parent
== NULL
) {
2946 mutex_exit(&db
->db_mtx
);
2947 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
2948 parent
= dbuf_hold_level(dn
, db
->db_level
+ 1,
2949 db
->db_blkid
>> epbs
, db
);
2950 rw_exit(&dn
->dn_struct_rwlock
);
2951 mutex_enter(&db
->db_mtx
);
2952 db
->db_parent
= parent
;
2954 db
->db_blkptr
= (blkptr_t
*)parent
->db
.db_data
+
2955 (db
->db_blkid
& ((1ULL << epbs
) - 1));
2961 dbuf_sync_indirect(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
2963 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2967 ASSERT(dmu_tx_is_syncing(tx
));
2969 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
2971 mutex_enter(&db
->db_mtx
);
2973 ASSERT(db
->db_level
> 0);
2976 /* Read the block if it hasn't been read yet. */
2977 if (db
->db_buf
== NULL
) {
2978 mutex_exit(&db
->db_mtx
);
2979 (void) dbuf_read(db
, NULL
, DB_RF_MUST_SUCCEED
);
2980 mutex_enter(&db
->db_mtx
);
2982 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
2983 ASSERT(db
->db_buf
!= NULL
);
2987 /* Indirect block size must match what the dnode thinks it is. */
2988 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
2989 dbuf_check_blkptr(dn
, db
);
2992 /* Provide the pending dirty record to child dbufs */
2993 db
->db_data_pending
= dr
;
2995 mutex_exit(&db
->db_mtx
);
2996 dbuf_write(dr
, db
->db_buf
, tx
);
2999 mutex_enter(&dr
->dt
.di
.dr_mtx
);
3000 dbuf_sync_list(&dr
->dt
.di
.dr_children
, db
->db_level
- 1, tx
);
3001 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
3002 mutex_exit(&dr
->dt
.di
.dr_mtx
);
3007 dbuf_sync_leaf(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
3009 arc_buf_t
**datap
= &dr
->dt
.dl
.dr_data
;
3010 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3013 uint64_t txg
= tx
->tx_txg
;
3015 ASSERT(dmu_tx_is_syncing(tx
));
3017 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
3019 mutex_enter(&db
->db_mtx
);
3021 * To be synced, we must be dirtied. But we
3022 * might have been freed after the dirty.
3024 if (db
->db_state
== DB_UNCACHED
) {
3025 /* This buffer has been freed since it was dirtied */
3026 ASSERT(db
->db
.db_data
== NULL
);
3027 } else if (db
->db_state
== DB_FILL
) {
3028 /* This buffer was freed and is now being re-filled */
3029 ASSERT(db
->db
.db_data
!= dr
->dt
.dl
.dr_data
);
3031 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_NOFILL
);
3038 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3039 mutex_enter(&dn
->dn_mtx
);
3040 dn
->dn_phys
->dn_flags
|= DNODE_FLAG_SPILL_BLKPTR
;
3041 mutex_exit(&dn
->dn_mtx
);
3045 * If this is a bonus buffer, simply copy the bonus data into the
3046 * dnode. It will be written out when the dnode is synced (and it
3047 * will be synced, since it must have been dirty for dbuf_sync to
3050 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
3051 dbuf_dirty_record_t
**drp
;
3053 ASSERT(*datap
!= NULL
);
3054 ASSERT0(db
->db_level
);
3055 ASSERT3U(dn
->dn_phys
->dn_bonuslen
, <=, DN_MAX_BONUSLEN
);
3056 bcopy(*datap
, DN_BONUS(dn
->dn_phys
), dn
->dn_phys
->dn_bonuslen
);
3059 if (*datap
!= db
->db
.db_data
) {
3060 zio_buf_free(*datap
, DN_MAX_BONUSLEN
);
3061 arc_space_return(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
3063 db
->db_data_pending
= NULL
;
3064 drp
= &db
->db_last_dirty
;
3066 drp
= &(*drp
)->dr_next
;
3067 ASSERT(dr
->dr_next
== NULL
);
3068 ASSERT(dr
->dr_dbuf
== db
);
3070 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
3071 ASSERT(db
->db_dirtycnt
> 0);
3072 db
->db_dirtycnt
-= 1;
3073 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)txg
);
3080 * This function may have dropped the db_mtx lock allowing a dmu_sync
3081 * operation to sneak in. As a result, we need to ensure that we
3082 * don't check the dr_override_state until we have returned from
3083 * dbuf_check_blkptr.
3085 dbuf_check_blkptr(dn
, db
);
3088 * If this buffer is in the middle of an immediate write,
3089 * wait for the synchronous IO to complete.
3091 while (dr
->dt
.dl
.dr_override_state
== DR_IN_DMU_SYNC
) {
3092 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
3093 cv_wait(&db
->db_changed
, &db
->db_mtx
);
3094 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_NOT_OVERRIDDEN
);
3097 if (db
->db_state
!= DB_NOFILL
&&
3098 dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
3099 refcount_count(&db
->db_holds
) > 1 &&
3100 dr
->dt
.dl
.dr_override_state
!= DR_OVERRIDDEN
&&
3101 *datap
== db
->db_buf
) {
3103 * If this buffer is currently "in use" (i.e., there
3104 * are active holds and db_data still references it),
3105 * then make a copy before we start the write so that
3106 * any modifications from the open txg will not leak
3109 * NOTE: this copy does not need to be made for
3110 * objects only modified in the syncing context (e.g.
3111 * DNONE_DNODE blocks).
3113 int psize
= arc_buf_size(*datap
);
3114 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
3115 enum zio_compress compress_type
= arc_get_compression(*datap
);
3117 if (compress_type
== ZIO_COMPRESS_OFF
) {
3118 *datap
= arc_alloc_buf(os
->os_spa
, db
, type
, psize
);
3120 ASSERT3U(type
, ==, ARC_BUFC_DATA
);
3121 int lsize
= arc_buf_lsize(*datap
);
3122 *datap
= arc_alloc_compressed_buf(os
->os_spa
, db
,
3123 psize
, lsize
, compress_type
);
3125 bcopy(db
->db
.db_data
, (*datap
)->b_data
, psize
);
3127 db
->db_data_pending
= dr
;
3129 mutex_exit(&db
->db_mtx
);
3131 dbuf_write(dr
, *datap
, tx
);
3133 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
3134 if (dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
3135 list_insert_tail(&dn
->dn_dirty_records
[txg
&TXG_MASK
], dr
);
3139 * Although zio_nowait() does not "wait for an IO", it does
3140 * initiate the IO. If this is an empty write it seems plausible
3141 * that the IO could actually be completed before the nowait
3142 * returns. We need to DB_DNODE_EXIT() first in case
3143 * zio_nowait() invalidates the dbuf.
3146 zio_nowait(dr
->dr_zio
);
3151 dbuf_sync_list(list_t
*list
, int level
, dmu_tx_t
*tx
)
3153 dbuf_dirty_record_t
*dr
;
3155 while (dr
= list_head(list
)) {
3156 if (dr
->dr_zio
!= NULL
) {
3158 * If we find an already initialized zio then we
3159 * are processing the meta-dnode, and we have finished.
3160 * The dbufs for all dnodes are put back on the list
3161 * during processing, so that we can zio_wait()
3162 * these IOs after initiating all child IOs.
3164 ASSERT3U(dr
->dr_dbuf
->db
.db_object
, ==,
3165 DMU_META_DNODE_OBJECT
);
3168 if (dr
->dr_dbuf
->db_blkid
!= DMU_BONUS_BLKID
&&
3169 dr
->dr_dbuf
->db_blkid
!= DMU_SPILL_BLKID
) {
3170 VERIFY3U(dr
->dr_dbuf
->db_level
, ==, level
);
3172 list_remove(list
, dr
);
3173 if (dr
->dr_dbuf
->db_level
> 0)
3174 dbuf_sync_indirect(dr
, tx
);
3176 dbuf_sync_leaf(dr
, tx
);
3182 dbuf_write_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
3184 dmu_buf_impl_t
*db
= vdb
;
3186 blkptr_t
*bp
= zio
->io_bp
;
3187 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
3188 spa_t
*spa
= zio
->io_spa
;
3193 ASSERT3P(db
->db_blkptr
, !=, NULL
);
3194 ASSERT3P(&db
->db_data_pending
->dr_bp_copy
, ==, bp
);
3198 delta
= bp_get_dsize_sync(spa
, bp
) - bp_get_dsize_sync(spa
, bp_orig
);
3199 dnode_diduse_space(dn
, delta
- zio
->io_prev_space_delta
);
3200 zio
->io_prev_space_delta
= delta
;
3202 if (bp
->blk_birth
!= 0) {
3203 ASSERT((db
->db_blkid
!= DMU_SPILL_BLKID
&&
3204 BP_GET_TYPE(bp
) == dn
->dn_type
) ||
3205 (db
->db_blkid
== DMU_SPILL_BLKID
&&
3206 BP_GET_TYPE(bp
) == dn
->dn_bonustype
) ||
3207 BP_IS_EMBEDDED(bp
));
3208 ASSERT(BP_GET_LEVEL(bp
) == db
->db_level
);
3211 mutex_enter(&db
->db_mtx
);
3214 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3215 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
3216 ASSERT(!(BP_IS_HOLE(bp
)) &&
3217 db
->db_blkptr
== &dn
->dn_phys
->dn_spill
);
3221 if (db
->db_level
== 0) {
3222 mutex_enter(&dn
->dn_mtx
);
3223 if (db
->db_blkid
> dn
->dn_phys
->dn_maxblkid
&&
3224 db
->db_blkid
!= DMU_SPILL_BLKID
)
3225 dn
->dn_phys
->dn_maxblkid
= db
->db_blkid
;
3226 mutex_exit(&dn
->dn_mtx
);
3228 if (dn
->dn_type
== DMU_OT_DNODE
) {
3229 dnode_phys_t
*dnp
= db
->db
.db_data
;
3230 for (i
= db
->db
.db_size
>> DNODE_SHIFT
; i
> 0;
3232 if (dnp
->dn_type
!= DMU_OT_NONE
)
3236 if (BP_IS_HOLE(bp
)) {
3243 blkptr_t
*ibp
= db
->db
.db_data
;
3244 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
3245 for (i
= db
->db
.db_size
>> SPA_BLKPTRSHIFT
; i
> 0; i
--, ibp
++) {
3246 if (BP_IS_HOLE(ibp
))
3248 fill
+= BP_GET_FILL(ibp
);
3253 if (!BP_IS_EMBEDDED(bp
))
3254 bp
->blk_fill
= fill
;
3256 mutex_exit(&db
->db_mtx
);
3258 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
3259 *db
->db_blkptr
= *bp
;
3260 rw_exit(&dn
->dn_struct_rwlock
);
3265 * This function gets called just prior to running through the compression
3266 * stage of the zio pipeline. If we're an indirect block comprised of only
3267 * holes, then we want this indirect to be compressed away to a hole. In
3268 * order to do that we must zero out any information about the holes that
3269 * this indirect points to prior to before we try to compress it.
3272 dbuf_write_children_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
3274 dmu_buf_impl_t
*db
= vdb
;
3277 unsigned int epbs
, i
;
3279 ASSERT3U(db
->db_level
, >, 0);
3282 epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
3283 ASSERT3U(epbs
, <, 31);
3285 /* Determine if all our children are holes */
3286 for (i
= 0, bp
= db
->db
.db_data
; i
< 1 << epbs
; i
++, bp
++) {
3287 if (!BP_IS_HOLE(bp
))
3292 * If all the children are holes, then zero them all out so that
3293 * we may get compressed away.
3295 if (i
== 1 << epbs
) {
3297 * We only found holes. Grab the rwlock to prevent
3298 * anybody from reading the blocks we're about to
3301 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
3302 bzero(db
->db
.db_data
, db
->db
.db_size
);
3303 rw_exit(&dn
->dn_struct_rwlock
);
3309 * The SPA will call this callback several times for each zio - once
3310 * for every physical child i/o (zio->io_phys_children times). This
3311 * allows the DMU to monitor the progress of each logical i/o. For example,
3312 * there may be 2 copies of an indirect block, or many fragments of a RAID-Z
3313 * block. There may be a long delay before all copies/fragments are completed,
3314 * so this callback allows us to retire dirty space gradually, as the physical
3319 dbuf_write_physdone(zio_t
*zio
, arc_buf_t
*buf
, void *arg
)
3321 dmu_buf_impl_t
*db
= arg
;
3322 objset_t
*os
= db
->db_objset
;
3323 dsl_pool_t
*dp
= dmu_objset_pool(os
);
3324 dbuf_dirty_record_t
*dr
;
3327 dr
= db
->db_data_pending
;
3328 ASSERT3U(dr
->dr_txg
, ==, zio
->io_txg
);
3331 * The callback will be called io_phys_children times. Retire one
3332 * portion of our dirty space each time we are called. Any rounding
3333 * error will be cleaned up by dsl_pool_sync()'s call to
3334 * dsl_pool_undirty_space().
3336 delta
= dr
->dr_accounted
/ zio
->io_phys_children
;
3337 dsl_pool_undirty_space(dp
, delta
, zio
->io_txg
);
3342 dbuf_write_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
3344 dmu_buf_impl_t
*db
= vdb
;
3345 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
3346 blkptr_t
*bp
= db
->db_blkptr
;
3347 objset_t
*os
= db
->db_objset
;
3348 dmu_tx_t
*tx
= os
->os_synctx
;
3349 dbuf_dirty_record_t
**drp
, *dr
;
3351 ASSERT0(zio
->io_error
);
3352 ASSERT(db
->db_blkptr
== bp
);
3355 * For nopwrites and rewrites we ensure that the bp matches our
3356 * original and bypass all the accounting.
3358 if (zio
->io_flags
& (ZIO_FLAG_IO_REWRITE
| ZIO_FLAG_NOPWRITE
)) {
3359 ASSERT(BP_EQUAL(bp
, bp_orig
));
3361 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
3362 (void) dsl_dataset_block_kill(ds
, bp_orig
, tx
, B_TRUE
);
3363 dsl_dataset_block_born(ds
, bp
, tx
);
3366 mutex_enter(&db
->db_mtx
);
3370 drp
= &db
->db_last_dirty
;
3371 while ((dr
= *drp
) != db
->db_data_pending
)
3373 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
3374 ASSERT(dr
->dr_dbuf
== db
);
3375 ASSERT(dr
->dr_next
== NULL
);
3379 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3384 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
3385 ASSERT(!(BP_IS_HOLE(db
->db_blkptr
)) &&
3386 db
->db_blkptr
== &dn
->dn_phys
->dn_spill
);
3391 if (db
->db_level
== 0) {
3392 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
3393 ASSERT(dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
);
3394 if (db
->db_state
!= DB_NOFILL
) {
3395 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
3396 arc_buf_destroy(dr
->dt
.dl
.dr_data
, db
);
3403 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
3404 ASSERT3U(db
->db
.db_size
, ==, 1 << dn
->dn_phys
->dn_indblkshift
);
3405 if (!BP_IS_HOLE(db
->db_blkptr
)) {
3407 dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
3408 ASSERT3U(db
->db_blkid
, <=,
3409 dn
->dn_phys
->dn_maxblkid
>> (db
->db_level
* epbs
));
3410 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==,
3414 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
3415 list_destroy(&dr
->dt
.di
.dr_children
);
3417 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
3419 cv_broadcast(&db
->db_changed
);
3420 ASSERT(db
->db_dirtycnt
> 0);
3421 db
->db_dirtycnt
-= 1;
3422 db
->db_data_pending
= NULL
;
3423 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)tx
->tx_txg
);
3427 dbuf_write_nofill_ready(zio_t
*zio
)
3429 dbuf_write_ready(zio
, NULL
, zio
->io_private
);
3433 dbuf_write_nofill_done(zio_t
*zio
)
3435 dbuf_write_done(zio
, NULL
, zio
->io_private
);
3439 dbuf_write_override_ready(zio_t
*zio
)
3441 dbuf_dirty_record_t
*dr
= zio
->io_private
;
3442 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3444 dbuf_write_ready(zio
, NULL
, db
);
3448 dbuf_write_override_done(zio_t
*zio
)
3450 dbuf_dirty_record_t
*dr
= zio
->io_private
;
3451 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3452 blkptr_t
*obp
= &dr
->dt
.dl
.dr_overridden_by
;
3454 mutex_enter(&db
->db_mtx
);
3455 if (!BP_EQUAL(zio
->io_bp
, obp
)) {
3456 if (!BP_IS_HOLE(obp
))
3457 dsl_free(spa_get_dsl(zio
->io_spa
), zio
->io_txg
, obp
);
3458 arc_release(dr
->dt
.dl
.dr_data
, db
);
3460 mutex_exit(&db
->db_mtx
);
3461 dbuf_write_done(zio
, NULL
, db
);
3463 if (zio
->io_abd
!= NULL
)
3464 abd_put(zio
->io_abd
);
3467 /* Issue I/O to commit a dirty buffer to disk. */
3469 dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
)
3471 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3474 dmu_buf_impl_t
*parent
= db
->db_parent
;
3475 uint64_t txg
= tx
->tx_txg
;
3476 zbookmark_phys_t zb
;
3481 ASSERT(dmu_tx_is_syncing(tx
));
3487 if (db
->db_state
!= DB_NOFILL
) {
3488 if (db
->db_level
> 0 || dn
->dn_type
== DMU_OT_DNODE
) {
3490 * Private object buffers are released here rather
3491 * than in dbuf_dirty() since they are only modified
3492 * in the syncing context and we don't want the
3493 * overhead of making multiple copies of the data.
3495 if (BP_IS_HOLE(db
->db_blkptr
)) {
3498 dbuf_release_bp(db
);
3503 if (parent
!= dn
->dn_dbuf
) {
3504 /* Our parent is an indirect block. */
3505 /* We have a dirty parent that has been scheduled for write. */
3506 ASSERT(parent
&& parent
->db_data_pending
);
3507 /* Our parent's buffer is one level closer to the dnode. */
3508 ASSERT(db
->db_level
== parent
->db_level
-1);
3510 * We're about to modify our parent's db_data by modifying
3511 * our block pointer, so the parent must be released.
3513 ASSERT(arc_released(parent
->db_buf
));
3514 zio
= parent
->db_data_pending
->dr_zio
;
3516 /* Our parent is the dnode itself. */
3517 ASSERT((db
->db_level
== dn
->dn_phys
->dn_nlevels
-1 &&
3518 db
->db_blkid
!= DMU_SPILL_BLKID
) ||
3519 (db
->db_blkid
== DMU_SPILL_BLKID
&& db
->db_level
== 0));
3520 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
3521 ASSERT3P(db
->db_blkptr
, ==,
3522 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
3526 ASSERT(db
->db_level
== 0 || data
== db
->db_buf
);
3527 ASSERT3U(db
->db_blkptr
->blk_birth
, <=, txg
);
3530 SET_BOOKMARK(&zb
, os
->os_dsl_dataset
?
3531 os
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
3532 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
3534 if (db
->db_blkid
== DMU_SPILL_BLKID
)
3536 wp_flag
|= (db
->db_state
== DB_NOFILL
) ? WP_NOFILL
: 0;
3538 dmu_write_policy(os
, dn
, db
->db_level
, wp_flag
, &zp
);
3542 * We copy the blkptr now (rather than when we instantiate the dirty
3543 * record), because its value can change between open context and
3544 * syncing context. We do not need to hold dn_struct_rwlock to read
3545 * db_blkptr because we are in syncing context.
3547 dr
->dr_bp_copy
= *db
->db_blkptr
;
3549 if (db
->db_level
== 0 &&
3550 dr
->dt
.dl
.dr_override_state
== DR_OVERRIDDEN
) {
3552 * The BP for this block has been provided by open context
3553 * (by dmu_sync() or dmu_buf_write_embedded()).
3555 abd_t
*contents
= (data
!= NULL
) ?
3556 abd_get_from_buf(data
->b_data
, arc_buf_size(data
)) : NULL
;
3558 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
, &dr
->dr_bp_copy
,
3559 contents
, db
->db
.db_size
, db
->db
.db_size
, &zp
,
3560 dbuf_write_override_ready
, NULL
, NULL
,
3561 dbuf_write_override_done
,
3562 dr
, ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
3563 mutex_enter(&db
->db_mtx
);
3564 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
3565 zio_write_override(dr
->dr_zio
, &dr
->dt
.dl
.dr_overridden_by
,
3566 dr
->dt
.dl
.dr_copies
, dr
->dt
.dl
.dr_nopwrite
);
3567 mutex_exit(&db
->db_mtx
);
3568 } else if (db
->db_state
== DB_NOFILL
) {
3569 ASSERT(zp
.zp_checksum
== ZIO_CHECKSUM_OFF
||
3570 zp
.zp_checksum
== ZIO_CHECKSUM_NOPARITY
);
3571 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
,
3572 &dr
->dr_bp_copy
, NULL
, db
->db
.db_size
, db
->db
.db_size
, &zp
,
3573 dbuf_write_nofill_ready
, NULL
, NULL
,
3574 dbuf_write_nofill_done
, db
,
3575 ZIO_PRIORITY_ASYNC_WRITE
,
3576 ZIO_FLAG_MUSTSUCCEED
| ZIO_FLAG_NODATA
, &zb
);
3578 ASSERT(arc_released(data
));
3581 * For indirect blocks, we want to setup the children
3582 * ready callback so that we can properly handle an indirect
3583 * block that only contains holes.
3585 arc_done_func_t
*children_ready_cb
= NULL
;
3586 if (db
->db_level
!= 0)
3587 children_ready_cb
= dbuf_write_children_ready
;
3589 dr
->dr_zio
= arc_write(zio
, os
->os_spa
, txg
,
3590 &dr
->dr_bp_copy
, data
, DBUF_IS_L2CACHEABLE(db
),
3591 &zp
, dbuf_write_ready
, children_ready_cb
,
3592 dbuf_write_physdone
, dbuf_write_done
, db
,
3593 ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);