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 https://opensource.org/licenses/CDDL-1.0.
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, 2020 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.
27 * Copyright (c) 2019, Klara Inc.
28 * Copyright (c) 2019, Allan Jude
29 * Copyright (c) 2021, 2022 by Pawel Jakub Dawidek
32 #include <sys/zfs_context.h>
35 #include <sys/dmu_send.h>
36 #include <sys/dmu_impl.h>
38 #include <sys/dmu_objset.h>
39 #include <sys/dsl_dataset.h>
40 #include <sys/dsl_dir.h>
41 #include <sys/dmu_tx.h>
44 #include <sys/dmu_zfetch.h>
46 #include <sys/sa_impl.h>
47 #include <sys/zfeature.h>
48 #include <sys/blkptr.h>
49 #include <sys/range_tree.h>
50 #include <sys/trace_zfs.h>
51 #include <sys/callb.h>
56 #include <sys/spa_impl.h>
57 #include <sys/wmsum.h>
58 #include <sys/vdev_impl.h>
60 static kstat_t
*dbuf_ksp
;
62 typedef struct dbuf_stats
{
64 * Various statistics about the size of the dbuf cache.
66 kstat_named_t cache_count
;
67 kstat_named_t cache_size_bytes
;
68 kstat_named_t cache_size_bytes_max
;
70 * Statistics regarding the bounds on the dbuf cache size.
72 kstat_named_t cache_target_bytes
;
73 kstat_named_t cache_lowater_bytes
;
74 kstat_named_t cache_hiwater_bytes
;
76 * Total number of dbuf cache evictions that have occurred.
78 kstat_named_t cache_total_evicts
;
80 * The distribution of dbuf levels in the dbuf cache and
81 * the total size of all dbufs at each level.
83 kstat_named_t cache_levels
[DN_MAX_LEVELS
];
84 kstat_named_t cache_levels_bytes
[DN_MAX_LEVELS
];
86 * Statistics about the dbuf hash table.
88 kstat_named_t hash_hits
;
89 kstat_named_t hash_misses
;
90 kstat_named_t hash_collisions
;
91 kstat_named_t hash_elements
;
93 * Number of sublists containing more than one dbuf in the dbuf
94 * hash table. Keep track of the longest hash chain.
96 kstat_named_t hash_chains
;
97 kstat_named_t hash_chain_max
;
99 * Number of times a dbuf_create() discovers that a dbuf was
100 * already created and in the dbuf hash table.
102 kstat_named_t hash_insert_race
;
104 * Number of entries in the hash table dbuf and mutex arrays.
106 kstat_named_t hash_table_count
;
107 kstat_named_t hash_mutex_count
;
109 * Statistics about the size of the metadata dbuf cache.
111 kstat_named_t metadata_cache_count
;
112 kstat_named_t metadata_cache_size_bytes
;
113 kstat_named_t metadata_cache_size_bytes_max
;
115 * For diagnostic purposes, this is incremented whenever we can't add
116 * something to the metadata cache because it's full, and instead put
117 * the data in the regular dbuf cache.
119 kstat_named_t metadata_cache_overflow
;
122 dbuf_stats_t dbuf_stats
= {
123 { "cache_count", KSTAT_DATA_UINT64
},
124 { "cache_size_bytes", KSTAT_DATA_UINT64
},
125 { "cache_size_bytes_max", KSTAT_DATA_UINT64
},
126 { "cache_target_bytes", KSTAT_DATA_UINT64
},
127 { "cache_lowater_bytes", KSTAT_DATA_UINT64
},
128 { "cache_hiwater_bytes", KSTAT_DATA_UINT64
},
129 { "cache_total_evicts", KSTAT_DATA_UINT64
},
130 { { "cache_levels_N", KSTAT_DATA_UINT64
} },
131 { { "cache_levels_bytes_N", KSTAT_DATA_UINT64
} },
132 { "hash_hits", KSTAT_DATA_UINT64
},
133 { "hash_misses", KSTAT_DATA_UINT64
},
134 { "hash_collisions", KSTAT_DATA_UINT64
},
135 { "hash_elements", KSTAT_DATA_UINT64
},
136 { "hash_chains", KSTAT_DATA_UINT64
},
137 { "hash_chain_max", KSTAT_DATA_UINT64
},
138 { "hash_insert_race", KSTAT_DATA_UINT64
},
139 { "hash_table_count", KSTAT_DATA_UINT64
},
140 { "hash_mutex_count", KSTAT_DATA_UINT64
},
141 { "metadata_cache_count", KSTAT_DATA_UINT64
},
142 { "metadata_cache_size_bytes", KSTAT_DATA_UINT64
},
143 { "metadata_cache_size_bytes_max", KSTAT_DATA_UINT64
},
144 { "metadata_cache_overflow", KSTAT_DATA_UINT64
}
149 wmsum_t cache_total_evicts
;
150 wmsum_t cache_levels
[DN_MAX_LEVELS
];
151 wmsum_t cache_levels_bytes
[DN_MAX_LEVELS
];
154 wmsum_t hash_collisions
;
155 wmsum_t hash_elements
;
157 wmsum_t hash_insert_race
;
158 wmsum_t metadata_cache_count
;
159 wmsum_t metadata_cache_overflow
;
162 #define DBUF_STAT_INCR(stat, val) \
163 wmsum_add(&dbuf_sums.stat, val)
164 #define DBUF_STAT_DECR(stat, val) \
165 DBUF_STAT_INCR(stat, -(val))
166 #define DBUF_STAT_BUMP(stat) \
167 DBUF_STAT_INCR(stat, 1)
168 #define DBUF_STAT_BUMPDOWN(stat) \
169 DBUF_STAT_INCR(stat, -1)
170 #define DBUF_STAT_MAX(stat, v) { \
172 while ((v) > (_m = dbuf_stats.stat.value.ui64) && \
173 (_m != atomic_cas_64(&dbuf_stats.stat.value.ui64, _m, (v))))\
177 static void dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
);
178 static void dbuf_sync_leaf_verify_bonus_dnode(dbuf_dirty_record_t
*dr
);
181 * Global data structures and functions for the dbuf cache.
183 static kmem_cache_t
*dbuf_kmem_cache
;
184 kmem_cache_t
*dbuf_dirty_kmem_cache
;
185 static taskq_t
*dbu_evict_taskq
;
187 static kthread_t
*dbuf_cache_evict_thread
;
188 static kmutex_t dbuf_evict_lock
;
189 static kcondvar_t dbuf_evict_cv
;
190 static boolean_t dbuf_evict_thread_exit
;
193 * There are two dbuf caches; each dbuf can only be in one of them at a time.
195 * 1. Cache of metadata dbufs, to help make read-heavy administrative commands
196 * from /sbin/zfs run faster. The "metadata cache" specifically stores dbufs
197 * that represent the metadata that describes filesystems/snapshots/
198 * bookmarks/properties/etc. We only evict from this cache when we export a
199 * pool, to short-circuit as much I/O as possible for all administrative
200 * commands that need the metadata. There is no eviction policy for this
201 * cache, because we try to only include types in it which would occupy a
202 * very small amount of space per object but create a large impact on the
203 * performance of these commands. Instead, after it reaches a maximum size
204 * (which should only happen on very small memory systems with a very large
205 * number of filesystem objects), we stop taking new dbufs into the
206 * metadata cache, instead putting them in the normal dbuf cache.
208 * 2. LRU cache of dbufs. The dbuf cache maintains a list of dbufs that
209 * are not currently held but have been recently released. These dbufs
210 * are not eligible for arc eviction until they are aged out of the cache.
211 * Dbufs that are aged out of the cache will be immediately destroyed and
212 * become eligible for arc eviction.
214 * Dbufs are added to these caches once the last hold is released. If a dbuf is
215 * later accessed and still exists in the dbuf cache, then it will be removed
216 * from the cache and later re-added to the head of the cache.
218 * If a given dbuf meets the requirements for the metadata cache, it will go
219 * there, otherwise it will be considered for the generic LRU dbuf cache. The
220 * caches and the refcounts tracking their sizes are stored in an array indexed
221 * by those caches' matching enum values (from dbuf_cached_state_t).
223 typedef struct dbuf_cache
{
225 zfs_refcount_t size ____cacheline_aligned
;
227 dbuf_cache_t dbuf_caches
[DB_CACHE_MAX
];
229 /* Size limits for the caches */
230 static uint64_t dbuf_cache_max_bytes
= UINT64_MAX
;
231 static uint64_t dbuf_metadata_cache_max_bytes
= UINT64_MAX
;
233 /* Set the default sizes of the caches to log2 fraction of arc size */
234 static uint_t dbuf_cache_shift
= 5;
235 static uint_t dbuf_metadata_cache_shift
= 6;
237 /* Set the dbuf hash mutex count as log2 shift (dynamic by default) */
238 static uint_t dbuf_mutex_cache_shift
= 0;
240 static unsigned long dbuf_cache_target_bytes(void);
241 static unsigned long dbuf_metadata_cache_target_bytes(void);
244 * The LRU dbuf cache uses a three-stage eviction policy:
245 * - A low water marker designates when the dbuf eviction thread
246 * should stop evicting from the dbuf cache.
247 * - When we reach the maximum size (aka mid water mark), we
248 * signal the eviction thread to run.
249 * - The high water mark indicates when the eviction thread
250 * is unable to keep up with the incoming load and eviction must
251 * happen in the context of the calling thread.
255 * low water mid water hi water
256 * +----------------------------------------+----------+----------+
261 * +----------------------------------------+----------+----------+
263 * evicting eviction directly
266 * The high and low water marks indicate the operating range for the eviction
267 * thread. The low water mark is, by default, 90% of the total size of the
268 * cache and the high water mark is at 110% (both of these percentages can be
269 * changed by setting dbuf_cache_lowater_pct and dbuf_cache_hiwater_pct,
270 * respectively). The eviction thread will try to ensure that the cache remains
271 * within this range by waking up every second and checking if the cache is
272 * above the low water mark. The thread can also be woken up by callers adding
273 * elements into the cache if the cache is larger than the mid water (i.e max
274 * cache size). Once the eviction thread is woken up and eviction is required,
275 * it will continue evicting buffers until it's able to reduce the cache size
276 * to the low water mark. If the cache size continues to grow and hits the high
277 * water mark, then callers adding elements to the cache will begin to evict
278 * directly from the cache until the cache is no longer above the high water
283 * The percentage above and below the maximum cache size.
285 static uint_t dbuf_cache_hiwater_pct
= 10;
286 static uint_t dbuf_cache_lowater_pct
= 10;
289 dbuf_cons(void *vdb
, void *unused
, int kmflag
)
291 (void) unused
, (void) kmflag
;
292 dmu_buf_impl_t
*db
= vdb
;
293 memset(db
, 0, sizeof (dmu_buf_impl_t
));
295 mutex_init(&db
->db_mtx
, NULL
, MUTEX_NOLOCKDEP
, NULL
);
296 rw_init(&db
->db_rwlock
, NULL
, RW_NOLOCKDEP
, NULL
);
297 cv_init(&db
->db_changed
, NULL
, CV_DEFAULT
, NULL
);
298 multilist_link_init(&db
->db_cache_link
);
299 zfs_refcount_create(&db
->db_holds
);
305 dbuf_dest(void *vdb
, void *unused
)
308 dmu_buf_impl_t
*db
= vdb
;
309 mutex_destroy(&db
->db_mtx
);
310 rw_destroy(&db
->db_rwlock
);
311 cv_destroy(&db
->db_changed
);
312 ASSERT(!multilist_link_active(&db
->db_cache_link
));
313 zfs_refcount_destroy(&db
->db_holds
);
317 * dbuf hash table routines
319 static dbuf_hash_table_t dbuf_hash_table
;
322 * We use Cityhash for this. It's fast, and has good hash properties without
323 * requiring any large static buffers.
326 dbuf_hash(void *os
, uint64_t obj
, uint8_t lvl
, uint64_t blkid
)
328 return (cityhash4((uintptr_t)os
, obj
, (uint64_t)lvl
, blkid
));
331 #define DTRACE_SET_STATE(db, why) \
332 DTRACE_PROBE2(dbuf__state_change, dmu_buf_impl_t *, db, \
335 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
336 ((dbuf)->db.db_object == (obj) && \
337 (dbuf)->db_objset == (os) && \
338 (dbuf)->db_level == (level) && \
339 (dbuf)->db_blkid == (blkid))
342 dbuf_find(objset_t
*os
, uint64_t obj
, uint8_t level
, uint64_t blkid
,
345 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
350 hv
= dbuf_hash(os
, obj
, level
, blkid
);
351 idx
= hv
& h
->hash_table_mask
;
353 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
354 for (db
= h
->hash_table
[idx
]; db
!= NULL
; db
= db
->db_hash_next
) {
355 if (DBUF_EQUAL(db
, os
, obj
, level
, blkid
)) {
356 mutex_enter(&db
->db_mtx
);
357 if (db
->db_state
!= DB_EVICTING
) {
358 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
361 mutex_exit(&db
->db_mtx
);
364 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
365 if (hash_out
!= NULL
)
370 static dmu_buf_impl_t
*
371 dbuf_find_bonus(objset_t
*os
, uint64_t object
)
374 dmu_buf_impl_t
*db
= NULL
;
376 if (dnode_hold(os
, object
, FTAG
, &dn
) == 0) {
377 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
378 if (dn
->dn_bonus
!= NULL
) {
380 mutex_enter(&db
->db_mtx
);
382 rw_exit(&dn
->dn_struct_rwlock
);
383 dnode_rele(dn
, FTAG
);
389 * Insert an entry into the hash table. If there is already an element
390 * equal to elem in the hash table, then the already existing element
391 * will be returned and the new element will not be inserted.
392 * Otherwise returns NULL.
394 static dmu_buf_impl_t
*
395 dbuf_hash_insert(dmu_buf_impl_t
*db
)
397 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
398 objset_t
*os
= db
->db_objset
;
399 uint64_t obj
= db
->db
.db_object
;
400 int level
= db
->db_level
;
405 blkid
= db
->db_blkid
;
406 ASSERT3U(dbuf_hash(os
, obj
, level
, blkid
), ==, db
->db_hash
);
407 idx
= db
->db_hash
& h
->hash_table_mask
;
409 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
410 for (dbf
= h
->hash_table
[idx
], i
= 0; dbf
!= NULL
;
411 dbf
= dbf
->db_hash_next
, i
++) {
412 if (DBUF_EQUAL(dbf
, os
, obj
, level
, blkid
)) {
413 mutex_enter(&dbf
->db_mtx
);
414 if (dbf
->db_state
!= DB_EVICTING
) {
415 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
418 mutex_exit(&dbf
->db_mtx
);
423 DBUF_STAT_BUMP(hash_collisions
);
425 DBUF_STAT_BUMP(hash_chains
);
427 DBUF_STAT_MAX(hash_chain_max
, i
);
430 mutex_enter(&db
->db_mtx
);
431 db
->db_hash_next
= h
->hash_table
[idx
];
432 h
->hash_table
[idx
] = db
;
433 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
434 DBUF_STAT_BUMP(hash_elements
);
440 * This returns whether this dbuf should be stored in the metadata cache, which
441 * is based on whether it's from one of the dnode types that store data related
442 * to traversing dataset hierarchies.
445 dbuf_include_in_metadata_cache(dmu_buf_impl_t
*db
)
448 dmu_object_type_t type
= DB_DNODE(db
)->dn_type
;
451 /* Check if this dbuf is one of the types we care about */
452 if (DMU_OT_IS_METADATA_CACHED(type
)) {
453 /* If we hit this, then we set something up wrong in dmu_ot */
454 ASSERT(DMU_OT_IS_METADATA(type
));
457 * Sanity check for small-memory systems: don't allocate too
458 * much memory for this purpose.
460 if (zfs_refcount_count(
461 &dbuf_caches
[DB_DBUF_METADATA_CACHE
].size
) >
462 dbuf_metadata_cache_target_bytes()) {
463 DBUF_STAT_BUMP(metadata_cache_overflow
);
474 * Remove an entry from the hash table. It must be in the EVICTING state.
477 dbuf_hash_remove(dmu_buf_impl_t
*db
)
479 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
481 dmu_buf_impl_t
*dbf
, **dbp
;
483 ASSERT3U(dbuf_hash(db
->db_objset
, db
->db
.db_object
, db
->db_level
,
484 db
->db_blkid
), ==, db
->db_hash
);
485 idx
= db
->db_hash
& h
->hash_table_mask
;
488 * We mustn't hold db_mtx to maintain lock ordering:
489 * DBUF_HASH_MUTEX > db_mtx.
491 ASSERT(zfs_refcount_is_zero(&db
->db_holds
));
492 ASSERT(db
->db_state
== DB_EVICTING
);
493 ASSERT(!MUTEX_HELD(&db
->db_mtx
));
495 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
496 dbp
= &h
->hash_table
[idx
];
497 while ((dbf
= *dbp
) != db
) {
498 dbp
= &dbf
->db_hash_next
;
501 *dbp
= db
->db_hash_next
;
502 db
->db_hash_next
= NULL
;
503 if (h
->hash_table
[idx
] &&
504 h
->hash_table
[idx
]->db_hash_next
== NULL
)
505 DBUF_STAT_BUMPDOWN(hash_chains
);
506 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
507 DBUF_STAT_BUMPDOWN(hash_elements
);
513 } dbvu_verify_type_t
;
516 dbuf_verify_user(dmu_buf_impl_t
*db
, dbvu_verify_type_t verify_type
)
521 if (db
->db_user
== NULL
)
524 /* Only data blocks support the attachment of user data. */
525 ASSERT(db
->db_level
== 0);
527 /* Clients must resolve a dbuf before attaching user data. */
528 ASSERT(db
->db
.db_data
!= NULL
);
529 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
531 holds
= zfs_refcount_count(&db
->db_holds
);
532 if (verify_type
== DBVU_EVICTING
) {
534 * Immediate eviction occurs when holds == dirtycnt.
535 * For normal eviction buffers, holds is zero on
536 * eviction, except when dbuf_fix_old_data() calls
537 * dbuf_clear_data(). However, the hold count can grow
538 * during eviction even though db_mtx is held (see
539 * dmu_bonus_hold() for an example), so we can only
540 * test the generic invariant that holds >= dirtycnt.
542 ASSERT3U(holds
, >=, db
->db_dirtycnt
);
544 if (db
->db_user_immediate_evict
== TRUE
)
545 ASSERT3U(holds
, >=, db
->db_dirtycnt
);
547 ASSERT3U(holds
, >, 0);
553 dbuf_evict_user(dmu_buf_impl_t
*db
)
555 dmu_buf_user_t
*dbu
= db
->db_user
;
557 ASSERT(MUTEX_HELD(&db
->db_mtx
));
562 dbuf_verify_user(db
, DBVU_EVICTING
);
566 if (dbu
->dbu_clear_on_evict_dbufp
!= NULL
)
567 *dbu
->dbu_clear_on_evict_dbufp
= NULL
;
570 if (db
->db_caching_status
!= DB_NO_CACHE
) {
572 * This is a cached dbuf, so the size of the user data is
573 * included in its cached amount. We adjust it here because the
574 * user data has already been detached from the dbuf, and the
575 * sync functions are not supposed to touch it (the dbuf might
576 * not exist anymore by the time the sync functions run.
578 uint64_t size
= dbu
->dbu_size
;
579 (void) zfs_refcount_remove_many(
580 &dbuf_caches
[db
->db_caching_status
].size
, size
, dbu
);
581 if (db
->db_caching_status
== DB_DBUF_CACHE
)
582 DBUF_STAT_DECR(cache_levels_bytes
[db
->db_level
], size
);
586 * There are two eviction callbacks - one that we call synchronously
587 * and one that we invoke via a taskq. The async one is useful for
588 * avoiding lock order reversals and limiting stack depth.
590 * Note that if we have a sync callback but no async callback,
591 * it's likely that the sync callback will free the structure
592 * containing the dbu. In that case we need to take care to not
593 * dereference dbu after calling the sync evict func.
595 boolean_t has_async
= (dbu
->dbu_evict_func_async
!= NULL
);
597 if (dbu
->dbu_evict_func_sync
!= NULL
)
598 dbu
->dbu_evict_func_sync(dbu
);
601 taskq_dispatch_ent(dbu_evict_taskq
, dbu
->dbu_evict_func_async
,
602 dbu
, 0, &dbu
->dbu_tqent
);
607 dbuf_is_metadata(dmu_buf_impl_t
*db
)
610 * Consider indirect blocks and spill blocks to be meta data.
612 if (db
->db_level
> 0 || db
->db_blkid
== DMU_SPILL_BLKID
) {
615 boolean_t is_metadata
;
618 is_metadata
= DMU_OT_IS_METADATA(DB_DNODE(db
)->dn_type
);
621 return (is_metadata
);
626 * We want to exclude buffers that are on a special allocation class from
630 dbuf_is_l2cacheable(dmu_buf_impl_t
*db
, blkptr_t
*bp
)
632 if (db
->db_objset
->os_secondary_cache
== ZFS_CACHE_ALL
||
633 (db
->db_objset
->os_secondary_cache
==
634 ZFS_CACHE_METADATA
&& dbuf_is_metadata(db
))) {
635 if (l2arc_exclude_special
== 0)
639 * bp must be checked in the event it was passed from
640 * dbuf_read_impl() as the result of a the BP being set from
641 * a Direct I/O write in dbuf_read(). See comments in
644 blkptr_t
*db_bp
= bp
== NULL
? db
->db_blkptr
: bp
;
646 if (db_bp
== NULL
|| BP_IS_HOLE(db_bp
))
648 uint64_t vdev
= DVA_GET_VDEV(db_bp
->blk_dva
);
649 vdev_t
*rvd
= db
->db_objset
->os_spa
->spa_root_vdev
;
652 if (vdev
< rvd
->vdev_children
)
653 vd
= rvd
->vdev_child
[vdev
];
658 if (vd
->vdev_alloc_bias
!= VDEV_BIAS_SPECIAL
&&
659 vd
->vdev_alloc_bias
!= VDEV_BIAS_DEDUP
)
665 static inline boolean_t
666 dnode_level_is_l2cacheable(blkptr_t
*bp
, dnode_t
*dn
, int64_t level
)
668 if (dn
->dn_objset
->os_secondary_cache
== ZFS_CACHE_ALL
||
669 (dn
->dn_objset
->os_secondary_cache
== ZFS_CACHE_METADATA
&&
671 DMU_OT_IS_METADATA(dn
->dn_handle
->dnh_dnode
->dn_type
)))) {
672 if (l2arc_exclude_special
== 0)
675 if (bp
== NULL
|| BP_IS_HOLE(bp
))
677 uint64_t vdev
= DVA_GET_VDEV(bp
->blk_dva
);
678 vdev_t
*rvd
= dn
->dn_objset
->os_spa
->spa_root_vdev
;
681 if (vdev
< rvd
->vdev_children
)
682 vd
= rvd
->vdev_child
[vdev
];
687 if (vd
->vdev_alloc_bias
!= VDEV_BIAS_SPECIAL
&&
688 vd
->vdev_alloc_bias
!= VDEV_BIAS_DEDUP
)
696 * This function *must* return indices evenly distributed between all
697 * sublists of the multilist. This is needed due to how the dbuf eviction
698 * code is laid out; dbuf_evict_thread() assumes dbufs are evenly
699 * distributed between all sublists and uses this assumption when
700 * deciding which sublist to evict from and how much to evict from it.
703 dbuf_cache_multilist_index_func(multilist_t
*ml
, void *obj
)
705 dmu_buf_impl_t
*db
= obj
;
708 * The assumption here, is the hash value for a given
709 * dmu_buf_impl_t will remain constant throughout it's lifetime
710 * (i.e. it's objset, object, level and blkid fields don't change).
711 * Thus, we don't need to store the dbuf's sublist index
712 * on insertion, as this index can be recalculated on removal.
714 * Also, the low order bits of the hash value are thought to be
715 * distributed evenly. Otherwise, in the case that the multilist
716 * has a power of two number of sublists, each sublists' usage
717 * would not be evenly distributed. In this context full 64bit
718 * division would be a waste of time, so limit it to 32 bits.
720 return ((unsigned int)dbuf_hash(db
->db_objset
, db
->db
.db_object
,
721 db
->db_level
, db
->db_blkid
) %
722 multilist_get_num_sublists(ml
));
726 * The target size of the dbuf cache can grow with the ARC target,
727 * unless limited by the tunable dbuf_cache_max_bytes.
729 static inline unsigned long
730 dbuf_cache_target_bytes(void)
732 return (MIN(dbuf_cache_max_bytes
,
733 arc_target_bytes() >> dbuf_cache_shift
));
737 * The target size of the dbuf metadata cache can grow with the ARC target,
738 * unless limited by the tunable dbuf_metadata_cache_max_bytes.
740 static inline unsigned long
741 dbuf_metadata_cache_target_bytes(void)
743 return (MIN(dbuf_metadata_cache_max_bytes
,
744 arc_target_bytes() >> dbuf_metadata_cache_shift
));
747 static inline uint64_t
748 dbuf_cache_hiwater_bytes(void)
750 uint64_t dbuf_cache_target
= dbuf_cache_target_bytes();
751 return (dbuf_cache_target
+
752 (dbuf_cache_target
* dbuf_cache_hiwater_pct
) / 100);
755 static inline uint64_t
756 dbuf_cache_lowater_bytes(void)
758 uint64_t dbuf_cache_target
= dbuf_cache_target_bytes();
759 return (dbuf_cache_target
-
760 (dbuf_cache_target
* dbuf_cache_lowater_pct
) / 100);
763 static inline boolean_t
764 dbuf_cache_above_lowater(void)
766 return (zfs_refcount_count(&dbuf_caches
[DB_DBUF_CACHE
].size
) >
767 dbuf_cache_lowater_bytes());
771 * Evict the oldest eligible dbuf from the dbuf cache.
776 int idx
= multilist_get_random_index(&dbuf_caches
[DB_DBUF_CACHE
].cache
);
777 multilist_sublist_t
*mls
= multilist_sublist_lock_idx(
778 &dbuf_caches
[DB_DBUF_CACHE
].cache
, idx
);
780 ASSERT(!MUTEX_HELD(&dbuf_evict_lock
));
782 dmu_buf_impl_t
*db
= multilist_sublist_tail(mls
);
783 while (db
!= NULL
&& mutex_tryenter(&db
->db_mtx
) == 0) {
784 db
= multilist_sublist_prev(mls
, db
);
787 DTRACE_PROBE2(dbuf__evict__one
, dmu_buf_impl_t
*, db
,
788 multilist_sublist_t
*, mls
);
791 multilist_sublist_remove(mls
, db
);
792 multilist_sublist_unlock(mls
);
793 uint64_t size
= db
->db
.db_size
;
794 uint64_t usize
= dmu_buf_user_size(&db
->db
);
795 (void) zfs_refcount_remove_many(
796 &dbuf_caches
[DB_DBUF_CACHE
].size
, size
, db
);
797 (void) zfs_refcount_remove_many(
798 &dbuf_caches
[DB_DBUF_CACHE
].size
, usize
, db
->db_user
);
799 DBUF_STAT_BUMPDOWN(cache_levels
[db
->db_level
]);
800 DBUF_STAT_BUMPDOWN(cache_count
);
801 DBUF_STAT_DECR(cache_levels_bytes
[db
->db_level
], size
+ usize
);
802 ASSERT3U(db
->db_caching_status
, ==, DB_DBUF_CACHE
);
803 db
->db_caching_status
= DB_NO_CACHE
;
805 DBUF_STAT_BUMP(cache_total_evicts
);
807 multilist_sublist_unlock(mls
);
812 * The dbuf evict thread is responsible for aging out dbufs from the
813 * cache. Once the cache has reached it's maximum size, dbufs are removed
814 * and destroyed. The eviction thread will continue running until the size
815 * of the dbuf cache is at or below the maximum size. Once the dbuf is aged
816 * out of the cache it is destroyed and becomes eligible for arc eviction.
818 static __attribute__((noreturn
)) void
819 dbuf_evict_thread(void *unused
)
824 CALLB_CPR_INIT(&cpr
, &dbuf_evict_lock
, callb_generic_cpr
, FTAG
);
826 mutex_enter(&dbuf_evict_lock
);
827 while (!dbuf_evict_thread_exit
) {
828 while (!dbuf_cache_above_lowater() && !dbuf_evict_thread_exit
) {
829 CALLB_CPR_SAFE_BEGIN(&cpr
);
830 (void) cv_timedwait_idle_hires(&dbuf_evict_cv
,
831 &dbuf_evict_lock
, SEC2NSEC(1), MSEC2NSEC(1), 0);
832 CALLB_CPR_SAFE_END(&cpr
, &dbuf_evict_lock
);
834 mutex_exit(&dbuf_evict_lock
);
837 * Keep evicting as long as we're above the low water mark
838 * for the cache. We do this without holding the locks to
839 * minimize lock contention.
841 while (dbuf_cache_above_lowater() && !dbuf_evict_thread_exit
) {
845 mutex_enter(&dbuf_evict_lock
);
848 dbuf_evict_thread_exit
= B_FALSE
;
849 cv_broadcast(&dbuf_evict_cv
);
850 CALLB_CPR_EXIT(&cpr
); /* drops dbuf_evict_lock */
855 * Wake up the dbuf eviction thread if the dbuf cache is at its max size.
856 * If the dbuf cache is at its high water mark, then evict a dbuf from the
857 * dbuf cache using the caller's context.
860 dbuf_evict_notify(uint64_t size
)
863 * We check if we should evict without holding the dbuf_evict_lock,
864 * because it's OK to occasionally make the wrong decision here,
865 * and grabbing the lock results in massive lock contention.
867 if (size
> dbuf_cache_target_bytes()) {
868 if (size
> dbuf_cache_hiwater_bytes())
870 cv_signal(&dbuf_evict_cv
);
875 dbuf_kstat_update(kstat_t
*ksp
, int rw
)
877 dbuf_stats_t
*ds
= ksp
->ks_data
;
878 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
880 if (rw
== KSTAT_WRITE
)
881 return (SET_ERROR(EACCES
));
883 ds
->cache_count
.value
.ui64
=
884 wmsum_value(&dbuf_sums
.cache_count
);
885 ds
->cache_size_bytes
.value
.ui64
=
886 zfs_refcount_count(&dbuf_caches
[DB_DBUF_CACHE
].size
);
887 ds
->cache_target_bytes
.value
.ui64
= dbuf_cache_target_bytes();
888 ds
->cache_hiwater_bytes
.value
.ui64
= dbuf_cache_hiwater_bytes();
889 ds
->cache_lowater_bytes
.value
.ui64
= dbuf_cache_lowater_bytes();
890 ds
->cache_total_evicts
.value
.ui64
=
891 wmsum_value(&dbuf_sums
.cache_total_evicts
);
892 for (int i
= 0; i
< DN_MAX_LEVELS
; i
++) {
893 ds
->cache_levels
[i
].value
.ui64
=
894 wmsum_value(&dbuf_sums
.cache_levels
[i
]);
895 ds
->cache_levels_bytes
[i
].value
.ui64
=
896 wmsum_value(&dbuf_sums
.cache_levels_bytes
[i
]);
898 ds
->hash_hits
.value
.ui64
=
899 wmsum_value(&dbuf_sums
.hash_hits
);
900 ds
->hash_misses
.value
.ui64
=
901 wmsum_value(&dbuf_sums
.hash_misses
);
902 ds
->hash_collisions
.value
.ui64
=
903 wmsum_value(&dbuf_sums
.hash_collisions
);
904 ds
->hash_elements
.value
.ui64
=
905 wmsum_value(&dbuf_sums
.hash_elements
);
906 ds
->hash_chains
.value
.ui64
=
907 wmsum_value(&dbuf_sums
.hash_chains
);
908 ds
->hash_insert_race
.value
.ui64
=
909 wmsum_value(&dbuf_sums
.hash_insert_race
);
910 ds
->hash_table_count
.value
.ui64
= h
->hash_table_mask
+ 1;
911 ds
->hash_mutex_count
.value
.ui64
= h
->hash_mutex_mask
+ 1;
912 ds
->metadata_cache_count
.value
.ui64
=
913 wmsum_value(&dbuf_sums
.metadata_cache_count
);
914 ds
->metadata_cache_size_bytes
.value
.ui64
= zfs_refcount_count(
915 &dbuf_caches
[DB_DBUF_METADATA_CACHE
].size
);
916 ds
->metadata_cache_overflow
.value
.ui64
=
917 wmsum_value(&dbuf_sums
.metadata_cache_overflow
);
924 uint64_t hmsize
, hsize
= 1ULL << 16;
925 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
928 * The hash table is big enough to fill one eighth of physical memory
929 * with an average block size of zfs_arc_average_blocksize (default 8K).
930 * By default, the table will take up
931 * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers).
933 while (hsize
* zfs_arc_average_blocksize
< arc_all_memory() / 8)
936 h
->hash_table
= NULL
;
937 while (h
->hash_table
== NULL
) {
938 h
->hash_table_mask
= hsize
- 1;
940 h
->hash_table
= vmem_zalloc(hsize
* sizeof (void *), KM_SLEEP
);
941 if (h
->hash_table
== NULL
)
944 ASSERT3U(hsize
, >=, 1ULL << 10);
948 * The hash table buckets are protected by an array of mutexes where
949 * each mutex is reponsible for protecting 128 buckets. A minimum
950 * array size of 8192 is targeted to avoid contention.
952 if (dbuf_mutex_cache_shift
== 0)
953 hmsize
= MAX(hsize
>> 7, 1ULL << 13);
955 hmsize
= 1ULL << MIN(dbuf_mutex_cache_shift
, 24);
957 h
->hash_mutexes
= NULL
;
958 while (h
->hash_mutexes
== NULL
) {
959 h
->hash_mutex_mask
= hmsize
- 1;
961 h
->hash_mutexes
= vmem_zalloc(hmsize
* sizeof (kmutex_t
),
963 if (h
->hash_mutexes
== NULL
)
967 dbuf_kmem_cache
= kmem_cache_create("dmu_buf_impl_t",
968 sizeof (dmu_buf_impl_t
),
969 0, dbuf_cons
, dbuf_dest
, NULL
, NULL
, NULL
, 0);
970 dbuf_dirty_kmem_cache
= kmem_cache_create("dbuf_dirty_record_t",
971 sizeof (dbuf_dirty_record_t
), 0, NULL
, NULL
, NULL
, NULL
, NULL
, 0);
973 for (int i
= 0; i
< hmsize
; i
++)
974 mutex_init(&h
->hash_mutexes
[i
], NULL
, MUTEX_NOLOCKDEP
, NULL
);
979 * All entries are queued via taskq_dispatch_ent(), so min/maxalloc
980 * configuration is not required.
982 dbu_evict_taskq
= taskq_create("dbu_evict", 1, defclsyspri
, 0, 0, 0);
984 for (dbuf_cached_state_t dcs
= 0; dcs
< DB_CACHE_MAX
; dcs
++) {
985 multilist_create(&dbuf_caches
[dcs
].cache
,
986 sizeof (dmu_buf_impl_t
),
987 offsetof(dmu_buf_impl_t
, db_cache_link
),
988 dbuf_cache_multilist_index_func
);
989 zfs_refcount_create(&dbuf_caches
[dcs
].size
);
992 dbuf_evict_thread_exit
= B_FALSE
;
993 mutex_init(&dbuf_evict_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
994 cv_init(&dbuf_evict_cv
, NULL
, CV_DEFAULT
, NULL
);
995 dbuf_cache_evict_thread
= thread_create(NULL
, 0, dbuf_evict_thread
,
996 NULL
, 0, &p0
, TS_RUN
, minclsyspri
);
998 wmsum_init(&dbuf_sums
.cache_count
, 0);
999 wmsum_init(&dbuf_sums
.cache_total_evicts
, 0);
1000 for (int i
= 0; i
< DN_MAX_LEVELS
; i
++) {
1001 wmsum_init(&dbuf_sums
.cache_levels
[i
], 0);
1002 wmsum_init(&dbuf_sums
.cache_levels_bytes
[i
], 0);
1004 wmsum_init(&dbuf_sums
.hash_hits
, 0);
1005 wmsum_init(&dbuf_sums
.hash_misses
, 0);
1006 wmsum_init(&dbuf_sums
.hash_collisions
, 0);
1007 wmsum_init(&dbuf_sums
.hash_elements
, 0);
1008 wmsum_init(&dbuf_sums
.hash_chains
, 0);
1009 wmsum_init(&dbuf_sums
.hash_insert_race
, 0);
1010 wmsum_init(&dbuf_sums
.metadata_cache_count
, 0);
1011 wmsum_init(&dbuf_sums
.metadata_cache_overflow
, 0);
1013 dbuf_ksp
= kstat_create("zfs", 0, "dbufstats", "misc",
1014 KSTAT_TYPE_NAMED
, sizeof (dbuf_stats
) / sizeof (kstat_named_t
),
1015 KSTAT_FLAG_VIRTUAL
);
1016 if (dbuf_ksp
!= NULL
) {
1017 for (int i
= 0; i
< DN_MAX_LEVELS
; i
++) {
1018 snprintf(dbuf_stats
.cache_levels
[i
].name
,
1019 KSTAT_STRLEN
, "cache_level_%d", i
);
1020 dbuf_stats
.cache_levels
[i
].data_type
=
1022 snprintf(dbuf_stats
.cache_levels_bytes
[i
].name
,
1023 KSTAT_STRLEN
, "cache_level_%d_bytes", i
);
1024 dbuf_stats
.cache_levels_bytes
[i
].data_type
=
1027 dbuf_ksp
->ks_data
= &dbuf_stats
;
1028 dbuf_ksp
->ks_update
= dbuf_kstat_update
;
1029 kstat_install(dbuf_ksp
);
1036 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
1038 dbuf_stats_destroy();
1040 for (int i
= 0; i
< (h
->hash_mutex_mask
+ 1); i
++)
1041 mutex_destroy(&h
->hash_mutexes
[i
]);
1043 vmem_free(h
->hash_table
, (h
->hash_table_mask
+ 1) * sizeof (void *));
1044 vmem_free(h
->hash_mutexes
, (h
->hash_mutex_mask
+ 1) *
1047 kmem_cache_destroy(dbuf_kmem_cache
);
1048 kmem_cache_destroy(dbuf_dirty_kmem_cache
);
1049 taskq_destroy(dbu_evict_taskq
);
1051 mutex_enter(&dbuf_evict_lock
);
1052 dbuf_evict_thread_exit
= B_TRUE
;
1053 while (dbuf_evict_thread_exit
) {
1054 cv_signal(&dbuf_evict_cv
);
1055 cv_wait(&dbuf_evict_cv
, &dbuf_evict_lock
);
1057 mutex_exit(&dbuf_evict_lock
);
1059 mutex_destroy(&dbuf_evict_lock
);
1060 cv_destroy(&dbuf_evict_cv
);
1062 for (dbuf_cached_state_t dcs
= 0; dcs
< DB_CACHE_MAX
; dcs
++) {
1063 zfs_refcount_destroy(&dbuf_caches
[dcs
].size
);
1064 multilist_destroy(&dbuf_caches
[dcs
].cache
);
1067 if (dbuf_ksp
!= NULL
) {
1068 kstat_delete(dbuf_ksp
);
1072 wmsum_fini(&dbuf_sums
.cache_count
);
1073 wmsum_fini(&dbuf_sums
.cache_total_evicts
);
1074 for (int i
= 0; i
< DN_MAX_LEVELS
; i
++) {
1075 wmsum_fini(&dbuf_sums
.cache_levels
[i
]);
1076 wmsum_fini(&dbuf_sums
.cache_levels_bytes
[i
]);
1078 wmsum_fini(&dbuf_sums
.hash_hits
);
1079 wmsum_fini(&dbuf_sums
.hash_misses
);
1080 wmsum_fini(&dbuf_sums
.hash_collisions
);
1081 wmsum_fini(&dbuf_sums
.hash_elements
);
1082 wmsum_fini(&dbuf_sums
.hash_chains
);
1083 wmsum_fini(&dbuf_sums
.hash_insert_race
);
1084 wmsum_fini(&dbuf_sums
.metadata_cache_count
);
1085 wmsum_fini(&dbuf_sums
.metadata_cache_overflow
);
1094 dbuf_verify(dmu_buf_impl_t
*db
)
1097 dbuf_dirty_record_t
*dr
;
1100 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1102 if (!(zfs_flags
& ZFS_DEBUG_DBUF_VERIFY
))
1105 ASSERT(db
->db_objset
!= NULL
);
1109 ASSERT(db
->db_parent
== NULL
);
1110 ASSERT(db
->db_blkptr
== NULL
);
1112 ASSERT3U(db
->db
.db_object
, ==, dn
->dn_object
);
1113 ASSERT3P(db
->db_objset
, ==, dn
->dn_objset
);
1114 ASSERT3U(db
->db_level
, <, dn
->dn_nlevels
);
1115 ASSERT(db
->db_blkid
== DMU_BONUS_BLKID
||
1116 db
->db_blkid
== DMU_SPILL_BLKID
||
1117 !avl_is_empty(&dn
->dn_dbufs
));
1119 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1121 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
1122 ASSERT3U(db
->db
.db_offset
, ==, DMU_BONUS_BLKID
);
1123 } else if (db
->db_blkid
== DMU_SPILL_BLKID
) {
1125 ASSERT0(db
->db
.db_offset
);
1127 ASSERT3U(db
->db
.db_offset
, ==, db
->db_blkid
* db
->db
.db_size
);
1130 if ((dr
= list_head(&db
->db_dirty_records
)) != NULL
) {
1131 ASSERT(dr
->dr_dbuf
== db
);
1132 txg_prev
= dr
->dr_txg
;
1133 for (dr
= list_next(&db
->db_dirty_records
, dr
); dr
!= NULL
;
1134 dr
= list_next(&db
->db_dirty_records
, dr
)) {
1135 ASSERT(dr
->dr_dbuf
== db
);
1136 ASSERT(txg_prev
> dr
->dr_txg
);
1137 txg_prev
= dr
->dr_txg
;
1142 * We can't assert that db_size matches dn_datablksz because it
1143 * can be momentarily different when another thread is doing
1144 * dnode_set_blksz().
1146 if (db
->db_level
== 0 && db
->db
.db_object
== DMU_META_DNODE_OBJECT
) {
1147 dr
= db
->db_data_pending
;
1149 * It should only be modified in syncing context, so
1150 * make sure we only have one copy of the data.
1152 ASSERT(dr
== NULL
|| dr
->dt
.dl
.dr_data
== db
->db_buf
);
1155 /* verify db->db_blkptr */
1156 if (db
->db_blkptr
) {
1157 if (db
->db_parent
== dn
->dn_dbuf
) {
1158 /* db is pointed to by the dnode */
1159 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
1160 if (DMU_OBJECT_IS_SPECIAL(db
->db
.db_object
))
1161 ASSERT(db
->db_parent
== NULL
);
1163 ASSERT(db
->db_parent
!= NULL
);
1164 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
1165 ASSERT3P(db
->db_blkptr
, ==,
1166 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
1168 /* db is pointed to by an indirect block */
1169 int epb __maybe_unused
= db
->db_parent
->db
.db_size
>>
1171 ASSERT3U(db
->db_parent
->db_level
, ==, db
->db_level
+1);
1172 ASSERT3U(db
->db_parent
->db
.db_object
, ==,
1175 * dnode_grow_indblksz() can make this fail if we don't
1176 * have the parent's rwlock. XXX indblksz no longer
1177 * grows. safe to do this now?
1179 if (RW_LOCK_HELD(&db
->db_parent
->db_rwlock
)) {
1180 ASSERT3P(db
->db_blkptr
, ==,
1181 ((blkptr_t
*)db
->db_parent
->db
.db_data
+
1182 db
->db_blkid
% epb
));
1186 if ((db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
)) &&
1187 (db
->db_buf
== NULL
|| db
->db_buf
->b_data
) &&
1188 db
->db
.db_data
&& db
->db_blkid
!= DMU_BONUS_BLKID
&&
1189 db
->db_state
!= DB_FILL
&& (dn
== NULL
|| !dn
->dn_free_txg
)) {
1191 * If the blkptr isn't set but they have nonzero data,
1192 * it had better be dirty, otherwise we'll lose that
1193 * data when we evict this buffer.
1195 * There is an exception to this rule for indirect blocks; in
1196 * this case, if the indirect block is a hole, we fill in a few
1197 * fields on each of the child blocks (importantly, birth time)
1198 * to prevent hole birth times from being lost when you
1199 * partially fill in a hole.
1201 if (db
->db_dirtycnt
== 0) {
1202 if (db
->db_level
== 0) {
1203 uint64_t *buf
= db
->db
.db_data
;
1206 for (i
= 0; i
< db
->db
.db_size
>> 3; i
++) {
1207 ASSERT(buf
[i
] == 0);
1210 blkptr_t
*bps
= db
->db
.db_data
;
1211 ASSERT3U(1 << DB_DNODE(db
)->dn_indblkshift
, ==,
1214 * We want to verify that all the blkptrs in the
1215 * indirect block are holes, but we may have
1216 * automatically set up a few fields for them.
1217 * We iterate through each blkptr and verify
1218 * they only have those fields set.
1221 i
< db
->db
.db_size
/ sizeof (blkptr_t
);
1223 blkptr_t
*bp
= &bps
[i
];
1224 ASSERT(ZIO_CHECKSUM_IS_ZERO(
1227 DVA_IS_EMPTY(&bp
->blk_dva
[0]) &&
1228 DVA_IS_EMPTY(&bp
->blk_dva
[1]) &&
1229 DVA_IS_EMPTY(&bp
->blk_dva
[2]));
1230 ASSERT0(bp
->blk_fill
);
1231 ASSERT0(bp
->blk_pad
[0]);
1232 ASSERT0(bp
->blk_pad
[1]);
1233 ASSERT(!BP_IS_EMBEDDED(bp
));
1234 ASSERT(BP_IS_HOLE(bp
));
1235 ASSERT0(BP_GET_PHYSICAL_BIRTH(bp
));
1245 dbuf_clear_data(dmu_buf_impl_t
*db
)
1247 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1248 dbuf_evict_user(db
);
1249 ASSERT3P(db
->db_buf
, ==, NULL
);
1250 db
->db
.db_data
= NULL
;
1251 if (db
->db_state
!= DB_NOFILL
) {
1252 db
->db_state
= DB_UNCACHED
;
1253 DTRACE_SET_STATE(db
, "clear data");
1258 dbuf_set_data(dmu_buf_impl_t
*db
, arc_buf_t
*buf
)
1260 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1261 ASSERT(buf
!= NULL
);
1264 ASSERT(buf
->b_data
!= NULL
);
1265 db
->db
.db_data
= buf
->b_data
;
1269 dbuf_alloc_arcbuf(dmu_buf_impl_t
*db
)
1271 spa_t
*spa
= db
->db_objset
->os_spa
;
1273 return (arc_alloc_buf(spa
, db
, DBUF_GET_BUFC_TYPE(db
), db
->db
.db_size
));
1277 * Loan out an arc_buf for read. Return the loaned arc_buf.
1280 dbuf_loan_arcbuf(dmu_buf_impl_t
*db
)
1284 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1285 mutex_enter(&db
->db_mtx
);
1286 if (arc_released(db
->db_buf
) || zfs_refcount_count(&db
->db_holds
) > 1) {
1287 int blksz
= db
->db
.db_size
;
1288 spa_t
*spa
= db
->db_objset
->os_spa
;
1290 mutex_exit(&db
->db_mtx
);
1291 abuf
= arc_loan_buf(spa
, B_FALSE
, blksz
);
1292 memcpy(abuf
->b_data
, db
->db
.db_data
, blksz
);
1295 arc_loan_inuse_buf(abuf
, db
);
1297 dbuf_clear_data(db
);
1298 mutex_exit(&db
->db_mtx
);
1304 * Calculate which level n block references the data at the level 0 offset
1308 dbuf_whichblock(const dnode_t
*dn
, const int64_t level
, const uint64_t offset
)
1310 if (dn
->dn_datablkshift
!= 0 && dn
->dn_indblkshift
!= 0) {
1312 * The level n blkid is equal to the level 0 blkid divided by
1313 * the number of level 0s in a level n block.
1315 * The level 0 blkid is offset >> datablkshift =
1316 * offset / 2^datablkshift.
1318 * The number of level 0s in a level n is the number of block
1319 * pointers in an indirect block, raised to the power of level.
1320 * This is 2^(indblkshift - SPA_BLKPTRSHIFT)^level =
1321 * 2^(level*(indblkshift - SPA_BLKPTRSHIFT)).
1323 * Thus, the level n blkid is: offset /
1324 * ((2^datablkshift)*(2^(level*(indblkshift-SPA_BLKPTRSHIFT))))
1325 * = offset / 2^(datablkshift + level *
1326 * (indblkshift - SPA_BLKPTRSHIFT))
1327 * = offset >> (datablkshift + level *
1328 * (indblkshift - SPA_BLKPTRSHIFT))
1331 const unsigned exp
= dn
->dn_datablkshift
+
1332 level
* (dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
);
1334 if (exp
>= 8 * sizeof (offset
)) {
1335 /* This only happens on the highest indirection level */
1336 ASSERT3U(level
, ==, dn
->dn_nlevels
- 1);
1340 ASSERT3U(exp
, <, 8 * sizeof (offset
));
1342 return (offset
>> exp
);
1344 ASSERT3U(offset
, <, dn
->dn_datablksz
);
1350 * This function is used to lock the parent of the provided dbuf. This should be
1351 * used when modifying or reading db_blkptr.
1354 dmu_buf_lock_parent(dmu_buf_impl_t
*db
, krw_t rw
, const void *tag
)
1356 enum db_lock_type ret
= DLT_NONE
;
1357 if (db
->db_parent
!= NULL
) {
1358 rw_enter(&db
->db_parent
->db_rwlock
, rw
);
1360 } else if (dmu_objset_ds(db
->db_objset
) != NULL
) {
1361 rrw_enter(&dmu_objset_ds(db
->db_objset
)->ds_bp_rwlock
, rw
,
1366 * We only return a DLT_NONE lock when it's the top-most indirect block
1367 * of the meta-dnode of the MOS.
1373 * We need to pass the lock type in because it's possible that the block will
1374 * move from being the topmost indirect block in a dnode (and thus, have no
1375 * parent) to not the top-most via an indirection increase. This would cause a
1376 * panic if we didn't pass the lock type in.
1379 dmu_buf_unlock_parent(dmu_buf_impl_t
*db
, db_lock_type_t type
, const void *tag
)
1381 if (type
== DLT_PARENT
)
1382 rw_exit(&db
->db_parent
->db_rwlock
);
1383 else if (type
== DLT_OBJSET
)
1384 rrw_exit(&dmu_objset_ds(db
->db_objset
)->ds_bp_rwlock
, tag
);
1388 dbuf_read_done(zio_t
*zio
, const zbookmark_phys_t
*zb
, const blkptr_t
*bp
,
1389 arc_buf_t
*buf
, void *vdb
)
1391 (void) zb
, (void) bp
;
1392 dmu_buf_impl_t
*db
= vdb
;
1394 mutex_enter(&db
->db_mtx
);
1395 ASSERT3U(db
->db_state
, ==, DB_READ
);
1398 * All reads are synchronous, so we must have a hold on the dbuf
1400 ASSERT(zfs_refcount_count(&db
->db_holds
) > 0);
1401 ASSERT(db
->db_buf
== NULL
);
1402 ASSERT(db
->db
.db_data
== NULL
);
1405 ASSERT(zio
== NULL
|| zio
->io_error
!= 0);
1406 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1407 ASSERT3P(db
->db_buf
, ==, NULL
);
1408 db
->db_state
= DB_UNCACHED
;
1409 DTRACE_SET_STATE(db
, "i/o error");
1410 } else if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
1411 /* freed in flight */
1412 ASSERT(zio
== NULL
|| zio
->io_error
== 0);
1413 arc_release(buf
, db
);
1414 memset(buf
->b_data
, 0, db
->db
.db_size
);
1415 arc_buf_freeze(buf
);
1416 db
->db_freed_in_flight
= FALSE
;
1417 dbuf_set_data(db
, buf
);
1418 db
->db_state
= DB_CACHED
;
1419 DTRACE_SET_STATE(db
, "freed in flight");
1422 ASSERT(zio
== NULL
|| zio
->io_error
== 0);
1423 dbuf_set_data(db
, buf
);
1424 db
->db_state
= DB_CACHED
;
1425 DTRACE_SET_STATE(db
, "successful read");
1427 cv_broadcast(&db
->db_changed
);
1428 dbuf_rele_and_unlock(db
, NULL
, B_FALSE
);
1432 * Shortcut for performing reads on bonus dbufs. Returns
1433 * an error if we fail to verify the dnode associated with
1434 * a decrypted block. Otherwise success.
1437 dbuf_read_bonus(dmu_buf_impl_t
*db
, dnode_t
*dn
)
1439 int bonuslen
, max_bonuslen
;
1441 bonuslen
= MIN(dn
->dn_bonuslen
, dn
->dn_phys
->dn_bonuslen
);
1442 max_bonuslen
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
);
1443 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1444 ASSERT(DB_DNODE_HELD(db
));
1445 ASSERT3U(bonuslen
, <=, db
->db
.db_size
);
1446 db
->db
.db_data
= kmem_alloc(max_bonuslen
, KM_SLEEP
);
1447 arc_space_consume(max_bonuslen
, ARC_SPACE_BONUS
);
1448 if (bonuslen
< max_bonuslen
)
1449 memset(db
->db
.db_data
, 0, max_bonuslen
);
1451 memcpy(db
->db
.db_data
, DN_BONUS(dn
->dn_phys
), bonuslen
);
1452 db
->db_state
= DB_CACHED
;
1453 DTRACE_SET_STATE(db
, "bonus buffer filled");
1458 dbuf_handle_indirect_hole(dmu_buf_impl_t
*db
, dnode_t
*dn
, blkptr_t
*dbbp
)
1460 blkptr_t
*bps
= db
->db
.db_data
;
1461 uint32_t indbs
= 1ULL << dn
->dn_indblkshift
;
1462 int n_bps
= indbs
>> SPA_BLKPTRSHIFT
;
1464 for (int i
= 0; i
< n_bps
; i
++) {
1465 blkptr_t
*bp
= &bps
[i
];
1467 ASSERT3U(BP_GET_LSIZE(dbbp
), ==, indbs
);
1468 BP_SET_LSIZE(bp
, BP_GET_LEVEL(dbbp
) == 1 ?
1469 dn
->dn_datablksz
: BP_GET_LSIZE(dbbp
));
1470 BP_SET_TYPE(bp
, BP_GET_TYPE(dbbp
));
1471 BP_SET_LEVEL(bp
, BP_GET_LEVEL(dbbp
) - 1);
1472 BP_SET_BIRTH(bp
, BP_GET_LOGICAL_BIRTH(dbbp
), 0);
1477 * Handle reads on dbufs that are holes, if necessary. This function
1478 * requires that the dbuf's mutex is held. Returns success (0) if action
1479 * was taken, ENOENT if no action was taken.
1482 dbuf_read_hole(dmu_buf_impl_t
*db
, dnode_t
*dn
, blkptr_t
*bp
)
1484 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1486 int is_hole
= bp
== NULL
|| BP_IS_HOLE(bp
);
1488 * For level 0 blocks only, if the above check fails:
1489 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
1490 * processes the delete record and clears the bp while we are waiting
1491 * for the dn_mtx (resulting in a "no" from block_freed).
1493 if (!is_hole
&& db
->db_level
== 0)
1494 is_hole
= dnode_block_freed(dn
, db
->db_blkid
) || BP_IS_HOLE(bp
);
1497 dbuf_set_data(db
, dbuf_alloc_arcbuf(db
));
1498 memset(db
->db
.db_data
, 0, db
->db
.db_size
);
1500 if (bp
!= NULL
&& db
->db_level
> 0 && BP_IS_HOLE(bp
) &&
1501 BP_GET_LOGICAL_BIRTH(bp
) != 0) {
1502 dbuf_handle_indirect_hole(db
, dn
, bp
);
1504 db
->db_state
= DB_CACHED
;
1505 DTRACE_SET_STATE(db
, "hole read satisfied");
1512 * This function ensures that, when doing a decrypting read of a block,
1513 * we make sure we have decrypted the dnode associated with it. We must do
1514 * this so that we ensure we are fully authenticating the checksum-of-MACs
1515 * tree from the root of the objset down to this block. Indirect blocks are
1516 * always verified against their secure checksum-of-MACs assuming that the
1517 * dnode containing them is correct. Now that we are doing a decrypting read,
1518 * we can be sure that the key is loaded and verify that assumption. This is
1519 * especially important considering that we always read encrypted dnode
1520 * blocks as raw data (without verifying their MACs) to start, and
1521 * decrypt / authenticate them when we need to read an encrypted bonus buffer.
1524 dbuf_read_verify_dnode_crypt(dmu_buf_impl_t
*db
, dnode_t
*dn
, uint32_t flags
)
1526 objset_t
*os
= db
->db_objset
;
1527 dmu_buf_impl_t
*dndb
;
1529 zbookmark_phys_t zb
;
1532 if ((flags
& DB_RF_NO_DECRYPT
) != 0 ||
1533 !os
->os_encrypted
|| os
->os_raw_receive
||
1534 (dndb
= dn
->dn_dbuf
) == NULL
)
1537 dnbuf
= dndb
->db_buf
;
1538 if (!arc_is_encrypted(dnbuf
))
1541 mutex_enter(&dndb
->db_mtx
);
1544 * Since dnode buffer is modified by sync process, there can be only
1545 * one copy of it. It means we can not modify (decrypt) it while it
1546 * is being written. I don't see how this may happen now, since
1547 * encrypted dnode writes by receive should be completed before any
1548 * plain-text reads due to txg wait, but better be safe than sorry.
1551 if (!arc_is_encrypted(dnbuf
)) {
1552 mutex_exit(&dndb
->db_mtx
);
1555 dbuf_dirty_record_t
*dr
= dndb
->db_data_pending
;
1556 if (dr
== NULL
|| dr
->dt
.dl
.dr_data
!= dnbuf
)
1558 cv_wait(&dndb
->db_changed
, &dndb
->db_mtx
);
1561 SET_BOOKMARK(&zb
, dmu_objset_id(os
),
1562 DMU_META_DNODE_OBJECT
, 0, dndb
->db_blkid
);
1563 err
= arc_untransform(dnbuf
, os
->os_spa
, &zb
, B_TRUE
);
1566 * An error code of EACCES tells us that the key is still not
1567 * available. This is ok if we are only reading authenticated
1568 * (and therefore non-encrypted) blocks.
1570 if (err
== EACCES
&& ((db
->db_blkid
!= DMU_BONUS_BLKID
&&
1571 !DMU_OT_IS_ENCRYPTED(dn
->dn_type
)) ||
1572 (db
->db_blkid
== DMU_BONUS_BLKID
&&
1573 !DMU_OT_IS_ENCRYPTED(dn
->dn_bonustype
))))
1576 mutex_exit(&dndb
->db_mtx
);
1582 * Drops db_mtx and the parent lock specified by dblt and tag before
1586 dbuf_read_impl(dmu_buf_impl_t
*db
, dnode_t
*dn
, zio_t
*zio
, uint32_t flags
,
1587 db_lock_type_t dblt
, blkptr_t
*bp
, const void *tag
)
1589 zbookmark_phys_t zb
;
1590 uint32_t aflags
= ARC_FLAG_NOWAIT
;
1593 ASSERT(!zfs_refcount_is_zero(&db
->db_holds
));
1594 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1595 ASSERT(db
->db_state
== DB_UNCACHED
|| db
->db_state
== DB_NOFILL
);
1596 ASSERT(db
->db_buf
== NULL
);
1597 ASSERT(db
->db_parent
== NULL
||
1598 RW_LOCK_HELD(&db
->db_parent
->db_rwlock
));
1600 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1601 err
= dbuf_read_bonus(db
, dn
);
1605 err
= dbuf_read_hole(db
, dn
, bp
);
1612 * Any attempt to read a redacted block should result in an error. This
1613 * will never happen under normal conditions, but can be useful for
1614 * debugging purposes.
1616 if (BP_IS_REDACTED(bp
)) {
1617 ASSERT(dsl_dataset_feature_is_active(
1618 db
->db_objset
->os_dsl_dataset
,
1619 SPA_FEATURE_REDACTED_DATASETS
));
1620 err
= SET_ERROR(EIO
);
1624 SET_BOOKMARK(&zb
, dmu_objset_id(db
->db_objset
),
1625 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
1628 * All bps of an encrypted os should have the encryption bit set.
1629 * If this is not true it indicates tampering and we report an error.
1631 if (db
->db_objset
->os_encrypted
&& !BP_USES_CRYPT(bp
)) {
1632 spa_log_error(db
->db_objset
->os_spa
, &zb
,
1633 BP_GET_LOGICAL_BIRTH(bp
));
1634 err
= SET_ERROR(EIO
);
1638 db
->db_state
= DB_READ
;
1639 DTRACE_SET_STATE(db
, "read issued");
1640 mutex_exit(&db
->db_mtx
);
1642 if (!DBUF_IS_CACHEABLE(db
))
1643 aflags
|= ARC_FLAG_UNCACHED
;
1644 else if (dbuf_is_l2cacheable(db
, bp
))
1645 aflags
|= ARC_FLAG_L2CACHE
;
1647 dbuf_add_ref(db
, NULL
);
1649 zio_flags
= (flags
& DB_RF_CANFAIL
) ?
1650 ZIO_FLAG_CANFAIL
: ZIO_FLAG_MUSTSUCCEED
;
1652 if ((flags
& DB_RF_NO_DECRYPT
) && BP_IS_PROTECTED(bp
))
1653 zio_flags
|= ZIO_FLAG_RAW
;
1656 * The zio layer will copy the provided blkptr later, but we need to
1657 * do this now so that we can release the parent's rwlock. We have to
1658 * do that now so that if dbuf_read_done is called synchronously (on
1659 * an l1 cache hit) we don't acquire the db_mtx while holding the
1660 * parent's rwlock, which would be a lock ordering violation.
1662 blkptr_t copy
= *bp
;
1663 dmu_buf_unlock_parent(db
, dblt
, tag
);
1664 return (arc_read(zio
, db
->db_objset
->os_spa
, ©
,
1665 dbuf_read_done
, db
, ZIO_PRIORITY_SYNC_READ
, zio_flags
,
1669 mutex_exit(&db
->db_mtx
);
1670 dmu_buf_unlock_parent(db
, dblt
, tag
);
1675 * This is our just-in-time copy function. It makes a copy of buffers that
1676 * have been modified in a previous transaction group before we access them in
1677 * the current active group.
1679 * This function is used in three places: when we are dirtying a buffer for the
1680 * first time in a txg, when we are freeing a range in a dnode that includes
1681 * this buffer, and when we are accessing a buffer which was received compressed
1682 * and later referenced in a WRITE_BYREF record.
1684 * Note that when we are called from dbuf_free_range() we do not put a hold on
1685 * the buffer, we just traverse the active dbuf list for the dnode.
1688 dbuf_fix_old_data(dmu_buf_impl_t
*db
, uint64_t txg
)
1690 dbuf_dirty_record_t
*dr
= list_head(&db
->db_dirty_records
);
1692 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1693 ASSERT(db
->db
.db_data
!= NULL
);
1694 ASSERT(db
->db_level
== 0);
1695 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
);
1698 (dr
->dt
.dl
.dr_data
!=
1699 ((db
->db_blkid
== DMU_BONUS_BLKID
) ? db
->db
.db_data
: db
->db_buf
)))
1703 * If the last dirty record for this dbuf has not yet synced
1704 * and its referencing the dbuf data, either:
1705 * reset the reference to point to a new copy,
1706 * or (if there a no active holders)
1707 * just null out the current db_data pointer.
1709 ASSERT3U(dr
->dr_txg
, >=, txg
- 2);
1710 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1711 dnode_t
*dn
= DB_DNODE(db
);
1712 int bonuslen
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
);
1713 dr
->dt
.dl
.dr_data
= kmem_alloc(bonuslen
, KM_SLEEP
);
1714 arc_space_consume(bonuslen
, ARC_SPACE_BONUS
);
1715 memcpy(dr
->dt
.dl
.dr_data
, db
->db
.db_data
, bonuslen
);
1716 } else if (zfs_refcount_count(&db
->db_holds
) > db
->db_dirtycnt
) {
1717 dnode_t
*dn
= DB_DNODE(db
);
1718 int size
= arc_buf_size(db
->db_buf
);
1719 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1720 spa_t
*spa
= db
->db_objset
->os_spa
;
1721 enum zio_compress compress_type
=
1722 arc_get_compression(db
->db_buf
);
1723 uint8_t complevel
= arc_get_complevel(db
->db_buf
);
1725 if (arc_is_encrypted(db
->db_buf
)) {
1726 boolean_t byteorder
;
1727 uint8_t salt
[ZIO_DATA_SALT_LEN
];
1728 uint8_t iv
[ZIO_DATA_IV_LEN
];
1729 uint8_t mac
[ZIO_DATA_MAC_LEN
];
1731 arc_get_raw_params(db
->db_buf
, &byteorder
, salt
,
1733 dr
->dt
.dl
.dr_data
= arc_alloc_raw_buf(spa
, db
,
1734 dmu_objset_id(dn
->dn_objset
), byteorder
, salt
, iv
,
1735 mac
, dn
->dn_type
, size
, arc_buf_lsize(db
->db_buf
),
1736 compress_type
, complevel
);
1737 } else if (compress_type
!= ZIO_COMPRESS_OFF
) {
1738 ASSERT3U(type
, ==, ARC_BUFC_DATA
);
1739 dr
->dt
.dl
.dr_data
= arc_alloc_compressed_buf(spa
, db
,
1740 size
, arc_buf_lsize(db
->db_buf
), compress_type
,
1743 dr
->dt
.dl
.dr_data
= arc_alloc_buf(spa
, db
, type
, size
);
1745 memcpy(dr
->dt
.dl
.dr_data
->b_data
, db
->db
.db_data
, size
);
1748 dbuf_clear_data(db
);
1753 dbuf_read(dmu_buf_impl_t
*db
, zio_t
*pio
, uint32_t flags
)
1756 boolean_t miss
= B_TRUE
, need_wait
= B_FALSE
, prefetch
;
1759 ASSERT(!zfs_refcount_is_zero(&db
->db_holds
));
1765 * Ensure that this block's dnode has been decrypted if the caller
1766 * has requested decrypted data.
1768 err
= dbuf_read_verify_dnode_crypt(db
, dn
, flags
);
1772 prefetch
= db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1773 (flags
& DB_RF_NOPREFETCH
) == 0;
1775 mutex_enter(&db
->db_mtx
);
1776 if (flags
& DB_RF_PARTIAL_FIRST
)
1777 db
->db_partial_read
= B_TRUE
;
1778 else if (!(flags
& DB_RF_PARTIAL_MORE
))
1779 db
->db_partial_read
= B_FALSE
;
1780 miss
= (db
->db_state
!= DB_CACHED
);
1782 if (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
) {
1784 * Another reader came in while the dbuf was in flight between
1785 * UNCACHED and CACHED. Either a writer will finish filling
1786 * the buffer, sending the dbuf to CACHED, or the first reader's
1787 * request will reach the read_done callback and send the dbuf
1788 * to CACHED. Otherwise, a failure occurred and the dbuf will
1789 * be sent to UNCACHED.
1791 if (flags
& DB_RF_NEVERWAIT
) {
1792 mutex_exit(&db
->db_mtx
);
1797 ASSERT(db
->db_state
== DB_READ
||
1798 (flags
& DB_RF_HAVESTRUCT
) == 0);
1799 DTRACE_PROBE2(blocked__read
, dmu_buf_impl_t
*, db
,
1801 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1802 } while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
);
1803 if (db
->db_state
== DB_UNCACHED
) {
1804 err
= SET_ERROR(EIO
);
1805 mutex_exit(&db
->db_mtx
);
1811 if (db
->db_state
== DB_CACHED
) {
1813 * If the arc buf is compressed or encrypted and the caller
1814 * requested uncompressed data, we need to untransform it
1815 * before returning. We also call arc_untransform() on any
1816 * unauthenticated blocks, which will verify their MAC if
1817 * the key is now available.
1819 if ((flags
& DB_RF_NO_DECRYPT
) == 0 && db
->db_buf
!= NULL
&&
1820 (arc_is_encrypted(db
->db_buf
) ||
1821 arc_is_unauthenticated(db
->db_buf
) ||
1822 arc_get_compression(db
->db_buf
) != ZIO_COMPRESS_OFF
)) {
1823 spa_t
*spa
= dn
->dn_objset
->os_spa
;
1824 zbookmark_phys_t zb
;
1826 SET_BOOKMARK(&zb
, dmu_objset_id(db
->db_objset
),
1827 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
1828 dbuf_fix_old_data(db
, spa_syncing_txg(spa
));
1829 err
= arc_untransform(db
->db_buf
, spa
, &zb
, B_FALSE
);
1830 dbuf_set_data(db
, db
->db_buf
);
1832 mutex_exit(&db
->db_mtx
);
1834 ASSERT(db
->db_state
== DB_UNCACHED
||
1835 db
->db_state
== DB_NOFILL
);
1836 db_lock_type_t dblt
= dmu_buf_lock_parent(db
, RW_READER
, FTAG
);
1840 * If a block clone or Direct I/O write has occurred we will
1841 * get the dirty records overridden BP so we get the most
1844 err
= dmu_buf_get_bp_from_dbuf(db
, &bp
);
1847 if (pio
== NULL
&& (db
->db_state
== DB_NOFILL
||
1848 (bp
!= NULL
&& !BP_IS_HOLE(bp
)))) {
1849 spa_t
*spa
= dn
->dn_objset
->os_spa
;
1851 zio_root(spa
, NULL
, NULL
, ZIO_FLAG_CANFAIL
);
1856 dbuf_read_impl(db
, dn
, pio
, flags
, dblt
, bp
, FTAG
);
1858 mutex_exit(&db
->db_mtx
);
1859 dmu_buf_unlock_parent(db
, dblt
, FTAG
);
1861 /* dbuf_read_impl drops db_mtx and parent's rwlock. */
1862 miss
= (db
->db_state
!= DB_CACHED
);
1865 if (err
== 0 && prefetch
) {
1866 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
, miss
,
1867 flags
& DB_RF_HAVESTRUCT
);
1872 * If we created a zio we must execute it to avoid leaking it, even if
1873 * it isn't attached to any work due to an error in dbuf_read_impl().
1877 err
= zio_wait(pio
);
1879 (void) zio_wait(pio
);
1885 DBUF_STAT_BUMP(hash_misses
);
1887 DBUF_STAT_BUMP(hash_hits
);
1888 if (pio
&& err
!= 0) {
1889 zio_t
*zio
= zio_null(pio
, pio
->io_spa
, NULL
, NULL
, NULL
,
1891 zio
->io_error
= err
;
1899 dbuf_noread(dmu_buf_impl_t
*db
)
1901 ASSERT(!zfs_refcount_is_zero(&db
->db_holds
));
1902 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1903 mutex_enter(&db
->db_mtx
);
1904 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
1905 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1906 if (db
->db_state
== DB_UNCACHED
) {
1907 ASSERT(db
->db_buf
== NULL
);
1908 ASSERT(db
->db
.db_data
== NULL
);
1909 dbuf_set_data(db
, dbuf_alloc_arcbuf(db
));
1910 db
->db_state
= DB_FILL
;
1911 DTRACE_SET_STATE(db
, "assigning filled buffer");
1912 } else if (db
->db_state
== DB_NOFILL
) {
1913 dbuf_clear_data(db
);
1915 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
1917 mutex_exit(&db
->db_mtx
);
1921 dbuf_unoverride(dbuf_dirty_record_t
*dr
)
1923 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
1924 blkptr_t
*bp
= &dr
->dt
.dl
.dr_overridden_by
;
1925 uint64_t txg
= dr
->dr_txg
;
1927 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1930 * This assert is valid because dmu_sync() expects to be called by
1931 * a zilog's get_data while holding a range lock. This call only
1932 * comes from dbuf_dirty() callers who must also hold a range lock.
1934 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_IN_DMU_SYNC
);
1935 ASSERT(db
->db_level
== 0);
1937 if (db
->db_blkid
== DMU_BONUS_BLKID
||
1938 dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
)
1941 ASSERT(db
->db_data_pending
!= dr
);
1943 /* free this block */
1944 if (!BP_IS_HOLE(bp
) && !dr
->dt
.dl
.dr_nopwrite
)
1945 zio_free(db
->db_objset
->os_spa
, txg
, bp
);
1947 if (dr
->dt
.dl
.dr_brtwrite
|| dr
->dt
.dl
.dr_diowrite
) {
1948 ASSERT0P(dr
->dt
.dl
.dr_data
);
1949 dr
->dt
.dl
.dr_data
= db
->db_buf
;
1951 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
1952 dr
->dt
.dl
.dr_nopwrite
= B_FALSE
;
1953 dr
->dt
.dl
.dr_brtwrite
= B_FALSE
;
1954 dr
->dt
.dl
.dr_diowrite
= B_FALSE
;
1955 dr
->dt
.dl
.dr_has_raw_params
= B_FALSE
;
1958 * In the event that Direct I/O was used, we do not
1959 * need to release the buffer from the ARC.
1961 * Release the already-written buffer, so we leave it in
1962 * a consistent dirty state. Note that all callers are
1963 * modifying the buffer, so they will immediately do
1964 * another (redundant) arc_release(). Therefore, leave
1965 * the buf thawed to save the effort of freezing &
1966 * immediately re-thawing it.
1968 if (dr
->dt
.dl
.dr_data
)
1969 arc_release(dr
->dt
.dl
.dr_data
, db
);
1973 * Evict (if its unreferenced) or clear (if its referenced) any level-0
1974 * data blocks in the free range, so that any future readers will find
1978 dbuf_free_range(dnode_t
*dn
, uint64_t start_blkid
, uint64_t end_blkid
,
1981 dmu_buf_impl_t
*db_search
;
1982 dmu_buf_impl_t
*db
, *db_next
;
1983 uint64_t txg
= tx
->tx_txg
;
1985 dbuf_dirty_record_t
*dr
;
1987 if (end_blkid
> dn
->dn_maxblkid
&&
1988 !(start_blkid
== DMU_SPILL_BLKID
|| end_blkid
== DMU_SPILL_BLKID
))
1989 end_blkid
= dn
->dn_maxblkid
;
1990 dprintf_dnode(dn
, "start=%llu end=%llu\n", (u_longlong_t
)start_blkid
,
1991 (u_longlong_t
)end_blkid
);
1993 db_search
= kmem_alloc(sizeof (dmu_buf_impl_t
), KM_SLEEP
);
1994 db_search
->db_level
= 0;
1995 db_search
->db_blkid
= start_blkid
;
1996 db_search
->db_state
= DB_SEARCH
;
1998 mutex_enter(&dn
->dn_dbufs_mtx
);
1999 db
= avl_find(&dn
->dn_dbufs
, db_search
, &where
);
2000 ASSERT3P(db
, ==, NULL
);
2002 db
= avl_nearest(&dn
->dn_dbufs
, where
, AVL_AFTER
);
2004 for (; db
!= NULL
; db
= db_next
) {
2005 db_next
= AVL_NEXT(&dn
->dn_dbufs
, db
);
2006 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2008 if (db
->db_level
!= 0 || db
->db_blkid
> end_blkid
) {
2011 ASSERT3U(db
->db_blkid
, >=, start_blkid
);
2013 /* found a level 0 buffer in the range */
2014 mutex_enter(&db
->db_mtx
);
2015 if (dbuf_undirty(db
, tx
)) {
2016 /* mutex has been dropped and dbuf destroyed */
2020 if (db
->db_state
== DB_UNCACHED
||
2021 db
->db_state
== DB_NOFILL
||
2022 db
->db_state
== DB_EVICTING
) {
2023 ASSERT(db
->db
.db_data
== NULL
);
2024 mutex_exit(&db
->db_mtx
);
2027 if (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
) {
2028 /* will be handled in dbuf_read_done or dbuf_rele */
2029 db
->db_freed_in_flight
= TRUE
;
2030 mutex_exit(&db
->db_mtx
);
2033 if (zfs_refcount_count(&db
->db_holds
) == 0) {
2038 /* The dbuf is referenced */
2040 dr
= list_head(&db
->db_dirty_records
);
2042 if (dr
->dr_txg
== txg
) {
2044 * This buffer is "in-use", re-adjust the file
2045 * size to reflect that this buffer may
2046 * contain new data when we sync.
2048 if (db
->db_blkid
!= DMU_SPILL_BLKID
&&
2049 db
->db_blkid
> dn
->dn_maxblkid
)
2050 dn
->dn_maxblkid
= db
->db_blkid
;
2051 dbuf_unoverride(dr
);
2054 * This dbuf is not dirty in the open context.
2055 * Either uncache it (if its not referenced in
2056 * the open context) or reset its contents to
2059 dbuf_fix_old_data(db
, txg
);
2062 /* clear the contents if its cached */
2063 if (db
->db_state
== DB_CACHED
) {
2064 ASSERT(db
->db
.db_data
!= NULL
);
2065 arc_release(db
->db_buf
, db
);
2066 rw_enter(&db
->db_rwlock
, RW_WRITER
);
2067 memset(db
->db
.db_data
, 0, db
->db
.db_size
);
2068 rw_exit(&db
->db_rwlock
);
2069 arc_buf_freeze(db
->db_buf
);
2072 mutex_exit(&db
->db_mtx
);
2075 mutex_exit(&dn
->dn_dbufs_mtx
);
2076 kmem_free(db_search
, sizeof (dmu_buf_impl_t
));
2080 dbuf_new_size(dmu_buf_impl_t
*db
, int size
, dmu_tx_t
*tx
)
2082 arc_buf_t
*buf
, *old_buf
;
2083 dbuf_dirty_record_t
*dr
;
2084 int osize
= db
->db
.db_size
;
2085 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
2088 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2094 * XXX we should be doing a dbuf_read, checking the return
2095 * value and returning that up to our callers
2097 dmu_buf_will_dirty(&db
->db
, tx
);
2099 VERIFY3P(db
->db_buf
, !=, NULL
);
2101 /* create the data buffer for the new block */
2102 buf
= arc_alloc_buf(dn
->dn_objset
->os_spa
, db
, type
, size
);
2104 /* copy old block data to the new block */
2105 old_buf
= db
->db_buf
;
2106 memcpy(buf
->b_data
, old_buf
->b_data
, MIN(osize
, size
));
2107 /* zero the remainder */
2109 memset((uint8_t *)buf
->b_data
+ osize
, 0, size
- osize
);
2111 mutex_enter(&db
->db_mtx
);
2112 dbuf_set_data(db
, buf
);
2113 arc_buf_destroy(old_buf
, db
);
2114 db
->db
.db_size
= size
;
2116 dr
= list_head(&db
->db_dirty_records
);
2117 /* dirty record added by dmu_buf_will_dirty() */
2119 if (db
->db_level
== 0)
2120 dr
->dt
.dl
.dr_data
= buf
;
2121 ASSERT3U(dr
->dr_txg
, ==, tx
->tx_txg
);
2122 ASSERT3U(dr
->dr_accounted
, ==, osize
);
2123 dr
->dr_accounted
= size
;
2124 mutex_exit(&db
->db_mtx
);
2126 dmu_objset_willuse_space(dn
->dn_objset
, size
- osize
, tx
);
2131 dbuf_release_bp(dmu_buf_impl_t
*db
)
2133 objset_t
*os __maybe_unused
= db
->db_objset
;
2135 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os
)));
2136 ASSERT(arc_released(os
->os_phys_buf
) ||
2137 list_link_active(&os
->os_dsl_dataset
->ds_synced_link
));
2138 ASSERT(db
->db_parent
== NULL
|| arc_released(db
->db_parent
->db_buf
));
2140 (void) arc_release(db
->db_buf
, db
);
2144 * We already have a dirty record for this TXG, and we are being
2148 dbuf_redirty(dbuf_dirty_record_t
*dr
)
2150 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2152 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2154 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
) {
2156 * If this buffer has already been written out,
2157 * we now need to reset its state.
2159 dbuf_unoverride(dr
);
2160 if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
&&
2161 db
->db_state
!= DB_NOFILL
) {
2162 /* Already released on initial dirty, so just thaw. */
2163 ASSERT(arc_released(db
->db_buf
));
2164 arc_buf_thaw(db
->db_buf
);
2169 dbuf_dirty_record_t
*
2170 dbuf_dirty_lightweight(dnode_t
*dn
, uint64_t blkid
, dmu_tx_t
*tx
)
2172 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
2173 IMPLY(dn
->dn_objset
->os_raw_receive
, dn
->dn_maxblkid
>= blkid
);
2174 dnode_new_blkid(dn
, blkid
, tx
, B_TRUE
, B_FALSE
);
2175 ASSERT(dn
->dn_maxblkid
>= blkid
);
2177 dbuf_dirty_record_t
*dr
= kmem_zalloc(sizeof (*dr
), KM_SLEEP
);
2178 list_link_init(&dr
->dr_dirty_node
);
2179 list_link_init(&dr
->dr_dbuf_node
);
2181 dr
->dr_txg
= tx
->tx_txg
;
2182 dr
->dt
.dll
.dr_blkid
= blkid
;
2183 dr
->dr_accounted
= dn
->dn_datablksz
;
2186 * There should not be any dbuf for the block that we're dirtying.
2187 * Otherwise the buffer contents could be inconsistent between the
2188 * dbuf and the lightweight dirty record.
2190 ASSERT3P(NULL
, ==, dbuf_find(dn
->dn_objset
, dn
->dn_object
, 0, blkid
,
2193 mutex_enter(&dn
->dn_mtx
);
2194 int txgoff
= tx
->tx_txg
& TXG_MASK
;
2195 if (dn
->dn_free_ranges
[txgoff
] != NULL
) {
2196 range_tree_clear(dn
->dn_free_ranges
[txgoff
], blkid
, 1);
2199 if (dn
->dn_nlevels
== 1) {
2200 ASSERT3U(blkid
, <, dn
->dn_nblkptr
);
2201 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
2202 mutex_exit(&dn
->dn_mtx
);
2203 rw_exit(&dn
->dn_struct_rwlock
);
2204 dnode_setdirty(dn
, tx
);
2206 mutex_exit(&dn
->dn_mtx
);
2208 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2209 dmu_buf_impl_t
*parent_db
= dbuf_hold_level(dn
,
2210 1, blkid
>> epbs
, FTAG
);
2211 rw_exit(&dn
->dn_struct_rwlock
);
2212 if (parent_db
== NULL
) {
2213 kmem_free(dr
, sizeof (*dr
));
2216 int err
= dbuf_read(parent_db
, NULL
,
2217 (DB_RF_NOPREFETCH
| DB_RF_CANFAIL
));
2219 dbuf_rele(parent_db
, FTAG
);
2220 kmem_free(dr
, sizeof (*dr
));
2224 dbuf_dirty_record_t
*parent_dr
= dbuf_dirty(parent_db
, tx
);
2225 dbuf_rele(parent_db
, FTAG
);
2226 mutex_enter(&parent_dr
->dt
.di
.dr_mtx
);
2227 ASSERT3U(parent_dr
->dr_txg
, ==, tx
->tx_txg
);
2228 list_insert_tail(&parent_dr
->dt
.di
.dr_children
, dr
);
2229 mutex_exit(&parent_dr
->dt
.di
.dr_mtx
);
2230 dr
->dr_parent
= parent_dr
;
2233 dmu_objset_willuse_space(dn
->dn_objset
, dr
->dr_accounted
, tx
);
2238 dbuf_dirty_record_t
*
2239 dbuf_dirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
2243 dbuf_dirty_record_t
*dr
, *dr_next
, *dr_head
;
2244 int txgoff
= tx
->tx_txg
& TXG_MASK
;
2245 boolean_t drop_struct_rwlock
= B_FALSE
;
2247 ASSERT(tx
->tx_txg
!= 0);
2248 ASSERT(!zfs_refcount_is_zero(&db
->db_holds
));
2249 DMU_TX_DIRTY_BUF(tx
, db
);
2254 * Shouldn't dirty a regular buffer in syncing context. Private
2255 * objects may be dirtied in syncing context, but only if they
2256 * were already pre-dirtied in open context.
2259 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
) {
2260 rrw_enter(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
,
2263 ASSERT(!dmu_tx_is_syncing(tx
) ||
2264 BP_IS_HOLE(dn
->dn_objset
->os_rootbp
) ||
2265 DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
2266 dn
->dn_objset
->os_dsl_dataset
== NULL
);
2267 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
2268 rrw_exit(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
, FTAG
);
2271 * We make this assert for private objects as well, but after we
2272 * check if we're already dirty. They are allowed to re-dirty
2273 * in syncing context.
2275 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
2276 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
2277 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
2279 mutex_enter(&db
->db_mtx
);
2281 * XXX make this true for indirects too? The problem is that
2282 * transactions created with dmu_tx_create_assigned() from
2283 * syncing context don't bother holding ahead.
2285 ASSERT(db
->db_level
!= 0 ||
2286 db
->db_state
== DB_CACHED
|| db
->db_state
== DB_FILL
||
2287 db
->db_state
== DB_NOFILL
);
2289 mutex_enter(&dn
->dn_mtx
);
2290 dnode_set_dirtyctx(dn
, tx
, db
);
2291 if (tx
->tx_txg
> dn
->dn_dirty_txg
)
2292 dn
->dn_dirty_txg
= tx
->tx_txg
;
2293 mutex_exit(&dn
->dn_mtx
);
2295 if (db
->db_blkid
== DMU_SPILL_BLKID
)
2296 dn
->dn_have_spill
= B_TRUE
;
2299 * If this buffer is already dirty, we're done.
2301 dr_head
= list_head(&db
->db_dirty_records
);
2302 ASSERT(dr_head
== NULL
|| dr_head
->dr_txg
<= tx
->tx_txg
||
2303 db
->db
.db_object
== DMU_META_DNODE_OBJECT
);
2304 dr_next
= dbuf_find_dirty_lte(db
, tx
->tx_txg
);
2305 if (dr_next
&& dr_next
->dr_txg
== tx
->tx_txg
) {
2308 dbuf_redirty(dr_next
);
2309 mutex_exit(&db
->db_mtx
);
2314 * Only valid if not already dirty.
2316 ASSERT(dn
->dn_object
== 0 ||
2317 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
2318 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
2320 ASSERT3U(dn
->dn_nlevels
, >, db
->db_level
);
2323 * We should only be dirtying in syncing context if it's the
2324 * mos or we're initializing the os or it's a special object.
2325 * However, we are allowed to dirty in syncing context provided
2326 * we already dirtied it in open context. Hence we must make
2327 * this assertion only if we're not already dirty.
2330 VERIFY3U(tx
->tx_txg
, <=, spa_final_dirty_txg(os
->os_spa
));
2332 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
2333 rrw_enter(&os
->os_dsl_dataset
->ds_bp_rwlock
, RW_READER
, FTAG
);
2334 ASSERT(!dmu_tx_is_syncing(tx
) || DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
2335 os
->os_dsl_dataset
== NULL
|| BP_IS_HOLE(os
->os_rootbp
));
2336 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
2337 rrw_exit(&os
->os_dsl_dataset
->ds_bp_rwlock
, FTAG
);
2339 ASSERT(db
->db
.db_size
!= 0);
2341 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
2343 if (db
->db_blkid
!= DMU_BONUS_BLKID
&& db
->db_state
!= DB_NOFILL
) {
2344 dmu_objset_willuse_space(os
, db
->db
.db_size
, tx
);
2348 * If this buffer is dirty in an old transaction group we need
2349 * to make a copy of it so that the changes we make in this
2350 * transaction group won't leak out when we sync the older txg.
2352 dr
= kmem_cache_alloc(dbuf_dirty_kmem_cache
, KM_SLEEP
);
2353 memset(dr
, 0, sizeof (*dr
));
2354 list_link_init(&dr
->dr_dirty_node
);
2355 list_link_init(&dr
->dr_dbuf_node
);
2357 if (db
->db_level
== 0) {
2358 void *data_old
= db
->db_buf
;
2360 if (db
->db_state
!= DB_NOFILL
) {
2361 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2362 dbuf_fix_old_data(db
, tx
->tx_txg
);
2363 data_old
= db
->db
.db_data
;
2364 } else if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
) {
2366 * Release the data buffer from the cache so
2367 * that we can modify it without impacting
2368 * possible other users of this cached data
2369 * block. Note that indirect blocks and
2370 * private objects are not released until the
2371 * syncing state (since they are only modified
2374 arc_release(db
->db_buf
, db
);
2375 dbuf_fix_old_data(db
, tx
->tx_txg
);
2376 data_old
= db
->db_buf
;
2378 ASSERT(data_old
!= NULL
);
2380 dr
->dt
.dl
.dr_data
= data_old
;
2382 mutex_init(&dr
->dt
.di
.dr_mtx
, NULL
, MUTEX_NOLOCKDEP
, NULL
);
2383 list_create(&dr
->dt
.di
.dr_children
,
2384 sizeof (dbuf_dirty_record_t
),
2385 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
2387 if (db
->db_blkid
!= DMU_BONUS_BLKID
&& db
->db_state
!= DB_NOFILL
) {
2388 dr
->dr_accounted
= db
->db
.db_size
;
2391 dr
->dr_txg
= tx
->tx_txg
;
2392 list_insert_before(&db
->db_dirty_records
, dr_next
, dr
);
2395 * We could have been freed_in_flight between the dbuf_noread
2396 * and dbuf_dirty. We win, as though the dbuf_noread() had
2397 * happened after the free.
2399 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
2400 db
->db_blkid
!= DMU_SPILL_BLKID
) {
2401 mutex_enter(&dn
->dn_mtx
);
2402 if (dn
->dn_free_ranges
[txgoff
] != NULL
) {
2403 range_tree_clear(dn
->dn_free_ranges
[txgoff
],
2406 mutex_exit(&dn
->dn_mtx
);
2407 db
->db_freed_in_flight
= FALSE
;
2411 * This buffer is now part of this txg
2413 dbuf_add_ref(db
, (void *)(uintptr_t)tx
->tx_txg
);
2414 db
->db_dirtycnt
+= 1;
2415 ASSERT3U(db
->db_dirtycnt
, <=, 3);
2417 mutex_exit(&db
->db_mtx
);
2419 if (db
->db_blkid
== DMU_BONUS_BLKID
||
2420 db
->db_blkid
== DMU_SPILL_BLKID
) {
2421 mutex_enter(&dn
->dn_mtx
);
2422 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
2423 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
2424 mutex_exit(&dn
->dn_mtx
);
2425 dnode_setdirty(dn
, tx
);
2430 if (!RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
2431 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
2432 drop_struct_rwlock
= B_TRUE
;
2436 * If we are overwriting a dedup BP, then unless it is snapshotted,
2437 * when we get to syncing context we will need to decrement its
2438 * refcount in the DDT. Prefetch the relevant DDT block so that
2439 * syncing context won't have to wait for the i/o.
2441 if (db
->db_blkptr
!= NULL
) {
2442 db_lock_type_t dblt
= dmu_buf_lock_parent(db
, RW_READER
, FTAG
);
2443 ddt_prefetch(os
->os_spa
, db
->db_blkptr
);
2444 dmu_buf_unlock_parent(db
, dblt
, FTAG
);
2448 * We need to hold the dn_struct_rwlock to make this assertion,
2449 * because it protects dn_phys / dn_next_nlevels from changing.
2451 ASSERT((dn
->dn_phys
->dn_nlevels
== 0 && db
->db_level
== 0) ||
2452 dn
->dn_phys
->dn_nlevels
> db
->db_level
||
2453 dn
->dn_next_nlevels
[txgoff
] > db
->db_level
||
2454 dn
->dn_next_nlevels
[(tx
->tx_txg
-1) & TXG_MASK
] > db
->db_level
||
2455 dn
->dn_next_nlevels
[(tx
->tx_txg
-2) & TXG_MASK
] > db
->db_level
);
2458 if (db
->db_level
== 0) {
2459 ASSERT(!db
->db_objset
->os_raw_receive
||
2460 dn
->dn_maxblkid
>= db
->db_blkid
);
2461 dnode_new_blkid(dn
, db
->db_blkid
, tx
,
2462 drop_struct_rwlock
, B_FALSE
);
2463 ASSERT(dn
->dn_maxblkid
>= db
->db_blkid
);
2466 if (db
->db_level
+1 < dn
->dn_nlevels
) {
2467 dmu_buf_impl_t
*parent
= db
->db_parent
;
2468 dbuf_dirty_record_t
*di
;
2469 int parent_held
= FALSE
;
2471 if (db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
) {
2472 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2473 parent
= dbuf_hold_level(dn
, db
->db_level
+ 1,
2474 db
->db_blkid
>> epbs
, FTAG
);
2475 ASSERT(parent
!= NULL
);
2478 if (drop_struct_rwlock
)
2479 rw_exit(&dn
->dn_struct_rwlock
);
2480 ASSERT3U(db
->db_level
+ 1, ==, parent
->db_level
);
2481 di
= dbuf_dirty(parent
, tx
);
2483 dbuf_rele(parent
, FTAG
);
2485 mutex_enter(&db
->db_mtx
);
2487 * Since we've dropped the mutex, it's possible that
2488 * dbuf_undirty() might have changed this out from under us.
2490 if (list_head(&db
->db_dirty_records
) == dr
||
2491 dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
2492 mutex_enter(&di
->dt
.di
.dr_mtx
);
2493 ASSERT3U(di
->dr_txg
, ==, tx
->tx_txg
);
2494 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
2495 list_insert_tail(&di
->dt
.di
.dr_children
, dr
);
2496 mutex_exit(&di
->dt
.di
.dr_mtx
);
2499 mutex_exit(&db
->db_mtx
);
2501 ASSERT(db
->db_level
+ 1 == dn
->dn_nlevels
);
2502 ASSERT(db
->db_blkid
< dn
->dn_nblkptr
);
2503 ASSERT(db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
2504 mutex_enter(&dn
->dn_mtx
);
2505 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
2506 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
2507 mutex_exit(&dn
->dn_mtx
);
2508 if (drop_struct_rwlock
)
2509 rw_exit(&dn
->dn_struct_rwlock
);
2512 dnode_setdirty(dn
, tx
);
2518 dbuf_undirty_bonus(dbuf_dirty_record_t
*dr
)
2520 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2522 if (dr
->dt
.dl
.dr_data
!= db
->db
.db_data
) {
2523 struct dnode
*dn
= dr
->dr_dnode
;
2524 int max_bonuslen
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
);
2526 kmem_free(dr
->dt
.dl
.dr_data
, max_bonuslen
);
2527 arc_space_return(max_bonuslen
, ARC_SPACE_BONUS
);
2529 db
->db_data_pending
= NULL
;
2530 ASSERT(list_next(&db
->db_dirty_records
, dr
) == NULL
);
2531 list_remove(&db
->db_dirty_records
, dr
);
2532 if (dr
->dr_dbuf
->db_level
!= 0) {
2533 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
2534 list_destroy(&dr
->dt
.di
.dr_children
);
2536 kmem_cache_free(dbuf_dirty_kmem_cache
, dr
);
2537 ASSERT3U(db
->db_dirtycnt
, >, 0);
2538 db
->db_dirtycnt
-= 1;
2542 * Undirty a buffer in the transaction group referenced by the given
2543 * transaction. Return whether this evicted the dbuf.
2546 dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
2548 uint64_t txg
= tx
->tx_txg
;
2555 * Due to our use of dn_nlevels below, this can only be called
2556 * in open context, unless we are operating on the MOS.
2557 * From syncing context, dn_nlevels may be different from the
2558 * dn_nlevels used when dbuf was dirtied.
2560 ASSERT(db
->db_objset
==
2561 dmu_objset_pool(db
->db_objset
)->dp_meta_objset
||
2562 txg
!= spa_syncing_txg(dmu_objset_spa(db
->db_objset
)));
2563 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2564 ASSERT0(db
->db_level
);
2565 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2568 * If this buffer is not dirty, we're done.
2570 dbuf_dirty_record_t
*dr
= dbuf_find_dirty_eq(db
, txg
);
2573 ASSERT(dr
->dr_dbuf
== db
);
2575 brtwrite
= dr
->dt
.dl
.dr_brtwrite
;
2576 diowrite
= dr
->dt
.dl
.dr_diowrite
;
2578 ASSERT3B(diowrite
, ==, B_FALSE
);
2580 * We are freeing a block that we cloned in the same
2581 * transaction group.
2583 blkptr_t
*bp
= &dr
->dt
.dl
.dr_overridden_by
;
2584 if (!BP_IS_HOLE(bp
) && !BP_IS_EMBEDDED(bp
)) {
2585 brt_pending_remove(dmu_objset_spa(db
->db_objset
),
2590 dnode_t
*dn
= dr
->dr_dnode
;
2592 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
2594 ASSERT(db
->db
.db_size
!= 0);
2596 dsl_pool_undirty_space(dmu_objset_pool(dn
->dn_objset
),
2597 dr
->dr_accounted
, txg
);
2599 list_remove(&db
->db_dirty_records
, dr
);
2602 * Note that there are three places in dbuf_dirty()
2603 * where this dirty record may be put on a list.
2604 * Make sure to do a list_remove corresponding to
2605 * every one of those list_insert calls.
2607 if (dr
->dr_parent
) {
2608 mutex_enter(&dr
->dr_parent
->dt
.di
.dr_mtx
);
2609 list_remove(&dr
->dr_parent
->dt
.di
.dr_children
, dr
);
2610 mutex_exit(&dr
->dr_parent
->dt
.di
.dr_mtx
);
2611 } else if (db
->db_blkid
== DMU_SPILL_BLKID
||
2612 db
->db_level
+ 1 == dn
->dn_nlevels
) {
2613 ASSERT(db
->db_blkptr
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
2614 mutex_enter(&dn
->dn_mtx
);
2615 list_remove(&dn
->dn_dirty_records
[txg
& TXG_MASK
], dr
);
2616 mutex_exit(&dn
->dn_mtx
);
2619 if (db
->db_state
!= DB_NOFILL
&& !brtwrite
) {
2620 dbuf_unoverride(dr
);
2622 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
) {
2623 ASSERT(db
->db_buf
!= NULL
);
2624 ASSERT(dr
->dt
.dl
.dr_data
!= NULL
);
2625 arc_buf_destroy(dr
->dt
.dl
.dr_data
, db
);
2629 kmem_cache_free(dbuf_dirty_kmem_cache
, dr
);
2631 ASSERT(db
->db_dirtycnt
> 0);
2632 db
->db_dirtycnt
-= 1;
2634 if (zfs_refcount_remove(&db
->db_holds
, (void *)(uintptr_t)txg
) == 0) {
2635 ASSERT(db
->db_state
== DB_NOFILL
|| brtwrite
|| diowrite
||
2636 arc_released(db
->db_buf
));
2645 dmu_buf_will_dirty_impl(dmu_buf_t
*db_fake
, int flags
, dmu_tx_t
*tx
)
2647 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2648 boolean_t undirty
= B_FALSE
;
2650 ASSERT(tx
->tx_txg
!= 0);
2651 ASSERT(!zfs_refcount_is_zero(&db
->db_holds
));
2654 * Quick check for dirtiness to improve performance for some workloads
2655 * (e.g. file deletion with indirect blocks cached).
2657 mutex_enter(&db
->db_mtx
);
2658 if (db
->db_state
== DB_CACHED
|| db
->db_state
== DB_NOFILL
) {
2660 * It's possible that the dbuf is already dirty but not cached,
2661 * because there are some calls to dbuf_dirty() that don't
2662 * go through dmu_buf_will_dirty().
2664 dbuf_dirty_record_t
*dr
= dbuf_find_dirty_eq(db
, tx
->tx_txg
);
2666 if (db
->db_level
== 0 &&
2667 dr
->dt
.dl
.dr_brtwrite
) {
2669 * Block cloning: If we are dirtying a cloned
2670 * level 0 block, we cannot simply redirty it,
2671 * because this dr has no associated data.
2672 * We will go through a full undirtying below,
2673 * before dirtying it again.
2677 /* This dbuf is already dirty and cached. */
2679 mutex_exit(&db
->db_mtx
);
2684 mutex_exit(&db
->db_mtx
);
2687 if (RW_WRITE_HELD(&DB_DNODE(db
)->dn_struct_rwlock
))
2688 flags
|= DB_RF_HAVESTRUCT
;
2692 * Block cloning: Do the dbuf_read() before undirtying the dbuf, as we
2693 * want to make sure dbuf_read() will read the pending cloned block and
2694 * not the uderlying block that is being replaced. dbuf_undirty() will
2695 * do brt_pending_remove() before removing the dirty record.
2697 (void) dbuf_read(db
, NULL
, flags
);
2699 mutex_enter(&db
->db_mtx
);
2700 VERIFY(!dbuf_undirty(db
, tx
));
2701 mutex_exit(&db
->db_mtx
);
2703 (void) dbuf_dirty(db
, tx
);
2707 dmu_buf_will_dirty(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
2709 dmu_buf_will_dirty_impl(db_fake
,
2710 DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
, tx
);
2714 dmu_buf_is_dirty(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
2716 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2717 dbuf_dirty_record_t
*dr
;
2719 mutex_enter(&db
->db_mtx
);
2720 dr
= dbuf_find_dirty_eq(db
, tx
->tx_txg
);
2721 mutex_exit(&db
->db_mtx
);
2722 return (dr
!= NULL
);
2726 * Normally the db_blkptr points to the most recent on-disk content for the
2727 * dbuf (and anything newer will be cached in the dbuf). However, a pending
2728 * block clone or not yet synced Direct I/O write will have a dirty record BP
2729 * pointing to the most recent data.
2732 dmu_buf_get_bp_from_dbuf(dmu_buf_impl_t
*db
, blkptr_t
**bp
)
2734 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2737 if (db
->db_level
!= 0) {
2738 *bp
= db
->db_blkptr
;
2742 *bp
= db
->db_blkptr
;
2743 dbuf_dirty_record_t
*dr
= list_head(&db
->db_dirty_records
);
2744 if (dr
&& db
->db_state
== DB_NOFILL
) {
2746 if (!dr
->dt
.dl
.dr_brtwrite
)
2749 *bp
= &dr
->dt
.dl
.dr_overridden_by
;
2750 } else if (dr
&& db
->db_state
== DB_UNCACHED
) {
2751 /* Direct I/O write */
2752 if (dr
->dt
.dl
.dr_diowrite
)
2753 *bp
= &dr
->dt
.dl
.dr_overridden_by
;
2760 * Direct I/O reads can read directly from the ARC, but the data has
2761 * to be untransformed in order to copy it over into user pages.
2764 dmu_buf_untransform_direct(dmu_buf_impl_t
*db
, spa_t
*spa
)
2768 dnode_t
*dn
= DB_DNODE(db
);
2770 ASSERT3S(db
->db_state
, ==, DB_CACHED
);
2771 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2774 * Ensure that this block's dnode has been decrypted if
2775 * the caller has requested decrypted data.
2777 err
= dbuf_read_verify_dnode_crypt(db
, dn
, 0);
2780 * If the arc buf is compressed or encrypted and the caller
2781 * requested uncompressed data, we need to untransform it
2782 * before returning. We also call arc_untransform() on any
2783 * unauthenticated blocks, which will verify their MAC if
2784 * the key is now available.
2786 if (err
== 0 && db
->db_buf
!= NULL
&&
2787 (arc_is_encrypted(db
->db_buf
) ||
2788 arc_is_unauthenticated(db
->db_buf
) ||
2789 arc_get_compression(db
->db_buf
) != ZIO_COMPRESS_OFF
)) {
2790 zbookmark_phys_t zb
;
2792 SET_BOOKMARK(&zb
, dmu_objset_id(db
->db_objset
),
2793 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
2794 dbuf_fix_old_data(db
, spa_syncing_txg(spa
));
2795 err
= arc_untransform(db
->db_buf
, spa
, &zb
, B_FALSE
);
2796 dbuf_set_data(db
, db
->db_buf
);
2799 DBUF_STAT_BUMP(hash_hits
);
2805 dmu_buf_will_clone_or_dio(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
2808 * Block clones and Direct I/O writes always happen in open-context.
2810 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2811 ASSERT0(db
->db_level
);
2812 ASSERT(!dmu_tx_is_syncing(tx
));
2813 ASSERT0(db
->db_level
);
2814 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2815 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
);
2817 mutex_enter(&db
->db_mtx
);
2821 * We are going to clone or issue a Direct I/O write on this block, so
2822 * undirty modifications done to this block so far in this txg. This
2823 * includes writes and clones into this block.
2825 * If there dirty record associated with this txg from a previous Direct
2826 * I/O write then space accounting cleanup takes place. It is important
2827 * to go ahead free up the space accounting through dbuf_undirty() ->
2828 * dbuf_unoverride() -> zio_free(). Space accountiung for determining
2829 * if a write can occur in zfs_write() happens through dmu_tx_assign().
2830 * This can cause an issue with Direct I/O writes in the case of
2831 * overwriting the same block, because all DVA allocations are being
2832 * done in open-context. Constantly allowing Direct I/O overwrites to
2833 * the same block can exhaust the pools available space leading to
2834 * ENOSPC errors at the DVA allocation part of the ZIO pipeline, which
2835 * will eventually suspend the pool. By cleaning up sapce acccounting
2836 * now, the ENOSPC error can be avoided.
2838 * Since we are undirtying the record in open-context, we must have a
2839 * hold on the db, so it should never be evicted after calling
2842 VERIFY3B(dbuf_undirty(db
, tx
), ==, B_FALSE
);
2843 ASSERT0P(dbuf_find_dirty_eq(db
, tx
->tx_txg
));
2845 if (db
->db_buf
!= NULL
) {
2847 * If there is an associated ARC buffer with this dbuf we can
2848 * only destroy it if the previous dirty record does not
2851 dbuf_dirty_record_t
*dr
= list_head(&db
->db_dirty_records
);
2852 if (dr
== NULL
|| dr
->dt
.dl
.dr_data
!= db
->db_buf
)
2853 arc_buf_destroy(db
->db_buf
, db
);
2856 * Setting the dbuf's data pointers to NULL will force all
2857 * future reads down to the devices to get the most up to date
2858 * version of the data after a Direct I/O write has completed.
2861 dbuf_clear_data(db
);
2864 ASSERT3P(db
->db_buf
, ==, NULL
);
2865 ASSERT3P(db
->db
.db_data
, ==, NULL
);
2867 db
->db_state
= DB_NOFILL
;
2868 DTRACE_SET_STATE(db
,
2869 "allocating NOFILL buffer for clone or direct I/O write");
2872 mutex_exit(&db
->db_mtx
);
2875 (void) dbuf_dirty(db
, tx
);
2879 dmu_buf_will_not_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
2881 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2883 mutex_enter(&db
->db_mtx
);
2884 db
->db_state
= DB_NOFILL
;
2885 DTRACE_SET_STATE(db
, "allocating NOFILL buffer");
2886 mutex_exit(&db
->db_mtx
);
2889 (void) dbuf_dirty(db
, tx
);
2893 dmu_buf_will_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
, boolean_t canfail
)
2895 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2897 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2898 ASSERT(tx
->tx_txg
!= 0);
2899 ASSERT(db
->db_level
== 0);
2900 ASSERT(!zfs_refcount_is_zero(&db
->db_holds
));
2902 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
||
2903 dmu_tx_private_ok(tx
));
2905 mutex_enter(&db
->db_mtx
);
2906 dbuf_dirty_record_t
*dr
= dbuf_find_dirty_eq(db
, tx
->tx_txg
);
2907 if (db
->db_state
== DB_NOFILL
||
2908 (db
->db_state
== DB_UNCACHED
&& dr
&& dr
->dt
.dl
.dr_diowrite
)) {
2910 * If the fill can fail we should have a way to return back to
2911 * the cloned or Direct I/O write data.
2913 if (canfail
&& dr
) {
2914 mutex_exit(&db
->db_mtx
);
2915 dmu_buf_will_dirty(db_fake
, tx
);
2919 * Block cloning: We will be completely overwriting a block
2920 * cloned in this transaction group, so let's undirty the
2921 * pending clone and mark the block as uncached. This will be
2922 * as if the clone was never done.
2924 if (db
->db_state
== DB_NOFILL
) {
2925 VERIFY(!dbuf_undirty(db
, tx
));
2926 db
->db_state
= DB_UNCACHED
;
2929 mutex_exit(&db
->db_mtx
);
2932 (void) dbuf_dirty(db
, tx
);
2936 * This function is effectively the same as dmu_buf_will_dirty(), but
2937 * indicates the caller expects raw encrypted data in the db, and provides
2938 * the crypt params (byteorder, salt, iv, mac) which should be stored in the
2939 * blkptr_t when this dbuf is written. This is only used for blocks of
2940 * dnodes, during raw receive.
2943 dmu_buf_set_crypt_params(dmu_buf_t
*db_fake
, boolean_t byteorder
,
2944 const uint8_t *salt
, const uint8_t *iv
, const uint8_t *mac
, dmu_tx_t
*tx
)
2946 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2947 dbuf_dirty_record_t
*dr
;
2950 * dr_has_raw_params is only processed for blocks of dnodes
2951 * (see dbuf_sync_dnode_leaf_crypt()).
2953 ASSERT3U(db
->db
.db_object
, ==, DMU_META_DNODE_OBJECT
);
2954 ASSERT0(db
->db_level
);
2955 ASSERT(db
->db_objset
->os_raw_receive
);
2957 dmu_buf_will_dirty_impl(db_fake
,
2958 DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
| DB_RF_NO_DECRYPT
, tx
);
2960 dr
= dbuf_find_dirty_eq(db
, tx
->tx_txg
);
2962 ASSERT3P(dr
, !=, NULL
);
2963 ASSERT3U(dr
->dt
.dl
.dr_override_state
, ==, DR_NOT_OVERRIDDEN
);
2965 dr
->dt
.dl
.dr_has_raw_params
= B_TRUE
;
2966 dr
->dt
.dl
.dr_byteorder
= byteorder
;
2967 memcpy(dr
->dt
.dl
.dr_salt
, salt
, ZIO_DATA_SALT_LEN
);
2968 memcpy(dr
->dt
.dl
.dr_iv
, iv
, ZIO_DATA_IV_LEN
);
2969 memcpy(dr
->dt
.dl
.dr_mac
, mac
, ZIO_DATA_MAC_LEN
);
2973 dbuf_override_impl(dmu_buf_impl_t
*db
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
2975 struct dirty_leaf
*dl
;
2976 dbuf_dirty_record_t
*dr
;
2978 ASSERT3U(db
->db
.db_object
, !=, DMU_META_DNODE_OBJECT
);
2979 ASSERT0(db
->db_level
);
2981 dr
= list_head(&db
->db_dirty_records
);
2982 ASSERT3P(dr
, !=, NULL
);
2983 ASSERT3U(dr
->dr_txg
, ==, tx
->tx_txg
);
2985 ASSERT0(dl
->dr_has_raw_params
);
2986 dl
->dr_overridden_by
= *bp
;
2987 dl
->dr_override_state
= DR_OVERRIDDEN
;
2988 BP_SET_LOGICAL_BIRTH(&dl
->dr_overridden_by
, dr
->dr_txg
);
2992 dmu_buf_fill_done(dmu_buf_t
*dbuf
, dmu_tx_t
*tx
, boolean_t failed
)
2995 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbuf
;
2996 mutex_enter(&db
->db_mtx
);
2999 if (db
->db_state
== DB_FILL
) {
3000 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
3001 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
3002 /* we were freed while filling */
3003 /* XXX dbuf_undirty? */
3004 memset(db
->db
.db_data
, 0, db
->db
.db_size
);
3005 db
->db_freed_in_flight
= FALSE
;
3006 db
->db_state
= DB_CACHED
;
3007 DTRACE_SET_STATE(db
,
3008 "fill done handling freed in flight");
3010 } else if (failed
) {
3011 VERIFY(!dbuf_undirty(db
, tx
));
3012 arc_buf_destroy(db
->db_buf
, db
);
3014 dbuf_clear_data(db
);
3015 DTRACE_SET_STATE(db
, "fill failed");
3017 db
->db_state
= DB_CACHED
;
3018 DTRACE_SET_STATE(db
, "fill done");
3020 cv_broadcast(&db
->db_changed
);
3022 db
->db_state
= DB_CACHED
;
3025 mutex_exit(&db
->db_mtx
);
3030 dmu_buf_write_embedded(dmu_buf_t
*dbuf
, void *data
,
3031 bp_embedded_type_t etype
, enum zio_compress comp
,
3032 int uncompressed_size
, int compressed_size
, int byteorder
,
3035 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbuf
;
3036 struct dirty_leaf
*dl
;
3037 dmu_object_type_t type
;
3038 dbuf_dirty_record_t
*dr
;
3040 if (etype
== BP_EMBEDDED_TYPE_DATA
) {
3041 ASSERT(spa_feature_is_active(dmu_objset_spa(db
->db_objset
),
3042 SPA_FEATURE_EMBEDDED_DATA
));
3046 type
= DB_DNODE(db
)->dn_type
;
3049 ASSERT0(db
->db_level
);
3050 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
3052 dmu_buf_will_not_fill(dbuf
, tx
);
3054 dr
= list_head(&db
->db_dirty_records
);
3055 ASSERT3P(dr
, !=, NULL
);
3056 ASSERT3U(dr
->dr_txg
, ==, tx
->tx_txg
);
3058 ASSERT0(dl
->dr_has_raw_params
);
3059 encode_embedded_bp_compressed(&dl
->dr_overridden_by
,
3060 data
, comp
, uncompressed_size
, compressed_size
);
3061 BPE_SET_ETYPE(&dl
->dr_overridden_by
, etype
);
3062 BP_SET_TYPE(&dl
->dr_overridden_by
, type
);
3063 BP_SET_LEVEL(&dl
->dr_overridden_by
, 0);
3064 BP_SET_BYTEORDER(&dl
->dr_overridden_by
, byteorder
);
3066 dl
->dr_override_state
= DR_OVERRIDDEN
;
3067 BP_SET_LOGICAL_BIRTH(&dl
->dr_overridden_by
, dr
->dr_txg
);
3071 dmu_buf_redact(dmu_buf_t
*dbuf
, dmu_tx_t
*tx
)
3073 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbuf
;
3074 dmu_object_type_t type
;
3075 ASSERT(dsl_dataset_feature_is_active(db
->db_objset
->os_dsl_dataset
,
3076 SPA_FEATURE_REDACTED_DATASETS
));
3079 type
= DB_DNODE(db
)->dn_type
;
3082 ASSERT0(db
->db_level
);
3083 dmu_buf_will_not_fill(dbuf
, tx
);
3085 blkptr_t bp
= { { { {0} } } };
3086 BP_SET_TYPE(&bp
, type
);
3087 BP_SET_LEVEL(&bp
, 0);
3088 BP_SET_BIRTH(&bp
, tx
->tx_txg
, 0);
3089 BP_SET_REDACTED(&bp
);
3090 BPE_SET_LSIZE(&bp
, dbuf
->db_size
);
3092 dbuf_override_impl(db
, &bp
, tx
);
3096 * Directly assign a provided arc buf to a given dbuf if it's not referenced
3097 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
3100 dbuf_assign_arcbuf(dmu_buf_impl_t
*db
, arc_buf_t
*buf
, dmu_tx_t
*tx
)
3102 ASSERT(!zfs_refcount_is_zero(&db
->db_holds
));
3103 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
3104 ASSERT(db
->db_level
== 0);
3105 ASSERT3U(dbuf_is_metadata(db
), ==, arc_is_metadata(buf
));
3106 ASSERT(buf
!= NULL
);
3107 ASSERT3U(arc_buf_lsize(buf
), ==, db
->db
.db_size
);
3108 ASSERT(tx
->tx_txg
!= 0);
3110 arc_return_buf(buf
, db
);
3111 ASSERT(arc_released(buf
));
3113 mutex_enter(&db
->db_mtx
);
3115 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
3116 cv_wait(&db
->db_changed
, &db
->db_mtx
);
3118 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_UNCACHED
||
3119 db
->db_state
== DB_NOFILL
);
3121 if (db
->db_state
== DB_CACHED
&&
3122 zfs_refcount_count(&db
->db_holds
) - 1 > db
->db_dirtycnt
) {
3124 * In practice, we will never have a case where we have an
3125 * encrypted arc buffer while additional holds exist on the
3126 * dbuf. We don't handle this here so we simply assert that
3129 ASSERT(!arc_is_encrypted(buf
));
3130 mutex_exit(&db
->db_mtx
);
3131 (void) dbuf_dirty(db
, tx
);
3132 memcpy(db
->db
.db_data
, buf
->b_data
, db
->db
.db_size
);
3133 arc_buf_destroy(buf
, db
);
3137 if (db
->db_state
== DB_CACHED
) {
3138 dbuf_dirty_record_t
*dr
= list_head(&db
->db_dirty_records
);
3140 ASSERT(db
->db_buf
!= NULL
);
3141 if (dr
!= NULL
&& dr
->dr_txg
== tx
->tx_txg
) {
3142 ASSERT(dr
->dt
.dl
.dr_data
== db
->db_buf
);
3144 if (!arc_released(db
->db_buf
)) {
3145 ASSERT(dr
->dt
.dl
.dr_override_state
==
3147 arc_release(db
->db_buf
, db
);
3149 dr
->dt
.dl
.dr_data
= buf
;
3150 arc_buf_destroy(db
->db_buf
, db
);
3151 } else if (dr
== NULL
|| dr
->dt
.dl
.dr_data
!= db
->db_buf
) {
3152 arc_release(db
->db_buf
, db
);
3153 arc_buf_destroy(db
->db_buf
, db
);
3156 } else if (db
->db_state
== DB_NOFILL
) {
3158 * We will be completely replacing the cloned block. In case
3159 * it was cloned in this transaction group, let's undirty the
3160 * pending clone and mark the block as uncached. This will be
3161 * as if the clone was never done.
3163 VERIFY(!dbuf_undirty(db
, tx
));
3164 db
->db_state
= DB_UNCACHED
;
3166 ASSERT(db
->db_buf
== NULL
);
3167 dbuf_set_data(db
, buf
);
3168 db
->db_state
= DB_FILL
;
3169 DTRACE_SET_STATE(db
, "filling assigned arcbuf");
3170 mutex_exit(&db
->db_mtx
);
3171 (void) dbuf_dirty(db
, tx
);
3172 dmu_buf_fill_done(&db
->db
, tx
, B_FALSE
);
3176 dbuf_destroy(dmu_buf_impl_t
*db
)
3179 dmu_buf_impl_t
*parent
= db
->db_parent
;
3180 dmu_buf_impl_t
*dndb
;
3182 ASSERT(MUTEX_HELD(&db
->db_mtx
));
3183 ASSERT(zfs_refcount_is_zero(&db
->db_holds
));
3185 if (db
->db_buf
!= NULL
) {
3186 arc_buf_destroy(db
->db_buf
, db
);
3190 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
3191 int slots
= DB_DNODE(db
)->dn_num_slots
;
3192 int bonuslen
= DN_SLOTS_TO_BONUSLEN(slots
);
3193 if (db
->db
.db_data
!= NULL
) {
3194 kmem_free(db
->db
.db_data
, bonuslen
);
3195 arc_space_return(bonuslen
, ARC_SPACE_BONUS
);
3196 db
->db_state
= DB_UNCACHED
;
3197 DTRACE_SET_STATE(db
, "buffer cleared");
3201 dbuf_clear_data(db
);
3203 if (multilist_link_active(&db
->db_cache_link
)) {
3204 ASSERT(db
->db_caching_status
== DB_DBUF_CACHE
||
3205 db
->db_caching_status
== DB_DBUF_METADATA_CACHE
);
3207 multilist_remove(&dbuf_caches
[db
->db_caching_status
].cache
, db
);
3209 ASSERT0(dmu_buf_user_size(&db
->db
));
3210 (void) zfs_refcount_remove_many(
3211 &dbuf_caches
[db
->db_caching_status
].size
,
3212 db
->db
.db_size
, db
);
3214 if (db
->db_caching_status
== DB_DBUF_METADATA_CACHE
) {
3215 DBUF_STAT_BUMPDOWN(metadata_cache_count
);
3217 DBUF_STAT_BUMPDOWN(cache_levels
[db
->db_level
]);
3218 DBUF_STAT_BUMPDOWN(cache_count
);
3219 DBUF_STAT_DECR(cache_levels_bytes
[db
->db_level
],
3222 db
->db_caching_status
= DB_NO_CACHE
;
3225 ASSERT(db
->db_state
== DB_UNCACHED
|| db
->db_state
== DB_NOFILL
);
3226 ASSERT(db
->db_data_pending
== NULL
);
3227 ASSERT(list_is_empty(&db
->db_dirty_records
));
3229 db
->db_state
= DB_EVICTING
;
3230 DTRACE_SET_STATE(db
, "buffer eviction started");
3231 db
->db_blkptr
= NULL
;
3234 * Now that db_state is DB_EVICTING, nobody else can find this via
3235 * the hash table. We can now drop db_mtx, which allows us to
3236 * acquire the dn_dbufs_mtx.
3238 mutex_exit(&db
->db_mtx
);
3243 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
3244 boolean_t needlock
= !MUTEX_HELD(&dn
->dn_dbufs_mtx
);
3246 mutex_enter_nested(&dn
->dn_dbufs_mtx
,
3248 avl_remove(&dn
->dn_dbufs
, db
);
3252 mutex_exit(&dn
->dn_dbufs_mtx
);
3254 * Decrementing the dbuf count means that the hold corresponding
3255 * to the removed dbuf is no longer discounted in dnode_move(),
3256 * so the dnode cannot be moved until after we release the hold.
3257 * The membar_producer() ensures visibility of the decremented
3258 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
3261 mutex_enter(&dn
->dn_mtx
);
3262 dnode_rele_and_unlock(dn
, db
, B_TRUE
);
3263 #ifdef USE_DNODE_HANDLE
3264 db
->db_dnode_handle
= NULL
;
3266 db
->db_dnode
= NULL
;
3269 dbuf_hash_remove(db
);
3274 ASSERT(zfs_refcount_is_zero(&db
->db_holds
));
3276 db
->db_parent
= NULL
;
3278 ASSERT(db
->db_buf
== NULL
);
3279 ASSERT(db
->db
.db_data
== NULL
);
3280 ASSERT(db
->db_hash_next
== NULL
);
3281 ASSERT(db
->db_blkptr
== NULL
);
3282 ASSERT(db
->db_data_pending
== NULL
);
3283 ASSERT3U(db
->db_caching_status
, ==, DB_NO_CACHE
);
3284 ASSERT(!multilist_link_active(&db
->db_cache_link
));
3287 * If this dbuf is referenced from an indirect dbuf,
3288 * decrement the ref count on the indirect dbuf.
3290 if (parent
&& parent
!= dndb
) {
3291 mutex_enter(&parent
->db_mtx
);
3292 dbuf_rele_and_unlock(parent
, db
, B_TRUE
);
3295 kmem_cache_free(dbuf_kmem_cache
, db
);
3296 arc_space_return(sizeof (dmu_buf_impl_t
), ARC_SPACE_DBUF
);
3300 * Note: While bpp will always be updated if the function returns success,
3301 * parentp will not be updated if the dnode does not have dn_dbuf filled in;
3302 * this happens when the dnode is the meta-dnode, or {user|group|project}used
3305 __attribute__((always_inline
))
3307 dbuf_findbp(dnode_t
*dn
, int level
, uint64_t blkid
, int fail_sparse
,
3308 dmu_buf_impl_t
**parentp
, blkptr_t
**bpp
)
3313 ASSERT(blkid
!= DMU_BONUS_BLKID
);
3315 if (blkid
== DMU_SPILL_BLKID
) {
3316 mutex_enter(&dn
->dn_mtx
);
3317 if (dn
->dn_have_spill
&&
3318 (dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
))
3319 *bpp
= DN_SPILL_BLKPTR(dn
->dn_phys
);
3322 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
3323 *parentp
= dn
->dn_dbuf
;
3324 mutex_exit(&dn
->dn_mtx
);
3329 (dn
->dn_phys
->dn_nlevels
== 0) ? 1 : dn
->dn_phys
->dn_nlevels
;
3330 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
3332 ASSERT3U(level
* epbs
, <, 64);
3333 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
3335 * This assertion shouldn't trip as long as the max indirect block size
3336 * is less than 1M. The reason for this is that up to that point,
3337 * the number of levels required to address an entire object with blocks
3338 * of size SPA_MINBLOCKSIZE satisfies nlevels * epbs + 1 <= 64. In
3339 * other words, if N * epbs + 1 > 64, then if (N-1) * epbs + 1 > 55
3340 * (i.e. we can address the entire object), objects will all use at most
3341 * N-1 levels and the assertion won't overflow. However, once epbs is
3342 * 13, 4 * 13 + 1 = 53, but 5 * 13 + 1 = 66. Then, 4 levels will not be
3343 * enough to address an entire object, so objects will have 5 levels,
3344 * but then this assertion will overflow.
3346 * All this is to say that if we ever increase DN_MAX_INDBLKSHIFT, we
3347 * need to redo this logic to handle overflows.
3349 ASSERT(level
>= nlevels
||
3350 ((nlevels
- level
- 1) * epbs
) +
3351 highbit64(dn
->dn_phys
->dn_nblkptr
) <= 64);
3352 if (level
>= nlevels
||
3353 blkid
>= ((uint64_t)dn
->dn_phys
->dn_nblkptr
<<
3354 ((nlevels
- level
- 1) * epbs
)) ||
3356 blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))) {
3357 /* the buffer has no parent yet */
3358 return (SET_ERROR(ENOENT
));
3359 } else if (level
< nlevels
-1) {
3360 /* this block is referenced from an indirect block */
3363 err
= dbuf_hold_impl(dn
, level
+ 1,
3364 blkid
>> epbs
, fail_sparse
, FALSE
, NULL
, parentp
);
3368 err
= dbuf_read(*parentp
, NULL
,
3369 (DB_RF_HAVESTRUCT
| DB_RF_NOPREFETCH
| DB_RF_CANFAIL
));
3371 dbuf_rele(*parentp
, NULL
);
3375 rw_enter(&(*parentp
)->db_rwlock
, RW_READER
);
3376 *bpp
= ((blkptr_t
*)(*parentp
)->db
.db_data
) +
3377 (blkid
& ((1ULL << epbs
) - 1));
3378 if (blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))
3379 ASSERT(BP_IS_HOLE(*bpp
));
3380 rw_exit(&(*parentp
)->db_rwlock
);
3383 /* the block is referenced from the dnode */
3384 ASSERT3U(level
, ==, nlevels
-1);
3385 ASSERT(dn
->dn_phys
->dn_nblkptr
== 0 ||
3386 blkid
< dn
->dn_phys
->dn_nblkptr
);
3388 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
3389 *parentp
= dn
->dn_dbuf
;
3391 *bpp
= &dn
->dn_phys
->dn_blkptr
[blkid
];
3396 static dmu_buf_impl_t
*
3397 dbuf_create(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
3398 dmu_buf_impl_t
*parent
, blkptr_t
*blkptr
, uint64_t hash
)
3400 objset_t
*os
= dn
->dn_objset
;
3401 dmu_buf_impl_t
*db
, *odb
;
3403 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
3404 ASSERT(dn
->dn_type
!= DMU_OT_NONE
);
3406 db
= kmem_cache_alloc(dbuf_kmem_cache
, KM_SLEEP
);
3408 list_create(&db
->db_dirty_records
, sizeof (dbuf_dirty_record_t
),
3409 offsetof(dbuf_dirty_record_t
, dr_dbuf_node
));
3412 db
->db
.db_object
= dn
->dn_object
;
3413 db
->db_level
= level
;
3414 db
->db_blkid
= blkid
;
3415 db
->db_dirtycnt
= 0;
3416 #ifdef USE_DNODE_HANDLE
3417 db
->db_dnode_handle
= dn
->dn_handle
;
3421 db
->db_parent
= parent
;
3422 db
->db_blkptr
= blkptr
;
3426 db
->db_user_immediate_evict
= FALSE
;
3427 db
->db_freed_in_flight
= FALSE
;
3428 db
->db_pending_evict
= FALSE
;
3430 if (blkid
== DMU_BONUS_BLKID
) {
3431 ASSERT3P(parent
, ==, dn
->dn_dbuf
);
3432 db
->db
.db_size
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
) -
3433 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
);
3434 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
3435 db
->db
.db_offset
= DMU_BONUS_BLKID
;
3436 db
->db_state
= DB_UNCACHED
;
3437 DTRACE_SET_STATE(db
, "bonus buffer created");
3438 db
->db_caching_status
= DB_NO_CACHE
;
3439 /* the bonus dbuf is not placed in the hash table */
3440 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_DBUF
);
3442 } else if (blkid
== DMU_SPILL_BLKID
) {
3443 db
->db
.db_size
= (blkptr
!= NULL
) ?
3444 BP_GET_LSIZE(blkptr
) : SPA_MINBLOCKSIZE
;
3445 db
->db
.db_offset
= 0;
3448 db
->db_level
? 1 << dn
->dn_indblkshift
: dn
->dn_datablksz
;
3449 db
->db
.db_size
= blocksize
;
3450 db
->db
.db_offset
= db
->db_blkid
* blocksize
;
3454 * Hold the dn_dbufs_mtx while we get the new dbuf
3455 * in the hash table *and* added to the dbufs list.
3456 * This prevents a possible deadlock with someone
3457 * trying to look up this dbuf before it's added to the
3460 mutex_enter(&dn
->dn_dbufs_mtx
);
3461 db
->db_state
= DB_EVICTING
; /* not worth logging this state change */
3462 if ((odb
= dbuf_hash_insert(db
)) != NULL
) {
3463 /* someone else inserted it first */
3464 mutex_exit(&dn
->dn_dbufs_mtx
);
3465 kmem_cache_free(dbuf_kmem_cache
, db
);
3466 DBUF_STAT_BUMP(hash_insert_race
);
3469 avl_add(&dn
->dn_dbufs
, db
);
3471 db
->db_state
= DB_UNCACHED
;
3472 DTRACE_SET_STATE(db
, "regular buffer created");
3473 db
->db_caching_status
= DB_NO_CACHE
;
3474 mutex_exit(&dn
->dn_dbufs_mtx
);
3475 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_DBUF
);
3477 if (parent
&& parent
!= dn
->dn_dbuf
)
3478 dbuf_add_ref(parent
, db
);
3480 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
3481 zfs_refcount_count(&dn
->dn_holds
) > 0);
3482 (void) zfs_refcount_add(&dn
->dn_holds
, db
);
3484 dprintf_dbuf(db
, "db=%p\n", db
);
3490 * This function returns a block pointer and information about the object,
3491 * given a dnode and a block. This is a publicly accessible version of
3492 * dbuf_findbp that only returns some information, rather than the
3493 * dbuf. Note that the dnode passed in must be held, and the dn_struct_rwlock
3494 * should be locked as (at least) a reader.
3497 dbuf_dnode_findbp(dnode_t
*dn
, uint64_t level
, uint64_t blkid
,
3498 blkptr_t
*bp
, uint16_t *datablkszsec
, uint8_t *indblkshift
)
3500 dmu_buf_impl_t
*dbp
= NULL
;
3503 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
3505 err
= dbuf_findbp(dn
, level
, blkid
, B_FALSE
, &dbp
, &bp2
);
3507 ASSERT3P(bp2
, !=, NULL
);
3510 dbuf_rele(dbp
, NULL
);
3511 if (datablkszsec
!= NULL
)
3512 *datablkszsec
= dn
->dn_phys
->dn_datablkszsec
;
3513 if (indblkshift
!= NULL
)
3514 *indblkshift
= dn
->dn_phys
->dn_indblkshift
;
3520 typedef struct dbuf_prefetch_arg
{
3521 spa_t
*dpa_spa
; /* The spa to issue the prefetch in. */
3522 zbookmark_phys_t dpa_zb
; /* The target block to prefetch. */
3523 int dpa_epbs
; /* Entries (blkptr_t's) Per Block Shift. */
3524 int dpa_curlevel
; /* The current level that we're reading */
3525 dnode_t
*dpa_dnode
; /* The dnode associated with the prefetch */
3526 zio_priority_t dpa_prio
; /* The priority I/Os should be issued at. */
3527 zio_t
*dpa_zio
; /* The parent zio_t for all prefetches. */
3528 arc_flags_t dpa_aflags
; /* Flags to pass to the final prefetch. */
3529 dbuf_prefetch_fn dpa_cb
; /* prefetch completion callback */
3530 void *dpa_arg
; /* prefetch completion arg */
3531 } dbuf_prefetch_arg_t
;
3534 dbuf_prefetch_fini(dbuf_prefetch_arg_t
*dpa
, boolean_t io_done
)
3536 if (dpa
->dpa_cb
!= NULL
) {
3537 dpa
->dpa_cb(dpa
->dpa_arg
, dpa
->dpa_zb
.zb_level
,
3538 dpa
->dpa_zb
.zb_blkid
, io_done
);
3540 kmem_free(dpa
, sizeof (*dpa
));
3544 dbuf_issue_final_prefetch_done(zio_t
*zio
, const zbookmark_phys_t
*zb
,
3545 const blkptr_t
*iobp
, arc_buf_t
*abuf
, void *private)
3547 (void) zio
, (void) zb
, (void) iobp
;
3548 dbuf_prefetch_arg_t
*dpa
= private;
3551 arc_buf_destroy(abuf
, private);
3553 dbuf_prefetch_fini(dpa
, B_TRUE
);
3557 * Actually issue the prefetch read for the block given.
3560 dbuf_issue_final_prefetch(dbuf_prefetch_arg_t
*dpa
, blkptr_t
*bp
)
3562 ASSERT(!BP_IS_REDACTED(bp
) ||
3563 dsl_dataset_feature_is_active(
3564 dpa
->dpa_dnode
->dn_objset
->os_dsl_dataset
,
3565 SPA_FEATURE_REDACTED_DATASETS
));
3567 if (BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
) || BP_IS_REDACTED(bp
))
3568 return (dbuf_prefetch_fini(dpa
, B_FALSE
));
3570 int zio_flags
= ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
;
3571 arc_flags_t aflags
=
3572 dpa
->dpa_aflags
| ARC_FLAG_NOWAIT
| ARC_FLAG_PREFETCH
|
3575 /* dnodes are always read as raw and then converted later */
3576 if (BP_GET_TYPE(bp
) == DMU_OT_DNODE
&& BP_IS_PROTECTED(bp
) &&
3577 dpa
->dpa_curlevel
== 0)
3578 zio_flags
|= ZIO_FLAG_RAW
;
3580 ASSERT3U(dpa
->dpa_curlevel
, ==, BP_GET_LEVEL(bp
));
3581 ASSERT3U(dpa
->dpa_curlevel
, ==, dpa
->dpa_zb
.zb_level
);
3582 ASSERT(dpa
->dpa_zio
!= NULL
);
3583 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
, bp
,
3584 dbuf_issue_final_prefetch_done
, dpa
,
3585 dpa
->dpa_prio
, zio_flags
, &aflags
, &dpa
->dpa_zb
);
3589 * Called when an indirect block above our prefetch target is read in. This
3590 * will either read in the next indirect block down the tree or issue the actual
3591 * prefetch if the next block down is our target.
3594 dbuf_prefetch_indirect_done(zio_t
*zio
, const zbookmark_phys_t
*zb
,
3595 const blkptr_t
*iobp
, arc_buf_t
*abuf
, void *private)
3597 (void) zb
, (void) iobp
;
3598 dbuf_prefetch_arg_t
*dpa
= private;
3600 ASSERT3S(dpa
->dpa_zb
.zb_level
, <, dpa
->dpa_curlevel
);
3601 ASSERT3S(dpa
->dpa_curlevel
, >, 0);
3604 ASSERT(zio
== NULL
|| zio
->io_error
!= 0);
3605 dbuf_prefetch_fini(dpa
, B_TRUE
);
3608 ASSERT(zio
== NULL
|| zio
->io_error
== 0);
3611 * The dpa_dnode is only valid if we are called with a NULL
3612 * zio. This indicates that the arc_read() returned without
3613 * first calling zio_read() to issue a physical read. Once
3614 * a physical read is made the dpa_dnode must be invalidated
3615 * as the locks guarding it may have been dropped. If the
3616 * dpa_dnode is still valid, then we want to add it to the dbuf
3617 * cache. To do so, we must hold the dbuf associated with the block
3618 * we just prefetched, read its contents so that we associate it
3619 * with an arc_buf_t, and then release it.
3622 ASSERT3S(BP_GET_LEVEL(zio
->io_bp
), ==, dpa
->dpa_curlevel
);
3623 if (zio
->io_flags
& ZIO_FLAG_RAW_COMPRESS
) {
3624 ASSERT3U(BP_GET_PSIZE(zio
->io_bp
), ==, zio
->io_size
);
3626 ASSERT3U(BP_GET_LSIZE(zio
->io_bp
), ==, zio
->io_size
);
3628 ASSERT3P(zio
->io_spa
, ==, dpa
->dpa_spa
);
3630 dpa
->dpa_dnode
= NULL
;
3631 } else if (dpa
->dpa_dnode
!= NULL
) {
3632 uint64_t curblkid
= dpa
->dpa_zb
.zb_blkid
>>
3633 (dpa
->dpa_epbs
* (dpa
->dpa_curlevel
-
3634 dpa
->dpa_zb
.zb_level
));
3635 dmu_buf_impl_t
*db
= dbuf_hold_level(dpa
->dpa_dnode
,
3636 dpa
->dpa_curlevel
, curblkid
, FTAG
);
3638 arc_buf_destroy(abuf
, private);
3639 dbuf_prefetch_fini(dpa
, B_TRUE
);
3642 (void) dbuf_read(db
, NULL
,
3643 DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
| DB_RF_HAVESTRUCT
);
3644 dbuf_rele(db
, FTAG
);
3647 dpa
->dpa_curlevel
--;
3648 uint64_t nextblkid
= dpa
->dpa_zb
.zb_blkid
>>
3649 (dpa
->dpa_epbs
* (dpa
->dpa_curlevel
- dpa
->dpa_zb
.zb_level
));
3650 blkptr_t
*bp
= ((blkptr_t
*)abuf
->b_data
) +
3651 P2PHASE(nextblkid
, 1ULL << dpa
->dpa_epbs
);
3653 ASSERT(!BP_IS_REDACTED(bp
) || (dpa
->dpa_dnode
&&
3654 dsl_dataset_feature_is_active(
3655 dpa
->dpa_dnode
->dn_objset
->os_dsl_dataset
,
3656 SPA_FEATURE_REDACTED_DATASETS
)));
3657 if (BP_IS_HOLE(bp
) || BP_IS_REDACTED(bp
)) {
3658 arc_buf_destroy(abuf
, private);
3659 dbuf_prefetch_fini(dpa
, B_TRUE
);
3661 } else if (dpa
->dpa_curlevel
== dpa
->dpa_zb
.zb_level
) {
3662 ASSERT3U(nextblkid
, ==, dpa
->dpa_zb
.zb_blkid
);
3663 dbuf_issue_final_prefetch(dpa
, bp
);
3665 arc_flags_t iter_aflags
= ARC_FLAG_NOWAIT
;
3666 zbookmark_phys_t zb
;
3668 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
3669 if (dpa
->dpa_aflags
& ARC_FLAG_L2CACHE
)
3670 iter_aflags
|= ARC_FLAG_L2CACHE
;
3672 ASSERT3U(dpa
->dpa_curlevel
, ==, BP_GET_LEVEL(bp
));
3674 SET_BOOKMARK(&zb
, dpa
->dpa_zb
.zb_objset
,
3675 dpa
->dpa_zb
.zb_object
, dpa
->dpa_curlevel
, nextblkid
);
3677 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
,
3678 bp
, dbuf_prefetch_indirect_done
, dpa
,
3679 ZIO_PRIORITY_SYNC_READ
,
3680 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
3684 arc_buf_destroy(abuf
, private);
3688 * Issue prefetch reads for the given block on the given level. If the indirect
3689 * blocks above that block are not in memory, we will read them in
3690 * asynchronously. As a result, this call never blocks waiting for a read to
3691 * complete. Note that the prefetch might fail if the dataset is encrypted and
3692 * the encryption key is unmapped before the IO completes.
3695 dbuf_prefetch_impl(dnode_t
*dn
, int64_t level
, uint64_t blkid
,
3696 zio_priority_t prio
, arc_flags_t aflags
, dbuf_prefetch_fn cb
,
3700 int epbs
, nlevels
, curlevel
;
3703 ASSERT(blkid
!= DMU_BONUS_BLKID
);
3704 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
3706 if (blkid
> dn
->dn_maxblkid
)
3709 if (level
== 0 && dnode_block_freed(dn
, blkid
))
3713 * This dnode hasn't been written to disk yet, so there's nothing to
3716 nlevels
= dn
->dn_phys
->dn_nlevels
;
3717 if (level
>= nlevels
|| dn
->dn_phys
->dn_nblkptr
== 0)
3720 epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
3721 if (dn
->dn_phys
->dn_maxblkid
< blkid
<< (epbs
* level
))
3724 dmu_buf_impl_t
*db
= dbuf_find(dn
->dn_objset
, dn
->dn_object
,
3725 level
, blkid
, NULL
);
3727 mutex_exit(&db
->db_mtx
);
3729 * This dbuf already exists. It is either CACHED, or
3730 * (we assume) about to be read or filled.
3736 * Find the closest ancestor (indirect block) of the target block
3737 * that is present in the cache. In this indirect block, we will
3738 * find the bp that is at curlevel, curblkid.
3742 while (curlevel
< nlevels
- 1) {
3743 int parent_level
= curlevel
+ 1;
3744 uint64_t parent_blkid
= curblkid
>> epbs
;
3747 if (dbuf_hold_impl(dn
, parent_level
, parent_blkid
,
3748 FALSE
, TRUE
, FTAG
, &db
) == 0) {
3749 blkptr_t
*bpp
= db
->db_buf
->b_data
;
3750 bp
= bpp
[P2PHASE(curblkid
, 1 << epbs
)];
3751 dbuf_rele(db
, FTAG
);
3755 curlevel
= parent_level
;
3756 curblkid
= parent_blkid
;
3759 if (curlevel
== nlevels
- 1) {
3760 /* No cached indirect blocks found. */
3761 ASSERT3U(curblkid
, <, dn
->dn_phys
->dn_nblkptr
);
3762 bp
= dn
->dn_phys
->dn_blkptr
[curblkid
];
3764 ASSERT(!BP_IS_REDACTED(&bp
) ||
3765 dsl_dataset_feature_is_active(dn
->dn_objset
->os_dsl_dataset
,
3766 SPA_FEATURE_REDACTED_DATASETS
));
3767 if (BP_IS_HOLE(&bp
) || BP_IS_REDACTED(&bp
))
3770 ASSERT3U(curlevel
, ==, BP_GET_LEVEL(&bp
));
3772 zio_t
*pio
= zio_root(dmu_objset_spa(dn
->dn_objset
), NULL
, NULL
,
3775 dbuf_prefetch_arg_t
*dpa
= kmem_zalloc(sizeof (*dpa
), KM_SLEEP
);
3776 dsl_dataset_t
*ds
= dn
->dn_objset
->os_dsl_dataset
;
3777 SET_BOOKMARK(&dpa
->dpa_zb
, ds
!= NULL
? ds
->ds_object
: DMU_META_OBJSET
,
3778 dn
->dn_object
, level
, blkid
);
3779 dpa
->dpa_curlevel
= curlevel
;
3780 dpa
->dpa_prio
= prio
;
3781 dpa
->dpa_aflags
= aflags
;
3782 dpa
->dpa_spa
= dn
->dn_objset
->os_spa
;
3783 dpa
->dpa_dnode
= dn
;
3784 dpa
->dpa_epbs
= epbs
;
3789 if (!DNODE_LEVEL_IS_CACHEABLE(dn
, level
))
3790 dpa
->dpa_aflags
|= ARC_FLAG_UNCACHED
;
3791 else if (dnode_level_is_l2cacheable(&bp
, dn
, level
))
3792 dpa
->dpa_aflags
|= ARC_FLAG_L2CACHE
;
3795 * If we have the indirect just above us, no need to do the asynchronous
3796 * prefetch chain; we'll just run the last step ourselves. If we're at
3797 * a higher level, though, we want to issue the prefetches for all the
3798 * indirect blocks asynchronously, so we can go on with whatever we were
3801 if (curlevel
== level
) {
3802 ASSERT3U(curblkid
, ==, blkid
);
3803 dbuf_issue_final_prefetch(dpa
, &bp
);
3805 arc_flags_t iter_aflags
= ARC_FLAG_NOWAIT
;
3806 zbookmark_phys_t zb
;
3808 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
3809 if (dnode_level_is_l2cacheable(&bp
, dn
, level
))
3810 iter_aflags
|= ARC_FLAG_L2CACHE
;
3812 SET_BOOKMARK(&zb
, ds
!= NULL
? ds
->ds_object
: DMU_META_OBJSET
,
3813 dn
->dn_object
, curlevel
, curblkid
);
3814 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
,
3815 &bp
, dbuf_prefetch_indirect_done
, dpa
,
3816 ZIO_PRIORITY_SYNC_READ
,
3817 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
3821 * We use pio here instead of dpa_zio since it's possible that
3822 * dpa may have already been freed.
3828 cb(arg
, level
, blkid
, B_FALSE
);
3833 dbuf_prefetch(dnode_t
*dn
, int64_t level
, uint64_t blkid
, zio_priority_t prio
,
3837 return (dbuf_prefetch_impl(dn
, level
, blkid
, prio
, aflags
, NULL
, NULL
));
3841 * Helper function for dbuf_hold_impl() to copy a buffer. Handles
3842 * the case of encrypted, compressed and uncompressed buffers by
3843 * allocating the new buffer, respectively, with arc_alloc_raw_buf(),
3844 * arc_alloc_compressed_buf() or arc_alloc_buf().*
3846 * NOTE: Declared noinline to avoid stack bloat in dbuf_hold_impl().
3848 noinline
static void
3849 dbuf_hold_copy(dnode_t
*dn
, dmu_buf_impl_t
*db
)
3851 dbuf_dirty_record_t
*dr
= db
->db_data_pending
;
3852 arc_buf_t
*data
= dr
->dt
.dl
.dr_data
;
3853 enum zio_compress compress_type
= arc_get_compression(data
);
3854 uint8_t complevel
= arc_get_complevel(data
);
3856 if (arc_is_encrypted(data
)) {
3857 boolean_t byteorder
;
3858 uint8_t salt
[ZIO_DATA_SALT_LEN
];
3859 uint8_t iv
[ZIO_DATA_IV_LEN
];
3860 uint8_t mac
[ZIO_DATA_MAC_LEN
];
3862 arc_get_raw_params(data
, &byteorder
, salt
, iv
, mac
);
3863 dbuf_set_data(db
, arc_alloc_raw_buf(dn
->dn_objset
->os_spa
, db
,
3864 dmu_objset_id(dn
->dn_objset
), byteorder
, salt
, iv
, mac
,
3865 dn
->dn_type
, arc_buf_size(data
), arc_buf_lsize(data
),
3866 compress_type
, complevel
));
3867 } else if (compress_type
!= ZIO_COMPRESS_OFF
) {
3868 dbuf_set_data(db
, arc_alloc_compressed_buf(
3869 dn
->dn_objset
->os_spa
, db
, arc_buf_size(data
),
3870 arc_buf_lsize(data
), compress_type
, complevel
));
3872 dbuf_set_data(db
, arc_alloc_buf(dn
->dn_objset
->os_spa
, db
,
3873 DBUF_GET_BUFC_TYPE(db
), db
->db
.db_size
));
3876 rw_enter(&db
->db_rwlock
, RW_WRITER
);
3877 memcpy(db
->db
.db_data
, data
->b_data
, arc_buf_size(data
));
3878 rw_exit(&db
->db_rwlock
);
3882 * Returns with db_holds incremented, and db_mtx not held.
3883 * Note: dn_struct_rwlock must be held.
3886 dbuf_hold_impl(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
3887 boolean_t fail_sparse
, boolean_t fail_uncached
,
3888 const void *tag
, dmu_buf_impl_t
**dbp
)
3890 dmu_buf_impl_t
*db
, *parent
= NULL
;
3893 /* If the pool has been created, verify the tx_sync_lock is not held */
3894 spa_t
*spa
= dn
->dn_objset
->os_spa
;
3895 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
3897 ASSERT(!MUTEX_HELD(&dp
->dp_tx
.tx_sync_lock
));
3900 ASSERT(blkid
!= DMU_BONUS_BLKID
);
3901 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
3902 ASSERT3U(dn
->dn_nlevels
, >, level
);
3906 /* dbuf_find() returns with db_mtx held */
3907 db
= dbuf_find(dn
->dn_objset
, dn
->dn_object
, level
, blkid
, &hv
);
3910 blkptr_t
*bp
= NULL
;
3914 return (SET_ERROR(ENOENT
));
3916 ASSERT3P(parent
, ==, NULL
);
3917 err
= dbuf_findbp(dn
, level
, blkid
, fail_sparse
, &parent
, &bp
);
3919 if (err
== 0 && bp
&& BP_IS_HOLE(bp
))
3920 err
= SET_ERROR(ENOENT
);
3923 dbuf_rele(parent
, NULL
);
3927 if (err
&& err
!= ENOENT
)
3929 db
= dbuf_create(dn
, level
, blkid
, parent
, bp
, hv
);
3932 if (fail_uncached
&& db
->db_state
!= DB_CACHED
) {
3933 mutex_exit(&db
->db_mtx
);
3934 return (SET_ERROR(ENOENT
));
3937 if (db
->db_buf
!= NULL
) {
3938 arc_buf_access(db
->db_buf
);
3939 ASSERT3P(db
->db
.db_data
, ==, db
->db_buf
->b_data
);
3942 ASSERT(db
->db_buf
== NULL
|| arc_referenced(db
->db_buf
));
3945 * If this buffer is currently syncing out, and we are
3946 * still referencing it from db_data, we need to make a copy
3947 * of it in case we decide we want to dirty it again in this txg.
3949 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
3950 dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
3951 db
->db_state
== DB_CACHED
&& db
->db_data_pending
) {
3952 dbuf_dirty_record_t
*dr
= db
->db_data_pending
;
3953 if (dr
->dt
.dl
.dr_data
== db
->db_buf
) {
3954 ASSERT3P(db
->db_buf
, !=, NULL
);
3955 dbuf_hold_copy(dn
, db
);
3959 if (multilist_link_active(&db
->db_cache_link
)) {
3960 ASSERT(zfs_refcount_is_zero(&db
->db_holds
));
3961 ASSERT(db
->db_caching_status
== DB_DBUF_CACHE
||
3962 db
->db_caching_status
== DB_DBUF_METADATA_CACHE
);
3964 multilist_remove(&dbuf_caches
[db
->db_caching_status
].cache
, db
);
3966 uint64_t size
= db
->db
.db_size
;
3967 uint64_t usize
= dmu_buf_user_size(&db
->db
);
3968 (void) zfs_refcount_remove_many(
3969 &dbuf_caches
[db
->db_caching_status
].size
, size
, db
);
3970 (void) zfs_refcount_remove_many(
3971 &dbuf_caches
[db
->db_caching_status
].size
, usize
,
3974 if (db
->db_caching_status
== DB_DBUF_METADATA_CACHE
) {
3975 DBUF_STAT_BUMPDOWN(metadata_cache_count
);
3977 DBUF_STAT_BUMPDOWN(cache_levels
[db
->db_level
]);
3978 DBUF_STAT_BUMPDOWN(cache_count
);
3979 DBUF_STAT_DECR(cache_levels_bytes
[db
->db_level
],
3982 db
->db_caching_status
= DB_NO_CACHE
;
3984 (void) zfs_refcount_add(&db
->db_holds
, tag
);
3986 mutex_exit(&db
->db_mtx
);
3988 /* NOTE: we can't rele the parent until after we drop the db_mtx */
3990 dbuf_rele(parent
, NULL
);
3992 ASSERT3P(DB_DNODE(db
), ==, dn
);
3993 ASSERT3U(db
->db_blkid
, ==, blkid
);
3994 ASSERT3U(db
->db_level
, ==, level
);
4001 dbuf_hold(dnode_t
*dn
, uint64_t blkid
, const void *tag
)
4003 return (dbuf_hold_level(dn
, 0, blkid
, tag
));
4007 dbuf_hold_level(dnode_t
*dn
, int level
, uint64_t blkid
, const void *tag
)
4010 int err
= dbuf_hold_impl(dn
, level
, blkid
, FALSE
, FALSE
, tag
, &db
);
4011 return (err
? NULL
: db
);
4015 dbuf_create_bonus(dnode_t
*dn
)
4017 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
4019 ASSERT(dn
->dn_bonus
== NULL
);
4020 dn
->dn_bonus
= dbuf_create(dn
, 0, DMU_BONUS_BLKID
, dn
->dn_dbuf
, NULL
,
4021 dbuf_hash(dn
->dn_objset
, dn
->dn_object
, 0, DMU_BONUS_BLKID
));
4025 dbuf_spill_set_blksz(dmu_buf_t
*db_fake
, uint64_t blksz
, dmu_tx_t
*tx
)
4027 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
4029 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
4030 return (SET_ERROR(ENOTSUP
));
4032 blksz
= SPA_MINBLOCKSIZE
;
4033 ASSERT3U(blksz
, <=, spa_maxblocksize(dmu_objset_spa(db
->db_objset
)));
4034 blksz
= P2ROUNDUP(blksz
, SPA_MINBLOCKSIZE
);
4036 dbuf_new_size(db
, blksz
, tx
);
4042 dbuf_rm_spill(dnode_t
*dn
, dmu_tx_t
*tx
)
4044 dbuf_free_range(dn
, DMU_SPILL_BLKID
, DMU_SPILL_BLKID
, tx
);
4047 #pragma weak dmu_buf_add_ref = dbuf_add_ref
4049 dbuf_add_ref(dmu_buf_impl_t
*db
, const void *tag
)
4051 int64_t holds
= zfs_refcount_add(&db
->db_holds
, tag
);
4052 VERIFY3S(holds
, >, 1);
4055 #pragma weak dmu_buf_try_add_ref = dbuf_try_add_ref
4057 dbuf_try_add_ref(dmu_buf_t
*db_fake
, objset_t
*os
, uint64_t obj
, uint64_t blkid
,
4060 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
4061 dmu_buf_impl_t
*found_db
;
4062 boolean_t result
= B_FALSE
;
4064 if (blkid
== DMU_BONUS_BLKID
)
4065 found_db
= dbuf_find_bonus(os
, obj
);
4067 found_db
= dbuf_find(os
, obj
, 0, blkid
, NULL
);
4069 if (found_db
!= NULL
) {
4070 if (db
== found_db
&& dbuf_refcount(db
) > db
->db_dirtycnt
) {
4071 (void) zfs_refcount_add(&db
->db_holds
, tag
);
4074 mutex_exit(&found_db
->db_mtx
);
4080 * If you call dbuf_rele() you had better not be referencing the dnode handle
4081 * unless you have some other direct or indirect hold on the dnode. (An indirect
4082 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
4083 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
4084 * dnode's parent dbuf evicting its dnode handles.
4087 dbuf_rele(dmu_buf_impl_t
*db
, const void *tag
)
4089 mutex_enter(&db
->db_mtx
);
4090 dbuf_rele_and_unlock(db
, tag
, B_FALSE
);
4094 dmu_buf_rele(dmu_buf_t
*db
, const void *tag
)
4096 dbuf_rele((dmu_buf_impl_t
*)db
, tag
);
4100 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
4101 * db_dirtycnt and db_holds to be updated atomically. The 'evicting'
4102 * argument should be set if we are already in the dbuf-evicting code
4103 * path, in which case we don't want to recursively evict. This allows us to
4104 * avoid deeply nested stacks that would have a call flow similar to this:
4106 * dbuf_rele()-->dbuf_rele_and_unlock()-->dbuf_evict_notify()
4109 * +-----dbuf_destroy()<--dbuf_evict_one()<--------+
4113 dbuf_rele_and_unlock(dmu_buf_impl_t
*db
, const void *tag
, boolean_t evicting
)
4118 ASSERT(MUTEX_HELD(&db
->db_mtx
));
4122 * Remove the reference to the dbuf before removing its hold on the
4123 * dnode so we can guarantee in dnode_move() that a referenced bonus
4124 * buffer has a corresponding dnode hold.
4126 holds
= zfs_refcount_remove(&db
->db_holds
, tag
);
4130 * We can't freeze indirects if there is a possibility that they
4131 * may be modified in the current syncing context.
4133 if (db
->db_buf
!= NULL
&&
4134 holds
== (db
->db_level
== 0 ? db
->db_dirtycnt
: 0)) {
4135 arc_buf_freeze(db
->db_buf
);
4138 if (holds
== db
->db_dirtycnt
&&
4139 db
->db_level
== 0 && db
->db_user_immediate_evict
)
4140 dbuf_evict_user(db
);
4143 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
4145 boolean_t evict_dbuf
= db
->db_pending_evict
;
4148 * If the dnode moves here, we cannot cross this
4149 * barrier until the move completes.
4154 atomic_dec_32(&dn
->dn_dbufs_count
);
4157 * Decrementing the dbuf count means that the bonus
4158 * buffer's dnode hold is no longer discounted in
4159 * dnode_move(). The dnode cannot move until after
4160 * the dnode_rele() below.
4165 * Do not reference db after its lock is dropped.
4166 * Another thread may evict it.
4168 mutex_exit(&db
->db_mtx
);
4171 dnode_evict_bonus(dn
);
4174 } else if (db
->db_buf
== NULL
) {
4176 * This is a special case: we never associated this
4177 * dbuf with any data allocated from the ARC.
4179 ASSERT(db
->db_state
== DB_UNCACHED
||
4180 db
->db_state
== DB_NOFILL
);
4182 } else if (arc_released(db
->db_buf
)) {
4184 * This dbuf has anonymous data associated with it.
4187 } else if (!(DBUF_IS_CACHEABLE(db
) || db
->db_partial_read
) ||
4188 db
->db_pending_evict
) {
4190 } else if (!multilist_link_active(&db
->db_cache_link
)) {
4191 ASSERT3U(db
->db_caching_status
, ==, DB_NO_CACHE
);
4193 dbuf_cached_state_t dcs
=
4194 dbuf_include_in_metadata_cache(db
) ?
4195 DB_DBUF_METADATA_CACHE
: DB_DBUF_CACHE
;
4196 db
->db_caching_status
= dcs
;
4198 multilist_insert(&dbuf_caches
[dcs
].cache
, db
);
4199 uint64_t db_size
= db
->db
.db_size
;
4200 uint64_t dbu_size
= dmu_buf_user_size(&db
->db
);
4201 (void) zfs_refcount_add_many(
4202 &dbuf_caches
[dcs
].size
, db_size
, db
);
4203 size
= zfs_refcount_add_many(
4204 &dbuf_caches
[dcs
].size
, dbu_size
, db
->db_user
);
4205 uint8_t db_level
= db
->db_level
;
4206 mutex_exit(&db
->db_mtx
);
4208 if (dcs
== DB_DBUF_METADATA_CACHE
) {
4209 DBUF_STAT_BUMP(metadata_cache_count
);
4210 DBUF_STAT_MAX(metadata_cache_size_bytes_max
,
4213 DBUF_STAT_BUMP(cache_count
);
4214 DBUF_STAT_MAX(cache_size_bytes_max
, size
);
4215 DBUF_STAT_BUMP(cache_levels
[db_level
]);
4216 DBUF_STAT_INCR(cache_levels_bytes
[db_level
],
4217 db_size
+ dbu_size
);
4220 if (dcs
== DB_DBUF_CACHE
&& !evicting
)
4221 dbuf_evict_notify(size
);
4224 mutex_exit(&db
->db_mtx
);
4228 #pragma weak dmu_buf_refcount = dbuf_refcount
4230 dbuf_refcount(dmu_buf_impl_t
*db
)
4232 return (zfs_refcount_count(&db
->db_holds
));
4236 dmu_buf_user_refcount(dmu_buf_t
*db_fake
)
4239 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
4241 mutex_enter(&db
->db_mtx
);
4242 ASSERT3U(zfs_refcount_count(&db
->db_holds
), >=, db
->db_dirtycnt
);
4243 holds
= zfs_refcount_count(&db
->db_holds
) - db
->db_dirtycnt
;
4244 mutex_exit(&db
->db_mtx
);
4250 dmu_buf_replace_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*old_user
,
4251 dmu_buf_user_t
*new_user
)
4253 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
4255 mutex_enter(&db
->db_mtx
);
4256 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
4257 if (db
->db_user
== old_user
)
4258 db
->db_user
= new_user
;
4260 old_user
= db
->db_user
;
4261 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
4262 mutex_exit(&db
->db_mtx
);
4268 dmu_buf_set_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
4270 return (dmu_buf_replace_user(db_fake
, NULL
, user
));
4274 dmu_buf_set_user_ie(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
4276 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
4278 db
->db_user_immediate_evict
= TRUE
;
4279 return (dmu_buf_set_user(db_fake
, user
));
4283 dmu_buf_remove_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
4285 return (dmu_buf_replace_user(db_fake
, user
, NULL
));
4289 dmu_buf_get_user(dmu_buf_t
*db_fake
)
4291 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
4293 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
4294 return (db
->db_user
);
4298 dmu_buf_user_size(dmu_buf_t
*db_fake
)
4300 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
4301 if (db
->db_user
== NULL
)
4303 return (atomic_load_64(&db
->db_user
->dbu_size
));
4307 dmu_buf_add_user_size(dmu_buf_t
*db_fake
, uint64_t nadd
)
4309 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
4310 ASSERT3U(db
->db_caching_status
, ==, DB_NO_CACHE
);
4311 ASSERT3P(db
->db_user
, !=, NULL
);
4312 ASSERT3U(atomic_load_64(&db
->db_user
->dbu_size
), <, UINT64_MAX
- nadd
);
4313 atomic_add_64(&db
->db_user
->dbu_size
, nadd
);
4317 dmu_buf_sub_user_size(dmu_buf_t
*db_fake
, uint64_t nsub
)
4319 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
4320 ASSERT3U(db
->db_caching_status
, ==, DB_NO_CACHE
);
4321 ASSERT3P(db
->db_user
, !=, NULL
);
4322 ASSERT3U(atomic_load_64(&db
->db_user
->dbu_size
), >=, nsub
);
4323 atomic_sub_64(&db
->db_user
->dbu_size
, nsub
);
4327 dmu_buf_user_evict_wait(void)
4329 taskq_wait(dbu_evict_taskq
);
4333 dmu_buf_get_blkptr(dmu_buf_t
*db
)
4335 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
4336 return (dbi
->db_blkptr
);
4340 dmu_buf_get_objset(dmu_buf_t
*db
)
4342 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
4343 return (dbi
->db_objset
);
4347 dbuf_check_blkptr(dnode_t
*dn
, dmu_buf_impl_t
*db
)
4349 /* ASSERT(dmu_tx_is_syncing(tx) */
4350 ASSERT(MUTEX_HELD(&db
->db_mtx
));
4352 if (db
->db_blkptr
!= NULL
)
4355 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
4356 db
->db_blkptr
= DN_SPILL_BLKPTR(dn
->dn_phys
);
4357 BP_ZERO(db
->db_blkptr
);
4360 if (db
->db_level
== dn
->dn_phys
->dn_nlevels
-1) {
4362 * This buffer was allocated at a time when there was
4363 * no available blkptrs from the dnode, or it was
4364 * inappropriate to hook it in (i.e., nlevels mismatch).
4366 ASSERT(db
->db_blkid
< dn
->dn_phys
->dn_nblkptr
);
4367 ASSERT(db
->db_parent
== NULL
);
4368 db
->db_parent
= dn
->dn_dbuf
;
4369 db
->db_blkptr
= &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
];
4372 dmu_buf_impl_t
*parent
= db
->db_parent
;
4373 int epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
4375 ASSERT(dn
->dn_phys
->dn_nlevels
> 1);
4376 if (parent
== NULL
) {
4377 mutex_exit(&db
->db_mtx
);
4378 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
4379 parent
= dbuf_hold_level(dn
, db
->db_level
+ 1,
4380 db
->db_blkid
>> epbs
, db
);
4381 rw_exit(&dn
->dn_struct_rwlock
);
4382 mutex_enter(&db
->db_mtx
);
4383 db
->db_parent
= parent
;
4385 db
->db_blkptr
= (blkptr_t
*)parent
->db
.db_data
+
4386 (db
->db_blkid
& ((1ULL << epbs
) - 1));
4392 dbuf_sync_bonus(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
4394 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
4395 void *data
= dr
->dt
.dl
.dr_data
;
4397 ASSERT0(db
->db_level
);
4398 ASSERT(MUTEX_HELD(&db
->db_mtx
));
4399 ASSERT(db
->db_blkid
== DMU_BONUS_BLKID
);
4400 ASSERT(data
!= NULL
);
4402 dnode_t
*dn
= dr
->dr_dnode
;
4403 ASSERT3U(DN_MAX_BONUS_LEN(dn
->dn_phys
), <=,
4404 DN_SLOTS_TO_BONUSLEN(dn
->dn_phys
->dn_extra_slots
+ 1));
4405 memcpy(DN_BONUS(dn
->dn_phys
), data
, DN_MAX_BONUS_LEN(dn
->dn_phys
));
4407 dbuf_sync_leaf_verify_bonus_dnode(dr
);
4409 dbuf_undirty_bonus(dr
);
4410 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)tx
->tx_txg
, B_FALSE
);
4414 * When syncing out a blocks of dnodes, adjust the block to deal with
4415 * encryption. Normally, we make sure the block is decrypted before writing
4416 * it. If we have crypt params, then we are writing a raw (encrypted) block,
4417 * from a raw receive. In this case, set the ARC buf's crypt params so
4418 * that the BP will be filled with the correct byteorder, salt, iv, and mac.
4421 dbuf_prepare_encrypted_dnode_leaf(dbuf_dirty_record_t
*dr
)
4424 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
4426 ASSERT(MUTEX_HELD(&db
->db_mtx
));
4427 ASSERT3U(db
->db
.db_object
, ==, DMU_META_DNODE_OBJECT
);
4428 ASSERT3U(db
->db_level
, ==, 0);
4430 if (!db
->db_objset
->os_raw_receive
&& arc_is_encrypted(db
->db_buf
)) {
4431 zbookmark_phys_t zb
;
4434 * Unfortunately, there is currently no mechanism for
4435 * syncing context to handle decryption errors. An error
4436 * here is only possible if an attacker maliciously
4437 * changed a dnode block and updated the associated
4438 * checksums going up the block tree.
4440 SET_BOOKMARK(&zb
, dmu_objset_id(db
->db_objset
),
4441 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
4442 err
= arc_untransform(db
->db_buf
, db
->db_objset
->os_spa
,
4445 panic("Invalid dnode block MAC");
4446 } else if (dr
->dt
.dl
.dr_has_raw_params
) {
4447 (void) arc_release(dr
->dt
.dl
.dr_data
, db
);
4448 arc_convert_to_raw(dr
->dt
.dl
.dr_data
,
4449 dmu_objset_id(db
->db_objset
),
4450 dr
->dt
.dl
.dr_byteorder
, DMU_OT_DNODE
,
4451 dr
->dt
.dl
.dr_salt
, dr
->dt
.dl
.dr_iv
, dr
->dt
.dl
.dr_mac
);
4456 * dbuf_sync_indirect() is called recursively from dbuf_sync_list() so it
4457 * is critical the we not allow the compiler to inline this function in to
4458 * dbuf_sync_list() thereby drastically bloating the stack usage.
4460 noinline
static void
4461 dbuf_sync_indirect(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
4463 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
4464 dnode_t
*dn
= dr
->dr_dnode
;
4466 ASSERT(dmu_tx_is_syncing(tx
));
4468 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
4470 mutex_enter(&db
->db_mtx
);
4472 ASSERT(db
->db_level
> 0);
4475 /* Read the block if it hasn't been read yet. */
4476 if (db
->db_buf
== NULL
) {
4477 mutex_exit(&db
->db_mtx
);
4478 (void) dbuf_read(db
, NULL
, DB_RF_MUST_SUCCEED
);
4479 mutex_enter(&db
->db_mtx
);
4481 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
4482 ASSERT(db
->db_buf
!= NULL
);
4484 /* Indirect block size must match what the dnode thinks it is. */
4485 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
4486 dbuf_check_blkptr(dn
, db
);
4488 /* Provide the pending dirty record to child dbufs */
4489 db
->db_data_pending
= dr
;
4491 mutex_exit(&db
->db_mtx
);
4493 dbuf_write(dr
, db
->db_buf
, tx
);
4495 zio_t
*zio
= dr
->dr_zio
;
4496 mutex_enter(&dr
->dt
.di
.dr_mtx
);
4497 dbuf_sync_list(&dr
->dt
.di
.dr_children
, db
->db_level
- 1, tx
);
4498 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
4499 mutex_exit(&dr
->dt
.di
.dr_mtx
);
4504 * Verify that the size of the data in our bonus buffer does not exceed
4505 * its recorded size.
4507 * The purpose of this verification is to catch any cases in development
4508 * where the size of a phys structure (i.e space_map_phys_t) grows and,
4509 * due to incorrect feature management, older pools expect to read more
4510 * data even though they didn't actually write it to begin with.
4512 * For a example, this would catch an error in the feature logic where we
4513 * open an older pool and we expect to write the space map histogram of
4514 * a space map with size SPACE_MAP_SIZE_V0.
4517 dbuf_sync_leaf_verify_bonus_dnode(dbuf_dirty_record_t
*dr
)
4520 dnode_t
*dn
= dr
->dr_dnode
;
4523 * Encrypted bonus buffers can have data past their bonuslen.
4524 * Skip the verification of these blocks.
4526 if (DMU_OT_IS_ENCRYPTED(dn
->dn_bonustype
))
4529 uint16_t bonuslen
= dn
->dn_phys
->dn_bonuslen
;
4530 uint16_t maxbonuslen
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
);
4531 ASSERT3U(bonuslen
, <=, maxbonuslen
);
4533 arc_buf_t
*datap
= dr
->dt
.dl
.dr_data
;
4534 char *datap_end
= ((char *)datap
) + bonuslen
;
4535 char *datap_max
= ((char *)datap
) + maxbonuslen
;
4537 /* ensure that everything is zero after our data */
4538 for (; datap_end
< datap_max
; datap_end
++)
4539 ASSERT(*datap_end
== 0);
4544 dbuf_lightweight_bp(dbuf_dirty_record_t
*dr
)
4546 /* This must be a lightweight dirty record. */
4547 ASSERT3P(dr
->dr_dbuf
, ==, NULL
);
4548 dnode_t
*dn
= dr
->dr_dnode
;
4550 if (dn
->dn_phys
->dn_nlevels
== 1) {
4551 VERIFY3U(dr
->dt
.dll
.dr_blkid
, <, dn
->dn_phys
->dn_nblkptr
);
4552 return (&dn
->dn_phys
->dn_blkptr
[dr
->dt
.dll
.dr_blkid
]);
4554 dmu_buf_impl_t
*parent_db
= dr
->dr_parent
->dr_dbuf
;
4555 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
4556 VERIFY3U(parent_db
->db_level
, ==, 1);
4557 VERIFY3P(DB_DNODE(parent_db
), ==, dn
);
4558 VERIFY3U(dr
->dt
.dll
.dr_blkid
>> epbs
, ==, parent_db
->db_blkid
);
4559 blkptr_t
*bp
= parent_db
->db
.db_data
;
4560 return (&bp
[dr
->dt
.dll
.dr_blkid
& ((1 << epbs
) - 1)]);
4565 dbuf_lightweight_ready(zio_t
*zio
)
4567 dbuf_dirty_record_t
*dr
= zio
->io_private
;
4568 blkptr_t
*bp
= zio
->io_bp
;
4570 if (zio
->io_error
!= 0)
4573 dnode_t
*dn
= dr
->dr_dnode
;
4575 blkptr_t
*bp_orig
= dbuf_lightweight_bp(dr
);
4576 spa_t
*spa
= dmu_objset_spa(dn
->dn_objset
);
4577 int64_t delta
= bp_get_dsize_sync(spa
, bp
) -
4578 bp_get_dsize_sync(spa
, bp_orig
);
4579 dnode_diduse_space(dn
, delta
);
4581 uint64_t blkid
= dr
->dt
.dll
.dr_blkid
;
4582 mutex_enter(&dn
->dn_mtx
);
4583 if (blkid
> dn
->dn_phys
->dn_maxblkid
) {
4584 ASSERT0(dn
->dn_objset
->os_raw_receive
);
4585 dn
->dn_phys
->dn_maxblkid
= blkid
;
4587 mutex_exit(&dn
->dn_mtx
);
4589 if (!BP_IS_EMBEDDED(bp
)) {
4590 uint64_t fill
= BP_IS_HOLE(bp
) ? 0 : 1;
4591 BP_SET_FILL(bp
, fill
);
4594 dmu_buf_impl_t
*parent_db
;
4595 EQUIV(dr
->dr_parent
== NULL
, dn
->dn_phys
->dn_nlevels
== 1);
4596 if (dr
->dr_parent
== NULL
) {
4597 parent_db
= dn
->dn_dbuf
;
4599 parent_db
= dr
->dr_parent
->dr_dbuf
;
4601 rw_enter(&parent_db
->db_rwlock
, RW_WRITER
);
4603 rw_exit(&parent_db
->db_rwlock
);
4607 dbuf_lightweight_done(zio_t
*zio
)
4609 dbuf_dirty_record_t
*dr
= zio
->io_private
;
4611 VERIFY0(zio
->io_error
);
4613 objset_t
*os
= dr
->dr_dnode
->dn_objset
;
4614 dmu_tx_t
*tx
= os
->os_synctx
;
4616 if (zio
->io_flags
& (ZIO_FLAG_IO_REWRITE
| ZIO_FLAG_NOPWRITE
)) {
4617 ASSERT(BP_EQUAL(zio
->io_bp
, &zio
->io_bp_orig
));
4619 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
4620 (void) dsl_dataset_block_kill(ds
, &zio
->io_bp_orig
, tx
, B_TRUE
);
4621 dsl_dataset_block_born(ds
, zio
->io_bp
, tx
);
4624 dsl_pool_undirty_space(dmu_objset_pool(os
), dr
->dr_accounted
,
4627 abd_free(dr
->dt
.dll
.dr_abd
);
4628 kmem_free(dr
, sizeof (*dr
));
4631 noinline
static void
4632 dbuf_sync_lightweight(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
4634 dnode_t
*dn
= dr
->dr_dnode
;
4636 if (dn
->dn_phys
->dn_nlevels
== 1) {
4639 pio
= dr
->dr_parent
->dr_zio
;
4642 zbookmark_phys_t zb
= {
4643 .zb_objset
= dmu_objset_id(dn
->dn_objset
),
4644 .zb_object
= dn
->dn_object
,
4646 .zb_blkid
= dr
->dt
.dll
.dr_blkid
,
4650 * See comment in dbuf_write(). This is so that zio->io_bp_orig
4651 * will have the old BP in dbuf_lightweight_done().
4653 dr
->dr_bp_copy
= *dbuf_lightweight_bp(dr
);
4655 dr
->dr_zio
= zio_write(pio
, dmu_objset_spa(dn
->dn_objset
),
4656 dmu_tx_get_txg(tx
), &dr
->dr_bp_copy
, dr
->dt
.dll
.dr_abd
,
4657 dn
->dn_datablksz
, abd_get_size(dr
->dt
.dll
.dr_abd
),
4658 &dr
->dt
.dll
.dr_props
, dbuf_lightweight_ready
, NULL
,
4659 dbuf_lightweight_done
, dr
, ZIO_PRIORITY_ASYNC_WRITE
,
4660 ZIO_FLAG_MUSTSUCCEED
| dr
->dt
.dll
.dr_flags
, &zb
);
4662 zio_nowait(dr
->dr_zio
);
4666 * dbuf_sync_leaf() is called recursively from dbuf_sync_list() so it is
4667 * critical the we not allow the compiler to inline this function in to
4668 * dbuf_sync_list() thereby drastically bloating the stack usage.
4670 noinline
static void
4671 dbuf_sync_leaf(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
4673 arc_buf_t
**datap
= &dr
->dt
.dl
.dr_data
;
4674 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
4675 dnode_t
*dn
= dr
->dr_dnode
;
4677 uint64_t txg
= tx
->tx_txg
;
4679 ASSERT(dmu_tx_is_syncing(tx
));
4681 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
4683 mutex_enter(&db
->db_mtx
);
4685 * To be synced, we must be dirtied. But we might have been freed
4688 if (db
->db_state
== DB_UNCACHED
) {
4689 /* This buffer has been freed since it was dirtied */
4690 ASSERT3P(db
->db
.db_data
, ==, NULL
);
4691 } else if (db
->db_state
== DB_FILL
) {
4692 /* This buffer was freed and is now being re-filled */
4693 ASSERT(db
->db
.db_data
!= dr
->dt
.dl
.dr_data
);
4694 } else if (db
->db_state
== DB_READ
) {
4696 * This buffer was either cloned or had a Direct I/O write
4697 * occur and has an in-flgiht read on the BP. It is safe to
4698 * issue the write here, because the read has already been
4699 * issued and the contents won't change.
4701 * We can verify the case of both the clone and Direct I/O
4702 * write by making sure the first dirty record for the dbuf
4703 * has no ARC buffer associated with it.
4705 dbuf_dirty_record_t
*dr_head
=
4706 list_head(&db
->db_dirty_records
);
4707 ASSERT3P(db
->db_buf
, ==, NULL
);
4708 ASSERT3P(db
->db
.db_data
, ==, NULL
);
4709 ASSERT3P(dr_head
->dt
.dl
.dr_data
, ==, NULL
);
4710 ASSERT3U(dr_head
->dt
.dl
.dr_override_state
, ==, DR_OVERRIDDEN
);
4712 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_NOFILL
);
4716 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
4717 mutex_enter(&dn
->dn_mtx
);
4718 if (!(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
)) {
4720 * In the previous transaction group, the bonus buffer
4721 * was entirely used to store the attributes for the
4722 * dnode which overrode the dn_spill field. However,
4723 * when adding more attributes to the file a spill
4724 * block was required to hold the extra attributes.
4726 * Make sure to clear the garbage left in the dn_spill
4727 * field from the previous attributes in the bonus
4728 * buffer. Otherwise, after writing out the spill
4729 * block to the new allocated dva, it will free
4730 * the old block pointed to by the invalid dn_spill.
4732 db
->db_blkptr
= NULL
;
4734 dn
->dn_phys
->dn_flags
|= DNODE_FLAG_SPILL_BLKPTR
;
4735 mutex_exit(&dn
->dn_mtx
);
4739 * If this is a bonus buffer, simply copy the bonus data into the
4740 * dnode. It will be written out when the dnode is synced (and it
4741 * will be synced, since it must have been dirty for dbuf_sync to
4744 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
4745 ASSERT(dr
->dr_dbuf
== db
);
4746 dbuf_sync_bonus(dr
, tx
);
4753 * This function may have dropped the db_mtx lock allowing a dmu_sync
4754 * operation to sneak in. As a result, we need to ensure that we
4755 * don't check the dr_override_state until we have returned from
4756 * dbuf_check_blkptr.
4758 dbuf_check_blkptr(dn
, db
);
4761 * If this buffer is in the middle of an immediate write, wait for the
4762 * synchronous IO to complete.
4764 * This is also valid even with Direct I/O writes setting a dirty
4765 * records override state into DR_IN_DMU_SYNC, because all
4766 * Direct I/O writes happen in open-context.
4768 while (dr
->dt
.dl
.dr_override_state
== DR_IN_DMU_SYNC
) {
4769 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
4770 cv_wait(&db
->db_changed
, &db
->db_mtx
);
4774 * If this is a dnode block, ensure it is appropriately encrypted
4775 * or decrypted, depending on what we are writing to it this txg.
4777 if (os
->os_encrypted
&& dn
->dn_object
== DMU_META_DNODE_OBJECT
)
4778 dbuf_prepare_encrypted_dnode_leaf(dr
);
4780 if (*datap
!= NULL
&& *datap
== db
->db_buf
&&
4781 dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
4782 zfs_refcount_count(&db
->db_holds
) > 1) {
4784 * If this buffer is currently "in use" (i.e., there
4785 * are active holds and db_data still references it),
4786 * then make a copy before we start the write so that
4787 * any modifications from the open txg will not leak
4790 * NOTE: this copy does not need to be made for
4791 * objects only modified in the syncing context (e.g.
4792 * DNONE_DNODE blocks).
4794 int psize
= arc_buf_size(*datap
);
4795 int lsize
= arc_buf_lsize(*datap
);
4796 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
4797 enum zio_compress compress_type
= arc_get_compression(*datap
);
4798 uint8_t complevel
= arc_get_complevel(*datap
);
4800 if (arc_is_encrypted(*datap
)) {
4801 boolean_t byteorder
;
4802 uint8_t salt
[ZIO_DATA_SALT_LEN
];
4803 uint8_t iv
[ZIO_DATA_IV_LEN
];
4804 uint8_t mac
[ZIO_DATA_MAC_LEN
];
4806 arc_get_raw_params(*datap
, &byteorder
, salt
, iv
, mac
);
4807 *datap
= arc_alloc_raw_buf(os
->os_spa
, db
,
4808 dmu_objset_id(os
), byteorder
, salt
, iv
, mac
,
4809 dn
->dn_type
, psize
, lsize
, compress_type
,
4811 } else if (compress_type
!= ZIO_COMPRESS_OFF
) {
4812 ASSERT3U(type
, ==, ARC_BUFC_DATA
);
4813 *datap
= arc_alloc_compressed_buf(os
->os_spa
, db
,
4814 psize
, lsize
, compress_type
, complevel
);
4816 *datap
= arc_alloc_buf(os
->os_spa
, db
, type
, psize
);
4818 memcpy((*datap
)->b_data
, db
->db
.db_data
, psize
);
4820 db
->db_data_pending
= dr
;
4822 mutex_exit(&db
->db_mtx
);
4824 dbuf_write(dr
, *datap
, tx
);
4826 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
4827 if (dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
4828 list_insert_tail(&dn
->dn_dirty_records
[txg
& TXG_MASK
], dr
);
4830 zio_nowait(dr
->dr_zio
);
4835 * Syncs out a range of dirty records for indirect or leaf dbufs. May be
4836 * called recursively from dbuf_sync_indirect().
4839 dbuf_sync_list(list_t
*list
, int level
, dmu_tx_t
*tx
)
4841 dbuf_dirty_record_t
*dr
;
4843 while ((dr
= list_head(list
))) {
4844 if (dr
->dr_zio
!= NULL
) {
4846 * If we find an already initialized zio then we
4847 * are processing the meta-dnode, and we have finished.
4848 * The dbufs for all dnodes are put back on the list
4849 * during processing, so that we can zio_wait()
4850 * these IOs after initiating all child IOs.
4852 ASSERT3U(dr
->dr_dbuf
->db
.db_object
, ==,
4853 DMU_META_DNODE_OBJECT
);
4856 list_remove(list
, dr
);
4857 if (dr
->dr_dbuf
== NULL
) {
4858 dbuf_sync_lightweight(dr
, tx
);
4860 if (dr
->dr_dbuf
->db_blkid
!= DMU_BONUS_BLKID
&&
4861 dr
->dr_dbuf
->db_blkid
!= DMU_SPILL_BLKID
) {
4862 VERIFY3U(dr
->dr_dbuf
->db_level
, ==, level
);
4864 if (dr
->dr_dbuf
->db_level
> 0)
4865 dbuf_sync_indirect(dr
, tx
);
4867 dbuf_sync_leaf(dr
, tx
);
4873 dbuf_write_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
4876 dmu_buf_impl_t
*db
= vdb
;
4878 blkptr_t
*bp
= zio
->io_bp
;
4879 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
4880 spa_t
*spa
= zio
->io_spa
;
4885 ASSERT3P(db
->db_blkptr
, !=, NULL
);
4886 ASSERT3P(&db
->db_data_pending
->dr_bp_copy
, ==, bp
);
4890 delta
= bp_get_dsize_sync(spa
, bp
) - bp_get_dsize_sync(spa
, bp_orig
);
4891 dnode_diduse_space(dn
, delta
- zio
->io_prev_space_delta
);
4892 zio
->io_prev_space_delta
= delta
;
4894 if (BP_GET_LOGICAL_BIRTH(bp
) != 0) {
4895 ASSERT((db
->db_blkid
!= DMU_SPILL_BLKID
&&
4896 BP_GET_TYPE(bp
) == dn
->dn_type
) ||
4897 (db
->db_blkid
== DMU_SPILL_BLKID
&&
4898 BP_GET_TYPE(bp
) == dn
->dn_bonustype
) ||
4899 BP_IS_EMBEDDED(bp
));
4900 ASSERT(BP_GET_LEVEL(bp
) == db
->db_level
);
4903 mutex_enter(&db
->db_mtx
);
4906 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
4907 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
4908 ASSERT(!(BP_IS_HOLE(bp
)) &&
4909 db
->db_blkptr
== DN_SPILL_BLKPTR(dn
->dn_phys
));
4913 if (db
->db_level
== 0) {
4914 mutex_enter(&dn
->dn_mtx
);
4915 if (db
->db_blkid
> dn
->dn_phys
->dn_maxblkid
&&
4916 db
->db_blkid
!= DMU_SPILL_BLKID
) {
4917 ASSERT0(db
->db_objset
->os_raw_receive
);
4918 dn
->dn_phys
->dn_maxblkid
= db
->db_blkid
;
4920 mutex_exit(&dn
->dn_mtx
);
4922 if (dn
->dn_type
== DMU_OT_DNODE
) {
4924 while (i
< db
->db
.db_size
) {
4926 (void *)(((char *)db
->db
.db_data
) + i
);
4928 i
+= DNODE_MIN_SIZE
;
4929 if (dnp
->dn_type
!= DMU_OT_NONE
) {
4931 for (int j
= 0; j
< dnp
->dn_nblkptr
;
4933 (void) zfs_blkptr_verify(spa
,
4939 DNODE_FLAG_SPILL_BLKPTR
) {
4940 (void) zfs_blkptr_verify(spa
,
4941 DN_SPILL_BLKPTR(dnp
),
4945 i
+= dnp
->dn_extra_slots
*
4950 if (BP_IS_HOLE(bp
)) {
4957 blkptr_t
*ibp
= db
->db
.db_data
;
4958 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
4959 for (i
= db
->db
.db_size
>> SPA_BLKPTRSHIFT
; i
> 0; i
--, ibp
++) {
4960 if (BP_IS_HOLE(ibp
))
4962 (void) zfs_blkptr_verify(spa
, ibp
,
4963 BLK_CONFIG_SKIP
, BLK_VERIFY_HALT
);
4964 fill
+= BP_GET_FILL(ibp
);
4969 if (!BP_IS_EMBEDDED(bp
))
4970 BP_SET_FILL(bp
, fill
);
4972 mutex_exit(&db
->db_mtx
);
4974 db_lock_type_t dblt
= dmu_buf_lock_parent(db
, RW_WRITER
, FTAG
);
4975 *db
->db_blkptr
= *bp
;
4976 dmu_buf_unlock_parent(db
, dblt
, FTAG
);
4980 * This function gets called just prior to running through the compression
4981 * stage of the zio pipeline. If we're an indirect block comprised of only
4982 * holes, then we want this indirect to be compressed away to a hole. In
4983 * order to do that we must zero out any information about the holes that
4984 * this indirect points to prior to before we try to compress it.
4987 dbuf_write_children_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
4989 (void) zio
, (void) buf
;
4990 dmu_buf_impl_t
*db
= vdb
;
4992 unsigned int epbs
, i
;
4994 ASSERT3U(db
->db_level
, >, 0);
4996 epbs
= DB_DNODE(db
)->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
4998 ASSERT3U(epbs
, <, 31);
5000 /* Determine if all our children are holes */
5001 for (i
= 0, bp
= db
->db
.db_data
; i
< 1ULL << epbs
; i
++, bp
++) {
5002 if (!BP_IS_HOLE(bp
))
5007 * If all the children are holes, then zero them all out so that
5008 * we may get compressed away.
5010 if (i
== 1ULL << epbs
) {
5012 * We only found holes. Grab the rwlock to prevent
5013 * anybody from reading the blocks we're about to
5016 rw_enter(&db
->db_rwlock
, RW_WRITER
);
5017 memset(db
->db
.db_data
, 0, db
->db
.db_size
);
5018 rw_exit(&db
->db_rwlock
);
5023 dbuf_write_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
5026 dmu_buf_impl_t
*db
= vdb
;
5027 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
5028 blkptr_t
*bp
= db
->db_blkptr
;
5029 objset_t
*os
= db
->db_objset
;
5030 dmu_tx_t
*tx
= os
->os_synctx
;
5032 ASSERT0(zio
->io_error
);
5033 ASSERT(db
->db_blkptr
== bp
);
5036 * For nopwrites and rewrites we ensure that the bp matches our
5037 * original and bypass all the accounting.
5039 if (zio
->io_flags
& (ZIO_FLAG_IO_REWRITE
| ZIO_FLAG_NOPWRITE
)) {
5040 ASSERT(BP_EQUAL(bp
, bp_orig
));
5042 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
5043 (void) dsl_dataset_block_kill(ds
, bp_orig
, tx
, B_TRUE
);
5044 dsl_dataset_block_born(ds
, bp
, tx
);
5047 mutex_enter(&db
->db_mtx
);
5051 dbuf_dirty_record_t
*dr
= db
->db_data_pending
;
5052 dnode_t
*dn
= dr
->dr_dnode
;
5053 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
5054 ASSERT(dr
->dr_dbuf
== db
);
5055 ASSERT(list_next(&db
->db_dirty_records
, dr
) == NULL
);
5056 list_remove(&db
->db_dirty_records
, dr
);
5059 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
5060 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
5061 ASSERT(!(BP_IS_HOLE(db
->db_blkptr
)) &&
5062 db
->db_blkptr
== DN_SPILL_BLKPTR(dn
->dn_phys
));
5066 if (db
->db_level
== 0) {
5067 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
5068 ASSERT(dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
);
5070 /* no dr_data if this is a NO_FILL or Direct I/O */
5071 if (dr
->dt
.dl
.dr_data
!= NULL
&&
5072 dr
->dt
.dl
.dr_data
!= db
->db_buf
) {
5073 ASSERT3B(dr
->dt
.dl
.dr_brtwrite
, ==, B_FALSE
);
5074 ASSERT3B(dr
->dt
.dl
.dr_diowrite
, ==, B_FALSE
);
5075 arc_buf_destroy(dr
->dt
.dl
.dr_data
, db
);
5078 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
5079 ASSERT3U(db
->db
.db_size
, ==, 1 << dn
->dn_phys
->dn_indblkshift
);
5080 if (!BP_IS_HOLE(db
->db_blkptr
)) {
5081 int epbs __maybe_unused
= dn
->dn_phys
->dn_indblkshift
-
5083 ASSERT3U(db
->db_blkid
, <=,
5084 dn
->dn_phys
->dn_maxblkid
>> (db
->db_level
* epbs
));
5085 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==,
5088 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
5089 list_destroy(&dr
->dt
.di
.dr_children
);
5092 cv_broadcast(&db
->db_changed
);
5093 ASSERT(db
->db_dirtycnt
> 0);
5094 db
->db_dirtycnt
-= 1;
5095 db
->db_data_pending
= NULL
;
5096 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)tx
->tx_txg
, B_FALSE
);
5098 dsl_pool_undirty_space(dmu_objset_pool(os
), dr
->dr_accounted
,
5101 kmem_cache_free(dbuf_dirty_kmem_cache
, dr
);
5105 dbuf_write_nofill_ready(zio_t
*zio
)
5107 dbuf_write_ready(zio
, NULL
, zio
->io_private
);
5111 dbuf_write_nofill_done(zio_t
*zio
)
5113 dbuf_write_done(zio
, NULL
, zio
->io_private
);
5117 dbuf_write_override_ready(zio_t
*zio
)
5119 dbuf_dirty_record_t
*dr
= zio
->io_private
;
5120 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
5122 dbuf_write_ready(zio
, NULL
, db
);
5126 dbuf_write_override_done(zio_t
*zio
)
5128 dbuf_dirty_record_t
*dr
= zio
->io_private
;
5129 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
5130 blkptr_t
*obp
= &dr
->dt
.dl
.dr_overridden_by
;
5132 mutex_enter(&db
->db_mtx
);
5133 if (!BP_EQUAL(zio
->io_bp
, obp
)) {
5134 if (!BP_IS_HOLE(obp
))
5135 dsl_free(spa_get_dsl(zio
->io_spa
), zio
->io_txg
, obp
);
5136 arc_release(dr
->dt
.dl
.dr_data
, db
);
5138 mutex_exit(&db
->db_mtx
);
5140 dbuf_write_done(zio
, NULL
, db
);
5142 if (zio
->io_abd
!= NULL
)
5143 abd_free(zio
->io_abd
);
5146 typedef struct dbuf_remap_impl_callback_arg
{
5148 uint64_t drica_blk_birth
;
5150 } dbuf_remap_impl_callback_arg_t
;
5153 dbuf_remap_impl_callback(uint64_t vdev
, uint64_t offset
, uint64_t size
,
5156 dbuf_remap_impl_callback_arg_t
*drica
= arg
;
5157 objset_t
*os
= drica
->drica_os
;
5158 spa_t
*spa
= dmu_objset_spa(os
);
5159 dmu_tx_t
*tx
= drica
->drica_tx
;
5161 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa
)));
5163 if (os
== spa_meta_objset(spa
)) {
5164 spa_vdev_indirect_mark_obsolete(spa
, vdev
, offset
, size
, tx
);
5166 dsl_dataset_block_remapped(dmu_objset_ds(os
), vdev
, offset
,
5167 size
, drica
->drica_blk_birth
, tx
);
5172 dbuf_remap_impl(dnode_t
*dn
, blkptr_t
*bp
, krwlock_t
*rw
, dmu_tx_t
*tx
)
5174 blkptr_t bp_copy
= *bp
;
5175 spa_t
*spa
= dmu_objset_spa(dn
->dn_objset
);
5176 dbuf_remap_impl_callback_arg_t drica
;
5178 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa
)));
5180 drica
.drica_os
= dn
->dn_objset
;
5181 drica
.drica_blk_birth
= BP_GET_LOGICAL_BIRTH(bp
);
5182 drica
.drica_tx
= tx
;
5183 if (spa_remap_blkptr(spa
, &bp_copy
, dbuf_remap_impl_callback
,
5186 * If the blkptr being remapped is tracked by a livelist,
5187 * then we need to make sure the livelist reflects the update.
5188 * First, cancel out the old blkptr by appending a 'FREE'
5189 * entry. Next, add an 'ALLOC' to track the new version. This
5190 * way we avoid trying to free an inaccurate blkptr at delete.
5191 * Note that embedded blkptrs are not tracked in livelists.
5193 if (dn
->dn_objset
!= spa_meta_objset(spa
)) {
5194 dsl_dataset_t
*ds
= dmu_objset_ds(dn
->dn_objset
);
5195 if (dsl_deadlist_is_open(&ds
->ds_dir
->dd_livelist
) &&
5196 BP_GET_LOGICAL_BIRTH(bp
) >
5197 ds
->ds_dir
->dd_origin_txg
) {
5198 ASSERT(!BP_IS_EMBEDDED(bp
));
5199 ASSERT(dsl_dir_is_clone(ds
->ds_dir
));
5200 ASSERT(spa_feature_is_enabled(spa
,
5201 SPA_FEATURE_LIVELIST
));
5202 bplist_append(&ds
->ds_dir
->dd_pending_frees
,
5204 bplist_append(&ds
->ds_dir
->dd_pending_allocs
,
5210 * The db_rwlock prevents dbuf_read_impl() from
5211 * dereferencing the BP while we are changing it. To
5212 * avoid lock contention, only grab it when we are actually
5216 rw_enter(rw
, RW_WRITER
);
5224 * Remap any existing BP's to concrete vdevs, if possible.
5227 dbuf_remap(dnode_t
*dn
, dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
5229 spa_t
*spa
= dmu_objset_spa(db
->db_objset
);
5230 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa
)));
5232 if (!spa_feature_is_active(spa
, SPA_FEATURE_DEVICE_REMOVAL
))
5235 if (db
->db_level
> 0) {
5236 blkptr_t
*bp
= db
->db
.db_data
;
5237 for (int i
= 0; i
< db
->db
.db_size
>> SPA_BLKPTRSHIFT
; i
++) {
5238 dbuf_remap_impl(dn
, &bp
[i
], &db
->db_rwlock
, tx
);
5240 } else if (db
->db
.db_object
== DMU_META_DNODE_OBJECT
) {
5241 dnode_phys_t
*dnp
= db
->db
.db_data
;
5242 ASSERT3U(dn
->dn_type
, ==, DMU_OT_DNODE
);
5243 for (int i
= 0; i
< db
->db
.db_size
>> DNODE_SHIFT
;
5244 i
+= dnp
[i
].dn_extra_slots
+ 1) {
5245 for (int j
= 0; j
< dnp
[i
].dn_nblkptr
; j
++) {
5246 krwlock_t
*lock
= (dn
->dn_dbuf
== NULL
? NULL
:
5247 &dn
->dn_dbuf
->db_rwlock
);
5248 dbuf_remap_impl(dn
, &dnp
[i
].dn_blkptr
[j
], lock
,
5257 * Populate dr->dr_zio with a zio to commit a dirty buffer to disk.
5258 * Caller is responsible for issuing the zio_[no]wait(dr->dr_zio).
5261 dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
)
5263 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
5264 dnode_t
*dn
= dr
->dr_dnode
;
5266 dmu_buf_impl_t
*parent
= db
->db_parent
;
5267 uint64_t txg
= tx
->tx_txg
;
5268 zbookmark_phys_t zb
;
5270 zio_t
*pio
; /* parent I/O */
5273 ASSERT(dmu_tx_is_syncing(tx
));
5277 if (db
->db_level
> 0 || dn
->dn_type
== DMU_OT_DNODE
) {
5279 * Private object buffers are released here rather than in
5280 * dbuf_dirty() since they are only modified in the syncing
5281 * context and we don't want the overhead of making multiple
5282 * copies of the data.
5284 if (BP_IS_HOLE(db
->db_blkptr
))
5287 dbuf_release_bp(db
);
5288 dbuf_remap(dn
, db
, tx
);
5291 if (parent
!= dn
->dn_dbuf
) {
5292 /* Our parent is an indirect block. */
5293 /* We have a dirty parent that has been scheduled for write. */
5294 ASSERT(parent
&& parent
->db_data_pending
);
5295 /* Our parent's buffer is one level closer to the dnode. */
5296 ASSERT(db
->db_level
== parent
->db_level
-1);
5298 * We're about to modify our parent's db_data by modifying
5299 * our block pointer, so the parent must be released.
5301 ASSERT(arc_released(parent
->db_buf
));
5302 pio
= parent
->db_data_pending
->dr_zio
;
5304 /* Our parent is the dnode itself. */
5305 ASSERT((db
->db_level
== dn
->dn_phys
->dn_nlevels
-1 &&
5306 db
->db_blkid
!= DMU_SPILL_BLKID
) ||
5307 (db
->db_blkid
== DMU_SPILL_BLKID
&& db
->db_level
== 0));
5308 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
5309 ASSERT3P(db
->db_blkptr
, ==,
5310 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
5314 ASSERT(db
->db_level
== 0 || data
== db
->db_buf
);
5315 ASSERT3U(BP_GET_LOGICAL_BIRTH(db
->db_blkptr
), <=, txg
);
5318 SET_BOOKMARK(&zb
, os
->os_dsl_dataset
?
5319 os
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
5320 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
5322 if (db
->db_blkid
== DMU_SPILL_BLKID
)
5324 wp_flag
|= (data
== NULL
) ? WP_NOFILL
: 0;
5326 dmu_write_policy(os
, dn
, db
->db_level
, wp_flag
, &zp
);
5329 * We copy the blkptr now (rather than when we instantiate the dirty
5330 * record), because its value can change between open context and
5331 * syncing context. We do not need to hold dn_struct_rwlock to read
5332 * db_blkptr because we are in syncing context.
5334 dr
->dr_bp_copy
= *db
->db_blkptr
;
5336 if (db
->db_level
== 0 &&
5337 dr
->dt
.dl
.dr_override_state
== DR_OVERRIDDEN
) {
5339 * The BP for this block has been provided by open context
5340 * (by dmu_sync(), dmu_write_direct(),
5341 * or dmu_buf_write_embedded()).
5343 abd_t
*contents
= (data
!= NULL
) ?
5344 abd_get_from_buf(data
->b_data
, arc_buf_size(data
)) : NULL
;
5346 dr
->dr_zio
= zio_write(pio
, os
->os_spa
, txg
, &dr
->dr_bp_copy
,
5347 contents
, db
->db
.db_size
, db
->db
.db_size
, &zp
,
5348 dbuf_write_override_ready
, NULL
,
5349 dbuf_write_override_done
,
5350 dr
, ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
5351 mutex_enter(&db
->db_mtx
);
5352 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
5353 zio_write_override(dr
->dr_zio
, &dr
->dt
.dl
.dr_overridden_by
,
5354 dr
->dt
.dl
.dr_copies
, dr
->dt
.dl
.dr_nopwrite
,
5355 dr
->dt
.dl
.dr_brtwrite
);
5356 mutex_exit(&db
->db_mtx
);
5357 } else if (data
== NULL
) {
5358 ASSERT(zp
.zp_checksum
== ZIO_CHECKSUM_OFF
||
5359 zp
.zp_checksum
== ZIO_CHECKSUM_NOPARITY
);
5360 dr
->dr_zio
= zio_write(pio
, os
->os_spa
, txg
,
5361 &dr
->dr_bp_copy
, NULL
, db
->db
.db_size
, db
->db
.db_size
, &zp
,
5362 dbuf_write_nofill_ready
, NULL
,
5363 dbuf_write_nofill_done
, db
,
5364 ZIO_PRIORITY_ASYNC_WRITE
,
5365 ZIO_FLAG_MUSTSUCCEED
| ZIO_FLAG_NODATA
, &zb
);
5367 ASSERT(arc_released(data
));
5370 * For indirect blocks, we want to setup the children
5371 * ready callback so that we can properly handle an indirect
5372 * block that only contains holes.
5374 arc_write_done_func_t
*children_ready_cb
= NULL
;
5375 if (db
->db_level
!= 0)
5376 children_ready_cb
= dbuf_write_children_ready
;
5378 dr
->dr_zio
= arc_write(pio
, os
->os_spa
, txg
,
5379 &dr
->dr_bp_copy
, data
, !DBUF_IS_CACHEABLE(db
),
5380 dbuf_is_l2cacheable(db
, NULL
), &zp
, dbuf_write_ready
,
5381 children_ready_cb
, dbuf_write_done
, db
,
5382 ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
5386 EXPORT_SYMBOL(dbuf_find
);
5387 EXPORT_SYMBOL(dbuf_is_metadata
);
5388 EXPORT_SYMBOL(dbuf_destroy
);
5389 EXPORT_SYMBOL(dbuf_loan_arcbuf
);
5390 EXPORT_SYMBOL(dbuf_whichblock
);
5391 EXPORT_SYMBOL(dbuf_read
);
5392 EXPORT_SYMBOL(dbuf_unoverride
);
5393 EXPORT_SYMBOL(dbuf_free_range
);
5394 EXPORT_SYMBOL(dbuf_new_size
);
5395 EXPORT_SYMBOL(dbuf_release_bp
);
5396 EXPORT_SYMBOL(dbuf_dirty
);
5397 EXPORT_SYMBOL(dmu_buf_set_crypt_params
);
5398 EXPORT_SYMBOL(dmu_buf_will_dirty
);
5399 EXPORT_SYMBOL(dmu_buf_is_dirty
);
5400 EXPORT_SYMBOL(dmu_buf_will_clone_or_dio
);
5401 EXPORT_SYMBOL(dmu_buf_will_not_fill
);
5402 EXPORT_SYMBOL(dmu_buf_will_fill
);
5403 EXPORT_SYMBOL(dmu_buf_fill_done
);
5404 EXPORT_SYMBOL(dmu_buf_rele
);
5405 EXPORT_SYMBOL(dbuf_assign_arcbuf
);
5406 EXPORT_SYMBOL(dbuf_prefetch
);
5407 EXPORT_SYMBOL(dbuf_hold_impl
);
5408 EXPORT_SYMBOL(dbuf_hold
);
5409 EXPORT_SYMBOL(dbuf_hold_level
);
5410 EXPORT_SYMBOL(dbuf_create_bonus
);
5411 EXPORT_SYMBOL(dbuf_spill_set_blksz
);
5412 EXPORT_SYMBOL(dbuf_rm_spill
);
5413 EXPORT_SYMBOL(dbuf_add_ref
);
5414 EXPORT_SYMBOL(dbuf_rele
);
5415 EXPORT_SYMBOL(dbuf_rele_and_unlock
);
5416 EXPORT_SYMBOL(dbuf_refcount
);
5417 EXPORT_SYMBOL(dbuf_sync_list
);
5418 EXPORT_SYMBOL(dmu_buf_set_user
);
5419 EXPORT_SYMBOL(dmu_buf_set_user_ie
);
5420 EXPORT_SYMBOL(dmu_buf_get_user
);
5421 EXPORT_SYMBOL(dmu_buf_get_blkptr
);
5423 ZFS_MODULE_PARAM(zfs_dbuf_cache
, dbuf_cache_
, max_bytes
, U64
, ZMOD_RW
,
5424 "Maximum size in bytes of the dbuf cache.");
5426 ZFS_MODULE_PARAM(zfs_dbuf_cache
, dbuf_cache_
, hiwater_pct
, UINT
, ZMOD_RW
,
5427 "Percentage over dbuf_cache_max_bytes for direct dbuf eviction.");
5429 ZFS_MODULE_PARAM(zfs_dbuf_cache
, dbuf_cache_
, lowater_pct
, UINT
, ZMOD_RW
,
5430 "Percentage below dbuf_cache_max_bytes when dbuf eviction stops.");
5432 ZFS_MODULE_PARAM(zfs_dbuf
, dbuf_
, metadata_cache_max_bytes
, U64
, ZMOD_RW
,
5433 "Maximum size in bytes of dbuf metadata cache.");
5435 ZFS_MODULE_PARAM(zfs_dbuf
, dbuf_
, cache_shift
, UINT
, ZMOD_RW
,
5436 "Set size of dbuf cache to log2 fraction of arc size.");
5438 ZFS_MODULE_PARAM(zfs_dbuf
, dbuf_
, metadata_cache_shift
, UINT
, ZMOD_RW
,
5439 "Set size of dbuf metadata cache to log2 fraction of arc size.");
5441 ZFS_MODULE_PARAM(zfs_dbuf
, dbuf_
, mutex_cache_shift
, UINT
, ZMOD_RD
,
5442 "Set size of dbuf cache mutex array as log2 shift.");