4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
25 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
26 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
29 #include <sys/zfs_context.h>
32 #include <sys/dmu_send.h>
33 #include <sys/dmu_impl.h>
35 #include <sys/dmu_objset.h>
36 #include <sys/dsl_dataset.h>
37 #include <sys/dsl_dir.h>
38 #include <sys/dmu_tx.h>
41 #include <sys/dmu_zfetch.h>
43 #include <sys/sa_impl.h>
44 #include <sys/zfeature.h>
45 #include <sys/blkptr.h>
46 #include <sys/range_tree.h>
47 #include <sys/trace_dbuf.h>
48 #include <sys/callb.h>
54 typedef struct dbuf_stats
{
56 * Various statistics about the size of the dbuf cache.
58 kstat_named_t cache_count
;
59 kstat_named_t cache_size_bytes
;
60 kstat_named_t cache_size_bytes_max
;
62 * Statistics regarding the bounds on the dbuf cache size.
64 kstat_named_t cache_target_bytes
;
65 kstat_named_t cache_lowater_bytes
;
66 kstat_named_t cache_hiwater_bytes
;
68 * Total number of dbuf cache evictions that have occurred.
70 kstat_named_t cache_total_evicts
;
72 * The distribution of dbuf levels in the dbuf cache and
73 * the total size of all dbufs at each level.
75 kstat_named_t cache_levels
[DN_MAX_LEVELS
];
76 kstat_named_t cache_levels_bytes
[DN_MAX_LEVELS
];
78 * Statistics about the dbuf hash table.
80 kstat_named_t hash_hits
;
81 kstat_named_t hash_misses
;
82 kstat_named_t hash_collisions
;
83 kstat_named_t hash_elements
;
84 kstat_named_t hash_elements_max
;
86 * Number of sublists containing more than one dbuf in the dbuf
87 * hash table. Keep track of the longest hash chain.
89 kstat_named_t hash_chains
;
90 kstat_named_t hash_chain_max
;
92 * Number of times a dbuf_create() discovers that a dbuf was
93 * already created and in the dbuf hash table.
95 kstat_named_t hash_insert_race
;
98 dbuf_stats_t dbuf_stats
= {
99 { "cache_count", KSTAT_DATA_UINT64
},
100 { "cache_size_bytes", KSTAT_DATA_UINT64
},
101 { "cache_size_bytes_max", KSTAT_DATA_UINT64
},
102 { "cache_target_bytes", KSTAT_DATA_UINT64
},
103 { "cache_lowater_bytes", KSTAT_DATA_UINT64
},
104 { "cache_hiwater_bytes", KSTAT_DATA_UINT64
},
105 { "cache_total_evicts", KSTAT_DATA_UINT64
},
106 { { "cache_levels_N", KSTAT_DATA_UINT64
} },
107 { { "cache_levels_bytes_N", KSTAT_DATA_UINT64
} },
108 { "hash_hits", KSTAT_DATA_UINT64
},
109 { "hash_misses", KSTAT_DATA_UINT64
},
110 { "hash_collisions", KSTAT_DATA_UINT64
},
111 { "hash_elements", KSTAT_DATA_UINT64
},
112 { "hash_elements_max", KSTAT_DATA_UINT64
},
113 { "hash_chains", KSTAT_DATA_UINT64
},
114 { "hash_chain_max", KSTAT_DATA_UINT64
},
115 { "hash_insert_race", KSTAT_DATA_UINT64
}
118 #define DBUF_STAT_INCR(stat, val) \
119 atomic_add_64(&dbuf_stats.stat.value.ui64, (val));
120 #define DBUF_STAT_DECR(stat, val) \
121 DBUF_STAT_INCR(stat, -(val));
122 #define DBUF_STAT_BUMP(stat) \
123 DBUF_STAT_INCR(stat, 1);
124 #define DBUF_STAT_BUMPDOWN(stat) \
125 DBUF_STAT_INCR(stat, -1);
126 #define DBUF_STAT_MAX(stat, v) { \
128 while ((v) > (_m = dbuf_stats.stat.value.ui64) && \
129 (_m != atomic_cas_64(&dbuf_stats.stat.value.ui64, _m, (v))))\
133 struct dbuf_hold_impl_data
{
134 /* Function arguments */
138 boolean_t dh_fail_sparse
;
139 boolean_t dh_fail_uncached
;
141 dmu_buf_impl_t
**dh_dbp
;
142 /* Local variables */
143 dmu_buf_impl_t
*dh_db
;
144 dmu_buf_impl_t
*dh_parent
;
147 dbuf_dirty_record_t
*dh_dr
;
151 static void __dbuf_hold_impl_init(struct dbuf_hold_impl_data
*dh
,
152 dnode_t
*dn
, uint8_t level
, uint64_t blkid
, boolean_t fail_sparse
,
153 boolean_t fail_uncached
,
154 void *tag
, dmu_buf_impl_t
**dbp
, int depth
);
155 static int __dbuf_hold_impl(struct dbuf_hold_impl_data
*dh
);
157 uint_t zfs_dbuf_evict_key
;
159 static boolean_t
dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
);
160 static void dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
);
162 extern inline void dmu_buf_init_user(dmu_buf_user_t
*dbu
,
163 dmu_buf_evict_func_t
*evict_func_sync
,
164 dmu_buf_evict_func_t
*evict_func_async
,
165 dmu_buf_t
**clear_on_evict_dbufp
);
168 * Global data structures and functions for the dbuf cache.
170 static kmem_cache_t
*dbuf_kmem_cache
;
171 static taskq_t
*dbu_evict_taskq
;
173 static kthread_t
*dbuf_cache_evict_thread
;
174 static kmutex_t dbuf_evict_lock
;
175 static kcondvar_t dbuf_evict_cv
;
176 static boolean_t dbuf_evict_thread_exit
;
179 * LRU cache of dbufs. The dbuf cache maintains a list of dbufs that
180 * are not currently held but have been recently released. These dbufs
181 * are not eligible for arc eviction until they are aged out of the cache.
182 * Dbufs are added to the dbuf cache once the last hold is released. If a
183 * dbuf is later accessed and still exists in the dbuf cache, then it will
184 * be removed from the cache and later re-added to the head of the cache.
185 * Dbufs that are aged out of the cache will be immediately destroyed and
186 * become eligible for arc eviction.
188 static multilist_t
*dbuf_cache
;
189 static refcount_t dbuf_cache_size
;
190 unsigned long dbuf_cache_max_bytes
= 0;
192 /* Set the default size of the dbuf cache to log2 fraction of arc size. */
193 int dbuf_cache_shift
= 5;
196 * The dbuf cache uses a three-stage eviction policy:
197 * - A low water marker designates when the dbuf eviction thread
198 * should stop evicting from the dbuf cache.
199 * - When we reach the maximum size (aka mid water mark), we
200 * signal the eviction thread to run.
201 * - The high water mark indicates when the eviction thread
202 * is unable to keep up with the incoming load and eviction must
203 * happen in the context of the calling thread.
207 * low water mid water hi water
208 * +----------------------------------------+----------+----------+
213 * +----------------------------------------+----------+----------+
215 * evicting eviction directly
218 * The high and low water marks indicate the operating range for the eviction
219 * thread. The low water mark is, by default, 90% of the total size of the
220 * cache and the high water mark is at 110% (both of these percentages can be
221 * changed by setting dbuf_cache_lowater_pct and dbuf_cache_hiwater_pct,
222 * respectively). The eviction thread will try to ensure that the cache remains
223 * within this range by waking up every second and checking if the cache is
224 * above the low water mark. The thread can also be woken up by callers adding
225 * elements into the cache if the cache is larger than the mid water (i.e max
226 * cache size). Once the eviction thread is woken up and eviction is required,
227 * it will continue evicting buffers until it's able to reduce the cache size
228 * to the low water mark. If the cache size continues to grow and hits the high
229 * water mark, then callers adding elements to the cache will begin to evict
230 * directly from the cache until the cache is no longer above the high water
235 * The percentage above and below the maximum cache size.
237 uint_t dbuf_cache_hiwater_pct
= 10;
238 uint_t dbuf_cache_lowater_pct
= 10;
242 dbuf_cons(void *vdb
, void *unused
, int kmflag
)
244 dmu_buf_impl_t
*db
= vdb
;
245 bzero(db
, sizeof (dmu_buf_impl_t
));
247 mutex_init(&db
->db_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
248 cv_init(&db
->db_changed
, NULL
, CV_DEFAULT
, NULL
);
249 multilist_link_init(&db
->db_cache_link
);
250 refcount_create(&db
->db_holds
);
257 dbuf_dest(void *vdb
, void *unused
)
259 dmu_buf_impl_t
*db
= vdb
;
260 mutex_destroy(&db
->db_mtx
);
261 cv_destroy(&db
->db_changed
);
262 ASSERT(!multilist_link_active(&db
->db_cache_link
));
263 refcount_destroy(&db
->db_holds
);
267 * dbuf hash table routines
269 static dbuf_hash_table_t dbuf_hash_table
;
271 static uint64_t dbuf_hash_count
;
274 dbuf_hash(void *os
, uint64_t obj
, uint8_t lvl
, uint64_t blkid
)
276 uintptr_t osv
= (uintptr_t)os
;
277 uint64_t crc
= -1ULL;
279 ASSERT(zfs_crc64_table
[128] == ZFS_CRC64_POLY
);
280 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (lvl
)) & 0xFF];
281 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (osv
>> 6)) & 0xFF];
282 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 0)) & 0xFF];
283 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (obj
>> 8)) & 0xFF];
284 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (blkid
>> 0)) & 0xFF];
285 crc
= (crc
>> 8) ^ zfs_crc64_table
[(crc
^ (blkid
>> 8)) & 0xFF];
287 crc
^= (osv
>>14) ^ (obj
>>16) ^ (blkid
>>16);
292 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
293 ((dbuf)->db.db_object == (obj) && \
294 (dbuf)->db_objset == (os) && \
295 (dbuf)->db_level == (level) && \
296 (dbuf)->db_blkid == (blkid))
299 dbuf_find(objset_t
*os
, uint64_t obj
, uint8_t level
, uint64_t blkid
)
301 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
306 hv
= dbuf_hash(os
, obj
, level
, blkid
);
307 idx
= hv
& h
->hash_table_mask
;
309 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
310 for (db
= h
->hash_table
[idx
]; db
!= NULL
; db
= db
->db_hash_next
) {
311 if (DBUF_EQUAL(db
, os
, obj
, level
, blkid
)) {
312 mutex_enter(&db
->db_mtx
);
313 if (db
->db_state
!= DB_EVICTING
) {
314 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
317 mutex_exit(&db
->db_mtx
);
320 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
324 static dmu_buf_impl_t
*
325 dbuf_find_bonus(objset_t
*os
, uint64_t object
)
328 dmu_buf_impl_t
*db
= NULL
;
330 if (dnode_hold(os
, object
, FTAG
, &dn
) == 0) {
331 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
332 if (dn
->dn_bonus
!= NULL
) {
334 mutex_enter(&db
->db_mtx
);
336 rw_exit(&dn
->dn_struct_rwlock
);
337 dnode_rele(dn
, FTAG
);
343 * Insert an entry into the hash table. If there is already an element
344 * equal to elem in the hash table, then the already existing element
345 * will be returned and the new element will not be inserted.
346 * Otherwise returns NULL.
348 static dmu_buf_impl_t
*
349 dbuf_hash_insert(dmu_buf_impl_t
*db
)
351 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
352 objset_t
*os
= db
->db_objset
;
353 uint64_t obj
= db
->db
.db_object
;
354 int level
= db
->db_level
;
355 uint64_t blkid
, hv
, idx
;
359 blkid
= db
->db_blkid
;
360 hv
= dbuf_hash(os
, obj
, level
, blkid
);
361 idx
= hv
& h
->hash_table_mask
;
363 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
364 for (dbf
= h
->hash_table
[idx
], i
= 0; dbf
!= NULL
;
365 dbf
= dbf
->db_hash_next
, i
++) {
366 if (DBUF_EQUAL(dbf
, os
, obj
, level
, blkid
)) {
367 mutex_enter(&dbf
->db_mtx
);
368 if (dbf
->db_state
!= DB_EVICTING
) {
369 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
372 mutex_exit(&dbf
->db_mtx
);
377 DBUF_STAT_BUMP(hash_collisions
);
379 DBUF_STAT_BUMP(hash_chains
);
381 DBUF_STAT_MAX(hash_chain_max
, i
);
384 mutex_enter(&db
->db_mtx
);
385 db
->db_hash_next
= h
->hash_table
[idx
];
386 h
->hash_table
[idx
] = db
;
387 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
388 atomic_inc_64(&dbuf_hash_count
);
389 DBUF_STAT_MAX(hash_elements_max
, dbuf_hash_count
);
395 * Remove an entry from the hash table. It must be in the EVICTING state.
398 dbuf_hash_remove(dmu_buf_impl_t
*db
)
400 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
402 dmu_buf_impl_t
*dbf
, **dbp
;
404 hv
= dbuf_hash(db
->db_objset
, db
->db
.db_object
,
405 db
->db_level
, db
->db_blkid
);
406 idx
= hv
& h
->hash_table_mask
;
409 * We mustn't hold db_mtx to maintain lock ordering:
410 * DBUF_HASH_MUTEX > db_mtx.
412 ASSERT(refcount_is_zero(&db
->db_holds
));
413 ASSERT(db
->db_state
== DB_EVICTING
);
414 ASSERT(!MUTEX_HELD(&db
->db_mtx
));
416 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
417 dbp
= &h
->hash_table
[idx
];
418 while ((dbf
= *dbp
) != db
) {
419 dbp
= &dbf
->db_hash_next
;
422 *dbp
= db
->db_hash_next
;
423 db
->db_hash_next
= NULL
;
424 if (h
->hash_table
[idx
] &&
425 h
->hash_table
[idx
]->db_hash_next
== NULL
)
426 DBUF_STAT_BUMPDOWN(hash_chains
);
427 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
428 atomic_dec_64(&dbuf_hash_count
);
434 } dbvu_verify_type_t
;
437 dbuf_verify_user(dmu_buf_impl_t
*db
, dbvu_verify_type_t verify_type
)
442 if (db
->db_user
== NULL
)
445 /* Only data blocks support the attachment of user data. */
446 ASSERT(db
->db_level
== 0);
448 /* Clients must resolve a dbuf before attaching user data. */
449 ASSERT(db
->db
.db_data
!= NULL
);
450 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
452 holds
= refcount_count(&db
->db_holds
);
453 if (verify_type
== DBVU_EVICTING
) {
455 * Immediate eviction occurs when holds == dirtycnt.
456 * For normal eviction buffers, holds is zero on
457 * eviction, except when dbuf_fix_old_data() calls
458 * dbuf_clear_data(). However, the hold count can grow
459 * during eviction even though db_mtx is held (see
460 * dmu_bonus_hold() for an example), so we can only
461 * test the generic invariant that holds >= dirtycnt.
463 ASSERT3U(holds
, >=, db
->db_dirtycnt
);
465 if (db
->db_user_immediate_evict
== TRUE
)
466 ASSERT3U(holds
, >=, db
->db_dirtycnt
);
468 ASSERT3U(holds
, >, 0);
474 dbuf_evict_user(dmu_buf_impl_t
*db
)
476 dmu_buf_user_t
*dbu
= db
->db_user
;
478 ASSERT(MUTEX_HELD(&db
->db_mtx
));
483 dbuf_verify_user(db
, DBVU_EVICTING
);
487 if (dbu
->dbu_clear_on_evict_dbufp
!= NULL
)
488 *dbu
->dbu_clear_on_evict_dbufp
= NULL
;
492 * There are two eviction callbacks - one that we call synchronously
493 * and one that we invoke via a taskq. The async one is useful for
494 * avoiding lock order reversals and limiting stack depth.
496 * Note that if we have a sync callback but no async callback,
497 * it's likely that the sync callback will free the structure
498 * containing the dbu. In that case we need to take care to not
499 * dereference dbu after calling the sync evict func.
501 boolean_t has_async
= (dbu
->dbu_evict_func_async
!= NULL
);
503 if (dbu
->dbu_evict_func_sync
!= NULL
)
504 dbu
->dbu_evict_func_sync(dbu
);
507 taskq_dispatch_ent(dbu_evict_taskq
, dbu
->dbu_evict_func_async
,
508 dbu
, 0, &dbu
->dbu_tqent
);
513 dbuf_is_metadata(dmu_buf_impl_t
*db
)
516 * Consider indirect blocks and spill blocks to be meta data.
518 if (db
->db_level
> 0 || db
->db_blkid
== DMU_SPILL_BLKID
) {
521 boolean_t is_metadata
;
524 is_metadata
= DMU_OT_IS_METADATA(DB_DNODE(db
)->dn_type
);
527 return (is_metadata
);
533 * This function *must* return indices evenly distributed between all
534 * sublists of the multilist. This is needed due to how the dbuf eviction
535 * code is laid out; dbuf_evict_thread() assumes dbufs are evenly
536 * distributed between all sublists and uses this assumption when
537 * deciding which sublist to evict from and how much to evict from it.
540 dbuf_cache_multilist_index_func(multilist_t
*ml
, void *obj
)
542 dmu_buf_impl_t
*db
= obj
;
545 * The assumption here, is the hash value for a given
546 * dmu_buf_impl_t will remain constant throughout it's lifetime
547 * (i.e. it's objset, object, level and blkid fields don't change).
548 * Thus, we don't need to store the dbuf's sublist index
549 * on insertion, as this index can be recalculated on removal.
551 * Also, the low order bits of the hash value are thought to be
552 * distributed evenly. Otherwise, in the case that the multilist
553 * has a power of two number of sublists, each sublists' usage
554 * would not be evenly distributed.
556 return (dbuf_hash(db
->db_objset
, db
->db
.db_object
,
557 db
->db_level
, db
->db_blkid
) %
558 multilist_get_num_sublists(ml
));
561 static inline unsigned long
562 dbuf_cache_target_bytes(void)
564 return MIN(dbuf_cache_max_bytes
,
565 arc_target_bytes() >> dbuf_cache_shift
);
568 static inline uint64_t
569 dbuf_cache_hiwater_bytes(void)
571 uint64_t dbuf_cache_target
= dbuf_cache_target_bytes();
572 return (dbuf_cache_target
+
573 (dbuf_cache_target
* dbuf_cache_hiwater_pct
) / 100);
576 static inline uint64_t
577 dbuf_cache_lowater_bytes(void)
579 uint64_t dbuf_cache_target
= dbuf_cache_target_bytes();
580 return (dbuf_cache_target
-
581 (dbuf_cache_target
* dbuf_cache_lowater_pct
) / 100);
584 static inline boolean_t
585 dbuf_cache_above_hiwater(void)
587 return (refcount_count(&dbuf_cache_size
) > dbuf_cache_hiwater_bytes());
590 static inline boolean_t
591 dbuf_cache_above_lowater(void)
593 return (refcount_count(&dbuf_cache_size
) > dbuf_cache_lowater_bytes());
597 * Evict the oldest eligible dbuf from the dbuf cache.
602 int idx
= multilist_get_random_index(dbuf_cache
);
603 multilist_sublist_t
*mls
= multilist_sublist_lock(dbuf_cache
, idx
);
605 ASSERT(!MUTEX_HELD(&dbuf_evict_lock
));
608 * Set the thread's tsd to indicate that it's processing evictions.
609 * Once a thread stops evicting from the dbuf cache it will
610 * reset its tsd to NULL.
612 ASSERT3P(tsd_get(zfs_dbuf_evict_key
), ==, NULL
);
613 (void) tsd_set(zfs_dbuf_evict_key
, (void *)B_TRUE
);
615 dmu_buf_impl_t
*db
= multilist_sublist_tail(mls
);
616 while (db
!= NULL
&& mutex_tryenter(&db
->db_mtx
) == 0) {
617 db
= multilist_sublist_prev(mls
, db
);
620 DTRACE_PROBE2(dbuf__evict__one
, dmu_buf_impl_t
*, db
,
621 multilist_sublist_t
*, mls
);
624 multilist_sublist_remove(mls
, db
);
625 multilist_sublist_unlock(mls
);
626 (void) refcount_remove_many(&dbuf_cache_size
,
628 DBUF_STAT_BUMPDOWN(cache_levels
[db
->db_level
]);
629 DBUF_STAT_BUMPDOWN(cache_count
);
630 DBUF_STAT_DECR(cache_levels_bytes
[db
->db_level
],
633 DBUF_STAT_MAX(cache_size_bytes_max
,
634 refcount_count(&dbuf_cache_size
));
635 DBUF_STAT_BUMP(cache_total_evicts
);
637 multilist_sublist_unlock(mls
);
639 (void) tsd_set(zfs_dbuf_evict_key
, NULL
);
643 * The dbuf evict thread is responsible for aging out dbufs from the
644 * cache. Once the cache has reached it's maximum size, dbufs are removed
645 * and destroyed. The eviction thread will continue running until the size
646 * of the dbuf cache is at or below the maximum size. Once the dbuf is aged
647 * out of the cache it is destroyed and becomes eligible for arc eviction.
651 dbuf_evict_thread(void *unused
)
655 CALLB_CPR_INIT(&cpr
, &dbuf_evict_lock
, callb_generic_cpr
, FTAG
);
657 mutex_enter(&dbuf_evict_lock
);
658 while (!dbuf_evict_thread_exit
) {
659 while (!dbuf_cache_above_lowater() && !dbuf_evict_thread_exit
) {
660 CALLB_CPR_SAFE_BEGIN(&cpr
);
661 (void) cv_timedwait_sig_hires(&dbuf_evict_cv
,
662 &dbuf_evict_lock
, SEC2NSEC(1), MSEC2NSEC(1), 0);
663 CALLB_CPR_SAFE_END(&cpr
, &dbuf_evict_lock
);
665 mutex_exit(&dbuf_evict_lock
);
668 * Keep evicting as long as we're above the low water mark
669 * for the cache. We do this without holding the locks to
670 * minimize lock contention.
672 while (dbuf_cache_above_lowater() && !dbuf_evict_thread_exit
) {
676 mutex_enter(&dbuf_evict_lock
);
679 dbuf_evict_thread_exit
= B_FALSE
;
680 cv_broadcast(&dbuf_evict_cv
);
681 CALLB_CPR_EXIT(&cpr
); /* drops dbuf_evict_lock */
686 * Wake up the dbuf eviction thread if the dbuf cache is at its max size.
687 * If the dbuf cache is at its high water mark, then evict a dbuf from the
688 * dbuf cache using the callers context.
691 dbuf_evict_notify(void)
695 * We use thread specific data to track when a thread has
696 * started processing evictions. This allows us to avoid deeply
697 * nested stacks that would have a call flow similar to this:
699 * dbuf_rele()-->dbuf_rele_and_unlock()-->dbuf_evict_notify()
702 * +-----dbuf_destroy()<--dbuf_evict_one()<--------+
704 * The dbuf_eviction_thread will always have its tsd set until
705 * that thread exits. All other threads will only set their tsd
706 * if they are participating in the eviction process. This only
707 * happens if the eviction thread is unable to process evictions
708 * fast enough. To keep the dbuf cache size in check, other threads
709 * can evict from the dbuf cache directly. Those threads will set
710 * their tsd values so that we ensure that they only evict one dbuf
711 * from the dbuf cache.
713 if (tsd_get(zfs_dbuf_evict_key
) != NULL
)
717 * We check if we should evict without holding the dbuf_evict_lock,
718 * because it's OK to occasionally make the wrong decision here,
719 * and grabbing the lock results in massive lock contention.
721 if (refcount_count(&dbuf_cache_size
) > dbuf_cache_target_bytes()) {
722 if (dbuf_cache_above_hiwater())
724 cv_signal(&dbuf_evict_cv
);
729 dbuf_kstat_update(kstat_t
*ksp
, int rw
)
731 dbuf_stats_t
*ds
= ksp
->ks_data
;
733 if (rw
== KSTAT_WRITE
) {
734 return (SET_ERROR(EACCES
));
736 ds
->cache_size_bytes
.value
.ui64
=
737 refcount_count(&dbuf_cache_size
);
738 ds
->cache_target_bytes
.value
.ui64
= dbuf_cache_target_bytes();
739 ds
->cache_hiwater_bytes
.value
.ui64
= dbuf_cache_hiwater_bytes();
740 ds
->cache_lowater_bytes
.value
.ui64
= dbuf_cache_lowater_bytes();
741 ds
->hash_elements
.value
.ui64
= dbuf_hash_count
;
750 uint64_t hsize
= 1ULL << 16;
751 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
755 * The hash table is big enough to fill all of physical memory
756 * with an average block size of zfs_arc_average_blocksize (default 8K).
757 * By default, the table will take up
758 * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers).
760 while (hsize
* zfs_arc_average_blocksize
< physmem
* PAGESIZE
)
764 h
->hash_table_mask
= hsize
- 1;
765 #if defined(_KERNEL) && defined(HAVE_SPL)
767 * Large allocations which do not require contiguous pages
768 * should be using vmem_alloc() in the linux kernel
770 h
->hash_table
= vmem_zalloc(hsize
* sizeof (void *), KM_SLEEP
);
772 h
->hash_table
= kmem_zalloc(hsize
* sizeof (void *), KM_NOSLEEP
);
774 if (h
->hash_table
== NULL
) {
775 /* XXX - we should really return an error instead of assert */
776 ASSERT(hsize
> (1ULL << 10));
781 dbuf_kmem_cache
= kmem_cache_create("dmu_buf_impl_t",
782 sizeof (dmu_buf_impl_t
),
783 0, dbuf_cons
, dbuf_dest
, NULL
, NULL
, NULL
, 0);
785 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
786 mutex_init(&h
->hash_mutexes
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
791 * Setup the parameters for the dbuf cache. We set the size of the
792 * dbuf cache to 1/32nd (default) of the target size of the ARC. If
793 * the value has been specified as a module option and it's not
794 * greater than the target size of the ARC, then we honor that value.
796 if (dbuf_cache_max_bytes
== 0 ||
797 dbuf_cache_max_bytes
>= arc_target_bytes()) {
798 dbuf_cache_max_bytes
= arc_target_bytes() >> dbuf_cache_shift
;
802 * All entries are queued via taskq_dispatch_ent(), so min/maxalloc
803 * configuration is not required.
805 dbu_evict_taskq
= taskq_create("dbu_evict", 1, defclsyspri
, 0, 0, 0);
807 dbuf_cache
= multilist_create(sizeof (dmu_buf_impl_t
),
808 offsetof(dmu_buf_impl_t
, db_cache_link
),
809 dbuf_cache_multilist_index_func
);
810 refcount_create(&dbuf_cache_size
);
812 tsd_create(&zfs_dbuf_evict_key
, NULL
);
813 dbuf_evict_thread_exit
= B_FALSE
;
814 mutex_init(&dbuf_evict_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
815 cv_init(&dbuf_evict_cv
, NULL
, CV_DEFAULT
, NULL
);
816 dbuf_cache_evict_thread
= thread_create(NULL
, 0, dbuf_evict_thread
,
817 NULL
, 0, &p0
, TS_RUN
, minclsyspri
);
819 dbuf_ksp
= kstat_create("zfs", 0, "dbufstats", "misc",
820 KSTAT_TYPE_NAMED
, sizeof (dbuf_stats
) / sizeof (kstat_named_t
),
822 if (dbuf_ksp
!= NULL
) {
823 dbuf_ksp
->ks_data
= &dbuf_stats
;
824 dbuf_ksp
->ks_update
= dbuf_kstat_update
;
825 kstat_install(dbuf_ksp
);
827 for (i
= 0; i
< DN_MAX_LEVELS
; i
++) {
828 snprintf(dbuf_stats
.cache_levels
[i
].name
,
829 KSTAT_STRLEN
, "cache_level_%d", i
);
830 dbuf_stats
.cache_levels
[i
].data_type
=
832 snprintf(dbuf_stats
.cache_levels_bytes
[i
].name
,
833 KSTAT_STRLEN
, "cache_level_%d_bytes", i
);
834 dbuf_stats
.cache_levels_bytes
[i
].data_type
=
843 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
846 dbuf_stats_destroy();
848 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
849 mutex_destroy(&h
->hash_mutexes
[i
]);
850 #if defined(_KERNEL) && defined(HAVE_SPL)
852 * Large allocations which do not require contiguous pages
853 * should be using vmem_free() in the linux kernel
855 vmem_free(h
->hash_table
, (h
->hash_table_mask
+ 1) * sizeof (void *));
857 kmem_free(h
->hash_table
, (h
->hash_table_mask
+ 1) * sizeof (void *));
859 kmem_cache_destroy(dbuf_kmem_cache
);
860 taskq_destroy(dbu_evict_taskq
);
862 mutex_enter(&dbuf_evict_lock
);
863 dbuf_evict_thread_exit
= B_TRUE
;
864 while (dbuf_evict_thread_exit
) {
865 cv_signal(&dbuf_evict_cv
);
866 cv_wait(&dbuf_evict_cv
, &dbuf_evict_lock
);
868 mutex_exit(&dbuf_evict_lock
);
869 tsd_destroy(&zfs_dbuf_evict_key
);
871 mutex_destroy(&dbuf_evict_lock
);
872 cv_destroy(&dbuf_evict_cv
);
874 refcount_destroy(&dbuf_cache_size
);
875 multilist_destroy(dbuf_cache
);
877 if (dbuf_ksp
!= NULL
) {
878 kstat_delete(dbuf_ksp
);
889 dbuf_verify(dmu_buf_impl_t
*db
)
892 dbuf_dirty_record_t
*dr
;
894 ASSERT(MUTEX_HELD(&db
->db_mtx
));
896 if (!(zfs_flags
& ZFS_DEBUG_DBUF_VERIFY
))
899 ASSERT(db
->db_objset
!= NULL
);
903 ASSERT(db
->db_parent
== NULL
);
904 ASSERT(db
->db_blkptr
== NULL
);
906 ASSERT3U(db
->db
.db_object
, ==, dn
->dn_object
);
907 ASSERT3P(db
->db_objset
, ==, dn
->dn_objset
);
908 ASSERT3U(db
->db_level
, <, dn
->dn_nlevels
);
909 ASSERT(db
->db_blkid
== DMU_BONUS_BLKID
||
910 db
->db_blkid
== DMU_SPILL_BLKID
||
911 !avl_is_empty(&dn
->dn_dbufs
));
913 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
915 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
916 ASSERT3U(db
->db
.db_offset
, ==, DMU_BONUS_BLKID
);
917 } else if (db
->db_blkid
== DMU_SPILL_BLKID
) {
919 ASSERT0(db
->db
.db_offset
);
921 ASSERT3U(db
->db
.db_offset
, ==, db
->db_blkid
* db
->db
.db_size
);
924 for (dr
= db
->db_data_pending
; dr
!= NULL
; dr
= dr
->dr_next
)
925 ASSERT(dr
->dr_dbuf
== db
);
927 for (dr
= db
->db_last_dirty
; dr
!= NULL
; dr
= dr
->dr_next
)
928 ASSERT(dr
->dr_dbuf
== db
);
931 * We can't assert that db_size matches dn_datablksz because it
932 * can be momentarily different when another thread is doing
935 if (db
->db_level
== 0 && db
->db
.db_object
== DMU_META_DNODE_OBJECT
) {
936 dr
= db
->db_data_pending
;
938 * It should only be modified in syncing context, so
939 * make sure we only have one copy of the data.
941 ASSERT(dr
== NULL
|| dr
->dt
.dl
.dr_data
== db
->db_buf
);
944 /* verify db->db_blkptr */
946 if (db
->db_parent
== dn
->dn_dbuf
) {
947 /* db is pointed to by the dnode */
948 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
949 if (DMU_OBJECT_IS_SPECIAL(db
->db
.db_object
))
950 ASSERT(db
->db_parent
== NULL
);
952 ASSERT(db
->db_parent
!= NULL
);
953 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
954 ASSERT3P(db
->db_blkptr
, ==,
955 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
957 /* db is pointed to by an indirect block */
958 ASSERTV(int epb
= db
->db_parent
->db
.db_size
>>
960 ASSERT3U(db
->db_parent
->db_level
, ==, db
->db_level
+1);
961 ASSERT3U(db
->db_parent
->db
.db_object
, ==,
964 * dnode_grow_indblksz() can make this fail if we don't
965 * have the struct_rwlock. XXX indblksz no longer
966 * grows. safe to do this now?
968 if (RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
969 ASSERT3P(db
->db_blkptr
, ==,
970 ((blkptr_t
*)db
->db_parent
->db
.db_data
+
971 db
->db_blkid
% epb
));
975 if ((db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
)) &&
976 (db
->db_buf
== NULL
|| db
->db_buf
->b_data
) &&
977 db
->db
.db_data
&& db
->db_blkid
!= DMU_BONUS_BLKID
&&
978 db
->db_state
!= DB_FILL
&& !dn
->dn_free_txg
) {
980 * If the blkptr isn't set but they have nonzero data,
981 * it had better be dirty, otherwise we'll lose that
982 * data when we evict this buffer.
984 * There is an exception to this rule for indirect blocks; in
985 * this case, if the indirect block is a hole, we fill in a few
986 * fields on each of the child blocks (importantly, birth time)
987 * to prevent hole birth times from being lost when you
988 * partially fill in a hole.
990 if (db
->db_dirtycnt
== 0) {
991 if (db
->db_level
== 0) {
992 uint64_t *buf
= db
->db
.db_data
;
995 for (i
= 0; i
< db
->db
.db_size
>> 3; i
++) {
999 blkptr_t
*bps
= db
->db
.db_data
;
1000 ASSERT3U(1 << DB_DNODE(db
)->dn_indblkshift
, ==,
1003 * We want to verify that all the blkptrs in the
1004 * indirect block are holes, but we may have
1005 * automatically set up a few fields for them.
1006 * We iterate through each blkptr and verify
1007 * they only have those fields set.
1010 i
< db
->db
.db_size
/ sizeof (blkptr_t
);
1012 blkptr_t
*bp
= &bps
[i
];
1013 ASSERT(ZIO_CHECKSUM_IS_ZERO(
1016 DVA_IS_EMPTY(&bp
->blk_dva
[0]) &&
1017 DVA_IS_EMPTY(&bp
->blk_dva
[1]) &&
1018 DVA_IS_EMPTY(&bp
->blk_dva
[2]));
1019 ASSERT0(bp
->blk_fill
);
1020 ASSERT0(bp
->blk_pad
[0]);
1021 ASSERT0(bp
->blk_pad
[1]);
1022 ASSERT(!BP_IS_EMBEDDED(bp
));
1023 ASSERT(BP_IS_HOLE(bp
));
1024 ASSERT0(bp
->blk_phys_birth
);
1034 dbuf_clear_data(dmu_buf_impl_t
*db
)
1036 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1037 dbuf_evict_user(db
);
1038 ASSERT3P(db
->db_buf
, ==, NULL
);
1039 db
->db
.db_data
= NULL
;
1040 if (db
->db_state
!= DB_NOFILL
)
1041 db
->db_state
= DB_UNCACHED
;
1045 dbuf_set_data(dmu_buf_impl_t
*db
, arc_buf_t
*buf
)
1047 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1048 ASSERT(buf
!= NULL
);
1051 ASSERT(buf
->b_data
!= NULL
);
1052 db
->db
.db_data
= buf
->b_data
;
1056 * Loan out an arc_buf for read. Return the loaned arc_buf.
1059 dbuf_loan_arcbuf(dmu_buf_impl_t
*db
)
1063 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1064 mutex_enter(&db
->db_mtx
);
1065 if (arc_released(db
->db_buf
) || refcount_count(&db
->db_holds
) > 1) {
1066 int blksz
= db
->db
.db_size
;
1067 spa_t
*spa
= db
->db_objset
->os_spa
;
1069 mutex_exit(&db
->db_mtx
);
1070 abuf
= arc_loan_buf(spa
, B_FALSE
, blksz
);
1071 bcopy(db
->db
.db_data
, abuf
->b_data
, blksz
);
1074 arc_loan_inuse_buf(abuf
, db
);
1076 dbuf_clear_data(db
);
1077 mutex_exit(&db
->db_mtx
);
1083 * Calculate which level n block references the data at the level 0 offset
1087 dbuf_whichblock(const dnode_t
*dn
, const int64_t level
, const uint64_t offset
)
1089 if (dn
->dn_datablkshift
!= 0 && dn
->dn_indblkshift
!= 0) {
1091 * The level n blkid is equal to the level 0 blkid divided by
1092 * the number of level 0s in a level n block.
1094 * The level 0 blkid is offset >> datablkshift =
1095 * offset / 2^datablkshift.
1097 * The number of level 0s in a level n is the number of block
1098 * pointers in an indirect block, raised to the power of level.
1099 * This is 2^(indblkshift - SPA_BLKPTRSHIFT)^level =
1100 * 2^(level*(indblkshift - SPA_BLKPTRSHIFT)).
1102 * Thus, the level n blkid is: offset /
1103 * ((2^datablkshift)*(2^(level*(indblkshift - SPA_BLKPTRSHIFT)))
1104 * = offset / 2^(datablkshift + level *
1105 * (indblkshift - SPA_BLKPTRSHIFT))
1106 * = offset >> (datablkshift + level *
1107 * (indblkshift - SPA_BLKPTRSHIFT))
1110 const unsigned exp
= dn
->dn_datablkshift
+
1111 level
* (dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
);
1113 if (exp
>= 8 * sizeof (offset
)) {
1114 /* This only happens on the highest indirection level */
1115 ASSERT3U(level
, ==, dn
->dn_nlevels
- 1);
1119 ASSERT3U(exp
, <, 8 * sizeof (offset
));
1121 return (offset
>> exp
);
1123 ASSERT3U(offset
, <, dn
->dn_datablksz
);
1129 dbuf_read_done(zio_t
*zio
, const zbookmark_phys_t
*zb
, const blkptr_t
*bp
,
1130 arc_buf_t
*buf
, void *vdb
)
1132 dmu_buf_impl_t
*db
= vdb
;
1134 mutex_enter(&db
->db_mtx
);
1135 ASSERT3U(db
->db_state
, ==, DB_READ
);
1137 * All reads are synchronous, so we must have a hold on the dbuf
1139 ASSERT(refcount_count(&db
->db_holds
) > 0);
1140 ASSERT(db
->db_buf
== NULL
);
1141 ASSERT(db
->db
.db_data
== NULL
);
1142 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
1143 /* we were freed in flight; disregard any error */
1145 buf
= arc_alloc_buf(db
->db_objset
->os_spa
,
1146 db
, DBUF_GET_BUFC_TYPE(db
), db
->db
.db_size
);
1148 arc_release(buf
, db
);
1149 bzero(buf
->b_data
, db
->db
.db_size
);
1150 arc_buf_freeze(buf
);
1151 db
->db_freed_in_flight
= FALSE
;
1152 dbuf_set_data(db
, buf
);
1153 db
->db_state
= DB_CACHED
;
1154 } else if (buf
!= NULL
) {
1155 dbuf_set_data(db
, buf
);
1156 db
->db_state
= DB_CACHED
;
1158 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1159 ASSERT3P(db
->db_buf
, ==, NULL
);
1160 db
->db_state
= DB_UNCACHED
;
1162 cv_broadcast(&db
->db_changed
);
1163 dbuf_rele_and_unlock(db
, NULL
);
1167 dbuf_read_impl(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t flags
)
1170 zbookmark_phys_t zb
;
1171 uint32_t aflags
= ARC_FLAG_NOWAIT
;
1172 int err
, zio_flags
= 0;
1176 ASSERT(!refcount_is_zero(&db
->db_holds
));
1177 /* We need the struct_rwlock to prevent db_blkptr from changing. */
1178 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
1179 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1180 ASSERT(db
->db_state
== DB_UNCACHED
);
1181 ASSERT(db
->db_buf
== NULL
);
1183 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1185 * The bonus length stored in the dnode may be less than
1186 * the maximum available space in the bonus buffer.
1188 int bonuslen
= MIN(dn
->dn_bonuslen
, dn
->dn_phys
->dn_bonuslen
);
1189 int max_bonuslen
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
);
1190 arc_buf_t
*dn_buf
= (dn
->dn_dbuf
!= NULL
) ?
1191 dn
->dn_dbuf
->db_buf
: NULL
;
1193 /* if the underlying dnode block is encrypted, decrypt it */
1194 if (dn_buf
!= NULL
&& dn
->dn_objset
->os_encrypted
&&
1195 DMU_OT_IS_ENCRYPTED(dn
->dn_bonustype
) &&
1196 (flags
& DB_RF_NO_DECRYPT
) == 0 &&
1197 arc_is_encrypted(dn_buf
)) {
1198 SET_BOOKMARK(&zb
, dmu_objset_id(db
->db_objset
),
1199 DMU_META_DNODE_OBJECT
, 0, dn
->dn_dbuf
->db_blkid
);
1200 err
= arc_untransform(dn_buf
, dn
->dn_objset
->os_spa
,
1204 mutex_exit(&db
->db_mtx
);
1209 ASSERT3U(bonuslen
, <=, db
->db
.db_size
);
1210 db
->db
.db_data
= kmem_alloc(max_bonuslen
, KM_SLEEP
);
1211 arc_space_consume(max_bonuslen
, ARC_SPACE_BONUS
);
1212 if (bonuslen
< max_bonuslen
)
1213 bzero(db
->db
.db_data
, max_bonuslen
);
1215 bcopy(DN_BONUS(dn
->dn_phys
), db
->db
.db_data
, bonuslen
);
1217 db
->db_state
= DB_CACHED
;
1218 mutex_exit(&db
->db_mtx
);
1223 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
1224 * processes the delete record and clears the bp while we are waiting
1225 * for the dn_mtx (resulting in a "no" from block_freed).
1227 if (db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
) ||
1228 (db
->db_level
== 0 && (dnode_block_freed(dn
, db
->db_blkid
) ||
1229 BP_IS_HOLE(db
->db_blkptr
)))) {
1230 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1232 dbuf_set_data(db
, arc_alloc_buf(db
->db_objset
->os_spa
, db
, type
,
1234 bzero(db
->db
.db_data
, db
->db
.db_size
);
1236 if (db
->db_blkptr
!= NULL
&& db
->db_level
> 0 &&
1237 BP_IS_HOLE(db
->db_blkptr
) &&
1238 db
->db_blkptr
->blk_birth
!= 0) {
1239 blkptr_t
*bps
= db
->db
.db_data
;
1240 for (int i
= 0; i
< ((1 <<
1241 DB_DNODE(db
)->dn_indblkshift
) / sizeof (blkptr_t
));
1243 blkptr_t
*bp
= &bps
[i
];
1244 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==,
1245 1 << dn
->dn_indblkshift
);
1247 BP_GET_LEVEL(db
->db_blkptr
) == 1 ?
1249 BP_GET_LSIZE(db
->db_blkptr
));
1250 BP_SET_TYPE(bp
, BP_GET_TYPE(db
->db_blkptr
));
1252 BP_GET_LEVEL(db
->db_blkptr
) - 1);
1253 BP_SET_BIRTH(bp
, db
->db_blkptr
->blk_birth
, 0);
1257 db
->db_state
= DB_CACHED
;
1258 mutex_exit(&db
->db_mtx
);
1264 db
->db_state
= DB_READ
;
1265 mutex_exit(&db
->db_mtx
);
1267 if (DBUF_IS_L2CACHEABLE(db
))
1268 aflags
|= ARC_FLAG_L2CACHE
;
1270 SET_BOOKMARK(&zb
, dmu_objset_id(db
->db_objset
),
1271 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
1274 * All bps of an encrypted os should have the encryption bit set.
1275 * If this is not true it indicates tampering and we report an error.
1277 if (db
->db_objset
->os_encrypted
&& !BP_USES_CRYPT(db
->db_blkptr
)) {
1278 spa_log_error(db
->db_objset
->os_spa
, &zb
);
1279 zfs_panic_recover("unencrypted block in encrypted "
1280 "object set %llu", dmu_objset_id(db
->db_objset
));
1281 return (SET_ERROR(EIO
));
1284 dbuf_add_ref(db
, NULL
);
1286 zio_flags
= (flags
& DB_RF_CANFAIL
) ?
1287 ZIO_FLAG_CANFAIL
: ZIO_FLAG_MUSTSUCCEED
;
1289 if ((flags
& DB_RF_NO_DECRYPT
) && BP_IS_PROTECTED(db
->db_blkptr
))
1290 zio_flags
|= ZIO_FLAG_RAW
;
1292 err
= arc_read(zio
, db
->db_objset
->os_spa
, db
->db_blkptr
,
1293 dbuf_read_done
, db
, ZIO_PRIORITY_SYNC_READ
, zio_flags
,
1300 * This is our just-in-time copy function. It makes a copy of buffers that
1301 * have been modified in a previous transaction group before we access them in
1302 * the current active group.
1304 * This function is used in three places: when we are dirtying a buffer for the
1305 * first time in a txg, when we are freeing a range in a dnode that includes
1306 * this buffer, and when we are accessing a buffer which was received compressed
1307 * and later referenced in a WRITE_BYREF record.
1309 * Note that when we are called from dbuf_free_range() we do not put a hold on
1310 * the buffer, we just traverse the active dbuf list for the dnode.
1313 dbuf_fix_old_data(dmu_buf_impl_t
*db
, uint64_t txg
)
1315 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
1317 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1318 ASSERT(db
->db
.db_data
!= NULL
);
1319 ASSERT(db
->db_level
== 0);
1320 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
);
1323 (dr
->dt
.dl
.dr_data
!=
1324 ((db
->db_blkid
== DMU_BONUS_BLKID
) ? db
->db
.db_data
: db
->db_buf
)))
1328 * If the last dirty record for this dbuf has not yet synced
1329 * and its referencing the dbuf data, either:
1330 * reset the reference to point to a new copy,
1331 * or (if there a no active holders)
1332 * just null out the current db_data pointer.
1334 ASSERT3U(dr
->dr_txg
, >=, txg
- 2);
1335 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1336 dnode_t
*dn
= DB_DNODE(db
);
1337 int bonuslen
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
);
1338 dr
->dt
.dl
.dr_data
= kmem_alloc(bonuslen
, KM_SLEEP
);
1339 arc_space_consume(bonuslen
, ARC_SPACE_BONUS
);
1340 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
, bonuslen
);
1341 } else if (refcount_count(&db
->db_holds
) > db
->db_dirtycnt
) {
1342 dnode_t
*dn
= DB_DNODE(db
);
1343 int size
= arc_buf_size(db
->db_buf
);
1344 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1345 spa_t
*spa
= db
->db_objset
->os_spa
;
1346 enum zio_compress compress_type
=
1347 arc_get_compression(db
->db_buf
);
1349 if (arc_is_encrypted(db
->db_buf
)) {
1350 boolean_t byteorder
;
1351 uint8_t salt
[ZIO_DATA_SALT_LEN
];
1352 uint8_t iv
[ZIO_DATA_IV_LEN
];
1353 uint8_t mac
[ZIO_DATA_MAC_LEN
];
1355 arc_get_raw_params(db
->db_buf
, &byteorder
, salt
,
1357 dr
->dt
.dl
.dr_data
= arc_alloc_raw_buf(spa
, db
,
1358 dmu_objset_id(dn
->dn_objset
), byteorder
, salt
, iv
,
1359 mac
, dn
->dn_type
, size
, arc_buf_lsize(db
->db_buf
),
1361 } else if (compress_type
!= ZIO_COMPRESS_OFF
) {
1362 ASSERT3U(type
, ==, ARC_BUFC_DATA
);
1363 dr
->dt
.dl
.dr_data
= arc_alloc_compressed_buf(spa
, db
,
1364 size
, arc_buf_lsize(db
->db_buf
), compress_type
);
1366 dr
->dt
.dl
.dr_data
= arc_alloc_buf(spa
, db
, type
, size
);
1368 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
->b_data
, size
);
1371 dbuf_clear_data(db
);
1376 dbuf_read(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t flags
)
1383 * We don't have to hold the mutex to check db_state because it
1384 * can't be freed while we have a hold on the buffer.
1386 ASSERT(!refcount_is_zero(&db
->db_holds
));
1388 if (db
->db_state
== DB_NOFILL
)
1389 return (SET_ERROR(EIO
));
1393 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1394 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
1396 prefetch
= db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1397 (flags
& DB_RF_NOPREFETCH
) == 0 && dn
!= NULL
&&
1398 DBUF_IS_CACHEABLE(db
);
1400 mutex_enter(&db
->db_mtx
);
1401 if (db
->db_state
== DB_CACHED
) {
1402 spa_t
*spa
= dn
->dn_objset
->os_spa
;
1405 * If the arc buf is compressed or encrypted, we need to
1406 * untransform it to read the data. This could happen during
1407 * the "zfs receive" of a stream which is deduplicated and
1408 * either raw or compressed. We do not need to do this if the
1409 * caller wants raw encrypted data.
1411 if (db
->db_buf
!= NULL
&& (flags
& DB_RF_NO_DECRYPT
) == 0 &&
1412 (arc_is_encrypted(db
->db_buf
) ||
1413 arc_get_compression(db
->db_buf
) != ZIO_COMPRESS_OFF
)) {
1414 zbookmark_phys_t zb
;
1416 SET_BOOKMARK(&zb
, dmu_objset_id(db
->db_objset
),
1417 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
1418 dbuf_fix_old_data(db
, spa_syncing_txg(spa
));
1419 err
= arc_untransform(db
->db_buf
, spa
, &zb
, B_FALSE
);
1420 dbuf_set_data(db
, db
->db_buf
);
1422 mutex_exit(&db
->db_mtx
);
1424 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
);
1425 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1426 rw_exit(&dn
->dn_struct_rwlock
);
1428 DBUF_STAT_BUMP(hash_hits
);
1429 } else if (db
->db_state
== DB_UNCACHED
) {
1430 spa_t
*spa
= dn
->dn_objset
->os_spa
;
1431 boolean_t need_wait
= B_FALSE
;
1434 db
->db_blkptr
!= NULL
&& !BP_IS_HOLE(db
->db_blkptr
)) {
1435 zio
= zio_root(spa
, NULL
, NULL
, ZIO_FLAG_CANFAIL
);
1438 err
= dbuf_read_impl(db
, zio
, flags
);
1440 /* dbuf_read_impl has dropped db_mtx for us */
1442 if (!err
&& prefetch
)
1443 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
);
1445 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1446 rw_exit(&dn
->dn_struct_rwlock
);
1448 DBUF_STAT_BUMP(hash_misses
);
1450 if (!err
&& need_wait
)
1451 err
= zio_wait(zio
);
1454 * Another reader came in while the dbuf was in flight
1455 * between UNCACHED and CACHED. Either a writer will finish
1456 * writing the buffer (sending the dbuf to CACHED) or the
1457 * first reader's request will reach the read_done callback
1458 * and send the dbuf to CACHED. Otherwise, a failure
1459 * occurred and the dbuf went to UNCACHED.
1461 mutex_exit(&db
->db_mtx
);
1463 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
);
1464 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1465 rw_exit(&dn
->dn_struct_rwlock
);
1467 DBUF_STAT_BUMP(hash_misses
);
1469 /* Skip the wait per the caller's request. */
1470 mutex_enter(&db
->db_mtx
);
1471 if ((flags
& DB_RF_NEVERWAIT
) == 0) {
1472 while (db
->db_state
== DB_READ
||
1473 db
->db_state
== DB_FILL
) {
1474 ASSERT(db
->db_state
== DB_READ
||
1475 (flags
& DB_RF_HAVESTRUCT
) == 0);
1476 DTRACE_PROBE2(blocked__read
, dmu_buf_impl_t
*,
1478 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1480 if (db
->db_state
== DB_UNCACHED
)
1481 err
= SET_ERROR(EIO
);
1483 mutex_exit(&db
->db_mtx
);
1490 dbuf_noread(dmu_buf_impl_t
*db
)
1492 ASSERT(!refcount_is_zero(&db
->db_holds
));
1493 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1494 mutex_enter(&db
->db_mtx
);
1495 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
1496 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1497 if (db
->db_state
== DB_UNCACHED
) {
1498 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1499 spa_t
*spa
= db
->db_objset
->os_spa
;
1501 ASSERT(db
->db_buf
== NULL
);
1502 ASSERT(db
->db
.db_data
== NULL
);
1503 dbuf_set_data(db
, arc_alloc_buf(spa
, db
, type
, db
->db
.db_size
));
1504 db
->db_state
= DB_FILL
;
1505 } else if (db
->db_state
== DB_NOFILL
) {
1506 dbuf_clear_data(db
);
1508 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
1510 mutex_exit(&db
->db_mtx
);
1514 dbuf_unoverride(dbuf_dirty_record_t
*dr
)
1516 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
1517 blkptr_t
*bp
= &dr
->dt
.dl
.dr_overridden_by
;
1518 uint64_t txg
= dr
->dr_txg
;
1520 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1522 * This assert is valid because dmu_sync() expects to be called by
1523 * a zilog's get_data while holding a range lock. This call only
1524 * comes from dbuf_dirty() callers who must also hold a range lock.
1526 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_IN_DMU_SYNC
);
1527 ASSERT(db
->db_level
== 0);
1529 if (db
->db_blkid
== DMU_BONUS_BLKID
||
1530 dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
)
1533 ASSERT(db
->db_data_pending
!= dr
);
1535 /* free this block */
1536 if (!BP_IS_HOLE(bp
) && !dr
->dt
.dl
.dr_nopwrite
)
1537 zio_free(db
->db_objset
->os_spa
, txg
, bp
);
1539 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
1540 dr
->dt
.dl
.dr_nopwrite
= B_FALSE
;
1541 dr
->dt
.dl
.dr_raw
= B_FALSE
;
1544 * Release the already-written buffer, so we leave it in
1545 * a consistent dirty state. Note that all callers are
1546 * modifying the buffer, so they will immediately do
1547 * another (redundant) arc_release(). Therefore, leave
1548 * the buf thawed to save the effort of freezing &
1549 * immediately re-thawing it.
1551 arc_release(dr
->dt
.dl
.dr_data
, db
);
1555 * Evict (if its unreferenced) or clear (if its referenced) any level-0
1556 * data blocks in the free range, so that any future readers will find
1560 dbuf_free_range(dnode_t
*dn
, uint64_t start_blkid
, uint64_t end_blkid
,
1563 dmu_buf_impl_t
*db_search
;
1564 dmu_buf_impl_t
*db
, *db_next
;
1565 uint64_t txg
= tx
->tx_txg
;
1568 if (end_blkid
> dn
->dn_maxblkid
&&
1569 !(start_blkid
== DMU_SPILL_BLKID
|| end_blkid
== DMU_SPILL_BLKID
))
1570 end_blkid
= dn
->dn_maxblkid
;
1571 dprintf_dnode(dn
, "start=%llu end=%llu\n", start_blkid
, end_blkid
);
1573 db_search
= kmem_alloc(sizeof (dmu_buf_impl_t
), KM_SLEEP
);
1574 db_search
->db_level
= 0;
1575 db_search
->db_blkid
= start_blkid
;
1576 db_search
->db_state
= DB_SEARCH
;
1578 mutex_enter(&dn
->dn_dbufs_mtx
);
1579 db
= avl_find(&dn
->dn_dbufs
, db_search
, &where
);
1580 ASSERT3P(db
, ==, NULL
);
1582 db
= avl_nearest(&dn
->dn_dbufs
, where
, AVL_AFTER
);
1584 for (; db
!= NULL
; db
= db_next
) {
1585 db_next
= AVL_NEXT(&dn
->dn_dbufs
, db
);
1586 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1588 if (db
->db_level
!= 0 || db
->db_blkid
> end_blkid
) {
1591 ASSERT3U(db
->db_blkid
, >=, start_blkid
);
1593 /* found a level 0 buffer in the range */
1594 mutex_enter(&db
->db_mtx
);
1595 if (dbuf_undirty(db
, tx
)) {
1596 /* mutex has been dropped and dbuf destroyed */
1600 if (db
->db_state
== DB_UNCACHED
||
1601 db
->db_state
== DB_NOFILL
||
1602 db
->db_state
== DB_EVICTING
) {
1603 ASSERT(db
->db
.db_data
== NULL
);
1604 mutex_exit(&db
->db_mtx
);
1607 if (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
) {
1608 /* will be handled in dbuf_read_done or dbuf_rele */
1609 db
->db_freed_in_flight
= TRUE
;
1610 mutex_exit(&db
->db_mtx
);
1613 if (refcount_count(&db
->db_holds
) == 0) {
1618 /* The dbuf is referenced */
1620 if (db
->db_last_dirty
!= NULL
) {
1621 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
1623 if (dr
->dr_txg
== txg
) {
1625 * This buffer is "in-use", re-adjust the file
1626 * size to reflect that this buffer may
1627 * contain new data when we sync.
1629 if (db
->db_blkid
!= DMU_SPILL_BLKID
&&
1630 db
->db_blkid
> dn
->dn_maxblkid
)
1631 dn
->dn_maxblkid
= db
->db_blkid
;
1632 dbuf_unoverride(dr
);
1635 * This dbuf is not dirty in the open context.
1636 * Either uncache it (if its not referenced in
1637 * the open context) or reset its contents to
1640 dbuf_fix_old_data(db
, txg
);
1643 /* clear the contents if its cached */
1644 if (db
->db_state
== DB_CACHED
) {
1645 ASSERT(db
->db
.db_data
!= NULL
);
1646 arc_release(db
->db_buf
, db
);
1647 bzero(db
->db
.db_data
, db
->db
.db_size
);
1648 arc_buf_freeze(db
->db_buf
);
1651 mutex_exit(&db
->db_mtx
);
1654 kmem_free(db_search
, sizeof (dmu_buf_impl_t
));
1655 mutex_exit(&dn
->dn_dbufs_mtx
);
1659 dbuf_new_size(dmu_buf_impl_t
*db
, int size
, dmu_tx_t
*tx
)
1661 arc_buf_t
*buf
, *obuf
;
1662 int osize
= db
->db
.db_size
;
1663 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1666 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1671 /* XXX does *this* func really need the lock? */
1672 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
1675 * This call to dmu_buf_will_dirty() with the dn_struct_rwlock held
1676 * is OK, because there can be no other references to the db
1677 * when we are changing its size, so no concurrent DB_FILL can
1681 * XXX we should be doing a dbuf_read, checking the return
1682 * value and returning that up to our callers
1684 dmu_buf_will_dirty(&db
->db
, tx
);
1686 /* create the data buffer for the new block */
1687 buf
= arc_alloc_buf(dn
->dn_objset
->os_spa
, db
, type
, size
);
1689 /* copy old block data to the new block */
1691 bcopy(obuf
->b_data
, buf
->b_data
, MIN(osize
, size
));
1692 /* zero the remainder */
1694 bzero((uint8_t *)buf
->b_data
+ osize
, size
- osize
);
1696 mutex_enter(&db
->db_mtx
);
1697 dbuf_set_data(db
, buf
);
1698 arc_buf_destroy(obuf
, db
);
1699 db
->db
.db_size
= size
;
1701 if (db
->db_level
== 0) {
1702 ASSERT3U(db
->db_last_dirty
->dr_txg
, ==, tx
->tx_txg
);
1703 db
->db_last_dirty
->dt
.dl
.dr_data
= buf
;
1705 mutex_exit(&db
->db_mtx
);
1707 dmu_objset_willuse_space(dn
->dn_objset
, size
- osize
, tx
);
1712 dbuf_release_bp(dmu_buf_impl_t
*db
)
1714 ASSERTV(objset_t
*os
= db
->db_objset
);
1716 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os
)));
1717 ASSERT(arc_released(os
->os_phys_buf
) ||
1718 list_link_active(&os
->os_dsl_dataset
->ds_synced_link
));
1719 ASSERT(db
->db_parent
== NULL
|| arc_released(db
->db_parent
->db_buf
));
1721 (void) arc_release(db
->db_buf
, db
);
1725 * We already have a dirty record for this TXG, and we are being
1729 dbuf_redirty(dbuf_dirty_record_t
*dr
)
1731 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
1733 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1735 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
) {
1737 * If this buffer has already been written out,
1738 * we now need to reset its state.
1740 dbuf_unoverride(dr
);
1741 if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
&&
1742 db
->db_state
!= DB_NOFILL
) {
1743 /* Already released on initial dirty, so just thaw. */
1744 ASSERT(arc_released(db
->db_buf
));
1745 arc_buf_thaw(db
->db_buf
);
1750 dbuf_dirty_record_t
*
1751 dbuf_dirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1755 dbuf_dirty_record_t
**drp
, *dr
;
1756 int drop_struct_lock
= FALSE
;
1757 int txgoff
= tx
->tx_txg
& TXG_MASK
;
1759 ASSERT(tx
->tx_txg
!= 0);
1760 ASSERT(!refcount_is_zero(&db
->db_holds
));
1761 DMU_TX_DIRTY_BUF(tx
, db
);
1766 * Shouldn't dirty a regular buffer in syncing context. Private
1767 * objects may be dirtied in syncing context, but only if they
1768 * were already pre-dirtied in open context.
1771 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
) {
1772 rrw_enter(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
,
1775 ASSERT(!dmu_tx_is_syncing(tx
) ||
1776 BP_IS_HOLE(dn
->dn_objset
->os_rootbp
) ||
1777 DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
1778 dn
->dn_objset
->os_dsl_dataset
== NULL
);
1779 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
1780 rrw_exit(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
, FTAG
);
1783 * We make this assert for private objects as well, but after we
1784 * check if we're already dirty. They are allowed to re-dirty
1785 * in syncing context.
1787 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
1788 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
1789 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
1791 mutex_enter(&db
->db_mtx
);
1793 * XXX make this true for indirects too? The problem is that
1794 * transactions created with dmu_tx_create_assigned() from
1795 * syncing context don't bother holding ahead.
1797 ASSERT(db
->db_level
!= 0 ||
1798 db
->db_state
== DB_CACHED
|| db
->db_state
== DB_FILL
||
1799 db
->db_state
== DB_NOFILL
);
1801 mutex_enter(&dn
->dn_mtx
);
1803 * Don't set dirtyctx to SYNC if we're just modifying this as we
1804 * initialize the objset.
1806 if (dn
->dn_dirtyctx
== DN_UNDIRTIED
) {
1807 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
) {
1808 rrw_enter(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
,
1811 if (!BP_IS_HOLE(dn
->dn_objset
->os_rootbp
)) {
1812 dn
->dn_dirtyctx
= (dmu_tx_is_syncing(tx
) ?
1813 DN_DIRTY_SYNC
: DN_DIRTY_OPEN
);
1814 ASSERT(dn
->dn_dirtyctx_firstset
== NULL
);
1815 dn
->dn_dirtyctx_firstset
= kmem_alloc(1, KM_SLEEP
);
1817 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
) {
1818 rrw_exit(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
,
1823 if (tx
->tx_txg
> dn
->dn_dirty_txg
)
1824 dn
->dn_dirty_txg
= tx
->tx_txg
;
1825 mutex_exit(&dn
->dn_mtx
);
1827 if (db
->db_blkid
== DMU_SPILL_BLKID
)
1828 dn
->dn_have_spill
= B_TRUE
;
1831 * If this buffer is already dirty, we're done.
1833 drp
= &db
->db_last_dirty
;
1834 ASSERT(*drp
== NULL
|| (*drp
)->dr_txg
<= tx
->tx_txg
||
1835 db
->db
.db_object
== DMU_META_DNODE_OBJECT
);
1836 while ((dr
= *drp
) != NULL
&& dr
->dr_txg
> tx
->tx_txg
)
1838 if (dr
&& dr
->dr_txg
== tx
->tx_txg
) {
1842 mutex_exit(&db
->db_mtx
);
1847 * Only valid if not already dirty.
1849 ASSERT(dn
->dn_object
== 0 ||
1850 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
1851 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
1853 ASSERT3U(dn
->dn_nlevels
, >, db
->db_level
);
1856 * We should only be dirtying in syncing context if it's the
1857 * mos or we're initializing the os or it's a special object.
1858 * However, we are allowed to dirty in syncing context provided
1859 * we already dirtied it in open context. Hence we must make
1860 * this assertion only if we're not already dirty.
1863 VERIFY3U(tx
->tx_txg
, <=, spa_final_dirty_txg(os
->os_spa
));
1865 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
1866 rrw_enter(&os
->os_dsl_dataset
->ds_bp_rwlock
, RW_READER
, FTAG
);
1867 ASSERT(!dmu_tx_is_syncing(tx
) || DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
1868 os
->os_dsl_dataset
== NULL
|| BP_IS_HOLE(os
->os_rootbp
));
1869 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
1870 rrw_exit(&os
->os_dsl_dataset
->ds_bp_rwlock
, FTAG
);
1872 ASSERT(db
->db
.db_size
!= 0);
1874 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
1876 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
1877 dmu_objset_willuse_space(os
, db
->db
.db_size
, tx
);
1881 * If this buffer is dirty in an old transaction group we need
1882 * to make a copy of it so that the changes we make in this
1883 * transaction group won't leak out when we sync the older txg.
1885 dr
= kmem_zalloc(sizeof (dbuf_dirty_record_t
), KM_SLEEP
);
1886 list_link_init(&dr
->dr_dirty_node
);
1887 if (db
->db_level
== 0) {
1888 void *data_old
= db
->db_buf
;
1890 if (db
->db_state
!= DB_NOFILL
) {
1891 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1892 dbuf_fix_old_data(db
, tx
->tx_txg
);
1893 data_old
= db
->db
.db_data
;
1894 } else if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
) {
1896 * Release the data buffer from the cache so
1897 * that we can modify it without impacting
1898 * possible other users of this cached data
1899 * block. Note that indirect blocks and
1900 * private objects are not released until the
1901 * syncing state (since they are only modified
1904 arc_release(db
->db_buf
, db
);
1905 dbuf_fix_old_data(db
, tx
->tx_txg
);
1906 data_old
= db
->db_buf
;
1908 ASSERT(data_old
!= NULL
);
1910 dr
->dt
.dl
.dr_data
= data_old
;
1912 mutex_init(&dr
->dt
.di
.dr_mtx
, NULL
, MUTEX_NOLOCKDEP
, NULL
);
1913 list_create(&dr
->dt
.di
.dr_children
,
1914 sizeof (dbuf_dirty_record_t
),
1915 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
1917 if (db
->db_blkid
!= DMU_BONUS_BLKID
&& os
->os_dsl_dataset
!= NULL
)
1918 dr
->dr_accounted
= db
->db
.db_size
;
1920 dr
->dr_txg
= tx
->tx_txg
;
1925 * We could have been freed_in_flight between the dbuf_noread
1926 * and dbuf_dirty. We win, as though the dbuf_noread() had
1927 * happened after the free.
1929 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1930 db
->db_blkid
!= DMU_SPILL_BLKID
) {
1931 mutex_enter(&dn
->dn_mtx
);
1932 if (dn
->dn_free_ranges
[txgoff
] != NULL
) {
1933 range_tree_clear(dn
->dn_free_ranges
[txgoff
],
1936 mutex_exit(&dn
->dn_mtx
);
1937 db
->db_freed_in_flight
= FALSE
;
1941 * This buffer is now part of this txg
1943 dbuf_add_ref(db
, (void *)(uintptr_t)tx
->tx_txg
);
1944 db
->db_dirtycnt
+= 1;
1945 ASSERT3U(db
->db_dirtycnt
, <=, 3);
1947 mutex_exit(&db
->db_mtx
);
1949 if (db
->db_blkid
== DMU_BONUS_BLKID
||
1950 db
->db_blkid
== DMU_SPILL_BLKID
) {
1951 mutex_enter(&dn
->dn_mtx
);
1952 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
1953 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
1954 mutex_exit(&dn
->dn_mtx
);
1955 dnode_setdirty(dn
, tx
);
1961 * The dn_struct_rwlock prevents db_blkptr from changing
1962 * due to a write from syncing context completing
1963 * while we are running, so we want to acquire it before
1964 * looking at db_blkptr.
1966 if (!RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
1967 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
1968 drop_struct_lock
= TRUE
;
1972 * We need to hold the dn_struct_rwlock to make this assertion,
1973 * because it protects dn_phys / dn_next_nlevels from changing.
1975 ASSERT((dn
->dn_phys
->dn_nlevels
== 0 && db
->db_level
== 0) ||
1976 dn
->dn_phys
->dn_nlevels
> db
->db_level
||
1977 dn
->dn_next_nlevels
[txgoff
] > db
->db_level
||
1978 dn
->dn_next_nlevels
[(tx
->tx_txg
-1) & TXG_MASK
] > db
->db_level
||
1979 dn
->dn_next_nlevels
[(tx
->tx_txg
-2) & TXG_MASK
] > db
->db_level
);
1982 * If we are overwriting a dedup BP, then unless it is snapshotted,
1983 * when we get to syncing context we will need to decrement its
1984 * refcount in the DDT. Prefetch the relevant DDT block so that
1985 * syncing context won't have to wait for the i/o.
1987 ddt_prefetch(os
->os_spa
, db
->db_blkptr
);
1989 if (db
->db_level
== 0) {
1990 dnode_new_blkid(dn
, db
->db_blkid
, tx
, drop_struct_lock
);
1991 ASSERT(dn
->dn_maxblkid
>= db
->db_blkid
);
1994 if (db
->db_level
+1 < dn
->dn_nlevels
) {
1995 dmu_buf_impl_t
*parent
= db
->db_parent
;
1996 dbuf_dirty_record_t
*di
;
1997 int parent_held
= FALSE
;
1999 if (db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
) {
2000 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2002 parent
= dbuf_hold_level(dn
, db
->db_level
+1,
2003 db
->db_blkid
>> epbs
, FTAG
);
2004 ASSERT(parent
!= NULL
);
2007 if (drop_struct_lock
)
2008 rw_exit(&dn
->dn_struct_rwlock
);
2009 ASSERT3U(db
->db_level
+1, ==, parent
->db_level
);
2010 di
= dbuf_dirty(parent
, tx
);
2012 dbuf_rele(parent
, FTAG
);
2014 mutex_enter(&db
->db_mtx
);
2016 * Since we've dropped the mutex, it's possible that
2017 * dbuf_undirty() might have changed this out from under us.
2019 if (db
->db_last_dirty
== dr
||
2020 dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
2021 mutex_enter(&di
->dt
.di
.dr_mtx
);
2022 ASSERT3U(di
->dr_txg
, ==, tx
->tx_txg
);
2023 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
2024 list_insert_tail(&di
->dt
.di
.dr_children
, dr
);
2025 mutex_exit(&di
->dt
.di
.dr_mtx
);
2028 mutex_exit(&db
->db_mtx
);
2030 ASSERT(db
->db_level
+1 == dn
->dn_nlevels
);
2031 ASSERT(db
->db_blkid
< dn
->dn_nblkptr
);
2032 ASSERT(db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
2033 mutex_enter(&dn
->dn_mtx
);
2034 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
2035 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
2036 mutex_exit(&dn
->dn_mtx
);
2037 if (drop_struct_lock
)
2038 rw_exit(&dn
->dn_struct_rwlock
);
2041 dnode_setdirty(dn
, tx
);
2047 * Undirty a buffer in the transaction group referenced by the given
2048 * transaction. Return whether this evicted the dbuf.
2051 dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
2054 uint64_t txg
= tx
->tx_txg
;
2055 dbuf_dirty_record_t
*dr
, **drp
;
2060 * Due to our use of dn_nlevels below, this can only be called
2061 * in open context, unless we are operating on the MOS.
2062 * From syncing context, dn_nlevels may be different from the
2063 * dn_nlevels used when dbuf was dirtied.
2065 ASSERT(db
->db_objset
==
2066 dmu_objset_pool(db
->db_objset
)->dp_meta_objset
||
2067 txg
!= spa_syncing_txg(dmu_objset_spa(db
->db_objset
)));
2068 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2069 ASSERT0(db
->db_level
);
2070 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2073 * If this buffer is not dirty, we're done.
2075 for (drp
= &db
->db_last_dirty
; (dr
= *drp
) != NULL
; drp
= &dr
->dr_next
)
2076 if (dr
->dr_txg
<= txg
)
2078 if (dr
== NULL
|| dr
->dr_txg
< txg
)
2080 ASSERT(dr
->dr_txg
== txg
);
2081 ASSERT(dr
->dr_dbuf
== db
);
2086 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
2088 ASSERT(db
->db
.db_size
!= 0);
2090 dsl_pool_undirty_space(dmu_objset_pool(dn
->dn_objset
),
2091 dr
->dr_accounted
, txg
);
2096 * Note that there are three places in dbuf_dirty()
2097 * where this dirty record may be put on a list.
2098 * Make sure to do a list_remove corresponding to
2099 * every one of those list_insert calls.
2101 if (dr
->dr_parent
) {
2102 mutex_enter(&dr
->dr_parent
->dt
.di
.dr_mtx
);
2103 list_remove(&dr
->dr_parent
->dt
.di
.dr_children
, dr
);
2104 mutex_exit(&dr
->dr_parent
->dt
.di
.dr_mtx
);
2105 } else if (db
->db_blkid
== DMU_SPILL_BLKID
||
2106 db
->db_level
+ 1 == dn
->dn_nlevels
) {
2107 ASSERT(db
->db_blkptr
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
2108 mutex_enter(&dn
->dn_mtx
);
2109 list_remove(&dn
->dn_dirty_records
[txg
& TXG_MASK
], dr
);
2110 mutex_exit(&dn
->dn_mtx
);
2114 if (db
->db_state
!= DB_NOFILL
) {
2115 dbuf_unoverride(dr
);
2117 ASSERT(db
->db_buf
!= NULL
);
2118 ASSERT(dr
->dt
.dl
.dr_data
!= NULL
);
2119 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
2120 arc_buf_destroy(dr
->dt
.dl
.dr_data
, db
);
2123 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
2125 ASSERT(db
->db_dirtycnt
> 0);
2126 db
->db_dirtycnt
-= 1;
2128 if (refcount_remove(&db
->db_holds
, (void *)(uintptr_t)txg
) == 0) {
2129 ASSERT(db
->db_state
== DB_NOFILL
|| arc_released(db
->db_buf
));
2138 dmu_buf_will_dirty_impl(dmu_buf_t
*db_fake
, int flags
, dmu_tx_t
*tx
)
2140 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2142 ASSERT(tx
->tx_txg
!= 0);
2143 ASSERT(!refcount_is_zero(&db
->db_holds
));
2146 * Quick check for dirtyness. For already dirty blocks, this
2147 * reduces runtime of this function by >90%, and overall performance
2148 * by 50% for some workloads (e.g. file deletion with indirect blocks
2151 mutex_enter(&db
->db_mtx
);
2153 dbuf_dirty_record_t
*dr
;
2154 for (dr
= db
->db_last_dirty
;
2155 dr
!= NULL
&& dr
->dr_txg
>= tx
->tx_txg
; dr
= dr
->dr_next
) {
2157 * It's possible that it is already dirty but not cached,
2158 * because there are some calls to dbuf_dirty() that don't
2159 * go through dmu_buf_will_dirty().
2161 if (dr
->dr_txg
== tx
->tx_txg
&& db
->db_state
== DB_CACHED
) {
2162 /* This dbuf is already dirty and cached. */
2164 mutex_exit(&db
->db_mtx
);
2168 mutex_exit(&db
->db_mtx
);
2171 if (RW_WRITE_HELD(&DB_DNODE(db
)->dn_struct_rwlock
))
2172 flags
|= DB_RF_HAVESTRUCT
;
2174 (void) dbuf_read(db
, NULL
, flags
);
2175 (void) dbuf_dirty(db
, tx
);
2179 dmu_buf_will_dirty(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
2181 dmu_buf_will_dirty_impl(db_fake
,
2182 DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
, tx
);
2186 dmu_buf_will_not_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
2188 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2190 db
->db_state
= DB_NOFILL
;
2192 dmu_buf_will_fill(db_fake
, tx
);
2196 dmu_buf_will_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
2198 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2200 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2201 ASSERT(tx
->tx_txg
!= 0);
2202 ASSERT(db
->db_level
== 0);
2203 ASSERT(!refcount_is_zero(&db
->db_holds
));
2205 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
||
2206 dmu_tx_private_ok(tx
));
2209 (void) dbuf_dirty(db
, tx
);
2213 * This function is effectively the same as dmu_buf_will_dirty(), but
2214 * indicates the caller expects raw encrypted data in the db. It will
2215 * also set the raw flag on the created dirty record.
2218 dmu_buf_will_change_crypt_params(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
2220 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2221 dbuf_dirty_record_t
*dr
;
2223 dmu_buf_will_dirty_impl(db_fake
,
2224 DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
| DB_RF_NO_DECRYPT
, tx
);
2226 dr
= db
->db_last_dirty
;
2227 while (dr
!= NULL
&& dr
->dr_txg
> tx
->tx_txg
)
2230 ASSERT3P(dr
, !=, NULL
);
2231 ASSERT3U(dr
->dr_txg
, ==, tx
->tx_txg
);
2232 dr
->dt
.dl
.dr_raw
= B_TRUE
;
2233 db
->db_objset
->os_next_write_raw
[tx
->tx_txg
& TXG_MASK
] = B_TRUE
;
2236 #pragma weak dmu_buf_fill_done = dbuf_fill_done
2239 dbuf_fill_done(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
2241 mutex_enter(&db
->db_mtx
);
2244 if (db
->db_state
== DB_FILL
) {
2245 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
2246 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2247 /* we were freed while filling */
2248 /* XXX dbuf_undirty? */
2249 bzero(db
->db
.db_data
, db
->db
.db_size
);
2250 db
->db_freed_in_flight
= FALSE
;
2252 db
->db_state
= DB_CACHED
;
2253 cv_broadcast(&db
->db_changed
);
2255 mutex_exit(&db
->db_mtx
);
2259 dmu_buf_write_embedded(dmu_buf_t
*dbuf
, void *data
,
2260 bp_embedded_type_t etype
, enum zio_compress comp
,
2261 int uncompressed_size
, int compressed_size
, int byteorder
,
2264 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbuf
;
2265 struct dirty_leaf
*dl
;
2266 dmu_object_type_t type
;
2268 if (etype
== BP_EMBEDDED_TYPE_DATA
) {
2269 ASSERT(spa_feature_is_active(dmu_objset_spa(db
->db_objset
),
2270 SPA_FEATURE_EMBEDDED_DATA
));
2274 type
= DB_DNODE(db
)->dn_type
;
2277 ASSERT0(db
->db_level
);
2278 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2280 dmu_buf_will_not_fill(dbuf
, tx
);
2282 ASSERT3U(db
->db_last_dirty
->dr_txg
, ==, tx
->tx_txg
);
2283 dl
= &db
->db_last_dirty
->dt
.dl
;
2284 encode_embedded_bp_compressed(&dl
->dr_overridden_by
,
2285 data
, comp
, uncompressed_size
, compressed_size
);
2286 BPE_SET_ETYPE(&dl
->dr_overridden_by
, etype
);
2287 BP_SET_TYPE(&dl
->dr_overridden_by
, type
);
2288 BP_SET_LEVEL(&dl
->dr_overridden_by
, 0);
2289 BP_SET_BYTEORDER(&dl
->dr_overridden_by
, byteorder
);
2291 dl
->dr_override_state
= DR_OVERRIDDEN
;
2292 dl
->dr_overridden_by
.blk_birth
= db
->db_last_dirty
->dr_txg
;
2296 * Directly assign a provided arc buf to a given dbuf if it's not referenced
2297 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
2300 dbuf_assign_arcbuf(dmu_buf_impl_t
*db
, arc_buf_t
*buf
, dmu_tx_t
*tx
)
2302 ASSERT(!refcount_is_zero(&db
->db_holds
));
2303 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2304 ASSERT(db
->db_level
== 0);
2305 ASSERT3U(dbuf_is_metadata(db
), ==, arc_is_metadata(buf
));
2306 ASSERT(buf
!= NULL
);
2307 ASSERT(arc_buf_lsize(buf
) == db
->db
.db_size
);
2308 ASSERT(tx
->tx_txg
!= 0);
2310 arc_return_buf(buf
, db
);
2311 ASSERT(arc_released(buf
));
2313 mutex_enter(&db
->db_mtx
);
2315 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
2316 cv_wait(&db
->db_changed
, &db
->db_mtx
);
2318 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_UNCACHED
);
2320 if (db
->db_state
== DB_CACHED
&&
2321 refcount_count(&db
->db_holds
) - 1 > db
->db_dirtycnt
) {
2323 * In practice, we will never have a case where we have an
2324 * encrypted arc buffer while additional holds exist on the
2325 * dbuf. We don't handle this here so we simply assert that
2328 ASSERT(!arc_is_encrypted(buf
));
2329 mutex_exit(&db
->db_mtx
);
2330 (void) dbuf_dirty(db
, tx
);
2331 bcopy(buf
->b_data
, db
->db
.db_data
, db
->db
.db_size
);
2332 arc_buf_destroy(buf
, db
);
2333 xuio_stat_wbuf_copied();
2337 xuio_stat_wbuf_nocopy();
2338 if (db
->db_state
== DB_CACHED
) {
2339 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
2341 ASSERT(db
->db_buf
!= NULL
);
2342 if (dr
!= NULL
&& dr
->dr_txg
== tx
->tx_txg
) {
2343 ASSERT(dr
->dt
.dl
.dr_data
== db
->db_buf
);
2344 IMPLY(arc_is_encrypted(buf
), dr
->dt
.dl
.dr_raw
);
2346 if (!arc_released(db
->db_buf
)) {
2347 ASSERT(dr
->dt
.dl
.dr_override_state
==
2349 arc_release(db
->db_buf
, db
);
2351 dr
->dt
.dl
.dr_data
= buf
;
2352 arc_buf_destroy(db
->db_buf
, db
);
2353 } else if (dr
== NULL
|| dr
->dt
.dl
.dr_data
!= db
->db_buf
) {
2354 arc_release(db
->db_buf
, db
);
2355 arc_buf_destroy(db
->db_buf
, db
);
2359 ASSERT(db
->db_buf
== NULL
);
2360 dbuf_set_data(db
, buf
);
2361 db
->db_state
= DB_FILL
;
2362 mutex_exit(&db
->db_mtx
);
2363 (void) dbuf_dirty(db
, tx
);
2364 dmu_buf_fill_done(&db
->db
, tx
);
2368 dbuf_destroy(dmu_buf_impl_t
*db
)
2371 dmu_buf_impl_t
*parent
= db
->db_parent
;
2372 dmu_buf_impl_t
*dndb
;
2374 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2375 ASSERT(refcount_is_zero(&db
->db_holds
));
2377 if (db
->db_buf
!= NULL
) {
2378 arc_buf_destroy(db
->db_buf
, db
);
2382 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2383 int slots
= DB_DNODE(db
)->dn_num_slots
;
2384 int bonuslen
= DN_SLOTS_TO_BONUSLEN(slots
);
2385 if (db
->db
.db_data
!= NULL
) {
2386 kmem_free(db
->db
.db_data
, bonuslen
);
2387 arc_space_return(bonuslen
, ARC_SPACE_BONUS
);
2388 db
->db_state
= DB_UNCACHED
;
2392 dbuf_clear_data(db
);
2394 if (multilist_link_active(&db
->db_cache_link
)) {
2395 multilist_remove(dbuf_cache
, db
);
2396 (void) refcount_remove_many(&dbuf_cache_size
,
2397 db
->db
.db_size
, db
);
2398 DBUF_STAT_BUMPDOWN(cache_levels
[db
->db_level
]);
2399 DBUF_STAT_BUMPDOWN(cache_count
);
2400 DBUF_STAT_DECR(cache_levels_bytes
[db
->db_level
],
2404 ASSERT(db
->db_state
== DB_UNCACHED
|| db
->db_state
== DB_NOFILL
);
2405 ASSERT(db
->db_data_pending
== NULL
);
2407 db
->db_state
= DB_EVICTING
;
2408 db
->db_blkptr
= NULL
;
2411 * Now that db_state is DB_EVICTING, nobody else can find this via
2412 * the hash table. We can now drop db_mtx, which allows us to
2413 * acquire the dn_dbufs_mtx.
2415 mutex_exit(&db
->db_mtx
);
2420 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
2421 boolean_t needlock
= !MUTEX_HELD(&dn
->dn_dbufs_mtx
);
2423 mutex_enter(&dn
->dn_dbufs_mtx
);
2424 avl_remove(&dn
->dn_dbufs
, db
);
2425 atomic_dec_32(&dn
->dn_dbufs_count
);
2429 mutex_exit(&dn
->dn_dbufs_mtx
);
2431 * Decrementing the dbuf count means that the hold corresponding
2432 * to the removed dbuf is no longer discounted in dnode_move(),
2433 * so the dnode cannot be moved until after we release the hold.
2434 * The membar_producer() ensures visibility of the decremented
2435 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
2439 db
->db_dnode_handle
= NULL
;
2441 dbuf_hash_remove(db
);
2446 ASSERT(refcount_is_zero(&db
->db_holds
));
2448 db
->db_parent
= NULL
;
2450 ASSERT(db
->db_buf
== NULL
);
2451 ASSERT(db
->db
.db_data
== NULL
);
2452 ASSERT(db
->db_hash_next
== NULL
);
2453 ASSERT(db
->db_blkptr
== NULL
);
2454 ASSERT(db
->db_data_pending
== NULL
);
2455 ASSERT(!multilist_link_active(&db
->db_cache_link
));
2457 kmem_cache_free(dbuf_kmem_cache
, db
);
2458 arc_space_return(sizeof (dmu_buf_impl_t
), ARC_SPACE_DBUF
);
2461 * If this dbuf is referenced from an indirect dbuf,
2462 * decrement the ref count on the indirect dbuf.
2464 if (parent
&& parent
!= dndb
)
2465 dbuf_rele(parent
, db
);
2469 * Note: While bpp will always be updated if the function returns success,
2470 * parentp will not be updated if the dnode does not have dn_dbuf filled in;
2471 * this happens when the dnode is the meta-dnode, or {user|group|project}used
2474 __attribute__((always_inline
))
2476 dbuf_findbp(dnode_t
*dn
, int level
, uint64_t blkid
, int fail_sparse
,
2477 dmu_buf_impl_t
**parentp
, blkptr_t
**bpp
, struct dbuf_hold_impl_data
*dh
)
2482 ASSERT(blkid
!= DMU_BONUS_BLKID
);
2484 if (blkid
== DMU_SPILL_BLKID
) {
2485 mutex_enter(&dn
->dn_mtx
);
2486 if (dn
->dn_have_spill
&&
2487 (dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
))
2488 *bpp
= DN_SPILL_BLKPTR(dn
->dn_phys
);
2491 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
2492 *parentp
= dn
->dn_dbuf
;
2493 mutex_exit(&dn
->dn_mtx
);
2498 (dn
->dn_phys
->dn_nlevels
== 0) ? 1 : dn
->dn_phys
->dn_nlevels
;
2499 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2501 ASSERT3U(level
* epbs
, <, 64);
2502 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2504 * This assertion shouldn't trip as long as the max indirect block size
2505 * is less than 1M. The reason for this is that up to that point,
2506 * the number of levels required to address an entire object with blocks
2507 * of size SPA_MINBLOCKSIZE satisfies nlevels * epbs + 1 <= 64. In
2508 * other words, if N * epbs + 1 > 64, then if (N-1) * epbs + 1 > 55
2509 * (i.e. we can address the entire object), objects will all use at most
2510 * N-1 levels and the assertion won't overflow. However, once epbs is
2511 * 13, 4 * 13 + 1 = 53, but 5 * 13 + 1 = 66. Then, 4 levels will not be
2512 * enough to address an entire object, so objects will have 5 levels,
2513 * but then this assertion will overflow.
2515 * All this is to say that if we ever increase DN_MAX_INDBLKSHIFT, we
2516 * need to redo this logic to handle overflows.
2518 ASSERT(level
>= nlevels
||
2519 ((nlevels
- level
- 1) * epbs
) +
2520 highbit64(dn
->dn_phys
->dn_nblkptr
) <= 64);
2521 if (level
>= nlevels
||
2522 blkid
>= ((uint64_t)dn
->dn_phys
->dn_nblkptr
<<
2523 ((nlevels
- level
- 1) * epbs
)) ||
2525 blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))) {
2526 /* the buffer has no parent yet */
2527 return (SET_ERROR(ENOENT
));
2528 } else if (level
< nlevels
-1) {
2529 /* this block is referenced from an indirect block */
2532 err
= dbuf_hold_impl(dn
, level
+1,
2533 blkid
>> epbs
, fail_sparse
, FALSE
, NULL
, parentp
);
2535 __dbuf_hold_impl_init(dh
+ 1, dn
, dh
->dh_level
+ 1,
2536 blkid
>> epbs
, fail_sparse
, FALSE
, NULL
,
2537 parentp
, dh
->dh_depth
+ 1);
2538 err
= __dbuf_hold_impl(dh
+ 1);
2542 err
= dbuf_read(*parentp
, NULL
,
2543 (DB_RF_HAVESTRUCT
| DB_RF_NOPREFETCH
| DB_RF_CANFAIL
));
2545 dbuf_rele(*parentp
, NULL
);
2549 *bpp
= ((blkptr_t
*)(*parentp
)->db
.db_data
) +
2550 (blkid
& ((1ULL << epbs
) - 1));
2551 if (blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))
2552 ASSERT(BP_IS_HOLE(*bpp
));
2555 /* the block is referenced from the dnode */
2556 ASSERT3U(level
, ==, nlevels
-1);
2557 ASSERT(dn
->dn_phys
->dn_nblkptr
== 0 ||
2558 blkid
< dn
->dn_phys
->dn_nblkptr
);
2560 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
2561 *parentp
= dn
->dn_dbuf
;
2563 *bpp
= &dn
->dn_phys
->dn_blkptr
[blkid
];
2568 static dmu_buf_impl_t
*
2569 dbuf_create(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
2570 dmu_buf_impl_t
*parent
, blkptr_t
*blkptr
)
2572 objset_t
*os
= dn
->dn_objset
;
2573 dmu_buf_impl_t
*db
, *odb
;
2575 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2576 ASSERT(dn
->dn_type
!= DMU_OT_NONE
);
2578 db
= kmem_cache_alloc(dbuf_kmem_cache
, KM_SLEEP
);
2581 db
->db
.db_object
= dn
->dn_object
;
2582 db
->db_level
= level
;
2583 db
->db_blkid
= blkid
;
2584 db
->db_last_dirty
= NULL
;
2585 db
->db_dirtycnt
= 0;
2586 db
->db_dnode_handle
= dn
->dn_handle
;
2587 db
->db_parent
= parent
;
2588 db
->db_blkptr
= blkptr
;
2591 db
->db_user_immediate_evict
= FALSE
;
2592 db
->db_freed_in_flight
= FALSE
;
2593 db
->db_pending_evict
= FALSE
;
2595 if (blkid
== DMU_BONUS_BLKID
) {
2596 ASSERT3P(parent
, ==, dn
->dn_dbuf
);
2597 db
->db
.db_size
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
) -
2598 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
);
2599 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
2600 db
->db
.db_offset
= DMU_BONUS_BLKID
;
2601 db
->db_state
= DB_UNCACHED
;
2602 /* the bonus dbuf is not placed in the hash table */
2603 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_DBUF
);
2605 } else if (blkid
== DMU_SPILL_BLKID
) {
2606 db
->db
.db_size
= (blkptr
!= NULL
) ?
2607 BP_GET_LSIZE(blkptr
) : SPA_MINBLOCKSIZE
;
2608 db
->db
.db_offset
= 0;
2611 db
->db_level
? 1 << dn
->dn_indblkshift
: dn
->dn_datablksz
;
2612 db
->db
.db_size
= blocksize
;
2613 db
->db
.db_offset
= db
->db_blkid
* blocksize
;
2617 * Hold the dn_dbufs_mtx while we get the new dbuf
2618 * in the hash table *and* added to the dbufs list.
2619 * This prevents a possible deadlock with someone
2620 * trying to look up this dbuf before its added to the
2623 mutex_enter(&dn
->dn_dbufs_mtx
);
2624 db
->db_state
= DB_EVICTING
;
2625 if ((odb
= dbuf_hash_insert(db
)) != NULL
) {
2626 /* someone else inserted it first */
2627 kmem_cache_free(dbuf_kmem_cache
, db
);
2628 mutex_exit(&dn
->dn_dbufs_mtx
);
2629 DBUF_STAT_BUMP(hash_insert_race
);
2632 avl_add(&dn
->dn_dbufs
, db
);
2634 db
->db_state
= DB_UNCACHED
;
2635 mutex_exit(&dn
->dn_dbufs_mtx
);
2636 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_DBUF
);
2638 if (parent
&& parent
!= dn
->dn_dbuf
)
2639 dbuf_add_ref(parent
, db
);
2641 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
2642 refcount_count(&dn
->dn_holds
) > 0);
2643 (void) refcount_add(&dn
->dn_holds
, db
);
2644 atomic_inc_32(&dn
->dn_dbufs_count
);
2646 dprintf_dbuf(db
, "db=%p\n", db
);
2651 typedef struct dbuf_prefetch_arg
{
2652 spa_t
*dpa_spa
; /* The spa to issue the prefetch in. */
2653 zbookmark_phys_t dpa_zb
; /* The target block to prefetch. */
2654 int dpa_epbs
; /* Entries (blkptr_t's) Per Block Shift. */
2655 int dpa_curlevel
; /* The current level that we're reading */
2656 dnode_t
*dpa_dnode
; /* The dnode associated with the prefetch */
2657 zio_priority_t dpa_prio
; /* The priority I/Os should be issued at. */
2658 zio_t
*dpa_zio
; /* The parent zio_t for all prefetches. */
2659 arc_flags_t dpa_aflags
; /* Flags to pass to the final prefetch. */
2660 } dbuf_prefetch_arg_t
;
2663 * Actually issue the prefetch read for the block given.
2666 dbuf_issue_final_prefetch(dbuf_prefetch_arg_t
*dpa
, blkptr_t
*bp
)
2668 if (BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
))
2671 int zio_flags
= ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
;
2672 arc_flags_t aflags
=
2673 dpa
->dpa_aflags
| ARC_FLAG_NOWAIT
| ARC_FLAG_PREFETCH
;
2675 /* dnodes are always read as raw and then converted later */
2676 if (BP_GET_TYPE(bp
) == DMU_OT_DNODE
&& BP_IS_PROTECTED(bp
) &&
2677 dpa
->dpa_curlevel
== 0)
2678 zio_flags
|= ZIO_FLAG_RAW
;
2680 ASSERT3U(dpa
->dpa_curlevel
, ==, BP_GET_LEVEL(bp
));
2681 ASSERT3U(dpa
->dpa_curlevel
, ==, dpa
->dpa_zb
.zb_level
);
2682 ASSERT(dpa
->dpa_zio
!= NULL
);
2683 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
, bp
, NULL
, NULL
,
2684 dpa
->dpa_prio
, zio_flags
, &aflags
, &dpa
->dpa_zb
);
2688 * Called when an indirect block above our prefetch target is read in. This
2689 * will either read in the next indirect block down the tree or issue the actual
2690 * prefetch if the next block down is our target.
2693 dbuf_prefetch_indirect_done(zio_t
*zio
, const zbookmark_phys_t
*zb
,
2694 const blkptr_t
*iobp
, arc_buf_t
*abuf
, void *private)
2696 dbuf_prefetch_arg_t
*dpa
= private;
2698 ASSERT3S(dpa
->dpa_zb
.zb_level
, <, dpa
->dpa_curlevel
);
2699 ASSERT3S(dpa
->dpa_curlevel
, >, 0);
2702 * The dpa_dnode is only valid if we are called with a NULL
2703 * zio. This indicates that the arc_read() returned without
2704 * first calling zio_read() to issue a physical read. Once
2705 * a physical read is made the dpa_dnode must be invalidated
2706 * as the locks guarding it may have been dropped. If the
2707 * dpa_dnode is still valid, then we want to add it to the dbuf
2708 * cache. To do so, we must hold the dbuf associated with the block
2709 * we just prefetched, read its contents so that we associate it
2710 * with an arc_buf_t, and then release it.
2713 ASSERT3S(BP_GET_LEVEL(zio
->io_bp
), ==, dpa
->dpa_curlevel
);
2714 if (zio
->io_flags
& ZIO_FLAG_RAW_COMPRESS
) {
2715 ASSERT3U(BP_GET_PSIZE(zio
->io_bp
), ==, zio
->io_size
);
2717 ASSERT3U(BP_GET_LSIZE(zio
->io_bp
), ==, zio
->io_size
);
2719 ASSERT3P(zio
->io_spa
, ==, dpa
->dpa_spa
);
2721 dpa
->dpa_dnode
= NULL
;
2722 } else if (dpa
->dpa_dnode
!= NULL
) {
2723 uint64_t curblkid
= dpa
->dpa_zb
.zb_blkid
>>
2724 (dpa
->dpa_epbs
* (dpa
->dpa_curlevel
-
2725 dpa
->dpa_zb
.zb_level
));
2726 dmu_buf_impl_t
*db
= dbuf_hold_level(dpa
->dpa_dnode
,
2727 dpa
->dpa_curlevel
, curblkid
, FTAG
);
2728 (void) dbuf_read(db
, NULL
,
2729 DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
| DB_RF_HAVESTRUCT
);
2730 dbuf_rele(db
, FTAG
);
2734 kmem_free(dpa
, sizeof (*dpa
));
2738 dpa
->dpa_curlevel
--;
2739 uint64_t nextblkid
= dpa
->dpa_zb
.zb_blkid
>>
2740 (dpa
->dpa_epbs
* (dpa
->dpa_curlevel
- dpa
->dpa_zb
.zb_level
));
2741 blkptr_t
*bp
= ((blkptr_t
*)abuf
->b_data
) +
2742 P2PHASE(nextblkid
, 1ULL << dpa
->dpa_epbs
);
2744 if (BP_IS_HOLE(bp
)) {
2745 kmem_free(dpa
, sizeof (*dpa
));
2746 } else if (dpa
->dpa_curlevel
== dpa
->dpa_zb
.zb_level
) {
2747 ASSERT3U(nextblkid
, ==, dpa
->dpa_zb
.zb_blkid
);
2748 dbuf_issue_final_prefetch(dpa
, bp
);
2749 kmem_free(dpa
, sizeof (*dpa
));
2751 arc_flags_t iter_aflags
= ARC_FLAG_NOWAIT
;
2752 zbookmark_phys_t zb
;
2754 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
2755 if (dpa
->dpa_aflags
& ARC_FLAG_L2CACHE
)
2756 iter_aflags
|= ARC_FLAG_L2CACHE
;
2758 ASSERT3U(dpa
->dpa_curlevel
, ==, BP_GET_LEVEL(bp
));
2760 SET_BOOKMARK(&zb
, dpa
->dpa_zb
.zb_objset
,
2761 dpa
->dpa_zb
.zb_object
, dpa
->dpa_curlevel
, nextblkid
);
2763 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
,
2764 bp
, dbuf_prefetch_indirect_done
, dpa
, dpa
->dpa_prio
,
2765 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
2769 arc_buf_destroy(abuf
, private);
2773 * Issue prefetch reads for the given block on the given level. If the indirect
2774 * blocks above that block are not in memory, we will read them in
2775 * asynchronously. As a result, this call never blocks waiting for a read to
2776 * complete. Note that the prefetch might fail if the dataset is encrypted and
2777 * the encryption key is unmapped before the IO completes.
2780 dbuf_prefetch(dnode_t
*dn
, int64_t level
, uint64_t blkid
, zio_priority_t prio
,
2784 int epbs
, nlevels
, curlevel
;
2787 ASSERT(blkid
!= DMU_BONUS_BLKID
);
2788 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2790 if (blkid
> dn
->dn_maxblkid
)
2793 if (dnode_block_freed(dn
, blkid
))
2797 * This dnode hasn't been written to disk yet, so there's nothing to
2800 nlevels
= dn
->dn_phys
->dn_nlevels
;
2801 if (level
>= nlevels
|| dn
->dn_phys
->dn_nblkptr
== 0)
2804 epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2805 if (dn
->dn_phys
->dn_maxblkid
< blkid
<< (epbs
* level
))
2808 dmu_buf_impl_t
*db
= dbuf_find(dn
->dn_objset
, dn
->dn_object
,
2811 mutex_exit(&db
->db_mtx
);
2813 * This dbuf already exists. It is either CACHED, or
2814 * (we assume) about to be read or filled.
2820 * Find the closest ancestor (indirect block) of the target block
2821 * that is present in the cache. In this indirect block, we will
2822 * find the bp that is at curlevel, curblkid.
2826 while (curlevel
< nlevels
- 1) {
2827 int parent_level
= curlevel
+ 1;
2828 uint64_t parent_blkid
= curblkid
>> epbs
;
2831 if (dbuf_hold_impl(dn
, parent_level
, parent_blkid
,
2832 FALSE
, TRUE
, FTAG
, &db
) == 0) {
2833 blkptr_t
*bpp
= db
->db_buf
->b_data
;
2834 bp
= bpp
[P2PHASE(curblkid
, 1 << epbs
)];
2835 dbuf_rele(db
, FTAG
);
2839 curlevel
= parent_level
;
2840 curblkid
= parent_blkid
;
2843 if (curlevel
== nlevels
- 1) {
2844 /* No cached indirect blocks found. */
2845 ASSERT3U(curblkid
, <, dn
->dn_phys
->dn_nblkptr
);
2846 bp
= dn
->dn_phys
->dn_blkptr
[curblkid
];
2848 if (BP_IS_HOLE(&bp
))
2851 ASSERT3U(curlevel
, ==, BP_GET_LEVEL(&bp
));
2853 zio_t
*pio
= zio_root(dmu_objset_spa(dn
->dn_objset
), NULL
, NULL
,
2856 dbuf_prefetch_arg_t
*dpa
= kmem_zalloc(sizeof (*dpa
), KM_SLEEP
);
2857 dsl_dataset_t
*ds
= dn
->dn_objset
->os_dsl_dataset
;
2858 SET_BOOKMARK(&dpa
->dpa_zb
, ds
!= NULL
? ds
->ds_object
: DMU_META_OBJSET
,
2859 dn
->dn_object
, level
, blkid
);
2860 dpa
->dpa_curlevel
= curlevel
;
2861 dpa
->dpa_prio
= prio
;
2862 dpa
->dpa_aflags
= aflags
;
2863 dpa
->dpa_spa
= dn
->dn_objset
->os_spa
;
2864 dpa
->dpa_dnode
= dn
;
2865 dpa
->dpa_epbs
= epbs
;
2868 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
2869 if (DNODE_LEVEL_IS_L2CACHEABLE(dn
, level
))
2870 dpa
->dpa_aflags
|= ARC_FLAG_L2CACHE
;
2873 * If we have the indirect just above us, no need to do the asynchronous
2874 * prefetch chain; we'll just run the last step ourselves. If we're at
2875 * a higher level, though, we want to issue the prefetches for all the
2876 * indirect blocks asynchronously, so we can go on with whatever we were
2879 if (curlevel
== level
) {
2880 ASSERT3U(curblkid
, ==, blkid
);
2881 dbuf_issue_final_prefetch(dpa
, &bp
);
2882 kmem_free(dpa
, sizeof (*dpa
));
2884 arc_flags_t iter_aflags
= ARC_FLAG_NOWAIT
;
2885 zbookmark_phys_t zb
;
2887 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
2888 if (DNODE_LEVEL_IS_L2CACHEABLE(dn
, level
))
2889 iter_aflags
|= ARC_FLAG_L2CACHE
;
2891 SET_BOOKMARK(&zb
, ds
!= NULL
? ds
->ds_object
: DMU_META_OBJSET
,
2892 dn
->dn_object
, curlevel
, curblkid
);
2893 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
,
2894 &bp
, dbuf_prefetch_indirect_done
, dpa
, prio
,
2895 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
2899 * We use pio here instead of dpa_zio since it's possible that
2900 * dpa may have already been freed.
2905 #define DBUF_HOLD_IMPL_MAX_DEPTH 20
2908 * Helper function for __dbuf_hold_impl() to copy a buffer. Handles
2909 * the case of encrypted, compressed and uncompressed buffers by
2910 * allocating the new buffer, respectively, with arc_alloc_raw_buf(),
2911 * arc_alloc_compressed_buf() or arc_alloc_buf().*
2913 * NOTE: Declared noinline to avoid stack bloat in __dbuf_hold_impl().
2915 noinline
static void
2916 dbuf_hold_copy(struct dbuf_hold_impl_data
*dh
)
2918 dnode_t
*dn
= dh
->dh_dn
;
2919 dmu_buf_impl_t
*db
= dh
->dh_db
;
2920 dbuf_dirty_record_t
*dr
= dh
->dh_dr
;
2921 arc_buf_t
*data
= dr
->dt
.dl
.dr_data
;
2923 enum zio_compress compress_type
= arc_get_compression(data
);
2925 if (arc_is_encrypted(data
)) {
2926 boolean_t byteorder
;
2927 uint8_t salt
[ZIO_DATA_SALT_LEN
];
2928 uint8_t iv
[ZIO_DATA_IV_LEN
];
2929 uint8_t mac
[ZIO_DATA_MAC_LEN
];
2931 arc_get_raw_params(data
, &byteorder
, salt
, iv
, mac
);
2932 dbuf_set_data(db
, arc_alloc_raw_buf(dn
->dn_objset
->os_spa
, db
,
2933 dmu_objset_id(dn
->dn_objset
), byteorder
, salt
, iv
, mac
,
2934 dn
->dn_type
, arc_buf_size(data
), arc_buf_lsize(data
),
2936 } else if (compress_type
!= ZIO_COMPRESS_OFF
) {
2937 dbuf_set_data(db
, arc_alloc_compressed_buf(
2938 dn
->dn_objset
->os_spa
, db
, arc_buf_size(data
),
2939 arc_buf_lsize(data
), compress_type
));
2941 dbuf_set_data(db
, arc_alloc_buf(dn
->dn_objset
->os_spa
, db
,
2942 DBUF_GET_BUFC_TYPE(db
), db
->db
.db_size
));
2945 bcopy(data
->b_data
, db
->db
.db_data
, arc_buf_size(data
));
2949 * Returns with db_holds incremented, and db_mtx not held.
2950 * Note: dn_struct_rwlock must be held.
2953 __dbuf_hold_impl(struct dbuf_hold_impl_data
*dh
)
2955 ASSERT3S(dh
->dh_depth
, <, DBUF_HOLD_IMPL_MAX_DEPTH
);
2956 dh
->dh_parent
= NULL
;
2958 ASSERT(dh
->dh_blkid
!= DMU_BONUS_BLKID
);
2959 ASSERT(RW_LOCK_HELD(&dh
->dh_dn
->dn_struct_rwlock
));
2960 ASSERT3U(dh
->dh_dn
->dn_nlevels
, >, dh
->dh_level
);
2962 *(dh
->dh_dbp
) = NULL
;
2964 /* dbuf_find() returns with db_mtx held */
2965 dh
->dh_db
= dbuf_find(dh
->dh_dn
->dn_objset
, dh
->dh_dn
->dn_object
,
2966 dh
->dh_level
, dh
->dh_blkid
);
2968 if (dh
->dh_db
== NULL
) {
2971 if (dh
->dh_fail_uncached
)
2972 return (SET_ERROR(ENOENT
));
2974 ASSERT3P(dh
->dh_parent
, ==, NULL
);
2975 dh
->dh_err
= dbuf_findbp(dh
->dh_dn
, dh
->dh_level
, dh
->dh_blkid
,
2976 dh
->dh_fail_sparse
, &dh
->dh_parent
, &dh
->dh_bp
, dh
);
2977 if (dh
->dh_fail_sparse
) {
2978 if (dh
->dh_err
== 0 &&
2979 dh
->dh_bp
&& BP_IS_HOLE(dh
->dh_bp
))
2980 dh
->dh_err
= SET_ERROR(ENOENT
);
2983 dbuf_rele(dh
->dh_parent
, NULL
);
2984 return (dh
->dh_err
);
2987 if (dh
->dh_err
&& dh
->dh_err
!= ENOENT
)
2988 return (dh
->dh_err
);
2989 dh
->dh_db
= dbuf_create(dh
->dh_dn
, dh
->dh_level
, dh
->dh_blkid
,
2990 dh
->dh_parent
, dh
->dh_bp
);
2993 if (dh
->dh_fail_uncached
&& dh
->dh_db
->db_state
!= DB_CACHED
) {
2994 mutex_exit(&dh
->dh_db
->db_mtx
);
2995 return (SET_ERROR(ENOENT
));
2998 if (dh
->dh_db
->db_buf
!= NULL
) {
2999 arc_buf_access(dh
->dh_db
->db_buf
);
3000 ASSERT3P(dh
->dh_db
->db
.db_data
, ==, dh
->dh_db
->db_buf
->b_data
);
3003 ASSERT(dh
->dh_db
->db_buf
== NULL
|| arc_referenced(dh
->dh_db
->db_buf
));
3006 * If this buffer is currently syncing out, and we are are
3007 * still referencing it from db_data, we need to make a copy
3008 * of it in case we decide we want to dirty it again in this txg.
3010 if (dh
->dh_db
->db_level
== 0 &&
3011 dh
->dh_db
->db_blkid
!= DMU_BONUS_BLKID
&&
3012 dh
->dh_dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
3013 dh
->dh_db
->db_state
== DB_CACHED
&& dh
->dh_db
->db_data_pending
) {
3014 dh
->dh_dr
= dh
->dh_db
->db_data_pending
;
3015 if (dh
->dh_dr
->dt
.dl
.dr_data
== dh
->dh_db
->db_buf
)
3019 if (multilist_link_active(&dh
->dh_db
->db_cache_link
)) {
3020 ASSERT(refcount_is_zero(&dh
->dh_db
->db_holds
));
3021 multilist_remove(dbuf_cache
, dh
->dh_db
);
3022 (void) refcount_remove_many(&dbuf_cache_size
,
3023 dh
->dh_db
->db
.db_size
, dh
->dh_db
);
3024 DBUF_STAT_BUMPDOWN(cache_levels
[dh
->dh_db
->db_level
]);
3025 DBUF_STAT_BUMPDOWN(cache_count
);
3026 DBUF_STAT_DECR(cache_levels_bytes
[dh
->dh_db
->db_level
],
3027 dh
->dh_db
->db
.db_size
);
3029 (void) refcount_add(&dh
->dh_db
->db_holds
, dh
->dh_tag
);
3030 DBUF_VERIFY(dh
->dh_db
);
3031 mutex_exit(&dh
->dh_db
->db_mtx
);
3033 /* NOTE: we can't rele the parent until after we drop the db_mtx */
3035 dbuf_rele(dh
->dh_parent
, NULL
);
3037 ASSERT3P(DB_DNODE(dh
->dh_db
), ==, dh
->dh_dn
);
3038 ASSERT3U(dh
->dh_db
->db_blkid
, ==, dh
->dh_blkid
);
3039 ASSERT3U(dh
->dh_db
->db_level
, ==, dh
->dh_level
);
3040 *(dh
->dh_dbp
) = dh
->dh_db
;
3046 * The following code preserves the recursive function dbuf_hold_impl()
3047 * but moves the local variables AND function arguments to the heap to
3048 * minimize the stack frame size. Enough space is initially allocated
3049 * on the stack for 20 levels of recursion.
3052 dbuf_hold_impl(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
3053 boolean_t fail_sparse
, boolean_t fail_uncached
,
3054 void *tag
, dmu_buf_impl_t
**dbp
)
3056 struct dbuf_hold_impl_data
*dh
;
3059 dh
= kmem_alloc(sizeof (struct dbuf_hold_impl_data
) *
3060 DBUF_HOLD_IMPL_MAX_DEPTH
, KM_SLEEP
);
3061 __dbuf_hold_impl_init(dh
, dn
, level
, blkid
, fail_sparse
,
3062 fail_uncached
, tag
, dbp
, 0);
3064 error
= __dbuf_hold_impl(dh
);
3066 kmem_free(dh
, sizeof (struct dbuf_hold_impl_data
) *
3067 DBUF_HOLD_IMPL_MAX_DEPTH
);
3073 __dbuf_hold_impl_init(struct dbuf_hold_impl_data
*dh
,
3074 dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
3075 boolean_t fail_sparse
, boolean_t fail_uncached
,
3076 void *tag
, dmu_buf_impl_t
**dbp
, int depth
)
3079 dh
->dh_level
= level
;
3080 dh
->dh_blkid
= blkid
;
3082 dh
->dh_fail_sparse
= fail_sparse
;
3083 dh
->dh_fail_uncached
= fail_uncached
;
3089 dh
->dh_parent
= NULL
;
3094 dh
->dh_depth
= depth
;
3098 dbuf_hold(dnode_t
*dn
, uint64_t blkid
, void *tag
)
3100 return (dbuf_hold_level(dn
, 0, blkid
, tag
));
3104 dbuf_hold_level(dnode_t
*dn
, int level
, uint64_t blkid
, void *tag
)
3107 int err
= dbuf_hold_impl(dn
, level
, blkid
, FALSE
, FALSE
, tag
, &db
);
3108 return (err
? NULL
: db
);
3112 dbuf_create_bonus(dnode_t
*dn
)
3114 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
3116 ASSERT(dn
->dn_bonus
== NULL
);
3117 dn
->dn_bonus
= dbuf_create(dn
, 0, DMU_BONUS_BLKID
, dn
->dn_dbuf
, NULL
);
3121 dbuf_spill_set_blksz(dmu_buf_t
*db_fake
, uint64_t blksz
, dmu_tx_t
*tx
)
3123 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3126 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
3127 return (SET_ERROR(ENOTSUP
));
3129 blksz
= SPA_MINBLOCKSIZE
;
3130 ASSERT3U(blksz
, <=, spa_maxblocksize(dmu_objset_spa(db
->db_objset
)));
3131 blksz
= P2ROUNDUP(blksz
, SPA_MINBLOCKSIZE
);
3135 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
3136 dbuf_new_size(db
, blksz
, tx
);
3137 rw_exit(&dn
->dn_struct_rwlock
);
3144 dbuf_rm_spill(dnode_t
*dn
, dmu_tx_t
*tx
)
3146 dbuf_free_range(dn
, DMU_SPILL_BLKID
, DMU_SPILL_BLKID
, tx
);
3149 #pragma weak dmu_buf_add_ref = dbuf_add_ref
3151 dbuf_add_ref(dmu_buf_impl_t
*db
, void *tag
)
3153 int64_t holds
= refcount_add(&db
->db_holds
, tag
);
3154 VERIFY3S(holds
, >, 1);
3157 #pragma weak dmu_buf_try_add_ref = dbuf_try_add_ref
3159 dbuf_try_add_ref(dmu_buf_t
*db_fake
, objset_t
*os
, uint64_t obj
, uint64_t blkid
,
3162 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3163 dmu_buf_impl_t
*found_db
;
3164 boolean_t result
= B_FALSE
;
3166 if (blkid
== DMU_BONUS_BLKID
)
3167 found_db
= dbuf_find_bonus(os
, obj
);
3169 found_db
= dbuf_find(os
, obj
, 0, blkid
);
3171 if (found_db
!= NULL
) {
3172 if (db
== found_db
&& dbuf_refcount(db
) > db
->db_dirtycnt
) {
3173 (void) refcount_add(&db
->db_holds
, tag
);
3176 mutex_exit(&found_db
->db_mtx
);
3182 * If you call dbuf_rele() you had better not be referencing the dnode handle
3183 * unless you have some other direct or indirect hold on the dnode. (An indirect
3184 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
3185 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
3186 * dnode's parent dbuf evicting its dnode handles.
3189 dbuf_rele(dmu_buf_impl_t
*db
, void *tag
)
3191 mutex_enter(&db
->db_mtx
);
3192 dbuf_rele_and_unlock(db
, tag
);
3196 dmu_buf_rele(dmu_buf_t
*db
, void *tag
)
3198 dbuf_rele((dmu_buf_impl_t
*)db
, tag
);
3202 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
3203 * db_dirtycnt and db_holds to be updated atomically.
3206 dbuf_rele_and_unlock(dmu_buf_impl_t
*db
, void *tag
)
3210 ASSERT(MUTEX_HELD(&db
->db_mtx
));
3214 * Remove the reference to the dbuf before removing its hold on the
3215 * dnode so we can guarantee in dnode_move() that a referenced bonus
3216 * buffer has a corresponding dnode hold.
3218 holds
= refcount_remove(&db
->db_holds
, tag
);
3222 * We can't freeze indirects if there is a possibility that they
3223 * may be modified in the current syncing context.
3225 if (db
->db_buf
!= NULL
&&
3226 holds
== (db
->db_level
== 0 ? db
->db_dirtycnt
: 0)) {
3227 arc_buf_freeze(db
->db_buf
);
3230 if (holds
== db
->db_dirtycnt
&&
3231 db
->db_level
== 0 && db
->db_user_immediate_evict
)
3232 dbuf_evict_user(db
);
3235 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
3237 boolean_t evict_dbuf
= db
->db_pending_evict
;
3240 * If the dnode moves here, we cannot cross this
3241 * barrier until the move completes.
3246 atomic_dec_32(&dn
->dn_dbufs_count
);
3249 * Decrementing the dbuf count means that the bonus
3250 * buffer's dnode hold is no longer discounted in
3251 * dnode_move(). The dnode cannot move until after
3252 * the dnode_rele() below.
3257 * Do not reference db after its lock is dropped.
3258 * Another thread may evict it.
3260 mutex_exit(&db
->db_mtx
);
3263 dnode_evict_bonus(dn
);
3266 } else if (db
->db_buf
== NULL
) {
3268 * This is a special case: we never associated this
3269 * dbuf with any data allocated from the ARC.
3271 ASSERT(db
->db_state
== DB_UNCACHED
||
3272 db
->db_state
== DB_NOFILL
);
3274 } else if (arc_released(db
->db_buf
)) {
3276 * This dbuf has anonymous data associated with it.
3280 boolean_t do_arc_evict
= B_FALSE
;
3282 spa_t
*spa
= dmu_objset_spa(db
->db_objset
);
3284 if (!DBUF_IS_CACHEABLE(db
) &&
3285 db
->db_blkptr
!= NULL
&&
3286 !BP_IS_HOLE(db
->db_blkptr
) &&
3287 !BP_IS_EMBEDDED(db
->db_blkptr
)) {
3288 do_arc_evict
= B_TRUE
;
3289 bp
= *db
->db_blkptr
;
3292 if (!DBUF_IS_CACHEABLE(db
) ||
3293 db
->db_pending_evict
) {
3295 } else if (!multilist_link_active(&db
->db_cache_link
)) {
3296 multilist_insert(dbuf_cache
, db
);
3297 (void) refcount_add_many(&dbuf_cache_size
,
3298 db
->db
.db_size
, db
);
3299 DBUF_STAT_BUMP(cache_levels
[db
->db_level
]);
3300 DBUF_STAT_BUMP(cache_count
);
3301 DBUF_STAT_INCR(cache_levels_bytes
[db
->db_level
],
3303 DBUF_STAT_MAX(cache_size_bytes_max
,
3304 refcount_count(&dbuf_cache_size
));
3305 mutex_exit(&db
->db_mtx
);
3307 dbuf_evict_notify();
3311 arc_freed(spa
, &bp
);
3314 mutex_exit(&db
->db_mtx
);
3319 #pragma weak dmu_buf_refcount = dbuf_refcount
3321 dbuf_refcount(dmu_buf_impl_t
*db
)
3323 return (refcount_count(&db
->db_holds
));
3327 dmu_buf_replace_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*old_user
,
3328 dmu_buf_user_t
*new_user
)
3330 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3332 mutex_enter(&db
->db_mtx
);
3333 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
3334 if (db
->db_user
== old_user
)
3335 db
->db_user
= new_user
;
3337 old_user
= db
->db_user
;
3338 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
3339 mutex_exit(&db
->db_mtx
);
3345 dmu_buf_set_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
3347 return (dmu_buf_replace_user(db_fake
, NULL
, user
));
3351 dmu_buf_set_user_ie(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
3353 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3355 db
->db_user_immediate_evict
= TRUE
;
3356 return (dmu_buf_set_user(db_fake
, user
));
3360 dmu_buf_remove_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
3362 return (dmu_buf_replace_user(db_fake
, user
, NULL
));
3366 dmu_buf_get_user(dmu_buf_t
*db_fake
)
3368 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3370 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
3371 return (db
->db_user
);
3375 dmu_buf_user_evict_wait()
3377 taskq_wait(dbu_evict_taskq
);
3381 dmu_buf_get_blkptr(dmu_buf_t
*db
)
3383 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3384 return (dbi
->db_blkptr
);
3388 dmu_buf_get_objset(dmu_buf_t
*db
)
3390 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3391 return (dbi
->db_objset
);
3395 dmu_buf_dnode_enter(dmu_buf_t
*db
)
3397 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3398 DB_DNODE_ENTER(dbi
);
3399 return (DB_DNODE(dbi
));
3403 dmu_buf_dnode_exit(dmu_buf_t
*db
)
3405 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3410 dbuf_check_blkptr(dnode_t
*dn
, dmu_buf_impl_t
*db
)
3412 /* ASSERT(dmu_tx_is_syncing(tx) */
3413 ASSERT(MUTEX_HELD(&db
->db_mtx
));
3415 if (db
->db_blkptr
!= NULL
)
3418 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3419 db
->db_blkptr
= DN_SPILL_BLKPTR(dn
->dn_phys
);
3420 BP_ZERO(db
->db_blkptr
);
3423 if (db
->db_level
== dn
->dn_phys
->dn_nlevels
-1) {
3425 * This buffer was allocated at a time when there was
3426 * no available blkptrs from the dnode, or it was
3427 * inappropriate to hook it in (i.e., nlevels mis-match).
3429 ASSERT(db
->db_blkid
< dn
->dn_phys
->dn_nblkptr
);
3430 ASSERT(db
->db_parent
== NULL
);
3431 db
->db_parent
= dn
->dn_dbuf
;
3432 db
->db_blkptr
= &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
];
3435 dmu_buf_impl_t
*parent
= db
->db_parent
;
3436 int epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
3438 ASSERT(dn
->dn_phys
->dn_nlevels
> 1);
3439 if (parent
== NULL
) {
3440 mutex_exit(&db
->db_mtx
);
3441 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
3442 parent
= dbuf_hold_level(dn
, db
->db_level
+ 1,
3443 db
->db_blkid
>> epbs
, db
);
3444 rw_exit(&dn
->dn_struct_rwlock
);
3445 mutex_enter(&db
->db_mtx
);
3446 db
->db_parent
= parent
;
3448 db
->db_blkptr
= (blkptr_t
*)parent
->db
.db_data
+
3449 (db
->db_blkid
& ((1ULL << epbs
) - 1));
3455 * Ensure the dbuf's data is untransformed if the associated dirty
3456 * record requires it. This is used by dbuf_sync_leaf() to ensure
3457 * that a dnode block is decrypted before we write new data to it.
3458 * For raw writes we assert that the buffer is already encrypted.
3461 dbuf_check_crypt(dbuf_dirty_record_t
*dr
)
3464 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3466 ASSERT(MUTEX_HELD(&db
->db_mtx
));
3468 if (!dr
->dt
.dl
.dr_raw
&& arc_is_encrypted(db
->db_buf
)) {
3469 zbookmark_phys_t zb
;
3472 * Unfortunately, there is currently no mechanism for
3473 * syncing context to handle decryption errors. An error
3474 * here is only possible if an attacker maliciously
3475 * changed a dnode block and updated the associated
3476 * checksums going up the block tree.
3478 SET_BOOKMARK(&zb
, dmu_objset_id(db
->db_objset
),
3479 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
3480 err
= arc_untransform(db
->db_buf
, db
->db_objset
->os_spa
,
3483 panic("Invalid dnode block MAC");
3484 } else if (dr
->dt
.dl
.dr_raw
) {
3486 * Writing raw encrypted data requires the db's arc buffer
3487 * to be converted to raw by the caller.
3489 ASSERT(arc_is_encrypted(db
->db_buf
));
3494 * dbuf_sync_indirect() is called recursively from dbuf_sync_list() so it
3495 * is critical the we not allow the compiler to inline this function in to
3496 * dbuf_sync_list() thereby drastically bloating the stack usage.
3498 noinline
static void
3499 dbuf_sync_indirect(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
3501 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3505 ASSERT(dmu_tx_is_syncing(tx
));
3507 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
3509 mutex_enter(&db
->db_mtx
);
3511 ASSERT(db
->db_level
> 0);
3514 /* Read the block if it hasn't been read yet. */
3515 if (db
->db_buf
== NULL
) {
3516 mutex_exit(&db
->db_mtx
);
3517 (void) dbuf_read(db
, NULL
, DB_RF_MUST_SUCCEED
);
3518 mutex_enter(&db
->db_mtx
);
3520 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
3521 ASSERT(db
->db_buf
!= NULL
);
3525 /* Indirect block size must match what the dnode thinks it is. */
3526 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
3527 dbuf_check_blkptr(dn
, db
);
3530 /* Provide the pending dirty record to child dbufs */
3531 db
->db_data_pending
= dr
;
3533 mutex_exit(&db
->db_mtx
);
3535 dbuf_write(dr
, db
->db_buf
, tx
);
3538 mutex_enter(&dr
->dt
.di
.dr_mtx
);
3539 dbuf_sync_list(&dr
->dt
.di
.dr_children
, db
->db_level
- 1, tx
);
3540 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
3541 mutex_exit(&dr
->dt
.di
.dr_mtx
);
3546 * dbuf_sync_leaf() is called recursively from dbuf_sync_list() so it is
3547 * critical the we not allow the compiler to inline this function in to
3548 * dbuf_sync_list() thereby drastically bloating the stack usage.
3550 noinline
static void
3551 dbuf_sync_leaf(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
3553 arc_buf_t
**datap
= &dr
->dt
.dl
.dr_data
;
3554 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3557 uint64_t txg
= tx
->tx_txg
;
3559 ASSERT(dmu_tx_is_syncing(tx
));
3561 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
3563 mutex_enter(&db
->db_mtx
);
3565 * To be synced, we must be dirtied. But we
3566 * might have been freed after the dirty.
3568 if (db
->db_state
== DB_UNCACHED
) {
3569 /* This buffer has been freed since it was dirtied */
3570 ASSERT(db
->db
.db_data
== NULL
);
3571 } else if (db
->db_state
== DB_FILL
) {
3572 /* This buffer was freed and is now being re-filled */
3573 ASSERT(db
->db
.db_data
!= dr
->dt
.dl
.dr_data
);
3575 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_NOFILL
);
3582 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3583 mutex_enter(&dn
->dn_mtx
);
3584 if (!(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
)) {
3586 * In the previous transaction group, the bonus buffer
3587 * was entirely used to store the attributes for the
3588 * dnode which overrode the dn_spill field. However,
3589 * when adding more attributes to the file a spill
3590 * block was required to hold the extra attributes.
3592 * Make sure to clear the garbage left in the dn_spill
3593 * field from the previous attributes in the bonus
3594 * buffer. Otherwise, after writing out the spill
3595 * block to the new allocated dva, it will free
3596 * the old block pointed to by the invalid dn_spill.
3598 db
->db_blkptr
= NULL
;
3600 dn
->dn_phys
->dn_flags
|= DNODE_FLAG_SPILL_BLKPTR
;
3601 mutex_exit(&dn
->dn_mtx
);
3605 * If this is a bonus buffer, simply copy the bonus data into the
3606 * dnode. It will be written out when the dnode is synced (and it
3607 * will be synced, since it must have been dirty for dbuf_sync to
3610 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
3611 dbuf_dirty_record_t
**drp
;
3613 ASSERT(*datap
!= NULL
);
3614 ASSERT0(db
->db_level
);
3615 ASSERT3U(DN_MAX_BONUS_LEN(dn
->dn_phys
), <=,
3616 DN_SLOTS_TO_BONUSLEN(dn
->dn_phys
->dn_extra_slots
+ 1));
3617 bcopy(*datap
, DN_BONUS(dn
->dn_phys
),
3618 DN_MAX_BONUS_LEN(dn
->dn_phys
));
3621 if (*datap
!= db
->db
.db_data
) {
3622 int slots
= DB_DNODE(db
)->dn_num_slots
;
3623 int bonuslen
= DN_SLOTS_TO_BONUSLEN(slots
);
3624 kmem_free(*datap
, bonuslen
);
3625 arc_space_return(bonuslen
, ARC_SPACE_BONUS
);
3627 db
->db_data_pending
= NULL
;
3628 drp
= &db
->db_last_dirty
;
3630 drp
= &(*drp
)->dr_next
;
3631 ASSERT(dr
->dr_next
== NULL
);
3632 ASSERT(dr
->dr_dbuf
== db
);
3634 if (dr
->dr_dbuf
->db_level
!= 0) {
3635 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
3636 list_destroy(&dr
->dt
.di
.dr_children
);
3638 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
3639 ASSERT(db
->db_dirtycnt
> 0);
3640 db
->db_dirtycnt
-= 1;
3641 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)txg
);
3648 * This function may have dropped the db_mtx lock allowing a dmu_sync
3649 * operation to sneak in. As a result, we need to ensure that we
3650 * don't check the dr_override_state until we have returned from
3651 * dbuf_check_blkptr.
3653 dbuf_check_blkptr(dn
, db
);
3656 * If this buffer is in the middle of an immediate write,
3657 * wait for the synchronous IO to complete.
3659 while (dr
->dt
.dl
.dr_override_state
== DR_IN_DMU_SYNC
) {
3660 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
3661 cv_wait(&db
->db_changed
, &db
->db_mtx
);
3662 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_NOT_OVERRIDDEN
);
3666 * If this is a dnode block, ensure it is appropriately encrypted
3667 * or decrypted, depending on what we are writing to it this txg.
3669 if (os
->os_encrypted
&& dn
->dn_object
== DMU_META_DNODE_OBJECT
)
3670 dbuf_check_crypt(dr
);
3672 if (db
->db_state
!= DB_NOFILL
&&
3673 dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
3674 refcount_count(&db
->db_holds
) > 1 &&
3675 dr
->dt
.dl
.dr_override_state
!= DR_OVERRIDDEN
&&
3676 *datap
== db
->db_buf
) {
3678 * If this buffer is currently "in use" (i.e., there
3679 * are active holds and db_data still references it),
3680 * then make a copy before we start the write so that
3681 * any modifications from the open txg will not leak
3684 * NOTE: this copy does not need to be made for
3685 * objects only modified in the syncing context (e.g.
3686 * DNONE_DNODE blocks).
3688 int psize
= arc_buf_size(*datap
);
3689 int lsize
= arc_buf_lsize(*datap
);
3690 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
3691 enum zio_compress compress_type
= arc_get_compression(*datap
);
3693 if (arc_is_encrypted(*datap
)) {
3694 boolean_t byteorder
;
3695 uint8_t salt
[ZIO_DATA_SALT_LEN
];
3696 uint8_t iv
[ZIO_DATA_IV_LEN
];
3697 uint8_t mac
[ZIO_DATA_MAC_LEN
];
3699 arc_get_raw_params(*datap
, &byteorder
, salt
, iv
, mac
);
3700 *datap
= arc_alloc_raw_buf(os
->os_spa
, db
,
3701 dmu_objset_id(os
), byteorder
, salt
, iv
, mac
,
3702 dn
->dn_type
, psize
, lsize
, compress_type
);
3703 } else if (compress_type
!= ZIO_COMPRESS_OFF
) {
3704 ASSERT3U(type
, ==, ARC_BUFC_DATA
);
3705 *datap
= arc_alloc_compressed_buf(os
->os_spa
, db
,
3706 psize
, lsize
, compress_type
);
3708 *datap
= arc_alloc_buf(os
->os_spa
, db
, type
, psize
);
3710 bcopy(db
->db
.db_data
, (*datap
)->b_data
, psize
);
3712 db
->db_data_pending
= dr
;
3714 mutex_exit(&db
->db_mtx
);
3716 dbuf_write(dr
, *datap
, tx
);
3718 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
3719 if (dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
3720 list_insert_tail(&dn
->dn_dirty_records
[txg
&TXG_MASK
], dr
);
3724 * Although zio_nowait() does not "wait for an IO", it does
3725 * initiate the IO. If this is an empty write it seems plausible
3726 * that the IO could actually be completed before the nowait
3727 * returns. We need to DB_DNODE_EXIT() first in case
3728 * zio_nowait() invalidates the dbuf.
3731 zio_nowait(dr
->dr_zio
);
3736 dbuf_sync_list(list_t
*list
, int level
, dmu_tx_t
*tx
)
3738 dbuf_dirty_record_t
*dr
;
3740 while ((dr
= list_head(list
))) {
3741 if (dr
->dr_zio
!= NULL
) {
3743 * If we find an already initialized zio then we
3744 * are processing the meta-dnode, and we have finished.
3745 * The dbufs for all dnodes are put back on the list
3746 * during processing, so that we can zio_wait()
3747 * these IOs after initiating all child IOs.
3749 ASSERT3U(dr
->dr_dbuf
->db
.db_object
, ==,
3750 DMU_META_DNODE_OBJECT
);
3753 if (dr
->dr_dbuf
->db_blkid
!= DMU_BONUS_BLKID
&&
3754 dr
->dr_dbuf
->db_blkid
!= DMU_SPILL_BLKID
) {
3755 VERIFY3U(dr
->dr_dbuf
->db_level
, ==, level
);
3757 list_remove(list
, dr
);
3758 if (dr
->dr_dbuf
->db_level
> 0)
3759 dbuf_sync_indirect(dr
, tx
);
3761 dbuf_sync_leaf(dr
, tx
);
3767 dbuf_write_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
3769 dmu_buf_impl_t
*db
= vdb
;
3771 blkptr_t
*bp
= zio
->io_bp
;
3772 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
3773 spa_t
*spa
= zio
->io_spa
;
3778 ASSERT3P(db
->db_blkptr
, !=, NULL
);
3779 ASSERT3P(&db
->db_data_pending
->dr_bp_copy
, ==, bp
);
3783 delta
= bp_get_dsize_sync(spa
, bp
) - bp_get_dsize_sync(spa
, bp_orig
);
3784 dnode_diduse_space(dn
, delta
- zio
->io_prev_space_delta
);
3785 zio
->io_prev_space_delta
= delta
;
3787 if (bp
->blk_birth
!= 0) {
3788 ASSERT((db
->db_blkid
!= DMU_SPILL_BLKID
&&
3789 BP_GET_TYPE(bp
) == dn
->dn_type
) ||
3790 (db
->db_blkid
== DMU_SPILL_BLKID
&&
3791 BP_GET_TYPE(bp
) == dn
->dn_bonustype
) ||
3792 BP_IS_EMBEDDED(bp
));
3793 ASSERT(BP_GET_LEVEL(bp
) == db
->db_level
);
3796 mutex_enter(&db
->db_mtx
);
3799 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3800 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
3801 ASSERT(!(BP_IS_HOLE(bp
)) &&
3802 db
->db_blkptr
== DN_SPILL_BLKPTR(dn
->dn_phys
));
3806 if (db
->db_level
== 0) {
3807 mutex_enter(&dn
->dn_mtx
);
3808 if (db
->db_blkid
> dn
->dn_phys
->dn_maxblkid
&&
3809 db
->db_blkid
!= DMU_SPILL_BLKID
)
3810 dn
->dn_phys
->dn_maxblkid
= db
->db_blkid
;
3811 mutex_exit(&dn
->dn_mtx
);
3813 if (dn
->dn_type
== DMU_OT_DNODE
) {
3815 while (i
< db
->db
.db_size
) {
3817 (void *)(((char *)db
->db
.db_data
) + i
);
3819 i
+= DNODE_MIN_SIZE
;
3820 if (dnp
->dn_type
!= DMU_OT_NONE
) {
3822 i
+= dnp
->dn_extra_slots
*
3827 if (BP_IS_HOLE(bp
)) {
3834 blkptr_t
*ibp
= db
->db
.db_data
;
3835 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
3836 for (i
= db
->db
.db_size
>> SPA_BLKPTRSHIFT
; i
> 0; i
--, ibp
++) {
3837 if (BP_IS_HOLE(ibp
))
3839 fill
+= BP_GET_FILL(ibp
);
3844 if (!BP_IS_EMBEDDED(bp
))
3845 BP_SET_FILL(bp
, fill
);
3847 mutex_exit(&db
->db_mtx
);
3849 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
3850 *db
->db_blkptr
= *bp
;
3851 rw_exit(&dn
->dn_struct_rwlock
);
3856 * This function gets called just prior to running through the compression
3857 * stage of the zio pipeline. If we're an indirect block comprised of only
3858 * holes, then we want this indirect to be compressed away to a hole. In
3859 * order to do that we must zero out any information about the holes that
3860 * this indirect points to prior to before we try to compress it.
3863 dbuf_write_children_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
3865 dmu_buf_impl_t
*db
= vdb
;
3868 unsigned int epbs
, i
;
3870 ASSERT3U(db
->db_level
, >, 0);
3873 epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
3874 ASSERT3U(epbs
, <, 31);
3876 /* Determine if all our children are holes */
3877 for (i
= 0, bp
= db
->db
.db_data
; i
< 1ULL << epbs
; i
++, bp
++) {
3878 if (!BP_IS_HOLE(bp
))
3883 * If all the children are holes, then zero them all out so that
3884 * we may get compressed away.
3886 if (i
== 1ULL << epbs
) {
3888 * We only found holes. Grab the rwlock to prevent
3889 * anybody from reading the blocks we're about to
3892 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
3893 bzero(db
->db
.db_data
, db
->db
.db_size
);
3894 rw_exit(&dn
->dn_struct_rwlock
);
3900 * The SPA will call this callback several times for each zio - once
3901 * for every physical child i/o (zio->io_phys_children times). This
3902 * allows the DMU to monitor the progress of each logical i/o. For example,
3903 * there may be 2 copies of an indirect block, or many fragments of a RAID-Z
3904 * block. There may be a long delay before all copies/fragments are completed,
3905 * so this callback allows us to retire dirty space gradually, as the physical
3910 dbuf_write_physdone(zio_t
*zio
, arc_buf_t
*buf
, void *arg
)
3912 dmu_buf_impl_t
*db
= arg
;
3913 objset_t
*os
= db
->db_objset
;
3914 dsl_pool_t
*dp
= dmu_objset_pool(os
);
3915 dbuf_dirty_record_t
*dr
;
3918 dr
= db
->db_data_pending
;
3919 ASSERT3U(dr
->dr_txg
, ==, zio
->io_txg
);
3922 * The callback will be called io_phys_children times. Retire one
3923 * portion of our dirty space each time we are called. Any rounding
3924 * error will be cleaned up by dsl_pool_sync()'s call to
3925 * dsl_pool_undirty_space().
3927 delta
= dr
->dr_accounted
/ zio
->io_phys_children
;
3928 dsl_pool_undirty_space(dp
, delta
, zio
->io_txg
);
3933 dbuf_write_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
3935 dmu_buf_impl_t
*db
= vdb
;
3936 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
3937 blkptr_t
*bp
= db
->db_blkptr
;
3938 objset_t
*os
= db
->db_objset
;
3939 dmu_tx_t
*tx
= os
->os_synctx
;
3940 dbuf_dirty_record_t
**drp
, *dr
;
3942 ASSERT0(zio
->io_error
);
3943 ASSERT(db
->db_blkptr
== bp
);
3946 * For nopwrites and rewrites we ensure that the bp matches our
3947 * original and bypass all the accounting.
3949 if (zio
->io_flags
& (ZIO_FLAG_IO_REWRITE
| ZIO_FLAG_NOPWRITE
)) {
3950 ASSERT(BP_EQUAL(bp
, bp_orig
));
3952 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
3953 (void) dsl_dataset_block_kill(ds
, bp_orig
, tx
, B_TRUE
);
3954 dsl_dataset_block_born(ds
, bp
, tx
);
3957 mutex_enter(&db
->db_mtx
);
3961 drp
= &db
->db_last_dirty
;
3962 while ((dr
= *drp
) != db
->db_data_pending
)
3964 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
3965 ASSERT(dr
->dr_dbuf
== db
);
3966 ASSERT(dr
->dr_next
== NULL
);
3970 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3975 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
3976 ASSERT(!(BP_IS_HOLE(db
->db_blkptr
)) &&
3977 db
->db_blkptr
== DN_SPILL_BLKPTR(dn
->dn_phys
));
3982 if (db
->db_level
== 0) {
3983 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
3984 ASSERT(dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
);
3985 if (db
->db_state
!= DB_NOFILL
) {
3986 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
3987 arc_buf_destroy(dr
->dt
.dl
.dr_data
, db
);
3994 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
3995 ASSERT3U(db
->db
.db_size
, ==, 1 << dn
->dn_phys
->dn_indblkshift
);
3996 if (!BP_IS_HOLE(db
->db_blkptr
)) {
3997 ASSERTV(int epbs
= dn
->dn_phys
->dn_indblkshift
-
3999 ASSERT3U(db
->db_blkid
, <=,
4000 dn
->dn_phys
->dn_maxblkid
>> (db
->db_level
* epbs
));
4001 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==,
4005 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
4006 list_destroy(&dr
->dt
.di
.dr_children
);
4008 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
4010 cv_broadcast(&db
->db_changed
);
4011 ASSERT(db
->db_dirtycnt
> 0);
4012 db
->db_dirtycnt
-= 1;
4013 db
->db_data_pending
= NULL
;
4014 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)tx
->tx_txg
);
4018 dbuf_write_nofill_ready(zio_t
*zio
)
4020 dbuf_write_ready(zio
, NULL
, zio
->io_private
);
4024 dbuf_write_nofill_done(zio_t
*zio
)
4026 dbuf_write_done(zio
, NULL
, zio
->io_private
);
4030 dbuf_write_override_ready(zio_t
*zio
)
4032 dbuf_dirty_record_t
*dr
= zio
->io_private
;
4033 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
4035 dbuf_write_ready(zio
, NULL
, db
);
4039 dbuf_write_override_done(zio_t
*zio
)
4041 dbuf_dirty_record_t
*dr
= zio
->io_private
;
4042 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
4043 blkptr_t
*obp
= &dr
->dt
.dl
.dr_overridden_by
;
4045 mutex_enter(&db
->db_mtx
);
4046 if (!BP_EQUAL(zio
->io_bp
, obp
)) {
4047 if (!BP_IS_HOLE(obp
))
4048 dsl_free(spa_get_dsl(zio
->io_spa
), zio
->io_txg
, obp
);
4049 arc_release(dr
->dt
.dl
.dr_data
, db
);
4051 mutex_exit(&db
->db_mtx
);
4053 dbuf_write_done(zio
, NULL
, db
);
4055 if (zio
->io_abd
!= NULL
)
4056 abd_put(zio
->io_abd
);
4059 typedef struct dbuf_remap_impl_callback_arg
{
4061 uint64_t drica_blk_birth
;
4063 } dbuf_remap_impl_callback_arg_t
;
4066 dbuf_remap_impl_callback(uint64_t vdev
, uint64_t offset
, uint64_t size
,
4069 dbuf_remap_impl_callback_arg_t
*drica
= arg
;
4070 objset_t
*os
= drica
->drica_os
;
4071 spa_t
*spa
= dmu_objset_spa(os
);
4072 dmu_tx_t
*tx
= drica
->drica_tx
;
4074 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa
)));
4076 if (os
== spa_meta_objset(spa
)) {
4077 spa_vdev_indirect_mark_obsolete(spa
, vdev
, offset
, size
, tx
);
4079 dsl_dataset_block_remapped(dmu_objset_ds(os
), vdev
, offset
,
4080 size
, drica
->drica_blk_birth
, tx
);
4085 dbuf_remap_impl(dnode_t
*dn
, blkptr_t
*bp
, dmu_tx_t
*tx
)
4087 blkptr_t bp_copy
= *bp
;
4088 spa_t
*spa
= dmu_objset_spa(dn
->dn_objset
);
4089 dbuf_remap_impl_callback_arg_t drica
;
4091 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa
)));
4093 drica
.drica_os
= dn
->dn_objset
;
4094 drica
.drica_blk_birth
= bp
->blk_birth
;
4095 drica
.drica_tx
= tx
;
4096 if (spa_remap_blkptr(spa
, &bp_copy
, dbuf_remap_impl_callback
,
4099 * The struct_rwlock prevents dbuf_read_impl() from
4100 * dereferencing the BP while we are changing it. To
4101 * avoid lock contention, only grab it when we are actually
4104 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
4106 rw_exit(&dn
->dn_struct_rwlock
);
4111 * Returns true if a dbuf_remap would modify the dbuf. We do this by attempting
4112 * to remap a copy of every bp in the dbuf.
4115 dbuf_can_remap(const dmu_buf_impl_t
*db
)
4117 spa_t
*spa
= dmu_objset_spa(db
->db_objset
);
4118 blkptr_t
*bp
= db
->db
.db_data
;
4119 boolean_t ret
= B_FALSE
;
4121 ASSERT3U(db
->db_level
, >, 0);
4122 ASSERT3S(db
->db_state
, ==, DB_CACHED
);
4124 ASSERT(spa_feature_is_active(spa
, SPA_FEATURE_DEVICE_REMOVAL
));
4126 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
4127 for (int i
= 0; i
< db
->db
.db_size
>> SPA_BLKPTRSHIFT
; i
++) {
4128 blkptr_t bp_copy
= bp
[i
];
4129 if (spa_remap_blkptr(spa
, &bp_copy
, NULL
, NULL
)) {
4134 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
4140 dnode_needs_remap(const dnode_t
*dn
)
4142 spa_t
*spa
= dmu_objset_spa(dn
->dn_objset
);
4143 boolean_t ret
= B_FALSE
;
4145 if (dn
->dn_phys
->dn_nlevels
== 0) {
4149 ASSERT(spa_feature_is_active(spa
, SPA_FEATURE_DEVICE_REMOVAL
));
4151 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
4152 for (int j
= 0; j
< dn
->dn_phys
->dn_nblkptr
; j
++) {
4153 blkptr_t bp_copy
= dn
->dn_phys
->dn_blkptr
[j
];
4154 if (spa_remap_blkptr(spa
, &bp_copy
, NULL
, NULL
)) {
4159 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
4165 * Remap any existing BP's to concrete vdevs, if possible.
4168 dbuf_remap(dnode_t
*dn
, dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
4170 spa_t
*spa
= dmu_objset_spa(db
->db_objset
);
4171 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa
)));
4173 if (!spa_feature_is_active(spa
, SPA_FEATURE_DEVICE_REMOVAL
))
4176 if (db
->db_level
> 0) {
4177 blkptr_t
*bp
= db
->db
.db_data
;
4178 for (int i
= 0; i
< db
->db
.db_size
>> SPA_BLKPTRSHIFT
; i
++) {
4179 dbuf_remap_impl(dn
, &bp
[i
], tx
);
4181 } else if (db
->db
.db_object
== DMU_META_DNODE_OBJECT
) {
4182 dnode_phys_t
*dnp
= db
->db
.db_data
;
4183 ASSERT3U(db
->db_dnode_handle
->dnh_dnode
->dn_type
, ==,
4185 for (int i
= 0; i
< db
->db
.db_size
>> DNODE_SHIFT
;
4186 i
+= dnp
[i
].dn_extra_slots
+ 1) {
4187 for (int j
= 0; j
< dnp
[i
].dn_nblkptr
; j
++) {
4188 dbuf_remap_impl(dn
, &dnp
[i
].dn_blkptr
[j
], tx
);
4195 /* Issue I/O to commit a dirty buffer to disk. */
4197 dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
)
4199 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
4202 dmu_buf_impl_t
*parent
= db
->db_parent
;
4203 uint64_t txg
= tx
->tx_txg
;
4204 zbookmark_phys_t zb
;
4209 ASSERT(dmu_tx_is_syncing(tx
));
4215 if (db
->db_state
!= DB_NOFILL
) {
4216 if (db
->db_level
> 0 || dn
->dn_type
== DMU_OT_DNODE
) {
4218 * Private object buffers are released here rather
4219 * than in dbuf_dirty() since they are only modified
4220 * in the syncing context and we don't want the
4221 * overhead of making multiple copies of the data.
4223 if (BP_IS_HOLE(db
->db_blkptr
)) {
4226 dbuf_release_bp(db
);
4228 dbuf_remap(dn
, db
, tx
);
4232 if (parent
!= dn
->dn_dbuf
) {
4233 /* Our parent is an indirect block. */
4234 /* We have a dirty parent that has been scheduled for write. */
4235 ASSERT(parent
&& parent
->db_data_pending
);
4236 /* Our parent's buffer is one level closer to the dnode. */
4237 ASSERT(db
->db_level
== parent
->db_level
-1);
4239 * We're about to modify our parent's db_data by modifying
4240 * our block pointer, so the parent must be released.
4242 ASSERT(arc_released(parent
->db_buf
));
4243 zio
= parent
->db_data_pending
->dr_zio
;
4245 /* Our parent is the dnode itself. */
4246 ASSERT((db
->db_level
== dn
->dn_phys
->dn_nlevels
-1 &&
4247 db
->db_blkid
!= DMU_SPILL_BLKID
) ||
4248 (db
->db_blkid
== DMU_SPILL_BLKID
&& db
->db_level
== 0));
4249 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
4250 ASSERT3P(db
->db_blkptr
, ==,
4251 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
4255 ASSERT(db
->db_level
== 0 || data
== db
->db_buf
);
4256 ASSERT3U(db
->db_blkptr
->blk_birth
, <=, txg
);
4259 SET_BOOKMARK(&zb
, os
->os_dsl_dataset
?
4260 os
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
4261 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
4263 if (db
->db_blkid
== DMU_SPILL_BLKID
)
4265 wp_flag
|= (db
->db_state
== DB_NOFILL
) ? WP_NOFILL
: 0;
4267 dmu_write_policy(os
, dn
, db
->db_level
, wp_flag
, &zp
);
4271 * We copy the blkptr now (rather than when we instantiate the dirty
4272 * record), because its value can change between open context and
4273 * syncing context. We do not need to hold dn_struct_rwlock to read
4274 * db_blkptr because we are in syncing context.
4276 dr
->dr_bp_copy
= *db
->db_blkptr
;
4278 if (db
->db_level
== 0 &&
4279 dr
->dt
.dl
.dr_override_state
== DR_OVERRIDDEN
) {
4281 * The BP for this block has been provided by open context
4282 * (by dmu_sync() or dmu_buf_write_embedded()).
4284 abd_t
*contents
= (data
!= NULL
) ?
4285 abd_get_from_buf(data
->b_data
, arc_buf_size(data
)) : NULL
;
4287 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
,
4288 &dr
->dr_bp_copy
, contents
, db
->db
.db_size
, db
->db
.db_size
,
4289 &zp
, dbuf_write_override_ready
, NULL
, NULL
,
4290 dbuf_write_override_done
,
4291 dr
, ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
4292 mutex_enter(&db
->db_mtx
);
4293 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
4294 zio_write_override(dr
->dr_zio
, &dr
->dt
.dl
.dr_overridden_by
,
4295 dr
->dt
.dl
.dr_copies
, dr
->dt
.dl
.dr_nopwrite
);
4296 mutex_exit(&db
->db_mtx
);
4297 } else if (db
->db_state
== DB_NOFILL
) {
4298 ASSERT(zp
.zp_checksum
== ZIO_CHECKSUM_OFF
||
4299 zp
.zp_checksum
== ZIO_CHECKSUM_NOPARITY
);
4300 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
,
4301 &dr
->dr_bp_copy
, NULL
, db
->db
.db_size
, db
->db
.db_size
, &zp
,
4302 dbuf_write_nofill_ready
, NULL
, NULL
,
4303 dbuf_write_nofill_done
, db
,
4304 ZIO_PRIORITY_ASYNC_WRITE
,
4305 ZIO_FLAG_MUSTSUCCEED
| ZIO_FLAG_NODATA
, &zb
);
4307 ASSERT(arc_released(data
));
4310 * For indirect blocks, we want to setup the children
4311 * ready callback so that we can properly handle an indirect
4312 * block that only contains holes.
4314 arc_write_done_func_t
*children_ready_cb
= NULL
;
4315 if (db
->db_level
!= 0)
4316 children_ready_cb
= dbuf_write_children_ready
;
4318 dr
->dr_zio
= arc_write(zio
, os
->os_spa
, txg
,
4319 &dr
->dr_bp_copy
, data
, DBUF_IS_L2CACHEABLE(db
),
4320 &zp
, dbuf_write_ready
,
4321 children_ready_cb
, dbuf_write_physdone
,
4322 dbuf_write_done
, db
, ZIO_PRIORITY_ASYNC_WRITE
,
4323 ZIO_FLAG_MUSTSUCCEED
, &zb
);
4327 #if defined(_KERNEL) && defined(HAVE_SPL)
4328 EXPORT_SYMBOL(dbuf_find
);
4329 EXPORT_SYMBOL(dbuf_is_metadata
);
4330 EXPORT_SYMBOL(dbuf_destroy
);
4331 EXPORT_SYMBOL(dbuf_loan_arcbuf
);
4332 EXPORT_SYMBOL(dbuf_whichblock
);
4333 EXPORT_SYMBOL(dbuf_read
);
4334 EXPORT_SYMBOL(dbuf_unoverride
);
4335 EXPORT_SYMBOL(dbuf_free_range
);
4336 EXPORT_SYMBOL(dbuf_new_size
);
4337 EXPORT_SYMBOL(dbuf_release_bp
);
4338 EXPORT_SYMBOL(dbuf_dirty
);
4339 EXPORT_SYMBOL(dmu_buf_will_change_crypt_params
);
4340 EXPORT_SYMBOL(dmu_buf_will_dirty
);
4341 EXPORT_SYMBOL(dmu_buf_will_not_fill
);
4342 EXPORT_SYMBOL(dmu_buf_will_fill
);
4343 EXPORT_SYMBOL(dmu_buf_fill_done
);
4344 EXPORT_SYMBOL(dmu_buf_rele
);
4345 EXPORT_SYMBOL(dbuf_assign_arcbuf
);
4346 EXPORT_SYMBOL(dbuf_prefetch
);
4347 EXPORT_SYMBOL(dbuf_hold_impl
);
4348 EXPORT_SYMBOL(dbuf_hold
);
4349 EXPORT_SYMBOL(dbuf_hold_level
);
4350 EXPORT_SYMBOL(dbuf_create_bonus
);
4351 EXPORT_SYMBOL(dbuf_spill_set_blksz
);
4352 EXPORT_SYMBOL(dbuf_rm_spill
);
4353 EXPORT_SYMBOL(dbuf_add_ref
);
4354 EXPORT_SYMBOL(dbuf_rele
);
4355 EXPORT_SYMBOL(dbuf_rele_and_unlock
);
4356 EXPORT_SYMBOL(dbuf_refcount
);
4357 EXPORT_SYMBOL(dbuf_sync_list
);
4358 EXPORT_SYMBOL(dmu_buf_set_user
);
4359 EXPORT_SYMBOL(dmu_buf_set_user_ie
);
4360 EXPORT_SYMBOL(dmu_buf_get_user
);
4361 EXPORT_SYMBOL(dmu_buf_get_blkptr
);
4364 module_param(dbuf_cache_max_bytes
, ulong
, 0644);
4365 MODULE_PARM_DESC(dbuf_cache_max_bytes
,
4366 "Maximum size in bytes of the dbuf cache.");
4368 module_param(dbuf_cache_hiwater_pct
, uint
, 0644);
4369 MODULE_PARM_DESC(dbuf_cache_hiwater_pct
,
4370 "Percentage over dbuf_cache_max_bytes when dbufs must be evicted "
4373 module_param(dbuf_cache_lowater_pct
, uint
, 0644);
4374 MODULE_PARM_DESC(dbuf_cache_lowater_pct
,
4375 "Percentage below dbuf_cache_max_bytes when the evict thread stops "
4378 module_param(dbuf_cache_shift
, int, 0644);
4379 MODULE_PARM_DESC(dbuf_cache_shift
,
4380 "Set the size of the dbuf cache to a log2 fraction of arc size.");