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>
59 typedef struct dbuf_stats
{
61 * Various statistics about the size of the dbuf cache.
63 kstat_named_t cache_count
;
64 kstat_named_t cache_size_bytes
;
65 kstat_named_t cache_size_bytes_max
;
67 * Statistics regarding the bounds on the dbuf cache size.
69 kstat_named_t cache_target_bytes
;
70 kstat_named_t cache_lowater_bytes
;
71 kstat_named_t cache_hiwater_bytes
;
73 * Total number of dbuf cache evictions that have occurred.
75 kstat_named_t cache_total_evicts
;
77 * The distribution of dbuf levels in the dbuf cache and
78 * the total size of all dbufs at each level.
80 kstat_named_t cache_levels
[DN_MAX_LEVELS
];
81 kstat_named_t cache_levels_bytes
[DN_MAX_LEVELS
];
83 * Statistics about the dbuf hash table.
85 kstat_named_t hash_hits
;
86 kstat_named_t hash_misses
;
87 kstat_named_t hash_collisions
;
88 kstat_named_t hash_elements
;
89 kstat_named_t hash_elements_max
;
91 * Number of sublists containing more than one dbuf in the dbuf
92 * hash table. Keep track of the longest hash chain.
94 kstat_named_t hash_chains
;
95 kstat_named_t hash_chain_max
;
97 * Number of times a dbuf_create() discovers that a dbuf was
98 * already created and in the dbuf hash table.
100 kstat_named_t hash_insert_race
;
102 * Statistics about the size of the metadata dbuf cache.
104 kstat_named_t metadata_cache_count
;
105 kstat_named_t metadata_cache_size_bytes
;
106 kstat_named_t metadata_cache_size_bytes_max
;
108 * For diagnostic purposes, this is incremented whenever we can't add
109 * something to the metadata cache because it's full, and instead put
110 * the data in the regular dbuf cache.
112 kstat_named_t metadata_cache_overflow
;
115 dbuf_stats_t dbuf_stats
= {
116 { "cache_count", KSTAT_DATA_UINT64
},
117 { "cache_size_bytes", KSTAT_DATA_UINT64
},
118 { "cache_size_bytes_max", KSTAT_DATA_UINT64
},
119 { "cache_target_bytes", KSTAT_DATA_UINT64
},
120 { "cache_lowater_bytes", KSTAT_DATA_UINT64
},
121 { "cache_hiwater_bytes", KSTAT_DATA_UINT64
},
122 { "cache_total_evicts", KSTAT_DATA_UINT64
},
123 { { "cache_levels_N", KSTAT_DATA_UINT64
} },
124 { { "cache_levels_bytes_N", KSTAT_DATA_UINT64
} },
125 { "hash_hits", KSTAT_DATA_UINT64
},
126 { "hash_misses", KSTAT_DATA_UINT64
},
127 { "hash_collisions", KSTAT_DATA_UINT64
},
128 { "hash_elements", KSTAT_DATA_UINT64
},
129 { "hash_elements_max", KSTAT_DATA_UINT64
},
130 { "hash_chains", KSTAT_DATA_UINT64
},
131 { "hash_chain_max", KSTAT_DATA_UINT64
},
132 { "hash_insert_race", KSTAT_DATA_UINT64
},
133 { "metadata_cache_count", KSTAT_DATA_UINT64
},
134 { "metadata_cache_size_bytes", KSTAT_DATA_UINT64
},
135 { "metadata_cache_size_bytes_max", KSTAT_DATA_UINT64
},
136 { "metadata_cache_overflow", KSTAT_DATA_UINT64
}
141 wmsum_t cache_total_evicts
;
142 wmsum_t cache_levels
[DN_MAX_LEVELS
];
143 wmsum_t cache_levels_bytes
[DN_MAX_LEVELS
];
146 wmsum_t hash_collisions
;
148 wmsum_t hash_insert_race
;
149 wmsum_t metadata_cache_count
;
150 wmsum_t metadata_cache_overflow
;
153 #define DBUF_STAT_INCR(stat, val) \
154 wmsum_add(&dbuf_sums.stat, val);
155 #define DBUF_STAT_DECR(stat, val) \
156 DBUF_STAT_INCR(stat, -(val));
157 #define DBUF_STAT_BUMP(stat) \
158 DBUF_STAT_INCR(stat, 1);
159 #define DBUF_STAT_BUMPDOWN(stat) \
160 DBUF_STAT_INCR(stat, -1);
161 #define DBUF_STAT_MAX(stat, v) { \
163 while ((v) > (_m = dbuf_stats.stat.value.ui64) && \
164 (_m != atomic_cas_64(&dbuf_stats.stat.value.ui64, _m, (v))))\
168 static boolean_t
dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
);
169 static void dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
);
170 static void dbuf_sync_leaf_verify_bonus_dnode(dbuf_dirty_record_t
*dr
);
171 static int dbuf_read_verify_dnode_crypt(dmu_buf_impl_t
*db
, uint32_t flags
);
173 extern inline void dmu_buf_init_user(dmu_buf_user_t
*dbu
,
174 dmu_buf_evict_func_t
*evict_func_sync
,
175 dmu_buf_evict_func_t
*evict_func_async
,
176 dmu_buf_t
**clear_on_evict_dbufp
);
179 * Global data structures and functions for the dbuf cache.
181 static kmem_cache_t
*dbuf_kmem_cache
;
182 static taskq_t
*dbu_evict_taskq
;
184 static kthread_t
*dbuf_cache_evict_thread
;
185 static kmutex_t dbuf_evict_lock
;
186 static kcondvar_t dbuf_evict_cv
;
187 static boolean_t dbuf_evict_thread_exit
;
190 * There are two dbuf caches; each dbuf can only be in one of them at a time.
192 * 1. Cache of metadata dbufs, to help make read-heavy administrative commands
193 * from /sbin/zfs run faster. The "metadata cache" specifically stores dbufs
194 * that represent the metadata that describes filesystems/snapshots/
195 * bookmarks/properties/etc. We only evict from this cache when we export a
196 * pool, to short-circuit as much I/O as possible for all administrative
197 * commands that need the metadata. There is no eviction policy for this
198 * cache, because we try to only include types in it which would occupy a
199 * very small amount of space per object but create a large impact on the
200 * performance of these commands. Instead, after it reaches a maximum size
201 * (which should only happen on very small memory systems with a very large
202 * number of filesystem objects), we stop taking new dbufs into the
203 * metadata cache, instead putting them in the normal dbuf cache.
205 * 2. LRU cache of dbufs. The dbuf cache maintains a list of dbufs that
206 * are not currently held but have been recently released. These dbufs
207 * are not eligible for arc eviction until they are aged out of the cache.
208 * Dbufs that are aged out of the cache will be immediately destroyed and
209 * become eligible for arc eviction.
211 * Dbufs are added to these caches once the last hold is released. If a dbuf is
212 * later accessed and still exists in the dbuf cache, then it will be removed
213 * from the cache and later re-added to the head of the cache.
215 * If a given dbuf meets the requirements for the metadata cache, it will go
216 * there, otherwise it will be considered for the generic LRU dbuf cache. The
217 * caches and the refcounts tracking their sizes are stored in an array indexed
218 * by those caches' matching enum values (from dbuf_cached_state_t).
220 typedef struct dbuf_cache
{
222 zfs_refcount_t size ____cacheline_aligned
;
224 dbuf_cache_t dbuf_caches
[DB_CACHE_MAX
];
226 /* Size limits for the caches */
227 unsigned long dbuf_cache_max_bytes
= ULONG_MAX
;
228 unsigned long dbuf_metadata_cache_max_bytes
= ULONG_MAX
;
230 /* Set the default sizes of the caches to log2 fraction of arc size */
231 int dbuf_cache_shift
= 5;
232 int dbuf_metadata_cache_shift
= 6;
234 static unsigned long dbuf_cache_target_bytes(void);
235 static unsigned long dbuf_metadata_cache_target_bytes(void);
238 * The LRU dbuf cache uses a three-stage eviction policy:
239 * - A low water marker designates when the dbuf eviction thread
240 * should stop evicting from the dbuf cache.
241 * - When we reach the maximum size (aka mid water mark), we
242 * signal the eviction thread to run.
243 * - The high water mark indicates when the eviction thread
244 * is unable to keep up with the incoming load and eviction must
245 * happen in the context of the calling thread.
249 * low water mid water hi water
250 * +----------------------------------------+----------+----------+
255 * +----------------------------------------+----------+----------+
257 * evicting eviction directly
260 * The high and low water marks indicate the operating range for the eviction
261 * thread. The low water mark is, by default, 90% of the total size of the
262 * cache and the high water mark is at 110% (both of these percentages can be
263 * changed by setting dbuf_cache_lowater_pct and dbuf_cache_hiwater_pct,
264 * respectively). The eviction thread will try to ensure that the cache remains
265 * within this range by waking up every second and checking if the cache is
266 * above the low water mark. The thread can also be woken up by callers adding
267 * elements into the cache if the cache is larger than the mid water (i.e max
268 * cache size). Once the eviction thread is woken up and eviction is required,
269 * it will continue evicting buffers until it's able to reduce the cache size
270 * to the low water mark. If the cache size continues to grow and hits the high
271 * water mark, then callers adding elements to the cache will begin to evict
272 * directly from the cache until the cache is no longer above the high water
277 * The percentage above and below the maximum cache size.
279 uint_t dbuf_cache_hiwater_pct
= 10;
280 uint_t dbuf_cache_lowater_pct
= 10;
284 dbuf_cons(void *vdb
, void *unused
, int kmflag
)
286 dmu_buf_impl_t
*db
= vdb
;
287 bzero(db
, sizeof (dmu_buf_impl_t
));
289 mutex_init(&db
->db_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
290 rw_init(&db
->db_rwlock
, NULL
, RW_DEFAULT
, NULL
);
291 cv_init(&db
->db_changed
, NULL
, CV_DEFAULT
, NULL
);
292 multilist_link_init(&db
->db_cache_link
);
293 zfs_refcount_create(&db
->db_holds
);
300 dbuf_dest(void *vdb
, void *unused
)
302 dmu_buf_impl_t
*db
= vdb
;
303 mutex_destroy(&db
->db_mtx
);
304 rw_destroy(&db
->db_rwlock
);
305 cv_destroy(&db
->db_changed
);
306 ASSERT(!multilist_link_active(&db
->db_cache_link
));
307 zfs_refcount_destroy(&db
->db_holds
);
311 * dbuf hash table routines
313 static dbuf_hash_table_t dbuf_hash_table
;
316 * We use Cityhash for this. It's fast, and has good hash properties without
317 * requiring any large static buffers.
320 dbuf_hash(void *os
, uint64_t obj
, uint8_t lvl
, uint64_t blkid
)
322 return (cityhash4((uintptr_t)os
, obj
, (uint64_t)lvl
, blkid
));
325 #define DTRACE_SET_STATE(db, why) \
326 DTRACE_PROBE2(dbuf__state_change, dmu_buf_impl_t *, db, \
329 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
330 ((dbuf)->db.db_object == (obj) && \
331 (dbuf)->db_objset == (os) && \
332 (dbuf)->db_level == (level) && \
333 (dbuf)->db_blkid == (blkid))
336 dbuf_find(objset_t
*os
, uint64_t obj
, uint8_t level
, uint64_t blkid
)
338 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
343 hv
= dbuf_hash(os
, obj
, level
, blkid
);
344 idx
= hv
& h
->hash_table_mask
;
346 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
347 for (db
= h
->hash_table
[idx
]; db
!= NULL
; db
= db
->db_hash_next
) {
348 if (DBUF_EQUAL(db
, os
, obj
, level
, blkid
)) {
349 mutex_enter(&db
->db_mtx
);
350 if (db
->db_state
!= DB_EVICTING
) {
351 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
354 mutex_exit(&db
->db_mtx
);
357 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
361 static dmu_buf_impl_t
*
362 dbuf_find_bonus(objset_t
*os
, uint64_t object
)
365 dmu_buf_impl_t
*db
= NULL
;
367 if (dnode_hold(os
, object
, FTAG
, &dn
) == 0) {
368 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
369 if (dn
->dn_bonus
!= NULL
) {
371 mutex_enter(&db
->db_mtx
);
373 rw_exit(&dn
->dn_struct_rwlock
);
374 dnode_rele(dn
, FTAG
);
380 * Insert an entry into the hash table. If there is already an element
381 * equal to elem in the hash table, then the already existing element
382 * will be returned and the new element will not be inserted.
383 * Otherwise returns NULL.
385 static dmu_buf_impl_t
*
386 dbuf_hash_insert(dmu_buf_impl_t
*db
)
388 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
389 objset_t
*os
= db
->db_objset
;
390 uint64_t obj
= db
->db
.db_object
;
391 int level
= db
->db_level
;
392 uint64_t blkid
, hv
, idx
;
396 blkid
= db
->db_blkid
;
397 hv
= dbuf_hash(os
, obj
, level
, blkid
);
398 idx
= hv
& h
->hash_table_mask
;
400 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
401 for (dbf
= h
->hash_table
[idx
], i
= 0; dbf
!= NULL
;
402 dbf
= dbf
->db_hash_next
, i
++) {
403 if (DBUF_EQUAL(dbf
, os
, obj
, level
, blkid
)) {
404 mutex_enter(&dbf
->db_mtx
);
405 if (dbf
->db_state
!= DB_EVICTING
) {
406 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
409 mutex_exit(&dbf
->db_mtx
);
414 DBUF_STAT_BUMP(hash_collisions
);
416 DBUF_STAT_BUMP(hash_chains
);
418 DBUF_STAT_MAX(hash_chain_max
, i
);
421 mutex_enter(&db
->db_mtx
);
422 db
->db_hash_next
= h
->hash_table
[idx
];
423 h
->hash_table
[idx
] = db
;
424 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
425 uint64_t he
= atomic_inc_64_nv(&dbuf_stats
.hash_elements
.value
.ui64
);
426 DBUF_STAT_MAX(hash_elements_max
, he
);
432 * This returns whether this dbuf should be stored in the metadata cache, which
433 * is based on whether it's from one of the dnode types that store data related
434 * to traversing dataset hierarchies.
437 dbuf_include_in_metadata_cache(dmu_buf_impl_t
*db
)
440 dmu_object_type_t type
= DB_DNODE(db
)->dn_type
;
443 /* Check if this dbuf is one of the types we care about */
444 if (DMU_OT_IS_METADATA_CACHED(type
)) {
445 /* If we hit this, then we set something up wrong in dmu_ot */
446 ASSERT(DMU_OT_IS_METADATA(type
));
449 * Sanity check for small-memory systems: don't allocate too
450 * much memory for this purpose.
452 if (zfs_refcount_count(
453 &dbuf_caches
[DB_DBUF_METADATA_CACHE
].size
) >
454 dbuf_metadata_cache_target_bytes()) {
455 DBUF_STAT_BUMP(metadata_cache_overflow
);
466 * Remove an entry from the hash table. It must be in the EVICTING state.
469 dbuf_hash_remove(dmu_buf_impl_t
*db
)
471 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
473 dmu_buf_impl_t
*dbf
, **dbp
;
475 hv
= dbuf_hash(db
->db_objset
, db
->db
.db_object
,
476 db
->db_level
, db
->db_blkid
);
477 idx
= hv
& h
->hash_table_mask
;
480 * We mustn't hold db_mtx to maintain lock ordering:
481 * DBUF_HASH_MUTEX > db_mtx.
483 ASSERT(zfs_refcount_is_zero(&db
->db_holds
));
484 ASSERT(db
->db_state
== DB_EVICTING
);
485 ASSERT(!MUTEX_HELD(&db
->db_mtx
));
487 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
488 dbp
= &h
->hash_table
[idx
];
489 while ((dbf
= *dbp
) != db
) {
490 dbp
= &dbf
->db_hash_next
;
493 *dbp
= db
->db_hash_next
;
494 db
->db_hash_next
= NULL
;
495 if (h
->hash_table
[idx
] &&
496 h
->hash_table
[idx
]->db_hash_next
== NULL
)
497 DBUF_STAT_BUMPDOWN(hash_chains
);
498 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
499 atomic_dec_64(&dbuf_stats
.hash_elements
.value
.ui64
);
505 } dbvu_verify_type_t
;
508 dbuf_verify_user(dmu_buf_impl_t
*db
, dbvu_verify_type_t verify_type
)
513 if (db
->db_user
== NULL
)
516 /* Only data blocks support the attachment of user data. */
517 ASSERT(db
->db_level
== 0);
519 /* Clients must resolve a dbuf before attaching user data. */
520 ASSERT(db
->db
.db_data
!= NULL
);
521 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
523 holds
= zfs_refcount_count(&db
->db_holds
);
524 if (verify_type
== DBVU_EVICTING
) {
526 * Immediate eviction occurs when holds == dirtycnt.
527 * For normal eviction buffers, holds is zero on
528 * eviction, except when dbuf_fix_old_data() calls
529 * dbuf_clear_data(). However, the hold count can grow
530 * during eviction even though db_mtx is held (see
531 * dmu_bonus_hold() for an example), so we can only
532 * test the generic invariant that holds >= dirtycnt.
534 ASSERT3U(holds
, >=, db
->db_dirtycnt
);
536 if (db
->db_user_immediate_evict
== TRUE
)
537 ASSERT3U(holds
, >=, db
->db_dirtycnt
);
539 ASSERT3U(holds
, >, 0);
545 dbuf_evict_user(dmu_buf_impl_t
*db
)
547 dmu_buf_user_t
*dbu
= db
->db_user
;
549 ASSERT(MUTEX_HELD(&db
->db_mtx
));
554 dbuf_verify_user(db
, DBVU_EVICTING
);
558 if (dbu
->dbu_clear_on_evict_dbufp
!= NULL
)
559 *dbu
->dbu_clear_on_evict_dbufp
= NULL
;
563 * There are two eviction callbacks - one that we call synchronously
564 * and one that we invoke via a taskq. The async one is useful for
565 * avoiding lock order reversals and limiting stack depth.
567 * Note that if we have a sync callback but no async callback,
568 * it's likely that the sync callback will free the structure
569 * containing the dbu. In that case we need to take care to not
570 * dereference dbu after calling the sync evict func.
572 boolean_t has_async
= (dbu
->dbu_evict_func_async
!= NULL
);
574 if (dbu
->dbu_evict_func_sync
!= NULL
)
575 dbu
->dbu_evict_func_sync(dbu
);
578 taskq_dispatch_ent(dbu_evict_taskq
, dbu
->dbu_evict_func_async
,
579 dbu
, 0, &dbu
->dbu_tqent
);
584 dbuf_is_metadata(dmu_buf_impl_t
*db
)
587 * Consider indirect blocks and spill blocks to be meta data.
589 if (db
->db_level
> 0 || db
->db_blkid
== DMU_SPILL_BLKID
) {
592 boolean_t is_metadata
;
595 is_metadata
= DMU_OT_IS_METADATA(DB_DNODE(db
)->dn_type
);
598 return (is_metadata
);
604 * This function *must* return indices evenly distributed between all
605 * sublists of the multilist. This is needed due to how the dbuf eviction
606 * code is laid out; dbuf_evict_thread() assumes dbufs are evenly
607 * distributed between all sublists and uses this assumption when
608 * deciding which sublist to evict from and how much to evict from it.
611 dbuf_cache_multilist_index_func(multilist_t
*ml
, void *obj
)
613 dmu_buf_impl_t
*db
= obj
;
616 * The assumption here, is the hash value for a given
617 * dmu_buf_impl_t will remain constant throughout it's lifetime
618 * (i.e. it's objset, object, level and blkid fields don't change).
619 * Thus, we don't need to store the dbuf's sublist index
620 * on insertion, as this index can be recalculated on removal.
622 * Also, the low order bits of the hash value are thought to be
623 * distributed evenly. Otherwise, in the case that the multilist
624 * has a power of two number of sublists, each sublists' usage
625 * would not be evenly distributed.
627 return (dbuf_hash(db
->db_objset
, db
->db
.db_object
,
628 db
->db_level
, db
->db_blkid
) %
629 multilist_get_num_sublists(ml
));
633 * The target size of the dbuf cache can grow with the ARC target,
634 * unless limited by the tunable dbuf_cache_max_bytes.
636 static inline unsigned long
637 dbuf_cache_target_bytes(void)
639 return (MIN(dbuf_cache_max_bytes
,
640 arc_target_bytes() >> dbuf_cache_shift
));
644 * The target size of the dbuf metadata cache can grow with the ARC target,
645 * unless limited by the tunable dbuf_metadata_cache_max_bytes.
647 static inline unsigned long
648 dbuf_metadata_cache_target_bytes(void)
650 return (MIN(dbuf_metadata_cache_max_bytes
,
651 arc_target_bytes() >> dbuf_metadata_cache_shift
));
654 static inline uint64_t
655 dbuf_cache_hiwater_bytes(void)
657 uint64_t dbuf_cache_target
= dbuf_cache_target_bytes();
658 return (dbuf_cache_target
+
659 (dbuf_cache_target
* dbuf_cache_hiwater_pct
) / 100);
662 static inline uint64_t
663 dbuf_cache_lowater_bytes(void)
665 uint64_t dbuf_cache_target
= dbuf_cache_target_bytes();
666 return (dbuf_cache_target
-
667 (dbuf_cache_target
* dbuf_cache_lowater_pct
) / 100);
670 static inline boolean_t
671 dbuf_cache_above_lowater(void)
673 return (zfs_refcount_count(&dbuf_caches
[DB_DBUF_CACHE
].size
) >
674 dbuf_cache_lowater_bytes());
678 * Evict the oldest eligible dbuf from the dbuf cache.
683 int idx
= multilist_get_random_index(&dbuf_caches
[DB_DBUF_CACHE
].cache
);
684 multilist_sublist_t
*mls
= multilist_sublist_lock(
685 &dbuf_caches
[DB_DBUF_CACHE
].cache
, idx
);
687 ASSERT(!MUTEX_HELD(&dbuf_evict_lock
));
689 dmu_buf_impl_t
*db
= multilist_sublist_tail(mls
);
690 while (db
!= NULL
&& mutex_tryenter(&db
->db_mtx
) == 0) {
691 db
= multilist_sublist_prev(mls
, db
);
694 DTRACE_PROBE2(dbuf__evict__one
, dmu_buf_impl_t
*, db
,
695 multilist_sublist_t
*, mls
);
698 multilist_sublist_remove(mls
, db
);
699 multilist_sublist_unlock(mls
);
700 (void) zfs_refcount_remove_many(
701 &dbuf_caches
[DB_DBUF_CACHE
].size
, db
->db
.db_size
, db
);
702 DBUF_STAT_BUMPDOWN(cache_levels
[db
->db_level
]);
703 DBUF_STAT_BUMPDOWN(cache_count
);
704 DBUF_STAT_DECR(cache_levels_bytes
[db
->db_level
],
706 ASSERT3U(db
->db_caching_status
, ==, DB_DBUF_CACHE
);
707 db
->db_caching_status
= DB_NO_CACHE
;
709 DBUF_STAT_BUMP(cache_total_evicts
);
711 multilist_sublist_unlock(mls
);
716 * The dbuf evict thread is responsible for aging out dbufs from the
717 * cache. Once the cache has reached it's maximum size, dbufs are removed
718 * and destroyed. The eviction thread will continue running until the size
719 * of the dbuf cache is at or below the maximum size. Once the dbuf is aged
720 * out of the cache it is destroyed and becomes eligible for arc eviction.
724 dbuf_evict_thread(void *unused
)
728 CALLB_CPR_INIT(&cpr
, &dbuf_evict_lock
, callb_generic_cpr
, FTAG
);
730 mutex_enter(&dbuf_evict_lock
);
731 while (!dbuf_evict_thread_exit
) {
732 while (!dbuf_cache_above_lowater() && !dbuf_evict_thread_exit
) {
733 CALLB_CPR_SAFE_BEGIN(&cpr
);
734 (void) cv_timedwait_idle_hires(&dbuf_evict_cv
,
735 &dbuf_evict_lock
, SEC2NSEC(1), MSEC2NSEC(1), 0);
736 CALLB_CPR_SAFE_END(&cpr
, &dbuf_evict_lock
);
738 mutex_exit(&dbuf_evict_lock
);
741 * Keep evicting as long as we're above the low water mark
742 * for the cache. We do this without holding the locks to
743 * minimize lock contention.
745 while (dbuf_cache_above_lowater() && !dbuf_evict_thread_exit
) {
749 mutex_enter(&dbuf_evict_lock
);
752 dbuf_evict_thread_exit
= B_FALSE
;
753 cv_broadcast(&dbuf_evict_cv
);
754 CALLB_CPR_EXIT(&cpr
); /* drops dbuf_evict_lock */
759 * Wake up the dbuf eviction thread if the dbuf cache is at its max size.
760 * If the dbuf cache is at its high water mark, then evict a dbuf from the
761 * dbuf cache using the callers context.
764 dbuf_evict_notify(uint64_t size
)
767 * We check if we should evict without holding the dbuf_evict_lock,
768 * because it's OK to occasionally make the wrong decision here,
769 * and grabbing the lock results in massive lock contention.
771 if (size
> dbuf_cache_target_bytes()) {
772 if (size
> dbuf_cache_hiwater_bytes())
774 cv_signal(&dbuf_evict_cv
);
779 dbuf_kstat_update(kstat_t
*ksp
, int rw
)
781 dbuf_stats_t
*ds
= ksp
->ks_data
;
783 if (rw
== KSTAT_WRITE
)
784 return (SET_ERROR(EACCES
));
786 ds
->cache_count
.value
.ui64
=
787 wmsum_value(&dbuf_sums
.cache_count
);
788 ds
->cache_size_bytes
.value
.ui64
=
789 zfs_refcount_count(&dbuf_caches
[DB_DBUF_CACHE
].size
);
790 ds
->cache_target_bytes
.value
.ui64
= dbuf_cache_target_bytes();
791 ds
->cache_hiwater_bytes
.value
.ui64
= dbuf_cache_hiwater_bytes();
792 ds
->cache_lowater_bytes
.value
.ui64
= dbuf_cache_lowater_bytes();
793 ds
->cache_total_evicts
.value
.ui64
=
794 wmsum_value(&dbuf_sums
.cache_total_evicts
);
795 for (int i
= 0; i
< DN_MAX_LEVELS
; i
++) {
796 ds
->cache_levels
[i
].value
.ui64
=
797 wmsum_value(&dbuf_sums
.cache_levels
[i
]);
798 ds
->cache_levels_bytes
[i
].value
.ui64
=
799 wmsum_value(&dbuf_sums
.cache_levels_bytes
[i
]);
801 ds
->hash_hits
.value
.ui64
=
802 wmsum_value(&dbuf_sums
.hash_hits
);
803 ds
->hash_misses
.value
.ui64
=
804 wmsum_value(&dbuf_sums
.hash_misses
);
805 ds
->hash_collisions
.value
.ui64
=
806 wmsum_value(&dbuf_sums
.hash_collisions
);
807 ds
->hash_chains
.value
.ui64
=
808 wmsum_value(&dbuf_sums
.hash_chains
);
809 ds
->hash_insert_race
.value
.ui64
=
810 wmsum_value(&dbuf_sums
.hash_insert_race
);
811 ds
->metadata_cache_count
.value
.ui64
=
812 wmsum_value(&dbuf_sums
.metadata_cache_count
);
813 ds
->metadata_cache_size_bytes
.value
.ui64
= zfs_refcount_count(
814 &dbuf_caches
[DB_DBUF_METADATA_CACHE
].size
);
815 ds
->metadata_cache_overflow
.value
.ui64
=
816 wmsum_value(&dbuf_sums
.metadata_cache_overflow
);
823 uint64_t hsize
= 1ULL << 16;
824 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
828 * The hash table is big enough to fill all of physical memory
829 * with an average block size of zfs_arc_average_blocksize (default 8K).
830 * By default, the table will take up
831 * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers).
833 while (hsize
* zfs_arc_average_blocksize
< physmem
* PAGESIZE
)
837 h
->hash_table_mask
= hsize
- 1;
840 * Large allocations which do not require contiguous pages
841 * should be using vmem_alloc() in the linux kernel
843 h
->hash_table
= vmem_zalloc(hsize
* sizeof (void *), KM_SLEEP
);
845 h
->hash_table
= kmem_zalloc(hsize
* sizeof (void *), KM_NOSLEEP
);
847 if (h
->hash_table
== NULL
) {
848 /* XXX - we should really return an error instead of assert */
849 ASSERT(hsize
> (1ULL << 10));
854 dbuf_kmem_cache
= kmem_cache_create("dmu_buf_impl_t",
855 sizeof (dmu_buf_impl_t
),
856 0, dbuf_cons
, dbuf_dest
, NULL
, NULL
, NULL
, 0);
858 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
859 mutex_init(&h
->hash_mutexes
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
864 * All entries are queued via taskq_dispatch_ent(), so min/maxalloc
865 * configuration is not required.
867 dbu_evict_taskq
= taskq_create("dbu_evict", 1, defclsyspri
, 0, 0, 0);
869 for (dbuf_cached_state_t dcs
= 0; dcs
< DB_CACHE_MAX
; dcs
++) {
870 multilist_create(&dbuf_caches
[dcs
].cache
,
871 sizeof (dmu_buf_impl_t
),
872 offsetof(dmu_buf_impl_t
, db_cache_link
),
873 dbuf_cache_multilist_index_func
);
874 zfs_refcount_create(&dbuf_caches
[dcs
].size
);
877 dbuf_evict_thread_exit
= B_FALSE
;
878 mutex_init(&dbuf_evict_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
879 cv_init(&dbuf_evict_cv
, NULL
, CV_DEFAULT
, NULL
);
880 dbuf_cache_evict_thread
= thread_create(NULL
, 0, dbuf_evict_thread
,
881 NULL
, 0, &p0
, TS_RUN
, minclsyspri
);
883 wmsum_init(&dbuf_sums
.cache_count
, 0);
884 wmsum_init(&dbuf_sums
.cache_total_evicts
, 0);
885 for (i
= 0; i
< DN_MAX_LEVELS
; i
++) {
886 wmsum_init(&dbuf_sums
.cache_levels
[i
], 0);
887 wmsum_init(&dbuf_sums
.cache_levels_bytes
[i
], 0);
889 wmsum_init(&dbuf_sums
.hash_hits
, 0);
890 wmsum_init(&dbuf_sums
.hash_misses
, 0);
891 wmsum_init(&dbuf_sums
.hash_collisions
, 0);
892 wmsum_init(&dbuf_sums
.hash_chains
, 0);
893 wmsum_init(&dbuf_sums
.hash_insert_race
, 0);
894 wmsum_init(&dbuf_sums
.metadata_cache_count
, 0);
895 wmsum_init(&dbuf_sums
.metadata_cache_overflow
, 0);
897 dbuf_ksp
= kstat_create("zfs", 0, "dbufstats", "misc",
898 KSTAT_TYPE_NAMED
, sizeof (dbuf_stats
) / sizeof (kstat_named_t
),
900 if (dbuf_ksp
!= NULL
) {
901 for (i
= 0; i
< DN_MAX_LEVELS
; i
++) {
902 snprintf(dbuf_stats
.cache_levels
[i
].name
,
903 KSTAT_STRLEN
, "cache_level_%d", i
);
904 dbuf_stats
.cache_levels
[i
].data_type
=
906 snprintf(dbuf_stats
.cache_levels_bytes
[i
].name
,
907 KSTAT_STRLEN
, "cache_level_%d_bytes", i
);
908 dbuf_stats
.cache_levels_bytes
[i
].data_type
=
911 dbuf_ksp
->ks_data
= &dbuf_stats
;
912 dbuf_ksp
->ks_update
= dbuf_kstat_update
;
913 kstat_install(dbuf_ksp
);
920 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
923 dbuf_stats_destroy();
925 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
926 mutex_destroy(&h
->hash_mutexes
[i
]);
929 * Large allocations which do not require contiguous pages
930 * should be using vmem_free() in the linux kernel
932 vmem_free(h
->hash_table
, (h
->hash_table_mask
+ 1) * sizeof (void *));
934 kmem_free(h
->hash_table
, (h
->hash_table_mask
+ 1) * sizeof (void *));
936 kmem_cache_destroy(dbuf_kmem_cache
);
937 taskq_destroy(dbu_evict_taskq
);
939 mutex_enter(&dbuf_evict_lock
);
940 dbuf_evict_thread_exit
= B_TRUE
;
941 while (dbuf_evict_thread_exit
) {
942 cv_signal(&dbuf_evict_cv
);
943 cv_wait(&dbuf_evict_cv
, &dbuf_evict_lock
);
945 mutex_exit(&dbuf_evict_lock
);
947 mutex_destroy(&dbuf_evict_lock
);
948 cv_destroy(&dbuf_evict_cv
);
950 for (dbuf_cached_state_t dcs
= 0; dcs
< DB_CACHE_MAX
; dcs
++) {
951 zfs_refcount_destroy(&dbuf_caches
[dcs
].size
);
952 multilist_destroy(&dbuf_caches
[dcs
].cache
);
955 if (dbuf_ksp
!= NULL
) {
956 kstat_delete(dbuf_ksp
);
960 wmsum_fini(&dbuf_sums
.cache_count
);
961 wmsum_fini(&dbuf_sums
.cache_total_evicts
);
962 for (i
= 0; i
< DN_MAX_LEVELS
; i
++) {
963 wmsum_fini(&dbuf_sums
.cache_levels
[i
]);
964 wmsum_fini(&dbuf_sums
.cache_levels_bytes
[i
]);
966 wmsum_fini(&dbuf_sums
.hash_hits
);
967 wmsum_fini(&dbuf_sums
.hash_misses
);
968 wmsum_fini(&dbuf_sums
.hash_collisions
);
969 wmsum_fini(&dbuf_sums
.hash_chains
);
970 wmsum_fini(&dbuf_sums
.hash_insert_race
);
971 wmsum_fini(&dbuf_sums
.metadata_cache_count
);
972 wmsum_fini(&dbuf_sums
.metadata_cache_overflow
);
981 dbuf_verify(dmu_buf_impl_t
*db
)
984 dbuf_dirty_record_t
*dr
;
987 ASSERT(MUTEX_HELD(&db
->db_mtx
));
989 if (!(zfs_flags
& ZFS_DEBUG_DBUF_VERIFY
))
992 ASSERT(db
->db_objset
!= NULL
);
996 ASSERT(db
->db_parent
== NULL
);
997 ASSERT(db
->db_blkptr
== NULL
);
999 ASSERT3U(db
->db
.db_object
, ==, dn
->dn_object
);
1000 ASSERT3P(db
->db_objset
, ==, dn
->dn_objset
);
1001 ASSERT3U(db
->db_level
, <, dn
->dn_nlevels
);
1002 ASSERT(db
->db_blkid
== DMU_BONUS_BLKID
||
1003 db
->db_blkid
== DMU_SPILL_BLKID
||
1004 !avl_is_empty(&dn
->dn_dbufs
));
1006 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1008 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
1009 ASSERT3U(db
->db
.db_offset
, ==, DMU_BONUS_BLKID
);
1010 } else if (db
->db_blkid
== DMU_SPILL_BLKID
) {
1012 ASSERT0(db
->db
.db_offset
);
1014 ASSERT3U(db
->db
.db_offset
, ==, db
->db_blkid
* db
->db
.db_size
);
1017 if ((dr
= list_head(&db
->db_dirty_records
)) != NULL
) {
1018 ASSERT(dr
->dr_dbuf
== db
);
1019 txg_prev
= dr
->dr_txg
;
1020 for (dr
= list_next(&db
->db_dirty_records
, dr
); dr
!= NULL
;
1021 dr
= list_next(&db
->db_dirty_records
, dr
)) {
1022 ASSERT(dr
->dr_dbuf
== db
);
1023 ASSERT(txg_prev
> dr
->dr_txg
);
1024 txg_prev
= dr
->dr_txg
;
1029 * We can't assert that db_size matches dn_datablksz because it
1030 * can be momentarily different when another thread is doing
1031 * dnode_set_blksz().
1033 if (db
->db_level
== 0 && db
->db
.db_object
== DMU_META_DNODE_OBJECT
) {
1034 dr
= db
->db_data_pending
;
1036 * It should only be modified in syncing context, so
1037 * make sure we only have one copy of the data.
1039 ASSERT(dr
== NULL
|| dr
->dt
.dl
.dr_data
== db
->db_buf
);
1042 /* verify db->db_blkptr */
1043 if (db
->db_blkptr
) {
1044 if (db
->db_parent
== dn
->dn_dbuf
) {
1045 /* db is pointed to by the dnode */
1046 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
1047 if (DMU_OBJECT_IS_SPECIAL(db
->db
.db_object
))
1048 ASSERT(db
->db_parent
== NULL
);
1050 ASSERT(db
->db_parent
!= NULL
);
1051 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
1052 ASSERT3P(db
->db_blkptr
, ==,
1053 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
1055 /* db is pointed to by an indirect block */
1056 int epb __maybe_unused
= db
->db_parent
->db
.db_size
>>
1058 ASSERT3U(db
->db_parent
->db_level
, ==, db
->db_level
+1);
1059 ASSERT3U(db
->db_parent
->db
.db_object
, ==,
1062 * dnode_grow_indblksz() can make this fail if we don't
1063 * have the parent's rwlock. XXX indblksz no longer
1064 * grows. safe to do this now?
1066 if (RW_LOCK_HELD(&db
->db_parent
->db_rwlock
)) {
1067 ASSERT3P(db
->db_blkptr
, ==,
1068 ((blkptr_t
*)db
->db_parent
->db
.db_data
+
1069 db
->db_blkid
% epb
));
1073 if ((db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
)) &&
1074 (db
->db_buf
== NULL
|| db
->db_buf
->b_data
) &&
1075 db
->db
.db_data
&& db
->db_blkid
!= DMU_BONUS_BLKID
&&
1076 db
->db_state
!= DB_FILL
&& !dn
->dn_free_txg
) {
1078 * If the blkptr isn't set but they have nonzero data,
1079 * it had better be dirty, otherwise we'll lose that
1080 * data when we evict this buffer.
1082 * There is an exception to this rule for indirect blocks; in
1083 * this case, if the indirect block is a hole, we fill in a few
1084 * fields on each of the child blocks (importantly, birth time)
1085 * to prevent hole birth times from being lost when you
1086 * partially fill in a hole.
1088 if (db
->db_dirtycnt
== 0) {
1089 if (db
->db_level
== 0) {
1090 uint64_t *buf
= db
->db
.db_data
;
1093 for (i
= 0; i
< db
->db
.db_size
>> 3; i
++) {
1094 ASSERT(buf
[i
] == 0);
1097 blkptr_t
*bps
= db
->db
.db_data
;
1098 ASSERT3U(1 << DB_DNODE(db
)->dn_indblkshift
, ==,
1101 * We want to verify that all the blkptrs in the
1102 * indirect block are holes, but we may have
1103 * automatically set up a few fields for them.
1104 * We iterate through each blkptr and verify
1105 * they only have those fields set.
1108 i
< db
->db
.db_size
/ sizeof (blkptr_t
);
1110 blkptr_t
*bp
= &bps
[i
];
1111 ASSERT(ZIO_CHECKSUM_IS_ZERO(
1114 DVA_IS_EMPTY(&bp
->blk_dva
[0]) &&
1115 DVA_IS_EMPTY(&bp
->blk_dva
[1]) &&
1116 DVA_IS_EMPTY(&bp
->blk_dva
[2]));
1117 ASSERT0(bp
->blk_fill
);
1118 ASSERT0(bp
->blk_pad
[0]);
1119 ASSERT0(bp
->blk_pad
[1]);
1120 ASSERT(!BP_IS_EMBEDDED(bp
));
1121 ASSERT(BP_IS_HOLE(bp
));
1122 ASSERT0(bp
->blk_phys_birth
);
1132 dbuf_clear_data(dmu_buf_impl_t
*db
)
1134 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1135 dbuf_evict_user(db
);
1136 ASSERT3P(db
->db_buf
, ==, NULL
);
1137 db
->db
.db_data
= NULL
;
1138 if (db
->db_state
!= DB_NOFILL
) {
1139 db
->db_state
= DB_UNCACHED
;
1140 DTRACE_SET_STATE(db
, "clear data");
1145 dbuf_set_data(dmu_buf_impl_t
*db
, arc_buf_t
*buf
)
1147 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1148 ASSERT(buf
!= NULL
);
1151 ASSERT(buf
->b_data
!= NULL
);
1152 db
->db
.db_data
= buf
->b_data
;
1156 dbuf_alloc_arcbuf(dmu_buf_impl_t
*db
)
1158 spa_t
*spa
= db
->db_objset
->os_spa
;
1160 return (arc_alloc_buf(spa
, db
, DBUF_GET_BUFC_TYPE(db
), db
->db
.db_size
));
1164 * Loan out an arc_buf for read. Return the loaned arc_buf.
1167 dbuf_loan_arcbuf(dmu_buf_impl_t
*db
)
1171 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1172 mutex_enter(&db
->db_mtx
);
1173 if (arc_released(db
->db_buf
) || zfs_refcount_count(&db
->db_holds
) > 1) {
1174 int blksz
= db
->db
.db_size
;
1175 spa_t
*spa
= db
->db_objset
->os_spa
;
1177 mutex_exit(&db
->db_mtx
);
1178 abuf
= arc_loan_buf(spa
, B_FALSE
, blksz
);
1179 bcopy(db
->db
.db_data
, abuf
->b_data
, blksz
);
1182 arc_loan_inuse_buf(abuf
, db
);
1184 dbuf_clear_data(db
);
1185 mutex_exit(&db
->db_mtx
);
1191 * Calculate which level n block references the data at the level 0 offset
1195 dbuf_whichblock(const dnode_t
*dn
, const int64_t level
, const uint64_t offset
)
1197 if (dn
->dn_datablkshift
!= 0 && dn
->dn_indblkshift
!= 0) {
1199 * The level n blkid is equal to the level 0 blkid divided by
1200 * the number of level 0s in a level n block.
1202 * The level 0 blkid is offset >> datablkshift =
1203 * offset / 2^datablkshift.
1205 * The number of level 0s in a level n is the number of block
1206 * pointers in an indirect block, raised to the power of level.
1207 * This is 2^(indblkshift - SPA_BLKPTRSHIFT)^level =
1208 * 2^(level*(indblkshift - SPA_BLKPTRSHIFT)).
1210 * Thus, the level n blkid is: offset /
1211 * ((2^datablkshift)*(2^(level*(indblkshift-SPA_BLKPTRSHIFT))))
1212 * = offset / 2^(datablkshift + level *
1213 * (indblkshift - SPA_BLKPTRSHIFT))
1214 * = offset >> (datablkshift + level *
1215 * (indblkshift - SPA_BLKPTRSHIFT))
1218 const unsigned exp
= dn
->dn_datablkshift
+
1219 level
* (dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
);
1221 if (exp
>= 8 * sizeof (offset
)) {
1222 /* This only happens on the highest indirection level */
1223 ASSERT3U(level
, ==, dn
->dn_nlevels
- 1);
1227 ASSERT3U(exp
, <, 8 * sizeof (offset
));
1229 return (offset
>> exp
);
1231 ASSERT3U(offset
, <, dn
->dn_datablksz
);
1237 * This function is used to lock the parent of the provided dbuf. This should be
1238 * used when modifying or reading db_blkptr.
1241 dmu_buf_lock_parent(dmu_buf_impl_t
*db
, krw_t rw
, void *tag
)
1243 enum db_lock_type ret
= DLT_NONE
;
1244 if (db
->db_parent
!= NULL
) {
1245 rw_enter(&db
->db_parent
->db_rwlock
, rw
);
1247 } else if (dmu_objset_ds(db
->db_objset
) != NULL
) {
1248 rrw_enter(&dmu_objset_ds(db
->db_objset
)->ds_bp_rwlock
, rw
,
1253 * We only return a DLT_NONE lock when it's the top-most indirect block
1254 * of the meta-dnode of the MOS.
1260 * We need to pass the lock type in because it's possible that the block will
1261 * move from being the topmost indirect block in a dnode (and thus, have no
1262 * parent) to not the top-most via an indirection increase. This would cause a
1263 * panic if we didn't pass the lock type in.
1266 dmu_buf_unlock_parent(dmu_buf_impl_t
*db
, db_lock_type_t type
, void *tag
)
1268 if (type
== DLT_PARENT
)
1269 rw_exit(&db
->db_parent
->db_rwlock
);
1270 else if (type
== DLT_OBJSET
)
1271 rrw_exit(&dmu_objset_ds(db
->db_objset
)->ds_bp_rwlock
, tag
);
1275 dbuf_read_done(zio_t
*zio
, const zbookmark_phys_t
*zb
, const blkptr_t
*bp
,
1276 arc_buf_t
*buf
, void *vdb
)
1278 dmu_buf_impl_t
*db
= vdb
;
1280 mutex_enter(&db
->db_mtx
);
1281 ASSERT3U(db
->db_state
, ==, DB_READ
);
1283 * All reads are synchronous, so we must have a hold on the dbuf
1285 ASSERT(zfs_refcount_count(&db
->db_holds
) > 0);
1286 ASSERT(db
->db_buf
== NULL
);
1287 ASSERT(db
->db
.db_data
== NULL
);
1290 ASSERT(zio
== NULL
|| zio
->io_error
!= 0);
1291 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1292 ASSERT3P(db
->db_buf
, ==, NULL
);
1293 db
->db_state
= DB_UNCACHED
;
1294 DTRACE_SET_STATE(db
, "i/o error");
1295 } else if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
1296 /* freed in flight */
1297 ASSERT(zio
== NULL
|| zio
->io_error
== 0);
1298 arc_release(buf
, db
);
1299 bzero(buf
->b_data
, db
->db
.db_size
);
1300 arc_buf_freeze(buf
);
1301 db
->db_freed_in_flight
= FALSE
;
1302 dbuf_set_data(db
, buf
);
1303 db
->db_state
= DB_CACHED
;
1304 DTRACE_SET_STATE(db
, "freed in flight");
1307 ASSERT(zio
== NULL
|| zio
->io_error
== 0);
1308 dbuf_set_data(db
, buf
);
1309 db
->db_state
= DB_CACHED
;
1310 DTRACE_SET_STATE(db
, "successful read");
1312 cv_broadcast(&db
->db_changed
);
1313 dbuf_rele_and_unlock(db
, NULL
, B_FALSE
);
1317 * Shortcut for performing reads on bonus dbufs. Returns
1318 * an error if we fail to verify the dnode associated with
1319 * a decrypted block. Otherwise success.
1322 dbuf_read_bonus(dmu_buf_impl_t
*db
, dnode_t
*dn
, uint32_t flags
)
1324 int bonuslen
, max_bonuslen
, err
;
1326 err
= dbuf_read_verify_dnode_crypt(db
, flags
);
1330 bonuslen
= MIN(dn
->dn_bonuslen
, dn
->dn_phys
->dn_bonuslen
);
1331 max_bonuslen
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
);
1332 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1333 ASSERT(DB_DNODE_HELD(db
));
1334 ASSERT3U(bonuslen
, <=, db
->db
.db_size
);
1335 db
->db
.db_data
= kmem_alloc(max_bonuslen
, KM_SLEEP
);
1336 arc_space_consume(max_bonuslen
, ARC_SPACE_BONUS
);
1337 if (bonuslen
< max_bonuslen
)
1338 bzero(db
->db
.db_data
, max_bonuslen
);
1340 bcopy(DN_BONUS(dn
->dn_phys
), db
->db
.db_data
, bonuslen
);
1341 db
->db_state
= DB_CACHED
;
1342 DTRACE_SET_STATE(db
, "bonus buffer filled");
1347 dbuf_handle_indirect_hole(dmu_buf_impl_t
*db
, dnode_t
*dn
)
1349 blkptr_t
*bps
= db
->db
.db_data
;
1350 uint32_t indbs
= 1ULL << dn
->dn_indblkshift
;
1351 int n_bps
= indbs
>> SPA_BLKPTRSHIFT
;
1353 for (int i
= 0; i
< n_bps
; i
++) {
1354 blkptr_t
*bp
= &bps
[i
];
1356 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==, indbs
);
1357 BP_SET_LSIZE(bp
, BP_GET_LEVEL(db
->db_blkptr
) == 1 ?
1358 dn
->dn_datablksz
: BP_GET_LSIZE(db
->db_blkptr
));
1359 BP_SET_TYPE(bp
, BP_GET_TYPE(db
->db_blkptr
));
1360 BP_SET_LEVEL(bp
, BP_GET_LEVEL(db
->db_blkptr
) - 1);
1361 BP_SET_BIRTH(bp
, db
->db_blkptr
->blk_birth
, 0);
1366 * Handle reads on dbufs that are holes, if necessary. This function
1367 * requires that the dbuf's mutex is held. Returns success (0) if action
1368 * was taken, ENOENT if no action was taken.
1371 dbuf_read_hole(dmu_buf_impl_t
*db
, dnode_t
*dn
, uint32_t flags
)
1373 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1375 int is_hole
= db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
);
1377 * For level 0 blocks only, if the above check fails:
1378 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
1379 * processes the delete record and clears the bp while we are waiting
1380 * for the dn_mtx (resulting in a "no" from block_freed).
1382 if (!is_hole
&& db
->db_level
== 0) {
1383 is_hole
= dnode_block_freed(dn
, db
->db_blkid
) ||
1384 BP_IS_HOLE(db
->db_blkptr
);
1388 dbuf_set_data(db
, dbuf_alloc_arcbuf(db
));
1389 bzero(db
->db
.db_data
, db
->db
.db_size
);
1391 if (db
->db_blkptr
!= NULL
&& db
->db_level
> 0 &&
1392 BP_IS_HOLE(db
->db_blkptr
) &&
1393 db
->db_blkptr
->blk_birth
!= 0) {
1394 dbuf_handle_indirect_hole(db
, dn
);
1396 db
->db_state
= DB_CACHED
;
1397 DTRACE_SET_STATE(db
, "hole read satisfied");
1404 * This function ensures that, when doing a decrypting read of a block,
1405 * we make sure we have decrypted the dnode associated with it. We must do
1406 * this so that we ensure we are fully authenticating the checksum-of-MACs
1407 * tree from the root of the objset down to this block. Indirect blocks are
1408 * always verified against their secure checksum-of-MACs assuming that the
1409 * dnode containing them is correct. Now that we are doing a decrypting read,
1410 * we can be sure that the key is loaded and verify that assumption. This is
1411 * especially important considering that we always read encrypted dnode
1412 * blocks as raw data (without verifying their MACs) to start, and
1413 * decrypt / authenticate them when we need to read an encrypted bonus buffer.
1416 dbuf_read_verify_dnode_crypt(dmu_buf_impl_t
*db
, uint32_t flags
)
1419 objset_t
*os
= db
->db_objset
;
1420 arc_buf_t
*dnode_abuf
;
1422 zbookmark_phys_t zb
;
1424 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1426 if (!os
->os_encrypted
|| os
->os_raw_receive
||
1427 (flags
& DB_RF_NO_DECRYPT
) != 0)
1432 dnode_abuf
= (dn
->dn_dbuf
!= NULL
) ? dn
->dn_dbuf
->db_buf
: NULL
;
1434 if (dnode_abuf
== NULL
|| !arc_is_encrypted(dnode_abuf
)) {
1439 SET_BOOKMARK(&zb
, dmu_objset_id(os
),
1440 DMU_META_DNODE_OBJECT
, 0, dn
->dn_dbuf
->db_blkid
);
1441 err
= arc_untransform(dnode_abuf
, os
->os_spa
, &zb
, B_TRUE
);
1444 * An error code of EACCES tells us that the key is still not
1445 * available. This is ok if we are only reading authenticated
1446 * (and therefore non-encrypted) blocks.
1448 if (err
== EACCES
&& ((db
->db_blkid
!= DMU_BONUS_BLKID
&&
1449 !DMU_OT_IS_ENCRYPTED(dn
->dn_type
)) ||
1450 (db
->db_blkid
== DMU_BONUS_BLKID
&&
1451 !DMU_OT_IS_ENCRYPTED(dn
->dn_bonustype
))))
1460 * Drops db_mtx and the parent lock specified by dblt and tag before
1464 dbuf_read_impl(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t flags
,
1465 db_lock_type_t dblt
, void *tag
)
1468 zbookmark_phys_t zb
;
1469 uint32_t aflags
= ARC_FLAG_NOWAIT
;
1472 err
= zio_flags
= 0;
1475 ASSERT(!zfs_refcount_is_zero(&db
->db_holds
));
1476 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1477 ASSERT(db
->db_state
== DB_UNCACHED
);
1478 ASSERT(db
->db_buf
== NULL
);
1479 ASSERT(db
->db_parent
== NULL
||
1480 RW_LOCK_HELD(&db
->db_parent
->db_rwlock
));
1482 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1483 err
= dbuf_read_bonus(db
, dn
, flags
);
1487 err
= dbuf_read_hole(db
, dn
, flags
);
1492 * Any attempt to read a redacted block should result in an error. This
1493 * will never happen under normal conditions, but can be useful for
1494 * debugging purposes.
1496 if (BP_IS_REDACTED(db
->db_blkptr
)) {
1497 ASSERT(dsl_dataset_feature_is_active(
1498 db
->db_objset
->os_dsl_dataset
,
1499 SPA_FEATURE_REDACTED_DATASETS
));
1500 err
= SET_ERROR(EIO
);
1504 SET_BOOKMARK(&zb
, dmu_objset_id(db
->db_objset
),
1505 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
1508 * All bps of an encrypted os should have the encryption bit set.
1509 * If this is not true it indicates tampering and we report an error.
1511 if (db
->db_objset
->os_encrypted
&& !BP_USES_CRYPT(db
->db_blkptr
)) {
1512 spa_log_error(db
->db_objset
->os_spa
, &zb
);
1513 zfs_panic_recover("unencrypted block in encrypted "
1514 "object set %llu", dmu_objset_id(db
->db_objset
));
1515 err
= SET_ERROR(EIO
);
1519 err
= dbuf_read_verify_dnode_crypt(db
, flags
);
1525 db
->db_state
= DB_READ
;
1526 DTRACE_SET_STATE(db
, "read issued");
1527 mutex_exit(&db
->db_mtx
);
1529 if (DBUF_IS_L2CACHEABLE(db
))
1530 aflags
|= ARC_FLAG_L2CACHE
;
1532 dbuf_add_ref(db
, NULL
);
1534 zio_flags
= (flags
& DB_RF_CANFAIL
) ?
1535 ZIO_FLAG_CANFAIL
: ZIO_FLAG_MUSTSUCCEED
;
1537 if ((flags
& DB_RF_NO_DECRYPT
) && BP_IS_PROTECTED(db
->db_blkptr
))
1538 zio_flags
|= ZIO_FLAG_RAW
;
1540 * The zio layer will copy the provided blkptr later, but we need to
1541 * do this now so that we can release the parent's rwlock. We have to
1542 * do that now so that if dbuf_read_done is called synchronously (on
1543 * an l1 cache hit) we don't acquire the db_mtx while holding the
1544 * parent's rwlock, which would be a lock ordering violation.
1546 blkptr_t bp
= *db
->db_blkptr
;
1547 dmu_buf_unlock_parent(db
, dblt
, tag
);
1548 (void) arc_read(zio
, db
->db_objset
->os_spa
, &bp
,
1549 dbuf_read_done
, db
, ZIO_PRIORITY_SYNC_READ
, zio_flags
,
1554 mutex_exit(&db
->db_mtx
);
1555 dmu_buf_unlock_parent(db
, dblt
, tag
);
1560 * This is our just-in-time copy function. It makes a copy of buffers that
1561 * have been modified in a previous transaction group before we access them in
1562 * the current active group.
1564 * This function is used in three places: when we are dirtying a buffer for the
1565 * first time in a txg, when we are freeing a range in a dnode that includes
1566 * this buffer, and when we are accessing a buffer which was received compressed
1567 * and later referenced in a WRITE_BYREF record.
1569 * Note that when we are called from dbuf_free_range() we do not put a hold on
1570 * the buffer, we just traverse the active dbuf list for the dnode.
1573 dbuf_fix_old_data(dmu_buf_impl_t
*db
, uint64_t txg
)
1575 dbuf_dirty_record_t
*dr
= list_head(&db
->db_dirty_records
);
1577 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1578 ASSERT(db
->db
.db_data
!= NULL
);
1579 ASSERT(db
->db_level
== 0);
1580 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
);
1583 (dr
->dt
.dl
.dr_data
!=
1584 ((db
->db_blkid
== DMU_BONUS_BLKID
) ? db
->db
.db_data
: db
->db_buf
)))
1588 * If the last dirty record for this dbuf has not yet synced
1589 * and its referencing the dbuf data, either:
1590 * reset the reference to point to a new copy,
1591 * or (if there a no active holders)
1592 * just null out the current db_data pointer.
1594 ASSERT3U(dr
->dr_txg
, >=, txg
- 2);
1595 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1596 dnode_t
*dn
= DB_DNODE(db
);
1597 int bonuslen
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
);
1598 dr
->dt
.dl
.dr_data
= kmem_alloc(bonuslen
, KM_SLEEP
);
1599 arc_space_consume(bonuslen
, ARC_SPACE_BONUS
);
1600 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
, bonuslen
);
1601 } else if (zfs_refcount_count(&db
->db_holds
) > db
->db_dirtycnt
) {
1602 dnode_t
*dn
= DB_DNODE(db
);
1603 int size
= arc_buf_size(db
->db_buf
);
1604 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1605 spa_t
*spa
= db
->db_objset
->os_spa
;
1606 enum zio_compress compress_type
=
1607 arc_get_compression(db
->db_buf
);
1608 uint8_t complevel
= arc_get_complevel(db
->db_buf
);
1610 if (arc_is_encrypted(db
->db_buf
)) {
1611 boolean_t byteorder
;
1612 uint8_t salt
[ZIO_DATA_SALT_LEN
];
1613 uint8_t iv
[ZIO_DATA_IV_LEN
];
1614 uint8_t mac
[ZIO_DATA_MAC_LEN
];
1616 arc_get_raw_params(db
->db_buf
, &byteorder
, salt
,
1618 dr
->dt
.dl
.dr_data
= arc_alloc_raw_buf(spa
, db
,
1619 dmu_objset_id(dn
->dn_objset
), byteorder
, salt
, iv
,
1620 mac
, dn
->dn_type
, size
, arc_buf_lsize(db
->db_buf
),
1621 compress_type
, complevel
);
1622 } else if (compress_type
!= ZIO_COMPRESS_OFF
) {
1623 ASSERT3U(type
, ==, ARC_BUFC_DATA
);
1624 dr
->dt
.dl
.dr_data
= arc_alloc_compressed_buf(spa
, db
,
1625 size
, arc_buf_lsize(db
->db_buf
), compress_type
,
1628 dr
->dt
.dl
.dr_data
= arc_alloc_buf(spa
, db
, type
, size
);
1630 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
->b_data
, size
);
1633 dbuf_clear_data(db
);
1638 dbuf_read(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t flags
)
1645 * We don't have to hold the mutex to check db_state because it
1646 * can't be freed while we have a hold on the buffer.
1648 ASSERT(!zfs_refcount_is_zero(&db
->db_holds
));
1650 if (db
->db_state
== DB_NOFILL
)
1651 return (SET_ERROR(EIO
));
1656 prefetch
= db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1657 (flags
& DB_RF_NOPREFETCH
) == 0 && dn
!= NULL
&&
1658 DBUF_IS_CACHEABLE(db
);
1660 mutex_enter(&db
->db_mtx
);
1661 if (db
->db_state
== DB_CACHED
) {
1662 spa_t
*spa
= dn
->dn_objset
->os_spa
;
1665 * Ensure that this block's dnode has been decrypted if
1666 * the caller has requested decrypted data.
1668 err
= dbuf_read_verify_dnode_crypt(db
, flags
);
1671 * If the arc buf is compressed or encrypted and the caller
1672 * requested uncompressed data, we need to untransform it
1673 * before returning. We also call arc_untransform() on any
1674 * unauthenticated blocks, which will verify their MAC if
1675 * the key is now available.
1677 if (err
== 0 && db
->db_buf
!= NULL
&&
1678 (flags
& DB_RF_NO_DECRYPT
) == 0 &&
1679 (arc_is_encrypted(db
->db_buf
) ||
1680 arc_is_unauthenticated(db
->db_buf
) ||
1681 arc_get_compression(db
->db_buf
) != ZIO_COMPRESS_OFF
)) {
1682 zbookmark_phys_t zb
;
1684 SET_BOOKMARK(&zb
, dmu_objset_id(db
->db_objset
),
1685 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
1686 dbuf_fix_old_data(db
, spa_syncing_txg(spa
));
1687 err
= arc_untransform(db
->db_buf
, spa
, &zb
, B_FALSE
);
1688 dbuf_set_data(db
, db
->db_buf
);
1690 mutex_exit(&db
->db_mtx
);
1691 if (err
== 0 && prefetch
) {
1692 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
,
1693 B_FALSE
, flags
& DB_RF_HAVESTRUCT
);
1696 DBUF_STAT_BUMP(hash_hits
);
1697 } else if (db
->db_state
== DB_UNCACHED
) {
1698 spa_t
*spa
= dn
->dn_objset
->os_spa
;
1699 boolean_t need_wait
= B_FALSE
;
1701 db_lock_type_t dblt
= dmu_buf_lock_parent(db
, RW_READER
, FTAG
);
1704 db
->db_blkptr
!= NULL
&& !BP_IS_HOLE(db
->db_blkptr
)) {
1705 zio
= zio_root(spa
, NULL
, NULL
, ZIO_FLAG_CANFAIL
);
1708 err
= dbuf_read_impl(db
, zio
, flags
, dblt
, FTAG
);
1710 * dbuf_read_impl has dropped db_mtx and our parent's rwlock
1713 if (!err
&& prefetch
) {
1714 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
,
1715 db
->db_state
!= DB_CACHED
,
1716 flags
& DB_RF_HAVESTRUCT
);
1720 DBUF_STAT_BUMP(hash_misses
);
1723 * If we created a zio_root we must execute it to avoid
1724 * leaking it, even if it isn't attached to any work due
1725 * to an error in dbuf_read_impl().
1729 err
= zio_wait(zio
);
1731 VERIFY0(zio_wait(zio
));
1735 * Another reader came in while the dbuf was in flight
1736 * between UNCACHED and CACHED. Either a writer will finish
1737 * writing the buffer (sending the dbuf to CACHED) or the
1738 * first reader's request will reach the read_done callback
1739 * and send the dbuf to CACHED. Otherwise, a failure
1740 * occurred and the dbuf went to UNCACHED.
1742 mutex_exit(&db
->db_mtx
);
1744 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
,
1745 B_TRUE
, flags
& DB_RF_HAVESTRUCT
);
1748 DBUF_STAT_BUMP(hash_misses
);
1750 /* Skip the wait per the caller's request. */
1751 if ((flags
& DB_RF_NEVERWAIT
) == 0) {
1752 mutex_enter(&db
->db_mtx
);
1753 while (db
->db_state
== DB_READ
||
1754 db
->db_state
== DB_FILL
) {
1755 ASSERT(db
->db_state
== DB_READ
||
1756 (flags
& DB_RF_HAVESTRUCT
) == 0);
1757 DTRACE_PROBE2(blocked__read
, dmu_buf_impl_t
*,
1759 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1761 if (db
->db_state
== DB_UNCACHED
)
1762 err
= SET_ERROR(EIO
);
1763 mutex_exit(&db
->db_mtx
);
1771 dbuf_noread(dmu_buf_impl_t
*db
)
1773 ASSERT(!zfs_refcount_is_zero(&db
->db_holds
));
1774 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1775 mutex_enter(&db
->db_mtx
);
1776 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
1777 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1778 if (db
->db_state
== DB_UNCACHED
) {
1779 ASSERT(db
->db_buf
== NULL
);
1780 ASSERT(db
->db
.db_data
== NULL
);
1781 dbuf_set_data(db
, dbuf_alloc_arcbuf(db
));
1782 db
->db_state
= DB_FILL
;
1783 DTRACE_SET_STATE(db
, "assigning filled buffer");
1784 } else if (db
->db_state
== DB_NOFILL
) {
1785 dbuf_clear_data(db
);
1787 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
1789 mutex_exit(&db
->db_mtx
);
1793 dbuf_unoverride(dbuf_dirty_record_t
*dr
)
1795 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
1796 blkptr_t
*bp
= &dr
->dt
.dl
.dr_overridden_by
;
1797 uint64_t txg
= dr
->dr_txg
;
1799 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1801 * This assert is valid because dmu_sync() expects to be called by
1802 * a zilog's get_data while holding a range lock. This call only
1803 * comes from dbuf_dirty() callers who must also hold a range lock.
1805 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_IN_DMU_SYNC
);
1806 ASSERT(db
->db_level
== 0);
1808 if (db
->db_blkid
== DMU_BONUS_BLKID
||
1809 dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
)
1812 ASSERT(db
->db_data_pending
!= dr
);
1814 /* free this block */
1815 if (!BP_IS_HOLE(bp
) && !dr
->dt
.dl
.dr_nopwrite
)
1816 zio_free(db
->db_objset
->os_spa
, txg
, bp
);
1818 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
1819 dr
->dt
.dl
.dr_nopwrite
= B_FALSE
;
1820 dr
->dt
.dl
.dr_has_raw_params
= B_FALSE
;
1823 * Release the already-written buffer, so we leave it in
1824 * a consistent dirty state. Note that all callers are
1825 * modifying the buffer, so they will immediately do
1826 * another (redundant) arc_release(). Therefore, leave
1827 * the buf thawed to save the effort of freezing &
1828 * immediately re-thawing it.
1830 arc_release(dr
->dt
.dl
.dr_data
, db
);
1834 * Evict (if its unreferenced) or clear (if its referenced) any level-0
1835 * data blocks in the free range, so that any future readers will find
1839 dbuf_free_range(dnode_t
*dn
, uint64_t start_blkid
, uint64_t end_blkid
,
1842 dmu_buf_impl_t
*db_search
;
1843 dmu_buf_impl_t
*db
, *db_next
;
1844 uint64_t txg
= tx
->tx_txg
;
1846 dbuf_dirty_record_t
*dr
;
1848 if (end_blkid
> dn
->dn_maxblkid
&&
1849 !(start_blkid
== DMU_SPILL_BLKID
|| end_blkid
== DMU_SPILL_BLKID
))
1850 end_blkid
= dn
->dn_maxblkid
;
1851 dprintf_dnode(dn
, "start=%llu end=%llu\n", (u_longlong_t
)start_blkid
,
1852 (u_longlong_t
)end_blkid
);
1854 db_search
= kmem_alloc(sizeof (dmu_buf_impl_t
), KM_SLEEP
);
1855 db_search
->db_level
= 0;
1856 db_search
->db_blkid
= start_blkid
;
1857 db_search
->db_state
= DB_SEARCH
;
1859 mutex_enter(&dn
->dn_dbufs_mtx
);
1860 db
= avl_find(&dn
->dn_dbufs
, db_search
, &where
);
1861 ASSERT3P(db
, ==, NULL
);
1863 db
= avl_nearest(&dn
->dn_dbufs
, where
, AVL_AFTER
);
1865 for (; db
!= NULL
; db
= db_next
) {
1866 db_next
= AVL_NEXT(&dn
->dn_dbufs
, db
);
1867 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1869 if (db
->db_level
!= 0 || db
->db_blkid
> end_blkid
) {
1872 ASSERT3U(db
->db_blkid
, >=, start_blkid
);
1874 /* found a level 0 buffer in the range */
1875 mutex_enter(&db
->db_mtx
);
1876 if (dbuf_undirty(db
, tx
)) {
1877 /* mutex has been dropped and dbuf destroyed */
1881 if (db
->db_state
== DB_UNCACHED
||
1882 db
->db_state
== DB_NOFILL
||
1883 db
->db_state
== DB_EVICTING
) {
1884 ASSERT(db
->db
.db_data
== NULL
);
1885 mutex_exit(&db
->db_mtx
);
1888 if (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
) {
1889 /* will be handled in dbuf_read_done or dbuf_rele */
1890 db
->db_freed_in_flight
= TRUE
;
1891 mutex_exit(&db
->db_mtx
);
1894 if (zfs_refcount_count(&db
->db_holds
) == 0) {
1899 /* The dbuf is referenced */
1901 dr
= list_head(&db
->db_dirty_records
);
1903 if (dr
->dr_txg
== txg
) {
1905 * This buffer is "in-use", re-adjust the file
1906 * size to reflect that this buffer may
1907 * contain new data when we sync.
1909 if (db
->db_blkid
!= DMU_SPILL_BLKID
&&
1910 db
->db_blkid
> dn
->dn_maxblkid
)
1911 dn
->dn_maxblkid
= db
->db_blkid
;
1912 dbuf_unoverride(dr
);
1915 * This dbuf is not dirty in the open context.
1916 * Either uncache it (if its not referenced in
1917 * the open context) or reset its contents to
1920 dbuf_fix_old_data(db
, txg
);
1923 /* clear the contents if its cached */
1924 if (db
->db_state
== DB_CACHED
) {
1925 ASSERT(db
->db
.db_data
!= NULL
);
1926 arc_release(db
->db_buf
, db
);
1927 rw_enter(&db
->db_rwlock
, RW_WRITER
);
1928 bzero(db
->db
.db_data
, db
->db
.db_size
);
1929 rw_exit(&db
->db_rwlock
);
1930 arc_buf_freeze(db
->db_buf
);
1933 mutex_exit(&db
->db_mtx
);
1936 kmem_free(db_search
, sizeof (dmu_buf_impl_t
));
1937 mutex_exit(&dn
->dn_dbufs_mtx
);
1941 dbuf_new_size(dmu_buf_impl_t
*db
, int size
, dmu_tx_t
*tx
)
1943 arc_buf_t
*buf
, *old_buf
;
1944 dbuf_dirty_record_t
*dr
;
1945 int osize
= db
->db
.db_size
;
1946 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1949 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1955 * XXX we should be doing a dbuf_read, checking the return
1956 * value and returning that up to our callers
1958 dmu_buf_will_dirty(&db
->db
, tx
);
1960 /* create the data buffer for the new block */
1961 buf
= arc_alloc_buf(dn
->dn_objset
->os_spa
, db
, type
, size
);
1963 /* copy old block data to the new block */
1964 old_buf
= db
->db_buf
;
1965 bcopy(old_buf
->b_data
, buf
->b_data
, MIN(osize
, size
));
1966 /* zero the remainder */
1968 bzero((uint8_t *)buf
->b_data
+ osize
, size
- osize
);
1970 mutex_enter(&db
->db_mtx
);
1971 dbuf_set_data(db
, buf
);
1972 arc_buf_destroy(old_buf
, db
);
1973 db
->db
.db_size
= size
;
1975 dr
= list_head(&db
->db_dirty_records
);
1976 /* dirty record added by dmu_buf_will_dirty() */
1978 if (db
->db_level
== 0)
1979 dr
->dt
.dl
.dr_data
= buf
;
1980 ASSERT3U(dr
->dr_txg
, ==, tx
->tx_txg
);
1981 ASSERT3U(dr
->dr_accounted
, ==, osize
);
1982 dr
->dr_accounted
= size
;
1983 mutex_exit(&db
->db_mtx
);
1985 dmu_objset_willuse_space(dn
->dn_objset
, size
- osize
, tx
);
1990 dbuf_release_bp(dmu_buf_impl_t
*db
)
1992 objset_t
*os __maybe_unused
= db
->db_objset
;
1994 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os
)));
1995 ASSERT(arc_released(os
->os_phys_buf
) ||
1996 list_link_active(&os
->os_dsl_dataset
->ds_synced_link
));
1997 ASSERT(db
->db_parent
== NULL
|| arc_released(db
->db_parent
->db_buf
));
1999 (void) arc_release(db
->db_buf
, db
);
2003 * We already have a dirty record for this TXG, and we are being
2007 dbuf_redirty(dbuf_dirty_record_t
*dr
)
2009 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2011 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2013 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
) {
2015 * If this buffer has already been written out,
2016 * we now need to reset its state.
2018 dbuf_unoverride(dr
);
2019 if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
&&
2020 db
->db_state
!= DB_NOFILL
) {
2021 /* Already released on initial dirty, so just thaw. */
2022 ASSERT(arc_released(db
->db_buf
));
2023 arc_buf_thaw(db
->db_buf
);
2028 dbuf_dirty_record_t
*
2029 dbuf_dirty_lightweight(dnode_t
*dn
, uint64_t blkid
, dmu_tx_t
*tx
)
2031 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
2032 IMPLY(dn
->dn_objset
->os_raw_receive
, dn
->dn_maxblkid
>= blkid
);
2033 dnode_new_blkid(dn
, blkid
, tx
, B_TRUE
, B_FALSE
);
2034 ASSERT(dn
->dn_maxblkid
>= blkid
);
2036 dbuf_dirty_record_t
*dr
= kmem_zalloc(sizeof (*dr
), KM_SLEEP
);
2037 list_link_init(&dr
->dr_dirty_node
);
2038 list_link_init(&dr
->dr_dbuf_node
);
2040 dr
->dr_txg
= tx
->tx_txg
;
2041 dr
->dt
.dll
.dr_blkid
= blkid
;
2042 dr
->dr_accounted
= dn
->dn_datablksz
;
2045 * There should not be any dbuf for the block that we're dirtying.
2046 * Otherwise the buffer contents could be inconsistent between the
2047 * dbuf and the lightweight dirty record.
2049 ASSERT3P(NULL
, ==, dbuf_find(dn
->dn_objset
, dn
->dn_object
, 0, blkid
));
2051 mutex_enter(&dn
->dn_mtx
);
2052 int txgoff
= tx
->tx_txg
& TXG_MASK
;
2053 if (dn
->dn_free_ranges
[txgoff
] != NULL
) {
2054 range_tree_clear(dn
->dn_free_ranges
[txgoff
], blkid
, 1);
2057 if (dn
->dn_nlevels
== 1) {
2058 ASSERT3U(blkid
, <, dn
->dn_nblkptr
);
2059 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
2060 mutex_exit(&dn
->dn_mtx
);
2061 rw_exit(&dn
->dn_struct_rwlock
);
2062 dnode_setdirty(dn
, tx
);
2064 mutex_exit(&dn
->dn_mtx
);
2066 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2067 dmu_buf_impl_t
*parent_db
= dbuf_hold_level(dn
,
2068 1, blkid
>> epbs
, FTAG
);
2069 rw_exit(&dn
->dn_struct_rwlock
);
2070 if (parent_db
== NULL
) {
2071 kmem_free(dr
, sizeof (*dr
));
2074 int err
= dbuf_read(parent_db
, NULL
,
2075 (DB_RF_NOPREFETCH
| DB_RF_CANFAIL
));
2077 dbuf_rele(parent_db
, FTAG
);
2078 kmem_free(dr
, sizeof (*dr
));
2082 dbuf_dirty_record_t
*parent_dr
= dbuf_dirty(parent_db
, tx
);
2083 dbuf_rele(parent_db
, FTAG
);
2084 mutex_enter(&parent_dr
->dt
.di
.dr_mtx
);
2085 ASSERT3U(parent_dr
->dr_txg
, ==, tx
->tx_txg
);
2086 list_insert_tail(&parent_dr
->dt
.di
.dr_children
, dr
);
2087 mutex_exit(&parent_dr
->dt
.di
.dr_mtx
);
2088 dr
->dr_parent
= parent_dr
;
2091 dmu_objset_willuse_space(dn
->dn_objset
, dr
->dr_accounted
, tx
);
2096 dbuf_dirty_record_t
*
2097 dbuf_dirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
2101 dbuf_dirty_record_t
*dr
, *dr_next
, *dr_head
;
2102 int txgoff
= tx
->tx_txg
& TXG_MASK
;
2103 boolean_t drop_struct_rwlock
= B_FALSE
;
2105 ASSERT(tx
->tx_txg
!= 0);
2106 ASSERT(!zfs_refcount_is_zero(&db
->db_holds
));
2107 DMU_TX_DIRTY_BUF(tx
, db
);
2112 * Shouldn't dirty a regular buffer in syncing context. Private
2113 * objects may be dirtied in syncing context, but only if they
2114 * were already pre-dirtied in open context.
2117 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
) {
2118 rrw_enter(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
,
2121 ASSERT(!dmu_tx_is_syncing(tx
) ||
2122 BP_IS_HOLE(dn
->dn_objset
->os_rootbp
) ||
2123 DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
2124 dn
->dn_objset
->os_dsl_dataset
== NULL
);
2125 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
2126 rrw_exit(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
, FTAG
);
2129 * We make this assert for private objects as well, but after we
2130 * check if we're already dirty. They are allowed to re-dirty
2131 * in syncing context.
2133 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
2134 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
2135 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
2137 mutex_enter(&db
->db_mtx
);
2139 * XXX make this true for indirects too? The problem is that
2140 * transactions created with dmu_tx_create_assigned() from
2141 * syncing context don't bother holding ahead.
2143 ASSERT(db
->db_level
!= 0 ||
2144 db
->db_state
== DB_CACHED
|| db
->db_state
== DB_FILL
||
2145 db
->db_state
== DB_NOFILL
);
2147 mutex_enter(&dn
->dn_mtx
);
2148 dnode_set_dirtyctx(dn
, tx
, db
);
2149 if (tx
->tx_txg
> dn
->dn_dirty_txg
)
2150 dn
->dn_dirty_txg
= tx
->tx_txg
;
2151 mutex_exit(&dn
->dn_mtx
);
2153 if (db
->db_blkid
== DMU_SPILL_BLKID
)
2154 dn
->dn_have_spill
= B_TRUE
;
2157 * If this buffer is already dirty, we're done.
2159 dr_head
= list_head(&db
->db_dirty_records
);
2160 ASSERT(dr_head
== NULL
|| dr_head
->dr_txg
<= tx
->tx_txg
||
2161 db
->db
.db_object
== DMU_META_DNODE_OBJECT
);
2162 dr_next
= dbuf_find_dirty_lte(db
, tx
->tx_txg
);
2163 if (dr_next
&& dr_next
->dr_txg
== tx
->tx_txg
) {
2166 dbuf_redirty(dr_next
);
2167 mutex_exit(&db
->db_mtx
);
2172 * Only valid if not already dirty.
2174 ASSERT(dn
->dn_object
== 0 ||
2175 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
2176 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
2178 ASSERT3U(dn
->dn_nlevels
, >, db
->db_level
);
2181 * We should only be dirtying in syncing context if it's the
2182 * mos or we're initializing the os or it's a special object.
2183 * However, we are allowed to dirty in syncing context provided
2184 * we already dirtied it in open context. Hence we must make
2185 * this assertion only if we're not already dirty.
2188 VERIFY3U(tx
->tx_txg
, <=, spa_final_dirty_txg(os
->os_spa
));
2190 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
2191 rrw_enter(&os
->os_dsl_dataset
->ds_bp_rwlock
, RW_READER
, FTAG
);
2192 ASSERT(!dmu_tx_is_syncing(tx
) || DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
2193 os
->os_dsl_dataset
== NULL
|| BP_IS_HOLE(os
->os_rootbp
));
2194 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
2195 rrw_exit(&os
->os_dsl_dataset
->ds_bp_rwlock
, FTAG
);
2197 ASSERT(db
->db
.db_size
!= 0);
2199 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
2201 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
2202 dmu_objset_willuse_space(os
, db
->db
.db_size
, tx
);
2206 * If this buffer is dirty in an old transaction group we need
2207 * to make a copy of it so that the changes we make in this
2208 * transaction group won't leak out when we sync the older txg.
2210 dr
= kmem_zalloc(sizeof (dbuf_dirty_record_t
), KM_SLEEP
);
2211 list_link_init(&dr
->dr_dirty_node
);
2212 list_link_init(&dr
->dr_dbuf_node
);
2214 if (db
->db_level
== 0) {
2215 void *data_old
= db
->db_buf
;
2217 if (db
->db_state
!= DB_NOFILL
) {
2218 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2219 dbuf_fix_old_data(db
, tx
->tx_txg
);
2220 data_old
= db
->db
.db_data
;
2221 } else if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
) {
2223 * Release the data buffer from the cache so
2224 * that we can modify it without impacting
2225 * possible other users of this cached data
2226 * block. Note that indirect blocks and
2227 * private objects are not released until the
2228 * syncing state (since they are only modified
2231 arc_release(db
->db_buf
, db
);
2232 dbuf_fix_old_data(db
, tx
->tx_txg
);
2233 data_old
= db
->db_buf
;
2235 ASSERT(data_old
!= NULL
);
2237 dr
->dt
.dl
.dr_data
= data_old
;
2239 mutex_init(&dr
->dt
.di
.dr_mtx
, NULL
, MUTEX_NOLOCKDEP
, NULL
);
2240 list_create(&dr
->dt
.di
.dr_children
,
2241 sizeof (dbuf_dirty_record_t
),
2242 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
2244 if (db
->db_blkid
!= DMU_BONUS_BLKID
)
2245 dr
->dr_accounted
= db
->db
.db_size
;
2247 dr
->dr_txg
= tx
->tx_txg
;
2248 list_insert_before(&db
->db_dirty_records
, dr_next
, dr
);
2251 * We could have been freed_in_flight between the dbuf_noread
2252 * and dbuf_dirty. We win, as though the dbuf_noread() had
2253 * happened after the free.
2255 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
2256 db
->db_blkid
!= DMU_SPILL_BLKID
) {
2257 mutex_enter(&dn
->dn_mtx
);
2258 if (dn
->dn_free_ranges
[txgoff
] != NULL
) {
2259 range_tree_clear(dn
->dn_free_ranges
[txgoff
],
2262 mutex_exit(&dn
->dn_mtx
);
2263 db
->db_freed_in_flight
= FALSE
;
2267 * This buffer is now part of this txg
2269 dbuf_add_ref(db
, (void *)(uintptr_t)tx
->tx_txg
);
2270 db
->db_dirtycnt
+= 1;
2271 ASSERT3U(db
->db_dirtycnt
, <=, 3);
2273 mutex_exit(&db
->db_mtx
);
2275 if (db
->db_blkid
== DMU_BONUS_BLKID
||
2276 db
->db_blkid
== DMU_SPILL_BLKID
) {
2277 mutex_enter(&dn
->dn_mtx
);
2278 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
2279 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
2280 mutex_exit(&dn
->dn_mtx
);
2281 dnode_setdirty(dn
, tx
);
2286 if (!RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
2287 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
2288 drop_struct_rwlock
= B_TRUE
;
2292 * If we are overwriting a dedup BP, then unless it is snapshotted,
2293 * when we get to syncing context we will need to decrement its
2294 * refcount in the DDT. Prefetch the relevant DDT block so that
2295 * syncing context won't have to wait for the i/o.
2297 if (db
->db_blkptr
!= NULL
) {
2298 db_lock_type_t dblt
= dmu_buf_lock_parent(db
, RW_READER
, FTAG
);
2299 ddt_prefetch(os
->os_spa
, db
->db_blkptr
);
2300 dmu_buf_unlock_parent(db
, dblt
, FTAG
);
2304 * We need to hold the dn_struct_rwlock to make this assertion,
2305 * because it protects dn_phys / dn_next_nlevels from changing.
2307 ASSERT((dn
->dn_phys
->dn_nlevels
== 0 && db
->db_level
== 0) ||
2308 dn
->dn_phys
->dn_nlevels
> db
->db_level
||
2309 dn
->dn_next_nlevels
[txgoff
] > db
->db_level
||
2310 dn
->dn_next_nlevels
[(tx
->tx_txg
-1) & TXG_MASK
] > db
->db_level
||
2311 dn
->dn_next_nlevels
[(tx
->tx_txg
-2) & TXG_MASK
] > db
->db_level
);
2314 if (db
->db_level
== 0) {
2315 ASSERT(!db
->db_objset
->os_raw_receive
||
2316 dn
->dn_maxblkid
>= db
->db_blkid
);
2317 dnode_new_blkid(dn
, db
->db_blkid
, tx
,
2318 drop_struct_rwlock
, B_FALSE
);
2319 ASSERT(dn
->dn_maxblkid
>= db
->db_blkid
);
2322 if (db
->db_level
+1 < dn
->dn_nlevels
) {
2323 dmu_buf_impl_t
*parent
= db
->db_parent
;
2324 dbuf_dirty_record_t
*di
;
2325 int parent_held
= FALSE
;
2327 if (db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
) {
2328 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2329 parent
= dbuf_hold_level(dn
, db
->db_level
+ 1,
2330 db
->db_blkid
>> epbs
, FTAG
);
2331 ASSERT(parent
!= NULL
);
2334 if (drop_struct_rwlock
)
2335 rw_exit(&dn
->dn_struct_rwlock
);
2336 ASSERT3U(db
->db_level
+ 1, ==, parent
->db_level
);
2337 di
= dbuf_dirty(parent
, tx
);
2339 dbuf_rele(parent
, FTAG
);
2341 mutex_enter(&db
->db_mtx
);
2343 * Since we've dropped the mutex, it's possible that
2344 * dbuf_undirty() might have changed this out from under us.
2346 if (list_head(&db
->db_dirty_records
) == dr
||
2347 dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
2348 mutex_enter(&di
->dt
.di
.dr_mtx
);
2349 ASSERT3U(di
->dr_txg
, ==, tx
->tx_txg
);
2350 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
2351 list_insert_tail(&di
->dt
.di
.dr_children
, dr
);
2352 mutex_exit(&di
->dt
.di
.dr_mtx
);
2355 mutex_exit(&db
->db_mtx
);
2357 ASSERT(db
->db_level
+ 1 == dn
->dn_nlevels
);
2358 ASSERT(db
->db_blkid
< dn
->dn_nblkptr
);
2359 ASSERT(db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
2360 mutex_enter(&dn
->dn_mtx
);
2361 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
2362 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
2363 mutex_exit(&dn
->dn_mtx
);
2364 if (drop_struct_rwlock
)
2365 rw_exit(&dn
->dn_struct_rwlock
);
2368 dnode_setdirty(dn
, tx
);
2374 dbuf_undirty_bonus(dbuf_dirty_record_t
*dr
)
2376 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
2378 if (dr
->dt
.dl
.dr_data
!= db
->db
.db_data
) {
2379 struct dnode
*dn
= dr
->dr_dnode
;
2380 int max_bonuslen
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
);
2382 kmem_free(dr
->dt
.dl
.dr_data
, max_bonuslen
);
2383 arc_space_return(max_bonuslen
, ARC_SPACE_BONUS
);
2385 db
->db_data_pending
= NULL
;
2386 ASSERT(list_next(&db
->db_dirty_records
, dr
) == NULL
);
2387 list_remove(&db
->db_dirty_records
, dr
);
2388 if (dr
->dr_dbuf
->db_level
!= 0) {
2389 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
2390 list_destroy(&dr
->dt
.di
.dr_children
);
2392 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
2393 ASSERT3U(db
->db_dirtycnt
, >, 0);
2394 db
->db_dirtycnt
-= 1;
2398 * Undirty a buffer in the transaction group referenced by the given
2399 * transaction. Return whether this evicted the dbuf.
2402 dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
2404 uint64_t txg
= tx
->tx_txg
;
2409 * Due to our use of dn_nlevels below, this can only be called
2410 * in open context, unless we are operating on the MOS.
2411 * From syncing context, dn_nlevels may be different from the
2412 * dn_nlevels used when dbuf was dirtied.
2414 ASSERT(db
->db_objset
==
2415 dmu_objset_pool(db
->db_objset
)->dp_meta_objset
||
2416 txg
!= spa_syncing_txg(dmu_objset_spa(db
->db_objset
)));
2417 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2418 ASSERT0(db
->db_level
);
2419 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2422 * If this buffer is not dirty, we're done.
2424 dbuf_dirty_record_t
*dr
= dbuf_find_dirty_eq(db
, txg
);
2427 ASSERT(dr
->dr_dbuf
== db
);
2429 dnode_t
*dn
= dr
->dr_dnode
;
2431 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
2433 ASSERT(db
->db
.db_size
!= 0);
2435 dsl_pool_undirty_space(dmu_objset_pool(dn
->dn_objset
),
2436 dr
->dr_accounted
, txg
);
2438 list_remove(&db
->db_dirty_records
, dr
);
2441 * Note that there are three places in dbuf_dirty()
2442 * where this dirty record may be put on a list.
2443 * Make sure to do a list_remove corresponding to
2444 * every one of those list_insert calls.
2446 if (dr
->dr_parent
) {
2447 mutex_enter(&dr
->dr_parent
->dt
.di
.dr_mtx
);
2448 list_remove(&dr
->dr_parent
->dt
.di
.dr_children
, dr
);
2449 mutex_exit(&dr
->dr_parent
->dt
.di
.dr_mtx
);
2450 } else if (db
->db_blkid
== DMU_SPILL_BLKID
||
2451 db
->db_level
+ 1 == dn
->dn_nlevels
) {
2452 ASSERT(db
->db_blkptr
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
2453 mutex_enter(&dn
->dn_mtx
);
2454 list_remove(&dn
->dn_dirty_records
[txg
& TXG_MASK
], dr
);
2455 mutex_exit(&dn
->dn_mtx
);
2458 if (db
->db_state
!= DB_NOFILL
) {
2459 dbuf_unoverride(dr
);
2461 ASSERT(db
->db_buf
!= NULL
);
2462 ASSERT(dr
->dt
.dl
.dr_data
!= NULL
);
2463 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
2464 arc_buf_destroy(dr
->dt
.dl
.dr_data
, db
);
2467 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
2469 ASSERT(db
->db_dirtycnt
> 0);
2470 db
->db_dirtycnt
-= 1;
2472 if (zfs_refcount_remove(&db
->db_holds
, (void *)(uintptr_t)txg
) == 0) {
2473 ASSERT(db
->db_state
== DB_NOFILL
|| arc_released(db
->db_buf
));
2482 dmu_buf_will_dirty_impl(dmu_buf_t
*db_fake
, int flags
, dmu_tx_t
*tx
)
2484 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2486 ASSERT(tx
->tx_txg
!= 0);
2487 ASSERT(!zfs_refcount_is_zero(&db
->db_holds
));
2490 * Quick check for dirtiness. For already dirty blocks, this
2491 * reduces runtime of this function by >90%, and overall performance
2492 * by 50% for some workloads (e.g. file deletion with indirect blocks
2495 mutex_enter(&db
->db_mtx
);
2497 if (db
->db_state
== DB_CACHED
) {
2498 dbuf_dirty_record_t
*dr
= dbuf_find_dirty_eq(db
, tx
->tx_txg
);
2500 * It's possible that it is already dirty but not cached,
2501 * because there are some calls to dbuf_dirty() that don't
2502 * go through dmu_buf_will_dirty().
2505 /* This dbuf is already dirty and cached. */
2507 mutex_exit(&db
->db_mtx
);
2511 mutex_exit(&db
->db_mtx
);
2514 if (RW_WRITE_HELD(&DB_DNODE(db
)->dn_struct_rwlock
))
2515 flags
|= DB_RF_HAVESTRUCT
;
2517 (void) dbuf_read(db
, NULL
, flags
);
2518 (void) dbuf_dirty(db
, tx
);
2522 dmu_buf_will_dirty(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
2524 dmu_buf_will_dirty_impl(db_fake
,
2525 DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
, tx
);
2529 dmu_buf_is_dirty(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
2531 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2532 dbuf_dirty_record_t
*dr
;
2534 mutex_enter(&db
->db_mtx
);
2535 dr
= dbuf_find_dirty_eq(db
, tx
->tx_txg
);
2536 mutex_exit(&db
->db_mtx
);
2537 return (dr
!= NULL
);
2541 dmu_buf_will_not_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
2543 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2545 db
->db_state
= DB_NOFILL
;
2546 DTRACE_SET_STATE(db
, "allocating NOFILL buffer");
2547 dmu_buf_will_fill(db_fake
, tx
);
2551 dmu_buf_will_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
2553 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2555 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2556 ASSERT(tx
->tx_txg
!= 0);
2557 ASSERT(db
->db_level
== 0);
2558 ASSERT(!zfs_refcount_is_zero(&db
->db_holds
));
2560 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
||
2561 dmu_tx_private_ok(tx
));
2564 (void) dbuf_dirty(db
, tx
);
2568 * This function is effectively the same as dmu_buf_will_dirty(), but
2569 * indicates the caller expects raw encrypted data in the db, and provides
2570 * the crypt params (byteorder, salt, iv, mac) which should be stored in the
2571 * blkptr_t when this dbuf is written. This is only used for blocks of
2572 * dnodes, during raw receive.
2575 dmu_buf_set_crypt_params(dmu_buf_t
*db_fake
, boolean_t byteorder
,
2576 const uint8_t *salt
, const uint8_t *iv
, const uint8_t *mac
, dmu_tx_t
*tx
)
2578 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2579 dbuf_dirty_record_t
*dr
;
2582 * dr_has_raw_params is only processed for blocks of dnodes
2583 * (see dbuf_sync_dnode_leaf_crypt()).
2585 ASSERT3U(db
->db
.db_object
, ==, DMU_META_DNODE_OBJECT
);
2586 ASSERT3U(db
->db_level
, ==, 0);
2587 ASSERT(db
->db_objset
->os_raw_receive
);
2589 dmu_buf_will_dirty_impl(db_fake
,
2590 DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
| DB_RF_NO_DECRYPT
, tx
);
2592 dr
= dbuf_find_dirty_eq(db
, tx
->tx_txg
);
2594 ASSERT3P(dr
, !=, NULL
);
2596 dr
->dt
.dl
.dr_has_raw_params
= B_TRUE
;
2597 dr
->dt
.dl
.dr_byteorder
= byteorder
;
2598 bcopy(salt
, dr
->dt
.dl
.dr_salt
, ZIO_DATA_SALT_LEN
);
2599 bcopy(iv
, dr
->dt
.dl
.dr_iv
, ZIO_DATA_IV_LEN
);
2600 bcopy(mac
, dr
->dt
.dl
.dr_mac
, ZIO_DATA_MAC_LEN
);
2604 dbuf_override_impl(dmu_buf_impl_t
*db
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
2606 struct dirty_leaf
*dl
;
2607 dbuf_dirty_record_t
*dr
;
2609 dr
= list_head(&db
->db_dirty_records
);
2610 ASSERT3U(dr
->dr_txg
, ==, tx
->tx_txg
);
2612 dl
->dr_overridden_by
= *bp
;
2613 dl
->dr_override_state
= DR_OVERRIDDEN
;
2614 dl
->dr_overridden_by
.blk_birth
= dr
->dr_txg
;
2619 dmu_buf_fill_done(dmu_buf_t
*dbuf
, dmu_tx_t
*tx
)
2621 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbuf
;
2622 dbuf_states_t old_state
;
2623 mutex_enter(&db
->db_mtx
);
2626 old_state
= db
->db_state
;
2627 db
->db_state
= DB_CACHED
;
2628 if (old_state
== DB_FILL
) {
2629 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
2630 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2631 /* we were freed while filling */
2632 /* XXX dbuf_undirty? */
2633 bzero(db
->db
.db_data
, db
->db
.db_size
);
2634 db
->db_freed_in_flight
= FALSE
;
2635 DTRACE_SET_STATE(db
,
2636 "fill done handling freed in flight");
2638 DTRACE_SET_STATE(db
, "fill done");
2640 cv_broadcast(&db
->db_changed
);
2642 mutex_exit(&db
->db_mtx
);
2646 dmu_buf_write_embedded(dmu_buf_t
*dbuf
, void *data
,
2647 bp_embedded_type_t etype
, enum zio_compress comp
,
2648 int uncompressed_size
, int compressed_size
, int byteorder
,
2651 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbuf
;
2652 struct dirty_leaf
*dl
;
2653 dmu_object_type_t type
;
2654 dbuf_dirty_record_t
*dr
;
2656 if (etype
== BP_EMBEDDED_TYPE_DATA
) {
2657 ASSERT(spa_feature_is_active(dmu_objset_spa(db
->db_objset
),
2658 SPA_FEATURE_EMBEDDED_DATA
));
2662 type
= DB_DNODE(db
)->dn_type
;
2665 ASSERT0(db
->db_level
);
2666 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2668 dmu_buf_will_not_fill(dbuf
, tx
);
2670 dr
= list_head(&db
->db_dirty_records
);
2671 ASSERT3U(dr
->dr_txg
, ==, tx
->tx_txg
);
2673 encode_embedded_bp_compressed(&dl
->dr_overridden_by
,
2674 data
, comp
, uncompressed_size
, compressed_size
);
2675 BPE_SET_ETYPE(&dl
->dr_overridden_by
, etype
);
2676 BP_SET_TYPE(&dl
->dr_overridden_by
, type
);
2677 BP_SET_LEVEL(&dl
->dr_overridden_by
, 0);
2678 BP_SET_BYTEORDER(&dl
->dr_overridden_by
, byteorder
);
2680 dl
->dr_override_state
= DR_OVERRIDDEN
;
2681 dl
->dr_overridden_by
.blk_birth
= dr
->dr_txg
;
2685 dmu_buf_redact(dmu_buf_t
*dbuf
, dmu_tx_t
*tx
)
2687 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbuf
;
2688 dmu_object_type_t type
;
2689 ASSERT(dsl_dataset_feature_is_active(db
->db_objset
->os_dsl_dataset
,
2690 SPA_FEATURE_REDACTED_DATASETS
));
2693 type
= DB_DNODE(db
)->dn_type
;
2696 ASSERT0(db
->db_level
);
2697 dmu_buf_will_not_fill(dbuf
, tx
);
2699 blkptr_t bp
= { { { {0} } } };
2700 BP_SET_TYPE(&bp
, type
);
2701 BP_SET_LEVEL(&bp
, 0);
2702 BP_SET_BIRTH(&bp
, tx
->tx_txg
, 0);
2703 BP_SET_REDACTED(&bp
);
2704 BPE_SET_LSIZE(&bp
, dbuf
->db_size
);
2706 dbuf_override_impl(db
, &bp
, tx
);
2710 * Directly assign a provided arc buf to a given dbuf if it's not referenced
2711 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
2714 dbuf_assign_arcbuf(dmu_buf_impl_t
*db
, arc_buf_t
*buf
, dmu_tx_t
*tx
)
2716 ASSERT(!zfs_refcount_is_zero(&db
->db_holds
));
2717 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2718 ASSERT(db
->db_level
== 0);
2719 ASSERT3U(dbuf_is_metadata(db
), ==, arc_is_metadata(buf
));
2720 ASSERT(buf
!= NULL
);
2721 ASSERT3U(arc_buf_lsize(buf
), ==, db
->db
.db_size
);
2722 ASSERT(tx
->tx_txg
!= 0);
2724 arc_return_buf(buf
, db
);
2725 ASSERT(arc_released(buf
));
2727 mutex_enter(&db
->db_mtx
);
2729 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
2730 cv_wait(&db
->db_changed
, &db
->db_mtx
);
2732 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_UNCACHED
);
2734 if (db
->db_state
== DB_CACHED
&&
2735 zfs_refcount_count(&db
->db_holds
) - 1 > db
->db_dirtycnt
) {
2737 * In practice, we will never have a case where we have an
2738 * encrypted arc buffer while additional holds exist on the
2739 * dbuf. We don't handle this here so we simply assert that
2742 ASSERT(!arc_is_encrypted(buf
));
2743 mutex_exit(&db
->db_mtx
);
2744 (void) dbuf_dirty(db
, tx
);
2745 bcopy(buf
->b_data
, db
->db
.db_data
, db
->db
.db_size
);
2746 arc_buf_destroy(buf
, db
);
2750 if (db
->db_state
== DB_CACHED
) {
2751 dbuf_dirty_record_t
*dr
= list_head(&db
->db_dirty_records
);
2753 ASSERT(db
->db_buf
!= NULL
);
2754 if (dr
!= NULL
&& dr
->dr_txg
== tx
->tx_txg
) {
2755 ASSERT(dr
->dt
.dl
.dr_data
== db
->db_buf
);
2757 if (!arc_released(db
->db_buf
)) {
2758 ASSERT(dr
->dt
.dl
.dr_override_state
==
2760 arc_release(db
->db_buf
, db
);
2762 dr
->dt
.dl
.dr_data
= buf
;
2763 arc_buf_destroy(db
->db_buf
, db
);
2764 } else if (dr
== NULL
|| dr
->dt
.dl
.dr_data
!= db
->db_buf
) {
2765 arc_release(db
->db_buf
, db
);
2766 arc_buf_destroy(db
->db_buf
, db
);
2770 ASSERT(db
->db_buf
== NULL
);
2771 dbuf_set_data(db
, buf
);
2772 db
->db_state
= DB_FILL
;
2773 DTRACE_SET_STATE(db
, "filling assigned arcbuf");
2774 mutex_exit(&db
->db_mtx
);
2775 (void) dbuf_dirty(db
, tx
);
2776 dmu_buf_fill_done(&db
->db
, tx
);
2780 dbuf_destroy(dmu_buf_impl_t
*db
)
2783 dmu_buf_impl_t
*parent
= db
->db_parent
;
2784 dmu_buf_impl_t
*dndb
;
2786 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2787 ASSERT(zfs_refcount_is_zero(&db
->db_holds
));
2789 if (db
->db_buf
!= NULL
) {
2790 arc_buf_destroy(db
->db_buf
, db
);
2794 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2795 int slots
= DB_DNODE(db
)->dn_num_slots
;
2796 int bonuslen
= DN_SLOTS_TO_BONUSLEN(slots
);
2797 if (db
->db
.db_data
!= NULL
) {
2798 kmem_free(db
->db
.db_data
, bonuslen
);
2799 arc_space_return(bonuslen
, ARC_SPACE_BONUS
);
2800 db
->db_state
= DB_UNCACHED
;
2801 DTRACE_SET_STATE(db
, "buffer cleared");
2805 dbuf_clear_data(db
);
2807 if (multilist_link_active(&db
->db_cache_link
)) {
2808 ASSERT(db
->db_caching_status
== DB_DBUF_CACHE
||
2809 db
->db_caching_status
== DB_DBUF_METADATA_CACHE
);
2811 multilist_remove(&dbuf_caches
[db
->db_caching_status
].cache
, db
);
2812 (void) zfs_refcount_remove_many(
2813 &dbuf_caches
[db
->db_caching_status
].size
,
2814 db
->db
.db_size
, db
);
2816 if (db
->db_caching_status
== DB_DBUF_METADATA_CACHE
) {
2817 DBUF_STAT_BUMPDOWN(metadata_cache_count
);
2819 DBUF_STAT_BUMPDOWN(cache_levels
[db
->db_level
]);
2820 DBUF_STAT_BUMPDOWN(cache_count
);
2821 DBUF_STAT_DECR(cache_levels_bytes
[db
->db_level
],
2824 db
->db_caching_status
= DB_NO_CACHE
;
2827 ASSERT(db
->db_state
== DB_UNCACHED
|| db
->db_state
== DB_NOFILL
);
2828 ASSERT(db
->db_data_pending
== NULL
);
2829 ASSERT(list_is_empty(&db
->db_dirty_records
));
2831 db
->db_state
= DB_EVICTING
;
2832 DTRACE_SET_STATE(db
, "buffer eviction started");
2833 db
->db_blkptr
= NULL
;
2836 * Now that db_state is DB_EVICTING, nobody else can find this via
2837 * the hash table. We can now drop db_mtx, which allows us to
2838 * acquire the dn_dbufs_mtx.
2840 mutex_exit(&db
->db_mtx
);
2845 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
2846 boolean_t needlock
= !MUTEX_HELD(&dn
->dn_dbufs_mtx
);
2848 mutex_enter_nested(&dn
->dn_dbufs_mtx
,
2850 avl_remove(&dn
->dn_dbufs
, db
);
2854 mutex_exit(&dn
->dn_dbufs_mtx
);
2856 * Decrementing the dbuf count means that the hold corresponding
2857 * to the removed dbuf is no longer discounted in dnode_move(),
2858 * so the dnode cannot be moved until after we release the hold.
2859 * The membar_producer() ensures visibility of the decremented
2860 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
2863 mutex_enter(&dn
->dn_mtx
);
2864 dnode_rele_and_unlock(dn
, db
, B_TRUE
);
2865 db
->db_dnode_handle
= NULL
;
2867 dbuf_hash_remove(db
);
2872 ASSERT(zfs_refcount_is_zero(&db
->db_holds
));
2874 db
->db_parent
= NULL
;
2876 ASSERT(db
->db_buf
== NULL
);
2877 ASSERT(db
->db
.db_data
== NULL
);
2878 ASSERT(db
->db_hash_next
== NULL
);
2879 ASSERT(db
->db_blkptr
== NULL
);
2880 ASSERT(db
->db_data_pending
== NULL
);
2881 ASSERT3U(db
->db_caching_status
, ==, DB_NO_CACHE
);
2882 ASSERT(!multilist_link_active(&db
->db_cache_link
));
2884 kmem_cache_free(dbuf_kmem_cache
, db
);
2885 arc_space_return(sizeof (dmu_buf_impl_t
), ARC_SPACE_DBUF
);
2888 * If this dbuf is referenced from an indirect dbuf,
2889 * decrement the ref count on the indirect dbuf.
2891 if (parent
&& parent
!= dndb
) {
2892 mutex_enter(&parent
->db_mtx
);
2893 dbuf_rele_and_unlock(parent
, db
, B_TRUE
);
2898 * Note: While bpp will always be updated if the function returns success,
2899 * parentp will not be updated if the dnode does not have dn_dbuf filled in;
2900 * this happens when the dnode is the meta-dnode, or {user|group|project}used
2903 __attribute__((always_inline
))
2905 dbuf_findbp(dnode_t
*dn
, int level
, uint64_t blkid
, int fail_sparse
,
2906 dmu_buf_impl_t
**parentp
, blkptr_t
**bpp
)
2911 ASSERT(blkid
!= DMU_BONUS_BLKID
);
2913 if (blkid
== DMU_SPILL_BLKID
) {
2914 mutex_enter(&dn
->dn_mtx
);
2915 if (dn
->dn_have_spill
&&
2916 (dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
))
2917 *bpp
= DN_SPILL_BLKPTR(dn
->dn_phys
);
2920 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
2921 *parentp
= dn
->dn_dbuf
;
2922 mutex_exit(&dn
->dn_mtx
);
2927 (dn
->dn_phys
->dn_nlevels
== 0) ? 1 : dn
->dn_phys
->dn_nlevels
;
2928 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2930 ASSERT3U(level
* epbs
, <, 64);
2931 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2933 * This assertion shouldn't trip as long as the max indirect block size
2934 * is less than 1M. The reason for this is that up to that point,
2935 * the number of levels required to address an entire object with blocks
2936 * of size SPA_MINBLOCKSIZE satisfies nlevels * epbs + 1 <= 64. In
2937 * other words, if N * epbs + 1 > 64, then if (N-1) * epbs + 1 > 55
2938 * (i.e. we can address the entire object), objects will all use at most
2939 * N-1 levels and the assertion won't overflow. However, once epbs is
2940 * 13, 4 * 13 + 1 = 53, but 5 * 13 + 1 = 66. Then, 4 levels will not be
2941 * enough to address an entire object, so objects will have 5 levels,
2942 * but then this assertion will overflow.
2944 * All this is to say that if we ever increase DN_MAX_INDBLKSHIFT, we
2945 * need to redo this logic to handle overflows.
2947 ASSERT(level
>= nlevels
||
2948 ((nlevels
- level
- 1) * epbs
) +
2949 highbit64(dn
->dn_phys
->dn_nblkptr
) <= 64);
2950 if (level
>= nlevels
||
2951 blkid
>= ((uint64_t)dn
->dn_phys
->dn_nblkptr
<<
2952 ((nlevels
- level
- 1) * epbs
)) ||
2954 blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))) {
2955 /* the buffer has no parent yet */
2956 return (SET_ERROR(ENOENT
));
2957 } else if (level
< nlevels
-1) {
2958 /* this block is referenced from an indirect block */
2961 err
= dbuf_hold_impl(dn
, level
+ 1,
2962 blkid
>> epbs
, fail_sparse
, FALSE
, NULL
, parentp
);
2966 err
= dbuf_read(*parentp
, NULL
,
2967 (DB_RF_HAVESTRUCT
| DB_RF_NOPREFETCH
| DB_RF_CANFAIL
));
2969 dbuf_rele(*parentp
, NULL
);
2973 rw_enter(&(*parentp
)->db_rwlock
, RW_READER
);
2974 *bpp
= ((blkptr_t
*)(*parentp
)->db
.db_data
) +
2975 (blkid
& ((1ULL << epbs
) - 1));
2976 if (blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))
2977 ASSERT(BP_IS_HOLE(*bpp
));
2978 rw_exit(&(*parentp
)->db_rwlock
);
2981 /* the block is referenced from the dnode */
2982 ASSERT3U(level
, ==, nlevels
-1);
2983 ASSERT(dn
->dn_phys
->dn_nblkptr
== 0 ||
2984 blkid
< dn
->dn_phys
->dn_nblkptr
);
2986 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
2987 *parentp
= dn
->dn_dbuf
;
2989 *bpp
= &dn
->dn_phys
->dn_blkptr
[blkid
];
2994 static dmu_buf_impl_t
*
2995 dbuf_create(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
2996 dmu_buf_impl_t
*parent
, blkptr_t
*blkptr
)
2998 objset_t
*os
= dn
->dn_objset
;
2999 dmu_buf_impl_t
*db
, *odb
;
3001 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
3002 ASSERT(dn
->dn_type
!= DMU_OT_NONE
);
3004 db
= kmem_cache_alloc(dbuf_kmem_cache
, KM_SLEEP
);
3006 list_create(&db
->db_dirty_records
, sizeof (dbuf_dirty_record_t
),
3007 offsetof(dbuf_dirty_record_t
, dr_dbuf_node
));
3010 db
->db
.db_object
= dn
->dn_object
;
3011 db
->db_level
= level
;
3012 db
->db_blkid
= blkid
;
3013 db
->db_dirtycnt
= 0;
3014 db
->db_dnode_handle
= dn
->dn_handle
;
3015 db
->db_parent
= parent
;
3016 db
->db_blkptr
= blkptr
;
3019 db
->db_user_immediate_evict
= FALSE
;
3020 db
->db_freed_in_flight
= FALSE
;
3021 db
->db_pending_evict
= FALSE
;
3023 if (blkid
== DMU_BONUS_BLKID
) {
3024 ASSERT3P(parent
, ==, dn
->dn_dbuf
);
3025 db
->db
.db_size
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
) -
3026 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
);
3027 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
3028 db
->db
.db_offset
= DMU_BONUS_BLKID
;
3029 db
->db_state
= DB_UNCACHED
;
3030 DTRACE_SET_STATE(db
, "bonus buffer created");
3031 db
->db_caching_status
= DB_NO_CACHE
;
3032 /* the bonus dbuf is not placed in the hash table */
3033 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_DBUF
);
3035 } else if (blkid
== DMU_SPILL_BLKID
) {
3036 db
->db
.db_size
= (blkptr
!= NULL
) ?
3037 BP_GET_LSIZE(blkptr
) : SPA_MINBLOCKSIZE
;
3038 db
->db
.db_offset
= 0;
3041 db
->db_level
? 1 << dn
->dn_indblkshift
: dn
->dn_datablksz
;
3042 db
->db
.db_size
= blocksize
;
3043 db
->db
.db_offset
= db
->db_blkid
* blocksize
;
3047 * Hold the dn_dbufs_mtx while we get the new dbuf
3048 * in the hash table *and* added to the dbufs list.
3049 * This prevents a possible deadlock with someone
3050 * trying to look up this dbuf before it's added to the
3053 mutex_enter(&dn
->dn_dbufs_mtx
);
3054 db
->db_state
= DB_EVICTING
; /* not worth logging this state change */
3055 if ((odb
= dbuf_hash_insert(db
)) != NULL
) {
3056 /* someone else inserted it first */
3057 kmem_cache_free(dbuf_kmem_cache
, db
);
3058 mutex_exit(&dn
->dn_dbufs_mtx
);
3059 DBUF_STAT_BUMP(hash_insert_race
);
3062 avl_add(&dn
->dn_dbufs
, db
);
3064 db
->db_state
= DB_UNCACHED
;
3065 DTRACE_SET_STATE(db
, "regular buffer created");
3066 db
->db_caching_status
= DB_NO_CACHE
;
3067 mutex_exit(&dn
->dn_dbufs_mtx
);
3068 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_DBUF
);
3070 if (parent
&& parent
!= dn
->dn_dbuf
)
3071 dbuf_add_ref(parent
, db
);
3073 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
3074 zfs_refcount_count(&dn
->dn_holds
) > 0);
3075 (void) zfs_refcount_add(&dn
->dn_holds
, db
);
3077 dprintf_dbuf(db
, "db=%p\n", db
);
3083 * This function returns a block pointer and information about the object,
3084 * given a dnode and a block. This is a publicly accessible version of
3085 * dbuf_findbp that only returns some information, rather than the
3086 * dbuf. Note that the dnode passed in must be held, and the dn_struct_rwlock
3087 * should be locked as (at least) a reader.
3090 dbuf_dnode_findbp(dnode_t
*dn
, uint64_t level
, uint64_t blkid
,
3091 blkptr_t
*bp
, uint16_t *datablkszsec
, uint8_t *indblkshift
)
3093 dmu_buf_impl_t
*dbp
= NULL
;
3096 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
3098 err
= dbuf_findbp(dn
, level
, blkid
, B_FALSE
, &dbp
, &bp2
);
3102 dbuf_rele(dbp
, NULL
);
3103 if (datablkszsec
!= NULL
)
3104 *datablkszsec
= dn
->dn_phys
->dn_datablkszsec
;
3105 if (indblkshift
!= NULL
)
3106 *indblkshift
= dn
->dn_phys
->dn_indblkshift
;
3112 typedef struct dbuf_prefetch_arg
{
3113 spa_t
*dpa_spa
; /* The spa to issue the prefetch in. */
3114 zbookmark_phys_t dpa_zb
; /* The target block to prefetch. */
3115 int dpa_epbs
; /* Entries (blkptr_t's) Per Block Shift. */
3116 int dpa_curlevel
; /* The current level that we're reading */
3117 dnode_t
*dpa_dnode
; /* The dnode associated with the prefetch */
3118 zio_priority_t dpa_prio
; /* The priority I/Os should be issued at. */
3119 zio_t
*dpa_zio
; /* The parent zio_t for all prefetches. */
3120 arc_flags_t dpa_aflags
; /* Flags to pass to the final prefetch. */
3121 dbuf_prefetch_fn dpa_cb
; /* prefetch completion callback */
3122 void *dpa_arg
; /* prefetch completion arg */
3123 } dbuf_prefetch_arg_t
;
3126 dbuf_prefetch_fini(dbuf_prefetch_arg_t
*dpa
, boolean_t io_done
)
3128 if (dpa
->dpa_cb
!= NULL
)
3129 dpa
->dpa_cb(dpa
->dpa_arg
, io_done
);
3130 kmem_free(dpa
, sizeof (*dpa
));
3134 dbuf_issue_final_prefetch_done(zio_t
*zio
, const zbookmark_phys_t
*zb
,
3135 const blkptr_t
*iobp
, arc_buf_t
*abuf
, void *private)
3137 dbuf_prefetch_arg_t
*dpa
= private;
3139 dbuf_prefetch_fini(dpa
, B_TRUE
);
3141 arc_buf_destroy(abuf
, private);
3145 * Actually issue the prefetch read for the block given.
3148 dbuf_issue_final_prefetch(dbuf_prefetch_arg_t
*dpa
, blkptr_t
*bp
)
3150 ASSERT(!BP_IS_REDACTED(bp
) ||
3151 dsl_dataset_feature_is_active(
3152 dpa
->dpa_dnode
->dn_objset
->os_dsl_dataset
,
3153 SPA_FEATURE_REDACTED_DATASETS
));
3155 if (BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
) || BP_IS_REDACTED(bp
))
3156 return (dbuf_prefetch_fini(dpa
, B_FALSE
));
3158 int zio_flags
= ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
;
3159 arc_flags_t aflags
=
3160 dpa
->dpa_aflags
| ARC_FLAG_NOWAIT
| ARC_FLAG_PREFETCH
|
3163 /* dnodes are always read as raw and then converted later */
3164 if (BP_GET_TYPE(bp
) == DMU_OT_DNODE
&& BP_IS_PROTECTED(bp
) &&
3165 dpa
->dpa_curlevel
== 0)
3166 zio_flags
|= ZIO_FLAG_RAW
;
3168 ASSERT3U(dpa
->dpa_curlevel
, ==, BP_GET_LEVEL(bp
));
3169 ASSERT3U(dpa
->dpa_curlevel
, ==, dpa
->dpa_zb
.zb_level
);
3170 ASSERT(dpa
->dpa_zio
!= NULL
);
3171 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
, bp
,
3172 dbuf_issue_final_prefetch_done
, dpa
,
3173 dpa
->dpa_prio
, zio_flags
, &aflags
, &dpa
->dpa_zb
);
3177 * Called when an indirect block above our prefetch target is read in. This
3178 * will either read in the next indirect block down the tree or issue the actual
3179 * prefetch if the next block down is our target.
3182 dbuf_prefetch_indirect_done(zio_t
*zio
, const zbookmark_phys_t
*zb
,
3183 const blkptr_t
*iobp
, arc_buf_t
*abuf
, void *private)
3185 dbuf_prefetch_arg_t
*dpa
= private;
3187 ASSERT3S(dpa
->dpa_zb
.zb_level
, <, dpa
->dpa_curlevel
);
3188 ASSERT3S(dpa
->dpa_curlevel
, >, 0);
3191 ASSERT(zio
== NULL
|| zio
->io_error
!= 0);
3192 return (dbuf_prefetch_fini(dpa
, B_TRUE
));
3194 ASSERT(zio
== NULL
|| zio
->io_error
== 0);
3197 * The dpa_dnode is only valid if we are called with a NULL
3198 * zio. This indicates that the arc_read() returned without
3199 * first calling zio_read() to issue a physical read. Once
3200 * a physical read is made the dpa_dnode must be invalidated
3201 * as the locks guarding it may have been dropped. If the
3202 * dpa_dnode is still valid, then we want to add it to the dbuf
3203 * cache. To do so, we must hold the dbuf associated with the block
3204 * we just prefetched, read its contents so that we associate it
3205 * with an arc_buf_t, and then release it.
3208 ASSERT3S(BP_GET_LEVEL(zio
->io_bp
), ==, dpa
->dpa_curlevel
);
3209 if (zio
->io_flags
& ZIO_FLAG_RAW_COMPRESS
) {
3210 ASSERT3U(BP_GET_PSIZE(zio
->io_bp
), ==, zio
->io_size
);
3212 ASSERT3U(BP_GET_LSIZE(zio
->io_bp
), ==, zio
->io_size
);
3214 ASSERT3P(zio
->io_spa
, ==, dpa
->dpa_spa
);
3216 dpa
->dpa_dnode
= NULL
;
3217 } else if (dpa
->dpa_dnode
!= NULL
) {
3218 uint64_t curblkid
= dpa
->dpa_zb
.zb_blkid
>>
3219 (dpa
->dpa_epbs
* (dpa
->dpa_curlevel
-
3220 dpa
->dpa_zb
.zb_level
));
3221 dmu_buf_impl_t
*db
= dbuf_hold_level(dpa
->dpa_dnode
,
3222 dpa
->dpa_curlevel
, curblkid
, FTAG
);
3224 arc_buf_destroy(abuf
, private);
3225 return (dbuf_prefetch_fini(dpa
, B_TRUE
));
3227 (void) dbuf_read(db
, NULL
,
3228 DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
| DB_RF_HAVESTRUCT
);
3229 dbuf_rele(db
, FTAG
);
3232 dpa
->dpa_curlevel
--;
3233 uint64_t nextblkid
= dpa
->dpa_zb
.zb_blkid
>>
3234 (dpa
->dpa_epbs
* (dpa
->dpa_curlevel
- dpa
->dpa_zb
.zb_level
));
3235 blkptr_t
*bp
= ((blkptr_t
*)abuf
->b_data
) +
3236 P2PHASE(nextblkid
, 1ULL << dpa
->dpa_epbs
);
3238 ASSERT(!BP_IS_REDACTED(bp
) ||
3239 dsl_dataset_feature_is_active(
3240 dpa
->dpa_dnode
->dn_objset
->os_dsl_dataset
,
3241 SPA_FEATURE_REDACTED_DATASETS
));
3242 if (BP_IS_HOLE(bp
) || BP_IS_REDACTED(bp
)) {
3243 dbuf_prefetch_fini(dpa
, B_TRUE
);
3244 } else if (dpa
->dpa_curlevel
== dpa
->dpa_zb
.zb_level
) {
3245 ASSERT3U(nextblkid
, ==, dpa
->dpa_zb
.zb_blkid
);
3246 dbuf_issue_final_prefetch(dpa
, bp
);
3248 arc_flags_t iter_aflags
= ARC_FLAG_NOWAIT
;
3249 zbookmark_phys_t zb
;
3251 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
3252 if (dpa
->dpa_aflags
& ARC_FLAG_L2CACHE
)
3253 iter_aflags
|= ARC_FLAG_L2CACHE
;
3255 ASSERT3U(dpa
->dpa_curlevel
, ==, BP_GET_LEVEL(bp
));
3257 SET_BOOKMARK(&zb
, dpa
->dpa_zb
.zb_objset
,
3258 dpa
->dpa_zb
.zb_object
, dpa
->dpa_curlevel
, nextblkid
);
3260 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
,
3261 bp
, dbuf_prefetch_indirect_done
, dpa
, dpa
->dpa_prio
,
3262 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
3266 arc_buf_destroy(abuf
, private);
3270 * Issue prefetch reads for the given block on the given level. If the indirect
3271 * blocks above that block are not in memory, we will read them in
3272 * asynchronously. As a result, this call never blocks waiting for a read to
3273 * complete. Note that the prefetch might fail if the dataset is encrypted and
3274 * the encryption key is unmapped before the IO completes.
3277 dbuf_prefetch_impl(dnode_t
*dn
, int64_t level
, uint64_t blkid
,
3278 zio_priority_t prio
, arc_flags_t aflags
, dbuf_prefetch_fn cb
,
3282 int epbs
, nlevels
, curlevel
;
3285 ASSERT(blkid
!= DMU_BONUS_BLKID
);
3286 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
3288 if (blkid
> dn
->dn_maxblkid
)
3291 if (level
== 0 && dnode_block_freed(dn
, blkid
))
3295 * This dnode hasn't been written to disk yet, so there's nothing to
3298 nlevels
= dn
->dn_phys
->dn_nlevels
;
3299 if (level
>= nlevels
|| dn
->dn_phys
->dn_nblkptr
== 0)
3302 epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
3303 if (dn
->dn_phys
->dn_maxblkid
< blkid
<< (epbs
* level
))
3306 dmu_buf_impl_t
*db
= dbuf_find(dn
->dn_objset
, dn
->dn_object
,
3309 mutex_exit(&db
->db_mtx
);
3311 * This dbuf already exists. It is either CACHED, or
3312 * (we assume) about to be read or filled.
3318 * Find the closest ancestor (indirect block) of the target block
3319 * that is present in the cache. In this indirect block, we will
3320 * find the bp that is at curlevel, curblkid.
3324 while (curlevel
< nlevels
- 1) {
3325 int parent_level
= curlevel
+ 1;
3326 uint64_t parent_blkid
= curblkid
>> epbs
;
3329 if (dbuf_hold_impl(dn
, parent_level
, parent_blkid
,
3330 FALSE
, TRUE
, FTAG
, &db
) == 0) {
3331 blkptr_t
*bpp
= db
->db_buf
->b_data
;
3332 bp
= bpp
[P2PHASE(curblkid
, 1 << epbs
)];
3333 dbuf_rele(db
, FTAG
);
3337 curlevel
= parent_level
;
3338 curblkid
= parent_blkid
;
3341 if (curlevel
== nlevels
- 1) {
3342 /* No cached indirect blocks found. */
3343 ASSERT3U(curblkid
, <, dn
->dn_phys
->dn_nblkptr
);
3344 bp
= dn
->dn_phys
->dn_blkptr
[curblkid
];
3346 ASSERT(!BP_IS_REDACTED(&bp
) ||
3347 dsl_dataset_feature_is_active(dn
->dn_objset
->os_dsl_dataset
,
3348 SPA_FEATURE_REDACTED_DATASETS
));
3349 if (BP_IS_HOLE(&bp
) || BP_IS_REDACTED(&bp
))
3352 ASSERT3U(curlevel
, ==, BP_GET_LEVEL(&bp
));
3354 zio_t
*pio
= zio_root(dmu_objset_spa(dn
->dn_objset
), NULL
, NULL
,
3357 dbuf_prefetch_arg_t
*dpa
= kmem_zalloc(sizeof (*dpa
), KM_SLEEP
);
3358 dsl_dataset_t
*ds
= dn
->dn_objset
->os_dsl_dataset
;
3359 SET_BOOKMARK(&dpa
->dpa_zb
, ds
!= NULL
? ds
->ds_object
: DMU_META_OBJSET
,
3360 dn
->dn_object
, level
, blkid
);
3361 dpa
->dpa_curlevel
= curlevel
;
3362 dpa
->dpa_prio
= prio
;
3363 dpa
->dpa_aflags
= aflags
;
3364 dpa
->dpa_spa
= dn
->dn_objset
->os_spa
;
3365 dpa
->dpa_dnode
= dn
;
3366 dpa
->dpa_epbs
= epbs
;
3371 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
3372 if (DNODE_LEVEL_IS_L2CACHEABLE(dn
, level
))
3373 dpa
->dpa_aflags
|= ARC_FLAG_L2CACHE
;
3376 * If we have the indirect just above us, no need to do the asynchronous
3377 * prefetch chain; we'll just run the last step ourselves. If we're at
3378 * a higher level, though, we want to issue the prefetches for all the
3379 * indirect blocks asynchronously, so we can go on with whatever we were
3382 if (curlevel
== level
) {
3383 ASSERT3U(curblkid
, ==, blkid
);
3384 dbuf_issue_final_prefetch(dpa
, &bp
);
3386 arc_flags_t iter_aflags
= ARC_FLAG_NOWAIT
;
3387 zbookmark_phys_t zb
;
3389 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
3390 if (DNODE_LEVEL_IS_L2CACHEABLE(dn
, level
))
3391 iter_aflags
|= ARC_FLAG_L2CACHE
;
3393 SET_BOOKMARK(&zb
, ds
!= NULL
? ds
->ds_object
: DMU_META_OBJSET
,
3394 dn
->dn_object
, curlevel
, curblkid
);
3395 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
,
3396 &bp
, dbuf_prefetch_indirect_done
, dpa
, prio
,
3397 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
3401 * We use pio here instead of dpa_zio since it's possible that
3402 * dpa may have already been freed.
3413 dbuf_prefetch(dnode_t
*dn
, int64_t level
, uint64_t blkid
, zio_priority_t prio
,
3417 return (dbuf_prefetch_impl(dn
, level
, blkid
, prio
, aflags
, NULL
, NULL
));
3421 * Helper function for dbuf_hold_impl() to copy a buffer. Handles
3422 * the case of encrypted, compressed and uncompressed buffers by
3423 * allocating the new buffer, respectively, with arc_alloc_raw_buf(),
3424 * arc_alloc_compressed_buf() or arc_alloc_buf().*
3426 * NOTE: Declared noinline to avoid stack bloat in dbuf_hold_impl().
3428 noinline
static void
3429 dbuf_hold_copy(dnode_t
*dn
, dmu_buf_impl_t
*db
)
3431 dbuf_dirty_record_t
*dr
= db
->db_data_pending
;
3432 arc_buf_t
*data
= dr
->dt
.dl
.dr_data
;
3433 enum zio_compress compress_type
= arc_get_compression(data
);
3434 uint8_t complevel
= arc_get_complevel(data
);
3436 if (arc_is_encrypted(data
)) {
3437 boolean_t byteorder
;
3438 uint8_t salt
[ZIO_DATA_SALT_LEN
];
3439 uint8_t iv
[ZIO_DATA_IV_LEN
];
3440 uint8_t mac
[ZIO_DATA_MAC_LEN
];
3442 arc_get_raw_params(data
, &byteorder
, salt
, iv
, mac
);
3443 dbuf_set_data(db
, arc_alloc_raw_buf(dn
->dn_objset
->os_spa
, db
,
3444 dmu_objset_id(dn
->dn_objset
), byteorder
, salt
, iv
, mac
,
3445 dn
->dn_type
, arc_buf_size(data
), arc_buf_lsize(data
),
3446 compress_type
, complevel
));
3447 } else if (compress_type
!= ZIO_COMPRESS_OFF
) {
3448 dbuf_set_data(db
, arc_alloc_compressed_buf(
3449 dn
->dn_objset
->os_spa
, db
, arc_buf_size(data
),
3450 arc_buf_lsize(data
), compress_type
, complevel
));
3452 dbuf_set_data(db
, arc_alloc_buf(dn
->dn_objset
->os_spa
, db
,
3453 DBUF_GET_BUFC_TYPE(db
), db
->db
.db_size
));
3456 rw_enter(&db
->db_rwlock
, RW_WRITER
);
3457 bcopy(data
->b_data
, db
->db
.db_data
, arc_buf_size(data
));
3458 rw_exit(&db
->db_rwlock
);
3462 * Returns with db_holds incremented, and db_mtx not held.
3463 * Note: dn_struct_rwlock must be held.
3466 dbuf_hold_impl(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
3467 boolean_t fail_sparse
, boolean_t fail_uncached
,
3468 void *tag
, dmu_buf_impl_t
**dbp
)
3470 dmu_buf_impl_t
*db
, *parent
= NULL
;
3472 /* If the pool has been created, verify the tx_sync_lock is not held */
3473 spa_t
*spa
= dn
->dn_objset
->os_spa
;
3474 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
3476 ASSERT(!MUTEX_HELD(&dp
->dp_tx
.tx_sync_lock
));
3479 ASSERT(blkid
!= DMU_BONUS_BLKID
);
3480 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
3481 ASSERT3U(dn
->dn_nlevels
, >, level
);
3485 /* dbuf_find() returns with db_mtx held */
3486 db
= dbuf_find(dn
->dn_objset
, dn
->dn_object
, level
, blkid
);
3489 blkptr_t
*bp
= NULL
;
3493 return (SET_ERROR(ENOENT
));
3495 ASSERT3P(parent
, ==, NULL
);
3496 err
= dbuf_findbp(dn
, level
, blkid
, fail_sparse
, &parent
, &bp
);
3498 if (err
== 0 && bp
&& BP_IS_HOLE(bp
))
3499 err
= SET_ERROR(ENOENT
);
3502 dbuf_rele(parent
, NULL
);
3506 if (err
&& err
!= ENOENT
)
3508 db
= dbuf_create(dn
, level
, blkid
, parent
, bp
);
3511 if (fail_uncached
&& db
->db_state
!= DB_CACHED
) {
3512 mutex_exit(&db
->db_mtx
);
3513 return (SET_ERROR(ENOENT
));
3516 if (db
->db_buf
!= NULL
) {
3517 arc_buf_access(db
->db_buf
);
3518 ASSERT3P(db
->db
.db_data
, ==, db
->db_buf
->b_data
);
3521 ASSERT(db
->db_buf
== NULL
|| arc_referenced(db
->db_buf
));
3524 * If this buffer is currently syncing out, and we are
3525 * still referencing it from db_data, we need to make a copy
3526 * of it in case we decide we want to dirty it again in this txg.
3528 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
3529 dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
3530 db
->db_state
== DB_CACHED
&& db
->db_data_pending
) {
3531 dbuf_dirty_record_t
*dr
= db
->db_data_pending
;
3532 if (dr
->dt
.dl
.dr_data
== db
->db_buf
)
3533 dbuf_hold_copy(dn
, db
);
3536 if (multilist_link_active(&db
->db_cache_link
)) {
3537 ASSERT(zfs_refcount_is_zero(&db
->db_holds
));
3538 ASSERT(db
->db_caching_status
== DB_DBUF_CACHE
||
3539 db
->db_caching_status
== DB_DBUF_METADATA_CACHE
);
3541 multilist_remove(&dbuf_caches
[db
->db_caching_status
].cache
, db
);
3542 (void) zfs_refcount_remove_many(
3543 &dbuf_caches
[db
->db_caching_status
].size
,
3544 db
->db
.db_size
, db
);
3546 if (db
->db_caching_status
== DB_DBUF_METADATA_CACHE
) {
3547 DBUF_STAT_BUMPDOWN(metadata_cache_count
);
3549 DBUF_STAT_BUMPDOWN(cache_levels
[db
->db_level
]);
3550 DBUF_STAT_BUMPDOWN(cache_count
);
3551 DBUF_STAT_DECR(cache_levels_bytes
[db
->db_level
],
3554 db
->db_caching_status
= DB_NO_CACHE
;
3556 (void) zfs_refcount_add(&db
->db_holds
, tag
);
3558 mutex_exit(&db
->db_mtx
);
3560 /* NOTE: we can't rele the parent until after we drop the db_mtx */
3562 dbuf_rele(parent
, NULL
);
3564 ASSERT3P(DB_DNODE(db
), ==, dn
);
3565 ASSERT3U(db
->db_blkid
, ==, blkid
);
3566 ASSERT3U(db
->db_level
, ==, level
);
3573 dbuf_hold(dnode_t
*dn
, uint64_t blkid
, void *tag
)
3575 return (dbuf_hold_level(dn
, 0, blkid
, tag
));
3579 dbuf_hold_level(dnode_t
*dn
, int level
, uint64_t blkid
, void *tag
)
3582 int err
= dbuf_hold_impl(dn
, level
, blkid
, FALSE
, FALSE
, tag
, &db
);
3583 return (err
? NULL
: db
);
3587 dbuf_create_bonus(dnode_t
*dn
)
3589 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
3591 ASSERT(dn
->dn_bonus
== NULL
);
3592 dn
->dn_bonus
= dbuf_create(dn
, 0, DMU_BONUS_BLKID
, dn
->dn_dbuf
, NULL
);
3596 dbuf_spill_set_blksz(dmu_buf_t
*db_fake
, uint64_t blksz
, dmu_tx_t
*tx
)
3598 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3600 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
3601 return (SET_ERROR(ENOTSUP
));
3603 blksz
= SPA_MINBLOCKSIZE
;
3604 ASSERT3U(blksz
, <=, spa_maxblocksize(dmu_objset_spa(db
->db_objset
)));
3605 blksz
= P2ROUNDUP(blksz
, SPA_MINBLOCKSIZE
);
3607 dbuf_new_size(db
, blksz
, tx
);
3613 dbuf_rm_spill(dnode_t
*dn
, dmu_tx_t
*tx
)
3615 dbuf_free_range(dn
, DMU_SPILL_BLKID
, DMU_SPILL_BLKID
, tx
);
3618 #pragma weak dmu_buf_add_ref = dbuf_add_ref
3620 dbuf_add_ref(dmu_buf_impl_t
*db
, void *tag
)
3622 int64_t holds
= zfs_refcount_add(&db
->db_holds
, tag
);
3623 VERIFY3S(holds
, >, 1);
3626 #pragma weak dmu_buf_try_add_ref = dbuf_try_add_ref
3628 dbuf_try_add_ref(dmu_buf_t
*db_fake
, objset_t
*os
, uint64_t obj
, uint64_t blkid
,
3631 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3632 dmu_buf_impl_t
*found_db
;
3633 boolean_t result
= B_FALSE
;
3635 if (blkid
== DMU_BONUS_BLKID
)
3636 found_db
= dbuf_find_bonus(os
, obj
);
3638 found_db
= dbuf_find(os
, obj
, 0, blkid
);
3640 if (found_db
!= NULL
) {
3641 if (db
== found_db
&& dbuf_refcount(db
) > db
->db_dirtycnt
) {
3642 (void) zfs_refcount_add(&db
->db_holds
, tag
);
3645 mutex_exit(&found_db
->db_mtx
);
3651 * If you call dbuf_rele() you had better not be referencing the dnode handle
3652 * unless you have some other direct or indirect hold on the dnode. (An indirect
3653 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
3654 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
3655 * dnode's parent dbuf evicting its dnode handles.
3658 dbuf_rele(dmu_buf_impl_t
*db
, void *tag
)
3660 mutex_enter(&db
->db_mtx
);
3661 dbuf_rele_and_unlock(db
, tag
, B_FALSE
);
3665 dmu_buf_rele(dmu_buf_t
*db
, void *tag
)
3667 dbuf_rele((dmu_buf_impl_t
*)db
, tag
);
3671 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
3672 * db_dirtycnt and db_holds to be updated atomically. The 'evicting'
3673 * argument should be set if we are already in the dbuf-evicting code
3674 * path, in which case we don't want to recursively evict. This allows us to
3675 * avoid deeply nested stacks that would have a call flow similar to this:
3677 * dbuf_rele()-->dbuf_rele_and_unlock()-->dbuf_evict_notify()
3680 * +-----dbuf_destroy()<--dbuf_evict_one()<--------+
3684 dbuf_rele_and_unlock(dmu_buf_impl_t
*db
, void *tag
, boolean_t evicting
)
3689 ASSERT(MUTEX_HELD(&db
->db_mtx
));
3693 * Remove the reference to the dbuf before removing its hold on the
3694 * dnode so we can guarantee in dnode_move() that a referenced bonus
3695 * buffer has a corresponding dnode hold.
3697 holds
= zfs_refcount_remove(&db
->db_holds
, tag
);
3701 * We can't freeze indirects if there is a possibility that they
3702 * may be modified in the current syncing context.
3704 if (db
->db_buf
!= NULL
&&
3705 holds
== (db
->db_level
== 0 ? db
->db_dirtycnt
: 0)) {
3706 arc_buf_freeze(db
->db_buf
);
3709 if (holds
== db
->db_dirtycnt
&&
3710 db
->db_level
== 0 && db
->db_user_immediate_evict
)
3711 dbuf_evict_user(db
);
3714 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
3716 boolean_t evict_dbuf
= db
->db_pending_evict
;
3719 * If the dnode moves here, we cannot cross this
3720 * barrier until the move completes.
3725 atomic_dec_32(&dn
->dn_dbufs_count
);
3728 * Decrementing the dbuf count means that the bonus
3729 * buffer's dnode hold is no longer discounted in
3730 * dnode_move(). The dnode cannot move until after
3731 * the dnode_rele() below.
3736 * Do not reference db after its lock is dropped.
3737 * Another thread may evict it.
3739 mutex_exit(&db
->db_mtx
);
3742 dnode_evict_bonus(dn
);
3745 } else if (db
->db_buf
== NULL
) {
3747 * This is a special case: we never associated this
3748 * dbuf with any data allocated from the ARC.
3750 ASSERT(db
->db_state
== DB_UNCACHED
||
3751 db
->db_state
== DB_NOFILL
);
3753 } else if (arc_released(db
->db_buf
)) {
3755 * This dbuf has anonymous data associated with it.
3759 boolean_t do_arc_evict
= B_FALSE
;
3761 spa_t
*spa
= dmu_objset_spa(db
->db_objset
);
3763 if (!DBUF_IS_CACHEABLE(db
) &&
3764 db
->db_blkptr
!= NULL
&&
3765 !BP_IS_HOLE(db
->db_blkptr
) &&
3766 !BP_IS_EMBEDDED(db
->db_blkptr
)) {
3767 do_arc_evict
= B_TRUE
;
3768 bp
= *db
->db_blkptr
;
3771 if (!DBUF_IS_CACHEABLE(db
) ||
3772 db
->db_pending_evict
) {
3774 } else if (!multilist_link_active(&db
->db_cache_link
)) {
3775 ASSERT3U(db
->db_caching_status
, ==,
3778 dbuf_cached_state_t dcs
=
3779 dbuf_include_in_metadata_cache(db
) ?
3780 DB_DBUF_METADATA_CACHE
: DB_DBUF_CACHE
;
3781 db
->db_caching_status
= dcs
;
3783 multilist_insert(&dbuf_caches
[dcs
].cache
, db
);
3784 uint64_t db_size
= db
->db
.db_size
;
3785 size
= zfs_refcount_add_many(
3786 &dbuf_caches
[dcs
].size
, db_size
, db
);
3787 uint8_t db_level
= db
->db_level
;
3788 mutex_exit(&db
->db_mtx
);
3790 if (dcs
== DB_DBUF_METADATA_CACHE
) {
3791 DBUF_STAT_BUMP(metadata_cache_count
);
3793 metadata_cache_size_bytes_max
,
3796 DBUF_STAT_BUMP(cache_count
);
3797 DBUF_STAT_MAX(cache_size_bytes_max
,
3799 DBUF_STAT_BUMP(cache_levels
[db_level
]);
3801 cache_levels_bytes
[db_level
],
3805 if (dcs
== DB_DBUF_CACHE
&& !evicting
)
3806 dbuf_evict_notify(size
);
3810 arc_freed(spa
, &bp
);
3813 mutex_exit(&db
->db_mtx
);
3818 #pragma weak dmu_buf_refcount = dbuf_refcount
3820 dbuf_refcount(dmu_buf_impl_t
*db
)
3822 return (zfs_refcount_count(&db
->db_holds
));
3826 dmu_buf_user_refcount(dmu_buf_t
*db_fake
)
3829 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3831 mutex_enter(&db
->db_mtx
);
3832 ASSERT3U(zfs_refcount_count(&db
->db_holds
), >=, db
->db_dirtycnt
);
3833 holds
= zfs_refcount_count(&db
->db_holds
) - db
->db_dirtycnt
;
3834 mutex_exit(&db
->db_mtx
);
3840 dmu_buf_replace_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*old_user
,
3841 dmu_buf_user_t
*new_user
)
3843 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3845 mutex_enter(&db
->db_mtx
);
3846 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
3847 if (db
->db_user
== old_user
)
3848 db
->db_user
= new_user
;
3850 old_user
= db
->db_user
;
3851 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
3852 mutex_exit(&db
->db_mtx
);
3858 dmu_buf_set_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
3860 return (dmu_buf_replace_user(db_fake
, NULL
, user
));
3864 dmu_buf_set_user_ie(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
3866 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3868 db
->db_user_immediate_evict
= TRUE
;
3869 return (dmu_buf_set_user(db_fake
, user
));
3873 dmu_buf_remove_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
3875 return (dmu_buf_replace_user(db_fake
, user
, NULL
));
3879 dmu_buf_get_user(dmu_buf_t
*db_fake
)
3881 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3883 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
3884 return (db
->db_user
);
3888 dmu_buf_user_evict_wait()
3890 taskq_wait(dbu_evict_taskq
);
3894 dmu_buf_get_blkptr(dmu_buf_t
*db
)
3896 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3897 return (dbi
->db_blkptr
);
3901 dmu_buf_get_objset(dmu_buf_t
*db
)
3903 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3904 return (dbi
->db_objset
);
3908 dmu_buf_dnode_enter(dmu_buf_t
*db
)
3910 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3911 DB_DNODE_ENTER(dbi
);
3912 return (DB_DNODE(dbi
));
3916 dmu_buf_dnode_exit(dmu_buf_t
*db
)
3918 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3923 dbuf_check_blkptr(dnode_t
*dn
, dmu_buf_impl_t
*db
)
3925 /* ASSERT(dmu_tx_is_syncing(tx) */
3926 ASSERT(MUTEX_HELD(&db
->db_mtx
));
3928 if (db
->db_blkptr
!= NULL
)
3931 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3932 db
->db_blkptr
= DN_SPILL_BLKPTR(dn
->dn_phys
);
3933 BP_ZERO(db
->db_blkptr
);
3936 if (db
->db_level
== dn
->dn_phys
->dn_nlevels
-1) {
3938 * This buffer was allocated at a time when there was
3939 * no available blkptrs from the dnode, or it was
3940 * inappropriate to hook it in (i.e., nlevels mismatch).
3942 ASSERT(db
->db_blkid
< dn
->dn_phys
->dn_nblkptr
);
3943 ASSERT(db
->db_parent
== NULL
);
3944 db
->db_parent
= dn
->dn_dbuf
;
3945 db
->db_blkptr
= &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
];
3948 dmu_buf_impl_t
*parent
= db
->db_parent
;
3949 int epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
3951 ASSERT(dn
->dn_phys
->dn_nlevels
> 1);
3952 if (parent
== NULL
) {
3953 mutex_exit(&db
->db_mtx
);
3954 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
3955 parent
= dbuf_hold_level(dn
, db
->db_level
+ 1,
3956 db
->db_blkid
>> epbs
, db
);
3957 rw_exit(&dn
->dn_struct_rwlock
);
3958 mutex_enter(&db
->db_mtx
);
3959 db
->db_parent
= parent
;
3961 db
->db_blkptr
= (blkptr_t
*)parent
->db
.db_data
+
3962 (db
->db_blkid
& ((1ULL << epbs
) - 1));
3968 dbuf_sync_bonus(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
3970 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3971 void *data
= dr
->dt
.dl
.dr_data
;
3973 ASSERT0(db
->db_level
);
3974 ASSERT(MUTEX_HELD(&db
->db_mtx
));
3975 ASSERT(db
->db_blkid
== DMU_BONUS_BLKID
);
3976 ASSERT(data
!= NULL
);
3978 dnode_t
*dn
= dr
->dr_dnode
;
3979 ASSERT3U(DN_MAX_BONUS_LEN(dn
->dn_phys
), <=,
3980 DN_SLOTS_TO_BONUSLEN(dn
->dn_phys
->dn_extra_slots
+ 1));
3981 bcopy(data
, DN_BONUS(dn
->dn_phys
), DN_MAX_BONUS_LEN(dn
->dn_phys
));
3983 dbuf_sync_leaf_verify_bonus_dnode(dr
);
3985 dbuf_undirty_bonus(dr
);
3986 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)tx
->tx_txg
, B_FALSE
);
3990 * When syncing out a blocks of dnodes, adjust the block to deal with
3991 * encryption. Normally, we make sure the block is decrypted before writing
3992 * it. If we have crypt params, then we are writing a raw (encrypted) block,
3993 * from a raw receive. In this case, set the ARC buf's crypt params so
3994 * that the BP will be filled with the correct byteorder, salt, iv, and mac.
3997 dbuf_prepare_encrypted_dnode_leaf(dbuf_dirty_record_t
*dr
)
4000 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
4002 ASSERT(MUTEX_HELD(&db
->db_mtx
));
4003 ASSERT3U(db
->db
.db_object
, ==, DMU_META_DNODE_OBJECT
);
4004 ASSERT3U(db
->db_level
, ==, 0);
4006 if (!db
->db_objset
->os_raw_receive
&& arc_is_encrypted(db
->db_buf
)) {
4007 zbookmark_phys_t zb
;
4010 * Unfortunately, there is currently no mechanism for
4011 * syncing context to handle decryption errors. An error
4012 * here is only possible if an attacker maliciously
4013 * changed a dnode block and updated the associated
4014 * checksums going up the block tree.
4016 SET_BOOKMARK(&zb
, dmu_objset_id(db
->db_objset
),
4017 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
4018 err
= arc_untransform(db
->db_buf
, db
->db_objset
->os_spa
,
4021 panic("Invalid dnode block MAC");
4022 } else if (dr
->dt
.dl
.dr_has_raw_params
) {
4023 (void) arc_release(dr
->dt
.dl
.dr_data
, db
);
4024 arc_convert_to_raw(dr
->dt
.dl
.dr_data
,
4025 dmu_objset_id(db
->db_objset
),
4026 dr
->dt
.dl
.dr_byteorder
, DMU_OT_DNODE
,
4027 dr
->dt
.dl
.dr_salt
, dr
->dt
.dl
.dr_iv
, dr
->dt
.dl
.dr_mac
);
4032 * dbuf_sync_indirect() is called recursively from dbuf_sync_list() so it
4033 * is critical the we not allow the compiler to inline this function in to
4034 * dbuf_sync_list() thereby drastically bloating the stack usage.
4036 noinline
static void
4037 dbuf_sync_indirect(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
4039 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
4040 dnode_t
*dn
= dr
->dr_dnode
;
4042 ASSERT(dmu_tx_is_syncing(tx
));
4044 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
4046 mutex_enter(&db
->db_mtx
);
4048 ASSERT(db
->db_level
> 0);
4051 /* Read the block if it hasn't been read yet. */
4052 if (db
->db_buf
== NULL
) {
4053 mutex_exit(&db
->db_mtx
);
4054 (void) dbuf_read(db
, NULL
, DB_RF_MUST_SUCCEED
);
4055 mutex_enter(&db
->db_mtx
);
4057 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
4058 ASSERT(db
->db_buf
!= NULL
);
4060 /* Indirect block size must match what the dnode thinks it is. */
4061 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
4062 dbuf_check_blkptr(dn
, db
);
4064 /* Provide the pending dirty record to child dbufs */
4065 db
->db_data_pending
= dr
;
4067 mutex_exit(&db
->db_mtx
);
4069 dbuf_write(dr
, db
->db_buf
, tx
);
4071 zio_t
*zio
= dr
->dr_zio
;
4072 mutex_enter(&dr
->dt
.di
.dr_mtx
);
4073 dbuf_sync_list(&dr
->dt
.di
.dr_children
, db
->db_level
- 1, tx
);
4074 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
4075 mutex_exit(&dr
->dt
.di
.dr_mtx
);
4080 * Verify that the size of the data in our bonus buffer does not exceed
4081 * its recorded size.
4083 * The purpose of this verification is to catch any cases in development
4084 * where the size of a phys structure (i.e space_map_phys_t) grows and,
4085 * due to incorrect feature management, older pools expect to read more
4086 * data even though they didn't actually write it to begin with.
4088 * For a example, this would catch an error in the feature logic where we
4089 * open an older pool and we expect to write the space map histogram of
4090 * a space map with size SPACE_MAP_SIZE_V0.
4093 dbuf_sync_leaf_verify_bonus_dnode(dbuf_dirty_record_t
*dr
)
4096 dnode_t
*dn
= dr
->dr_dnode
;
4099 * Encrypted bonus buffers can have data past their bonuslen.
4100 * Skip the verification of these blocks.
4102 if (DMU_OT_IS_ENCRYPTED(dn
->dn_bonustype
))
4105 uint16_t bonuslen
= dn
->dn_phys
->dn_bonuslen
;
4106 uint16_t maxbonuslen
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
);
4107 ASSERT3U(bonuslen
, <=, maxbonuslen
);
4109 arc_buf_t
*datap
= dr
->dt
.dl
.dr_data
;
4110 char *datap_end
= ((char *)datap
) + bonuslen
;
4111 char *datap_max
= ((char *)datap
) + maxbonuslen
;
4113 /* ensure that everything is zero after our data */
4114 for (; datap_end
< datap_max
; datap_end
++)
4115 ASSERT(*datap_end
== 0);
4120 dbuf_lightweight_bp(dbuf_dirty_record_t
*dr
)
4122 /* This must be a lightweight dirty record. */
4123 ASSERT3P(dr
->dr_dbuf
, ==, NULL
);
4124 dnode_t
*dn
= dr
->dr_dnode
;
4126 if (dn
->dn_phys
->dn_nlevels
== 1) {
4127 VERIFY3U(dr
->dt
.dll
.dr_blkid
, <, dn
->dn_phys
->dn_nblkptr
);
4128 return (&dn
->dn_phys
->dn_blkptr
[dr
->dt
.dll
.dr_blkid
]);
4130 dmu_buf_impl_t
*parent_db
= dr
->dr_parent
->dr_dbuf
;
4131 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
4132 VERIFY3U(parent_db
->db_level
, ==, 1);
4133 VERIFY3P(parent_db
->db_dnode_handle
->dnh_dnode
, ==, dn
);
4134 VERIFY3U(dr
->dt
.dll
.dr_blkid
>> epbs
, ==, parent_db
->db_blkid
);
4135 blkptr_t
*bp
= parent_db
->db
.db_data
;
4136 return (&bp
[dr
->dt
.dll
.dr_blkid
& ((1 << epbs
) - 1)]);
4141 dbuf_lightweight_ready(zio_t
*zio
)
4143 dbuf_dirty_record_t
*dr
= zio
->io_private
;
4144 blkptr_t
*bp
= zio
->io_bp
;
4146 if (zio
->io_error
!= 0)
4149 dnode_t
*dn
= dr
->dr_dnode
;
4151 blkptr_t
*bp_orig
= dbuf_lightweight_bp(dr
);
4152 spa_t
*spa
= dmu_objset_spa(dn
->dn_objset
);
4153 int64_t delta
= bp_get_dsize_sync(spa
, bp
) -
4154 bp_get_dsize_sync(spa
, bp_orig
);
4155 dnode_diduse_space(dn
, delta
);
4157 uint64_t blkid
= dr
->dt
.dll
.dr_blkid
;
4158 mutex_enter(&dn
->dn_mtx
);
4159 if (blkid
> dn
->dn_phys
->dn_maxblkid
) {
4160 ASSERT0(dn
->dn_objset
->os_raw_receive
);
4161 dn
->dn_phys
->dn_maxblkid
= blkid
;
4163 mutex_exit(&dn
->dn_mtx
);
4165 if (!BP_IS_EMBEDDED(bp
)) {
4166 uint64_t fill
= BP_IS_HOLE(bp
) ? 0 : 1;
4167 BP_SET_FILL(bp
, fill
);
4170 dmu_buf_impl_t
*parent_db
;
4171 EQUIV(dr
->dr_parent
== NULL
, dn
->dn_phys
->dn_nlevels
== 1);
4172 if (dr
->dr_parent
== NULL
) {
4173 parent_db
= dn
->dn_dbuf
;
4175 parent_db
= dr
->dr_parent
->dr_dbuf
;
4177 rw_enter(&parent_db
->db_rwlock
, RW_WRITER
);
4179 rw_exit(&parent_db
->db_rwlock
);
4183 dbuf_lightweight_physdone(zio_t
*zio
)
4185 dbuf_dirty_record_t
*dr
= zio
->io_private
;
4186 dsl_pool_t
*dp
= spa_get_dsl(zio
->io_spa
);
4187 ASSERT3U(dr
->dr_txg
, ==, zio
->io_txg
);
4190 * The callback will be called io_phys_children times. Retire one
4191 * portion of our dirty space each time we are called. Any rounding
4192 * error will be cleaned up by dbuf_lightweight_done().
4194 int delta
= dr
->dr_accounted
/ zio
->io_phys_children
;
4195 dsl_pool_undirty_space(dp
, delta
, zio
->io_txg
);
4199 dbuf_lightweight_done(zio_t
*zio
)
4201 dbuf_dirty_record_t
*dr
= zio
->io_private
;
4203 VERIFY0(zio
->io_error
);
4205 objset_t
*os
= dr
->dr_dnode
->dn_objset
;
4206 dmu_tx_t
*tx
= os
->os_synctx
;
4208 if (zio
->io_flags
& (ZIO_FLAG_IO_REWRITE
| ZIO_FLAG_NOPWRITE
)) {
4209 ASSERT(BP_EQUAL(zio
->io_bp
, &zio
->io_bp_orig
));
4211 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
4212 (void) dsl_dataset_block_kill(ds
, &zio
->io_bp_orig
, tx
, B_TRUE
);
4213 dsl_dataset_block_born(ds
, zio
->io_bp
, tx
);
4217 * See comment in dbuf_write_done().
4219 if (zio
->io_phys_children
== 0) {
4220 dsl_pool_undirty_space(dmu_objset_pool(os
),
4221 dr
->dr_accounted
, zio
->io_txg
);
4223 dsl_pool_undirty_space(dmu_objset_pool(os
),
4224 dr
->dr_accounted
% zio
->io_phys_children
, zio
->io_txg
);
4227 abd_free(dr
->dt
.dll
.dr_abd
);
4228 kmem_free(dr
, sizeof (*dr
));
4231 noinline
static void
4232 dbuf_sync_lightweight(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
4234 dnode_t
*dn
= dr
->dr_dnode
;
4236 if (dn
->dn_phys
->dn_nlevels
== 1) {
4239 pio
= dr
->dr_parent
->dr_zio
;
4242 zbookmark_phys_t zb
= {
4243 .zb_objset
= dmu_objset_id(dn
->dn_objset
),
4244 .zb_object
= dn
->dn_object
,
4246 .zb_blkid
= dr
->dt
.dll
.dr_blkid
,
4250 * See comment in dbuf_write(). This is so that zio->io_bp_orig
4251 * will have the old BP in dbuf_lightweight_done().
4253 dr
->dr_bp_copy
= *dbuf_lightweight_bp(dr
);
4255 dr
->dr_zio
= zio_write(pio
, dmu_objset_spa(dn
->dn_objset
),
4256 dmu_tx_get_txg(tx
), &dr
->dr_bp_copy
, dr
->dt
.dll
.dr_abd
,
4257 dn
->dn_datablksz
, abd_get_size(dr
->dt
.dll
.dr_abd
),
4258 &dr
->dt
.dll
.dr_props
, dbuf_lightweight_ready
, NULL
,
4259 dbuf_lightweight_physdone
, dbuf_lightweight_done
, dr
,
4260 ZIO_PRIORITY_ASYNC_WRITE
,
4261 ZIO_FLAG_MUSTSUCCEED
| dr
->dt
.dll
.dr_flags
, &zb
);
4263 zio_nowait(dr
->dr_zio
);
4267 * dbuf_sync_leaf() is called recursively from dbuf_sync_list() so it is
4268 * critical the we not allow the compiler to inline this function in to
4269 * dbuf_sync_list() thereby drastically bloating the stack usage.
4271 noinline
static void
4272 dbuf_sync_leaf(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
4274 arc_buf_t
**datap
= &dr
->dt
.dl
.dr_data
;
4275 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
4276 dnode_t
*dn
= dr
->dr_dnode
;
4278 uint64_t txg
= tx
->tx_txg
;
4280 ASSERT(dmu_tx_is_syncing(tx
));
4282 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
4284 mutex_enter(&db
->db_mtx
);
4286 * To be synced, we must be dirtied. But we
4287 * might have been freed after the dirty.
4289 if (db
->db_state
== DB_UNCACHED
) {
4290 /* This buffer has been freed since it was dirtied */
4291 ASSERT(db
->db
.db_data
== NULL
);
4292 } else if (db
->db_state
== DB_FILL
) {
4293 /* This buffer was freed and is now being re-filled */
4294 ASSERT(db
->db
.db_data
!= dr
->dt
.dl
.dr_data
);
4296 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_NOFILL
);
4300 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
4301 mutex_enter(&dn
->dn_mtx
);
4302 if (!(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
)) {
4304 * In the previous transaction group, the bonus buffer
4305 * was entirely used to store the attributes for the
4306 * dnode which overrode the dn_spill field. However,
4307 * when adding more attributes to the file a spill
4308 * block was required to hold the extra attributes.
4310 * Make sure to clear the garbage left in the dn_spill
4311 * field from the previous attributes in the bonus
4312 * buffer. Otherwise, after writing out the spill
4313 * block to the new allocated dva, it will free
4314 * the old block pointed to by the invalid dn_spill.
4316 db
->db_blkptr
= NULL
;
4318 dn
->dn_phys
->dn_flags
|= DNODE_FLAG_SPILL_BLKPTR
;
4319 mutex_exit(&dn
->dn_mtx
);
4323 * If this is a bonus buffer, simply copy the bonus data into the
4324 * dnode. It will be written out when the dnode is synced (and it
4325 * will be synced, since it must have been dirty for dbuf_sync to
4328 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
4329 ASSERT(dr
->dr_dbuf
== db
);
4330 dbuf_sync_bonus(dr
, tx
);
4337 * This function may have dropped the db_mtx lock allowing a dmu_sync
4338 * operation to sneak in. As a result, we need to ensure that we
4339 * don't check the dr_override_state until we have returned from
4340 * dbuf_check_blkptr.
4342 dbuf_check_blkptr(dn
, db
);
4345 * If this buffer is in the middle of an immediate write,
4346 * wait for the synchronous IO to complete.
4348 while (dr
->dt
.dl
.dr_override_state
== DR_IN_DMU_SYNC
) {
4349 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
4350 cv_wait(&db
->db_changed
, &db
->db_mtx
);
4351 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_NOT_OVERRIDDEN
);
4355 * If this is a dnode block, ensure it is appropriately encrypted
4356 * or decrypted, depending on what we are writing to it this txg.
4358 if (os
->os_encrypted
&& dn
->dn_object
== DMU_META_DNODE_OBJECT
)
4359 dbuf_prepare_encrypted_dnode_leaf(dr
);
4361 if (db
->db_state
!= DB_NOFILL
&&
4362 dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
4363 zfs_refcount_count(&db
->db_holds
) > 1 &&
4364 dr
->dt
.dl
.dr_override_state
!= DR_OVERRIDDEN
&&
4365 *datap
== db
->db_buf
) {
4367 * If this buffer is currently "in use" (i.e., there
4368 * are active holds and db_data still references it),
4369 * then make a copy before we start the write so that
4370 * any modifications from the open txg will not leak
4373 * NOTE: this copy does not need to be made for
4374 * objects only modified in the syncing context (e.g.
4375 * DNONE_DNODE blocks).
4377 int psize
= arc_buf_size(*datap
);
4378 int lsize
= arc_buf_lsize(*datap
);
4379 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
4380 enum zio_compress compress_type
= arc_get_compression(*datap
);
4381 uint8_t complevel
= arc_get_complevel(*datap
);
4383 if (arc_is_encrypted(*datap
)) {
4384 boolean_t byteorder
;
4385 uint8_t salt
[ZIO_DATA_SALT_LEN
];
4386 uint8_t iv
[ZIO_DATA_IV_LEN
];
4387 uint8_t mac
[ZIO_DATA_MAC_LEN
];
4389 arc_get_raw_params(*datap
, &byteorder
, salt
, iv
, mac
);
4390 *datap
= arc_alloc_raw_buf(os
->os_spa
, db
,
4391 dmu_objset_id(os
), byteorder
, salt
, iv
, mac
,
4392 dn
->dn_type
, psize
, lsize
, compress_type
,
4394 } else if (compress_type
!= ZIO_COMPRESS_OFF
) {
4395 ASSERT3U(type
, ==, ARC_BUFC_DATA
);
4396 *datap
= arc_alloc_compressed_buf(os
->os_spa
, db
,
4397 psize
, lsize
, compress_type
, complevel
);
4399 *datap
= arc_alloc_buf(os
->os_spa
, db
, type
, psize
);
4401 bcopy(db
->db
.db_data
, (*datap
)->b_data
, psize
);
4403 db
->db_data_pending
= dr
;
4405 mutex_exit(&db
->db_mtx
);
4407 dbuf_write(dr
, *datap
, tx
);
4409 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
4410 if (dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
4411 list_insert_tail(&dn
->dn_dirty_records
[txg
& TXG_MASK
], dr
);
4413 zio_nowait(dr
->dr_zio
);
4418 dbuf_sync_list(list_t
*list
, int level
, dmu_tx_t
*tx
)
4420 dbuf_dirty_record_t
*dr
;
4422 while ((dr
= list_head(list
))) {
4423 if (dr
->dr_zio
!= NULL
) {
4425 * If we find an already initialized zio then we
4426 * are processing the meta-dnode, and we have finished.
4427 * The dbufs for all dnodes are put back on the list
4428 * during processing, so that we can zio_wait()
4429 * these IOs after initiating all child IOs.
4431 ASSERT3U(dr
->dr_dbuf
->db
.db_object
, ==,
4432 DMU_META_DNODE_OBJECT
);
4435 list_remove(list
, dr
);
4436 if (dr
->dr_dbuf
== NULL
) {
4437 dbuf_sync_lightweight(dr
, tx
);
4439 if (dr
->dr_dbuf
->db_blkid
!= DMU_BONUS_BLKID
&&
4440 dr
->dr_dbuf
->db_blkid
!= DMU_SPILL_BLKID
) {
4441 VERIFY3U(dr
->dr_dbuf
->db_level
, ==, level
);
4443 if (dr
->dr_dbuf
->db_level
> 0)
4444 dbuf_sync_indirect(dr
, tx
);
4446 dbuf_sync_leaf(dr
, tx
);
4453 dbuf_write_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
4455 dmu_buf_impl_t
*db
= vdb
;
4457 blkptr_t
*bp
= zio
->io_bp
;
4458 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
4459 spa_t
*spa
= zio
->io_spa
;
4464 ASSERT3P(db
->db_blkptr
, !=, NULL
);
4465 ASSERT3P(&db
->db_data_pending
->dr_bp_copy
, ==, bp
);
4469 delta
= bp_get_dsize_sync(spa
, bp
) - bp_get_dsize_sync(spa
, bp_orig
);
4470 dnode_diduse_space(dn
, delta
- zio
->io_prev_space_delta
);
4471 zio
->io_prev_space_delta
= delta
;
4473 if (bp
->blk_birth
!= 0) {
4474 ASSERT((db
->db_blkid
!= DMU_SPILL_BLKID
&&
4475 BP_GET_TYPE(bp
) == dn
->dn_type
) ||
4476 (db
->db_blkid
== DMU_SPILL_BLKID
&&
4477 BP_GET_TYPE(bp
) == dn
->dn_bonustype
) ||
4478 BP_IS_EMBEDDED(bp
));
4479 ASSERT(BP_GET_LEVEL(bp
) == db
->db_level
);
4482 mutex_enter(&db
->db_mtx
);
4485 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
4486 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
4487 ASSERT(!(BP_IS_HOLE(bp
)) &&
4488 db
->db_blkptr
== DN_SPILL_BLKPTR(dn
->dn_phys
));
4492 if (db
->db_level
== 0) {
4493 mutex_enter(&dn
->dn_mtx
);
4494 if (db
->db_blkid
> dn
->dn_phys
->dn_maxblkid
&&
4495 db
->db_blkid
!= DMU_SPILL_BLKID
) {
4496 ASSERT0(db
->db_objset
->os_raw_receive
);
4497 dn
->dn_phys
->dn_maxblkid
= db
->db_blkid
;
4499 mutex_exit(&dn
->dn_mtx
);
4501 if (dn
->dn_type
== DMU_OT_DNODE
) {
4503 while (i
< db
->db
.db_size
) {
4505 (void *)(((char *)db
->db
.db_data
) + i
);
4507 i
+= DNODE_MIN_SIZE
;
4508 if (dnp
->dn_type
!= DMU_OT_NONE
) {
4510 i
+= dnp
->dn_extra_slots
*
4515 if (BP_IS_HOLE(bp
)) {
4522 blkptr_t
*ibp
= db
->db
.db_data
;
4523 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
4524 for (i
= db
->db
.db_size
>> SPA_BLKPTRSHIFT
; i
> 0; i
--, ibp
++) {
4525 if (BP_IS_HOLE(ibp
))
4527 fill
+= BP_GET_FILL(ibp
);
4532 if (!BP_IS_EMBEDDED(bp
))
4533 BP_SET_FILL(bp
, fill
);
4535 mutex_exit(&db
->db_mtx
);
4537 db_lock_type_t dblt
= dmu_buf_lock_parent(db
, RW_WRITER
, FTAG
);
4538 *db
->db_blkptr
= *bp
;
4539 dmu_buf_unlock_parent(db
, dblt
, FTAG
);
4544 * This function gets called just prior to running through the compression
4545 * stage of the zio pipeline. If we're an indirect block comprised of only
4546 * holes, then we want this indirect to be compressed away to a hole. In
4547 * order to do that we must zero out any information about the holes that
4548 * this indirect points to prior to before we try to compress it.
4551 dbuf_write_children_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
4553 dmu_buf_impl_t
*db
= vdb
;
4556 unsigned int epbs
, i
;
4558 ASSERT3U(db
->db_level
, >, 0);
4561 epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
4562 ASSERT3U(epbs
, <, 31);
4564 /* Determine if all our children are holes */
4565 for (i
= 0, bp
= db
->db
.db_data
; i
< 1ULL << epbs
; i
++, bp
++) {
4566 if (!BP_IS_HOLE(bp
))
4571 * If all the children are holes, then zero them all out so that
4572 * we may get compressed away.
4574 if (i
== 1ULL << epbs
) {
4576 * We only found holes. Grab the rwlock to prevent
4577 * anybody from reading the blocks we're about to
4580 rw_enter(&db
->db_rwlock
, RW_WRITER
);
4581 bzero(db
->db
.db_data
, db
->db
.db_size
);
4582 rw_exit(&db
->db_rwlock
);
4588 * The SPA will call this callback several times for each zio - once
4589 * for every physical child i/o (zio->io_phys_children times). This
4590 * allows the DMU to monitor the progress of each logical i/o. For example,
4591 * there may be 2 copies of an indirect block, or many fragments of a RAID-Z
4592 * block. There may be a long delay before all copies/fragments are completed,
4593 * so this callback allows us to retire dirty space gradually, as the physical
4598 dbuf_write_physdone(zio_t
*zio
, arc_buf_t
*buf
, void *arg
)
4600 dmu_buf_impl_t
*db
= arg
;
4601 objset_t
*os
= db
->db_objset
;
4602 dsl_pool_t
*dp
= dmu_objset_pool(os
);
4603 dbuf_dirty_record_t
*dr
;
4606 dr
= db
->db_data_pending
;
4607 ASSERT3U(dr
->dr_txg
, ==, zio
->io_txg
);
4610 * The callback will be called io_phys_children times. Retire one
4611 * portion of our dirty space each time we are called. Any rounding
4612 * error will be cleaned up by dbuf_write_done().
4614 delta
= dr
->dr_accounted
/ zio
->io_phys_children
;
4615 dsl_pool_undirty_space(dp
, delta
, zio
->io_txg
);
4620 dbuf_write_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
4622 dmu_buf_impl_t
*db
= vdb
;
4623 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
4624 blkptr_t
*bp
= db
->db_blkptr
;
4625 objset_t
*os
= db
->db_objset
;
4626 dmu_tx_t
*tx
= os
->os_synctx
;
4628 ASSERT0(zio
->io_error
);
4629 ASSERT(db
->db_blkptr
== bp
);
4632 * For nopwrites and rewrites we ensure that the bp matches our
4633 * original and bypass all the accounting.
4635 if (zio
->io_flags
& (ZIO_FLAG_IO_REWRITE
| ZIO_FLAG_NOPWRITE
)) {
4636 ASSERT(BP_EQUAL(bp
, bp_orig
));
4638 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
4639 (void) dsl_dataset_block_kill(ds
, bp_orig
, tx
, B_TRUE
);
4640 dsl_dataset_block_born(ds
, bp
, tx
);
4643 mutex_enter(&db
->db_mtx
);
4647 dbuf_dirty_record_t
*dr
= db
->db_data_pending
;
4648 dnode_t
*dn
= dr
->dr_dnode
;
4649 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
4650 ASSERT(dr
->dr_dbuf
== db
);
4651 ASSERT(list_next(&db
->db_dirty_records
, dr
) == NULL
);
4652 list_remove(&db
->db_dirty_records
, dr
);
4655 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
4656 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
4657 ASSERT(!(BP_IS_HOLE(db
->db_blkptr
)) &&
4658 db
->db_blkptr
== DN_SPILL_BLKPTR(dn
->dn_phys
));
4662 if (db
->db_level
== 0) {
4663 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
4664 ASSERT(dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
);
4665 if (db
->db_state
!= DB_NOFILL
) {
4666 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
4667 arc_buf_destroy(dr
->dt
.dl
.dr_data
, db
);
4670 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
4671 ASSERT3U(db
->db
.db_size
, ==, 1 << dn
->dn_phys
->dn_indblkshift
);
4672 if (!BP_IS_HOLE(db
->db_blkptr
)) {
4673 int epbs __maybe_unused
= dn
->dn_phys
->dn_indblkshift
-
4675 ASSERT3U(db
->db_blkid
, <=,
4676 dn
->dn_phys
->dn_maxblkid
>> (db
->db_level
* epbs
));
4677 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==,
4680 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
4681 list_destroy(&dr
->dt
.di
.dr_children
);
4684 cv_broadcast(&db
->db_changed
);
4685 ASSERT(db
->db_dirtycnt
> 0);
4686 db
->db_dirtycnt
-= 1;
4687 db
->db_data_pending
= NULL
;
4688 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)tx
->tx_txg
, B_FALSE
);
4691 * If we didn't do a physical write in this ZIO and we
4692 * still ended up here, it means that the space of the
4693 * dbuf that we just released (and undirtied) above hasn't
4694 * been marked as undirtied in the pool's accounting.
4696 * Thus, we undirty that space in the pool's view of the
4697 * world here. For physical writes this type of update
4698 * happens in dbuf_write_physdone().
4700 * If we did a physical write, cleanup any rounding errors
4701 * that came up due to writing multiple copies of a block
4702 * on disk [see dbuf_write_physdone()].
4704 if (zio
->io_phys_children
== 0) {
4705 dsl_pool_undirty_space(dmu_objset_pool(os
),
4706 dr
->dr_accounted
, zio
->io_txg
);
4708 dsl_pool_undirty_space(dmu_objset_pool(os
),
4709 dr
->dr_accounted
% zio
->io_phys_children
, zio
->io_txg
);
4712 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
4716 dbuf_write_nofill_ready(zio_t
*zio
)
4718 dbuf_write_ready(zio
, NULL
, zio
->io_private
);
4722 dbuf_write_nofill_done(zio_t
*zio
)
4724 dbuf_write_done(zio
, NULL
, zio
->io_private
);
4728 dbuf_write_override_ready(zio_t
*zio
)
4730 dbuf_dirty_record_t
*dr
= zio
->io_private
;
4731 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
4733 dbuf_write_ready(zio
, NULL
, db
);
4737 dbuf_write_override_done(zio_t
*zio
)
4739 dbuf_dirty_record_t
*dr
= zio
->io_private
;
4740 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
4741 blkptr_t
*obp
= &dr
->dt
.dl
.dr_overridden_by
;
4743 mutex_enter(&db
->db_mtx
);
4744 if (!BP_EQUAL(zio
->io_bp
, obp
)) {
4745 if (!BP_IS_HOLE(obp
))
4746 dsl_free(spa_get_dsl(zio
->io_spa
), zio
->io_txg
, obp
);
4747 arc_release(dr
->dt
.dl
.dr_data
, db
);
4749 mutex_exit(&db
->db_mtx
);
4751 dbuf_write_done(zio
, NULL
, db
);
4753 if (zio
->io_abd
!= NULL
)
4754 abd_free(zio
->io_abd
);
4757 typedef struct dbuf_remap_impl_callback_arg
{
4759 uint64_t drica_blk_birth
;
4761 } dbuf_remap_impl_callback_arg_t
;
4764 dbuf_remap_impl_callback(uint64_t vdev
, uint64_t offset
, uint64_t size
,
4767 dbuf_remap_impl_callback_arg_t
*drica
= arg
;
4768 objset_t
*os
= drica
->drica_os
;
4769 spa_t
*spa
= dmu_objset_spa(os
);
4770 dmu_tx_t
*tx
= drica
->drica_tx
;
4772 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa
)));
4774 if (os
== spa_meta_objset(spa
)) {
4775 spa_vdev_indirect_mark_obsolete(spa
, vdev
, offset
, size
, tx
);
4777 dsl_dataset_block_remapped(dmu_objset_ds(os
), vdev
, offset
,
4778 size
, drica
->drica_blk_birth
, tx
);
4783 dbuf_remap_impl(dnode_t
*dn
, blkptr_t
*bp
, krwlock_t
*rw
, dmu_tx_t
*tx
)
4785 blkptr_t bp_copy
= *bp
;
4786 spa_t
*spa
= dmu_objset_spa(dn
->dn_objset
);
4787 dbuf_remap_impl_callback_arg_t drica
;
4789 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa
)));
4791 drica
.drica_os
= dn
->dn_objset
;
4792 drica
.drica_blk_birth
= bp
->blk_birth
;
4793 drica
.drica_tx
= tx
;
4794 if (spa_remap_blkptr(spa
, &bp_copy
, dbuf_remap_impl_callback
,
4797 * If the blkptr being remapped is tracked by a livelist,
4798 * then we need to make sure the livelist reflects the update.
4799 * First, cancel out the old blkptr by appending a 'FREE'
4800 * entry. Next, add an 'ALLOC' to track the new version. This
4801 * way we avoid trying to free an inaccurate blkptr at delete.
4802 * Note that embedded blkptrs are not tracked in livelists.
4804 if (dn
->dn_objset
!= spa_meta_objset(spa
)) {
4805 dsl_dataset_t
*ds
= dmu_objset_ds(dn
->dn_objset
);
4806 if (dsl_deadlist_is_open(&ds
->ds_dir
->dd_livelist
) &&
4807 bp
->blk_birth
> ds
->ds_dir
->dd_origin_txg
) {
4808 ASSERT(!BP_IS_EMBEDDED(bp
));
4809 ASSERT(dsl_dir_is_clone(ds
->ds_dir
));
4810 ASSERT(spa_feature_is_enabled(spa
,
4811 SPA_FEATURE_LIVELIST
));
4812 bplist_append(&ds
->ds_dir
->dd_pending_frees
,
4814 bplist_append(&ds
->ds_dir
->dd_pending_allocs
,
4820 * The db_rwlock prevents dbuf_read_impl() from
4821 * dereferencing the BP while we are changing it. To
4822 * avoid lock contention, only grab it when we are actually
4826 rw_enter(rw
, RW_WRITER
);
4834 * Remap any existing BP's to concrete vdevs, if possible.
4837 dbuf_remap(dnode_t
*dn
, dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
4839 spa_t
*spa
= dmu_objset_spa(db
->db_objset
);
4840 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa
)));
4842 if (!spa_feature_is_active(spa
, SPA_FEATURE_DEVICE_REMOVAL
))
4845 if (db
->db_level
> 0) {
4846 blkptr_t
*bp
= db
->db
.db_data
;
4847 for (int i
= 0; i
< db
->db
.db_size
>> SPA_BLKPTRSHIFT
; i
++) {
4848 dbuf_remap_impl(dn
, &bp
[i
], &db
->db_rwlock
, tx
);
4850 } else if (db
->db
.db_object
== DMU_META_DNODE_OBJECT
) {
4851 dnode_phys_t
*dnp
= db
->db
.db_data
;
4852 ASSERT3U(db
->db_dnode_handle
->dnh_dnode
->dn_type
, ==,
4854 for (int i
= 0; i
< db
->db
.db_size
>> DNODE_SHIFT
;
4855 i
+= dnp
[i
].dn_extra_slots
+ 1) {
4856 for (int j
= 0; j
< dnp
[i
].dn_nblkptr
; j
++) {
4857 krwlock_t
*lock
= (dn
->dn_dbuf
== NULL
? NULL
:
4858 &dn
->dn_dbuf
->db_rwlock
);
4859 dbuf_remap_impl(dn
, &dnp
[i
].dn_blkptr
[j
], lock
,
4867 /* Issue I/O to commit a dirty buffer to disk. */
4869 dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
)
4871 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
4872 dnode_t
*dn
= dr
->dr_dnode
;
4874 dmu_buf_impl_t
*parent
= db
->db_parent
;
4875 uint64_t txg
= tx
->tx_txg
;
4876 zbookmark_phys_t zb
;
4878 zio_t
*pio
; /* parent I/O */
4881 ASSERT(dmu_tx_is_syncing(tx
));
4885 if (db
->db_state
!= DB_NOFILL
) {
4886 if (db
->db_level
> 0 || dn
->dn_type
== DMU_OT_DNODE
) {
4888 * Private object buffers are released here rather
4889 * than in dbuf_dirty() since they are only modified
4890 * in the syncing context and we don't want the
4891 * overhead of making multiple copies of the data.
4893 if (BP_IS_HOLE(db
->db_blkptr
)) {
4896 dbuf_release_bp(db
);
4898 dbuf_remap(dn
, db
, tx
);
4902 if (parent
!= dn
->dn_dbuf
) {
4903 /* Our parent is an indirect block. */
4904 /* We have a dirty parent that has been scheduled for write. */
4905 ASSERT(parent
&& parent
->db_data_pending
);
4906 /* Our parent's buffer is one level closer to the dnode. */
4907 ASSERT(db
->db_level
== parent
->db_level
-1);
4909 * We're about to modify our parent's db_data by modifying
4910 * our block pointer, so the parent must be released.
4912 ASSERT(arc_released(parent
->db_buf
));
4913 pio
= parent
->db_data_pending
->dr_zio
;
4915 /* Our parent is the dnode itself. */
4916 ASSERT((db
->db_level
== dn
->dn_phys
->dn_nlevels
-1 &&
4917 db
->db_blkid
!= DMU_SPILL_BLKID
) ||
4918 (db
->db_blkid
== DMU_SPILL_BLKID
&& db
->db_level
== 0));
4919 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
4920 ASSERT3P(db
->db_blkptr
, ==,
4921 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
4925 ASSERT(db
->db_level
== 0 || data
== db
->db_buf
);
4926 ASSERT3U(db
->db_blkptr
->blk_birth
, <=, txg
);
4929 SET_BOOKMARK(&zb
, os
->os_dsl_dataset
?
4930 os
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
4931 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
4933 if (db
->db_blkid
== DMU_SPILL_BLKID
)
4935 wp_flag
|= (db
->db_state
== DB_NOFILL
) ? WP_NOFILL
: 0;
4937 dmu_write_policy(os
, dn
, db
->db_level
, wp_flag
, &zp
);
4940 * We copy the blkptr now (rather than when we instantiate the dirty
4941 * record), because its value can change between open context and
4942 * syncing context. We do not need to hold dn_struct_rwlock to read
4943 * db_blkptr because we are in syncing context.
4945 dr
->dr_bp_copy
= *db
->db_blkptr
;
4947 if (db
->db_level
== 0 &&
4948 dr
->dt
.dl
.dr_override_state
== DR_OVERRIDDEN
) {
4950 * The BP for this block has been provided by open context
4951 * (by dmu_sync() or dmu_buf_write_embedded()).
4953 abd_t
*contents
= (data
!= NULL
) ?
4954 abd_get_from_buf(data
->b_data
, arc_buf_size(data
)) : NULL
;
4956 dr
->dr_zio
= zio_write(pio
, os
->os_spa
, txg
, &dr
->dr_bp_copy
,
4957 contents
, db
->db
.db_size
, db
->db
.db_size
, &zp
,
4958 dbuf_write_override_ready
, NULL
, NULL
,
4959 dbuf_write_override_done
,
4960 dr
, ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
4961 mutex_enter(&db
->db_mtx
);
4962 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
4963 zio_write_override(dr
->dr_zio
, &dr
->dt
.dl
.dr_overridden_by
,
4964 dr
->dt
.dl
.dr_copies
, dr
->dt
.dl
.dr_nopwrite
);
4965 mutex_exit(&db
->db_mtx
);
4966 } else if (db
->db_state
== DB_NOFILL
) {
4967 ASSERT(zp
.zp_checksum
== ZIO_CHECKSUM_OFF
||
4968 zp
.zp_checksum
== ZIO_CHECKSUM_NOPARITY
);
4969 dr
->dr_zio
= zio_write(pio
, os
->os_spa
, txg
,
4970 &dr
->dr_bp_copy
, NULL
, db
->db
.db_size
, db
->db
.db_size
, &zp
,
4971 dbuf_write_nofill_ready
, NULL
, NULL
,
4972 dbuf_write_nofill_done
, db
,
4973 ZIO_PRIORITY_ASYNC_WRITE
,
4974 ZIO_FLAG_MUSTSUCCEED
| ZIO_FLAG_NODATA
, &zb
);
4976 ASSERT(arc_released(data
));
4979 * For indirect blocks, we want to setup the children
4980 * ready callback so that we can properly handle an indirect
4981 * block that only contains holes.
4983 arc_write_done_func_t
*children_ready_cb
= NULL
;
4984 if (db
->db_level
!= 0)
4985 children_ready_cb
= dbuf_write_children_ready
;
4987 dr
->dr_zio
= arc_write(pio
, os
->os_spa
, txg
,
4988 &dr
->dr_bp_copy
, data
, DBUF_IS_L2CACHEABLE(db
),
4989 &zp
, dbuf_write_ready
,
4990 children_ready_cb
, dbuf_write_physdone
,
4991 dbuf_write_done
, db
, ZIO_PRIORITY_ASYNC_WRITE
,
4992 ZIO_FLAG_MUSTSUCCEED
, &zb
);
4996 EXPORT_SYMBOL(dbuf_find
);
4997 EXPORT_SYMBOL(dbuf_is_metadata
);
4998 EXPORT_SYMBOL(dbuf_destroy
);
4999 EXPORT_SYMBOL(dbuf_loan_arcbuf
);
5000 EXPORT_SYMBOL(dbuf_whichblock
);
5001 EXPORT_SYMBOL(dbuf_read
);
5002 EXPORT_SYMBOL(dbuf_unoverride
);
5003 EXPORT_SYMBOL(dbuf_free_range
);
5004 EXPORT_SYMBOL(dbuf_new_size
);
5005 EXPORT_SYMBOL(dbuf_release_bp
);
5006 EXPORT_SYMBOL(dbuf_dirty
);
5007 EXPORT_SYMBOL(dmu_buf_set_crypt_params
);
5008 EXPORT_SYMBOL(dmu_buf_will_dirty
);
5009 EXPORT_SYMBOL(dmu_buf_is_dirty
);
5010 EXPORT_SYMBOL(dmu_buf_will_not_fill
);
5011 EXPORT_SYMBOL(dmu_buf_will_fill
);
5012 EXPORT_SYMBOL(dmu_buf_fill_done
);
5013 EXPORT_SYMBOL(dmu_buf_rele
);
5014 EXPORT_SYMBOL(dbuf_assign_arcbuf
);
5015 EXPORT_SYMBOL(dbuf_prefetch
);
5016 EXPORT_SYMBOL(dbuf_hold_impl
);
5017 EXPORT_SYMBOL(dbuf_hold
);
5018 EXPORT_SYMBOL(dbuf_hold_level
);
5019 EXPORT_SYMBOL(dbuf_create_bonus
);
5020 EXPORT_SYMBOL(dbuf_spill_set_blksz
);
5021 EXPORT_SYMBOL(dbuf_rm_spill
);
5022 EXPORT_SYMBOL(dbuf_add_ref
);
5023 EXPORT_SYMBOL(dbuf_rele
);
5024 EXPORT_SYMBOL(dbuf_rele_and_unlock
);
5025 EXPORT_SYMBOL(dbuf_refcount
);
5026 EXPORT_SYMBOL(dbuf_sync_list
);
5027 EXPORT_SYMBOL(dmu_buf_set_user
);
5028 EXPORT_SYMBOL(dmu_buf_set_user_ie
);
5029 EXPORT_SYMBOL(dmu_buf_get_user
);
5030 EXPORT_SYMBOL(dmu_buf_get_blkptr
);
5033 ZFS_MODULE_PARAM(zfs_dbuf_cache
, dbuf_cache_
, max_bytes
, ULONG
, ZMOD_RW
,
5034 "Maximum size in bytes of the dbuf cache.");
5036 ZFS_MODULE_PARAM(zfs_dbuf_cache
, dbuf_cache_
, hiwater_pct
, UINT
, ZMOD_RW
,
5037 "Percentage over dbuf_cache_max_bytes when dbufs must be evicted "
5040 ZFS_MODULE_PARAM(zfs_dbuf_cache
, dbuf_cache_
, lowater_pct
, UINT
, ZMOD_RW
,
5041 "Percentage below dbuf_cache_max_bytes when the evict thread stops "
5044 ZFS_MODULE_PARAM(zfs_dbuf
, dbuf_
, metadata_cache_max_bytes
, ULONG
, ZMOD_RW
,
5045 "Maximum size in bytes of the dbuf metadata cache.");
5047 ZFS_MODULE_PARAM(zfs_dbuf
, dbuf_
, cache_shift
, INT
, ZMOD_RW
,
5048 "Set the size of the dbuf cache to a log2 fraction of arc size.");
5050 ZFS_MODULE_PARAM(zfs_dbuf
, dbuf_
, metadata_cache_shift
, INT
, ZMOD_RW
,
5051 "Set the size of the dbuf metadata cache to a log2 fraction of arc "