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, 2015 by Delphix. All rights reserved.
25 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
26 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
29 #include <sys/zfs_context.h>
32 #include <sys/dmu_send.h>
33 #include <sys/dmu_impl.h>
35 #include <sys/dmu_objset.h>
36 #include <sys/dsl_dataset.h>
37 #include <sys/dsl_dir.h>
38 #include <sys/dmu_tx.h>
41 #include <sys/dmu_zfetch.h>
43 #include <sys/sa_impl.h>
44 #include <sys/zfeature.h>
45 #include <sys/blkptr.h>
46 #include <sys/range_tree.h>
47 #include <sys/trace_dbuf.h>
48 #include <sys/callb.h>
51 struct dbuf_hold_impl_data
{
52 /* Function arguments */
56 boolean_t dh_fail_sparse
;
57 boolean_t dh_fail_uncached
;
59 dmu_buf_impl_t
**dh_dbp
;
61 dmu_buf_impl_t
*dh_db
;
62 dmu_buf_impl_t
*dh_parent
;
65 dbuf_dirty_record_t
*dh_dr
;
66 arc_buf_contents_t dh_type
;
70 static void __dbuf_hold_impl_init(struct dbuf_hold_impl_data
*dh
,
71 dnode_t
*dn
, uint8_t level
, uint64_t blkid
, boolean_t fail_sparse
,
72 boolean_t fail_uncached
,
73 void *tag
, dmu_buf_impl_t
**dbp
, int depth
);
74 static int __dbuf_hold_impl(struct dbuf_hold_impl_data
*dh
);
76 uint_t zfs_dbuf_evict_key
;
78 * Number of times that zfs_free_range() took the slow path while doing
79 * a zfs receive. A nonzero value indicates a potential performance problem.
81 uint64_t zfs_free_range_recv_miss
;
83 static boolean_t
dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
);
84 static void dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
);
87 extern inline void dmu_buf_init_user(dmu_buf_user_t
*dbu
,
88 dmu_buf_evict_func_t
*evict_func
, dmu_buf_t
**clear_on_evict_dbufp
);
92 * Global data structures and functions for the dbuf cache.
94 static kmem_cache_t
*dbuf_kmem_cache
;
95 static taskq_t
*dbu_evict_taskq
;
97 static kthread_t
*dbuf_cache_evict_thread
;
98 static kmutex_t dbuf_evict_lock
;
99 static kcondvar_t dbuf_evict_cv
;
100 static boolean_t dbuf_evict_thread_exit
;
103 * LRU cache of dbufs. The dbuf cache maintains a list of dbufs that
104 * are not currently held but have been recently released. These dbufs
105 * are not eligible for arc eviction until they are aged out of the cache.
106 * Dbufs are added to the dbuf cache once the last hold is released. If a
107 * dbuf is later accessed and still exists in the dbuf cache, then it will
108 * be removed from the cache and later re-added to the head of the cache.
109 * Dbufs that are aged out of the cache will be immediately destroyed and
110 * become eligible for arc eviction.
112 static multilist_t dbuf_cache
;
113 static refcount_t dbuf_cache_size
;
114 unsigned long dbuf_cache_max_bytes
= 100 * 1024 * 1024;
116 /* Cap the size of the dbuf cache to log2 fraction of arc size. */
117 int dbuf_cache_max_shift
= 5;
120 * The dbuf cache uses a three-stage eviction policy:
121 * - A low water marker designates when the dbuf eviction thread
122 * should stop evicting from the dbuf cache.
123 * - When we reach the maximum size (aka mid water mark), we
124 * signal the eviction thread to run.
125 * - The high water mark indicates when the eviction thread
126 * is unable to keep up with the incoming load and eviction must
127 * happen in the context of the calling thread.
131 * low water mid water hi water
132 * +----------------------------------------+----------+----------+
137 * +----------------------------------------+----------+----------+
139 * evicting eviction directly
142 * The high and low water marks indicate the operating range for the eviction
143 * thread. The low water mark is, by default, 90% of the total size of the
144 * cache and the high water mark is at 110% (both of these percentages can be
145 * changed by setting dbuf_cache_lowater_pct and dbuf_cache_hiwater_pct,
146 * respectively). The eviction thread will try to ensure that the cache remains
147 * within this range by waking up every second and checking if the cache is
148 * above the low water mark. The thread can also be woken up by callers adding
149 * elements into the cache if the cache is larger than the mid water (i.e max
150 * cache size). Once the eviction thread is woken up and eviction is required,
151 * it will continue evicting buffers until it's able to reduce the cache size
152 * to the low water mark. If the cache size continues to grow and hits the high
153 * water mark, then callers adding elements to the cache will begin to evict
154 * directly from the cache until the cache is no longer above the high water
159 * The percentage above and below the maximum cache size.
161 uint_t dbuf_cache_hiwater_pct
= 10;
162 uint_t dbuf_cache_lowater_pct
= 10;
166 dbuf_cons(void *vdb
, void *unused
, int kmflag
)
168 dmu_buf_impl_t
*db
= vdb
;
169 bzero(db
, sizeof (dmu_buf_impl_t
));
171 mutex_init(&db
->db_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
172 cv_init(&db
->db_changed
, NULL
, CV_DEFAULT
, NULL
);
173 multilist_link_init(&db
->db_cache_link
);
174 refcount_create(&db
->db_holds
);
175 multilist_link_init(&db
->db_cache_link
);
182 dbuf_dest(void *vdb
, void *unused
)
184 dmu_buf_impl_t
*db
= vdb
;
185 mutex_destroy(&db
->db_mtx
);
186 cv_destroy(&db
->db_changed
);
187 ASSERT(!multilist_link_active(&db
->db_cache_link
));
188 refcount_destroy(&db
->db_holds
);
192 * dbuf hash table routines
194 static dbuf_hash_table_t dbuf_hash_table
;
196 static uint64_t dbuf_hash_count
;
199 dbuf_hash(void *os
, uint64_t obj
, uint8_t lvl
, uint64_t blkid
)
201 uintptr_t osv
= (uintptr_t)os
;
202 uint64_t crc
= -1ULL;
204 ASSERT(zfs_crc64_table
[128] == ZFS_CRC64_POLY
);
205 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (lvl
)) & 0xFF];
206 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (osv
>> 6)) & 0xFF];
207 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 0)) & 0xFF];
208 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 8)) & 0xFF];
209 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (blkid
>> 0)) & 0xFF];
210 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (blkid
>> 8)) & 0xFF];
212 crc
^= (osv
>>14) ^ (obj
>>16) ^ (blkid
>>16);
217 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
218 ((dbuf)->db.db_object == (obj) && \
219 (dbuf)->db_objset == (os) && \
220 (dbuf)->db_level == (level) && \
221 (dbuf)->db_blkid == (blkid))
224 dbuf_find(objset_t
*os
, uint64_t obj
, uint8_t level
, uint64_t blkid
)
226 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
231 hv
= dbuf_hash(os
, obj
, level
, blkid
);
232 idx
= hv
& h
->hash_table_mask
;
234 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
235 for (db
= h
->hash_table
[idx
]; db
!= NULL
; db
= db
->db_hash_next
) {
236 if (DBUF_EQUAL(db
, os
, obj
, level
, blkid
)) {
237 mutex_enter(&db
->db_mtx
);
238 if (db
->db_state
!= DB_EVICTING
) {
239 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
242 mutex_exit(&db
->db_mtx
);
245 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
249 static dmu_buf_impl_t
*
250 dbuf_find_bonus(objset_t
*os
, uint64_t object
)
253 dmu_buf_impl_t
*db
= NULL
;
255 if (dnode_hold(os
, object
, FTAG
, &dn
) == 0) {
256 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
257 if (dn
->dn_bonus
!= NULL
) {
259 mutex_enter(&db
->db_mtx
);
261 rw_exit(&dn
->dn_struct_rwlock
);
262 dnode_rele(dn
, FTAG
);
268 * Insert an entry into the hash table. If there is already an element
269 * equal to elem in the hash table, then the already existing element
270 * will be returned and the new element will not be inserted.
271 * Otherwise returns NULL.
273 static dmu_buf_impl_t
*
274 dbuf_hash_insert(dmu_buf_impl_t
*db
)
276 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
277 objset_t
*os
= db
->db_objset
;
278 uint64_t obj
= db
->db
.db_object
;
279 int level
= db
->db_level
;
280 uint64_t blkid
, hv
, idx
;
283 blkid
= db
->db_blkid
;
284 hv
= dbuf_hash(os
, obj
, level
, blkid
);
285 idx
= hv
& h
->hash_table_mask
;
287 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
288 for (dbf
= h
->hash_table
[idx
]; dbf
!= NULL
; dbf
= dbf
->db_hash_next
) {
289 if (DBUF_EQUAL(dbf
, os
, obj
, level
, blkid
)) {
290 mutex_enter(&dbf
->db_mtx
);
291 if (dbf
->db_state
!= DB_EVICTING
) {
292 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
295 mutex_exit(&dbf
->db_mtx
);
299 mutex_enter(&db
->db_mtx
);
300 db
->db_hash_next
= h
->hash_table
[idx
];
301 h
->hash_table
[idx
] = db
;
302 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
303 atomic_inc_64(&dbuf_hash_count
);
309 * Remove an entry from the hash table. It must be in the EVICTING state.
312 dbuf_hash_remove(dmu_buf_impl_t
*db
)
314 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
316 dmu_buf_impl_t
*dbf
, **dbp
;
318 hv
= dbuf_hash(db
->db_objset
, db
->db
.db_object
,
319 db
->db_level
, db
->db_blkid
);
320 idx
= hv
& h
->hash_table_mask
;
323 * We mustn't hold db_mtx to maintain lock ordering:
324 * DBUF_HASH_MUTEX > db_mtx.
326 ASSERT(refcount_is_zero(&db
->db_holds
));
327 ASSERT(db
->db_state
== DB_EVICTING
);
328 ASSERT(!MUTEX_HELD(&db
->db_mtx
));
330 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
331 dbp
= &h
->hash_table
[idx
];
332 while ((dbf
= *dbp
) != db
) {
333 dbp
= &dbf
->db_hash_next
;
336 *dbp
= db
->db_hash_next
;
337 db
->db_hash_next
= NULL
;
338 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
339 atomic_dec_64(&dbuf_hash_count
);
345 } dbvu_verify_type_t
;
348 dbuf_verify_user(dmu_buf_impl_t
*db
, dbvu_verify_type_t verify_type
)
353 if (db
->db_user
== NULL
)
356 /* Only data blocks support the attachment of user data. */
357 ASSERT(db
->db_level
== 0);
359 /* Clients must resolve a dbuf before attaching user data. */
360 ASSERT(db
->db
.db_data
!= NULL
);
361 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
363 holds
= refcount_count(&db
->db_holds
);
364 if (verify_type
== DBVU_EVICTING
) {
366 * Immediate eviction occurs when holds == dirtycnt.
367 * For normal eviction buffers, holds is zero on
368 * eviction, except when dbuf_fix_old_data() calls
369 * dbuf_clear_data(). However, the hold count can grow
370 * during eviction even though db_mtx is held (see
371 * dmu_bonus_hold() for an example), so we can only
372 * test the generic invariant that holds >= dirtycnt.
374 ASSERT3U(holds
, >=, db
->db_dirtycnt
);
376 if (db
->db_user_immediate_evict
== TRUE
)
377 ASSERT3U(holds
, >=, db
->db_dirtycnt
);
379 ASSERT3U(holds
, >, 0);
385 dbuf_evict_user(dmu_buf_impl_t
*db
)
387 dmu_buf_user_t
*dbu
= db
->db_user
;
389 ASSERT(MUTEX_HELD(&db
->db_mtx
));
394 dbuf_verify_user(db
, DBVU_EVICTING
);
398 if (dbu
->dbu_clear_on_evict_dbufp
!= NULL
)
399 *dbu
->dbu_clear_on_evict_dbufp
= NULL
;
403 * Invoke the callback from a taskq to avoid lock order reversals
404 * and limit stack depth.
406 taskq_dispatch_ent(dbu_evict_taskq
, dbu
->dbu_evict_func
, dbu
, 0,
411 dbuf_is_metadata(dmu_buf_impl_t
*db
)
414 * Consider indirect blocks and spill blocks to be meta data.
416 if (db
->db_level
> 0 || db
->db_blkid
== DMU_SPILL_BLKID
) {
419 boolean_t is_metadata
;
422 is_metadata
= DMU_OT_IS_METADATA(DB_DNODE(db
)->dn_type
);
425 return (is_metadata
);
431 * This function *must* return indices evenly distributed between all
432 * sublists of the multilist. This is needed due to how the dbuf eviction
433 * code is laid out; dbuf_evict_thread() assumes dbufs are evenly
434 * distributed between all sublists and uses this assumption when
435 * deciding which sublist to evict from and how much to evict from it.
438 dbuf_cache_multilist_index_func(multilist_t
*ml
, void *obj
)
440 dmu_buf_impl_t
*db
= obj
;
443 * The assumption here, is the hash value for a given
444 * dmu_buf_impl_t will remain constant throughout it's lifetime
445 * (i.e. it's objset, object, level and blkid fields don't change).
446 * Thus, we don't need to store the dbuf's sublist index
447 * on insertion, as this index can be recalculated on removal.
449 * Also, the low order bits of the hash value are thought to be
450 * distributed evenly. Otherwise, in the case that the multilist
451 * has a power of two number of sublists, each sublists' usage
452 * would not be evenly distributed.
454 return (dbuf_hash(db
->db_objset
, db
->db
.db_object
,
455 db
->db_level
, db
->db_blkid
) %
456 multilist_get_num_sublists(ml
));
459 static inline boolean_t
460 dbuf_cache_above_hiwater(void)
462 uint64_t dbuf_cache_hiwater_bytes
=
463 (dbuf_cache_max_bytes
* dbuf_cache_hiwater_pct
) / 100;
465 return (refcount_count(&dbuf_cache_size
) >
466 dbuf_cache_max_bytes
+ dbuf_cache_hiwater_bytes
);
469 static inline boolean_t
470 dbuf_cache_above_lowater(void)
472 uint64_t dbuf_cache_lowater_bytes
=
473 (dbuf_cache_max_bytes
* dbuf_cache_lowater_pct
) / 100;
475 return (refcount_count(&dbuf_cache_size
) >
476 dbuf_cache_max_bytes
- dbuf_cache_lowater_bytes
);
480 * Evict the oldest eligible dbuf from the dbuf cache.
485 int idx
= multilist_get_random_index(&dbuf_cache
);
486 multilist_sublist_t
*mls
= multilist_sublist_lock(&dbuf_cache
, idx
);
488 ASSERT(!MUTEX_HELD(&dbuf_evict_lock
));
491 * Set the thread's tsd to indicate that it's processing evictions.
492 * Once a thread stops evicting from the dbuf cache it will
493 * reset its tsd to NULL.
495 ASSERT3P(tsd_get(zfs_dbuf_evict_key
), ==, NULL
);
496 (void) tsd_set(zfs_dbuf_evict_key
, (void *)B_TRUE
);
498 db
= multilist_sublist_tail(mls
);
499 while (db
!= NULL
&& mutex_tryenter(&db
->db_mtx
) == 0) {
500 db
= multilist_sublist_prev(mls
, db
);
503 DTRACE_PROBE2(dbuf__evict__one
, dmu_buf_impl_t
*, db
,
504 multilist_sublist_t
*, mls
);
507 multilist_sublist_remove(mls
, db
);
508 multilist_sublist_unlock(mls
);
509 (void) refcount_remove_many(&dbuf_cache_size
,
513 multilist_sublist_unlock(mls
);
515 (void) tsd_set(zfs_dbuf_evict_key
, NULL
);
519 * The dbuf evict thread is responsible for aging out dbufs from the
520 * cache. Once the cache has reached it's maximum size, dbufs are removed
521 * and destroyed. The eviction thread will continue running until the size
522 * of the dbuf cache is at or below the maximum size. Once the dbuf is aged
523 * out of the cache it is destroyed and becomes eligible for arc eviction.
526 dbuf_evict_thread(void)
530 CALLB_CPR_INIT(&cpr
, &dbuf_evict_lock
, callb_generic_cpr
, FTAG
);
532 mutex_enter(&dbuf_evict_lock
);
533 while (!dbuf_evict_thread_exit
) {
534 while (!dbuf_cache_above_lowater() && !dbuf_evict_thread_exit
) {
535 CALLB_CPR_SAFE_BEGIN(&cpr
);
536 (void) cv_timedwait_sig_hires(&dbuf_evict_cv
,
537 &dbuf_evict_lock
, SEC2NSEC(1), MSEC2NSEC(1), 0);
538 CALLB_CPR_SAFE_END(&cpr
, &dbuf_evict_lock
);
540 mutex_exit(&dbuf_evict_lock
);
543 * Keep evicting as long as we're above the low water mark
544 * for the cache. We do this without holding the locks to
545 * minimize lock contention.
547 while (dbuf_cache_above_lowater() && !dbuf_evict_thread_exit
) {
551 mutex_enter(&dbuf_evict_lock
);
554 dbuf_evict_thread_exit
= B_FALSE
;
555 cv_broadcast(&dbuf_evict_cv
);
556 CALLB_CPR_EXIT(&cpr
); /* drops dbuf_evict_lock */
561 * Wake up the dbuf eviction thread if the dbuf cache is at its max size.
562 * If the dbuf cache is at its high water mark, then evict a dbuf from the
563 * dbuf cache using the callers context.
566 dbuf_evict_notify(void)
570 * We use thread specific data to track when a thread has
571 * started processing evictions. This allows us to avoid deeply
572 * nested stacks that would have a call flow similar to this:
574 * dbuf_rele()-->dbuf_rele_and_unlock()-->dbuf_evict_notify()
577 * +-----dbuf_destroy()<--dbuf_evict_one()<--------+
579 * The dbuf_eviction_thread will always have its tsd set until
580 * that thread exits. All other threads will only set their tsd
581 * if they are participating in the eviction process. This only
582 * happens if the eviction thread is unable to process evictions
583 * fast enough. To keep the dbuf cache size in check, other threads
584 * can evict from the dbuf cache directly. Those threads will set
585 * their tsd values so that we ensure that they only evict one dbuf
586 * from the dbuf cache.
588 if (tsd_get(zfs_dbuf_evict_key
) != NULL
)
591 if (refcount_count(&dbuf_cache_size
) > dbuf_cache_max_bytes
) {
592 boolean_t evict_now
= B_FALSE
;
594 mutex_enter(&dbuf_evict_lock
);
595 if (refcount_count(&dbuf_cache_size
) > dbuf_cache_max_bytes
) {
596 evict_now
= dbuf_cache_above_hiwater();
597 cv_signal(&dbuf_evict_cv
);
599 mutex_exit(&dbuf_evict_lock
);
612 uint64_t hsize
= 1ULL << 16;
613 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
617 * The hash table is big enough to fill all of physical memory
618 * with an average block size of zfs_arc_average_blocksize (default 8K).
619 * By default, the table will take up
620 * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers).
622 while (hsize
* zfs_arc_average_blocksize
< physmem
* PAGESIZE
)
626 h
->hash_table_mask
= hsize
- 1;
627 #if defined(_KERNEL) && defined(HAVE_SPL)
629 * Large allocations which do not require contiguous pages
630 * should be using vmem_alloc() in the linux kernel
632 h
->hash_table
= vmem_zalloc(hsize
* sizeof (void *), KM_SLEEP
);
634 h
->hash_table
= kmem_zalloc(hsize
* sizeof (void *), KM_NOSLEEP
);
636 if (h
->hash_table
== NULL
) {
637 /* XXX - we should really return an error instead of assert */
638 ASSERT(hsize
> (1ULL << 10));
643 dbuf_kmem_cache
= kmem_cache_create("dmu_buf_impl_t",
644 sizeof (dmu_buf_impl_t
),
645 0, dbuf_cons
, dbuf_dest
, NULL
, NULL
, NULL
, 0);
647 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
648 mutex_init(&h
->hash_mutexes
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
653 * Setup the parameters for the dbuf cache. We cap the size of the
654 * dbuf cache to 1/32nd (default) of the size of the ARC.
656 dbuf_cache_max_bytes
= MIN(dbuf_cache_max_bytes
,
657 arc_max_bytes() >> dbuf_cache_max_shift
);
660 * All entries are queued via taskq_dispatch_ent(), so min/maxalloc
661 * configuration is not required.
663 dbu_evict_taskq
= taskq_create("dbu_evict", 1, defclsyspri
, 0, 0, 0);
665 multilist_create(&dbuf_cache
, sizeof (dmu_buf_impl_t
),
666 offsetof(dmu_buf_impl_t
, db_cache_link
),
667 zfs_arc_num_sublists_per_state
,
668 dbuf_cache_multilist_index_func
);
669 refcount_create(&dbuf_cache_size
);
671 tsd_create(&zfs_dbuf_evict_key
, NULL
);
672 dbuf_evict_thread_exit
= B_FALSE
;
673 mutex_init(&dbuf_evict_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
674 cv_init(&dbuf_evict_cv
, NULL
, CV_DEFAULT
, NULL
);
675 dbuf_cache_evict_thread
= thread_create(NULL
, 0, dbuf_evict_thread
,
676 NULL
, 0, &p0
, TS_RUN
, minclsyspri
);
682 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
685 dbuf_stats_destroy();
687 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
688 mutex_destroy(&h
->hash_mutexes
[i
]);
689 #if defined(_KERNEL) && defined(HAVE_SPL)
691 * Large allocations which do not require contiguous pages
692 * should be using vmem_free() in the linux kernel
694 vmem_free(h
->hash_table
, (h
->hash_table_mask
+ 1) * sizeof (void *));
696 kmem_free(h
->hash_table
, (h
->hash_table_mask
+ 1) * sizeof (void *));
698 kmem_cache_destroy(dbuf_kmem_cache
);
699 taskq_destroy(dbu_evict_taskq
);
701 mutex_enter(&dbuf_evict_lock
);
702 dbuf_evict_thread_exit
= B_TRUE
;
703 while (dbuf_evict_thread_exit
) {
704 cv_signal(&dbuf_evict_cv
);
705 cv_wait(&dbuf_evict_cv
, &dbuf_evict_lock
);
707 mutex_exit(&dbuf_evict_lock
);
708 tsd_destroy(&zfs_dbuf_evict_key
);
710 mutex_destroy(&dbuf_evict_lock
);
711 cv_destroy(&dbuf_evict_cv
);
713 refcount_destroy(&dbuf_cache_size
);
714 multilist_destroy(&dbuf_cache
);
723 dbuf_verify(dmu_buf_impl_t
*db
)
726 dbuf_dirty_record_t
*dr
;
728 ASSERT(MUTEX_HELD(&db
->db_mtx
));
730 if (!(zfs_flags
& ZFS_DEBUG_DBUF_VERIFY
))
733 ASSERT(db
->db_objset
!= NULL
);
737 ASSERT(db
->db_parent
== NULL
);
738 ASSERT(db
->db_blkptr
== NULL
);
740 ASSERT3U(db
->db
.db_object
, ==, dn
->dn_object
);
741 ASSERT3P(db
->db_objset
, ==, dn
->dn_objset
);
742 ASSERT3U(db
->db_level
, <, dn
->dn_nlevels
);
743 ASSERT(db
->db_blkid
== DMU_BONUS_BLKID
||
744 db
->db_blkid
== DMU_SPILL_BLKID
||
745 !avl_is_empty(&dn
->dn_dbufs
));
747 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
749 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
750 ASSERT3U(db
->db
.db_offset
, ==, DMU_BONUS_BLKID
);
751 } else if (db
->db_blkid
== DMU_SPILL_BLKID
) {
753 ASSERT0(db
->db
.db_offset
);
755 ASSERT3U(db
->db
.db_offset
, ==, db
->db_blkid
* db
->db
.db_size
);
758 for (dr
= db
->db_data_pending
; dr
!= NULL
; dr
= dr
->dr_next
)
759 ASSERT(dr
->dr_dbuf
== db
);
761 for (dr
= db
->db_last_dirty
; dr
!= NULL
; dr
= dr
->dr_next
)
762 ASSERT(dr
->dr_dbuf
== db
);
765 * We can't assert that db_size matches dn_datablksz because it
766 * can be momentarily different when another thread is doing
769 if (db
->db_level
== 0 && db
->db
.db_object
== DMU_META_DNODE_OBJECT
) {
770 dr
= db
->db_data_pending
;
772 * It should only be modified in syncing context, so
773 * make sure we only have one copy of the data.
775 ASSERT(dr
== NULL
|| dr
->dt
.dl
.dr_data
== db
->db_buf
);
778 /* verify db->db_blkptr */
780 if (db
->db_parent
== dn
->dn_dbuf
) {
781 /* db is pointed to by the dnode */
782 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
783 if (DMU_OBJECT_IS_SPECIAL(db
->db
.db_object
))
784 ASSERT(db
->db_parent
== NULL
);
786 ASSERT(db
->db_parent
!= NULL
);
787 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
788 ASSERT3P(db
->db_blkptr
, ==,
789 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
791 /* db is pointed to by an indirect block */
792 ASSERTV(int epb
= db
->db_parent
->db
.db_size
>>
794 ASSERT3U(db
->db_parent
->db_level
, ==, db
->db_level
+1);
795 ASSERT3U(db
->db_parent
->db
.db_object
, ==,
798 * dnode_grow_indblksz() can make this fail if we don't
799 * have the struct_rwlock. XXX indblksz no longer
800 * grows. safe to do this now?
802 if (RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
803 ASSERT3P(db
->db_blkptr
, ==,
804 ((blkptr_t
*)db
->db_parent
->db
.db_data
+
805 db
->db_blkid
% epb
));
809 if ((db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
)) &&
810 (db
->db_buf
== NULL
|| db
->db_buf
->b_data
) &&
811 db
->db
.db_data
&& db
->db_blkid
!= DMU_BONUS_BLKID
&&
812 db
->db_state
!= DB_FILL
&& !dn
->dn_free_txg
) {
814 * If the blkptr isn't set but they have nonzero data,
815 * it had better be dirty, otherwise we'll lose that
816 * data when we evict this buffer.
818 * There is an exception to this rule for indirect blocks; in
819 * this case, if the indirect block is a hole, we fill in a few
820 * fields on each of the child blocks (importantly, birth time)
821 * to prevent hole birth times from being lost when you
822 * partially fill in a hole.
824 if (db
->db_dirtycnt
== 0) {
825 if (db
->db_level
== 0) {
826 uint64_t *buf
= db
->db
.db_data
;
829 for (i
= 0; i
< db
->db
.db_size
>> 3; i
++) {
834 blkptr_t
*bps
= db
->db
.db_data
;
835 ASSERT3U(1 << DB_DNODE(db
)->dn_indblkshift
, ==,
838 * We want to verify that all the blkptrs in the
839 * indirect block are holes, but we may have
840 * automatically set up a few fields for them.
841 * We iterate through each blkptr and verify
842 * they only have those fields set.
845 i
< db
->db
.db_size
/ sizeof (blkptr_t
);
847 blkptr_t
*bp
= &bps
[i
];
848 ASSERT(ZIO_CHECKSUM_IS_ZERO(
851 DVA_IS_EMPTY(&bp
->blk_dva
[0]) &&
852 DVA_IS_EMPTY(&bp
->blk_dva
[1]) &&
853 DVA_IS_EMPTY(&bp
->blk_dva
[2]));
854 ASSERT0(bp
->blk_fill
);
855 ASSERT0(bp
->blk_pad
[0]);
856 ASSERT0(bp
->blk_pad
[1]);
857 ASSERT(!BP_IS_EMBEDDED(bp
));
858 ASSERT(BP_IS_HOLE(bp
));
859 ASSERT0(bp
->blk_phys_birth
);
869 dbuf_clear_data(dmu_buf_impl_t
*db
)
871 ASSERT(MUTEX_HELD(&db
->db_mtx
));
873 ASSERT3P(db
->db_buf
, ==, NULL
);
874 db
->db
.db_data
= NULL
;
875 if (db
->db_state
!= DB_NOFILL
)
876 db
->db_state
= DB_UNCACHED
;
880 dbuf_set_data(dmu_buf_impl_t
*db
, arc_buf_t
*buf
)
882 ASSERT(MUTEX_HELD(&db
->db_mtx
));
886 ASSERT(buf
->b_data
!= NULL
);
887 db
->db
.db_data
= buf
->b_data
;
891 * Loan out an arc_buf for read. Return the loaned arc_buf.
894 dbuf_loan_arcbuf(dmu_buf_impl_t
*db
)
898 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
899 mutex_enter(&db
->db_mtx
);
900 if (arc_released(db
->db_buf
) || refcount_count(&db
->db_holds
) > 1) {
901 int blksz
= db
->db
.db_size
;
902 spa_t
*spa
= db
->db_objset
->os_spa
;
904 mutex_exit(&db
->db_mtx
);
905 abuf
= arc_loan_buf(spa
, B_FALSE
, blksz
);
906 bcopy(db
->db
.db_data
, abuf
->b_data
, blksz
);
909 arc_loan_inuse_buf(abuf
, db
);
912 mutex_exit(&db
->db_mtx
);
918 * Calculate which level n block references the data at the level 0 offset
922 dbuf_whichblock(const dnode_t
*dn
, const int64_t level
, const uint64_t offset
)
924 if (dn
->dn_datablkshift
!= 0 && dn
->dn_indblkshift
!= 0) {
926 * The level n blkid is equal to the level 0 blkid divided by
927 * the number of level 0s in a level n block.
929 * The level 0 blkid is offset >> datablkshift =
930 * offset / 2^datablkshift.
932 * The number of level 0s in a level n is the number of block
933 * pointers in an indirect block, raised to the power of level.
934 * This is 2^(indblkshift - SPA_BLKPTRSHIFT)^level =
935 * 2^(level*(indblkshift - SPA_BLKPTRSHIFT)).
937 * Thus, the level n blkid is: offset /
938 * ((2^datablkshift)*(2^(level*(indblkshift - SPA_BLKPTRSHIFT)))
939 * = offset / 2^(datablkshift + level *
940 * (indblkshift - SPA_BLKPTRSHIFT))
941 * = offset >> (datablkshift + level *
942 * (indblkshift - SPA_BLKPTRSHIFT))
945 const unsigned exp
= dn
->dn_datablkshift
+
946 level
* (dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
);
948 if (exp
>= 8 * sizeof (offset
)) {
949 /* This only happens on the highest indirection level */
950 ASSERT3U(level
, ==, dn
->dn_nlevels
- 1);
954 ASSERT3U(exp
, <, 8 * sizeof (offset
));
956 return (offset
>> exp
);
958 ASSERT3U(offset
, <, dn
->dn_datablksz
);
964 dbuf_read_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
966 dmu_buf_impl_t
*db
= vdb
;
968 mutex_enter(&db
->db_mtx
);
969 ASSERT3U(db
->db_state
, ==, DB_READ
);
971 * All reads are synchronous, so we must have a hold on the dbuf
973 ASSERT(refcount_count(&db
->db_holds
) > 0);
974 ASSERT(db
->db_buf
== NULL
);
975 ASSERT(db
->db
.db_data
== NULL
);
976 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
977 /* we were freed in flight; disregard any error */
978 arc_release(buf
, db
);
979 bzero(buf
->b_data
, db
->db
.db_size
);
981 db
->db_freed_in_flight
= FALSE
;
982 dbuf_set_data(db
, buf
);
983 db
->db_state
= DB_CACHED
;
984 } else if (zio
== NULL
|| zio
->io_error
== 0) {
985 dbuf_set_data(db
, buf
);
986 db
->db_state
= DB_CACHED
;
988 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
989 ASSERT3P(db
->db_buf
, ==, NULL
);
990 arc_buf_destroy(buf
, db
);
991 db
->db_state
= DB_UNCACHED
;
993 cv_broadcast(&db
->db_changed
);
994 dbuf_rele_and_unlock(db
, NULL
);
998 dbuf_read_impl(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t flags
)
1001 zbookmark_phys_t zb
;
1002 uint32_t aflags
= ARC_FLAG_NOWAIT
;
1007 ASSERT(!refcount_is_zero(&db
->db_holds
));
1008 /* We need the struct_rwlock to prevent db_blkptr from changing. */
1009 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
1010 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1011 ASSERT(db
->db_state
== DB_UNCACHED
);
1012 ASSERT(db
->db_buf
== NULL
);
1014 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1016 * The bonus length stored in the dnode may be less than
1017 * the maximum available space in the bonus buffer.
1019 int bonuslen
= MIN(dn
->dn_bonuslen
, dn
->dn_phys
->dn_bonuslen
);
1020 int max_bonuslen
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
);
1022 ASSERT3U(bonuslen
, <=, db
->db
.db_size
);
1023 db
->db
.db_data
= kmem_alloc(max_bonuslen
, KM_SLEEP
);
1024 arc_space_consume(max_bonuslen
, ARC_SPACE_BONUS
);
1025 if (bonuslen
< max_bonuslen
)
1026 bzero(db
->db
.db_data
, max_bonuslen
);
1028 bcopy(DN_BONUS(dn
->dn_phys
), db
->db
.db_data
, bonuslen
);
1030 db
->db_state
= DB_CACHED
;
1031 mutex_exit(&db
->db_mtx
);
1036 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
1037 * processes the delete record and clears the bp while we are waiting
1038 * for the dn_mtx (resulting in a "no" from block_freed).
1040 if (db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
) ||
1041 (db
->db_level
== 0 && (dnode_block_freed(dn
, db
->db_blkid
) ||
1042 BP_IS_HOLE(db
->db_blkptr
)))) {
1043 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1045 dbuf_set_data(db
, arc_alloc_buf(db
->db_objset
->os_spa
, db
, type
,
1047 bzero(db
->db
.db_data
, db
->db
.db_size
);
1049 if (db
->db_blkptr
!= NULL
&& db
->db_level
> 0 &&
1050 BP_IS_HOLE(db
->db_blkptr
) &&
1051 db
->db_blkptr
->blk_birth
!= 0) {
1052 blkptr_t
*bps
= db
->db
.db_data
;
1054 for (i
= 0; i
< ((1 <<
1055 DB_DNODE(db
)->dn_indblkshift
) / sizeof (blkptr_t
));
1057 blkptr_t
*bp
= &bps
[i
];
1058 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==,
1059 1 << dn
->dn_indblkshift
);
1061 BP_GET_LEVEL(db
->db_blkptr
) == 1 ?
1063 BP_GET_LSIZE(db
->db_blkptr
));
1064 BP_SET_TYPE(bp
, BP_GET_TYPE(db
->db_blkptr
));
1066 BP_GET_LEVEL(db
->db_blkptr
) - 1);
1067 BP_SET_BIRTH(bp
, db
->db_blkptr
->blk_birth
, 0);
1071 db
->db_state
= DB_CACHED
;
1072 mutex_exit(&db
->db_mtx
);
1078 db
->db_state
= DB_READ
;
1079 mutex_exit(&db
->db_mtx
);
1081 if (DBUF_IS_L2CACHEABLE(db
))
1082 aflags
|= ARC_FLAG_L2CACHE
;
1084 SET_BOOKMARK(&zb
, db
->db_objset
->os_dsl_dataset
?
1085 db
->db_objset
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
1086 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
1088 dbuf_add_ref(db
, NULL
);
1090 err
= arc_read(zio
, db
->db_objset
->os_spa
, db
->db_blkptr
,
1091 dbuf_read_done
, db
, ZIO_PRIORITY_SYNC_READ
,
1092 (flags
& DB_RF_CANFAIL
) ? ZIO_FLAG_CANFAIL
: ZIO_FLAG_MUSTSUCCEED
,
1099 * This is our just-in-time copy function. It makes a copy of buffers that
1100 * have been modified in a previous transaction group before we access them in
1101 * the current active group.
1103 * This function is used in three places: when we are dirtying a buffer for the
1104 * first time in a txg, when we are freeing a range in a dnode that includes
1105 * this buffer, and when we are accessing a buffer which was received compressed
1106 * and later referenced in a WRITE_BYREF record.
1108 * Note that when we are called from dbuf_free_range() we do not put a hold on
1109 * the buffer, we just traverse the active dbuf list for the dnode.
1112 dbuf_fix_old_data(dmu_buf_impl_t
*db
, uint64_t txg
)
1114 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
1116 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1117 ASSERT(db
->db
.db_data
!= NULL
);
1118 ASSERT(db
->db_level
== 0);
1119 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
);
1122 (dr
->dt
.dl
.dr_data
!=
1123 ((db
->db_blkid
== DMU_BONUS_BLKID
) ? db
->db
.db_data
: db
->db_buf
)))
1127 * If the last dirty record for this dbuf has not yet synced
1128 * and its referencing the dbuf data, either:
1129 * reset the reference to point to a new copy,
1130 * or (if there a no active holders)
1131 * just null out the current db_data pointer.
1133 ASSERT(dr
->dr_txg
>= txg
- 2);
1134 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1135 dnode_t
*dn
= DB_DNODE(db
);
1136 int bonuslen
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
);
1137 dr
->dt
.dl
.dr_data
= kmem_alloc(bonuslen
, KM_SLEEP
);
1138 arc_space_consume(bonuslen
, ARC_SPACE_BONUS
);
1139 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
, bonuslen
);
1140 } else if (refcount_count(&db
->db_holds
) > db
->db_dirtycnt
) {
1141 int size
= arc_buf_size(db
->db_buf
);
1142 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1143 spa_t
*spa
= db
->db_objset
->os_spa
;
1144 enum zio_compress compress_type
=
1145 arc_get_compression(db
->db_buf
);
1147 if (compress_type
== ZIO_COMPRESS_OFF
) {
1148 dr
->dt
.dl
.dr_data
= arc_alloc_buf(spa
, db
, type
, size
);
1150 ASSERT3U(type
, ==, ARC_BUFC_DATA
);
1151 dr
->dt
.dl
.dr_data
= arc_alloc_compressed_buf(spa
, db
,
1152 size
, arc_buf_lsize(db
->db_buf
), compress_type
);
1154 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
->b_data
, size
);
1157 dbuf_clear_data(db
);
1162 dbuf_read(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t flags
)
1165 boolean_t havepzio
= (zio
!= NULL
);
1170 * We don't have to hold the mutex to check db_state because it
1171 * can't be freed while we have a hold on the buffer.
1173 ASSERT(!refcount_is_zero(&db
->db_holds
));
1175 if (db
->db_state
== DB_NOFILL
)
1176 return (SET_ERROR(EIO
));
1180 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1181 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
1183 prefetch
= db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1184 (flags
& DB_RF_NOPREFETCH
) == 0 && dn
!= NULL
&&
1185 DBUF_IS_CACHEABLE(db
);
1187 mutex_enter(&db
->db_mtx
);
1188 if (db
->db_state
== DB_CACHED
) {
1190 * If the arc buf is compressed, we need to decompress it to
1191 * read the data. This could happen during the "zfs receive" of
1192 * a stream which is compressed and deduplicated.
1194 if (db
->db_buf
!= NULL
&&
1195 arc_get_compression(db
->db_buf
) != ZIO_COMPRESS_OFF
) {
1196 dbuf_fix_old_data(db
,
1197 spa_syncing_txg(dmu_objset_spa(db
->db_objset
)));
1198 err
= arc_decompress(db
->db_buf
);
1199 dbuf_set_data(db
, db
->db_buf
);
1201 mutex_exit(&db
->db_mtx
);
1203 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
);
1204 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1205 rw_exit(&dn
->dn_struct_rwlock
);
1207 } else if (db
->db_state
== DB_UNCACHED
) {
1208 spa_t
*spa
= dn
->dn_objset
->os_spa
;
1211 db
->db_blkptr
!= NULL
&& !BP_IS_HOLE(db
->db_blkptr
))
1212 zio
= zio_root(spa
, NULL
, NULL
, ZIO_FLAG_CANFAIL
);
1214 err
= dbuf_read_impl(db
, zio
, flags
);
1216 /* dbuf_read_impl has dropped db_mtx for us */
1218 if (!err
&& prefetch
)
1219 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
);
1221 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1222 rw_exit(&dn
->dn_struct_rwlock
);
1225 if (!err
&& !havepzio
&& zio
!= NULL
)
1226 err
= zio_wait(zio
);
1229 * Another reader came in while the dbuf was in flight
1230 * between UNCACHED and CACHED. Either a writer will finish
1231 * writing the buffer (sending the dbuf to CACHED) or the
1232 * first reader's request will reach the read_done callback
1233 * and send the dbuf to CACHED. Otherwise, a failure
1234 * occurred and the dbuf went to UNCACHED.
1236 mutex_exit(&db
->db_mtx
);
1238 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
);
1239 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1240 rw_exit(&dn
->dn_struct_rwlock
);
1243 /* Skip the wait per the caller's request. */
1244 mutex_enter(&db
->db_mtx
);
1245 if ((flags
& DB_RF_NEVERWAIT
) == 0) {
1246 while (db
->db_state
== DB_READ
||
1247 db
->db_state
== DB_FILL
) {
1248 ASSERT(db
->db_state
== DB_READ
||
1249 (flags
& DB_RF_HAVESTRUCT
) == 0);
1250 DTRACE_PROBE2(blocked__read
, dmu_buf_impl_t
*,
1252 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1254 if (db
->db_state
== DB_UNCACHED
)
1255 err
= SET_ERROR(EIO
);
1257 mutex_exit(&db
->db_mtx
);
1260 ASSERT(err
|| havepzio
|| db
->db_state
== DB_CACHED
);
1265 dbuf_noread(dmu_buf_impl_t
*db
)
1267 ASSERT(!refcount_is_zero(&db
->db_holds
));
1268 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1269 mutex_enter(&db
->db_mtx
);
1270 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
1271 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1272 if (db
->db_state
== DB_UNCACHED
) {
1273 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1274 spa_t
*spa
= db
->db_objset
->os_spa
;
1276 ASSERT(db
->db_buf
== NULL
);
1277 ASSERT(db
->db
.db_data
== NULL
);
1278 dbuf_set_data(db
, arc_alloc_buf(spa
, db
, type
, db
->db
.db_size
));
1279 db
->db_state
= DB_FILL
;
1280 } else if (db
->db_state
== DB_NOFILL
) {
1281 dbuf_clear_data(db
);
1283 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
1285 mutex_exit(&db
->db_mtx
);
1289 dbuf_unoverride(dbuf_dirty_record_t
*dr
)
1291 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
1292 blkptr_t
*bp
= &dr
->dt
.dl
.dr_overridden_by
;
1293 uint64_t txg
= dr
->dr_txg
;
1295 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1296 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_IN_DMU_SYNC
);
1297 ASSERT(db
->db_level
== 0);
1299 if (db
->db_blkid
== DMU_BONUS_BLKID
||
1300 dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
)
1303 ASSERT(db
->db_data_pending
!= dr
);
1305 /* free this block */
1306 if (!BP_IS_HOLE(bp
) && !dr
->dt
.dl
.dr_nopwrite
)
1307 zio_free(db
->db_objset
->os_spa
, txg
, bp
);
1309 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
1310 dr
->dt
.dl
.dr_nopwrite
= B_FALSE
;
1313 * Release the already-written buffer, so we leave it in
1314 * a consistent dirty state. Note that all callers are
1315 * modifying the buffer, so they will immediately do
1316 * another (redundant) arc_release(). Therefore, leave
1317 * the buf thawed to save the effort of freezing &
1318 * immediately re-thawing it.
1320 arc_release(dr
->dt
.dl
.dr_data
, db
);
1324 * Evict (if its unreferenced) or clear (if its referenced) any level-0
1325 * data blocks in the free range, so that any future readers will find
1328 * This is a no-op if the dataset is in the middle of an incremental
1329 * receive; see comment below for details.
1332 dbuf_free_range(dnode_t
*dn
, uint64_t start_blkid
, uint64_t end_blkid
,
1335 dmu_buf_impl_t
*db_search
;
1336 dmu_buf_impl_t
*db
, *db_next
;
1337 uint64_t txg
= tx
->tx_txg
;
1339 boolean_t freespill
=
1340 (start_blkid
== DMU_SPILL_BLKID
|| end_blkid
== DMU_SPILL_BLKID
);
1342 if (end_blkid
> dn
->dn_maxblkid
&& !freespill
)
1343 end_blkid
= dn
->dn_maxblkid
;
1344 dprintf_dnode(dn
, "start=%llu end=%llu\n", start_blkid
, end_blkid
);
1346 db_search
= kmem_alloc(sizeof (dmu_buf_impl_t
), KM_SLEEP
);
1347 db_search
->db_level
= 0;
1348 db_search
->db_blkid
= start_blkid
;
1349 db_search
->db_state
= DB_SEARCH
;
1351 mutex_enter(&dn
->dn_dbufs_mtx
);
1352 if (start_blkid
>= dn
->dn_unlisted_l0_blkid
&& !freespill
) {
1353 /* There can't be any dbufs in this range; no need to search. */
1355 db
= avl_find(&dn
->dn_dbufs
, db_search
, &where
);
1356 ASSERT3P(db
, ==, NULL
);
1357 db
= avl_nearest(&dn
->dn_dbufs
, where
, AVL_AFTER
);
1358 ASSERT(db
== NULL
|| db
->db_level
> 0);
1361 } else if (dmu_objset_is_receiving(dn
->dn_objset
)) {
1363 * If we are receiving, we expect there to be no dbufs in
1364 * the range to be freed, because receive modifies each
1365 * block at most once, and in offset order. If this is
1366 * not the case, it can lead to performance problems,
1367 * so note that we unexpectedly took the slow path.
1369 atomic_inc_64(&zfs_free_range_recv_miss
);
1372 db
= avl_find(&dn
->dn_dbufs
, db_search
, &where
);
1373 ASSERT3P(db
, ==, NULL
);
1374 db
= avl_nearest(&dn
->dn_dbufs
, where
, AVL_AFTER
);
1376 for (; db
!= NULL
; db
= db_next
) {
1377 db_next
= AVL_NEXT(&dn
->dn_dbufs
, db
);
1378 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1380 if (db
->db_level
!= 0 || db
->db_blkid
> end_blkid
) {
1383 ASSERT3U(db
->db_blkid
, >=, start_blkid
);
1385 /* found a level 0 buffer in the range */
1386 mutex_enter(&db
->db_mtx
);
1387 if (dbuf_undirty(db
, tx
)) {
1388 /* mutex has been dropped and dbuf destroyed */
1392 if (db
->db_state
== DB_UNCACHED
||
1393 db
->db_state
== DB_NOFILL
||
1394 db
->db_state
== DB_EVICTING
) {
1395 ASSERT(db
->db
.db_data
== NULL
);
1396 mutex_exit(&db
->db_mtx
);
1399 if (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
) {
1400 /* will be handled in dbuf_read_done or dbuf_rele */
1401 db
->db_freed_in_flight
= TRUE
;
1402 mutex_exit(&db
->db_mtx
);
1405 if (refcount_count(&db
->db_holds
) == 0) {
1410 /* The dbuf is referenced */
1412 if (db
->db_last_dirty
!= NULL
) {
1413 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
1415 if (dr
->dr_txg
== txg
) {
1417 * This buffer is "in-use", re-adjust the file
1418 * size to reflect that this buffer may
1419 * contain new data when we sync.
1421 if (db
->db_blkid
!= DMU_SPILL_BLKID
&&
1422 db
->db_blkid
> dn
->dn_maxblkid
)
1423 dn
->dn_maxblkid
= db
->db_blkid
;
1424 dbuf_unoverride(dr
);
1427 * This dbuf is not dirty in the open context.
1428 * Either uncache it (if its not referenced in
1429 * the open context) or reset its contents to
1432 dbuf_fix_old_data(db
, txg
);
1435 /* clear the contents if its cached */
1436 if (db
->db_state
== DB_CACHED
) {
1437 ASSERT(db
->db
.db_data
!= NULL
);
1438 arc_release(db
->db_buf
, db
);
1439 bzero(db
->db
.db_data
, db
->db
.db_size
);
1440 arc_buf_freeze(db
->db_buf
);
1443 mutex_exit(&db
->db_mtx
);
1447 kmem_free(db_search
, sizeof (dmu_buf_impl_t
));
1448 mutex_exit(&dn
->dn_dbufs_mtx
);
1452 dbuf_block_freeable(dmu_buf_impl_t
*db
)
1454 dsl_dataset_t
*ds
= db
->db_objset
->os_dsl_dataset
;
1455 uint64_t birth_txg
= 0;
1458 * We don't need any locking to protect db_blkptr:
1459 * If it's syncing, then db_last_dirty will be set
1460 * so we'll ignore db_blkptr.
1462 * This logic ensures that only block births for
1463 * filled blocks are considered.
1465 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1466 if (db
->db_last_dirty
&& (db
->db_blkptr
== NULL
||
1467 !BP_IS_HOLE(db
->db_blkptr
))) {
1468 birth_txg
= db
->db_last_dirty
->dr_txg
;
1469 } else if (db
->db_blkptr
!= NULL
&& !BP_IS_HOLE(db
->db_blkptr
)) {
1470 birth_txg
= db
->db_blkptr
->blk_birth
;
1474 * If this block don't exist or is in a snapshot, it can't be freed.
1475 * Don't pass the bp to dsl_dataset_block_freeable() since we
1476 * are holding the db_mtx lock and might deadlock if we are
1477 * prefetching a dedup-ed block.
1480 return (ds
== NULL
||
1481 dsl_dataset_block_freeable(ds
, NULL
, birth_txg
));
1487 dbuf_new_size(dmu_buf_impl_t
*db
, int size
, dmu_tx_t
*tx
)
1489 arc_buf_t
*buf
, *obuf
;
1490 int osize
= db
->db
.db_size
;
1491 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1494 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1499 /* XXX does *this* func really need the lock? */
1500 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
1503 * This call to dmu_buf_will_dirty() with the dn_struct_rwlock held
1504 * is OK, because there can be no other references to the db
1505 * when we are changing its size, so no concurrent DB_FILL can
1509 * XXX we should be doing a dbuf_read, checking the return
1510 * value and returning that up to our callers
1512 dmu_buf_will_dirty(&db
->db
, tx
);
1514 /* create the data buffer for the new block */
1515 buf
= arc_alloc_buf(dn
->dn_objset
->os_spa
, db
, type
, size
);
1517 /* copy old block data to the new block */
1519 bcopy(obuf
->b_data
, buf
->b_data
, MIN(osize
, size
));
1520 /* zero the remainder */
1522 bzero((uint8_t *)buf
->b_data
+ osize
, size
- osize
);
1524 mutex_enter(&db
->db_mtx
);
1525 dbuf_set_data(db
, buf
);
1526 arc_buf_destroy(obuf
, db
);
1527 db
->db
.db_size
= size
;
1529 if (db
->db_level
== 0) {
1530 ASSERT3U(db
->db_last_dirty
->dr_txg
, ==, tx
->tx_txg
);
1531 db
->db_last_dirty
->dt
.dl
.dr_data
= buf
;
1533 mutex_exit(&db
->db_mtx
);
1535 dnode_willuse_space(dn
, size
-osize
, tx
);
1540 dbuf_release_bp(dmu_buf_impl_t
*db
)
1542 ASSERTV(objset_t
*os
= db
->db_objset
);
1544 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os
)));
1545 ASSERT(arc_released(os
->os_phys_buf
) ||
1546 list_link_active(&os
->os_dsl_dataset
->ds_synced_link
));
1547 ASSERT(db
->db_parent
== NULL
|| arc_released(db
->db_parent
->db_buf
));
1549 (void) arc_release(db
->db_buf
, db
);
1553 * We already have a dirty record for this TXG, and we are being
1557 dbuf_redirty(dbuf_dirty_record_t
*dr
)
1559 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
1561 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1563 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
) {
1565 * If this buffer has already been written out,
1566 * we now need to reset its state.
1568 dbuf_unoverride(dr
);
1569 if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
&&
1570 db
->db_state
!= DB_NOFILL
) {
1571 /* Already released on initial dirty, so just thaw. */
1572 ASSERT(arc_released(db
->db_buf
));
1573 arc_buf_thaw(db
->db_buf
);
1578 dbuf_dirty_record_t
*
1579 dbuf_dirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1583 dbuf_dirty_record_t
**drp
, *dr
;
1584 int drop_struct_lock
= FALSE
;
1585 boolean_t do_free_accounting
= B_FALSE
;
1586 int txgoff
= tx
->tx_txg
& TXG_MASK
;
1588 ASSERT(tx
->tx_txg
!= 0);
1589 ASSERT(!refcount_is_zero(&db
->db_holds
));
1590 DMU_TX_DIRTY_BUF(tx
, db
);
1595 * Shouldn't dirty a regular buffer in syncing context. Private
1596 * objects may be dirtied in syncing context, but only if they
1597 * were already pre-dirtied in open context.
1599 ASSERT(!dmu_tx_is_syncing(tx
) ||
1600 BP_IS_HOLE(dn
->dn_objset
->os_rootbp
) ||
1601 DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
1602 dn
->dn_objset
->os_dsl_dataset
== NULL
);
1604 * We make this assert for private objects as well, but after we
1605 * check if we're already dirty. They are allowed to re-dirty
1606 * in syncing context.
1608 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
1609 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
1610 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
1612 mutex_enter(&db
->db_mtx
);
1614 * XXX make this true for indirects too? The problem is that
1615 * transactions created with dmu_tx_create_assigned() from
1616 * syncing context don't bother holding ahead.
1618 ASSERT(db
->db_level
!= 0 ||
1619 db
->db_state
== DB_CACHED
|| db
->db_state
== DB_FILL
||
1620 db
->db_state
== DB_NOFILL
);
1622 mutex_enter(&dn
->dn_mtx
);
1624 * Don't set dirtyctx to SYNC if we're just modifying this as we
1625 * initialize the objset.
1627 if (dn
->dn_dirtyctx
== DN_UNDIRTIED
&&
1628 !BP_IS_HOLE(dn
->dn_objset
->os_rootbp
)) {
1630 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
);
1631 ASSERT(dn
->dn_dirtyctx_firstset
== NULL
);
1632 dn
->dn_dirtyctx_firstset
= kmem_alloc(1, KM_SLEEP
);
1634 mutex_exit(&dn
->dn_mtx
);
1636 if (db
->db_blkid
== DMU_SPILL_BLKID
)
1637 dn
->dn_have_spill
= B_TRUE
;
1640 * If this buffer is already dirty, we're done.
1642 drp
= &db
->db_last_dirty
;
1643 ASSERT(*drp
== NULL
|| (*drp
)->dr_txg
<= tx
->tx_txg
||
1644 db
->db
.db_object
== DMU_META_DNODE_OBJECT
);
1645 while ((dr
= *drp
) != NULL
&& dr
->dr_txg
> tx
->tx_txg
)
1647 if (dr
&& dr
->dr_txg
== tx
->tx_txg
) {
1651 mutex_exit(&db
->db_mtx
);
1656 * Only valid if not already dirty.
1658 ASSERT(dn
->dn_object
== 0 ||
1659 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
1660 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
1662 ASSERT3U(dn
->dn_nlevels
, >, db
->db_level
);
1663 ASSERT((dn
->dn_phys
->dn_nlevels
== 0 && db
->db_level
== 0) ||
1664 dn
->dn_phys
->dn_nlevels
> db
->db_level
||
1665 dn
->dn_next_nlevels
[txgoff
] > db
->db_level
||
1666 dn
->dn_next_nlevels
[(tx
->tx_txg
-1) & TXG_MASK
] > db
->db_level
||
1667 dn
->dn_next_nlevels
[(tx
->tx_txg
-2) & TXG_MASK
] > db
->db_level
);
1670 * We should only be dirtying in syncing context if it's the
1671 * mos or we're initializing the os or it's a special object.
1672 * However, we are allowed to dirty in syncing context provided
1673 * we already dirtied it in open context. Hence we must make
1674 * this assertion only if we're not already dirty.
1677 ASSERT(!dmu_tx_is_syncing(tx
) || DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
1678 os
->os_dsl_dataset
== NULL
|| BP_IS_HOLE(os
->os_rootbp
));
1679 ASSERT(db
->db
.db_size
!= 0);
1681 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
1683 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
1685 * Update the accounting.
1686 * Note: we delay "free accounting" until after we drop
1687 * the db_mtx. This keeps us from grabbing other locks
1688 * (and possibly deadlocking) in bp_get_dsize() while
1689 * also holding the db_mtx.
1691 dnode_willuse_space(dn
, db
->db
.db_size
, tx
);
1692 do_free_accounting
= dbuf_block_freeable(db
);
1696 * If this buffer is dirty in an old transaction group we need
1697 * to make a copy of it so that the changes we make in this
1698 * transaction group won't leak out when we sync the older txg.
1700 dr
= kmem_zalloc(sizeof (dbuf_dirty_record_t
), KM_SLEEP
);
1701 list_link_init(&dr
->dr_dirty_node
);
1702 if (db
->db_level
== 0) {
1703 void *data_old
= db
->db_buf
;
1705 if (db
->db_state
!= DB_NOFILL
) {
1706 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1707 dbuf_fix_old_data(db
, tx
->tx_txg
);
1708 data_old
= db
->db
.db_data
;
1709 } else if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
) {
1711 * Release the data buffer from the cache so
1712 * that we can modify it without impacting
1713 * possible other users of this cached data
1714 * block. Note that indirect blocks and
1715 * private objects are not released until the
1716 * syncing state (since they are only modified
1719 arc_release(db
->db_buf
, db
);
1720 dbuf_fix_old_data(db
, tx
->tx_txg
);
1721 data_old
= db
->db_buf
;
1723 ASSERT(data_old
!= NULL
);
1725 dr
->dt
.dl
.dr_data
= data_old
;
1727 mutex_init(&dr
->dt
.di
.dr_mtx
, NULL
, MUTEX_NOLOCKDEP
, NULL
);
1728 list_create(&dr
->dt
.di
.dr_children
,
1729 sizeof (dbuf_dirty_record_t
),
1730 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
1732 if (db
->db_blkid
!= DMU_BONUS_BLKID
&& os
->os_dsl_dataset
!= NULL
)
1733 dr
->dr_accounted
= db
->db
.db_size
;
1735 dr
->dr_txg
= tx
->tx_txg
;
1740 * We could have been freed_in_flight between the dbuf_noread
1741 * and dbuf_dirty. We win, as though the dbuf_noread() had
1742 * happened after the free.
1744 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1745 db
->db_blkid
!= DMU_SPILL_BLKID
) {
1746 mutex_enter(&dn
->dn_mtx
);
1747 if (dn
->dn_free_ranges
[txgoff
] != NULL
) {
1748 range_tree_clear(dn
->dn_free_ranges
[txgoff
],
1751 mutex_exit(&dn
->dn_mtx
);
1752 db
->db_freed_in_flight
= FALSE
;
1756 * This buffer is now part of this txg
1758 dbuf_add_ref(db
, (void *)(uintptr_t)tx
->tx_txg
);
1759 db
->db_dirtycnt
+= 1;
1760 ASSERT3U(db
->db_dirtycnt
, <=, 3);
1762 mutex_exit(&db
->db_mtx
);
1764 if (db
->db_blkid
== DMU_BONUS_BLKID
||
1765 db
->db_blkid
== DMU_SPILL_BLKID
) {
1766 mutex_enter(&dn
->dn_mtx
);
1767 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1768 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
1769 mutex_exit(&dn
->dn_mtx
);
1770 dnode_setdirty(dn
, tx
);
1776 * The dn_struct_rwlock prevents db_blkptr from changing
1777 * due to a write from syncing context completing
1778 * while we are running, so we want to acquire it before
1779 * looking at db_blkptr.
1781 if (!RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
1782 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
1783 drop_struct_lock
= TRUE
;
1786 if (do_free_accounting
) {
1787 blkptr_t
*bp
= db
->db_blkptr
;
1788 int64_t willfree
= (bp
&& !BP_IS_HOLE(bp
)) ?
1789 bp_get_dsize(os
->os_spa
, bp
) : db
->db
.db_size
;
1791 * This is only a guess -- if the dbuf is dirty
1792 * in a previous txg, we don't know how much
1793 * space it will use on disk yet. We should
1794 * really have the struct_rwlock to access
1795 * db_blkptr, but since this is just a guess,
1796 * it's OK if we get an odd answer.
1798 ddt_prefetch(os
->os_spa
, bp
);
1799 dnode_willuse_space(dn
, -willfree
, tx
);
1802 if (db
->db_level
== 0) {
1803 dnode_new_blkid(dn
, db
->db_blkid
, tx
, drop_struct_lock
);
1804 ASSERT(dn
->dn_maxblkid
>= db
->db_blkid
);
1807 if (db
->db_level
+1 < dn
->dn_nlevels
) {
1808 dmu_buf_impl_t
*parent
= db
->db_parent
;
1809 dbuf_dirty_record_t
*di
;
1810 int parent_held
= FALSE
;
1812 if (db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
) {
1813 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1815 parent
= dbuf_hold_level(dn
, db
->db_level
+1,
1816 db
->db_blkid
>> epbs
, FTAG
);
1817 ASSERT(parent
!= NULL
);
1820 if (drop_struct_lock
)
1821 rw_exit(&dn
->dn_struct_rwlock
);
1822 ASSERT3U(db
->db_level
+1, ==, parent
->db_level
);
1823 di
= dbuf_dirty(parent
, tx
);
1825 dbuf_rele(parent
, FTAG
);
1827 mutex_enter(&db
->db_mtx
);
1829 * Since we've dropped the mutex, it's possible that
1830 * dbuf_undirty() might have changed this out from under us.
1832 if (db
->db_last_dirty
== dr
||
1833 dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
1834 mutex_enter(&di
->dt
.di
.dr_mtx
);
1835 ASSERT3U(di
->dr_txg
, ==, tx
->tx_txg
);
1836 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1837 list_insert_tail(&di
->dt
.di
.dr_children
, dr
);
1838 mutex_exit(&di
->dt
.di
.dr_mtx
);
1841 mutex_exit(&db
->db_mtx
);
1843 ASSERT(db
->db_level
+1 == dn
->dn_nlevels
);
1844 ASSERT(db
->db_blkid
< dn
->dn_nblkptr
);
1845 ASSERT(db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
1846 mutex_enter(&dn
->dn_mtx
);
1847 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1848 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
1849 mutex_exit(&dn
->dn_mtx
);
1850 if (drop_struct_lock
)
1851 rw_exit(&dn
->dn_struct_rwlock
);
1854 dnode_setdirty(dn
, tx
);
1860 * Undirty a buffer in the transaction group referenced by the given
1861 * transaction. Return whether this evicted the dbuf.
1864 dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1867 uint64_t txg
= tx
->tx_txg
;
1868 dbuf_dirty_record_t
*dr
, **drp
;
1873 * Due to our use of dn_nlevels below, this can only be called
1874 * in open context, unless we are operating on the MOS.
1875 * From syncing context, dn_nlevels may be different from the
1876 * dn_nlevels used when dbuf was dirtied.
1878 ASSERT(db
->db_objset
==
1879 dmu_objset_pool(db
->db_objset
)->dp_meta_objset
||
1880 txg
!= spa_syncing_txg(dmu_objset_spa(db
->db_objset
)));
1881 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1882 ASSERT0(db
->db_level
);
1883 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1886 * If this buffer is not dirty, we're done.
1888 for (drp
= &db
->db_last_dirty
; (dr
= *drp
) != NULL
; drp
= &dr
->dr_next
)
1889 if (dr
->dr_txg
<= txg
)
1891 if (dr
== NULL
|| dr
->dr_txg
< txg
)
1893 ASSERT(dr
->dr_txg
== txg
);
1894 ASSERT(dr
->dr_dbuf
== db
);
1899 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
1901 ASSERT(db
->db
.db_size
!= 0);
1903 dsl_pool_undirty_space(dmu_objset_pool(dn
->dn_objset
),
1904 dr
->dr_accounted
, txg
);
1909 * Note that there are three places in dbuf_dirty()
1910 * where this dirty record may be put on a list.
1911 * Make sure to do a list_remove corresponding to
1912 * every one of those list_insert calls.
1914 if (dr
->dr_parent
) {
1915 mutex_enter(&dr
->dr_parent
->dt
.di
.dr_mtx
);
1916 list_remove(&dr
->dr_parent
->dt
.di
.dr_children
, dr
);
1917 mutex_exit(&dr
->dr_parent
->dt
.di
.dr_mtx
);
1918 } else if (db
->db_blkid
== DMU_SPILL_BLKID
||
1919 db
->db_level
+ 1 == dn
->dn_nlevels
) {
1920 ASSERT(db
->db_blkptr
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
1921 mutex_enter(&dn
->dn_mtx
);
1922 list_remove(&dn
->dn_dirty_records
[txg
& TXG_MASK
], dr
);
1923 mutex_exit(&dn
->dn_mtx
);
1927 if (db
->db_state
!= DB_NOFILL
) {
1928 dbuf_unoverride(dr
);
1930 ASSERT(db
->db_buf
!= NULL
);
1931 ASSERT(dr
->dt
.dl
.dr_data
!= NULL
);
1932 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
1933 arc_buf_destroy(dr
->dt
.dl
.dr_data
, db
);
1936 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
1938 ASSERT(db
->db_dirtycnt
> 0);
1939 db
->db_dirtycnt
-= 1;
1941 if (refcount_remove(&db
->db_holds
, (void *)(uintptr_t)txg
) == 0) {
1942 ASSERT(db
->db_state
== DB_NOFILL
|| arc_released(db
->db_buf
));
1951 dmu_buf_will_dirty(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1953 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1954 int rf
= DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
;
1955 dbuf_dirty_record_t
*dr
;
1957 ASSERT(tx
->tx_txg
!= 0);
1958 ASSERT(!refcount_is_zero(&db
->db_holds
));
1961 * Quick check for dirtyness. For already dirty blocks, this
1962 * reduces runtime of this function by >90%, and overall performance
1963 * by 50% for some workloads (e.g. file deletion with indirect blocks
1966 mutex_enter(&db
->db_mtx
);
1968 for (dr
= db
->db_last_dirty
;
1969 dr
!= NULL
&& dr
->dr_txg
>= tx
->tx_txg
; dr
= dr
->dr_next
) {
1971 * It's possible that it is already dirty but not cached,
1972 * because there are some calls to dbuf_dirty() that don't
1973 * go through dmu_buf_will_dirty().
1975 if (dr
->dr_txg
== tx
->tx_txg
&& db
->db_state
== DB_CACHED
) {
1976 /* This dbuf is already dirty and cached. */
1978 mutex_exit(&db
->db_mtx
);
1982 mutex_exit(&db
->db_mtx
);
1985 if (RW_WRITE_HELD(&DB_DNODE(db
)->dn_struct_rwlock
))
1986 rf
|= DB_RF_HAVESTRUCT
;
1988 (void) dbuf_read(db
, NULL
, rf
);
1989 (void) dbuf_dirty(db
, tx
);
1993 dmu_buf_will_not_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1995 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1997 db
->db_state
= DB_NOFILL
;
1999 dmu_buf_will_fill(db_fake
, tx
);
2003 dmu_buf_will_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
2005 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2007 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2008 ASSERT(tx
->tx_txg
!= 0);
2009 ASSERT(db
->db_level
== 0);
2010 ASSERT(!refcount_is_zero(&db
->db_holds
));
2012 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
||
2013 dmu_tx_private_ok(tx
));
2016 (void) dbuf_dirty(db
, tx
);
2019 #pragma weak dmu_buf_fill_done = dbuf_fill_done
2022 dbuf_fill_done(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
2024 mutex_enter(&db
->db_mtx
);
2027 if (db
->db_state
== DB_FILL
) {
2028 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
2029 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2030 /* we were freed while filling */
2031 /* XXX dbuf_undirty? */
2032 bzero(db
->db
.db_data
, db
->db
.db_size
);
2033 db
->db_freed_in_flight
= FALSE
;
2035 db
->db_state
= DB_CACHED
;
2036 cv_broadcast(&db
->db_changed
);
2038 mutex_exit(&db
->db_mtx
);
2042 dmu_buf_write_embedded(dmu_buf_t
*dbuf
, void *data
,
2043 bp_embedded_type_t etype
, enum zio_compress comp
,
2044 int uncompressed_size
, int compressed_size
, int byteorder
,
2047 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbuf
;
2048 struct dirty_leaf
*dl
;
2049 dmu_object_type_t type
;
2051 if (etype
== BP_EMBEDDED_TYPE_DATA
) {
2052 ASSERT(spa_feature_is_active(dmu_objset_spa(db
->db_objset
),
2053 SPA_FEATURE_EMBEDDED_DATA
));
2057 type
= DB_DNODE(db
)->dn_type
;
2060 ASSERT0(db
->db_level
);
2061 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2063 dmu_buf_will_not_fill(dbuf
, tx
);
2065 ASSERT3U(db
->db_last_dirty
->dr_txg
, ==, tx
->tx_txg
);
2066 dl
= &db
->db_last_dirty
->dt
.dl
;
2067 encode_embedded_bp_compressed(&dl
->dr_overridden_by
,
2068 data
, comp
, uncompressed_size
, compressed_size
);
2069 BPE_SET_ETYPE(&dl
->dr_overridden_by
, etype
);
2070 BP_SET_TYPE(&dl
->dr_overridden_by
, type
);
2071 BP_SET_LEVEL(&dl
->dr_overridden_by
, 0);
2072 BP_SET_BYTEORDER(&dl
->dr_overridden_by
, byteorder
);
2074 dl
->dr_override_state
= DR_OVERRIDDEN
;
2075 dl
->dr_overridden_by
.blk_birth
= db
->db_last_dirty
->dr_txg
;
2079 * Directly assign a provided arc buf to a given dbuf if it's not referenced
2080 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
2083 dbuf_assign_arcbuf(dmu_buf_impl_t
*db
, arc_buf_t
*buf
, dmu_tx_t
*tx
)
2085 ASSERT(!refcount_is_zero(&db
->db_holds
));
2086 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2087 ASSERT(db
->db_level
== 0);
2088 ASSERT3U(dbuf_is_metadata(db
), ==, arc_is_metadata(buf
));
2089 ASSERT(buf
!= NULL
);
2090 ASSERT(arc_buf_lsize(buf
) == db
->db
.db_size
);
2091 ASSERT(tx
->tx_txg
!= 0);
2093 arc_return_buf(buf
, db
);
2094 ASSERT(arc_released(buf
));
2096 mutex_enter(&db
->db_mtx
);
2098 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
2099 cv_wait(&db
->db_changed
, &db
->db_mtx
);
2101 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_UNCACHED
);
2103 if (db
->db_state
== DB_CACHED
&&
2104 refcount_count(&db
->db_holds
) - 1 > db
->db_dirtycnt
) {
2105 mutex_exit(&db
->db_mtx
);
2106 (void) dbuf_dirty(db
, tx
);
2107 bcopy(buf
->b_data
, db
->db
.db_data
, db
->db
.db_size
);
2108 arc_buf_destroy(buf
, db
);
2109 xuio_stat_wbuf_copied();
2113 xuio_stat_wbuf_nocopy();
2114 if (db
->db_state
== DB_CACHED
) {
2115 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
2117 ASSERT(db
->db_buf
!= NULL
);
2118 if (dr
!= NULL
&& dr
->dr_txg
== tx
->tx_txg
) {
2119 ASSERT(dr
->dt
.dl
.dr_data
== db
->db_buf
);
2120 if (!arc_released(db
->db_buf
)) {
2121 ASSERT(dr
->dt
.dl
.dr_override_state
==
2123 arc_release(db
->db_buf
, db
);
2125 dr
->dt
.dl
.dr_data
= buf
;
2126 arc_buf_destroy(db
->db_buf
, db
);
2127 } else if (dr
== NULL
|| dr
->dt
.dl
.dr_data
!= db
->db_buf
) {
2128 arc_release(db
->db_buf
, db
);
2129 arc_buf_destroy(db
->db_buf
, db
);
2133 ASSERT(db
->db_buf
== NULL
);
2134 dbuf_set_data(db
, buf
);
2135 db
->db_state
= DB_FILL
;
2136 mutex_exit(&db
->db_mtx
);
2137 (void) dbuf_dirty(db
, tx
);
2138 dmu_buf_fill_done(&db
->db
, tx
);
2142 dbuf_destroy(dmu_buf_impl_t
*db
)
2145 dmu_buf_impl_t
*parent
= db
->db_parent
;
2146 dmu_buf_impl_t
*dndb
;
2148 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2149 ASSERT(refcount_is_zero(&db
->db_holds
));
2151 if (db
->db_buf
!= NULL
) {
2152 arc_buf_destroy(db
->db_buf
, db
);
2156 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2157 int slots
= DB_DNODE(db
)->dn_num_slots
;
2158 int bonuslen
= DN_SLOTS_TO_BONUSLEN(slots
);
2159 ASSERT(db
->db
.db_data
!= NULL
);
2160 kmem_free(db
->db
.db_data
, bonuslen
);
2161 arc_space_return(bonuslen
, ARC_SPACE_BONUS
);
2162 db
->db_state
= DB_UNCACHED
;
2165 dbuf_clear_data(db
);
2167 if (multilist_link_active(&db
->db_cache_link
)) {
2168 multilist_remove(&dbuf_cache
, db
);
2169 (void) refcount_remove_many(&dbuf_cache_size
,
2170 db
->db
.db_size
, db
);
2173 ASSERT(db
->db_state
== DB_UNCACHED
|| db
->db_state
== DB_NOFILL
);
2174 ASSERT(db
->db_data_pending
== NULL
);
2176 db
->db_state
= DB_EVICTING
;
2177 db
->db_blkptr
= NULL
;
2180 * Now that db_state is DB_EVICTING, nobody else can find this via
2181 * the hash table. We can now drop db_mtx, which allows us to
2182 * acquire the dn_dbufs_mtx.
2184 mutex_exit(&db
->db_mtx
);
2189 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
2190 boolean_t needlock
= !MUTEX_HELD(&dn
->dn_dbufs_mtx
);
2192 mutex_enter(&dn
->dn_dbufs_mtx
);
2193 avl_remove(&dn
->dn_dbufs
, db
);
2194 atomic_dec_32(&dn
->dn_dbufs_count
);
2198 mutex_exit(&dn
->dn_dbufs_mtx
);
2200 * Decrementing the dbuf count means that the hold corresponding
2201 * to the removed dbuf is no longer discounted in dnode_move(),
2202 * so the dnode cannot be moved until after we release the hold.
2203 * The membar_producer() ensures visibility of the decremented
2204 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
2208 db
->db_dnode_handle
= NULL
;
2210 dbuf_hash_remove(db
);
2215 ASSERT(refcount_is_zero(&db
->db_holds
));
2217 db
->db_parent
= NULL
;
2219 ASSERT(db
->db_buf
== NULL
);
2220 ASSERT(db
->db
.db_data
== NULL
);
2221 ASSERT(db
->db_hash_next
== NULL
);
2222 ASSERT(db
->db_blkptr
== NULL
);
2223 ASSERT(db
->db_data_pending
== NULL
);
2224 ASSERT(!multilist_link_active(&db
->db_cache_link
));
2226 kmem_cache_free(dbuf_kmem_cache
, db
);
2227 arc_space_return(sizeof (dmu_buf_impl_t
), ARC_SPACE_DBUF
);
2230 * If this dbuf is referenced from an indirect dbuf,
2231 * decrement the ref count on the indirect dbuf.
2233 if (parent
&& parent
!= dndb
)
2234 dbuf_rele(parent
, db
);
2238 * Note: While bpp will always be updated if the function returns success,
2239 * parentp will not be updated if the dnode does not have dn_dbuf filled in;
2240 * this happens when the dnode is the meta-dnode, or a userused or groupused
2243 __attribute__((always_inline
))
2245 dbuf_findbp(dnode_t
*dn
, int level
, uint64_t blkid
, int fail_sparse
,
2246 dmu_buf_impl_t
**parentp
, blkptr_t
**bpp
, struct dbuf_hold_impl_data
*dh
)
2253 ASSERT(blkid
!= DMU_BONUS_BLKID
);
2255 if (blkid
== DMU_SPILL_BLKID
) {
2256 mutex_enter(&dn
->dn_mtx
);
2257 if (dn
->dn_have_spill
&&
2258 (dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
))
2259 *bpp
= DN_SPILL_BLKPTR(dn
->dn_phys
);
2262 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
2263 *parentp
= dn
->dn_dbuf
;
2264 mutex_exit(&dn
->dn_mtx
);
2269 (dn
->dn_phys
->dn_nlevels
== 0) ? 1 : dn
->dn_phys
->dn_nlevels
;
2270 epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2272 ASSERT3U(level
* epbs
, <, 64);
2273 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2275 * This assertion shouldn't trip as long as the max indirect block size
2276 * is less than 1M. The reason for this is that up to that point,
2277 * the number of levels required to address an entire object with blocks
2278 * of size SPA_MINBLOCKSIZE satisfies nlevels * epbs + 1 <= 64. In
2279 * other words, if N * epbs + 1 > 64, then if (N-1) * epbs + 1 > 55
2280 * (i.e. we can address the entire object), objects will all use at most
2281 * N-1 levels and the assertion won't overflow. However, once epbs is
2282 * 13, 4 * 13 + 1 = 53, but 5 * 13 + 1 = 66. Then, 4 levels will not be
2283 * enough to address an entire object, so objects will have 5 levels,
2284 * but then this assertion will overflow.
2286 * All this is to say that if we ever increase DN_MAX_INDBLKSHIFT, we
2287 * need to redo this logic to handle overflows.
2289 ASSERT(level
>= nlevels
||
2290 ((nlevels
- level
- 1) * epbs
) +
2291 highbit64(dn
->dn_phys
->dn_nblkptr
) <= 64);
2292 if (level
>= nlevels
||
2293 blkid
>= ((uint64_t)dn
->dn_phys
->dn_nblkptr
<<
2294 ((nlevels
- level
- 1) * epbs
)) ||
2296 blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))) {
2297 /* the buffer has no parent yet */
2298 return (SET_ERROR(ENOENT
));
2299 } else if (level
< nlevels
-1) {
2300 /* this block is referenced from an indirect block */
2303 err
= dbuf_hold_impl(dn
, level
+1,
2304 blkid
>> epbs
, fail_sparse
, FALSE
, NULL
, parentp
);
2306 __dbuf_hold_impl_init(dh
+ 1, dn
, dh
->dh_level
+ 1,
2307 blkid
>> epbs
, fail_sparse
, FALSE
, NULL
,
2308 parentp
, dh
->dh_depth
+ 1);
2309 err
= __dbuf_hold_impl(dh
+ 1);
2313 err
= dbuf_read(*parentp
, NULL
,
2314 (DB_RF_HAVESTRUCT
| DB_RF_NOPREFETCH
| DB_RF_CANFAIL
));
2316 dbuf_rele(*parentp
, NULL
);
2320 *bpp
= ((blkptr_t
*)(*parentp
)->db
.db_data
) +
2321 (blkid
& ((1ULL << epbs
) - 1));
2322 if (blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))
2323 ASSERT(BP_IS_HOLE(*bpp
));
2326 /* the block is referenced from the dnode */
2327 ASSERT3U(level
, ==, nlevels
-1);
2328 ASSERT(dn
->dn_phys
->dn_nblkptr
== 0 ||
2329 blkid
< dn
->dn_phys
->dn_nblkptr
);
2331 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
2332 *parentp
= dn
->dn_dbuf
;
2334 *bpp
= &dn
->dn_phys
->dn_blkptr
[blkid
];
2339 static dmu_buf_impl_t
*
2340 dbuf_create(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
2341 dmu_buf_impl_t
*parent
, blkptr_t
*blkptr
)
2343 objset_t
*os
= dn
->dn_objset
;
2344 dmu_buf_impl_t
*db
, *odb
;
2346 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2347 ASSERT(dn
->dn_type
!= DMU_OT_NONE
);
2349 db
= kmem_cache_alloc(dbuf_kmem_cache
, KM_SLEEP
);
2352 db
->db
.db_object
= dn
->dn_object
;
2353 db
->db_level
= level
;
2354 db
->db_blkid
= blkid
;
2355 db
->db_last_dirty
= NULL
;
2356 db
->db_dirtycnt
= 0;
2357 db
->db_dnode_handle
= dn
->dn_handle
;
2358 db
->db_parent
= parent
;
2359 db
->db_blkptr
= blkptr
;
2362 db
->db_user_immediate_evict
= FALSE
;
2363 db
->db_freed_in_flight
= FALSE
;
2364 db
->db_pending_evict
= FALSE
;
2366 if (blkid
== DMU_BONUS_BLKID
) {
2367 ASSERT3P(parent
, ==, dn
->dn_dbuf
);
2368 db
->db
.db_size
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
) -
2369 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
);
2370 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
2371 db
->db
.db_offset
= DMU_BONUS_BLKID
;
2372 db
->db_state
= DB_UNCACHED
;
2373 /* the bonus dbuf is not placed in the hash table */
2374 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_DBUF
);
2376 } else if (blkid
== DMU_SPILL_BLKID
) {
2377 db
->db
.db_size
= (blkptr
!= NULL
) ?
2378 BP_GET_LSIZE(blkptr
) : SPA_MINBLOCKSIZE
;
2379 db
->db
.db_offset
= 0;
2382 db
->db_level
? 1 << dn
->dn_indblkshift
: dn
->dn_datablksz
;
2383 db
->db
.db_size
= blocksize
;
2384 db
->db
.db_offset
= db
->db_blkid
* blocksize
;
2388 * Hold the dn_dbufs_mtx while we get the new dbuf
2389 * in the hash table *and* added to the dbufs list.
2390 * This prevents a possible deadlock with someone
2391 * trying to look up this dbuf before its added to the
2394 mutex_enter(&dn
->dn_dbufs_mtx
);
2395 db
->db_state
= DB_EVICTING
;
2396 if ((odb
= dbuf_hash_insert(db
)) != NULL
) {
2397 /* someone else inserted it first */
2398 kmem_cache_free(dbuf_kmem_cache
, db
);
2399 mutex_exit(&dn
->dn_dbufs_mtx
);
2402 avl_add(&dn
->dn_dbufs
, db
);
2403 if (db
->db_level
== 0 && db
->db_blkid
>=
2404 dn
->dn_unlisted_l0_blkid
)
2405 dn
->dn_unlisted_l0_blkid
= db
->db_blkid
+ 1;
2406 db
->db_state
= DB_UNCACHED
;
2407 mutex_exit(&dn
->dn_dbufs_mtx
);
2408 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_DBUF
);
2410 if (parent
&& parent
!= dn
->dn_dbuf
)
2411 dbuf_add_ref(parent
, db
);
2413 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
2414 refcount_count(&dn
->dn_holds
) > 0);
2415 (void) refcount_add(&dn
->dn_holds
, db
);
2416 atomic_inc_32(&dn
->dn_dbufs_count
);
2418 dprintf_dbuf(db
, "db=%p\n", db
);
2423 typedef struct dbuf_prefetch_arg
{
2424 spa_t
*dpa_spa
; /* The spa to issue the prefetch in. */
2425 zbookmark_phys_t dpa_zb
; /* The target block to prefetch. */
2426 int dpa_epbs
; /* Entries (blkptr_t's) Per Block Shift. */
2427 int dpa_curlevel
; /* The current level that we're reading */
2428 dnode_t
*dpa_dnode
; /* The dnode associated with the prefetch */
2429 zio_priority_t dpa_prio
; /* The priority I/Os should be issued at. */
2430 zio_t
*dpa_zio
; /* The parent zio_t for all prefetches. */
2431 arc_flags_t dpa_aflags
; /* Flags to pass to the final prefetch. */
2432 } dbuf_prefetch_arg_t
;
2435 * Actually issue the prefetch read for the block given.
2438 dbuf_issue_final_prefetch(dbuf_prefetch_arg_t
*dpa
, blkptr_t
*bp
)
2441 if (BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
))
2444 aflags
= dpa
->dpa_aflags
| ARC_FLAG_NOWAIT
| ARC_FLAG_PREFETCH
;
2446 ASSERT3U(dpa
->dpa_curlevel
, ==, BP_GET_LEVEL(bp
));
2447 ASSERT3U(dpa
->dpa_curlevel
, ==, dpa
->dpa_zb
.zb_level
);
2448 ASSERT(dpa
->dpa_zio
!= NULL
);
2449 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
, bp
, NULL
, NULL
,
2450 dpa
->dpa_prio
, ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
2451 &aflags
, &dpa
->dpa_zb
);
2455 * Called when an indirect block above our prefetch target is read in. This
2456 * will either read in the next indirect block down the tree or issue the actual
2457 * prefetch if the next block down is our target.
2460 dbuf_prefetch_indirect_done(zio_t
*zio
, arc_buf_t
*abuf
, void *private)
2462 dbuf_prefetch_arg_t
*dpa
= private;
2466 ASSERT3S(dpa
->dpa_zb
.zb_level
, <, dpa
->dpa_curlevel
);
2467 ASSERT3S(dpa
->dpa_curlevel
, >, 0);
2470 * The dpa_dnode is only valid if we are called with a NULL
2471 * zio. This indicates that the arc_read() returned without
2472 * first calling zio_read() to issue a physical read. Once
2473 * a physical read is made the dpa_dnode must be invalidated
2474 * as the locks guarding it may have been dropped. If the
2475 * dpa_dnode is still valid, then we want to add it to the dbuf
2476 * cache. To do so, we must hold the dbuf associated with the block
2477 * we just prefetched, read its contents so that we associate it
2478 * with an arc_buf_t, and then release it.
2481 ASSERT3S(BP_GET_LEVEL(zio
->io_bp
), ==, dpa
->dpa_curlevel
);
2482 if (zio
->io_flags
& ZIO_FLAG_RAW
) {
2483 ASSERT3U(BP_GET_PSIZE(zio
->io_bp
), ==, zio
->io_size
);
2485 ASSERT3U(BP_GET_LSIZE(zio
->io_bp
), ==, zio
->io_size
);
2487 ASSERT3P(zio
->io_spa
, ==, dpa
->dpa_spa
);
2489 dpa
->dpa_dnode
= NULL
;
2490 } else if (dpa
->dpa_dnode
!= NULL
) {
2491 uint64_t curblkid
= dpa
->dpa_zb
.zb_blkid
>>
2492 (dpa
->dpa_epbs
* (dpa
->dpa_curlevel
-
2493 dpa
->dpa_zb
.zb_level
));
2494 dmu_buf_impl_t
*db
= dbuf_hold_level(dpa
->dpa_dnode
,
2495 dpa
->dpa_curlevel
, curblkid
, FTAG
);
2496 (void) dbuf_read(db
, NULL
,
2497 DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
| DB_RF_HAVESTRUCT
);
2498 dbuf_rele(db
, FTAG
);
2501 dpa
->dpa_curlevel
--;
2503 nextblkid
= dpa
->dpa_zb
.zb_blkid
>>
2504 (dpa
->dpa_epbs
* (dpa
->dpa_curlevel
- dpa
->dpa_zb
.zb_level
));
2505 bp
= ((blkptr_t
*)abuf
->b_data
) +
2506 P2PHASE(nextblkid
, 1ULL << dpa
->dpa_epbs
);
2507 if (BP_IS_HOLE(bp
) || (zio
!= NULL
&& zio
->io_error
!= 0)) {
2508 kmem_free(dpa
, sizeof (*dpa
));
2509 } else if (dpa
->dpa_curlevel
== dpa
->dpa_zb
.zb_level
) {
2510 ASSERT3U(nextblkid
, ==, dpa
->dpa_zb
.zb_blkid
);
2511 dbuf_issue_final_prefetch(dpa
, bp
);
2512 kmem_free(dpa
, sizeof (*dpa
));
2514 arc_flags_t iter_aflags
= ARC_FLAG_NOWAIT
;
2515 zbookmark_phys_t zb
;
2517 ASSERT3U(dpa
->dpa_curlevel
, ==, BP_GET_LEVEL(bp
));
2519 SET_BOOKMARK(&zb
, dpa
->dpa_zb
.zb_objset
,
2520 dpa
->dpa_zb
.zb_object
, dpa
->dpa_curlevel
, nextblkid
);
2522 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
,
2523 bp
, dbuf_prefetch_indirect_done
, dpa
, dpa
->dpa_prio
,
2524 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
2528 arc_buf_destroy(abuf
, private);
2532 * Issue prefetch reads for the given block on the given level. If the indirect
2533 * blocks above that block are not in memory, we will read them in
2534 * asynchronously. As a result, this call never blocks waiting for a read to
2538 dbuf_prefetch(dnode_t
*dn
, int64_t level
, uint64_t blkid
, zio_priority_t prio
,
2542 int epbs
, nlevels
, curlevel
;
2546 dbuf_prefetch_arg_t
*dpa
;
2549 ASSERT(blkid
!= DMU_BONUS_BLKID
);
2550 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2552 if (blkid
> dn
->dn_maxblkid
)
2555 if (dnode_block_freed(dn
, blkid
))
2559 * This dnode hasn't been written to disk yet, so there's nothing to
2562 nlevels
= dn
->dn_phys
->dn_nlevels
;
2563 if (level
>= nlevels
|| dn
->dn_phys
->dn_nblkptr
== 0)
2566 epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2567 if (dn
->dn_phys
->dn_maxblkid
< blkid
<< (epbs
* level
))
2570 db
= dbuf_find(dn
->dn_objset
, dn
->dn_object
,
2573 mutex_exit(&db
->db_mtx
);
2575 * This dbuf already exists. It is either CACHED, or
2576 * (we assume) about to be read or filled.
2582 * Find the closest ancestor (indirect block) of the target block
2583 * that is present in the cache. In this indirect block, we will
2584 * find the bp that is at curlevel, curblkid.
2588 while (curlevel
< nlevels
- 1) {
2589 int parent_level
= curlevel
+ 1;
2590 uint64_t parent_blkid
= curblkid
>> epbs
;
2593 if (dbuf_hold_impl(dn
, parent_level
, parent_blkid
,
2594 FALSE
, TRUE
, FTAG
, &db
) == 0) {
2595 blkptr_t
*bpp
= db
->db_buf
->b_data
;
2596 bp
= bpp
[P2PHASE(curblkid
, 1 << epbs
)];
2597 dbuf_rele(db
, FTAG
);
2601 curlevel
= parent_level
;
2602 curblkid
= parent_blkid
;
2605 if (curlevel
== nlevels
- 1) {
2606 /* No cached indirect blocks found. */
2607 ASSERT3U(curblkid
, <, dn
->dn_phys
->dn_nblkptr
);
2608 bp
= dn
->dn_phys
->dn_blkptr
[curblkid
];
2610 if (BP_IS_HOLE(&bp
))
2613 ASSERT3U(curlevel
, ==, BP_GET_LEVEL(&bp
));
2615 pio
= zio_root(dmu_objset_spa(dn
->dn_objset
), NULL
, NULL
,
2618 dpa
= kmem_zalloc(sizeof (*dpa
), KM_SLEEP
);
2619 ds
= dn
->dn_objset
->os_dsl_dataset
;
2620 SET_BOOKMARK(&dpa
->dpa_zb
, ds
!= NULL
? ds
->ds_object
: DMU_META_OBJSET
,
2621 dn
->dn_object
, level
, blkid
);
2622 dpa
->dpa_curlevel
= curlevel
;
2623 dpa
->dpa_prio
= prio
;
2624 dpa
->dpa_aflags
= aflags
;
2625 dpa
->dpa_spa
= dn
->dn_objset
->os_spa
;
2626 dpa
->dpa_dnode
= dn
;
2627 dpa
->dpa_epbs
= epbs
;
2631 * If we have the indirect just above us, no need to do the asynchronous
2632 * prefetch chain; we'll just run the last step ourselves. If we're at
2633 * a higher level, though, we want to issue the prefetches for all the
2634 * indirect blocks asynchronously, so we can go on with whatever we were
2637 if (curlevel
== level
) {
2638 ASSERT3U(curblkid
, ==, blkid
);
2639 dbuf_issue_final_prefetch(dpa
, &bp
);
2640 kmem_free(dpa
, sizeof (*dpa
));
2642 arc_flags_t iter_aflags
= ARC_FLAG_NOWAIT
;
2643 zbookmark_phys_t zb
;
2645 SET_BOOKMARK(&zb
, ds
!= NULL
? ds
->ds_object
: DMU_META_OBJSET
,
2646 dn
->dn_object
, curlevel
, curblkid
);
2647 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
,
2648 &bp
, dbuf_prefetch_indirect_done
, dpa
, prio
,
2649 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
2653 * We use pio here instead of dpa_zio since it's possible that
2654 * dpa may have already been freed.
2659 #define DBUF_HOLD_IMPL_MAX_DEPTH 20
2662 * Returns with db_holds incremented, and db_mtx not held.
2663 * Note: dn_struct_rwlock must be held.
2666 __dbuf_hold_impl(struct dbuf_hold_impl_data
*dh
)
2668 ASSERT3S(dh
->dh_depth
, <, DBUF_HOLD_IMPL_MAX_DEPTH
);
2669 dh
->dh_parent
= NULL
;
2671 ASSERT(dh
->dh_blkid
!= DMU_BONUS_BLKID
);
2672 ASSERT(RW_LOCK_HELD(&dh
->dh_dn
->dn_struct_rwlock
));
2673 ASSERT3U(dh
->dh_dn
->dn_nlevels
, >, dh
->dh_level
);
2675 *(dh
->dh_dbp
) = NULL
;
2677 /* dbuf_find() returns with db_mtx held */
2678 dh
->dh_db
= dbuf_find(dh
->dh_dn
->dn_objset
, dh
->dh_dn
->dn_object
,
2679 dh
->dh_level
, dh
->dh_blkid
);
2681 if (dh
->dh_db
== NULL
) {
2684 if (dh
->dh_fail_uncached
)
2685 return (SET_ERROR(ENOENT
));
2687 ASSERT3P(dh
->dh_parent
, ==, NULL
);
2688 dh
->dh_err
= dbuf_findbp(dh
->dh_dn
, dh
->dh_level
, dh
->dh_blkid
,
2689 dh
->dh_fail_sparse
, &dh
->dh_parent
, &dh
->dh_bp
, dh
);
2690 if (dh
->dh_fail_sparse
) {
2691 if (dh
->dh_err
== 0 &&
2692 dh
->dh_bp
&& BP_IS_HOLE(dh
->dh_bp
))
2693 dh
->dh_err
= SET_ERROR(ENOENT
);
2696 dbuf_rele(dh
->dh_parent
, NULL
);
2697 return (dh
->dh_err
);
2700 if (dh
->dh_err
&& dh
->dh_err
!= ENOENT
)
2701 return (dh
->dh_err
);
2702 dh
->dh_db
= dbuf_create(dh
->dh_dn
, dh
->dh_level
, dh
->dh_blkid
,
2703 dh
->dh_parent
, dh
->dh_bp
);
2706 if (dh
->dh_fail_uncached
&& dh
->dh_db
->db_state
!= DB_CACHED
) {
2707 mutex_exit(&dh
->dh_db
->db_mtx
);
2708 return (SET_ERROR(ENOENT
));
2711 if (dh
->dh_db
->db_buf
!= NULL
)
2712 ASSERT3P(dh
->dh_db
->db
.db_data
, ==, dh
->dh_db
->db_buf
->b_data
);
2714 ASSERT(dh
->dh_db
->db_buf
== NULL
|| arc_referenced(dh
->dh_db
->db_buf
));
2717 * If this buffer is currently syncing out, and we are are
2718 * still referencing it from db_data, we need to make a copy
2719 * of it in case we decide we want to dirty it again in this txg.
2721 if (dh
->dh_db
->db_level
== 0 &&
2722 dh
->dh_db
->db_blkid
!= DMU_BONUS_BLKID
&&
2723 dh
->dh_dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
2724 dh
->dh_db
->db_state
== DB_CACHED
&& dh
->dh_db
->db_data_pending
) {
2725 dh
->dh_dr
= dh
->dh_db
->db_data_pending
;
2727 if (dh
->dh_dr
->dt
.dl
.dr_data
== dh
->dh_db
->db_buf
) {
2728 dh
->dh_type
= DBUF_GET_BUFC_TYPE(dh
->dh_db
);
2730 dbuf_set_data(dh
->dh_db
,
2731 arc_alloc_buf(dh
->dh_dn
->dn_objset
->os_spa
,
2732 dh
->dh_db
, dh
->dh_type
, dh
->dh_db
->db
.db_size
));
2733 bcopy(dh
->dh_dr
->dt
.dl
.dr_data
->b_data
,
2734 dh
->dh_db
->db
.db_data
, dh
->dh_db
->db
.db_size
);
2738 if (multilist_link_active(&dh
->dh_db
->db_cache_link
)) {
2739 ASSERT(refcount_is_zero(&dh
->dh_db
->db_holds
));
2740 multilist_remove(&dbuf_cache
, dh
->dh_db
);
2741 (void) refcount_remove_many(&dbuf_cache_size
,
2742 dh
->dh_db
->db
.db_size
, dh
->dh_db
);
2744 (void) refcount_add(&dh
->dh_db
->db_holds
, dh
->dh_tag
);
2745 DBUF_VERIFY(dh
->dh_db
);
2746 mutex_exit(&dh
->dh_db
->db_mtx
);
2748 /* NOTE: we can't rele the parent until after we drop the db_mtx */
2750 dbuf_rele(dh
->dh_parent
, NULL
);
2752 ASSERT3P(DB_DNODE(dh
->dh_db
), ==, dh
->dh_dn
);
2753 ASSERT3U(dh
->dh_db
->db_blkid
, ==, dh
->dh_blkid
);
2754 ASSERT3U(dh
->dh_db
->db_level
, ==, dh
->dh_level
);
2755 *(dh
->dh_dbp
) = dh
->dh_db
;
2761 * The following code preserves the recursive function dbuf_hold_impl()
2762 * but moves the local variables AND function arguments to the heap to
2763 * minimize the stack frame size. Enough space is initially allocated
2764 * on the stack for 20 levels of recursion.
2767 dbuf_hold_impl(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
2768 boolean_t fail_sparse
, boolean_t fail_uncached
,
2769 void *tag
, dmu_buf_impl_t
**dbp
)
2771 struct dbuf_hold_impl_data
*dh
;
2774 dh
= kmem_alloc(sizeof (struct dbuf_hold_impl_data
) *
2775 DBUF_HOLD_IMPL_MAX_DEPTH
, KM_SLEEP
);
2776 __dbuf_hold_impl_init(dh
, dn
, level
, blkid
, fail_sparse
,
2777 fail_uncached
, tag
, dbp
, 0);
2779 error
= __dbuf_hold_impl(dh
);
2781 kmem_free(dh
, sizeof (struct dbuf_hold_impl_data
) *
2782 DBUF_HOLD_IMPL_MAX_DEPTH
);
2788 __dbuf_hold_impl_init(struct dbuf_hold_impl_data
*dh
,
2789 dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
2790 boolean_t fail_sparse
, boolean_t fail_uncached
,
2791 void *tag
, dmu_buf_impl_t
**dbp
, int depth
)
2794 dh
->dh_level
= level
;
2795 dh
->dh_blkid
= blkid
;
2797 dh
->dh_fail_sparse
= fail_sparse
;
2798 dh
->dh_fail_uncached
= fail_uncached
;
2804 dh
->dh_parent
= NULL
;
2810 dh
->dh_depth
= depth
;
2814 dbuf_hold(dnode_t
*dn
, uint64_t blkid
, void *tag
)
2816 return (dbuf_hold_level(dn
, 0, blkid
, tag
));
2820 dbuf_hold_level(dnode_t
*dn
, int level
, uint64_t blkid
, void *tag
)
2823 int err
= dbuf_hold_impl(dn
, level
, blkid
, FALSE
, FALSE
, tag
, &db
);
2824 return (err
? NULL
: db
);
2828 dbuf_create_bonus(dnode_t
*dn
)
2830 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
2832 ASSERT(dn
->dn_bonus
== NULL
);
2833 dn
->dn_bonus
= dbuf_create(dn
, 0, DMU_BONUS_BLKID
, dn
->dn_dbuf
, NULL
);
2837 dbuf_spill_set_blksz(dmu_buf_t
*db_fake
, uint64_t blksz
, dmu_tx_t
*tx
)
2839 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2842 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
2843 return (SET_ERROR(ENOTSUP
));
2845 blksz
= SPA_MINBLOCKSIZE
;
2846 ASSERT3U(blksz
, <=, spa_maxblocksize(dmu_objset_spa(db
->db_objset
)));
2847 blksz
= P2ROUNDUP(blksz
, SPA_MINBLOCKSIZE
);
2851 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
2852 dbuf_new_size(db
, blksz
, tx
);
2853 rw_exit(&dn
->dn_struct_rwlock
);
2860 dbuf_rm_spill(dnode_t
*dn
, dmu_tx_t
*tx
)
2862 dbuf_free_range(dn
, DMU_SPILL_BLKID
, DMU_SPILL_BLKID
, tx
);
2865 #pragma weak dmu_buf_add_ref = dbuf_add_ref
2867 dbuf_add_ref(dmu_buf_impl_t
*db
, void *tag
)
2869 int64_t holds
= refcount_add(&db
->db_holds
, tag
);
2870 VERIFY3S(holds
, >, 1);
2873 #pragma weak dmu_buf_try_add_ref = dbuf_try_add_ref
2875 dbuf_try_add_ref(dmu_buf_t
*db_fake
, objset_t
*os
, uint64_t obj
, uint64_t blkid
,
2878 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2879 dmu_buf_impl_t
*found_db
;
2880 boolean_t result
= B_FALSE
;
2882 if (blkid
== DMU_BONUS_BLKID
)
2883 found_db
= dbuf_find_bonus(os
, obj
);
2885 found_db
= dbuf_find(os
, obj
, 0, blkid
);
2887 if (found_db
!= NULL
) {
2888 if (db
== found_db
&& dbuf_refcount(db
) > db
->db_dirtycnt
) {
2889 (void) refcount_add(&db
->db_holds
, tag
);
2892 mutex_exit(&found_db
->db_mtx
);
2898 * If you call dbuf_rele() you had better not be referencing the dnode handle
2899 * unless you have some other direct or indirect hold on the dnode. (An indirect
2900 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
2901 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
2902 * dnode's parent dbuf evicting its dnode handles.
2905 dbuf_rele(dmu_buf_impl_t
*db
, void *tag
)
2907 mutex_enter(&db
->db_mtx
);
2908 dbuf_rele_and_unlock(db
, tag
);
2912 dmu_buf_rele(dmu_buf_t
*db
, void *tag
)
2914 dbuf_rele((dmu_buf_impl_t
*)db
, tag
);
2918 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
2919 * db_dirtycnt and db_holds to be updated atomically.
2922 dbuf_rele_and_unlock(dmu_buf_impl_t
*db
, void *tag
)
2926 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2930 * Remove the reference to the dbuf before removing its hold on the
2931 * dnode so we can guarantee in dnode_move() that a referenced bonus
2932 * buffer has a corresponding dnode hold.
2934 holds
= refcount_remove(&db
->db_holds
, tag
);
2938 * We can't freeze indirects if there is a possibility that they
2939 * may be modified in the current syncing context.
2941 if (db
->db_buf
!= NULL
&&
2942 holds
== (db
->db_level
== 0 ? db
->db_dirtycnt
: 0)) {
2943 arc_buf_freeze(db
->db_buf
);
2946 if (holds
== db
->db_dirtycnt
&&
2947 db
->db_level
== 0 && db
->db_user_immediate_evict
)
2948 dbuf_evict_user(db
);
2951 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2953 boolean_t evict_dbuf
= db
->db_pending_evict
;
2956 * If the dnode moves here, we cannot cross this
2957 * barrier until the move completes.
2962 atomic_dec_32(&dn
->dn_dbufs_count
);
2965 * Decrementing the dbuf count means that the bonus
2966 * buffer's dnode hold is no longer discounted in
2967 * dnode_move(). The dnode cannot move until after
2968 * the dnode_rele() below.
2973 * Do not reference db after its lock is dropped.
2974 * Another thread may evict it.
2976 mutex_exit(&db
->db_mtx
);
2979 dnode_evict_bonus(dn
);
2982 } else if (db
->db_buf
== NULL
) {
2984 * This is a special case: we never associated this
2985 * dbuf with any data allocated from the ARC.
2987 ASSERT(db
->db_state
== DB_UNCACHED
||
2988 db
->db_state
== DB_NOFILL
);
2990 } else if (arc_released(db
->db_buf
)) {
2992 * This dbuf has anonymous data associated with it.
2996 boolean_t do_arc_evict
= B_FALSE
;
2998 spa_t
*spa
= dmu_objset_spa(db
->db_objset
);
3000 if (!DBUF_IS_CACHEABLE(db
) &&
3001 db
->db_blkptr
!= NULL
&&
3002 !BP_IS_HOLE(db
->db_blkptr
) &&
3003 !BP_IS_EMBEDDED(db
->db_blkptr
)) {
3004 do_arc_evict
= B_TRUE
;
3005 bp
= *db
->db_blkptr
;
3008 if (!DBUF_IS_CACHEABLE(db
) ||
3009 db
->db_pending_evict
) {
3011 } else if (!multilist_link_active(&db
->db_cache_link
)) {
3012 multilist_insert(&dbuf_cache
, db
);
3013 (void) refcount_add_many(&dbuf_cache_size
,
3014 db
->db
.db_size
, db
);
3015 mutex_exit(&db
->db_mtx
);
3017 dbuf_evict_notify();
3021 arc_freed(spa
, &bp
);
3024 mutex_exit(&db
->db_mtx
);
3029 #pragma weak dmu_buf_refcount = dbuf_refcount
3031 dbuf_refcount(dmu_buf_impl_t
*db
)
3033 return (refcount_count(&db
->db_holds
));
3037 dmu_buf_replace_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*old_user
,
3038 dmu_buf_user_t
*new_user
)
3040 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3042 mutex_enter(&db
->db_mtx
);
3043 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
3044 if (db
->db_user
== old_user
)
3045 db
->db_user
= new_user
;
3047 old_user
= db
->db_user
;
3048 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
3049 mutex_exit(&db
->db_mtx
);
3055 dmu_buf_set_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
3057 return (dmu_buf_replace_user(db_fake
, NULL
, user
));
3061 dmu_buf_set_user_ie(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
3063 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3065 db
->db_user_immediate_evict
= TRUE
;
3066 return (dmu_buf_set_user(db_fake
, user
));
3070 dmu_buf_remove_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
3072 return (dmu_buf_replace_user(db_fake
, user
, NULL
));
3076 dmu_buf_get_user(dmu_buf_t
*db_fake
)
3078 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3080 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
3081 return (db
->db_user
);
3085 dmu_buf_user_evict_wait()
3087 taskq_wait(dbu_evict_taskq
);
3091 dmu_buf_freeable(dmu_buf_t
*dbuf
)
3093 boolean_t res
= B_FALSE
;
3094 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbuf
;
3097 res
= dsl_dataset_block_freeable(db
->db_objset
->os_dsl_dataset
,
3098 db
->db_blkptr
, db
->db_blkptr
->blk_birth
);
3104 dmu_buf_get_blkptr(dmu_buf_t
*db
)
3106 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3107 return (dbi
->db_blkptr
);
3111 dmu_buf_get_objset(dmu_buf_t
*db
)
3113 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3114 return (dbi
->db_objset
);
3118 dmu_buf_dnode_enter(dmu_buf_t
*db
)
3120 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3121 DB_DNODE_ENTER(dbi
);
3122 return (DB_DNODE(dbi
));
3126 dmu_buf_dnode_exit(dmu_buf_t
*db
)
3128 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3133 dbuf_check_blkptr(dnode_t
*dn
, dmu_buf_impl_t
*db
)
3135 /* ASSERT(dmu_tx_is_syncing(tx) */
3136 ASSERT(MUTEX_HELD(&db
->db_mtx
));
3138 if (db
->db_blkptr
!= NULL
)
3141 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3142 db
->db_blkptr
= DN_SPILL_BLKPTR(dn
->dn_phys
);
3143 BP_ZERO(db
->db_blkptr
);
3146 if (db
->db_level
== dn
->dn_phys
->dn_nlevels
-1) {
3148 * This buffer was allocated at a time when there was
3149 * no available blkptrs from the dnode, or it was
3150 * inappropriate to hook it in (i.e., nlevels mis-match).
3152 ASSERT(db
->db_blkid
< dn
->dn_phys
->dn_nblkptr
);
3153 ASSERT(db
->db_parent
== NULL
);
3154 db
->db_parent
= dn
->dn_dbuf
;
3155 db
->db_blkptr
= &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
];
3158 dmu_buf_impl_t
*parent
= db
->db_parent
;
3159 int epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
3161 ASSERT(dn
->dn_phys
->dn_nlevels
> 1);
3162 if (parent
== NULL
) {
3163 mutex_exit(&db
->db_mtx
);
3164 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
3165 parent
= dbuf_hold_level(dn
, db
->db_level
+ 1,
3166 db
->db_blkid
>> epbs
, db
);
3167 rw_exit(&dn
->dn_struct_rwlock
);
3168 mutex_enter(&db
->db_mtx
);
3169 db
->db_parent
= parent
;
3171 db
->db_blkptr
= (blkptr_t
*)parent
->db
.db_data
+
3172 (db
->db_blkid
& ((1ULL << epbs
) - 1));
3178 * dbuf_sync_indirect() is called recursively from dbuf_sync_list() so it
3179 * is critical the we not allow the compiler to inline this function in to
3180 * dbuf_sync_list() thereby drastically bloating the stack usage.
3182 noinline
static void
3183 dbuf_sync_indirect(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
3185 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3189 ASSERT(dmu_tx_is_syncing(tx
));
3191 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
3193 mutex_enter(&db
->db_mtx
);
3195 ASSERT(db
->db_level
> 0);
3198 /* Read the block if it hasn't been read yet. */
3199 if (db
->db_buf
== NULL
) {
3200 mutex_exit(&db
->db_mtx
);
3201 (void) dbuf_read(db
, NULL
, DB_RF_MUST_SUCCEED
);
3202 mutex_enter(&db
->db_mtx
);
3204 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
3205 ASSERT(db
->db_buf
!= NULL
);
3209 /* Indirect block size must match what the dnode thinks it is. */
3210 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
3211 dbuf_check_blkptr(dn
, db
);
3214 /* Provide the pending dirty record to child dbufs */
3215 db
->db_data_pending
= dr
;
3217 mutex_exit(&db
->db_mtx
);
3218 dbuf_write(dr
, db
->db_buf
, tx
);
3221 mutex_enter(&dr
->dt
.di
.dr_mtx
);
3222 dbuf_sync_list(&dr
->dt
.di
.dr_children
, db
->db_level
- 1, tx
);
3223 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
3224 mutex_exit(&dr
->dt
.di
.dr_mtx
);
3229 * dbuf_sync_leaf() is called recursively from dbuf_sync_list() so it is
3230 * critical the we not allow the compiler to inline this function in to
3231 * dbuf_sync_list() thereby drastically bloating the stack usage.
3233 noinline
static void
3234 dbuf_sync_leaf(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
3236 arc_buf_t
**datap
= &dr
->dt
.dl
.dr_data
;
3237 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3240 uint64_t txg
= tx
->tx_txg
;
3242 ASSERT(dmu_tx_is_syncing(tx
));
3244 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
3246 mutex_enter(&db
->db_mtx
);
3248 * To be synced, we must be dirtied. But we
3249 * might have been freed after the dirty.
3251 if (db
->db_state
== DB_UNCACHED
) {
3252 /* This buffer has been freed since it was dirtied */
3253 ASSERT(db
->db
.db_data
== NULL
);
3254 } else if (db
->db_state
== DB_FILL
) {
3255 /* This buffer was freed and is now being re-filled */
3256 ASSERT(db
->db
.db_data
!= dr
->dt
.dl
.dr_data
);
3258 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_NOFILL
);
3265 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3266 mutex_enter(&dn
->dn_mtx
);
3267 if (!(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
)) {
3269 * In the previous transaction group, the bonus buffer
3270 * was entirely used to store the attributes for the
3271 * dnode which overrode the dn_spill field. However,
3272 * when adding more attributes to the file a spill
3273 * block was required to hold the extra attributes.
3275 * Make sure to clear the garbage left in the dn_spill
3276 * field from the previous attributes in the bonus
3277 * buffer. Otherwise, after writing out the spill
3278 * block to the new allocated dva, it will free
3279 * the old block pointed to by the invalid dn_spill.
3281 db
->db_blkptr
= NULL
;
3283 dn
->dn_phys
->dn_flags
|= DNODE_FLAG_SPILL_BLKPTR
;
3284 mutex_exit(&dn
->dn_mtx
);
3288 * If this is a bonus buffer, simply copy the bonus data into the
3289 * dnode. It will be written out when the dnode is synced (and it
3290 * will be synced, since it must have been dirty for dbuf_sync to
3293 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
3294 dbuf_dirty_record_t
**drp
;
3296 ASSERT(*datap
!= NULL
);
3297 ASSERT0(db
->db_level
);
3298 ASSERT3U(dn
->dn_phys
->dn_bonuslen
, <=,
3299 DN_SLOTS_TO_BONUSLEN(dn
->dn_phys
->dn_extra_slots
+ 1));
3300 bcopy(*datap
, DN_BONUS(dn
->dn_phys
), dn
->dn_phys
->dn_bonuslen
);
3303 if (*datap
!= db
->db
.db_data
) {
3304 int slots
= DB_DNODE(db
)->dn_num_slots
;
3305 int bonuslen
= DN_SLOTS_TO_BONUSLEN(slots
);
3306 kmem_free(*datap
, bonuslen
);
3307 arc_space_return(bonuslen
, ARC_SPACE_BONUS
);
3309 db
->db_data_pending
= NULL
;
3310 drp
= &db
->db_last_dirty
;
3312 drp
= &(*drp
)->dr_next
;
3313 ASSERT(dr
->dr_next
== NULL
);
3314 ASSERT(dr
->dr_dbuf
== db
);
3316 if (dr
->dr_dbuf
->db_level
!= 0) {
3317 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
3318 list_destroy(&dr
->dt
.di
.dr_children
);
3320 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
3321 ASSERT(db
->db_dirtycnt
> 0);
3322 db
->db_dirtycnt
-= 1;
3323 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)txg
);
3330 * This function may have dropped the db_mtx lock allowing a dmu_sync
3331 * operation to sneak in. As a result, we need to ensure that we
3332 * don't check the dr_override_state until we have returned from
3333 * dbuf_check_blkptr.
3335 dbuf_check_blkptr(dn
, db
);
3338 * If this buffer is in the middle of an immediate write,
3339 * wait for the synchronous IO to complete.
3341 while (dr
->dt
.dl
.dr_override_state
== DR_IN_DMU_SYNC
) {
3342 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
3343 cv_wait(&db
->db_changed
, &db
->db_mtx
);
3344 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_NOT_OVERRIDDEN
);
3347 if (db
->db_state
!= DB_NOFILL
&&
3348 dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
3349 refcount_count(&db
->db_holds
) > 1 &&
3350 dr
->dt
.dl
.dr_override_state
!= DR_OVERRIDDEN
&&
3351 *datap
== db
->db_buf
) {
3353 * If this buffer is currently "in use" (i.e., there
3354 * are active holds and db_data still references it),
3355 * then make a copy before we start the write so that
3356 * any modifications from the open txg will not leak
3359 * NOTE: this copy does not need to be made for
3360 * objects only modified in the syncing context (e.g.
3361 * DNONE_DNODE blocks).
3363 int psize
= arc_buf_size(*datap
);
3364 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
3365 enum zio_compress compress_type
= arc_get_compression(*datap
);
3367 if (compress_type
== ZIO_COMPRESS_OFF
) {
3368 *datap
= arc_alloc_buf(os
->os_spa
, db
, type
, psize
);
3370 int lsize
= arc_buf_lsize(*datap
);
3371 ASSERT3U(type
, ==, ARC_BUFC_DATA
);
3372 *datap
= arc_alloc_compressed_buf(os
->os_spa
, db
,
3373 psize
, lsize
, compress_type
);
3375 bcopy(db
->db
.db_data
, (*datap
)->b_data
, psize
);
3377 db
->db_data_pending
= dr
;
3379 mutex_exit(&db
->db_mtx
);
3381 dbuf_write(dr
, *datap
, tx
);
3383 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
3384 if (dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
3385 list_insert_tail(&dn
->dn_dirty_records
[txg
&TXG_MASK
], dr
);
3389 * Although zio_nowait() does not "wait for an IO", it does
3390 * initiate the IO. If this is an empty write it seems plausible
3391 * that the IO could actually be completed before the nowait
3392 * returns. We need to DB_DNODE_EXIT() first in case
3393 * zio_nowait() invalidates the dbuf.
3396 zio_nowait(dr
->dr_zio
);
3401 dbuf_sync_list(list_t
*list
, int level
, dmu_tx_t
*tx
)
3403 dbuf_dirty_record_t
*dr
;
3405 while ((dr
= list_head(list
))) {
3406 if (dr
->dr_zio
!= NULL
) {
3408 * If we find an already initialized zio then we
3409 * are processing the meta-dnode, and we have finished.
3410 * The dbufs for all dnodes are put back on the list
3411 * during processing, so that we can zio_wait()
3412 * these IOs after initiating all child IOs.
3414 ASSERT3U(dr
->dr_dbuf
->db
.db_object
, ==,
3415 DMU_META_DNODE_OBJECT
);
3418 if (dr
->dr_dbuf
->db_blkid
!= DMU_BONUS_BLKID
&&
3419 dr
->dr_dbuf
->db_blkid
!= DMU_SPILL_BLKID
) {
3420 VERIFY3U(dr
->dr_dbuf
->db_level
, ==, level
);
3422 list_remove(list
, dr
);
3423 if (dr
->dr_dbuf
->db_level
> 0)
3424 dbuf_sync_indirect(dr
, tx
);
3426 dbuf_sync_leaf(dr
, tx
);
3432 dbuf_write_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
3434 dmu_buf_impl_t
*db
= vdb
;
3436 blkptr_t
*bp
= zio
->io_bp
;
3437 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
3438 spa_t
*spa
= zio
->io_spa
;
3443 ASSERT3P(db
->db_blkptr
, !=, NULL
);
3444 ASSERT3P(&db
->db_data_pending
->dr_bp_copy
, ==, bp
);
3448 delta
= bp_get_dsize_sync(spa
, bp
) - bp_get_dsize_sync(spa
, bp_orig
);
3449 dnode_diduse_space(dn
, delta
- zio
->io_prev_space_delta
);
3450 zio
->io_prev_space_delta
= delta
;
3452 if (bp
->blk_birth
!= 0) {
3453 ASSERT((db
->db_blkid
!= DMU_SPILL_BLKID
&&
3454 BP_GET_TYPE(bp
) == dn
->dn_type
) ||
3455 (db
->db_blkid
== DMU_SPILL_BLKID
&&
3456 BP_GET_TYPE(bp
) == dn
->dn_bonustype
) ||
3457 BP_IS_EMBEDDED(bp
));
3458 ASSERT(BP_GET_LEVEL(bp
) == db
->db_level
);
3461 mutex_enter(&db
->db_mtx
);
3464 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3465 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
3466 ASSERT(!(BP_IS_HOLE(bp
)) &&
3467 db
->db_blkptr
== DN_SPILL_BLKPTR(dn
->dn_phys
));
3471 if (db
->db_level
== 0) {
3472 mutex_enter(&dn
->dn_mtx
);
3473 if (db
->db_blkid
> dn
->dn_phys
->dn_maxblkid
&&
3474 db
->db_blkid
!= DMU_SPILL_BLKID
)
3475 dn
->dn_phys
->dn_maxblkid
= db
->db_blkid
;
3476 mutex_exit(&dn
->dn_mtx
);
3478 if (dn
->dn_type
== DMU_OT_DNODE
) {
3480 while (i
< db
->db
.db_size
) {
3481 dnode_phys_t
*dnp
= db
->db
.db_data
+ i
;
3483 i
+= DNODE_MIN_SIZE
;
3484 if (dnp
->dn_type
!= DMU_OT_NONE
) {
3486 i
+= dnp
->dn_extra_slots
*
3491 if (BP_IS_HOLE(bp
)) {
3498 blkptr_t
*ibp
= db
->db
.db_data
;
3499 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
3500 for (i
= db
->db
.db_size
>> SPA_BLKPTRSHIFT
; i
> 0; i
--, ibp
++) {
3501 if (BP_IS_HOLE(ibp
))
3503 fill
+= BP_GET_FILL(ibp
);
3508 if (!BP_IS_EMBEDDED(bp
))
3509 bp
->blk_fill
= fill
;
3511 mutex_exit(&db
->db_mtx
);
3513 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
3514 *db
->db_blkptr
= *bp
;
3515 rw_exit(&dn
->dn_struct_rwlock
);
3520 * This function gets called just prior to running through the compression
3521 * stage of the zio pipeline. If we're an indirect block comprised of only
3522 * holes, then we want this indirect to be compressed away to a hole. In
3523 * order to do that we must zero out any information about the holes that
3524 * this indirect points to prior to before we try to compress it.
3527 dbuf_write_children_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
3529 dmu_buf_impl_t
*db
= vdb
;
3535 ASSERT3U(db
->db_level
, >, 0);
3538 epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
3540 /* Determine if all our children are holes */
3541 for (i
= 0, bp
= db
->db
.db_data
; i
< 1ULL << epbs
; i
++, bp
++) {
3542 if (!BP_IS_HOLE(bp
))
3547 * If all the children are holes, then zero them all out so that
3548 * we may get compressed away.
3550 if (i
== 1ULL << epbs
) {
3551 /* didn't find any non-holes */
3552 bzero(db
->db
.db_data
, db
->db
.db_size
);
3558 * The SPA will call this callback several times for each zio - once
3559 * for every physical child i/o (zio->io_phys_children times). This
3560 * allows the DMU to monitor the progress of each logical i/o. For example,
3561 * there may be 2 copies of an indirect block, or many fragments of a RAID-Z
3562 * block. There may be a long delay before all copies/fragments are completed,
3563 * so this callback allows us to retire dirty space gradually, as the physical
3568 dbuf_write_physdone(zio_t
*zio
, arc_buf_t
*buf
, void *arg
)
3570 dmu_buf_impl_t
*db
= arg
;
3571 objset_t
*os
= db
->db_objset
;
3572 dsl_pool_t
*dp
= dmu_objset_pool(os
);
3573 dbuf_dirty_record_t
*dr
;
3576 dr
= db
->db_data_pending
;
3577 ASSERT3U(dr
->dr_txg
, ==, zio
->io_txg
);
3580 * The callback will be called io_phys_children times. Retire one
3581 * portion of our dirty space each time we are called. Any rounding
3582 * error will be cleaned up by dsl_pool_sync()'s call to
3583 * dsl_pool_undirty_space().
3585 delta
= dr
->dr_accounted
/ zio
->io_phys_children
;
3586 dsl_pool_undirty_space(dp
, delta
, zio
->io_txg
);
3591 dbuf_write_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
3593 dmu_buf_impl_t
*db
= vdb
;
3594 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
3595 blkptr_t
*bp
= db
->db_blkptr
;
3596 objset_t
*os
= db
->db_objset
;
3597 dmu_tx_t
*tx
= os
->os_synctx
;
3598 dbuf_dirty_record_t
**drp
, *dr
;
3600 ASSERT0(zio
->io_error
);
3601 ASSERT(db
->db_blkptr
== bp
);
3604 * For nopwrites and rewrites we ensure that the bp matches our
3605 * original and bypass all the accounting.
3607 if (zio
->io_flags
& (ZIO_FLAG_IO_REWRITE
| ZIO_FLAG_NOPWRITE
)) {
3608 ASSERT(BP_EQUAL(bp
, bp_orig
));
3610 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
3611 (void) dsl_dataset_block_kill(ds
, bp_orig
, tx
, B_TRUE
);
3612 dsl_dataset_block_born(ds
, bp
, tx
);
3615 mutex_enter(&db
->db_mtx
);
3619 drp
= &db
->db_last_dirty
;
3620 while ((dr
= *drp
) != db
->db_data_pending
)
3622 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
3623 ASSERT(dr
->dr_dbuf
== db
);
3624 ASSERT(dr
->dr_next
== NULL
);
3628 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3633 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
3634 ASSERT(!(BP_IS_HOLE(db
->db_blkptr
)) &&
3635 db
->db_blkptr
== DN_SPILL_BLKPTR(dn
->dn_phys
));
3640 if (db
->db_level
== 0) {
3641 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
3642 ASSERT(dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
);
3643 if (db
->db_state
!= DB_NOFILL
) {
3644 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
3645 arc_buf_destroy(dr
->dt
.dl
.dr_data
, db
);
3652 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
3653 ASSERT3U(db
->db
.db_size
, ==, 1 << dn
->dn_phys
->dn_indblkshift
);
3654 if (!BP_IS_HOLE(db
->db_blkptr
)) {
3655 ASSERTV(int epbs
= dn
->dn_phys
->dn_indblkshift
-
3657 ASSERT3U(db
->db_blkid
, <=,
3658 dn
->dn_phys
->dn_maxblkid
>> (db
->db_level
* epbs
));
3659 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==,
3663 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
3664 list_destroy(&dr
->dt
.di
.dr_children
);
3666 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
3668 cv_broadcast(&db
->db_changed
);
3669 ASSERT(db
->db_dirtycnt
> 0);
3670 db
->db_dirtycnt
-= 1;
3671 db
->db_data_pending
= NULL
;
3672 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)tx
->tx_txg
);
3676 dbuf_write_nofill_ready(zio_t
*zio
)
3678 dbuf_write_ready(zio
, NULL
, zio
->io_private
);
3682 dbuf_write_nofill_done(zio_t
*zio
)
3684 dbuf_write_done(zio
, NULL
, zio
->io_private
);
3688 dbuf_write_override_ready(zio_t
*zio
)
3690 dbuf_dirty_record_t
*dr
= zio
->io_private
;
3691 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3693 dbuf_write_ready(zio
, NULL
, db
);
3697 dbuf_write_override_done(zio_t
*zio
)
3699 dbuf_dirty_record_t
*dr
= zio
->io_private
;
3700 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3701 blkptr_t
*obp
= &dr
->dt
.dl
.dr_overridden_by
;
3703 mutex_enter(&db
->db_mtx
);
3704 if (!BP_EQUAL(zio
->io_bp
, obp
)) {
3705 if (!BP_IS_HOLE(obp
))
3706 dsl_free(spa_get_dsl(zio
->io_spa
), zio
->io_txg
, obp
);
3707 arc_release(dr
->dt
.dl
.dr_data
, db
);
3709 mutex_exit(&db
->db_mtx
);
3711 dbuf_write_done(zio
, NULL
, db
);
3713 if (zio
->io_abd
!= NULL
)
3714 abd_put(zio
->io_abd
);
3717 /* Issue I/O to commit a dirty buffer to disk. */
3719 dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
)
3721 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3724 dmu_buf_impl_t
*parent
= db
->db_parent
;
3725 uint64_t txg
= tx
->tx_txg
;
3726 zbookmark_phys_t zb
;
3731 ASSERT(dmu_tx_is_syncing(tx
));
3737 if (db
->db_state
!= DB_NOFILL
) {
3738 if (db
->db_level
> 0 || dn
->dn_type
== DMU_OT_DNODE
) {
3740 * Private object buffers are released here rather
3741 * than in dbuf_dirty() since they are only modified
3742 * in the syncing context and we don't want the
3743 * overhead of making multiple copies of the data.
3745 if (BP_IS_HOLE(db
->db_blkptr
)) {
3748 dbuf_release_bp(db
);
3753 if (parent
!= dn
->dn_dbuf
) {
3754 /* Our parent is an indirect block. */
3755 /* We have a dirty parent that has been scheduled for write. */
3756 ASSERT(parent
&& parent
->db_data_pending
);
3757 /* Our parent's buffer is one level closer to the dnode. */
3758 ASSERT(db
->db_level
== parent
->db_level
-1);
3760 * We're about to modify our parent's db_data by modifying
3761 * our block pointer, so the parent must be released.
3763 ASSERT(arc_released(parent
->db_buf
));
3764 zio
= parent
->db_data_pending
->dr_zio
;
3766 /* Our parent is the dnode itself. */
3767 ASSERT((db
->db_level
== dn
->dn_phys
->dn_nlevels
-1 &&
3768 db
->db_blkid
!= DMU_SPILL_BLKID
) ||
3769 (db
->db_blkid
== DMU_SPILL_BLKID
&& db
->db_level
== 0));
3770 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
3771 ASSERT3P(db
->db_blkptr
, ==,
3772 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
3776 ASSERT(db
->db_level
== 0 || data
== db
->db_buf
);
3777 ASSERT3U(db
->db_blkptr
->blk_birth
, <=, txg
);
3780 SET_BOOKMARK(&zb
, os
->os_dsl_dataset
?
3781 os
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
3782 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
3784 if (db
->db_blkid
== DMU_SPILL_BLKID
)
3786 wp_flag
|= (db
->db_state
== DB_NOFILL
) ? WP_NOFILL
: 0;
3788 dmu_write_policy(os
, dn
, db
->db_level
, wp_flag
,
3789 (data
!= NULL
&& arc_get_compression(data
) != ZIO_COMPRESS_OFF
) ?
3790 arc_get_compression(data
) : ZIO_COMPRESS_INHERIT
, &zp
);
3794 * We copy the blkptr now (rather than when we instantiate the dirty
3795 * record), because its value can change between open context and
3796 * syncing context. We do not need to hold dn_struct_rwlock to read
3797 * db_blkptr because we are in syncing context.
3799 dr
->dr_bp_copy
= *db
->db_blkptr
;
3801 if (db
->db_level
== 0 &&
3802 dr
->dt
.dl
.dr_override_state
== DR_OVERRIDDEN
) {
3804 * The BP for this block has been provided by open context
3805 * (by dmu_sync() or dmu_buf_write_embedded()).
3807 abd_t
*contents
= (data
!= NULL
) ?
3808 abd_get_from_buf(data
->b_data
, arc_buf_size(data
)) : NULL
;
3810 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
,
3811 &dr
->dr_bp_copy
, contents
, db
->db
.db_size
, db
->db
.db_size
,
3812 &zp
, dbuf_write_override_ready
, NULL
, NULL
,
3813 dbuf_write_override_done
,
3814 dr
, ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
3815 mutex_enter(&db
->db_mtx
);
3816 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
3817 zio_write_override(dr
->dr_zio
, &dr
->dt
.dl
.dr_overridden_by
,
3818 dr
->dt
.dl
.dr_copies
, dr
->dt
.dl
.dr_nopwrite
);
3819 mutex_exit(&db
->db_mtx
);
3820 } else if (db
->db_state
== DB_NOFILL
) {
3821 ASSERT(zp
.zp_checksum
== ZIO_CHECKSUM_OFF
||
3822 zp
.zp_checksum
== ZIO_CHECKSUM_NOPARITY
);
3823 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
,
3824 &dr
->dr_bp_copy
, NULL
, db
->db
.db_size
, db
->db
.db_size
, &zp
,
3825 dbuf_write_nofill_ready
, NULL
, NULL
,
3826 dbuf_write_nofill_done
, db
,
3827 ZIO_PRIORITY_ASYNC_WRITE
,
3828 ZIO_FLAG_MUSTSUCCEED
| ZIO_FLAG_NODATA
, &zb
);
3830 arc_done_func_t
*children_ready_cb
= NULL
;
3831 ASSERT(arc_released(data
));
3834 * For indirect blocks, we want to setup the children
3835 * ready callback so that we can properly handle an indirect
3836 * block that only contains holes.
3838 if (db
->db_level
!= 0)
3839 children_ready_cb
= dbuf_write_children_ready
;
3841 dr
->dr_zio
= arc_write(zio
, os
->os_spa
, txg
,
3842 &dr
->dr_bp_copy
, data
, DBUF_IS_L2CACHEABLE(db
),
3843 &zp
, dbuf_write_ready
,
3844 children_ready_cb
, dbuf_write_physdone
,
3845 dbuf_write_done
, db
, ZIO_PRIORITY_ASYNC_WRITE
,
3846 ZIO_FLAG_MUSTSUCCEED
, &zb
);
3850 #if defined(_KERNEL) && defined(HAVE_SPL)
3851 EXPORT_SYMBOL(dbuf_find
);
3852 EXPORT_SYMBOL(dbuf_is_metadata
);
3853 EXPORT_SYMBOL(dbuf_destroy
);
3854 EXPORT_SYMBOL(dbuf_loan_arcbuf
);
3855 EXPORT_SYMBOL(dbuf_whichblock
);
3856 EXPORT_SYMBOL(dbuf_read
);
3857 EXPORT_SYMBOL(dbuf_unoverride
);
3858 EXPORT_SYMBOL(dbuf_free_range
);
3859 EXPORT_SYMBOL(dbuf_new_size
);
3860 EXPORT_SYMBOL(dbuf_release_bp
);
3861 EXPORT_SYMBOL(dbuf_dirty
);
3862 EXPORT_SYMBOL(dmu_buf_will_dirty
);
3863 EXPORT_SYMBOL(dmu_buf_will_not_fill
);
3864 EXPORT_SYMBOL(dmu_buf_will_fill
);
3865 EXPORT_SYMBOL(dmu_buf_fill_done
);
3866 EXPORT_SYMBOL(dmu_buf_rele
);
3867 EXPORT_SYMBOL(dbuf_assign_arcbuf
);
3868 EXPORT_SYMBOL(dbuf_prefetch
);
3869 EXPORT_SYMBOL(dbuf_hold_impl
);
3870 EXPORT_SYMBOL(dbuf_hold
);
3871 EXPORT_SYMBOL(dbuf_hold_level
);
3872 EXPORT_SYMBOL(dbuf_create_bonus
);
3873 EXPORT_SYMBOL(dbuf_spill_set_blksz
);
3874 EXPORT_SYMBOL(dbuf_rm_spill
);
3875 EXPORT_SYMBOL(dbuf_add_ref
);
3876 EXPORT_SYMBOL(dbuf_rele
);
3877 EXPORT_SYMBOL(dbuf_rele_and_unlock
);
3878 EXPORT_SYMBOL(dbuf_refcount
);
3879 EXPORT_SYMBOL(dbuf_sync_list
);
3880 EXPORT_SYMBOL(dmu_buf_set_user
);
3881 EXPORT_SYMBOL(dmu_buf_set_user_ie
);
3882 EXPORT_SYMBOL(dmu_buf_get_user
);
3883 EXPORT_SYMBOL(dmu_buf_freeable
);
3884 EXPORT_SYMBOL(dmu_buf_get_blkptr
);
3887 module_param(dbuf_cache_max_bytes
, ulong
, 0644);
3888 MODULE_PARM_DESC(dbuf_cache_max_bytes
,
3889 "Maximum size in bytes of the dbuf cache.");
3891 module_param(dbuf_cache_hiwater_pct
, uint
, 0644);
3892 MODULE_PARM_DESC(dbuf_cache_hiwater_pct
,
3893 "Percentage over dbuf_cache_max_bytes when dbufs must be evicted "
3896 module_param(dbuf_cache_lowater_pct
, uint
, 0644);
3897 MODULE_PARM_DESC(dbuf_cache_lowater_pct
,
3898 "Percentage below dbuf_cache_max_bytes when the evict thread stops "
3901 module_param(dbuf_cache_max_shift
, int, 0644);
3902 MODULE_PARM_DESC(dbuf_cache_max_shift
,
3903 "Cap the size of the dbuf cache to a log2 fraction of arc size.");