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>
50 uint_t zfs_dbuf_evict_key
;
52 static boolean_t
dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
);
53 static void dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
);
56 extern inline void dmu_buf_init_user(dmu_buf_user_t
*dbu
,
57 dmu_buf_evict_func_t
*evict_func_sync
,
58 dmu_buf_evict_func_t
*evict_func_async
,
59 dmu_buf_t
**clear_on_evict_dbufp
);
63 * Global data structures and functions for the dbuf cache.
65 static kmem_cache_t
*dbuf_kmem_cache
;
66 static taskq_t
*dbu_evict_taskq
;
68 static kthread_t
*dbuf_cache_evict_thread
;
69 static kmutex_t dbuf_evict_lock
;
70 static kcondvar_t dbuf_evict_cv
;
71 static boolean_t dbuf_evict_thread_exit
;
74 * LRU cache of dbufs. The dbuf cache maintains a list of dbufs that
75 * are not currently held but have been recently released. These dbufs
76 * are not eligible for arc eviction until they are aged out of the cache.
77 * Dbufs are added to the dbuf cache once the last hold is released. If a
78 * dbuf is later accessed and still exists in the dbuf cache, then it will
79 * be removed from the cache and later re-added to the head of the cache.
80 * Dbufs that are aged out of the cache will be immediately destroyed and
81 * become eligible for arc eviction.
83 static multilist_t
*dbuf_cache
;
84 static refcount_t dbuf_cache_size
;
85 uint64_t dbuf_cache_max_bytes
= 100 * 1024 * 1024;
87 /* Cap the size of the dbuf cache to log2 fraction of arc size. */
88 int dbuf_cache_max_shift
= 5;
91 * The dbuf cache uses a three-stage eviction policy:
92 * - A low water marker designates when the dbuf eviction thread
93 * should stop evicting from the dbuf cache.
94 * - When we reach the maximum size (aka mid water mark), we
95 * signal the eviction thread to run.
96 * - The high water mark indicates when the eviction thread
97 * is unable to keep up with the incoming load and eviction must
98 * happen in the context of the calling thread.
102 * low water mid water hi water
103 * +----------------------------------------+----------+----------+
108 * +----------------------------------------+----------+----------+
110 * evicting eviction directly
113 * The high and low water marks indicate the operating range for the eviction
114 * thread. The low water mark is, by default, 90% of the total size of the
115 * cache and the high water mark is at 110% (both of these percentages can be
116 * changed by setting dbuf_cache_lowater_pct and dbuf_cache_hiwater_pct,
117 * respectively). The eviction thread will try to ensure that the cache remains
118 * within this range by waking up every second and checking if the cache is
119 * above the low water mark. The thread can also be woken up by callers adding
120 * elements into the cache if the cache is larger than the mid water (i.e max
121 * cache size). Once the eviction thread is woken up and eviction is required,
122 * it will continue evicting buffers until it's able to reduce the cache size
123 * to the low water mark. If the cache size continues to grow and hits the high
124 * water mark, then callers adding elments to the cache will begin to evict
125 * directly from the cache until the cache is no longer above the high water
130 * The percentage above and below the maximum cache size.
132 uint_t dbuf_cache_hiwater_pct
= 10;
133 uint_t dbuf_cache_lowater_pct
= 10;
137 dbuf_cons(void *vdb
, void *unused
, int kmflag
)
139 dmu_buf_impl_t
*db
= vdb
;
140 bzero(db
, sizeof (dmu_buf_impl_t
));
142 mutex_init(&db
->db_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
143 cv_init(&db
->db_changed
, NULL
, CV_DEFAULT
, NULL
);
144 multilist_link_init(&db
->db_cache_link
);
145 refcount_create(&db
->db_holds
);
152 dbuf_dest(void *vdb
, void *unused
)
154 dmu_buf_impl_t
*db
= vdb
;
155 mutex_destroy(&db
->db_mtx
);
156 cv_destroy(&db
->db_changed
);
157 ASSERT(!multilist_link_active(&db
->db_cache_link
));
158 refcount_destroy(&db
->db_holds
);
162 * dbuf hash table routines
164 static dbuf_hash_table_t dbuf_hash_table
;
166 static uint64_t dbuf_hash_count
;
169 dbuf_hash(void *os
, uint64_t obj
, uint8_t lvl
, uint64_t blkid
)
171 uintptr_t osv
= (uintptr_t)os
;
172 uint64_t crc
= -1ULL;
174 ASSERT(zfs_crc64_table
[128] == ZFS_CRC64_POLY
);
175 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (lvl
)) & 0xFF];
176 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (osv
>> 6)) & 0xFF];
177 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 0)) & 0xFF];
178 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 8)) & 0xFF];
179 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (blkid
>> 0)) & 0xFF];
180 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (blkid
>> 8)) & 0xFF];
182 crc
^= (osv
>>14) ^ (obj
>>16) ^ (blkid
>>16);
187 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
188 ((dbuf)->db.db_object == (obj) && \
189 (dbuf)->db_objset == (os) && \
190 (dbuf)->db_level == (level) && \
191 (dbuf)->db_blkid == (blkid))
194 dbuf_find(objset_t
*os
, uint64_t obj
, uint8_t level
, uint64_t blkid
)
196 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
197 uint64_t hv
= dbuf_hash(os
, obj
, level
, blkid
);
198 uint64_t idx
= hv
& h
->hash_table_mask
;
201 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
202 for (db
= h
->hash_table
[idx
]; db
!= NULL
; db
= db
->db_hash_next
) {
203 if (DBUF_EQUAL(db
, os
, obj
, level
, blkid
)) {
204 mutex_enter(&db
->db_mtx
);
205 if (db
->db_state
!= DB_EVICTING
) {
206 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
209 mutex_exit(&db
->db_mtx
);
212 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
216 static dmu_buf_impl_t
*
217 dbuf_find_bonus(objset_t
*os
, uint64_t object
)
220 dmu_buf_impl_t
*db
= NULL
;
222 if (dnode_hold(os
, object
, FTAG
, &dn
) == 0) {
223 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
224 if (dn
->dn_bonus
!= NULL
) {
226 mutex_enter(&db
->db_mtx
);
228 rw_exit(&dn
->dn_struct_rwlock
);
229 dnode_rele(dn
, FTAG
);
235 * Insert an entry into the hash table. If there is already an element
236 * equal to elem in the hash table, then the already existing element
237 * will be returned and the new element will not be inserted.
238 * Otherwise returns NULL.
240 static dmu_buf_impl_t
*
241 dbuf_hash_insert(dmu_buf_impl_t
*db
)
243 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
244 objset_t
*os
= db
->db_objset
;
245 uint64_t obj
= db
->db
.db_object
;
246 int level
= db
->db_level
;
247 uint64_t blkid
= db
->db_blkid
;
248 uint64_t hv
= dbuf_hash(os
, obj
, level
, blkid
);
249 uint64_t idx
= hv
& h
->hash_table_mask
;
252 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
253 for (dbf
= h
->hash_table
[idx
]; dbf
!= NULL
; dbf
= dbf
->db_hash_next
) {
254 if (DBUF_EQUAL(dbf
, os
, obj
, level
, blkid
)) {
255 mutex_enter(&dbf
->db_mtx
);
256 if (dbf
->db_state
!= DB_EVICTING
) {
257 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
260 mutex_exit(&dbf
->db_mtx
);
264 mutex_enter(&db
->db_mtx
);
265 db
->db_hash_next
= h
->hash_table
[idx
];
266 h
->hash_table
[idx
] = db
;
267 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
268 atomic_inc_64(&dbuf_hash_count
);
274 * Remove an entry from the hash table. It must be in the EVICTING state.
277 dbuf_hash_remove(dmu_buf_impl_t
*db
)
279 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
280 uint64_t hv
= dbuf_hash(db
->db_objset
, db
->db
.db_object
,
281 db
->db_level
, db
->db_blkid
);
282 uint64_t idx
= hv
& h
->hash_table_mask
;
283 dmu_buf_impl_t
*dbf
, **dbp
;
286 * We musn't hold db_mtx to maintain lock ordering:
287 * DBUF_HASH_MUTEX > db_mtx.
289 ASSERT(refcount_is_zero(&db
->db_holds
));
290 ASSERT(db
->db_state
== DB_EVICTING
);
291 ASSERT(!MUTEX_HELD(&db
->db_mtx
));
293 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
294 dbp
= &h
->hash_table
[idx
];
295 while ((dbf
= *dbp
) != db
) {
296 dbp
= &dbf
->db_hash_next
;
299 *dbp
= db
->db_hash_next
;
300 db
->db_hash_next
= NULL
;
301 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
302 atomic_dec_64(&dbuf_hash_count
);
308 } dbvu_verify_type_t
;
311 dbuf_verify_user(dmu_buf_impl_t
*db
, dbvu_verify_type_t verify_type
)
316 if (db
->db_user
== NULL
)
319 /* Only data blocks support the attachment of user data. */
320 ASSERT(db
->db_level
== 0);
322 /* Clients must resolve a dbuf before attaching user data. */
323 ASSERT(db
->db
.db_data
!= NULL
);
324 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
326 holds
= refcount_count(&db
->db_holds
);
327 if (verify_type
== DBVU_EVICTING
) {
329 * Immediate eviction occurs when holds == dirtycnt.
330 * For normal eviction buffers, holds is zero on
331 * eviction, except when dbuf_fix_old_data() calls
332 * dbuf_clear_data(). However, the hold count can grow
333 * during eviction even though db_mtx is held (see
334 * dmu_bonus_hold() for an example), so we can only
335 * test the generic invariant that holds >= dirtycnt.
337 ASSERT3U(holds
, >=, db
->db_dirtycnt
);
339 if (db
->db_user_immediate_evict
== TRUE
)
340 ASSERT3U(holds
, >=, db
->db_dirtycnt
);
342 ASSERT3U(holds
, >, 0);
348 dbuf_evict_user(dmu_buf_impl_t
*db
)
350 dmu_buf_user_t
*dbu
= db
->db_user
;
352 ASSERT(MUTEX_HELD(&db
->db_mtx
));
357 dbuf_verify_user(db
, DBVU_EVICTING
);
361 if (dbu
->dbu_clear_on_evict_dbufp
!= NULL
)
362 *dbu
->dbu_clear_on_evict_dbufp
= NULL
;
366 * There are two eviction callbacks - one that we call synchronously
367 * and one that we invoke via a taskq. The async one is useful for
368 * avoiding lock order reversals and limiting stack depth.
370 * Note that if we have a sync callback but no async callback,
371 * it's likely that the sync callback will free the structure
372 * containing the dbu. In that case we need to take care to not
373 * dereference dbu after calling the sync evict func.
375 boolean_t has_async
= (dbu
->dbu_evict_func_async
!= NULL
);
377 if (dbu
->dbu_evict_func_sync
!= NULL
)
378 dbu
->dbu_evict_func_sync(dbu
);
381 taskq_dispatch_ent(dbu_evict_taskq
, dbu
->dbu_evict_func_async
,
382 dbu
, 0, &dbu
->dbu_tqent
);
387 dbuf_is_metadata(dmu_buf_impl_t
*db
)
389 if (db
->db_level
> 0) {
392 boolean_t is_metadata
;
395 is_metadata
= DMU_OT_IS_METADATA(DB_DNODE(db
)->dn_type
);
398 return (is_metadata
);
403 * This function *must* return indices evenly distributed between all
404 * sublists of the multilist. This is needed due to how the dbuf eviction
405 * code is laid out; dbuf_evict_thread() assumes dbufs are evenly
406 * distributed between all sublists and uses this assumption when
407 * deciding which sublist to evict from and how much to evict from it.
410 dbuf_cache_multilist_index_func(multilist_t
*ml
, void *obj
)
412 dmu_buf_impl_t
*db
= obj
;
415 * The assumption here, is the hash value for a given
416 * dmu_buf_impl_t will remain constant throughout it's lifetime
417 * (i.e. it's objset, object, level and blkid fields don't change).
418 * Thus, we don't need to store the dbuf's sublist index
419 * on insertion, as this index can be recalculated on removal.
421 * Also, the low order bits of the hash value are thought to be
422 * distributed evenly. Otherwise, in the case that the multilist
423 * has a power of two number of sublists, each sublists' usage
424 * would not be evenly distributed.
426 return (dbuf_hash(db
->db_objset
, db
->db
.db_object
,
427 db
->db_level
, db
->db_blkid
) %
428 multilist_get_num_sublists(ml
));
431 static inline boolean_t
432 dbuf_cache_above_hiwater(void)
434 uint64_t dbuf_cache_hiwater_bytes
=
435 (dbuf_cache_max_bytes
* dbuf_cache_hiwater_pct
) / 100;
437 return (refcount_count(&dbuf_cache_size
) >
438 dbuf_cache_max_bytes
+ dbuf_cache_hiwater_bytes
);
441 static inline boolean_t
442 dbuf_cache_above_lowater(void)
444 uint64_t dbuf_cache_lowater_bytes
=
445 (dbuf_cache_max_bytes
* dbuf_cache_lowater_pct
) / 100;
447 return (refcount_count(&dbuf_cache_size
) >
448 dbuf_cache_max_bytes
- dbuf_cache_lowater_bytes
);
452 * Evict the oldest eligible dbuf from the dbuf cache.
457 int idx
= multilist_get_random_index(dbuf_cache
);
458 multilist_sublist_t
*mls
= multilist_sublist_lock(dbuf_cache
, idx
);
460 ASSERT(!MUTEX_HELD(&dbuf_evict_lock
));
463 * Set the thread's tsd to indicate that it's processing evictions.
464 * Once a thread stops evicting from the dbuf cache it will
465 * reset its tsd to NULL.
467 ASSERT3P(tsd_get(zfs_dbuf_evict_key
), ==, NULL
);
468 (void) tsd_set(zfs_dbuf_evict_key
, (void *)B_TRUE
);
470 dmu_buf_impl_t
*db
= multilist_sublist_tail(mls
);
471 while (db
!= NULL
&& mutex_tryenter(&db
->db_mtx
) == 0) {
472 db
= multilist_sublist_prev(mls
, db
);
475 DTRACE_PROBE2(dbuf__evict__one
, dmu_buf_impl_t
*, db
,
476 multilist_sublist_t
*, mls
);
479 multilist_sublist_remove(mls
, db
);
480 multilist_sublist_unlock(mls
);
481 (void) refcount_remove_many(&dbuf_cache_size
,
485 multilist_sublist_unlock(mls
);
487 (void) tsd_set(zfs_dbuf_evict_key
, NULL
);
491 * The dbuf evict thread is responsible for aging out dbufs from the
492 * cache. Once the cache has reached it's maximum size, dbufs are removed
493 * and destroyed. The eviction thread will continue running until the size
494 * of the dbuf cache is at or below the maximum size. Once the dbuf is aged
495 * out of the cache it is destroyed and becomes eligible for arc eviction.
498 dbuf_evict_thread(void)
502 CALLB_CPR_INIT(&cpr
, &dbuf_evict_lock
, callb_generic_cpr
, FTAG
);
504 mutex_enter(&dbuf_evict_lock
);
505 while (!dbuf_evict_thread_exit
) {
506 while (!dbuf_cache_above_lowater() && !dbuf_evict_thread_exit
) {
507 CALLB_CPR_SAFE_BEGIN(&cpr
);
508 (void) cv_timedwait_hires(&dbuf_evict_cv
,
509 &dbuf_evict_lock
, SEC2NSEC(1), MSEC2NSEC(1), 0);
510 CALLB_CPR_SAFE_END(&cpr
, &dbuf_evict_lock
);
512 mutex_exit(&dbuf_evict_lock
);
515 * Keep evicting as long as we're above the low water mark
516 * for the cache. We do this without holding the locks to
517 * minimize lock contention.
519 while (dbuf_cache_above_lowater() && !dbuf_evict_thread_exit
) {
523 mutex_enter(&dbuf_evict_lock
);
526 dbuf_evict_thread_exit
= B_FALSE
;
527 cv_broadcast(&dbuf_evict_cv
);
528 CALLB_CPR_EXIT(&cpr
); /* drops dbuf_evict_lock */
533 * Wake up the dbuf eviction thread if the dbuf cache is at its max size.
534 * If the dbuf cache is at its high water mark, then evict a dbuf from the
535 * dbuf cache using the callers context.
538 dbuf_evict_notify(void)
542 * We use thread specific data to track when a thread has
543 * started processing evictions. This allows us to avoid deeply
544 * nested stacks that would have a call flow similar to this:
546 * dbuf_rele()-->dbuf_rele_and_unlock()-->dbuf_evict_notify()
549 * +-----dbuf_destroy()<--dbuf_evict_one()<--------+
551 * The dbuf_eviction_thread will always have its tsd set until
552 * that thread exits. All other threads will only set their tsd
553 * if they are participating in the eviction process. This only
554 * happens if the eviction thread is unable to process evictions
555 * fast enough. To keep the dbuf cache size in check, other threads
556 * can evict from the dbuf cache directly. Those threads will set
557 * their tsd values so that we ensure that they only evict one dbuf
558 * from the dbuf cache.
560 if (tsd_get(zfs_dbuf_evict_key
) != NULL
)
563 if (refcount_count(&dbuf_cache_size
) > dbuf_cache_max_bytes
) {
564 boolean_t evict_now
= B_FALSE
;
566 mutex_enter(&dbuf_evict_lock
);
567 if (refcount_count(&dbuf_cache_size
) > dbuf_cache_max_bytes
) {
568 evict_now
= dbuf_cache_above_hiwater();
569 cv_signal(&dbuf_evict_cv
);
571 mutex_exit(&dbuf_evict_lock
);
582 uint64_t hsize
= 1ULL << 16;
583 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
587 * The hash table is big enough to fill all of physical memory
588 * with an average 4K block size. The table will take up
589 * totalmem*sizeof(void*)/4K (i.e. 2MB/GB with 8-byte pointers).
591 while (hsize
* 4096 < physmem
* PAGESIZE
)
595 h
->hash_table_mask
= hsize
- 1;
596 h
->hash_table
= kmem_zalloc(hsize
* sizeof (void *), KM_NOSLEEP
);
597 if (h
->hash_table
== NULL
) {
598 /* XXX - we should really return an error instead of assert */
599 ASSERT(hsize
> (1ULL << 10));
604 dbuf_kmem_cache
= kmem_cache_create("dmu_buf_impl_t",
605 sizeof (dmu_buf_impl_t
),
606 0, dbuf_cons
, dbuf_dest
, NULL
, NULL
, NULL
, 0);
608 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
609 mutex_init(&h
->hash_mutexes
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
612 * Setup the parameters for the dbuf cache. We cap the size of the
613 * dbuf cache to 1/32nd (default) of the size of the ARC.
615 dbuf_cache_max_bytes
= MIN(dbuf_cache_max_bytes
,
616 arc_max_bytes() >> dbuf_cache_max_shift
);
619 * All entries are queued via taskq_dispatch_ent(), so min/maxalloc
620 * configuration is not required.
622 dbu_evict_taskq
= taskq_create("dbu_evict", 1, minclsyspri
, 0, 0, 0);
624 dbuf_cache
= multilist_create(sizeof (dmu_buf_impl_t
),
625 offsetof(dmu_buf_impl_t
, db_cache_link
),
626 dbuf_cache_multilist_index_func
);
627 refcount_create(&dbuf_cache_size
);
629 tsd_create(&zfs_dbuf_evict_key
, NULL
);
630 dbuf_evict_thread_exit
= B_FALSE
;
631 mutex_init(&dbuf_evict_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
632 cv_init(&dbuf_evict_cv
, NULL
, CV_DEFAULT
, NULL
);
633 dbuf_cache_evict_thread
= thread_create(NULL
, 0, dbuf_evict_thread
,
634 NULL
, 0, &p0
, TS_RUN
, minclsyspri
);
640 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
643 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
644 mutex_destroy(&h
->hash_mutexes
[i
]);
645 kmem_free(h
->hash_table
, (h
->hash_table_mask
+ 1) * sizeof (void *));
646 kmem_cache_destroy(dbuf_kmem_cache
);
647 taskq_destroy(dbu_evict_taskq
);
649 mutex_enter(&dbuf_evict_lock
);
650 dbuf_evict_thread_exit
= B_TRUE
;
651 while (dbuf_evict_thread_exit
) {
652 cv_signal(&dbuf_evict_cv
);
653 cv_wait(&dbuf_evict_cv
, &dbuf_evict_lock
);
655 mutex_exit(&dbuf_evict_lock
);
656 tsd_destroy(&zfs_dbuf_evict_key
);
658 mutex_destroy(&dbuf_evict_lock
);
659 cv_destroy(&dbuf_evict_cv
);
661 refcount_destroy(&dbuf_cache_size
);
662 multilist_destroy(dbuf_cache
);
671 dbuf_verify(dmu_buf_impl_t
*db
)
674 dbuf_dirty_record_t
*dr
;
676 ASSERT(MUTEX_HELD(&db
->db_mtx
));
678 if (!(zfs_flags
& ZFS_DEBUG_DBUF_VERIFY
))
681 ASSERT(db
->db_objset
!= NULL
);
685 ASSERT(db
->db_parent
== NULL
);
686 ASSERT(db
->db_blkptr
== NULL
);
688 ASSERT3U(db
->db
.db_object
, ==, dn
->dn_object
);
689 ASSERT3P(db
->db_objset
, ==, dn
->dn_objset
);
690 ASSERT3U(db
->db_level
, <, dn
->dn_nlevels
);
691 ASSERT(db
->db_blkid
== DMU_BONUS_BLKID
||
692 db
->db_blkid
== DMU_SPILL_BLKID
||
693 !avl_is_empty(&dn
->dn_dbufs
));
695 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
697 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
698 ASSERT3U(db
->db
.db_offset
, ==, DMU_BONUS_BLKID
);
699 } else if (db
->db_blkid
== DMU_SPILL_BLKID
) {
701 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
702 ASSERT0(db
->db
.db_offset
);
704 ASSERT3U(db
->db
.db_offset
, ==, db
->db_blkid
* db
->db
.db_size
);
707 for (dr
= db
->db_data_pending
; dr
!= NULL
; dr
= dr
->dr_next
)
708 ASSERT(dr
->dr_dbuf
== db
);
710 for (dr
= db
->db_last_dirty
; dr
!= NULL
; dr
= dr
->dr_next
)
711 ASSERT(dr
->dr_dbuf
== db
);
714 * We can't assert that db_size matches dn_datablksz because it
715 * can be momentarily different when another thread is doing
718 if (db
->db_level
== 0 && db
->db
.db_object
== DMU_META_DNODE_OBJECT
) {
719 dr
= db
->db_data_pending
;
721 * It should only be modified in syncing context, so
722 * make sure we only have one copy of the data.
724 ASSERT(dr
== NULL
|| dr
->dt
.dl
.dr_data
== db
->db_buf
);
727 /* verify db->db_blkptr */
729 if (db
->db_parent
== dn
->dn_dbuf
) {
730 /* db is pointed to by the dnode */
731 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
732 if (DMU_OBJECT_IS_SPECIAL(db
->db
.db_object
))
733 ASSERT(db
->db_parent
== NULL
);
735 ASSERT(db
->db_parent
!= NULL
);
736 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
737 ASSERT3P(db
->db_blkptr
, ==,
738 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
740 /* db is pointed to by an indirect block */
741 int epb
= db
->db_parent
->db
.db_size
>> SPA_BLKPTRSHIFT
;
742 ASSERT3U(db
->db_parent
->db_level
, ==, db
->db_level
+1);
743 ASSERT3U(db
->db_parent
->db
.db_object
, ==,
746 * dnode_grow_indblksz() can make this fail if we don't
747 * have the struct_rwlock. XXX indblksz no longer
748 * grows. safe to do this now?
750 if (RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
751 ASSERT3P(db
->db_blkptr
, ==,
752 ((blkptr_t
*)db
->db_parent
->db
.db_data
+
753 db
->db_blkid
% epb
));
757 if ((db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
)) &&
758 (db
->db_buf
== NULL
|| db
->db_buf
->b_data
) &&
759 db
->db
.db_data
&& db
->db_blkid
!= DMU_BONUS_BLKID
&&
760 db
->db_state
!= DB_FILL
&& !dn
->dn_free_txg
) {
762 * If the blkptr isn't set but they have nonzero data,
763 * it had better be dirty, otherwise we'll lose that
764 * data when we evict this buffer.
766 * There is an exception to this rule for indirect blocks; in
767 * this case, if the indirect block is a hole, we fill in a few
768 * fields on each of the child blocks (importantly, birth time)
769 * to prevent hole birth times from being lost when you
770 * partially fill in a hole.
772 if (db
->db_dirtycnt
== 0) {
773 if (db
->db_level
== 0) {
774 uint64_t *buf
= db
->db
.db_data
;
777 for (i
= 0; i
< db
->db
.db_size
>> 3; i
++) {
781 blkptr_t
*bps
= db
->db
.db_data
;
782 ASSERT3U(1 << DB_DNODE(db
)->dn_indblkshift
, ==,
785 * We want to verify that all the blkptrs in the
786 * indirect block are holes, but we may have
787 * automatically set up a few fields for them.
788 * We iterate through each blkptr and verify
789 * they only have those fields set.
792 i
< db
->db
.db_size
/ sizeof (blkptr_t
);
794 blkptr_t
*bp
= &bps
[i
];
795 ASSERT(ZIO_CHECKSUM_IS_ZERO(
798 DVA_IS_EMPTY(&bp
->blk_dva
[0]) &&
799 DVA_IS_EMPTY(&bp
->blk_dva
[1]) &&
800 DVA_IS_EMPTY(&bp
->blk_dva
[2]));
801 ASSERT0(bp
->blk_fill
);
802 ASSERT0(bp
->blk_pad
[0]);
803 ASSERT0(bp
->blk_pad
[1]);
804 ASSERT(!BP_IS_EMBEDDED(bp
));
805 ASSERT(BP_IS_HOLE(bp
));
806 ASSERT0(bp
->blk_phys_birth
);
816 dbuf_clear_data(dmu_buf_impl_t
*db
)
818 ASSERT(MUTEX_HELD(&db
->db_mtx
));
820 ASSERT3P(db
->db_buf
, ==, NULL
);
821 db
->db
.db_data
= NULL
;
822 if (db
->db_state
!= DB_NOFILL
)
823 db
->db_state
= DB_UNCACHED
;
827 dbuf_set_data(dmu_buf_impl_t
*db
, arc_buf_t
*buf
)
829 ASSERT(MUTEX_HELD(&db
->db_mtx
));
833 ASSERT(buf
->b_data
!= NULL
);
834 db
->db
.db_data
= buf
->b_data
;
838 * Loan out an arc_buf for read. Return the loaned arc_buf.
841 dbuf_loan_arcbuf(dmu_buf_impl_t
*db
)
845 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
846 mutex_enter(&db
->db_mtx
);
847 if (arc_released(db
->db_buf
) || refcount_count(&db
->db_holds
) > 1) {
848 int blksz
= db
->db
.db_size
;
849 spa_t
*spa
= db
->db_objset
->os_spa
;
851 mutex_exit(&db
->db_mtx
);
852 abuf
= arc_loan_buf(spa
, B_FALSE
, blksz
);
853 bcopy(db
->db
.db_data
, abuf
->b_data
, blksz
);
856 arc_loan_inuse_buf(abuf
, db
);
859 mutex_exit(&db
->db_mtx
);
865 * Calculate which level n block references the data at the level 0 offset
869 dbuf_whichblock(dnode_t
*dn
, int64_t level
, uint64_t offset
)
871 if (dn
->dn_datablkshift
!= 0 && dn
->dn_indblkshift
!= 0) {
873 * The level n blkid is equal to the level 0 blkid divided by
874 * the number of level 0s in a level n block.
876 * The level 0 blkid is offset >> datablkshift =
877 * offset / 2^datablkshift.
879 * The number of level 0s in a level n is the number of block
880 * pointers in an indirect block, raised to the power of level.
881 * This is 2^(indblkshift - SPA_BLKPTRSHIFT)^level =
882 * 2^(level*(indblkshift - SPA_BLKPTRSHIFT)).
884 * Thus, the level n blkid is: offset /
885 * ((2^datablkshift)*(2^(level*(indblkshift - SPA_BLKPTRSHIFT)))
886 * = offset / 2^(datablkshift + level *
887 * (indblkshift - SPA_BLKPTRSHIFT))
888 * = offset >> (datablkshift + level *
889 * (indblkshift - SPA_BLKPTRSHIFT))
891 return (offset
>> (dn
->dn_datablkshift
+ level
*
892 (dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
)));
894 ASSERT3U(offset
, <, dn
->dn_datablksz
);
900 dbuf_read_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
902 dmu_buf_impl_t
*db
= vdb
;
904 mutex_enter(&db
->db_mtx
);
905 ASSERT3U(db
->db_state
, ==, DB_READ
);
907 * All reads are synchronous, so we must have a hold on the dbuf
909 ASSERT(refcount_count(&db
->db_holds
) > 0);
910 ASSERT(db
->db_buf
== NULL
);
911 ASSERT(db
->db
.db_data
== NULL
);
912 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
913 /* we were freed in flight; disregard any error */
914 arc_release(buf
, db
);
915 bzero(buf
->b_data
, db
->db
.db_size
);
917 db
->db_freed_in_flight
= FALSE
;
918 dbuf_set_data(db
, buf
);
919 db
->db_state
= DB_CACHED
;
920 } else if (zio
== NULL
|| zio
->io_error
== 0) {
921 dbuf_set_data(db
, buf
);
922 db
->db_state
= DB_CACHED
;
924 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
925 ASSERT3P(db
->db_buf
, ==, NULL
);
926 arc_buf_destroy(buf
, db
);
927 db
->db_state
= DB_UNCACHED
;
929 cv_broadcast(&db
->db_changed
);
930 dbuf_rele_and_unlock(db
, NULL
);
934 dbuf_read_impl(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t flags
)
938 arc_flags_t aflags
= ARC_FLAG_NOWAIT
;
942 ASSERT(!refcount_is_zero(&db
->db_holds
));
943 /* We need the struct_rwlock to prevent db_blkptr from changing. */
944 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
945 ASSERT(MUTEX_HELD(&db
->db_mtx
));
946 ASSERT(db
->db_state
== DB_UNCACHED
);
947 ASSERT(db
->db_buf
== NULL
);
949 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
950 int bonuslen
= MIN(dn
->dn_bonuslen
, dn
->dn_phys
->dn_bonuslen
);
952 ASSERT3U(bonuslen
, <=, db
->db
.db_size
);
953 db
->db
.db_data
= zio_buf_alloc(DN_MAX_BONUSLEN
);
954 arc_space_consume(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
955 if (bonuslen
< DN_MAX_BONUSLEN
)
956 bzero(db
->db
.db_data
, DN_MAX_BONUSLEN
);
958 bcopy(DN_BONUS(dn
->dn_phys
), db
->db
.db_data
, bonuslen
);
960 db
->db_state
= DB_CACHED
;
961 mutex_exit(&db
->db_mtx
);
966 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
967 * processes the delete record and clears the bp while we are waiting
968 * for the dn_mtx (resulting in a "no" from block_freed).
970 if (db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
) ||
971 (db
->db_level
== 0 && (dnode_block_freed(dn
, db
->db_blkid
) ||
972 BP_IS_HOLE(db
->db_blkptr
)))) {
973 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
975 dbuf_set_data(db
, arc_alloc_buf(db
->db_objset
->os_spa
, db
, type
,
977 bzero(db
->db
.db_data
, db
->db
.db_size
);
979 if (db
->db_blkptr
!= NULL
&& db
->db_level
> 0 &&
980 BP_IS_HOLE(db
->db_blkptr
) &&
981 db
->db_blkptr
->blk_birth
!= 0) {
982 blkptr_t
*bps
= db
->db
.db_data
;
983 for (int i
= 0; i
< ((1 <<
984 DB_DNODE(db
)->dn_indblkshift
) / sizeof (blkptr_t
));
986 blkptr_t
*bp
= &bps
[i
];
987 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==,
988 1 << dn
->dn_indblkshift
);
990 BP_GET_LEVEL(db
->db_blkptr
) == 1 ?
992 BP_GET_LSIZE(db
->db_blkptr
));
993 BP_SET_TYPE(bp
, BP_GET_TYPE(db
->db_blkptr
));
995 BP_GET_LEVEL(db
->db_blkptr
) - 1);
996 BP_SET_BIRTH(bp
, db
->db_blkptr
->blk_birth
, 0);
1000 db
->db_state
= DB_CACHED
;
1001 mutex_exit(&db
->db_mtx
);
1007 db
->db_state
= DB_READ
;
1008 mutex_exit(&db
->db_mtx
);
1010 if (DBUF_IS_L2CACHEABLE(db
))
1011 aflags
|= ARC_FLAG_L2CACHE
;
1013 SET_BOOKMARK(&zb
, db
->db_objset
->os_dsl_dataset
?
1014 db
->db_objset
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
1015 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
1017 dbuf_add_ref(db
, NULL
);
1019 (void) arc_read(zio
, db
->db_objset
->os_spa
, db
->db_blkptr
,
1020 dbuf_read_done
, db
, ZIO_PRIORITY_SYNC_READ
,
1021 (flags
& DB_RF_CANFAIL
) ? ZIO_FLAG_CANFAIL
: ZIO_FLAG_MUSTSUCCEED
,
1026 * This is our just-in-time copy function. It makes a copy of buffers that
1027 * have been modified in a previous transaction group before we access them in
1028 * the current active group.
1030 * This function is used in three places: when we are dirtying a buffer for the
1031 * first time in a txg, when we are freeing a range in a dnode that includes
1032 * this buffer, and when we are accessing a buffer which was received compressed
1033 * and later referenced in a WRITE_BYREF record.
1035 * Note that when we are called from dbuf_free_range() we do not put a hold on
1036 * the buffer, we just traverse the active dbuf list for the dnode.
1039 dbuf_fix_old_data(dmu_buf_impl_t
*db
, uint64_t txg
)
1041 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
1043 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1044 ASSERT(db
->db
.db_data
!= NULL
);
1045 ASSERT(db
->db_level
== 0);
1046 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
);
1049 (dr
->dt
.dl
.dr_data
!=
1050 ((db
->db_blkid
== DMU_BONUS_BLKID
) ? db
->db
.db_data
: db
->db_buf
)))
1054 * If the last dirty record for this dbuf has not yet synced
1055 * and its referencing the dbuf data, either:
1056 * reset the reference to point to a new copy,
1057 * or (if there a no active holders)
1058 * just null out the current db_data pointer.
1060 ASSERT(dr
->dr_txg
>= txg
- 2);
1061 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1062 /* Note that the data bufs here are zio_bufs */
1063 dr
->dt
.dl
.dr_data
= zio_buf_alloc(DN_MAX_BONUSLEN
);
1064 arc_space_consume(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
1065 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
, DN_MAX_BONUSLEN
);
1066 } else if (refcount_count(&db
->db_holds
) > db
->db_dirtycnt
) {
1067 int size
= arc_buf_size(db
->db_buf
);
1068 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1069 spa_t
*spa
= db
->db_objset
->os_spa
;
1070 enum zio_compress compress_type
=
1071 arc_get_compression(db
->db_buf
);
1073 if (compress_type
== ZIO_COMPRESS_OFF
) {
1074 dr
->dt
.dl
.dr_data
= arc_alloc_buf(spa
, db
, type
, size
);
1076 ASSERT3U(type
, ==, ARC_BUFC_DATA
);
1077 dr
->dt
.dl
.dr_data
= arc_alloc_compressed_buf(spa
, db
,
1078 size
, arc_buf_lsize(db
->db_buf
), compress_type
);
1080 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
->b_data
, size
);
1083 dbuf_clear_data(db
);
1088 dbuf_read(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t flags
)
1091 boolean_t havepzio
= (zio
!= NULL
);
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
;
1137 zio
= zio_root(spa
, NULL
, NULL
, ZIO_FLAG_CANFAIL
);
1138 dbuf_read_impl(db
, zio
, flags
);
1140 /* dbuf_read_impl has dropped db_mtx for us */
1143 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
);
1145 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1146 rw_exit(&dn
->dn_struct_rwlock
);
1150 err
= zio_wait(zio
);
1153 * Another reader came in while the dbuf was in flight
1154 * between UNCACHED and CACHED. Either a writer will finish
1155 * writing the buffer (sending the dbuf to CACHED) or the
1156 * first reader's request will reach the read_done callback
1157 * and send the dbuf to CACHED. Otherwise, a failure
1158 * occurred and the dbuf went to UNCACHED.
1160 mutex_exit(&db
->db_mtx
);
1162 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
);
1163 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1164 rw_exit(&dn
->dn_struct_rwlock
);
1167 /* Skip the wait per the caller's request. */
1168 mutex_enter(&db
->db_mtx
);
1169 if ((flags
& DB_RF_NEVERWAIT
) == 0) {
1170 while (db
->db_state
== DB_READ
||
1171 db
->db_state
== DB_FILL
) {
1172 ASSERT(db
->db_state
== DB_READ
||
1173 (flags
& DB_RF_HAVESTRUCT
) == 0);
1174 DTRACE_PROBE2(blocked__read
, dmu_buf_impl_t
*,
1176 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1178 if (db
->db_state
== DB_UNCACHED
)
1179 err
= SET_ERROR(EIO
);
1181 mutex_exit(&db
->db_mtx
);
1184 ASSERT(err
|| havepzio
|| db
->db_state
== DB_CACHED
);
1189 dbuf_noread(dmu_buf_impl_t
*db
)
1191 ASSERT(!refcount_is_zero(&db
->db_holds
));
1192 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1193 mutex_enter(&db
->db_mtx
);
1194 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
1195 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1196 if (db
->db_state
== DB_UNCACHED
) {
1197 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1198 spa_t
*spa
= db
->db_objset
->os_spa
;
1200 ASSERT(db
->db_buf
== NULL
);
1201 ASSERT(db
->db
.db_data
== NULL
);
1202 dbuf_set_data(db
, arc_alloc_buf(spa
, db
, type
, db
->db
.db_size
));
1203 db
->db_state
= DB_FILL
;
1204 } else if (db
->db_state
== DB_NOFILL
) {
1205 dbuf_clear_data(db
);
1207 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
1209 mutex_exit(&db
->db_mtx
);
1213 dbuf_unoverride(dbuf_dirty_record_t
*dr
)
1215 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
1216 blkptr_t
*bp
= &dr
->dt
.dl
.dr_overridden_by
;
1217 uint64_t txg
= dr
->dr_txg
;
1219 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1220 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_IN_DMU_SYNC
);
1221 ASSERT(db
->db_level
== 0);
1223 if (db
->db_blkid
== DMU_BONUS_BLKID
||
1224 dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
)
1227 ASSERT(db
->db_data_pending
!= dr
);
1229 /* free this block */
1230 if (!BP_IS_HOLE(bp
) && !dr
->dt
.dl
.dr_nopwrite
)
1231 zio_free(db
->db_objset
->os_spa
, txg
, bp
);
1233 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
1234 dr
->dt
.dl
.dr_nopwrite
= B_FALSE
;
1237 * Release the already-written buffer, so we leave it in
1238 * a consistent dirty state. Note that all callers are
1239 * modifying the buffer, so they will immediately do
1240 * another (redundant) arc_release(). Therefore, leave
1241 * the buf thawed to save the effort of freezing &
1242 * immediately re-thawing it.
1244 arc_release(dr
->dt
.dl
.dr_data
, db
);
1248 * Evict (if its unreferenced) or clear (if its referenced) any level-0
1249 * data blocks in the free range, so that any future readers will find
1253 dbuf_free_range(dnode_t
*dn
, uint64_t start_blkid
, uint64_t end_blkid
,
1256 dmu_buf_impl_t db_search
;
1257 dmu_buf_impl_t
*db
, *db_next
;
1258 uint64_t txg
= tx
->tx_txg
;
1261 if (end_blkid
> dn
->dn_maxblkid
&&
1262 !(start_blkid
== DMU_SPILL_BLKID
|| end_blkid
== DMU_SPILL_BLKID
))
1263 end_blkid
= dn
->dn_maxblkid
;
1264 dprintf_dnode(dn
, "start=%llu end=%llu\n", start_blkid
, end_blkid
);
1266 db_search
.db_level
= 0;
1267 db_search
.db_blkid
= start_blkid
;
1268 db_search
.db_state
= DB_SEARCH
;
1270 mutex_enter(&dn
->dn_dbufs_mtx
);
1271 db
= avl_find(&dn
->dn_dbufs
, &db_search
, &where
);
1272 ASSERT3P(db
, ==, NULL
);
1274 db
= avl_nearest(&dn
->dn_dbufs
, where
, AVL_AFTER
);
1276 for (; db
!= NULL
; db
= db_next
) {
1277 db_next
= AVL_NEXT(&dn
->dn_dbufs
, db
);
1278 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1280 if (db
->db_level
!= 0 || db
->db_blkid
> end_blkid
) {
1283 ASSERT3U(db
->db_blkid
, >=, start_blkid
);
1285 /* found a level 0 buffer in the range */
1286 mutex_enter(&db
->db_mtx
);
1287 if (dbuf_undirty(db
, tx
)) {
1288 /* mutex has been dropped and dbuf destroyed */
1292 if (db
->db_state
== DB_UNCACHED
||
1293 db
->db_state
== DB_NOFILL
||
1294 db
->db_state
== DB_EVICTING
) {
1295 ASSERT(db
->db
.db_data
== NULL
);
1296 mutex_exit(&db
->db_mtx
);
1299 if (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
) {
1300 /* will be handled in dbuf_read_done or dbuf_rele */
1301 db
->db_freed_in_flight
= TRUE
;
1302 mutex_exit(&db
->db_mtx
);
1305 if (refcount_count(&db
->db_holds
) == 0) {
1310 /* The dbuf is referenced */
1312 if (db
->db_last_dirty
!= NULL
) {
1313 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
1315 if (dr
->dr_txg
== txg
) {
1317 * This buffer is "in-use", re-adjust the file
1318 * size to reflect that this buffer may
1319 * contain new data when we sync.
1321 if (db
->db_blkid
!= DMU_SPILL_BLKID
&&
1322 db
->db_blkid
> dn
->dn_maxblkid
)
1323 dn
->dn_maxblkid
= db
->db_blkid
;
1324 dbuf_unoverride(dr
);
1327 * This dbuf is not dirty in the open context.
1328 * Either uncache it (if its not referenced in
1329 * the open context) or reset its contents to
1332 dbuf_fix_old_data(db
, txg
);
1335 /* clear the contents if its cached */
1336 if (db
->db_state
== DB_CACHED
) {
1337 ASSERT(db
->db
.db_data
!= NULL
);
1338 arc_release(db
->db_buf
, db
);
1339 bzero(db
->db
.db_data
, db
->db
.db_size
);
1340 arc_buf_freeze(db
->db_buf
);
1343 mutex_exit(&db
->db_mtx
);
1345 mutex_exit(&dn
->dn_dbufs_mtx
);
1349 dbuf_new_size(dmu_buf_impl_t
*db
, int size
, dmu_tx_t
*tx
)
1351 arc_buf_t
*buf
, *obuf
;
1352 int osize
= db
->db
.db_size
;
1353 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1356 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1361 /* XXX does *this* func really need the lock? */
1362 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
1365 * This call to dmu_buf_will_dirty() with the dn_struct_rwlock held
1366 * is OK, because there can be no other references to the db
1367 * when we are changing its size, so no concurrent DB_FILL can
1371 * XXX we should be doing a dbuf_read, checking the return
1372 * value and returning that up to our callers
1374 dmu_buf_will_dirty(&db
->db
, tx
);
1376 /* create the data buffer for the new block */
1377 buf
= arc_alloc_buf(dn
->dn_objset
->os_spa
, db
, type
, size
);
1379 /* copy old block data to the new block */
1381 bcopy(obuf
->b_data
, buf
->b_data
, MIN(osize
, size
));
1382 /* zero the remainder */
1384 bzero((uint8_t *)buf
->b_data
+ osize
, size
- osize
);
1386 mutex_enter(&db
->db_mtx
);
1387 dbuf_set_data(db
, buf
);
1388 arc_buf_destroy(obuf
, db
);
1389 db
->db
.db_size
= size
;
1391 if (db
->db_level
== 0) {
1392 ASSERT3U(db
->db_last_dirty
->dr_txg
, ==, tx
->tx_txg
);
1393 db
->db_last_dirty
->dt
.dl
.dr_data
= buf
;
1395 mutex_exit(&db
->db_mtx
);
1397 dmu_objset_willuse_space(dn
->dn_objset
, size
- osize
, tx
);
1402 dbuf_release_bp(dmu_buf_impl_t
*db
)
1404 objset_t
*os
= db
->db_objset
;
1406 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os
)));
1407 ASSERT(arc_released(os
->os_phys_buf
) ||
1408 list_link_active(&os
->os_dsl_dataset
->ds_synced_link
));
1409 ASSERT(db
->db_parent
== NULL
|| arc_released(db
->db_parent
->db_buf
));
1411 (void) arc_release(db
->db_buf
, db
);
1415 * We already have a dirty record for this TXG, and we are being
1419 dbuf_redirty(dbuf_dirty_record_t
*dr
)
1421 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
1423 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1425 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
) {
1427 * If this buffer has already been written out,
1428 * we now need to reset its state.
1430 dbuf_unoverride(dr
);
1431 if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
&&
1432 db
->db_state
!= DB_NOFILL
) {
1433 /* Already released on initial dirty, so just thaw. */
1434 ASSERT(arc_released(db
->db_buf
));
1435 arc_buf_thaw(db
->db_buf
);
1440 dbuf_dirty_record_t
*
1441 dbuf_dirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1445 dbuf_dirty_record_t
**drp
, *dr
;
1446 int drop_struct_lock
= FALSE
;
1447 int txgoff
= tx
->tx_txg
& TXG_MASK
;
1449 ASSERT(tx
->tx_txg
!= 0);
1450 ASSERT(!refcount_is_zero(&db
->db_holds
));
1451 DMU_TX_DIRTY_BUF(tx
, db
);
1456 * Shouldn't dirty a regular buffer in syncing context. Private
1457 * objects may be dirtied in syncing context, but only if they
1458 * were already pre-dirtied in open context.
1461 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
) {
1462 rrw_enter(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
,
1465 ASSERT(!dmu_tx_is_syncing(tx
) ||
1466 BP_IS_HOLE(dn
->dn_objset
->os_rootbp
) ||
1467 DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
1468 dn
->dn_objset
->os_dsl_dataset
== NULL
);
1469 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
1470 rrw_exit(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
, FTAG
);
1473 * We make this assert for private objects as well, but after we
1474 * check if we're already dirty. They are allowed to re-dirty
1475 * in syncing context.
1477 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
1478 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
1479 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
1481 mutex_enter(&db
->db_mtx
);
1483 * XXX make this true for indirects too? The problem is that
1484 * transactions created with dmu_tx_create_assigned() from
1485 * syncing context don't bother holding ahead.
1487 ASSERT(db
->db_level
!= 0 ||
1488 db
->db_state
== DB_CACHED
|| db
->db_state
== DB_FILL
||
1489 db
->db_state
== DB_NOFILL
);
1491 mutex_enter(&dn
->dn_mtx
);
1493 * Don't set dirtyctx to SYNC if we're just modifying this as we
1494 * initialize the objset.
1496 if (dn
->dn_dirtyctx
== DN_UNDIRTIED
) {
1497 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
) {
1498 rrw_enter(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
,
1501 if (!BP_IS_HOLE(dn
->dn_objset
->os_rootbp
)) {
1502 dn
->dn_dirtyctx
= (dmu_tx_is_syncing(tx
) ?
1503 DN_DIRTY_SYNC
: DN_DIRTY_OPEN
);
1504 ASSERT(dn
->dn_dirtyctx_firstset
== NULL
);
1505 dn
->dn_dirtyctx_firstset
= kmem_alloc(1, KM_SLEEP
);
1507 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
) {
1508 rrw_exit(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
,
1512 mutex_exit(&dn
->dn_mtx
);
1514 if (db
->db_blkid
== DMU_SPILL_BLKID
)
1515 dn
->dn_have_spill
= B_TRUE
;
1518 * If this buffer is already dirty, we're done.
1520 drp
= &db
->db_last_dirty
;
1521 ASSERT(*drp
== NULL
|| (*drp
)->dr_txg
<= tx
->tx_txg
||
1522 db
->db
.db_object
== DMU_META_DNODE_OBJECT
);
1523 while ((dr
= *drp
) != NULL
&& dr
->dr_txg
> tx
->tx_txg
)
1525 if (dr
&& dr
->dr_txg
== tx
->tx_txg
) {
1529 mutex_exit(&db
->db_mtx
);
1534 * Only valid if not already dirty.
1536 ASSERT(dn
->dn_object
== 0 ||
1537 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
1538 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
1540 ASSERT3U(dn
->dn_nlevels
, >, db
->db_level
);
1541 ASSERT((dn
->dn_phys
->dn_nlevels
== 0 && db
->db_level
== 0) ||
1542 dn
->dn_phys
->dn_nlevels
> db
->db_level
||
1543 dn
->dn_next_nlevels
[txgoff
] > db
->db_level
||
1544 dn
->dn_next_nlevels
[(tx
->tx_txg
-1) & TXG_MASK
] > db
->db_level
||
1545 dn
->dn_next_nlevels
[(tx
->tx_txg
-2) & TXG_MASK
] > db
->db_level
);
1548 * We should only be dirtying in syncing context if it's the
1549 * mos or we're initializing the os or it's a special object.
1550 * However, we are allowed to dirty in syncing context provided
1551 * we already dirtied it in open context. Hence we must make
1552 * this assertion only if we're not already dirty.
1555 VERIFY3U(tx
->tx_txg
, <=, spa_final_dirty_txg(os
->os_spa
));
1557 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
1558 rrw_enter(&os
->os_dsl_dataset
->ds_bp_rwlock
, RW_READER
, FTAG
);
1559 ASSERT(!dmu_tx_is_syncing(tx
) || DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
1560 os
->os_dsl_dataset
== NULL
|| BP_IS_HOLE(os
->os_rootbp
));
1561 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
1562 rrw_exit(&os
->os_dsl_dataset
->ds_bp_rwlock
, FTAG
);
1564 ASSERT(db
->db
.db_size
!= 0);
1566 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
1568 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
1569 dmu_objset_willuse_space(os
, db
->db
.db_size
, tx
);
1573 * If this buffer is dirty in an old transaction group we need
1574 * to make a copy of it so that the changes we make in this
1575 * transaction group won't leak out when we sync the older txg.
1577 dr
= kmem_zalloc(sizeof (dbuf_dirty_record_t
), KM_SLEEP
);
1578 if (db
->db_level
== 0) {
1579 void *data_old
= db
->db_buf
;
1581 if (db
->db_state
!= DB_NOFILL
) {
1582 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1583 dbuf_fix_old_data(db
, tx
->tx_txg
);
1584 data_old
= db
->db
.db_data
;
1585 } else if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
) {
1587 * Release the data buffer from the cache so
1588 * that we can modify it without impacting
1589 * possible other users of this cached data
1590 * block. Note that indirect blocks and
1591 * private objects are not released until the
1592 * syncing state (since they are only modified
1595 arc_release(db
->db_buf
, db
);
1596 dbuf_fix_old_data(db
, tx
->tx_txg
);
1597 data_old
= db
->db_buf
;
1599 ASSERT(data_old
!= NULL
);
1601 dr
->dt
.dl
.dr_data
= data_old
;
1603 mutex_init(&dr
->dt
.di
.dr_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
1604 list_create(&dr
->dt
.di
.dr_children
,
1605 sizeof (dbuf_dirty_record_t
),
1606 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
1608 if (db
->db_blkid
!= DMU_BONUS_BLKID
&& os
->os_dsl_dataset
!= NULL
)
1609 dr
->dr_accounted
= db
->db
.db_size
;
1611 dr
->dr_txg
= tx
->tx_txg
;
1616 * We could have been freed_in_flight between the dbuf_noread
1617 * and dbuf_dirty. We win, as though the dbuf_noread() had
1618 * happened after the free.
1620 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1621 db
->db_blkid
!= DMU_SPILL_BLKID
) {
1622 mutex_enter(&dn
->dn_mtx
);
1623 if (dn
->dn_free_ranges
[txgoff
] != NULL
) {
1624 range_tree_clear(dn
->dn_free_ranges
[txgoff
],
1627 mutex_exit(&dn
->dn_mtx
);
1628 db
->db_freed_in_flight
= FALSE
;
1632 * This buffer is now part of this txg
1634 dbuf_add_ref(db
, (void *)(uintptr_t)tx
->tx_txg
);
1635 db
->db_dirtycnt
+= 1;
1636 ASSERT3U(db
->db_dirtycnt
, <=, 3);
1638 mutex_exit(&db
->db_mtx
);
1640 if (db
->db_blkid
== DMU_BONUS_BLKID
||
1641 db
->db_blkid
== DMU_SPILL_BLKID
) {
1642 mutex_enter(&dn
->dn_mtx
);
1643 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1644 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
1645 mutex_exit(&dn
->dn_mtx
);
1646 dnode_setdirty(dn
, tx
);
1652 * The dn_struct_rwlock prevents db_blkptr from changing
1653 * due to a write from syncing context completing
1654 * while we are running, so we want to acquire it before
1655 * looking at db_blkptr.
1657 if (!RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
1658 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
1659 drop_struct_lock
= TRUE
;
1663 * If we are overwriting a dedup BP, then unless it is snapshotted,
1664 * when we get to syncing context we will need to decrement its
1665 * refcount in the DDT. Prefetch the relevant DDT block so that
1666 * syncing context won't have to wait for the i/o.
1668 ddt_prefetch(os
->os_spa
, db
->db_blkptr
);
1670 if (db
->db_level
== 0) {
1671 dnode_new_blkid(dn
, db
->db_blkid
, tx
, drop_struct_lock
);
1672 ASSERT(dn
->dn_maxblkid
>= db
->db_blkid
);
1675 if (db
->db_level
+1 < dn
->dn_nlevels
) {
1676 dmu_buf_impl_t
*parent
= db
->db_parent
;
1677 dbuf_dirty_record_t
*di
;
1678 int parent_held
= FALSE
;
1680 if (db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
) {
1681 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1683 parent
= dbuf_hold_level(dn
, db
->db_level
+1,
1684 db
->db_blkid
>> epbs
, FTAG
);
1685 ASSERT(parent
!= NULL
);
1688 if (drop_struct_lock
)
1689 rw_exit(&dn
->dn_struct_rwlock
);
1690 ASSERT3U(db
->db_level
+1, ==, parent
->db_level
);
1691 di
= dbuf_dirty(parent
, tx
);
1693 dbuf_rele(parent
, FTAG
);
1695 mutex_enter(&db
->db_mtx
);
1697 * Since we've dropped the mutex, it's possible that
1698 * dbuf_undirty() might have changed this out from under us.
1700 if (db
->db_last_dirty
== dr
||
1701 dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
1702 mutex_enter(&di
->dt
.di
.dr_mtx
);
1703 ASSERT3U(di
->dr_txg
, ==, tx
->tx_txg
);
1704 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1705 list_insert_tail(&di
->dt
.di
.dr_children
, dr
);
1706 mutex_exit(&di
->dt
.di
.dr_mtx
);
1709 mutex_exit(&db
->db_mtx
);
1711 ASSERT(db
->db_level
+1 == dn
->dn_nlevels
);
1712 ASSERT(db
->db_blkid
< dn
->dn_nblkptr
);
1713 ASSERT(db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
1714 mutex_enter(&dn
->dn_mtx
);
1715 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1716 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
1717 mutex_exit(&dn
->dn_mtx
);
1718 if (drop_struct_lock
)
1719 rw_exit(&dn
->dn_struct_rwlock
);
1722 dnode_setdirty(dn
, tx
);
1728 * Undirty a buffer in the transaction group referenced by the given
1729 * transaction. Return whether this evicted the dbuf.
1732 dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1735 uint64_t txg
= tx
->tx_txg
;
1736 dbuf_dirty_record_t
*dr
, **drp
;
1741 * Due to our use of dn_nlevels below, this can only be called
1742 * in open context, unless we are operating on the MOS.
1743 * From syncing context, dn_nlevels may be different from the
1744 * dn_nlevels used when dbuf was dirtied.
1746 ASSERT(db
->db_objset
==
1747 dmu_objset_pool(db
->db_objset
)->dp_meta_objset
||
1748 txg
!= spa_syncing_txg(dmu_objset_spa(db
->db_objset
)));
1749 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1750 ASSERT0(db
->db_level
);
1751 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1754 * If this buffer is not dirty, we're done.
1756 for (drp
= &db
->db_last_dirty
; (dr
= *drp
) != NULL
; drp
= &dr
->dr_next
)
1757 if (dr
->dr_txg
<= txg
)
1759 if (dr
== NULL
|| dr
->dr_txg
< txg
)
1761 ASSERT(dr
->dr_txg
== txg
);
1762 ASSERT(dr
->dr_dbuf
== db
);
1767 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
1769 ASSERT(db
->db
.db_size
!= 0);
1771 dsl_pool_undirty_space(dmu_objset_pool(dn
->dn_objset
),
1772 dr
->dr_accounted
, txg
);
1777 * Note that there are three places in dbuf_dirty()
1778 * where this dirty record may be put on a list.
1779 * Make sure to do a list_remove corresponding to
1780 * every one of those list_insert calls.
1782 if (dr
->dr_parent
) {
1783 mutex_enter(&dr
->dr_parent
->dt
.di
.dr_mtx
);
1784 list_remove(&dr
->dr_parent
->dt
.di
.dr_children
, dr
);
1785 mutex_exit(&dr
->dr_parent
->dt
.di
.dr_mtx
);
1786 } else if (db
->db_blkid
== DMU_SPILL_BLKID
||
1787 db
->db_level
+ 1 == dn
->dn_nlevels
) {
1788 ASSERT(db
->db_blkptr
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
1789 mutex_enter(&dn
->dn_mtx
);
1790 list_remove(&dn
->dn_dirty_records
[txg
& TXG_MASK
], dr
);
1791 mutex_exit(&dn
->dn_mtx
);
1795 if (db
->db_state
!= DB_NOFILL
) {
1796 dbuf_unoverride(dr
);
1798 ASSERT(db
->db_buf
!= NULL
);
1799 ASSERT(dr
->dt
.dl
.dr_data
!= NULL
);
1800 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
1801 arc_buf_destroy(dr
->dt
.dl
.dr_data
, db
);
1804 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
1806 ASSERT(db
->db_dirtycnt
> 0);
1807 db
->db_dirtycnt
-= 1;
1809 if (refcount_remove(&db
->db_holds
, (void *)(uintptr_t)txg
) == 0) {
1810 ASSERT(db
->db_state
== DB_NOFILL
|| arc_released(db
->db_buf
));
1819 dmu_buf_will_dirty(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1821 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1822 int rf
= DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
;
1824 ASSERT(tx
->tx_txg
!= 0);
1825 ASSERT(!refcount_is_zero(&db
->db_holds
));
1828 * Quick check for dirtyness. For already dirty blocks, this
1829 * reduces runtime of this function by >90%, and overall performance
1830 * by 50% for some workloads (e.g. file deletion with indirect blocks
1833 mutex_enter(&db
->db_mtx
);
1834 dbuf_dirty_record_t
*dr
;
1835 for (dr
= db
->db_last_dirty
;
1836 dr
!= NULL
&& dr
->dr_txg
>= tx
->tx_txg
; dr
= dr
->dr_next
) {
1838 * It's possible that it is already dirty but not cached,
1839 * because there are some calls to dbuf_dirty() that don't
1840 * go through dmu_buf_will_dirty().
1842 if (dr
->dr_txg
== tx
->tx_txg
&& db
->db_state
== DB_CACHED
) {
1843 /* This dbuf is already dirty and cached. */
1845 mutex_exit(&db
->db_mtx
);
1849 mutex_exit(&db
->db_mtx
);
1852 if (RW_WRITE_HELD(&DB_DNODE(db
)->dn_struct_rwlock
))
1853 rf
|= DB_RF_HAVESTRUCT
;
1855 (void) dbuf_read(db
, NULL
, rf
);
1856 (void) dbuf_dirty(db
, tx
);
1860 dmu_buf_will_not_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1862 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1864 db
->db_state
= DB_NOFILL
;
1866 dmu_buf_will_fill(db_fake
, tx
);
1870 dmu_buf_will_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1872 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1874 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1875 ASSERT(tx
->tx_txg
!= 0);
1876 ASSERT(db
->db_level
== 0);
1877 ASSERT(!refcount_is_zero(&db
->db_holds
));
1879 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
||
1880 dmu_tx_private_ok(tx
));
1883 (void) dbuf_dirty(db
, tx
);
1886 #pragma weak dmu_buf_fill_done = dbuf_fill_done
1889 dbuf_fill_done(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1891 mutex_enter(&db
->db_mtx
);
1894 if (db
->db_state
== DB_FILL
) {
1895 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
1896 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1897 /* we were freed while filling */
1898 /* XXX dbuf_undirty? */
1899 bzero(db
->db
.db_data
, db
->db
.db_size
);
1900 db
->db_freed_in_flight
= FALSE
;
1902 db
->db_state
= DB_CACHED
;
1903 cv_broadcast(&db
->db_changed
);
1905 mutex_exit(&db
->db_mtx
);
1909 dmu_buf_write_embedded(dmu_buf_t
*dbuf
, void *data
,
1910 bp_embedded_type_t etype
, enum zio_compress comp
,
1911 int uncompressed_size
, int compressed_size
, int byteorder
,
1914 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbuf
;
1915 struct dirty_leaf
*dl
;
1916 dmu_object_type_t type
;
1918 if (etype
== BP_EMBEDDED_TYPE_DATA
) {
1919 ASSERT(spa_feature_is_active(dmu_objset_spa(db
->db_objset
),
1920 SPA_FEATURE_EMBEDDED_DATA
));
1924 type
= DB_DNODE(db
)->dn_type
;
1927 ASSERT0(db
->db_level
);
1928 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1930 dmu_buf_will_not_fill(dbuf
, tx
);
1932 ASSERT3U(db
->db_last_dirty
->dr_txg
, ==, tx
->tx_txg
);
1933 dl
= &db
->db_last_dirty
->dt
.dl
;
1934 encode_embedded_bp_compressed(&dl
->dr_overridden_by
,
1935 data
, comp
, uncompressed_size
, compressed_size
);
1936 BPE_SET_ETYPE(&dl
->dr_overridden_by
, etype
);
1937 BP_SET_TYPE(&dl
->dr_overridden_by
, type
);
1938 BP_SET_LEVEL(&dl
->dr_overridden_by
, 0);
1939 BP_SET_BYTEORDER(&dl
->dr_overridden_by
, byteorder
);
1941 dl
->dr_override_state
= DR_OVERRIDDEN
;
1942 dl
->dr_overridden_by
.blk_birth
= db
->db_last_dirty
->dr_txg
;
1946 * Directly assign a provided arc buf to a given dbuf if it's not referenced
1947 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
1950 dbuf_assign_arcbuf(dmu_buf_impl_t
*db
, arc_buf_t
*buf
, dmu_tx_t
*tx
)
1952 ASSERT(!refcount_is_zero(&db
->db_holds
));
1953 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1954 ASSERT(db
->db_level
== 0);
1955 ASSERT3U(dbuf_is_metadata(db
), ==, arc_is_metadata(buf
));
1956 ASSERT(buf
!= NULL
);
1957 ASSERT(arc_buf_lsize(buf
) == db
->db
.db_size
);
1958 ASSERT(tx
->tx_txg
!= 0);
1960 arc_return_buf(buf
, db
);
1961 ASSERT(arc_released(buf
));
1963 mutex_enter(&db
->db_mtx
);
1965 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
1966 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1968 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_UNCACHED
);
1970 if (db
->db_state
== DB_CACHED
&&
1971 refcount_count(&db
->db_holds
) - 1 > db
->db_dirtycnt
) {
1972 mutex_exit(&db
->db_mtx
);
1973 (void) dbuf_dirty(db
, tx
);
1974 bcopy(buf
->b_data
, db
->db
.db_data
, db
->db
.db_size
);
1975 arc_buf_destroy(buf
, db
);
1976 xuio_stat_wbuf_copied();
1980 xuio_stat_wbuf_nocopy();
1981 if (db
->db_state
== DB_CACHED
) {
1982 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
1984 ASSERT(db
->db_buf
!= NULL
);
1985 if (dr
!= NULL
&& dr
->dr_txg
== tx
->tx_txg
) {
1986 ASSERT(dr
->dt
.dl
.dr_data
== db
->db_buf
);
1987 if (!arc_released(db
->db_buf
)) {
1988 ASSERT(dr
->dt
.dl
.dr_override_state
==
1990 arc_release(db
->db_buf
, db
);
1992 dr
->dt
.dl
.dr_data
= buf
;
1993 arc_buf_destroy(db
->db_buf
, db
);
1994 } else if (dr
== NULL
|| dr
->dt
.dl
.dr_data
!= db
->db_buf
) {
1995 arc_release(db
->db_buf
, db
);
1996 arc_buf_destroy(db
->db_buf
, db
);
2000 ASSERT(db
->db_buf
== NULL
);
2001 dbuf_set_data(db
, buf
);
2002 db
->db_state
= DB_FILL
;
2003 mutex_exit(&db
->db_mtx
);
2004 (void) dbuf_dirty(db
, tx
);
2005 dmu_buf_fill_done(&db
->db
, tx
);
2009 dbuf_destroy(dmu_buf_impl_t
*db
)
2012 dmu_buf_impl_t
*parent
= db
->db_parent
;
2013 dmu_buf_impl_t
*dndb
;
2015 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2016 ASSERT(refcount_is_zero(&db
->db_holds
));
2018 if (db
->db_buf
!= NULL
) {
2019 arc_buf_destroy(db
->db_buf
, db
);
2023 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2024 ASSERT(db
->db
.db_data
!= NULL
);
2025 zio_buf_free(db
->db
.db_data
, DN_MAX_BONUSLEN
);
2026 arc_space_return(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
2027 db
->db_state
= DB_UNCACHED
;
2030 dbuf_clear_data(db
);
2032 if (multilist_link_active(&db
->db_cache_link
)) {
2033 multilist_remove(dbuf_cache
, db
);
2034 (void) refcount_remove_many(&dbuf_cache_size
,
2035 db
->db
.db_size
, db
);
2038 ASSERT(db
->db_state
== DB_UNCACHED
|| db
->db_state
== DB_NOFILL
);
2039 ASSERT(db
->db_data_pending
== NULL
);
2041 db
->db_state
= DB_EVICTING
;
2042 db
->db_blkptr
= NULL
;
2045 * Now that db_state is DB_EVICTING, nobody else can find this via
2046 * the hash table. We can now drop db_mtx, which allows us to
2047 * acquire the dn_dbufs_mtx.
2049 mutex_exit(&db
->db_mtx
);
2054 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
2055 boolean_t needlock
= !MUTEX_HELD(&dn
->dn_dbufs_mtx
);
2057 mutex_enter(&dn
->dn_dbufs_mtx
);
2058 avl_remove(&dn
->dn_dbufs
, db
);
2059 atomic_dec_32(&dn
->dn_dbufs_count
);
2063 mutex_exit(&dn
->dn_dbufs_mtx
);
2065 * Decrementing the dbuf count means that the hold corresponding
2066 * to the removed dbuf is no longer discounted in dnode_move(),
2067 * so the dnode cannot be moved until after we release the hold.
2068 * The membar_producer() ensures visibility of the decremented
2069 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
2073 db
->db_dnode_handle
= NULL
;
2075 dbuf_hash_remove(db
);
2080 ASSERT(refcount_is_zero(&db
->db_holds
));
2082 db
->db_parent
= NULL
;
2084 ASSERT(db
->db_buf
== NULL
);
2085 ASSERT(db
->db
.db_data
== NULL
);
2086 ASSERT(db
->db_hash_next
== NULL
);
2087 ASSERT(db
->db_blkptr
== NULL
);
2088 ASSERT(db
->db_data_pending
== NULL
);
2089 ASSERT(!multilist_link_active(&db
->db_cache_link
));
2091 kmem_cache_free(dbuf_kmem_cache
, db
);
2092 arc_space_return(sizeof (dmu_buf_impl_t
), ARC_SPACE_OTHER
);
2095 * If this dbuf is referenced from an indirect dbuf,
2096 * decrement the ref count on the indirect dbuf.
2098 if (parent
&& parent
!= dndb
)
2099 dbuf_rele(parent
, db
);
2103 * Note: While bpp will always be updated if the function returns success,
2104 * parentp will not be updated if the dnode does not have dn_dbuf filled in;
2105 * this happens when the dnode is the meta-dnode, or a userused or groupused
2109 dbuf_findbp(dnode_t
*dn
, int level
, uint64_t blkid
, int fail_sparse
,
2110 dmu_buf_impl_t
**parentp
, blkptr_t
**bpp
)
2115 ASSERT(blkid
!= DMU_BONUS_BLKID
);
2117 if (blkid
== DMU_SPILL_BLKID
) {
2118 mutex_enter(&dn
->dn_mtx
);
2119 if (dn
->dn_have_spill
&&
2120 (dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
))
2121 *bpp
= &dn
->dn_phys
->dn_spill
;
2124 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
2125 *parentp
= dn
->dn_dbuf
;
2126 mutex_exit(&dn
->dn_mtx
);
2131 (dn
->dn_phys
->dn_nlevels
== 0) ? 1 : dn
->dn_phys
->dn_nlevels
;
2132 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2134 ASSERT3U(level
* epbs
, <, 64);
2135 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2137 * This assertion shouldn't trip as long as the max indirect block size
2138 * is less than 1M. The reason for this is that up to that point,
2139 * the number of levels required to address an entire object with blocks
2140 * of size SPA_MINBLOCKSIZE satisfies nlevels * epbs + 1 <= 64. In
2141 * other words, if N * epbs + 1 > 64, then if (N-1) * epbs + 1 > 55
2142 * (i.e. we can address the entire object), objects will all use at most
2143 * N-1 levels and the assertion won't overflow. However, once epbs is
2144 * 13, 4 * 13 + 1 = 53, but 5 * 13 + 1 = 66. Then, 4 levels will not be
2145 * enough to address an entire object, so objects will have 5 levels,
2146 * but then this assertion will overflow.
2148 * All this is to say that if we ever increase DN_MAX_INDBLKSHIFT, we
2149 * need to redo this logic to handle overflows.
2151 ASSERT(level
>= nlevels
||
2152 ((nlevels
- level
- 1) * epbs
) +
2153 highbit64(dn
->dn_phys
->dn_nblkptr
) <= 64);
2154 if (level
>= nlevels
||
2155 blkid
>= ((uint64_t)dn
->dn_phys
->dn_nblkptr
<<
2156 ((nlevels
- level
- 1) * epbs
)) ||
2158 blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))) {
2159 /* the buffer has no parent yet */
2160 return (SET_ERROR(ENOENT
));
2161 } else if (level
< nlevels
-1) {
2162 /* this block is referenced from an indirect block */
2163 int err
= dbuf_hold_impl(dn
, level
+1,
2164 blkid
>> epbs
, fail_sparse
, FALSE
, NULL
, parentp
);
2167 err
= dbuf_read(*parentp
, NULL
,
2168 (DB_RF_HAVESTRUCT
| DB_RF_NOPREFETCH
| DB_RF_CANFAIL
));
2170 dbuf_rele(*parentp
, NULL
);
2174 *bpp
= ((blkptr_t
*)(*parentp
)->db
.db_data
) +
2175 (blkid
& ((1ULL << epbs
) - 1));
2176 if (blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))
2177 ASSERT(BP_IS_HOLE(*bpp
));
2180 /* the block is referenced from the dnode */
2181 ASSERT3U(level
, ==, nlevels
-1);
2182 ASSERT(dn
->dn_phys
->dn_nblkptr
== 0 ||
2183 blkid
< dn
->dn_phys
->dn_nblkptr
);
2185 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
2186 *parentp
= dn
->dn_dbuf
;
2188 *bpp
= &dn
->dn_phys
->dn_blkptr
[blkid
];
2193 static dmu_buf_impl_t
*
2194 dbuf_create(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
2195 dmu_buf_impl_t
*parent
, blkptr_t
*blkptr
)
2197 objset_t
*os
= dn
->dn_objset
;
2198 dmu_buf_impl_t
*db
, *odb
;
2200 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2201 ASSERT(dn
->dn_type
!= DMU_OT_NONE
);
2203 db
= kmem_cache_alloc(dbuf_kmem_cache
, KM_SLEEP
);
2206 db
->db
.db_object
= dn
->dn_object
;
2207 db
->db_level
= level
;
2208 db
->db_blkid
= blkid
;
2209 db
->db_last_dirty
= NULL
;
2210 db
->db_dirtycnt
= 0;
2211 db
->db_dnode_handle
= dn
->dn_handle
;
2212 db
->db_parent
= parent
;
2213 db
->db_blkptr
= blkptr
;
2216 db
->db_user_immediate_evict
= FALSE
;
2217 db
->db_freed_in_flight
= FALSE
;
2218 db
->db_pending_evict
= FALSE
;
2220 if (blkid
== DMU_BONUS_BLKID
) {
2221 ASSERT3P(parent
, ==, dn
->dn_dbuf
);
2222 db
->db
.db_size
= DN_MAX_BONUSLEN
-
2223 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
);
2224 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
2225 db
->db
.db_offset
= DMU_BONUS_BLKID
;
2226 db
->db_state
= DB_UNCACHED
;
2227 /* the bonus dbuf is not placed in the hash table */
2228 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_OTHER
);
2230 } else if (blkid
== DMU_SPILL_BLKID
) {
2231 db
->db
.db_size
= (blkptr
!= NULL
) ?
2232 BP_GET_LSIZE(blkptr
) : SPA_MINBLOCKSIZE
;
2233 db
->db
.db_offset
= 0;
2236 db
->db_level
? 1 << dn
->dn_indblkshift
: dn
->dn_datablksz
;
2237 db
->db
.db_size
= blocksize
;
2238 db
->db
.db_offset
= db
->db_blkid
* blocksize
;
2242 * Hold the dn_dbufs_mtx while we get the new dbuf
2243 * in the hash table *and* added to the dbufs list.
2244 * This prevents a possible deadlock with someone
2245 * trying to look up this dbuf before its added to the
2248 mutex_enter(&dn
->dn_dbufs_mtx
);
2249 db
->db_state
= DB_EVICTING
;
2250 if ((odb
= dbuf_hash_insert(db
)) != NULL
) {
2251 /* someone else inserted it first */
2252 kmem_cache_free(dbuf_kmem_cache
, db
);
2253 mutex_exit(&dn
->dn_dbufs_mtx
);
2256 avl_add(&dn
->dn_dbufs
, db
);
2258 db
->db_state
= DB_UNCACHED
;
2259 mutex_exit(&dn
->dn_dbufs_mtx
);
2260 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_OTHER
);
2262 if (parent
&& parent
!= dn
->dn_dbuf
)
2263 dbuf_add_ref(parent
, db
);
2265 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
2266 refcount_count(&dn
->dn_holds
) > 0);
2267 (void) refcount_add(&dn
->dn_holds
, db
);
2268 atomic_inc_32(&dn
->dn_dbufs_count
);
2270 dprintf_dbuf(db
, "db=%p\n", db
);
2275 typedef struct dbuf_prefetch_arg
{
2276 spa_t
*dpa_spa
; /* The spa to issue the prefetch in. */
2277 zbookmark_phys_t dpa_zb
; /* The target block to prefetch. */
2278 int dpa_epbs
; /* Entries (blkptr_t's) Per Block Shift. */
2279 int dpa_curlevel
; /* The current level that we're reading */
2280 dnode_t
*dpa_dnode
; /* The dnode associated with the prefetch */
2281 zio_priority_t dpa_prio
; /* The priority I/Os should be issued at. */
2282 zio_t
*dpa_zio
; /* The parent zio_t for all prefetches. */
2283 arc_flags_t dpa_aflags
; /* Flags to pass to the final prefetch. */
2284 } dbuf_prefetch_arg_t
;
2287 * Actually issue the prefetch read for the block given.
2290 dbuf_issue_final_prefetch(dbuf_prefetch_arg_t
*dpa
, blkptr_t
*bp
)
2292 if (BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
))
2295 arc_flags_t aflags
=
2296 dpa
->dpa_aflags
| ARC_FLAG_NOWAIT
| ARC_FLAG_PREFETCH
;
2298 ASSERT3U(dpa
->dpa_curlevel
, ==, BP_GET_LEVEL(bp
));
2299 ASSERT3U(dpa
->dpa_curlevel
, ==, dpa
->dpa_zb
.zb_level
);
2300 ASSERT(dpa
->dpa_zio
!= NULL
);
2301 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
, bp
, NULL
, NULL
,
2302 dpa
->dpa_prio
, ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
2303 &aflags
, &dpa
->dpa_zb
);
2307 * Called when an indirect block above our prefetch target is read in. This
2308 * will either read in the next indirect block down the tree or issue the actual
2309 * prefetch if the next block down is our target.
2312 dbuf_prefetch_indirect_done(zio_t
*zio
, arc_buf_t
*abuf
, void *private)
2314 dbuf_prefetch_arg_t
*dpa
= private;
2316 ASSERT3S(dpa
->dpa_zb
.zb_level
, <, dpa
->dpa_curlevel
);
2317 ASSERT3S(dpa
->dpa_curlevel
, >, 0);
2320 * The dpa_dnode is only valid if we are called with a NULL
2321 * zio. This indicates that the arc_read() returned without
2322 * first calling zio_read() to issue a physical read. Once
2323 * a physical read is made the dpa_dnode must be invalidated
2324 * as the locks guarding it may have been dropped. If the
2325 * dpa_dnode is still valid, then we want to add it to the dbuf
2326 * cache. To do so, we must hold the dbuf associated with the block
2327 * we just prefetched, read its contents so that we associate it
2328 * with an arc_buf_t, and then release it.
2331 ASSERT3S(BP_GET_LEVEL(zio
->io_bp
), ==, dpa
->dpa_curlevel
);
2332 if (zio
->io_flags
& ZIO_FLAG_RAW
) {
2333 ASSERT3U(BP_GET_PSIZE(zio
->io_bp
), ==, zio
->io_size
);
2335 ASSERT3U(BP_GET_LSIZE(zio
->io_bp
), ==, zio
->io_size
);
2337 ASSERT3P(zio
->io_spa
, ==, dpa
->dpa_spa
);
2339 dpa
->dpa_dnode
= NULL
;
2340 } else if (dpa
->dpa_dnode
!= NULL
) {
2341 uint64_t curblkid
= dpa
->dpa_zb
.zb_blkid
>>
2342 (dpa
->dpa_epbs
* (dpa
->dpa_curlevel
-
2343 dpa
->dpa_zb
.zb_level
));
2344 dmu_buf_impl_t
*db
= dbuf_hold_level(dpa
->dpa_dnode
,
2345 dpa
->dpa_curlevel
, curblkid
, FTAG
);
2346 (void) dbuf_read(db
, NULL
,
2347 DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
| DB_RF_HAVESTRUCT
);
2348 dbuf_rele(db
, FTAG
);
2351 dpa
->dpa_curlevel
--;
2353 uint64_t nextblkid
= dpa
->dpa_zb
.zb_blkid
>>
2354 (dpa
->dpa_epbs
* (dpa
->dpa_curlevel
- dpa
->dpa_zb
.zb_level
));
2355 blkptr_t
*bp
= ((blkptr_t
*)abuf
->b_data
) +
2356 P2PHASE(nextblkid
, 1ULL << dpa
->dpa_epbs
);
2357 if (BP_IS_HOLE(bp
) || (zio
!= NULL
&& zio
->io_error
!= 0)) {
2358 kmem_free(dpa
, sizeof (*dpa
));
2359 } else if (dpa
->dpa_curlevel
== dpa
->dpa_zb
.zb_level
) {
2360 ASSERT3U(nextblkid
, ==, dpa
->dpa_zb
.zb_blkid
);
2361 dbuf_issue_final_prefetch(dpa
, bp
);
2362 kmem_free(dpa
, sizeof (*dpa
));
2364 arc_flags_t iter_aflags
= ARC_FLAG_NOWAIT
;
2365 zbookmark_phys_t zb
;
2367 ASSERT3U(dpa
->dpa_curlevel
, ==, BP_GET_LEVEL(bp
));
2369 SET_BOOKMARK(&zb
, dpa
->dpa_zb
.zb_objset
,
2370 dpa
->dpa_zb
.zb_object
, dpa
->dpa_curlevel
, nextblkid
);
2372 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
,
2373 bp
, dbuf_prefetch_indirect_done
, dpa
, dpa
->dpa_prio
,
2374 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
2378 arc_buf_destroy(abuf
, private);
2382 * Issue prefetch reads for the given block on the given level. If the indirect
2383 * blocks above that block are not in memory, we will read them in
2384 * asynchronously. As a result, this call never blocks waiting for a read to
2388 dbuf_prefetch(dnode_t
*dn
, int64_t level
, uint64_t blkid
, zio_priority_t prio
,
2392 int epbs
, nlevels
, curlevel
;
2395 ASSERT(blkid
!= DMU_BONUS_BLKID
);
2396 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2398 if (blkid
> dn
->dn_maxblkid
)
2401 if (dnode_block_freed(dn
, blkid
))
2405 * This dnode hasn't been written to disk yet, so there's nothing to
2408 nlevels
= dn
->dn_phys
->dn_nlevels
;
2409 if (level
>= nlevels
|| dn
->dn_phys
->dn_nblkptr
== 0)
2412 epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2413 if (dn
->dn_phys
->dn_maxblkid
< blkid
<< (epbs
* level
))
2416 dmu_buf_impl_t
*db
= dbuf_find(dn
->dn_objset
, dn
->dn_object
,
2419 mutex_exit(&db
->db_mtx
);
2421 * This dbuf already exists. It is either CACHED, or
2422 * (we assume) about to be read or filled.
2428 * Find the closest ancestor (indirect block) of the target block
2429 * that is present in the cache. In this indirect block, we will
2430 * find the bp that is at curlevel, curblkid.
2434 while (curlevel
< nlevels
- 1) {
2435 int parent_level
= curlevel
+ 1;
2436 uint64_t parent_blkid
= curblkid
>> epbs
;
2439 if (dbuf_hold_impl(dn
, parent_level
, parent_blkid
,
2440 FALSE
, TRUE
, FTAG
, &db
) == 0) {
2441 blkptr_t
*bpp
= db
->db_buf
->b_data
;
2442 bp
= bpp
[P2PHASE(curblkid
, 1 << epbs
)];
2443 dbuf_rele(db
, FTAG
);
2447 curlevel
= parent_level
;
2448 curblkid
= parent_blkid
;
2451 if (curlevel
== nlevels
- 1) {
2452 /* No cached indirect blocks found. */
2453 ASSERT3U(curblkid
, <, dn
->dn_phys
->dn_nblkptr
);
2454 bp
= dn
->dn_phys
->dn_blkptr
[curblkid
];
2456 if (BP_IS_HOLE(&bp
))
2459 ASSERT3U(curlevel
, ==, BP_GET_LEVEL(&bp
));
2461 zio_t
*pio
= zio_root(dmu_objset_spa(dn
->dn_objset
), NULL
, NULL
,
2464 dbuf_prefetch_arg_t
*dpa
= kmem_zalloc(sizeof (*dpa
), KM_SLEEP
);
2465 dsl_dataset_t
*ds
= dn
->dn_objset
->os_dsl_dataset
;
2466 SET_BOOKMARK(&dpa
->dpa_zb
, ds
!= NULL
? ds
->ds_object
: DMU_META_OBJSET
,
2467 dn
->dn_object
, level
, blkid
);
2468 dpa
->dpa_curlevel
= curlevel
;
2469 dpa
->dpa_prio
= prio
;
2470 dpa
->dpa_aflags
= aflags
;
2471 dpa
->dpa_spa
= dn
->dn_objset
->os_spa
;
2472 dpa
->dpa_dnode
= dn
;
2473 dpa
->dpa_epbs
= epbs
;
2477 * If we have the indirect just above us, no need to do the asynchronous
2478 * prefetch chain; we'll just run the last step ourselves. If we're at
2479 * a higher level, though, we want to issue the prefetches for all the
2480 * indirect blocks asynchronously, so we can go on with whatever we were
2483 if (curlevel
== level
) {
2484 ASSERT3U(curblkid
, ==, blkid
);
2485 dbuf_issue_final_prefetch(dpa
, &bp
);
2486 kmem_free(dpa
, sizeof (*dpa
));
2488 arc_flags_t iter_aflags
= ARC_FLAG_NOWAIT
;
2489 zbookmark_phys_t zb
;
2491 SET_BOOKMARK(&zb
, ds
!= NULL
? ds
->ds_object
: DMU_META_OBJSET
,
2492 dn
->dn_object
, curlevel
, curblkid
);
2493 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
,
2494 &bp
, dbuf_prefetch_indirect_done
, dpa
, prio
,
2495 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
2499 * We use pio here instead of dpa_zio since it's possible that
2500 * dpa may have already been freed.
2506 * Returns with db_holds incremented, and db_mtx not held.
2507 * Note: dn_struct_rwlock must be held.
2510 dbuf_hold_impl(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
2511 boolean_t fail_sparse
, boolean_t fail_uncached
,
2512 void *tag
, dmu_buf_impl_t
**dbp
)
2514 dmu_buf_impl_t
*db
, *parent
= NULL
;
2516 ASSERT(blkid
!= DMU_BONUS_BLKID
);
2517 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2518 ASSERT3U(dn
->dn_nlevels
, >, level
);
2522 /* dbuf_find() returns with db_mtx held */
2523 db
= dbuf_find(dn
->dn_objset
, dn
->dn_object
, level
, blkid
);
2526 blkptr_t
*bp
= NULL
;
2530 return (SET_ERROR(ENOENT
));
2532 ASSERT3P(parent
, ==, NULL
);
2533 err
= dbuf_findbp(dn
, level
, blkid
, fail_sparse
, &parent
, &bp
);
2535 if (err
== 0 && bp
&& BP_IS_HOLE(bp
))
2536 err
= SET_ERROR(ENOENT
);
2539 dbuf_rele(parent
, NULL
);
2543 if (err
&& err
!= ENOENT
)
2545 db
= dbuf_create(dn
, level
, blkid
, parent
, bp
);
2548 if (fail_uncached
&& db
->db_state
!= DB_CACHED
) {
2549 mutex_exit(&db
->db_mtx
);
2550 return (SET_ERROR(ENOENT
));
2553 if (db
->db_buf
!= NULL
)
2554 ASSERT3P(db
->db
.db_data
, ==, db
->db_buf
->b_data
);
2556 ASSERT(db
->db_buf
== NULL
|| arc_referenced(db
->db_buf
));
2559 * If this buffer is currently syncing out, and we are are
2560 * still referencing it from db_data, we need to make a copy
2561 * of it in case we decide we want to dirty it again in this txg.
2563 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
2564 dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
2565 db
->db_state
== DB_CACHED
&& db
->db_data_pending
) {
2566 dbuf_dirty_record_t
*dr
= db
->db_data_pending
;
2568 if (dr
->dt
.dl
.dr_data
== db
->db_buf
) {
2569 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
2572 arc_alloc_buf(dn
->dn_objset
->os_spa
, db
, type
,
2574 bcopy(dr
->dt
.dl
.dr_data
->b_data
, db
->db
.db_data
,
2579 if (multilist_link_active(&db
->db_cache_link
)) {
2580 ASSERT(refcount_is_zero(&db
->db_holds
));
2581 multilist_remove(dbuf_cache
, db
);
2582 (void) refcount_remove_many(&dbuf_cache_size
,
2583 db
->db
.db_size
, db
);
2585 (void) refcount_add(&db
->db_holds
, tag
);
2587 mutex_exit(&db
->db_mtx
);
2589 /* NOTE: we can't rele the parent until after we drop the db_mtx */
2591 dbuf_rele(parent
, NULL
);
2593 ASSERT3P(DB_DNODE(db
), ==, dn
);
2594 ASSERT3U(db
->db_blkid
, ==, blkid
);
2595 ASSERT3U(db
->db_level
, ==, level
);
2602 dbuf_hold(dnode_t
*dn
, uint64_t blkid
, void *tag
)
2604 return (dbuf_hold_level(dn
, 0, blkid
, tag
));
2608 dbuf_hold_level(dnode_t
*dn
, int level
, uint64_t blkid
, void *tag
)
2611 int err
= dbuf_hold_impl(dn
, level
, blkid
, FALSE
, FALSE
, tag
, &db
);
2612 return (err
? NULL
: db
);
2616 dbuf_create_bonus(dnode_t
*dn
)
2618 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
2620 ASSERT(dn
->dn_bonus
== NULL
);
2621 dn
->dn_bonus
= dbuf_create(dn
, 0, DMU_BONUS_BLKID
, dn
->dn_dbuf
, NULL
);
2625 dbuf_spill_set_blksz(dmu_buf_t
*db_fake
, uint64_t blksz
, dmu_tx_t
*tx
)
2627 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2630 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
2631 return (SET_ERROR(ENOTSUP
));
2633 blksz
= SPA_MINBLOCKSIZE
;
2634 ASSERT3U(blksz
, <=, spa_maxblocksize(dmu_objset_spa(db
->db_objset
)));
2635 blksz
= P2ROUNDUP(blksz
, SPA_MINBLOCKSIZE
);
2639 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
2640 dbuf_new_size(db
, blksz
, tx
);
2641 rw_exit(&dn
->dn_struct_rwlock
);
2648 dbuf_rm_spill(dnode_t
*dn
, dmu_tx_t
*tx
)
2650 dbuf_free_range(dn
, DMU_SPILL_BLKID
, DMU_SPILL_BLKID
, tx
);
2653 #pragma weak dmu_buf_add_ref = dbuf_add_ref
2655 dbuf_add_ref(dmu_buf_impl_t
*db
, void *tag
)
2657 int64_t holds
= refcount_add(&db
->db_holds
, tag
);
2658 ASSERT3S(holds
, >, 1);
2661 #pragma weak dmu_buf_try_add_ref = dbuf_try_add_ref
2663 dbuf_try_add_ref(dmu_buf_t
*db_fake
, objset_t
*os
, uint64_t obj
, uint64_t blkid
,
2666 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2667 dmu_buf_impl_t
*found_db
;
2668 boolean_t result
= B_FALSE
;
2670 if (db
->db_blkid
== DMU_BONUS_BLKID
)
2671 found_db
= dbuf_find_bonus(os
, obj
);
2673 found_db
= dbuf_find(os
, obj
, 0, blkid
);
2675 if (found_db
!= NULL
) {
2676 if (db
== found_db
&& dbuf_refcount(db
) > db
->db_dirtycnt
) {
2677 (void) refcount_add(&db
->db_holds
, tag
);
2680 mutex_exit(&db
->db_mtx
);
2686 * If you call dbuf_rele() you had better not be referencing the dnode handle
2687 * unless you have some other direct or indirect hold on the dnode. (An indirect
2688 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
2689 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
2690 * dnode's parent dbuf evicting its dnode handles.
2693 dbuf_rele(dmu_buf_impl_t
*db
, void *tag
)
2695 mutex_enter(&db
->db_mtx
);
2696 dbuf_rele_and_unlock(db
, tag
);
2700 dmu_buf_rele(dmu_buf_t
*db
, void *tag
)
2702 dbuf_rele((dmu_buf_impl_t
*)db
, tag
);
2706 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
2707 * db_dirtycnt and db_holds to be updated atomically.
2710 dbuf_rele_and_unlock(dmu_buf_impl_t
*db
, void *tag
)
2714 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2718 * Remove the reference to the dbuf before removing its hold on the
2719 * dnode so we can guarantee in dnode_move() that a referenced bonus
2720 * buffer has a corresponding dnode hold.
2722 holds
= refcount_remove(&db
->db_holds
, tag
);
2726 * We can't freeze indirects if there is a possibility that they
2727 * may be modified in the current syncing context.
2729 if (db
->db_buf
!= NULL
&&
2730 holds
== (db
->db_level
== 0 ? db
->db_dirtycnt
: 0)) {
2731 arc_buf_freeze(db
->db_buf
);
2734 if (holds
== db
->db_dirtycnt
&&
2735 db
->db_level
== 0 && db
->db_user_immediate_evict
)
2736 dbuf_evict_user(db
);
2739 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2741 boolean_t evict_dbuf
= db
->db_pending_evict
;
2744 * If the dnode moves here, we cannot cross this
2745 * barrier until the move completes.
2750 atomic_dec_32(&dn
->dn_dbufs_count
);
2753 * Decrementing the dbuf count means that the bonus
2754 * buffer's dnode hold is no longer discounted in
2755 * dnode_move(). The dnode cannot move until after
2756 * the dnode_rele() below.
2761 * Do not reference db after its lock is dropped.
2762 * Another thread may evict it.
2764 mutex_exit(&db
->db_mtx
);
2767 dnode_evict_bonus(dn
);
2770 } else if (db
->db_buf
== NULL
) {
2772 * This is a special case: we never associated this
2773 * dbuf with any data allocated from the ARC.
2775 ASSERT(db
->db_state
== DB_UNCACHED
||
2776 db
->db_state
== DB_NOFILL
);
2778 } else if (arc_released(db
->db_buf
)) {
2780 * This dbuf has anonymous data associated with it.
2784 boolean_t do_arc_evict
= B_FALSE
;
2786 spa_t
*spa
= dmu_objset_spa(db
->db_objset
);
2788 if (!DBUF_IS_CACHEABLE(db
) &&
2789 db
->db_blkptr
!= NULL
&&
2790 !BP_IS_HOLE(db
->db_blkptr
) &&
2791 !BP_IS_EMBEDDED(db
->db_blkptr
)) {
2792 do_arc_evict
= B_TRUE
;
2793 bp
= *db
->db_blkptr
;
2796 if (!DBUF_IS_CACHEABLE(db
) ||
2797 db
->db_pending_evict
) {
2799 } else if (!multilist_link_active(&db
->db_cache_link
)) {
2800 multilist_insert(dbuf_cache
, db
);
2801 (void) refcount_add_many(&dbuf_cache_size
,
2802 db
->db
.db_size
, db
);
2803 mutex_exit(&db
->db_mtx
);
2805 dbuf_evict_notify();
2809 arc_freed(spa
, &bp
);
2812 mutex_exit(&db
->db_mtx
);
2817 #pragma weak dmu_buf_refcount = dbuf_refcount
2819 dbuf_refcount(dmu_buf_impl_t
*db
)
2821 return (refcount_count(&db
->db_holds
));
2825 dmu_buf_replace_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*old_user
,
2826 dmu_buf_user_t
*new_user
)
2828 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2830 mutex_enter(&db
->db_mtx
);
2831 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
2832 if (db
->db_user
== old_user
)
2833 db
->db_user
= new_user
;
2835 old_user
= db
->db_user
;
2836 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
2837 mutex_exit(&db
->db_mtx
);
2843 dmu_buf_set_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
2845 return (dmu_buf_replace_user(db_fake
, NULL
, user
));
2849 dmu_buf_set_user_ie(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
2851 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2853 db
->db_user_immediate_evict
= TRUE
;
2854 return (dmu_buf_set_user(db_fake
, user
));
2858 dmu_buf_remove_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
2860 return (dmu_buf_replace_user(db_fake
, user
, NULL
));
2864 dmu_buf_get_user(dmu_buf_t
*db_fake
)
2866 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2868 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
2869 return (db
->db_user
);
2873 dmu_buf_user_evict_wait()
2875 taskq_wait(dbu_evict_taskq
);
2879 dmu_buf_get_blkptr(dmu_buf_t
*db
)
2881 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
2882 return (dbi
->db_blkptr
);
2886 dmu_buf_get_objset(dmu_buf_t
*db
)
2888 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
2889 return (dbi
->db_objset
);
2893 dmu_buf_dnode_enter(dmu_buf_t
*db
)
2895 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
2896 DB_DNODE_ENTER(dbi
);
2897 return (DB_DNODE(dbi
));
2901 dmu_buf_dnode_exit(dmu_buf_t
*db
)
2903 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
2908 dbuf_check_blkptr(dnode_t
*dn
, dmu_buf_impl_t
*db
)
2910 /* ASSERT(dmu_tx_is_syncing(tx) */
2911 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2913 if (db
->db_blkptr
!= NULL
)
2916 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
2917 db
->db_blkptr
= &dn
->dn_phys
->dn_spill
;
2918 BP_ZERO(db
->db_blkptr
);
2921 if (db
->db_level
== dn
->dn_phys
->dn_nlevels
-1) {
2923 * This buffer was allocated at a time when there was
2924 * no available blkptrs from the dnode, or it was
2925 * inappropriate to hook it in (i.e., nlevels mis-match).
2927 ASSERT(db
->db_blkid
< dn
->dn_phys
->dn_nblkptr
);
2928 ASSERT(db
->db_parent
== NULL
);
2929 db
->db_parent
= dn
->dn_dbuf
;
2930 db
->db_blkptr
= &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
];
2933 dmu_buf_impl_t
*parent
= db
->db_parent
;
2934 int epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2936 ASSERT(dn
->dn_phys
->dn_nlevels
> 1);
2937 if (parent
== NULL
) {
2938 mutex_exit(&db
->db_mtx
);
2939 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
2940 parent
= dbuf_hold_level(dn
, db
->db_level
+ 1,
2941 db
->db_blkid
>> epbs
, db
);
2942 rw_exit(&dn
->dn_struct_rwlock
);
2943 mutex_enter(&db
->db_mtx
);
2944 db
->db_parent
= parent
;
2946 db
->db_blkptr
= (blkptr_t
*)parent
->db
.db_data
+
2947 (db
->db_blkid
& ((1ULL << epbs
) - 1));
2953 dbuf_sync_indirect(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
2955 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2959 ASSERT(dmu_tx_is_syncing(tx
));
2961 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
2963 mutex_enter(&db
->db_mtx
);
2965 ASSERT(db
->db_level
> 0);
2968 /* Read the block if it hasn't been read yet. */
2969 if (db
->db_buf
== NULL
) {
2970 mutex_exit(&db
->db_mtx
);
2971 (void) dbuf_read(db
, NULL
, DB_RF_MUST_SUCCEED
);
2972 mutex_enter(&db
->db_mtx
);
2974 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
2975 ASSERT(db
->db_buf
!= NULL
);
2979 /* Indirect block size must match what the dnode thinks it is. */
2980 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
2981 dbuf_check_blkptr(dn
, db
);
2984 /* Provide the pending dirty record to child dbufs */
2985 db
->db_data_pending
= dr
;
2987 mutex_exit(&db
->db_mtx
);
2988 dbuf_write(dr
, db
->db_buf
, tx
);
2991 mutex_enter(&dr
->dt
.di
.dr_mtx
);
2992 dbuf_sync_list(&dr
->dt
.di
.dr_children
, db
->db_level
- 1, tx
);
2993 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
2994 mutex_exit(&dr
->dt
.di
.dr_mtx
);
2999 dbuf_sync_leaf(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
3001 arc_buf_t
**datap
= &dr
->dt
.dl
.dr_data
;
3002 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3005 uint64_t txg
= tx
->tx_txg
;
3007 ASSERT(dmu_tx_is_syncing(tx
));
3009 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
3011 mutex_enter(&db
->db_mtx
);
3013 * To be synced, we must be dirtied. But we
3014 * might have been freed after the dirty.
3016 if (db
->db_state
== DB_UNCACHED
) {
3017 /* This buffer has been freed since it was dirtied */
3018 ASSERT(db
->db
.db_data
== NULL
);
3019 } else if (db
->db_state
== DB_FILL
) {
3020 /* This buffer was freed and is now being re-filled */
3021 ASSERT(db
->db
.db_data
!= dr
->dt
.dl
.dr_data
);
3023 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_NOFILL
);
3030 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3031 mutex_enter(&dn
->dn_mtx
);
3032 dn
->dn_phys
->dn_flags
|= DNODE_FLAG_SPILL_BLKPTR
;
3033 mutex_exit(&dn
->dn_mtx
);
3037 * If this is a bonus buffer, simply copy the bonus data into the
3038 * dnode. It will be written out when the dnode is synced (and it
3039 * will be synced, since it must have been dirty for dbuf_sync to
3042 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
3043 dbuf_dirty_record_t
**drp
;
3045 ASSERT(*datap
!= NULL
);
3046 ASSERT0(db
->db_level
);
3047 ASSERT3U(dn
->dn_phys
->dn_bonuslen
, <=, DN_MAX_BONUSLEN
);
3048 bcopy(*datap
, DN_BONUS(dn
->dn_phys
), dn
->dn_phys
->dn_bonuslen
);
3051 if (*datap
!= db
->db
.db_data
) {
3052 zio_buf_free(*datap
, DN_MAX_BONUSLEN
);
3053 arc_space_return(DN_MAX_BONUSLEN
, ARC_SPACE_OTHER
);
3055 db
->db_data_pending
= NULL
;
3056 drp
= &db
->db_last_dirty
;
3058 drp
= &(*drp
)->dr_next
;
3059 ASSERT(dr
->dr_next
== NULL
);
3060 ASSERT(dr
->dr_dbuf
== db
);
3062 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
3063 ASSERT(db
->db_dirtycnt
> 0);
3064 db
->db_dirtycnt
-= 1;
3065 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)txg
);
3072 * This function may have dropped the db_mtx lock allowing a dmu_sync
3073 * operation to sneak in. As a result, we need to ensure that we
3074 * don't check the dr_override_state until we have returned from
3075 * dbuf_check_blkptr.
3077 dbuf_check_blkptr(dn
, db
);
3080 * If this buffer is in the middle of an immediate write,
3081 * wait for the synchronous IO to complete.
3083 while (dr
->dt
.dl
.dr_override_state
== DR_IN_DMU_SYNC
) {
3084 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
3085 cv_wait(&db
->db_changed
, &db
->db_mtx
);
3086 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_NOT_OVERRIDDEN
);
3089 if (db
->db_state
!= DB_NOFILL
&&
3090 dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
3091 refcount_count(&db
->db_holds
) > 1 &&
3092 dr
->dt
.dl
.dr_override_state
!= DR_OVERRIDDEN
&&
3093 *datap
== db
->db_buf
) {
3095 * If this buffer is currently "in use" (i.e., there
3096 * are active holds and db_data still references it),
3097 * then make a copy before we start the write so that
3098 * any modifications from the open txg will not leak
3101 * NOTE: this copy does not need to be made for
3102 * objects only modified in the syncing context (e.g.
3103 * DNONE_DNODE blocks).
3105 int psize
= arc_buf_size(*datap
);
3106 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
3107 enum zio_compress compress_type
= arc_get_compression(*datap
);
3109 if (compress_type
== ZIO_COMPRESS_OFF
) {
3110 *datap
= arc_alloc_buf(os
->os_spa
, db
, type
, psize
);
3112 ASSERT3U(type
, ==, ARC_BUFC_DATA
);
3113 int lsize
= arc_buf_lsize(*datap
);
3114 *datap
= arc_alloc_compressed_buf(os
->os_spa
, db
,
3115 psize
, lsize
, compress_type
);
3117 bcopy(db
->db
.db_data
, (*datap
)->b_data
, psize
);
3119 db
->db_data_pending
= dr
;
3121 mutex_exit(&db
->db_mtx
);
3123 dbuf_write(dr
, *datap
, tx
);
3125 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
3126 if (dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
3127 list_insert_tail(&dn
->dn_dirty_records
[txg
&TXG_MASK
], dr
);
3131 * Although zio_nowait() does not "wait for an IO", it does
3132 * initiate the IO. If this is an empty write it seems plausible
3133 * that the IO could actually be completed before the nowait
3134 * returns. We need to DB_DNODE_EXIT() first in case
3135 * zio_nowait() invalidates the dbuf.
3138 zio_nowait(dr
->dr_zio
);
3143 dbuf_sync_list(list_t
*list
, int level
, dmu_tx_t
*tx
)
3145 dbuf_dirty_record_t
*dr
;
3147 while (dr
= list_head(list
)) {
3148 if (dr
->dr_zio
!= NULL
) {
3150 * If we find an already initialized zio then we
3151 * are processing the meta-dnode, and we have finished.
3152 * The dbufs for all dnodes are put back on the list
3153 * during processing, so that we can zio_wait()
3154 * these IOs after initiating all child IOs.
3156 ASSERT3U(dr
->dr_dbuf
->db
.db_object
, ==,
3157 DMU_META_DNODE_OBJECT
);
3160 if (dr
->dr_dbuf
->db_blkid
!= DMU_BONUS_BLKID
&&
3161 dr
->dr_dbuf
->db_blkid
!= DMU_SPILL_BLKID
) {
3162 VERIFY3U(dr
->dr_dbuf
->db_level
, ==, level
);
3164 list_remove(list
, dr
);
3165 if (dr
->dr_dbuf
->db_level
> 0)
3166 dbuf_sync_indirect(dr
, tx
);
3168 dbuf_sync_leaf(dr
, tx
);
3174 dbuf_write_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
3176 dmu_buf_impl_t
*db
= vdb
;
3178 blkptr_t
*bp
= zio
->io_bp
;
3179 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
3180 spa_t
*spa
= zio
->io_spa
;
3185 ASSERT3P(db
->db_blkptr
, !=, NULL
);
3186 ASSERT3P(&db
->db_data_pending
->dr_bp_copy
, ==, bp
);
3190 delta
= bp_get_dsize_sync(spa
, bp
) - bp_get_dsize_sync(spa
, bp_orig
);
3191 dnode_diduse_space(dn
, delta
- zio
->io_prev_space_delta
);
3192 zio
->io_prev_space_delta
= delta
;
3194 if (bp
->blk_birth
!= 0) {
3195 ASSERT((db
->db_blkid
!= DMU_SPILL_BLKID
&&
3196 BP_GET_TYPE(bp
) == dn
->dn_type
) ||
3197 (db
->db_blkid
== DMU_SPILL_BLKID
&&
3198 BP_GET_TYPE(bp
) == dn
->dn_bonustype
) ||
3199 BP_IS_EMBEDDED(bp
));
3200 ASSERT(BP_GET_LEVEL(bp
) == db
->db_level
);
3203 mutex_enter(&db
->db_mtx
);
3206 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3207 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
3208 ASSERT(!(BP_IS_HOLE(bp
)) &&
3209 db
->db_blkptr
== &dn
->dn_phys
->dn_spill
);
3213 if (db
->db_level
== 0) {
3214 mutex_enter(&dn
->dn_mtx
);
3215 if (db
->db_blkid
> dn
->dn_phys
->dn_maxblkid
&&
3216 db
->db_blkid
!= DMU_SPILL_BLKID
)
3217 dn
->dn_phys
->dn_maxblkid
= db
->db_blkid
;
3218 mutex_exit(&dn
->dn_mtx
);
3220 if (dn
->dn_type
== DMU_OT_DNODE
) {
3221 dnode_phys_t
*dnp
= db
->db
.db_data
;
3222 for (i
= db
->db
.db_size
>> DNODE_SHIFT
; i
> 0;
3224 if (dnp
->dn_type
!= DMU_OT_NONE
)
3228 if (BP_IS_HOLE(bp
)) {
3235 blkptr_t
*ibp
= db
->db
.db_data
;
3236 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
3237 for (i
= db
->db
.db_size
>> SPA_BLKPTRSHIFT
; i
> 0; i
--, ibp
++) {
3238 if (BP_IS_HOLE(ibp
))
3240 fill
+= BP_GET_FILL(ibp
);
3245 if (!BP_IS_EMBEDDED(bp
))
3246 bp
->blk_fill
= fill
;
3248 mutex_exit(&db
->db_mtx
);
3250 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
3251 *db
->db_blkptr
= *bp
;
3252 rw_exit(&dn
->dn_struct_rwlock
);
3257 * This function gets called just prior to running through the compression
3258 * stage of the zio pipeline. If we're an indirect block comprised of only
3259 * holes, then we want this indirect to be compressed away to a hole. In
3260 * order to do that we must zero out any information about the holes that
3261 * this indirect points to prior to before we try to compress it.
3264 dbuf_write_children_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
3266 dmu_buf_impl_t
*db
= vdb
;
3269 unsigned int epbs
, i
;
3271 ASSERT3U(db
->db_level
, >, 0);
3274 epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
3275 ASSERT3U(epbs
, <, 31);
3277 /* Determine if all our children are holes */
3278 for (i
= 0, bp
= db
->db
.db_data
; i
< 1 << epbs
; i
++, bp
++) {
3279 if (!BP_IS_HOLE(bp
))
3284 * If all the children are holes, then zero them all out so that
3285 * we may get compressed away.
3287 if (i
== 1 << epbs
) {
3289 * We only found holes. Grab the rwlock to prevent
3290 * anybody from reading the blocks we're about to
3293 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
3294 bzero(db
->db
.db_data
, db
->db
.db_size
);
3295 rw_exit(&dn
->dn_struct_rwlock
);
3301 * The SPA will call this callback several times for each zio - once
3302 * for every physical child i/o (zio->io_phys_children times). This
3303 * allows the DMU to monitor the progress of each logical i/o. For example,
3304 * there may be 2 copies of an indirect block, or many fragments of a RAID-Z
3305 * block. There may be a long delay before all copies/fragments are completed,
3306 * so this callback allows us to retire dirty space gradually, as the physical
3311 dbuf_write_physdone(zio_t
*zio
, arc_buf_t
*buf
, void *arg
)
3313 dmu_buf_impl_t
*db
= arg
;
3314 objset_t
*os
= db
->db_objset
;
3315 dsl_pool_t
*dp
= dmu_objset_pool(os
);
3316 dbuf_dirty_record_t
*dr
;
3319 dr
= db
->db_data_pending
;
3320 ASSERT3U(dr
->dr_txg
, ==, zio
->io_txg
);
3323 * The callback will be called io_phys_children times. Retire one
3324 * portion of our dirty space each time we are called. Any rounding
3325 * error will be cleaned up by dsl_pool_sync()'s call to
3326 * dsl_pool_undirty_space().
3328 delta
= dr
->dr_accounted
/ zio
->io_phys_children
;
3329 dsl_pool_undirty_space(dp
, delta
, zio
->io_txg
);
3334 dbuf_write_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
3336 dmu_buf_impl_t
*db
= vdb
;
3337 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
3338 blkptr_t
*bp
= db
->db_blkptr
;
3339 objset_t
*os
= db
->db_objset
;
3340 dmu_tx_t
*tx
= os
->os_synctx
;
3341 dbuf_dirty_record_t
**drp
, *dr
;
3343 ASSERT0(zio
->io_error
);
3344 ASSERT(db
->db_blkptr
== bp
);
3347 * For nopwrites and rewrites we ensure that the bp matches our
3348 * original and bypass all the accounting.
3350 if (zio
->io_flags
& (ZIO_FLAG_IO_REWRITE
| ZIO_FLAG_NOPWRITE
)) {
3351 ASSERT(BP_EQUAL(bp
, bp_orig
));
3353 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
3354 (void) dsl_dataset_block_kill(ds
, bp_orig
, tx
, B_TRUE
);
3355 dsl_dataset_block_born(ds
, bp
, tx
);
3358 mutex_enter(&db
->db_mtx
);
3362 drp
= &db
->db_last_dirty
;
3363 while ((dr
= *drp
) != db
->db_data_pending
)
3365 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
3366 ASSERT(dr
->dr_dbuf
== db
);
3367 ASSERT(dr
->dr_next
== NULL
);
3371 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3376 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
3377 ASSERT(!(BP_IS_HOLE(db
->db_blkptr
)) &&
3378 db
->db_blkptr
== &dn
->dn_phys
->dn_spill
);
3383 if (db
->db_level
== 0) {
3384 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
3385 ASSERT(dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
);
3386 if (db
->db_state
!= DB_NOFILL
) {
3387 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
3388 arc_buf_destroy(dr
->dt
.dl
.dr_data
, db
);
3395 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
3396 ASSERT3U(db
->db
.db_size
, ==, 1 << dn
->dn_phys
->dn_indblkshift
);
3397 if (!BP_IS_HOLE(db
->db_blkptr
)) {
3399 dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
3400 ASSERT3U(db
->db_blkid
, <=,
3401 dn
->dn_phys
->dn_maxblkid
>> (db
->db_level
* epbs
));
3402 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==,
3406 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
3407 list_destroy(&dr
->dt
.di
.dr_children
);
3409 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
3411 cv_broadcast(&db
->db_changed
);
3412 ASSERT(db
->db_dirtycnt
> 0);
3413 db
->db_dirtycnt
-= 1;
3414 db
->db_data_pending
= NULL
;
3415 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)tx
->tx_txg
);
3419 dbuf_write_nofill_ready(zio_t
*zio
)
3421 dbuf_write_ready(zio
, NULL
, zio
->io_private
);
3425 dbuf_write_nofill_done(zio_t
*zio
)
3427 dbuf_write_done(zio
, NULL
, zio
->io_private
);
3431 dbuf_write_override_ready(zio_t
*zio
)
3433 dbuf_dirty_record_t
*dr
= zio
->io_private
;
3434 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3436 dbuf_write_ready(zio
, NULL
, db
);
3440 dbuf_write_override_done(zio_t
*zio
)
3442 dbuf_dirty_record_t
*dr
= zio
->io_private
;
3443 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3444 blkptr_t
*obp
= &dr
->dt
.dl
.dr_overridden_by
;
3446 mutex_enter(&db
->db_mtx
);
3447 if (!BP_EQUAL(zio
->io_bp
, obp
)) {
3448 if (!BP_IS_HOLE(obp
))
3449 dsl_free(spa_get_dsl(zio
->io_spa
), zio
->io_txg
, obp
);
3450 arc_release(dr
->dt
.dl
.dr_data
, db
);
3452 mutex_exit(&db
->db_mtx
);
3454 dbuf_write_done(zio
, NULL
, db
);
3457 /* Issue I/O to commit a dirty buffer to disk. */
3459 dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
)
3461 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3464 dmu_buf_impl_t
*parent
= db
->db_parent
;
3465 uint64_t txg
= tx
->tx_txg
;
3466 zbookmark_phys_t zb
;
3471 ASSERT(dmu_tx_is_syncing(tx
));
3477 if (db
->db_state
!= DB_NOFILL
) {
3478 if (db
->db_level
> 0 || dn
->dn_type
== DMU_OT_DNODE
) {
3480 * Private object buffers are released here rather
3481 * than in dbuf_dirty() since they are only modified
3482 * in the syncing context and we don't want the
3483 * overhead of making multiple copies of the data.
3485 if (BP_IS_HOLE(db
->db_blkptr
)) {
3488 dbuf_release_bp(db
);
3493 if (parent
!= dn
->dn_dbuf
) {
3494 /* Our parent is an indirect block. */
3495 /* We have a dirty parent that has been scheduled for write. */
3496 ASSERT(parent
&& parent
->db_data_pending
);
3497 /* Our parent's buffer is one level closer to the dnode. */
3498 ASSERT(db
->db_level
== parent
->db_level
-1);
3500 * We're about to modify our parent's db_data by modifying
3501 * our block pointer, so the parent must be released.
3503 ASSERT(arc_released(parent
->db_buf
));
3504 zio
= parent
->db_data_pending
->dr_zio
;
3506 /* Our parent is the dnode itself. */
3507 ASSERT((db
->db_level
== dn
->dn_phys
->dn_nlevels
-1 &&
3508 db
->db_blkid
!= DMU_SPILL_BLKID
) ||
3509 (db
->db_blkid
== DMU_SPILL_BLKID
&& db
->db_level
== 0));
3510 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
3511 ASSERT3P(db
->db_blkptr
, ==,
3512 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
3516 ASSERT(db
->db_level
== 0 || data
== db
->db_buf
);
3517 ASSERT3U(db
->db_blkptr
->blk_birth
, <=, txg
);
3520 SET_BOOKMARK(&zb
, os
->os_dsl_dataset
?
3521 os
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
3522 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
3524 if (db
->db_blkid
== DMU_SPILL_BLKID
)
3526 wp_flag
|= (db
->db_state
== DB_NOFILL
) ? WP_NOFILL
: 0;
3528 dmu_write_policy(os
, dn
, db
->db_level
, wp_flag
,
3529 (data
!= NULL
&& arc_get_compression(data
) != ZIO_COMPRESS_OFF
) ?
3530 arc_get_compression(data
) : ZIO_COMPRESS_INHERIT
, &zp
);
3534 * We copy the blkptr now (rather than when we instantiate the dirty
3535 * record), because its value can change between open context and
3536 * syncing context. We do not need to hold dn_struct_rwlock to read
3537 * db_blkptr because we are in syncing context.
3539 dr
->dr_bp_copy
= *db
->db_blkptr
;
3541 if (db
->db_level
== 0 &&
3542 dr
->dt
.dl
.dr_override_state
== DR_OVERRIDDEN
) {
3544 * The BP for this block has been provided by open context
3545 * (by dmu_sync() or dmu_buf_write_embedded()).
3547 void *contents
= (data
!= NULL
) ? data
->b_data
: NULL
;
3549 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
, &dr
->dr_bp_copy
,
3550 contents
, db
->db
.db_size
, db
->db
.db_size
, &zp
,
3551 dbuf_write_override_ready
, NULL
, NULL
,
3552 dbuf_write_override_done
,
3553 dr
, ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
3554 mutex_enter(&db
->db_mtx
);
3555 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
3556 zio_write_override(dr
->dr_zio
, &dr
->dt
.dl
.dr_overridden_by
,
3557 dr
->dt
.dl
.dr_copies
, dr
->dt
.dl
.dr_nopwrite
);
3558 mutex_exit(&db
->db_mtx
);
3559 } else if (db
->db_state
== DB_NOFILL
) {
3560 ASSERT(zp
.zp_checksum
== ZIO_CHECKSUM_OFF
||
3561 zp
.zp_checksum
== ZIO_CHECKSUM_NOPARITY
);
3562 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
,
3563 &dr
->dr_bp_copy
, NULL
, db
->db
.db_size
, db
->db
.db_size
, &zp
,
3564 dbuf_write_nofill_ready
, NULL
, NULL
,
3565 dbuf_write_nofill_done
, db
,
3566 ZIO_PRIORITY_ASYNC_WRITE
,
3567 ZIO_FLAG_MUSTSUCCEED
| ZIO_FLAG_NODATA
, &zb
);
3569 ASSERT(arc_released(data
));
3572 * For indirect blocks, we want to setup the children
3573 * ready callback so that we can properly handle an indirect
3574 * block that only contains holes.
3576 arc_done_func_t
*children_ready_cb
= NULL
;
3577 if (db
->db_level
!= 0)
3578 children_ready_cb
= dbuf_write_children_ready
;
3580 dr
->dr_zio
= arc_write(zio
, os
->os_spa
, txg
,
3581 &dr
->dr_bp_copy
, data
, DBUF_IS_L2CACHEABLE(db
),
3582 &zp
, dbuf_write_ready
, children_ready_cb
,
3583 dbuf_write_physdone
, dbuf_write_done
, db
,
3584 ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);