4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2012, 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
31 #include <sys/zfs_context.h>
34 #include <sys/dmu_send.h>
35 #include <sys/dmu_impl.h>
37 #include <sys/dmu_objset.h>
38 #include <sys/dsl_dataset.h>
39 #include <sys/dsl_dir.h>
40 #include <sys/dmu_tx.h>
43 #include <sys/dmu_zfetch.h>
45 #include <sys/sa_impl.h>
46 #include <sys/zfeature.h>
47 #include <sys/blkptr.h>
48 #include <sys/range_tree.h>
49 #include <sys/trace_zfs.h>
50 #include <sys/callb.h>
54 #include <sys/spa_impl.h>
55 #include <sys/wmsum.h>
56 #include <sys/vdev_impl.h>
60 typedef struct dbuf_stats
{
62 * Various statistics about the size of the dbuf cache.
64 kstat_named_t cache_count
;
65 kstat_named_t cache_size_bytes
;
66 kstat_named_t cache_size_bytes_max
;
68 * Statistics regarding the bounds on the dbuf cache size.
70 kstat_named_t cache_target_bytes
;
71 kstat_named_t cache_lowater_bytes
;
72 kstat_named_t cache_hiwater_bytes
;
74 * Total number of dbuf cache evictions that have occurred.
76 kstat_named_t cache_total_evicts
;
78 * The distribution of dbuf levels in the dbuf cache and
79 * the total size of all dbufs at each level.
81 kstat_named_t cache_levels
[DN_MAX_LEVELS
];
82 kstat_named_t cache_levels_bytes
[DN_MAX_LEVELS
];
84 * Statistics about the dbuf hash table.
86 kstat_named_t hash_hits
;
87 kstat_named_t hash_misses
;
88 kstat_named_t hash_collisions
;
89 kstat_named_t hash_elements
;
90 kstat_named_t hash_elements_max
;
92 * Number of sublists containing more than one dbuf in the dbuf
93 * hash table. Keep track of the longest hash chain.
95 kstat_named_t hash_chains
;
96 kstat_named_t hash_chain_max
;
98 * Number of times a dbuf_create() discovers that a dbuf was
99 * already created and in the dbuf hash table.
101 kstat_named_t hash_insert_race
;
103 * Statistics about the size of the metadata dbuf cache.
105 kstat_named_t metadata_cache_count
;
106 kstat_named_t metadata_cache_size_bytes
;
107 kstat_named_t metadata_cache_size_bytes_max
;
109 * For diagnostic purposes, this is incremented whenever we can't add
110 * something to the metadata cache because it's full, and instead put
111 * the data in the regular dbuf cache.
113 kstat_named_t metadata_cache_overflow
;
116 dbuf_stats_t dbuf_stats
= {
117 { "cache_count", KSTAT_DATA_UINT64
},
118 { "cache_size_bytes", KSTAT_DATA_UINT64
},
119 { "cache_size_bytes_max", KSTAT_DATA_UINT64
},
120 { "cache_target_bytes", KSTAT_DATA_UINT64
},
121 { "cache_lowater_bytes", KSTAT_DATA_UINT64
},
122 { "cache_hiwater_bytes", KSTAT_DATA_UINT64
},
123 { "cache_total_evicts", KSTAT_DATA_UINT64
},
124 { { "cache_levels_N", KSTAT_DATA_UINT64
} },
125 { { "cache_levels_bytes_N", KSTAT_DATA_UINT64
} },
126 { "hash_hits", KSTAT_DATA_UINT64
},
127 { "hash_misses", KSTAT_DATA_UINT64
},
128 { "hash_collisions", KSTAT_DATA_UINT64
},
129 { "hash_elements", KSTAT_DATA_UINT64
},
130 { "hash_elements_max", KSTAT_DATA_UINT64
},
131 { "hash_chains", KSTAT_DATA_UINT64
},
132 { "hash_chain_max", KSTAT_DATA_UINT64
},
133 { "hash_insert_race", KSTAT_DATA_UINT64
},
134 { "metadata_cache_count", KSTAT_DATA_UINT64
},
135 { "metadata_cache_size_bytes", KSTAT_DATA_UINT64
},
136 { "metadata_cache_size_bytes_max", KSTAT_DATA_UINT64
},
137 { "metadata_cache_overflow", KSTAT_DATA_UINT64
}
142 wmsum_t cache_total_evicts
;
143 wmsum_t cache_levels
[DN_MAX_LEVELS
];
144 wmsum_t cache_levels_bytes
[DN_MAX_LEVELS
];
147 wmsum_t hash_collisions
;
149 wmsum_t hash_insert_race
;
150 wmsum_t metadata_cache_count
;
151 wmsum_t metadata_cache_overflow
;
154 #define DBUF_STAT_INCR(stat, val) \
155 wmsum_add(&dbuf_sums.stat, val);
156 #define DBUF_STAT_DECR(stat, val) \
157 DBUF_STAT_INCR(stat, -(val));
158 #define DBUF_STAT_BUMP(stat) \
159 DBUF_STAT_INCR(stat, 1);
160 #define DBUF_STAT_BUMPDOWN(stat) \
161 DBUF_STAT_INCR(stat, -1);
162 #define DBUF_STAT_MAX(stat, v) { \
164 while ((v) > (_m = dbuf_stats.stat.value.ui64) && \
165 (_m != atomic_cas_64(&dbuf_stats.stat.value.ui64, _m, (v))))\
169 static boolean_t
dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
);
170 static void dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
);
171 static void dbuf_sync_leaf_verify_bonus_dnode(dbuf_dirty_record_t
*dr
);
172 static int dbuf_read_verify_dnode_crypt(dmu_buf_impl_t
*db
, uint32_t flags
);
174 extern inline void dmu_buf_init_user(dmu_buf_user_t
*dbu
,
175 dmu_buf_evict_func_t
*evict_func_sync
,
176 dmu_buf_evict_func_t
*evict_func_async
,
177 dmu_buf_t
**clear_on_evict_dbufp
);
180 * Global data structures and functions for the dbuf cache.
182 static kmem_cache_t
*dbuf_kmem_cache
;
183 static taskq_t
*dbu_evict_taskq
;
185 static kthread_t
*dbuf_cache_evict_thread
;
186 static kmutex_t dbuf_evict_lock
;
187 static kcondvar_t dbuf_evict_cv
;
188 static boolean_t dbuf_evict_thread_exit
;
191 * There are two dbuf caches; each dbuf can only be in one of them at a time.
193 * 1. Cache of metadata dbufs, to help make read-heavy administrative commands
194 * from /sbin/zfs run faster. The "metadata cache" specifically stores dbufs
195 * that represent the metadata that describes filesystems/snapshots/
196 * bookmarks/properties/etc. We only evict from this cache when we export a
197 * pool, to short-circuit as much I/O as possible for all administrative
198 * commands that need the metadata. There is no eviction policy for this
199 * cache, because we try to only include types in it which would occupy a
200 * very small amount of space per object but create a large impact on the
201 * performance of these commands. Instead, after it reaches a maximum size
202 * (which should only happen on very small memory systems with a very large
203 * number of filesystem objects), we stop taking new dbufs into the
204 * metadata cache, instead putting them in the normal dbuf cache.
206 * 2. LRU cache of dbufs. The dbuf cache maintains a list of dbufs that
207 * are not currently held but have been recently released. These dbufs
208 * are not eligible for arc eviction until they are aged out of the cache.
209 * Dbufs that are aged out of the cache will be immediately destroyed and
210 * become eligible for arc eviction.
212 * Dbufs are added to these caches once the last hold is released. If a dbuf is
213 * later accessed and still exists in the dbuf cache, then it will be removed
214 * from the cache and later re-added to the head of the cache.
216 * If a given dbuf meets the requirements for the metadata cache, it will go
217 * there, otherwise it will be considered for the generic LRU dbuf cache. The
218 * caches and the refcounts tracking their sizes are stored in an array indexed
219 * by those caches' matching enum values (from dbuf_cached_state_t).
221 typedef struct dbuf_cache
{
223 zfs_refcount_t size ____cacheline_aligned
;
225 dbuf_cache_t dbuf_caches
[DB_CACHE_MAX
];
227 /* Size limits for the caches */
228 unsigned long dbuf_cache_max_bytes
= ULONG_MAX
;
229 unsigned long dbuf_metadata_cache_max_bytes
= ULONG_MAX
;
231 /* Set the default sizes of the caches to log2 fraction of arc size */
232 int dbuf_cache_shift
= 5;
233 int dbuf_metadata_cache_shift
= 6;
235 static unsigned long dbuf_cache_target_bytes(void);
236 static unsigned long dbuf_metadata_cache_target_bytes(void);
239 * The LRU dbuf cache uses a three-stage eviction policy:
240 * - A low water marker designates when the dbuf eviction thread
241 * should stop evicting from the dbuf cache.
242 * - When we reach the maximum size (aka mid water mark), we
243 * signal the eviction thread to run.
244 * - The high water mark indicates when the eviction thread
245 * is unable to keep up with the incoming load and eviction must
246 * happen in the context of the calling thread.
250 * low water mid water hi water
251 * +----------------------------------------+----------+----------+
256 * +----------------------------------------+----------+----------+
258 * evicting eviction directly
261 * The high and low water marks indicate the operating range for the eviction
262 * thread. The low water mark is, by default, 90% of the total size of the
263 * cache and the high water mark is at 110% (both of these percentages can be
264 * changed by setting dbuf_cache_lowater_pct and dbuf_cache_hiwater_pct,
265 * respectively). The eviction thread will try to ensure that the cache remains
266 * within this range by waking up every second and checking if the cache is
267 * above the low water mark. The thread can also be woken up by callers adding
268 * elements into the cache if the cache is larger than the mid water (i.e max
269 * cache size). Once the eviction thread is woken up and eviction is required,
270 * it will continue evicting buffers until it's able to reduce the cache size
271 * to the low water mark. If the cache size continues to grow and hits the high
272 * water mark, then callers adding elements to the cache will begin to evict
273 * directly from the cache until the cache is no longer above the high water
278 * The percentage above and below the maximum cache size.
280 uint_t dbuf_cache_hiwater_pct
= 10;
281 uint_t dbuf_cache_lowater_pct
= 10;
285 dbuf_cons(void *vdb
, void *unused
, int kmflag
)
287 dmu_buf_impl_t
*db
= vdb
;
288 bzero(db
, sizeof (dmu_buf_impl_t
));
290 mutex_init(&db
->db_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
291 rw_init(&db
->db_rwlock
, NULL
, RW_DEFAULT
, NULL
);
292 cv_init(&db
->db_changed
, NULL
, CV_DEFAULT
, NULL
);
293 multilist_link_init(&db
->db_cache_link
);
294 zfs_refcount_create(&db
->db_holds
);
301 dbuf_dest(void *vdb
, void *unused
)
303 dmu_buf_impl_t
*db
= vdb
;
304 mutex_destroy(&db
->db_mtx
);
305 rw_destroy(&db
->db_rwlock
);
306 cv_destroy(&db
->db_changed
);
307 ASSERT(!multilist_link_active(&db
->db_cache_link
));
308 zfs_refcount_destroy(&db
->db_holds
);
312 * dbuf hash table routines
314 static dbuf_hash_table_t dbuf_hash_table
;
317 * We use Cityhash for this. It's fast, and has good hash properties without
318 * requiring any large static buffers.
321 dbuf_hash(void *os
, uint64_t obj
, uint8_t lvl
, uint64_t blkid
)
323 return (cityhash4((uintptr_t)os
, obj
, (uint64_t)lvl
, blkid
));
326 #define DTRACE_SET_STATE(db, why) \
327 DTRACE_PROBE2(dbuf__state_change, dmu_buf_impl_t *, db, \
330 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
331 ((dbuf)->db.db_object == (obj) && \
332 (dbuf)->db_objset == (os) && \
333 (dbuf)->db_level == (level) && \
334 (dbuf)->db_blkid == (blkid))
337 dbuf_find(objset_t
*os
, uint64_t obj
, uint8_t level
, uint64_t blkid
)
339 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
344 hv
= dbuf_hash(os
, obj
, level
, blkid
);
345 idx
= hv
& h
->hash_table_mask
;
347 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
348 for (db
= h
->hash_table
[idx
]; db
!= NULL
; db
= db
->db_hash_next
) {
349 if (DBUF_EQUAL(db
, os
, obj
, level
, blkid
)) {
350 mutex_enter(&db
->db_mtx
);
351 if (db
->db_state
!= DB_EVICTING
) {
352 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
355 mutex_exit(&db
->db_mtx
);
358 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
362 static dmu_buf_impl_t
*
363 dbuf_find_bonus(objset_t
*os
, uint64_t object
)
366 dmu_buf_impl_t
*db
= NULL
;
368 if (dnode_hold(os
, object
, FTAG
, &dn
) == 0) {
369 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
370 if (dn
->dn_bonus
!= NULL
) {
372 mutex_enter(&db
->db_mtx
);
374 rw_exit(&dn
->dn_struct_rwlock
);
375 dnode_rele(dn
, FTAG
);
381 * Insert an entry into the hash table. If there is already an element
382 * equal to elem in the hash table, then the already existing element
383 * will be returned and the new element will not be inserted.
384 * Otherwise returns NULL.
386 static dmu_buf_impl_t
*
387 dbuf_hash_insert(dmu_buf_impl_t
*db
)
389 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
390 objset_t
*os
= db
->db_objset
;
391 uint64_t obj
= db
->db
.db_object
;
392 int level
= db
->db_level
;
393 uint64_t blkid
, hv
, idx
;
397 blkid
= db
->db_blkid
;
398 hv
= dbuf_hash(os
, obj
, level
, blkid
);
399 idx
= hv
& h
->hash_table_mask
;
401 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
402 for (dbf
= h
->hash_table
[idx
], i
= 0; dbf
!= NULL
;
403 dbf
= dbf
->db_hash_next
, i
++) {
404 if (DBUF_EQUAL(dbf
, os
, obj
, level
, blkid
)) {
405 mutex_enter(&dbf
->db_mtx
);
406 if (dbf
->db_state
!= DB_EVICTING
) {
407 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
410 mutex_exit(&dbf
->db_mtx
);
415 DBUF_STAT_BUMP(hash_collisions
);
417 DBUF_STAT_BUMP(hash_chains
);
419 DBUF_STAT_MAX(hash_chain_max
, i
);
422 mutex_enter(&db
->db_mtx
);
423 db
->db_hash_next
= h
->hash_table
[idx
];
424 h
->hash_table
[idx
] = db
;
425 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
426 uint64_t he
= atomic_inc_64_nv(&dbuf_stats
.hash_elements
.value
.ui64
);
427 DBUF_STAT_MAX(hash_elements_max
, he
);
433 * This returns whether this dbuf should be stored in the metadata cache, which
434 * is based on whether it's from one of the dnode types that store data related
435 * to traversing dataset hierarchies.
438 dbuf_include_in_metadata_cache(dmu_buf_impl_t
*db
)
441 dmu_object_type_t type
= DB_DNODE(db
)->dn_type
;
444 /* Check if this dbuf is one of the types we care about */
445 if (DMU_OT_IS_METADATA_CACHED(type
)) {
446 /* If we hit this, then we set something up wrong in dmu_ot */
447 ASSERT(DMU_OT_IS_METADATA(type
));
450 * Sanity check for small-memory systems: don't allocate too
451 * much memory for this purpose.
453 if (zfs_refcount_count(
454 &dbuf_caches
[DB_DBUF_METADATA_CACHE
].size
) >
455 dbuf_metadata_cache_target_bytes()) {
456 DBUF_STAT_BUMP(metadata_cache_overflow
);
467 * Remove an entry from the hash table. It must be in the EVICTING state.
470 dbuf_hash_remove(dmu_buf_impl_t
*db
)
472 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
474 dmu_buf_impl_t
*dbf
, **dbp
;
476 hv
= dbuf_hash(db
->db_objset
, db
->db
.db_object
,
477 db
->db_level
, db
->db_blkid
);
478 idx
= hv
& h
->hash_table_mask
;
481 * We mustn't hold db_mtx to maintain lock ordering:
482 * DBUF_HASH_MUTEX > db_mtx.
484 ASSERT(zfs_refcount_is_zero(&db
->db_holds
));
485 ASSERT(db
->db_state
== DB_EVICTING
);
486 ASSERT(!MUTEX_HELD(&db
->db_mtx
));
488 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
489 dbp
= &h
->hash_table
[idx
];
490 while ((dbf
= *dbp
) != db
) {
491 dbp
= &dbf
->db_hash_next
;
494 *dbp
= db
->db_hash_next
;
495 db
->db_hash_next
= NULL
;
496 if (h
->hash_table
[idx
] &&
497 h
->hash_table
[idx
]->db_hash_next
== NULL
)
498 DBUF_STAT_BUMPDOWN(hash_chains
);
499 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
500 atomic_dec_64(&dbuf_stats
.hash_elements
.value
.ui64
);
506 } dbvu_verify_type_t
;
509 dbuf_verify_user(dmu_buf_impl_t
*db
, dbvu_verify_type_t verify_type
)
514 if (db
->db_user
== NULL
)
517 /* Only data blocks support the attachment of user data. */
518 ASSERT(db
->db_level
== 0);
520 /* Clients must resolve a dbuf before attaching user data. */
521 ASSERT(db
->db
.db_data
!= NULL
);
522 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
524 holds
= zfs_refcount_count(&db
->db_holds
);
525 if (verify_type
== DBVU_EVICTING
) {
527 * Immediate eviction occurs when holds == dirtycnt.
528 * For normal eviction buffers, holds is zero on
529 * eviction, except when dbuf_fix_old_data() calls
530 * dbuf_clear_data(). However, the hold count can grow
531 * during eviction even though db_mtx is held (see
532 * dmu_bonus_hold() for an example), so we can only
533 * test the generic invariant that holds >= dirtycnt.
535 ASSERT3U(holds
, >=, db
->db_dirtycnt
);
537 if (db
->db_user_immediate_evict
== TRUE
)
538 ASSERT3U(holds
, >=, db
->db_dirtycnt
);
540 ASSERT3U(holds
, >, 0);
546 dbuf_evict_user(dmu_buf_impl_t
*db
)
548 dmu_buf_user_t
*dbu
= db
->db_user
;
550 ASSERT(MUTEX_HELD(&db
->db_mtx
));
555 dbuf_verify_user(db
, DBVU_EVICTING
);
559 if (dbu
->dbu_clear_on_evict_dbufp
!= NULL
)
560 *dbu
->dbu_clear_on_evict_dbufp
= NULL
;
564 * There are two eviction callbacks - one that we call synchronously
565 * and one that we invoke via a taskq. The async one is useful for
566 * avoiding lock order reversals and limiting stack depth.
568 * Note that if we have a sync callback but no async callback,
569 * it's likely that the sync callback will free the structure
570 * containing the dbu. In that case we need to take care to not
571 * dereference dbu after calling the sync evict func.
573 boolean_t has_async
= (dbu
->dbu_evict_func_async
!= NULL
);
575 if (dbu
->dbu_evict_func_sync
!= NULL
)
576 dbu
->dbu_evict_func_sync(dbu
);
579 taskq_dispatch_ent(dbu_evict_taskq
, dbu
->dbu_evict_func_async
,
580 dbu
, 0, &dbu
->dbu_tqent
);
585 dbuf_is_metadata(dmu_buf_impl_t
*db
)
588 * Consider indirect blocks and spill blocks to be meta data.
590 if (db
->db_level
> 0 || db
->db_blkid
== DMU_SPILL_BLKID
) {
593 boolean_t is_metadata
;
596 is_metadata
= DMU_OT_IS_METADATA(DB_DNODE(db
)->dn_type
);
599 return (is_metadata
);
604 * We want to exclude buffers that are on a special allocation class from
608 dbuf_is_l2cacheable(dmu_buf_impl_t
*db
)
611 zfs_cache_type_t cache
= db
->db_objset
->os_secondary_cache
;
612 blkptr_t
*bp
= db
->db_blkptr
;
614 if (bp
!= NULL
&& !BP_IS_HOLE(bp
)) {
615 uint64_t vdev
= DVA_GET_VDEV(bp
->blk_dva
);
616 vdev_t
*rvd
= db
->db_objset
->os_spa
->spa_root_vdev
;
618 if (vdev
< rvd
->vdev_children
)
619 vd
= rvd
->vdev_child
[vdev
];
621 if (cache
== ZFS_CACHE_ALL
||
622 (dbuf_is_metadata(db
) && cache
== ZFS_CACHE_METADATA
)) {
626 if ((vd
->vdev_alloc_bias
!= VDEV_BIAS_SPECIAL
&&
627 vd
->vdev_alloc_bias
!= VDEV_BIAS_DEDUP
) ||
628 l2arc_exclude_special
== 0)
636 static inline boolean_t
637 dnode_level_is_l2cacheable(blkptr_t
*bp
, dnode_t
*dn
, int64_t level
)
640 zfs_cache_type_t cache
= dn
->dn_objset
->os_secondary_cache
;
642 if (bp
!= NULL
&& !BP_IS_HOLE(bp
)) {
643 uint64_t vdev
= DVA_GET_VDEV(bp
->blk_dva
);
644 vdev_t
*rvd
= dn
->dn_objset
->os_spa
->spa_root_vdev
;
646 if (vdev
< rvd
->vdev_children
)
647 vd
= rvd
->vdev_child
[vdev
];
649 if (cache
== ZFS_CACHE_ALL
|| ((level
> 0 ||
650 DMU_OT_IS_METADATA(dn
->dn_handle
->dnh_dnode
->dn_type
)) &&
651 cache
== ZFS_CACHE_METADATA
)) {
655 if ((vd
->vdev_alloc_bias
!= VDEV_BIAS_SPECIAL
&&
656 vd
->vdev_alloc_bias
!= VDEV_BIAS_DEDUP
) ||
657 l2arc_exclude_special
== 0)
667 * This function *must* return indices evenly distributed between all
668 * sublists of the multilist. This is needed due to how the dbuf eviction
669 * code is laid out; dbuf_evict_thread() assumes dbufs are evenly
670 * distributed between all sublists and uses this assumption when
671 * deciding which sublist to evict from and how much to evict from it.
674 dbuf_cache_multilist_index_func(multilist_t
*ml
, void *obj
)
676 dmu_buf_impl_t
*db
= obj
;
679 * The assumption here, is the hash value for a given
680 * dmu_buf_impl_t will remain constant throughout it's lifetime
681 * (i.e. it's objset, object, level and blkid fields don't change).
682 * Thus, we don't need to store the dbuf's sublist index
683 * on insertion, as this index can be recalculated on removal.
685 * Also, the low order bits of the hash value are thought to be
686 * distributed evenly. Otherwise, in the case that the multilist
687 * has a power of two number of sublists, each sublists' usage
688 * would not be evenly distributed. In this context full 64bit
689 * division would be a waste of time, so limit it to 32 bits.
691 return ((unsigned int)dbuf_hash(db
->db_objset
, db
->db
.db_object
,
692 db
->db_level
, db
->db_blkid
) %
693 multilist_get_num_sublists(ml
));
697 * The target size of the dbuf cache can grow with the ARC target,
698 * unless limited by the tunable dbuf_cache_max_bytes.
700 static inline unsigned long
701 dbuf_cache_target_bytes(void)
703 return (MIN(dbuf_cache_max_bytes
,
704 arc_target_bytes() >> dbuf_cache_shift
));
708 * The target size of the dbuf metadata cache can grow with the ARC target,
709 * unless limited by the tunable dbuf_metadata_cache_max_bytes.
711 static inline unsigned long
712 dbuf_metadata_cache_target_bytes(void)
714 return (MIN(dbuf_metadata_cache_max_bytes
,
715 arc_target_bytes() >> dbuf_metadata_cache_shift
));
718 static inline uint64_t
719 dbuf_cache_hiwater_bytes(void)
721 uint64_t dbuf_cache_target
= dbuf_cache_target_bytes();
722 return (dbuf_cache_target
+
723 (dbuf_cache_target
* dbuf_cache_hiwater_pct
) / 100);
726 static inline uint64_t
727 dbuf_cache_lowater_bytes(void)
729 uint64_t dbuf_cache_target
= dbuf_cache_target_bytes();
730 return (dbuf_cache_target
-
731 (dbuf_cache_target
* dbuf_cache_lowater_pct
) / 100);
734 static inline boolean_t
735 dbuf_cache_above_lowater(void)
737 return (zfs_refcount_count(&dbuf_caches
[DB_DBUF_CACHE
].size
) >
738 dbuf_cache_lowater_bytes());
742 * Evict the oldest eligible dbuf from the dbuf cache.
747 int idx
= multilist_get_random_index(&dbuf_caches
[DB_DBUF_CACHE
].cache
);
748 multilist_sublist_t
*mls
= multilist_sublist_lock(
749 &dbuf_caches
[DB_DBUF_CACHE
].cache
, idx
);
751 ASSERT(!MUTEX_HELD(&dbuf_evict_lock
));
753 dmu_buf_impl_t
*db
= multilist_sublist_tail(mls
);
754 while (db
!= NULL
&& mutex_tryenter(&db
->db_mtx
) == 0) {
755 db
= multilist_sublist_prev(mls
, db
);
758 DTRACE_PROBE2(dbuf__evict__one
, dmu_buf_impl_t
*, db
,
759 multilist_sublist_t
*, mls
);
762 multilist_sublist_remove(mls
, db
);
763 multilist_sublist_unlock(mls
);
764 (void) zfs_refcount_remove_many(
765 &dbuf_caches
[DB_DBUF_CACHE
].size
, db
->db
.db_size
, db
);
766 DBUF_STAT_BUMPDOWN(cache_levels
[db
->db_level
]);
767 DBUF_STAT_BUMPDOWN(cache_count
);
768 DBUF_STAT_DECR(cache_levels_bytes
[db
->db_level
],
770 ASSERT3U(db
->db_caching_status
, ==, DB_DBUF_CACHE
);
771 db
->db_caching_status
= DB_NO_CACHE
;
773 DBUF_STAT_BUMP(cache_total_evicts
);
775 multilist_sublist_unlock(mls
);
780 * The dbuf evict thread is responsible for aging out dbufs from the
781 * cache. Once the cache has reached it's maximum size, dbufs are removed
782 * and destroyed. The eviction thread will continue running until the size
783 * of the dbuf cache is at or below the maximum size. Once the dbuf is aged
784 * out of the cache it is destroyed and becomes eligible for arc eviction.
788 dbuf_evict_thread(void *unused
)
792 CALLB_CPR_INIT(&cpr
, &dbuf_evict_lock
, callb_generic_cpr
, FTAG
);
794 mutex_enter(&dbuf_evict_lock
);
795 while (!dbuf_evict_thread_exit
) {
796 while (!dbuf_cache_above_lowater() && !dbuf_evict_thread_exit
) {
797 CALLB_CPR_SAFE_BEGIN(&cpr
);
798 (void) cv_timedwait_idle_hires(&dbuf_evict_cv
,
799 &dbuf_evict_lock
, SEC2NSEC(1), MSEC2NSEC(1), 0);
800 CALLB_CPR_SAFE_END(&cpr
, &dbuf_evict_lock
);
802 mutex_exit(&dbuf_evict_lock
);
805 * Keep evicting as long as we're above the low water mark
806 * for the cache. We do this without holding the locks to
807 * minimize lock contention.
809 while (dbuf_cache_above_lowater() && !dbuf_evict_thread_exit
) {
813 mutex_enter(&dbuf_evict_lock
);
816 dbuf_evict_thread_exit
= B_FALSE
;
817 cv_broadcast(&dbuf_evict_cv
);
818 CALLB_CPR_EXIT(&cpr
); /* drops dbuf_evict_lock */
823 * Wake up the dbuf eviction thread if the dbuf cache is at its max size.
824 * If the dbuf cache is at its high water mark, then evict a dbuf from the
825 * dbuf cache using the callers context.
828 dbuf_evict_notify(uint64_t size
)
831 * We check if we should evict without holding the dbuf_evict_lock,
832 * because it's OK to occasionally make the wrong decision here,
833 * and grabbing the lock results in massive lock contention.
835 if (size
> dbuf_cache_target_bytes()) {
836 if (size
> dbuf_cache_hiwater_bytes())
838 cv_signal(&dbuf_evict_cv
);
843 dbuf_kstat_update(kstat_t
*ksp
, int rw
)
845 dbuf_stats_t
*ds
= ksp
->ks_data
;
847 if (rw
== KSTAT_WRITE
)
848 return (SET_ERROR(EACCES
));
850 ds
->cache_count
.value
.ui64
=
851 wmsum_value(&dbuf_sums
.cache_count
);
852 ds
->cache_size_bytes
.value
.ui64
=
853 zfs_refcount_count(&dbuf_caches
[DB_DBUF_CACHE
].size
);
854 ds
->cache_target_bytes
.value
.ui64
= dbuf_cache_target_bytes();
855 ds
->cache_hiwater_bytes
.value
.ui64
= dbuf_cache_hiwater_bytes();
856 ds
->cache_lowater_bytes
.value
.ui64
= dbuf_cache_lowater_bytes();
857 ds
->cache_total_evicts
.value
.ui64
=
858 wmsum_value(&dbuf_sums
.cache_total_evicts
);
859 for (int i
= 0; i
< DN_MAX_LEVELS
; i
++) {
860 ds
->cache_levels
[i
].value
.ui64
=
861 wmsum_value(&dbuf_sums
.cache_levels
[i
]);
862 ds
->cache_levels_bytes
[i
].value
.ui64
=
863 wmsum_value(&dbuf_sums
.cache_levels_bytes
[i
]);
865 ds
->hash_hits
.value
.ui64
=
866 wmsum_value(&dbuf_sums
.hash_hits
);
867 ds
->hash_misses
.value
.ui64
=
868 wmsum_value(&dbuf_sums
.hash_misses
);
869 ds
->hash_collisions
.value
.ui64
=
870 wmsum_value(&dbuf_sums
.hash_collisions
);
871 ds
->hash_chains
.value
.ui64
=
872 wmsum_value(&dbuf_sums
.hash_chains
);
873 ds
->hash_insert_race
.value
.ui64
=
874 wmsum_value(&dbuf_sums
.hash_insert_race
);
875 ds
->metadata_cache_count
.value
.ui64
=
876 wmsum_value(&dbuf_sums
.metadata_cache_count
);
877 ds
->metadata_cache_size_bytes
.value
.ui64
= zfs_refcount_count(
878 &dbuf_caches
[DB_DBUF_METADATA_CACHE
].size
);
879 ds
->metadata_cache_overflow
.value
.ui64
=
880 wmsum_value(&dbuf_sums
.metadata_cache_overflow
);
887 uint64_t hsize
= 1ULL << 16;
888 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
892 * The hash table is big enough to fill one eighth of physical memory
893 * with an average block size of zfs_arc_average_blocksize (default 8K).
894 * By default, the table will take up
895 * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers).
897 while (hsize
* zfs_arc_average_blocksize
< arc_all_memory() / 8)
901 h
->hash_table_mask
= hsize
- 1;
904 * Large allocations which do not require contiguous pages
905 * should be using vmem_alloc() in the linux kernel
907 h
->hash_table
= vmem_zalloc(hsize
* sizeof (void *), KM_SLEEP
);
909 h
->hash_table
= kmem_zalloc(hsize
* sizeof (void *), KM_NOSLEEP
);
911 if (h
->hash_table
== NULL
) {
912 /* XXX - we should really return an error instead of assert */
913 ASSERT(hsize
> (1ULL << 10));
918 dbuf_kmem_cache
= kmem_cache_create("dmu_buf_impl_t",
919 sizeof (dmu_buf_impl_t
),
920 0, dbuf_cons
, dbuf_dest
, NULL
, NULL
, NULL
, 0);
922 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
923 mutex_init(&h
->hash_mutexes
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
928 * All entries are queued via taskq_dispatch_ent(), so min/maxalloc
929 * configuration is not required.
931 dbu_evict_taskq
= taskq_create("dbu_evict", 1, defclsyspri
, 0, 0, 0);
933 for (dbuf_cached_state_t dcs
= 0; dcs
< DB_CACHE_MAX
; dcs
++) {
934 multilist_create(&dbuf_caches
[dcs
].cache
,
935 sizeof (dmu_buf_impl_t
),
936 offsetof(dmu_buf_impl_t
, db_cache_link
),
937 dbuf_cache_multilist_index_func
);
938 zfs_refcount_create(&dbuf_caches
[dcs
].size
);
941 dbuf_evict_thread_exit
= B_FALSE
;
942 mutex_init(&dbuf_evict_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
943 cv_init(&dbuf_evict_cv
, NULL
, CV_DEFAULT
, NULL
);
944 dbuf_cache_evict_thread
= thread_create(NULL
, 0, dbuf_evict_thread
,
945 NULL
, 0, &p0
, TS_RUN
, minclsyspri
);
947 wmsum_init(&dbuf_sums
.cache_count
, 0);
948 wmsum_init(&dbuf_sums
.cache_total_evicts
, 0);
949 for (i
= 0; i
< DN_MAX_LEVELS
; i
++) {
950 wmsum_init(&dbuf_sums
.cache_levels
[i
], 0);
951 wmsum_init(&dbuf_sums
.cache_levels_bytes
[i
], 0);
953 wmsum_init(&dbuf_sums
.hash_hits
, 0);
954 wmsum_init(&dbuf_sums
.hash_misses
, 0);
955 wmsum_init(&dbuf_sums
.hash_collisions
, 0);
956 wmsum_init(&dbuf_sums
.hash_chains
, 0);
957 wmsum_init(&dbuf_sums
.hash_insert_race
, 0);
958 wmsum_init(&dbuf_sums
.metadata_cache_count
, 0);
959 wmsum_init(&dbuf_sums
.metadata_cache_overflow
, 0);
961 dbuf_ksp
= kstat_create("zfs", 0, "dbufstats", "misc",
962 KSTAT_TYPE_NAMED
, sizeof (dbuf_stats
) / sizeof (kstat_named_t
),
964 if (dbuf_ksp
!= NULL
) {
965 for (i
= 0; i
< DN_MAX_LEVELS
; i
++) {
966 snprintf(dbuf_stats
.cache_levels
[i
].name
,
967 KSTAT_STRLEN
, "cache_level_%d", i
);
968 dbuf_stats
.cache_levels
[i
].data_type
=
970 snprintf(dbuf_stats
.cache_levels_bytes
[i
].name
,
971 KSTAT_STRLEN
, "cache_level_%d_bytes", i
);
972 dbuf_stats
.cache_levels_bytes
[i
].data_type
=
975 dbuf_ksp
->ks_data
= &dbuf_stats
;
976 dbuf_ksp
->ks_update
= dbuf_kstat_update
;
977 kstat_install(dbuf_ksp
);
984 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
987 dbuf_stats_destroy();
989 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
990 mutex_destroy(&h
->hash_mutexes
[i
]);
993 * Large allocations which do not require contiguous pages
994 * should be using vmem_free() in the linux kernel
996 vmem_free(h
->hash_table
, (h
->hash_table_mask
+ 1) * sizeof (void *));
998 kmem_free(h
->hash_table
, (h
->hash_table_mask
+ 1) * sizeof (void *));
1000 kmem_cache_destroy(dbuf_kmem_cache
);
1001 taskq_destroy(dbu_evict_taskq
);
1003 mutex_enter(&dbuf_evict_lock
);
1004 dbuf_evict_thread_exit
= B_TRUE
;
1005 while (dbuf_evict_thread_exit
) {
1006 cv_signal(&dbuf_evict_cv
);
1007 cv_wait(&dbuf_evict_cv
, &dbuf_evict_lock
);
1009 mutex_exit(&dbuf_evict_lock
);
1011 mutex_destroy(&dbuf_evict_lock
);
1012 cv_destroy(&dbuf_evict_cv
);
1014 for (dbuf_cached_state_t dcs
= 0; dcs
< DB_CACHE_MAX
; dcs
++) {
1015 zfs_refcount_destroy(&dbuf_caches
[dcs
].size
);
1016 multilist_destroy(&dbuf_caches
[dcs
].cache
);
1019 if (dbuf_ksp
!= NULL
) {
1020 kstat_delete(dbuf_ksp
);
1024 wmsum_fini(&dbuf_sums
.cache_count
);
1025 wmsum_fini(&dbuf_sums
.cache_total_evicts
);
1026 for (i
= 0; i
< DN_MAX_LEVELS
; i
++) {
1027 wmsum_fini(&dbuf_sums
.cache_levels
[i
]);
1028 wmsum_fini(&dbuf_sums
.cache_levels_bytes
[i
]);
1030 wmsum_fini(&dbuf_sums
.hash_hits
);
1031 wmsum_fini(&dbuf_sums
.hash_misses
);
1032 wmsum_fini(&dbuf_sums
.hash_collisions
);
1033 wmsum_fini(&dbuf_sums
.hash_chains
);
1034 wmsum_fini(&dbuf_sums
.hash_insert_race
);
1035 wmsum_fini(&dbuf_sums
.metadata_cache_count
);
1036 wmsum_fini(&dbuf_sums
.metadata_cache_overflow
);
1045 dbuf_verify(dmu_buf_impl_t
*db
)
1048 dbuf_dirty_record_t
*dr
;
1051 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1053 if (!(zfs_flags
& ZFS_DEBUG_DBUF_VERIFY
))
1056 ASSERT(db
->db_objset
!= NULL
);
1060 ASSERT(db
->db_parent
== NULL
);
1061 ASSERT(db
->db_blkptr
== NULL
);
1063 ASSERT3U(db
->db
.db_object
, ==, dn
->dn_object
);
1064 ASSERT3P(db
->db_objset
, ==, dn
->dn_objset
);
1065 ASSERT3U(db
->db_level
, <, dn
->dn_nlevels
);
1066 ASSERT(db
->db_blkid
== DMU_BONUS_BLKID
||
1067 db
->db_blkid
== DMU_SPILL_BLKID
||
1068 !avl_is_empty(&dn
->dn_dbufs
));
1070 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1072 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
1073 ASSERT3U(db
->db
.db_offset
, ==, DMU_BONUS_BLKID
);
1074 } else if (db
->db_blkid
== DMU_SPILL_BLKID
) {
1076 ASSERT0(db
->db
.db_offset
);
1078 ASSERT3U(db
->db
.db_offset
, ==, db
->db_blkid
* db
->db
.db_size
);
1081 if ((dr
= list_head(&db
->db_dirty_records
)) != NULL
) {
1082 ASSERT(dr
->dr_dbuf
== db
);
1083 txg_prev
= dr
->dr_txg
;
1084 for (dr
= list_next(&db
->db_dirty_records
, dr
); dr
!= NULL
;
1085 dr
= list_next(&db
->db_dirty_records
, dr
)) {
1086 ASSERT(dr
->dr_dbuf
== db
);
1087 ASSERT(txg_prev
> dr
->dr_txg
);
1088 txg_prev
= dr
->dr_txg
;
1093 * We can't assert that db_size matches dn_datablksz because it
1094 * can be momentarily different when another thread is doing
1095 * dnode_set_blksz().
1097 if (db
->db_level
== 0 && db
->db
.db_object
== DMU_META_DNODE_OBJECT
) {
1098 dr
= db
->db_data_pending
;
1100 * It should only be modified in syncing context, so
1101 * make sure we only have one copy of the data.
1103 ASSERT(dr
== NULL
|| dr
->dt
.dl
.dr_data
== db
->db_buf
);
1106 /* verify db->db_blkptr */
1107 if (db
->db_blkptr
) {
1108 if (db
->db_parent
== dn
->dn_dbuf
) {
1109 /* db is pointed to by the dnode */
1110 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
1111 if (DMU_OBJECT_IS_SPECIAL(db
->db
.db_object
))
1112 ASSERT(db
->db_parent
== NULL
);
1114 ASSERT(db
->db_parent
!= NULL
);
1115 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
1116 ASSERT3P(db
->db_blkptr
, ==,
1117 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
1119 /* db is pointed to by an indirect block */
1120 int epb __maybe_unused
= db
->db_parent
->db
.db_size
>>
1122 ASSERT3U(db
->db_parent
->db_level
, ==, db
->db_level
+1);
1123 ASSERT3U(db
->db_parent
->db
.db_object
, ==,
1126 * dnode_grow_indblksz() can make this fail if we don't
1127 * have the parent's rwlock. XXX indblksz no longer
1128 * grows. safe to do this now?
1130 if (RW_LOCK_HELD(&db
->db_parent
->db_rwlock
)) {
1131 ASSERT3P(db
->db_blkptr
, ==,
1132 ((blkptr_t
*)db
->db_parent
->db
.db_data
+
1133 db
->db_blkid
% epb
));
1137 if ((db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
)) &&
1138 (db
->db_buf
== NULL
|| db
->db_buf
->b_data
) &&
1139 db
->db
.db_data
&& db
->db_blkid
!= DMU_BONUS_BLKID
&&
1140 db
->db_state
!= DB_FILL
&& !dn
->dn_free_txg
) {
1142 * If the blkptr isn't set but they have nonzero data,
1143 * it had better be dirty, otherwise we'll lose that
1144 * data when we evict this buffer.
1146 * There is an exception to this rule for indirect blocks; in
1147 * this case, if the indirect block is a hole, we fill in a few
1148 * fields on each of the child blocks (importantly, birth time)
1149 * to prevent hole birth times from being lost when you
1150 * partially fill in a hole.
1152 if (db
->db_dirtycnt
== 0) {
1153 if (db
->db_level
== 0) {
1154 uint64_t *buf
= db
->db
.db_data
;
1157 for (i
= 0; i
< db
->db
.db_size
>> 3; i
++) {
1158 ASSERT(buf
[i
] == 0);
1161 blkptr_t
*bps
= db
->db
.db_data
;
1162 ASSERT3U(1 << DB_DNODE(db
)->dn_indblkshift
, ==,
1165 * We want to verify that all the blkptrs in the
1166 * indirect block are holes, but we may have
1167 * automatically set up a few fields for them.
1168 * We iterate through each blkptr and verify
1169 * they only have those fields set.
1172 i
< db
->db
.db_size
/ sizeof (blkptr_t
);
1174 blkptr_t
*bp
= &bps
[i
];
1175 ASSERT(ZIO_CHECKSUM_IS_ZERO(
1178 DVA_IS_EMPTY(&bp
->blk_dva
[0]) &&
1179 DVA_IS_EMPTY(&bp
->blk_dva
[1]) &&
1180 DVA_IS_EMPTY(&bp
->blk_dva
[2]));
1181 ASSERT0(bp
->blk_fill
);
1182 ASSERT0(bp
->blk_pad
[0]);
1183 ASSERT0(bp
->blk_pad
[1]);
1184 ASSERT(!BP_IS_EMBEDDED(bp
));
1185 ASSERT(BP_IS_HOLE(bp
));
1186 ASSERT0(bp
->blk_phys_birth
);
1196 dbuf_clear_data(dmu_buf_impl_t
*db
)
1198 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1199 dbuf_evict_user(db
);
1200 ASSERT3P(db
->db_buf
, ==, NULL
);
1201 db
->db
.db_data
= NULL
;
1202 if (db
->db_state
!= DB_NOFILL
) {
1203 db
->db_state
= DB_UNCACHED
;
1204 DTRACE_SET_STATE(db
, "clear data");
1209 dbuf_set_data(dmu_buf_impl_t
*db
, arc_buf_t
*buf
)
1211 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1212 ASSERT(buf
!= NULL
);
1215 ASSERT(buf
->b_data
!= NULL
);
1216 db
->db
.db_data
= buf
->b_data
;
1220 dbuf_alloc_arcbuf(dmu_buf_impl_t
*db
)
1222 spa_t
*spa
= db
->db_objset
->os_spa
;
1224 return (arc_alloc_buf(spa
, db
, DBUF_GET_BUFC_TYPE(db
), db
->db
.db_size
));
1228 * Loan out an arc_buf for read. Return the loaned arc_buf.
1231 dbuf_loan_arcbuf(dmu_buf_impl_t
*db
)
1235 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1236 mutex_enter(&db
->db_mtx
);
1237 if (arc_released(db
->db_buf
) || zfs_refcount_count(&db
->db_holds
) > 1) {
1238 int blksz
= db
->db
.db_size
;
1239 spa_t
*spa
= db
->db_objset
->os_spa
;
1241 mutex_exit(&db
->db_mtx
);
1242 abuf
= arc_loan_buf(spa
, B_FALSE
, blksz
);
1243 bcopy(db
->db
.db_data
, abuf
->b_data
, blksz
);
1246 arc_loan_inuse_buf(abuf
, db
);
1248 dbuf_clear_data(db
);
1249 mutex_exit(&db
->db_mtx
);
1255 * Calculate which level n block references the data at the level 0 offset
1259 dbuf_whichblock(const dnode_t
*dn
, const int64_t level
, const uint64_t offset
)
1261 if (dn
->dn_datablkshift
!= 0 && dn
->dn_indblkshift
!= 0) {
1263 * The level n blkid is equal to the level 0 blkid divided by
1264 * the number of level 0s in a level n block.
1266 * The level 0 blkid is offset >> datablkshift =
1267 * offset / 2^datablkshift.
1269 * The number of level 0s in a level n is the number of block
1270 * pointers in an indirect block, raised to the power of level.
1271 * This is 2^(indblkshift - SPA_BLKPTRSHIFT)^level =
1272 * 2^(level*(indblkshift - SPA_BLKPTRSHIFT)).
1274 * Thus, the level n blkid is: offset /
1275 * ((2^datablkshift)*(2^(level*(indblkshift-SPA_BLKPTRSHIFT))))
1276 * = offset / 2^(datablkshift + level *
1277 * (indblkshift - SPA_BLKPTRSHIFT))
1278 * = offset >> (datablkshift + level *
1279 * (indblkshift - SPA_BLKPTRSHIFT))
1282 const unsigned exp
= dn
->dn_datablkshift
+
1283 level
* (dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
);
1285 if (exp
>= 8 * sizeof (offset
)) {
1286 /* This only happens on the highest indirection level */
1287 ASSERT3U(level
, ==, dn
->dn_nlevels
- 1);
1291 ASSERT3U(exp
, <, 8 * sizeof (offset
));
1293 return (offset
>> exp
);
1295 ASSERT3U(offset
, <, dn
->dn_datablksz
);
1301 * This function is used to lock the parent of the provided dbuf. This should be
1302 * used when modifying or reading db_blkptr.
1305 dmu_buf_lock_parent(dmu_buf_impl_t
*db
, krw_t rw
, void *tag
)
1307 enum db_lock_type ret
= DLT_NONE
;
1308 if (db
->db_parent
!= NULL
) {
1309 rw_enter(&db
->db_parent
->db_rwlock
, rw
);
1311 } else if (dmu_objset_ds(db
->db_objset
) != NULL
) {
1312 rrw_enter(&dmu_objset_ds(db
->db_objset
)->ds_bp_rwlock
, rw
,
1317 * We only return a DLT_NONE lock when it's the top-most indirect block
1318 * of the meta-dnode of the MOS.
1324 * We need to pass the lock type in because it's possible that the block will
1325 * move from being the topmost indirect block in a dnode (and thus, have no
1326 * parent) to not the top-most via an indirection increase. This would cause a
1327 * panic if we didn't pass the lock type in.
1330 dmu_buf_unlock_parent(dmu_buf_impl_t
*db
, db_lock_type_t type
, void *tag
)
1332 if (type
== DLT_PARENT
)
1333 rw_exit(&db
->db_parent
->db_rwlock
);
1334 else if (type
== DLT_OBJSET
)
1335 rrw_exit(&dmu_objset_ds(db
->db_objset
)->ds_bp_rwlock
, tag
);
1339 dbuf_read_done(zio_t
*zio
, const zbookmark_phys_t
*zb
, const blkptr_t
*bp
,
1340 arc_buf_t
*buf
, void *vdb
)
1342 dmu_buf_impl_t
*db
= vdb
;
1344 mutex_enter(&db
->db_mtx
);
1345 ASSERT3U(db
->db_state
, ==, DB_READ
);
1347 * All reads are synchronous, so we must have a hold on the dbuf
1349 ASSERT(zfs_refcount_count(&db
->db_holds
) > 0);
1350 ASSERT(db
->db_buf
== NULL
);
1351 ASSERT(db
->db
.db_data
== NULL
);
1354 ASSERT(zio
== NULL
|| zio
->io_error
!= 0);
1355 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1356 ASSERT3P(db
->db_buf
, ==, NULL
);
1357 db
->db_state
= DB_UNCACHED
;
1358 DTRACE_SET_STATE(db
, "i/o error");
1359 } else if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
1360 /* freed in flight */
1361 ASSERT(zio
== NULL
|| zio
->io_error
== 0);
1362 arc_release(buf
, db
);
1363 bzero(buf
->b_data
, db
->db
.db_size
);
1364 arc_buf_freeze(buf
);
1365 db
->db_freed_in_flight
= FALSE
;
1366 dbuf_set_data(db
, buf
);
1367 db
->db_state
= DB_CACHED
;
1368 DTRACE_SET_STATE(db
, "freed in flight");
1371 ASSERT(zio
== NULL
|| zio
->io_error
== 0);
1372 dbuf_set_data(db
, buf
);
1373 db
->db_state
= DB_CACHED
;
1374 DTRACE_SET_STATE(db
, "successful read");
1376 cv_broadcast(&db
->db_changed
);
1377 dbuf_rele_and_unlock(db
, NULL
, B_FALSE
);
1381 * Shortcut for performing reads on bonus dbufs. Returns
1382 * an error if we fail to verify the dnode associated with
1383 * a decrypted block. Otherwise success.
1386 dbuf_read_bonus(dmu_buf_impl_t
*db
, dnode_t
*dn
, uint32_t flags
)
1388 int bonuslen
, max_bonuslen
, err
;
1390 err
= dbuf_read_verify_dnode_crypt(db
, flags
);
1394 bonuslen
= MIN(dn
->dn_bonuslen
, dn
->dn_phys
->dn_bonuslen
);
1395 max_bonuslen
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
);
1396 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1397 ASSERT(DB_DNODE_HELD(db
));
1398 ASSERT3U(bonuslen
, <=, db
->db
.db_size
);
1399 db
->db
.db_data
= kmem_alloc(max_bonuslen
, KM_SLEEP
);
1400 arc_space_consume(max_bonuslen
, ARC_SPACE_BONUS
);
1401 if (bonuslen
< max_bonuslen
)
1402 bzero(db
->db
.db_data
, max_bonuslen
);
1404 bcopy(DN_BONUS(dn
->dn_phys
), db
->db
.db_data
, bonuslen
);
1405 db
->db_state
= DB_CACHED
;
1406 DTRACE_SET_STATE(db
, "bonus buffer filled");
1411 dbuf_handle_indirect_hole(dmu_buf_impl_t
*db
, dnode_t
*dn
)
1413 blkptr_t
*bps
= db
->db
.db_data
;
1414 uint32_t indbs
= 1ULL << dn
->dn_indblkshift
;
1415 int n_bps
= indbs
>> SPA_BLKPTRSHIFT
;
1417 for (int i
= 0; i
< n_bps
; i
++) {
1418 blkptr_t
*bp
= &bps
[i
];
1420 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==, indbs
);
1421 BP_SET_LSIZE(bp
, BP_GET_LEVEL(db
->db_blkptr
) == 1 ?
1422 dn
->dn_datablksz
: BP_GET_LSIZE(db
->db_blkptr
));
1423 BP_SET_TYPE(bp
, BP_GET_TYPE(db
->db_blkptr
));
1424 BP_SET_LEVEL(bp
, BP_GET_LEVEL(db
->db_blkptr
) - 1);
1425 BP_SET_BIRTH(bp
, db
->db_blkptr
->blk_birth
, 0);
1430 * Handle reads on dbufs that are holes, if necessary. This function
1431 * requires that the dbuf's mutex is held. Returns success (0) if action
1432 * was taken, ENOENT if no action was taken.
1435 dbuf_read_hole(dmu_buf_impl_t
*db
, dnode_t
*dn
, uint32_t flags
)
1437 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1439 int is_hole
= db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
);
1441 * For level 0 blocks only, if the above check fails:
1442 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
1443 * processes the delete record and clears the bp while we are waiting
1444 * for the dn_mtx (resulting in a "no" from block_freed).
1446 if (!is_hole
&& db
->db_level
== 0) {
1447 is_hole
= dnode_block_freed(dn
, db
->db_blkid
) ||
1448 BP_IS_HOLE(db
->db_blkptr
);
1452 dbuf_set_data(db
, dbuf_alloc_arcbuf(db
));
1453 bzero(db
->db
.db_data
, db
->db
.db_size
);
1455 if (db
->db_blkptr
!= NULL
&& db
->db_level
> 0 &&
1456 BP_IS_HOLE(db
->db_blkptr
) &&
1457 db
->db_blkptr
->blk_birth
!= 0) {
1458 dbuf_handle_indirect_hole(db
, dn
);
1460 db
->db_state
= DB_CACHED
;
1461 DTRACE_SET_STATE(db
, "hole read satisfied");
1468 * This function ensures that, when doing a decrypting read of a block,
1469 * we make sure we have decrypted the dnode associated with it. We must do
1470 * this so that we ensure we are fully authenticating the checksum-of-MACs
1471 * tree from the root of the objset down to this block. Indirect blocks are
1472 * always verified against their secure checksum-of-MACs assuming that the
1473 * dnode containing them is correct. Now that we are doing a decrypting read,
1474 * we can be sure that the key is loaded and verify that assumption. This is
1475 * especially important considering that we always read encrypted dnode
1476 * blocks as raw data (without verifying their MACs) to start, and
1477 * decrypt / authenticate them when we need to read an encrypted bonus buffer.
1480 dbuf_read_verify_dnode_crypt(dmu_buf_impl_t
*db
, uint32_t flags
)
1483 objset_t
*os
= db
->db_objset
;
1484 arc_buf_t
*dnode_abuf
;
1486 zbookmark_phys_t zb
;
1488 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1490 if (!os
->os_encrypted
|| os
->os_raw_receive
||
1491 (flags
& DB_RF_NO_DECRYPT
) != 0)
1496 dnode_abuf
= (dn
->dn_dbuf
!= NULL
) ? dn
->dn_dbuf
->db_buf
: NULL
;
1498 if (dnode_abuf
== NULL
|| !arc_is_encrypted(dnode_abuf
)) {
1503 SET_BOOKMARK(&zb
, dmu_objset_id(os
),
1504 DMU_META_DNODE_OBJECT
, 0, dn
->dn_dbuf
->db_blkid
);
1505 err
= arc_untransform(dnode_abuf
, os
->os_spa
, &zb
, B_TRUE
);
1508 * An error code of EACCES tells us that the key is still not
1509 * available. This is ok if we are only reading authenticated
1510 * (and therefore non-encrypted) blocks.
1512 if (err
== EACCES
&& ((db
->db_blkid
!= DMU_BONUS_BLKID
&&
1513 !DMU_OT_IS_ENCRYPTED(dn
->dn_type
)) ||
1514 (db
->db_blkid
== DMU_BONUS_BLKID
&&
1515 !DMU_OT_IS_ENCRYPTED(dn
->dn_bonustype
))))
1524 * Drops db_mtx and the parent lock specified by dblt and tag before
1528 dbuf_read_impl(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t flags
,
1529 db_lock_type_t dblt
, void *tag
)
1532 zbookmark_phys_t zb
;
1533 uint32_t aflags
= ARC_FLAG_NOWAIT
;
1536 err
= zio_flags
= 0;
1539 ASSERT(!zfs_refcount_is_zero(&db
->db_holds
));
1540 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1541 ASSERT(db
->db_state
== DB_UNCACHED
);
1542 ASSERT(db
->db_buf
== NULL
);
1543 ASSERT(db
->db_parent
== NULL
||
1544 RW_LOCK_HELD(&db
->db_parent
->db_rwlock
));
1546 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1547 err
= dbuf_read_bonus(db
, dn
, flags
);
1551 err
= dbuf_read_hole(db
, dn
, flags
);
1556 * Any attempt to read a redacted block should result in an error. This
1557 * will never happen under normal conditions, but can be useful for
1558 * debugging purposes.
1560 if (BP_IS_REDACTED(db
->db_blkptr
)) {
1561 ASSERT(dsl_dataset_feature_is_active(
1562 db
->db_objset
->os_dsl_dataset
,
1563 SPA_FEATURE_REDACTED_DATASETS
));
1564 err
= SET_ERROR(EIO
);
1568 SET_BOOKMARK(&zb
, dmu_objset_id(db
->db_objset
),
1569 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
1572 * All bps of an encrypted os should have the encryption bit set.
1573 * If this is not true it indicates tampering and we report an error.
1575 if (db
->db_objset
->os_encrypted
&& !BP_USES_CRYPT(db
->db_blkptr
)) {
1576 spa_log_error(db
->db_objset
->os_spa
, &zb
);
1577 zfs_panic_recover("unencrypted block in encrypted "
1578 "object set %llu", dmu_objset_id(db
->db_objset
));
1579 err
= SET_ERROR(EIO
);
1583 err
= dbuf_read_verify_dnode_crypt(db
, flags
);
1589 db
->db_state
= DB_READ
;
1590 DTRACE_SET_STATE(db
, "read issued");
1591 mutex_exit(&db
->db_mtx
);
1593 if (dbuf_is_l2cacheable(db
))
1594 aflags
|= ARC_FLAG_L2CACHE
;
1596 dbuf_add_ref(db
, NULL
);
1598 zio_flags
= (flags
& DB_RF_CANFAIL
) ?
1599 ZIO_FLAG_CANFAIL
: ZIO_FLAG_MUSTSUCCEED
;
1601 if ((flags
& DB_RF_NO_DECRYPT
) && BP_IS_PROTECTED(db
->db_blkptr
))
1602 zio_flags
|= ZIO_FLAG_RAW
;
1604 * The zio layer will copy the provided blkptr later, but we need to
1605 * do this now so that we can release the parent's rwlock. We have to
1606 * do that now so that if dbuf_read_done is called synchronously (on
1607 * an l1 cache hit) we don't acquire the db_mtx while holding the
1608 * parent's rwlock, which would be a lock ordering violation.
1610 blkptr_t bp
= *db
->db_blkptr
;
1611 dmu_buf_unlock_parent(db
, dblt
, tag
);
1612 (void) arc_read(zio
, db
->db_objset
->os_spa
, &bp
,
1613 dbuf_read_done
, db
, ZIO_PRIORITY_SYNC_READ
, zio_flags
,
1618 mutex_exit(&db
->db_mtx
);
1619 dmu_buf_unlock_parent(db
, dblt
, tag
);
1624 * This is our just-in-time copy function. It makes a copy of buffers that
1625 * have been modified in a previous transaction group before we access them in
1626 * the current active group.
1628 * This function is used in three places: when we are dirtying a buffer for the
1629 * first time in a txg, when we are freeing a range in a dnode that includes
1630 * this buffer, and when we are accessing a buffer which was received compressed
1631 * and later referenced in a WRITE_BYREF record.
1633 * Note that when we are called from dbuf_free_range() we do not put a hold on
1634 * the buffer, we just traverse the active dbuf list for the dnode.
1637 dbuf_fix_old_data(dmu_buf_impl_t
*db
, uint64_t txg
)
1639 dbuf_dirty_record_t
*dr
= list_head(&db
->db_dirty_records
);
1641 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1642 ASSERT(db
->db
.db_data
!= NULL
);
1643 ASSERT(db
->db_level
== 0);
1644 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
);
1647 (dr
->dt
.dl
.dr_data
!=
1648 ((db
->db_blkid
== DMU_BONUS_BLKID
) ? db
->db
.db_data
: db
->db_buf
)))
1652 * If the last dirty record for this dbuf has not yet synced
1653 * and its referencing the dbuf data, either:
1654 * reset the reference to point to a new copy,
1655 * or (if there a no active holders)
1656 * just null out the current db_data pointer.
1658 ASSERT3U(dr
->dr_txg
, >=, txg
- 2);
1659 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1660 dnode_t
*dn
= DB_DNODE(db
);
1661 int bonuslen
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
);
1662 dr
->dt
.dl
.dr_data
= kmem_alloc(bonuslen
, KM_SLEEP
);
1663 arc_space_consume(bonuslen
, ARC_SPACE_BONUS
);
1664 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
, bonuslen
);
1665 } else if (zfs_refcount_count(&db
->db_holds
) > db
->db_dirtycnt
) {
1666 dnode_t
*dn
= DB_DNODE(db
);
1667 int size
= arc_buf_size(db
->db_buf
);
1668 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1669 spa_t
*spa
= db
->db_objset
->os_spa
;
1670 enum zio_compress compress_type
=
1671 arc_get_compression(db
->db_buf
);
1672 uint8_t complevel
= arc_get_complevel(db
->db_buf
);
1674 if (arc_is_encrypted(db
->db_buf
)) {
1675 boolean_t byteorder
;
1676 uint8_t salt
[ZIO_DATA_SALT_LEN
];
1677 uint8_t iv
[ZIO_DATA_IV_LEN
];
1678 uint8_t mac
[ZIO_DATA_MAC_LEN
];
1680 arc_get_raw_params(db
->db_buf
, &byteorder
, salt
,
1682 dr
->dt
.dl
.dr_data
= arc_alloc_raw_buf(spa
, db
,
1683 dmu_objset_id(dn
->dn_objset
), byteorder
, salt
, iv
,
1684 mac
, dn
->dn_type
, size
, arc_buf_lsize(db
->db_buf
),
1685 compress_type
, complevel
);
1686 } else if (compress_type
!= ZIO_COMPRESS_OFF
) {
1687 ASSERT3U(type
, ==, ARC_BUFC_DATA
);
1688 dr
->dt
.dl
.dr_data
= arc_alloc_compressed_buf(spa
, db
,
1689 size
, arc_buf_lsize(db
->db_buf
), compress_type
,
1692 dr
->dt
.dl
.dr_data
= arc_alloc_buf(spa
, db
, type
, size
);
1694 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
->b_data
, size
);
1697 dbuf_clear_data(db
);
1702 dbuf_read(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t flags
)
1709 * We don't have to hold the mutex to check db_state because it
1710 * can't be freed while we have a hold on the buffer.
1712 ASSERT(!zfs_refcount_is_zero(&db
->db_holds
));
1714 if (db
->db_state
== DB_NOFILL
)
1715 return (SET_ERROR(EIO
));
1720 prefetch
= db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1721 (flags
& DB_RF_NOPREFETCH
) == 0 && dn
!= NULL
&&
1722 DBUF_IS_CACHEABLE(db
);
1724 mutex_enter(&db
->db_mtx
);
1725 if (db
->db_state
== DB_CACHED
) {
1726 spa_t
*spa
= dn
->dn_objset
->os_spa
;
1729 * Ensure that this block's dnode has been decrypted if
1730 * the caller has requested decrypted data.
1732 err
= dbuf_read_verify_dnode_crypt(db
, flags
);
1735 * If the arc buf is compressed or encrypted and the caller
1736 * requested uncompressed data, we need to untransform it
1737 * before returning. We also call arc_untransform() on any
1738 * unauthenticated blocks, which will verify their MAC if
1739 * the key is now available.
1741 if (err
== 0 && db
->db_buf
!= NULL
&&
1742 (flags
& DB_RF_NO_DECRYPT
) == 0 &&
1743 (arc_is_encrypted(db
->db_buf
) ||
1744 arc_is_unauthenticated(db
->db_buf
) ||
1745 arc_get_compression(db
->db_buf
) != ZIO_COMPRESS_OFF
)) {
1746 zbookmark_phys_t zb
;
1748 SET_BOOKMARK(&zb
, dmu_objset_id(db
->db_objset
),
1749 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
1750 dbuf_fix_old_data(db
, spa_syncing_txg(spa
));
1751 err
= arc_untransform(db
->db_buf
, spa
, &zb
, B_FALSE
);
1752 dbuf_set_data(db
, db
->db_buf
);
1754 mutex_exit(&db
->db_mtx
);
1755 if (err
== 0 && prefetch
) {
1756 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
,
1757 B_FALSE
, flags
& DB_RF_HAVESTRUCT
);
1760 DBUF_STAT_BUMP(hash_hits
);
1761 } else if (db
->db_state
== DB_UNCACHED
) {
1762 spa_t
*spa
= dn
->dn_objset
->os_spa
;
1763 boolean_t need_wait
= B_FALSE
;
1765 db_lock_type_t dblt
= dmu_buf_lock_parent(db
, RW_READER
, FTAG
);
1768 db
->db_blkptr
!= NULL
&& !BP_IS_HOLE(db
->db_blkptr
)) {
1769 zio
= zio_root(spa
, NULL
, NULL
, ZIO_FLAG_CANFAIL
);
1772 err
= dbuf_read_impl(db
, zio
, flags
, dblt
, FTAG
);
1774 * dbuf_read_impl has dropped db_mtx and our parent's rwlock
1777 if (!err
&& prefetch
) {
1778 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
,
1779 db
->db_state
!= DB_CACHED
,
1780 flags
& DB_RF_HAVESTRUCT
);
1784 DBUF_STAT_BUMP(hash_misses
);
1787 * If we created a zio_root we must execute it to avoid
1788 * leaking it, even if it isn't attached to any work due
1789 * to an error in dbuf_read_impl().
1793 err
= zio_wait(zio
);
1795 VERIFY0(zio_wait(zio
));
1799 * Another reader came in while the dbuf was in flight
1800 * between UNCACHED and CACHED. Either a writer will finish
1801 * writing the buffer (sending the dbuf to CACHED) or the
1802 * first reader's request will reach the read_done callback
1803 * and send the dbuf to CACHED. Otherwise, a failure
1804 * occurred and the dbuf went to UNCACHED.
1806 mutex_exit(&db
->db_mtx
);
1808 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
,
1809 B_TRUE
, flags
& DB_RF_HAVESTRUCT
);
1812 DBUF_STAT_BUMP(hash_misses
);
1814 /* Skip the wait per the caller's request. */
1815 if ((flags
& DB_RF_NEVERWAIT
) == 0) {
1816 mutex_enter(&db
->db_mtx
);
1817 while (db
->db_state
== DB_READ
||
1818 db
->db_state
== DB_FILL
) {
1819 ASSERT(db
->db_state
== DB_READ
||
1820 (flags
& DB_RF_HAVESTRUCT
) == 0);
1821 DTRACE_PROBE2(blocked__read
, dmu_buf_impl_t
*,
1823 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1825 if (db
->db_state
== DB_UNCACHED
)
1826 err
= SET_ERROR(EIO
);
1827 mutex_exit(&db
->db_mtx
);
1835 dbuf_noread(dmu_buf_impl_t
*db
)
1837 ASSERT(!zfs_refcount_is_zero(&db
->db_holds
));
1838 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1839 mutex_enter(&db
->db_mtx
);
1840 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
1841 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1842 if (db
->db_state
== DB_UNCACHED
) {
1843 ASSERT(db
->db_buf
== NULL
);
1844 ASSERT(db
->db
.db_data
== NULL
);
1845 dbuf_set_data(db
, dbuf_alloc_arcbuf(db
));
1846 db
->db_state
= DB_FILL
;
1847 DTRACE_SET_STATE(db
, "assigning filled buffer");
1848 } else if (db
->db_state
== DB_NOFILL
) {
1849 dbuf_clear_data(db
);
1851 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
1853 mutex_exit(&db
->db_mtx
);
1857 dbuf_unoverride(dbuf_dirty_record_t
*dr
)
1859 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
1860 blkptr_t
*bp
= &dr
->dt
.dl
.dr_overridden_by
;
1861 uint64_t txg
= dr
->dr_txg
;
1863 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1865 * This assert is valid because dmu_sync() expects to be called by
1866 * a zilog's get_data while holding a range lock. This call only
1867 * comes from dbuf_dirty() callers who must also hold a range lock.
1869 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_IN_DMU_SYNC
);
1870 ASSERT(db
->db_level
== 0);
1872 if (db
->db_blkid
== DMU_BONUS_BLKID
||
1873 dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
)
1876 ASSERT(db
->db_data_pending
!= dr
);
1878 /* free this block */
1879 if (!BP_IS_HOLE(bp
) && !dr
->dt
.dl
.dr_nopwrite
)
1880 zio_free(db
->db_objset
->os_spa
, txg
, bp
);
1882 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
1883 dr
->dt
.dl
.dr_nopwrite
= B_FALSE
;
1884 dr
->dt
.dl
.dr_has_raw_params
= B_FALSE
;
1887 * Release the already-written buffer, so we leave it in
1888 * a consistent dirty state. Note that all callers are
1889 * modifying the buffer, so they will immediately do
1890 * another (redundant) arc_release(). Therefore, leave
1891 * the buf thawed to save the effort of freezing &
1892 * immediately re-thawing it.
1894 arc_release(dr
->dt
.dl
.dr_data
, db
);
1898 * Evict (if its unreferenced) or clear (if its referenced) any level-0
1899 * data blocks in the free range, so that any future readers will find
1903 dbuf_free_range(dnode_t
*dn
, uint64_t start_blkid
, uint64_t end_blkid
,
1906 dmu_buf_impl_t
*db_search
;
1907 dmu_buf_impl_t
*db
, *db_next
;
1908 uint64_t txg
= tx
->tx_txg
;
1910 dbuf_dirty_record_t
*dr
;
1912 if (end_blkid
> dn
->dn_maxblkid
&&
1913 !(start_blkid
== DMU_SPILL_BLKID
|| end_blkid
== DMU_SPILL_BLKID
))
1914 end_blkid
= dn
->dn_maxblkid
;
1915 dprintf_dnode(dn
, "start=%llu end=%llu\n", (u_longlong_t
)start_blkid
,
1916 (u_longlong_t
)end_blkid
);
1918 db_search
= kmem_alloc(sizeof (dmu_buf_impl_t
), KM_SLEEP
);
1919 db_search
->db_level
= 0;
1920 db_search
->db_blkid
= start_blkid
;
1921 db_search
->db_state
= DB_SEARCH
;
1923 mutex_enter(&dn
->dn_dbufs_mtx
);
1924 db
= avl_find(&dn
->dn_dbufs
, db_search
, &where
);
1925 ASSERT3P(db
, ==, NULL
);
1927 db
= avl_nearest(&dn
->dn_dbufs
, where
, AVL_AFTER
);
1929 for (; db
!= NULL
; db
= db_next
) {
1930 db_next
= AVL_NEXT(&dn
->dn_dbufs
, db
);
1931 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1933 if (db
->db_level
!= 0 || db
->db_blkid
> end_blkid
) {
1936 ASSERT3U(db
->db_blkid
, >=, start_blkid
);
1938 /* found a level 0 buffer in the range */
1939 mutex_enter(&db
->db_mtx
);
1940 if (dbuf_undirty(db
, tx
)) {
1941 /* mutex has been dropped and dbuf destroyed */
1945 if (db
->db_state
== DB_UNCACHED
||
1946 db
->db_state
== DB_NOFILL
||
1947 db
->db_state
== DB_EVICTING
) {
1948 ASSERT(db
->db
.db_data
== NULL
);
1949 mutex_exit(&db
->db_mtx
);
1952 if (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
) {
1953 /* will be handled in dbuf_read_done or dbuf_rele */
1954 db
->db_freed_in_flight
= TRUE
;
1955 mutex_exit(&db
->db_mtx
);
1958 if (zfs_refcount_count(&db
->db_holds
) == 0) {
1963 /* The dbuf is referenced */
1965 dr
= list_head(&db
->db_dirty_records
);
1967 if (dr
->dr_txg
== txg
) {
1969 * This buffer is "in-use", re-adjust the file
1970 * size to reflect that this buffer may
1971 * contain new data when we sync.
1973 if (db
->db_blkid
!= DMU_SPILL_BLKID
&&
1974 db
->db_blkid
> dn
->dn_maxblkid
)
1975 dn
->dn_maxblkid
= db
->db_blkid
;
1976 dbuf_unoverride(dr
);
1979 * This dbuf is not dirty in the open context.
1980 * Either uncache it (if its not referenced in
1981 * the open context) or reset its contents to
1984 dbuf_fix_old_data(db
, txg
);
1987 /* clear the contents if its cached */
1988 if (db
->db_state
== DB_CACHED
) {
1989 ASSERT(db
->db
.db_data
!= NULL
);
1990 arc_release(db
->db_buf
, db
);
1991 rw_enter(&db
->db_rwlock
, RW_WRITER
);
1992 bzero(db
->db
.db_data
, db
->db
.db_size
);
1993 rw_exit(&db
->db_rwlock
);
1994 arc_buf_freeze(db
->db_buf
);
1997 mutex_exit(&db
->db_mtx
);
2000 kmem_free(db_search
, sizeof (dmu_buf_impl_t
));
2001 mutex_exit(&dn
->dn_dbufs_mtx
);
2005 dbuf_new_size(dmu_buf_impl_t
*db
, int size
, dmu_tx_t
*tx
)
2007 arc_buf_t
*buf
, *old_buf
;
2008 dbuf_dirty_record_t
*dr
;
2009 int osize
= db
->db
.db_size
;
2010 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
2013 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2019 * XXX we should be doing a dbuf_read, checking the return
2020 * value and returning that up to our callers
2022 dmu_buf_will_dirty(&db
->db
, tx
);
2024 /* create the data buffer for the new block */
2025 buf
= arc_alloc_buf(dn
->dn_objset
->os_spa
, db
, type
, size
);
2027 /* copy old block data to the new block */
2028 old_buf
= db
->db_buf
;
2029 bcopy(old_buf
->b_data
, buf
->b_data
, MIN(osize
, size
));
2030 /* zero the remainder */
2032 bzero((uint8_t *)buf
->b_data
+ osize
, size
- osize
);
2034 mutex_enter(&db
->db_mtx
);
2035 dbuf_set_data(db
, buf
);
2036 arc_buf_destroy(old_buf
, db
);
2037 db
->db
.db_size
= size
;
2039 dr
= list_head(&db
->db_dirty_records
);
2040 /* dirty record added by dmu_buf_will_dirty() */
2042 if (db
->db_level
== 0)
2043 dr
->dt
.dl
.dr_data
= buf
;
2044 ASSERT3U(dr
->dr_txg
, ==, tx
->tx_txg
);
2045 ASSERT3U(dr
->dr_accounted
, ==, osize
);
2046 dr
->dr_accounted
= size
;
2047 mutex_exit(&db
->db_mtx
);
2049 dmu_objset_willuse_space(dn
->dn_objset
, size
- osize
, tx
);
2054 dbuf_release_bp(dmu_buf_impl_t
*db
)
2056 objset_t
*os __maybe_unused
= db
->db_objset
;
2058 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os
)));
2059 ASSERT(arc_released(os
->os_phys_buf
) ||
2060 list_link_active(&os
->os_dsl_dataset
->ds_synced_link
));
2061 ASSERT(db
->db_parent
== NULL
|| arc_released(db
->db_parent
->db_buf
));
2063 (void) arc_release(db
->db_buf
, db
);
2067 * We already have a dirty record for this TXG, and we are being
2071 dbuf_redirty(dbuf_dirty_record_t
*dr
)
2073 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2075 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2077 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
) {
2079 * If this buffer has already been written out,
2080 * we now need to reset its state.
2082 dbuf_unoverride(dr
);
2083 if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
&&
2084 db
->db_state
!= DB_NOFILL
) {
2085 /* Already released on initial dirty, so just thaw. */
2086 ASSERT(arc_released(db
->db_buf
));
2087 arc_buf_thaw(db
->db_buf
);
2092 dbuf_dirty_record_t
*
2093 dbuf_dirty_lightweight(dnode_t
*dn
, uint64_t blkid
, dmu_tx_t
*tx
)
2095 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
2096 IMPLY(dn
->dn_objset
->os_raw_receive
, dn
->dn_maxblkid
>= blkid
);
2097 dnode_new_blkid(dn
, blkid
, tx
, B_TRUE
, B_FALSE
);
2098 ASSERT(dn
->dn_maxblkid
>= blkid
);
2100 dbuf_dirty_record_t
*dr
= kmem_zalloc(sizeof (*dr
), KM_SLEEP
);
2101 list_link_init(&dr
->dr_dirty_node
);
2102 list_link_init(&dr
->dr_dbuf_node
);
2104 dr
->dr_txg
= tx
->tx_txg
;
2105 dr
->dt
.dll
.dr_blkid
= blkid
;
2106 dr
->dr_accounted
= dn
->dn_datablksz
;
2109 * There should not be any dbuf for the block that we're dirtying.
2110 * Otherwise the buffer contents could be inconsistent between the
2111 * dbuf and the lightweight dirty record.
2113 ASSERT3P(NULL
, ==, dbuf_find(dn
->dn_objset
, dn
->dn_object
, 0, blkid
));
2115 mutex_enter(&dn
->dn_mtx
);
2116 int txgoff
= tx
->tx_txg
& TXG_MASK
;
2117 if (dn
->dn_free_ranges
[txgoff
] != NULL
) {
2118 range_tree_clear(dn
->dn_free_ranges
[txgoff
], blkid
, 1);
2121 if (dn
->dn_nlevels
== 1) {
2122 ASSERT3U(blkid
, <, dn
->dn_nblkptr
);
2123 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
2124 mutex_exit(&dn
->dn_mtx
);
2125 rw_exit(&dn
->dn_struct_rwlock
);
2126 dnode_setdirty(dn
, tx
);
2128 mutex_exit(&dn
->dn_mtx
);
2130 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2131 dmu_buf_impl_t
*parent_db
= dbuf_hold_level(dn
,
2132 1, blkid
>> epbs
, FTAG
);
2133 rw_exit(&dn
->dn_struct_rwlock
);
2134 if (parent_db
== NULL
) {
2135 kmem_free(dr
, sizeof (*dr
));
2138 int err
= dbuf_read(parent_db
, NULL
,
2139 (DB_RF_NOPREFETCH
| DB_RF_CANFAIL
));
2141 dbuf_rele(parent_db
, FTAG
);
2142 kmem_free(dr
, sizeof (*dr
));
2146 dbuf_dirty_record_t
*parent_dr
= dbuf_dirty(parent_db
, tx
);
2147 dbuf_rele(parent_db
, FTAG
);
2148 mutex_enter(&parent_dr
->dt
.di
.dr_mtx
);
2149 ASSERT3U(parent_dr
->dr_txg
, ==, tx
->tx_txg
);
2150 list_insert_tail(&parent_dr
->dt
.di
.dr_children
, dr
);
2151 mutex_exit(&parent_dr
->dt
.di
.dr_mtx
);
2152 dr
->dr_parent
= parent_dr
;
2155 dmu_objset_willuse_space(dn
->dn_objset
, dr
->dr_accounted
, tx
);
2160 dbuf_dirty_record_t
*
2161 dbuf_dirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
2165 dbuf_dirty_record_t
*dr
, *dr_next
, *dr_head
;
2166 int txgoff
= tx
->tx_txg
& TXG_MASK
;
2167 boolean_t drop_struct_rwlock
= B_FALSE
;
2169 ASSERT(tx
->tx_txg
!= 0);
2170 ASSERT(!zfs_refcount_is_zero(&db
->db_holds
));
2171 DMU_TX_DIRTY_BUF(tx
, db
);
2176 * Shouldn't dirty a regular buffer in syncing context. Private
2177 * objects may be dirtied in syncing context, but only if they
2178 * were already pre-dirtied in open context.
2181 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
) {
2182 rrw_enter(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
,
2185 ASSERT(!dmu_tx_is_syncing(tx
) ||
2186 BP_IS_HOLE(dn
->dn_objset
->os_rootbp
) ||
2187 DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
2188 dn
->dn_objset
->os_dsl_dataset
== NULL
);
2189 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
2190 rrw_exit(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
, FTAG
);
2193 * We make this assert for private objects as well, but after we
2194 * check if we're already dirty. They are allowed to re-dirty
2195 * in syncing context.
2197 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
2198 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
2199 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
2201 mutex_enter(&db
->db_mtx
);
2203 * XXX make this true for indirects too? The problem is that
2204 * transactions created with dmu_tx_create_assigned() from
2205 * syncing context don't bother holding ahead.
2207 ASSERT(db
->db_level
!= 0 ||
2208 db
->db_state
== DB_CACHED
|| db
->db_state
== DB_FILL
||
2209 db
->db_state
== DB_NOFILL
);
2211 mutex_enter(&dn
->dn_mtx
);
2212 dnode_set_dirtyctx(dn
, tx
, db
);
2213 if (tx
->tx_txg
> dn
->dn_dirty_txg
)
2214 dn
->dn_dirty_txg
= tx
->tx_txg
;
2215 mutex_exit(&dn
->dn_mtx
);
2217 if (db
->db_blkid
== DMU_SPILL_BLKID
)
2218 dn
->dn_have_spill
= B_TRUE
;
2221 * If this buffer is already dirty, we're done.
2223 dr_head
= list_head(&db
->db_dirty_records
);
2224 ASSERT(dr_head
== NULL
|| dr_head
->dr_txg
<= tx
->tx_txg
||
2225 db
->db
.db_object
== DMU_META_DNODE_OBJECT
);
2226 dr_next
= dbuf_find_dirty_lte(db
, tx
->tx_txg
);
2227 if (dr_next
&& dr_next
->dr_txg
== tx
->tx_txg
) {
2230 dbuf_redirty(dr_next
);
2231 mutex_exit(&db
->db_mtx
);
2236 * Only valid if not already dirty.
2238 ASSERT(dn
->dn_object
== 0 ||
2239 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
2240 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
2242 ASSERT3U(dn
->dn_nlevels
, >, db
->db_level
);
2245 * We should only be dirtying in syncing context if it's the
2246 * mos or we're initializing the os or it's a special object.
2247 * However, we are allowed to dirty in syncing context provided
2248 * we already dirtied it in open context. Hence we must make
2249 * this assertion only if we're not already dirty.
2252 VERIFY3U(tx
->tx_txg
, <=, spa_final_dirty_txg(os
->os_spa
));
2254 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
2255 rrw_enter(&os
->os_dsl_dataset
->ds_bp_rwlock
, RW_READER
, FTAG
);
2256 ASSERT(!dmu_tx_is_syncing(tx
) || DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
2257 os
->os_dsl_dataset
== NULL
|| BP_IS_HOLE(os
->os_rootbp
));
2258 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
2259 rrw_exit(&os
->os_dsl_dataset
->ds_bp_rwlock
, FTAG
);
2261 ASSERT(db
->db
.db_size
!= 0);
2263 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
2265 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
2266 dmu_objset_willuse_space(os
, db
->db
.db_size
, tx
);
2270 * If this buffer is dirty in an old transaction group we need
2271 * to make a copy of it so that the changes we make in this
2272 * transaction group won't leak out when we sync the older txg.
2274 dr
= kmem_zalloc(sizeof (dbuf_dirty_record_t
), KM_SLEEP
);
2275 list_link_init(&dr
->dr_dirty_node
);
2276 list_link_init(&dr
->dr_dbuf_node
);
2278 if (db
->db_level
== 0) {
2279 void *data_old
= db
->db_buf
;
2281 if (db
->db_state
!= DB_NOFILL
) {
2282 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2283 dbuf_fix_old_data(db
, tx
->tx_txg
);
2284 data_old
= db
->db
.db_data
;
2285 } else if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
) {
2287 * Release the data buffer from the cache so
2288 * that we can modify it without impacting
2289 * possible other users of this cached data
2290 * block. Note that indirect blocks and
2291 * private objects are not released until the
2292 * syncing state (since they are only modified
2295 arc_release(db
->db_buf
, db
);
2296 dbuf_fix_old_data(db
, tx
->tx_txg
);
2297 data_old
= db
->db_buf
;
2299 ASSERT(data_old
!= NULL
);
2301 dr
->dt
.dl
.dr_data
= data_old
;
2303 mutex_init(&dr
->dt
.di
.dr_mtx
, NULL
, MUTEX_NOLOCKDEP
, NULL
);
2304 list_create(&dr
->dt
.di
.dr_children
,
2305 sizeof (dbuf_dirty_record_t
),
2306 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
2308 if (db
->db_blkid
!= DMU_BONUS_BLKID
)
2309 dr
->dr_accounted
= db
->db
.db_size
;
2311 dr
->dr_txg
= tx
->tx_txg
;
2312 list_insert_before(&db
->db_dirty_records
, dr_next
, dr
);
2315 * We could have been freed_in_flight between the dbuf_noread
2316 * and dbuf_dirty. We win, as though the dbuf_noread() had
2317 * happened after the free.
2319 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
2320 db
->db_blkid
!= DMU_SPILL_BLKID
) {
2321 mutex_enter(&dn
->dn_mtx
);
2322 if (dn
->dn_free_ranges
[txgoff
] != NULL
) {
2323 range_tree_clear(dn
->dn_free_ranges
[txgoff
],
2326 mutex_exit(&dn
->dn_mtx
);
2327 db
->db_freed_in_flight
= FALSE
;
2331 * This buffer is now part of this txg
2333 dbuf_add_ref(db
, (void *)(uintptr_t)tx
->tx_txg
);
2334 db
->db_dirtycnt
+= 1;
2335 ASSERT3U(db
->db_dirtycnt
, <=, 3);
2337 mutex_exit(&db
->db_mtx
);
2339 if (db
->db_blkid
== DMU_BONUS_BLKID
||
2340 db
->db_blkid
== DMU_SPILL_BLKID
) {
2341 mutex_enter(&dn
->dn_mtx
);
2342 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
2343 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
2344 mutex_exit(&dn
->dn_mtx
);
2345 dnode_setdirty(dn
, tx
);
2350 if (!RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
2351 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
2352 drop_struct_rwlock
= B_TRUE
;
2356 * If we are overwriting a dedup BP, then unless it is snapshotted,
2357 * when we get to syncing context we will need to decrement its
2358 * refcount in the DDT. Prefetch the relevant DDT block so that
2359 * syncing context won't have to wait for the i/o.
2361 if (db
->db_blkptr
!= NULL
) {
2362 db_lock_type_t dblt
= dmu_buf_lock_parent(db
, RW_READER
, FTAG
);
2363 ddt_prefetch(os
->os_spa
, db
->db_blkptr
);
2364 dmu_buf_unlock_parent(db
, dblt
, FTAG
);
2368 * We need to hold the dn_struct_rwlock to make this assertion,
2369 * because it protects dn_phys / dn_next_nlevels from changing.
2371 ASSERT((dn
->dn_phys
->dn_nlevels
== 0 && db
->db_level
== 0) ||
2372 dn
->dn_phys
->dn_nlevels
> db
->db_level
||
2373 dn
->dn_next_nlevels
[txgoff
] > db
->db_level
||
2374 dn
->dn_next_nlevels
[(tx
->tx_txg
-1) & TXG_MASK
] > db
->db_level
||
2375 dn
->dn_next_nlevels
[(tx
->tx_txg
-2) & TXG_MASK
] > db
->db_level
);
2378 if (db
->db_level
== 0) {
2379 ASSERT(!db
->db_objset
->os_raw_receive
||
2380 dn
->dn_maxblkid
>= db
->db_blkid
);
2381 dnode_new_blkid(dn
, db
->db_blkid
, tx
,
2382 drop_struct_rwlock
, B_FALSE
);
2383 ASSERT(dn
->dn_maxblkid
>= db
->db_blkid
);
2386 if (db
->db_level
+1 < dn
->dn_nlevels
) {
2387 dmu_buf_impl_t
*parent
= db
->db_parent
;
2388 dbuf_dirty_record_t
*di
;
2389 int parent_held
= FALSE
;
2391 if (db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
) {
2392 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2393 parent
= dbuf_hold_level(dn
, db
->db_level
+ 1,
2394 db
->db_blkid
>> epbs
, FTAG
);
2395 ASSERT(parent
!= NULL
);
2398 if (drop_struct_rwlock
)
2399 rw_exit(&dn
->dn_struct_rwlock
);
2400 ASSERT3U(db
->db_level
+ 1, ==, parent
->db_level
);
2401 di
= dbuf_dirty(parent
, tx
);
2403 dbuf_rele(parent
, FTAG
);
2405 mutex_enter(&db
->db_mtx
);
2407 * Since we've dropped the mutex, it's possible that
2408 * dbuf_undirty() might have changed this out from under us.
2410 if (list_head(&db
->db_dirty_records
) == dr
||
2411 dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
2412 mutex_enter(&di
->dt
.di
.dr_mtx
);
2413 ASSERT3U(di
->dr_txg
, ==, tx
->tx_txg
);
2414 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
2415 list_insert_tail(&di
->dt
.di
.dr_children
, dr
);
2416 mutex_exit(&di
->dt
.di
.dr_mtx
);
2419 mutex_exit(&db
->db_mtx
);
2421 ASSERT(db
->db_level
+ 1 == dn
->dn_nlevels
);
2422 ASSERT(db
->db_blkid
< dn
->dn_nblkptr
);
2423 ASSERT(db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
2424 mutex_enter(&dn
->dn_mtx
);
2425 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
2426 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
2427 mutex_exit(&dn
->dn_mtx
);
2428 if (drop_struct_rwlock
)
2429 rw_exit(&dn
->dn_struct_rwlock
);
2432 dnode_setdirty(dn
, tx
);
2438 dbuf_undirty_bonus(dbuf_dirty_record_t
*dr
)
2440 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2442 if (dr
->dt
.dl
.dr_data
!= db
->db
.db_data
) {
2443 struct dnode
*dn
= dr
->dr_dnode
;
2444 int max_bonuslen
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
);
2446 kmem_free(dr
->dt
.dl
.dr_data
, max_bonuslen
);
2447 arc_space_return(max_bonuslen
, ARC_SPACE_BONUS
);
2449 db
->db_data_pending
= NULL
;
2450 ASSERT(list_next(&db
->db_dirty_records
, dr
) == NULL
);
2451 list_remove(&db
->db_dirty_records
, dr
);
2452 if (dr
->dr_dbuf
->db_level
!= 0) {
2453 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
2454 list_destroy(&dr
->dt
.di
.dr_children
);
2456 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
2457 ASSERT3U(db
->db_dirtycnt
, >, 0);
2458 db
->db_dirtycnt
-= 1;
2462 * Undirty a buffer in the transaction group referenced by the given
2463 * transaction. Return whether this evicted the dbuf.
2466 dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
2468 uint64_t txg
= tx
->tx_txg
;
2473 * Due to our use of dn_nlevels below, this can only be called
2474 * in open context, unless we are operating on the MOS.
2475 * From syncing context, dn_nlevels may be different from the
2476 * dn_nlevels used when dbuf was dirtied.
2478 ASSERT(db
->db_objset
==
2479 dmu_objset_pool(db
->db_objset
)->dp_meta_objset
||
2480 txg
!= spa_syncing_txg(dmu_objset_spa(db
->db_objset
)));
2481 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2482 ASSERT0(db
->db_level
);
2483 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2486 * If this buffer is not dirty, we're done.
2488 dbuf_dirty_record_t
*dr
= dbuf_find_dirty_eq(db
, txg
);
2491 ASSERT(dr
->dr_dbuf
== db
);
2493 dnode_t
*dn
= dr
->dr_dnode
;
2495 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
2497 ASSERT(db
->db
.db_size
!= 0);
2499 dsl_pool_undirty_space(dmu_objset_pool(dn
->dn_objset
),
2500 dr
->dr_accounted
, txg
);
2502 list_remove(&db
->db_dirty_records
, dr
);
2505 * Note that there are three places in dbuf_dirty()
2506 * where this dirty record may be put on a list.
2507 * Make sure to do a list_remove corresponding to
2508 * every one of those list_insert calls.
2510 if (dr
->dr_parent
) {
2511 mutex_enter(&dr
->dr_parent
->dt
.di
.dr_mtx
);
2512 list_remove(&dr
->dr_parent
->dt
.di
.dr_children
, dr
);
2513 mutex_exit(&dr
->dr_parent
->dt
.di
.dr_mtx
);
2514 } else if (db
->db_blkid
== DMU_SPILL_BLKID
||
2515 db
->db_level
+ 1 == dn
->dn_nlevels
) {
2516 ASSERT(db
->db_blkptr
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
2517 mutex_enter(&dn
->dn_mtx
);
2518 list_remove(&dn
->dn_dirty_records
[txg
& TXG_MASK
], dr
);
2519 mutex_exit(&dn
->dn_mtx
);
2522 if (db
->db_state
!= DB_NOFILL
) {
2523 dbuf_unoverride(dr
);
2525 ASSERT(db
->db_buf
!= NULL
);
2526 ASSERT(dr
->dt
.dl
.dr_data
!= NULL
);
2527 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
2528 arc_buf_destroy(dr
->dt
.dl
.dr_data
, db
);
2531 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
2533 ASSERT(db
->db_dirtycnt
> 0);
2534 db
->db_dirtycnt
-= 1;
2536 if (zfs_refcount_remove(&db
->db_holds
, (void *)(uintptr_t)txg
) == 0) {
2537 ASSERT(db
->db_state
== DB_NOFILL
|| arc_released(db
->db_buf
));
2546 dmu_buf_will_dirty_impl(dmu_buf_t
*db_fake
, int flags
, dmu_tx_t
*tx
)
2548 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2550 ASSERT(tx
->tx_txg
!= 0);
2551 ASSERT(!zfs_refcount_is_zero(&db
->db_holds
));
2554 * Quick check for dirtiness. For already dirty blocks, this
2555 * reduces runtime of this function by >90%, and overall performance
2556 * by 50% for some workloads (e.g. file deletion with indirect blocks
2559 mutex_enter(&db
->db_mtx
);
2561 if (db
->db_state
== DB_CACHED
) {
2562 dbuf_dirty_record_t
*dr
= dbuf_find_dirty_eq(db
, tx
->tx_txg
);
2564 * It's possible that it is already dirty but not cached,
2565 * because there are some calls to dbuf_dirty() that don't
2566 * go through dmu_buf_will_dirty().
2569 /* This dbuf is already dirty and cached. */
2571 mutex_exit(&db
->db_mtx
);
2575 mutex_exit(&db
->db_mtx
);
2578 if (RW_WRITE_HELD(&DB_DNODE(db
)->dn_struct_rwlock
))
2579 flags
|= DB_RF_HAVESTRUCT
;
2581 (void) dbuf_read(db
, NULL
, flags
);
2582 (void) dbuf_dirty(db
, tx
);
2586 dmu_buf_will_dirty(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
2588 dmu_buf_will_dirty_impl(db_fake
,
2589 DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
, tx
);
2593 dmu_buf_is_dirty(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
2595 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2596 dbuf_dirty_record_t
*dr
;
2598 mutex_enter(&db
->db_mtx
);
2599 dr
= dbuf_find_dirty_eq(db
, tx
->tx_txg
);
2600 mutex_exit(&db
->db_mtx
);
2601 return (dr
!= NULL
);
2605 dmu_buf_will_not_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
2607 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2609 db
->db_state
= DB_NOFILL
;
2610 DTRACE_SET_STATE(db
, "allocating NOFILL buffer");
2611 dmu_buf_will_fill(db_fake
, tx
);
2615 dmu_buf_will_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
2617 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2619 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2620 ASSERT(tx
->tx_txg
!= 0);
2621 ASSERT(db
->db_level
== 0);
2622 ASSERT(!zfs_refcount_is_zero(&db
->db_holds
));
2624 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
||
2625 dmu_tx_private_ok(tx
));
2628 (void) dbuf_dirty(db
, tx
);
2632 * This function is effectively the same as dmu_buf_will_dirty(), but
2633 * indicates the caller expects raw encrypted data in the db, and provides
2634 * the crypt params (byteorder, salt, iv, mac) which should be stored in the
2635 * blkptr_t when this dbuf is written. This is only used for blocks of
2636 * dnodes, during raw receive.
2639 dmu_buf_set_crypt_params(dmu_buf_t
*db_fake
, boolean_t byteorder
,
2640 const uint8_t *salt
, const uint8_t *iv
, const uint8_t *mac
, dmu_tx_t
*tx
)
2642 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2643 dbuf_dirty_record_t
*dr
;
2646 * dr_has_raw_params is only processed for blocks of dnodes
2647 * (see dbuf_sync_dnode_leaf_crypt()).
2649 ASSERT3U(db
->db
.db_object
, ==, DMU_META_DNODE_OBJECT
);
2650 ASSERT3U(db
->db_level
, ==, 0);
2651 ASSERT(db
->db_objset
->os_raw_receive
);
2653 dmu_buf_will_dirty_impl(db_fake
,
2654 DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
| DB_RF_NO_DECRYPT
, tx
);
2656 dr
= dbuf_find_dirty_eq(db
, tx
->tx_txg
);
2658 ASSERT3P(dr
, !=, NULL
);
2660 dr
->dt
.dl
.dr_has_raw_params
= B_TRUE
;
2661 dr
->dt
.dl
.dr_byteorder
= byteorder
;
2662 bcopy(salt
, dr
->dt
.dl
.dr_salt
, ZIO_DATA_SALT_LEN
);
2663 bcopy(iv
, dr
->dt
.dl
.dr_iv
, ZIO_DATA_IV_LEN
);
2664 bcopy(mac
, dr
->dt
.dl
.dr_mac
, ZIO_DATA_MAC_LEN
);
2668 dbuf_override_impl(dmu_buf_impl_t
*db
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
2670 struct dirty_leaf
*dl
;
2671 dbuf_dirty_record_t
*dr
;
2673 dr
= list_head(&db
->db_dirty_records
);
2674 ASSERT3U(dr
->dr_txg
, ==, tx
->tx_txg
);
2676 dl
->dr_overridden_by
= *bp
;
2677 dl
->dr_override_state
= DR_OVERRIDDEN
;
2678 dl
->dr_overridden_by
.blk_birth
= dr
->dr_txg
;
2683 dmu_buf_fill_done(dmu_buf_t
*dbuf
, dmu_tx_t
*tx
)
2685 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbuf
;
2686 dbuf_states_t old_state
;
2687 mutex_enter(&db
->db_mtx
);
2690 old_state
= db
->db_state
;
2691 db
->db_state
= DB_CACHED
;
2692 if (old_state
== DB_FILL
) {
2693 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
2694 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2695 /* we were freed while filling */
2696 /* XXX dbuf_undirty? */
2697 bzero(db
->db
.db_data
, db
->db
.db_size
);
2698 db
->db_freed_in_flight
= FALSE
;
2699 DTRACE_SET_STATE(db
,
2700 "fill done handling freed in flight");
2702 DTRACE_SET_STATE(db
, "fill done");
2704 cv_broadcast(&db
->db_changed
);
2706 mutex_exit(&db
->db_mtx
);
2710 dmu_buf_write_embedded(dmu_buf_t
*dbuf
, void *data
,
2711 bp_embedded_type_t etype
, enum zio_compress comp
,
2712 int uncompressed_size
, int compressed_size
, int byteorder
,
2715 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbuf
;
2716 struct dirty_leaf
*dl
;
2717 dmu_object_type_t type
;
2718 dbuf_dirty_record_t
*dr
;
2720 if (etype
== BP_EMBEDDED_TYPE_DATA
) {
2721 ASSERT(spa_feature_is_active(dmu_objset_spa(db
->db_objset
),
2722 SPA_FEATURE_EMBEDDED_DATA
));
2726 type
= DB_DNODE(db
)->dn_type
;
2729 ASSERT0(db
->db_level
);
2730 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2732 dmu_buf_will_not_fill(dbuf
, tx
);
2734 dr
= list_head(&db
->db_dirty_records
);
2735 ASSERT3U(dr
->dr_txg
, ==, tx
->tx_txg
);
2737 encode_embedded_bp_compressed(&dl
->dr_overridden_by
,
2738 data
, comp
, uncompressed_size
, compressed_size
);
2739 BPE_SET_ETYPE(&dl
->dr_overridden_by
, etype
);
2740 BP_SET_TYPE(&dl
->dr_overridden_by
, type
);
2741 BP_SET_LEVEL(&dl
->dr_overridden_by
, 0);
2742 BP_SET_BYTEORDER(&dl
->dr_overridden_by
, byteorder
);
2744 dl
->dr_override_state
= DR_OVERRIDDEN
;
2745 dl
->dr_overridden_by
.blk_birth
= dr
->dr_txg
;
2749 dmu_buf_redact(dmu_buf_t
*dbuf
, dmu_tx_t
*tx
)
2751 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbuf
;
2752 dmu_object_type_t type
;
2753 ASSERT(dsl_dataset_feature_is_active(db
->db_objset
->os_dsl_dataset
,
2754 SPA_FEATURE_REDACTED_DATASETS
));
2757 type
= DB_DNODE(db
)->dn_type
;
2760 ASSERT0(db
->db_level
);
2761 dmu_buf_will_not_fill(dbuf
, tx
);
2763 blkptr_t bp
= { { { {0} } } };
2764 BP_SET_TYPE(&bp
, type
);
2765 BP_SET_LEVEL(&bp
, 0);
2766 BP_SET_BIRTH(&bp
, tx
->tx_txg
, 0);
2767 BP_SET_REDACTED(&bp
);
2768 BPE_SET_LSIZE(&bp
, dbuf
->db_size
);
2770 dbuf_override_impl(db
, &bp
, tx
);
2774 * Directly assign a provided arc buf to a given dbuf if it's not referenced
2775 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
2778 dbuf_assign_arcbuf(dmu_buf_impl_t
*db
, arc_buf_t
*buf
, dmu_tx_t
*tx
)
2780 ASSERT(!zfs_refcount_is_zero(&db
->db_holds
));
2781 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2782 ASSERT(db
->db_level
== 0);
2783 ASSERT3U(dbuf_is_metadata(db
), ==, arc_is_metadata(buf
));
2784 ASSERT(buf
!= NULL
);
2785 ASSERT3U(arc_buf_lsize(buf
), ==, db
->db
.db_size
);
2786 ASSERT(tx
->tx_txg
!= 0);
2788 arc_return_buf(buf
, db
);
2789 ASSERT(arc_released(buf
));
2791 mutex_enter(&db
->db_mtx
);
2793 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
2794 cv_wait(&db
->db_changed
, &db
->db_mtx
);
2796 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_UNCACHED
);
2798 if (db
->db_state
== DB_CACHED
&&
2799 zfs_refcount_count(&db
->db_holds
) - 1 > db
->db_dirtycnt
) {
2801 * In practice, we will never have a case where we have an
2802 * encrypted arc buffer while additional holds exist on the
2803 * dbuf. We don't handle this here so we simply assert that
2806 ASSERT(!arc_is_encrypted(buf
));
2807 mutex_exit(&db
->db_mtx
);
2808 (void) dbuf_dirty(db
, tx
);
2809 bcopy(buf
->b_data
, db
->db
.db_data
, db
->db
.db_size
);
2810 arc_buf_destroy(buf
, db
);
2814 if (db
->db_state
== DB_CACHED
) {
2815 dbuf_dirty_record_t
*dr
= list_head(&db
->db_dirty_records
);
2817 ASSERT(db
->db_buf
!= NULL
);
2818 if (dr
!= NULL
&& dr
->dr_txg
== tx
->tx_txg
) {
2819 ASSERT(dr
->dt
.dl
.dr_data
== db
->db_buf
);
2821 if (!arc_released(db
->db_buf
)) {
2822 ASSERT(dr
->dt
.dl
.dr_override_state
==
2824 arc_release(db
->db_buf
, db
);
2826 dr
->dt
.dl
.dr_data
= buf
;
2827 arc_buf_destroy(db
->db_buf
, db
);
2828 } else if (dr
== NULL
|| dr
->dt
.dl
.dr_data
!= db
->db_buf
) {
2829 arc_release(db
->db_buf
, db
);
2830 arc_buf_destroy(db
->db_buf
, db
);
2834 ASSERT(db
->db_buf
== NULL
);
2835 dbuf_set_data(db
, buf
);
2836 db
->db_state
= DB_FILL
;
2837 DTRACE_SET_STATE(db
, "filling assigned arcbuf");
2838 mutex_exit(&db
->db_mtx
);
2839 (void) dbuf_dirty(db
, tx
);
2840 dmu_buf_fill_done(&db
->db
, tx
);
2844 dbuf_destroy(dmu_buf_impl_t
*db
)
2847 dmu_buf_impl_t
*parent
= db
->db_parent
;
2848 dmu_buf_impl_t
*dndb
;
2850 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2851 ASSERT(zfs_refcount_is_zero(&db
->db_holds
));
2853 if (db
->db_buf
!= NULL
) {
2854 arc_buf_destroy(db
->db_buf
, db
);
2858 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2859 int slots
= DB_DNODE(db
)->dn_num_slots
;
2860 int bonuslen
= DN_SLOTS_TO_BONUSLEN(slots
);
2861 if (db
->db
.db_data
!= NULL
) {
2862 kmem_free(db
->db
.db_data
, bonuslen
);
2863 arc_space_return(bonuslen
, ARC_SPACE_BONUS
);
2864 db
->db_state
= DB_UNCACHED
;
2865 DTRACE_SET_STATE(db
, "buffer cleared");
2869 dbuf_clear_data(db
);
2871 if (multilist_link_active(&db
->db_cache_link
)) {
2872 ASSERT(db
->db_caching_status
== DB_DBUF_CACHE
||
2873 db
->db_caching_status
== DB_DBUF_METADATA_CACHE
);
2875 multilist_remove(&dbuf_caches
[db
->db_caching_status
].cache
, db
);
2876 (void) zfs_refcount_remove_many(
2877 &dbuf_caches
[db
->db_caching_status
].size
,
2878 db
->db
.db_size
, db
);
2880 if (db
->db_caching_status
== DB_DBUF_METADATA_CACHE
) {
2881 DBUF_STAT_BUMPDOWN(metadata_cache_count
);
2883 DBUF_STAT_BUMPDOWN(cache_levels
[db
->db_level
]);
2884 DBUF_STAT_BUMPDOWN(cache_count
);
2885 DBUF_STAT_DECR(cache_levels_bytes
[db
->db_level
],
2888 db
->db_caching_status
= DB_NO_CACHE
;
2891 ASSERT(db
->db_state
== DB_UNCACHED
|| db
->db_state
== DB_NOFILL
);
2892 ASSERT(db
->db_data_pending
== NULL
);
2893 ASSERT(list_is_empty(&db
->db_dirty_records
));
2895 db
->db_state
= DB_EVICTING
;
2896 DTRACE_SET_STATE(db
, "buffer eviction started");
2897 db
->db_blkptr
= NULL
;
2900 * Now that db_state is DB_EVICTING, nobody else can find this via
2901 * the hash table. We can now drop db_mtx, which allows us to
2902 * acquire the dn_dbufs_mtx.
2904 mutex_exit(&db
->db_mtx
);
2909 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
2910 boolean_t needlock
= !MUTEX_HELD(&dn
->dn_dbufs_mtx
);
2912 mutex_enter_nested(&dn
->dn_dbufs_mtx
,
2914 avl_remove(&dn
->dn_dbufs
, db
);
2918 mutex_exit(&dn
->dn_dbufs_mtx
);
2920 * Decrementing the dbuf count means that the hold corresponding
2921 * to the removed dbuf is no longer discounted in dnode_move(),
2922 * so the dnode cannot be moved until after we release the hold.
2923 * The membar_producer() ensures visibility of the decremented
2924 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
2927 mutex_enter(&dn
->dn_mtx
);
2928 dnode_rele_and_unlock(dn
, db
, B_TRUE
);
2929 db
->db_dnode_handle
= NULL
;
2931 dbuf_hash_remove(db
);
2936 ASSERT(zfs_refcount_is_zero(&db
->db_holds
));
2938 db
->db_parent
= NULL
;
2940 ASSERT(db
->db_buf
== NULL
);
2941 ASSERT(db
->db
.db_data
== NULL
);
2942 ASSERT(db
->db_hash_next
== NULL
);
2943 ASSERT(db
->db_blkptr
== NULL
);
2944 ASSERT(db
->db_data_pending
== NULL
);
2945 ASSERT3U(db
->db_caching_status
, ==, DB_NO_CACHE
);
2946 ASSERT(!multilist_link_active(&db
->db_cache_link
));
2948 kmem_cache_free(dbuf_kmem_cache
, db
);
2949 arc_space_return(sizeof (dmu_buf_impl_t
), ARC_SPACE_DBUF
);
2952 * If this dbuf is referenced from an indirect dbuf,
2953 * decrement the ref count on the indirect dbuf.
2955 if (parent
&& parent
!= dndb
) {
2956 mutex_enter(&parent
->db_mtx
);
2957 dbuf_rele_and_unlock(parent
, db
, B_TRUE
);
2962 * Note: While bpp will always be updated if the function returns success,
2963 * parentp will not be updated if the dnode does not have dn_dbuf filled in;
2964 * this happens when the dnode is the meta-dnode, or {user|group|project}used
2967 __attribute__((always_inline
))
2969 dbuf_findbp(dnode_t
*dn
, int level
, uint64_t blkid
, int fail_sparse
,
2970 dmu_buf_impl_t
**parentp
, blkptr_t
**bpp
)
2975 ASSERT(blkid
!= DMU_BONUS_BLKID
);
2977 if (blkid
== DMU_SPILL_BLKID
) {
2978 mutex_enter(&dn
->dn_mtx
);
2979 if (dn
->dn_have_spill
&&
2980 (dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
))
2981 *bpp
= DN_SPILL_BLKPTR(dn
->dn_phys
);
2984 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
2985 *parentp
= dn
->dn_dbuf
;
2986 mutex_exit(&dn
->dn_mtx
);
2991 (dn
->dn_phys
->dn_nlevels
== 0) ? 1 : dn
->dn_phys
->dn_nlevels
;
2992 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2994 ASSERT3U(level
* epbs
, <, 64);
2995 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2997 * This assertion shouldn't trip as long as the max indirect block size
2998 * is less than 1M. The reason for this is that up to that point,
2999 * the number of levels required to address an entire object with blocks
3000 * of size SPA_MINBLOCKSIZE satisfies nlevels * epbs + 1 <= 64. In
3001 * other words, if N * epbs + 1 > 64, then if (N-1) * epbs + 1 > 55
3002 * (i.e. we can address the entire object), objects will all use at most
3003 * N-1 levels and the assertion won't overflow. However, once epbs is
3004 * 13, 4 * 13 + 1 = 53, but 5 * 13 + 1 = 66. Then, 4 levels will not be
3005 * enough to address an entire object, so objects will have 5 levels,
3006 * but then this assertion will overflow.
3008 * All this is to say that if we ever increase DN_MAX_INDBLKSHIFT, we
3009 * need to redo this logic to handle overflows.
3011 ASSERT(level
>= nlevels
||
3012 ((nlevels
- level
- 1) * epbs
) +
3013 highbit64(dn
->dn_phys
->dn_nblkptr
) <= 64);
3014 if (level
>= nlevels
||
3015 blkid
>= ((uint64_t)dn
->dn_phys
->dn_nblkptr
<<
3016 ((nlevels
- level
- 1) * epbs
)) ||
3018 blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))) {
3019 /* the buffer has no parent yet */
3020 return (SET_ERROR(ENOENT
));
3021 } else if (level
< nlevels
-1) {
3022 /* this block is referenced from an indirect block */
3025 err
= dbuf_hold_impl(dn
, level
+ 1,
3026 blkid
>> epbs
, fail_sparse
, FALSE
, NULL
, parentp
);
3030 err
= dbuf_read(*parentp
, NULL
,
3031 (DB_RF_HAVESTRUCT
| DB_RF_NOPREFETCH
| DB_RF_CANFAIL
));
3033 dbuf_rele(*parentp
, NULL
);
3037 rw_enter(&(*parentp
)->db_rwlock
, RW_READER
);
3038 *bpp
= ((blkptr_t
*)(*parentp
)->db
.db_data
) +
3039 (blkid
& ((1ULL << epbs
) - 1));
3040 if (blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))
3041 ASSERT(BP_IS_HOLE(*bpp
));
3042 rw_exit(&(*parentp
)->db_rwlock
);
3045 /* the block is referenced from the dnode */
3046 ASSERT3U(level
, ==, nlevels
-1);
3047 ASSERT(dn
->dn_phys
->dn_nblkptr
== 0 ||
3048 blkid
< dn
->dn_phys
->dn_nblkptr
);
3050 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
3051 *parentp
= dn
->dn_dbuf
;
3053 *bpp
= &dn
->dn_phys
->dn_blkptr
[blkid
];
3058 static dmu_buf_impl_t
*
3059 dbuf_create(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
3060 dmu_buf_impl_t
*parent
, blkptr_t
*blkptr
)
3062 objset_t
*os
= dn
->dn_objset
;
3063 dmu_buf_impl_t
*db
, *odb
;
3065 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
3066 ASSERT(dn
->dn_type
!= DMU_OT_NONE
);
3068 db
= kmem_cache_alloc(dbuf_kmem_cache
, KM_SLEEP
);
3070 list_create(&db
->db_dirty_records
, sizeof (dbuf_dirty_record_t
),
3071 offsetof(dbuf_dirty_record_t
, dr_dbuf_node
));
3074 db
->db
.db_object
= dn
->dn_object
;
3075 db
->db_level
= level
;
3076 db
->db_blkid
= blkid
;
3077 db
->db_dirtycnt
= 0;
3078 db
->db_dnode_handle
= dn
->dn_handle
;
3079 db
->db_parent
= parent
;
3080 db
->db_blkptr
= blkptr
;
3083 db
->db_user_immediate_evict
= FALSE
;
3084 db
->db_freed_in_flight
= FALSE
;
3085 db
->db_pending_evict
= FALSE
;
3087 if (blkid
== DMU_BONUS_BLKID
) {
3088 ASSERT3P(parent
, ==, dn
->dn_dbuf
);
3089 db
->db
.db_size
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
) -
3090 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
);
3091 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
3092 db
->db
.db_offset
= DMU_BONUS_BLKID
;
3093 db
->db_state
= DB_UNCACHED
;
3094 DTRACE_SET_STATE(db
, "bonus buffer created");
3095 db
->db_caching_status
= DB_NO_CACHE
;
3096 /* the bonus dbuf is not placed in the hash table */
3097 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_DBUF
);
3099 } else if (blkid
== DMU_SPILL_BLKID
) {
3100 db
->db
.db_size
= (blkptr
!= NULL
) ?
3101 BP_GET_LSIZE(blkptr
) : SPA_MINBLOCKSIZE
;
3102 db
->db
.db_offset
= 0;
3105 db
->db_level
? 1 << dn
->dn_indblkshift
: dn
->dn_datablksz
;
3106 db
->db
.db_size
= blocksize
;
3107 db
->db
.db_offset
= db
->db_blkid
* blocksize
;
3111 * Hold the dn_dbufs_mtx while we get the new dbuf
3112 * in the hash table *and* added to the dbufs list.
3113 * This prevents a possible deadlock with someone
3114 * trying to look up this dbuf before it's added to the
3117 mutex_enter(&dn
->dn_dbufs_mtx
);
3118 db
->db_state
= DB_EVICTING
; /* not worth logging this state change */
3119 if ((odb
= dbuf_hash_insert(db
)) != NULL
) {
3120 /* someone else inserted it first */
3121 mutex_exit(&dn
->dn_dbufs_mtx
);
3122 kmem_cache_free(dbuf_kmem_cache
, db
);
3123 DBUF_STAT_BUMP(hash_insert_race
);
3126 avl_add(&dn
->dn_dbufs
, db
);
3128 db
->db_state
= DB_UNCACHED
;
3129 DTRACE_SET_STATE(db
, "regular buffer created");
3130 db
->db_caching_status
= DB_NO_CACHE
;
3131 mutex_exit(&dn
->dn_dbufs_mtx
);
3132 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_DBUF
);
3134 if (parent
&& parent
!= dn
->dn_dbuf
)
3135 dbuf_add_ref(parent
, db
);
3137 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
3138 zfs_refcount_count(&dn
->dn_holds
) > 0);
3139 (void) zfs_refcount_add(&dn
->dn_holds
, db
);
3141 dprintf_dbuf(db
, "db=%p\n", db
);
3147 * This function returns a block pointer and information about the object,
3148 * given a dnode and a block. This is a publicly accessible version of
3149 * dbuf_findbp that only returns some information, rather than the
3150 * dbuf. Note that the dnode passed in must be held, and the dn_struct_rwlock
3151 * should be locked as (at least) a reader.
3154 dbuf_dnode_findbp(dnode_t
*dn
, uint64_t level
, uint64_t blkid
,
3155 blkptr_t
*bp
, uint16_t *datablkszsec
, uint8_t *indblkshift
)
3157 dmu_buf_impl_t
*dbp
= NULL
;
3160 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
3162 err
= dbuf_findbp(dn
, level
, blkid
, B_FALSE
, &dbp
, &bp2
);
3166 dbuf_rele(dbp
, NULL
);
3167 if (datablkszsec
!= NULL
)
3168 *datablkszsec
= dn
->dn_phys
->dn_datablkszsec
;
3169 if (indblkshift
!= NULL
)
3170 *indblkshift
= dn
->dn_phys
->dn_indblkshift
;
3176 typedef struct dbuf_prefetch_arg
{
3177 spa_t
*dpa_spa
; /* The spa to issue the prefetch in. */
3178 zbookmark_phys_t dpa_zb
; /* The target block to prefetch. */
3179 int dpa_epbs
; /* Entries (blkptr_t's) Per Block Shift. */
3180 int dpa_curlevel
; /* The current level that we're reading */
3181 dnode_t
*dpa_dnode
; /* The dnode associated with the prefetch */
3182 zio_priority_t dpa_prio
; /* The priority I/Os should be issued at. */
3183 zio_t
*dpa_zio
; /* The parent zio_t for all prefetches. */
3184 arc_flags_t dpa_aflags
; /* Flags to pass to the final prefetch. */
3185 dbuf_prefetch_fn dpa_cb
; /* prefetch completion callback */
3186 void *dpa_arg
; /* prefetch completion arg */
3187 } dbuf_prefetch_arg_t
;
3190 dbuf_prefetch_fini(dbuf_prefetch_arg_t
*dpa
, boolean_t io_done
)
3192 if (dpa
->dpa_cb
!= NULL
)
3193 dpa
->dpa_cb(dpa
->dpa_arg
, io_done
);
3194 kmem_free(dpa
, sizeof (*dpa
));
3198 dbuf_issue_final_prefetch_done(zio_t
*zio
, const zbookmark_phys_t
*zb
,
3199 const blkptr_t
*iobp
, arc_buf_t
*abuf
, void *private)
3201 dbuf_prefetch_arg_t
*dpa
= private;
3203 dbuf_prefetch_fini(dpa
, B_TRUE
);
3205 arc_buf_destroy(abuf
, private);
3209 * Actually issue the prefetch read for the block given.
3212 dbuf_issue_final_prefetch(dbuf_prefetch_arg_t
*dpa
, blkptr_t
*bp
)
3214 ASSERT(!BP_IS_REDACTED(bp
) ||
3215 dsl_dataset_feature_is_active(
3216 dpa
->dpa_dnode
->dn_objset
->os_dsl_dataset
,
3217 SPA_FEATURE_REDACTED_DATASETS
));
3219 if (BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
) || BP_IS_REDACTED(bp
))
3220 return (dbuf_prefetch_fini(dpa
, B_FALSE
));
3222 int zio_flags
= ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
;
3223 arc_flags_t aflags
=
3224 dpa
->dpa_aflags
| ARC_FLAG_NOWAIT
| ARC_FLAG_PREFETCH
|
3227 /* dnodes are always read as raw and then converted later */
3228 if (BP_GET_TYPE(bp
) == DMU_OT_DNODE
&& BP_IS_PROTECTED(bp
) &&
3229 dpa
->dpa_curlevel
== 0)
3230 zio_flags
|= ZIO_FLAG_RAW
;
3232 ASSERT3U(dpa
->dpa_curlevel
, ==, BP_GET_LEVEL(bp
));
3233 ASSERT3U(dpa
->dpa_curlevel
, ==, dpa
->dpa_zb
.zb_level
);
3234 ASSERT(dpa
->dpa_zio
!= NULL
);
3235 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
, bp
,
3236 dbuf_issue_final_prefetch_done
, dpa
,
3237 dpa
->dpa_prio
, zio_flags
, &aflags
, &dpa
->dpa_zb
);
3241 * Called when an indirect block above our prefetch target is read in. This
3242 * will either read in the next indirect block down the tree or issue the actual
3243 * prefetch if the next block down is our target.
3246 dbuf_prefetch_indirect_done(zio_t
*zio
, const zbookmark_phys_t
*zb
,
3247 const blkptr_t
*iobp
, arc_buf_t
*abuf
, void *private)
3249 dbuf_prefetch_arg_t
*dpa
= private;
3251 ASSERT3S(dpa
->dpa_zb
.zb_level
, <, dpa
->dpa_curlevel
);
3252 ASSERT3S(dpa
->dpa_curlevel
, >, 0);
3255 ASSERT(zio
== NULL
|| zio
->io_error
!= 0);
3256 return (dbuf_prefetch_fini(dpa
, B_TRUE
));
3258 ASSERT(zio
== NULL
|| zio
->io_error
== 0);
3261 * The dpa_dnode is only valid if we are called with a NULL
3262 * zio. This indicates that the arc_read() returned without
3263 * first calling zio_read() to issue a physical read. Once
3264 * a physical read is made the dpa_dnode must be invalidated
3265 * as the locks guarding it may have been dropped. If the
3266 * dpa_dnode is still valid, then we want to add it to the dbuf
3267 * cache. To do so, we must hold the dbuf associated with the block
3268 * we just prefetched, read its contents so that we associate it
3269 * with an arc_buf_t, and then release it.
3272 ASSERT3S(BP_GET_LEVEL(zio
->io_bp
), ==, dpa
->dpa_curlevel
);
3273 if (zio
->io_flags
& ZIO_FLAG_RAW_COMPRESS
) {
3274 ASSERT3U(BP_GET_PSIZE(zio
->io_bp
), ==, zio
->io_size
);
3276 ASSERT3U(BP_GET_LSIZE(zio
->io_bp
), ==, zio
->io_size
);
3278 ASSERT3P(zio
->io_spa
, ==, dpa
->dpa_spa
);
3280 dpa
->dpa_dnode
= NULL
;
3281 } else if (dpa
->dpa_dnode
!= NULL
) {
3282 uint64_t curblkid
= dpa
->dpa_zb
.zb_blkid
>>
3283 (dpa
->dpa_epbs
* (dpa
->dpa_curlevel
-
3284 dpa
->dpa_zb
.zb_level
));
3285 dmu_buf_impl_t
*db
= dbuf_hold_level(dpa
->dpa_dnode
,
3286 dpa
->dpa_curlevel
, curblkid
, FTAG
);
3288 arc_buf_destroy(abuf
, private);
3289 return (dbuf_prefetch_fini(dpa
, B_TRUE
));
3291 (void) dbuf_read(db
, NULL
,
3292 DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
| DB_RF_HAVESTRUCT
);
3293 dbuf_rele(db
, FTAG
);
3296 dpa
->dpa_curlevel
--;
3297 uint64_t nextblkid
= dpa
->dpa_zb
.zb_blkid
>>
3298 (dpa
->dpa_epbs
* (dpa
->dpa_curlevel
- dpa
->dpa_zb
.zb_level
));
3299 blkptr_t
*bp
= ((blkptr_t
*)abuf
->b_data
) +
3300 P2PHASE(nextblkid
, 1ULL << dpa
->dpa_epbs
);
3302 ASSERT(!BP_IS_REDACTED(bp
) ||
3303 dsl_dataset_feature_is_active(
3304 dpa
->dpa_dnode
->dn_objset
->os_dsl_dataset
,
3305 SPA_FEATURE_REDACTED_DATASETS
));
3306 if (BP_IS_HOLE(bp
) || BP_IS_REDACTED(bp
)) {
3307 dbuf_prefetch_fini(dpa
, B_TRUE
);
3308 } else if (dpa
->dpa_curlevel
== dpa
->dpa_zb
.zb_level
) {
3309 ASSERT3U(nextblkid
, ==, dpa
->dpa_zb
.zb_blkid
);
3310 dbuf_issue_final_prefetch(dpa
, bp
);
3312 arc_flags_t iter_aflags
= ARC_FLAG_NOWAIT
;
3313 zbookmark_phys_t zb
;
3315 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
3316 if (dpa
->dpa_aflags
& ARC_FLAG_L2CACHE
)
3317 iter_aflags
|= ARC_FLAG_L2CACHE
;
3319 ASSERT3U(dpa
->dpa_curlevel
, ==, BP_GET_LEVEL(bp
));
3321 SET_BOOKMARK(&zb
, dpa
->dpa_zb
.zb_objset
,
3322 dpa
->dpa_zb
.zb_object
, dpa
->dpa_curlevel
, nextblkid
);
3324 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
,
3325 bp
, dbuf_prefetch_indirect_done
, dpa
, dpa
->dpa_prio
,
3326 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
3330 arc_buf_destroy(abuf
, private);
3334 * Issue prefetch reads for the given block on the given level. If the indirect
3335 * blocks above that block are not in memory, we will read them in
3336 * asynchronously. As a result, this call never blocks waiting for a read to
3337 * complete. Note that the prefetch might fail if the dataset is encrypted and
3338 * the encryption key is unmapped before the IO completes.
3341 dbuf_prefetch_impl(dnode_t
*dn
, int64_t level
, uint64_t blkid
,
3342 zio_priority_t prio
, arc_flags_t aflags
, dbuf_prefetch_fn cb
,
3346 int epbs
, nlevels
, curlevel
;
3349 ASSERT(blkid
!= DMU_BONUS_BLKID
);
3350 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
3352 if (blkid
> dn
->dn_maxblkid
)
3355 if (level
== 0 && dnode_block_freed(dn
, blkid
))
3359 * This dnode hasn't been written to disk yet, so there's nothing to
3362 nlevels
= dn
->dn_phys
->dn_nlevels
;
3363 if (level
>= nlevels
|| dn
->dn_phys
->dn_nblkptr
== 0)
3366 epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
3367 if (dn
->dn_phys
->dn_maxblkid
< blkid
<< (epbs
* level
))
3370 dmu_buf_impl_t
*db
= dbuf_find(dn
->dn_objset
, dn
->dn_object
,
3373 mutex_exit(&db
->db_mtx
);
3375 * This dbuf already exists. It is either CACHED, or
3376 * (we assume) about to be read or filled.
3382 * Find the closest ancestor (indirect block) of the target block
3383 * that is present in the cache. In this indirect block, we will
3384 * find the bp that is at curlevel, curblkid.
3388 while (curlevel
< nlevels
- 1) {
3389 int parent_level
= curlevel
+ 1;
3390 uint64_t parent_blkid
= curblkid
>> epbs
;
3393 if (dbuf_hold_impl(dn
, parent_level
, parent_blkid
,
3394 FALSE
, TRUE
, FTAG
, &db
) == 0) {
3395 blkptr_t
*bpp
= db
->db_buf
->b_data
;
3396 bp
= bpp
[P2PHASE(curblkid
, 1 << epbs
)];
3397 dbuf_rele(db
, FTAG
);
3401 curlevel
= parent_level
;
3402 curblkid
= parent_blkid
;
3405 if (curlevel
== nlevels
- 1) {
3406 /* No cached indirect blocks found. */
3407 ASSERT3U(curblkid
, <, dn
->dn_phys
->dn_nblkptr
);
3408 bp
= dn
->dn_phys
->dn_blkptr
[curblkid
];
3410 ASSERT(!BP_IS_REDACTED(&bp
) ||
3411 dsl_dataset_feature_is_active(dn
->dn_objset
->os_dsl_dataset
,
3412 SPA_FEATURE_REDACTED_DATASETS
));
3413 if (BP_IS_HOLE(&bp
) || BP_IS_REDACTED(&bp
))
3416 ASSERT3U(curlevel
, ==, BP_GET_LEVEL(&bp
));
3418 zio_t
*pio
= zio_root(dmu_objset_spa(dn
->dn_objset
), NULL
, NULL
,
3421 dbuf_prefetch_arg_t
*dpa
= kmem_zalloc(sizeof (*dpa
), KM_SLEEP
);
3422 dsl_dataset_t
*ds
= dn
->dn_objset
->os_dsl_dataset
;
3423 SET_BOOKMARK(&dpa
->dpa_zb
, ds
!= NULL
? ds
->ds_object
: DMU_META_OBJSET
,
3424 dn
->dn_object
, level
, blkid
);
3425 dpa
->dpa_curlevel
= curlevel
;
3426 dpa
->dpa_prio
= prio
;
3427 dpa
->dpa_aflags
= aflags
;
3428 dpa
->dpa_spa
= dn
->dn_objset
->os_spa
;
3429 dpa
->dpa_dnode
= dn
;
3430 dpa
->dpa_epbs
= epbs
;
3435 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
3436 if (dnode_level_is_l2cacheable(&bp
, dn
, level
))
3437 dpa
->dpa_aflags
|= ARC_FLAG_L2CACHE
;
3440 * If we have the indirect just above us, no need to do the asynchronous
3441 * prefetch chain; we'll just run the last step ourselves. If we're at
3442 * a higher level, though, we want to issue the prefetches for all the
3443 * indirect blocks asynchronously, so we can go on with whatever we were
3446 if (curlevel
== level
) {
3447 ASSERT3U(curblkid
, ==, blkid
);
3448 dbuf_issue_final_prefetch(dpa
, &bp
);
3450 arc_flags_t iter_aflags
= ARC_FLAG_NOWAIT
;
3451 zbookmark_phys_t zb
;
3453 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
3454 if (dnode_level_is_l2cacheable(&bp
, dn
, level
))
3455 iter_aflags
|= ARC_FLAG_L2CACHE
;
3457 SET_BOOKMARK(&zb
, ds
!= NULL
? ds
->ds_object
: DMU_META_OBJSET
,
3458 dn
->dn_object
, curlevel
, curblkid
);
3459 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
,
3460 &bp
, dbuf_prefetch_indirect_done
, dpa
, prio
,
3461 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
3465 * We use pio here instead of dpa_zio since it's possible that
3466 * dpa may have already been freed.
3477 dbuf_prefetch(dnode_t
*dn
, int64_t level
, uint64_t blkid
, zio_priority_t prio
,
3481 return (dbuf_prefetch_impl(dn
, level
, blkid
, prio
, aflags
, NULL
, NULL
));
3485 * Helper function for dbuf_hold_impl() to copy a buffer. Handles
3486 * the case of encrypted, compressed and uncompressed buffers by
3487 * allocating the new buffer, respectively, with arc_alloc_raw_buf(),
3488 * arc_alloc_compressed_buf() or arc_alloc_buf().*
3490 * NOTE: Declared noinline to avoid stack bloat in dbuf_hold_impl().
3492 noinline
static void
3493 dbuf_hold_copy(dnode_t
*dn
, dmu_buf_impl_t
*db
)
3495 dbuf_dirty_record_t
*dr
= db
->db_data_pending
;
3496 arc_buf_t
*data
= dr
->dt
.dl
.dr_data
;
3497 enum zio_compress compress_type
= arc_get_compression(data
);
3498 uint8_t complevel
= arc_get_complevel(data
);
3500 if (arc_is_encrypted(data
)) {
3501 boolean_t byteorder
;
3502 uint8_t salt
[ZIO_DATA_SALT_LEN
];
3503 uint8_t iv
[ZIO_DATA_IV_LEN
];
3504 uint8_t mac
[ZIO_DATA_MAC_LEN
];
3506 arc_get_raw_params(data
, &byteorder
, salt
, iv
, mac
);
3507 dbuf_set_data(db
, arc_alloc_raw_buf(dn
->dn_objset
->os_spa
, db
,
3508 dmu_objset_id(dn
->dn_objset
), byteorder
, salt
, iv
, mac
,
3509 dn
->dn_type
, arc_buf_size(data
), arc_buf_lsize(data
),
3510 compress_type
, complevel
));
3511 } else if (compress_type
!= ZIO_COMPRESS_OFF
) {
3512 dbuf_set_data(db
, arc_alloc_compressed_buf(
3513 dn
->dn_objset
->os_spa
, db
, arc_buf_size(data
),
3514 arc_buf_lsize(data
), compress_type
, complevel
));
3516 dbuf_set_data(db
, arc_alloc_buf(dn
->dn_objset
->os_spa
, db
,
3517 DBUF_GET_BUFC_TYPE(db
), db
->db
.db_size
));
3520 rw_enter(&db
->db_rwlock
, RW_WRITER
);
3521 bcopy(data
->b_data
, db
->db
.db_data
, arc_buf_size(data
));
3522 rw_exit(&db
->db_rwlock
);
3526 * Returns with db_holds incremented, and db_mtx not held.
3527 * Note: dn_struct_rwlock must be held.
3530 dbuf_hold_impl(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
3531 boolean_t fail_sparse
, boolean_t fail_uncached
,
3532 void *tag
, dmu_buf_impl_t
**dbp
)
3534 dmu_buf_impl_t
*db
, *parent
= NULL
;
3536 /* If the pool has been created, verify the tx_sync_lock is not held */
3537 spa_t
*spa
= dn
->dn_objset
->os_spa
;
3538 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
3540 ASSERT(!MUTEX_HELD(&dp
->dp_tx
.tx_sync_lock
));
3543 ASSERT(blkid
!= DMU_BONUS_BLKID
);
3544 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
3545 ASSERT3U(dn
->dn_nlevels
, >, level
);
3549 /* dbuf_find() returns with db_mtx held */
3550 db
= dbuf_find(dn
->dn_objset
, dn
->dn_object
, level
, blkid
);
3553 blkptr_t
*bp
= NULL
;
3557 return (SET_ERROR(ENOENT
));
3559 ASSERT3P(parent
, ==, NULL
);
3560 err
= dbuf_findbp(dn
, level
, blkid
, fail_sparse
, &parent
, &bp
);
3562 if (err
== 0 && bp
&& BP_IS_HOLE(bp
))
3563 err
= SET_ERROR(ENOENT
);
3566 dbuf_rele(parent
, NULL
);
3570 if (err
&& err
!= ENOENT
)
3572 db
= dbuf_create(dn
, level
, blkid
, parent
, bp
);
3575 if (fail_uncached
&& db
->db_state
!= DB_CACHED
) {
3576 mutex_exit(&db
->db_mtx
);
3577 return (SET_ERROR(ENOENT
));
3580 if (db
->db_buf
!= NULL
) {
3581 arc_buf_access(db
->db_buf
);
3582 ASSERT3P(db
->db
.db_data
, ==, db
->db_buf
->b_data
);
3585 ASSERT(db
->db_buf
== NULL
|| arc_referenced(db
->db_buf
));
3588 * If this buffer is currently syncing out, and we are
3589 * still referencing it from db_data, we need to make a copy
3590 * of it in case we decide we want to dirty it again in this txg.
3592 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
3593 dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
3594 db
->db_state
== DB_CACHED
&& db
->db_data_pending
) {
3595 dbuf_dirty_record_t
*dr
= db
->db_data_pending
;
3596 if (dr
->dt
.dl
.dr_data
== db
->db_buf
)
3597 dbuf_hold_copy(dn
, db
);
3600 if (multilist_link_active(&db
->db_cache_link
)) {
3601 ASSERT(zfs_refcount_is_zero(&db
->db_holds
));
3602 ASSERT(db
->db_caching_status
== DB_DBUF_CACHE
||
3603 db
->db_caching_status
== DB_DBUF_METADATA_CACHE
);
3605 multilist_remove(&dbuf_caches
[db
->db_caching_status
].cache
, db
);
3606 (void) zfs_refcount_remove_many(
3607 &dbuf_caches
[db
->db_caching_status
].size
,
3608 db
->db
.db_size
, db
);
3610 if (db
->db_caching_status
== DB_DBUF_METADATA_CACHE
) {
3611 DBUF_STAT_BUMPDOWN(metadata_cache_count
);
3613 DBUF_STAT_BUMPDOWN(cache_levels
[db
->db_level
]);
3614 DBUF_STAT_BUMPDOWN(cache_count
);
3615 DBUF_STAT_DECR(cache_levels_bytes
[db
->db_level
],
3618 db
->db_caching_status
= DB_NO_CACHE
;
3620 (void) zfs_refcount_add(&db
->db_holds
, tag
);
3622 mutex_exit(&db
->db_mtx
);
3624 /* NOTE: we can't rele the parent until after we drop the db_mtx */
3626 dbuf_rele(parent
, NULL
);
3628 ASSERT3P(DB_DNODE(db
), ==, dn
);
3629 ASSERT3U(db
->db_blkid
, ==, blkid
);
3630 ASSERT3U(db
->db_level
, ==, level
);
3637 dbuf_hold(dnode_t
*dn
, uint64_t blkid
, void *tag
)
3639 return (dbuf_hold_level(dn
, 0, blkid
, tag
));
3643 dbuf_hold_level(dnode_t
*dn
, int level
, uint64_t blkid
, void *tag
)
3646 int err
= dbuf_hold_impl(dn
, level
, blkid
, FALSE
, FALSE
, tag
, &db
);
3647 return (err
? NULL
: db
);
3651 dbuf_create_bonus(dnode_t
*dn
)
3653 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
3655 ASSERT(dn
->dn_bonus
== NULL
);
3656 dn
->dn_bonus
= dbuf_create(dn
, 0, DMU_BONUS_BLKID
, dn
->dn_dbuf
, NULL
);
3660 dbuf_spill_set_blksz(dmu_buf_t
*db_fake
, uint64_t blksz
, dmu_tx_t
*tx
)
3662 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3664 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
3665 return (SET_ERROR(ENOTSUP
));
3667 blksz
= SPA_MINBLOCKSIZE
;
3668 ASSERT3U(blksz
, <=, spa_maxblocksize(dmu_objset_spa(db
->db_objset
)));
3669 blksz
= P2ROUNDUP(blksz
, SPA_MINBLOCKSIZE
);
3671 dbuf_new_size(db
, blksz
, tx
);
3677 dbuf_rm_spill(dnode_t
*dn
, dmu_tx_t
*tx
)
3679 dbuf_free_range(dn
, DMU_SPILL_BLKID
, DMU_SPILL_BLKID
, tx
);
3682 #pragma weak dmu_buf_add_ref = dbuf_add_ref
3684 dbuf_add_ref(dmu_buf_impl_t
*db
, void *tag
)
3686 int64_t holds
= zfs_refcount_add(&db
->db_holds
, tag
);
3687 VERIFY3S(holds
, >, 1);
3690 #pragma weak dmu_buf_try_add_ref = dbuf_try_add_ref
3692 dbuf_try_add_ref(dmu_buf_t
*db_fake
, objset_t
*os
, uint64_t obj
, uint64_t blkid
,
3695 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3696 dmu_buf_impl_t
*found_db
;
3697 boolean_t result
= B_FALSE
;
3699 if (blkid
== DMU_BONUS_BLKID
)
3700 found_db
= dbuf_find_bonus(os
, obj
);
3702 found_db
= dbuf_find(os
, obj
, 0, blkid
);
3704 if (found_db
!= NULL
) {
3705 if (db
== found_db
&& dbuf_refcount(db
) > db
->db_dirtycnt
) {
3706 (void) zfs_refcount_add(&db
->db_holds
, tag
);
3709 mutex_exit(&found_db
->db_mtx
);
3715 * If you call dbuf_rele() you had better not be referencing the dnode handle
3716 * unless you have some other direct or indirect hold on the dnode. (An indirect
3717 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
3718 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
3719 * dnode's parent dbuf evicting its dnode handles.
3722 dbuf_rele(dmu_buf_impl_t
*db
, void *tag
)
3724 mutex_enter(&db
->db_mtx
);
3725 dbuf_rele_and_unlock(db
, tag
, B_FALSE
);
3729 dmu_buf_rele(dmu_buf_t
*db
, void *tag
)
3731 dbuf_rele((dmu_buf_impl_t
*)db
, tag
);
3735 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
3736 * db_dirtycnt and db_holds to be updated atomically. The 'evicting'
3737 * argument should be set if we are already in the dbuf-evicting code
3738 * path, in which case we don't want to recursively evict. This allows us to
3739 * avoid deeply nested stacks that would have a call flow similar to this:
3741 * dbuf_rele()-->dbuf_rele_and_unlock()-->dbuf_evict_notify()
3744 * +-----dbuf_destroy()<--dbuf_evict_one()<--------+
3748 dbuf_rele_and_unlock(dmu_buf_impl_t
*db
, void *tag
, boolean_t evicting
)
3753 ASSERT(MUTEX_HELD(&db
->db_mtx
));
3757 * Remove the reference to the dbuf before removing its hold on the
3758 * dnode so we can guarantee in dnode_move() that a referenced bonus
3759 * buffer has a corresponding dnode hold.
3761 holds
= zfs_refcount_remove(&db
->db_holds
, tag
);
3765 * We can't freeze indirects if there is a possibility that they
3766 * may be modified in the current syncing context.
3768 if (db
->db_buf
!= NULL
&&
3769 holds
== (db
->db_level
== 0 ? db
->db_dirtycnt
: 0)) {
3770 arc_buf_freeze(db
->db_buf
);
3773 if (holds
== db
->db_dirtycnt
&&
3774 db
->db_level
== 0 && db
->db_user_immediate_evict
)
3775 dbuf_evict_user(db
);
3778 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
3780 boolean_t evict_dbuf
= db
->db_pending_evict
;
3783 * If the dnode moves here, we cannot cross this
3784 * barrier until the move completes.
3789 atomic_dec_32(&dn
->dn_dbufs_count
);
3792 * Decrementing the dbuf count means that the bonus
3793 * buffer's dnode hold is no longer discounted in
3794 * dnode_move(). The dnode cannot move until after
3795 * the dnode_rele() below.
3800 * Do not reference db after its lock is dropped.
3801 * Another thread may evict it.
3803 mutex_exit(&db
->db_mtx
);
3806 dnode_evict_bonus(dn
);
3809 } else if (db
->db_buf
== NULL
) {
3811 * This is a special case: we never associated this
3812 * dbuf with any data allocated from the ARC.
3814 ASSERT(db
->db_state
== DB_UNCACHED
||
3815 db
->db_state
== DB_NOFILL
);
3817 } else if (arc_released(db
->db_buf
)) {
3819 * This dbuf has anonymous data associated with it.
3823 boolean_t do_arc_evict
= B_FALSE
;
3825 spa_t
*spa
= dmu_objset_spa(db
->db_objset
);
3827 if (!DBUF_IS_CACHEABLE(db
) &&
3828 db
->db_blkptr
!= NULL
&&
3829 !BP_IS_HOLE(db
->db_blkptr
) &&
3830 !BP_IS_EMBEDDED(db
->db_blkptr
)) {
3831 do_arc_evict
= B_TRUE
;
3832 bp
= *db
->db_blkptr
;
3835 if (!DBUF_IS_CACHEABLE(db
) ||
3836 db
->db_pending_evict
) {
3838 } else if (!multilist_link_active(&db
->db_cache_link
)) {
3839 ASSERT3U(db
->db_caching_status
, ==,
3842 dbuf_cached_state_t dcs
=
3843 dbuf_include_in_metadata_cache(db
) ?
3844 DB_DBUF_METADATA_CACHE
: DB_DBUF_CACHE
;
3845 db
->db_caching_status
= dcs
;
3847 multilist_insert(&dbuf_caches
[dcs
].cache
, db
);
3848 uint64_t db_size
= db
->db
.db_size
;
3849 size
= zfs_refcount_add_many(
3850 &dbuf_caches
[dcs
].size
, db_size
, db
);
3851 uint8_t db_level
= db
->db_level
;
3852 mutex_exit(&db
->db_mtx
);
3854 if (dcs
== DB_DBUF_METADATA_CACHE
) {
3855 DBUF_STAT_BUMP(metadata_cache_count
);
3857 metadata_cache_size_bytes_max
,
3860 DBUF_STAT_BUMP(cache_count
);
3861 DBUF_STAT_MAX(cache_size_bytes_max
,
3863 DBUF_STAT_BUMP(cache_levels
[db_level
]);
3865 cache_levels_bytes
[db_level
],
3869 if (dcs
== DB_DBUF_CACHE
&& !evicting
)
3870 dbuf_evict_notify(size
);
3874 arc_freed(spa
, &bp
);
3877 mutex_exit(&db
->db_mtx
);
3882 #pragma weak dmu_buf_refcount = dbuf_refcount
3884 dbuf_refcount(dmu_buf_impl_t
*db
)
3886 return (zfs_refcount_count(&db
->db_holds
));
3890 dmu_buf_user_refcount(dmu_buf_t
*db_fake
)
3893 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3895 mutex_enter(&db
->db_mtx
);
3896 ASSERT3U(zfs_refcount_count(&db
->db_holds
), >=, db
->db_dirtycnt
);
3897 holds
= zfs_refcount_count(&db
->db_holds
) - db
->db_dirtycnt
;
3898 mutex_exit(&db
->db_mtx
);
3904 dmu_buf_replace_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*old_user
,
3905 dmu_buf_user_t
*new_user
)
3907 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3909 mutex_enter(&db
->db_mtx
);
3910 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
3911 if (db
->db_user
== old_user
)
3912 db
->db_user
= new_user
;
3914 old_user
= db
->db_user
;
3915 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
3916 mutex_exit(&db
->db_mtx
);
3922 dmu_buf_set_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
3924 return (dmu_buf_replace_user(db_fake
, NULL
, user
));
3928 dmu_buf_set_user_ie(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
3930 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3932 db
->db_user_immediate_evict
= TRUE
;
3933 return (dmu_buf_set_user(db_fake
, user
));
3937 dmu_buf_remove_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
3939 return (dmu_buf_replace_user(db_fake
, user
, NULL
));
3943 dmu_buf_get_user(dmu_buf_t
*db_fake
)
3945 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3947 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
3948 return (db
->db_user
);
3952 dmu_buf_user_evict_wait()
3954 taskq_wait(dbu_evict_taskq
);
3958 dmu_buf_get_blkptr(dmu_buf_t
*db
)
3960 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3961 return (dbi
->db_blkptr
);
3965 dmu_buf_get_objset(dmu_buf_t
*db
)
3967 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3968 return (dbi
->db_objset
);
3972 dmu_buf_dnode_enter(dmu_buf_t
*db
)
3974 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3975 DB_DNODE_ENTER(dbi
);
3976 return (DB_DNODE(dbi
));
3980 dmu_buf_dnode_exit(dmu_buf_t
*db
)
3982 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3987 dbuf_check_blkptr(dnode_t
*dn
, dmu_buf_impl_t
*db
)
3989 /* ASSERT(dmu_tx_is_syncing(tx) */
3990 ASSERT(MUTEX_HELD(&db
->db_mtx
));
3992 if (db
->db_blkptr
!= NULL
)
3995 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3996 db
->db_blkptr
= DN_SPILL_BLKPTR(dn
->dn_phys
);
3997 BP_ZERO(db
->db_blkptr
);
4000 if (db
->db_level
== dn
->dn_phys
->dn_nlevels
-1) {
4002 * This buffer was allocated at a time when there was
4003 * no available blkptrs from the dnode, or it was
4004 * inappropriate to hook it in (i.e., nlevels mismatch).
4006 ASSERT(db
->db_blkid
< dn
->dn_phys
->dn_nblkptr
);
4007 ASSERT(db
->db_parent
== NULL
);
4008 db
->db_parent
= dn
->dn_dbuf
;
4009 db
->db_blkptr
= &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
];
4012 dmu_buf_impl_t
*parent
= db
->db_parent
;
4013 int epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
4015 ASSERT(dn
->dn_phys
->dn_nlevels
> 1);
4016 if (parent
== NULL
) {
4017 mutex_exit(&db
->db_mtx
);
4018 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
4019 parent
= dbuf_hold_level(dn
, db
->db_level
+ 1,
4020 db
->db_blkid
>> epbs
, db
);
4021 rw_exit(&dn
->dn_struct_rwlock
);
4022 mutex_enter(&db
->db_mtx
);
4023 db
->db_parent
= parent
;
4025 db
->db_blkptr
= (blkptr_t
*)parent
->db
.db_data
+
4026 (db
->db_blkid
& ((1ULL << epbs
) - 1));
4032 dbuf_sync_bonus(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
4034 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
4035 void *data
= dr
->dt
.dl
.dr_data
;
4037 ASSERT0(db
->db_level
);
4038 ASSERT(MUTEX_HELD(&db
->db_mtx
));
4039 ASSERT(db
->db_blkid
== DMU_BONUS_BLKID
);
4040 ASSERT(data
!= NULL
);
4042 dnode_t
*dn
= dr
->dr_dnode
;
4043 ASSERT3U(DN_MAX_BONUS_LEN(dn
->dn_phys
), <=,
4044 DN_SLOTS_TO_BONUSLEN(dn
->dn_phys
->dn_extra_slots
+ 1));
4045 bcopy(data
, DN_BONUS(dn
->dn_phys
), DN_MAX_BONUS_LEN(dn
->dn_phys
));
4047 dbuf_sync_leaf_verify_bonus_dnode(dr
);
4049 dbuf_undirty_bonus(dr
);
4050 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)tx
->tx_txg
, B_FALSE
);
4054 * When syncing out a blocks of dnodes, adjust the block to deal with
4055 * encryption. Normally, we make sure the block is decrypted before writing
4056 * it. If we have crypt params, then we are writing a raw (encrypted) block,
4057 * from a raw receive. In this case, set the ARC buf's crypt params so
4058 * that the BP will be filled with the correct byteorder, salt, iv, and mac.
4061 dbuf_prepare_encrypted_dnode_leaf(dbuf_dirty_record_t
*dr
)
4064 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
4066 ASSERT(MUTEX_HELD(&db
->db_mtx
));
4067 ASSERT3U(db
->db
.db_object
, ==, DMU_META_DNODE_OBJECT
);
4068 ASSERT3U(db
->db_level
, ==, 0);
4070 if (!db
->db_objset
->os_raw_receive
&& arc_is_encrypted(db
->db_buf
)) {
4071 zbookmark_phys_t zb
;
4074 * Unfortunately, there is currently no mechanism for
4075 * syncing context to handle decryption errors. An error
4076 * here is only possible if an attacker maliciously
4077 * changed a dnode block and updated the associated
4078 * checksums going up the block tree.
4080 SET_BOOKMARK(&zb
, dmu_objset_id(db
->db_objset
),
4081 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
4082 err
= arc_untransform(db
->db_buf
, db
->db_objset
->os_spa
,
4085 panic("Invalid dnode block MAC");
4086 } else if (dr
->dt
.dl
.dr_has_raw_params
) {
4087 (void) arc_release(dr
->dt
.dl
.dr_data
, db
);
4088 arc_convert_to_raw(dr
->dt
.dl
.dr_data
,
4089 dmu_objset_id(db
->db_objset
),
4090 dr
->dt
.dl
.dr_byteorder
, DMU_OT_DNODE
,
4091 dr
->dt
.dl
.dr_salt
, dr
->dt
.dl
.dr_iv
, dr
->dt
.dl
.dr_mac
);
4096 * dbuf_sync_indirect() is called recursively from dbuf_sync_list() so it
4097 * is critical the we not allow the compiler to inline this function in to
4098 * dbuf_sync_list() thereby drastically bloating the stack usage.
4100 noinline
static void
4101 dbuf_sync_indirect(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
4103 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
4104 dnode_t
*dn
= dr
->dr_dnode
;
4106 ASSERT(dmu_tx_is_syncing(tx
));
4108 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
4110 mutex_enter(&db
->db_mtx
);
4112 ASSERT(db
->db_level
> 0);
4115 /* Read the block if it hasn't been read yet. */
4116 if (db
->db_buf
== NULL
) {
4117 mutex_exit(&db
->db_mtx
);
4118 (void) dbuf_read(db
, NULL
, DB_RF_MUST_SUCCEED
);
4119 mutex_enter(&db
->db_mtx
);
4121 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
4122 ASSERT(db
->db_buf
!= NULL
);
4124 /* Indirect block size must match what the dnode thinks it is. */
4125 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
4126 dbuf_check_blkptr(dn
, db
);
4128 /* Provide the pending dirty record to child dbufs */
4129 db
->db_data_pending
= dr
;
4131 mutex_exit(&db
->db_mtx
);
4133 dbuf_write(dr
, db
->db_buf
, tx
);
4135 zio_t
*zio
= dr
->dr_zio
;
4136 mutex_enter(&dr
->dt
.di
.dr_mtx
);
4137 dbuf_sync_list(&dr
->dt
.di
.dr_children
, db
->db_level
- 1, tx
);
4138 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
4139 mutex_exit(&dr
->dt
.di
.dr_mtx
);
4144 * Verify that the size of the data in our bonus buffer does not exceed
4145 * its recorded size.
4147 * The purpose of this verification is to catch any cases in development
4148 * where the size of a phys structure (i.e space_map_phys_t) grows and,
4149 * due to incorrect feature management, older pools expect to read more
4150 * data even though they didn't actually write it to begin with.
4152 * For a example, this would catch an error in the feature logic where we
4153 * open an older pool and we expect to write the space map histogram of
4154 * a space map with size SPACE_MAP_SIZE_V0.
4157 dbuf_sync_leaf_verify_bonus_dnode(dbuf_dirty_record_t
*dr
)
4160 dnode_t
*dn
= dr
->dr_dnode
;
4163 * Encrypted bonus buffers can have data past their bonuslen.
4164 * Skip the verification of these blocks.
4166 if (DMU_OT_IS_ENCRYPTED(dn
->dn_bonustype
))
4169 uint16_t bonuslen
= dn
->dn_phys
->dn_bonuslen
;
4170 uint16_t maxbonuslen
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
);
4171 ASSERT3U(bonuslen
, <=, maxbonuslen
);
4173 arc_buf_t
*datap
= dr
->dt
.dl
.dr_data
;
4174 char *datap_end
= ((char *)datap
) + bonuslen
;
4175 char *datap_max
= ((char *)datap
) + maxbonuslen
;
4177 /* ensure that everything is zero after our data */
4178 for (; datap_end
< datap_max
; datap_end
++)
4179 ASSERT(*datap_end
== 0);
4184 dbuf_lightweight_bp(dbuf_dirty_record_t
*dr
)
4186 /* This must be a lightweight dirty record. */
4187 ASSERT3P(dr
->dr_dbuf
, ==, NULL
);
4188 dnode_t
*dn
= dr
->dr_dnode
;
4190 if (dn
->dn_phys
->dn_nlevels
== 1) {
4191 VERIFY3U(dr
->dt
.dll
.dr_blkid
, <, dn
->dn_phys
->dn_nblkptr
);
4192 return (&dn
->dn_phys
->dn_blkptr
[dr
->dt
.dll
.dr_blkid
]);
4194 dmu_buf_impl_t
*parent_db
= dr
->dr_parent
->dr_dbuf
;
4195 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
4196 VERIFY3U(parent_db
->db_level
, ==, 1);
4197 VERIFY3P(parent_db
->db_dnode_handle
->dnh_dnode
, ==, dn
);
4198 VERIFY3U(dr
->dt
.dll
.dr_blkid
>> epbs
, ==, parent_db
->db_blkid
);
4199 blkptr_t
*bp
= parent_db
->db
.db_data
;
4200 return (&bp
[dr
->dt
.dll
.dr_blkid
& ((1 << epbs
) - 1)]);
4205 dbuf_lightweight_ready(zio_t
*zio
)
4207 dbuf_dirty_record_t
*dr
= zio
->io_private
;
4208 blkptr_t
*bp
= zio
->io_bp
;
4210 if (zio
->io_error
!= 0)
4213 dnode_t
*dn
= dr
->dr_dnode
;
4215 blkptr_t
*bp_orig
= dbuf_lightweight_bp(dr
);
4216 spa_t
*spa
= dmu_objset_spa(dn
->dn_objset
);
4217 int64_t delta
= bp_get_dsize_sync(spa
, bp
) -
4218 bp_get_dsize_sync(spa
, bp_orig
);
4219 dnode_diduse_space(dn
, delta
);
4221 uint64_t blkid
= dr
->dt
.dll
.dr_blkid
;
4222 mutex_enter(&dn
->dn_mtx
);
4223 if (blkid
> dn
->dn_phys
->dn_maxblkid
) {
4224 ASSERT0(dn
->dn_objset
->os_raw_receive
);
4225 dn
->dn_phys
->dn_maxblkid
= blkid
;
4227 mutex_exit(&dn
->dn_mtx
);
4229 if (!BP_IS_EMBEDDED(bp
)) {
4230 uint64_t fill
= BP_IS_HOLE(bp
) ? 0 : 1;
4231 BP_SET_FILL(bp
, fill
);
4234 dmu_buf_impl_t
*parent_db
;
4235 EQUIV(dr
->dr_parent
== NULL
, dn
->dn_phys
->dn_nlevels
== 1);
4236 if (dr
->dr_parent
== NULL
) {
4237 parent_db
= dn
->dn_dbuf
;
4239 parent_db
= dr
->dr_parent
->dr_dbuf
;
4241 rw_enter(&parent_db
->db_rwlock
, RW_WRITER
);
4243 rw_exit(&parent_db
->db_rwlock
);
4247 dbuf_lightweight_physdone(zio_t
*zio
)
4249 dbuf_dirty_record_t
*dr
= zio
->io_private
;
4250 dsl_pool_t
*dp
= spa_get_dsl(zio
->io_spa
);
4251 ASSERT3U(dr
->dr_txg
, ==, zio
->io_txg
);
4254 * The callback will be called io_phys_children times. Retire one
4255 * portion of our dirty space each time we are called. Any rounding
4256 * error will be cleaned up by dbuf_lightweight_done().
4258 int delta
= dr
->dr_accounted
/ zio
->io_phys_children
;
4259 dsl_pool_undirty_space(dp
, delta
, zio
->io_txg
);
4263 dbuf_lightweight_done(zio_t
*zio
)
4265 dbuf_dirty_record_t
*dr
= zio
->io_private
;
4267 VERIFY0(zio
->io_error
);
4269 objset_t
*os
= dr
->dr_dnode
->dn_objset
;
4270 dmu_tx_t
*tx
= os
->os_synctx
;
4272 if (zio
->io_flags
& (ZIO_FLAG_IO_REWRITE
| ZIO_FLAG_NOPWRITE
)) {
4273 ASSERT(BP_EQUAL(zio
->io_bp
, &zio
->io_bp_orig
));
4275 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
4276 (void) dsl_dataset_block_kill(ds
, &zio
->io_bp_orig
, tx
, B_TRUE
);
4277 dsl_dataset_block_born(ds
, zio
->io_bp
, tx
);
4281 * See comment in dbuf_write_done().
4283 if (zio
->io_phys_children
== 0) {
4284 dsl_pool_undirty_space(dmu_objset_pool(os
),
4285 dr
->dr_accounted
, zio
->io_txg
);
4287 dsl_pool_undirty_space(dmu_objset_pool(os
),
4288 dr
->dr_accounted
% zio
->io_phys_children
, zio
->io_txg
);
4291 abd_free(dr
->dt
.dll
.dr_abd
);
4292 kmem_free(dr
, sizeof (*dr
));
4295 noinline
static void
4296 dbuf_sync_lightweight(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
4298 dnode_t
*dn
= dr
->dr_dnode
;
4300 if (dn
->dn_phys
->dn_nlevels
== 1) {
4303 pio
= dr
->dr_parent
->dr_zio
;
4306 zbookmark_phys_t zb
= {
4307 .zb_objset
= dmu_objset_id(dn
->dn_objset
),
4308 .zb_object
= dn
->dn_object
,
4310 .zb_blkid
= dr
->dt
.dll
.dr_blkid
,
4314 * See comment in dbuf_write(). This is so that zio->io_bp_orig
4315 * will have the old BP in dbuf_lightweight_done().
4317 dr
->dr_bp_copy
= *dbuf_lightweight_bp(dr
);
4319 dr
->dr_zio
= zio_write(pio
, dmu_objset_spa(dn
->dn_objset
),
4320 dmu_tx_get_txg(tx
), &dr
->dr_bp_copy
, dr
->dt
.dll
.dr_abd
,
4321 dn
->dn_datablksz
, abd_get_size(dr
->dt
.dll
.dr_abd
),
4322 &dr
->dt
.dll
.dr_props
, dbuf_lightweight_ready
, NULL
,
4323 dbuf_lightweight_physdone
, dbuf_lightweight_done
, dr
,
4324 ZIO_PRIORITY_ASYNC_WRITE
,
4325 ZIO_FLAG_MUSTSUCCEED
| dr
->dt
.dll
.dr_flags
, &zb
);
4327 zio_nowait(dr
->dr_zio
);
4331 * dbuf_sync_leaf() is called recursively from dbuf_sync_list() so it is
4332 * critical the we not allow the compiler to inline this function in to
4333 * dbuf_sync_list() thereby drastically bloating the stack usage.
4335 noinline
static void
4336 dbuf_sync_leaf(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
4338 arc_buf_t
**datap
= &dr
->dt
.dl
.dr_data
;
4339 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
4340 dnode_t
*dn
= dr
->dr_dnode
;
4342 uint64_t txg
= tx
->tx_txg
;
4344 ASSERT(dmu_tx_is_syncing(tx
));
4346 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
4348 mutex_enter(&db
->db_mtx
);
4350 * To be synced, we must be dirtied. But we
4351 * might have been freed after the dirty.
4353 if (db
->db_state
== DB_UNCACHED
) {
4354 /* This buffer has been freed since it was dirtied */
4355 ASSERT(db
->db
.db_data
== NULL
);
4356 } else if (db
->db_state
== DB_FILL
) {
4357 /* This buffer was freed and is now being re-filled */
4358 ASSERT(db
->db
.db_data
!= dr
->dt
.dl
.dr_data
);
4360 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_NOFILL
);
4364 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
4365 mutex_enter(&dn
->dn_mtx
);
4366 if (!(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
)) {
4368 * In the previous transaction group, the bonus buffer
4369 * was entirely used to store the attributes for the
4370 * dnode which overrode the dn_spill field. However,
4371 * when adding more attributes to the file a spill
4372 * block was required to hold the extra attributes.
4374 * Make sure to clear the garbage left in the dn_spill
4375 * field from the previous attributes in the bonus
4376 * buffer. Otherwise, after writing out the spill
4377 * block to the new allocated dva, it will free
4378 * the old block pointed to by the invalid dn_spill.
4380 db
->db_blkptr
= NULL
;
4382 dn
->dn_phys
->dn_flags
|= DNODE_FLAG_SPILL_BLKPTR
;
4383 mutex_exit(&dn
->dn_mtx
);
4387 * If this is a bonus buffer, simply copy the bonus data into the
4388 * dnode. It will be written out when the dnode is synced (and it
4389 * will be synced, since it must have been dirty for dbuf_sync to
4392 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
4393 ASSERT(dr
->dr_dbuf
== db
);
4394 dbuf_sync_bonus(dr
, tx
);
4401 * This function may have dropped the db_mtx lock allowing a dmu_sync
4402 * operation to sneak in. As a result, we need to ensure that we
4403 * don't check the dr_override_state until we have returned from
4404 * dbuf_check_blkptr.
4406 dbuf_check_blkptr(dn
, db
);
4409 * If this buffer is in the middle of an immediate write,
4410 * wait for the synchronous IO to complete.
4412 while (dr
->dt
.dl
.dr_override_state
== DR_IN_DMU_SYNC
) {
4413 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
4414 cv_wait(&db
->db_changed
, &db
->db_mtx
);
4415 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_NOT_OVERRIDDEN
);
4419 * If this is a dnode block, ensure it is appropriately encrypted
4420 * or decrypted, depending on what we are writing to it this txg.
4422 if (os
->os_encrypted
&& dn
->dn_object
== DMU_META_DNODE_OBJECT
)
4423 dbuf_prepare_encrypted_dnode_leaf(dr
);
4425 if (db
->db_state
!= DB_NOFILL
&&
4426 dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
4427 zfs_refcount_count(&db
->db_holds
) > 1 &&
4428 dr
->dt
.dl
.dr_override_state
!= DR_OVERRIDDEN
&&
4429 *datap
== db
->db_buf
) {
4431 * If this buffer is currently "in use" (i.e., there
4432 * are active holds and db_data still references it),
4433 * then make a copy before we start the write so that
4434 * any modifications from the open txg will not leak
4437 * NOTE: this copy does not need to be made for
4438 * objects only modified in the syncing context (e.g.
4439 * DNONE_DNODE blocks).
4441 int psize
= arc_buf_size(*datap
);
4442 int lsize
= arc_buf_lsize(*datap
);
4443 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
4444 enum zio_compress compress_type
= arc_get_compression(*datap
);
4445 uint8_t complevel
= arc_get_complevel(*datap
);
4447 if (arc_is_encrypted(*datap
)) {
4448 boolean_t byteorder
;
4449 uint8_t salt
[ZIO_DATA_SALT_LEN
];
4450 uint8_t iv
[ZIO_DATA_IV_LEN
];
4451 uint8_t mac
[ZIO_DATA_MAC_LEN
];
4453 arc_get_raw_params(*datap
, &byteorder
, salt
, iv
, mac
);
4454 *datap
= arc_alloc_raw_buf(os
->os_spa
, db
,
4455 dmu_objset_id(os
), byteorder
, salt
, iv
, mac
,
4456 dn
->dn_type
, psize
, lsize
, compress_type
,
4458 } else if (compress_type
!= ZIO_COMPRESS_OFF
) {
4459 ASSERT3U(type
, ==, ARC_BUFC_DATA
);
4460 *datap
= arc_alloc_compressed_buf(os
->os_spa
, db
,
4461 psize
, lsize
, compress_type
, complevel
);
4463 *datap
= arc_alloc_buf(os
->os_spa
, db
, type
, psize
);
4465 bcopy(db
->db
.db_data
, (*datap
)->b_data
, psize
);
4467 db
->db_data_pending
= dr
;
4469 mutex_exit(&db
->db_mtx
);
4471 dbuf_write(dr
, *datap
, tx
);
4473 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
4474 if (dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
4475 list_insert_tail(&dn
->dn_dirty_records
[txg
& TXG_MASK
], dr
);
4477 zio_nowait(dr
->dr_zio
);
4482 dbuf_sync_list(list_t
*list
, int level
, dmu_tx_t
*tx
)
4484 dbuf_dirty_record_t
*dr
;
4486 while ((dr
= list_head(list
))) {
4487 if (dr
->dr_zio
!= NULL
) {
4489 * If we find an already initialized zio then we
4490 * are processing the meta-dnode, and we have finished.
4491 * The dbufs for all dnodes are put back on the list
4492 * during processing, so that we can zio_wait()
4493 * these IOs after initiating all child IOs.
4495 ASSERT3U(dr
->dr_dbuf
->db
.db_object
, ==,
4496 DMU_META_DNODE_OBJECT
);
4499 list_remove(list
, dr
);
4500 if (dr
->dr_dbuf
== NULL
) {
4501 dbuf_sync_lightweight(dr
, tx
);
4503 if (dr
->dr_dbuf
->db_blkid
!= DMU_BONUS_BLKID
&&
4504 dr
->dr_dbuf
->db_blkid
!= DMU_SPILL_BLKID
) {
4505 VERIFY3U(dr
->dr_dbuf
->db_level
, ==, level
);
4507 if (dr
->dr_dbuf
->db_level
> 0)
4508 dbuf_sync_indirect(dr
, tx
);
4510 dbuf_sync_leaf(dr
, tx
);
4517 dbuf_write_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
4519 dmu_buf_impl_t
*db
= vdb
;
4521 blkptr_t
*bp
= zio
->io_bp
;
4522 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
4523 spa_t
*spa
= zio
->io_spa
;
4528 ASSERT3P(db
->db_blkptr
, !=, NULL
);
4529 ASSERT3P(&db
->db_data_pending
->dr_bp_copy
, ==, bp
);
4533 delta
= bp_get_dsize_sync(spa
, bp
) - bp_get_dsize_sync(spa
, bp_orig
);
4534 dnode_diduse_space(dn
, delta
- zio
->io_prev_space_delta
);
4535 zio
->io_prev_space_delta
= delta
;
4537 if (bp
->blk_birth
!= 0) {
4538 ASSERT((db
->db_blkid
!= DMU_SPILL_BLKID
&&
4539 BP_GET_TYPE(bp
) == dn
->dn_type
) ||
4540 (db
->db_blkid
== DMU_SPILL_BLKID
&&
4541 BP_GET_TYPE(bp
) == dn
->dn_bonustype
) ||
4542 BP_IS_EMBEDDED(bp
));
4543 ASSERT(BP_GET_LEVEL(bp
) == db
->db_level
);
4546 mutex_enter(&db
->db_mtx
);
4549 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
4550 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
4551 ASSERT(!(BP_IS_HOLE(bp
)) &&
4552 db
->db_blkptr
== DN_SPILL_BLKPTR(dn
->dn_phys
));
4556 if (db
->db_level
== 0) {
4557 mutex_enter(&dn
->dn_mtx
);
4558 if (db
->db_blkid
> dn
->dn_phys
->dn_maxblkid
&&
4559 db
->db_blkid
!= DMU_SPILL_BLKID
) {
4560 ASSERT0(db
->db_objset
->os_raw_receive
);
4561 dn
->dn_phys
->dn_maxblkid
= db
->db_blkid
;
4563 mutex_exit(&dn
->dn_mtx
);
4565 if (dn
->dn_type
== DMU_OT_DNODE
) {
4567 while (i
< db
->db
.db_size
) {
4569 (void *)(((char *)db
->db
.db_data
) + i
);
4571 i
+= DNODE_MIN_SIZE
;
4572 if (dnp
->dn_type
!= DMU_OT_NONE
) {
4574 i
+= dnp
->dn_extra_slots
*
4579 if (BP_IS_HOLE(bp
)) {
4586 blkptr_t
*ibp
= db
->db
.db_data
;
4587 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
4588 for (i
= db
->db
.db_size
>> SPA_BLKPTRSHIFT
; i
> 0; i
--, ibp
++) {
4589 if (BP_IS_HOLE(ibp
))
4591 fill
+= BP_GET_FILL(ibp
);
4596 if (!BP_IS_EMBEDDED(bp
))
4597 BP_SET_FILL(bp
, fill
);
4599 mutex_exit(&db
->db_mtx
);
4601 db_lock_type_t dblt
= dmu_buf_lock_parent(db
, RW_WRITER
, FTAG
);
4602 *db
->db_blkptr
= *bp
;
4603 dmu_buf_unlock_parent(db
, dblt
, FTAG
);
4608 * This function gets called just prior to running through the compression
4609 * stage of the zio pipeline. If we're an indirect block comprised of only
4610 * holes, then we want this indirect to be compressed away to a hole. In
4611 * order to do that we must zero out any information about the holes that
4612 * this indirect points to prior to before we try to compress it.
4615 dbuf_write_children_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
4617 dmu_buf_impl_t
*db
= vdb
;
4620 unsigned int epbs
, i
;
4622 ASSERT3U(db
->db_level
, >, 0);
4625 epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
4626 ASSERT3U(epbs
, <, 31);
4628 /* Determine if all our children are holes */
4629 for (i
= 0, bp
= db
->db
.db_data
; i
< 1ULL << epbs
; i
++, bp
++) {
4630 if (!BP_IS_HOLE(bp
))
4635 * If all the children are holes, then zero them all out so that
4636 * we may get compressed away.
4638 if (i
== 1ULL << epbs
) {
4640 * We only found holes. Grab the rwlock to prevent
4641 * anybody from reading the blocks we're about to
4644 rw_enter(&db
->db_rwlock
, RW_WRITER
);
4645 bzero(db
->db
.db_data
, db
->db
.db_size
);
4646 rw_exit(&db
->db_rwlock
);
4652 * The SPA will call this callback several times for each zio - once
4653 * for every physical child i/o (zio->io_phys_children times). This
4654 * allows the DMU to monitor the progress of each logical i/o. For example,
4655 * there may be 2 copies of an indirect block, or many fragments of a RAID-Z
4656 * block. There may be a long delay before all copies/fragments are completed,
4657 * so this callback allows us to retire dirty space gradually, as the physical
4662 dbuf_write_physdone(zio_t
*zio
, arc_buf_t
*buf
, void *arg
)
4664 dmu_buf_impl_t
*db
= arg
;
4665 objset_t
*os
= db
->db_objset
;
4666 dsl_pool_t
*dp
= dmu_objset_pool(os
);
4667 dbuf_dirty_record_t
*dr
;
4670 dr
= db
->db_data_pending
;
4671 ASSERT3U(dr
->dr_txg
, ==, zio
->io_txg
);
4674 * The callback will be called io_phys_children times. Retire one
4675 * portion of our dirty space each time we are called. Any rounding
4676 * error will be cleaned up by dbuf_write_done().
4678 delta
= dr
->dr_accounted
/ zio
->io_phys_children
;
4679 dsl_pool_undirty_space(dp
, delta
, zio
->io_txg
);
4684 dbuf_write_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
4686 dmu_buf_impl_t
*db
= vdb
;
4687 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
4688 blkptr_t
*bp
= db
->db_blkptr
;
4689 objset_t
*os
= db
->db_objset
;
4690 dmu_tx_t
*tx
= os
->os_synctx
;
4692 ASSERT0(zio
->io_error
);
4693 ASSERT(db
->db_blkptr
== bp
);
4696 * For nopwrites and rewrites we ensure that the bp matches our
4697 * original and bypass all the accounting.
4699 if (zio
->io_flags
& (ZIO_FLAG_IO_REWRITE
| ZIO_FLAG_NOPWRITE
)) {
4700 ASSERT(BP_EQUAL(bp
, bp_orig
));
4702 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
4703 (void) dsl_dataset_block_kill(ds
, bp_orig
, tx
, B_TRUE
);
4704 dsl_dataset_block_born(ds
, bp
, tx
);
4707 mutex_enter(&db
->db_mtx
);
4711 dbuf_dirty_record_t
*dr
= db
->db_data_pending
;
4712 dnode_t
*dn
= dr
->dr_dnode
;
4713 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
4714 ASSERT(dr
->dr_dbuf
== db
);
4715 ASSERT(list_next(&db
->db_dirty_records
, dr
) == NULL
);
4716 list_remove(&db
->db_dirty_records
, dr
);
4719 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
4720 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
4721 ASSERT(!(BP_IS_HOLE(db
->db_blkptr
)) &&
4722 db
->db_blkptr
== DN_SPILL_BLKPTR(dn
->dn_phys
));
4726 if (db
->db_level
== 0) {
4727 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
4728 ASSERT(dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
);
4729 if (db
->db_state
!= DB_NOFILL
) {
4730 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
4731 arc_buf_destroy(dr
->dt
.dl
.dr_data
, db
);
4734 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
4735 ASSERT3U(db
->db
.db_size
, ==, 1 << dn
->dn_phys
->dn_indblkshift
);
4736 if (!BP_IS_HOLE(db
->db_blkptr
)) {
4737 int epbs __maybe_unused
= dn
->dn_phys
->dn_indblkshift
-
4739 ASSERT3U(db
->db_blkid
, <=,
4740 dn
->dn_phys
->dn_maxblkid
>> (db
->db_level
* epbs
));
4741 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==,
4744 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
4745 list_destroy(&dr
->dt
.di
.dr_children
);
4748 cv_broadcast(&db
->db_changed
);
4749 ASSERT(db
->db_dirtycnt
> 0);
4750 db
->db_dirtycnt
-= 1;
4751 db
->db_data_pending
= NULL
;
4752 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)tx
->tx_txg
, B_FALSE
);
4755 * If we didn't do a physical write in this ZIO and we
4756 * still ended up here, it means that the space of the
4757 * dbuf that we just released (and undirtied) above hasn't
4758 * been marked as undirtied in the pool's accounting.
4760 * Thus, we undirty that space in the pool's view of the
4761 * world here. For physical writes this type of update
4762 * happens in dbuf_write_physdone().
4764 * If we did a physical write, cleanup any rounding errors
4765 * that came up due to writing multiple copies of a block
4766 * on disk [see dbuf_write_physdone()].
4768 if (zio
->io_phys_children
== 0) {
4769 dsl_pool_undirty_space(dmu_objset_pool(os
),
4770 dr
->dr_accounted
, zio
->io_txg
);
4772 dsl_pool_undirty_space(dmu_objset_pool(os
),
4773 dr
->dr_accounted
% zio
->io_phys_children
, zio
->io_txg
);
4776 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
4780 dbuf_write_nofill_ready(zio_t
*zio
)
4782 dbuf_write_ready(zio
, NULL
, zio
->io_private
);
4786 dbuf_write_nofill_done(zio_t
*zio
)
4788 dbuf_write_done(zio
, NULL
, zio
->io_private
);
4792 dbuf_write_override_ready(zio_t
*zio
)
4794 dbuf_dirty_record_t
*dr
= zio
->io_private
;
4795 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
4797 dbuf_write_ready(zio
, NULL
, db
);
4801 dbuf_write_override_done(zio_t
*zio
)
4803 dbuf_dirty_record_t
*dr
= zio
->io_private
;
4804 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
4805 blkptr_t
*obp
= &dr
->dt
.dl
.dr_overridden_by
;
4807 mutex_enter(&db
->db_mtx
);
4808 if (!BP_EQUAL(zio
->io_bp
, obp
)) {
4809 if (!BP_IS_HOLE(obp
))
4810 dsl_free(spa_get_dsl(zio
->io_spa
), zio
->io_txg
, obp
);
4811 arc_release(dr
->dt
.dl
.dr_data
, db
);
4813 mutex_exit(&db
->db_mtx
);
4815 dbuf_write_done(zio
, NULL
, db
);
4817 if (zio
->io_abd
!= NULL
)
4818 abd_free(zio
->io_abd
);
4821 typedef struct dbuf_remap_impl_callback_arg
{
4823 uint64_t drica_blk_birth
;
4825 } dbuf_remap_impl_callback_arg_t
;
4828 dbuf_remap_impl_callback(uint64_t vdev
, uint64_t offset
, uint64_t size
,
4831 dbuf_remap_impl_callback_arg_t
*drica
= arg
;
4832 objset_t
*os
= drica
->drica_os
;
4833 spa_t
*spa
= dmu_objset_spa(os
);
4834 dmu_tx_t
*tx
= drica
->drica_tx
;
4836 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa
)));
4838 if (os
== spa_meta_objset(spa
)) {
4839 spa_vdev_indirect_mark_obsolete(spa
, vdev
, offset
, size
, tx
);
4841 dsl_dataset_block_remapped(dmu_objset_ds(os
), vdev
, offset
,
4842 size
, drica
->drica_blk_birth
, tx
);
4847 dbuf_remap_impl(dnode_t
*dn
, blkptr_t
*bp
, krwlock_t
*rw
, dmu_tx_t
*tx
)
4849 blkptr_t bp_copy
= *bp
;
4850 spa_t
*spa
= dmu_objset_spa(dn
->dn_objset
);
4851 dbuf_remap_impl_callback_arg_t drica
;
4853 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa
)));
4855 drica
.drica_os
= dn
->dn_objset
;
4856 drica
.drica_blk_birth
= bp
->blk_birth
;
4857 drica
.drica_tx
= tx
;
4858 if (spa_remap_blkptr(spa
, &bp_copy
, dbuf_remap_impl_callback
,
4861 * If the blkptr being remapped is tracked by a livelist,
4862 * then we need to make sure the livelist reflects the update.
4863 * First, cancel out the old blkptr by appending a 'FREE'
4864 * entry. Next, add an 'ALLOC' to track the new version. This
4865 * way we avoid trying to free an inaccurate blkptr at delete.
4866 * Note that embedded blkptrs are not tracked in livelists.
4868 if (dn
->dn_objset
!= spa_meta_objset(spa
)) {
4869 dsl_dataset_t
*ds
= dmu_objset_ds(dn
->dn_objset
);
4870 if (dsl_deadlist_is_open(&ds
->ds_dir
->dd_livelist
) &&
4871 bp
->blk_birth
> ds
->ds_dir
->dd_origin_txg
) {
4872 ASSERT(!BP_IS_EMBEDDED(bp
));
4873 ASSERT(dsl_dir_is_clone(ds
->ds_dir
));
4874 ASSERT(spa_feature_is_enabled(spa
,
4875 SPA_FEATURE_LIVELIST
));
4876 bplist_append(&ds
->ds_dir
->dd_pending_frees
,
4878 bplist_append(&ds
->ds_dir
->dd_pending_allocs
,
4884 * The db_rwlock prevents dbuf_read_impl() from
4885 * dereferencing the BP while we are changing it. To
4886 * avoid lock contention, only grab it when we are actually
4890 rw_enter(rw
, RW_WRITER
);
4898 * Remap any existing BP's to concrete vdevs, if possible.
4901 dbuf_remap(dnode_t
*dn
, dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
4903 spa_t
*spa
= dmu_objset_spa(db
->db_objset
);
4904 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa
)));
4906 if (!spa_feature_is_active(spa
, SPA_FEATURE_DEVICE_REMOVAL
))
4909 if (db
->db_level
> 0) {
4910 blkptr_t
*bp
= db
->db
.db_data
;
4911 for (int i
= 0; i
< db
->db
.db_size
>> SPA_BLKPTRSHIFT
; i
++) {
4912 dbuf_remap_impl(dn
, &bp
[i
], &db
->db_rwlock
, tx
);
4914 } else if (db
->db
.db_object
== DMU_META_DNODE_OBJECT
) {
4915 dnode_phys_t
*dnp
= db
->db
.db_data
;
4916 ASSERT3U(db
->db_dnode_handle
->dnh_dnode
->dn_type
, ==,
4918 for (int i
= 0; i
< db
->db
.db_size
>> DNODE_SHIFT
;
4919 i
+= dnp
[i
].dn_extra_slots
+ 1) {
4920 for (int j
= 0; j
< dnp
[i
].dn_nblkptr
; j
++) {
4921 krwlock_t
*lock
= (dn
->dn_dbuf
== NULL
? NULL
:
4922 &dn
->dn_dbuf
->db_rwlock
);
4923 dbuf_remap_impl(dn
, &dnp
[i
].dn_blkptr
[j
], lock
,
4931 /* Issue I/O to commit a dirty buffer to disk. */
4933 dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
)
4935 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
4936 dnode_t
*dn
= dr
->dr_dnode
;
4938 dmu_buf_impl_t
*parent
= db
->db_parent
;
4939 uint64_t txg
= tx
->tx_txg
;
4940 zbookmark_phys_t zb
;
4942 zio_t
*pio
; /* parent I/O */
4945 ASSERT(dmu_tx_is_syncing(tx
));
4949 if (db
->db_state
!= DB_NOFILL
) {
4950 if (db
->db_level
> 0 || dn
->dn_type
== DMU_OT_DNODE
) {
4952 * Private object buffers are released here rather
4953 * than in dbuf_dirty() since they are only modified
4954 * in the syncing context and we don't want the
4955 * overhead of making multiple copies of the data.
4957 if (BP_IS_HOLE(db
->db_blkptr
)) {
4960 dbuf_release_bp(db
);
4962 dbuf_remap(dn
, db
, tx
);
4966 if (parent
!= dn
->dn_dbuf
) {
4967 /* Our parent is an indirect block. */
4968 /* We have a dirty parent that has been scheduled for write. */
4969 ASSERT(parent
&& parent
->db_data_pending
);
4970 /* Our parent's buffer is one level closer to the dnode. */
4971 ASSERT(db
->db_level
== parent
->db_level
-1);
4973 * We're about to modify our parent's db_data by modifying
4974 * our block pointer, so the parent must be released.
4976 ASSERT(arc_released(parent
->db_buf
));
4977 pio
= parent
->db_data_pending
->dr_zio
;
4979 /* Our parent is the dnode itself. */
4980 ASSERT((db
->db_level
== dn
->dn_phys
->dn_nlevels
-1 &&
4981 db
->db_blkid
!= DMU_SPILL_BLKID
) ||
4982 (db
->db_blkid
== DMU_SPILL_BLKID
&& db
->db_level
== 0));
4983 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
4984 ASSERT3P(db
->db_blkptr
, ==,
4985 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
4989 ASSERT(db
->db_level
== 0 || data
== db
->db_buf
);
4990 ASSERT3U(db
->db_blkptr
->blk_birth
, <=, txg
);
4993 SET_BOOKMARK(&zb
, os
->os_dsl_dataset
?
4994 os
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
4995 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
4997 if (db
->db_blkid
== DMU_SPILL_BLKID
)
4999 wp_flag
|= (db
->db_state
== DB_NOFILL
) ? WP_NOFILL
: 0;
5001 dmu_write_policy(os
, dn
, db
->db_level
, wp_flag
, &zp
);
5004 * We copy the blkptr now (rather than when we instantiate the dirty
5005 * record), because its value can change between open context and
5006 * syncing context. We do not need to hold dn_struct_rwlock to read
5007 * db_blkptr because we are in syncing context.
5009 dr
->dr_bp_copy
= *db
->db_blkptr
;
5011 if (db
->db_level
== 0 &&
5012 dr
->dt
.dl
.dr_override_state
== DR_OVERRIDDEN
) {
5014 * The BP for this block has been provided by open context
5015 * (by dmu_sync() or dmu_buf_write_embedded()).
5017 abd_t
*contents
= (data
!= NULL
) ?
5018 abd_get_from_buf(data
->b_data
, arc_buf_size(data
)) : NULL
;
5020 dr
->dr_zio
= zio_write(pio
, os
->os_spa
, txg
, &dr
->dr_bp_copy
,
5021 contents
, db
->db
.db_size
, db
->db
.db_size
, &zp
,
5022 dbuf_write_override_ready
, NULL
, NULL
,
5023 dbuf_write_override_done
,
5024 dr
, ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
5025 mutex_enter(&db
->db_mtx
);
5026 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
5027 zio_write_override(dr
->dr_zio
, &dr
->dt
.dl
.dr_overridden_by
,
5028 dr
->dt
.dl
.dr_copies
, dr
->dt
.dl
.dr_nopwrite
);
5029 mutex_exit(&db
->db_mtx
);
5030 } else if (db
->db_state
== DB_NOFILL
) {
5031 ASSERT(zp
.zp_checksum
== ZIO_CHECKSUM_OFF
||
5032 zp
.zp_checksum
== ZIO_CHECKSUM_NOPARITY
);
5033 dr
->dr_zio
= zio_write(pio
, os
->os_spa
, txg
,
5034 &dr
->dr_bp_copy
, NULL
, db
->db
.db_size
, db
->db
.db_size
, &zp
,
5035 dbuf_write_nofill_ready
, NULL
, NULL
,
5036 dbuf_write_nofill_done
, db
,
5037 ZIO_PRIORITY_ASYNC_WRITE
,
5038 ZIO_FLAG_MUSTSUCCEED
| ZIO_FLAG_NODATA
, &zb
);
5040 ASSERT(arc_released(data
));
5043 * For indirect blocks, we want to setup the children
5044 * ready callback so that we can properly handle an indirect
5045 * block that only contains holes.
5047 arc_write_done_func_t
*children_ready_cb
= NULL
;
5048 if (db
->db_level
!= 0)
5049 children_ready_cb
= dbuf_write_children_ready
;
5051 dr
->dr_zio
= arc_write(pio
, os
->os_spa
, txg
,
5052 &dr
->dr_bp_copy
, data
, dbuf_is_l2cacheable(db
),
5053 &zp
, dbuf_write_ready
,
5054 children_ready_cb
, dbuf_write_physdone
,
5055 dbuf_write_done
, db
, ZIO_PRIORITY_ASYNC_WRITE
,
5056 ZIO_FLAG_MUSTSUCCEED
, &zb
);
5060 EXPORT_SYMBOL(dbuf_find
);
5061 EXPORT_SYMBOL(dbuf_is_metadata
);
5062 EXPORT_SYMBOL(dbuf_destroy
);
5063 EXPORT_SYMBOL(dbuf_loan_arcbuf
);
5064 EXPORT_SYMBOL(dbuf_whichblock
);
5065 EXPORT_SYMBOL(dbuf_read
);
5066 EXPORT_SYMBOL(dbuf_unoverride
);
5067 EXPORT_SYMBOL(dbuf_free_range
);
5068 EXPORT_SYMBOL(dbuf_new_size
);
5069 EXPORT_SYMBOL(dbuf_release_bp
);
5070 EXPORT_SYMBOL(dbuf_dirty
);
5071 EXPORT_SYMBOL(dmu_buf_set_crypt_params
);
5072 EXPORT_SYMBOL(dmu_buf_will_dirty
);
5073 EXPORT_SYMBOL(dmu_buf_is_dirty
);
5074 EXPORT_SYMBOL(dmu_buf_will_not_fill
);
5075 EXPORT_SYMBOL(dmu_buf_will_fill
);
5076 EXPORT_SYMBOL(dmu_buf_fill_done
);
5077 EXPORT_SYMBOL(dmu_buf_rele
);
5078 EXPORT_SYMBOL(dbuf_assign_arcbuf
);
5079 EXPORT_SYMBOL(dbuf_prefetch
);
5080 EXPORT_SYMBOL(dbuf_hold_impl
);
5081 EXPORT_SYMBOL(dbuf_hold
);
5082 EXPORT_SYMBOL(dbuf_hold_level
);
5083 EXPORT_SYMBOL(dbuf_create_bonus
);
5084 EXPORT_SYMBOL(dbuf_spill_set_blksz
);
5085 EXPORT_SYMBOL(dbuf_rm_spill
);
5086 EXPORT_SYMBOL(dbuf_add_ref
);
5087 EXPORT_SYMBOL(dbuf_rele
);
5088 EXPORT_SYMBOL(dbuf_rele_and_unlock
);
5089 EXPORT_SYMBOL(dbuf_refcount
);
5090 EXPORT_SYMBOL(dbuf_sync_list
);
5091 EXPORT_SYMBOL(dmu_buf_set_user
);
5092 EXPORT_SYMBOL(dmu_buf_set_user_ie
);
5093 EXPORT_SYMBOL(dmu_buf_get_user
);
5094 EXPORT_SYMBOL(dmu_buf_get_blkptr
);
5097 ZFS_MODULE_PARAM(zfs_dbuf_cache
, dbuf_cache_
, max_bytes
, ULONG
, ZMOD_RW
,
5098 "Maximum size in bytes of the dbuf cache.");
5100 ZFS_MODULE_PARAM(zfs_dbuf_cache
, dbuf_cache_
, hiwater_pct
, UINT
, ZMOD_RW
,
5101 "Percentage over dbuf_cache_max_bytes when dbufs must be evicted "
5104 ZFS_MODULE_PARAM(zfs_dbuf_cache
, dbuf_cache_
, lowater_pct
, UINT
, ZMOD_RW
,
5105 "Percentage below dbuf_cache_max_bytes when the evict thread stops "
5108 ZFS_MODULE_PARAM(zfs_dbuf
, dbuf_
, metadata_cache_max_bytes
, ULONG
, ZMOD_RW
,
5109 "Maximum size in bytes of the dbuf metadata cache.");
5111 ZFS_MODULE_PARAM(zfs_dbuf
, dbuf_
, cache_shift
, INT
, ZMOD_RW
,
5112 "Set the size of the dbuf cache to a log2 fraction of arc size.");
5114 ZFS_MODULE_PARAM(zfs_dbuf
, dbuf_
, metadata_cache_shift
, INT
, ZMOD_RW
,
5115 "Set the size of the dbuf metadata cache to a log2 fraction of arc "