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, 2019 by Delphix. All rights reserved.
25 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
26 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
29 #include <sys/zfs_context.h>
32 #include <sys/dmu_send.h>
33 #include <sys/dmu_impl.h>
35 #include <sys/dmu_objset.h>
36 #include <sys/dsl_dataset.h>
37 #include <sys/dsl_dir.h>
38 #include <sys/dmu_tx.h>
41 #include <sys/dmu_zfetch.h>
43 #include <sys/sa_impl.h>
44 #include <sys/zfeature.h>
45 #include <sys/blkptr.h>
46 #include <sys/range_tree.h>
47 #include <sys/trace_zfs.h>
48 #include <sys/callb.h>
51 #include <sys/cityhash.h>
52 #include <sys/spa_impl.h>
56 typedef struct dbuf_stats
{
58 * Various statistics about the size of the dbuf cache.
60 kstat_named_t cache_count
;
61 kstat_named_t cache_size_bytes
;
62 kstat_named_t cache_size_bytes_max
;
64 * Statistics regarding the bounds on the dbuf cache size.
66 kstat_named_t cache_target_bytes
;
67 kstat_named_t cache_lowater_bytes
;
68 kstat_named_t cache_hiwater_bytes
;
70 * Total number of dbuf cache evictions that have occurred.
72 kstat_named_t cache_total_evicts
;
74 * The distribution of dbuf levels in the dbuf cache and
75 * the total size of all dbufs at each level.
77 kstat_named_t cache_levels
[DN_MAX_LEVELS
];
78 kstat_named_t cache_levels_bytes
[DN_MAX_LEVELS
];
80 * Statistics about the dbuf hash table.
82 kstat_named_t hash_hits
;
83 kstat_named_t hash_misses
;
84 kstat_named_t hash_collisions
;
85 kstat_named_t hash_elements
;
86 kstat_named_t hash_elements_max
;
88 * Number of sublists containing more than one dbuf in the dbuf
89 * hash table. Keep track of the longest hash chain.
91 kstat_named_t hash_chains
;
92 kstat_named_t hash_chain_max
;
94 * Number of times a dbuf_create() discovers that a dbuf was
95 * already created and in the dbuf hash table.
97 kstat_named_t hash_insert_race
;
99 * Statistics about the size of the metadata dbuf cache.
101 kstat_named_t metadata_cache_count
;
102 kstat_named_t metadata_cache_size_bytes
;
103 kstat_named_t metadata_cache_size_bytes_max
;
105 * For diagnostic purposes, this is incremented whenever we can't add
106 * something to the metadata cache because it's full, and instead put
107 * the data in the regular dbuf cache.
109 kstat_named_t metadata_cache_overflow
;
112 dbuf_stats_t dbuf_stats
= {
113 { "cache_count", KSTAT_DATA_UINT64
},
114 { "cache_size_bytes", KSTAT_DATA_UINT64
},
115 { "cache_size_bytes_max", KSTAT_DATA_UINT64
},
116 { "cache_target_bytes", KSTAT_DATA_UINT64
},
117 { "cache_lowater_bytes", KSTAT_DATA_UINT64
},
118 { "cache_hiwater_bytes", KSTAT_DATA_UINT64
},
119 { "cache_total_evicts", KSTAT_DATA_UINT64
},
120 { { "cache_levels_N", KSTAT_DATA_UINT64
} },
121 { { "cache_levels_bytes_N", KSTAT_DATA_UINT64
} },
122 { "hash_hits", KSTAT_DATA_UINT64
},
123 { "hash_misses", KSTAT_DATA_UINT64
},
124 { "hash_collisions", KSTAT_DATA_UINT64
},
125 { "hash_elements", KSTAT_DATA_UINT64
},
126 { "hash_elements_max", KSTAT_DATA_UINT64
},
127 { "hash_chains", KSTAT_DATA_UINT64
},
128 { "hash_chain_max", KSTAT_DATA_UINT64
},
129 { "hash_insert_race", KSTAT_DATA_UINT64
},
130 { "metadata_cache_count", KSTAT_DATA_UINT64
},
131 { "metadata_cache_size_bytes", KSTAT_DATA_UINT64
},
132 { "metadata_cache_size_bytes_max", KSTAT_DATA_UINT64
},
133 { "metadata_cache_overflow", KSTAT_DATA_UINT64
}
136 #define DBUF_STAT_INCR(stat, val) \
137 atomic_add_64(&dbuf_stats.stat.value.ui64, (val));
138 #define DBUF_STAT_DECR(stat, val) \
139 DBUF_STAT_INCR(stat, -(val));
140 #define DBUF_STAT_BUMP(stat) \
141 DBUF_STAT_INCR(stat, 1);
142 #define DBUF_STAT_BUMPDOWN(stat) \
143 DBUF_STAT_INCR(stat, -1);
144 #define DBUF_STAT_MAX(stat, v) { \
146 while ((v) > (_m = dbuf_stats.stat.value.ui64) && \
147 (_m != atomic_cas_64(&dbuf_stats.stat.value.ui64, _m, (v))))\
151 static boolean_t
dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
);
152 static void dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
);
154 extern inline void dmu_buf_init_user(dmu_buf_user_t
*dbu
,
155 dmu_buf_evict_func_t
*evict_func_sync
,
156 dmu_buf_evict_func_t
*evict_func_async
,
157 dmu_buf_t
**clear_on_evict_dbufp
);
160 * Global data structures and functions for the dbuf cache.
162 static kmem_cache_t
*dbuf_kmem_cache
;
163 static taskq_t
*dbu_evict_taskq
;
165 static kthread_t
*dbuf_cache_evict_thread
;
166 static kmutex_t dbuf_evict_lock
;
167 static kcondvar_t dbuf_evict_cv
;
168 static boolean_t dbuf_evict_thread_exit
;
171 * There are two dbuf caches; each dbuf can only be in one of them at a time.
173 * 1. Cache of metadata dbufs, to help make read-heavy administrative commands
174 * from /sbin/zfs run faster. The "metadata cache" specifically stores dbufs
175 * that represent the metadata that describes filesystems/snapshots/
176 * bookmarks/properties/etc. We only evict from this cache when we export a
177 * pool, to short-circuit as much I/O as possible for all administrative
178 * commands that need the metadata. There is no eviction policy for this
179 * cache, because we try to only include types in it which would occupy a
180 * very small amount of space per object but create a large impact on the
181 * performance of these commands. Instead, after it reaches a maximum size
182 * (which should only happen on very small memory systems with a very large
183 * number of filesystem objects), we stop taking new dbufs into the
184 * metadata cache, instead putting them in the normal dbuf cache.
186 * 2. LRU cache of dbufs. The dbuf cache maintains a list of dbufs that
187 * are not currently held but have been recently released. These dbufs
188 * are not eligible for arc eviction until they are aged out of the cache.
189 * Dbufs that are aged out of the cache will be immediately destroyed and
190 * become eligible for arc eviction.
192 * Dbufs are added to these caches once the last hold is released. If a dbuf is
193 * later accessed and still exists in the dbuf cache, then it will be removed
194 * from the cache and later re-added to the head of the cache.
196 * If a given dbuf meets the requirements for the metadata cache, it will go
197 * there, otherwise it will be considered for the generic LRU dbuf cache. The
198 * caches and the refcounts tracking their sizes are stored in an array indexed
199 * by those caches' matching enum values (from dbuf_cached_state_t).
201 typedef struct dbuf_cache
{
205 dbuf_cache_t dbuf_caches
[DB_CACHE_MAX
];
207 /* Size limits for the caches */
208 unsigned long dbuf_cache_max_bytes
= 0;
209 unsigned long dbuf_metadata_cache_max_bytes
= 0;
210 /* Set the default sizes of the caches to log2 fraction of arc size */
211 int dbuf_cache_shift
= 5;
212 int dbuf_metadata_cache_shift
= 6;
215 * The LRU dbuf cache uses a three-stage eviction policy:
216 * - A low water marker designates when the dbuf eviction thread
217 * should stop evicting from the dbuf cache.
218 * - When we reach the maximum size (aka mid water mark), we
219 * signal the eviction thread to run.
220 * - The high water mark indicates when the eviction thread
221 * is unable to keep up with the incoming load and eviction must
222 * happen in the context of the calling thread.
226 * low water mid water hi water
227 * +----------------------------------------+----------+----------+
232 * +----------------------------------------+----------+----------+
234 * evicting eviction directly
237 * The high and low water marks indicate the operating range for the eviction
238 * thread. The low water mark is, by default, 90% of the total size of the
239 * cache and the high water mark is at 110% (both of these percentages can be
240 * changed by setting dbuf_cache_lowater_pct and dbuf_cache_hiwater_pct,
241 * respectively). The eviction thread will try to ensure that the cache remains
242 * within this range by waking up every second and checking if the cache is
243 * above the low water mark. The thread can also be woken up by callers adding
244 * elements into the cache if the cache is larger than the mid water (i.e max
245 * cache size). Once the eviction thread is woken up and eviction is required,
246 * it will continue evicting buffers until it's able to reduce the cache size
247 * to the low water mark. If the cache size continues to grow and hits the high
248 * water mark, then callers adding elements to the cache will begin to evict
249 * directly from the cache until the cache is no longer above the high water
254 * The percentage above and below the maximum cache size.
256 uint_t dbuf_cache_hiwater_pct
= 10;
257 uint_t dbuf_cache_lowater_pct
= 10;
261 dbuf_cons(void *vdb
, void *unused
, int kmflag
)
263 dmu_buf_impl_t
*db
= vdb
;
264 bzero(db
, sizeof (dmu_buf_impl_t
));
266 mutex_init(&db
->db_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
267 rw_init(&db
->db_rwlock
, NULL
, RW_DEFAULT
, NULL
);
268 cv_init(&db
->db_changed
, NULL
, CV_DEFAULT
, NULL
);
269 multilist_link_init(&db
->db_cache_link
);
270 zfs_refcount_create(&db
->db_holds
);
277 dbuf_dest(void *vdb
, void *unused
)
279 dmu_buf_impl_t
*db
= vdb
;
280 mutex_destroy(&db
->db_mtx
);
281 rw_destroy(&db
->db_rwlock
);
282 cv_destroy(&db
->db_changed
);
283 ASSERT(!multilist_link_active(&db
->db_cache_link
));
284 zfs_refcount_destroy(&db
->db_holds
);
288 * dbuf hash table routines
290 static dbuf_hash_table_t dbuf_hash_table
;
292 static uint64_t dbuf_hash_count
;
295 * We use Cityhash for this. It's fast, and has good hash properties without
296 * requiring any large static buffers.
299 dbuf_hash(void *os
, uint64_t obj
, uint8_t lvl
, uint64_t blkid
)
301 return (cityhash4((uintptr_t)os
, obj
, (uint64_t)lvl
, blkid
));
304 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
305 ((dbuf)->db.db_object == (obj) && \
306 (dbuf)->db_objset == (os) && \
307 (dbuf)->db_level == (level) && \
308 (dbuf)->db_blkid == (blkid))
311 dbuf_find(objset_t
*os
, uint64_t obj
, uint8_t level
, uint64_t blkid
)
313 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
318 hv
= dbuf_hash(os
, obj
, level
, blkid
);
319 idx
= hv
& h
->hash_table_mask
;
321 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
322 for (db
= h
->hash_table
[idx
]; db
!= NULL
; db
= db
->db_hash_next
) {
323 if (DBUF_EQUAL(db
, os
, obj
, level
, blkid
)) {
324 mutex_enter(&db
->db_mtx
);
325 if (db
->db_state
!= DB_EVICTING
) {
326 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
329 mutex_exit(&db
->db_mtx
);
332 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
336 static dmu_buf_impl_t
*
337 dbuf_find_bonus(objset_t
*os
, uint64_t object
)
340 dmu_buf_impl_t
*db
= NULL
;
342 if (dnode_hold(os
, object
, FTAG
, &dn
) == 0) {
343 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
344 if (dn
->dn_bonus
!= NULL
) {
346 mutex_enter(&db
->db_mtx
);
348 rw_exit(&dn
->dn_struct_rwlock
);
349 dnode_rele(dn
, FTAG
);
355 * Insert an entry into the hash table. If there is already an element
356 * equal to elem in the hash table, then the already existing element
357 * will be returned and the new element will not be inserted.
358 * Otherwise returns NULL.
360 static dmu_buf_impl_t
*
361 dbuf_hash_insert(dmu_buf_impl_t
*db
)
363 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
364 objset_t
*os
= db
->db_objset
;
365 uint64_t obj
= db
->db
.db_object
;
366 int level
= db
->db_level
;
367 uint64_t blkid
, hv
, idx
;
371 blkid
= db
->db_blkid
;
372 hv
= dbuf_hash(os
, obj
, level
, blkid
);
373 idx
= hv
& h
->hash_table_mask
;
375 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
376 for (dbf
= h
->hash_table
[idx
], i
= 0; dbf
!= NULL
;
377 dbf
= dbf
->db_hash_next
, i
++) {
378 if (DBUF_EQUAL(dbf
, os
, obj
, level
, blkid
)) {
379 mutex_enter(&dbf
->db_mtx
);
380 if (dbf
->db_state
!= DB_EVICTING
) {
381 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
384 mutex_exit(&dbf
->db_mtx
);
389 DBUF_STAT_BUMP(hash_collisions
);
391 DBUF_STAT_BUMP(hash_chains
);
393 DBUF_STAT_MAX(hash_chain_max
, i
);
396 mutex_enter(&db
->db_mtx
);
397 db
->db_hash_next
= h
->hash_table
[idx
];
398 h
->hash_table
[idx
] = db
;
399 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
400 atomic_inc_64(&dbuf_hash_count
);
401 DBUF_STAT_MAX(hash_elements_max
, dbuf_hash_count
);
407 * This returns whether this dbuf should be stored in the metadata cache, which
408 * is based on whether it's from one of the dnode types that store data related
409 * to traversing dataset hierarchies.
412 dbuf_include_in_metadata_cache(dmu_buf_impl_t
*db
)
415 dmu_object_type_t type
= DB_DNODE(db
)->dn_type
;
418 /* Check if this dbuf is one of the types we care about */
419 if (DMU_OT_IS_METADATA_CACHED(type
)) {
420 /* If we hit this, then we set something up wrong in dmu_ot */
421 ASSERT(DMU_OT_IS_METADATA(type
));
424 * Sanity check for small-memory systems: don't allocate too
425 * much memory for this purpose.
427 if (zfs_refcount_count(
428 &dbuf_caches
[DB_DBUF_METADATA_CACHE
].size
) >
429 dbuf_metadata_cache_max_bytes
) {
430 DBUF_STAT_BUMP(metadata_cache_overflow
);
441 * Remove an entry from the hash table. It must be in the EVICTING state.
444 dbuf_hash_remove(dmu_buf_impl_t
*db
)
446 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
448 dmu_buf_impl_t
*dbf
, **dbp
;
450 hv
= dbuf_hash(db
->db_objset
, db
->db
.db_object
,
451 db
->db_level
, db
->db_blkid
);
452 idx
= hv
& h
->hash_table_mask
;
455 * We mustn't hold db_mtx to maintain lock ordering:
456 * DBUF_HASH_MUTEX > db_mtx.
458 ASSERT(zfs_refcount_is_zero(&db
->db_holds
));
459 ASSERT(db
->db_state
== DB_EVICTING
);
460 ASSERT(!MUTEX_HELD(&db
->db_mtx
));
462 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
463 dbp
= &h
->hash_table
[idx
];
464 while ((dbf
= *dbp
) != db
) {
465 dbp
= &dbf
->db_hash_next
;
468 *dbp
= db
->db_hash_next
;
469 db
->db_hash_next
= NULL
;
470 if (h
->hash_table
[idx
] &&
471 h
->hash_table
[idx
]->db_hash_next
== NULL
)
472 DBUF_STAT_BUMPDOWN(hash_chains
);
473 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
474 atomic_dec_64(&dbuf_hash_count
);
480 } dbvu_verify_type_t
;
483 dbuf_verify_user(dmu_buf_impl_t
*db
, dbvu_verify_type_t verify_type
)
488 if (db
->db_user
== NULL
)
491 /* Only data blocks support the attachment of user data. */
492 ASSERT(db
->db_level
== 0);
494 /* Clients must resolve a dbuf before attaching user data. */
495 ASSERT(db
->db
.db_data
!= NULL
);
496 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
498 holds
= zfs_refcount_count(&db
->db_holds
);
499 if (verify_type
== DBVU_EVICTING
) {
501 * Immediate eviction occurs when holds == dirtycnt.
502 * For normal eviction buffers, holds is zero on
503 * eviction, except when dbuf_fix_old_data() calls
504 * dbuf_clear_data(). However, the hold count can grow
505 * during eviction even though db_mtx is held (see
506 * dmu_bonus_hold() for an example), so we can only
507 * test the generic invariant that holds >= dirtycnt.
509 ASSERT3U(holds
, >=, db
->db_dirtycnt
);
511 if (db
->db_user_immediate_evict
== TRUE
)
512 ASSERT3U(holds
, >=, db
->db_dirtycnt
);
514 ASSERT3U(holds
, >, 0);
520 dbuf_evict_user(dmu_buf_impl_t
*db
)
522 dmu_buf_user_t
*dbu
= db
->db_user
;
524 ASSERT(MUTEX_HELD(&db
->db_mtx
));
529 dbuf_verify_user(db
, DBVU_EVICTING
);
533 if (dbu
->dbu_clear_on_evict_dbufp
!= NULL
)
534 *dbu
->dbu_clear_on_evict_dbufp
= NULL
;
538 * There are two eviction callbacks - one that we call synchronously
539 * and one that we invoke via a taskq. The async one is useful for
540 * avoiding lock order reversals and limiting stack depth.
542 * Note that if we have a sync callback but no async callback,
543 * it's likely that the sync callback will free the structure
544 * containing the dbu. In that case we need to take care to not
545 * dereference dbu after calling the sync evict func.
547 boolean_t has_async
= (dbu
->dbu_evict_func_async
!= NULL
);
549 if (dbu
->dbu_evict_func_sync
!= NULL
)
550 dbu
->dbu_evict_func_sync(dbu
);
553 taskq_dispatch_ent(dbu_evict_taskq
, dbu
->dbu_evict_func_async
,
554 dbu
, 0, &dbu
->dbu_tqent
);
559 dbuf_is_metadata(dmu_buf_impl_t
*db
)
562 * Consider indirect blocks and spill blocks to be meta data.
564 if (db
->db_level
> 0 || db
->db_blkid
== DMU_SPILL_BLKID
) {
567 boolean_t is_metadata
;
570 is_metadata
= DMU_OT_IS_METADATA(DB_DNODE(db
)->dn_type
);
573 return (is_metadata
);
579 * This function *must* return indices evenly distributed between all
580 * sublists of the multilist. This is needed due to how the dbuf eviction
581 * code is laid out; dbuf_evict_thread() assumes dbufs are evenly
582 * distributed between all sublists and uses this assumption when
583 * deciding which sublist to evict from and how much to evict from it.
586 dbuf_cache_multilist_index_func(multilist_t
*ml
, void *obj
)
588 dmu_buf_impl_t
*db
= obj
;
591 * The assumption here, is the hash value for a given
592 * dmu_buf_impl_t will remain constant throughout it's lifetime
593 * (i.e. it's objset, object, level and blkid fields don't change).
594 * Thus, we don't need to store the dbuf's sublist index
595 * on insertion, as this index can be recalculated on removal.
597 * Also, the low order bits of the hash value are thought to be
598 * distributed evenly. Otherwise, in the case that the multilist
599 * has a power of two number of sublists, each sublists' usage
600 * would not be evenly distributed.
602 return (dbuf_hash(db
->db_objset
, db
->db
.db_object
,
603 db
->db_level
, db
->db_blkid
) %
604 multilist_get_num_sublists(ml
));
607 static inline unsigned long
608 dbuf_cache_target_bytes(void)
610 return MIN(dbuf_cache_max_bytes
,
611 arc_target_bytes() >> dbuf_cache_shift
);
614 static inline uint64_t
615 dbuf_cache_hiwater_bytes(void)
617 uint64_t dbuf_cache_target
= dbuf_cache_target_bytes();
618 return (dbuf_cache_target
+
619 (dbuf_cache_target
* dbuf_cache_hiwater_pct
) / 100);
622 static inline uint64_t
623 dbuf_cache_lowater_bytes(void)
625 uint64_t dbuf_cache_target
= dbuf_cache_target_bytes();
626 return (dbuf_cache_target
-
627 (dbuf_cache_target
* dbuf_cache_lowater_pct
) / 100);
630 static inline boolean_t
631 dbuf_cache_above_hiwater(void)
633 return (zfs_refcount_count(&dbuf_caches
[DB_DBUF_CACHE
].size
) >
634 dbuf_cache_hiwater_bytes());
637 static inline boolean_t
638 dbuf_cache_above_lowater(void)
640 return (zfs_refcount_count(&dbuf_caches
[DB_DBUF_CACHE
].size
) >
641 dbuf_cache_lowater_bytes());
645 * Evict the oldest eligible dbuf from the dbuf cache.
650 int idx
= multilist_get_random_index(dbuf_caches
[DB_DBUF_CACHE
].cache
);
651 multilist_sublist_t
*mls
= multilist_sublist_lock(
652 dbuf_caches
[DB_DBUF_CACHE
].cache
, idx
);
654 ASSERT(!MUTEX_HELD(&dbuf_evict_lock
));
656 dmu_buf_impl_t
*db
= multilist_sublist_tail(mls
);
657 while (db
!= NULL
&& mutex_tryenter(&db
->db_mtx
) == 0) {
658 db
= multilist_sublist_prev(mls
, db
);
661 DTRACE_PROBE2(dbuf__evict__one
, dmu_buf_impl_t
*, db
,
662 multilist_sublist_t
*, mls
);
665 multilist_sublist_remove(mls
, db
);
666 multilist_sublist_unlock(mls
);
667 (void) zfs_refcount_remove_many(
668 &dbuf_caches
[DB_DBUF_CACHE
].size
, db
->db
.db_size
, db
);
669 DBUF_STAT_BUMPDOWN(cache_levels
[db
->db_level
]);
670 DBUF_STAT_BUMPDOWN(cache_count
);
671 DBUF_STAT_DECR(cache_levels_bytes
[db
->db_level
],
673 ASSERT3U(db
->db_caching_status
, ==, DB_DBUF_CACHE
);
674 db
->db_caching_status
= DB_NO_CACHE
;
676 DBUF_STAT_MAX(cache_size_bytes_max
,
677 zfs_refcount_count(&dbuf_caches
[DB_DBUF_CACHE
].size
));
678 DBUF_STAT_BUMP(cache_total_evicts
);
680 multilist_sublist_unlock(mls
);
685 * The dbuf evict thread is responsible for aging out dbufs from the
686 * cache. Once the cache has reached it's maximum size, dbufs are removed
687 * and destroyed. The eviction thread will continue running until the size
688 * of the dbuf cache is at or below the maximum size. Once the dbuf is aged
689 * out of the cache it is destroyed and becomes eligible for arc eviction.
693 dbuf_evict_thread(void *unused
)
697 CALLB_CPR_INIT(&cpr
, &dbuf_evict_lock
, callb_generic_cpr
, FTAG
);
699 mutex_enter(&dbuf_evict_lock
);
700 while (!dbuf_evict_thread_exit
) {
701 while (!dbuf_cache_above_lowater() && !dbuf_evict_thread_exit
) {
702 CALLB_CPR_SAFE_BEGIN(&cpr
);
703 (void) cv_timedwait_sig_hires(&dbuf_evict_cv
,
704 &dbuf_evict_lock
, SEC2NSEC(1), MSEC2NSEC(1), 0);
705 CALLB_CPR_SAFE_END(&cpr
, &dbuf_evict_lock
);
707 mutex_exit(&dbuf_evict_lock
);
710 * Keep evicting as long as we're above the low water mark
711 * for the cache. We do this without holding the locks to
712 * minimize lock contention.
714 while (dbuf_cache_above_lowater() && !dbuf_evict_thread_exit
) {
718 mutex_enter(&dbuf_evict_lock
);
721 dbuf_evict_thread_exit
= B_FALSE
;
722 cv_broadcast(&dbuf_evict_cv
);
723 CALLB_CPR_EXIT(&cpr
); /* drops dbuf_evict_lock */
728 * Wake up the dbuf eviction thread if the dbuf cache is at its max size.
729 * If the dbuf cache is at its high water mark, then evict a dbuf from the
730 * dbuf cache using the callers context.
733 dbuf_evict_notify(void)
736 * We check if we should evict without holding the dbuf_evict_lock,
737 * because it's OK to occasionally make the wrong decision here,
738 * and grabbing the lock results in massive lock contention.
740 if (zfs_refcount_count(&dbuf_caches
[DB_DBUF_CACHE
].size
) >
741 dbuf_cache_target_bytes()) {
742 if (dbuf_cache_above_hiwater())
744 cv_signal(&dbuf_evict_cv
);
749 dbuf_kstat_update(kstat_t
*ksp
, int rw
)
751 dbuf_stats_t
*ds
= ksp
->ks_data
;
753 if (rw
== KSTAT_WRITE
) {
754 return (SET_ERROR(EACCES
));
756 ds
->metadata_cache_size_bytes
.value
.ui64
= zfs_refcount_count(
757 &dbuf_caches
[DB_DBUF_METADATA_CACHE
].size
);
758 ds
->cache_size_bytes
.value
.ui64
=
759 zfs_refcount_count(&dbuf_caches
[DB_DBUF_CACHE
].size
);
760 ds
->cache_target_bytes
.value
.ui64
= dbuf_cache_target_bytes();
761 ds
->cache_hiwater_bytes
.value
.ui64
= dbuf_cache_hiwater_bytes();
762 ds
->cache_lowater_bytes
.value
.ui64
= dbuf_cache_lowater_bytes();
763 ds
->hash_elements
.value
.ui64
= dbuf_hash_count
;
772 uint64_t hsize
= 1ULL << 16;
773 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
777 * The hash table is big enough to fill all of physical memory
778 * with an average block size of zfs_arc_average_blocksize (default 8K).
779 * By default, the table will take up
780 * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers).
782 while (hsize
* zfs_arc_average_blocksize
< physmem
* PAGESIZE
)
786 h
->hash_table_mask
= hsize
- 1;
789 * Large allocations which do not require contiguous pages
790 * should be using vmem_alloc() in the linux kernel
792 h
->hash_table
= vmem_zalloc(hsize
* sizeof (void *), KM_SLEEP
);
794 h
->hash_table
= kmem_zalloc(hsize
* sizeof (void *), KM_NOSLEEP
);
796 if (h
->hash_table
== NULL
) {
797 /* XXX - we should really return an error instead of assert */
798 ASSERT(hsize
> (1ULL << 10));
803 dbuf_kmem_cache
= kmem_cache_create("dmu_buf_impl_t",
804 sizeof (dmu_buf_impl_t
),
805 0, dbuf_cons
, dbuf_dest
, NULL
, NULL
, NULL
, 0);
807 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
808 mutex_init(&h
->hash_mutexes
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
813 * Setup the parameters for the dbuf caches. We set the sizes of the
814 * dbuf cache and the metadata cache to 1/32nd and 1/16th (default)
815 * of the target size of the ARC. If the values has been specified as
816 * a module option and they're not greater than the target size of the
817 * ARC, then we honor that value.
819 if (dbuf_cache_max_bytes
== 0 ||
820 dbuf_cache_max_bytes
>= arc_target_bytes()) {
821 dbuf_cache_max_bytes
= arc_target_bytes() >> dbuf_cache_shift
;
823 if (dbuf_metadata_cache_max_bytes
== 0 ||
824 dbuf_metadata_cache_max_bytes
>= arc_target_bytes()) {
825 dbuf_metadata_cache_max_bytes
=
826 arc_target_bytes() >> dbuf_metadata_cache_shift
;
830 * All entries are queued via taskq_dispatch_ent(), so min/maxalloc
831 * configuration is not required.
833 dbu_evict_taskq
= taskq_create("dbu_evict", 1, defclsyspri
, 0, 0, 0);
835 for (dbuf_cached_state_t dcs
= 0; dcs
< DB_CACHE_MAX
; dcs
++) {
836 dbuf_caches
[dcs
].cache
=
837 multilist_create(sizeof (dmu_buf_impl_t
),
838 offsetof(dmu_buf_impl_t
, db_cache_link
),
839 dbuf_cache_multilist_index_func
);
840 zfs_refcount_create(&dbuf_caches
[dcs
].size
);
843 dbuf_evict_thread_exit
= B_FALSE
;
844 mutex_init(&dbuf_evict_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
845 cv_init(&dbuf_evict_cv
, NULL
, CV_DEFAULT
, NULL
);
846 dbuf_cache_evict_thread
= thread_create(NULL
, 0, dbuf_evict_thread
,
847 NULL
, 0, &p0
, TS_RUN
, minclsyspri
);
849 dbuf_ksp
= kstat_create("zfs", 0, "dbufstats", "misc",
850 KSTAT_TYPE_NAMED
, sizeof (dbuf_stats
) / sizeof (kstat_named_t
),
852 if (dbuf_ksp
!= NULL
) {
853 for (i
= 0; i
< DN_MAX_LEVELS
; i
++) {
854 snprintf(dbuf_stats
.cache_levels
[i
].name
,
855 KSTAT_STRLEN
, "cache_level_%d", i
);
856 dbuf_stats
.cache_levels
[i
].data_type
=
858 snprintf(dbuf_stats
.cache_levels_bytes
[i
].name
,
859 KSTAT_STRLEN
, "cache_level_%d_bytes", i
);
860 dbuf_stats
.cache_levels_bytes
[i
].data_type
=
863 dbuf_ksp
->ks_data
= &dbuf_stats
;
864 dbuf_ksp
->ks_update
= dbuf_kstat_update
;
865 kstat_install(dbuf_ksp
);
872 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
875 dbuf_stats_destroy();
877 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
878 mutex_destroy(&h
->hash_mutexes
[i
]);
881 * Large allocations which do not require contiguous pages
882 * should be using vmem_free() in the linux kernel
884 vmem_free(h
->hash_table
, (h
->hash_table_mask
+ 1) * sizeof (void *));
886 kmem_free(h
->hash_table
, (h
->hash_table_mask
+ 1) * sizeof (void *));
888 kmem_cache_destroy(dbuf_kmem_cache
);
889 taskq_destroy(dbu_evict_taskq
);
891 mutex_enter(&dbuf_evict_lock
);
892 dbuf_evict_thread_exit
= B_TRUE
;
893 while (dbuf_evict_thread_exit
) {
894 cv_signal(&dbuf_evict_cv
);
895 cv_wait(&dbuf_evict_cv
, &dbuf_evict_lock
);
897 mutex_exit(&dbuf_evict_lock
);
899 mutex_destroy(&dbuf_evict_lock
);
900 cv_destroy(&dbuf_evict_cv
);
902 for (dbuf_cached_state_t dcs
= 0; dcs
< DB_CACHE_MAX
; dcs
++) {
903 zfs_refcount_destroy(&dbuf_caches
[dcs
].size
);
904 multilist_destroy(dbuf_caches
[dcs
].cache
);
907 if (dbuf_ksp
!= NULL
) {
908 kstat_delete(dbuf_ksp
);
919 dbuf_verify(dmu_buf_impl_t
*db
)
922 dbuf_dirty_record_t
*dr
;
925 ASSERT(MUTEX_HELD(&db
->db_mtx
));
927 if (!(zfs_flags
& ZFS_DEBUG_DBUF_VERIFY
))
930 ASSERT(db
->db_objset
!= NULL
);
934 ASSERT(db
->db_parent
== NULL
);
935 ASSERT(db
->db_blkptr
== NULL
);
937 ASSERT3U(db
->db
.db_object
, ==, dn
->dn_object
);
938 ASSERT3P(db
->db_objset
, ==, dn
->dn_objset
);
939 ASSERT3U(db
->db_level
, <, dn
->dn_nlevels
);
940 ASSERT(db
->db_blkid
== DMU_BONUS_BLKID
||
941 db
->db_blkid
== DMU_SPILL_BLKID
||
942 !avl_is_empty(&dn
->dn_dbufs
));
944 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
946 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
947 ASSERT3U(db
->db
.db_offset
, ==, DMU_BONUS_BLKID
);
948 } else if (db
->db_blkid
== DMU_SPILL_BLKID
) {
950 ASSERT0(db
->db
.db_offset
);
952 ASSERT3U(db
->db
.db_offset
, ==, db
->db_blkid
* db
->db
.db_size
);
955 if ((dr
= list_head(&db
->db_dirty_records
)) != NULL
) {
956 ASSERT(dr
->dr_dbuf
== db
);
957 txg_prev
= dr
->dr_txg
;
958 for (dr
= list_next(&db
->db_dirty_records
, dr
); dr
!= NULL
;
959 dr
= list_next(&db
->db_dirty_records
, dr
)) {
960 ASSERT(dr
->dr_dbuf
== db
);
961 ASSERT(txg_prev
> dr
->dr_txg
);
962 txg_prev
= dr
->dr_txg
;
967 * We can't assert that db_size matches dn_datablksz because it
968 * can be momentarily different when another thread is doing
971 if (db
->db_level
== 0 && db
->db
.db_object
== DMU_META_DNODE_OBJECT
) {
972 dr
= db
->db_data_pending
;
974 * It should only be modified in syncing context, so
975 * make sure we only have one copy of the data.
977 ASSERT(dr
== NULL
|| dr
->dt
.dl
.dr_data
== db
->db_buf
);
980 /* verify db->db_blkptr */
982 if (db
->db_parent
== dn
->dn_dbuf
) {
983 /* db is pointed to by the dnode */
984 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
985 if (DMU_OBJECT_IS_SPECIAL(db
->db
.db_object
))
986 ASSERT(db
->db_parent
== NULL
);
988 ASSERT(db
->db_parent
!= NULL
);
989 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
990 ASSERT3P(db
->db_blkptr
, ==,
991 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
993 /* db is pointed to by an indirect block */
994 int epb __maybe_unused
= db
->db_parent
->db
.db_size
>>
996 ASSERT3U(db
->db_parent
->db_level
, ==, db
->db_level
+1);
997 ASSERT3U(db
->db_parent
->db
.db_object
, ==,
1000 * dnode_grow_indblksz() can make this fail if we don't
1001 * have the parent's rwlock. XXX indblksz no longer
1002 * grows. safe to do this now?
1004 if (RW_LOCK_HELD(&db
->db_parent
->db_rwlock
)) {
1005 ASSERT3P(db
->db_blkptr
, ==,
1006 ((blkptr_t
*)db
->db_parent
->db
.db_data
+
1007 db
->db_blkid
% epb
));
1011 if ((db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
)) &&
1012 (db
->db_buf
== NULL
|| db
->db_buf
->b_data
) &&
1013 db
->db
.db_data
&& db
->db_blkid
!= DMU_BONUS_BLKID
&&
1014 db
->db_state
!= DB_FILL
&& !dn
->dn_free_txg
) {
1016 * If the blkptr isn't set but they have nonzero data,
1017 * it had better be dirty, otherwise we'll lose that
1018 * data when we evict this buffer.
1020 * There is an exception to this rule for indirect blocks; in
1021 * this case, if the indirect block is a hole, we fill in a few
1022 * fields on each of the child blocks (importantly, birth time)
1023 * to prevent hole birth times from being lost when you
1024 * partially fill in a hole.
1026 if (db
->db_dirtycnt
== 0) {
1027 if (db
->db_level
== 0) {
1028 uint64_t *buf
= db
->db
.db_data
;
1031 for (i
= 0; i
< db
->db
.db_size
>> 3; i
++) {
1032 ASSERT(buf
[i
] == 0);
1035 blkptr_t
*bps
= db
->db
.db_data
;
1036 ASSERT3U(1 << DB_DNODE(db
)->dn_indblkshift
, ==,
1039 * We want to verify that all the blkptrs in the
1040 * indirect block are holes, but we may have
1041 * automatically set up a few fields for them.
1042 * We iterate through each blkptr and verify
1043 * they only have those fields set.
1046 i
< db
->db
.db_size
/ sizeof (blkptr_t
);
1048 blkptr_t
*bp
= &bps
[i
];
1049 ASSERT(ZIO_CHECKSUM_IS_ZERO(
1052 DVA_IS_EMPTY(&bp
->blk_dva
[0]) &&
1053 DVA_IS_EMPTY(&bp
->blk_dva
[1]) &&
1054 DVA_IS_EMPTY(&bp
->blk_dva
[2]));
1055 ASSERT0(bp
->blk_fill
);
1056 ASSERT0(bp
->blk_pad
[0]);
1057 ASSERT0(bp
->blk_pad
[1]);
1058 ASSERT(!BP_IS_EMBEDDED(bp
));
1059 ASSERT(BP_IS_HOLE(bp
));
1060 ASSERT0(bp
->blk_phys_birth
);
1070 dbuf_clear_data(dmu_buf_impl_t
*db
)
1072 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1073 dbuf_evict_user(db
);
1074 ASSERT3P(db
->db_buf
, ==, NULL
);
1075 db
->db
.db_data
= NULL
;
1076 if (db
->db_state
!= DB_NOFILL
)
1077 db
->db_state
= DB_UNCACHED
;
1081 dbuf_set_data(dmu_buf_impl_t
*db
, arc_buf_t
*buf
)
1083 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1084 ASSERT(buf
!= NULL
);
1087 ASSERT(buf
->b_data
!= NULL
);
1088 db
->db
.db_data
= buf
->b_data
;
1092 * Loan out an arc_buf for read. Return the loaned arc_buf.
1095 dbuf_loan_arcbuf(dmu_buf_impl_t
*db
)
1099 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1100 mutex_enter(&db
->db_mtx
);
1101 if (arc_released(db
->db_buf
) || zfs_refcount_count(&db
->db_holds
) > 1) {
1102 int blksz
= db
->db
.db_size
;
1103 spa_t
*spa
= db
->db_objset
->os_spa
;
1105 mutex_exit(&db
->db_mtx
);
1106 abuf
= arc_loan_buf(spa
, B_FALSE
, blksz
);
1107 bcopy(db
->db
.db_data
, abuf
->b_data
, blksz
);
1110 arc_loan_inuse_buf(abuf
, db
);
1112 dbuf_clear_data(db
);
1113 mutex_exit(&db
->db_mtx
);
1119 * Calculate which level n block references the data at the level 0 offset
1123 dbuf_whichblock(const dnode_t
*dn
, const int64_t level
, const uint64_t offset
)
1125 if (dn
->dn_datablkshift
!= 0 && dn
->dn_indblkshift
!= 0) {
1127 * The level n blkid is equal to the level 0 blkid divided by
1128 * the number of level 0s in a level n block.
1130 * The level 0 blkid is offset >> datablkshift =
1131 * offset / 2^datablkshift.
1133 * The number of level 0s in a level n is the number of block
1134 * pointers in an indirect block, raised to the power of level.
1135 * This is 2^(indblkshift - SPA_BLKPTRSHIFT)^level =
1136 * 2^(level*(indblkshift - SPA_BLKPTRSHIFT)).
1138 * Thus, the level n blkid is: offset /
1139 * ((2^datablkshift)*(2^(level*(indblkshift-SPA_BLKPTRSHIFT))))
1140 * = offset / 2^(datablkshift + level *
1141 * (indblkshift - SPA_BLKPTRSHIFT))
1142 * = offset >> (datablkshift + level *
1143 * (indblkshift - SPA_BLKPTRSHIFT))
1146 const unsigned exp
= dn
->dn_datablkshift
+
1147 level
* (dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
);
1149 if (exp
>= 8 * sizeof (offset
)) {
1150 /* This only happens on the highest indirection level */
1151 ASSERT3U(level
, ==, dn
->dn_nlevels
- 1);
1155 ASSERT3U(exp
, <, 8 * sizeof (offset
));
1157 return (offset
>> exp
);
1159 ASSERT3U(offset
, <, dn
->dn_datablksz
);
1165 * This function is used to lock the parent of the provided dbuf. This should be
1166 * used when modifying or reading db_blkptr.
1169 dmu_buf_lock_parent(dmu_buf_impl_t
*db
, krw_t rw
, void *tag
)
1171 enum db_lock_type ret
= DLT_NONE
;
1172 if (db
->db_parent
!= NULL
) {
1173 rw_enter(&db
->db_parent
->db_rwlock
, rw
);
1175 } else if (dmu_objset_ds(db
->db_objset
) != NULL
) {
1176 rrw_enter(&dmu_objset_ds(db
->db_objset
)->ds_bp_rwlock
, rw
,
1181 * We only return a DLT_NONE lock when it's the top-most indirect block
1182 * of the meta-dnode of the MOS.
1188 * We need to pass the lock type in because it's possible that the block will
1189 * move from being the topmost indirect block in a dnode (and thus, have no
1190 * parent) to not the top-most via an indirection increase. This would cause a
1191 * panic if we didn't pass the lock type in.
1194 dmu_buf_unlock_parent(dmu_buf_impl_t
*db
, db_lock_type_t type
, void *tag
)
1196 if (type
== DLT_PARENT
)
1197 rw_exit(&db
->db_parent
->db_rwlock
);
1198 else if (type
== DLT_OBJSET
)
1199 rrw_exit(&dmu_objset_ds(db
->db_objset
)->ds_bp_rwlock
, tag
);
1203 dbuf_read_done(zio_t
*zio
, const zbookmark_phys_t
*zb
, const blkptr_t
*bp
,
1204 arc_buf_t
*buf
, void *vdb
)
1206 dmu_buf_impl_t
*db
= vdb
;
1208 mutex_enter(&db
->db_mtx
);
1209 ASSERT3U(db
->db_state
, ==, DB_READ
);
1211 * All reads are synchronous, so we must have a hold on the dbuf
1213 ASSERT(zfs_refcount_count(&db
->db_holds
) > 0);
1214 ASSERT(db
->db_buf
== NULL
);
1215 ASSERT(db
->db
.db_data
== NULL
);
1218 ASSERT(zio
== NULL
|| zio
->io_error
!= 0);
1219 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1220 ASSERT3P(db
->db_buf
, ==, NULL
);
1221 db
->db_state
= DB_UNCACHED
;
1222 } else if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
1223 /* freed in flight */
1224 ASSERT(zio
== NULL
|| zio
->io_error
== 0);
1225 arc_release(buf
, db
);
1226 bzero(buf
->b_data
, db
->db
.db_size
);
1227 arc_buf_freeze(buf
);
1228 db
->db_freed_in_flight
= FALSE
;
1229 dbuf_set_data(db
, buf
);
1230 db
->db_state
= DB_CACHED
;
1233 ASSERT(zio
== NULL
|| zio
->io_error
== 0);
1234 dbuf_set_data(db
, buf
);
1235 db
->db_state
= DB_CACHED
;
1237 cv_broadcast(&db
->db_changed
);
1238 dbuf_rele_and_unlock(db
, NULL
, B_FALSE
);
1243 * This function ensures that, when doing a decrypting read of a block,
1244 * we make sure we have decrypted the dnode associated with it. We must do
1245 * this so that we ensure we are fully authenticating the checksum-of-MACs
1246 * tree from the root of the objset down to this block. Indirect blocks are
1247 * always verified against their secure checksum-of-MACs assuming that the
1248 * dnode containing them is correct. Now that we are doing a decrypting read,
1249 * we can be sure that the key is loaded and verify that assumption. This is
1250 * especially important considering that we always read encrypted dnode
1251 * blocks as raw data (without verifying their MACs) to start, and
1252 * decrypt / authenticate them when we need to read an encrypted bonus buffer.
1255 dbuf_read_verify_dnode_crypt(dmu_buf_impl_t
*db
, uint32_t flags
)
1258 objset_t
*os
= db
->db_objset
;
1259 arc_buf_t
*dnode_abuf
;
1261 zbookmark_phys_t zb
;
1263 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1265 if (!os
->os_encrypted
|| os
->os_raw_receive
||
1266 (flags
& DB_RF_NO_DECRYPT
) != 0)
1271 dnode_abuf
= (dn
->dn_dbuf
!= NULL
) ? dn
->dn_dbuf
->db_buf
: NULL
;
1273 if (dnode_abuf
== NULL
|| !arc_is_encrypted(dnode_abuf
)) {
1278 SET_BOOKMARK(&zb
, dmu_objset_id(os
),
1279 DMU_META_DNODE_OBJECT
, 0, dn
->dn_dbuf
->db_blkid
);
1280 err
= arc_untransform(dnode_abuf
, os
->os_spa
, &zb
, B_TRUE
);
1283 * An error code of EACCES tells us that the key is still not
1284 * available. This is ok if we are only reading authenticated
1285 * (and therefore non-encrypted) blocks.
1287 if (err
== EACCES
&& ((db
->db_blkid
!= DMU_BONUS_BLKID
&&
1288 !DMU_OT_IS_ENCRYPTED(dn
->dn_type
)) ||
1289 (db
->db_blkid
== DMU_BONUS_BLKID
&&
1290 !DMU_OT_IS_ENCRYPTED(dn
->dn_bonustype
))))
1299 * Drops db_mtx and the parent lock specified by dblt and tag before
1303 dbuf_read_impl(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t flags
,
1304 db_lock_type_t dblt
, void *tag
)
1307 zbookmark_phys_t zb
;
1308 uint32_t aflags
= ARC_FLAG_NOWAIT
;
1309 int err
, zio_flags
= 0;
1313 ASSERT(!zfs_refcount_is_zero(&db
->db_holds
));
1314 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1315 ASSERT(db
->db_state
== DB_UNCACHED
);
1316 ASSERT(db
->db_buf
== NULL
);
1317 ASSERT(db
->db_parent
== NULL
||
1318 RW_LOCK_HELD(&db
->db_parent
->db_rwlock
));
1320 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1322 * The bonus length stored in the dnode may be less than
1323 * the maximum available space in the bonus buffer.
1325 int bonuslen
= MIN(dn
->dn_bonuslen
, dn
->dn_phys
->dn_bonuslen
);
1326 int max_bonuslen
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
);
1328 /* if the underlying dnode block is encrypted, decrypt it */
1329 err
= dbuf_read_verify_dnode_crypt(db
, flags
);
1332 mutex_exit(&db
->db_mtx
);
1336 ASSERT3U(bonuslen
, <=, db
->db
.db_size
);
1337 db
->db
.db_data
= kmem_alloc(max_bonuslen
, KM_SLEEP
);
1338 arc_space_consume(max_bonuslen
, ARC_SPACE_BONUS
);
1339 if (bonuslen
< max_bonuslen
)
1340 bzero(db
->db
.db_data
, max_bonuslen
);
1342 bcopy(DN_BONUS(dn
->dn_phys
), db
->db
.db_data
, bonuslen
);
1344 db
->db_state
= DB_CACHED
;
1345 mutex_exit(&db
->db_mtx
);
1346 dmu_buf_unlock_parent(db
, dblt
, tag
);
1351 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
1352 * processes the delete record and clears the bp while we are waiting
1353 * for the dn_mtx (resulting in a "no" from block_freed).
1355 if (db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
) ||
1356 (db
->db_level
== 0 && (dnode_block_freed(dn
, db
->db_blkid
) ||
1357 BP_IS_HOLE(db
->db_blkptr
)))) {
1358 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1360 dbuf_set_data(db
, arc_alloc_buf(db
->db_objset
->os_spa
, db
, type
,
1362 bzero(db
->db
.db_data
, db
->db
.db_size
);
1364 if (db
->db_blkptr
!= NULL
&& db
->db_level
> 0 &&
1365 BP_IS_HOLE(db
->db_blkptr
) &&
1366 db
->db_blkptr
->blk_birth
!= 0) {
1367 blkptr_t
*bps
= db
->db
.db_data
;
1368 for (int i
= 0; i
< ((1 <<
1369 DB_DNODE(db
)->dn_indblkshift
) / sizeof (blkptr_t
));
1371 blkptr_t
*bp
= &bps
[i
];
1372 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==,
1373 1 << dn
->dn_indblkshift
);
1375 BP_GET_LEVEL(db
->db_blkptr
) == 1 ?
1377 BP_GET_LSIZE(db
->db_blkptr
));
1378 BP_SET_TYPE(bp
, BP_GET_TYPE(db
->db_blkptr
));
1380 BP_GET_LEVEL(db
->db_blkptr
) - 1);
1381 BP_SET_BIRTH(bp
, db
->db_blkptr
->blk_birth
, 0);
1385 db
->db_state
= DB_CACHED
;
1386 mutex_exit(&db
->db_mtx
);
1387 dmu_buf_unlock_parent(db
, dblt
, tag
);
1392 * Any attempt to read a redacted block should result in an error. This
1393 * will never happen under normal conditions, but can be useful for
1394 * debugging purposes.
1396 if (BP_IS_REDACTED(db
->db_blkptr
)) {
1397 ASSERT(dsl_dataset_feature_is_active(
1398 db
->db_objset
->os_dsl_dataset
,
1399 SPA_FEATURE_REDACTED_DATASETS
));
1401 mutex_exit(&db
->db_mtx
);
1402 return (SET_ERROR(EIO
));
1406 SET_BOOKMARK(&zb
, dmu_objset_id(db
->db_objset
),
1407 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
1410 * All bps of an encrypted os should have the encryption bit set.
1411 * If this is not true it indicates tampering and we report an error.
1413 if (db
->db_objset
->os_encrypted
&& !BP_USES_CRYPT(db
->db_blkptr
)) {
1414 spa_log_error(db
->db_objset
->os_spa
, &zb
);
1415 zfs_panic_recover("unencrypted block in encrypted "
1416 "object set %llu", dmu_objset_id(db
->db_objset
));
1418 mutex_exit(&db
->db_mtx
);
1419 dmu_buf_unlock_parent(db
, dblt
, tag
);
1420 return (SET_ERROR(EIO
));
1423 err
= dbuf_read_verify_dnode_crypt(db
, flags
);
1426 dmu_buf_unlock_parent(db
, dblt
, tag
);
1427 mutex_exit(&db
->db_mtx
);
1433 db
->db_state
= DB_READ
;
1434 mutex_exit(&db
->db_mtx
);
1436 if (DBUF_IS_L2CACHEABLE(db
))
1437 aflags
|= ARC_FLAG_L2CACHE
;
1439 dbuf_add_ref(db
, NULL
);
1441 zio_flags
= (flags
& DB_RF_CANFAIL
) ?
1442 ZIO_FLAG_CANFAIL
: ZIO_FLAG_MUSTSUCCEED
;
1444 if ((flags
& DB_RF_NO_DECRYPT
) && BP_IS_PROTECTED(db
->db_blkptr
))
1445 zio_flags
|= ZIO_FLAG_RAW
;
1447 * The zio layer will copy the provided blkptr later, but we need to
1448 * do this now so that we can release the parent's rwlock. We have to
1449 * do that now so that if dbuf_read_done is called synchronously (on
1450 * an l1 cache hit) we don't acquire the db_mtx while holding the
1451 * parent's rwlock, which would be a lock ordering violation.
1453 blkptr_t bp
= *db
->db_blkptr
;
1454 dmu_buf_unlock_parent(db
, dblt
, tag
);
1455 (void) arc_read(zio
, db
->db_objset
->os_spa
, &bp
,
1456 dbuf_read_done
, db
, ZIO_PRIORITY_SYNC_READ
, zio_flags
,
1462 * This is our just-in-time copy function. It makes a copy of buffers that
1463 * have been modified in a previous transaction group before we access them in
1464 * the current active group.
1466 * This function is used in three places: when we are dirtying a buffer for the
1467 * first time in a txg, when we are freeing a range in a dnode that includes
1468 * this buffer, and when we are accessing a buffer which was received compressed
1469 * and later referenced in a WRITE_BYREF record.
1471 * Note that when we are called from dbuf_free_range() we do not put a hold on
1472 * the buffer, we just traverse the active dbuf list for the dnode.
1475 dbuf_fix_old_data(dmu_buf_impl_t
*db
, uint64_t txg
)
1477 dbuf_dirty_record_t
*dr
= list_head(&db
->db_dirty_records
);
1479 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1480 ASSERT(db
->db
.db_data
!= NULL
);
1481 ASSERT(db
->db_level
== 0);
1482 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
);
1485 (dr
->dt
.dl
.dr_data
!=
1486 ((db
->db_blkid
== DMU_BONUS_BLKID
) ? db
->db
.db_data
: db
->db_buf
)))
1490 * If the last dirty record for this dbuf has not yet synced
1491 * and its referencing the dbuf data, either:
1492 * reset the reference to point to a new copy,
1493 * or (if there a no active holders)
1494 * just null out the current db_data pointer.
1496 ASSERT3U(dr
->dr_txg
, >=, txg
- 2);
1497 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1498 dnode_t
*dn
= DB_DNODE(db
);
1499 int bonuslen
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
);
1500 dr
->dt
.dl
.dr_data
= kmem_alloc(bonuslen
, KM_SLEEP
);
1501 arc_space_consume(bonuslen
, ARC_SPACE_BONUS
);
1502 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
, bonuslen
);
1503 } else if (zfs_refcount_count(&db
->db_holds
) > db
->db_dirtycnt
) {
1504 dnode_t
*dn
= DB_DNODE(db
);
1505 int size
= arc_buf_size(db
->db_buf
);
1506 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1507 spa_t
*spa
= db
->db_objset
->os_spa
;
1508 enum zio_compress compress_type
=
1509 arc_get_compression(db
->db_buf
);
1511 if (arc_is_encrypted(db
->db_buf
)) {
1512 boolean_t byteorder
;
1513 uint8_t salt
[ZIO_DATA_SALT_LEN
];
1514 uint8_t iv
[ZIO_DATA_IV_LEN
];
1515 uint8_t mac
[ZIO_DATA_MAC_LEN
];
1517 arc_get_raw_params(db
->db_buf
, &byteorder
, salt
,
1519 dr
->dt
.dl
.dr_data
= arc_alloc_raw_buf(spa
, db
,
1520 dmu_objset_id(dn
->dn_objset
), byteorder
, salt
, iv
,
1521 mac
, dn
->dn_type
, size
, arc_buf_lsize(db
->db_buf
),
1523 } else if (compress_type
!= ZIO_COMPRESS_OFF
) {
1524 ASSERT3U(type
, ==, ARC_BUFC_DATA
);
1525 dr
->dt
.dl
.dr_data
= arc_alloc_compressed_buf(spa
, db
,
1526 size
, arc_buf_lsize(db
->db_buf
), compress_type
);
1528 dr
->dt
.dl
.dr_data
= arc_alloc_buf(spa
, db
, type
, size
);
1530 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
->b_data
, size
);
1533 dbuf_clear_data(db
);
1538 dbuf_read(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t flags
)
1545 * We don't have to hold the mutex to check db_state because it
1546 * can't be freed while we have a hold on the buffer.
1548 ASSERT(!zfs_refcount_is_zero(&db
->db_holds
));
1550 if (db
->db_state
== DB_NOFILL
)
1551 return (SET_ERROR(EIO
));
1556 prefetch
= db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1557 (flags
& DB_RF_NOPREFETCH
) == 0 && dn
!= NULL
&&
1558 DBUF_IS_CACHEABLE(db
);
1560 mutex_enter(&db
->db_mtx
);
1561 if (db
->db_state
== DB_CACHED
) {
1562 spa_t
*spa
= dn
->dn_objset
->os_spa
;
1565 * Ensure that this block's dnode has been decrypted if
1566 * the caller has requested decrypted data.
1568 err
= dbuf_read_verify_dnode_crypt(db
, flags
);
1571 * If the arc buf is compressed or encrypted and the caller
1572 * requested uncompressed data, we need to untransform it
1573 * before returning. We also call arc_untransform() on any
1574 * unauthenticated blocks, which will verify their MAC if
1575 * the key is now available.
1577 if (err
== 0 && db
->db_buf
!= NULL
&&
1578 (flags
& DB_RF_NO_DECRYPT
) == 0 &&
1579 (arc_is_encrypted(db
->db_buf
) ||
1580 arc_is_unauthenticated(db
->db_buf
) ||
1581 arc_get_compression(db
->db_buf
) != ZIO_COMPRESS_OFF
)) {
1582 zbookmark_phys_t zb
;
1584 SET_BOOKMARK(&zb
, dmu_objset_id(db
->db_objset
),
1585 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
1586 dbuf_fix_old_data(db
, spa_syncing_txg(spa
));
1587 err
= arc_untransform(db
->db_buf
, spa
, &zb
, B_FALSE
);
1588 dbuf_set_data(db
, db
->db_buf
);
1590 mutex_exit(&db
->db_mtx
);
1591 if (err
== 0 && prefetch
) {
1592 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
,
1593 flags
& DB_RF_HAVESTRUCT
);
1596 DBUF_STAT_BUMP(hash_hits
);
1597 } else if (db
->db_state
== DB_UNCACHED
) {
1598 spa_t
*spa
= dn
->dn_objset
->os_spa
;
1599 boolean_t need_wait
= B_FALSE
;
1601 db_lock_type_t dblt
= dmu_buf_lock_parent(db
, RW_READER
, FTAG
);
1604 db
->db_blkptr
!= NULL
&& !BP_IS_HOLE(db
->db_blkptr
)) {
1605 zio
= zio_root(spa
, NULL
, NULL
, ZIO_FLAG_CANFAIL
);
1608 err
= dbuf_read_impl(db
, zio
, flags
, dblt
, FTAG
);
1610 * dbuf_read_impl has dropped db_mtx and our parent's rwlock
1613 if (!err
&& prefetch
) {
1614 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
,
1615 flags
& DB_RF_HAVESTRUCT
);
1619 DBUF_STAT_BUMP(hash_misses
);
1622 * If we created a zio_root we must execute it to avoid
1623 * leaking it, even if it isn't attached to any work due
1624 * to an error in dbuf_read_impl().
1628 err
= zio_wait(zio
);
1630 VERIFY0(zio_wait(zio
));
1634 * Another reader came in while the dbuf was in flight
1635 * between UNCACHED and CACHED. Either a writer will finish
1636 * writing the buffer (sending the dbuf to CACHED) or the
1637 * first reader's request will reach the read_done callback
1638 * and send the dbuf to CACHED. Otherwise, a failure
1639 * occurred and the dbuf went to UNCACHED.
1641 mutex_exit(&db
->db_mtx
);
1643 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
,
1644 flags
& DB_RF_HAVESTRUCT
);
1647 DBUF_STAT_BUMP(hash_misses
);
1649 /* Skip the wait per the caller's request. */
1650 mutex_enter(&db
->db_mtx
);
1651 if ((flags
& DB_RF_NEVERWAIT
) == 0) {
1652 while (db
->db_state
== DB_READ
||
1653 db
->db_state
== DB_FILL
) {
1654 ASSERT(db
->db_state
== DB_READ
||
1655 (flags
& DB_RF_HAVESTRUCT
) == 0);
1656 DTRACE_PROBE2(blocked__read
, dmu_buf_impl_t
*,
1658 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1660 if (db
->db_state
== DB_UNCACHED
)
1661 err
= SET_ERROR(EIO
);
1663 mutex_exit(&db
->db_mtx
);
1670 dbuf_noread(dmu_buf_impl_t
*db
)
1672 ASSERT(!zfs_refcount_is_zero(&db
->db_holds
));
1673 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1674 mutex_enter(&db
->db_mtx
);
1675 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
1676 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1677 if (db
->db_state
== DB_UNCACHED
) {
1678 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1679 spa_t
*spa
= db
->db_objset
->os_spa
;
1681 ASSERT(db
->db_buf
== NULL
);
1682 ASSERT(db
->db
.db_data
== NULL
);
1683 dbuf_set_data(db
, arc_alloc_buf(spa
, db
, type
, db
->db
.db_size
));
1684 db
->db_state
= DB_FILL
;
1685 } else if (db
->db_state
== DB_NOFILL
) {
1686 dbuf_clear_data(db
);
1688 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
1690 mutex_exit(&db
->db_mtx
);
1694 dbuf_unoverride(dbuf_dirty_record_t
*dr
)
1696 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
1697 blkptr_t
*bp
= &dr
->dt
.dl
.dr_overridden_by
;
1698 uint64_t txg
= dr
->dr_txg
;
1700 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1702 * This assert is valid because dmu_sync() expects to be called by
1703 * a zilog's get_data while holding a range lock. This call only
1704 * comes from dbuf_dirty() callers who must also hold a range lock.
1706 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_IN_DMU_SYNC
);
1707 ASSERT(db
->db_level
== 0);
1709 if (db
->db_blkid
== DMU_BONUS_BLKID
||
1710 dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
)
1713 ASSERT(db
->db_data_pending
!= dr
);
1715 /* free this block */
1716 if (!BP_IS_HOLE(bp
) && !dr
->dt
.dl
.dr_nopwrite
)
1717 zio_free(db
->db_objset
->os_spa
, txg
, bp
);
1719 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
1720 dr
->dt
.dl
.dr_nopwrite
= B_FALSE
;
1721 dr
->dt
.dl
.dr_has_raw_params
= B_FALSE
;
1724 * Release the already-written buffer, so we leave it in
1725 * a consistent dirty state. Note that all callers are
1726 * modifying the buffer, so they will immediately do
1727 * another (redundant) arc_release(). Therefore, leave
1728 * the buf thawed to save the effort of freezing &
1729 * immediately re-thawing it.
1731 arc_release(dr
->dt
.dl
.dr_data
, db
);
1735 * Evict (if its unreferenced) or clear (if its referenced) any level-0
1736 * data blocks in the free range, so that any future readers will find
1740 dbuf_free_range(dnode_t
*dn
, uint64_t start_blkid
, uint64_t end_blkid
,
1743 dmu_buf_impl_t
*db_search
;
1744 dmu_buf_impl_t
*db
, *db_next
;
1745 uint64_t txg
= tx
->tx_txg
;
1748 if (end_blkid
> dn
->dn_maxblkid
&&
1749 !(start_blkid
== DMU_SPILL_BLKID
|| end_blkid
== DMU_SPILL_BLKID
))
1750 end_blkid
= dn
->dn_maxblkid
;
1751 dprintf_dnode(dn
, "start=%llu end=%llu\n", start_blkid
, end_blkid
);
1753 db_search
= kmem_alloc(sizeof (dmu_buf_impl_t
), KM_SLEEP
);
1754 db_search
->db_level
= 0;
1755 db_search
->db_blkid
= start_blkid
;
1756 db_search
->db_state
= DB_SEARCH
;
1758 mutex_enter(&dn
->dn_dbufs_mtx
);
1759 db
= avl_find(&dn
->dn_dbufs
, db_search
, &where
);
1760 ASSERT3P(db
, ==, NULL
);
1762 db
= avl_nearest(&dn
->dn_dbufs
, where
, AVL_AFTER
);
1764 for (; db
!= NULL
; db
= db_next
) {
1765 db_next
= AVL_NEXT(&dn
->dn_dbufs
, db
);
1766 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1768 if (db
->db_level
!= 0 || db
->db_blkid
> end_blkid
) {
1771 ASSERT3U(db
->db_blkid
, >=, start_blkid
);
1773 /* found a level 0 buffer in the range */
1774 mutex_enter(&db
->db_mtx
);
1775 if (dbuf_undirty(db
, tx
)) {
1776 /* mutex has been dropped and dbuf destroyed */
1780 if (db
->db_state
== DB_UNCACHED
||
1781 db
->db_state
== DB_NOFILL
||
1782 db
->db_state
== DB_EVICTING
) {
1783 ASSERT(db
->db
.db_data
== NULL
);
1784 mutex_exit(&db
->db_mtx
);
1787 if (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
) {
1788 /* will be handled in dbuf_read_done or dbuf_rele */
1789 db
->db_freed_in_flight
= TRUE
;
1790 mutex_exit(&db
->db_mtx
);
1793 if (zfs_refcount_count(&db
->db_holds
) == 0) {
1798 /* The dbuf is referenced */
1800 if (!list_is_empty(&db
->db_dirty_records
)) {
1801 dbuf_dirty_record_t
*dr
;
1803 dr
= list_head(&db
->db_dirty_records
);
1804 if (dr
->dr_txg
== txg
) {
1806 * This buffer is "in-use", re-adjust the file
1807 * size to reflect that this buffer may
1808 * contain new data when we sync.
1810 if (db
->db_blkid
!= DMU_SPILL_BLKID
&&
1811 db
->db_blkid
> dn
->dn_maxblkid
)
1812 dn
->dn_maxblkid
= db
->db_blkid
;
1813 dbuf_unoverride(dr
);
1816 * This dbuf is not dirty in the open context.
1817 * Either uncache it (if its not referenced in
1818 * the open context) or reset its contents to
1821 dbuf_fix_old_data(db
, txg
);
1824 /* clear the contents if its cached */
1825 if (db
->db_state
== DB_CACHED
) {
1826 ASSERT(db
->db
.db_data
!= NULL
);
1827 arc_release(db
->db_buf
, db
);
1828 rw_enter(&db
->db_rwlock
, RW_WRITER
);
1829 bzero(db
->db
.db_data
, db
->db
.db_size
);
1830 rw_exit(&db
->db_rwlock
);
1831 arc_buf_freeze(db
->db_buf
);
1834 mutex_exit(&db
->db_mtx
);
1837 kmem_free(db_search
, sizeof (dmu_buf_impl_t
));
1838 mutex_exit(&dn
->dn_dbufs_mtx
);
1842 dbuf_new_size(dmu_buf_impl_t
*db
, int size
, dmu_tx_t
*tx
)
1844 arc_buf_t
*buf
, *obuf
;
1845 dbuf_dirty_record_t
*dr
;
1846 int osize
= db
->db
.db_size
;
1847 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1850 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1856 * XXX we should be doing a dbuf_read, checking the return
1857 * value and returning that up to our callers
1859 dmu_buf_will_dirty(&db
->db
, tx
);
1861 /* create the data buffer for the new block */
1862 buf
= arc_alloc_buf(dn
->dn_objset
->os_spa
, db
, type
, size
);
1864 /* copy old block data to the new block */
1866 bcopy(obuf
->b_data
, buf
->b_data
, MIN(osize
, size
));
1867 /* zero the remainder */
1869 bzero((uint8_t *)buf
->b_data
+ osize
, size
- osize
);
1871 mutex_enter(&db
->db_mtx
);
1872 dbuf_set_data(db
, buf
);
1873 arc_buf_destroy(obuf
, db
);
1874 db
->db
.db_size
= size
;
1876 dr
= list_head(&db
->db_dirty_records
);
1877 if (db
->db_level
== 0)
1878 dr
->dt
.dl
.dr_data
= buf
;
1879 ASSERT3U(dr
->dr_txg
, ==, tx
->tx_txg
);
1880 ASSERT3U(dr
->dr_accounted
, ==, osize
);
1881 dr
->dr_accounted
= size
;
1882 mutex_exit(&db
->db_mtx
);
1884 dmu_objset_willuse_space(dn
->dn_objset
, size
- osize
, tx
);
1889 dbuf_release_bp(dmu_buf_impl_t
*db
)
1891 objset_t
*os __maybe_unused
= db
->db_objset
;
1893 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os
)));
1894 ASSERT(arc_released(os
->os_phys_buf
) ||
1895 list_link_active(&os
->os_dsl_dataset
->ds_synced_link
));
1896 ASSERT(db
->db_parent
== NULL
|| arc_released(db
->db_parent
->db_buf
));
1898 (void) arc_release(db
->db_buf
, db
);
1902 * We already have a dirty record for this TXG, and we are being
1906 dbuf_redirty(dbuf_dirty_record_t
*dr
)
1908 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
1910 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1912 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
) {
1914 * If this buffer has already been written out,
1915 * we now need to reset its state.
1917 dbuf_unoverride(dr
);
1918 if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
&&
1919 db
->db_state
!= DB_NOFILL
) {
1920 /* Already released on initial dirty, so just thaw. */
1921 ASSERT(arc_released(db
->db_buf
));
1922 arc_buf_thaw(db
->db_buf
);
1927 dbuf_dirty_record_t
*
1928 dbuf_dirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1932 dbuf_dirty_record_t
*dr
, *dr_next
, *dr_head
;
1933 int txgoff
= tx
->tx_txg
& TXG_MASK
;
1934 boolean_t drop_struct_rwlock
= B_FALSE
;
1936 ASSERT(tx
->tx_txg
!= 0);
1937 ASSERT(!zfs_refcount_is_zero(&db
->db_holds
));
1938 DMU_TX_DIRTY_BUF(tx
, db
);
1943 * Shouldn't dirty a regular buffer in syncing context. Private
1944 * objects may be dirtied in syncing context, but only if they
1945 * were already pre-dirtied in open context.
1948 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
) {
1949 rrw_enter(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
,
1952 ASSERT(!dmu_tx_is_syncing(tx
) ||
1953 BP_IS_HOLE(dn
->dn_objset
->os_rootbp
) ||
1954 DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
1955 dn
->dn_objset
->os_dsl_dataset
== NULL
);
1956 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
1957 rrw_exit(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
, FTAG
);
1960 * We make this assert for private objects as well, but after we
1961 * check if we're already dirty. They are allowed to re-dirty
1962 * in syncing context.
1964 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
1965 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
1966 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
1968 mutex_enter(&db
->db_mtx
);
1970 * XXX make this true for indirects too? The problem is that
1971 * transactions created with dmu_tx_create_assigned() from
1972 * syncing context don't bother holding ahead.
1974 ASSERT(db
->db_level
!= 0 ||
1975 db
->db_state
== DB_CACHED
|| db
->db_state
== DB_FILL
||
1976 db
->db_state
== DB_NOFILL
);
1978 mutex_enter(&dn
->dn_mtx
);
1980 * Don't set dirtyctx to SYNC if we're just modifying this as we
1981 * initialize the objset.
1983 if (dn
->dn_dirtyctx
== DN_UNDIRTIED
) {
1984 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
) {
1985 rrw_enter(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
,
1988 if (!BP_IS_HOLE(dn
->dn_objset
->os_rootbp
)) {
1989 dn
->dn_dirtyctx
= (dmu_tx_is_syncing(tx
) ?
1990 DN_DIRTY_SYNC
: DN_DIRTY_OPEN
);
1991 ASSERT(dn
->dn_dirtyctx_firstset
== NULL
);
1992 dn
->dn_dirtyctx_firstset
= kmem_alloc(1, KM_SLEEP
);
1994 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
) {
1995 rrw_exit(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
,
2000 if (tx
->tx_txg
> dn
->dn_dirty_txg
)
2001 dn
->dn_dirty_txg
= tx
->tx_txg
;
2002 mutex_exit(&dn
->dn_mtx
);
2004 if (db
->db_blkid
== DMU_SPILL_BLKID
)
2005 dn
->dn_have_spill
= B_TRUE
;
2008 * If this buffer is already dirty, we're done.
2010 dr_head
= list_head(&db
->db_dirty_records
);
2011 ASSERT(dr_head
== NULL
|| dr_head
->dr_txg
<= tx
->tx_txg
||
2012 db
->db
.db_object
== DMU_META_DNODE_OBJECT
);
2013 dr_next
= dbuf_find_dirty_lte(db
, tx
->tx_txg
);
2014 if (dr_next
&& dr_next
->dr_txg
== tx
->tx_txg
) {
2017 dbuf_redirty(dr_next
);
2018 mutex_exit(&db
->db_mtx
);
2023 * Only valid if not already dirty.
2025 ASSERT(dn
->dn_object
== 0 ||
2026 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
2027 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
2029 ASSERT3U(dn
->dn_nlevels
, >, db
->db_level
);
2032 * We should only be dirtying in syncing context if it's the
2033 * mos or we're initializing the os or it's a special object.
2034 * However, we are allowed to dirty in syncing context provided
2035 * we already dirtied it in open context. Hence we must make
2036 * this assertion only if we're not already dirty.
2039 VERIFY3U(tx
->tx_txg
, <=, spa_final_dirty_txg(os
->os_spa
));
2041 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
2042 rrw_enter(&os
->os_dsl_dataset
->ds_bp_rwlock
, RW_READER
, FTAG
);
2043 ASSERT(!dmu_tx_is_syncing(tx
) || DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
2044 os
->os_dsl_dataset
== NULL
|| BP_IS_HOLE(os
->os_rootbp
));
2045 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
2046 rrw_exit(&os
->os_dsl_dataset
->ds_bp_rwlock
, FTAG
);
2048 ASSERT(db
->db
.db_size
!= 0);
2050 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
2052 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
2053 dmu_objset_willuse_space(os
, db
->db
.db_size
, tx
);
2057 * If this buffer is dirty in an old transaction group we need
2058 * to make a copy of it so that the changes we make in this
2059 * transaction group won't leak out when we sync the older txg.
2061 dr
= kmem_zalloc(sizeof (dbuf_dirty_record_t
), KM_SLEEP
);
2062 list_link_init(&dr
->dr_dirty_node
);
2063 list_link_init(&dr
->dr_dbuf_node
);
2064 if (db
->db_level
== 0) {
2065 void *data_old
= db
->db_buf
;
2067 if (db
->db_state
!= DB_NOFILL
) {
2068 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2069 dbuf_fix_old_data(db
, tx
->tx_txg
);
2070 data_old
= db
->db
.db_data
;
2071 } else if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
) {
2073 * Release the data buffer from the cache so
2074 * that we can modify it without impacting
2075 * possible other users of this cached data
2076 * block. Note that indirect blocks and
2077 * private objects are not released until the
2078 * syncing state (since they are only modified
2081 arc_release(db
->db_buf
, db
);
2082 dbuf_fix_old_data(db
, tx
->tx_txg
);
2083 data_old
= db
->db_buf
;
2085 ASSERT(data_old
!= NULL
);
2087 dr
->dt
.dl
.dr_data
= data_old
;
2089 mutex_init(&dr
->dt
.di
.dr_mtx
, NULL
, MUTEX_NOLOCKDEP
, NULL
);
2090 list_create(&dr
->dt
.di
.dr_children
,
2091 sizeof (dbuf_dirty_record_t
),
2092 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
2094 if (db
->db_blkid
!= DMU_BONUS_BLKID
)
2095 dr
->dr_accounted
= db
->db
.db_size
;
2097 dr
->dr_txg
= tx
->tx_txg
;
2098 list_insert_before(&db
->db_dirty_records
, dr_next
, dr
);
2101 * We could have been freed_in_flight between the dbuf_noread
2102 * and dbuf_dirty. We win, as though the dbuf_noread() had
2103 * happened after the free.
2105 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
2106 db
->db_blkid
!= DMU_SPILL_BLKID
) {
2107 mutex_enter(&dn
->dn_mtx
);
2108 if (dn
->dn_free_ranges
[txgoff
] != NULL
) {
2109 range_tree_clear(dn
->dn_free_ranges
[txgoff
],
2112 mutex_exit(&dn
->dn_mtx
);
2113 db
->db_freed_in_flight
= FALSE
;
2117 * This buffer is now part of this txg
2119 dbuf_add_ref(db
, (void *)(uintptr_t)tx
->tx_txg
);
2120 db
->db_dirtycnt
+= 1;
2121 ASSERT3U(db
->db_dirtycnt
, <=, 3);
2123 mutex_exit(&db
->db_mtx
);
2125 if (db
->db_blkid
== DMU_BONUS_BLKID
||
2126 db
->db_blkid
== DMU_SPILL_BLKID
) {
2127 mutex_enter(&dn
->dn_mtx
);
2128 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
2129 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
2130 mutex_exit(&dn
->dn_mtx
);
2131 dnode_setdirty(dn
, tx
);
2136 if (!RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
2137 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
2138 drop_struct_rwlock
= B_TRUE
;
2142 * If we are overwriting a dedup BP, then unless it is snapshotted,
2143 * when we get to syncing context we will need to decrement its
2144 * refcount in the DDT. Prefetch the relevant DDT block so that
2145 * syncing context won't have to wait for the i/o.
2147 if (db
->db_blkptr
!= NULL
) {
2148 db_lock_type_t dblt
= dmu_buf_lock_parent(db
, RW_READER
, FTAG
);
2149 ddt_prefetch(os
->os_spa
, db
->db_blkptr
);
2150 dmu_buf_unlock_parent(db
, dblt
, FTAG
);
2154 * We need to hold the dn_struct_rwlock to make this assertion,
2155 * because it protects dn_phys / dn_next_nlevels from changing.
2157 ASSERT((dn
->dn_phys
->dn_nlevels
== 0 && db
->db_level
== 0) ||
2158 dn
->dn_phys
->dn_nlevels
> db
->db_level
||
2159 dn
->dn_next_nlevels
[txgoff
] > db
->db_level
||
2160 dn
->dn_next_nlevels
[(tx
->tx_txg
-1) & TXG_MASK
] > db
->db_level
||
2161 dn
->dn_next_nlevels
[(tx
->tx_txg
-2) & TXG_MASK
] > db
->db_level
);
2164 if (db
->db_level
== 0) {
2165 ASSERT(!db
->db_objset
->os_raw_receive
||
2166 dn
->dn_maxblkid
>= db
->db_blkid
);
2167 dnode_new_blkid(dn
, db
->db_blkid
, tx
,
2168 drop_struct_rwlock
, B_FALSE
);
2169 ASSERT(dn
->dn_maxblkid
>= db
->db_blkid
);
2172 if (db
->db_level
+1 < dn
->dn_nlevels
) {
2173 dmu_buf_impl_t
*parent
= db
->db_parent
;
2174 dbuf_dirty_record_t
*di
;
2175 int parent_held
= FALSE
;
2177 if (db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
) {
2178 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2179 parent
= dbuf_hold_level(dn
, db
->db_level
+ 1,
2180 db
->db_blkid
>> epbs
, FTAG
);
2181 ASSERT(parent
!= NULL
);
2184 if (drop_struct_rwlock
)
2185 rw_exit(&dn
->dn_struct_rwlock
);
2186 ASSERT3U(db
->db_level
+ 1, ==, parent
->db_level
);
2187 di
= dbuf_dirty(parent
, tx
);
2189 dbuf_rele(parent
, FTAG
);
2191 mutex_enter(&db
->db_mtx
);
2193 * Since we've dropped the mutex, it's possible that
2194 * dbuf_undirty() might have changed this out from under us.
2196 if (list_head(&db
->db_dirty_records
) == dr
||
2197 dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
2198 mutex_enter(&di
->dt
.di
.dr_mtx
);
2199 ASSERT3U(di
->dr_txg
, ==, tx
->tx_txg
);
2200 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
2201 list_insert_tail(&di
->dt
.di
.dr_children
, dr
);
2202 mutex_exit(&di
->dt
.di
.dr_mtx
);
2205 mutex_exit(&db
->db_mtx
);
2207 ASSERT(db
->db_level
+ 1 == dn
->dn_nlevels
);
2208 ASSERT(db
->db_blkid
< dn
->dn_nblkptr
);
2209 ASSERT(db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
2210 mutex_enter(&dn
->dn_mtx
);
2211 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
2212 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
2213 mutex_exit(&dn
->dn_mtx
);
2214 if (drop_struct_rwlock
)
2215 rw_exit(&dn
->dn_struct_rwlock
);
2218 dnode_setdirty(dn
, tx
);
2224 * Undirty a buffer in the transaction group referenced by the given
2225 * transaction. Return whether this evicted the dbuf.
2228 dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
2231 uint64_t txg
= tx
->tx_txg
;
2232 dbuf_dirty_record_t
*dr
;
2237 * Due to our use of dn_nlevels below, this can only be called
2238 * in open context, unless we are operating on the MOS.
2239 * From syncing context, dn_nlevels may be different from the
2240 * dn_nlevels used when dbuf was dirtied.
2242 ASSERT(db
->db_objset
==
2243 dmu_objset_pool(db
->db_objset
)->dp_meta_objset
||
2244 txg
!= spa_syncing_txg(dmu_objset_spa(db
->db_objset
)));
2245 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2246 ASSERT0(db
->db_level
);
2247 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2250 * If this buffer is not dirty, we're done.
2252 dr
= dbuf_find_dirty_eq(db
, txg
);
2255 ASSERT(dr
->dr_dbuf
== db
);
2260 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
2262 ASSERT(db
->db
.db_size
!= 0);
2264 dsl_pool_undirty_space(dmu_objset_pool(dn
->dn_objset
),
2265 dr
->dr_accounted
, txg
);
2267 list_remove(&db
->db_dirty_records
, dr
);
2270 * Note that there are three places in dbuf_dirty()
2271 * where this dirty record may be put on a list.
2272 * Make sure to do a list_remove corresponding to
2273 * every one of those list_insert calls.
2275 if (dr
->dr_parent
) {
2276 mutex_enter(&dr
->dr_parent
->dt
.di
.dr_mtx
);
2277 list_remove(&dr
->dr_parent
->dt
.di
.dr_children
, dr
);
2278 mutex_exit(&dr
->dr_parent
->dt
.di
.dr_mtx
);
2279 } else if (db
->db_blkid
== DMU_SPILL_BLKID
||
2280 db
->db_level
+ 1 == dn
->dn_nlevels
) {
2281 ASSERT(db
->db_blkptr
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
2282 mutex_enter(&dn
->dn_mtx
);
2283 list_remove(&dn
->dn_dirty_records
[txg
& TXG_MASK
], dr
);
2284 mutex_exit(&dn
->dn_mtx
);
2288 if (db
->db_state
!= DB_NOFILL
) {
2289 dbuf_unoverride(dr
);
2291 ASSERT(db
->db_buf
!= NULL
);
2292 ASSERT(dr
->dt
.dl
.dr_data
!= NULL
);
2293 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
2294 arc_buf_destroy(dr
->dt
.dl
.dr_data
, db
);
2297 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
2299 ASSERT(db
->db_dirtycnt
> 0);
2300 db
->db_dirtycnt
-= 1;
2302 if (zfs_refcount_remove(&db
->db_holds
, (void *)(uintptr_t)txg
) == 0) {
2303 ASSERT(db
->db_state
== DB_NOFILL
|| arc_released(db
->db_buf
));
2312 dmu_buf_will_dirty_impl(dmu_buf_t
*db_fake
, int flags
, dmu_tx_t
*tx
)
2314 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2316 ASSERT(tx
->tx_txg
!= 0);
2317 ASSERT(!zfs_refcount_is_zero(&db
->db_holds
));
2320 * Quick check for dirtiness. For already dirty blocks, this
2321 * reduces runtime of this function by >90%, and overall performance
2322 * by 50% for some workloads (e.g. file deletion with indirect blocks
2325 mutex_enter(&db
->db_mtx
);
2327 if (db
->db_state
== DB_CACHED
) {
2328 dbuf_dirty_record_t
*dr
= dbuf_find_dirty_eq(db
, tx
->tx_txg
);
2330 * It's possible that it is already dirty but not cached,
2331 * because there are some calls to dbuf_dirty() that don't
2332 * go through dmu_buf_will_dirty().
2335 /* This dbuf is already dirty and cached. */
2337 mutex_exit(&db
->db_mtx
);
2341 mutex_exit(&db
->db_mtx
);
2344 if (RW_WRITE_HELD(&DB_DNODE(db
)->dn_struct_rwlock
))
2345 flags
|= DB_RF_HAVESTRUCT
;
2347 (void) dbuf_read(db
, NULL
, flags
);
2348 (void) dbuf_dirty(db
, tx
);
2352 dmu_buf_will_dirty(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
2354 dmu_buf_will_dirty_impl(db_fake
,
2355 DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
, tx
);
2359 dmu_buf_is_dirty(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
2361 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2362 dbuf_dirty_record_t
*dr
;
2364 mutex_enter(&db
->db_mtx
);
2365 dr
= dbuf_find_dirty_eq(db
, tx
->tx_txg
);
2366 mutex_exit(&db
->db_mtx
);
2367 return (dr
!= NULL
);
2371 dmu_buf_will_not_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
2373 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2375 db
->db_state
= DB_NOFILL
;
2377 dmu_buf_will_fill(db_fake
, tx
);
2381 dmu_buf_will_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
2383 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2385 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2386 ASSERT(tx
->tx_txg
!= 0);
2387 ASSERT(db
->db_level
== 0);
2388 ASSERT(!zfs_refcount_is_zero(&db
->db_holds
));
2390 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
||
2391 dmu_tx_private_ok(tx
));
2394 (void) dbuf_dirty(db
, tx
);
2398 * This function is effectively the same as dmu_buf_will_dirty(), but
2399 * indicates the caller expects raw encrypted data in the db, and provides
2400 * the crypt params (byteorder, salt, iv, mac) which should be stored in the
2401 * blkptr_t when this dbuf is written. This is only used for blocks of
2402 * dnodes, during raw receive.
2405 dmu_buf_set_crypt_params(dmu_buf_t
*db_fake
, boolean_t byteorder
,
2406 const uint8_t *salt
, const uint8_t *iv
, const uint8_t *mac
, dmu_tx_t
*tx
)
2408 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2409 dbuf_dirty_record_t
*dr
;
2412 * dr_has_raw_params is only processed for blocks of dnodes
2413 * (see dbuf_sync_dnode_leaf_crypt()).
2415 ASSERT3U(db
->db
.db_object
, ==, DMU_META_DNODE_OBJECT
);
2416 ASSERT3U(db
->db_level
, ==, 0);
2417 ASSERT(db
->db_objset
->os_raw_receive
);
2419 dmu_buf_will_dirty_impl(db_fake
,
2420 DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
| DB_RF_NO_DECRYPT
, tx
);
2422 dr
= dbuf_find_dirty_eq(db
, tx
->tx_txg
);
2424 ASSERT3P(dr
, !=, NULL
);
2426 dr
->dt
.dl
.dr_has_raw_params
= B_TRUE
;
2427 dr
->dt
.dl
.dr_byteorder
= byteorder
;
2428 bcopy(salt
, dr
->dt
.dl
.dr_salt
, ZIO_DATA_SALT_LEN
);
2429 bcopy(iv
, dr
->dt
.dl
.dr_iv
, ZIO_DATA_IV_LEN
);
2430 bcopy(mac
, dr
->dt
.dl
.dr_mac
, ZIO_DATA_MAC_LEN
);
2434 dbuf_override_impl(dmu_buf_impl_t
*db
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
2436 struct dirty_leaf
*dl
;
2437 dbuf_dirty_record_t
*dr
;
2439 dr
= list_head(&db
->db_dirty_records
);
2440 ASSERT3U(dr
->dr_txg
, ==, tx
->tx_txg
);
2442 dl
->dr_overridden_by
= *bp
;
2443 dl
->dr_override_state
= DR_OVERRIDDEN
;
2444 dl
->dr_overridden_by
.blk_birth
= dr
->dr_txg
;
2449 dmu_buf_fill_done(dmu_buf_t
*dbuf
, dmu_tx_t
*tx
)
2451 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbuf
;
2452 mutex_enter(&db
->db_mtx
);
2455 if (db
->db_state
== DB_FILL
) {
2456 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
2457 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2458 /* we were freed while filling */
2459 /* XXX dbuf_undirty? */
2460 bzero(db
->db
.db_data
, db
->db
.db_size
);
2461 db
->db_freed_in_flight
= FALSE
;
2463 db
->db_state
= DB_CACHED
;
2464 cv_broadcast(&db
->db_changed
);
2466 mutex_exit(&db
->db_mtx
);
2470 dmu_buf_write_embedded(dmu_buf_t
*dbuf
, void *data
,
2471 bp_embedded_type_t etype
, enum zio_compress comp
,
2472 int uncompressed_size
, int compressed_size
, int byteorder
,
2475 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbuf
;
2476 struct dirty_leaf
*dl
;
2477 dmu_object_type_t type
;
2478 dbuf_dirty_record_t
*dr
;
2480 if (etype
== BP_EMBEDDED_TYPE_DATA
) {
2481 ASSERT(spa_feature_is_active(dmu_objset_spa(db
->db_objset
),
2482 SPA_FEATURE_EMBEDDED_DATA
));
2486 type
= DB_DNODE(db
)->dn_type
;
2489 ASSERT0(db
->db_level
);
2490 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2492 dmu_buf_will_not_fill(dbuf
, tx
);
2494 dr
= list_head(&db
->db_dirty_records
);
2495 ASSERT3U(dr
->dr_txg
, ==, tx
->tx_txg
);
2497 encode_embedded_bp_compressed(&dl
->dr_overridden_by
,
2498 data
, comp
, uncompressed_size
, compressed_size
);
2499 BPE_SET_ETYPE(&dl
->dr_overridden_by
, etype
);
2500 BP_SET_TYPE(&dl
->dr_overridden_by
, type
);
2501 BP_SET_LEVEL(&dl
->dr_overridden_by
, 0);
2502 BP_SET_BYTEORDER(&dl
->dr_overridden_by
, byteorder
);
2504 dl
->dr_override_state
= DR_OVERRIDDEN
;
2505 dl
->dr_overridden_by
.blk_birth
= dr
->dr_txg
;
2509 dmu_buf_redact(dmu_buf_t
*dbuf
, dmu_tx_t
*tx
)
2511 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbuf
;
2512 dmu_object_type_t type
;
2513 ASSERT(dsl_dataset_feature_is_active(db
->db_objset
->os_dsl_dataset
,
2514 SPA_FEATURE_REDACTED_DATASETS
));
2517 type
= DB_DNODE(db
)->dn_type
;
2520 ASSERT0(db
->db_level
);
2521 dmu_buf_will_not_fill(dbuf
, tx
);
2523 blkptr_t bp
= { { { {0} } } };
2524 BP_SET_TYPE(&bp
, type
);
2525 BP_SET_LEVEL(&bp
, 0);
2526 BP_SET_BIRTH(&bp
, tx
->tx_txg
, 0);
2527 BP_SET_REDACTED(&bp
);
2528 BPE_SET_LSIZE(&bp
, dbuf
->db_size
);
2530 dbuf_override_impl(db
, &bp
, tx
);
2534 * Directly assign a provided arc buf to a given dbuf if it's not referenced
2535 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
2538 dbuf_assign_arcbuf(dmu_buf_impl_t
*db
, arc_buf_t
*buf
, dmu_tx_t
*tx
)
2540 ASSERT(!zfs_refcount_is_zero(&db
->db_holds
));
2541 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2542 ASSERT(db
->db_level
== 0);
2543 ASSERT3U(dbuf_is_metadata(db
), ==, arc_is_metadata(buf
));
2544 ASSERT(buf
!= NULL
);
2545 ASSERT3U(arc_buf_lsize(buf
), ==, db
->db
.db_size
);
2546 ASSERT(tx
->tx_txg
!= 0);
2548 arc_return_buf(buf
, db
);
2549 ASSERT(arc_released(buf
));
2551 mutex_enter(&db
->db_mtx
);
2553 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
2554 cv_wait(&db
->db_changed
, &db
->db_mtx
);
2556 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_UNCACHED
);
2558 if (db
->db_state
== DB_CACHED
&&
2559 zfs_refcount_count(&db
->db_holds
) - 1 > db
->db_dirtycnt
) {
2561 * In practice, we will never have a case where we have an
2562 * encrypted arc buffer while additional holds exist on the
2563 * dbuf. We don't handle this here so we simply assert that
2566 ASSERT(!arc_is_encrypted(buf
));
2567 mutex_exit(&db
->db_mtx
);
2568 (void) dbuf_dirty(db
, tx
);
2569 bcopy(buf
->b_data
, db
->db
.db_data
, db
->db
.db_size
);
2570 arc_buf_destroy(buf
, db
);
2571 xuio_stat_wbuf_copied();
2575 xuio_stat_wbuf_nocopy();
2576 if (db
->db_state
== DB_CACHED
) {
2577 dbuf_dirty_record_t
*dr
= list_head(&db
->db_dirty_records
);
2579 ASSERT(db
->db_buf
!= NULL
);
2580 if (dr
!= NULL
&& dr
->dr_txg
== tx
->tx_txg
) {
2581 ASSERT(dr
->dt
.dl
.dr_data
== db
->db_buf
);
2583 if (!arc_released(db
->db_buf
)) {
2584 ASSERT(dr
->dt
.dl
.dr_override_state
==
2586 arc_release(db
->db_buf
, db
);
2588 dr
->dt
.dl
.dr_data
= buf
;
2589 arc_buf_destroy(db
->db_buf
, db
);
2590 } else if (dr
== NULL
|| dr
->dt
.dl
.dr_data
!= db
->db_buf
) {
2591 arc_release(db
->db_buf
, db
);
2592 arc_buf_destroy(db
->db_buf
, db
);
2596 ASSERT(db
->db_buf
== NULL
);
2597 dbuf_set_data(db
, buf
);
2598 db
->db_state
= DB_FILL
;
2599 mutex_exit(&db
->db_mtx
);
2600 (void) dbuf_dirty(db
, tx
);
2601 dmu_buf_fill_done(&db
->db
, tx
);
2605 dbuf_destroy(dmu_buf_impl_t
*db
)
2608 dmu_buf_impl_t
*parent
= db
->db_parent
;
2609 dmu_buf_impl_t
*dndb
;
2611 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2612 ASSERT(zfs_refcount_is_zero(&db
->db_holds
));
2614 if (db
->db_buf
!= NULL
) {
2615 arc_buf_destroy(db
->db_buf
, db
);
2619 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2620 int slots
= DB_DNODE(db
)->dn_num_slots
;
2621 int bonuslen
= DN_SLOTS_TO_BONUSLEN(slots
);
2622 if (db
->db
.db_data
!= NULL
) {
2623 kmem_free(db
->db
.db_data
, bonuslen
);
2624 arc_space_return(bonuslen
, ARC_SPACE_BONUS
);
2625 db
->db_state
= DB_UNCACHED
;
2629 dbuf_clear_data(db
);
2631 if (multilist_link_active(&db
->db_cache_link
)) {
2632 ASSERT(db
->db_caching_status
== DB_DBUF_CACHE
||
2633 db
->db_caching_status
== DB_DBUF_METADATA_CACHE
);
2635 multilist_remove(dbuf_caches
[db
->db_caching_status
].cache
, db
);
2636 (void) zfs_refcount_remove_many(
2637 &dbuf_caches
[db
->db_caching_status
].size
,
2638 db
->db
.db_size
, db
);
2640 if (db
->db_caching_status
== DB_DBUF_METADATA_CACHE
) {
2641 DBUF_STAT_BUMPDOWN(metadata_cache_count
);
2643 DBUF_STAT_BUMPDOWN(cache_levels
[db
->db_level
]);
2644 DBUF_STAT_BUMPDOWN(cache_count
);
2645 DBUF_STAT_DECR(cache_levels_bytes
[db
->db_level
],
2648 db
->db_caching_status
= DB_NO_CACHE
;
2651 ASSERT(db
->db_state
== DB_UNCACHED
|| db
->db_state
== DB_NOFILL
);
2652 ASSERT(db
->db_data_pending
== NULL
);
2654 db
->db_state
= DB_EVICTING
;
2655 db
->db_blkptr
= NULL
;
2658 * Now that db_state is DB_EVICTING, nobody else can find this via
2659 * the hash table. We can now drop db_mtx, which allows us to
2660 * acquire the dn_dbufs_mtx.
2662 mutex_exit(&db
->db_mtx
);
2667 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
2668 boolean_t needlock
= !MUTEX_HELD(&dn
->dn_dbufs_mtx
);
2670 mutex_enter_nested(&dn
->dn_dbufs_mtx
,
2672 avl_remove(&dn
->dn_dbufs
, db
);
2673 atomic_dec_32(&dn
->dn_dbufs_count
);
2677 mutex_exit(&dn
->dn_dbufs_mtx
);
2679 * Decrementing the dbuf count means that the hold corresponding
2680 * to the removed dbuf is no longer discounted in dnode_move(),
2681 * so the dnode cannot be moved until after we release the hold.
2682 * The membar_producer() ensures visibility of the decremented
2683 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
2686 mutex_enter(&dn
->dn_mtx
);
2687 dnode_rele_and_unlock(dn
, db
, B_TRUE
);
2688 db
->db_dnode_handle
= NULL
;
2690 dbuf_hash_remove(db
);
2695 ASSERT(zfs_refcount_is_zero(&db
->db_holds
));
2697 db
->db_parent
= NULL
;
2699 ASSERT(db
->db_buf
== NULL
);
2700 ASSERT(db
->db
.db_data
== NULL
);
2701 ASSERT(db
->db_hash_next
== NULL
);
2702 ASSERT(db
->db_blkptr
== NULL
);
2703 ASSERT(db
->db_data_pending
== NULL
);
2704 ASSERT3U(db
->db_caching_status
, ==, DB_NO_CACHE
);
2705 ASSERT(!multilist_link_active(&db
->db_cache_link
));
2707 kmem_cache_free(dbuf_kmem_cache
, db
);
2708 arc_space_return(sizeof (dmu_buf_impl_t
), ARC_SPACE_DBUF
);
2711 * If this dbuf is referenced from an indirect dbuf,
2712 * decrement the ref count on the indirect dbuf.
2714 if (parent
&& parent
!= dndb
) {
2715 mutex_enter(&parent
->db_mtx
);
2716 dbuf_rele_and_unlock(parent
, db
, B_TRUE
);
2721 * Note: While bpp will always be updated if the function returns success,
2722 * parentp will not be updated if the dnode does not have dn_dbuf filled in;
2723 * this happens when the dnode is the meta-dnode, or {user|group|project}used
2726 __attribute__((always_inline
))
2728 dbuf_findbp(dnode_t
*dn
, int level
, uint64_t blkid
, int fail_sparse
,
2729 dmu_buf_impl_t
**parentp
, blkptr_t
**bpp
)
2734 ASSERT(blkid
!= DMU_BONUS_BLKID
);
2736 if (blkid
== DMU_SPILL_BLKID
) {
2737 mutex_enter(&dn
->dn_mtx
);
2738 if (dn
->dn_have_spill
&&
2739 (dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
))
2740 *bpp
= DN_SPILL_BLKPTR(dn
->dn_phys
);
2743 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
2744 *parentp
= dn
->dn_dbuf
;
2745 mutex_exit(&dn
->dn_mtx
);
2750 (dn
->dn_phys
->dn_nlevels
== 0) ? 1 : dn
->dn_phys
->dn_nlevels
;
2751 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2753 ASSERT3U(level
* epbs
, <, 64);
2754 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2756 * This assertion shouldn't trip as long as the max indirect block size
2757 * is less than 1M. The reason for this is that up to that point,
2758 * the number of levels required to address an entire object with blocks
2759 * of size SPA_MINBLOCKSIZE satisfies nlevels * epbs + 1 <= 64. In
2760 * other words, if N * epbs + 1 > 64, then if (N-1) * epbs + 1 > 55
2761 * (i.e. we can address the entire object), objects will all use at most
2762 * N-1 levels and the assertion won't overflow. However, once epbs is
2763 * 13, 4 * 13 + 1 = 53, but 5 * 13 + 1 = 66. Then, 4 levels will not be
2764 * enough to address an entire object, so objects will have 5 levels,
2765 * but then this assertion will overflow.
2767 * All this is to say that if we ever increase DN_MAX_INDBLKSHIFT, we
2768 * need to redo this logic to handle overflows.
2770 ASSERT(level
>= nlevels
||
2771 ((nlevels
- level
- 1) * epbs
) +
2772 highbit64(dn
->dn_phys
->dn_nblkptr
) <= 64);
2773 if (level
>= nlevels
||
2774 blkid
>= ((uint64_t)dn
->dn_phys
->dn_nblkptr
<<
2775 ((nlevels
- level
- 1) * epbs
)) ||
2777 blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))) {
2778 /* the buffer has no parent yet */
2779 return (SET_ERROR(ENOENT
));
2780 } else if (level
< nlevels
-1) {
2781 /* this block is referenced from an indirect block */
2784 err
= dbuf_hold_impl(dn
, level
+ 1,
2785 blkid
>> epbs
, fail_sparse
, FALSE
, NULL
, parentp
);
2789 err
= dbuf_read(*parentp
, NULL
,
2790 (DB_RF_HAVESTRUCT
| DB_RF_NOPREFETCH
| DB_RF_CANFAIL
));
2792 dbuf_rele(*parentp
, NULL
);
2796 rw_enter(&(*parentp
)->db_rwlock
, RW_READER
);
2797 *bpp
= ((blkptr_t
*)(*parentp
)->db
.db_data
) +
2798 (blkid
& ((1ULL << epbs
) - 1));
2799 if (blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))
2800 ASSERT(BP_IS_HOLE(*bpp
));
2801 rw_exit(&(*parentp
)->db_rwlock
);
2804 /* the block is referenced from the dnode */
2805 ASSERT3U(level
, ==, nlevels
-1);
2806 ASSERT(dn
->dn_phys
->dn_nblkptr
== 0 ||
2807 blkid
< dn
->dn_phys
->dn_nblkptr
);
2809 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
2810 *parentp
= dn
->dn_dbuf
;
2812 *bpp
= &dn
->dn_phys
->dn_blkptr
[blkid
];
2817 static dmu_buf_impl_t
*
2818 dbuf_create(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
2819 dmu_buf_impl_t
*parent
, blkptr_t
*blkptr
)
2821 objset_t
*os
= dn
->dn_objset
;
2822 dmu_buf_impl_t
*db
, *odb
;
2824 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2825 ASSERT(dn
->dn_type
!= DMU_OT_NONE
);
2827 db
= kmem_cache_alloc(dbuf_kmem_cache
, KM_SLEEP
);
2829 list_create(&db
->db_dirty_records
, sizeof (dbuf_dirty_record_t
),
2830 offsetof(dbuf_dirty_record_t
, dr_dbuf_node
));
2833 db
->db
.db_object
= dn
->dn_object
;
2834 db
->db_level
= level
;
2835 db
->db_blkid
= blkid
;
2836 db
->db_dirtycnt
= 0;
2837 db
->db_dnode_handle
= dn
->dn_handle
;
2838 db
->db_parent
= parent
;
2839 db
->db_blkptr
= blkptr
;
2842 db
->db_user_immediate_evict
= FALSE
;
2843 db
->db_freed_in_flight
= FALSE
;
2844 db
->db_pending_evict
= FALSE
;
2846 if (blkid
== DMU_BONUS_BLKID
) {
2847 ASSERT3P(parent
, ==, dn
->dn_dbuf
);
2848 db
->db
.db_size
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
) -
2849 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
);
2850 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
2851 db
->db
.db_offset
= DMU_BONUS_BLKID
;
2852 db
->db_state
= DB_UNCACHED
;
2853 db
->db_caching_status
= DB_NO_CACHE
;
2854 /* the bonus dbuf is not placed in the hash table */
2855 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_DBUF
);
2857 } else if (blkid
== DMU_SPILL_BLKID
) {
2858 db
->db
.db_size
= (blkptr
!= NULL
) ?
2859 BP_GET_LSIZE(blkptr
) : SPA_MINBLOCKSIZE
;
2860 db
->db
.db_offset
= 0;
2863 db
->db_level
? 1 << dn
->dn_indblkshift
: dn
->dn_datablksz
;
2864 db
->db
.db_size
= blocksize
;
2865 db
->db
.db_offset
= db
->db_blkid
* blocksize
;
2869 * Hold the dn_dbufs_mtx while we get the new dbuf
2870 * in the hash table *and* added to the dbufs list.
2871 * This prevents a possible deadlock with someone
2872 * trying to look up this dbuf before it's added to the
2875 mutex_enter(&dn
->dn_dbufs_mtx
);
2876 db
->db_state
= DB_EVICTING
;
2877 if ((odb
= dbuf_hash_insert(db
)) != NULL
) {
2878 /* someone else inserted it first */
2879 kmem_cache_free(dbuf_kmem_cache
, db
);
2880 mutex_exit(&dn
->dn_dbufs_mtx
);
2881 DBUF_STAT_BUMP(hash_insert_race
);
2884 avl_add(&dn
->dn_dbufs
, db
);
2886 db
->db_state
= DB_UNCACHED
;
2887 db
->db_caching_status
= DB_NO_CACHE
;
2888 mutex_exit(&dn
->dn_dbufs_mtx
);
2889 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_DBUF
);
2891 if (parent
&& parent
!= dn
->dn_dbuf
)
2892 dbuf_add_ref(parent
, db
);
2894 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
2895 zfs_refcount_count(&dn
->dn_holds
) > 0);
2896 (void) zfs_refcount_add(&dn
->dn_holds
, db
);
2897 atomic_inc_32(&dn
->dn_dbufs_count
);
2899 dprintf_dbuf(db
, "db=%p\n", db
);
2905 * This function returns a block pointer and information about the object,
2906 * given a dnode and a block. This is a publicly accessible version of
2907 * dbuf_findbp that only returns some information, rather than the
2908 * dbuf. Note that the dnode passed in must be held, and the dn_struct_rwlock
2909 * should be locked as (at least) a reader.
2912 dbuf_dnode_findbp(dnode_t
*dn
, uint64_t level
, uint64_t blkid
,
2913 blkptr_t
*bp
, uint16_t *datablkszsec
, uint8_t *indblkshift
)
2915 dmu_buf_impl_t
*dbp
= NULL
;
2918 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2920 err
= dbuf_findbp(dn
, level
, blkid
, B_FALSE
, &dbp
, &bp2
);
2924 dbuf_rele(dbp
, NULL
);
2925 if (datablkszsec
!= NULL
)
2926 *datablkszsec
= dn
->dn_phys
->dn_datablkszsec
;
2927 if (indblkshift
!= NULL
)
2928 *indblkshift
= dn
->dn_phys
->dn_indblkshift
;
2934 typedef struct dbuf_prefetch_arg
{
2935 spa_t
*dpa_spa
; /* The spa to issue the prefetch in. */
2936 zbookmark_phys_t dpa_zb
; /* The target block to prefetch. */
2937 int dpa_epbs
; /* Entries (blkptr_t's) Per Block Shift. */
2938 int dpa_curlevel
; /* The current level that we're reading */
2939 dnode_t
*dpa_dnode
; /* The dnode associated with the prefetch */
2940 zio_priority_t dpa_prio
; /* The priority I/Os should be issued at. */
2941 zio_t
*dpa_zio
; /* The parent zio_t for all prefetches. */
2942 arc_flags_t dpa_aflags
; /* Flags to pass to the final prefetch. */
2943 } dbuf_prefetch_arg_t
;
2946 * Actually issue the prefetch read for the block given.
2949 dbuf_issue_final_prefetch(dbuf_prefetch_arg_t
*dpa
, blkptr_t
*bp
)
2951 ASSERT(!BP_IS_REDACTED(bp
) ||
2952 dsl_dataset_feature_is_active(
2953 dpa
->dpa_dnode
->dn_objset
->os_dsl_dataset
,
2954 SPA_FEATURE_REDACTED_DATASETS
));
2956 if (BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
) || BP_IS_REDACTED(bp
))
2959 int zio_flags
= ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
;
2960 arc_flags_t aflags
=
2961 dpa
->dpa_aflags
| ARC_FLAG_NOWAIT
| ARC_FLAG_PREFETCH
;
2963 /* dnodes are always read as raw and then converted later */
2964 if (BP_GET_TYPE(bp
) == DMU_OT_DNODE
&& BP_IS_PROTECTED(bp
) &&
2965 dpa
->dpa_curlevel
== 0)
2966 zio_flags
|= ZIO_FLAG_RAW
;
2968 ASSERT3U(dpa
->dpa_curlevel
, ==, BP_GET_LEVEL(bp
));
2969 ASSERT3U(dpa
->dpa_curlevel
, ==, dpa
->dpa_zb
.zb_level
);
2970 ASSERT(dpa
->dpa_zio
!= NULL
);
2971 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
, bp
, NULL
, NULL
,
2972 dpa
->dpa_prio
, zio_flags
, &aflags
, &dpa
->dpa_zb
);
2976 * Called when an indirect block above our prefetch target is read in. This
2977 * will either read in the next indirect block down the tree or issue the actual
2978 * prefetch if the next block down is our target.
2981 dbuf_prefetch_indirect_done(zio_t
*zio
, const zbookmark_phys_t
*zb
,
2982 const blkptr_t
*iobp
, arc_buf_t
*abuf
, void *private)
2984 dbuf_prefetch_arg_t
*dpa
= private;
2986 ASSERT3S(dpa
->dpa_zb
.zb_level
, <, dpa
->dpa_curlevel
);
2987 ASSERT3S(dpa
->dpa_curlevel
, >, 0);
2990 ASSERT(zio
== NULL
|| zio
->io_error
!= 0);
2991 kmem_free(dpa
, sizeof (*dpa
));
2994 ASSERT(zio
== NULL
|| zio
->io_error
== 0);
2997 * The dpa_dnode is only valid if we are called with a NULL
2998 * zio. This indicates that the arc_read() returned without
2999 * first calling zio_read() to issue a physical read. Once
3000 * a physical read is made the dpa_dnode must be invalidated
3001 * as the locks guarding it may have been dropped. If the
3002 * dpa_dnode is still valid, then we want to add it to the dbuf
3003 * cache. To do so, we must hold the dbuf associated with the block
3004 * we just prefetched, read its contents so that we associate it
3005 * with an arc_buf_t, and then release it.
3008 ASSERT3S(BP_GET_LEVEL(zio
->io_bp
), ==, dpa
->dpa_curlevel
);
3009 if (zio
->io_flags
& ZIO_FLAG_RAW_COMPRESS
) {
3010 ASSERT3U(BP_GET_PSIZE(zio
->io_bp
), ==, zio
->io_size
);
3012 ASSERT3U(BP_GET_LSIZE(zio
->io_bp
), ==, zio
->io_size
);
3014 ASSERT3P(zio
->io_spa
, ==, dpa
->dpa_spa
);
3016 dpa
->dpa_dnode
= NULL
;
3017 } else if (dpa
->dpa_dnode
!= NULL
) {
3018 uint64_t curblkid
= dpa
->dpa_zb
.zb_blkid
>>
3019 (dpa
->dpa_epbs
* (dpa
->dpa_curlevel
-
3020 dpa
->dpa_zb
.zb_level
));
3021 dmu_buf_impl_t
*db
= dbuf_hold_level(dpa
->dpa_dnode
,
3022 dpa
->dpa_curlevel
, curblkid
, FTAG
);
3024 kmem_free(dpa
, sizeof (*dpa
));
3025 arc_buf_destroy(abuf
, private);
3029 (void) dbuf_read(db
, NULL
,
3030 DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
| DB_RF_HAVESTRUCT
);
3031 dbuf_rele(db
, FTAG
);
3034 dpa
->dpa_curlevel
--;
3035 uint64_t nextblkid
= dpa
->dpa_zb
.zb_blkid
>>
3036 (dpa
->dpa_epbs
* (dpa
->dpa_curlevel
- dpa
->dpa_zb
.zb_level
));
3037 blkptr_t
*bp
= ((blkptr_t
*)abuf
->b_data
) +
3038 P2PHASE(nextblkid
, 1ULL << dpa
->dpa_epbs
);
3040 ASSERT(!BP_IS_REDACTED(bp
) ||
3041 dsl_dataset_feature_is_active(
3042 dpa
->dpa_dnode
->dn_objset
->os_dsl_dataset
,
3043 SPA_FEATURE_REDACTED_DATASETS
));
3044 if (BP_IS_HOLE(bp
) || BP_IS_REDACTED(bp
)) {
3045 kmem_free(dpa
, sizeof (*dpa
));
3046 } else if (dpa
->dpa_curlevel
== dpa
->dpa_zb
.zb_level
) {
3047 ASSERT3U(nextblkid
, ==, dpa
->dpa_zb
.zb_blkid
);
3048 dbuf_issue_final_prefetch(dpa
, bp
);
3049 kmem_free(dpa
, sizeof (*dpa
));
3051 arc_flags_t iter_aflags
= ARC_FLAG_NOWAIT
;
3052 zbookmark_phys_t zb
;
3054 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
3055 if (dpa
->dpa_aflags
& ARC_FLAG_L2CACHE
)
3056 iter_aflags
|= ARC_FLAG_L2CACHE
;
3058 ASSERT3U(dpa
->dpa_curlevel
, ==, BP_GET_LEVEL(bp
));
3060 SET_BOOKMARK(&zb
, dpa
->dpa_zb
.zb_objset
,
3061 dpa
->dpa_zb
.zb_object
, dpa
->dpa_curlevel
, nextblkid
);
3063 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
,
3064 bp
, dbuf_prefetch_indirect_done
, dpa
, dpa
->dpa_prio
,
3065 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
3069 arc_buf_destroy(abuf
, private);
3073 * Issue prefetch reads for the given block on the given level. If the indirect
3074 * blocks above that block are not in memory, we will read them in
3075 * asynchronously. As a result, this call never blocks waiting for a read to
3076 * complete. Note that the prefetch might fail if the dataset is encrypted and
3077 * the encryption key is unmapped before the IO completes.
3080 dbuf_prefetch(dnode_t
*dn
, int64_t level
, uint64_t blkid
, zio_priority_t prio
,
3084 int epbs
, nlevels
, curlevel
;
3087 ASSERT(blkid
!= DMU_BONUS_BLKID
);
3088 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
3090 if (blkid
> dn
->dn_maxblkid
)
3093 if (level
== 0 && dnode_block_freed(dn
, blkid
))
3097 * This dnode hasn't been written to disk yet, so there's nothing to
3100 nlevels
= dn
->dn_phys
->dn_nlevels
;
3101 if (level
>= nlevels
|| dn
->dn_phys
->dn_nblkptr
== 0)
3104 epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
3105 if (dn
->dn_phys
->dn_maxblkid
< blkid
<< (epbs
* level
))
3108 dmu_buf_impl_t
*db
= dbuf_find(dn
->dn_objset
, dn
->dn_object
,
3111 mutex_exit(&db
->db_mtx
);
3113 * This dbuf already exists. It is either CACHED, or
3114 * (we assume) about to be read or filled.
3120 * Find the closest ancestor (indirect block) of the target block
3121 * that is present in the cache. In this indirect block, we will
3122 * find the bp that is at curlevel, curblkid.
3126 while (curlevel
< nlevels
- 1) {
3127 int parent_level
= curlevel
+ 1;
3128 uint64_t parent_blkid
= curblkid
>> epbs
;
3131 if (dbuf_hold_impl(dn
, parent_level
, parent_blkid
,
3132 FALSE
, TRUE
, FTAG
, &db
) == 0) {
3133 blkptr_t
*bpp
= db
->db_buf
->b_data
;
3134 bp
= bpp
[P2PHASE(curblkid
, 1 << epbs
)];
3135 dbuf_rele(db
, FTAG
);
3139 curlevel
= parent_level
;
3140 curblkid
= parent_blkid
;
3143 if (curlevel
== nlevels
- 1) {
3144 /* No cached indirect blocks found. */
3145 ASSERT3U(curblkid
, <, dn
->dn_phys
->dn_nblkptr
);
3146 bp
= dn
->dn_phys
->dn_blkptr
[curblkid
];
3148 ASSERT(!BP_IS_REDACTED(&bp
) ||
3149 dsl_dataset_feature_is_active(dn
->dn_objset
->os_dsl_dataset
,
3150 SPA_FEATURE_REDACTED_DATASETS
));
3151 if (BP_IS_HOLE(&bp
) || BP_IS_REDACTED(&bp
))
3154 ASSERT3U(curlevel
, ==, BP_GET_LEVEL(&bp
));
3156 zio_t
*pio
= zio_root(dmu_objset_spa(dn
->dn_objset
), NULL
, NULL
,
3159 dbuf_prefetch_arg_t
*dpa
= kmem_zalloc(sizeof (*dpa
), KM_SLEEP
);
3160 dsl_dataset_t
*ds
= dn
->dn_objset
->os_dsl_dataset
;
3161 SET_BOOKMARK(&dpa
->dpa_zb
, ds
!= NULL
? ds
->ds_object
: DMU_META_OBJSET
,
3162 dn
->dn_object
, level
, blkid
);
3163 dpa
->dpa_curlevel
= curlevel
;
3164 dpa
->dpa_prio
= prio
;
3165 dpa
->dpa_aflags
= aflags
;
3166 dpa
->dpa_spa
= dn
->dn_objset
->os_spa
;
3167 dpa
->dpa_dnode
= dn
;
3168 dpa
->dpa_epbs
= epbs
;
3171 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
3172 if (DNODE_LEVEL_IS_L2CACHEABLE(dn
, level
))
3173 dpa
->dpa_aflags
|= ARC_FLAG_L2CACHE
;
3176 * If we have the indirect just above us, no need to do the asynchronous
3177 * prefetch chain; we'll just run the last step ourselves. If we're at
3178 * a higher level, though, we want to issue the prefetches for all the
3179 * indirect blocks asynchronously, so we can go on with whatever we were
3182 if (curlevel
== level
) {
3183 ASSERT3U(curblkid
, ==, blkid
);
3184 dbuf_issue_final_prefetch(dpa
, &bp
);
3185 kmem_free(dpa
, sizeof (*dpa
));
3187 arc_flags_t iter_aflags
= ARC_FLAG_NOWAIT
;
3188 zbookmark_phys_t zb
;
3190 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
3191 if (DNODE_LEVEL_IS_L2CACHEABLE(dn
, level
))
3192 iter_aflags
|= ARC_FLAG_L2CACHE
;
3194 SET_BOOKMARK(&zb
, ds
!= NULL
? ds
->ds_object
: DMU_META_OBJSET
,
3195 dn
->dn_object
, curlevel
, curblkid
);
3196 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
,
3197 &bp
, dbuf_prefetch_indirect_done
, dpa
, prio
,
3198 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
3202 * We use pio here instead of dpa_zio since it's possible that
3203 * dpa may have already been freed.
3209 * Helper function for dbuf_hold_impl() to copy a buffer. Handles
3210 * the case of encrypted, compressed and uncompressed buffers by
3211 * allocating the new buffer, respectively, with arc_alloc_raw_buf(),
3212 * arc_alloc_compressed_buf() or arc_alloc_buf().*
3214 * NOTE: Declared noinline to avoid stack bloat in dbuf_hold_impl().
3216 noinline
static void
3217 dbuf_hold_copy(dnode_t
*dn
, dmu_buf_impl_t
*db
)
3219 dbuf_dirty_record_t
*dr
= db
->db_data_pending
;
3220 arc_buf_t
*data
= dr
->dt
.dl
.dr_data
;
3221 enum zio_compress compress_type
= arc_get_compression(data
);
3223 if (arc_is_encrypted(data
)) {
3224 boolean_t byteorder
;
3225 uint8_t salt
[ZIO_DATA_SALT_LEN
];
3226 uint8_t iv
[ZIO_DATA_IV_LEN
];
3227 uint8_t mac
[ZIO_DATA_MAC_LEN
];
3229 arc_get_raw_params(data
, &byteorder
, salt
, iv
, mac
);
3230 dbuf_set_data(db
, arc_alloc_raw_buf(dn
->dn_objset
->os_spa
, db
,
3231 dmu_objset_id(dn
->dn_objset
), byteorder
, salt
, iv
, mac
,
3232 dn
->dn_type
, arc_buf_size(data
), arc_buf_lsize(data
),
3234 } else if (compress_type
!= ZIO_COMPRESS_OFF
) {
3235 dbuf_set_data(db
, arc_alloc_compressed_buf(
3236 dn
->dn_objset
->os_spa
, db
, arc_buf_size(data
),
3237 arc_buf_lsize(data
), compress_type
));
3239 dbuf_set_data(db
, arc_alloc_buf(dn
->dn_objset
->os_spa
, db
,
3240 DBUF_GET_BUFC_TYPE(db
), db
->db
.db_size
));
3243 rw_enter(&db
->db_rwlock
, RW_WRITER
);
3244 bcopy(data
->b_data
, db
->db
.db_data
, arc_buf_size(data
));
3245 rw_exit(&db
->db_rwlock
);
3249 * Returns with db_holds incremented, and db_mtx not held.
3250 * Note: dn_struct_rwlock must be held.
3253 dbuf_hold_impl(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
3254 boolean_t fail_sparse
, boolean_t fail_uncached
,
3255 void *tag
, dmu_buf_impl_t
**dbp
)
3257 dmu_buf_impl_t
*db
, *parent
= NULL
;
3259 /* If the pool has been created, verify the tx_sync_lock is not held */
3260 spa_t
*spa
= dn
->dn_objset
->os_spa
;
3261 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
3263 ASSERT(!MUTEX_HELD(&dp
->dp_tx
.tx_sync_lock
));
3266 ASSERT(blkid
!= DMU_BONUS_BLKID
);
3267 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
3268 ASSERT3U(dn
->dn_nlevels
, >, level
);
3272 /* dbuf_find() returns with db_mtx held */
3273 db
= dbuf_find(dn
->dn_objset
, dn
->dn_object
, level
, blkid
);
3276 blkptr_t
*bp
= NULL
;
3280 return (SET_ERROR(ENOENT
));
3282 ASSERT3P(parent
, ==, NULL
);
3283 err
= dbuf_findbp(dn
, level
, blkid
, fail_sparse
, &parent
, &bp
);
3285 if (err
== 0 && bp
&& BP_IS_HOLE(bp
))
3286 err
= SET_ERROR(ENOENT
);
3289 dbuf_rele(parent
, NULL
);
3293 if (err
&& err
!= ENOENT
)
3295 db
= dbuf_create(dn
, level
, blkid
, parent
, bp
);
3298 if (fail_uncached
&& db
->db_state
!= DB_CACHED
) {
3299 mutex_exit(&db
->db_mtx
);
3300 return (SET_ERROR(ENOENT
));
3303 if (db
->db_buf
!= NULL
) {
3304 arc_buf_access(db
->db_buf
);
3305 ASSERT3P(db
->db
.db_data
, ==, db
->db_buf
->b_data
);
3308 ASSERT(db
->db_buf
== NULL
|| arc_referenced(db
->db_buf
));
3311 * If this buffer is currently syncing out, and we are
3312 * still referencing it from db_data, we need to make a copy
3313 * of it in case we decide we want to dirty it again in this txg.
3315 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
3316 dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
3317 db
->db_state
== DB_CACHED
&& db
->db_data_pending
) {
3318 dbuf_dirty_record_t
*dr
= db
->db_data_pending
;
3319 if (dr
->dt
.dl
.dr_data
== db
->db_buf
)
3320 dbuf_hold_copy(dn
, db
);
3323 if (multilist_link_active(&db
->db_cache_link
)) {
3324 ASSERT(zfs_refcount_is_zero(&db
->db_holds
));
3325 ASSERT(db
->db_caching_status
== DB_DBUF_CACHE
||
3326 db
->db_caching_status
== DB_DBUF_METADATA_CACHE
);
3328 multilist_remove(dbuf_caches
[db
->db_caching_status
].cache
, db
);
3329 (void) zfs_refcount_remove_many(
3330 &dbuf_caches
[db
->db_caching_status
].size
,
3331 db
->db
.db_size
, db
);
3333 if (db
->db_caching_status
== DB_DBUF_METADATA_CACHE
) {
3334 DBUF_STAT_BUMPDOWN(metadata_cache_count
);
3336 DBUF_STAT_BUMPDOWN(cache_levels
[db
->db_level
]);
3337 DBUF_STAT_BUMPDOWN(cache_count
);
3338 DBUF_STAT_DECR(cache_levels_bytes
[db
->db_level
],
3341 db
->db_caching_status
= DB_NO_CACHE
;
3343 (void) zfs_refcount_add(&db
->db_holds
, tag
);
3345 mutex_exit(&db
->db_mtx
);
3347 /* NOTE: we can't rele the parent until after we drop the db_mtx */
3349 dbuf_rele(parent
, NULL
);
3351 ASSERT3P(DB_DNODE(db
), ==, dn
);
3352 ASSERT3U(db
->db_blkid
, ==, blkid
);
3353 ASSERT3U(db
->db_level
, ==, level
);
3360 dbuf_hold(dnode_t
*dn
, uint64_t blkid
, void *tag
)
3362 return (dbuf_hold_level(dn
, 0, blkid
, tag
));
3366 dbuf_hold_level(dnode_t
*dn
, int level
, uint64_t blkid
, void *tag
)
3369 int err
= dbuf_hold_impl(dn
, level
, blkid
, FALSE
, FALSE
, tag
, &db
);
3370 return (err
? NULL
: db
);
3374 dbuf_create_bonus(dnode_t
*dn
)
3376 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
3378 ASSERT(dn
->dn_bonus
== NULL
);
3379 dn
->dn_bonus
= dbuf_create(dn
, 0, DMU_BONUS_BLKID
, dn
->dn_dbuf
, NULL
);
3383 dbuf_spill_set_blksz(dmu_buf_t
*db_fake
, uint64_t blksz
, dmu_tx_t
*tx
)
3385 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3387 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
3388 return (SET_ERROR(ENOTSUP
));
3390 blksz
= SPA_MINBLOCKSIZE
;
3391 ASSERT3U(blksz
, <=, spa_maxblocksize(dmu_objset_spa(db
->db_objset
)));
3392 blksz
= P2ROUNDUP(blksz
, SPA_MINBLOCKSIZE
);
3394 dbuf_new_size(db
, blksz
, tx
);
3400 dbuf_rm_spill(dnode_t
*dn
, dmu_tx_t
*tx
)
3402 dbuf_free_range(dn
, DMU_SPILL_BLKID
, DMU_SPILL_BLKID
, tx
);
3405 #pragma weak dmu_buf_add_ref = dbuf_add_ref
3407 dbuf_add_ref(dmu_buf_impl_t
*db
, void *tag
)
3409 int64_t holds
= zfs_refcount_add(&db
->db_holds
, tag
);
3410 VERIFY3S(holds
, >, 1);
3413 #pragma weak dmu_buf_try_add_ref = dbuf_try_add_ref
3415 dbuf_try_add_ref(dmu_buf_t
*db_fake
, objset_t
*os
, uint64_t obj
, uint64_t blkid
,
3418 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3419 dmu_buf_impl_t
*found_db
;
3420 boolean_t result
= B_FALSE
;
3422 if (blkid
== DMU_BONUS_BLKID
)
3423 found_db
= dbuf_find_bonus(os
, obj
);
3425 found_db
= dbuf_find(os
, obj
, 0, blkid
);
3427 if (found_db
!= NULL
) {
3428 if (db
== found_db
&& dbuf_refcount(db
) > db
->db_dirtycnt
) {
3429 (void) zfs_refcount_add(&db
->db_holds
, tag
);
3432 mutex_exit(&found_db
->db_mtx
);
3438 * If you call dbuf_rele() you had better not be referencing the dnode handle
3439 * unless you have some other direct or indirect hold on the dnode. (An indirect
3440 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
3441 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
3442 * dnode's parent dbuf evicting its dnode handles.
3445 dbuf_rele(dmu_buf_impl_t
*db
, void *tag
)
3447 mutex_enter(&db
->db_mtx
);
3448 dbuf_rele_and_unlock(db
, tag
, B_FALSE
);
3452 dmu_buf_rele(dmu_buf_t
*db
, void *tag
)
3454 dbuf_rele((dmu_buf_impl_t
*)db
, tag
);
3458 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
3459 * db_dirtycnt and db_holds to be updated atomically. The 'evicting'
3460 * argument should be set if we are already in the dbuf-evicting code
3461 * path, in which case we don't want to recursively evict. This allows us to
3462 * avoid deeply nested stacks that would have a call flow similar to this:
3464 * dbuf_rele()-->dbuf_rele_and_unlock()-->dbuf_evict_notify()
3467 * +-----dbuf_destroy()<--dbuf_evict_one()<--------+
3471 dbuf_rele_and_unlock(dmu_buf_impl_t
*db
, void *tag
, boolean_t evicting
)
3475 ASSERT(MUTEX_HELD(&db
->db_mtx
));
3479 * Remove the reference to the dbuf before removing its hold on the
3480 * dnode so we can guarantee in dnode_move() that a referenced bonus
3481 * buffer has a corresponding dnode hold.
3483 holds
= zfs_refcount_remove(&db
->db_holds
, tag
);
3487 * We can't freeze indirects if there is a possibility that they
3488 * may be modified in the current syncing context.
3490 if (db
->db_buf
!= NULL
&&
3491 holds
== (db
->db_level
== 0 ? db
->db_dirtycnt
: 0)) {
3492 arc_buf_freeze(db
->db_buf
);
3495 if (holds
== db
->db_dirtycnt
&&
3496 db
->db_level
== 0 && db
->db_user_immediate_evict
)
3497 dbuf_evict_user(db
);
3500 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
3502 boolean_t evict_dbuf
= db
->db_pending_evict
;
3505 * If the dnode moves here, we cannot cross this
3506 * barrier until the move completes.
3511 atomic_dec_32(&dn
->dn_dbufs_count
);
3514 * Decrementing the dbuf count means that the bonus
3515 * buffer's dnode hold is no longer discounted in
3516 * dnode_move(). The dnode cannot move until after
3517 * the dnode_rele() below.
3522 * Do not reference db after its lock is dropped.
3523 * Another thread may evict it.
3525 mutex_exit(&db
->db_mtx
);
3528 dnode_evict_bonus(dn
);
3531 } else if (db
->db_buf
== NULL
) {
3533 * This is a special case: we never associated this
3534 * dbuf with any data allocated from the ARC.
3536 ASSERT(db
->db_state
== DB_UNCACHED
||
3537 db
->db_state
== DB_NOFILL
);
3539 } else if (arc_released(db
->db_buf
)) {
3541 * This dbuf has anonymous data associated with it.
3545 boolean_t do_arc_evict
= B_FALSE
;
3547 spa_t
*spa
= dmu_objset_spa(db
->db_objset
);
3549 if (!DBUF_IS_CACHEABLE(db
) &&
3550 db
->db_blkptr
!= NULL
&&
3551 !BP_IS_HOLE(db
->db_blkptr
) &&
3552 !BP_IS_EMBEDDED(db
->db_blkptr
)) {
3553 do_arc_evict
= B_TRUE
;
3554 bp
= *db
->db_blkptr
;
3557 if (!DBUF_IS_CACHEABLE(db
) ||
3558 db
->db_pending_evict
) {
3560 } else if (!multilist_link_active(&db
->db_cache_link
)) {
3561 ASSERT3U(db
->db_caching_status
, ==,
3564 dbuf_cached_state_t dcs
=
3565 dbuf_include_in_metadata_cache(db
) ?
3566 DB_DBUF_METADATA_CACHE
: DB_DBUF_CACHE
;
3567 db
->db_caching_status
= dcs
;
3569 multilist_insert(dbuf_caches
[dcs
].cache
, db
);
3570 (void) zfs_refcount_add_many(
3571 &dbuf_caches
[dcs
].size
,
3572 db
->db
.db_size
, db
);
3574 if (dcs
== DB_DBUF_METADATA_CACHE
) {
3575 DBUF_STAT_BUMP(metadata_cache_count
);
3577 metadata_cache_size_bytes_max
,
3579 &dbuf_caches
[dcs
].size
));
3582 cache_levels
[db
->db_level
]);
3583 DBUF_STAT_BUMP(cache_count
);
3585 cache_levels_bytes
[db
->db_level
],
3587 DBUF_STAT_MAX(cache_size_bytes_max
,
3589 &dbuf_caches
[dcs
].size
));
3591 mutex_exit(&db
->db_mtx
);
3593 if (db
->db_caching_status
== DB_DBUF_CACHE
&&
3595 dbuf_evict_notify();
3600 arc_freed(spa
, &bp
);
3603 mutex_exit(&db
->db_mtx
);
3608 #pragma weak dmu_buf_refcount = dbuf_refcount
3610 dbuf_refcount(dmu_buf_impl_t
*db
)
3612 return (zfs_refcount_count(&db
->db_holds
));
3616 dmu_buf_user_refcount(dmu_buf_t
*db_fake
)
3619 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3621 mutex_enter(&db
->db_mtx
);
3622 ASSERT3U(zfs_refcount_count(&db
->db_holds
), >=, db
->db_dirtycnt
);
3623 holds
= zfs_refcount_count(&db
->db_holds
) - db
->db_dirtycnt
;
3624 mutex_exit(&db
->db_mtx
);
3630 dmu_buf_replace_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*old_user
,
3631 dmu_buf_user_t
*new_user
)
3633 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3635 mutex_enter(&db
->db_mtx
);
3636 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
3637 if (db
->db_user
== old_user
)
3638 db
->db_user
= new_user
;
3640 old_user
= db
->db_user
;
3641 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
3642 mutex_exit(&db
->db_mtx
);
3648 dmu_buf_set_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
3650 return (dmu_buf_replace_user(db_fake
, NULL
, user
));
3654 dmu_buf_set_user_ie(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
3656 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3658 db
->db_user_immediate_evict
= TRUE
;
3659 return (dmu_buf_set_user(db_fake
, user
));
3663 dmu_buf_remove_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
3665 return (dmu_buf_replace_user(db_fake
, user
, NULL
));
3669 dmu_buf_get_user(dmu_buf_t
*db_fake
)
3671 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3673 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
3674 return (db
->db_user
);
3678 dmu_buf_user_evict_wait()
3680 taskq_wait(dbu_evict_taskq
);
3684 dmu_buf_get_blkptr(dmu_buf_t
*db
)
3686 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3687 return (dbi
->db_blkptr
);
3691 dmu_buf_get_objset(dmu_buf_t
*db
)
3693 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3694 return (dbi
->db_objset
);
3698 dmu_buf_dnode_enter(dmu_buf_t
*db
)
3700 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3701 DB_DNODE_ENTER(dbi
);
3702 return (DB_DNODE(dbi
));
3706 dmu_buf_dnode_exit(dmu_buf_t
*db
)
3708 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3713 dbuf_check_blkptr(dnode_t
*dn
, dmu_buf_impl_t
*db
)
3715 /* ASSERT(dmu_tx_is_syncing(tx) */
3716 ASSERT(MUTEX_HELD(&db
->db_mtx
));
3718 if (db
->db_blkptr
!= NULL
)
3721 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3722 db
->db_blkptr
= DN_SPILL_BLKPTR(dn
->dn_phys
);
3723 BP_ZERO(db
->db_blkptr
);
3726 if (db
->db_level
== dn
->dn_phys
->dn_nlevels
-1) {
3728 * This buffer was allocated at a time when there was
3729 * no available blkptrs from the dnode, or it was
3730 * inappropriate to hook it in (i.e., nlevels mismatch).
3732 ASSERT(db
->db_blkid
< dn
->dn_phys
->dn_nblkptr
);
3733 ASSERT(db
->db_parent
== NULL
);
3734 db
->db_parent
= dn
->dn_dbuf
;
3735 db
->db_blkptr
= &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
];
3738 dmu_buf_impl_t
*parent
= db
->db_parent
;
3739 int epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
3741 ASSERT(dn
->dn_phys
->dn_nlevels
> 1);
3742 if (parent
== NULL
) {
3743 mutex_exit(&db
->db_mtx
);
3744 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
3745 parent
= dbuf_hold_level(dn
, db
->db_level
+ 1,
3746 db
->db_blkid
>> epbs
, db
);
3747 rw_exit(&dn
->dn_struct_rwlock
);
3748 mutex_enter(&db
->db_mtx
);
3749 db
->db_parent
= parent
;
3751 db
->db_blkptr
= (blkptr_t
*)parent
->db
.db_data
+
3752 (db
->db_blkid
& ((1ULL << epbs
) - 1));
3758 * When syncing out a blocks of dnodes, adjust the block to deal with
3759 * encryption. Normally, we make sure the block is decrypted before writing
3760 * it. If we have crypt params, then we are writing a raw (encrypted) block,
3761 * from a raw receive. In this case, set the ARC buf's crypt params so
3762 * that the BP will be filled with the correct byteorder, salt, iv, and mac.
3765 dbuf_prepare_encrypted_dnode_leaf(dbuf_dirty_record_t
*dr
)
3768 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3770 ASSERT(MUTEX_HELD(&db
->db_mtx
));
3771 ASSERT3U(db
->db
.db_object
, ==, DMU_META_DNODE_OBJECT
);
3772 ASSERT3U(db
->db_level
, ==, 0);
3774 if (!db
->db_objset
->os_raw_receive
&& arc_is_encrypted(db
->db_buf
)) {
3775 zbookmark_phys_t zb
;
3778 * Unfortunately, there is currently no mechanism for
3779 * syncing context to handle decryption errors. An error
3780 * here is only possible if an attacker maliciously
3781 * changed a dnode block and updated the associated
3782 * checksums going up the block tree.
3784 SET_BOOKMARK(&zb
, dmu_objset_id(db
->db_objset
),
3785 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
3786 err
= arc_untransform(db
->db_buf
, db
->db_objset
->os_spa
,
3789 panic("Invalid dnode block MAC");
3790 } else if (dr
->dt
.dl
.dr_has_raw_params
) {
3791 (void) arc_release(dr
->dt
.dl
.dr_data
, db
);
3792 arc_convert_to_raw(dr
->dt
.dl
.dr_data
,
3793 dmu_objset_id(db
->db_objset
),
3794 dr
->dt
.dl
.dr_byteorder
, DMU_OT_DNODE
,
3795 dr
->dt
.dl
.dr_salt
, dr
->dt
.dl
.dr_iv
, dr
->dt
.dl
.dr_mac
);
3800 * dbuf_sync_indirect() is called recursively from dbuf_sync_list() so it
3801 * is critical the we not allow the compiler to inline this function in to
3802 * dbuf_sync_list() thereby drastically bloating the stack usage.
3804 noinline
static void
3805 dbuf_sync_indirect(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
3807 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3811 ASSERT(dmu_tx_is_syncing(tx
));
3813 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
3815 mutex_enter(&db
->db_mtx
);
3817 ASSERT(db
->db_level
> 0);
3820 /* Read the block if it hasn't been read yet. */
3821 if (db
->db_buf
== NULL
) {
3822 mutex_exit(&db
->db_mtx
);
3823 (void) dbuf_read(db
, NULL
, DB_RF_MUST_SUCCEED
);
3824 mutex_enter(&db
->db_mtx
);
3826 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
3827 ASSERT(db
->db_buf
!= NULL
);
3831 /* Indirect block size must match what the dnode thinks it is. */
3832 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
3833 dbuf_check_blkptr(dn
, db
);
3836 /* Provide the pending dirty record to child dbufs */
3837 db
->db_data_pending
= dr
;
3839 mutex_exit(&db
->db_mtx
);
3841 dbuf_write(dr
, db
->db_buf
, tx
);
3844 mutex_enter(&dr
->dt
.di
.dr_mtx
);
3845 dbuf_sync_list(&dr
->dt
.di
.dr_children
, db
->db_level
- 1, tx
);
3846 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
3847 mutex_exit(&dr
->dt
.di
.dr_mtx
);
3853 * Verify that the size of the data in our bonus buffer does not exceed
3854 * its recorded size.
3856 * The purpose of this verification is to catch any cases in development
3857 * where the size of a phys structure (i.e space_map_phys_t) grows and,
3858 * due to incorrect feature management, older pools expect to read more
3859 * data even though they didn't actually write it to begin with.
3861 * For a example, this would catch an error in the feature logic where we
3862 * open an older pool and we expect to write the space map histogram of
3863 * a space map with size SPACE_MAP_SIZE_V0.
3866 dbuf_sync_leaf_verify_bonus_dnode(dbuf_dirty_record_t
*dr
)
3868 dnode_t
*dn
= DB_DNODE(dr
->dr_dbuf
);
3871 * Encrypted bonus buffers can have data past their bonuslen.
3872 * Skip the verification of these blocks.
3874 if (DMU_OT_IS_ENCRYPTED(dn
->dn_bonustype
))
3877 uint16_t bonuslen
= dn
->dn_phys
->dn_bonuslen
;
3878 uint16_t maxbonuslen
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
);
3879 ASSERT3U(bonuslen
, <=, maxbonuslen
);
3881 arc_buf_t
*datap
= dr
->dt
.dl
.dr_data
;
3882 char *datap_end
= ((char *)datap
) + bonuslen
;
3883 char *datap_max
= ((char *)datap
) + maxbonuslen
;
3885 /* ensure that everything is zero after our data */
3886 for (; datap_end
< datap_max
; datap_end
++)
3887 ASSERT(*datap_end
== 0);
3892 * dbuf_sync_leaf() is called recursively from dbuf_sync_list() so it is
3893 * critical the we not allow the compiler to inline this function in to
3894 * dbuf_sync_list() thereby drastically bloating the stack usage.
3896 noinline
static void
3897 dbuf_sync_leaf(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
3899 arc_buf_t
**datap
= &dr
->dt
.dl
.dr_data
;
3900 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3903 uint64_t txg
= tx
->tx_txg
;
3905 ASSERT(dmu_tx_is_syncing(tx
));
3907 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
3909 mutex_enter(&db
->db_mtx
);
3911 * To be synced, we must be dirtied. But we
3912 * might have been freed after the dirty.
3914 if (db
->db_state
== DB_UNCACHED
) {
3915 /* This buffer has been freed since it was dirtied */
3916 ASSERT(db
->db
.db_data
== NULL
);
3917 } else if (db
->db_state
== DB_FILL
) {
3918 /* This buffer was freed and is now being re-filled */
3919 ASSERT(db
->db
.db_data
!= dr
->dt
.dl
.dr_data
);
3921 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_NOFILL
);
3928 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3929 mutex_enter(&dn
->dn_mtx
);
3930 if (!(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
)) {
3932 * In the previous transaction group, the bonus buffer
3933 * was entirely used to store the attributes for the
3934 * dnode which overrode the dn_spill field. However,
3935 * when adding more attributes to the file a spill
3936 * block was required to hold the extra attributes.
3938 * Make sure to clear the garbage left in the dn_spill
3939 * field from the previous attributes in the bonus
3940 * buffer. Otherwise, after writing out the spill
3941 * block to the new allocated dva, it will free
3942 * the old block pointed to by the invalid dn_spill.
3944 db
->db_blkptr
= NULL
;
3946 dn
->dn_phys
->dn_flags
|= DNODE_FLAG_SPILL_BLKPTR
;
3947 mutex_exit(&dn
->dn_mtx
);
3951 * If this is a bonus buffer, simply copy the bonus data into the
3952 * dnode. It will be written out when the dnode is synced (and it
3953 * will be synced, since it must have been dirty for dbuf_sync to
3956 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
3957 ASSERT(*datap
!= NULL
);
3958 ASSERT0(db
->db_level
);
3959 ASSERT3U(DN_MAX_BONUS_LEN(dn
->dn_phys
), <=,
3960 DN_SLOTS_TO_BONUSLEN(dn
->dn_phys
->dn_extra_slots
+ 1));
3961 bcopy(*datap
, DN_BONUS(dn
->dn_phys
),
3962 DN_MAX_BONUS_LEN(dn
->dn_phys
));
3966 dbuf_sync_leaf_verify_bonus_dnode(dr
);
3969 if (*datap
!= db
->db
.db_data
) {
3970 int slots
= DB_DNODE(db
)->dn_num_slots
;
3971 int bonuslen
= DN_SLOTS_TO_BONUSLEN(slots
);
3972 kmem_free(*datap
, bonuslen
);
3973 arc_space_return(bonuslen
, ARC_SPACE_BONUS
);
3975 db
->db_data_pending
= NULL
;
3976 ASSERT(list_next(&db
->db_dirty_records
, dr
) == NULL
);
3977 ASSERT(dr
->dr_dbuf
== db
);
3978 list_remove(&db
->db_dirty_records
, dr
);
3979 if (dr
->dr_dbuf
->db_level
!= 0) {
3980 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
3981 list_destroy(&dr
->dt
.di
.dr_children
);
3983 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
3984 ASSERT(db
->db_dirtycnt
> 0);
3985 db
->db_dirtycnt
-= 1;
3986 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)txg
, B_FALSE
);
3993 * This function may have dropped the db_mtx lock allowing a dmu_sync
3994 * operation to sneak in. As a result, we need to ensure that we
3995 * don't check the dr_override_state until we have returned from
3996 * dbuf_check_blkptr.
3998 dbuf_check_blkptr(dn
, db
);
4001 * If this buffer is in the middle of an immediate write,
4002 * wait for the synchronous IO to complete.
4004 while (dr
->dt
.dl
.dr_override_state
== DR_IN_DMU_SYNC
) {
4005 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
4006 cv_wait(&db
->db_changed
, &db
->db_mtx
);
4007 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_NOT_OVERRIDDEN
);
4011 * If this is a dnode block, ensure it is appropriately encrypted
4012 * or decrypted, depending on what we are writing to it this txg.
4014 if (os
->os_encrypted
&& dn
->dn_object
== DMU_META_DNODE_OBJECT
)
4015 dbuf_prepare_encrypted_dnode_leaf(dr
);
4017 if (db
->db_state
!= DB_NOFILL
&&
4018 dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
4019 zfs_refcount_count(&db
->db_holds
) > 1 &&
4020 dr
->dt
.dl
.dr_override_state
!= DR_OVERRIDDEN
&&
4021 *datap
== db
->db_buf
) {
4023 * If this buffer is currently "in use" (i.e., there
4024 * are active holds and db_data still references it),
4025 * then make a copy before we start the write so that
4026 * any modifications from the open txg will not leak
4029 * NOTE: this copy does not need to be made for
4030 * objects only modified in the syncing context (e.g.
4031 * DNONE_DNODE blocks).
4033 int psize
= arc_buf_size(*datap
);
4034 int lsize
= arc_buf_lsize(*datap
);
4035 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
4036 enum zio_compress compress_type
= arc_get_compression(*datap
);
4038 if (arc_is_encrypted(*datap
)) {
4039 boolean_t byteorder
;
4040 uint8_t salt
[ZIO_DATA_SALT_LEN
];
4041 uint8_t iv
[ZIO_DATA_IV_LEN
];
4042 uint8_t mac
[ZIO_DATA_MAC_LEN
];
4044 arc_get_raw_params(*datap
, &byteorder
, salt
, iv
, mac
);
4045 *datap
= arc_alloc_raw_buf(os
->os_spa
, db
,
4046 dmu_objset_id(os
), byteorder
, salt
, iv
, mac
,
4047 dn
->dn_type
, psize
, lsize
, compress_type
);
4048 } else if (compress_type
!= ZIO_COMPRESS_OFF
) {
4049 ASSERT3U(type
, ==, ARC_BUFC_DATA
);
4050 *datap
= arc_alloc_compressed_buf(os
->os_spa
, db
,
4051 psize
, lsize
, compress_type
);
4053 *datap
= arc_alloc_buf(os
->os_spa
, db
, type
, psize
);
4055 bcopy(db
->db
.db_data
, (*datap
)->b_data
, psize
);
4057 db
->db_data_pending
= dr
;
4059 mutex_exit(&db
->db_mtx
);
4061 dbuf_write(dr
, *datap
, tx
);
4063 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
4064 if (dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
4065 list_insert_tail(&dn
->dn_dirty_records
[txg
& TXG_MASK
], dr
);
4069 * Although zio_nowait() does not "wait for an IO", it does
4070 * initiate the IO. If this is an empty write it seems plausible
4071 * that the IO could actually be completed before the nowait
4072 * returns. We need to DB_DNODE_EXIT() first in case
4073 * zio_nowait() invalidates the dbuf.
4076 zio_nowait(dr
->dr_zio
);
4081 dbuf_sync_list(list_t
*list
, int level
, dmu_tx_t
*tx
)
4083 dbuf_dirty_record_t
*dr
;
4085 while ((dr
= list_head(list
))) {
4086 if (dr
->dr_zio
!= NULL
) {
4088 * If we find an already initialized zio then we
4089 * are processing the meta-dnode, and we have finished.
4090 * The dbufs for all dnodes are put back on the list
4091 * during processing, so that we can zio_wait()
4092 * these IOs after initiating all child IOs.
4094 ASSERT3U(dr
->dr_dbuf
->db
.db_object
, ==,
4095 DMU_META_DNODE_OBJECT
);
4098 if (dr
->dr_dbuf
->db_blkid
!= DMU_BONUS_BLKID
&&
4099 dr
->dr_dbuf
->db_blkid
!= DMU_SPILL_BLKID
) {
4100 VERIFY3U(dr
->dr_dbuf
->db_level
, ==, level
);
4102 list_remove(list
, dr
);
4103 if (dr
->dr_dbuf
->db_level
> 0)
4104 dbuf_sync_indirect(dr
, tx
);
4106 dbuf_sync_leaf(dr
, tx
);
4112 dbuf_write_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
4114 dmu_buf_impl_t
*db
= vdb
;
4116 blkptr_t
*bp
= zio
->io_bp
;
4117 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
4118 spa_t
*spa
= zio
->io_spa
;
4123 ASSERT3P(db
->db_blkptr
, !=, NULL
);
4124 ASSERT3P(&db
->db_data_pending
->dr_bp_copy
, ==, bp
);
4128 delta
= bp_get_dsize_sync(spa
, bp
) - bp_get_dsize_sync(spa
, bp_orig
);
4129 dnode_diduse_space(dn
, delta
- zio
->io_prev_space_delta
);
4130 zio
->io_prev_space_delta
= delta
;
4132 if (bp
->blk_birth
!= 0) {
4133 ASSERT((db
->db_blkid
!= DMU_SPILL_BLKID
&&
4134 BP_GET_TYPE(bp
) == dn
->dn_type
) ||
4135 (db
->db_blkid
== DMU_SPILL_BLKID
&&
4136 BP_GET_TYPE(bp
) == dn
->dn_bonustype
) ||
4137 BP_IS_EMBEDDED(bp
));
4138 ASSERT(BP_GET_LEVEL(bp
) == db
->db_level
);
4141 mutex_enter(&db
->db_mtx
);
4144 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
4145 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
4146 ASSERT(!(BP_IS_HOLE(bp
)) &&
4147 db
->db_blkptr
== DN_SPILL_BLKPTR(dn
->dn_phys
));
4151 if (db
->db_level
== 0) {
4152 mutex_enter(&dn
->dn_mtx
);
4153 if (db
->db_blkid
> dn
->dn_phys
->dn_maxblkid
&&
4154 db
->db_blkid
!= DMU_SPILL_BLKID
) {
4155 ASSERT0(db
->db_objset
->os_raw_receive
);
4156 dn
->dn_phys
->dn_maxblkid
= db
->db_blkid
;
4158 mutex_exit(&dn
->dn_mtx
);
4160 if (dn
->dn_type
== DMU_OT_DNODE
) {
4162 while (i
< db
->db
.db_size
) {
4164 (void *)(((char *)db
->db
.db_data
) + i
);
4166 i
+= DNODE_MIN_SIZE
;
4167 if (dnp
->dn_type
!= DMU_OT_NONE
) {
4169 i
+= dnp
->dn_extra_slots
*
4174 if (BP_IS_HOLE(bp
)) {
4181 blkptr_t
*ibp
= db
->db
.db_data
;
4182 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
4183 for (i
= db
->db
.db_size
>> SPA_BLKPTRSHIFT
; i
> 0; i
--, ibp
++) {
4184 if (BP_IS_HOLE(ibp
))
4186 fill
+= BP_GET_FILL(ibp
);
4191 if (!BP_IS_EMBEDDED(bp
))
4192 BP_SET_FILL(bp
, fill
);
4194 mutex_exit(&db
->db_mtx
);
4196 db_lock_type_t dblt
= dmu_buf_lock_parent(db
, RW_WRITER
, FTAG
);
4197 *db
->db_blkptr
= *bp
;
4198 dmu_buf_unlock_parent(db
, dblt
, FTAG
);
4203 * This function gets called just prior to running through the compression
4204 * stage of the zio pipeline. If we're an indirect block comprised of only
4205 * holes, then we want this indirect to be compressed away to a hole. In
4206 * order to do that we must zero out any information about the holes that
4207 * this indirect points to prior to before we try to compress it.
4210 dbuf_write_children_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
4212 dmu_buf_impl_t
*db
= vdb
;
4215 unsigned int epbs
, i
;
4217 ASSERT3U(db
->db_level
, >, 0);
4220 epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
4221 ASSERT3U(epbs
, <, 31);
4223 /* Determine if all our children are holes */
4224 for (i
= 0, bp
= db
->db
.db_data
; i
< 1ULL << epbs
; i
++, bp
++) {
4225 if (!BP_IS_HOLE(bp
))
4230 * If all the children are holes, then zero them all out so that
4231 * we may get compressed away.
4233 if (i
== 1ULL << epbs
) {
4235 * We only found holes. Grab the rwlock to prevent
4236 * anybody from reading the blocks we're about to
4239 rw_enter(&db
->db_rwlock
, RW_WRITER
);
4240 bzero(db
->db
.db_data
, db
->db
.db_size
);
4241 rw_exit(&db
->db_rwlock
);
4247 * The SPA will call this callback several times for each zio - once
4248 * for every physical child i/o (zio->io_phys_children times). This
4249 * allows the DMU to monitor the progress of each logical i/o. For example,
4250 * there may be 2 copies of an indirect block, or many fragments of a RAID-Z
4251 * block. There may be a long delay before all copies/fragments are completed,
4252 * so this callback allows us to retire dirty space gradually, as the physical
4257 dbuf_write_physdone(zio_t
*zio
, arc_buf_t
*buf
, void *arg
)
4259 dmu_buf_impl_t
*db
= arg
;
4260 objset_t
*os
= db
->db_objset
;
4261 dsl_pool_t
*dp
= dmu_objset_pool(os
);
4262 dbuf_dirty_record_t
*dr
;
4265 dr
= db
->db_data_pending
;
4266 ASSERT3U(dr
->dr_txg
, ==, zio
->io_txg
);
4269 * The callback will be called io_phys_children times. Retire one
4270 * portion of our dirty space each time we are called. Any rounding
4271 * error will be cleaned up by dbuf_write_done().
4273 delta
= dr
->dr_accounted
/ zio
->io_phys_children
;
4274 dsl_pool_undirty_space(dp
, delta
, zio
->io_txg
);
4279 dbuf_write_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
4281 dmu_buf_impl_t
*db
= vdb
;
4282 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
4283 blkptr_t
*bp
= db
->db_blkptr
;
4284 objset_t
*os
= db
->db_objset
;
4285 dmu_tx_t
*tx
= os
->os_synctx
;
4286 dbuf_dirty_record_t
*dr
;
4288 ASSERT0(zio
->io_error
);
4289 ASSERT(db
->db_blkptr
== bp
);
4292 * For nopwrites and rewrites we ensure that the bp matches our
4293 * original and bypass all the accounting.
4295 if (zio
->io_flags
& (ZIO_FLAG_IO_REWRITE
| ZIO_FLAG_NOPWRITE
)) {
4296 ASSERT(BP_EQUAL(bp
, bp_orig
));
4298 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
4299 (void) dsl_dataset_block_kill(ds
, bp_orig
, tx
, B_TRUE
);
4300 dsl_dataset_block_born(ds
, bp
, tx
);
4303 mutex_enter(&db
->db_mtx
);
4307 dr
= db
->db_data_pending
;
4308 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
4309 ASSERT(dr
->dr_dbuf
== db
);
4310 ASSERT(list_next(&db
->db_dirty_records
, dr
) == NULL
);
4311 list_remove(&db
->db_dirty_records
, dr
);
4314 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
4319 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
4320 ASSERT(!(BP_IS_HOLE(db
->db_blkptr
)) &&
4321 db
->db_blkptr
== DN_SPILL_BLKPTR(dn
->dn_phys
));
4326 if (db
->db_level
== 0) {
4327 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
4328 ASSERT(dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
);
4329 if (db
->db_state
!= DB_NOFILL
) {
4330 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
4331 arc_buf_destroy(dr
->dt
.dl
.dr_data
, db
);
4338 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
4339 ASSERT3U(db
->db
.db_size
, ==, 1 << dn
->dn_phys
->dn_indblkshift
);
4340 if (!BP_IS_HOLE(db
->db_blkptr
)) {
4341 int epbs __maybe_unused
= dn
->dn_phys
->dn_indblkshift
-
4343 ASSERT3U(db
->db_blkid
, <=,
4344 dn
->dn_phys
->dn_maxblkid
>> (db
->db_level
* epbs
));
4345 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==,
4349 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
4350 list_destroy(&dr
->dt
.di
.dr_children
);
4353 cv_broadcast(&db
->db_changed
);
4354 ASSERT(db
->db_dirtycnt
> 0);
4355 db
->db_dirtycnt
-= 1;
4356 db
->db_data_pending
= NULL
;
4357 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)tx
->tx_txg
, B_FALSE
);
4360 * If we didn't do a physical write in this ZIO and we
4361 * still ended up here, it means that the space of the
4362 * dbuf that we just released (and undirtied) above hasn't
4363 * been marked as undirtied in the pool's accounting.
4365 * Thus, we undirty that space in the pool's view of the
4366 * world here. For physical writes this type of update
4367 * happens in dbuf_write_physdone().
4369 * If we did a physical write, cleanup any rounding errors
4370 * that came up due to writing multiple copies of a block
4371 * on disk [see dbuf_write_physdone()].
4373 if (zio
->io_phys_children
== 0) {
4374 dsl_pool_undirty_space(dmu_objset_pool(os
),
4375 dr
->dr_accounted
, zio
->io_txg
);
4377 dsl_pool_undirty_space(dmu_objset_pool(os
),
4378 dr
->dr_accounted
% zio
->io_phys_children
, zio
->io_txg
);
4381 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
4385 dbuf_write_nofill_ready(zio_t
*zio
)
4387 dbuf_write_ready(zio
, NULL
, zio
->io_private
);
4391 dbuf_write_nofill_done(zio_t
*zio
)
4393 dbuf_write_done(zio
, NULL
, zio
->io_private
);
4397 dbuf_write_override_ready(zio_t
*zio
)
4399 dbuf_dirty_record_t
*dr
= zio
->io_private
;
4400 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
4402 dbuf_write_ready(zio
, NULL
, db
);
4406 dbuf_write_override_done(zio_t
*zio
)
4408 dbuf_dirty_record_t
*dr
= zio
->io_private
;
4409 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
4410 blkptr_t
*obp
= &dr
->dt
.dl
.dr_overridden_by
;
4412 mutex_enter(&db
->db_mtx
);
4413 if (!BP_EQUAL(zio
->io_bp
, obp
)) {
4414 if (!BP_IS_HOLE(obp
))
4415 dsl_free(spa_get_dsl(zio
->io_spa
), zio
->io_txg
, obp
);
4416 arc_release(dr
->dt
.dl
.dr_data
, db
);
4418 mutex_exit(&db
->db_mtx
);
4420 dbuf_write_done(zio
, NULL
, db
);
4422 if (zio
->io_abd
!= NULL
)
4423 abd_put(zio
->io_abd
);
4426 typedef struct dbuf_remap_impl_callback_arg
{
4428 uint64_t drica_blk_birth
;
4430 } dbuf_remap_impl_callback_arg_t
;
4433 dbuf_remap_impl_callback(uint64_t vdev
, uint64_t offset
, uint64_t size
,
4436 dbuf_remap_impl_callback_arg_t
*drica
= arg
;
4437 objset_t
*os
= drica
->drica_os
;
4438 spa_t
*spa
= dmu_objset_spa(os
);
4439 dmu_tx_t
*tx
= drica
->drica_tx
;
4441 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa
)));
4443 if (os
== spa_meta_objset(spa
)) {
4444 spa_vdev_indirect_mark_obsolete(spa
, vdev
, offset
, size
, tx
);
4446 dsl_dataset_block_remapped(dmu_objset_ds(os
), vdev
, offset
,
4447 size
, drica
->drica_blk_birth
, tx
);
4452 dbuf_remap_impl(dnode_t
*dn
, blkptr_t
*bp
, krwlock_t
*rw
, dmu_tx_t
*tx
)
4454 blkptr_t bp_copy
= *bp
;
4455 spa_t
*spa
= dmu_objset_spa(dn
->dn_objset
);
4456 dbuf_remap_impl_callback_arg_t drica
;
4458 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa
)));
4460 drica
.drica_os
= dn
->dn_objset
;
4461 drica
.drica_blk_birth
= bp
->blk_birth
;
4462 drica
.drica_tx
= tx
;
4463 if (spa_remap_blkptr(spa
, &bp_copy
, dbuf_remap_impl_callback
,
4466 * If the blkptr being remapped is tracked by a livelist,
4467 * then we need to make sure the livelist reflects the update.
4468 * First, cancel out the old blkptr by appending a 'FREE'
4469 * entry. Next, add an 'ALLOC' to track the new version. This
4470 * way we avoid trying to free an inaccurate blkptr at delete.
4471 * Note that embedded blkptrs are not tracked in livelists.
4473 if (dn
->dn_objset
!= spa_meta_objset(spa
)) {
4474 dsl_dataset_t
*ds
= dmu_objset_ds(dn
->dn_objset
);
4475 if (dsl_deadlist_is_open(&ds
->ds_dir
->dd_livelist
) &&
4476 bp
->blk_birth
> ds
->ds_dir
->dd_origin_txg
) {
4477 ASSERT(!BP_IS_EMBEDDED(bp
));
4478 ASSERT(dsl_dir_is_clone(ds
->ds_dir
));
4479 ASSERT(spa_feature_is_enabled(spa
,
4480 SPA_FEATURE_LIVELIST
));
4481 bplist_append(&ds
->ds_dir
->dd_pending_frees
,
4483 bplist_append(&ds
->ds_dir
->dd_pending_allocs
,
4489 * The db_rwlock prevents dbuf_read_impl() from
4490 * dereferencing the BP while we are changing it. To
4491 * avoid lock contention, only grab it when we are actually
4495 rw_enter(rw
, RW_WRITER
);
4503 * Remap any existing BP's to concrete vdevs, if possible.
4506 dbuf_remap(dnode_t
*dn
, dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
4508 spa_t
*spa
= dmu_objset_spa(db
->db_objset
);
4509 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa
)));
4511 if (!spa_feature_is_active(spa
, SPA_FEATURE_DEVICE_REMOVAL
))
4514 if (db
->db_level
> 0) {
4515 blkptr_t
*bp
= db
->db
.db_data
;
4516 for (int i
= 0; i
< db
->db
.db_size
>> SPA_BLKPTRSHIFT
; i
++) {
4517 dbuf_remap_impl(dn
, &bp
[i
], &db
->db_rwlock
, tx
);
4519 } else if (db
->db
.db_object
== DMU_META_DNODE_OBJECT
) {
4520 dnode_phys_t
*dnp
= db
->db
.db_data
;
4521 ASSERT3U(db
->db_dnode_handle
->dnh_dnode
->dn_type
, ==,
4523 for (int i
= 0; i
< db
->db
.db_size
>> DNODE_SHIFT
;
4524 i
+= dnp
[i
].dn_extra_slots
+ 1) {
4525 for (int j
= 0; j
< dnp
[i
].dn_nblkptr
; j
++) {
4526 krwlock_t
*lock
= (dn
->dn_dbuf
== NULL
? NULL
:
4527 &dn
->dn_dbuf
->db_rwlock
);
4528 dbuf_remap_impl(dn
, &dnp
[i
].dn_blkptr
[j
], lock
,
4536 /* Issue I/O to commit a dirty buffer to disk. */
4538 dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
)
4540 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
4543 dmu_buf_impl_t
*parent
= db
->db_parent
;
4544 uint64_t txg
= tx
->tx_txg
;
4545 zbookmark_phys_t zb
;
4550 ASSERT(dmu_tx_is_syncing(tx
));
4556 if (db
->db_state
!= DB_NOFILL
) {
4557 if (db
->db_level
> 0 || dn
->dn_type
== DMU_OT_DNODE
) {
4559 * Private object buffers are released here rather
4560 * than in dbuf_dirty() since they are only modified
4561 * in the syncing context and we don't want the
4562 * overhead of making multiple copies of the data.
4564 if (BP_IS_HOLE(db
->db_blkptr
)) {
4567 dbuf_release_bp(db
);
4569 dbuf_remap(dn
, db
, tx
);
4573 if (parent
!= dn
->dn_dbuf
) {
4574 /* Our parent is an indirect block. */
4575 /* We have a dirty parent that has been scheduled for write. */
4576 ASSERT(parent
&& parent
->db_data_pending
);
4577 /* Our parent's buffer is one level closer to the dnode. */
4578 ASSERT(db
->db_level
== parent
->db_level
-1);
4580 * We're about to modify our parent's db_data by modifying
4581 * our block pointer, so the parent must be released.
4583 ASSERT(arc_released(parent
->db_buf
));
4584 zio
= parent
->db_data_pending
->dr_zio
;
4586 /* Our parent is the dnode itself. */
4587 ASSERT((db
->db_level
== dn
->dn_phys
->dn_nlevels
-1 &&
4588 db
->db_blkid
!= DMU_SPILL_BLKID
) ||
4589 (db
->db_blkid
== DMU_SPILL_BLKID
&& db
->db_level
== 0));
4590 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
4591 ASSERT3P(db
->db_blkptr
, ==,
4592 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
4596 ASSERT(db
->db_level
== 0 || data
== db
->db_buf
);
4597 ASSERT3U(db
->db_blkptr
->blk_birth
, <=, txg
);
4600 SET_BOOKMARK(&zb
, os
->os_dsl_dataset
?
4601 os
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
4602 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
4604 if (db
->db_blkid
== DMU_SPILL_BLKID
)
4606 wp_flag
|= (db
->db_state
== DB_NOFILL
) ? WP_NOFILL
: 0;
4608 dmu_write_policy(os
, dn
, db
->db_level
, wp_flag
, &zp
);
4612 * We copy the blkptr now (rather than when we instantiate the dirty
4613 * record), because its value can change between open context and
4614 * syncing context. We do not need to hold dn_struct_rwlock to read
4615 * db_blkptr because we are in syncing context.
4617 dr
->dr_bp_copy
= *db
->db_blkptr
;
4619 if (db
->db_level
== 0 &&
4620 dr
->dt
.dl
.dr_override_state
== DR_OVERRIDDEN
) {
4622 * The BP for this block has been provided by open context
4623 * (by dmu_sync() or dmu_buf_write_embedded()).
4625 abd_t
*contents
= (data
!= NULL
) ?
4626 abd_get_from_buf(data
->b_data
, arc_buf_size(data
)) : NULL
;
4628 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
,
4629 &dr
->dr_bp_copy
, contents
, db
->db
.db_size
, db
->db
.db_size
,
4630 &zp
, dbuf_write_override_ready
, NULL
, NULL
,
4631 dbuf_write_override_done
,
4632 dr
, ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
4633 mutex_enter(&db
->db_mtx
);
4634 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
4635 zio_write_override(dr
->dr_zio
, &dr
->dt
.dl
.dr_overridden_by
,
4636 dr
->dt
.dl
.dr_copies
, dr
->dt
.dl
.dr_nopwrite
);
4637 mutex_exit(&db
->db_mtx
);
4638 } else if (db
->db_state
== DB_NOFILL
) {
4639 ASSERT(zp
.zp_checksum
== ZIO_CHECKSUM_OFF
||
4640 zp
.zp_checksum
== ZIO_CHECKSUM_NOPARITY
);
4641 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
,
4642 &dr
->dr_bp_copy
, NULL
, db
->db
.db_size
, db
->db
.db_size
, &zp
,
4643 dbuf_write_nofill_ready
, NULL
, NULL
,
4644 dbuf_write_nofill_done
, db
,
4645 ZIO_PRIORITY_ASYNC_WRITE
,
4646 ZIO_FLAG_MUSTSUCCEED
| ZIO_FLAG_NODATA
, &zb
);
4648 ASSERT(arc_released(data
));
4651 * For indirect blocks, we want to setup the children
4652 * ready callback so that we can properly handle an indirect
4653 * block that only contains holes.
4655 arc_write_done_func_t
*children_ready_cb
= NULL
;
4656 if (db
->db_level
!= 0)
4657 children_ready_cb
= dbuf_write_children_ready
;
4659 dr
->dr_zio
= arc_write(zio
, os
->os_spa
, txg
,
4660 &dr
->dr_bp_copy
, data
, DBUF_IS_L2CACHEABLE(db
),
4661 &zp
, dbuf_write_ready
,
4662 children_ready_cb
, dbuf_write_physdone
,
4663 dbuf_write_done
, db
, ZIO_PRIORITY_ASYNC_WRITE
,
4664 ZIO_FLAG_MUSTSUCCEED
, &zb
);
4668 EXPORT_SYMBOL(dbuf_find
);
4669 EXPORT_SYMBOL(dbuf_is_metadata
);
4670 EXPORT_SYMBOL(dbuf_destroy
);
4671 EXPORT_SYMBOL(dbuf_loan_arcbuf
);
4672 EXPORT_SYMBOL(dbuf_whichblock
);
4673 EXPORT_SYMBOL(dbuf_read
);
4674 EXPORT_SYMBOL(dbuf_unoverride
);
4675 EXPORT_SYMBOL(dbuf_free_range
);
4676 EXPORT_SYMBOL(dbuf_new_size
);
4677 EXPORT_SYMBOL(dbuf_release_bp
);
4678 EXPORT_SYMBOL(dbuf_dirty
);
4679 EXPORT_SYMBOL(dmu_buf_set_crypt_params
);
4680 EXPORT_SYMBOL(dmu_buf_will_dirty
);
4681 EXPORT_SYMBOL(dmu_buf_is_dirty
);
4682 EXPORT_SYMBOL(dmu_buf_will_not_fill
);
4683 EXPORT_SYMBOL(dmu_buf_will_fill
);
4684 EXPORT_SYMBOL(dmu_buf_fill_done
);
4685 EXPORT_SYMBOL(dmu_buf_rele
);
4686 EXPORT_SYMBOL(dbuf_assign_arcbuf
);
4687 EXPORT_SYMBOL(dbuf_prefetch
);
4688 EXPORT_SYMBOL(dbuf_hold_impl
);
4689 EXPORT_SYMBOL(dbuf_hold
);
4690 EXPORT_SYMBOL(dbuf_hold_level
);
4691 EXPORT_SYMBOL(dbuf_create_bonus
);
4692 EXPORT_SYMBOL(dbuf_spill_set_blksz
);
4693 EXPORT_SYMBOL(dbuf_rm_spill
);
4694 EXPORT_SYMBOL(dbuf_add_ref
);
4695 EXPORT_SYMBOL(dbuf_rele
);
4696 EXPORT_SYMBOL(dbuf_rele_and_unlock
);
4697 EXPORT_SYMBOL(dbuf_refcount
);
4698 EXPORT_SYMBOL(dbuf_sync_list
);
4699 EXPORT_SYMBOL(dmu_buf_set_user
);
4700 EXPORT_SYMBOL(dmu_buf_set_user_ie
);
4701 EXPORT_SYMBOL(dmu_buf_get_user
);
4702 EXPORT_SYMBOL(dmu_buf_get_blkptr
);
4705 ZFS_MODULE_PARAM(zfs_dbuf_cache
, dbuf_cache_
, max_bytes
, ULONG
, ZMOD_RW
,
4706 "Maximum size in bytes of the dbuf cache.");
4708 ZFS_MODULE_PARAM(zfs_dbuf_cache
, dbuf_cache_
, hiwater_pct
, UINT
, ZMOD_RW
,
4709 "Percentage over dbuf_cache_max_bytes when dbufs must be evicted "
4712 ZFS_MODULE_PARAM(zfs_dbuf_cache
, dbuf_cache_
, lowater_pct
, UINT
, ZMOD_RW
,
4713 "Percentage below dbuf_cache_max_bytes when the evict thread stops "
4716 ZFS_MODULE_PARAM(zfs_dbuf
, dbuf_
, metadata_cache_max_bytes
, ULONG
, ZMOD_RW
,
4717 "Maximum size in bytes of the dbuf metadata cache.");
4719 ZFS_MODULE_PARAM(zfs_dbuf
, dbuf_
, cache_shift
, INT
, ZMOD_RW
,
4720 "Set the size of the dbuf cache to a log2 fraction of arc size.");
4722 ZFS_MODULE_PARAM(zfs_dbuf
, dbuf_
, metadata_cache_shift
, INT
, ZMOD_RW
,
4723 "Set the size of the dbuf metadata cache to a log2 fraction of arc "