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, 2018 by Delphix. All rights reserved.
25 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
26 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
29 #include <sys/zfs_context.h>
32 #include <sys/dmu_send.h>
33 #include <sys/dmu_impl.h>
35 #include <sys/dmu_objset.h>
36 #include <sys/dsl_dataset.h>
37 #include <sys/dsl_dir.h>
38 #include <sys/dmu_tx.h>
41 #include <sys/dmu_zfetch.h>
43 #include <sys/sa_impl.h>
44 #include <sys/zfeature.h>
45 #include <sys/blkptr.h>
46 #include <sys/range_tree.h>
47 #include <sys/trace_dbuf.h>
48 #include <sys/callb.h>
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 typedef struct dbuf_hold_arg
{
152 /* Function arguments */
156 boolean_t dh_fail_sparse
;
157 boolean_t dh_fail_uncached
;
159 dmu_buf_impl_t
**dh_dbp
;
160 /* Local variables */
161 dmu_buf_impl_t
*dh_db
;
162 dmu_buf_impl_t
*dh_parent
;
165 dbuf_dirty_record_t
*dh_dr
;
168 static dbuf_hold_arg_t
*dbuf_hold_arg_create(dnode_t
*dn
, uint8_t level
,
169 uint64_t blkid
, boolean_t fail_sparse
, boolean_t fail_uncached
,
170 void *tag
, dmu_buf_impl_t
**dbp
);
171 static int dbuf_hold_impl_arg(dbuf_hold_arg_t
*dh
);
172 static void dbuf_hold_arg_destroy(dbuf_hold_arg_t
*dh
);
174 static boolean_t
dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
);
175 static void dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
);
177 extern inline void dmu_buf_init_user(dmu_buf_user_t
*dbu
,
178 dmu_buf_evict_func_t
*evict_func_sync
,
179 dmu_buf_evict_func_t
*evict_func_async
,
180 dmu_buf_t
**clear_on_evict_dbufp
);
183 * Global data structures and functions for the dbuf cache.
185 static kmem_cache_t
*dbuf_kmem_cache
;
186 static taskq_t
*dbu_evict_taskq
;
188 static kthread_t
*dbuf_cache_evict_thread
;
189 static kmutex_t dbuf_evict_lock
;
190 static kcondvar_t dbuf_evict_cv
;
191 static boolean_t dbuf_evict_thread_exit
;
194 * There are two dbuf caches; each dbuf can only be in one of them at a time.
196 * 1. Cache of metadata dbufs, to help make read-heavy administrative commands
197 * from /sbin/zfs run faster. The "metadata cache" specifically stores dbufs
198 * that represent the metadata that describes filesystems/snapshots/
199 * bookmarks/properties/etc. We only evict from this cache when we export a
200 * pool, to short-circuit as much I/O as possible for all administrative
201 * commands that need the metadata. There is no eviction policy for this
202 * cache, because we try to only include types in it which would occupy a
203 * very small amount of space per object but create a large impact on the
204 * performance of these commands. Instead, after it reaches a maximum size
205 * (which should only happen on very small memory systems with a very large
206 * number of filesystem objects), we stop taking new dbufs into the
207 * metadata cache, instead putting them in the normal dbuf cache.
209 * 2. LRU cache of dbufs. The dbuf cache maintains a list of dbufs that
210 * are not currently held but have been recently released. These dbufs
211 * are not eligible for arc eviction until they are aged out of the cache.
212 * Dbufs that are aged out of the cache will be immediately destroyed and
213 * become eligible for arc eviction.
215 * Dbufs are added to these caches once the last hold is released. If a dbuf is
216 * later accessed and still exists in the dbuf cache, then it will be removed
217 * from the cache and later re-added to the head of the cache.
219 * If a given dbuf meets the requirements for the metadata cache, it will go
220 * there, otherwise it will be considered for the generic LRU dbuf cache. The
221 * caches and the refcounts tracking their sizes are stored in an array indexed
222 * by those caches' matching enum values (from dbuf_cached_state_t).
224 typedef struct dbuf_cache
{
228 dbuf_cache_t dbuf_caches
[DB_CACHE_MAX
];
230 /* Size limits for the caches */
231 unsigned long dbuf_cache_max_bytes
= 0;
232 unsigned long dbuf_metadata_cache_max_bytes
= 0;
233 /* Set the default sizes of the caches to log2 fraction of arc size */
234 int dbuf_cache_shift
= 5;
235 int dbuf_metadata_cache_shift
= 6;
238 * The LRU dbuf cache uses a three-stage eviction policy:
239 * - A low water marker designates when the dbuf eviction thread
240 * should stop evicting from the dbuf cache.
241 * - When we reach the maximum size (aka mid water mark), we
242 * signal the eviction thread to run.
243 * - The high water mark indicates when the eviction thread
244 * is unable to keep up with the incoming load and eviction must
245 * happen in the context of the calling thread.
249 * low water mid water hi water
250 * +----------------------------------------+----------+----------+
255 * +----------------------------------------+----------+----------+
257 * evicting eviction directly
260 * The high and low water marks indicate the operating range for the eviction
261 * thread. The low water mark is, by default, 90% of the total size of the
262 * cache and the high water mark is at 110% (both of these percentages can be
263 * changed by setting dbuf_cache_lowater_pct and dbuf_cache_hiwater_pct,
264 * respectively). The eviction thread will try to ensure that the cache remains
265 * within this range by waking up every second and checking if the cache is
266 * above the low water mark. The thread can also be woken up by callers adding
267 * elements into the cache if the cache is larger than the mid water (i.e max
268 * cache size). Once the eviction thread is woken up and eviction is required,
269 * it will continue evicting buffers until it's able to reduce the cache size
270 * to the low water mark. If the cache size continues to grow and hits the high
271 * water mark, then callers adding elements to the cache will begin to evict
272 * directly from the cache until the cache is no longer above the high water
277 * The percentage above and below the maximum cache size.
279 uint_t dbuf_cache_hiwater_pct
= 10;
280 uint_t dbuf_cache_lowater_pct
= 10;
284 dbuf_cons(void *vdb
, void *unused
, int kmflag
)
286 dmu_buf_impl_t
*db
= vdb
;
287 bzero(db
, sizeof (dmu_buf_impl_t
));
289 mutex_init(&db
->db_mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
290 cv_init(&db
->db_changed
, NULL
, CV_DEFAULT
, NULL
);
291 multilist_link_init(&db
->db_cache_link
);
292 zfs_refcount_create(&db
->db_holds
);
299 dbuf_dest(void *vdb
, void *unused
)
301 dmu_buf_impl_t
*db
= vdb
;
302 mutex_destroy(&db
->db_mtx
);
303 cv_destroy(&db
->db_changed
);
304 ASSERT(!multilist_link_active(&db
->db_cache_link
));
305 zfs_refcount_destroy(&db
->db_holds
);
309 * dbuf hash table routines
311 static dbuf_hash_table_t dbuf_hash_table
;
313 static uint64_t dbuf_hash_count
;
316 * We use Cityhash for this. It's fast, and has good hash properties without
317 * requiring any large static buffers.
320 dbuf_hash(void *os
, uint64_t obj
, uint8_t lvl
, uint64_t blkid
)
322 return (cityhash4((uintptr_t)os
, obj
, (uint64_t)lvl
, blkid
));
325 #define DBUF_EQUAL(dbuf, os, obj, level, blkid) \
326 ((dbuf)->db.db_object == (obj) && \
327 (dbuf)->db_objset == (os) && \
328 (dbuf)->db_level == (level) && \
329 (dbuf)->db_blkid == (blkid))
332 dbuf_find(objset_t
*os
, uint64_t obj
, uint8_t level
, uint64_t blkid
)
334 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
339 hv
= dbuf_hash(os
, obj
, level
, blkid
);
340 idx
= hv
& h
->hash_table_mask
;
342 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
343 for (db
= h
->hash_table
[idx
]; db
!= NULL
; db
= db
->db_hash_next
) {
344 if (DBUF_EQUAL(db
, os
, obj
, level
, blkid
)) {
345 mutex_enter(&db
->db_mtx
);
346 if (db
->db_state
!= DB_EVICTING
) {
347 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
350 mutex_exit(&db
->db_mtx
);
353 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
357 static dmu_buf_impl_t
*
358 dbuf_find_bonus(objset_t
*os
, uint64_t object
)
361 dmu_buf_impl_t
*db
= NULL
;
363 if (dnode_hold(os
, object
, FTAG
, &dn
) == 0) {
364 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
365 if (dn
->dn_bonus
!= NULL
) {
367 mutex_enter(&db
->db_mtx
);
369 rw_exit(&dn
->dn_struct_rwlock
);
370 dnode_rele(dn
, FTAG
);
376 * Insert an entry into the hash table. If there is already an element
377 * equal to elem in the hash table, then the already existing element
378 * will be returned and the new element will not be inserted.
379 * Otherwise returns NULL.
381 static dmu_buf_impl_t
*
382 dbuf_hash_insert(dmu_buf_impl_t
*db
)
384 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
385 objset_t
*os
= db
->db_objset
;
386 uint64_t obj
= db
->db
.db_object
;
387 int level
= db
->db_level
;
388 uint64_t blkid
, hv
, idx
;
392 blkid
= db
->db_blkid
;
393 hv
= dbuf_hash(os
, obj
, level
, blkid
);
394 idx
= hv
& h
->hash_table_mask
;
396 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
397 for (dbf
= h
->hash_table
[idx
], i
= 0; dbf
!= NULL
;
398 dbf
= dbf
->db_hash_next
, i
++) {
399 if (DBUF_EQUAL(dbf
, os
, obj
, level
, blkid
)) {
400 mutex_enter(&dbf
->db_mtx
);
401 if (dbf
->db_state
!= DB_EVICTING
) {
402 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
405 mutex_exit(&dbf
->db_mtx
);
410 DBUF_STAT_BUMP(hash_collisions
);
412 DBUF_STAT_BUMP(hash_chains
);
414 DBUF_STAT_MAX(hash_chain_max
, i
);
417 mutex_enter(&db
->db_mtx
);
418 db
->db_hash_next
= h
->hash_table
[idx
];
419 h
->hash_table
[idx
] = db
;
420 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
421 atomic_inc_64(&dbuf_hash_count
);
422 DBUF_STAT_MAX(hash_elements_max
, dbuf_hash_count
);
428 * This returns whether this dbuf should be stored in the metadata cache, which
429 * is based on whether it's from one of the dnode types that store data related
430 * to traversing dataset hierarchies.
433 dbuf_include_in_metadata_cache(dmu_buf_impl_t
*db
)
436 dmu_object_type_t type
= DB_DNODE(db
)->dn_type
;
439 /* Check if this dbuf is one of the types we care about */
440 if (DMU_OT_IS_METADATA_CACHED(type
)) {
441 /* If we hit this, then we set something up wrong in dmu_ot */
442 ASSERT(DMU_OT_IS_METADATA(type
));
445 * Sanity check for small-memory systems: don't allocate too
446 * much memory for this purpose.
448 if (zfs_refcount_count(
449 &dbuf_caches
[DB_DBUF_METADATA_CACHE
].size
) >
450 dbuf_metadata_cache_max_bytes
) {
451 DBUF_STAT_BUMP(metadata_cache_overflow
);
462 * Remove an entry from the hash table. It must be in the EVICTING state.
465 dbuf_hash_remove(dmu_buf_impl_t
*db
)
467 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
469 dmu_buf_impl_t
*dbf
, **dbp
;
471 hv
= dbuf_hash(db
->db_objset
, db
->db
.db_object
,
472 db
->db_level
, db
->db_blkid
);
473 idx
= hv
& h
->hash_table_mask
;
476 * We mustn't hold db_mtx to maintain lock ordering:
477 * DBUF_HASH_MUTEX > db_mtx.
479 ASSERT(zfs_refcount_is_zero(&db
->db_holds
));
480 ASSERT(db
->db_state
== DB_EVICTING
);
481 ASSERT(!MUTEX_HELD(&db
->db_mtx
));
483 mutex_enter(DBUF_HASH_MUTEX(h
, idx
));
484 dbp
= &h
->hash_table
[idx
];
485 while ((dbf
= *dbp
) != db
) {
486 dbp
= &dbf
->db_hash_next
;
489 *dbp
= db
->db_hash_next
;
490 db
->db_hash_next
= NULL
;
491 if (h
->hash_table
[idx
] &&
492 h
->hash_table
[idx
]->db_hash_next
== NULL
)
493 DBUF_STAT_BUMPDOWN(hash_chains
);
494 mutex_exit(DBUF_HASH_MUTEX(h
, idx
));
495 atomic_dec_64(&dbuf_hash_count
);
501 } dbvu_verify_type_t
;
504 dbuf_verify_user(dmu_buf_impl_t
*db
, dbvu_verify_type_t verify_type
)
509 if (db
->db_user
== NULL
)
512 /* Only data blocks support the attachment of user data. */
513 ASSERT(db
->db_level
== 0);
515 /* Clients must resolve a dbuf before attaching user data. */
516 ASSERT(db
->db
.db_data
!= NULL
);
517 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
519 holds
= zfs_refcount_count(&db
->db_holds
);
520 if (verify_type
== DBVU_EVICTING
) {
522 * Immediate eviction occurs when holds == dirtycnt.
523 * For normal eviction buffers, holds is zero on
524 * eviction, except when dbuf_fix_old_data() calls
525 * dbuf_clear_data(). However, the hold count can grow
526 * during eviction even though db_mtx is held (see
527 * dmu_bonus_hold() for an example), so we can only
528 * test the generic invariant that holds >= dirtycnt.
530 ASSERT3U(holds
, >=, db
->db_dirtycnt
);
532 if (db
->db_user_immediate_evict
== TRUE
)
533 ASSERT3U(holds
, >=, db
->db_dirtycnt
);
535 ASSERT3U(holds
, >, 0);
541 dbuf_evict_user(dmu_buf_impl_t
*db
)
543 dmu_buf_user_t
*dbu
= db
->db_user
;
545 ASSERT(MUTEX_HELD(&db
->db_mtx
));
550 dbuf_verify_user(db
, DBVU_EVICTING
);
554 if (dbu
->dbu_clear_on_evict_dbufp
!= NULL
)
555 *dbu
->dbu_clear_on_evict_dbufp
= NULL
;
559 * There are two eviction callbacks - one that we call synchronously
560 * and one that we invoke via a taskq. The async one is useful for
561 * avoiding lock order reversals and limiting stack depth.
563 * Note that if we have a sync callback but no async callback,
564 * it's likely that the sync callback will free the structure
565 * containing the dbu. In that case we need to take care to not
566 * dereference dbu after calling the sync evict func.
568 boolean_t has_async
= (dbu
->dbu_evict_func_async
!= NULL
);
570 if (dbu
->dbu_evict_func_sync
!= NULL
)
571 dbu
->dbu_evict_func_sync(dbu
);
574 taskq_dispatch_ent(dbu_evict_taskq
, dbu
->dbu_evict_func_async
,
575 dbu
, 0, &dbu
->dbu_tqent
);
580 dbuf_is_metadata(dmu_buf_impl_t
*db
)
583 * Consider indirect blocks and spill blocks to be meta data.
585 if (db
->db_level
> 0 || db
->db_blkid
== DMU_SPILL_BLKID
) {
588 boolean_t is_metadata
;
591 is_metadata
= DMU_OT_IS_METADATA(DB_DNODE(db
)->dn_type
);
594 return (is_metadata
);
600 * This function *must* return indices evenly distributed between all
601 * sublists of the multilist. This is needed due to how the dbuf eviction
602 * code is laid out; dbuf_evict_thread() assumes dbufs are evenly
603 * distributed between all sublists and uses this assumption when
604 * deciding which sublist to evict from and how much to evict from it.
607 dbuf_cache_multilist_index_func(multilist_t
*ml
, void *obj
)
609 dmu_buf_impl_t
*db
= obj
;
612 * The assumption here, is the hash value for a given
613 * dmu_buf_impl_t will remain constant throughout it's lifetime
614 * (i.e. it's objset, object, level and blkid fields don't change).
615 * Thus, we don't need to store the dbuf's sublist index
616 * on insertion, as this index can be recalculated on removal.
618 * Also, the low order bits of the hash value are thought to be
619 * distributed evenly. Otherwise, in the case that the multilist
620 * has a power of two number of sublists, each sublists' usage
621 * would not be evenly distributed.
623 return (dbuf_hash(db
->db_objset
, db
->db
.db_object
,
624 db
->db_level
, db
->db_blkid
) %
625 multilist_get_num_sublists(ml
));
628 static inline unsigned long
629 dbuf_cache_target_bytes(void)
631 return MIN(dbuf_cache_max_bytes
,
632 arc_target_bytes() >> dbuf_cache_shift
);
635 static inline uint64_t
636 dbuf_cache_hiwater_bytes(void)
638 uint64_t dbuf_cache_target
= dbuf_cache_target_bytes();
639 return (dbuf_cache_target
+
640 (dbuf_cache_target
* dbuf_cache_hiwater_pct
) / 100);
643 static inline uint64_t
644 dbuf_cache_lowater_bytes(void)
646 uint64_t dbuf_cache_target
= dbuf_cache_target_bytes();
647 return (dbuf_cache_target
-
648 (dbuf_cache_target
* dbuf_cache_lowater_pct
) / 100);
651 static inline boolean_t
652 dbuf_cache_above_hiwater(void)
654 return (zfs_refcount_count(&dbuf_caches
[DB_DBUF_CACHE
].size
) >
655 dbuf_cache_hiwater_bytes());
658 static inline boolean_t
659 dbuf_cache_above_lowater(void)
661 return (zfs_refcount_count(&dbuf_caches
[DB_DBUF_CACHE
].size
) >
662 dbuf_cache_lowater_bytes());
666 * Evict the oldest eligible dbuf from the dbuf cache.
671 int idx
= multilist_get_random_index(dbuf_caches
[DB_DBUF_CACHE
].cache
);
672 multilist_sublist_t
*mls
= multilist_sublist_lock(
673 dbuf_caches
[DB_DBUF_CACHE
].cache
, idx
);
675 ASSERT(!MUTEX_HELD(&dbuf_evict_lock
));
677 dmu_buf_impl_t
*db
= multilist_sublist_tail(mls
);
678 while (db
!= NULL
&& mutex_tryenter(&db
->db_mtx
) == 0) {
679 db
= multilist_sublist_prev(mls
, db
);
682 DTRACE_PROBE2(dbuf__evict__one
, dmu_buf_impl_t
*, db
,
683 multilist_sublist_t
*, mls
);
686 multilist_sublist_remove(mls
, db
);
687 multilist_sublist_unlock(mls
);
688 (void) zfs_refcount_remove_many(
689 &dbuf_caches
[DB_DBUF_CACHE
].size
, db
->db
.db_size
, db
);
690 DBUF_STAT_BUMPDOWN(cache_levels
[db
->db_level
]);
691 DBUF_STAT_BUMPDOWN(cache_count
);
692 DBUF_STAT_DECR(cache_levels_bytes
[db
->db_level
],
694 ASSERT3U(db
->db_caching_status
, ==, DB_DBUF_CACHE
);
695 db
->db_caching_status
= DB_NO_CACHE
;
697 DBUF_STAT_MAX(cache_size_bytes_max
,
698 zfs_refcount_count(&dbuf_caches
[DB_DBUF_CACHE
].size
));
699 DBUF_STAT_BUMP(cache_total_evicts
);
701 multilist_sublist_unlock(mls
);
706 * The dbuf evict thread is responsible for aging out dbufs from the
707 * cache. Once the cache has reached it's maximum size, dbufs are removed
708 * and destroyed. The eviction thread will continue running until the size
709 * of the dbuf cache is at or below the maximum size. Once the dbuf is aged
710 * out of the cache it is destroyed and becomes eligible for arc eviction.
714 dbuf_evict_thread(void *unused
)
718 CALLB_CPR_INIT(&cpr
, &dbuf_evict_lock
, callb_generic_cpr
, FTAG
);
720 mutex_enter(&dbuf_evict_lock
);
721 while (!dbuf_evict_thread_exit
) {
722 while (!dbuf_cache_above_lowater() && !dbuf_evict_thread_exit
) {
723 CALLB_CPR_SAFE_BEGIN(&cpr
);
724 (void) cv_timedwait_sig_hires(&dbuf_evict_cv
,
725 &dbuf_evict_lock
, SEC2NSEC(1), MSEC2NSEC(1), 0);
726 CALLB_CPR_SAFE_END(&cpr
, &dbuf_evict_lock
);
728 mutex_exit(&dbuf_evict_lock
);
731 * Keep evicting as long as we're above the low water mark
732 * for the cache. We do this without holding the locks to
733 * minimize lock contention.
735 while (dbuf_cache_above_lowater() && !dbuf_evict_thread_exit
) {
739 mutex_enter(&dbuf_evict_lock
);
742 dbuf_evict_thread_exit
= B_FALSE
;
743 cv_broadcast(&dbuf_evict_cv
);
744 CALLB_CPR_EXIT(&cpr
); /* drops dbuf_evict_lock */
749 * Wake up the dbuf eviction thread if the dbuf cache is at its max size.
750 * If the dbuf cache is at its high water mark, then evict a dbuf from the
751 * dbuf cache using the callers context.
754 dbuf_evict_notify(void)
757 * We check if we should evict without holding the dbuf_evict_lock,
758 * because it's OK to occasionally make the wrong decision here,
759 * and grabbing the lock results in massive lock contention.
761 if (zfs_refcount_count(&dbuf_caches
[DB_DBUF_CACHE
].size
) >
762 dbuf_cache_target_bytes()) {
763 if (dbuf_cache_above_hiwater())
765 cv_signal(&dbuf_evict_cv
);
770 dbuf_kstat_update(kstat_t
*ksp
, int rw
)
772 dbuf_stats_t
*ds
= ksp
->ks_data
;
774 if (rw
== KSTAT_WRITE
) {
775 return (SET_ERROR(EACCES
));
777 ds
->metadata_cache_size_bytes
.value
.ui64
= zfs_refcount_count(
778 &dbuf_caches
[DB_DBUF_METADATA_CACHE
].size
);
779 ds
->cache_size_bytes
.value
.ui64
=
780 zfs_refcount_count(&dbuf_caches
[DB_DBUF_CACHE
].size
);
781 ds
->cache_target_bytes
.value
.ui64
= dbuf_cache_target_bytes();
782 ds
->cache_hiwater_bytes
.value
.ui64
= dbuf_cache_hiwater_bytes();
783 ds
->cache_lowater_bytes
.value
.ui64
= dbuf_cache_lowater_bytes();
784 ds
->hash_elements
.value
.ui64
= dbuf_hash_count
;
793 uint64_t hsize
= 1ULL << 16;
794 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
798 * The hash table is big enough to fill all of physical memory
799 * with an average block size of zfs_arc_average_blocksize (default 8K).
800 * By default, the table will take up
801 * totalmem * sizeof(void*) / 8K (1MB per GB with 8-byte pointers).
803 while (hsize
* zfs_arc_average_blocksize
< physmem
* PAGESIZE
)
807 h
->hash_table_mask
= hsize
- 1;
810 * Large allocations which do not require contiguous pages
811 * should be using vmem_alloc() in the linux kernel
813 h
->hash_table
= vmem_zalloc(hsize
* sizeof (void *), KM_SLEEP
);
815 h
->hash_table
= kmem_zalloc(hsize
* sizeof (void *), KM_NOSLEEP
);
817 if (h
->hash_table
== NULL
) {
818 /* XXX - we should really return an error instead of assert */
819 ASSERT(hsize
> (1ULL << 10));
824 dbuf_kmem_cache
= kmem_cache_create("dmu_buf_impl_t",
825 sizeof (dmu_buf_impl_t
),
826 0, dbuf_cons
, dbuf_dest
, NULL
, NULL
, NULL
, 0);
828 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
829 mutex_init(&h
->hash_mutexes
[i
], NULL
, MUTEX_DEFAULT
, NULL
);
834 * Setup the parameters for the dbuf caches. We set the sizes of the
835 * dbuf cache and the metadata cache to 1/32nd and 1/16th (default)
836 * of the target size of the ARC. If the values has been specified as
837 * a module option and they're not greater than the target size of the
838 * ARC, then we honor that value.
840 if (dbuf_cache_max_bytes
== 0 ||
841 dbuf_cache_max_bytes
>= arc_target_bytes()) {
842 dbuf_cache_max_bytes
= arc_target_bytes() >> dbuf_cache_shift
;
844 if (dbuf_metadata_cache_max_bytes
== 0 ||
845 dbuf_metadata_cache_max_bytes
>= arc_target_bytes()) {
846 dbuf_metadata_cache_max_bytes
=
847 arc_target_bytes() >> dbuf_metadata_cache_shift
;
851 * All entries are queued via taskq_dispatch_ent(), so min/maxalloc
852 * configuration is not required.
854 dbu_evict_taskq
= taskq_create("dbu_evict", 1, defclsyspri
, 0, 0, 0);
856 for (dbuf_cached_state_t dcs
= 0; dcs
< DB_CACHE_MAX
; dcs
++) {
857 dbuf_caches
[dcs
].cache
=
858 multilist_create(sizeof (dmu_buf_impl_t
),
859 offsetof(dmu_buf_impl_t
, db_cache_link
),
860 dbuf_cache_multilist_index_func
);
861 zfs_refcount_create(&dbuf_caches
[dcs
].size
);
864 dbuf_evict_thread_exit
= B_FALSE
;
865 mutex_init(&dbuf_evict_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
866 cv_init(&dbuf_evict_cv
, NULL
, CV_DEFAULT
, NULL
);
867 dbuf_cache_evict_thread
= thread_create(NULL
, 0, dbuf_evict_thread
,
868 NULL
, 0, &p0
, TS_RUN
, minclsyspri
);
870 dbuf_ksp
= kstat_create("zfs", 0, "dbufstats", "misc",
871 KSTAT_TYPE_NAMED
, sizeof (dbuf_stats
) / sizeof (kstat_named_t
),
873 if (dbuf_ksp
!= NULL
) {
874 dbuf_ksp
->ks_data
= &dbuf_stats
;
875 dbuf_ksp
->ks_update
= dbuf_kstat_update
;
876 kstat_install(dbuf_ksp
);
878 for (i
= 0; i
< DN_MAX_LEVELS
; i
++) {
879 snprintf(dbuf_stats
.cache_levels
[i
].name
,
880 KSTAT_STRLEN
, "cache_level_%d", i
);
881 dbuf_stats
.cache_levels
[i
].data_type
=
883 snprintf(dbuf_stats
.cache_levels_bytes
[i
].name
,
884 KSTAT_STRLEN
, "cache_level_%d_bytes", i
);
885 dbuf_stats
.cache_levels_bytes
[i
].data_type
=
894 dbuf_hash_table_t
*h
= &dbuf_hash_table
;
897 dbuf_stats_destroy();
899 for (i
= 0; i
< DBUF_MUTEXES
; i
++)
900 mutex_destroy(&h
->hash_mutexes
[i
]);
903 * Large allocations which do not require contiguous pages
904 * should be using vmem_free() in the linux kernel
906 vmem_free(h
->hash_table
, (h
->hash_table_mask
+ 1) * sizeof (void *));
908 kmem_free(h
->hash_table
, (h
->hash_table_mask
+ 1) * sizeof (void *));
910 kmem_cache_destroy(dbuf_kmem_cache
);
911 taskq_destroy(dbu_evict_taskq
);
913 mutex_enter(&dbuf_evict_lock
);
914 dbuf_evict_thread_exit
= B_TRUE
;
915 while (dbuf_evict_thread_exit
) {
916 cv_signal(&dbuf_evict_cv
);
917 cv_wait(&dbuf_evict_cv
, &dbuf_evict_lock
);
919 mutex_exit(&dbuf_evict_lock
);
921 mutex_destroy(&dbuf_evict_lock
);
922 cv_destroy(&dbuf_evict_cv
);
924 for (dbuf_cached_state_t dcs
= 0; dcs
< DB_CACHE_MAX
; dcs
++) {
925 zfs_refcount_destroy(&dbuf_caches
[dcs
].size
);
926 multilist_destroy(dbuf_caches
[dcs
].cache
);
929 if (dbuf_ksp
!= NULL
) {
930 kstat_delete(dbuf_ksp
);
941 dbuf_verify(dmu_buf_impl_t
*db
)
944 dbuf_dirty_record_t
*dr
;
946 ASSERT(MUTEX_HELD(&db
->db_mtx
));
948 if (!(zfs_flags
& ZFS_DEBUG_DBUF_VERIFY
))
951 ASSERT(db
->db_objset
!= NULL
);
955 ASSERT(db
->db_parent
== NULL
);
956 ASSERT(db
->db_blkptr
== NULL
);
958 ASSERT3U(db
->db
.db_object
, ==, dn
->dn_object
);
959 ASSERT3P(db
->db_objset
, ==, dn
->dn_objset
);
960 ASSERT3U(db
->db_level
, <, dn
->dn_nlevels
);
961 ASSERT(db
->db_blkid
== DMU_BONUS_BLKID
||
962 db
->db_blkid
== DMU_SPILL_BLKID
||
963 !avl_is_empty(&dn
->dn_dbufs
));
965 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
967 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
968 ASSERT3U(db
->db
.db_offset
, ==, DMU_BONUS_BLKID
);
969 } else if (db
->db_blkid
== DMU_SPILL_BLKID
) {
971 ASSERT0(db
->db
.db_offset
);
973 ASSERT3U(db
->db
.db_offset
, ==, db
->db_blkid
* db
->db
.db_size
);
976 for (dr
= db
->db_data_pending
; dr
!= NULL
; dr
= dr
->dr_next
)
977 ASSERT(dr
->dr_dbuf
== db
);
979 for (dr
= db
->db_last_dirty
; dr
!= NULL
; dr
= dr
->dr_next
)
980 ASSERT(dr
->dr_dbuf
== db
);
983 * We can't assert that db_size matches dn_datablksz because it
984 * can be momentarily different when another thread is doing
987 if (db
->db_level
== 0 && db
->db
.db_object
== DMU_META_DNODE_OBJECT
) {
988 dr
= db
->db_data_pending
;
990 * It should only be modified in syncing context, so
991 * make sure we only have one copy of the data.
993 ASSERT(dr
== NULL
|| dr
->dt
.dl
.dr_data
== db
->db_buf
);
996 /* verify db->db_blkptr */
998 if (db
->db_parent
== dn
->dn_dbuf
) {
999 /* db is pointed to by the dnode */
1000 /* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
1001 if (DMU_OBJECT_IS_SPECIAL(db
->db
.db_object
))
1002 ASSERT(db
->db_parent
== NULL
);
1004 ASSERT(db
->db_parent
!= NULL
);
1005 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
1006 ASSERT3P(db
->db_blkptr
, ==,
1007 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
1009 /* db is pointed to by an indirect block */
1010 ASSERTV(int epb
= db
->db_parent
->db
.db_size
>>
1012 ASSERT3U(db
->db_parent
->db_level
, ==, db
->db_level
+1);
1013 ASSERT3U(db
->db_parent
->db
.db_object
, ==,
1016 * dnode_grow_indblksz() can make this fail if we don't
1017 * have the struct_rwlock. XXX indblksz no longer
1018 * grows. safe to do this now?
1020 if (RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
1021 ASSERT3P(db
->db_blkptr
, ==,
1022 ((blkptr_t
*)db
->db_parent
->db
.db_data
+
1023 db
->db_blkid
% epb
));
1027 if ((db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
)) &&
1028 (db
->db_buf
== NULL
|| db
->db_buf
->b_data
) &&
1029 db
->db
.db_data
&& db
->db_blkid
!= DMU_BONUS_BLKID
&&
1030 db
->db_state
!= DB_FILL
&& !dn
->dn_free_txg
) {
1032 * If the blkptr isn't set but they have nonzero data,
1033 * it had better be dirty, otherwise we'll lose that
1034 * data when we evict this buffer.
1036 * There is an exception to this rule for indirect blocks; in
1037 * this case, if the indirect block is a hole, we fill in a few
1038 * fields on each of the child blocks (importantly, birth time)
1039 * to prevent hole birth times from being lost when you
1040 * partially fill in a hole.
1042 if (db
->db_dirtycnt
== 0) {
1043 if (db
->db_level
== 0) {
1044 uint64_t *buf
= db
->db
.db_data
;
1047 for (i
= 0; i
< db
->db
.db_size
>> 3; i
++) {
1048 ASSERT(buf
[i
] == 0);
1051 blkptr_t
*bps
= db
->db
.db_data
;
1052 ASSERT3U(1 << DB_DNODE(db
)->dn_indblkshift
, ==,
1055 * We want to verify that all the blkptrs in the
1056 * indirect block are holes, but we may have
1057 * automatically set up a few fields for them.
1058 * We iterate through each blkptr and verify
1059 * they only have those fields set.
1062 i
< db
->db
.db_size
/ sizeof (blkptr_t
);
1064 blkptr_t
*bp
= &bps
[i
];
1065 ASSERT(ZIO_CHECKSUM_IS_ZERO(
1068 DVA_IS_EMPTY(&bp
->blk_dva
[0]) &&
1069 DVA_IS_EMPTY(&bp
->blk_dva
[1]) &&
1070 DVA_IS_EMPTY(&bp
->blk_dva
[2]));
1071 ASSERT0(bp
->blk_fill
);
1072 ASSERT0(bp
->blk_pad
[0]);
1073 ASSERT0(bp
->blk_pad
[1]);
1074 ASSERT(!BP_IS_EMBEDDED(bp
));
1075 ASSERT(BP_IS_HOLE(bp
));
1076 ASSERT0(bp
->blk_phys_birth
);
1086 dbuf_clear_data(dmu_buf_impl_t
*db
)
1088 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1089 dbuf_evict_user(db
);
1090 ASSERT3P(db
->db_buf
, ==, NULL
);
1091 db
->db
.db_data
= NULL
;
1092 if (db
->db_state
!= DB_NOFILL
)
1093 db
->db_state
= DB_UNCACHED
;
1097 dbuf_set_data(dmu_buf_impl_t
*db
, arc_buf_t
*buf
)
1099 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1100 ASSERT(buf
!= NULL
);
1103 ASSERT(buf
->b_data
!= NULL
);
1104 db
->db
.db_data
= buf
->b_data
;
1108 * Loan out an arc_buf for read. Return the loaned arc_buf.
1111 dbuf_loan_arcbuf(dmu_buf_impl_t
*db
)
1115 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1116 mutex_enter(&db
->db_mtx
);
1117 if (arc_released(db
->db_buf
) || zfs_refcount_count(&db
->db_holds
) > 1) {
1118 int blksz
= db
->db
.db_size
;
1119 spa_t
*spa
= db
->db_objset
->os_spa
;
1121 mutex_exit(&db
->db_mtx
);
1122 abuf
= arc_loan_buf(spa
, B_FALSE
, blksz
);
1123 bcopy(db
->db
.db_data
, abuf
->b_data
, blksz
);
1126 arc_loan_inuse_buf(abuf
, db
);
1128 dbuf_clear_data(db
);
1129 mutex_exit(&db
->db_mtx
);
1135 * Calculate which level n block references the data at the level 0 offset
1139 dbuf_whichblock(const dnode_t
*dn
, const int64_t level
, const uint64_t offset
)
1141 if (dn
->dn_datablkshift
!= 0 && dn
->dn_indblkshift
!= 0) {
1143 * The level n blkid is equal to the level 0 blkid divided by
1144 * the number of level 0s in a level n block.
1146 * The level 0 blkid is offset >> datablkshift =
1147 * offset / 2^datablkshift.
1149 * The number of level 0s in a level n is the number of block
1150 * pointers in an indirect block, raised to the power of level.
1151 * This is 2^(indblkshift - SPA_BLKPTRSHIFT)^level =
1152 * 2^(level*(indblkshift - SPA_BLKPTRSHIFT)).
1154 * Thus, the level n blkid is: offset /
1155 * ((2^datablkshift)*(2^(level*(indblkshift-SPA_BLKPTRSHIFT))))
1156 * = offset / 2^(datablkshift + level *
1157 * (indblkshift - SPA_BLKPTRSHIFT))
1158 * = offset >> (datablkshift + level *
1159 * (indblkshift - SPA_BLKPTRSHIFT))
1162 const unsigned exp
= dn
->dn_datablkshift
+
1163 level
* (dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
);
1165 if (exp
>= 8 * sizeof (offset
)) {
1166 /* This only happens on the highest indirection level */
1167 ASSERT3U(level
, ==, dn
->dn_nlevels
- 1);
1171 ASSERT3U(exp
, <, 8 * sizeof (offset
));
1173 return (offset
>> exp
);
1175 ASSERT3U(offset
, <, dn
->dn_datablksz
);
1181 dbuf_read_done(zio_t
*zio
, const zbookmark_phys_t
*zb
, const blkptr_t
*bp
,
1182 arc_buf_t
*buf
, void *vdb
)
1184 dmu_buf_impl_t
*db
= vdb
;
1186 mutex_enter(&db
->db_mtx
);
1187 ASSERT3U(db
->db_state
, ==, DB_READ
);
1189 * All reads are synchronous, so we must have a hold on the dbuf
1191 ASSERT(zfs_refcount_count(&db
->db_holds
) > 0);
1192 ASSERT(db
->db_buf
== NULL
);
1193 ASSERT(db
->db
.db_data
== NULL
);
1196 ASSERT(zio
== NULL
|| zio
->io_error
!= 0);
1197 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1198 ASSERT3P(db
->db_buf
, ==, NULL
);
1199 db
->db_state
= DB_UNCACHED
;
1200 } else if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
1201 /* freed in flight */
1202 ASSERT(zio
== NULL
|| zio
->io_error
== 0);
1203 arc_release(buf
, db
);
1204 bzero(buf
->b_data
, db
->db
.db_size
);
1205 arc_buf_freeze(buf
);
1206 db
->db_freed_in_flight
= FALSE
;
1207 dbuf_set_data(db
, buf
);
1208 db
->db_state
= DB_CACHED
;
1211 ASSERT(zio
== NULL
|| zio
->io_error
== 0);
1212 dbuf_set_data(db
, buf
);
1213 db
->db_state
= DB_CACHED
;
1215 cv_broadcast(&db
->db_changed
);
1216 dbuf_rele_and_unlock(db
, NULL
, B_FALSE
);
1221 * This function ensures that, when doing a decrypting read of a block,
1222 * we make sure we have decrypted the dnode associated with it. We must do
1223 * this so that we ensure we are fully authenticating the checksum-of-MACs
1224 * tree from the root of the objset down to this block. Indirect blocks are
1225 * always verified against their secure checksum-of-MACs assuming that the
1226 * dnode containing them is correct. Now that we are doing a decrypting read,
1227 * we can be sure that the key is loaded and verify that assumption. This is
1228 * especially important considering that we always read encrypted dnode
1229 * blocks as raw data (without verifying their MACs) to start, and
1230 * decrypt / authenticate them when we need to read an encrypted bonus buffer.
1233 dbuf_read_verify_dnode_crypt(dmu_buf_impl_t
*db
, uint32_t flags
)
1236 objset_t
*os
= db
->db_objset
;
1237 arc_buf_t
*dnode_abuf
;
1239 zbookmark_phys_t zb
;
1241 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1243 if (!os
->os_encrypted
|| os
->os_raw_receive
||
1244 (flags
& DB_RF_NO_DECRYPT
) != 0)
1249 dnode_abuf
= (dn
->dn_dbuf
!= NULL
) ? dn
->dn_dbuf
->db_buf
: NULL
;
1251 if (dnode_abuf
== NULL
|| !arc_is_encrypted(dnode_abuf
)) {
1256 SET_BOOKMARK(&zb
, dmu_objset_id(os
),
1257 DMU_META_DNODE_OBJECT
, 0, dn
->dn_dbuf
->db_blkid
);
1258 err
= arc_untransform(dnode_abuf
, os
->os_spa
, &zb
, B_TRUE
);
1261 * An error code of EACCES tells us that the key is still not
1262 * available. This is ok if we are only reading authenticated
1263 * (and therefore non-encrypted) blocks.
1265 if (err
== EACCES
&& ((db
->db_blkid
!= DMU_BONUS_BLKID
&&
1266 !DMU_OT_IS_ENCRYPTED(dn
->dn_type
)) ||
1267 (db
->db_blkid
== DMU_BONUS_BLKID
&&
1268 !DMU_OT_IS_ENCRYPTED(dn
->dn_bonustype
))))
1277 dbuf_read_impl(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t flags
)
1280 zbookmark_phys_t zb
;
1281 uint32_t aflags
= ARC_FLAG_NOWAIT
;
1282 int err
, zio_flags
= 0;
1286 ASSERT(!zfs_refcount_is_zero(&db
->db_holds
));
1287 /* We need the struct_rwlock to prevent db_blkptr from changing. */
1288 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
1289 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1290 ASSERT(db
->db_state
== DB_UNCACHED
);
1291 ASSERT(db
->db_buf
== NULL
);
1293 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1295 * The bonus length stored in the dnode may be less than
1296 * the maximum available space in the bonus buffer.
1298 int bonuslen
= MIN(dn
->dn_bonuslen
, dn
->dn_phys
->dn_bonuslen
);
1299 int max_bonuslen
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
);
1301 /* if the underlying dnode block is encrypted, decrypt it */
1302 err
= dbuf_read_verify_dnode_crypt(db
, flags
);
1305 mutex_exit(&db
->db_mtx
);
1309 ASSERT3U(bonuslen
, <=, db
->db
.db_size
);
1310 db
->db
.db_data
= kmem_alloc(max_bonuslen
, KM_SLEEP
);
1311 arc_space_consume(max_bonuslen
, ARC_SPACE_BONUS
);
1312 if (bonuslen
< max_bonuslen
)
1313 bzero(db
->db
.db_data
, max_bonuslen
);
1315 bcopy(DN_BONUS(dn
->dn_phys
), db
->db
.db_data
, bonuslen
);
1317 db
->db_state
= DB_CACHED
;
1318 mutex_exit(&db
->db_mtx
);
1323 * Recheck BP_IS_HOLE() after dnode_block_freed() in case dnode_sync()
1324 * processes the delete record and clears the bp while we are waiting
1325 * for the dn_mtx (resulting in a "no" from block_freed).
1327 if (db
->db_blkptr
== NULL
|| BP_IS_HOLE(db
->db_blkptr
) ||
1328 (db
->db_level
== 0 && (dnode_block_freed(dn
, db
->db_blkid
) ||
1329 BP_IS_HOLE(db
->db_blkptr
)))) {
1330 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1332 dbuf_set_data(db
, arc_alloc_buf(db
->db_objset
->os_spa
, db
, type
,
1334 bzero(db
->db
.db_data
, db
->db
.db_size
);
1336 if (db
->db_blkptr
!= NULL
&& db
->db_level
> 0 &&
1337 BP_IS_HOLE(db
->db_blkptr
) &&
1338 db
->db_blkptr
->blk_birth
!= 0) {
1339 blkptr_t
*bps
= db
->db
.db_data
;
1340 for (int i
= 0; i
< ((1 <<
1341 DB_DNODE(db
)->dn_indblkshift
) / sizeof (blkptr_t
));
1343 blkptr_t
*bp
= &bps
[i
];
1344 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==,
1345 1 << dn
->dn_indblkshift
);
1347 BP_GET_LEVEL(db
->db_blkptr
) == 1 ?
1349 BP_GET_LSIZE(db
->db_blkptr
));
1350 BP_SET_TYPE(bp
, BP_GET_TYPE(db
->db_blkptr
));
1352 BP_GET_LEVEL(db
->db_blkptr
) - 1);
1353 BP_SET_BIRTH(bp
, db
->db_blkptr
->blk_birth
, 0);
1357 db
->db_state
= DB_CACHED
;
1358 mutex_exit(&db
->db_mtx
);
1363 SET_BOOKMARK(&zb
, dmu_objset_id(db
->db_objset
),
1364 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
1367 * All bps of an encrypted os should have the encryption bit set.
1368 * If this is not true it indicates tampering and we report an error.
1370 if (db
->db_objset
->os_encrypted
&& !BP_USES_CRYPT(db
->db_blkptr
)) {
1371 spa_log_error(db
->db_objset
->os_spa
, &zb
);
1372 zfs_panic_recover("unencrypted block in encrypted "
1373 "object set %llu", dmu_objset_id(db
->db_objset
));
1375 mutex_exit(&db
->db_mtx
);
1376 return (SET_ERROR(EIO
));
1379 err
= dbuf_read_verify_dnode_crypt(db
, flags
);
1382 mutex_exit(&db
->db_mtx
);
1388 db
->db_state
= DB_READ
;
1389 mutex_exit(&db
->db_mtx
);
1391 if (DBUF_IS_L2CACHEABLE(db
))
1392 aflags
|= ARC_FLAG_L2CACHE
;
1394 dbuf_add_ref(db
, NULL
);
1396 zio_flags
= (flags
& DB_RF_CANFAIL
) ?
1397 ZIO_FLAG_CANFAIL
: ZIO_FLAG_MUSTSUCCEED
;
1399 if ((flags
& DB_RF_NO_DECRYPT
) && BP_IS_PROTECTED(db
->db_blkptr
))
1400 zio_flags
|= ZIO_FLAG_RAW
;
1402 err
= arc_read(zio
, db
->db_objset
->os_spa
, db
->db_blkptr
,
1403 dbuf_read_done
, db
, ZIO_PRIORITY_SYNC_READ
, zio_flags
,
1410 * This is our just-in-time copy function. It makes a copy of buffers that
1411 * have been modified in a previous transaction group before we access them in
1412 * the current active group.
1414 * This function is used in three places: when we are dirtying a buffer for the
1415 * first time in a txg, when we are freeing a range in a dnode that includes
1416 * this buffer, and when we are accessing a buffer which was received compressed
1417 * and later referenced in a WRITE_BYREF record.
1419 * Note that when we are called from dbuf_free_range() we do not put a hold on
1420 * the buffer, we just traverse the active dbuf list for the dnode.
1423 dbuf_fix_old_data(dmu_buf_impl_t
*db
, uint64_t txg
)
1425 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
1427 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1428 ASSERT(db
->db
.db_data
!= NULL
);
1429 ASSERT(db
->db_level
== 0);
1430 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
);
1433 (dr
->dt
.dl
.dr_data
!=
1434 ((db
->db_blkid
== DMU_BONUS_BLKID
) ? db
->db
.db_data
: db
->db_buf
)))
1438 * If the last dirty record for this dbuf has not yet synced
1439 * and its referencing the dbuf data, either:
1440 * reset the reference to point to a new copy,
1441 * or (if there a no active holders)
1442 * just null out the current db_data pointer.
1444 ASSERT3U(dr
->dr_txg
, >=, txg
- 2);
1445 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
1446 dnode_t
*dn
= DB_DNODE(db
);
1447 int bonuslen
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
);
1448 dr
->dt
.dl
.dr_data
= kmem_alloc(bonuslen
, KM_SLEEP
);
1449 arc_space_consume(bonuslen
, ARC_SPACE_BONUS
);
1450 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
, bonuslen
);
1451 } else if (zfs_refcount_count(&db
->db_holds
) > db
->db_dirtycnt
) {
1452 dnode_t
*dn
= DB_DNODE(db
);
1453 int size
= arc_buf_size(db
->db_buf
);
1454 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1455 spa_t
*spa
= db
->db_objset
->os_spa
;
1456 enum zio_compress compress_type
=
1457 arc_get_compression(db
->db_buf
);
1459 if (arc_is_encrypted(db
->db_buf
)) {
1460 boolean_t byteorder
;
1461 uint8_t salt
[ZIO_DATA_SALT_LEN
];
1462 uint8_t iv
[ZIO_DATA_IV_LEN
];
1463 uint8_t mac
[ZIO_DATA_MAC_LEN
];
1465 arc_get_raw_params(db
->db_buf
, &byteorder
, salt
,
1467 dr
->dt
.dl
.dr_data
= arc_alloc_raw_buf(spa
, db
,
1468 dmu_objset_id(dn
->dn_objset
), byteorder
, salt
, iv
,
1469 mac
, dn
->dn_type
, size
, arc_buf_lsize(db
->db_buf
),
1471 } else if (compress_type
!= ZIO_COMPRESS_OFF
) {
1472 ASSERT3U(type
, ==, ARC_BUFC_DATA
);
1473 dr
->dt
.dl
.dr_data
= arc_alloc_compressed_buf(spa
, db
,
1474 size
, arc_buf_lsize(db
->db_buf
), compress_type
);
1476 dr
->dt
.dl
.dr_data
= arc_alloc_buf(spa
, db
, type
, size
);
1478 bcopy(db
->db
.db_data
, dr
->dt
.dl
.dr_data
->b_data
, size
);
1481 dbuf_clear_data(db
);
1486 dbuf_read(dmu_buf_impl_t
*db
, zio_t
*zio
, uint32_t flags
)
1493 * We don't have to hold the mutex to check db_state because it
1494 * can't be freed while we have a hold on the buffer.
1496 ASSERT(!zfs_refcount_is_zero(&db
->db_holds
));
1498 if (db
->db_state
== DB_NOFILL
)
1499 return (SET_ERROR(EIO
));
1503 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1504 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
1506 prefetch
= db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
1507 (flags
& DB_RF_NOPREFETCH
) == 0 && dn
!= NULL
&&
1508 DBUF_IS_CACHEABLE(db
);
1510 mutex_enter(&db
->db_mtx
);
1511 if (db
->db_state
== DB_CACHED
) {
1512 spa_t
*spa
= dn
->dn_objset
->os_spa
;
1515 * Ensure that this block's dnode has been decrypted if
1516 * the caller has requested decrypted data.
1518 err
= dbuf_read_verify_dnode_crypt(db
, flags
);
1521 * If the arc buf is compressed or encrypted and the caller
1522 * requested uncompressed data, we need to untransform it
1523 * before returning. We also call arc_untransform() on any
1524 * unauthenticated blocks, which will verify their MAC if
1525 * the key is now available.
1527 if (err
== 0 && db
->db_buf
!= NULL
&&
1528 (flags
& DB_RF_NO_DECRYPT
) == 0 &&
1529 (arc_is_encrypted(db
->db_buf
) ||
1530 arc_is_unauthenticated(db
->db_buf
) ||
1531 arc_get_compression(db
->db_buf
) != ZIO_COMPRESS_OFF
)) {
1532 zbookmark_phys_t zb
;
1534 SET_BOOKMARK(&zb
, dmu_objset_id(db
->db_objset
),
1535 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
1536 dbuf_fix_old_data(db
, spa_syncing_txg(spa
));
1537 err
= arc_untransform(db
->db_buf
, spa
, &zb
, B_FALSE
);
1538 dbuf_set_data(db
, db
->db_buf
);
1540 mutex_exit(&db
->db_mtx
);
1541 if (err
== 0 && prefetch
)
1542 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
);
1543 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1544 rw_exit(&dn
->dn_struct_rwlock
);
1546 DBUF_STAT_BUMP(hash_hits
);
1547 } else if (db
->db_state
== DB_UNCACHED
) {
1548 spa_t
*spa
= dn
->dn_objset
->os_spa
;
1549 boolean_t need_wait
= B_FALSE
;
1552 db
->db_blkptr
!= NULL
&& !BP_IS_HOLE(db
->db_blkptr
)) {
1553 zio
= zio_root(spa
, NULL
, NULL
, ZIO_FLAG_CANFAIL
);
1556 err
= dbuf_read_impl(db
, zio
, flags
);
1558 /* dbuf_read_impl has dropped db_mtx for us */
1560 if (!err
&& prefetch
)
1561 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
);
1563 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1564 rw_exit(&dn
->dn_struct_rwlock
);
1566 DBUF_STAT_BUMP(hash_misses
);
1569 * If we created a zio_root we must execute it to avoid
1570 * leaking it, even if it isn't attached to any work due
1571 * to an error in dbuf_read_impl().
1575 err
= zio_wait(zio
);
1577 VERIFY0(zio_wait(zio
));
1581 * Another reader came in while the dbuf was in flight
1582 * between UNCACHED and CACHED. Either a writer will finish
1583 * writing the buffer (sending the dbuf to CACHED) or the
1584 * first reader's request will reach the read_done callback
1585 * and send the dbuf to CACHED. Otherwise, a failure
1586 * occurred and the dbuf went to UNCACHED.
1588 mutex_exit(&db
->db_mtx
);
1590 dmu_zfetch(&dn
->dn_zfetch
, db
->db_blkid
, 1, B_TRUE
);
1591 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
1592 rw_exit(&dn
->dn_struct_rwlock
);
1594 DBUF_STAT_BUMP(hash_misses
);
1596 /* Skip the wait per the caller's request. */
1597 mutex_enter(&db
->db_mtx
);
1598 if ((flags
& DB_RF_NEVERWAIT
) == 0) {
1599 while (db
->db_state
== DB_READ
||
1600 db
->db_state
== DB_FILL
) {
1601 ASSERT(db
->db_state
== DB_READ
||
1602 (flags
& DB_RF_HAVESTRUCT
) == 0);
1603 DTRACE_PROBE2(blocked__read
, dmu_buf_impl_t
*,
1605 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1607 if (db
->db_state
== DB_UNCACHED
)
1608 err
= SET_ERROR(EIO
);
1610 mutex_exit(&db
->db_mtx
);
1617 dbuf_noread(dmu_buf_impl_t
*db
)
1619 ASSERT(!zfs_refcount_is_zero(&db
->db_holds
));
1620 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1621 mutex_enter(&db
->db_mtx
);
1622 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
1623 cv_wait(&db
->db_changed
, &db
->db_mtx
);
1624 if (db
->db_state
== DB_UNCACHED
) {
1625 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1626 spa_t
*spa
= db
->db_objset
->os_spa
;
1628 ASSERT(db
->db_buf
== NULL
);
1629 ASSERT(db
->db
.db_data
== NULL
);
1630 dbuf_set_data(db
, arc_alloc_buf(spa
, db
, type
, db
->db
.db_size
));
1631 db
->db_state
= DB_FILL
;
1632 } else if (db
->db_state
== DB_NOFILL
) {
1633 dbuf_clear_data(db
);
1635 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
1637 mutex_exit(&db
->db_mtx
);
1641 dbuf_unoverride(dbuf_dirty_record_t
*dr
)
1643 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
1644 blkptr_t
*bp
= &dr
->dt
.dl
.dr_overridden_by
;
1645 uint64_t txg
= dr
->dr_txg
;
1647 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1649 * This assert is valid because dmu_sync() expects to be called by
1650 * a zilog's get_data while holding a range lock. This call only
1651 * comes from dbuf_dirty() callers who must also hold a range lock.
1653 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_IN_DMU_SYNC
);
1654 ASSERT(db
->db_level
== 0);
1656 if (db
->db_blkid
== DMU_BONUS_BLKID
||
1657 dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
)
1660 ASSERT(db
->db_data_pending
!= dr
);
1662 /* free this block */
1663 if (!BP_IS_HOLE(bp
) && !dr
->dt
.dl
.dr_nopwrite
)
1664 zio_free(db
->db_objset
->os_spa
, txg
, bp
);
1666 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
1667 dr
->dt
.dl
.dr_nopwrite
= B_FALSE
;
1668 dr
->dt
.dl
.dr_has_raw_params
= B_FALSE
;
1671 * Release the already-written buffer, so we leave it in
1672 * a consistent dirty state. Note that all callers are
1673 * modifying the buffer, so they will immediately do
1674 * another (redundant) arc_release(). Therefore, leave
1675 * the buf thawed to save the effort of freezing &
1676 * immediately re-thawing it.
1678 arc_release(dr
->dt
.dl
.dr_data
, db
);
1682 * Evict (if its unreferenced) or clear (if its referenced) any level-0
1683 * data blocks in the free range, so that any future readers will find
1687 dbuf_free_range(dnode_t
*dn
, uint64_t start_blkid
, uint64_t end_blkid
,
1690 dmu_buf_impl_t
*db_search
;
1691 dmu_buf_impl_t
*db
, *db_next
;
1692 uint64_t txg
= tx
->tx_txg
;
1695 if (end_blkid
> dn
->dn_maxblkid
&&
1696 !(start_blkid
== DMU_SPILL_BLKID
|| end_blkid
== DMU_SPILL_BLKID
))
1697 end_blkid
= dn
->dn_maxblkid
;
1698 dprintf_dnode(dn
, "start=%llu end=%llu\n", start_blkid
, end_blkid
);
1700 db_search
= kmem_alloc(sizeof (dmu_buf_impl_t
), KM_SLEEP
);
1701 db_search
->db_level
= 0;
1702 db_search
->db_blkid
= start_blkid
;
1703 db_search
->db_state
= DB_SEARCH
;
1705 mutex_enter(&dn
->dn_dbufs_mtx
);
1706 db
= avl_find(&dn
->dn_dbufs
, db_search
, &where
);
1707 ASSERT3P(db
, ==, NULL
);
1709 db
= avl_nearest(&dn
->dn_dbufs
, where
, AVL_AFTER
);
1711 for (; db
!= NULL
; db
= db_next
) {
1712 db_next
= AVL_NEXT(&dn
->dn_dbufs
, db
);
1713 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1715 if (db
->db_level
!= 0 || db
->db_blkid
> end_blkid
) {
1718 ASSERT3U(db
->db_blkid
, >=, start_blkid
);
1720 /* found a level 0 buffer in the range */
1721 mutex_enter(&db
->db_mtx
);
1722 if (dbuf_undirty(db
, tx
)) {
1723 /* mutex has been dropped and dbuf destroyed */
1727 if (db
->db_state
== DB_UNCACHED
||
1728 db
->db_state
== DB_NOFILL
||
1729 db
->db_state
== DB_EVICTING
) {
1730 ASSERT(db
->db
.db_data
== NULL
);
1731 mutex_exit(&db
->db_mtx
);
1734 if (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
) {
1735 /* will be handled in dbuf_read_done or dbuf_rele */
1736 db
->db_freed_in_flight
= TRUE
;
1737 mutex_exit(&db
->db_mtx
);
1740 if (zfs_refcount_count(&db
->db_holds
) == 0) {
1745 /* The dbuf is referenced */
1747 if (db
->db_last_dirty
!= NULL
) {
1748 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
1750 if (dr
->dr_txg
== txg
) {
1752 * This buffer is "in-use", re-adjust the file
1753 * size to reflect that this buffer may
1754 * contain new data when we sync.
1756 if (db
->db_blkid
!= DMU_SPILL_BLKID
&&
1757 db
->db_blkid
> dn
->dn_maxblkid
)
1758 dn
->dn_maxblkid
= db
->db_blkid
;
1759 dbuf_unoverride(dr
);
1762 * This dbuf is not dirty in the open context.
1763 * Either uncache it (if its not referenced in
1764 * the open context) or reset its contents to
1767 dbuf_fix_old_data(db
, txg
);
1770 /* clear the contents if its cached */
1771 if (db
->db_state
== DB_CACHED
) {
1772 ASSERT(db
->db
.db_data
!= NULL
);
1773 arc_release(db
->db_buf
, db
);
1774 bzero(db
->db
.db_data
, db
->db
.db_size
);
1775 arc_buf_freeze(db
->db_buf
);
1778 mutex_exit(&db
->db_mtx
);
1781 kmem_free(db_search
, sizeof (dmu_buf_impl_t
));
1782 mutex_exit(&dn
->dn_dbufs_mtx
);
1786 dbuf_new_size(dmu_buf_impl_t
*db
, int size
, dmu_tx_t
*tx
)
1788 arc_buf_t
*buf
, *obuf
;
1789 int osize
= db
->db
.db_size
;
1790 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
1793 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
1798 /* XXX does *this* func really need the lock? */
1799 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
1802 * This call to dmu_buf_will_dirty() with the dn_struct_rwlock held
1803 * is OK, because there can be no other references to the db
1804 * when we are changing its size, so no concurrent DB_FILL can
1808 * XXX we should be doing a dbuf_read, checking the return
1809 * value and returning that up to our callers
1811 dmu_buf_will_dirty(&db
->db
, tx
);
1813 /* create the data buffer for the new block */
1814 buf
= arc_alloc_buf(dn
->dn_objset
->os_spa
, db
, type
, size
);
1816 /* copy old block data to the new block */
1818 bcopy(obuf
->b_data
, buf
->b_data
, MIN(osize
, size
));
1819 /* zero the remainder */
1821 bzero((uint8_t *)buf
->b_data
+ osize
, size
- osize
);
1823 mutex_enter(&db
->db_mtx
);
1824 dbuf_set_data(db
, buf
);
1825 arc_buf_destroy(obuf
, db
);
1826 db
->db
.db_size
= size
;
1828 if (db
->db_level
== 0) {
1829 ASSERT3U(db
->db_last_dirty
->dr_txg
, ==, tx
->tx_txg
);
1830 db
->db_last_dirty
->dt
.dl
.dr_data
= buf
;
1832 mutex_exit(&db
->db_mtx
);
1834 dmu_objset_willuse_space(dn
->dn_objset
, size
- osize
, tx
);
1839 dbuf_release_bp(dmu_buf_impl_t
*db
)
1841 ASSERTV(objset_t
*os
= db
->db_objset
);
1843 ASSERT(dsl_pool_sync_context(dmu_objset_pool(os
)));
1844 ASSERT(arc_released(os
->os_phys_buf
) ||
1845 list_link_active(&os
->os_dsl_dataset
->ds_synced_link
));
1846 ASSERT(db
->db_parent
== NULL
|| arc_released(db
->db_parent
->db_buf
));
1848 (void) arc_release(db
->db_buf
, db
);
1852 * We already have a dirty record for this TXG, and we are being
1856 dbuf_redirty(dbuf_dirty_record_t
*dr
)
1858 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
1860 ASSERT(MUTEX_HELD(&db
->db_mtx
));
1862 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
) {
1864 * If this buffer has already been written out,
1865 * we now need to reset its state.
1867 dbuf_unoverride(dr
);
1868 if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
&&
1869 db
->db_state
!= DB_NOFILL
) {
1870 /* Already released on initial dirty, so just thaw. */
1871 ASSERT(arc_released(db
->db_buf
));
1872 arc_buf_thaw(db
->db_buf
);
1877 dbuf_dirty_record_t
*
1878 dbuf_dirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
1882 dbuf_dirty_record_t
**drp
, *dr
;
1883 int drop_struct_lock
= FALSE
;
1884 int txgoff
= tx
->tx_txg
& TXG_MASK
;
1886 ASSERT(tx
->tx_txg
!= 0);
1887 ASSERT(!zfs_refcount_is_zero(&db
->db_holds
));
1888 DMU_TX_DIRTY_BUF(tx
, db
);
1893 * Shouldn't dirty a regular buffer in syncing context. Private
1894 * objects may be dirtied in syncing context, but only if they
1895 * were already pre-dirtied in open context.
1898 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
) {
1899 rrw_enter(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
,
1902 ASSERT(!dmu_tx_is_syncing(tx
) ||
1903 BP_IS_HOLE(dn
->dn_objset
->os_rootbp
) ||
1904 DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
1905 dn
->dn_objset
->os_dsl_dataset
== NULL
);
1906 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
1907 rrw_exit(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
, FTAG
);
1910 * We make this assert for private objects as well, but after we
1911 * check if we're already dirty. They are allowed to re-dirty
1912 * in syncing context.
1914 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
1915 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
1916 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
1918 mutex_enter(&db
->db_mtx
);
1920 * XXX make this true for indirects too? The problem is that
1921 * transactions created with dmu_tx_create_assigned() from
1922 * syncing context don't bother holding ahead.
1924 ASSERT(db
->db_level
!= 0 ||
1925 db
->db_state
== DB_CACHED
|| db
->db_state
== DB_FILL
||
1926 db
->db_state
== DB_NOFILL
);
1928 mutex_enter(&dn
->dn_mtx
);
1930 * Don't set dirtyctx to SYNC if we're just modifying this as we
1931 * initialize the objset.
1933 if (dn
->dn_dirtyctx
== DN_UNDIRTIED
) {
1934 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
) {
1935 rrw_enter(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
,
1938 if (!BP_IS_HOLE(dn
->dn_objset
->os_rootbp
)) {
1939 dn
->dn_dirtyctx
= (dmu_tx_is_syncing(tx
) ?
1940 DN_DIRTY_SYNC
: DN_DIRTY_OPEN
);
1941 ASSERT(dn
->dn_dirtyctx_firstset
== NULL
);
1942 dn
->dn_dirtyctx_firstset
= kmem_alloc(1, KM_SLEEP
);
1944 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
) {
1945 rrw_exit(&dn
->dn_objset
->os_dsl_dataset
->ds_bp_rwlock
,
1950 if (tx
->tx_txg
> dn
->dn_dirty_txg
)
1951 dn
->dn_dirty_txg
= tx
->tx_txg
;
1952 mutex_exit(&dn
->dn_mtx
);
1954 if (db
->db_blkid
== DMU_SPILL_BLKID
)
1955 dn
->dn_have_spill
= B_TRUE
;
1958 * If this buffer is already dirty, we're done.
1960 drp
= &db
->db_last_dirty
;
1961 ASSERT(*drp
== NULL
|| (*drp
)->dr_txg
<= tx
->tx_txg
||
1962 db
->db
.db_object
== DMU_META_DNODE_OBJECT
);
1963 while ((dr
= *drp
) != NULL
&& dr
->dr_txg
> tx
->tx_txg
)
1965 if (dr
&& dr
->dr_txg
== tx
->tx_txg
) {
1969 mutex_exit(&db
->db_mtx
);
1974 * Only valid if not already dirty.
1976 ASSERT(dn
->dn_object
== 0 ||
1977 dn
->dn_dirtyctx
== DN_UNDIRTIED
|| dn
->dn_dirtyctx
==
1978 (dmu_tx_is_syncing(tx
) ? DN_DIRTY_SYNC
: DN_DIRTY_OPEN
));
1980 ASSERT3U(dn
->dn_nlevels
, >, db
->db_level
);
1983 * We should only be dirtying in syncing context if it's the
1984 * mos or we're initializing the os or it's a special object.
1985 * However, we are allowed to dirty in syncing context provided
1986 * we already dirtied it in open context. Hence we must make
1987 * this assertion only if we're not already dirty.
1990 VERIFY3U(tx
->tx_txg
, <=, spa_final_dirty_txg(os
->os_spa
));
1992 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
1993 rrw_enter(&os
->os_dsl_dataset
->ds_bp_rwlock
, RW_READER
, FTAG
);
1994 ASSERT(!dmu_tx_is_syncing(tx
) || DMU_OBJECT_IS_SPECIAL(dn
->dn_object
) ||
1995 os
->os_dsl_dataset
== NULL
|| BP_IS_HOLE(os
->os_rootbp
));
1996 if (dn
->dn_objset
->os_dsl_dataset
!= NULL
)
1997 rrw_exit(&os
->os_dsl_dataset
->ds_bp_rwlock
, FTAG
);
1999 ASSERT(db
->db
.db_size
!= 0);
2001 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
2003 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
2004 dmu_objset_willuse_space(os
, db
->db
.db_size
, tx
);
2008 * If this buffer is dirty in an old transaction group we need
2009 * to make a copy of it so that the changes we make in this
2010 * transaction group won't leak out when we sync the older txg.
2012 dr
= kmem_zalloc(sizeof (dbuf_dirty_record_t
), KM_SLEEP
);
2013 list_link_init(&dr
->dr_dirty_node
);
2014 if (db
->db_level
== 0) {
2015 void *data_old
= db
->db_buf
;
2017 if (db
->db_state
!= DB_NOFILL
) {
2018 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2019 dbuf_fix_old_data(db
, tx
->tx_txg
);
2020 data_old
= db
->db
.db_data
;
2021 } else if (db
->db
.db_object
!= DMU_META_DNODE_OBJECT
) {
2023 * Release the data buffer from the cache so
2024 * that we can modify it without impacting
2025 * possible other users of this cached data
2026 * block. Note that indirect blocks and
2027 * private objects are not released until the
2028 * syncing state (since they are only modified
2031 arc_release(db
->db_buf
, db
);
2032 dbuf_fix_old_data(db
, tx
->tx_txg
);
2033 data_old
= db
->db_buf
;
2035 ASSERT(data_old
!= NULL
);
2037 dr
->dt
.dl
.dr_data
= data_old
;
2039 mutex_init(&dr
->dt
.di
.dr_mtx
, NULL
, MUTEX_NOLOCKDEP
, NULL
);
2040 list_create(&dr
->dt
.di
.dr_children
,
2041 sizeof (dbuf_dirty_record_t
),
2042 offsetof(dbuf_dirty_record_t
, dr_dirty_node
));
2044 if (db
->db_blkid
!= DMU_BONUS_BLKID
&& os
->os_dsl_dataset
!= NULL
)
2045 dr
->dr_accounted
= db
->db
.db_size
;
2047 dr
->dr_txg
= tx
->tx_txg
;
2052 * We could have been freed_in_flight between the dbuf_noread
2053 * and dbuf_dirty. We win, as though the dbuf_noread() had
2054 * happened after the free.
2056 if (db
->db_level
== 0 && db
->db_blkid
!= DMU_BONUS_BLKID
&&
2057 db
->db_blkid
!= DMU_SPILL_BLKID
) {
2058 mutex_enter(&dn
->dn_mtx
);
2059 if (dn
->dn_free_ranges
[txgoff
] != NULL
) {
2060 range_tree_clear(dn
->dn_free_ranges
[txgoff
],
2063 mutex_exit(&dn
->dn_mtx
);
2064 db
->db_freed_in_flight
= FALSE
;
2068 * This buffer is now part of this txg
2070 dbuf_add_ref(db
, (void *)(uintptr_t)tx
->tx_txg
);
2071 db
->db_dirtycnt
+= 1;
2072 ASSERT3U(db
->db_dirtycnt
, <=, 3);
2074 mutex_exit(&db
->db_mtx
);
2076 if (db
->db_blkid
== DMU_BONUS_BLKID
||
2077 db
->db_blkid
== DMU_SPILL_BLKID
) {
2078 mutex_enter(&dn
->dn_mtx
);
2079 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
2080 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
2081 mutex_exit(&dn
->dn_mtx
);
2082 dnode_setdirty(dn
, tx
);
2088 * The dn_struct_rwlock prevents db_blkptr from changing
2089 * due to a write from syncing context completing
2090 * while we are running, so we want to acquire it before
2091 * looking at db_blkptr.
2093 if (!RW_WRITE_HELD(&dn
->dn_struct_rwlock
)) {
2094 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
2095 drop_struct_lock
= TRUE
;
2099 * We need to hold the dn_struct_rwlock to make this assertion,
2100 * because it protects dn_phys / dn_next_nlevels from changing.
2102 ASSERT((dn
->dn_phys
->dn_nlevels
== 0 && db
->db_level
== 0) ||
2103 dn
->dn_phys
->dn_nlevels
> db
->db_level
||
2104 dn
->dn_next_nlevels
[txgoff
] > db
->db_level
||
2105 dn
->dn_next_nlevels
[(tx
->tx_txg
-1) & TXG_MASK
] > db
->db_level
||
2106 dn
->dn_next_nlevels
[(tx
->tx_txg
-2) & TXG_MASK
] > db
->db_level
);
2109 * If we are overwriting a dedup BP, then unless it is snapshotted,
2110 * when we get to syncing context we will need to decrement its
2111 * refcount in the DDT. Prefetch the relevant DDT block so that
2112 * syncing context won't have to wait for the i/o.
2114 ddt_prefetch(os
->os_spa
, db
->db_blkptr
);
2116 if (db
->db_level
== 0) {
2117 ASSERT(!db
->db_objset
->os_raw_receive
||
2118 dn
->dn_maxblkid
>= db
->db_blkid
);
2119 dnode_new_blkid(dn
, db
->db_blkid
, tx
, drop_struct_lock
);
2120 ASSERT(dn
->dn_maxblkid
>= db
->db_blkid
);
2123 if (db
->db_level
+1 < dn
->dn_nlevels
) {
2124 dmu_buf_impl_t
*parent
= db
->db_parent
;
2125 dbuf_dirty_record_t
*di
;
2126 int parent_held
= FALSE
;
2128 if (db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
) {
2129 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2131 parent
= dbuf_hold_level(dn
, db
->db_level
+1,
2132 db
->db_blkid
>> epbs
, FTAG
);
2133 ASSERT(parent
!= NULL
);
2136 if (drop_struct_lock
)
2137 rw_exit(&dn
->dn_struct_rwlock
);
2138 ASSERT3U(db
->db_level
+1, ==, parent
->db_level
);
2139 di
= dbuf_dirty(parent
, tx
);
2141 dbuf_rele(parent
, FTAG
);
2143 mutex_enter(&db
->db_mtx
);
2145 * Since we've dropped the mutex, it's possible that
2146 * dbuf_undirty() might have changed this out from under us.
2148 if (db
->db_last_dirty
== dr
||
2149 dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
2150 mutex_enter(&di
->dt
.di
.dr_mtx
);
2151 ASSERT3U(di
->dr_txg
, ==, tx
->tx_txg
);
2152 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
2153 list_insert_tail(&di
->dt
.di
.dr_children
, dr
);
2154 mutex_exit(&di
->dt
.di
.dr_mtx
);
2157 mutex_exit(&db
->db_mtx
);
2159 ASSERT(db
->db_level
+1 == dn
->dn_nlevels
);
2160 ASSERT(db
->db_blkid
< dn
->dn_nblkptr
);
2161 ASSERT(db
->db_parent
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
2162 mutex_enter(&dn
->dn_mtx
);
2163 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
2164 list_insert_tail(&dn
->dn_dirty_records
[txgoff
], dr
);
2165 mutex_exit(&dn
->dn_mtx
);
2166 if (drop_struct_lock
)
2167 rw_exit(&dn
->dn_struct_rwlock
);
2170 dnode_setdirty(dn
, tx
);
2176 * Undirty a buffer in the transaction group referenced by the given
2177 * transaction. Return whether this evicted the dbuf.
2180 dbuf_undirty(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
2183 uint64_t txg
= tx
->tx_txg
;
2184 dbuf_dirty_record_t
*dr
, **drp
;
2189 * Due to our use of dn_nlevels below, this can only be called
2190 * in open context, unless we are operating on the MOS.
2191 * From syncing context, dn_nlevels may be different from the
2192 * dn_nlevels used when dbuf was dirtied.
2194 ASSERT(db
->db_objset
==
2195 dmu_objset_pool(db
->db_objset
)->dp_meta_objset
||
2196 txg
!= spa_syncing_txg(dmu_objset_spa(db
->db_objset
)));
2197 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2198 ASSERT0(db
->db_level
);
2199 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2202 * If this buffer is not dirty, we're done.
2204 for (drp
= &db
->db_last_dirty
; (dr
= *drp
) != NULL
; drp
= &dr
->dr_next
)
2205 if (dr
->dr_txg
<= txg
)
2207 if (dr
== NULL
|| dr
->dr_txg
< txg
)
2209 ASSERT(dr
->dr_txg
== txg
);
2210 ASSERT(dr
->dr_dbuf
== db
);
2215 dprintf_dbuf(db
, "size=%llx\n", (u_longlong_t
)db
->db
.db_size
);
2217 ASSERT(db
->db
.db_size
!= 0);
2219 dsl_pool_undirty_space(dmu_objset_pool(dn
->dn_objset
),
2220 dr
->dr_accounted
, txg
);
2225 * Note that there are three places in dbuf_dirty()
2226 * where this dirty record may be put on a list.
2227 * Make sure to do a list_remove corresponding to
2228 * every one of those list_insert calls.
2230 if (dr
->dr_parent
) {
2231 mutex_enter(&dr
->dr_parent
->dt
.di
.dr_mtx
);
2232 list_remove(&dr
->dr_parent
->dt
.di
.dr_children
, dr
);
2233 mutex_exit(&dr
->dr_parent
->dt
.di
.dr_mtx
);
2234 } else if (db
->db_blkid
== DMU_SPILL_BLKID
||
2235 db
->db_level
+ 1 == dn
->dn_nlevels
) {
2236 ASSERT(db
->db_blkptr
== NULL
|| db
->db_parent
== dn
->dn_dbuf
);
2237 mutex_enter(&dn
->dn_mtx
);
2238 list_remove(&dn
->dn_dirty_records
[txg
& TXG_MASK
], dr
);
2239 mutex_exit(&dn
->dn_mtx
);
2243 if (db
->db_state
!= DB_NOFILL
) {
2244 dbuf_unoverride(dr
);
2246 ASSERT(db
->db_buf
!= NULL
);
2247 ASSERT(dr
->dt
.dl
.dr_data
!= NULL
);
2248 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
2249 arc_buf_destroy(dr
->dt
.dl
.dr_data
, db
);
2252 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
2254 ASSERT(db
->db_dirtycnt
> 0);
2255 db
->db_dirtycnt
-= 1;
2257 if (zfs_refcount_remove(&db
->db_holds
, (void *)(uintptr_t)txg
) == 0) {
2258 ASSERT(db
->db_state
== DB_NOFILL
|| arc_released(db
->db_buf
));
2267 dmu_buf_will_dirty_impl(dmu_buf_t
*db_fake
, int flags
, dmu_tx_t
*tx
)
2269 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2271 ASSERT(tx
->tx_txg
!= 0);
2272 ASSERT(!zfs_refcount_is_zero(&db
->db_holds
));
2275 * Quick check for dirtyness. For already dirty blocks, this
2276 * reduces runtime of this function by >90%, and overall performance
2277 * by 50% for some workloads (e.g. file deletion with indirect blocks
2280 mutex_enter(&db
->db_mtx
);
2282 dbuf_dirty_record_t
*dr
;
2283 for (dr
= db
->db_last_dirty
;
2284 dr
!= NULL
&& dr
->dr_txg
>= tx
->tx_txg
; dr
= dr
->dr_next
) {
2286 * It's possible that it is already dirty but not cached,
2287 * because there are some calls to dbuf_dirty() that don't
2288 * go through dmu_buf_will_dirty().
2290 if (dr
->dr_txg
== tx
->tx_txg
&& db
->db_state
== DB_CACHED
) {
2291 /* This dbuf is already dirty and cached. */
2293 mutex_exit(&db
->db_mtx
);
2297 mutex_exit(&db
->db_mtx
);
2300 if (RW_WRITE_HELD(&DB_DNODE(db
)->dn_struct_rwlock
))
2301 flags
|= DB_RF_HAVESTRUCT
;
2303 (void) dbuf_read(db
, NULL
, flags
);
2304 (void) dbuf_dirty(db
, tx
);
2308 dmu_buf_will_dirty(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
2310 dmu_buf_will_dirty_impl(db_fake
,
2311 DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
, tx
);
2315 dmu_buf_is_dirty(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
2317 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2319 mutex_enter(&db
->db_mtx
);
2320 for (dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
2321 dr
!= NULL
&& dr
->dr_txg
>= tx
->tx_txg
; dr
= dr
->dr_next
) {
2322 if (dr
->dr_txg
== tx
->tx_txg
) {
2323 mutex_exit(&db
->db_mtx
);
2327 mutex_exit(&db
->db_mtx
);
2332 dmu_buf_will_not_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
2334 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2336 db
->db_state
= DB_NOFILL
;
2338 dmu_buf_will_fill(db_fake
, tx
);
2342 dmu_buf_will_fill(dmu_buf_t
*db_fake
, dmu_tx_t
*tx
)
2344 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2346 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2347 ASSERT(tx
->tx_txg
!= 0);
2348 ASSERT(db
->db_level
== 0);
2349 ASSERT(!zfs_refcount_is_zero(&db
->db_holds
));
2351 ASSERT(db
->db
.db_object
!= DMU_META_DNODE_OBJECT
||
2352 dmu_tx_private_ok(tx
));
2355 (void) dbuf_dirty(db
, tx
);
2359 * This function is effectively the same as dmu_buf_will_dirty(), but
2360 * indicates the caller expects raw encrypted data in the db, and provides
2361 * the crypt params (byteorder, salt, iv, mac) which should be stored in the
2362 * blkptr_t when this dbuf is written. This is only used for blocks of
2363 * dnodes, during raw receive.
2366 dmu_buf_set_crypt_params(dmu_buf_t
*db_fake
, boolean_t byteorder
,
2367 const uint8_t *salt
, const uint8_t *iv
, const uint8_t *mac
, dmu_tx_t
*tx
)
2369 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2370 dbuf_dirty_record_t
*dr
;
2373 * dr_has_raw_params is only processed for blocks of dnodes
2374 * (see dbuf_sync_dnode_leaf_crypt()).
2376 ASSERT3U(db
->db
.db_object
, ==, DMU_META_DNODE_OBJECT
);
2377 ASSERT3U(db
->db_level
, ==, 0);
2378 ASSERT(db
->db_objset
->os_raw_receive
);
2380 dmu_buf_will_dirty_impl(db_fake
,
2381 DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
| DB_RF_NO_DECRYPT
, tx
);
2383 dr
= db
->db_last_dirty
;
2384 while (dr
!= NULL
&& dr
->dr_txg
> tx
->tx_txg
)
2387 ASSERT3P(dr
, !=, NULL
);
2388 ASSERT3U(dr
->dr_txg
, ==, tx
->tx_txg
);
2390 dr
->dt
.dl
.dr_has_raw_params
= B_TRUE
;
2391 dr
->dt
.dl
.dr_byteorder
= byteorder
;
2392 bcopy(salt
, dr
->dt
.dl
.dr_salt
, ZIO_DATA_SALT_LEN
);
2393 bcopy(iv
, dr
->dt
.dl
.dr_iv
, ZIO_DATA_IV_LEN
);
2394 bcopy(mac
, dr
->dt
.dl
.dr_mac
, ZIO_DATA_MAC_LEN
);
2397 #pragma weak dmu_buf_fill_done = dbuf_fill_done
2400 dbuf_fill_done(dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
2402 mutex_enter(&db
->db_mtx
);
2405 if (db
->db_state
== DB_FILL
) {
2406 if (db
->db_level
== 0 && db
->db_freed_in_flight
) {
2407 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2408 /* we were freed while filling */
2409 /* XXX dbuf_undirty? */
2410 bzero(db
->db
.db_data
, db
->db
.db_size
);
2411 db
->db_freed_in_flight
= FALSE
;
2413 db
->db_state
= DB_CACHED
;
2414 cv_broadcast(&db
->db_changed
);
2416 mutex_exit(&db
->db_mtx
);
2420 dmu_buf_write_embedded(dmu_buf_t
*dbuf
, void *data
,
2421 bp_embedded_type_t etype
, enum zio_compress comp
,
2422 int uncompressed_size
, int compressed_size
, int byteorder
,
2425 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbuf
;
2426 struct dirty_leaf
*dl
;
2427 dmu_object_type_t type
;
2429 if (etype
== BP_EMBEDDED_TYPE_DATA
) {
2430 ASSERT(spa_feature_is_active(dmu_objset_spa(db
->db_objset
),
2431 SPA_FEATURE_EMBEDDED_DATA
));
2435 type
= DB_DNODE(db
)->dn_type
;
2438 ASSERT0(db
->db_level
);
2439 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2441 dmu_buf_will_not_fill(dbuf
, tx
);
2443 ASSERT3U(db
->db_last_dirty
->dr_txg
, ==, tx
->tx_txg
);
2444 dl
= &db
->db_last_dirty
->dt
.dl
;
2445 encode_embedded_bp_compressed(&dl
->dr_overridden_by
,
2446 data
, comp
, uncompressed_size
, compressed_size
);
2447 BPE_SET_ETYPE(&dl
->dr_overridden_by
, etype
);
2448 BP_SET_TYPE(&dl
->dr_overridden_by
, type
);
2449 BP_SET_LEVEL(&dl
->dr_overridden_by
, 0);
2450 BP_SET_BYTEORDER(&dl
->dr_overridden_by
, byteorder
);
2452 dl
->dr_override_state
= DR_OVERRIDDEN
;
2453 dl
->dr_overridden_by
.blk_birth
= db
->db_last_dirty
->dr_txg
;
2457 * Directly assign a provided arc buf to a given dbuf if it's not referenced
2458 * by anybody except our caller. Otherwise copy arcbuf's contents to dbuf.
2461 dbuf_assign_arcbuf(dmu_buf_impl_t
*db
, arc_buf_t
*buf
, dmu_tx_t
*tx
)
2463 ASSERT(!zfs_refcount_is_zero(&db
->db_holds
));
2464 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
2465 ASSERT(db
->db_level
== 0);
2466 ASSERT3U(dbuf_is_metadata(db
), ==, arc_is_metadata(buf
));
2467 ASSERT(buf
!= NULL
);
2468 ASSERT(arc_buf_lsize(buf
) == db
->db
.db_size
);
2469 ASSERT(tx
->tx_txg
!= 0);
2471 arc_return_buf(buf
, db
);
2472 ASSERT(arc_released(buf
));
2474 mutex_enter(&db
->db_mtx
);
2476 while (db
->db_state
== DB_READ
|| db
->db_state
== DB_FILL
)
2477 cv_wait(&db
->db_changed
, &db
->db_mtx
);
2479 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_UNCACHED
);
2481 if (db
->db_state
== DB_CACHED
&&
2482 zfs_refcount_count(&db
->db_holds
) - 1 > db
->db_dirtycnt
) {
2484 * In practice, we will never have a case where we have an
2485 * encrypted arc buffer while additional holds exist on the
2486 * dbuf. We don't handle this here so we simply assert that
2489 ASSERT(!arc_is_encrypted(buf
));
2490 mutex_exit(&db
->db_mtx
);
2491 (void) dbuf_dirty(db
, tx
);
2492 bcopy(buf
->b_data
, db
->db
.db_data
, db
->db
.db_size
);
2493 arc_buf_destroy(buf
, db
);
2494 xuio_stat_wbuf_copied();
2498 xuio_stat_wbuf_nocopy();
2499 if (db
->db_state
== DB_CACHED
) {
2500 dbuf_dirty_record_t
*dr
= db
->db_last_dirty
;
2502 ASSERT(db
->db_buf
!= NULL
);
2503 if (dr
!= NULL
&& dr
->dr_txg
== tx
->tx_txg
) {
2504 ASSERT(dr
->dt
.dl
.dr_data
== db
->db_buf
);
2506 if (!arc_released(db
->db_buf
)) {
2507 ASSERT(dr
->dt
.dl
.dr_override_state
==
2509 arc_release(db
->db_buf
, db
);
2511 dr
->dt
.dl
.dr_data
= buf
;
2512 arc_buf_destroy(db
->db_buf
, db
);
2513 } else if (dr
== NULL
|| dr
->dt
.dl
.dr_data
!= db
->db_buf
) {
2514 arc_release(db
->db_buf
, db
);
2515 arc_buf_destroy(db
->db_buf
, db
);
2519 ASSERT(db
->db_buf
== NULL
);
2520 dbuf_set_data(db
, buf
);
2521 db
->db_state
= DB_FILL
;
2522 mutex_exit(&db
->db_mtx
);
2523 (void) dbuf_dirty(db
, tx
);
2524 dmu_buf_fill_done(&db
->db
, tx
);
2528 dbuf_destroy(dmu_buf_impl_t
*db
)
2531 dmu_buf_impl_t
*parent
= db
->db_parent
;
2532 dmu_buf_impl_t
*dndb
;
2534 ASSERT(MUTEX_HELD(&db
->db_mtx
));
2535 ASSERT(zfs_refcount_is_zero(&db
->db_holds
));
2537 if (db
->db_buf
!= NULL
) {
2538 arc_buf_destroy(db
->db_buf
, db
);
2542 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
2543 int slots
= DB_DNODE(db
)->dn_num_slots
;
2544 int bonuslen
= DN_SLOTS_TO_BONUSLEN(slots
);
2545 if (db
->db
.db_data
!= NULL
) {
2546 kmem_free(db
->db
.db_data
, bonuslen
);
2547 arc_space_return(bonuslen
, ARC_SPACE_BONUS
);
2548 db
->db_state
= DB_UNCACHED
;
2552 dbuf_clear_data(db
);
2554 if (multilist_link_active(&db
->db_cache_link
)) {
2555 ASSERT(db
->db_caching_status
== DB_DBUF_CACHE
||
2556 db
->db_caching_status
== DB_DBUF_METADATA_CACHE
);
2558 multilist_remove(dbuf_caches
[db
->db_caching_status
].cache
, db
);
2559 (void) zfs_refcount_remove_many(
2560 &dbuf_caches
[db
->db_caching_status
].size
,
2561 db
->db
.db_size
, db
);
2563 if (db
->db_caching_status
== DB_DBUF_METADATA_CACHE
) {
2564 DBUF_STAT_BUMPDOWN(metadata_cache_count
);
2566 DBUF_STAT_BUMPDOWN(cache_levels
[db
->db_level
]);
2567 DBUF_STAT_BUMPDOWN(cache_count
);
2568 DBUF_STAT_DECR(cache_levels_bytes
[db
->db_level
],
2571 db
->db_caching_status
= DB_NO_CACHE
;
2574 ASSERT(db
->db_state
== DB_UNCACHED
|| db
->db_state
== DB_NOFILL
);
2575 ASSERT(db
->db_data_pending
== NULL
);
2577 db
->db_state
= DB_EVICTING
;
2578 db
->db_blkptr
= NULL
;
2581 * Now that db_state is DB_EVICTING, nobody else can find this via
2582 * the hash table. We can now drop db_mtx, which allows us to
2583 * acquire the dn_dbufs_mtx.
2585 mutex_exit(&db
->db_mtx
);
2590 if (db
->db_blkid
!= DMU_BONUS_BLKID
) {
2591 boolean_t needlock
= !MUTEX_HELD(&dn
->dn_dbufs_mtx
);
2593 mutex_enter(&dn
->dn_dbufs_mtx
);
2594 avl_remove(&dn
->dn_dbufs
, db
);
2595 atomic_dec_32(&dn
->dn_dbufs_count
);
2599 mutex_exit(&dn
->dn_dbufs_mtx
);
2601 * Decrementing the dbuf count means that the hold corresponding
2602 * to the removed dbuf is no longer discounted in dnode_move(),
2603 * so the dnode cannot be moved until after we release the hold.
2604 * The membar_producer() ensures visibility of the decremented
2605 * value in dnode_move(), since DB_DNODE_EXIT doesn't actually
2608 mutex_enter(&dn
->dn_mtx
);
2609 dnode_rele_and_unlock(dn
, db
, B_TRUE
);
2610 db
->db_dnode_handle
= NULL
;
2612 dbuf_hash_remove(db
);
2617 ASSERT(zfs_refcount_is_zero(&db
->db_holds
));
2619 db
->db_parent
= NULL
;
2621 ASSERT(db
->db_buf
== NULL
);
2622 ASSERT(db
->db
.db_data
== NULL
);
2623 ASSERT(db
->db_hash_next
== NULL
);
2624 ASSERT(db
->db_blkptr
== NULL
);
2625 ASSERT(db
->db_data_pending
== NULL
);
2626 ASSERT3U(db
->db_caching_status
, ==, DB_NO_CACHE
);
2627 ASSERT(!multilist_link_active(&db
->db_cache_link
));
2629 kmem_cache_free(dbuf_kmem_cache
, db
);
2630 arc_space_return(sizeof (dmu_buf_impl_t
), ARC_SPACE_DBUF
);
2633 * If this dbuf is referenced from an indirect dbuf,
2634 * decrement the ref count on the indirect dbuf.
2636 if (parent
&& parent
!= dndb
) {
2637 mutex_enter(&parent
->db_mtx
);
2638 dbuf_rele_and_unlock(parent
, db
, B_TRUE
);
2643 * Note: While bpp will always be updated if the function returns success,
2644 * parentp will not be updated if the dnode does not have dn_dbuf filled in;
2645 * this happens when the dnode is the meta-dnode, or {user|group|project}used
2648 __attribute__((always_inline
))
2650 dbuf_findbp(dnode_t
*dn
, int level
, uint64_t blkid
, int fail_sparse
,
2651 dmu_buf_impl_t
**parentp
, blkptr_t
**bpp
)
2656 ASSERT(blkid
!= DMU_BONUS_BLKID
);
2658 if (blkid
== DMU_SPILL_BLKID
) {
2659 mutex_enter(&dn
->dn_mtx
);
2660 if (dn
->dn_have_spill
&&
2661 (dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
))
2662 *bpp
= DN_SPILL_BLKPTR(dn
->dn_phys
);
2665 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
2666 *parentp
= dn
->dn_dbuf
;
2667 mutex_exit(&dn
->dn_mtx
);
2672 (dn
->dn_phys
->dn_nlevels
== 0) ? 1 : dn
->dn_phys
->dn_nlevels
;
2673 int epbs
= dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2675 ASSERT3U(level
* epbs
, <, 64);
2676 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2678 * This assertion shouldn't trip as long as the max indirect block size
2679 * is less than 1M. The reason for this is that up to that point,
2680 * the number of levels required to address an entire object with blocks
2681 * of size SPA_MINBLOCKSIZE satisfies nlevels * epbs + 1 <= 64. In
2682 * other words, if N * epbs + 1 > 64, then if (N-1) * epbs + 1 > 55
2683 * (i.e. we can address the entire object), objects will all use at most
2684 * N-1 levels and the assertion won't overflow. However, once epbs is
2685 * 13, 4 * 13 + 1 = 53, but 5 * 13 + 1 = 66. Then, 4 levels will not be
2686 * enough to address an entire object, so objects will have 5 levels,
2687 * but then this assertion will overflow.
2689 * All this is to say that if we ever increase DN_MAX_INDBLKSHIFT, we
2690 * need to redo this logic to handle overflows.
2692 ASSERT(level
>= nlevels
||
2693 ((nlevels
- level
- 1) * epbs
) +
2694 highbit64(dn
->dn_phys
->dn_nblkptr
) <= 64);
2695 if (level
>= nlevels
||
2696 blkid
>= ((uint64_t)dn
->dn_phys
->dn_nblkptr
<<
2697 ((nlevels
- level
- 1) * epbs
)) ||
2699 blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))) {
2700 /* the buffer has no parent yet */
2701 return (SET_ERROR(ENOENT
));
2702 } else if (level
< nlevels
-1) {
2703 /* this block is referenced from an indirect block */
2705 dbuf_hold_arg_t
*dh
= dbuf_hold_arg_create(dn
, level
+ 1,
2706 blkid
>> epbs
, fail_sparse
, FALSE
, NULL
, parentp
);
2707 err
= dbuf_hold_impl_arg(dh
);
2708 dbuf_hold_arg_destroy(dh
);
2711 err
= dbuf_read(*parentp
, NULL
,
2712 (DB_RF_HAVESTRUCT
| DB_RF_NOPREFETCH
| DB_RF_CANFAIL
));
2714 dbuf_rele(*parentp
, NULL
);
2718 *bpp
= ((blkptr_t
*)(*parentp
)->db
.db_data
) +
2719 (blkid
& ((1ULL << epbs
) - 1));
2720 if (blkid
> (dn
->dn_phys
->dn_maxblkid
>> (level
* epbs
)))
2721 ASSERT(BP_IS_HOLE(*bpp
));
2724 /* the block is referenced from the dnode */
2725 ASSERT3U(level
, ==, nlevels
-1);
2726 ASSERT(dn
->dn_phys
->dn_nblkptr
== 0 ||
2727 blkid
< dn
->dn_phys
->dn_nblkptr
);
2729 dbuf_add_ref(dn
->dn_dbuf
, NULL
);
2730 *parentp
= dn
->dn_dbuf
;
2732 *bpp
= &dn
->dn_phys
->dn_blkptr
[blkid
];
2737 static dmu_buf_impl_t
*
2738 dbuf_create(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
2739 dmu_buf_impl_t
*parent
, blkptr_t
*blkptr
)
2741 objset_t
*os
= dn
->dn_objset
;
2742 dmu_buf_impl_t
*db
, *odb
;
2744 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2745 ASSERT(dn
->dn_type
!= DMU_OT_NONE
);
2747 db
= kmem_cache_alloc(dbuf_kmem_cache
, KM_SLEEP
);
2750 db
->db
.db_object
= dn
->dn_object
;
2751 db
->db_level
= level
;
2752 db
->db_blkid
= blkid
;
2753 db
->db_last_dirty
= NULL
;
2754 db
->db_dirtycnt
= 0;
2755 db
->db_dnode_handle
= dn
->dn_handle
;
2756 db
->db_parent
= parent
;
2757 db
->db_blkptr
= blkptr
;
2760 db
->db_user_immediate_evict
= FALSE
;
2761 db
->db_freed_in_flight
= FALSE
;
2762 db
->db_pending_evict
= FALSE
;
2764 if (blkid
== DMU_BONUS_BLKID
) {
2765 ASSERT3P(parent
, ==, dn
->dn_dbuf
);
2766 db
->db
.db_size
= DN_SLOTS_TO_BONUSLEN(dn
->dn_num_slots
) -
2767 (dn
->dn_nblkptr
-1) * sizeof (blkptr_t
);
2768 ASSERT3U(db
->db
.db_size
, >=, dn
->dn_bonuslen
);
2769 db
->db
.db_offset
= DMU_BONUS_BLKID
;
2770 db
->db_state
= DB_UNCACHED
;
2771 db
->db_caching_status
= DB_NO_CACHE
;
2772 /* the bonus dbuf is not placed in the hash table */
2773 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_DBUF
);
2775 } else if (blkid
== DMU_SPILL_BLKID
) {
2776 db
->db
.db_size
= (blkptr
!= NULL
) ?
2777 BP_GET_LSIZE(blkptr
) : SPA_MINBLOCKSIZE
;
2778 db
->db
.db_offset
= 0;
2781 db
->db_level
? 1 << dn
->dn_indblkshift
: dn
->dn_datablksz
;
2782 db
->db
.db_size
= blocksize
;
2783 db
->db
.db_offset
= db
->db_blkid
* blocksize
;
2787 * Hold the dn_dbufs_mtx while we get the new dbuf
2788 * in the hash table *and* added to the dbufs list.
2789 * This prevents a possible deadlock with someone
2790 * trying to look up this dbuf before its added to the
2793 mutex_enter(&dn
->dn_dbufs_mtx
);
2794 db
->db_state
= DB_EVICTING
;
2795 if ((odb
= dbuf_hash_insert(db
)) != NULL
) {
2796 /* someone else inserted it first */
2797 kmem_cache_free(dbuf_kmem_cache
, db
);
2798 mutex_exit(&dn
->dn_dbufs_mtx
);
2799 DBUF_STAT_BUMP(hash_insert_race
);
2802 avl_add(&dn
->dn_dbufs
, db
);
2804 db
->db_state
= DB_UNCACHED
;
2805 db
->db_caching_status
= DB_NO_CACHE
;
2806 mutex_exit(&dn
->dn_dbufs_mtx
);
2807 arc_space_consume(sizeof (dmu_buf_impl_t
), ARC_SPACE_DBUF
);
2809 if (parent
&& parent
!= dn
->dn_dbuf
)
2810 dbuf_add_ref(parent
, db
);
2812 ASSERT(dn
->dn_object
== DMU_META_DNODE_OBJECT
||
2813 zfs_refcount_count(&dn
->dn_holds
) > 0);
2814 (void) zfs_refcount_add(&dn
->dn_holds
, db
);
2815 atomic_inc_32(&dn
->dn_dbufs_count
);
2817 dprintf_dbuf(db
, "db=%p\n", db
);
2822 typedef struct dbuf_prefetch_arg
{
2823 spa_t
*dpa_spa
; /* The spa to issue the prefetch in. */
2824 zbookmark_phys_t dpa_zb
; /* The target block to prefetch. */
2825 int dpa_epbs
; /* Entries (blkptr_t's) Per Block Shift. */
2826 int dpa_curlevel
; /* The current level that we're reading */
2827 dnode_t
*dpa_dnode
; /* The dnode associated with the prefetch */
2828 zio_priority_t dpa_prio
; /* The priority I/Os should be issued at. */
2829 zio_t
*dpa_zio
; /* The parent zio_t for all prefetches. */
2830 arc_flags_t dpa_aflags
; /* Flags to pass to the final prefetch. */
2831 } dbuf_prefetch_arg_t
;
2834 * Actually issue the prefetch read for the block given.
2837 dbuf_issue_final_prefetch(dbuf_prefetch_arg_t
*dpa
, blkptr_t
*bp
)
2839 if (BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
))
2842 int zio_flags
= ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
;
2843 arc_flags_t aflags
=
2844 dpa
->dpa_aflags
| ARC_FLAG_NOWAIT
| ARC_FLAG_PREFETCH
;
2846 /* dnodes are always read as raw and then converted later */
2847 if (BP_GET_TYPE(bp
) == DMU_OT_DNODE
&& BP_IS_PROTECTED(bp
) &&
2848 dpa
->dpa_curlevel
== 0)
2849 zio_flags
|= ZIO_FLAG_RAW
;
2851 ASSERT3U(dpa
->dpa_curlevel
, ==, BP_GET_LEVEL(bp
));
2852 ASSERT3U(dpa
->dpa_curlevel
, ==, dpa
->dpa_zb
.zb_level
);
2853 ASSERT(dpa
->dpa_zio
!= NULL
);
2854 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
, bp
, NULL
, NULL
,
2855 dpa
->dpa_prio
, zio_flags
, &aflags
, &dpa
->dpa_zb
);
2859 * Called when an indirect block above our prefetch target is read in. This
2860 * will either read in the next indirect block down the tree or issue the actual
2861 * prefetch if the next block down is our target.
2864 dbuf_prefetch_indirect_done(zio_t
*zio
, const zbookmark_phys_t
*zb
,
2865 const blkptr_t
*iobp
, arc_buf_t
*abuf
, void *private)
2867 dbuf_prefetch_arg_t
*dpa
= private;
2869 ASSERT3S(dpa
->dpa_zb
.zb_level
, <, dpa
->dpa_curlevel
);
2870 ASSERT3S(dpa
->dpa_curlevel
, >, 0);
2873 ASSERT(zio
== NULL
|| zio
->io_error
!= 0);
2874 kmem_free(dpa
, sizeof (*dpa
));
2877 ASSERT(zio
== NULL
|| zio
->io_error
== 0);
2880 * The dpa_dnode is only valid if we are called with a NULL
2881 * zio. This indicates that the arc_read() returned without
2882 * first calling zio_read() to issue a physical read. Once
2883 * a physical read is made the dpa_dnode must be invalidated
2884 * as the locks guarding it may have been dropped. If the
2885 * dpa_dnode is still valid, then we want to add it to the dbuf
2886 * cache. To do so, we must hold the dbuf associated with the block
2887 * we just prefetched, read its contents so that we associate it
2888 * with an arc_buf_t, and then release it.
2891 ASSERT3S(BP_GET_LEVEL(zio
->io_bp
), ==, dpa
->dpa_curlevel
);
2892 if (zio
->io_flags
& ZIO_FLAG_RAW_COMPRESS
) {
2893 ASSERT3U(BP_GET_PSIZE(zio
->io_bp
), ==, zio
->io_size
);
2895 ASSERT3U(BP_GET_LSIZE(zio
->io_bp
), ==, zio
->io_size
);
2897 ASSERT3P(zio
->io_spa
, ==, dpa
->dpa_spa
);
2899 dpa
->dpa_dnode
= NULL
;
2900 } else if (dpa
->dpa_dnode
!= NULL
) {
2901 uint64_t curblkid
= dpa
->dpa_zb
.zb_blkid
>>
2902 (dpa
->dpa_epbs
* (dpa
->dpa_curlevel
-
2903 dpa
->dpa_zb
.zb_level
));
2904 dmu_buf_impl_t
*db
= dbuf_hold_level(dpa
->dpa_dnode
,
2905 dpa
->dpa_curlevel
, curblkid
, FTAG
);
2907 kmem_free(dpa
, sizeof (*dpa
));
2908 arc_buf_destroy(abuf
, private);
2912 (void) dbuf_read(db
, NULL
,
2913 DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
| DB_RF_HAVESTRUCT
);
2914 dbuf_rele(db
, FTAG
);
2917 dpa
->dpa_curlevel
--;
2918 uint64_t nextblkid
= dpa
->dpa_zb
.zb_blkid
>>
2919 (dpa
->dpa_epbs
* (dpa
->dpa_curlevel
- dpa
->dpa_zb
.zb_level
));
2920 blkptr_t
*bp
= ((blkptr_t
*)abuf
->b_data
) +
2921 P2PHASE(nextblkid
, 1ULL << dpa
->dpa_epbs
);
2923 if (BP_IS_HOLE(bp
)) {
2924 kmem_free(dpa
, sizeof (*dpa
));
2925 } else if (dpa
->dpa_curlevel
== dpa
->dpa_zb
.zb_level
) {
2926 ASSERT3U(nextblkid
, ==, dpa
->dpa_zb
.zb_blkid
);
2927 dbuf_issue_final_prefetch(dpa
, bp
);
2928 kmem_free(dpa
, sizeof (*dpa
));
2930 arc_flags_t iter_aflags
= ARC_FLAG_NOWAIT
;
2931 zbookmark_phys_t zb
;
2933 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
2934 if (dpa
->dpa_aflags
& ARC_FLAG_L2CACHE
)
2935 iter_aflags
|= ARC_FLAG_L2CACHE
;
2937 ASSERT3U(dpa
->dpa_curlevel
, ==, BP_GET_LEVEL(bp
));
2939 SET_BOOKMARK(&zb
, dpa
->dpa_zb
.zb_objset
,
2940 dpa
->dpa_zb
.zb_object
, dpa
->dpa_curlevel
, nextblkid
);
2942 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
,
2943 bp
, dbuf_prefetch_indirect_done
, dpa
, dpa
->dpa_prio
,
2944 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
2948 arc_buf_destroy(abuf
, private);
2952 * Issue prefetch reads for the given block on the given level. If the indirect
2953 * blocks above that block are not in memory, we will read them in
2954 * asynchronously. As a result, this call never blocks waiting for a read to
2955 * complete. Note that the prefetch might fail if the dataset is encrypted and
2956 * the encryption key is unmapped before the IO completes.
2959 dbuf_prefetch(dnode_t
*dn
, int64_t level
, uint64_t blkid
, zio_priority_t prio
,
2963 int epbs
, nlevels
, curlevel
;
2966 ASSERT(blkid
!= DMU_BONUS_BLKID
);
2967 ASSERT(RW_LOCK_HELD(&dn
->dn_struct_rwlock
));
2969 if (blkid
> dn
->dn_maxblkid
)
2972 if (dnode_block_freed(dn
, blkid
))
2976 * This dnode hasn't been written to disk yet, so there's nothing to
2979 nlevels
= dn
->dn_phys
->dn_nlevels
;
2980 if (level
>= nlevels
|| dn
->dn_phys
->dn_nblkptr
== 0)
2983 epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
2984 if (dn
->dn_phys
->dn_maxblkid
< blkid
<< (epbs
* level
))
2987 dmu_buf_impl_t
*db
= dbuf_find(dn
->dn_objset
, dn
->dn_object
,
2990 mutex_exit(&db
->db_mtx
);
2992 * This dbuf already exists. It is either CACHED, or
2993 * (we assume) about to be read or filled.
2999 * Find the closest ancestor (indirect block) of the target block
3000 * that is present in the cache. In this indirect block, we will
3001 * find the bp that is at curlevel, curblkid.
3005 while (curlevel
< nlevels
- 1) {
3006 int parent_level
= curlevel
+ 1;
3007 uint64_t parent_blkid
= curblkid
>> epbs
;
3010 if (dbuf_hold_impl(dn
, parent_level
, parent_blkid
,
3011 FALSE
, TRUE
, FTAG
, &db
) == 0) {
3012 blkptr_t
*bpp
= db
->db_buf
->b_data
;
3013 bp
= bpp
[P2PHASE(curblkid
, 1 << epbs
)];
3014 dbuf_rele(db
, FTAG
);
3018 curlevel
= parent_level
;
3019 curblkid
= parent_blkid
;
3022 if (curlevel
== nlevels
- 1) {
3023 /* No cached indirect blocks found. */
3024 ASSERT3U(curblkid
, <, dn
->dn_phys
->dn_nblkptr
);
3025 bp
= dn
->dn_phys
->dn_blkptr
[curblkid
];
3027 if (BP_IS_HOLE(&bp
))
3030 ASSERT3U(curlevel
, ==, BP_GET_LEVEL(&bp
));
3032 zio_t
*pio
= zio_root(dmu_objset_spa(dn
->dn_objset
), NULL
, NULL
,
3035 dbuf_prefetch_arg_t
*dpa
= kmem_zalloc(sizeof (*dpa
), KM_SLEEP
);
3036 dsl_dataset_t
*ds
= dn
->dn_objset
->os_dsl_dataset
;
3037 SET_BOOKMARK(&dpa
->dpa_zb
, ds
!= NULL
? ds
->ds_object
: DMU_META_OBJSET
,
3038 dn
->dn_object
, level
, blkid
);
3039 dpa
->dpa_curlevel
= curlevel
;
3040 dpa
->dpa_prio
= prio
;
3041 dpa
->dpa_aflags
= aflags
;
3042 dpa
->dpa_spa
= dn
->dn_objset
->os_spa
;
3043 dpa
->dpa_dnode
= dn
;
3044 dpa
->dpa_epbs
= epbs
;
3047 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
3048 if (DNODE_LEVEL_IS_L2CACHEABLE(dn
, level
))
3049 dpa
->dpa_aflags
|= ARC_FLAG_L2CACHE
;
3052 * If we have the indirect just above us, no need to do the asynchronous
3053 * prefetch chain; we'll just run the last step ourselves. If we're at
3054 * a higher level, though, we want to issue the prefetches for all the
3055 * indirect blocks asynchronously, so we can go on with whatever we were
3058 if (curlevel
== level
) {
3059 ASSERT3U(curblkid
, ==, blkid
);
3060 dbuf_issue_final_prefetch(dpa
, &bp
);
3061 kmem_free(dpa
, sizeof (*dpa
));
3063 arc_flags_t iter_aflags
= ARC_FLAG_NOWAIT
;
3064 zbookmark_phys_t zb
;
3066 /* flag if L2ARC eligible, l2arc_noprefetch then decides */
3067 if (DNODE_LEVEL_IS_L2CACHEABLE(dn
, level
))
3068 iter_aflags
|= ARC_FLAG_L2CACHE
;
3070 SET_BOOKMARK(&zb
, ds
!= NULL
? ds
->ds_object
: DMU_META_OBJSET
,
3071 dn
->dn_object
, curlevel
, curblkid
);
3072 (void) arc_read(dpa
->dpa_zio
, dpa
->dpa_spa
,
3073 &bp
, dbuf_prefetch_indirect_done
, dpa
, prio
,
3074 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
,
3078 * We use pio here instead of dpa_zio since it's possible that
3079 * dpa may have already been freed.
3084 #define DBUF_HOLD_IMPL_MAX_DEPTH 20
3087 * Helper function for dbuf_hold_impl_arg() to copy a buffer. Handles
3088 * the case of encrypted, compressed and uncompressed buffers by
3089 * allocating the new buffer, respectively, with arc_alloc_raw_buf(),
3090 * arc_alloc_compressed_buf() or arc_alloc_buf().*
3092 * NOTE: Declared noinline to avoid stack bloat in dbuf_hold_impl_arg().
3094 noinline
static void
3095 dbuf_hold_copy(struct dbuf_hold_arg
*dh
)
3097 dnode_t
*dn
= dh
->dh_dn
;
3098 dmu_buf_impl_t
*db
= dh
->dh_db
;
3099 dbuf_dirty_record_t
*dr
= dh
->dh_dr
;
3100 arc_buf_t
*data
= dr
->dt
.dl
.dr_data
;
3102 enum zio_compress compress_type
= arc_get_compression(data
);
3104 if (arc_is_encrypted(data
)) {
3105 boolean_t byteorder
;
3106 uint8_t salt
[ZIO_DATA_SALT_LEN
];
3107 uint8_t iv
[ZIO_DATA_IV_LEN
];
3108 uint8_t mac
[ZIO_DATA_MAC_LEN
];
3110 arc_get_raw_params(data
, &byteorder
, salt
, iv
, mac
);
3111 dbuf_set_data(db
, arc_alloc_raw_buf(dn
->dn_objset
->os_spa
, db
,
3112 dmu_objset_id(dn
->dn_objset
), byteorder
, salt
, iv
, mac
,
3113 dn
->dn_type
, arc_buf_size(data
), arc_buf_lsize(data
),
3115 } else if (compress_type
!= ZIO_COMPRESS_OFF
) {
3116 dbuf_set_data(db
, arc_alloc_compressed_buf(
3117 dn
->dn_objset
->os_spa
, db
, arc_buf_size(data
),
3118 arc_buf_lsize(data
), compress_type
));
3120 dbuf_set_data(db
, arc_alloc_buf(dn
->dn_objset
->os_spa
, db
,
3121 DBUF_GET_BUFC_TYPE(db
), db
->db
.db_size
));
3124 bcopy(data
->b_data
, db
->db
.db_data
, arc_buf_size(data
));
3128 * Returns with db_holds incremented, and db_mtx not held.
3129 * Note: dn_struct_rwlock must be held.
3132 dbuf_hold_impl_arg(struct dbuf_hold_arg
*dh
)
3134 dh
->dh_parent
= NULL
;
3136 ASSERT(dh
->dh_blkid
!= DMU_BONUS_BLKID
);
3137 ASSERT(RW_LOCK_HELD(&dh
->dh_dn
->dn_struct_rwlock
));
3138 ASSERT3U(dh
->dh_dn
->dn_nlevels
, >, dh
->dh_level
);
3140 *(dh
->dh_dbp
) = NULL
;
3142 /* dbuf_find() returns with db_mtx held */
3143 dh
->dh_db
= dbuf_find(dh
->dh_dn
->dn_objset
, dh
->dh_dn
->dn_object
,
3144 dh
->dh_level
, dh
->dh_blkid
);
3146 if (dh
->dh_db
== NULL
) {
3149 if (dh
->dh_fail_uncached
)
3150 return (SET_ERROR(ENOENT
));
3152 ASSERT3P(dh
->dh_parent
, ==, NULL
);
3153 dh
->dh_err
= dbuf_findbp(dh
->dh_dn
, dh
->dh_level
, dh
->dh_blkid
,
3154 dh
->dh_fail_sparse
, &dh
->dh_parent
, &dh
->dh_bp
);
3155 if (dh
->dh_fail_sparse
) {
3156 if (dh
->dh_err
== 0 &&
3157 dh
->dh_bp
&& BP_IS_HOLE(dh
->dh_bp
))
3158 dh
->dh_err
= SET_ERROR(ENOENT
);
3161 dbuf_rele(dh
->dh_parent
, NULL
);
3162 return (dh
->dh_err
);
3165 if (dh
->dh_err
&& dh
->dh_err
!= ENOENT
)
3166 return (dh
->dh_err
);
3167 dh
->dh_db
= dbuf_create(dh
->dh_dn
, dh
->dh_level
, dh
->dh_blkid
,
3168 dh
->dh_parent
, dh
->dh_bp
);
3171 if (dh
->dh_fail_uncached
&& dh
->dh_db
->db_state
!= DB_CACHED
) {
3172 mutex_exit(&dh
->dh_db
->db_mtx
);
3173 return (SET_ERROR(ENOENT
));
3176 if (dh
->dh_db
->db_buf
!= NULL
) {
3177 arc_buf_access(dh
->dh_db
->db_buf
);
3178 ASSERT3P(dh
->dh_db
->db
.db_data
, ==, dh
->dh_db
->db_buf
->b_data
);
3181 ASSERT(dh
->dh_db
->db_buf
== NULL
|| arc_referenced(dh
->dh_db
->db_buf
));
3184 * If this buffer is currently syncing out, and we are are
3185 * still referencing it from db_data, we need to make a copy
3186 * of it in case we decide we want to dirty it again in this txg.
3188 if (dh
->dh_db
->db_level
== 0 &&
3189 dh
->dh_db
->db_blkid
!= DMU_BONUS_BLKID
&&
3190 dh
->dh_dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
3191 dh
->dh_db
->db_state
== DB_CACHED
&& dh
->dh_db
->db_data_pending
) {
3192 dh
->dh_dr
= dh
->dh_db
->db_data_pending
;
3193 if (dh
->dh_dr
->dt
.dl
.dr_data
== dh
->dh_db
->db_buf
)
3197 if (multilist_link_active(&dh
->dh_db
->db_cache_link
)) {
3198 ASSERT(zfs_refcount_is_zero(&dh
->dh_db
->db_holds
));
3199 ASSERT(dh
->dh_db
->db_caching_status
== DB_DBUF_CACHE
||
3200 dh
->dh_db
->db_caching_status
== DB_DBUF_METADATA_CACHE
);
3203 dbuf_caches
[dh
->dh_db
->db_caching_status
].cache
,
3205 (void) zfs_refcount_remove_many(
3206 &dbuf_caches
[dh
->dh_db
->db_caching_status
].size
,
3207 dh
->dh_db
->db
.db_size
, dh
->dh_db
);
3209 if (dh
->dh_db
->db_caching_status
== DB_DBUF_METADATA_CACHE
) {
3210 DBUF_STAT_BUMPDOWN(metadata_cache_count
);
3212 DBUF_STAT_BUMPDOWN(cache_levels
[dh
->dh_db
->db_level
]);
3213 DBUF_STAT_BUMPDOWN(cache_count
);
3214 DBUF_STAT_DECR(cache_levels_bytes
[dh
->dh_db
->db_level
],
3215 dh
->dh_db
->db
.db_size
);
3217 dh
->dh_db
->db_caching_status
= DB_NO_CACHE
;
3219 (void) zfs_refcount_add(&dh
->dh_db
->db_holds
, dh
->dh_tag
);
3220 DBUF_VERIFY(dh
->dh_db
);
3221 mutex_exit(&dh
->dh_db
->db_mtx
);
3223 /* NOTE: we can't rele the parent until after we drop the db_mtx */
3225 dbuf_rele(dh
->dh_parent
, NULL
);
3227 ASSERT3P(DB_DNODE(dh
->dh_db
), ==, dh
->dh_dn
);
3228 ASSERT3U(dh
->dh_db
->db_blkid
, ==, dh
->dh_blkid
);
3229 ASSERT3U(dh
->dh_db
->db_level
, ==, dh
->dh_level
);
3230 *(dh
->dh_dbp
) = dh
->dh_db
;
3236 * dbuf_hold_impl_arg() is called recursively, via dbuf_findbp(). There can
3237 * be as many recursive calls as there are levels of on-disk indirect blocks,
3238 * but typically only 0-2 recursive calls. To minimize the stack frame size,
3239 * the recursive function's arguments and "local variables" are allocated on
3240 * the heap as the dbuf_hold_arg_t.
3243 dbuf_hold_impl(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
3244 boolean_t fail_sparse
, boolean_t fail_uncached
,
3245 void *tag
, dmu_buf_impl_t
**dbp
)
3247 dbuf_hold_arg_t
*dh
= dbuf_hold_arg_create(dn
, level
, blkid
,
3248 fail_sparse
, fail_uncached
, tag
, dbp
);
3250 int error
= dbuf_hold_impl_arg(dh
);
3252 dbuf_hold_arg_destroy(dh
);
3257 static dbuf_hold_arg_t
*
3258 dbuf_hold_arg_create(dnode_t
*dn
, uint8_t level
, uint64_t blkid
,
3259 boolean_t fail_sparse
, boolean_t fail_uncached
,
3260 void *tag
, dmu_buf_impl_t
**dbp
)
3262 dbuf_hold_arg_t
*dh
= kmem_alloc(sizeof (*dh
), KM_SLEEP
);
3264 dh
->dh_level
= level
;
3265 dh
->dh_blkid
= blkid
;
3267 dh
->dh_fail_sparse
= fail_sparse
;
3268 dh
->dh_fail_uncached
= fail_uncached
;
3274 dh
->dh_parent
= NULL
;
3283 dbuf_hold_arg_destroy(dbuf_hold_arg_t
*dh
)
3285 kmem_free(dh
, sizeof (*dh
));
3289 dbuf_hold(dnode_t
*dn
, uint64_t blkid
, void *tag
)
3291 return (dbuf_hold_level(dn
, 0, blkid
, tag
));
3295 dbuf_hold_level(dnode_t
*dn
, int level
, uint64_t blkid
, void *tag
)
3298 int err
= dbuf_hold_impl(dn
, level
, blkid
, FALSE
, FALSE
, tag
, &db
);
3299 return (err
? NULL
: db
);
3303 dbuf_create_bonus(dnode_t
*dn
)
3305 ASSERT(RW_WRITE_HELD(&dn
->dn_struct_rwlock
));
3307 ASSERT(dn
->dn_bonus
== NULL
);
3308 dn
->dn_bonus
= dbuf_create(dn
, 0, DMU_BONUS_BLKID
, dn
->dn_dbuf
, NULL
);
3312 dbuf_spill_set_blksz(dmu_buf_t
*db_fake
, uint64_t blksz
, dmu_tx_t
*tx
)
3314 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3317 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
3318 return (SET_ERROR(ENOTSUP
));
3320 blksz
= SPA_MINBLOCKSIZE
;
3321 ASSERT3U(blksz
, <=, spa_maxblocksize(dmu_objset_spa(db
->db_objset
)));
3322 blksz
= P2ROUNDUP(blksz
, SPA_MINBLOCKSIZE
);
3326 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
3327 dbuf_new_size(db
, blksz
, tx
);
3328 rw_exit(&dn
->dn_struct_rwlock
);
3335 dbuf_rm_spill(dnode_t
*dn
, dmu_tx_t
*tx
)
3337 dbuf_free_range(dn
, DMU_SPILL_BLKID
, DMU_SPILL_BLKID
, tx
);
3340 #pragma weak dmu_buf_add_ref = dbuf_add_ref
3342 dbuf_add_ref(dmu_buf_impl_t
*db
, void *tag
)
3344 int64_t holds
= zfs_refcount_add(&db
->db_holds
, tag
);
3345 VERIFY3S(holds
, >, 1);
3348 #pragma weak dmu_buf_try_add_ref = dbuf_try_add_ref
3350 dbuf_try_add_ref(dmu_buf_t
*db_fake
, objset_t
*os
, uint64_t obj
, uint64_t blkid
,
3353 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3354 dmu_buf_impl_t
*found_db
;
3355 boolean_t result
= B_FALSE
;
3357 if (blkid
== DMU_BONUS_BLKID
)
3358 found_db
= dbuf_find_bonus(os
, obj
);
3360 found_db
= dbuf_find(os
, obj
, 0, blkid
);
3362 if (found_db
!= NULL
) {
3363 if (db
== found_db
&& dbuf_refcount(db
) > db
->db_dirtycnt
) {
3364 (void) zfs_refcount_add(&db
->db_holds
, tag
);
3367 mutex_exit(&found_db
->db_mtx
);
3373 * If you call dbuf_rele() you had better not be referencing the dnode handle
3374 * unless you have some other direct or indirect hold on the dnode. (An indirect
3375 * hold is a hold on one of the dnode's dbufs, including the bonus buffer.)
3376 * Without that, the dbuf_rele() could lead to a dnode_rele() followed by the
3377 * dnode's parent dbuf evicting its dnode handles.
3380 dbuf_rele(dmu_buf_impl_t
*db
, void *tag
)
3382 mutex_enter(&db
->db_mtx
);
3383 dbuf_rele_and_unlock(db
, tag
, B_FALSE
);
3387 dmu_buf_rele(dmu_buf_t
*db
, void *tag
)
3389 dbuf_rele((dmu_buf_impl_t
*)db
, tag
);
3393 * dbuf_rele() for an already-locked dbuf. This is necessary to allow
3394 * db_dirtycnt and db_holds to be updated atomically. The 'evicting'
3395 * argument should be set if we are already in the dbuf-evicting code
3396 * path, in which case we don't want to recursively evict. This allows us to
3397 * avoid deeply nested stacks that would have a call flow similar to this:
3399 * dbuf_rele()-->dbuf_rele_and_unlock()-->dbuf_evict_notify()
3402 * +-----dbuf_destroy()<--dbuf_evict_one()<--------+
3406 dbuf_rele_and_unlock(dmu_buf_impl_t
*db
, void *tag
, boolean_t evicting
)
3410 ASSERT(MUTEX_HELD(&db
->db_mtx
));
3414 * Remove the reference to the dbuf before removing its hold on the
3415 * dnode so we can guarantee in dnode_move() that a referenced bonus
3416 * buffer has a corresponding dnode hold.
3418 holds
= zfs_refcount_remove(&db
->db_holds
, tag
);
3422 * We can't freeze indirects if there is a possibility that they
3423 * may be modified in the current syncing context.
3425 if (db
->db_buf
!= NULL
&&
3426 holds
== (db
->db_level
== 0 ? db
->db_dirtycnt
: 0)) {
3427 arc_buf_freeze(db
->db_buf
);
3430 if (holds
== db
->db_dirtycnt
&&
3431 db
->db_level
== 0 && db
->db_user_immediate_evict
)
3432 dbuf_evict_user(db
);
3435 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
3437 boolean_t evict_dbuf
= db
->db_pending_evict
;
3440 * If the dnode moves here, we cannot cross this
3441 * barrier until the move completes.
3446 atomic_dec_32(&dn
->dn_dbufs_count
);
3449 * Decrementing the dbuf count means that the bonus
3450 * buffer's dnode hold is no longer discounted in
3451 * dnode_move(). The dnode cannot move until after
3452 * the dnode_rele() below.
3457 * Do not reference db after its lock is dropped.
3458 * Another thread may evict it.
3460 mutex_exit(&db
->db_mtx
);
3463 dnode_evict_bonus(dn
);
3466 } else if (db
->db_buf
== NULL
) {
3468 * This is a special case: we never associated this
3469 * dbuf with any data allocated from the ARC.
3471 ASSERT(db
->db_state
== DB_UNCACHED
||
3472 db
->db_state
== DB_NOFILL
);
3474 } else if (arc_released(db
->db_buf
)) {
3476 * This dbuf has anonymous data associated with it.
3480 boolean_t do_arc_evict
= B_FALSE
;
3482 spa_t
*spa
= dmu_objset_spa(db
->db_objset
);
3484 if (!DBUF_IS_CACHEABLE(db
) &&
3485 db
->db_blkptr
!= NULL
&&
3486 !BP_IS_HOLE(db
->db_blkptr
) &&
3487 !BP_IS_EMBEDDED(db
->db_blkptr
)) {
3488 do_arc_evict
= B_TRUE
;
3489 bp
= *db
->db_blkptr
;
3492 if (!DBUF_IS_CACHEABLE(db
) ||
3493 db
->db_pending_evict
) {
3495 } else if (!multilist_link_active(&db
->db_cache_link
)) {
3496 ASSERT3U(db
->db_caching_status
, ==,
3499 dbuf_cached_state_t dcs
=
3500 dbuf_include_in_metadata_cache(db
) ?
3501 DB_DBUF_METADATA_CACHE
: DB_DBUF_CACHE
;
3502 db
->db_caching_status
= dcs
;
3504 multilist_insert(dbuf_caches
[dcs
].cache
, db
);
3505 (void) zfs_refcount_add_many(
3506 &dbuf_caches
[dcs
].size
,
3507 db
->db
.db_size
, db
);
3509 if (dcs
== DB_DBUF_METADATA_CACHE
) {
3510 DBUF_STAT_BUMP(metadata_cache_count
);
3512 metadata_cache_size_bytes_max
,
3514 &dbuf_caches
[dcs
].size
));
3517 cache_levels
[db
->db_level
]);
3518 DBUF_STAT_BUMP(cache_count
);
3520 cache_levels_bytes
[db
->db_level
],
3522 DBUF_STAT_MAX(cache_size_bytes_max
,
3524 &dbuf_caches
[dcs
].size
));
3526 mutex_exit(&db
->db_mtx
);
3528 if (db
->db_caching_status
== DB_DBUF_CACHE
&&
3530 dbuf_evict_notify();
3535 arc_freed(spa
, &bp
);
3538 mutex_exit(&db
->db_mtx
);
3543 #pragma weak dmu_buf_refcount = dbuf_refcount
3545 dbuf_refcount(dmu_buf_impl_t
*db
)
3547 return (zfs_refcount_count(&db
->db_holds
));
3551 dmu_buf_user_refcount(dmu_buf_t
*db_fake
)
3554 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3556 mutex_enter(&db
->db_mtx
);
3557 ASSERT3U(zfs_refcount_count(&db
->db_holds
), >=, db
->db_dirtycnt
);
3558 holds
= zfs_refcount_count(&db
->db_holds
) - db
->db_dirtycnt
;
3559 mutex_exit(&db
->db_mtx
);
3565 dmu_buf_replace_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*old_user
,
3566 dmu_buf_user_t
*new_user
)
3568 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3570 mutex_enter(&db
->db_mtx
);
3571 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
3572 if (db
->db_user
== old_user
)
3573 db
->db_user
= new_user
;
3575 old_user
= db
->db_user
;
3576 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
3577 mutex_exit(&db
->db_mtx
);
3583 dmu_buf_set_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
3585 return (dmu_buf_replace_user(db_fake
, NULL
, user
));
3589 dmu_buf_set_user_ie(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
3591 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3593 db
->db_user_immediate_evict
= TRUE
;
3594 return (dmu_buf_set_user(db_fake
, user
));
3598 dmu_buf_remove_user(dmu_buf_t
*db_fake
, dmu_buf_user_t
*user
)
3600 return (dmu_buf_replace_user(db_fake
, user
, NULL
));
3604 dmu_buf_get_user(dmu_buf_t
*db_fake
)
3606 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
3608 dbuf_verify_user(db
, DBVU_NOT_EVICTING
);
3609 return (db
->db_user
);
3613 dmu_buf_user_evict_wait()
3615 taskq_wait(dbu_evict_taskq
);
3619 dmu_buf_get_blkptr(dmu_buf_t
*db
)
3621 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3622 return (dbi
->db_blkptr
);
3626 dmu_buf_get_objset(dmu_buf_t
*db
)
3628 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3629 return (dbi
->db_objset
);
3633 dmu_buf_dnode_enter(dmu_buf_t
*db
)
3635 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3636 DB_DNODE_ENTER(dbi
);
3637 return (DB_DNODE(dbi
));
3641 dmu_buf_dnode_exit(dmu_buf_t
*db
)
3643 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
3648 dbuf_check_blkptr(dnode_t
*dn
, dmu_buf_impl_t
*db
)
3650 /* ASSERT(dmu_tx_is_syncing(tx) */
3651 ASSERT(MUTEX_HELD(&db
->db_mtx
));
3653 if (db
->db_blkptr
!= NULL
)
3656 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3657 db
->db_blkptr
= DN_SPILL_BLKPTR(dn
->dn_phys
);
3658 BP_ZERO(db
->db_blkptr
);
3661 if (db
->db_level
== dn
->dn_phys
->dn_nlevels
-1) {
3663 * This buffer was allocated at a time when there was
3664 * no available blkptrs from the dnode, or it was
3665 * inappropriate to hook it in (i.e., nlevels mis-match).
3667 ASSERT(db
->db_blkid
< dn
->dn_phys
->dn_nblkptr
);
3668 ASSERT(db
->db_parent
== NULL
);
3669 db
->db_parent
= dn
->dn_dbuf
;
3670 db
->db_blkptr
= &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
];
3673 dmu_buf_impl_t
*parent
= db
->db_parent
;
3674 int epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
3676 ASSERT(dn
->dn_phys
->dn_nlevels
> 1);
3677 if (parent
== NULL
) {
3678 mutex_exit(&db
->db_mtx
);
3679 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
3680 parent
= dbuf_hold_level(dn
, db
->db_level
+ 1,
3681 db
->db_blkid
>> epbs
, db
);
3682 rw_exit(&dn
->dn_struct_rwlock
);
3683 mutex_enter(&db
->db_mtx
);
3684 db
->db_parent
= parent
;
3686 db
->db_blkptr
= (blkptr_t
*)parent
->db
.db_data
+
3687 (db
->db_blkid
& ((1ULL << epbs
) - 1));
3693 * When syncing out a blocks of dnodes, adjust the block to deal with
3694 * encryption. Normally, we make sure the block is decrypted before writing
3695 * it. If we have crypt params, then we are writing a raw (encrypted) block,
3696 * from a raw receive. In this case, set the ARC buf's crypt params so
3697 * that the BP will be filled with the correct byteorder, salt, iv, and mac.
3700 dbuf_prepare_encrypted_dnode_leaf(dbuf_dirty_record_t
*dr
)
3703 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3705 ASSERT(MUTEX_HELD(&db
->db_mtx
));
3706 ASSERT3U(db
->db
.db_object
, ==, DMU_META_DNODE_OBJECT
);
3707 ASSERT3U(db
->db_level
, ==, 0);
3709 if (!db
->db_objset
->os_raw_receive
&& arc_is_encrypted(db
->db_buf
)) {
3710 zbookmark_phys_t zb
;
3713 * Unfortunately, there is currently no mechanism for
3714 * syncing context to handle decryption errors. An error
3715 * here is only possible if an attacker maliciously
3716 * changed a dnode block and updated the associated
3717 * checksums going up the block tree.
3719 SET_BOOKMARK(&zb
, dmu_objset_id(db
->db_objset
),
3720 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
3721 err
= arc_untransform(db
->db_buf
, db
->db_objset
->os_spa
,
3724 panic("Invalid dnode block MAC");
3725 } else if (dr
->dt
.dl
.dr_has_raw_params
) {
3726 (void) arc_release(dr
->dt
.dl
.dr_data
, db
);
3727 arc_convert_to_raw(dr
->dt
.dl
.dr_data
,
3728 dmu_objset_id(db
->db_objset
),
3729 dr
->dt
.dl
.dr_byteorder
, DMU_OT_DNODE
,
3730 dr
->dt
.dl
.dr_salt
, dr
->dt
.dl
.dr_iv
, dr
->dt
.dl
.dr_mac
);
3735 * dbuf_sync_indirect() is called recursively from dbuf_sync_list() so it
3736 * is critical the we not allow the compiler to inline this function in to
3737 * dbuf_sync_list() thereby drastically bloating the stack usage.
3739 noinline
static void
3740 dbuf_sync_indirect(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
3742 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3746 ASSERT(dmu_tx_is_syncing(tx
));
3748 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
3750 mutex_enter(&db
->db_mtx
);
3752 ASSERT(db
->db_level
> 0);
3755 /* Read the block if it hasn't been read yet. */
3756 if (db
->db_buf
== NULL
) {
3757 mutex_exit(&db
->db_mtx
);
3758 (void) dbuf_read(db
, NULL
, DB_RF_MUST_SUCCEED
);
3759 mutex_enter(&db
->db_mtx
);
3761 ASSERT3U(db
->db_state
, ==, DB_CACHED
);
3762 ASSERT(db
->db_buf
!= NULL
);
3766 /* Indirect block size must match what the dnode thinks it is. */
3767 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
3768 dbuf_check_blkptr(dn
, db
);
3771 /* Provide the pending dirty record to child dbufs */
3772 db
->db_data_pending
= dr
;
3774 mutex_exit(&db
->db_mtx
);
3776 dbuf_write(dr
, db
->db_buf
, tx
);
3779 mutex_enter(&dr
->dt
.di
.dr_mtx
);
3780 dbuf_sync_list(&dr
->dt
.di
.dr_children
, db
->db_level
- 1, tx
);
3781 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
3782 mutex_exit(&dr
->dt
.di
.dr_mtx
);
3787 * dbuf_sync_leaf() is called recursively from dbuf_sync_list() so it is
3788 * critical the we not allow the compiler to inline this function in to
3789 * dbuf_sync_list() thereby drastically bloating the stack usage.
3791 noinline
static void
3792 dbuf_sync_leaf(dbuf_dirty_record_t
*dr
, dmu_tx_t
*tx
)
3794 arc_buf_t
**datap
= &dr
->dt
.dl
.dr_data
;
3795 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
3798 uint64_t txg
= tx
->tx_txg
;
3800 ASSERT(dmu_tx_is_syncing(tx
));
3802 dprintf_dbuf_bp(db
, db
->db_blkptr
, "blkptr=%p", db
->db_blkptr
);
3804 mutex_enter(&db
->db_mtx
);
3806 * To be synced, we must be dirtied. But we
3807 * might have been freed after the dirty.
3809 if (db
->db_state
== DB_UNCACHED
) {
3810 /* This buffer has been freed since it was dirtied */
3811 ASSERT(db
->db
.db_data
== NULL
);
3812 } else if (db
->db_state
== DB_FILL
) {
3813 /* This buffer was freed and is now being re-filled */
3814 ASSERT(db
->db
.db_data
!= dr
->dt
.dl
.dr_data
);
3816 ASSERT(db
->db_state
== DB_CACHED
|| db
->db_state
== DB_NOFILL
);
3823 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
3824 mutex_enter(&dn
->dn_mtx
);
3825 if (!(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
)) {
3827 * In the previous transaction group, the bonus buffer
3828 * was entirely used to store the attributes for the
3829 * dnode which overrode the dn_spill field. However,
3830 * when adding more attributes to the file a spill
3831 * block was required to hold the extra attributes.
3833 * Make sure to clear the garbage left in the dn_spill
3834 * field from the previous attributes in the bonus
3835 * buffer. Otherwise, after writing out the spill
3836 * block to the new allocated dva, it will free
3837 * the old block pointed to by the invalid dn_spill.
3839 db
->db_blkptr
= NULL
;
3841 dn
->dn_phys
->dn_flags
|= DNODE_FLAG_SPILL_BLKPTR
;
3842 mutex_exit(&dn
->dn_mtx
);
3846 * If this is a bonus buffer, simply copy the bonus data into the
3847 * dnode. It will be written out when the dnode is synced (and it
3848 * will be synced, since it must have been dirty for dbuf_sync to
3851 if (db
->db_blkid
== DMU_BONUS_BLKID
) {
3852 dbuf_dirty_record_t
**drp
;
3854 ASSERT(*datap
!= NULL
);
3855 ASSERT0(db
->db_level
);
3856 ASSERT3U(DN_MAX_BONUS_LEN(dn
->dn_phys
), <=,
3857 DN_SLOTS_TO_BONUSLEN(dn
->dn_phys
->dn_extra_slots
+ 1));
3858 bcopy(*datap
, DN_BONUS(dn
->dn_phys
),
3859 DN_MAX_BONUS_LEN(dn
->dn_phys
));
3862 if (*datap
!= db
->db
.db_data
) {
3863 int slots
= DB_DNODE(db
)->dn_num_slots
;
3864 int bonuslen
= DN_SLOTS_TO_BONUSLEN(slots
);
3865 kmem_free(*datap
, bonuslen
);
3866 arc_space_return(bonuslen
, ARC_SPACE_BONUS
);
3868 db
->db_data_pending
= NULL
;
3869 drp
= &db
->db_last_dirty
;
3871 drp
= &(*drp
)->dr_next
;
3872 ASSERT(dr
->dr_next
== NULL
);
3873 ASSERT(dr
->dr_dbuf
== db
);
3875 if (dr
->dr_dbuf
->db_level
!= 0) {
3876 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
3877 list_destroy(&dr
->dt
.di
.dr_children
);
3879 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
3880 ASSERT(db
->db_dirtycnt
> 0);
3881 db
->db_dirtycnt
-= 1;
3882 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)txg
, B_FALSE
);
3889 * This function may have dropped the db_mtx lock allowing a dmu_sync
3890 * operation to sneak in. As a result, we need to ensure that we
3891 * don't check the dr_override_state until we have returned from
3892 * dbuf_check_blkptr.
3894 dbuf_check_blkptr(dn
, db
);
3897 * If this buffer is in the middle of an immediate write,
3898 * wait for the synchronous IO to complete.
3900 while (dr
->dt
.dl
.dr_override_state
== DR_IN_DMU_SYNC
) {
3901 ASSERT(dn
->dn_object
!= DMU_META_DNODE_OBJECT
);
3902 cv_wait(&db
->db_changed
, &db
->db_mtx
);
3903 ASSERT(dr
->dt
.dl
.dr_override_state
!= DR_NOT_OVERRIDDEN
);
3907 * If this is a dnode block, ensure it is appropriately encrypted
3908 * or decrypted, depending on what we are writing to it this txg.
3910 if (os
->os_encrypted
&& dn
->dn_object
== DMU_META_DNODE_OBJECT
)
3911 dbuf_prepare_encrypted_dnode_leaf(dr
);
3913 if (db
->db_state
!= DB_NOFILL
&&
3914 dn
->dn_object
!= DMU_META_DNODE_OBJECT
&&
3915 zfs_refcount_count(&db
->db_holds
) > 1 &&
3916 dr
->dt
.dl
.dr_override_state
!= DR_OVERRIDDEN
&&
3917 *datap
== db
->db_buf
) {
3919 * If this buffer is currently "in use" (i.e., there
3920 * are active holds and db_data still references it),
3921 * then make a copy before we start the write so that
3922 * any modifications from the open txg will not leak
3925 * NOTE: this copy does not need to be made for
3926 * objects only modified in the syncing context (e.g.
3927 * DNONE_DNODE blocks).
3929 int psize
= arc_buf_size(*datap
);
3930 int lsize
= arc_buf_lsize(*datap
);
3931 arc_buf_contents_t type
= DBUF_GET_BUFC_TYPE(db
);
3932 enum zio_compress compress_type
= arc_get_compression(*datap
);
3934 if (arc_is_encrypted(*datap
)) {
3935 boolean_t byteorder
;
3936 uint8_t salt
[ZIO_DATA_SALT_LEN
];
3937 uint8_t iv
[ZIO_DATA_IV_LEN
];
3938 uint8_t mac
[ZIO_DATA_MAC_LEN
];
3940 arc_get_raw_params(*datap
, &byteorder
, salt
, iv
, mac
);
3941 *datap
= arc_alloc_raw_buf(os
->os_spa
, db
,
3942 dmu_objset_id(os
), byteorder
, salt
, iv
, mac
,
3943 dn
->dn_type
, psize
, lsize
, compress_type
);
3944 } else if (compress_type
!= ZIO_COMPRESS_OFF
) {
3945 ASSERT3U(type
, ==, ARC_BUFC_DATA
);
3946 *datap
= arc_alloc_compressed_buf(os
->os_spa
, db
,
3947 psize
, lsize
, compress_type
);
3949 *datap
= arc_alloc_buf(os
->os_spa
, db
, type
, psize
);
3951 bcopy(db
->db
.db_data
, (*datap
)->b_data
, psize
);
3953 db
->db_data_pending
= dr
;
3955 mutex_exit(&db
->db_mtx
);
3957 dbuf_write(dr
, *datap
, tx
);
3959 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
3960 if (dn
->dn_object
== DMU_META_DNODE_OBJECT
) {
3961 list_insert_tail(&dn
->dn_dirty_records
[txg
&TXG_MASK
], dr
);
3965 * Although zio_nowait() does not "wait for an IO", it does
3966 * initiate the IO. If this is an empty write it seems plausible
3967 * that the IO could actually be completed before the nowait
3968 * returns. We need to DB_DNODE_EXIT() first in case
3969 * zio_nowait() invalidates the dbuf.
3972 zio_nowait(dr
->dr_zio
);
3977 dbuf_sync_list(list_t
*list
, int level
, dmu_tx_t
*tx
)
3979 dbuf_dirty_record_t
*dr
;
3981 while ((dr
= list_head(list
))) {
3982 if (dr
->dr_zio
!= NULL
) {
3984 * If we find an already initialized zio then we
3985 * are processing the meta-dnode, and we have finished.
3986 * The dbufs for all dnodes are put back on the list
3987 * during processing, so that we can zio_wait()
3988 * these IOs after initiating all child IOs.
3990 ASSERT3U(dr
->dr_dbuf
->db
.db_object
, ==,
3991 DMU_META_DNODE_OBJECT
);
3994 if (dr
->dr_dbuf
->db_blkid
!= DMU_BONUS_BLKID
&&
3995 dr
->dr_dbuf
->db_blkid
!= DMU_SPILL_BLKID
) {
3996 VERIFY3U(dr
->dr_dbuf
->db_level
, ==, level
);
3998 list_remove(list
, dr
);
3999 if (dr
->dr_dbuf
->db_level
> 0)
4000 dbuf_sync_indirect(dr
, tx
);
4002 dbuf_sync_leaf(dr
, tx
);
4008 dbuf_write_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
4010 dmu_buf_impl_t
*db
= vdb
;
4012 blkptr_t
*bp
= zio
->io_bp
;
4013 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
4014 spa_t
*spa
= zio
->io_spa
;
4019 ASSERT3P(db
->db_blkptr
, !=, NULL
);
4020 ASSERT3P(&db
->db_data_pending
->dr_bp_copy
, ==, bp
);
4024 delta
= bp_get_dsize_sync(spa
, bp
) - bp_get_dsize_sync(spa
, bp_orig
);
4025 dnode_diduse_space(dn
, delta
- zio
->io_prev_space_delta
);
4026 zio
->io_prev_space_delta
= delta
;
4028 if (bp
->blk_birth
!= 0) {
4029 ASSERT((db
->db_blkid
!= DMU_SPILL_BLKID
&&
4030 BP_GET_TYPE(bp
) == dn
->dn_type
) ||
4031 (db
->db_blkid
== DMU_SPILL_BLKID
&&
4032 BP_GET_TYPE(bp
) == dn
->dn_bonustype
) ||
4033 BP_IS_EMBEDDED(bp
));
4034 ASSERT(BP_GET_LEVEL(bp
) == db
->db_level
);
4037 mutex_enter(&db
->db_mtx
);
4040 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
4041 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
4042 ASSERT(!(BP_IS_HOLE(bp
)) &&
4043 db
->db_blkptr
== DN_SPILL_BLKPTR(dn
->dn_phys
));
4047 if (db
->db_level
== 0) {
4048 mutex_enter(&dn
->dn_mtx
);
4049 if (db
->db_blkid
> dn
->dn_phys
->dn_maxblkid
&&
4050 db
->db_blkid
!= DMU_SPILL_BLKID
) {
4051 ASSERT0(db
->db_objset
->os_raw_receive
);
4052 dn
->dn_phys
->dn_maxblkid
= db
->db_blkid
;
4054 mutex_exit(&dn
->dn_mtx
);
4056 if (dn
->dn_type
== DMU_OT_DNODE
) {
4058 while (i
< db
->db
.db_size
) {
4060 (void *)(((char *)db
->db
.db_data
) + i
);
4062 i
+= DNODE_MIN_SIZE
;
4063 if (dnp
->dn_type
!= DMU_OT_NONE
) {
4065 i
+= dnp
->dn_extra_slots
*
4070 if (BP_IS_HOLE(bp
)) {
4077 blkptr_t
*ibp
= db
->db
.db_data
;
4078 ASSERT3U(db
->db
.db_size
, ==, 1<<dn
->dn_phys
->dn_indblkshift
);
4079 for (i
= db
->db
.db_size
>> SPA_BLKPTRSHIFT
; i
> 0; i
--, ibp
++) {
4080 if (BP_IS_HOLE(ibp
))
4082 fill
+= BP_GET_FILL(ibp
);
4087 if (!BP_IS_EMBEDDED(bp
))
4088 BP_SET_FILL(bp
, fill
);
4090 mutex_exit(&db
->db_mtx
);
4092 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
4093 *db
->db_blkptr
= *bp
;
4094 rw_exit(&dn
->dn_struct_rwlock
);
4099 * This function gets called just prior to running through the compression
4100 * stage of the zio pipeline. If we're an indirect block comprised of only
4101 * holes, then we want this indirect to be compressed away to a hole. In
4102 * order to do that we must zero out any information about the holes that
4103 * this indirect points to prior to before we try to compress it.
4106 dbuf_write_children_ready(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
4108 dmu_buf_impl_t
*db
= vdb
;
4111 unsigned int epbs
, i
;
4113 ASSERT3U(db
->db_level
, >, 0);
4116 epbs
= dn
->dn_phys
->dn_indblkshift
- SPA_BLKPTRSHIFT
;
4117 ASSERT3U(epbs
, <, 31);
4119 /* Determine if all our children are holes */
4120 for (i
= 0, bp
= db
->db
.db_data
; i
< 1ULL << epbs
; i
++, bp
++) {
4121 if (!BP_IS_HOLE(bp
))
4126 * If all the children are holes, then zero them all out so that
4127 * we may get compressed away.
4129 if (i
== 1ULL << epbs
) {
4131 * We only found holes. Grab the rwlock to prevent
4132 * anybody from reading the blocks we're about to
4135 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
4136 bzero(db
->db
.db_data
, db
->db
.db_size
);
4137 rw_exit(&dn
->dn_struct_rwlock
);
4143 * The SPA will call this callback several times for each zio - once
4144 * for every physical child i/o (zio->io_phys_children times). This
4145 * allows the DMU to monitor the progress of each logical i/o. For example,
4146 * there may be 2 copies of an indirect block, or many fragments of a RAID-Z
4147 * block. There may be a long delay before all copies/fragments are completed,
4148 * so this callback allows us to retire dirty space gradually, as the physical
4153 dbuf_write_physdone(zio_t
*zio
, arc_buf_t
*buf
, void *arg
)
4155 dmu_buf_impl_t
*db
= arg
;
4156 objset_t
*os
= db
->db_objset
;
4157 dsl_pool_t
*dp
= dmu_objset_pool(os
);
4158 dbuf_dirty_record_t
*dr
;
4161 dr
= db
->db_data_pending
;
4162 ASSERT3U(dr
->dr_txg
, ==, zio
->io_txg
);
4165 * The callback will be called io_phys_children times. Retire one
4166 * portion of our dirty space each time we are called. Any rounding
4167 * error will be cleaned up by dsl_pool_sync()'s call to
4168 * dsl_pool_undirty_space().
4170 delta
= dr
->dr_accounted
/ zio
->io_phys_children
;
4171 dsl_pool_undirty_space(dp
, delta
, zio
->io_txg
);
4176 dbuf_write_done(zio_t
*zio
, arc_buf_t
*buf
, void *vdb
)
4178 dmu_buf_impl_t
*db
= vdb
;
4179 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
4180 blkptr_t
*bp
= db
->db_blkptr
;
4181 objset_t
*os
= db
->db_objset
;
4182 dmu_tx_t
*tx
= os
->os_synctx
;
4183 dbuf_dirty_record_t
**drp
, *dr
;
4185 ASSERT0(zio
->io_error
);
4186 ASSERT(db
->db_blkptr
== bp
);
4189 * For nopwrites and rewrites we ensure that the bp matches our
4190 * original and bypass all the accounting.
4192 if (zio
->io_flags
& (ZIO_FLAG_IO_REWRITE
| ZIO_FLAG_NOPWRITE
)) {
4193 ASSERT(BP_EQUAL(bp
, bp_orig
));
4195 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
4196 (void) dsl_dataset_block_kill(ds
, bp_orig
, tx
, B_TRUE
);
4197 dsl_dataset_block_born(ds
, bp
, tx
);
4200 mutex_enter(&db
->db_mtx
);
4204 drp
= &db
->db_last_dirty
;
4205 while ((dr
= *drp
) != db
->db_data_pending
)
4207 ASSERT(!list_link_active(&dr
->dr_dirty_node
));
4208 ASSERT(dr
->dr_dbuf
== db
);
4209 ASSERT(dr
->dr_next
== NULL
);
4213 if (db
->db_blkid
== DMU_SPILL_BLKID
) {
4218 ASSERT(dn
->dn_phys
->dn_flags
& DNODE_FLAG_SPILL_BLKPTR
);
4219 ASSERT(!(BP_IS_HOLE(db
->db_blkptr
)) &&
4220 db
->db_blkptr
== DN_SPILL_BLKPTR(dn
->dn_phys
));
4225 if (db
->db_level
== 0) {
4226 ASSERT(db
->db_blkid
!= DMU_BONUS_BLKID
);
4227 ASSERT(dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
);
4228 if (db
->db_state
!= DB_NOFILL
) {
4229 if (dr
->dt
.dl
.dr_data
!= db
->db_buf
)
4230 arc_buf_destroy(dr
->dt
.dl
.dr_data
, db
);
4237 ASSERT(list_head(&dr
->dt
.di
.dr_children
) == NULL
);
4238 ASSERT3U(db
->db
.db_size
, ==, 1 << dn
->dn_phys
->dn_indblkshift
);
4239 if (!BP_IS_HOLE(db
->db_blkptr
)) {
4240 ASSERTV(int epbs
= dn
->dn_phys
->dn_indblkshift
-
4242 ASSERT3U(db
->db_blkid
, <=,
4243 dn
->dn_phys
->dn_maxblkid
>> (db
->db_level
* epbs
));
4244 ASSERT3U(BP_GET_LSIZE(db
->db_blkptr
), ==,
4248 mutex_destroy(&dr
->dt
.di
.dr_mtx
);
4249 list_destroy(&dr
->dt
.di
.dr_children
);
4251 kmem_free(dr
, sizeof (dbuf_dirty_record_t
));
4253 cv_broadcast(&db
->db_changed
);
4254 ASSERT(db
->db_dirtycnt
> 0);
4255 db
->db_dirtycnt
-= 1;
4256 db
->db_data_pending
= NULL
;
4257 dbuf_rele_and_unlock(db
, (void *)(uintptr_t)tx
->tx_txg
, B_FALSE
);
4261 dbuf_write_nofill_ready(zio_t
*zio
)
4263 dbuf_write_ready(zio
, NULL
, zio
->io_private
);
4267 dbuf_write_nofill_done(zio_t
*zio
)
4269 dbuf_write_done(zio
, NULL
, zio
->io_private
);
4273 dbuf_write_override_ready(zio_t
*zio
)
4275 dbuf_dirty_record_t
*dr
= zio
->io_private
;
4276 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
4278 dbuf_write_ready(zio
, NULL
, db
);
4282 dbuf_write_override_done(zio_t
*zio
)
4284 dbuf_dirty_record_t
*dr
= zio
->io_private
;
4285 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
4286 blkptr_t
*obp
= &dr
->dt
.dl
.dr_overridden_by
;
4288 mutex_enter(&db
->db_mtx
);
4289 if (!BP_EQUAL(zio
->io_bp
, obp
)) {
4290 if (!BP_IS_HOLE(obp
))
4291 dsl_free(spa_get_dsl(zio
->io_spa
), zio
->io_txg
, obp
);
4292 arc_release(dr
->dt
.dl
.dr_data
, db
);
4294 mutex_exit(&db
->db_mtx
);
4296 dbuf_write_done(zio
, NULL
, db
);
4298 if (zio
->io_abd
!= NULL
)
4299 abd_put(zio
->io_abd
);
4302 typedef struct dbuf_remap_impl_callback_arg
{
4304 uint64_t drica_blk_birth
;
4306 } dbuf_remap_impl_callback_arg_t
;
4309 dbuf_remap_impl_callback(uint64_t vdev
, uint64_t offset
, uint64_t size
,
4312 dbuf_remap_impl_callback_arg_t
*drica
= arg
;
4313 objset_t
*os
= drica
->drica_os
;
4314 spa_t
*spa
= dmu_objset_spa(os
);
4315 dmu_tx_t
*tx
= drica
->drica_tx
;
4317 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa
)));
4319 if (os
== spa_meta_objset(spa
)) {
4320 spa_vdev_indirect_mark_obsolete(spa
, vdev
, offset
, size
, tx
);
4322 dsl_dataset_block_remapped(dmu_objset_ds(os
), vdev
, offset
,
4323 size
, drica
->drica_blk_birth
, tx
);
4328 dbuf_remap_impl(dnode_t
*dn
, blkptr_t
*bp
, dmu_tx_t
*tx
)
4330 blkptr_t bp_copy
= *bp
;
4331 spa_t
*spa
= dmu_objset_spa(dn
->dn_objset
);
4332 dbuf_remap_impl_callback_arg_t drica
;
4334 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa
)));
4336 drica
.drica_os
= dn
->dn_objset
;
4337 drica
.drica_blk_birth
= bp
->blk_birth
;
4338 drica
.drica_tx
= tx
;
4339 if (spa_remap_blkptr(spa
, &bp_copy
, dbuf_remap_impl_callback
,
4342 * The struct_rwlock prevents dbuf_read_impl() from
4343 * dereferencing the BP while we are changing it. To
4344 * avoid lock contention, only grab it when we are actually
4347 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
4349 rw_exit(&dn
->dn_struct_rwlock
);
4354 * Returns true if a dbuf_remap would modify the dbuf. We do this by attempting
4355 * to remap a copy of every bp in the dbuf.
4358 dbuf_can_remap(const dmu_buf_impl_t
*db
)
4360 spa_t
*spa
= dmu_objset_spa(db
->db_objset
);
4361 blkptr_t
*bp
= db
->db
.db_data
;
4362 boolean_t ret
= B_FALSE
;
4364 ASSERT3U(db
->db_level
, >, 0);
4365 ASSERT3S(db
->db_state
, ==, DB_CACHED
);
4367 ASSERT(spa_feature_is_active(spa
, SPA_FEATURE_DEVICE_REMOVAL
));
4369 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
4370 for (int i
= 0; i
< db
->db
.db_size
>> SPA_BLKPTRSHIFT
; i
++) {
4371 blkptr_t bp_copy
= bp
[i
];
4372 if (spa_remap_blkptr(spa
, &bp_copy
, NULL
, NULL
)) {
4377 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
4383 dnode_needs_remap(const dnode_t
*dn
)
4385 spa_t
*spa
= dmu_objset_spa(dn
->dn_objset
);
4386 boolean_t ret
= B_FALSE
;
4388 if (dn
->dn_phys
->dn_nlevels
== 0) {
4392 ASSERT(spa_feature_is_active(spa
, SPA_FEATURE_DEVICE_REMOVAL
));
4394 spa_config_enter(spa
, SCL_VDEV
, FTAG
, RW_READER
);
4395 for (int j
= 0; j
< dn
->dn_phys
->dn_nblkptr
; j
++) {
4396 blkptr_t bp_copy
= dn
->dn_phys
->dn_blkptr
[j
];
4397 if (spa_remap_blkptr(spa
, &bp_copy
, NULL
, NULL
)) {
4402 spa_config_exit(spa
, SCL_VDEV
, FTAG
);
4408 * Remap any existing BP's to concrete vdevs, if possible.
4411 dbuf_remap(dnode_t
*dn
, dmu_buf_impl_t
*db
, dmu_tx_t
*tx
)
4413 spa_t
*spa
= dmu_objset_spa(db
->db_objset
);
4414 ASSERT(dsl_pool_sync_context(spa_get_dsl(spa
)));
4416 if (!spa_feature_is_active(spa
, SPA_FEATURE_DEVICE_REMOVAL
))
4419 if (db
->db_level
> 0) {
4420 blkptr_t
*bp
= db
->db
.db_data
;
4421 for (int i
= 0; i
< db
->db
.db_size
>> SPA_BLKPTRSHIFT
; i
++) {
4422 dbuf_remap_impl(dn
, &bp
[i
], tx
);
4424 } else if (db
->db
.db_object
== DMU_META_DNODE_OBJECT
) {
4425 dnode_phys_t
*dnp
= db
->db
.db_data
;
4426 ASSERT3U(db
->db_dnode_handle
->dnh_dnode
->dn_type
, ==,
4428 for (int i
= 0; i
< db
->db
.db_size
>> DNODE_SHIFT
;
4429 i
+= dnp
[i
].dn_extra_slots
+ 1) {
4430 for (int j
= 0; j
< dnp
[i
].dn_nblkptr
; j
++) {
4431 dbuf_remap_impl(dn
, &dnp
[i
].dn_blkptr
[j
], tx
);
4438 /* Issue I/O to commit a dirty buffer to disk. */
4440 dbuf_write(dbuf_dirty_record_t
*dr
, arc_buf_t
*data
, dmu_tx_t
*tx
)
4442 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
4445 dmu_buf_impl_t
*parent
= db
->db_parent
;
4446 uint64_t txg
= tx
->tx_txg
;
4447 zbookmark_phys_t zb
;
4452 ASSERT(dmu_tx_is_syncing(tx
));
4458 if (db
->db_state
!= DB_NOFILL
) {
4459 if (db
->db_level
> 0 || dn
->dn_type
== DMU_OT_DNODE
) {
4461 * Private object buffers are released here rather
4462 * than in dbuf_dirty() since they are only modified
4463 * in the syncing context and we don't want the
4464 * overhead of making multiple copies of the data.
4466 if (BP_IS_HOLE(db
->db_blkptr
)) {
4469 dbuf_release_bp(db
);
4471 dbuf_remap(dn
, db
, tx
);
4475 if (parent
!= dn
->dn_dbuf
) {
4476 /* Our parent is an indirect block. */
4477 /* We have a dirty parent that has been scheduled for write. */
4478 ASSERT(parent
&& parent
->db_data_pending
);
4479 /* Our parent's buffer is one level closer to the dnode. */
4480 ASSERT(db
->db_level
== parent
->db_level
-1);
4482 * We're about to modify our parent's db_data by modifying
4483 * our block pointer, so the parent must be released.
4485 ASSERT(arc_released(parent
->db_buf
));
4486 zio
= parent
->db_data_pending
->dr_zio
;
4488 /* Our parent is the dnode itself. */
4489 ASSERT((db
->db_level
== dn
->dn_phys
->dn_nlevels
-1 &&
4490 db
->db_blkid
!= DMU_SPILL_BLKID
) ||
4491 (db
->db_blkid
== DMU_SPILL_BLKID
&& db
->db_level
== 0));
4492 if (db
->db_blkid
!= DMU_SPILL_BLKID
)
4493 ASSERT3P(db
->db_blkptr
, ==,
4494 &dn
->dn_phys
->dn_blkptr
[db
->db_blkid
]);
4498 ASSERT(db
->db_level
== 0 || data
== db
->db_buf
);
4499 ASSERT3U(db
->db_blkptr
->blk_birth
, <=, txg
);
4502 SET_BOOKMARK(&zb
, os
->os_dsl_dataset
?
4503 os
->os_dsl_dataset
->ds_object
: DMU_META_OBJSET
,
4504 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
4506 if (db
->db_blkid
== DMU_SPILL_BLKID
)
4508 wp_flag
|= (db
->db_state
== DB_NOFILL
) ? WP_NOFILL
: 0;
4510 dmu_write_policy(os
, dn
, db
->db_level
, wp_flag
, &zp
);
4514 * We copy the blkptr now (rather than when we instantiate the dirty
4515 * record), because its value can change between open context and
4516 * syncing context. We do not need to hold dn_struct_rwlock to read
4517 * db_blkptr because we are in syncing context.
4519 dr
->dr_bp_copy
= *db
->db_blkptr
;
4521 if (db
->db_level
== 0 &&
4522 dr
->dt
.dl
.dr_override_state
== DR_OVERRIDDEN
) {
4524 * The BP for this block has been provided by open context
4525 * (by dmu_sync() or dmu_buf_write_embedded()).
4527 abd_t
*contents
= (data
!= NULL
) ?
4528 abd_get_from_buf(data
->b_data
, arc_buf_size(data
)) : NULL
;
4530 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
,
4531 &dr
->dr_bp_copy
, contents
, db
->db
.db_size
, db
->db
.db_size
,
4532 &zp
, dbuf_write_override_ready
, NULL
, NULL
,
4533 dbuf_write_override_done
,
4534 dr
, ZIO_PRIORITY_ASYNC_WRITE
, ZIO_FLAG_MUSTSUCCEED
, &zb
);
4535 mutex_enter(&db
->db_mtx
);
4536 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
4537 zio_write_override(dr
->dr_zio
, &dr
->dt
.dl
.dr_overridden_by
,
4538 dr
->dt
.dl
.dr_copies
, dr
->dt
.dl
.dr_nopwrite
);
4539 mutex_exit(&db
->db_mtx
);
4540 } else if (db
->db_state
== DB_NOFILL
) {
4541 ASSERT(zp
.zp_checksum
== ZIO_CHECKSUM_OFF
||
4542 zp
.zp_checksum
== ZIO_CHECKSUM_NOPARITY
);
4543 dr
->dr_zio
= zio_write(zio
, os
->os_spa
, txg
,
4544 &dr
->dr_bp_copy
, NULL
, db
->db
.db_size
, db
->db
.db_size
, &zp
,
4545 dbuf_write_nofill_ready
, NULL
, NULL
,
4546 dbuf_write_nofill_done
, db
,
4547 ZIO_PRIORITY_ASYNC_WRITE
,
4548 ZIO_FLAG_MUSTSUCCEED
| ZIO_FLAG_NODATA
, &zb
);
4550 ASSERT(arc_released(data
));
4553 * For indirect blocks, we want to setup the children
4554 * ready callback so that we can properly handle an indirect
4555 * block that only contains holes.
4557 arc_write_done_func_t
*children_ready_cb
= NULL
;
4558 if (db
->db_level
!= 0)
4559 children_ready_cb
= dbuf_write_children_ready
;
4561 dr
->dr_zio
= arc_write(zio
, os
->os_spa
, txg
,
4562 &dr
->dr_bp_copy
, data
, DBUF_IS_L2CACHEABLE(db
),
4563 &zp
, dbuf_write_ready
,
4564 children_ready_cb
, dbuf_write_physdone
,
4565 dbuf_write_done
, db
, ZIO_PRIORITY_ASYNC_WRITE
,
4566 ZIO_FLAG_MUSTSUCCEED
, &zb
);
4570 #if defined(_KERNEL)
4571 EXPORT_SYMBOL(dbuf_find
);
4572 EXPORT_SYMBOL(dbuf_is_metadata
);
4573 EXPORT_SYMBOL(dbuf_destroy
);
4574 EXPORT_SYMBOL(dbuf_loan_arcbuf
);
4575 EXPORT_SYMBOL(dbuf_whichblock
);
4576 EXPORT_SYMBOL(dbuf_read
);
4577 EXPORT_SYMBOL(dbuf_unoverride
);
4578 EXPORT_SYMBOL(dbuf_free_range
);
4579 EXPORT_SYMBOL(dbuf_new_size
);
4580 EXPORT_SYMBOL(dbuf_release_bp
);
4581 EXPORT_SYMBOL(dbuf_dirty
);
4582 EXPORT_SYMBOL(dmu_buf_set_crypt_params
);
4583 EXPORT_SYMBOL(dmu_buf_will_dirty
);
4584 EXPORT_SYMBOL(dmu_buf_is_dirty
);
4585 EXPORT_SYMBOL(dmu_buf_will_not_fill
);
4586 EXPORT_SYMBOL(dmu_buf_will_fill
);
4587 EXPORT_SYMBOL(dmu_buf_fill_done
);
4588 EXPORT_SYMBOL(dmu_buf_rele
);
4589 EXPORT_SYMBOL(dbuf_assign_arcbuf
);
4590 EXPORT_SYMBOL(dbuf_prefetch
);
4591 EXPORT_SYMBOL(dbuf_hold_impl
);
4592 EXPORT_SYMBOL(dbuf_hold
);
4593 EXPORT_SYMBOL(dbuf_hold_level
);
4594 EXPORT_SYMBOL(dbuf_create_bonus
);
4595 EXPORT_SYMBOL(dbuf_spill_set_blksz
);
4596 EXPORT_SYMBOL(dbuf_rm_spill
);
4597 EXPORT_SYMBOL(dbuf_add_ref
);
4598 EXPORT_SYMBOL(dbuf_rele
);
4599 EXPORT_SYMBOL(dbuf_rele_and_unlock
);
4600 EXPORT_SYMBOL(dbuf_refcount
);
4601 EXPORT_SYMBOL(dbuf_sync_list
);
4602 EXPORT_SYMBOL(dmu_buf_set_user
);
4603 EXPORT_SYMBOL(dmu_buf_set_user_ie
);
4604 EXPORT_SYMBOL(dmu_buf_get_user
);
4605 EXPORT_SYMBOL(dmu_buf_get_blkptr
);
4608 module_param(dbuf_cache_max_bytes
, ulong
, 0644);
4609 MODULE_PARM_DESC(dbuf_cache_max_bytes
,
4610 "Maximum size in bytes of the dbuf cache.");
4612 module_param(dbuf_cache_hiwater_pct
, uint
, 0644);
4613 MODULE_PARM_DESC(dbuf_cache_hiwater_pct
,
4614 "Percentage over dbuf_cache_max_bytes when dbufs must be evicted "
4617 module_param(dbuf_cache_lowater_pct
, uint
, 0644);
4618 MODULE_PARM_DESC(dbuf_cache_lowater_pct
,
4619 "Percentage below dbuf_cache_max_bytes when the evict thread stops "
4622 module_param(dbuf_metadata_cache_max_bytes
, ulong
, 0644);
4623 MODULE_PARM_DESC(dbuf_metadata_cache_max_bytes
,
4624 "Maximum size in bytes of the dbuf metadata cache.");
4626 module_param(dbuf_cache_shift
, int, 0644);
4627 MODULE_PARM_DESC(dbuf_cache_shift
,
4628 "Set the size of the dbuf cache to a log2 fraction of arc size.");
4630 module_param(dbuf_metadata_cache_shift
, int, 0644);
4631 MODULE_PARM_DESC(dbuf_cache_shift
,
4632 "Set the size of the dbuf metadata cache to a log2 fraction of "