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, 2016 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>
50 struct dbuf_hold_impl_data
{
51 /* Function arguments */
55 boolean_t dh_fail_sparse
;
56 boolean_t dh_fail_uncached
;
58 dmu_buf_impl_t
**dh_dbp
;
60 dmu_buf_impl_t
*dh_db
;
61 dmu_buf_impl_t
*dh_parent
;
64 dbuf_dirty_record_t
*dh_dr
;
65 arc_buf_contents_t dh_type
;
69 static void __dbuf_hold_impl_init(struct dbuf_hold_impl_data
*dh
,
70 dnode_t
*dn
, uint8_t level
, uint64_t blkid
, boolean_t fail_sparse
,
71 boolean_t fail_uncached
,
72 void *tag
, dmu_buf_impl_t
**dbp
, int depth
);
73 static int __dbuf_hold_impl(struct dbuf_hold_impl_data
*dh
);
75 uint_t zfs_dbuf_evict_key
;
77 * Number of times that zfs_free_range() took the slow path while doing
78 * a zfs receive. A nonzero value indicates a potential performance problem.
80 uint64_t zfs_free_range_recv_miss
;
82 static boolean_t
dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
);
83 static void dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
);
86 extern inline void dmu_buf_init_user(dmu_buf_user_t
*dbu
,
87 dmu_buf_evict_func_t
*evict_func
, dmu_buf_t
**clear_on_evict_dbufp
);
91 * Global data structures and functions for the dbuf cache.
93 static kmem_cache_t
*dbuf_kmem_cache
;
94 static taskq_t
*dbu_evict_taskq
;
96 static kthread_t
*dbuf_cache_evict_thread
;
97 static kmutex_t dbuf_evict_lock
;
98 static kcondvar_t dbuf_evict_cv
;
99 static boolean_t dbuf_evict_thread_exit
;
102 * LRU cache of dbufs. The dbuf cache maintains a list of dbufs that
103 * are not currently held but have been recently released. These dbufs
104 * are not eligible for arc eviction until they are aged out of the cache.
105 * Dbufs are added to the dbuf cache once the last hold is released. If a
106 * dbuf is later accessed and still exists in the dbuf cache, then it will
107 * be removed from the cache and later re-added to the head of the cache.
108 * Dbufs that are aged out of the cache will be immediately destroyed and
109 * become eligible for arc eviction.
111 static multilist_t dbuf_cache
;
112 static refcount_t dbuf_cache_size
;
113 unsigned long dbuf_cache_max_bytes
= 100 * 1024 * 1024;
115 /* Cap the size of the dbuf cache to log2 fraction of arc size. */
116 int dbuf_cache_max_shift
= 5;
119 * The dbuf cache uses a three-stage eviction policy:
120 * - A low water marker designates when the dbuf eviction thread
121 * should stop evicting from the dbuf cache.
122 * - When we reach the maximum size (aka mid water mark), we
123 * signal the eviction thread to run.
124 * - The high water mark indicates when the eviction thread
125 * is unable to keep up with the incoming load and eviction must
126 * happen in the context of the calling thread.
130 * low water mid water hi water
131 * +----------------------------------------+----------+----------+
136 * +----------------------------------------+----------+----------+
138 * evicting eviction directly
141 * The high and low water marks indicate the operating range for the eviction
142 * thread. The low water mark is, by default, 90% of the total size of the
143 * cache and the high water mark is at 110% (both of these percentages can be
144 * changed by setting dbuf_cache_lowater_pct and dbuf_cache_hiwater_pct,
145 * respectively). The eviction thread will try to ensure that the cache remains
146 * within this range by waking up every second and checking if the cache is
147 * above the low water mark. The thread can also be woken up by callers adding
148 * elements into the cache if the cache is larger than the mid water (i.e max
149 * cache size). Once the eviction thread is woken up and eviction is required,
150 * it will continue evicting buffers until it's able to reduce the cache size
151 * to the low water mark. If the cache size continues to grow and hits the high
152 * water mark, then callers adding elments to the cache will begin to evict
153 * directly from the cache until the cache is no longer above the high water
158 * The percentage above and below the maximum cache size.
160 uint_t dbuf_cache_hiwater_pct
= 10;
161 uint_t dbuf_cache_lowater_pct
= 10;
165 dbuf_cons(void *vdb
, void *unused
, int kmflag
)
167 dmu_buf_impl_t
*db
= vdb
;
168 bzero(db
, sizeof (dmu_buf_impl_t
));
170 mutex_init(&db
->db_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
171 cv_init(&db
->db_changed
, NULL
, CV_DEFAULT
, NULL
);
172 multilist_link_init(&db
->db_cache_link
);
173 refcount_create(&db
->db_holds
);
174 multilist_link_init(&db
->db_cache_link
);
181 dbuf_dest(void *vdb
, void *unused
)
183 dmu_buf_impl_t
*db
= vdb
;
184 mutex_destroy(&db
->db_mtx
);
185 cv_destroy(&db
->db_changed
);
186 ASSERT(!multilist_link_active(&db
->db_cache_link
));
187 refcount_destroy(&db
->db_holds
);
191 * dbuf hash table routines
193 static dbuf_hash_table_t dbuf_hash_table
;
195 static uint64_t dbuf_hash_count
;
198 dbuf_hash(void *os
, uint64_t obj
, uint8_t lvl
, uint64_t blkid
)
200 uintptr_t osv
= (uintptr_t)os
;
201 uint64_t crc
= -1ULL;
203 ASSERT(zfs_crc64_table
[128] == ZFS_CRC64_POLY
);
204 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (lvl
)) & 0xFF];
205 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (osv
>> 6)) & 0xFF];
206 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 0)) & 0xFF];
207 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 8)) & 0xFF];
208 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (blkid
>> 0)) & 0xFF];
209 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (blkid
>> 8)) & 0xFF];
211 crc
^= (osv
>>14) ^ (obj
>>16) ^ (blkid
>>16);
216 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
217 ((dbuf)->db.db_object == (obj) && \
218 (dbuf)->db_objset == (os) && \
219 (dbuf)->db_level == (level) && \
220 (dbuf)->db_blkid == (blkid))
223 dbuf_find(objset_t
*os
, uint64_t obj
, uint8_t level
, uint64_t blkid
)
225 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
230 hv
= dbuf_hash(os
, obj
, level
, blkid
);
231 idx
= hv
& h
->hash_table_mask
;
233 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
234 for (db
= h
->hash_table
[idx
]; db
!= NULL
; db
= db
->db_hash_next
) {
235 if (DBUF_EQUAL(db
, os
, obj
, level
, blkid
)) {
236 mutex_enter(&db
->db_mtx
);
237 if (db
->db_state
!= DB_EVICTING
) {
238 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
241 mutex_exit(&db
->db_mtx
);
244 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
248 static dmu_buf_impl_t
*
249 dbuf_find_bonus(objset_t
*os
, uint64_t object
)
252 dmu_buf_impl_t
*db
= NULL
;
254 if (dnode_hold(os
, object
, FTAG
, &dn
) == 0) {
255 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
256 if (dn
->dn_bonus
!= NULL
) {
258 mutex_enter(&db
->db_mtx
);
260 rw_exit(&dn
->dn_struct_rwlock
);
261 dnode_rele(dn
, FTAG
);
267 * Insert an entry into the hash table. If there is already an element
268 * equal to elem in the hash table, then the already existing element
269 * will be returned and the new element will not be inserted.
270 * Otherwise returns NULL.
272 static dmu_buf_impl_t
*
273 dbuf_hash_insert(dmu_buf_impl_t
*db
)
275 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
276 objset_t
*os
= db
->db_objset
;
277 uint64_t obj
= db
->db
.db_object
;
278 int level
= db
->db_level
;
279 uint64_t blkid
, hv
, idx
;
282 blkid
= db
->db_blkid
;
283 hv
= dbuf_hash(os
, obj
, level
, blkid
);
284 idx
= hv
& h
->hash_table_mask
;
286 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
287 for (dbf
= h
->hash_table
[idx
]; dbf
!= NULL
; dbf
= dbf
->db_hash_next
) {
288 if (DBUF_EQUAL(dbf
, os
, obj
, level
, blkid
)) {
289 mutex_enter(&dbf
->db_mtx
);
290 if (dbf
->db_state
!= DB_EVICTING
) {
291 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
294 mutex_exit(&dbf
->db_mtx
);
298 mutex_enter(&db
->db_mtx
);
299 db
->db_hash_next
= h
->hash_table
[idx
];
300 h
->hash_table
[idx
] = db
;
301 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
302 atomic_inc_64(&dbuf_hash_count
);
308 * Remove an entry from the hash table. It must be in the EVICTING state.
311 dbuf_hash_remove(dmu_buf_impl_t
*db
)
313 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
315 dmu_buf_impl_t
*dbf
, **dbp
;
317 hv
= dbuf_hash(db
->db_objset
, db
->db
.db_object
,
318 db
->db_level
, db
->db_blkid
);
319 idx
= hv
& h
->hash_table_mask
;
322 * We musn't hold db_mtx to maintain lock ordering:
323 * DBUF_HASH_MUTEX > db_mtx.
325 ASSERT(refcount_is_zero(&db
->db_holds
));
326 ASSERT(db
->db_state
== DB_EVICTING
);
327 ASSERT(!MUTEX_HELD(&db
->db_mtx
));
329 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
330 dbp
= &h
->hash_table
[idx
];
331 while ((dbf
= *dbp
) != db
) {
332 dbp
= &dbf
->db_hash_next
;
335 *dbp
= db
->db_hash_next
;
336 db
->db_hash_next
= NULL
;
337 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
338 atomic_dec_64(&dbuf_hash_count
);
344 } dbvu_verify_type_t
;
347 dbuf_verify_user(dmu_buf_impl_t
*db
, dbvu_verify_type_t verify_type
)
352 if (db
->db_user
== NULL
)
355 /* Only data blocks support the attachment of user data. */
356 ASSERT(db
->db_level
== 0);
358 /* Clients must resolve a dbuf before attaching user data. */
359 ASSERT(db
->db
.db_data
!= NULL
);
360 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
362 holds
= refcount_count(&db
->db_holds
);
363 if (verify_type
== DBVU_EVICTING
) {
365 * Immediate eviction occurs when holds == dirtycnt.
366 * For normal eviction buffers, holds is zero on
367 * eviction, except when dbuf_fix_old_data() calls
368 * dbuf_clear_data(). However, the hold count can grow
369 * during eviction even though db_mtx is held (see
370 * dmu_bonus_hold() for an example), so we can only
371 * test the generic invariant that holds >= dirtycnt.
373 ASSERT3U(holds
, >=, db
->db_dirtycnt
);
375 if (db
->db_user_immediate_evict
== TRUE
)
376 ASSERT3U(holds
, >=, db
->db_dirtycnt
);
378 ASSERT3U(holds
, >, 0);
384 dbuf_evict_user(dmu_buf_impl_t
*db
)
386 dmu_buf_user_t
*dbu
= db
->db_user
;
388 ASSERT(MUTEX_HELD(&db
->db_mtx
));
393 dbuf_verify_user(db
, DBVU_EVICTING
);
397 if (dbu
->dbu_clear_on_evict_dbufp
!= NULL
)
398 *dbu
->dbu_clear_on_evict_dbufp
= NULL
;
402 * Invoke the callback from a taskq to avoid lock order reversals
403 * and limit stack depth.
405 taskq_dispatch_ent(dbu_evict_taskq
, dbu
->dbu_evict_func
, dbu
, 0,
410 dbuf_is_metadata(dmu_buf_impl_t
*db
)
413 * Consider indirect blocks and spill blocks to be meta data.
415 if (db
->db_level
> 0 || db
->db_blkid
== DMU_SPILL_BLKID
) {
418 boolean_t is_metadata
;
421 is_metadata
= DMU_OT_IS_METADATA(DB_DNODE(db
)->dn_type
);
424 return (is_metadata
);
430 * This function *must* return indices evenly distributed between all
431 * sublists of the multilist. This is needed due to how the dbuf eviction
432 * code is laid out; dbuf_evict_thread() assumes dbufs are evenly
433 * distributed between all sublists and uses this assumption when
434 * deciding which sublist to evict from and how much to evict from it.
437 dbuf_cache_multilist_index_func(multilist_t
*ml
, void *obj
)
439 dmu_buf_impl_t
*db
= obj
;
442 * The assumption here, is the hash value for a given
443 * dmu_buf_impl_t will remain constant throughout it's lifetime
444 * (i.e. it's objset, object, level and blkid fields don't change).
445 * Thus, we don't need to store the dbuf's sublist index
446 * on insertion, as this index can be recalculated on removal.
448 * Also, the low order bits of the hash value are thought to be
449 * distributed evenly. Otherwise, in the case that the multilist
450 * has a power of two number of sublists, each sublists' usage
451 * would not be evenly distributed.
453 return (dbuf_hash(db
->db_objset
, db
->db
.db_object
,
454 db
->db_level
, db
->db_blkid
) %
455 multilist_get_num_sublists(ml
));
458 static inline boolean_t
459 dbuf_cache_above_hiwater(void)
461 uint64_t dbuf_cache_hiwater_bytes
=
462 (dbuf_cache_max_bytes
* dbuf_cache_hiwater_pct
) / 100;
464 return (refcount_count(&dbuf_cache_size
) >
465 dbuf_cache_max_bytes
+ dbuf_cache_hiwater_bytes
);
468 static inline boolean_t
469 dbuf_cache_above_lowater(void)
471 uint64_t dbuf_cache_lowater_bytes
=
472 (dbuf_cache_max_bytes
* dbuf_cache_lowater_pct
) / 100;
474 return (refcount_count(&dbuf_cache_size
) >
475 dbuf_cache_max_bytes
- dbuf_cache_lowater_bytes
);
479 * Evict the oldest eligible dbuf from the dbuf cache.
484 int idx
= multilist_get_random_index(&dbuf_cache
);
485 multilist_sublist_t
*mls
= multilist_sublist_lock(&dbuf_cache
, idx
);
487 ASSERT(!MUTEX_HELD(&dbuf_evict_lock
));
490 * Set the thread's tsd to indicate that it's processing evictions.
491 * Once a thread stops evicting from the dbuf cache it will
492 * reset its tsd to NULL.
494 ASSERT3P(tsd_get(zfs_dbuf_evict_key
), ==, NULL
);
495 (void) tsd_set(zfs_dbuf_evict_key
, (void *)B_TRUE
);
497 db
= multilist_sublist_tail(mls
);
498 while (db
!= NULL
&& mutex_tryenter(&db
->db_mtx
) == 0) {
499 db
= multilist_sublist_prev(mls
, db
);
502 DTRACE_PROBE2(dbuf__evict__one
, dmu_buf_impl_t
*, db
,
503 multilist_sublist_t
*, mls
);
506 multilist_sublist_remove(mls
, db
);
507 multilist_sublist_unlock(mls
);
508 (void) refcount_remove_many(&dbuf_cache_size
,
512 multilist_sublist_unlock(mls
);
514 (void) tsd_set(zfs_dbuf_evict_key
, NULL
);
518 * The dbuf evict thread is responsible for aging out dbufs from the
519 * cache. Once the cache has reached it's maximum size, dbufs are removed
520 * and destroyed. The eviction thread will continue running until the size
521 * of the dbuf cache is at or below the maximum size. Once the dbuf is aged
522 * out of the cache it is destroyed and becomes eligible for arc eviction.
525 dbuf_evict_thread(void)
529 CALLB_CPR_INIT(&cpr
, &dbuf_evict_lock
, callb_generic_cpr
, FTAG
);
531 mutex_enter(&dbuf_evict_lock
);
532 while (!dbuf_evict_thread_exit
) {
533 while (!dbuf_cache_above_lowater() && !dbuf_evict_thread_exit
) {
534 CALLB_CPR_SAFE_BEGIN(&cpr
);
535 (void) cv_timedwait_sig_hires(&dbuf_evict_cv
,
536 &dbuf_evict_lock
, SEC2NSEC(1), MSEC2NSEC(1), 0);
537 CALLB_CPR_SAFE_END(&cpr
, &dbuf_evict_lock
);
539 mutex_exit(&dbuf_evict_lock
);
542 * Keep evicting as long as we're above the low water mark
543 * for the cache. We do this without holding the locks to
544 * minimize lock contention.
546 while (dbuf_cache_above_lowater() && !dbuf_evict_thread_exit
) {
550 mutex_enter(&dbuf_evict_lock
);
553 dbuf_evict_thread_exit
= B_FALSE
;
554 cv_broadcast(&dbuf_evict_cv
);
555 CALLB_CPR_EXIT(&cpr
); /* drops dbuf_evict_lock */
560 * Wake up the dbuf eviction thread if the dbuf cache is at its max size.
561 * If the dbuf cache is at its high water mark, then evict a dbuf from the
562 * dbuf cache using the callers context.
565 dbuf_evict_notify(void)
569 * We use thread specific data to track when a thread has
570 * started processing evictions. This allows us to avoid deeply
571 * nested stacks that would have a call flow similar to this:
573 * dbuf_rele()-->dbuf_rele_and_unlock()-->dbuf_evict_notify()
576 * +-----dbuf_destroy()<--dbuf_evict_one()<--------+
578 * The dbuf_eviction_thread will always have its tsd set until
579 * that thread exits. All other threads will only set their tsd
580 * if they are participating in the eviction process. This only
581 * happens if the eviction thread is unable to process evictions
582 * fast enough. To keep the dbuf cache size in check, other threads
583 * can evict from the dbuf cache directly. Those threads will set
584 * their tsd values so that we ensure that they only evict one dbuf
585 * from the dbuf cache.
587 if (tsd_get(zfs_dbuf_evict_key
) != NULL
)
590 if (refcount_count(&dbuf_cache_size
) > dbuf_cache_max_bytes
) {
591 boolean_t evict_now
= B_FALSE
;
593 mutex_enter(&dbuf_evict_lock
);
594 if (refcount_count(&dbuf_cache_size
) > dbuf_cache_max_bytes
) {
595 evict_now
= dbuf_cache_above_hiwater();
596 cv_signal(&dbuf_evict_cv
);
598 mutex_exit(&dbuf_evict_lock
);
611 uint64_t hsize
= 1ULL << 16;
612 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
616 * The hash table is big enough to fill all of physical memory
617 * with an average block size of zfs_arc_average_blocksize (default 8K).
618 * By default, the table will take up
619 * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers).
621 while (hsize
* zfs_arc_average_blocksize
< physmem
* PAGESIZE
)
625 h
->hash_table_mask
= hsize
- 1;
626 #if defined(_KERNEL) && defined(HAVE_SPL)
628 * Large allocations which do not require contiguous pages
629 * should be using vmem_alloc() in the linux kernel
631 h
->hash_table
= vmem_zalloc(hsize
* sizeof (void *), KM_SLEEP
);
633 h
->hash_table
= kmem_zalloc(hsize
* sizeof (void *), KM_NOSLEEP
);
635 if (h
->hash_table
== NULL
) {
636 /* XXX - we should really return an error instead of assert */
637 ASSERT(hsize
> (1ULL << 10));
642 dbuf_kmem_cache
= kmem_cache_create("dmu_buf_impl_t",
643 sizeof (dmu_buf_impl_t
),
644 0, dbuf_cons
, dbuf_dest
, NULL
, NULL
, NULL
, 0);
646 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
647 mutex_init(&h
->hash_mutexes
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
652 * Setup the parameters for the dbuf cache. We cap the size of the
653 * dbuf cache to 1/32nd (default) of the size of the ARC.
655 dbuf_cache_max_bytes
= MIN(dbuf_cache_max_bytes
,
656 arc_max_bytes() >> dbuf_cache_max_shift
);
659 * All entries are queued via taskq_dispatch_ent(), so min/maxalloc
660 * configuration is not required.
662 dbu_evict_taskq
= taskq_create("dbu_evict", 1, defclsyspri
, 0, 0, 0);
664 multilist_create(&dbuf_cache
, sizeof (dmu_buf_impl_t
),
665 offsetof(dmu_buf_impl_t
, db_cache_link
),
666 zfs_arc_num_sublists_per_state
,
667 dbuf_cache_multilist_index_func
);
668 refcount_create(&dbuf_cache_size
);
670 tsd_create(&zfs_dbuf_evict_key
, NULL
);
671 dbuf_evict_thread_exit
= B_FALSE
;
672 mutex_init(&dbuf_evict_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
673 cv_init(&dbuf_evict_cv
, NULL
, CV_DEFAULT
, NULL
);
674 dbuf_cache_evict_thread
= thread_create(NULL
, 0, dbuf_evict_thread
,
675 NULL
, 0, &p0
, TS_RUN
, minclsyspri
);
681 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
684 dbuf_stats_destroy();
686 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
687 mutex_destroy(&h
->hash_mutexes
[i
]);
688 #if defined(_KERNEL) && defined(HAVE_SPL)
690 * Large allocations which do not require contiguous pages
691 * should be using vmem_free() in the linux kernel
693 vmem_free(h
->hash_table
, (h
->hash_table_mask
+ 1) * sizeof (void *));
695 kmem_free(h
->hash_table
, (h
->hash_table_mask
+ 1) * sizeof (void *));
697 kmem_cache_destroy(dbuf_kmem_cache
);
698 taskq_destroy(dbu_evict_taskq
);
700 mutex_enter(&dbuf_evict_lock
);
701 dbuf_evict_thread_exit
= B_TRUE
;
702 while (dbuf_evict_thread_exit
) {
703 cv_signal(&dbuf_evict_cv
);
704 cv_wait(&dbuf_evict_cv
, &dbuf_evict_lock
);
706 mutex_exit(&dbuf_evict_lock
);
707 tsd_destroy(&zfs_dbuf_evict_key
);
709 mutex_destroy(&dbuf_evict_lock
);
710 cv_destroy(&dbuf_evict_cv
);
712 refcount_destroy(&dbuf_cache_size
);
713 multilist_destroy(&dbuf_cache
);
722 dbuf_verify(dmu_buf_impl_t
*db
)
725 dbuf_dirty_record_t
*dr
;
727 ASSERT(MUTEX_HELD(&db
->db_mtx
));
729 if (!(zfs_flags
& ZFS_DEBUG_DBUF_VERIFY
))
732 ASSERT(db
->db_objset
!= NULL
);
736 ASSERT(db
->db_parent
== NULL
);
737 ASSERT(db
->db_blkptr
== NULL
);
739 ASSERT3U(db
->db
.db_object
, ==, dn
->dn_object
);
740 ASSERT3P(db
->db_objset
, ==, dn
->dn_objset
);
741 ASSERT3U(db
->db_level
, <, dn
->dn_nlevels
);
742 ASSERT(db
->db_blkid
== DMU_BONUS_BLKID
||
743 db
->db_blkid
== DMU_SPILL_BLKID
||
744 !avl_is_empty(&dn
->dn_dbufs
));
746 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
748 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
749 ASSERT3U(db
->db
.db_offset
, ==, DMU_BONUS_BLKID
);
750 } else if (db
->db_blkid
== DMU_SPILL_BLKID
) {
752 ASSERT0(db
->db
.db_offset
);
754 ASSERT3U(db
->db
.db_offset
, ==, db
->db_blkid
* db
->db
.db_size
);
757 for (dr
= db
->db_data_pending
; dr
!= NULL
; dr
= dr
->dr_next
)
758 ASSERT(dr
->dr_dbuf
== db
);
760 for (dr
= db
->db_last_dirty
; dr
!= NULL
; dr
= dr
->dr_next
)
761 ASSERT(dr
->dr_dbuf
== db
);
764 * We can't assert that db_size matches dn_datablksz because it
765 * can be momentarily different when another thread is doing
768 if (db
->db_level
== 0 && db
->db
.db_object
== DMU_META_DNODE_OBJECT
) {
769 dr
= db
->db_data_pending
;
771 * It should only be modified in syncing context, so
772 * make sure we only have one copy of the data.
774 ASSERT(dr
== NULL
|| dr
->dt
.dl
.dr_data
== db
->db_buf
);
777 /* verify db->db_blkptr */
779 if (db
->db_parent
== dn
->dn_dbuf
) {
780 /* db is pointed to by the dnode */
781 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
782 if (DMU_OBJECT_IS_SPECIAL(db
->db
.db_object
))
783 ASSERT(db
->db_parent
== NULL
);
785 ASSERT(db
->db_parent
!= NULL
);
786 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
787 ASSERT3P(db
->db_blkptr
, ==,
788 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
790 /* db is pointed to by an indirect block */
791 ASSERTV(int epb
= db
->db_parent
->db
.db_size
>>
793 ASSERT3U(db
->db_parent
->db_level
, ==, db
->db_level
+1);
794 ASSERT3U(db
->db_parent
->db
.db_object
, ==,
797 * dnode_grow_indblksz() can make this fail if we don't
798 * have the struct_rwlock. XXX indblksz no longer
799 * grows. safe to do this now?
801 if (RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
802 ASSERT3P(db
->db_blkptr
, ==,
803 ((blkptr_t
*)db
->db_parent
->db
.db_data
+
804 db
->db_blkid
% epb
));
808 if ((db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
)) &&
809 (db
->db_buf
== NULL
|| db
->db_buf
->b_data
) &&
810 db
->db
.db_data
&& db
->db_blkid
!= DMU_BONUS_BLKID
&&
811 db
->db_state
!= DB_FILL
&& !dn
->dn_free_txg
) {
813 * If the blkptr isn't set but they have nonzero data,
814 * it had better be dirty, otherwise we'll lose that
815 * data when we evict this buffer.
817 * There is an exception to this rule for indirect blocks; in
818 * this case, if the indirect block is a hole, we fill in a few
819 * fields on each of the child blocks (importantly, birth time)
820 * to prevent hole birth times from being lost when you
821 * partially fill in a hole.
823 if (db
->db_dirtycnt
== 0) {
824 if (db
->db_level
== 0) {
825 uint64_t *buf
= db
->db
.db_data
;
828 for (i
= 0; i
< db
->db
.db_size
>> 3; i
++) {
833 blkptr_t
*bps
= db
->db
.db_data
;
834 ASSERT3U(1 << DB_DNODE(db
)->dn_indblkshift
, ==,
837 * We want to verify that all the blkptrs in the
838 * indirect block are holes, but we may have
839 * automatically set up a few fields for them.
840 * We iterate through each blkptr and verify
841 * they only have those fields set.
844 i
< db
->db
.db_size
/ sizeof (blkptr_t
);
846 blkptr_t
*bp
= &bps
[i
];
847 ASSERT(ZIO_CHECKSUM_IS_ZERO(
850 DVA_IS_EMPTY(&bp
->blk_dva
[0]) &&
851 DVA_IS_EMPTY(&bp
->blk_dva
[1]) &&
852 DVA_IS_EMPTY(&bp
->blk_dva
[2]));
853 ASSERT0(bp
->blk_fill
);
854 ASSERT0(bp
->blk_pad
[0]);
855 ASSERT0(bp
->blk_pad
[1]);
856 ASSERT(!BP_IS_EMBEDDED(bp
));
857 ASSERT(BP_IS_HOLE(bp
));
858 ASSERT0(bp
->blk_phys_birth
);
868 dbuf_clear_data(dmu_buf_impl_t
*db
)
870 ASSERT(MUTEX_HELD(&db
->db_mtx
));
872 ASSERT3P(db
->db_buf
, ==, NULL
);
873 db
->db
.db_data
= NULL
;
874 if (db
->db_state
!= DB_NOFILL
)
875 db
->db_state
= DB_UNCACHED
;
879 dbuf_set_data(dmu_buf_impl_t
*db
, arc_buf_t
*buf
)
881 ASSERT(MUTEX_HELD(&db
->db_mtx
));
885 ASSERT(buf
->b_data
!= NULL
);
886 db
->db
.db_data
= buf
->b_data
;
890 * Loan out an arc_buf for read. Return the loaned arc_buf.
893 dbuf_loan_arcbuf(dmu_buf_impl_t
*db
)
897 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
898 mutex_enter(&db
->db_mtx
);
899 if (arc_released(db
->db_buf
) || refcount_count(&db
->db_holds
) > 1) {
900 int blksz
= db
->db
.db_size
;
901 spa_t
*spa
= db
->db_objset
->os_spa
;
903 mutex_exit(&db
->db_mtx
);
904 abuf
= arc_loan_buf(spa
, B_FALSE
, blksz
);
905 bcopy(db
->db
.db_data
, abuf
->b_data
, blksz
);
908 arc_loan_inuse_buf(abuf
, db
);
911 mutex_exit(&db
->db_mtx
);
917 * Calculate which level n block references the data at the level 0 offset
921 dbuf_whichblock(dnode_t
*dn
, int64_t level
, uint64_t offset
)
923 if (dn
->dn_datablkshift
!= 0 && dn
->dn_indblkshift
!= 0) {
925 * The level n blkid is equal to the level 0 blkid divided by
926 * the number of level 0s in a level n block.
928 * The level 0 blkid is offset >> datablkshift =
929 * offset / 2^datablkshift.
931 * The number of level 0s in a level n is the number of block
932 * pointers in an indirect block, raised to the power of level.
933 * This is 2^(indblkshift - SPA_BLKPTRSHIFT)^level =
934 * 2^(level*(indblkshift - SPA_BLKPTRSHIFT)).
936 * Thus, the level n blkid is: offset /
937 * ((2^datablkshift)*(2^(level*(indblkshift - SPA_BLKPTRSHIFT)))
938 * = offset / 2^(datablkshift + level *
939 * (indblkshift - SPA_BLKPTRSHIFT))
940 * = offset >> (datablkshift + level *
941 * (indblkshift - SPA_BLKPTRSHIFT))
943 return (offset
>> (dn
->dn_datablkshift
+ level
*
944 (dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
)));
946 ASSERT3U(offset
, <, dn
->dn_datablksz
);
952 dbuf_read_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
954 dmu_buf_impl_t
*db
= vdb
;
956 mutex_enter(&db
->db_mtx
);
957 ASSERT3U(db
->db_state
, ==, DB_READ
);
959 * All reads are synchronous, so we must have a hold on the dbuf
961 ASSERT(refcount_count(&db
->db_holds
) > 0);
962 ASSERT(db
->db_buf
== NULL
);
963 ASSERT(db
->db
.db_data
== NULL
);
964 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
965 /* we were freed in flight; disregard any error */
966 arc_release(buf
, db
);
967 bzero(buf
->b_data
, db
->db
.db_size
);
969 db
->db_freed_in_flight
= FALSE
;
970 dbuf_set_data(db
, buf
);
971 db
->db_state
= DB_CACHED
;
972 } else if (zio
== NULL
|| zio
->io_error
== 0) {
973 dbuf_set_data(db
, buf
);
974 db
->db_state
= DB_CACHED
;
976 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
977 ASSERT3P(db
->db_buf
, ==, NULL
);
978 arc_buf_destroy(buf
, db
);
979 db
->db_state
= DB_UNCACHED
;
981 cv_broadcast(&db
->db_changed
);
982 dbuf_rele_and_unlock(db
, NULL
);
986 dbuf_read_impl(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t flags
)
990 uint32_t aflags
= ARC_FLAG_NOWAIT
;
995 ASSERT(!refcount_is_zero(&db
->db_holds
));
996 /* We need the struct_rwlock to prevent db_blkptr from changing. */
997 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
998 ASSERT(MUTEX_HELD(&db
->db_mtx
));
999 ASSERT(db
->db_state
== DB_UNCACHED
);
1000 ASSERT(db
->db_buf
== NULL
);
1002 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1004 * The bonus length stored in the dnode may be less than
1005 * the maximum available space in the bonus buffer.
1007 int bonuslen
= MIN(dn
->dn_bonuslen
, dn
->dn_phys
->dn_bonuslen
);
1008 int max_bonuslen
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
);
1010 ASSERT3U(bonuslen
, <=, db
->db
.db_size
);
1011 db
->db
.db_data
= zio_buf_alloc(max_bonuslen
);
1012 arc_space_consume(max_bonuslen
, ARC_SPACE_BONUS
);
1013 if (bonuslen
< max_bonuslen
)
1014 bzero(db
->db
.db_data
, max_bonuslen
);
1016 bcopy(DN_BONUS(dn
->dn_phys
), db
->db
.db_data
, bonuslen
);
1018 db
->db_state
= DB_CACHED
;
1019 mutex_exit(&db
->db_mtx
);
1024 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
1025 * processes the delete record and clears the bp while we are waiting
1026 * for the dn_mtx (resulting in a "no" from block_freed).
1028 if (db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
) ||
1029 (db
->db_level
== 0 && (dnode_block_freed(dn
, db
->db_blkid
) ||
1030 BP_IS_HOLE(db
->db_blkptr
)))) {
1031 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1033 dbuf_set_data(db
, arc_alloc_buf(db
->db_objset
->os_spa
, db
, type
,
1035 bzero(db
->db
.db_data
, db
->db
.db_size
);
1037 if (db
->db_blkptr
!= NULL
&& db
->db_level
> 0 &&
1038 BP_IS_HOLE(db
->db_blkptr
) &&
1039 db
->db_blkptr
->blk_birth
!= 0) {
1040 blkptr_t
*bps
= db
->db
.db_data
;
1042 for (i
= 0; i
< ((1 <<
1043 DB_DNODE(db
)->dn_indblkshift
) / sizeof (blkptr_t
));
1045 blkptr_t
*bp
= &bps
[i
];
1046 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==,
1047 1 << dn
->dn_indblkshift
);
1049 BP_GET_LEVEL(db
->db_blkptr
) == 1 ?
1051 BP_GET_LSIZE(db
->db_blkptr
));
1052 BP_SET_TYPE(bp
, BP_GET_TYPE(db
->db_blkptr
));
1054 BP_GET_LEVEL(db
->db_blkptr
) - 1);
1055 BP_SET_BIRTH(bp
, db
->db_blkptr
->blk_birth
, 0);
1059 db
->db_state
= DB_CACHED
;
1060 mutex_exit(&db
->db_mtx
);
1066 db
->db_state
= DB_READ
;
1067 mutex_exit(&db
->db_mtx
);
1069 if (DBUF_IS_L2CACHEABLE(db
))
1070 aflags
|= ARC_FLAG_L2CACHE
;
1072 SET_BOOKMARK(&zb
, db
->db_objset
->os_dsl_dataset
?
1073 db
->db_objset
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
1074 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
1076 dbuf_add_ref(db
, NULL
);
1078 err
= arc_read(zio
, db
->db_objset
->os_spa
, db
->db_blkptr
,
1079 dbuf_read_done
, db
, ZIO_PRIORITY_SYNC_READ
,
1080 (flags
& DB_RF_CANFAIL
) ? ZIO_FLAG_CANFAIL
: ZIO_FLAG_MUSTSUCCEED
,
1087 * This is our just-in-time copy function. It makes a copy of buffers that
1088 * have been modified in a previous transaction group before we access them in
1089 * the current active group.
1091 * This function is used in three places: when we are dirtying a buffer for the
1092 * first time in a txg, when we are freeing a range in a dnode that includes
1093 * this buffer, and when we are accessing a buffer which was received compressed
1094 * and later referenced in a WRITE_BYREF record.
1096 * Note that when we are called from dbuf_free_range() we do not put a hold on
1097 * the buffer, we just traverse the active dbuf list for the dnode.
1100 dbuf_fix_old_data(dmu_buf_impl_t
*db
, uint64_t txg
)
1102 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
1104 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1105 ASSERT(db
->db
.db_data
!= NULL
);
1106 ASSERT(db
->db_level
== 0);
1107 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
);
1110 (dr
->dt
.dl
.dr_data
!=
1111 ((db
->db_blkid
== DMU_BONUS_BLKID
) ? db
->db
.db_data
: db
->db_buf
)))
1115 * If the last dirty record for this dbuf has not yet synced
1116 * and its referencing the dbuf data, either:
1117 * reset the reference to point to a new copy,
1118 * or (if there a no active holders)
1119 * just null out the current db_data pointer.
1121 ASSERT(dr
->dr_txg
>= txg
- 2);
1122 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1123 /* Note that the data bufs here are zio_bufs */
1124 dnode_t
*dn
= DB_DNODE(db
);
1125 int bonuslen
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
);
1126 dr
->dt
.dl
.dr_data
= zio_buf_alloc(bonuslen
);
1127 arc_space_consume(bonuslen
, ARC_SPACE_BONUS
);
1128 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
, bonuslen
);
1129 } else if (refcount_count(&db
->db_holds
) > db
->db_dirtycnt
) {
1130 int size
= arc_buf_size(db
->db_buf
);
1131 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1132 spa_t
*spa
= db
->db_objset
->os_spa
;
1133 enum zio_compress compress_type
=
1134 arc_get_compression(db
->db_buf
);
1136 if (compress_type
== ZIO_COMPRESS_OFF
) {
1137 dr
->dt
.dl
.dr_data
= arc_alloc_buf(spa
, db
, type
, size
);
1139 ASSERT3U(type
, ==, ARC_BUFC_DATA
);
1140 dr
->dt
.dl
.dr_data
= arc_alloc_compressed_buf(spa
, db
,
1141 size
, arc_buf_lsize(db
->db_buf
), compress_type
);
1143 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
->b_data
, size
);
1146 dbuf_clear_data(db
);
1151 dbuf_read(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t flags
)
1154 boolean_t havepzio
= (zio
!= NULL
);
1159 * We don't have to hold the mutex to check db_state because it
1160 * can't be freed while we have a hold on the buffer.
1162 ASSERT(!refcount_is_zero(&db
->db_holds
));
1164 if (db
->db_state
== DB_NOFILL
)
1165 return (SET_ERROR(EIO
));
1169 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1170 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
1172 prefetch
= db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1173 (flags
& DB_RF_NOPREFETCH
) == 0 && dn
!= NULL
&&
1174 DBUF_IS_CACHEABLE(db
);
1176 mutex_enter(&db
->db_mtx
);
1177 if (db
->db_state
== DB_CACHED
) {
1179 * If the arc buf is compressed, we need to decompress it to
1180 * read the data. This could happen during the "zfs receive" of
1181 * a stream which is compressed and deduplicated.
1183 if (db
->db_buf
!= NULL
&&
1184 arc_get_compression(db
->db_buf
) != ZIO_COMPRESS_OFF
) {
1185 dbuf_fix_old_data(db
,
1186 spa_syncing_txg(dmu_objset_spa(db
->db_objset
)));
1187 err
= arc_decompress(db
->db_buf
);
1188 dbuf_set_data(db
, db
->db_buf
);
1190 mutex_exit(&db
->db_mtx
);
1192 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
);
1193 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1194 rw_exit(&dn
->dn_struct_rwlock
);
1196 } else if (db
->db_state
== DB_UNCACHED
) {
1197 spa_t
*spa
= dn
->dn_objset
->os_spa
;
1200 zio
= zio_root(spa
, NULL
, NULL
, ZIO_FLAG_CANFAIL
);
1202 err
= dbuf_read_impl(db
, zio
, flags
);
1204 /* dbuf_read_impl has dropped db_mtx for us */
1206 if (!err
&& prefetch
)
1207 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
);
1209 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1210 rw_exit(&dn
->dn_struct_rwlock
);
1213 if (!err
&& !havepzio
)
1214 err
= zio_wait(zio
);
1217 * Another reader came in while the dbuf was in flight
1218 * between UNCACHED and CACHED. Either a writer will finish
1219 * writing the buffer (sending the dbuf to CACHED) or the
1220 * first reader's request will reach the read_done callback
1221 * and send the dbuf to CACHED. Otherwise, a failure
1222 * occurred and the dbuf went to UNCACHED.
1224 mutex_exit(&db
->db_mtx
);
1226 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
);
1227 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1228 rw_exit(&dn
->dn_struct_rwlock
);
1231 /* Skip the wait per the caller's request. */
1232 mutex_enter(&db
->db_mtx
);
1233 if ((flags
& DB_RF_NEVERWAIT
) == 0) {
1234 while (db
->db_state
== DB_READ
||
1235 db
->db_state
== DB_FILL
) {
1236 ASSERT(db
->db_state
== DB_READ
||
1237 (flags
& DB_RF_HAVESTRUCT
) == 0);
1238 DTRACE_PROBE2(blocked__read
, dmu_buf_impl_t
*,
1240 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1242 if (db
->db_state
== DB_UNCACHED
)
1243 err
= SET_ERROR(EIO
);
1245 mutex_exit(&db
->db_mtx
);
1248 ASSERT(err
|| havepzio
|| db
->db_state
== DB_CACHED
);
1253 dbuf_noread(dmu_buf_impl_t
*db
)
1255 ASSERT(!refcount_is_zero(&db
->db_holds
));
1256 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1257 mutex_enter(&db
->db_mtx
);
1258 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
1259 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1260 if (db
->db_state
== DB_UNCACHED
) {
1261 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1262 spa_t
*spa
= db
->db_objset
->os_spa
;
1264 ASSERT(db
->db_buf
== NULL
);
1265 ASSERT(db
->db
.db_data
== NULL
);
1266 dbuf_set_data(db
, arc_alloc_buf(spa
, db
, type
, db
->db
.db_size
));
1267 db
->db_state
= DB_FILL
;
1268 } else if (db
->db_state
== DB_NOFILL
) {
1269 dbuf_clear_data(db
);
1271 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
1273 mutex_exit(&db
->db_mtx
);
1277 dbuf_unoverride(dbuf_dirty_record_t
*dr
)
1279 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
1280 blkptr_t
*bp
= &dr
->dt
.dl
.dr_overridden_by
;
1281 uint64_t txg
= dr
->dr_txg
;
1283 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1284 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_IN_DMU_SYNC
);
1285 ASSERT(db
->db_level
== 0);
1287 if (db
->db_blkid
== DMU_BONUS_BLKID
||
1288 dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
)
1291 ASSERT(db
->db_data_pending
!= dr
);
1293 /* free this block */
1294 if (!BP_IS_HOLE(bp
) && !dr
->dt
.dl
.dr_nopwrite
)
1295 zio_free(db
->db_objset
->os_spa
, txg
, bp
);
1297 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
1298 dr
->dt
.dl
.dr_nopwrite
= B_FALSE
;
1301 * Release the already-written buffer, so we leave it in
1302 * a consistent dirty state. Note that all callers are
1303 * modifying the buffer, so they will immediately do
1304 * another (redundant) arc_release(). Therefore, leave
1305 * the buf thawed to save the effort of freezing &
1306 * immediately re-thawing it.
1308 arc_release(dr
->dt
.dl
.dr_data
, db
);
1312 * Evict (if its unreferenced) or clear (if its referenced) any level-0
1313 * data blocks in the free range, so that any future readers will find
1316 * This is a no-op if the dataset is in the middle of an incremental
1317 * receive; see comment below for details.
1320 dbuf_free_range(dnode_t
*dn
, uint64_t start_blkid
, uint64_t end_blkid
,
1323 dmu_buf_impl_t
*db_search
;
1324 dmu_buf_impl_t
*db
, *db_next
;
1325 uint64_t txg
= tx
->tx_txg
;
1327 boolean_t freespill
=
1328 (start_blkid
== DMU_SPILL_BLKID
|| end_blkid
== DMU_SPILL_BLKID
);
1330 if (end_blkid
> dn
->dn_maxblkid
&& !freespill
)
1331 end_blkid
= dn
->dn_maxblkid
;
1332 dprintf_dnode(dn
, "start=%llu end=%llu\n", start_blkid
, end_blkid
);
1334 db_search
= kmem_alloc(sizeof (dmu_buf_impl_t
), KM_SLEEP
);
1335 db_search
->db_level
= 0;
1336 db_search
->db_blkid
= start_blkid
;
1337 db_search
->db_state
= DB_SEARCH
;
1339 mutex_enter(&dn
->dn_dbufs_mtx
);
1340 if (start_blkid
>= dn
->dn_unlisted_l0_blkid
&& !freespill
) {
1341 /* There can't be any dbufs in this range; no need to search. */
1343 db
= avl_find(&dn
->dn_dbufs
, db_search
, &where
);
1344 ASSERT3P(db
, ==, NULL
);
1345 db
= avl_nearest(&dn
->dn_dbufs
, where
, AVL_AFTER
);
1346 ASSERT(db
== NULL
|| db
->db_level
> 0);
1349 } else if (dmu_objset_is_receiving(dn
->dn_objset
)) {
1351 * If we are receiving, we expect there to be no dbufs in
1352 * the range to be freed, because receive modifies each
1353 * block at most once, and in offset order. If this is
1354 * not the case, it can lead to performance problems,
1355 * so note that we unexpectedly took the slow path.
1357 atomic_inc_64(&zfs_free_range_recv_miss
);
1360 db
= avl_find(&dn
->dn_dbufs
, db_search
, &where
);
1361 ASSERT3P(db
, ==, NULL
);
1362 db
= avl_nearest(&dn
->dn_dbufs
, where
, AVL_AFTER
);
1364 for (; db
!= NULL
; db
= db_next
) {
1365 db_next
= AVL_NEXT(&dn
->dn_dbufs
, db
);
1366 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1368 if (db
->db_level
!= 0 || db
->db_blkid
> end_blkid
) {
1371 ASSERT3U(db
->db_blkid
, >=, start_blkid
);
1373 /* found a level 0 buffer in the range */
1374 mutex_enter(&db
->db_mtx
);
1375 if (dbuf_undirty(db
, tx
)) {
1376 /* mutex has been dropped and dbuf destroyed */
1380 if (db
->db_state
== DB_UNCACHED
||
1381 db
->db_state
== DB_NOFILL
||
1382 db
->db_state
== DB_EVICTING
) {
1383 ASSERT(db
->db
.db_data
== NULL
);
1384 mutex_exit(&db
->db_mtx
);
1387 if (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
) {
1388 /* will be handled in dbuf_read_done or dbuf_rele */
1389 db
->db_freed_in_flight
= TRUE
;
1390 mutex_exit(&db
->db_mtx
);
1393 if (refcount_count(&db
->db_holds
) == 0) {
1398 /* The dbuf is referenced */
1400 if (db
->db_last_dirty
!= NULL
) {
1401 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
1403 if (dr
->dr_txg
== txg
) {
1405 * This buffer is "in-use", re-adjust the file
1406 * size to reflect that this buffer may
1407 * contain new data when we sync.
1409 if (db
->db_blkid
!= DMU_SPILL_BLKID
&&
1410 db
->db_blkid
> dn
->dn_maxblkid
)
1411 dn
->dn_maxblkid
= db
->db_blkid
;
1412 dbuf_unoverride(dr
);
1415 * This dbuf is not dirty in the open context.
1416 * Either uncache it (if its not referenced in
1417 * the open context) or reset its contents to
1420 dbuf_fix_old_data(db
, txg
);
1423 /* clear the contents if its cached */
1424 if (db
->db_state
== DB_CACHED
) {
1425 ASSERT(db
->db
.db_data
!= NULL
);
1426 arc_release(db
->db_buf
, db
);
1427 bzero(db
->db
.db_data
, db
->db
.db_size
);
1428 arc_buf_freeze(db
->db_buf
);
1431 mutex_exit(&db
->db_mtx
);
1435 kmem_free(db_search
, sizeof (dmu_buf_impl_t
));
1436 mutex_exit(&dn
->dn_dbufs_mtx
);
1440 dbuf_block_freeable(dmu_buf_impl_t
*db
)
1442 dsl_dataset_t
*ds
= db
->db_objset
->os_dsl_dataset
;
1443 uint64_t birth_txg
= 0;
1446 * We don't need any locking to protect db_blkptr:
1447 * If it's syncing, then db_last_dirty will be set
1448 * so we'll ignore db_blkptr.
1450 * This logic ensures that only block births for
1451 * filled blocks are considered.
1453 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1454 if (db
->db_last_dirty
&& (db
->db_blkptr
== NULL
||
1455 !BP_IS_HOLE(db
->db_blkptr
))) {
1456 birth_txg
= db
->db_last_dirty
->dr_txg
;
1457 } else if (db
->db_blkptr
!= NULL
&& !BP_IS_HOLE(db
->db_blkptr
)) {
1458 birth_txg
= db
->db_blkptr
->blk_birth
;
1462 * If this block don't exist or is in a snapshot, it can't be freed.
1463 * Don't pass the bp to dsl_dataset_block_freeable() since we
1464 * are holding the db_mtx lock and might deadlock if we are
1465 * prefetching a dedup-ed block.
1468 return (ds
== NULL
||
1469 dsl_dataset_block_freeable(ds
, NULL
, birth_txg
));
1475 dbuf_new_size(dmu_buf_impl_t
*db
, int size
, dmu_tx_t
*tx
)
1477 arc_buf_t
*buf
, *obuf
;
1478 int osize
= db
->db
.db_size
;
1479 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1482 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1487 /* XXX does *this* func really need the lock? */
1488 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
1491 * This call to dmu_buf_will_dirty() with the dn_struct_rwlock held
1492 * is OK, because there can be no other references to the db
1493 * when we are changing its size, so no concurrent DB_FILL can
1497 * XXX we should be doing a dbuf_read, checking the return
1498 * value and returning that up to our callers
1500 dmu_buf_will_dirty(&db
->db
, tx
);
1502 /* create the data buffer for the new block */
1503 buf
= arc_alloc_buf(dn
->dn_objset
->os_spa
, db
, type
, size
);
1505 /* copy old block data to the new block */
1507 bcopy(obuf
->b_data
, buf
->b_data
, MIN(osize
, size
));
1508 /* zero the remainder */
1510 bzero((uint8_t *)buf
->b_data
+ osize
, size
- osize
);
1512 mutex_enter(&db
->db_mtx
);
1513 dbuf_set_data(db
, buf
);
1514 arc_buf_destroy(obuf
, db
);
1515 db
->db
.db_size
= size
;
1517 if (db
->db_level
== 0) {
1518 ASSERT3U(db
->db_last_dirty
->dr_txg
, ==, tx
->tx_txg
);
1519 db
->db_last_dirty
->dt
.dl
.dr_data
= buf
;
1521 mutex_exit(&db
->db_mtx
);
1523 dnode_willuse_space(dn
, size
-osize
, tx
);
1528 dbuf_release_bp(dmu_buf_impl_t
*db
)
1530 ASSERTV(objset_t
*os
= db
->db_objset
);
1532 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os
)));
1533 ASSERT(arc_released(os
->os_phys_buf
) ||
1534 list_link_active(&os
->os_dsl_dataset
->ds_synced_link
));
1535 ASSERT(db
->db_parent
== NULL
|| arc_released(db
->db_parent
->db_buf
));
1537 (void) arc_release(db
->db_buf
, db
);
1541 * We already have a dirty record for this TXG, and we are being
1545 dbuf_redirty(dbuf_dirty_record_t
*dr
)
1547 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
1549 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1551 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
) {
1553 * If this buffer has already been written out,
1554 * we now need to reset its state.
1556 dbuf_unoverride(dr
);
1557 if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
&&
1558 db
->db_state
!= DB_NOFILL
) {
1559 /* Already released on initial dirty, so just thaw. */
1560 ASSERT(arc_released(db
->db_buf
));
1561 arc_buf_thaw(db
->db_buf
);
1566 dbuf_dirty_record_t
*
1567 dbuf_dirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1571 dbuf_dirty_record_t
**drp
, *dr
;
1572 int drop_struct_lock
= FALSE
;
1573 boolean_t do_free_accounting
= B_FALSE
;
1574 int txgoff
= tx
->tx_txg
& TXG_MASK
;
1576 ASSERT(tx
->tx_txg
!= 0);
1577 ASSERT(!refcount_is_zero(&db
->db_holds
));
1578 DMU_TX_DIRTY_BUF(tx
, db
);
1583 * Shouldn't dirty a regular buffer in syncing context. Private
1584 * objects may be dirtied in syncing context, but only if they
1585 * were already pre-dirtied in open context.
1587 ASSERT(!dmu_tx_is_syncing(tx
) ||
1588 BP_IS_HOLE(dn
->dn_objset
->os_rootbp
) ||
1589 DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
1590 dn
->dn_objset
->os_dsl_dataset
== NULL
);
1592 * We make this assert for private objects as well, but after we
1593 * check if we're already dirty. They are allowed to re-dirty
1594 * in syncing context.
1596 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
1597 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
1598 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
1600 mutex_enter(&db
->db_mtx
);
1602 * XXX make this true for indirects too? The problem is that
1603 * transactions created with dmu_tx_create_assigned() from
1604 * syncing context don't bother holding ahead.
1606 ASSERT(db
->db_level
!= 0 ||
1607 db
->db_state
== DB_CACHED
|| db
->db_state
== DB_FILL
||
1608 db
->db_state
== DB_NOFILL
);
1610 mutex_enter(&dn
->dn_mtx
);
1612 * Don't set dirtyctx to SYNC if we're just modifying this as we
1613 * initialize the objset.
1615 if (dn
->dn_dirtyctx
== DN_UNDIRTIED
&&
1616 !BP_IS_HOLE(dn
->dn_objset
->os_rootbp
)) {
1618 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
);
1619 ASSERT(dn
->dn_dirtyctx_firstset
== NULL
);
1620 dn
->dn_dirtyctx_firstset
= kmem_alloc(1, KM_SLEEP
);
1622 mutex_exit(&dn
->dn_mtx
);
1624 if (db
->db_blkid
== DMU_SPILL_BLKID
)
1625 dn
->dn_have_spill
= B_TRUE
;
1628 * If this buffer is already dirty, we're done.
1630 drp
= &db
->db_last_dirty
;
1631 ASSERT(*drp
== NULL
|| (*drp
)->dr_txg
<= tx
->tx_txg
||
1632 db
->db
.db_object
== DMU_META_DNODE_OBJECT
);
1633 while ((dr
= *drp
) != NULL
&& dr
->dr_txg
> tx
->tx_txg
)
1635 if (dr
&& dr
->dr_txg
== tx
->tx_txg
) {
1639 mutex_exit(&db
->db_mtx
);
1644 * Only valid if not already dirty.
1646 ASSERT(dn
->dn_object
== 0 ||
1647 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
1648 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
1650 ASSERT3U(dn
->dn_nlevels
, >, db
->db_level
);
1651 ASSERT((dn
->dn_phys
->dn_nlevels
== 0 && db
->db_level
== 0) ||
1652 dn
->dn_phys
->dn_nlevels
> db
->db_level
||
1653 dn
->dn_next_nlevels
[txgoff
] > db
->db_level
||
1654 dn
->dn_next_nlevels
[(tx
->tx_txg
-1) & TXG_MASK
] > db
->db_level
||
1655 dn
->dn_next_nlevels
[(tx
->tx_txg
-2) & TXG_MASK
] > db
->db_level
);
1658 * We should only be dirtying in syncing context if it's the
1659 * mos or we're initializing the os or it's a special object.
1660 * However, we are allowed to dirty in syncing context provided
1661 * we already dirtied it in open context. Hence we must make
1662 * this assertion only if we're not already dirty.
1665 ASSERT(!dmu_tx_is_syncing(tx
) || DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
1666 os
->os_dsl_dataset
== NULL
|| BP_IS_HOLE(os
->os_rootbp
));
1667 ASSERT(db
->db
.db_size
!= 0);
1669 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
1671 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
1673 * Update the accounting.
1674 * Note: we delay "free accounting" until after we drop
1675 * the db_mtx. This keeps us from grabbing other locks
1676 * (and possibly deadlocking) in bp_get_dsize() while
1677 * also holding the db_mtx.
1679 dnode_willuse_space(dn
, db
->db
.db_size
, tx
);
1680 do_free_accounting
= dbuf_block_freeable(db
);
1684 * If this buffer is dirty in an old transaction group we need
1685 * to make a copy of it so that the changes we make in this
1686 * transaction group won't leak out when we sync the older txg.
1688 dr
= kmem_zalloc(sizeof (dbuf_dirty_record_t
), KM_SLEEP
);
1689 list_link_init(&dr
->dr_dirty_node
);
1690 if (db
->db_level
== 0) {
1691 void *data_old
= db
->db_buf
;
1693 if (db
->db_state
!= DB_NOFILL
) {
1694 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1695 dbuf_fix_old_data(db
, tx
->tx_txg
);
1696 data_old
= db
->db
.db_data
;
1697 } else if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
) {
1699 * Release the data buffer from the cache so
1700 * that we can modify it without impacting
1701 * possible other users of this cached data
1702 * block. Note that indirect blocks and
1703 * private objects are not released until the
1704 * syncing state (since they are only modified
1707 arc_release(db
->db_buf
, db
);
1708 dbuf_fix_old_data(db
, tx
->tx_txg
);
1709 data_old
= db
->db_buf
;
1711 ASSERT(data_old
!= NULL
);
1713 dr
->dt
.dl
.dr_data
= data_old
;
1715 mutex_init(&dr
->dt
.di
.dr_mtx
, NULL
, MUTEX_NOLOCKDEP
, NULL
);
1716 list_create(&dr
->dt
.di
.dr_children
,
1717 sizeof (dbuf_dirty_record_t
),
1718 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
1720 if (db
->db_blkid
!= DMU_BONUS_BLKID
&& os
->os_dsl_dataset
!= NULL
)
1721 dr
->dr_accounted
= db
->db
.db_size
;
1723 dr
->dr_txg
= tx
->tx_txg
;
1728 * We could have been freed_in_flight between the dbuf_noread
1729 * and dbuf_dirty. We win, as though the dbuf_noread() had
1730 * happened after the free.
1732 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1733 db
->db_blkid
!= DMU_SPILL_BLKID
) {
1734 mutex_enter(&dn
->dn_mtx
);
1735 if (dn
->dn_free_ranges
[txgoff
] != NULL
) {
1736 range_tree_clear(dn
->dn_free_ranges
[txgoff
],
1739 mutex_exit(&dn
->dn_mtx
);
1740 db
->db_freed_in_flight
= FALSE
;
1744 * This buffer is now part of this txg
1746 dbuf_add_ref(db
, (void *)(uintptr_t)tx
->tx_txg
);
1747 db
->db_dirtycnt
+= 1;
1748 ASSERT3U(db
->db_dirtycnt
, <=, 3);
1750 mutex_exit(&db
->db_mtx
);
1752 if (db
->db_blkid
== DMU_BONUS_BLKID
||
1753 db
->db_blkid
== DMU_SPILL_BLKID
) {
1754 mutex_enter(&dn
->dn_mtx
);
1755 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1756 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
1757 mutex_exit(&dn
->dn_mtx
);
1758 dnode_setdirty(dn
, tx
);
1764 * The dn_struct_rwlock prevents db_blkptr from changing
1765 * due to a write from syncing context completing
1766 * while we are running, so we want to acquire it before
1767 * looking at db_blkptr.
1769 if (!RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
1770 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
1771 drop_struct_lock
= TRUE
;
1774 if (do_free_accounting
) {
1775 blkptr_t
*bp
= db
->db_blkptr
;
1776 int64_t willfree
= (bp
&& !BP_IS_HOLE(bp
)) ?
1777 bp_get_dsize(os
->os_spa
, bp
) : db
->db
.db_size
;
1779 * This is only a guess -- if the dbuf is dirty
1780 * in a previous txg, we don't know how much
1781 * space it will use on disk yet. We should
1782 * really have the struct_rwlock to access
1783 * db_blkptr, but since this is just a guess,
1784 * it's OK if we get an odd answer.
1786 ddt_prefetch(os
->os_spa
, bp
);
1787 dnode_willuse_space(dn
, -willfree
, tx
);
1790 if (db
->db_level
== 0) {
1791 dnode_new_blkid(dn
, db
->db_blkid
, tx
, drop_struct_lock
);
1792 ASSERT(dn
->dn_maxblkid
>= db
->db_blkid
);
1795 if (db
->db_level
+1 < dn
->dn_nlevels
) {
1796 dmu_buf_impl_t
*parent
= db
->db_parent
;
1797 dbuf_dirty_record_t
*di
;
1798 int parent_held
= FALSE
;
1800 if (db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
) {
1801 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
1803 parent
= dbuf_hold_level(dn
, db
->db_level
+1,
1804 db
->db_blkid
>> epbs
, FTAG
);
1805 ASSERT(parent
!= NULL
);
1808 if (drop_struct_lock
)
1809 rw_exit(&dn
->dn_struct_rwlock
);
1810 ASSERT3U(db
->db_level
+1, ==, parent
->db_level
);
1811 di
= dbuf_dirty(parent
, tx
);
1813 dbuf_rele(parent
, FTAG
);
1815 mutex_enter(&db
->db_mtx
);
1817 * Since we've dropped the mutex, it's possible that
1818 * dbuf_undirty() might have changed this out from under us.
1820 if (db
->db_last_dirty
== dr
||
1821 dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
1822 mutex_enter(&di
->dt
.di
.dr_mtx
);
1823 ASSERT3U(di
->dr_txg
, ==, tx
->tx_txg
);
1824 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1825 list_insert_tail(&di
->dt
.di
.dr_children
, dr
);
1826 mutex_exit(&di
->dt
.di
.dr_mtx
);
1829 mutex_exit(&db
->db_mtx
);
1831 ASSERT(db
->db_level
+1 == dn
->dn_nlevels
);
1832 ASSERT(db
->db_blkid
< dn
->dn_nblkptr
);
1833 ASSERT(db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
1834 mutex_enter(&dn
->dn_mtx
);
1835 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1836 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
1837 mutex_exit(&dn
->dn_mtx
);
1838 if (drop_struct_lock
)
1839 rw_exit(&dn
->dn_struct_rwlock
);
1842 dnode_setdirty(dn
, tx
);
1848 * Undirty a buffer in the transaction group referenced by the given
1849 * transaction. Return whether this evicted the dbuf.
1852 dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1855 uint64_t txg
= tx
->tx_txg
;
1856 dbuf_dirty_record_t
*dr
, **drp
;
1861 * Due to our use of dn_nlevels below, this can only be called
1862 * in open context, unless we are operating on the MOS.
1863 * From syncing context, dn_nlevels may be different from the
1864 * dn_nlevels used when dbuf was dirtied.
1866 ASSERT(db
->db_objset
==
1867 dmu_objset_pool(db
->db_objset
)->dp_meta_objset
||
1868 txg
!= spa_syncing_txg(dmu_objset_spa(db
->db_objset
)));
1869 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1870 ASSERT0(db
->db_level
);
1871 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1874 * If this buffer is not dirty, we're done.
1876 for (drp
= &db
->db_last_dirty
; (dr
= *drp
) != NULL
; drp
= &dr
->dr_next
)
1877 if (dr
->dr_txg
<= txg
)
1879 if (dr
== NULL
|| dr
->dr_txg
< txg
)
1881 ASSERT(dr
->dr_txg
== txg
);
1882 ASSERT(dr
->dr_dbuf
== db
);
1887 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
1889 ASSERT(db
->db
.db_size
!= 0);
1891 dsl_pool_undirty_space(dmu_objset_pool(dn
->dn_objset
),
1892 dr
->dr_accounted
, txg
);
1897 * Note that there are three places in dbuf_dirty()
1898 * where this dirty record may be put on a list.
1899 * Make sure to do a list_remove corresponding to
1900 * every one of those list_insert calls.
1902 if (dr
->dr_parent
) {
1903 mutex_enter(&dr
->dr_parent
->dt
.di
.dr_mtx
);
1904 list_remove(&dr
->dr_parent
->dt
.di
.dr_children
, dr
);
1905 mutex_exit(&dr
->dr_parent
->dt
.di
.dr_mtx
);
1906 } else if (db
->db_blkid
== DMU_SPILL_BLKID
||
1907 db
->db_level
+ 1 == dn
->dn_nlevels
) {
1908 ASSERT(db
->db_blkptr
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
1909 mutex_enter(&dn
->dn_mtx
);
1910 list_remove(&dn
->dn_dirty_records
[txg
& TXG_MASK
], dr
);
1911 mutex_exit(&dn
->dn_mtx
);
1915 if (db
->db_state
!= DB_NOFILL
) {
1916 dbuf_unoverride(dr
);
1918 ASSERT(db
->db_buf
!= NULL
);
1919 ASSERT(dr
->dt
.dl
.dr_data
!= NULL
);
1920 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
1921 arc_buf_destroy(dr
->dt
.dl
.dr_data
, db
);
1924 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
1926 ASSERT(db
->db_dirtycnt
> 0);
1927 db
->db_dirtycnt
-= 1;
1929 if (refcount_remove(&db
->db_holds
, (void *)(uintptr_t)txg
) == 0) {
1930 ASSERT(db
->db_state
== DB_NOFILL
|| arc_released(db
->db_buf
));
1939 dmu_buf_will_dirty(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1941 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1942 int rf
= DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
;
1943 dbuf_dirty_record_t
*dr
;
1945 ASSERT(tx
->tx_txg
!= 0);
1946 ASSERT(!refcount_is_zero(&db
->db_holds
));
1949 * Quick check for dirtyness. For already dirty blocks, this
1950 * reduces runtime of this function by >90%, and overall performance
1951 * by 50% for some workloads (e.g. file deletion with indirect blocks
1954 mutex_enter(&db
->db_mtx
);
1956 for (dr
= db
->db_last_dirty
;
1957 dr
!= NULL
&& dr
->dr_txg
>= tx
->tx_txg
; dr
= dr
->dr_next
) {
1959 * It's possible that it is already dirty but not cached,
1960 * because there are some calls to dbuf_dirty() that don't
1961 * go through dmu_buf_will_dirty().
1963 if (dr
->dr_txg
== tx
->tx_txg
&& db
->db_state
== DB_CACHED
) {
1964 /* This dbuf is already dirty and cached. */
1966 mutex_exit(&db
->db_mtx
);
1970 mutex_exit(&db
->db_mtx
);
1973 if (RW_WRITE_HELD(&DB_DNODE(db
)->dn_struct_rwlock
))
1974 rf
|= DB_RF_HAVESTRUCT
;
1976 (void) dbuf_read(db
, NULL
, rf
);
1977 (void) dbuf_dirty(db
, tx
);
1981 dmu_buf_will_not_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1983 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1985 db
->db_state
= DB_NOFILL
;
1987 dmu_buf_will_fill(db_fake
, tx
);
1991 dmu_buf_will_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
1993 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1995 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1996 ASSERT(tx
->tx_txg
!= 0);
1997 ASSERT(db
->db_level
== 0);
1998 ASSERT(!refcount_is_zero(&db
->db_holds
));
2000 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
||
2001 dmu_tx_private_ok(tx
));
2004 (void) dbuf_dirty(db
, tx
);
2007 #pragma weak dmu_buf_fill_done = dbuf_fill_done
2010 dbuf_fill_done(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
2012 mutex_enter(&db
->db_mtx
);
2015 if (db
->db_state
== DB_FILL
) {
2016 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
2017 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2018 /* we were freed while filling */
2019 /* XXX dbuf_undirty? */
2020 bzero(db
->db
.db_data
, db
->db
.db_size
);
2021 db
->db_freed_in_flight
= FALSE
;
2023 db
->db_state
= DB_CACHED
;
2024 cv_broadcast(&db
->db_changed
);
2026 mutex_exit(&db
->db_mtx
);
2030 dmu_buf_write_embedded(dmu_buf_t
*dbuf
, void *data
,
2031 bp_embedded_type_t etype
, enum zio_compress comp
,
2032 int uncompressed_size
, int compressed_size
, int byteorder
,
2035 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbuf
;
2036 struct dirty_leaf
*dl
;
2037 dmu_object_type_t type
;
2039 if (etype
== BP_EMBEDDED_TYPE_DATA
) {
2040 ASSERT(spa_feature_is_active(dmu_objset_spa(db
->db_objset
),
2041 SPA_FEATURE_EMBEDDED_DATA
));
2045 type
= DB_DNODE(db
)->dn_type
;
2048 ASSERT0(db
->db_level
);
2049 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2051 dmu_buf_will_not_fill(dbuf
, tx
);
2053 ASSERT3U(db
->db_last_dirty
->dr_txg
, ==, tx
->tx_txg
);
2054 dl
= &db
->db_last_dirty
->dt
.dl
;
2055 encode_embedded_bp_compressed(&dl
->dr_overridden_by
,
2056 data
, comp
, uncompressed_size
, compressed_size
);
2057 BPE_SET_ETYPE(&dl
->dr_overridden_by
, etype
);
2058 BP_SET_TYPE(&dl
->dr_overridden_by
, type
);
2059 BP_SET_LEVEL(&dl
->dr_overridden_by
, 0);
2060 BP_SET_BYTEORDER(&dl
->dr_overridden_by
, byteorder
);
2062 dl
->dr_override_state
= DR_OVERRIDDEN
;
2063 dl
->dr_overridden_by
.blk_birth
= db
->db_last_dirty
->dr_txg
;
2067 * Directly assign a provided arc buf to a given dbuf if it's not referenced
2068 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
2071 dbuf_assign_arcbuf(dmu_buf_impl_t
*db
, arc_buf_t
*buf
, dmu_tx_t
*tx
)
2073 ASSERT(!refcount_is_zero(&db
->db_holds
));
2074 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2075 ASSERT(db
->db_level
== 0);
2076 ASSERT3U(dbuf_is_metadata(db
), ==, arc_is_metadata(buf
));
2077 ASSERT(buf
!= NULL
);
2078 ASSERT(arc_buf_lsize(buf
) == db
->db
.db_size
);
2079 ASSERT(tx
->tx_txg
!= 0);
2081 arc_return_buf(buf
, db
);
2082 ASSERT(arc_released(buf
));
2084 mutex_enter(&db
->db_mtx
);
2086 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
2087 cv_wait(&db
->db_changed
, &db
->db_mtx
);
2089 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_UNCACHED
);
2091 if (db
->db_state
== DB_CACHED
&&
2092 refcount_count(&db
->db_holds
) - 1 > db
->db_dirtycnt
) {
2093 mutex_exit(&db
->db_mtx
);
2094 (void) dbuf_dirty(db
, tx
);
2095 bcopy(buf
->b_data
, db
->db
.db_data
, db
->db
.db_size
);
2096 arc_buf_destroy(buf
, db
);
2097 xuio_stat_wbuf_copied();
2101 xuio_stat_wbuf_nocopy();
2102 if (db
->db_state
== DB_CACHED
) {
2103 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
2105 ASSERT(db
->db_buf
!= NULL
);
2106 if (dr
!= NULL
&& dr
->dr_txg
== tx
->tx_txg
) {
2107 ASSERT(dr
->dt
.dl
.dr_data
== db
->db_buf
);
2108 if (!arc_released(db
->db_buf
)) {
2109 ASSERT(dr
->dt
.dl
.dr_override_state
==
2111 arc_release(db
->db_buf
, db
);
2113 dr
->dt
.dl
.dr_data
= buf
;
2114 arc_buf_destroy(db
->db_buf
, db
);
2115 } else if (dr
== NULL
|| dr
->dt
.dl
.dr_data
!= db
->db_buf
) {
2116 arc_release(db
->db_buf
, db
);
2117 arc_buf_destroy(db
->db_buf
, db
);
2121 ASSERT(db
->db_buf
== NULL
);
2122 dbuf_set_data(db
, buf
);
2123 db
->db_state
= DB_FILL
;
2124 mutex_exit(&db
->db_mtx
);
2125 (void) dbuf_dirty(db
, tx
);
2126 dmu_buf_fill_done(&db
->db
, tx
);
2130 dbuf_destroy(dmu_buf_impl_t
*db
)
2133 dmu_buf_impl_t
*parent
= db
->db_parent
;
2134 dmu_buf_impl_t
*dndb
;
2136 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2137 ASSERT(refcount_is_zero(&db
->db_holds
));
2139 if (db
->db_buf
!= NULL
) {
2140 arc_buf_destroy(db
->db_buf
, db
);
2144 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2145 int slots
= DB_DNODE(db
)->dn_num_slots
;
2146 int bonuslen
= DN_SLOTS_TO_BONUSLEN(slots
);
2147 ASSERT(db
->db
.db_data
!= NULL
);
2148 zio_buf_free(db
->db
.db_data
, bonuslen
);
2149 arc_space_return(bonuslen
, ARC_SPACE_BONUS
);
2150 db
->db_state
= DB_UNCACHED
;
2153 dbuf_clear_data(db
);
2155 if (multilist_link_active(&db
->db_cache_link
)) {
2156 multilist_remove(&dbuf_cache
, db
);
2157 (void) refcount_remove_many(&dbuf_cache_size
,
2158 db
->db
.db_size
, db
);
2161 ASSERT(db
->db_state
== DB_UNCACHED
|| db
->db_state
== DB_NOFILL
);
2162 ASSERT(db
->db_data_pending
== NULL
);
2164 db
->db_state
= DB_EVICTING
;
2165 db
->db_blkptr
= NULL
;
2168 * Now that db_state is DB_EVICTING, nobody else can find this via
2169 * the hash table. We can now drop db_mtx, which allows us to
2170 * acquire the dn_dbufs_mtx.
2172 mutex_exit(&db
->db_mtx
);
2177 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
2178 boolean_t needlock
= !MUTEX_HELD(&dn
->dn_dbufs_mtx
);
2180 mutex_enter(&dn
->dn_dbufs_mtx
);
2181 avl_remove(&dn
->dn_dbufs
, db
);
2182 atomic_dec_32(&dn
->dn_dbufs_count
);
2186 mutex_exit(&dn
->dn_dbufs_mtx
);
2188 * Decrementing the dbuf count means that the hold corresponding
2189 * to the removed dbuf is no longer discounted in dnode_move(),
2190 * so the dnode cannot be moved until after we release the hold.
2191 * The membar_producer() ensures visibility of the decremented
2192 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
2196 db
->db_dnode_handle
= NULL
;
2198 dbuf_hash_remove(db
);
2203 ASSERT(refcount_is_zero(&db
->db_holds
));
2205 db
->db_parent
= NULL
;
2207 ASSERT(db
->db_buf
== NULL
);
2208 ASSERT(db
->db
.db_data
== NULL
);
2209 ASSERT(db
->db_hash_next
== NULL
);
2210 ASSERT(db
->db_blkptr
== NULL
);
2211 ASSERT(db
->db_data_pending
== NULL
);
2212 ASSERT(!multilist_link_active(&db
->db_cache_link
));
2214 kmem_cache_free(dbuf_kmem_cache
, db
);
2215 arc_space_return(sizeof (dmu_buf_impl_t
), ARC_SPACE_DBUF
);
2218 * If this dbuf is referenced from an indirect dbuf,
2219 * decrement the ref count on the indirect dbuf.
2221 if (parent
&& parent
!= dndb
)
2222 dbuf_rele(parent
, db
);
2226 * Note: While bpp will always be updated if the function returns success,
2227 * parentp will not be updated if the dnode does not have dn_dbuf filled in;
2228 * this happens when the dnode is the meta-dnode, or a userused or groupused
2231 __attribute__((always_inline
))
2233 dbuf_findbp(dnode_t
*dn
, int level
, uint64_t blkid
, int fail_sparse
,
2234 dmu_buf_impl_t
**parentp
, blkptr_t
**bpp
, struct dbuf_hold_impl_data
*dh
)
2241 ASSERT(blkid
!= DMU_BONUS_BLKID
);
2243 if (blkid
== DMU_SPILL_BLKID
) {
2244 mutex_enter(&dn
->dn_mtx
);
2245 if (dn
->dn_have_spill
&&
2246 (dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
))
2247 *bpp
= DN_SPILL_BLKPTR(dn
->dn_phys
);
2250 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
2251 *parentp
= dn
->dn_dbuf
;
2252 mutex_exit(&dn
->dn_mtx
);
2257 (dn
->dn_phys
->dn_nlevels
== 0) ? 1 : dn
->dn_phys
->dn_nlevels
;
2258 epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2260 ASSERT3U(level
* epbs
, <, 64);
2261 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2263 * This assertion shouldn't trip as long as the max indirect block size
2264 * is less than 1M. The reason for this is that up to that point,
2265 * the number of levels required to address an entire object with blocks
2266 * of size SPA_MINBLOCKSIZE satisfies nlevels * epbs + 1 <= 64. In
2267 * other words, if N * epbs + 1 > 64, then if (N-1) * epbs + 1 > 55
2268 * (i.e. we can address the entire object), objects will all use at most
2269 * N-1 levels and the assertion won't overflow. However, once epbs is
2270 * 13, 4 * 13 + 1 = 53, but 5 * 13 + 1 = 66. Then, 4 levels will not be
2271 * enough to address an entire object, so objects will have 5 levels,
2272 * but then this assertion will overflow.
2274 * All this is to say that if we ever increase DN_MAX_INDBLKSHIFT, we
2275 * need to redo this logic to handle overflows.
2277 ASSERT(level
>= nlevels
||
2278 ((nlevels
- level
- 1) * epbs
) +
2279 highbit64(dn
->dn_phys
->dn_nblkptr
) <= 64);
2280 if (level
>= nlevels
||
2281 blkid
>= ((uint64_t)dn
->dn_phys
->dn_nblkptr
<<
2282 ((nlevels
- level
- 1) * epbs
)) ||
2284 blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))) {
2285 /* the buffer has no parent yet */
2286 return (SET_ERROR(ENOENT
));
2287 } else if (level
< nlevels
-1) {
2288 /* this block is referenced from an indirect block */
2291 err
= dbuf_hold_impl(dn
, level
+1,
2292 blkid
>> epbs
, fail_sparse
, FALSE
, NULL
, parentp
);
2294 __dbuf_hold_impl_init(dh
+ 1, dn
, dh
->dh_level
+ 1,
2295 blkid
>> epbs
, fail_sparse
, FALSE
, NULL
,
2296 parentp
, dh
->dh_depth
+ 1);
2297 err
= __dbuf_hold_impl(dh
+ 1);
2301 err
= dbuf_read(*parentp
, NULL
,
2302 (DB_RF_HAVESTRUCT
| DB_RF_NOPREFETCH
| DB_RF_CANFAIL
));
2304 dbuf_rele(*parentp
, NULL
);
2308 *bpp
= ((blkptr_t
*)(*parentp
)->db
.db_data
) +
2309 (blkid
& ((1ULL << epbs
) - 1));
2310 if (blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))
2311 ASSERT(BP_IS_HOLE(*bpp
));
2314 /* the block is referenced from the dnode */
2315 ASSERT3U(level
, ==, nlevels
-1);
2316 ASSERT(dn
->dn_phys
->dn_nblkptr
== 0 ||
2317 blkid
< dn
->dn_phys
->dn_nblkptr
);
2319 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
2320 *parentp
= dn
->dn_dbuf
;
2322 *bpp
= &dn
->dn_phys
->dn_blkptr
[blkid
];
2327 static dmu_buf_impl_t
*
2328 dbuf_create(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
2329 dmu_buf_impl_t
*parent
, blkptr_t
*blkptr
)
2331 objset_t
*os
= dn
->dn_objset
;
2332 dmu_buf_impl_t
*db
, *odb
;
2334 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2335 ASSERT(dn
->dn_type
!= DMU_OT_NONE
);
2337 db
= kmem_cache_alloc(dbuf_kmem_cache
, KM_SLEEP
);
2340 db
->db
.db_object
= dn
->dn_object
;
2341 db
->db_level
= level
;
2342 db
->db_blkid
= blkid
;
2343 db
->db_last_dirty
= NULL
;
2344 db
->db_dirtycnt
= 0;
2345 db
->db_dnode_handle
= dn
->dn_handle
;
2346 db
->db_parent
= parent
;
2347 db
->db_blkptr
= blkptr
;
2350 db
->db_user_immediate_evict
= FALSE
;
2351 db
->db_freed_in_flight
= FALSE
;
2352 db
->db_pending_evict
= FALSE
;
2354 if (blkid
== DMU_BONUS_BLKID
) {
2355 ASSERT3P(parent
, ==, dn
->dn_dbuf
);
2356 db
->db
.db_size
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
) -
2357 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
);
2358 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
2359 db
->db
.db_offset
= DMU_BONUS_BLKID
;
2360 db
->db_state
= DB_UNCACHED
;
2361 /* the bonus dbuf is not placed in the hash table */
2362 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_DBUF
);
2364 } else if (blkid
== DMU_SPILL_BLKID
) {
2365 db
->db
.db_size
= (blkptr
!= NULL
) ?
2366 BP_GET_LSIZE(blkptr
) : SPA_MINBLOCKSIZE
;
2367 db
->db
.db_offset
= 0;
2370 db
->db_level
? 1 << dn
->dn_indblkshift
: dn
->dn_datablksz
;
2371 db
->db
.db_size
= blocksize
;
2372 db
->db
.db_offset
= db
->db_blkid
* blocksize
;
2376 * Hold the dn_dbufs_mtx while we get the new dbuf
2377 * in the hash table *and* added to the dbufs list.
2378 * This prevents a possible deadlock with someone
2379 * trying to look up this dbuf before its added to the
2382 mutex_enter(&dn
->dn_dbufs_mtx
);
2383 db
->db_state
= DB_EVICTING
;
2384 if ((odb
= dbuf_hash_insert(db
)) != NULL
) {
2385 /* someone else inserted it first */
2386 kmem_cache_free(dbuf_kmem_cache
, db
);
2387 mutex_exit(&dn
->dn_dbufs_mtx
);
2390 avl_add(&dn
->dn_dbufs
, db
);
2391 if (db
->db_level
== 0 && db
->db_blkid
>=
2392 dn
->dn_unlisted_l0_blkid
)
2393 dn
->dn_unlisted_l0_blkid
= db
->db_blkid
+ 1;
2394 db
->db_state
= DB_UNCACHED
;
2395 mutex_exit(&dn
->dn_dbufs_mtx
);
2396 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_DBUF
);
2398 if (parent
&& parent
!= dn
->dn_dbuf
)
2399 dbuf_add_ref(parent
, db
);
2401 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
2402 refcount_count(&dn
->dn_holds
) > 0);
2403 (void) refcount_add(&dn
->dn_holds
, db
);
2404 atomic_inc_32(&dn
->dn_dbufs_count
);
2406 dprintf_dbuf(db
, "db=%p\n", db
);
2411 typedef struct dbuf_prefetch_arg
{
2412 spa_t
*dpa_spa
; /* The spa to issue the prefetch in. */
2413 zbookmark_phys_t dpa_zb
; /* The target block to prefetch. */
2414 int dpa_epbs
; /* Entries (blkptr_t's) Per Block Shift. */
2415 int dpa_curlevel
; /* The current level that we're reading */
2416 dnode_t
*dpa_dnode
; /* The dnode associated with the prefetch */
2417 zio_priority_t dpa_prio
; /* The priority I/Os should be issued at. */
2418 zio_t
*dpa_zio
; /* The parent zio_t for all prefetches. */
2419 arc_flags_t dpa_aflags
; /* Flags to pass to the final prefetch. */
2420 } dbuf_prefetch_arg_t
;
2423 * Actually issue the prefetch read for the block given.
2426 dbuf_issue_final_prefetch(dbuf_prefetch_arg_t
*dpa
, blkptr_t
*bp
)
2429 if (BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
))
2432 aflags
= dpa
->dpa_aflags
| ARC_FLAG_NOWAIT
| ARC_FLAG_PREFETCH
;
2434 ASSERT3U(dpa
->dpa_curlevel
, ==, BP_GET_LEVEL(bp
));
2435 ASSERT3U(dpa
->dpa_curlevel
, ==, dpa
->dpa_zb
.zb_level
);
2436 ASSERT(dpa
->dpa_zio
!= NULL
);
2437 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
, bp
, NULL
, NULL
,
2438 dpa
->dpa_prio
, ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
2439 &aflags
, &dpa
->dpa_zb
);
2443 * Called when an indirect block above our prefetch target is read in. This
2444 * will either read in the next indirect block down the tree or issue the actual
2445 * prefetch if the next block down is our target.
2448 dbuf_prefetch_indirect_done(zio_t
*zio
, arc_buf_t
*abuf
, void *private)
2450 dbuf_prefetch_arg_t
*dpa
= private;
2454 ASSERT3S(dpa
->dpa_zb
.zb_level
, <, dpa
->dpa_curlevel
);
2455 ASSERT3S(dpa
->dpa_curlevel
, >, 0);
2458 * The dpa_dnode is only valid if we are called with a NULL
2459 * zio. This indicates that the arc_read() returned without
2460 * first calling zio_read() to issue a physical read. Once
2461 * a physical read is made the dpa_dnode must be invalidated
2462 * as the locks guarding it may have been dropped. If the
2463 * dpa_dnode is still valid, then we want to add it to the dbuf
2464 * cache. To do so, we must hold the dbuf associated with the block
2465 * we just prefetched, read its contents so that we associate it
2466 * with an arc_buf_t, and then release it.
2469 ASSERT3S(BP_GET_LEVEL(zio
->io_bp
), ==, dpa
->dpa_curlevel
);
2470 if (zio
->io_flags
& ZIO_FLAG_RAW
) {
2471 ASSERT3U(BP_GET_PSIZE(zio
->io_bp
), ==, zio
->io_size
);
2473 ASSERT3U(BP_GET_LSIZE(zio
->io_bp
), ==, zio
->io_size
);
2475 ASSERT3P(zio
->io_spa
, ==, dpa
->dpa_spa
);
2477 dpa
->dpa_dnode
= NULL
;
2478 } else if (dpa
->dpa_dnode
!= NULL
) {
2479 uint64_t curblkid
= dpa
->dpa_zb
.zb_blkid
>>
2480 (dpa
->dpa_epbs
* (dpa
->dpa_curlevel
-
2481 dpa
->dpa_zb
.zb_level
));
2482 dmu_buf_impl_t
*db
= dbuf_hold_level(dpa
->dpa_dnode
,
2483 dpa
->dpa_curlevel
, curblkid
, FTAG
);
2484 (void) dbuf_read(db
, NULL
,
2485 DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
| DB_RF_HAVESTRUCT
);
2486 dbuf_rele(db
, FTAG
);
2489 dpa
->dpa_curlevel
--;
2491 nextblkid
= dpa
->dpa_zb
.zb_blkid
>>
2492 (dpa
->dpa_epbs
* (dpa
->dpa_curlevel
- dpa
->dpa_zb
.zb_level
));
2493 bp
= ((blkptr_t
*)abuf
->b_data
) +
2494 P2PHASE(nextblkid
, 1ULL << dpa
->dpa_epbs
);
2495 if (BP_IS_HOLE(bp
) || (zio
!= NULL
&& zio
->io_error
!= 0)) {
2496 kmem_free(dpa
, sizeof (*dpa
));
2497 } else if (dpa
->dpa_curlevel
== dpa
->dpa_zb
.zb_level
) {
2498 ASSERT3U(nextblkid
, ==, dpa
->dpa_zb
.zb_blkid
);
2499 dbuf_issue_final_prefetch(dpa
, bp
);
2500 kmem_free(dpa
, sizeof (*dpa
));
2502 arc_flags_t iter_aflags
= ARC_FLAG_NOWAIT
;
2503 zbookmark_phys_t zb
;
2505 ASSERT3U(dpa
->dpa_curlevel
, ==, BP_GET_LEVEL(bp
));
2507 SET_BOOKMARK(&zb
, dpa
->dpa_zb
.zb_objset
,
2508 dpa
->dpa_zb
.zb_object
, dpa
->dpa_curlevel
, nextblkid
);
2510 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
,
2511 bp
, dbuf_prefetch_indirect_done
, dpa
, dpa
->dpa_prio
,
2512 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
2516 arc_buf_destroy(abuf
, private);
2520 * Issue prefetch reads for the given block on the given level. If the indirect
2521 * blocks above that block are not in memory, we will read them in
2522 * asynchronously. As a result, this call never blocks waiting for a read to
2526 dbuf_prefetch(dnode_t
*dn
, int64_t level
, uint64_t blkid
, zio_priority_t prio
,
2530 int epbs
, nlevels
, curlevel
;
2534 dbuf_prefetch_arg_t
*dpa
;
2537 ASSERT(blkid
!= DMU_BONUS_BLKID
);
2538 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2540 if (blkid
> dn
->dn_maxblkid
)
2543 if (dnode_block_freed(dn
, blkid
))
2547 * This dnode hasn't been written to disk yet, so there's nothing to
2550 nlevels
= dn
->dn_phys
->dn_nlevels
;
2551 if (level
>= nlevels
|| dn
->dn_phys
->dn_nblkptr
== 0)
2554 epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2555 if (dn
->dn_phys
->dn_maxblkid
< blkid
<< (epbs
* level
))
2558 db
= dbuf_find(dn
->dn_objset
, dn
->dn_object
,
2561 mutex_exit(&db
->db_mtx
);
2563 * This dbuf already exists. It is either CACHED, or
2564 * (we assume) about to be read or filled.
2570 * Find the closest ancestor (indirect block) of the target block
2571 * that is present in the cache. In this indirect block, we will
2572 * find the bp that is at curlevel, curblkid.
2576 while (curlevel
< nlevels
- 1) {
2577 int parent_level
= curlevel
+ 1;
2578 uint64_t parent_blkid
= curblkid
>> epbs
;
2581 if (dbuf_hold_impl(dn
, parent_level
, parent_blkid
,
2582 FALSE
, TRUE
, FTAG
, &db
) == 0) {
2583 blkptr_t
*bpp
= db
->db_buf
->b_data
;
2584 bp
= bpp
[P2PHASE(curblkid
, 1 << epbs
)];
2585 dbuf_rele(db
, FTAG
);
2589 curlevel
= parent_level
;
2590 curblkid
= parent_blkid
;
2593 if (curlevel
== nlevels
- 1) {
2594 /* No cached indirect blocks found. */
2595 ASSERT3U(curblkid
, <, dn
->dn_phys
->dn_nblkptr
);
2596 bp
= dn
->dn_phys
->dn_blkptr
[curblkid
];
2598 if (BP_IS_HOLE(&bp
))
2601 ASSERT3U(curlevel
, ==, BP_GET_LEVEL(&bp
));
2603 pio
= zio_root(dmu_objset_spa(dn
->dn_objset
), NULL
, NULL
,
2606 dpa
= kmem_zalloc(sizeof (*dpa
), KM_SLEEP
);
2607 ds
= dn
->dn_objset
->os_dsl_dataset
;
2608 SET_BOOKMARK(&dpa
->dpa_zb
, ds
!= NULL
? ds
->ds_object
: DMU_META_OBJSET
,
2609 dn
->dn_object
, level
, blkid
);
2610 dpa
->dpa_curlevel
= curlevel
;
2611 dpa
->dpa_prio
= prio
;
2612 dpa
->dpa_aflags
= aflags
;
2613 dpa
->dpa_spa
= dn
->dn_objset
->os_spa
;
2614 dpa
->dpa_dnode
= dn
;
2615 dpa
->dpa_epbs
= epbs
;
2619 * If we have the indirect just above us, no need to do the asynchronous
2620 * prefetch chain; we'll just run the last step ourselves. If we're at
2621 * a higher level, though, we want to issue the prefetches for all the
2622 * indirect blocks asynchronously, so we can go on with whatever we were
2625 if (curlevel
== level
) {
2626 ASSERT3U(curblkid
, ==, blkid
);
2627 dbuf_issue_final_prefetch(dpa
, &bp
);
2628 kmem_free(dpa
, sizeof (*dpa
));
2630 arc_flags_t iter_aflags
= ARC_FLAG_NOWAIT
;
2631 zbookmark_phys_t zb
;
2633 SET_BOOKMARK(&zb
, ds
!= NULL
? ds
->ds_object
: DMU_META_OBJSET
,
2634 dn
->dn_object
, curlevel
, curblkid
);
2635 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
,
2636 &bp
, dbuf_prefetch_indirect_done
, dpa
, prio
,
2637 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
2641 * We use pio here instead of dpa_zio since it's possible that
2642 * dpa may have already been freed.
2647 #define DBUF_HOLD_IMPL_MAX_DEPTH 20
2650 * Returns with db_holds incremented, and db_mtx not held.
2651 * Note: dn_struct_rwlock must be held.
2654 __dbuf_hold_impl(struct dbuf_hold_impl_data
*dh
)
2656 ASSERT3S(dh
->dh_depth
, <, DBUF_HOLD_IMPL_MAX_DEPTH
);
2657 dh
->dh_parent
= NULL
;
2659 ASSERT(dh
->dh_blkid
!= DMU_BONUS_BLKID
);
2660 ASSERT(RW_LOCK_HELD(&dh
->dh_dn
->dn_struct_rwlock
));
2661 ASSERT3U(dh
->dh_dn
->dn_nlevels
, >, dh
->dh_level
);
2663 *(dh
->dh_dbp
) = NULL
;
2665 /* dbuf_find() returns with db_mtx held */
2666 dh
->dh_db
= dbuf_find(dh
->dh_dn
->dn_objset
, dh
->dh_dn
->dn_object
,
2667 dh
->dh_level
, dh
->dh_blkid
);
2669 if (dh
->dh_db
== NULL
) {
2672 if (dh
->dh_fail_uncached
)
2673 return (SET_ERROR(ENOENT
));
2675 ASSERT3P(dh
->dh_parent
, ==, NULL
);
2676 dh
->dh_err
= dbuf_findbp(dh
->dh_dn
, dh
->dh_level
, dh
->dh_blkid
,
2677 dh
->dh_fail_sparse
, &dh
->dh_parent
,
2679 if (dh
->dh_fail_sparse
) {
2680 if (dh
->dh_err
== 0 &&
2681 dh
->dh_bp
&& BP_IS_HOLE(dh
->dh_bp
))
2682 dh
->dh_err
= SET_ERROR(ENOENT
);
2685 dbuf_rele(dh
->dh_parent
, NULL
);
2686 return (dh
->dh_err
);
2689 if (dh
->dh_err
&& dh
->dh_err
!= ENOENT
)
2690 return (dh
->dh_err
);
2691 dh
->dh_db
= dbuf_create(dh
->dh_dn
, dh
->dh_level
, dh
->dh_blkid
,
2692 dh
->dh_parent
, dh
->dh_bp
);
2695 if (dh
->dh_fail_uncached
&& dh
->dh_db
->db_state
!= DB_CACHED
) {
2696 mutex_exit(&dh
->dh_db
->db_mtx
);
2697 return (SET_ERROR(ENOENT
));
2700 if (dh
->dh_db
->db_buf
!= NULL
)
2701 ASSERT3P(dh
->dh_db
->db
.db_data
, ==, dh
->dh_db
->db_buf
->b_data
);
2703 ASSERT(dh
->dh_db
->db_buf
== NULL
|| arc_referenced(dh
->dh_db
->db_buf
));
2706 * If this buffer is currently syncing out, and we are are
2707 * still referencing it from db_data, we need to make a copy
2708 * of it in case we decide we want to dirty it again in this txg.
2710 if (dh
->dh_db
->db_level
== 0 &&
2711 dh
->dh_db
->db_blkid
!= DMU_BONUS_BLKID
&&
2712 dh
->dh_dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
2713 dh
->dh_db
->db_state
== DB_CACHED
&& dh
->dh_db
->db_data_pending
) {
2714 dh
->dh_dr
= dh
->dh_db
->db_data_pending
;
2716 if (dh
->dh_dr
->dt
.dl
.dr_data
== dh
->dh_db
->db_buf
) {
2717 dh
->dh_type
= DBUF_GET_BUFC_TYPE(dh
->dh_db
);
2719 dbuf_set_data(dh
->dh_db
,
2720 arc_alloc_buf(dh
->dh_dn
->dn_objset
->os_spa
,
2721 dh
->dh_db
, dh
->dh_type
, dh
->dh_db
->db
.db_size
));
2722 bcopy(dh
->dh_dr
->dt
.dl
.dr_data
->b_data
,
2723 dh
->dh_db
->db
.db_data
, dh
->dh_db
->db
.db_size
);
2727 if (multilist_link_active(&dh
->dh_db
->db_cache_link
)) {
2728 ASSERT(refcount_is_zero(&dh
->dh_db
->db_holds
));
2729 multilist_remove(&dbuf_cache
, dh
->dh_db
);
2730 (void) refcount_remove_many(&dbuf_cache_size
,
2731 dh
->dh_db
->db
.db_size
, dh
->dh_db
);
2733 (void) refcount_add(&dh
->dh_db
->db_holds
, dh
->dh_tag
);
2734 DBUF_VERIFY(dh
->dh_db
);
2735 mutex_exit(&dh
->dh_db
->db_mtx
);
2737 /* NOTE: we can't rele the parent until after we drop the db_mtx */
2739 dbuf_rele(dh
->dh_parent
, NULL
);
2741 ASSERT3P(DB_DNODE(dh
->dh_db
), ==, dh
->dh_dn
);
2742 ASSERT3U(dh
->dh_db
->db_blkid
, ==, dh
->dh_blkid
);
2743 ASSERT3U(dh
->dh_db
->db_level
, ==, dh
->dh_level
);
2744 *(dh
->dh_dbp
) = dh
->dh_db
;
2750 * The following code preserves the recursive function dbuf_hold_impl()
2751 * but moves the local variables AND function arguments to the heap to
2752 * minimize the stack frame size. Enough space is initially allocated
2753 * on the stack for 20 levels of recursion.
2756 dbuf_hold_impl(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
2757 boolean_t fail_sparse
, boolean_t fail_uncached
,
2758 void *tag
, dmu_buf_impl_t
**dbp
)
2760 struct dbuf_hold_impl_data
*dh
;
2763 dh
= kmem_alloc(sizeof (struct dbuf_hold_impl_data
) *
2764 DBUF_HOLD_IMPL_MAX_DEPTH
, KM_SLEEP
);
2765 __dbuf_hold_impl_init(dh
, dn
, level
, blkid
, fail_sparse
,
2766 fail_uncached
, tag
, dbp
, 0);
2768 error
= __dbuf_hold_impl(dh
);
2770 kmem_free(dh
, sizeof (struct dbuf_hold_impl_data
) *
2771 DBUF_HOLD_IMPL_MAX_DEPTH
);
2777 __dbuf_hold_impl_init(struct dbuf_hold_impl_data
*dh
,
2778 dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
2779 boolean_t fail_sparse
, boolean_t fail_uncached
,
2780 void *tag
, dmu_buf_impl_t
**dbp
, int depth
)
2783 dh
->dh_level
= level
;
2784 dh
->dh_blkid
= blkid
;
2786 dh
->dh_fail_sparse
= fail_sparse
;
2787 dh
->dh_fail_uncached
= fail_uncached
;
2793 dh
->dh_parent
= NULL
;
2799 dh
->dh_depth
= depth
;
2803 dbuf_hold(dnode_t
*dn
, uint64_t blkid
, void *tag
)
2805 return (dbuf_hold_level(dn
, 0, blkid
, tag
));
2809 dbuf_hold_level(dnode_t
*dn
, int level
, uint64_t blkid
, void *tag
)
2812 int err
= dbuf_hold_impl(dn
, level
, blkid
, FALSE
, FALSE
, tag
, &db
);
2813 return (err
? NULL
: db
);
2817 dbuf_create_bonus(dnode_t
*dn
)
2819 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
2821 ASSERT(dn
->dn_bonus
== NULL
);
2822 dn
->dn_bonus
= dbuf_create(dn
, 0, DMU_BONUS_BLKID
, dn
->dn_dbuf
, NULL
);
2826 dbuf_spill_set_blksz(dmu_buf_t
*db_fake
, uint64_t blksz
, dmu_tx_t
*tx
)
2828 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2831 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
2832 return (SET_ERROR(ENOTSUP
));
2834 blksz
= SPA_MINBLOCKSIZE
;
2835 ASSERT3U(blksz
, <=, spa_maxblocksize(dmu_objset_spa(db
->db_objset
)));
2836 blksz
= P2ROUNDUP(blksz
, SPA_MINBLOCKSIZE
);
2840 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
2841 dbuf_new_size(db
, blksz
, tx
);
2842 rw_exit(&dn
->dn_struct_rwlock
);
2849 dbuf_rm_spill(dnode_t
*dn
, dmu_tx_t
*tx
)
2851 dbuf_free_range(dn
, DMU_SPILL_BLKID
, DMU_SPILL_BLKID
, tx
);
2854 #pragma weak dmu_buf_add_ref = dbuf_add_ref
2856 dbuf_add_ref(dmu_buf_impl_t
*db
, void *tag
)
2858 int64_t holds
= refcount_add(&db
->db_holds
, tag
);
2859 VERIFY3S(holds
, >, 1);
2862 #pragma weak dmu_buf_try_add_ref = dbuf_try_add_ref
2864 dbuf_try_add_ref(dmu_buf_t
*db_fake
, objset_t
*os
, uint64_t obj
, uint64_t blkid
,
2867 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2868 dmu_buf_impl_t
*found_db
;
2869 boolean_t result
= B_FALSE
;
2871 if (blkid
== DMU_BONUS_BLKID
)
2872 found_db
= dbuf_find_bonus(os
, obj
);
2874 found_db
= dbuf_find(os
, obj
, 0, blkid
);
2876 if (found_db
!= NULL
) {
2877 if (db
== found_db
&& dbuf_refcount(db
) > db
->db_dirtycnt
) {
2878 (void) refcount_add(&db
->db_holds
, tag
);
2881 mutex_exit(&found_db
->db_mtx
);
2887 * If you call dbuf_rele() you had better not be referencing the dnode handle
2888 * unless you have some other direct or indirect hold on the dnode. (An indirect
2889 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
2890 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
2891 * dnode's parent dbuf evicting its dnode handles.
2894 dbuf_rele(dmu_buf_impl_t
*db
, void *tag
)
2896 mutex_enter(&db
->db_mtx
);
2897 dbuf_rele_and_unlock(db
, tag
);
2901 dmu_buf_rele(dmu_buf_t
*db
, void *tag
)
2903 dbuf_rele((dmu_buf_impl_t
*)db
, tag
);
2907 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
2908 * db_dirtycnt and db_holds to be updated atomically.
2911 dbuf_rele_and_unlock(dmu_buf_impl_t
*db
, void *tag
)
2915 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2919 * Remove the reference to the dbuf before removing its hold on the
2920 * dnode so we can guarantee in dnode_move() that a referenced bonus
2921 * buffer has a corresponding dnode hold.
2923 holds
= refcount_remove(&db
->db_holds
, tag
);
2927 * We can't freeze indirects if there is a possibility that they
2928 * may be modified in the current syncing context.
2930 if (db
->db_buf
!= NULL
&&
2931 holds
== (db
->db_level
== 0 ? db
->db_dirtycnt
: 0)) {
2932 arc_buf_freeze(db
->db_buf
);
2935 if (holds
== db
->db_dirtycnt
&&
2936 db
->db_level
== 0 && db
->db_user_immediate_evict
)
2937 dbuf_evict_user(db
);
2940 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2942 boolean_t evict_dbuf
= db
->db_pending_evict
;
2945 * If the dnode moves here, we cannot cross this
2946 * barrier until the move completes.
2951 atomic_dec_32(&dn
->dn_dbufs_count
);
2954 * Decrementing the dbuf count means that the bonus
2955 * buffer's dnode hold is no longer discounted in
2956 * dnode_move(). The dnode cannot move until after
2957 * the dnode_rele() below.
2962 * Do not reference db after its lock is dropped.
2963 * Another thread may evict it.
2965 mutex_exit(&db
->db_mtx
);
2968 dnode_evict_bonus(dn
);
2971 } else if (db
->db_buf
== NULL
) {
2973 * This is a special case: we never associated this
2974 * dbuf with any data allocated from the ARC.
2976 ASSERT(db
->db_state
== DB_UNCACHED
||
2977 db
->db_state
== DB_NOFILL
);
2979 } else if (arc_released(db
->db_buf
)) {
2981 * This dbuf has anonymous data associated with it.
2985 boolean_t do_arc_evict
= B_FALSE
;
2987 spa_t
*spa
= dmu_objset_spa(db
->db_objset
);
2989 if (!DBUF_IS_CACHEABLE(db
) &&
2990 db
->db_blkptr
!= NULL
&&
2991 !BP_IS_HOLE(db
->db_blkptr
) &&
2992 !BP_IS_EMBEDDED(db
->db_blkptr
)) {
2993 do_arc_evict
= B_TRUE
;
2994 bp
= *db
->db_blkptr
;
2997 if (!DBUF_IS_CACHEABLE(db
) ||
2998 db
->db_pending_evict
) {
3000 } else if (!multilist_link_active(&db
->db_cache_link
)) {
3001 multilist_insert(&dbuf_cache
, db
);
3002 (void) refcount_add_many(&dbuf_cache_size
,
3003 db
->db
.db_size
, db
);
3004 mutex_exit(&db
->db_mtx
);
3006 dbuf_evict_notify();
3010 arc_freed(spa
, &bp
);
3013 mutex_exit(&db
->db_mtx
);
3018 #pragma weak dmu_buf_refcount = dbuf_refcount
3020 dbuf_refcount(dmu_buf_impl_t
*db
)
3022 return (refcount_count(&db
->db_holds
));
3026 dmu_buf_replace_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*old_user
,
3027 dmu_buf_user_t
*new_user
)
3029 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3031 mutex_enter(&db
->db_mtx
);
3032 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
3033 if (db
->db_user
== old_user
)
3034 db
->db_user
= new_user
;
3036 old_user
= db
->db_user
;
3037 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
3038 mutex_exit(&db
->db_mtx
);
3044 dmu_buf_set_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
3046 return (dmu_buf_replace_user(db_fake
, NULL
, user
));
3050 dmu_buf_set_user_ie(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
3052 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3054 db
->db_user_immediate_evict
= TRUE
;
3055 return (dmu_buf_set_user(db_fake
, user
));
3059 dmu_buf_remove_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
3061 return (dmu_buf_replace_user(db_fake
, user
, NULL
));
3065 dmu_buf_get_user(dmu_buf_t
*db_fake
)
3067 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3069 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
3070 return (db
->db_user
);
3074 dmu_buf_user_evict_wait()
3076 taskq_wait(dbu_evict_taskq
);
3080 dmu_buf_freeable(dmu_buf_t
*dbuf
)
3082 boolean_t res
= B_FALSE
;
3083 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbuf
;
3086 res
= dsl_dataset_block_freeable(db
->db_objset
->os_dsl_dataset
,
3087 db
->db_blkptr
, db
->db_blkptr
->blk_birth
);
3093 dmu_buf_get_blkptr(dmu_buf_t
*db
)
3095 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3096 return (dbi
->db_blkptr
);
3100 dmu_buf_get_objset(dmu_buf_t
*db
)
3102 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3103 return (dbi
->db_objset
);
3107 dmu_buf_dnode_enter(dmu_buf_t
*db
)
3109 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3110 DB_DNODE_ENTER(dbi
);
3111 return (DB_DNODE(dbi
));
3115 dmu_buf_dnode_exit(dmu_buf_t
*db
)
3117 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3122 dbuf_check_blkptr(dnode_t
*dn
, dmu_buf_impl_t
*db
)
3124 /* ASSERT(dmu_tx_is_syncing(tx) */
3125 ASSERT(MUTEX_HELD(&db
->db_mtx
));
3127 if (db
->db_blkptr
!= NULL
)
3130 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3131 db
->db_blkptr
= DN_SPILL_BLKPTR(dn
->dn_phys
);
3132 BP_ZERO(db
->db_blkptr
);
3135 if (db
->db_level
== dn
->dn_phys
->dn_nlevels
-1) {
3137 * This buffer was allocated at a time when there was
3138 * no available blkptrs from the dnode, or it was
3139 * inappropriate to hook it in (i.e., nlevels mis-match).
3141 ASSERT(db
->db_blkid
< dn
->dn_phys
->dn_nblkptr
);
3142 ASSERT(db
->db_parent
== NULL
);
3143 db
->db_parent
= dn
->dn_dbuf
;
3144 db
->db_blkptr
= &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
];
3147 dmu_buf_impl_t
*parent
= db
->db_parent
;
3148 int epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
3150 ASSERT(dn
->dn_phys
->dn_nlevels
> 1);
3151 if (parent
== NULL
) {
3152 mutex_exit(&db
->db_mtx
);
3153 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
3154 parent
= dbuf_hold_level(dn
, db
->db_level
+ 1,
3155 db
->db_blkid
>> epbs
, db
);
3156 rw_exit(&dn
->dn_struct_rwlock
);
3157 mutex_enter(&db
->db_mtx
);
3158 db
->db_parent
= parent
;
3160 db
->db_blkptr
= (blkptr_t
*)parent
->db
.db_data
+
3161 (db
->db_blkid
& ((1ULL << epbs
) - 1));
3167 * dbuf_sync_indirect() is called recursively from dbuf_sync_list() so it
3168 * is critical the we not allow the compiler to inline this function in to
3169 * dbuf_sync_list() thereby drastically bloating the stack usage.
3171 noinline
static void
3172 dbuf_sync_indirect(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
3174 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3178 ASSERT(dmu_tx_is_syncing(tx
));
3180 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
3182 mutex_enter(&db
->db_mtx
);
3184 ASSERT(db
->db_level
> 0);
3187 /* Read the block if it hasn't been read yet. */
3188 if (db
->db_buf
== NULL
) {
3189 mutex_exit(&db
->db_mtx
);
3190 (void) dbuf_read(db
, NULL
, DB_RF_MUST_SUCCEED
);
3191 mutex_enter(&db
->db_mtx
);
3193 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
3194 ASSERT(db
->db_buf
!= NULL
);
3198 /* Indirect block size must match what the dnode thinks it is. */
3199 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
3200 dbuf_check_blkptr(dn
, db
);
3203 /* Provide the pending dirty record to child dbufs */
3204 db
->db_data_pending
= dr
;
3206 mutex_exit(&db
->db_mtx
);
3207 dbuf_write(dr
, db
->db_buf
, tx
);
3210 mutex_enter(&dr
->dt
.di
.dr_mtx
);
3211 dbuf_sync_list(&dr
->dt
.di
.dr_children
, db
->db_level
- 1, tx
);
3212 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
3213 mutex_exit(&dr
->dt
.di
.dr_mtx
);
3218 * dbuf_sync_leaf() is called recursively from dbuf_sync_list() so it is
3219 * critical the we not allow the compiler to inline this function in to
3220 * dbuf_sync_list() thereby drastically bloating the stack usage.
3222 noinline
static void
3223 dbuf_sync_leaf(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
3225 arc_buf_t
**datap
= &dr
->dt
.dl
.dr_data
;
3226 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3229 uint64_t txg
= tx
->tx_txg
;
3231 ASSERT(dmu_tx_is_syncing(tx
));
3233 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
3235 mutex_enter(&db
->db_mtx
);
3237 * To be synced, we must be dirtied. But we
3238 * might have been freed after the dirty.
3240 if (db
->db_state
== DB_UNCACHED
) {
3241 /* This buffer has been freed since it was dirtied */
3242 ASSERT(db
->db
.db_data
== NULL
);
3243 } else if (db
->db_state
== DB_FILL
) {
3244 /* This buffer was freed and is now being re-filled */
3245 ASSERT(db
->db
.db_data
!= dr
->dt
.dl
.dr_data
);
3247 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_NOFILL
);
3254 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3255 mutex_enter(&dn
->dn_mtx
);
3256 if (!(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
)) {
3258 * In the previous transaction group, the bonus buffer
3259 * was entirely used to store the attributes for the
3260 * dnode which overrode the dn_spill field. However,
3261 * when adding more attributes to the file a spill
3262 * block was required to hold the extra attributes.
3264 * Make sure to clear the garbage left in the dn_spill
3265 * field from the previous attributes in the bonus
3266 * buffer. Otherwise, after writing out the spill
3267 * block to the new allocated dva, it will free
3268 * the old block pointed to by the invalid dn_spill.
3270 db
->db_blkptr
= NULL
;
3272 dn
->dn_phys
->dn_flags
|= DNODE_FLAG_SPILL_BLKPTR
;
3273 mutex_exit(&dn
->dn_mtx
);
3277 * If this is a bonus buffer, simply copy the bonus data into the
3278 * dnode. It will be written out when the dnode is synced (and it
3279 * will be synced, since it must have been dirty for dbuf_sync to
3282 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
3283 dbuf_dirty_record_t
**drp
;
3285 ASSERT(*datap
!= NULL
);
3286 ASSERT0(db
->db_level
);
3287 ASSERT3U(dn
->dn_phys
->dn_bonuslen
, <=,
3288 DN_SLOTS_TO_BONUSLEN(dn
->dn_phys
->dn_extra_slots
+ 1));
3289 bcopy(*datap
, DN_BONUS(dn
->dn_phys
), dn
->dn_phys
->dn_bonuslen
);
3292 if (*datap
!= db
->db
.db_data
) {
3293 int slots
= DB_DNODE(db
)->dn_num_slots
;
3294 int bonuslen
= DN_SLOTS_TO_BONUSLEN(slots
);
3295 zio_buf_free(*datap
, bonuslen
);
3296 arc_space_return(bonuslen
, ARC_SPACE_BONUS
);
3298 db
->db_data_pending
= NULL
;
3299 drp
= &db
->db_last_dirty
;
3301 drp
= &(*drp
)->dr_next
;
3302 ASSERT(dr
->dr_next
== NULL
);
3303 ASSERT(dr
->dr_dbuf
== db
);
3305 if (dr
->dr_dbuf
->db_level
!= 0) {
3306 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
3307 list_destroy(&dr
->dt
.di
.dr_children
);
3309 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
3310 ASSERT(db
->db_dirtycnt
> 0);
3311 db
->db_dirtycnt
-= 1;
3312 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)txg
);
3319 * This function may have dropped the db_mtx lock allowing a dmu_sync
3320 * operation to sneak in. As a result, we need to ensure that we
3321 * don't check the dr_override_state until we have returned from
3322 * dbuf_check_blkptr.
3324 dbuf_check_blkptr(dn
, db
);
3327 * If this buffer is in the middle of an immediate write,
3328 * wait for the synchronous IO to complete.
3330 while (dr
->dt
.dl
.dr_override_state
== DR_IN_DMU_SYNC
) {
3331 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
3332 cv_wait(&db
->db_changed
, &db
->db_mtx
);
3333 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_NOT_OVERRIDDEN
);
3336 if (db
->db_state
!= DB_NOFILL
&&
3337 dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
3338 refcount_count(&db
->db_holds
) > 1 &&
3339 dr
->dt
.dl
.dr_override_state
!= DR_OVERRIDDEN
&&
3340 *datap
== db
->db_buf
) {
3342 * If this buffer is currently "in use" (i.e., there
3343 * are active holds and db_data still references it),
3344 * then make a copy before we start the write so that
3345 * any modifications from the open txg will not leak
3348 * NOTE: this copy does not need to be made for
3349 * objects only modified in the syncing context (e.g.
3350 * DNONE_DNODE blocks).
3352 int psize
= arc_buf_size(*datap
);
3353 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
3354 enum zio_compress compress_type
= arc_get_compression(*datap
);
3356 if (compress_type
== ZIO_COMPRESS_OFF
) {
3357 *datap
= arc_alloc_buf(os
->os_spa
, db
, type
, psize
);
3359 int lsize
= arc_buf_lsize(*datap
);
3360 ASSERT3U(type
, ==, ARC_BUFC_DATA
);
3361 *datap
= arc_alloc_compressed_buf(os
->os_spa
, db
,
3362 psize
, lsize
, compress_type
);
3364 bcopy(db
->db
.db_data
, (*datap
)->b_data
, psize
);
3366 db
->db_data_pending
= dr
;
3368 mutex_exit(&db
->db_mtx
);
3370 dbuf_write(dr
, *datap
, tx
);
3372 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
3373 if (dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
3374 list_insert_tail(&dn
->dn_dirty_records
[txg
&TXG_MASK
], dr
);
3378 * Although zio_nowait() does not "wait for an IO", it does
3379 * initiate the IO. If this is an empty write it seems plausible
3380 * that the IO could actually be completed before the nowait
3381 * returns. We need to DB_DNODE_EXIT() first in case
3382 * zio_nowait() invalidates the dbuf.
3385 zio_nowait(dr
->dr_zio
);
3390 dbuf_sync_list(list_t
*list
, int level
, dmu_tx_t
*tx
)
3392 dbuf_dirty_record_t
*dr
;
3394 while ((dr
= list_head(list
))) {
3395 if (dr
->dr_zio
!= NULL
) {
3397 * If we find an already initialized zio then we
3398 * are processing the meta-dnode, and we have finished.
3399 * The dbufs for all dnodes are put back on the list
3400 * during processing, so that we can zio_wait()
3401 * these IOs after initiating all child IOs.
3403 ASSERT3U(dr
->dr_dbuf
->db
.db_object
, ==,
3404 DMU_META_DNODE_OBJECT
);
3407 if (dr
->dr_dbuf
->db_blkid
!= DMU_BONUS_BLKID
&&
3408 dr
->dr_dbuf
->db_blkid
!= DMU_SPILL_BLKID
) {
3409 VERIFY3U(dr
->dr_dbuf
->db_level
, ==, level
);
3411 list_remove(list
, dr
);
3412 if (dr
->dr_dbuf
->db_level
> 0)
3413 dbuf_sync_indirect(dr
, tx
);
3415 dbuf_sync_leaf(dr
, tx
);
3421 dbuf_write_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
3423 dmu_buf_impl_t
*db
= vdb
;
3425 blkptr_t
*bp
= zio
->io_bp
;
3426 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
3427 spa_t
*spa
= zio
->io_spa
;
3432 ASSERT3P(db
->db_blkptr
, !=, NULL
);
3433 ASSERT3P(&db
->db_data_pending
->dr_bp_copy
, ==, bp
);
3437 delta
= bp_get_dsize_sync(spa
, bp
) - bp_get_dsize_sync(spa
, bp_orig
);
3438 dnode_diduse_space(dn
, delta
- zio
->io_prev_space_delta
);
3439 zio
->io_prev_space_delta
= delta
;
3441 if (bp
->blk_birth
!= 0) {
3442 ASSERT((db
->db_blkid
!= DMU_SPILL_BLKID
&&
3443 BP_GET_TYPE(bp
) == dn
->dn_type
) ||
3444 (db
->db_blkid
== DMU_SPILL_BLKID
&&
3445 BP_GET_TYPE(bp
) == dn
->dn_bonustype
) ||
3446 BP_IS_EMBEDDED(bp
));
3447 ASSERT(BP_GET_LEVEL(bp
) == db
->db_level
);
3450 mutex_enter(&db
->db_mtx
);
3453 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3454 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
3455 ASSERT(!(BP_IS_HOLE(bp
)) &&
3456 db
->db_blkptr
== DN_SPILL_BLKPTR(dn
->dn_phys
));
3460 if (db
->db_level
== 0) {
3461 mutex_enter(&dn
->dn_mtx
);
3462 if (db
->db_blkid
> dn
->dn_phys
->dn_maxblkid
&&
3463 db
->db_blkid
!= DMU_SPILL_BLKID
)
3464 dn
->dn_phys
->dn_maxblkid
= db
->db_blkid
;
3465 mutex_exit(&dn
->dn_mtx
);
3467 if (dn
->dn_type
== DMU_OT_DNODE
) {
3469 while (i
< db
->db
.db_size
) {
3470 dnode_phys_t
*dnp
= db
->db
.db_data
+ i
;
3472 i
+= DNODE_MIN_SIZE
;
3473 if (dnp
->dn_type
!= DMU_OT_NONE
) {
3475 i
+= dnp
->dn_extra_slots
*
3480 if (BP_IS_HOLE(bp
)) {
3487 blkptr_t
*ibp
= db
->db
.db_data
;
3488 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
3489 for (i
= db
->db
.db_size
>> SPA_BLKPTRSHIFT
; i
> 0; i
--, ibp
++) {
3490 if (BP_IS_HOLE(ibp
))
3492 fill
+= BP_GET_FILL(ibp
);
3497 if (!BP_IS_EMBEDDED(bp
))
3498 bp
->blk_fill
= fill
;
3500 mutex_exit(&db
->db_mtx
);
3502 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
3503 *db
->db_blkptr
= *bp
;
3504 rw_exit(&dn
->dn_struct_rwlock
);
3509 * This function gets called just prior to running through the compression
3510 * stage of the zio pipeline. If we're an indirect block comprised of only
3511 * holes, then we want this indirect to be compressed away to a hole. In
3512 * order to do that we must zero out any information about the holes that
3513 * this indirect points to prior to before we try to compress it.
3516 dbuf_write_children_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
3518 dmu_buf_impl_t
*db
= vdb
;
3524 ASSERT3U(db
->db_level
, >, 0);
3527 epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
3529 /* Determine if all our children are holes */
3530 for (i
= 0, bp
= db
->db
.db_data
; i
< 1 << epbs
; i
++, bp
++) {
3531 if (!BP_IS_HOLE(bp
))
3536 * If all the children are holes, then zero them all out so that
3537 * we may get compressed away.
3539 if (i
== 1 << epbs
) {
3540 /* didn't find any non-holes */
3541 bzero(db
->db
.db_data
, db
->db
.db_size
);
3547 * The SPA will call this callback several times for each zio - once
3548 * for every physical child i/o (zio->io_phys_children times). This
3549 * allows the DMU to monitor the progress of each logical i/o. For example,
3550 * there may be 2 copies of an indirect block, or many fragments of a RAID-Z
3551 * block. There may be a long delay before all copies/fragments are completed,
3552 * so this callback allows us to retire dirty space gradually, as the physical
3557 dbuf_write_physdone(zio_t
*zio
, arc_buf_t
*buf
, void *arg
)
3559 dmu_buf_impl_t
*db
= arg
;
3560 objset_t
*os
= db
->db_objset
;
3561 dsl_pool_t
*dp
= dmu_objset_pool(os
);
3562 dbuf_dirty_record_t
*dr
;
3565 dr
= db
->db_data_pending
;
3566 ASSERT3U(dr
->dr_txg
, ==, zio
->io_txg
);
3569 * The callback will be called io_phys_children times. Retire one
3570 * portion of our dirty space each time we are called. Any rounding
3571 * error will be cleaned up by dsl_pool_sync()'s call to
3572 * dsl_pool_undirty_space().
3574 delta
= dr
->dr_accounted
/ zio
->io_phys_children
;
3575 dsl_pool_undirty_space(dp
, delta
, zio
->io_txg
);
3580 dbuf_write_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
3582 dmu_buf_impl_t
*db
= vdb
;
3583 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
3584 blkptr_t
*bp
= db
->db_blkptr
;
3585 objset_t
*os
= db
->db_objset
;
3586 dmu_tx_t
*tx
= os
->os_synctx
;
3587 dbuf_dirty_record_t
**drp
, *dr
;
3589 ASSERT0(zio
->io_error
);
3590 ASSERT(db
->db_blkptr
== bp
);
3593 * For nopwrites and rewrites we ensure that the bp matches our
3594 * original and bypass all the accounting.
3596 if (zio
->io_flags
& (ZIO_FLAG_IO_REWRITE
| ZIO_FLAG_NOPWRITE
)) {
3597 ASSERT(BP_EQUAL(bp
, bp_orig
));
3599 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
3600 (void) dsl_dataset_block_kill(ds
, bp_orig
, tx
, B_TRUE
);
3601 dsl_dataset_block_born(ds
, bp
, tx
);
3604 mutex_enter(&db
->db_mtx
);
3608 drp
= &db
->db_last_dirty
;
3609 while ((dr
= *drp
) != db
->db_data_pending
)
3611 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
3612 ASSERT(dr
->dr_dbuf
== db
);
3613 ASSERT(dr
->dr_next
== NULL
);
3617 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3622 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
3623 ASSERT(!(BP_IS_HOLE(db
->db_blkptr
)) &&
3624 db
->db_blkptr
== DN_SPILL_BLKPTR(dn
->dn_phys
));
3629 if (db
->db_level
== 0) {
3630 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
3631 ASSERT(dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
);
3632 if (db
->db_state
!= DB_NOFILL
) {
3633 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
3634 arc_buf_destroy(dr
->dt
.dl
.dr_data
, db
);
3641 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
3642 ASSERT3U(db
->db
.db_size
, ==, 1 << dn
->dn_phys
->dn_indblkshift
);
3643 if (!BP_IS_HOLE(db
->db_blkptr
)) {
3644 ASSERTV(int epbs
= dn
->dn_phys
->dn_indblkshift
-
3646 ASSERT3U(db
->db_blkid
, <=,
3647 dn
->dn_phys
->dn_maxblkid
>> (db
->db_level
* epbs
));
3648 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==,
3652 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
3653 list_destroy(&dr
->dt
.di
.dr_children
);
3655 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
3657 cv_broadcast(&db
->db_changed
);
3658 ASSERT(db
->db_dirtycnt
> 0);
3659 db
->db_dirtycnt
-= 1;
3660 db
->db_data_pending
= NULL
;
3661 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)tx
->tx_txg
);
3665 dbuf_write_nofill_ready(zio_t
*zio
)
3667 dbuf_write_ready(zio
, NULL
, zio
->io_private
);
3671 dbuf_write_nofill_done(zio_t
*zio
)
3673 dbuf_write_done(zio
, NULL
, zio
->io_private
);
3677 dbuf_write_override_ready(zio_t
*zio
)
3679 dbuf_dirty_record_t
*dr
= zio
->io_private
;
3680 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3682 dbuf_write_ready(zio
, NULL
, db
);
3686 dbuf_write_override_done(zio_t
*zio
)
3688 dbuf_dirty_record_t
*dr
= zio
->io_private
;
3689 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3690 blkptr_t
*obp
= &dr
->dt
.dl
.dr_overridden_by
;
3692 mutex_enter(&db
->db_mtx
);
3693 if (!BP_EQUAL(zio
->io_bp
, obp
)) {
3694 if (!BP_IS_HOLE(obp
))
3695 dsl_free(spa_get_dsl(zio
->io_spa
), zio
->io_txg
, obp
);
3696 arc_release(dr
->dt
.dl
.dr_data
, db
);
3698 mutex_exit(&db
->db_mtx
);
3700 dbuf_write_done(zio
, NULL
, db
);
3703 /* Issue I/O to commit a dirty buffer to disk. */
3705 dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
)
3707 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3710 dmu_buf_impl_t
*parent
= db
->db_parent
;
3711 uint64_t txg
= tx
->tx_txg
;
3712 zbookmark_phys_t zb
;
3717 ASSERT(dmu_tx_is_syncing(tx
));
3723 if (db
->db_state
!= DB_NOFILL
) {
3724 if (db
->db_level
> 0 || dn
->dn_type
== DMU_OT_DNODE
) {
3726 * Private object buffers are released here rather
3727 * than in dbuf_dirty() since they are only modified
3728 * in the syncing context and we don't want the
3729 * overhead of making multiple copies of the data.
3731 if (BP_IS_HOLE(db
->db_blkptr
)) {
3734 dbuf_release_bp(db
);
3739 if (parent
!= dn
->dn_dbuf
) {
3740 /* Our parent is an indirect block. */
3741 /* We have a dirty parent that has been scheduled for write. */
3742 ASSERT(parent
&& parent
->db_data_pending
);
3743 /* Our parent's buffer is one level closer to the dnode. */
3744 ASSERT(db
->db_level
== parent
->db_level
-1);
3746 * We're about to modify our parent's db_data by modifying
3747 * our block pointer, so the parent must be released.
3749 ASSERT(arc_released(parent
->db_buf
));
3750 zio
= parent
->db_data_pending
->dr_zio
;
3752 /* Our parent is the dnode itself. */
3753 ASSERT((db
->db_level
== dn
->dn_phys
->dn_nlevels
-1 &&
3754 db
->db_blkid
!= DMU_SPILL_BLKID
) ||
3755 (db
->db_blkid
== DMU_SPILL_BLKID
&& db
->db_level
== 0));
3756 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
3757 ASSERT3P(db
->db_blkptr
, ==,
3758 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
3762 ASSERT(db
->db_level
== 0 || data
== db
->db_buf
);
3763 ASSERT3U(db
->db_blkptr
->blk_birth
, <=, txg
);
3766 SET_BOOKMARK(&zb
, os
->os_dsl_dataset
?
3767 os
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
3768 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
3770 if (db
->db_blkid
== DMU_SPILL_BLKID
)
3772 wp_flag
|= (db
->db_state
== DB_NOFILL
) ? WP_NOFILL
: 0;
3774 dmu_write_policy(os
, dn
, db
->db_level
, wp_flag
,
3775 (data
!= NULL
&& arc_get_compression(data
) != ZIO_COMPRESS_OFF
) ?
3776 arc_get_compression(data
) : ZIO_COMPRESS_INHERIT
, &zp
);
3780 * We copy the blkptr now (rather than when we instantiate the dirty
3781 * record), because its value can change between open context and
3782 * syncing context. We do not need to hold dn_struct_rwlock to read
3783 * db_blkptr because we are in syncing context.
3785 dr
->dr_bp_copy
= *db
->db_blkptr
;
3787 if (db
->db_level
== 0 &&
3788 dr
->dt
.dl
.dr_override_state
== DR_OVERRIDDEN
) {
3790 * The BP for this block has been provided by open context
3791 * (by dmu_sync() or dmu_buf_write_embedded()).
3793 void *contents
= (data
!= NULL
) ? data
->b_data
: NULL
;
3795 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
,
3796 &dr
->dr_bp_copy
, contents
, db
->db
.db_size
, db
->db
.db_size
,
3797 &zp
, dbuf_write_override_ready
, NULL
, NULL
,
3798 dbuf_write_override_done
,
3799 dr
, ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
3800 mutex_enter(&db
->db_mtx
);
3801 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
3802 zio_write_override(dr
->dr_zio
, &dr
->dt
.dl
.dr_overridden_by
,
3803 dr
->dt
.dl
.dr_copies
, dr
->dt
.dl
.dr_nopwrite
);
3804 mutex_exit(&db
->db_mtx
);
3805 } else if (db
->db_state
== DB_NOFILL
) {
3806 ASSERT(zp
.zp_checksum
== ZIO_CHECKSUM_OFF
);
3807 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
,
3808 &dr
->dr_bp_copy
, NULL
, db
->db
.db_size
, db
->db
.db_size
, &zp
,
3809 dbuf_write_nofill_ready
, NULL
, NULL
,
3810 dbuf_write_nofill_done
, db
,
3811 ZIO_PRIORITY_ASYNC_WRITE
,
3812 ZIO_FLAG_MUSTSUCCEED
| ZIO_FLAG_NODATA
, &zb
);
3814 arc_done_func_t
*children_ready_cb
= NULL
;
3815 ASSERT(arc_released(data
));
3818 * For indirect blocks, we want to setup the children
3819 * ready callback so that we can properly handle an indirect
3820 * block that only contains holes.
3822 if (db
->db_level
!= 0)
3823 children_ready_cb
= dbuf_write_children_ready
;
3825 dr
->dr_zio
= arc_write(zio
, os
->os_spa
, txg
,
3826 &dr
->dr_bp_copy
, data
, DBUF_IS_L2CACHEABLE(db
),
3827 &zp
, dbuf_write_ready
,
3828 children_ready_cb
, dbuf_write_physdone
,
3829 dbuf_write_done
, db
, ZIO_PRIORITY_ASYNC_WRITE
,
3830 ZIO_FLAG_MUSTSUCCEED
, &zb
);
3834 #if defined(_KERNEL) && defined(HAVE_SPL)
3835 EXPORT_SYMBOL(dbuf_find
);
3836 EXPORT_SYMBOL(dbuf_is_metadata
);
3837 EXPORT_SYMBOL(dbuf_destroy
);
3838 EXPORT_SYMBOL(dbuf_loan_arcbuf
);
3839 EXPORT_SYMBOL(dbuf_whichblock
);
3840 EXPORT_SYMBOL(dbuf_read
);
3841 EXPORT_SYMBOL(dbuf_unoverride
);
3842 EXPORT_SYMBOL(dbuf_free_range
);
3843 EXPORT_SYMBOL(dbuf_new_size
);
3844 EXPORT_SYMBOL(dbuf_release_bp
);
3845 EXPORT_SYMBOL(dbuf_dirty
);
3846 EXPORT_SYMBOL(dmu_buf_will_dirty
);
3847 EXPORT_SYMBOL(dmu_buf_will_not_fill
);
3848 EXPORT_SYMBOL(dmu_buf_will_fill
);
3849 EXPORT_SYMBOL(dmu_buf_fill_done
);
3850 EXPORT_SYMBOL(dmu_buf_rele
);
3851 EXPORT_SYMBOL(dbuf_assign_arcbuf
);
3852 EXPORT_SYMBOL(dbuf_prefetch
);
3853 EXPORT_SYMBOL(dbuf_hold_impl
);
3854 EXPORT_SYMBOL(dbuf_hold
);
3855 EXPORT_SYMBOL(dbuf_hold_level
);
3856 EXPORT_SYMBOL(dbuf_create_bonus
);
3857 EXPORT_SYMBOL(dbuf_spill_set_blksz
);
3858 EXPORT_SYMBOL(dbuf_rm_spill
);
3859 EXPORT_SYMBOL(dbuf_add_ref
);
3860 EXPORT_SYMBOL(dbuf_rele
);
3861 EXPORT_SYMBOL(dbuf_rele_and_unlock
);
3862 EXPORT_SYMBOL(dbuf_refcount
);
3863 EXPORT_SYMBOL(dbuf_sync_list
);
3864 EXPORT_SYMBOL(dmu_buf_set_user
);
3865 EXPORT_SYMBOL(dmu_buf_set_user_ie
);
3866 EXPORT_SYMBOL(dmu_buf_get_user
);
3867 EXPORT_SYMBOL(dmu_buf_freeable
);
3868 EXPORT_SYMBOL(dmu_buf_get_blkptr
);
3871 module_param(dbuf_cache_max_bytes
, ulong
, 0644);
3872 MODULE_PARM_DESC(dbuf_cache_max_bytes
,
3873 "Maximum size in bytes of the dbuf cache.");
3875 module_param(dbuf_cache_hiwater_pct
, uint
, 0644);
3876 MODULE_PARM_DESC(dbuf_cache_hiwater_pct
,
3877 "Percentage over dbuf_cache_max_bytes when dbufs \
3878 much be evicted directly.");
3880 module_param(dbuf_cache_lowater_pct
, uint
, 0644);
3881 MODULE_PARM_DESC(dbuf_cache_lowater_pct
,
3882 "Percentage below dbuf_cache_max_bytes \
3883 when the evict thread stop evicting dbufs.");
3885 module_param(dbuf_cache_max_shift
, int, 0644);
3886 MODULE_PARM_DESC(dbuf_cache_max_shift
,
3887 "Cap the size of the dbuf cache to log2 fraction of arc size.");